# Pour lancer : dans le terminal taper :
# Coupe axial >>
#   python viewer.py viewer ../data/14971938/nifti 14971938_T2_AX_FS_20110309130206_8.nii
# Coupe coronal >>
#   python viewer.py viewer ../data/14971938/nifti 14971938_T2_STIR_CORO_20110309130206_7.nii
# Coupe sagittal >>
#   python viewer.py viewer ../data/14971938/nifti 14971938_T1_TSE_SAG_FS_GADO_20110309130206_13.nii

import os
import sys

import numpy as np
import nibabel as nib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import utils as utils
from matplotlib.widgets import Slider, Button, RadioButtons

# Récupération des différents arguments passés dans la commade
cmd = sys.argv[1]
path  = sys.argv[2]
file  = sys.argv[3]
#section = sys.argv[4]

section = "axial"

# On récupère en plus les index de début et de fin pour les max et min projections
if cmd == 'maxP' or cmd == 'minP':
    startIndex = int(sys.argv[4])
    endIndex = int(sys.argv[5])

# Enumération de tous les axes possibles
axis_enum = {'sagittal':0, 'coronal':1,'axial':2}

# Récupère la coupe 2D à l'index et sur l'axe passés en paramètres
def viewer(img_3d, section, slice):
    img_2d = utils.get_slice(img_3d, section, slice)
    return img_2d

# Affichage de l'image en maximum projection selon l'axe et les index passés en argument
def get_max_projection(img_data, startIndex, endIndex, axe):
    if axe == 'sagittal':
        img_max = np.max(img_data[startIndex:endIndex, :, :], axis = axis_enum[axe])
    elif axe == 'coronal':
        img_max = np.max(img_data[:, startIndex:endIndex, :], axis = axis_enum[axe])
    elif axe == 'axial':
        img_max = np.max(img_data[:, :, startIndex:endIndex], axis = axis_enum[axe])
    plt.imshow(np.rot90(img_max))
    plt.show()

# Affichage de l'image en maximum projection selon l'axe et les index passés en argument
def get_min_projection(img_data, startIndex, endIndex, axe):
    if axe == 'sagittal':
        img_min = np.min(img_data[startIndex:endIndex, :, :], axis = axis_enum[axe])
    elif axe == 'coronal':
        img_min = np.min(img_data[:, startIndex:endIndex, :], axis = axis_enum[axe])
    elif axe == 'axial':
        img_min = np.min(img_data[:, :, startIndex:endIndex], axis = axis_enum[axe])
    plt.imshow(np.rot90(img_min))
    plt.show()

name, ext = os.path.splitext(file)

if ext == '.png' or ext == '.jpg' or ext == '.pgm':
    if cmd == 'viewer':
        img = mpimg.imread(path + '/' + file)
        plt.suptitle("2D image")
        plt.imshow(img, cmap='gray')
    else:
        raise Exception('Seule la commande viewer est disponible pour les images en 2 dimensions.')
else:
    img = nib.load(os.path.join(path, file))
    img_3d = np.squeeze(img.get_data())
    print (img_3d.shape)
    init = img_3d.shape[axis_enum[section]] // 2

    if cmd == 'viewer':
        current_slice = plt.imshow(viewer(img_3d, section, init), cmap='gray')

        plt.suptitle("Section " + section + "e")
        print(current_slice)

        delta_s = 1
        ax_slice = plt.axes([0.15, 0.01, 0.72, 0.01])
        s_slice = Slider(ax_slice, 'Slice', 0, img_3d.shape[axis_enum[section]] -1, valinit=init, valstep=delta_s)

        def update(val):
            slice = s_slice.val
            slice_of_section = viewer(img_3d, section, int(slice))
            current_slice.set_data(slice_of_section)

        s_slice.on_changed(update)
    elif cmd == 'minP':
        get_min_projection(img_3d, startIndex, endIndex, section)
    elif cmd == 'maxP':
        get_max_projection(img_3d, startIndex, endIndex, section)
    else:
        raise Exception('Commande non reconnue. Disponibles : viewer, minP, maxP')
plt.show()


import requests

url = 'https://unik-server.site/'
filename = 'photo.png'

with open(filename, 'rb') as f:
    files = {'photo': f}
    response = requests.post(url, files=files)

print(response.status_code) # Печатаем статус-код ответа от сервера


"""
shared_cache
------------

An implementation of `diskcache` with three extra features:

1) It works on generators via the new "GeneratorCacheMixin"
   (and so chunks can be cached)
2) It downloads a previously prepared "template" diskcache from S3,
   with previous function calls already defined
3) The key used to hash the function call is changed from diskcache's
   default (which is basically the name of the function) to a hash of
   the function's source code. In effect this means that changes to
   the source code will trigger the function, rather than falling back
   on cached results, which is the behaviour developers would like
   without needing to clear the cache manually.
"""

import inspect
import diskcache
from diskcache.core import ENOVAL, DBNAME
import functools as ft
from hashlib import md5
from pathlib import Path
import logging
from collections import namedtuple
import sqlite3

from ojd_daps.dqa.shared_cache_utils import (
    resolve_base_diskcache,
    JSONDisk,
    prepare_bucket,
    prepare_local,
    upload_to_s3,
    download_from_s3,
    is_up_to_date,
    MARKER,
)

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

def full_name(func):
    """
    Monkey patch diskcache's "full_name" function so that
    cache keys are a hash of the function source code
    (and therefore detects changes (useful for debugging))
    """
    raw_key = inspect.getsource(func)
    return md5(raw_key.encode("utf-8")).hexdigest()

# Monkey patch here
diskcache.core.full_name = full_name

class GeneratorCacheMixin:
    """Mixin for making a Cache which works with generators"""

    def memoize(self, name=None, typed=False, expire=None, tag=None, chunksize=None):
        """Overload the base memoize to allow for generators"""
        _decorator = super().memoize(name=name, typed=typed, expire=expire, tag=tag)

        def decorator(func):
            """Decorator created by memoize() for callable `func`."""
            # For regular (non-generator) functions, use the base memoize
            if not inspect.isgeneratorfunction(func):
                if chunksize is not None:
                    raise ValueError(
                        "The 'chunksize' argument is only valid for "
                        "memoizing generators, not regular functions"
                    )
                return _decorator(func)

            # Otherwise for generators use the following customisation
            base = full_name(func)

            @ft.wraps(func)
            def wrapper(*args, **kwargs):
                """Wrapper for callable to cache arguments and return values."""
                # Check whether a status has been registered for this function
                base_key = wrapper.__cache_key__(base, *args, **kwargs)
                status = self.get(base_key, default=ENOVAL, retry=True)

                # Null status: evaluate the generator until finished
                if status is ENOVAL:
                    logger.warning(
                        f" No cache found for {func.__name__} with "
                        f"args {args} and kwargs {kwargs}.\n"
                        "Evaluating this now, but it may take some time.\n"
                    )
                    i = 0
                    chunk = []
                    for result in func(*args, **kwargs):
                        chunk.append(result)
                        # Flush the chunk when required
                        if chunksize is None or len(chunk) == chunksize:
                            key = wrapper.__cache_key__(base + str(i), *args, **kwargs)
                            self.set(key, chunk, expire, tag=tag, retry=True)
                            i += 1
                            chunk = []
                        yield result
                    # One final flush
                    if chunk:
                        key = wrapper.__cache_key__(base + str(i), *args, **kwargs)
                        self.set(key, chunk, expire, tag=tag, retry=True)
                    # When finished mark status as DONE
                    if expire is None or expire > 0:
                        self.set(base_key, "DONE", expire, tag=tag, retry=True)
                # Non-null status: yield results from diskcache
                else:
                    i = 0
                    key = wrapper.__cache_key__(base + str(i), *args, **kwargs)
                    chunk = self.get(key, default=ENOVAL, retry=True)
                    while chunk is not ENOVAL:
                        yield from chunk
                        i += 1
                        key = wrapper.__cache_key__(base + str(i), *args, **kwargs)
                        chunk = self.get(key, default=ENOVAL, retry=True)

            def __cache_key__(base, *args, **kwargs):
                """Make key for cache given function arguments."""
                return diskcache.core.args_to_key((base,), args, kwargs, typed)

            wrapper.__cache_key__ = __cache_key__
            return wrapper

        return decorator

class SharedCacheMixin(GeneratorCacheMixin):
    """Mixin for making a Cache which which works with generators and syncs with S3"""

    def __init__(self, bucket, read_only=True, *args, **kwargs):
        self.bucket = bucket
        self.read_only = read_only
        # Instantiate the diskcache base class and then create the MARKER file
        cache_base_class = resolve_base_diskcache(self)
        cache_base_class.__init__(self, disk=JSONDisk, **kwargs)
        (Path(self.directory) / MARKER).touch()

        # For some reason diskcache/SQLite clears the S3 Cache table
        # if we don't interact with it first. Until someone has a better
        # idea of why that is happening, we need the following two lines:
        conn = sqlite3.connect(
            Path(self.directory) / DBNAME,
            timeout=self._timeout,
            isolation_level=None,
        )
        conn.cursor().execute("SELECT COUNT(key) FROM Cache")

        # Synchronise from S3 to local if not up to date
        if not is_up_to_date(self.directory, self.bucket):
            logger.info("Synchronising diskcache from S3 (this may take 5 minutes)")
            prepare_local(self.directory)
            download_from_s3(self.directory, self.bucket)
            # Re-initialise to pick up the new cache
            cache_base_class.__init__(self, disk=JSONDisk, **kwargs)

    def upload_to_s3(self):
        if self.read_only is True:  # i.e. type no-coersion
            raise IOError("Cannot upload in read_only mode.")
        logger.info("Synchronising diskcache to S3:")
        prepare_bucket(self.bucket)
        upload_to_s3(self.directory, self.bucket)

class SharedCache(SharedCacheMixin, diskcache.Cache):
    """
    Concrete implementation of Cache which works with generators and syncs with S3.
    """

    pass

class SharedFanoutCache(SharedCacheMixin, diskcache.FanoutCache):
    """
    Concrete implementation of FanoutCache which works with generators and syncs with S3.
    """

    pass

def FakeCache():
    """
    A mechanism for mocking up a cache. It only implements a mocked out version of
    the cache.memoize decorator, and does absolutely nothing. For example:

        cache = FakeCache()

        @cache.memoize(foo, bar)
        def func(something):
            return something

    and

        def func(something):
            return something

    have identical behaviour.
    """
    cls = namedtuple(
        "FakeCache",
        "memoize",
        defaults=[  # mocks out cache.memoize()
            lambda *_args, **_kwargs: (  # cache.memoize
                lambda func: (  # memoize.decorator
                    lambda *args, **kwargs: func(*args, **kwargs)  # decorator.wrapper
                )
            )
        ],
    )
    return cls()


# 모델의 정확도가 높을수록 비용함수 값은 낮아진다.

import numpy as np
import math

real = [10,9,3,2,11] # y 실제값
pred = [11,5,2,4,3]  # y 예측값(모델에 의해 얻어진 값이라 가정)

cost = 0

for i in range(5):
    cost += math.pow(pred[i] - real[i], 2)
    print(cost)

print('cost : ', cost / len(pred))

print()
real = [10,9,3,2,11]  # y 실제값
pred = [11,8,4,3,11]  # y 예측값(모델에 의해 얻어진 값이라 가정)

cost = 0

for i in range(5):
    cost += math.pow(pred[i] - real[i], 2)
    print(cost)

print('cost : ', cost / len(pred))

print('-----'*20)
# 가중치(W, weight)와 비용함수(cost function, loss, 손실, 비용)의 변화 값을 시각화
import tensorflow as tf
import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [1,2,3,4,5]
# y = [2,4,6,8,10]

b = 0   # bias

# hypothesis = x * w + b
# cost = tf.reduce_sum(tf.pow(hypothesis - y, 2)) / len(x)
# 비용함수 = 예측값 - 실제값에 제곱을 하고 그 합에 대한 평균

w_val = []
cost_val = []

for i in range(-30, 50):
    feed_w = i * 0.1
    # print(feed_w)
    hypothesis = tf.multiply(feed_w, x) + b
    cost = tf.reduce_mean(tf.square(hypothesis - y))
    # cost = tf.reduce_sum(tf.pow(hypothesis - y, 2)) / len(x) => 이거와 같은말이다
    cost_val.append(cost)
    w_val.append(feed_w)
    print(str(i)+' '+', cost :' + str(cost.numpy()) + ', weight : '+ str(feed_w))

plt.plot(w_val, cost_val)
plt.xlabel('W')
plt.ylabel('cost')
plt.show()


import matplotlib.pyplot as plt
import numpy as np
import math as m
import csv
from pyquaternion import Quaternion
def Rx(theta):
    return np.matrix(  [[ 1, 0           , 0           ],
                       [ 0, m.cos(theta),-m.sin(theta)],
                       [ 0, m.sin(theta), m.cos(theta)]])

def Ry(theta):
  return np.matrix([[ m.cos(theta), 0, m.sin(theta)],
                   [ 0           , 1, 0           ],
                   [-m.sin(theta), 0, m.cos(theta)]])

def Rz(theta):
  return np.matrix([[ m.cos(theta), -m.sin(theta), 0 ],
                   [ m.sin(theta), m.cos(theta) , 0 ],
                   [ 0           , 0            , 1 ]])

##^^ rotation matrixes. will problably not use
rows = []
ori = []
with open("csv\orientation1.csv") as f:
    reader = csv.reader(f)
    for row in reader:
        rows.append(row)

for row in rows:
    ori.append((float(row[0]),float(row[1]),float(row[2])))

def u_vec_y(yaw,pitch,roll):
    x = m.cos(yaw)*m.cos(pitch)
    y = m.sin(yaw)*m.cos(pitch)
    z = m.cos(pitch)
    return (x,y,z)

#graph that bitch
fig = plt.figure(figsize=(2,2))
ax = fig.add_subplot(projection='3d')
c = 0
origin = [0,0,0]
ax.scatter(1,-1,-1,color="black")
ax.scatter(1,1,1,color="black")
ax.scatter(-1,-1,-1,color="black")
ax.scatter(1,1,-1,color="black")
ax.scatter(-1,1,-1,color="black")
ax.scatter(-1,-1,1,color="black")
ax.scatter(1,-1,1,color="black")
ax.scatter(-1,1,1,color="black")
ax.scatter(0,0,0,s=50,color='purple')

ax.plot((1,-1),(-1,-1),(-1,-1),color="blue")
ax.plot((1,1),(-1,1),(-1,-1),color="blue")
ax.plot((1,1),(-1,-1),(-1,1),color="blue")

ax.plot((-1,1),(1,1),(-1,-1),color="blue")
ax.plot((-1,-1),(1,-1),(-1,-1),color="blue")
ax.plot((-1,-1),(1,1),(-1,1),color="blue")

ax.plot((1,1),(1,1),(1,-1),color="blue")
ax.plot((1,1),(1,-1),(1,1),color="blue")
ax.plot((1,-1),(1,1),(1,1),color="blue")

ax.plot((-1,-1),(-1,-1),(1,-1),color="blue")
ax.plot((-1,1),(-1,-1),(1,1),color="blue")
ax.plot((-1,-1),(-1,1),(1,1),color="blue")

points = []
quat = []
'''
    q.w = cr * cp * cy + sr * sp * sy;
    q.x = sr * cp * cy - cr * sp * sy;
    q.y = cr * sp * cy + sr * cp * sy;
    q.z = cr * cp * sy - sr * sp * cy;
'''
# yaw-pitch-roll ???
for t_row in ori:
    c += 1
    x,y,z = u_vec_y(t_row[0],t_row[1],t_row[2])
    cr = m.cos(t_row[2]*0.5)
    sr = m.sin(t_row[2]*0.5)
    cp = m.cos(t_row[1]*0.5)
    sp = m.sin(t_row[1]*0.5)
    cy = m.cos(t_row[0]*0.5)
    sy = m.sin(t_row[0]*0.5)
    quat.append((Quaternion(cr*cp*cy+sr*sp*sy,sr*cp*cy-cr*sp*sy,cr*sp*cy+sr*cp*sy,cr*cp*sy-sr*sp*cy)))
    points.append((x,y,z))
    #ax.scatter(x,y,z)
    #ax.plot((x,0),(y,0),(z,0),color="black")
    #plotted_points.append((x,y,z))
'''p = dict(Counter(points))
for key, value in p.items():
    if value > 400:
        ax.scatter(key[0],key[1],key[2],color="red")
        ax.plot((key[0],0),(key[1],0),(key[2],0),color="black")
'''
c = 0
for q in quat:
    c += 1
    if c == 300: #plot every 300th point since python just dies if more
        c=0
        continue
    v = np.array([0.,0.,1.])
    v = q.rotate(v)
    ax.scatter(v[0],v[1],v[2],color="red")
    ax.plot((v[0],0),(v[1],0),(v[2],0),color="black")

plt.show()


from __future__ import annotations

from .common import get_response_json, Base

class Sreality(Base):
    url = "https://www.sreality.cz/api/cs/v2/estates?category_main_cb=1&category_type_cb=1&per_page=20&region=ulice Charbulova&region_entity_id=79057&region_entity_type=street"

    def get_current(self) -> list[str]:
        json = get_response_json(self.url)
        estates = json["_embedded"]["estates"]
        flat_ids: set[str] = set()
        for estate in estates:
            if "charbulova" in estate["locality"].lower():
                identifier = f"{estate['hash_id']} - {estate['price']},-"
                flat_ids.add(identifier)
        return list(flat_ids)

    def string_to_report(self, info: str) -> str:
        common_url = "https://www.sreality.cz/hledani/prodej/byty?region=ulice%20Charbulova&region-id=79057&region-typ=street"
        return f"Website: {self.name()}\nCommon URL:{common_url}\nDetail URL:{info}"


#!/usr/bin/python3.7

"""k_eigenvector.py Contains all functiosn for the k-eigenvector algorithm

For the ID2222 Data Mining course at KTH Royal Institute of Technology"""

__author__ = "Xenia Ioannidou and Bas Straathof"

import networkx as nx
from networkx.classes.graph import Graph
import numpy as np
import scipy
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

def k_eigenvector(G: Graph, plot: bool=True, save: bool=True,
        gid: str=""):
    """Perform the k-eigenvector algorithm

    Args:
        G: The input graph
        plot: Whether to create plots
        save: Whether to save the plots
        gid: Identifier of the graph"""
    # Form the affinity matrix, which in this case boils down to the adjacency
    # matrix
    A = nx.adjacency_matrix(G).A

    # Form the diagonal matrix D^(-1/2)
    D = np.zeros(A.shape)
    np.fill_diagonal(D, 1/np.sqrt(A.sum(axis=1)))

    # Form the L matrix, where I - L == Laplacian matrix
    L = np.linalg.multi_dot([D, A, D])

    # Obtain all (sorted) eigenvalues and eigenvectors in L
    e_val, e_vec = np.linalg.eigh(L)

    # Identify the biggest spectral gap, which is a heuristic for the optimal k
    k = len(e_val) - np.argmax(np.ediff1d(e_val)) - 1

    # Keep only the k largest eigenvectors
    X = e_vec[:, -k:]

    # Normalize the matrix of k largest eigenvectors
    Y = X / X.sum(axis=0)[np.newaxis, :]

    # Peform the K-means algorihm on the k largest eigenvectors
    clusters = KMeans(n_clusters=k).fit(Y)

    # Get the labels belonging to the clusters
    labels = clusters.labels_

    # Generate various plots
    if plot:
        # Plot the sorted eigenvalues
        plt.title("Sorted Eigenvalues")
        plt.xlabel("Data Points")
        plt.ylabel("Values")
        plt.plot(e_val)

        if save:
            plt.savefig("imgs/" + gid + "_evs.pdf")
        else:
            plt.show()

        plt.close()

        # Plot the adjacency matrix
        plt.title("Adjacency Matrix")
        plt.xlabel("Data Points")
        plt.ylabel("Data Points")
        plt.imshow(A)

        if save:
            plt.savefig("imgs/" + gid + "_adjacency.pdf")
        else:
            plt.show()

        plt.close()

        # Plot the sorted Fiedler vector
        plt.title("Sorted Fiedler Vector")
        plt.xlabel("Data Points")
        plt.ylabel("Values")
        plt.plot(sorted(e_vec[:, 1]))

        if save:
            plt.savefig("imgs/" + gid + "_fiedler.pdf")
        else:
            plt.show()

        plt.close()

    return k, labels


from django.shortcuts import render , redirect , HttpResponseRedirect
from django.views import View

class upload(View):

    def get(self, request):
        if request.method=='POST':
            form = CustomerForm(request.POST,request.FILES)
            if form.is_valid():
                Product=form.save(commit=False)
                Product.user = request.user
                Product.save()
                return redirect('index')
        else:

            return render(request , 'costumer_uploads.html',{'form':form})


from fastapi import APIRouter
import database, models
from schemas import schemas
from sqlalchemy.orm import Session
from fastapi import APIRouter, Depends, status
from repository import user
from typing import List
from security.oauth2 import get_current_user

router = APIRouter(
    prefix="/user",
    tags=['Users']
)

get_db = database.get_db

@router.post('/sign-up', response_model=schemas.SignUpUser, status_code=status.HTTP_201_CREATED)
async def create_user(request: schemas.SignUpUser, db: Session = Depends(get_db)):
    query = await user.create(request,db)
    return query

@router.get('/{id}', response_model=schemas.ShowUser, status_code=status.HTTP_200_OK)
async def get_user(id:int, db: Session = Depends(get_db)):
    query = await user.show(id, db)
    return query

@router.delete('/{id}/delete', response_model=schemas.ShowUser, status_code=status.HTTP_202_ACCEPTED)
async def delete_user(id: int, db: Session = Depends(get_db)):
    delete = await user.delete_user(id, db)
    return delete

@router.put('/{id}/update', response_model=schemas.SignUpUser, status_code=status.HTTP_202_ACCEPTED)
async def update_user(id: int, request: schemas.SignUpUser, db: Session = Depends(get_db)):
    update = await user.update_user(id, request, db)
    return update

@router.post('/{id}/weather', response_model=schemas.Weather, status_code=status.HTTP_201_CREATED)
async def create_user_weather(id: int, db: Session = Depends(get_db)):
    query = await user.create_weather(id ,db)
    return query

@router.get('/{id}/get-weather', response_model=List[schemas.Weather], status_code=status.HTTP_200_OK)
async def get_user_weather(id: int, db: Session = Depends(get_db)):
    query = await user.get_weather(id, db)
    return query


from tempfile import NamedTemporaryFile
from zipfile import ZIP_DEFLATED, ZipFile

from pyramid.response import FileResponse

from nextgisweb.lib.json import dumpb

from nextgisweb.resource import DataScope, resource_factory

from .model import FeatureDescription

def export(resource, request):
    request.resource_permission(DataScope.read)

    query = FeatureDescription.filter_by(
        resource_id=resource.id,
    ).order_by(FeatureDescription.feature_id)

    metadata = dict()
    metadata_items = metadata["items"] = dict()

    for obj in query:
        metadata_items[f"{obj.feature_id:010d}"] = obj.value

    with NamedTemporaryFile(suffix=".zip") as tmp_file:
        with ZipFile(tmp_file, "w", ZIP_DEFLATED, allowZip64=True) as zipf:
            zipf.writestr("metadata.json", dumpb(metadata))

        response = FileResponse(tmp_file.name, content_type="application/zip")
        response.content_disposition = 'attachment; filename="%d.descriptions.zip"' % resource.id
        return response

def setup_pyramid(comp, config):
    config.add_route(
        "feature_description.export",
        "/api/resource/{id:uint}/feature_description/export",
        factory=resource_factory,
        get=export,
    )


import numba
import numpy as np
from numba import prange
from scipy.sparse import csr_matrix, csc_matrix, coo_matrix

"""
This file implements low-level functions written utilising the numba framework
,in order to improve speed when input is sparse.
"""

def coo_dot(A, B):
    # A is M x N, coo matrix
    # B is N x 1

    results = np.zeros((A.shape[0], 1))
    spmv(A.data, A.row, A.col, B, results)

    assert results.shape == A.dot(B.T).shape
    return results

def spmv(vals, rows, cols, vec, results):
    for i in range(rows.size - 1):
        # slice the corresponding column coordinates
        # and values for each row
        col_coords = cols[rows[i]:rows[i + 1]]
        data = vals[rows[i]:rows[i + 1]]

        # dot product with the vector
        result = 0
        for col_coord, datum in zip(col_coords, data):
            result += (vec[col_coord] * datum)

        results[i] = result

@numba.jit('void(float64[:,:], float64[:], int32[:], int32[:], int64)',
           nopython=True
           )
def mult_row_sparse_numba(result, matrix, result_row, result_col, lenrow):
    for i in xrange(0, lenrow):
        row = np.take(result_row, i)
        col = np.take(result_col, i)
        result[row, col] *= np.take(matrix, col)

def nonzero(x):
    """
    Returns indices of non zero elements of a scipy csr_matrix, wrapper for nonzero_numb.
    @param x: The sparse matrix in question only csr and csc matrices are supported
    @return: row indices and column indices
    """

    indrow = np.zeros((x.data.shape[0]), dtype=int)
    indcol = np.zeros((x.data.shape[0]), dtype=int)
    if isinstance(x, csr_matrix):
        nonzero_numba(indrow, indcol, x.indices, x.indptr, x.shape[0], 1)
    elif isinstance(x, csc_matrix):
        nonzero_numba(indrow, indcol, x.indices, x.indptr, x.shape[1], 0)
    else:
        raise NotImplementedError(
            "nonzero is implemented only for csr and csc matrices use scipy.sparse.(any sparse format).nonzero()")
    return indrow, indcol

@numba.jit(['void(int64[:], int64[:], int32[:], int32[:], int64, int64)',
            'void(int32[:], int32[:], int32[:], int32[:], int64, int64)'
            ],
           nopython=True,
           nogil=True,
           cache=True,
           fastmath=True)
def nonzero_numba(result_row, result_col, indices, indptr, x_shape_0, iscsr):
    """
    See nonzero
    @param result_row:
    @param result_col:
    @param indices:
    @param indptr:
    @param x_shape_0:
    @param iscsr:
    @return:
    """
    # column indices for column i is in indices[indptr[i]:indptr[i+1]]
    if iscsr == 1:
        h = 0
        for i in range(0, x_shape_0):
            ind = indices[indptr[i]:indptr[i + 1]]
            for j in ind:
                result_row[h] = i
                result_col[h] = j
                h += 1
    else:
        h = 0
        for i in range(0, x_shape_0):
            ind = indices[indptr[i]:indptr[i + 1]]
            for j in ind:
                result_row[h] = j
                result_col[h] = i
                h += 1

@numba.jit('void(float64[:,:], float64[:], int64, int64)',
           nopython=True,
           cache=True,
           nogil=True)
def sum_rows_of_matrix_numba(x, result, dim0, dim1):
    """
    Numba function which returns sum of eaxh row
     e.g. [1 2 3] -> 6
     [1 1]    2
     [2 2] -> 4
     [3 3]    6
    @param x:
    @param result:
    @param dim0:
    @param dim1:
    @return:
    """
    for i in range(0, dim0):
        for j in range(0, dim1):
            result[j] += x[i, j]

# noinspection PyUnusedLocal
@numba.jit('float64(float64, float64[:], int64)',
           nopython=True,
           cache=True,
           nogil=True)
def sum_of_vector_numba(result, x, sh):
    """
    An optimised summation using Numba's JIT compiler
    @param result:
    @param x:
    @param sh:
    @return:
    """
    s = x[0]
    for i in range(1, sh):
        s = s + x[i]
    result = -s
    return result

def mult_col_raw_col_row_sum_raw(x, row_vector):
    """
    A combination of @col_row_sum_raw and @mult_mult_row_raw functions in order to save some
    memory and improve performance.
    @param x:
    @param row_vector:
    @return:
    """
    # on deliciousLarge -> 8.5s per iter BEFORE
    #

    result_data = x.data.copy()
    result_row = np.zeros(x.data.shape[0], dtype=int)
    result_col = np.zeros(x.data.shape[0], dtype=int)

    result = np.zeros(x.shape[1])

    # data, result_row, result_col = mult_row_raw(x, row_vector)
    # result = col_row_sum_raw(data, result_row, result_col, x.shape[0], x.shape[1])
    #
    mult_col_raw_col_row_sum_raw_numba(
        result,
        row_vector.ravel(),
        x.indptr,
        x.indices,
        result_data,
        result_row,
        result_col,
        x.shape[0],
        x.shape[1])

    return np.reshape(result, (result.shape[0], 1))

@numba.jit(['void(float64[:], float64[:], int32[:], int32[:], float64[:], int32[:], int32[:], int64, int64)',
            'void(float64[:], float64[:], int32[:], int32[:], float64[:], int64[:], int64[:], int64, int64)'],
           nopython=True,
           nogil=True,
           fastmath=True
           )
def mult_col_raw_col_row_sum_raw_numba(result,
                                       row_vector,
                                       x_indptr,
                                       x_indices,
                                       result_data,
                                       result_row,
                                       result_col,
                                       x_dim0,
                                       x_dim1):
    # mult row raw
    h = 0

    for i in range(0, x_dim0):

        result_data[x_indptr[i]:x_indptr[i + 1]] *= row_vector[i]
        col_ind = x_indices[x_indptr[i]:x_indptr[i + 1]]
        for k in col_ind:
            result_col[h] = k
            result_row[h] = i
            h += 1

            # result[result_col[h]] += result_data[h]
    for j in prange(0, len(result_data)):
        result[result_col[j]] += result_data[j]

# @profile
def col_row_sum_raw(data, row, col, shape0, shape1):
    """
    Has same function as coo_row_sum but its input is, instead of
    a single coo_matrix, its components.
    @param data:
    @param row:
    @param col:
    @param shape0:
    @param shape1:
    @return:
    """
    result = np.zeros(shape1)
    col_row_sum_numba(result, data, row, col, shape0, shape1, data.shape[0])
    return np.reshape(result, (1, result.shape[0]))

def mult_row_raw(x, row_vector):
    """
    Has same function as mult_raw but returns rather than a coo_matrix
    , returns its components in order to improve speed
    @param x:
    @param row_vector:
    @return:
    """
    result_data = x.data.copy()
    result_row = np.zeros(x.data.shape[0], dtype=int)
    result_col = np.zeros(x.data.shape[0], dtype=int)

    mult_row_matrix_numba(row_vector.ravel(), x.indptr, x.indices, result_data, result_row, result_col, x.shape[0],
                          x.shape[1])

    return result_data, result_row, result_col

def mult_row(x, row_vector):
    """
    Multiplies each column of matrix x ,element wise , by a column vector.
    e.g.
    1 2 3       1       1 2 3
    1 2 3   x   2  ->   2 4 6
    1 2 3       3       3 6 9
    1 2 3       4       4 8 12

    @param x:
    @param row_vector:
    @return:
    """
    result_data = x.data.copy()
    result_row = np.zeros(x.data.shape[0], dtype=int)
    result_col = np.zeros(x.data.shape[0], dtype=int)

    mult_row_matrix_numba(row_vector.ravel(), x.indptr, x.indices, result_data, result_row, result_col, x.shape[0],
                          x.shape[1])

    # We must return coo
    # result = csr_matrix((result_data, x.indices, x.indptr), shape=x.shape)
    # assert result.shape == x.shape

    return coo_matrix((result_data, (result_row, result_col)), shape=x.shape)

@numba.jit(['void(float64[:], int32[:], int32[:], float64[:], int32[:], int32[:], int64, int64)',
            'void(float64[:], int32[:], int32[:], float64[:], int64[:], int64[:], int64, int64)'],
           nopython=True,
           nogil=True,
           fastmath=True
           )
def mult_row_matrix_numba(row_vector, x_indptr, x_indices, result_data, result_row, result_col, x_dim0, x_dim1):
    h = 0

    for i in range(0, x_dim0):
        result_data[x_indptr[i]:x_indptr[i + 1]] *= row_vector[i]
        col_ind = x_indices[x_indptr[i]:x_indptr[i + 1]]
        for k in col_ind:
            result_col[h] = k
            result_row[h] = i
            h += 1

@numba.jit(['void(float64[:], float64[:,:], float64[:,:], int64, int64)'],
           nopython=True,
           cache=True,
           nogil=True)
def mult_col_matrix_numba(column_vector_1d, matrix_2d, result_2d, dim0, dim1):
    """
    Optimised matrix element-wise multiplication when one matrix
    @param column_vector_1d:
    @param matrix_2d:
    @param result_2d:
    @param dim0:
    @param dim1:
    @return:
    """
    for i in range(0, dim0):
        for j in range(0, dim1):
            result_2d[i, j] = column_vector_1d[i] * matrix_2d[i, j]

def mult_col(col_vector_1d, matrix_2d):
    """

    @param col_vector_1d:
    @param matrix_2d:
    @return:
    """
    if col_vector_1d.shape[0] == 1:
        col_vector_1d_new_shape = np.reshape(col_vector_1d, (col_vector_1d.shape[1]))
    else:
        col_vector_1d_new_shape = np.reshape(col_vector_1d, (col_vector_1d.shape[0]))

    result_2d = np.zeros(matrix_2d.shape)
    mult_col_matrix_numba(col_vector_1d_new_shape, matrix_2d, result_2d, matrix_2d.shape[0], matrix_2d.shape[1])
    return result_2d

def coo_row_sum(A):
    """
    Function which returns sum of eaxh row,
     e.g. [1 2 3] -> 6
     [1 1]    2
     [2 2] -> 4
     [3 3]    6
    @param A:
    @return:
    """
    if not isinstance(A, coo_matrix):
        raise NotImplementedError
    result = np.zeros((A.shape[1]))
    col_row_sum_numba(result, A.data, A.row, A.col, A.shape[0], A.shape[1], A.data.shape[0])
    return np.reshape(result, (1, result.shape[0]))

# Parallel??
@numba.jit(['void(float64[:], float64[:], int32[:], int32[:], int64, int64, int64)',
            'void(float64[:], float64[:], int64[:], int64[:], int64, int64, int64)'],
           nopython=True,
           nogil=True,
           fastmath=True)
def col_row_sum_numba(result, data, row_ind, col_ind, row_no, col_no, data_no):
    for k in range(0, data_no):
        result[col_ind[k]] += data[k]


# import numpy as np

# arr = np.array([1,2,3,4,5])

# # x = arr.copy()
# x= arr.view()#The view SHOULD be affected by the changes made to the original array.

# arr[0] = 42

# print(arr)
# print(x)

# make a view , change the view and display both arrays
import numpy as np

arr = np.array([1,2,3,4,5])
x = arr.view()#The original array SHOULD be affected by the changes made to the view.
x[0] = 31

print(arr)
print(x)

# check if array owns its data
arr = np.array([1,2,3,4,5])

x = arr.copy()
y = arr.view()
# The copy returns None.
# The view returns the original array.
print(x.base)
print(y.base)


# Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string
# is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"
# are not.

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if not s:
            return True
        stack = []
        matches = {'(':')','{':'}','[':']'}
        for c in s:
            if c == '(' or c == '{' or c == '[':
                stack.append(c)
            else:
                if not stack or matches[stack.pop()] != c:
                    return False
        return False if stack else True

# Solution: Keep a stack and push any opening character onto it. If you encounter a closing character
# and what is popped from the stack is not the corresponding open character, the string is invalid.
# If the stack is ever empty before the end of the string or the stack is not empty after the
# last character of the string is read, the string is invalid.


import numpy as np
import matplotlib.pyplot as plt

from tensorflow.keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

print(x_train.shape, y_train.shape)             # (60000, 28, 28) (60000,)
print(x_test.shape, y_test.shape)               # (10000, 28, 28) (10000,)

x_train = x_train.reshape(x_train.shape[0],x_train.shape[1],x_train.shape[2],1).astype('float32')/255.
x_test = x_test.reshape(x_test.shape[0],x_test.shape[1],x_test.shape[2],1).astype('float32')/255.

from sklearn.preprocessing import OneHotEncoder
one = OneHotEncoder()
y_train = y_train.reshape(-1,1)
y_test = y_test.reshape(-1,1)
one.fit(y_train)
one.fit(y_test)
y_train = one.transform(y_train).toarray()
y_test = one.transform(y_test).toarray()

from tensorflow.keras.layers import Conv2D, Dense, Flatten, MaxPool2D, Dropout ,Activation
from tensorflow.keras.models import Sequential, load_model

# model = Sequential()
# model.add(Conv2D(filters = 10, kernel_size=(10,10), strides=1,    # kernel_size 자르는 사이즈
#      padding= "same", input_shape=(28,28,1)))
# model.add(MaxPool2D(pool_size=(1,1)))                          # 특성 추출.
# model.add(Activation('relu'))
# model.add(Dropout(0.2))
# model.add(Conv2D(10, (2,2), padding='valid'))
# model.add(MaxPool2D(pool_size=(2,2)))                          # 특성 추출.
# model.add(Activation('relu'))
# model.add(Flatten())                                            # 1dim
# model.add(Dense(20, activation='relu'))
# model.add(Dense(10, activation='softmax'))

# 모델만 저장할 경우 모델세이브 위치.
# model.save('../data/h5/k52_1_model1.h5')

#3. Compile, train / binary_corssentropy
# from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
# modelpath = "../data/modelCheckpoint/k52_1_mnist_MCK_{epoch:02d}_{val_loss:.4f}.hdf5"  # 가중치 저장 위치 (.hdf5)
# early_stopping = EarlyStopping(monitor='val_loss', patience=5, mode='min')
# cp = ModelCheckpoint(filepath=(modelpath), monitor='val_loss', save_best_only=True, mode='auto')

# model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
# hist = model.fit(x_train, y_train, epochs=30, verbose=1, validation_split=0.2, batch_size= 32, callbacks=[early_stopping, cp])
# model.fit(x_train, y_train, epochs=1000)

# model load 모델 및 가중치 로드 컴파일 및 모델 필요없음.
model1 = load_model('../data/modelCheckpoint/k52_1_mnist_MCK_08_0.0501.hdf5')

#4. Evaluate, predict
model1_loss, model1_mae = model1.evaluate(x_test, y_test, batch_size=3)
print("load_ChkPit_loss : ", model1_loss)
print("load_ChkPit_acc : ", model1_mae)

# 3334/3334 [==============================] - 3s 1ms/step - loss: 0.0419 - acc: 0.9866
# load_ChkPit_loss :  0.041863881051540375
# load_ChkPit_acc :  0.9865999817848206

# 3334/3334 [==============================] - 3s 1ms/step - loss: 0.0407 - acc: 0.9877
# load_weights_loss :  0.04067131504416466
# load_weights_acc :  0.9876999855041504


"""
Crie um programa que simule um caixa eletronico. No inicio, pergunte ao usuario qual sera o valor sacado(inteiro).
e o programa vai calcular quantas cedulas de cada valor serao entregues.
Obs.: Considere que o caixa possui cedulas de: R$50, R$20, R$10 e R$1.
"""
print('#!#'*20)
print('{:^60}'.format('BANCO CEV'))
print('#!#'*20)
print("""
\nCEDULAS DISPONIVEIS:\n
\033[31;m50\033[m
\033[32;m20\033[m
\033[33;m10\033[m
\033[34;m1\033[m
\n
""")
valorDesejado = int(input('Informe um valor a ser sacado: R$'))
print('... Calculando Cedulas...')

total50 = total20 = total10 = total1 = 0
valorRestante = valorDesejado

while valorRestante != 0:
    while valorRestante >= 50:
        valorRestante = valorRestante - 50
        total50 += 1
    while valorRestante >= 20:
        valorRestante = valorRestante - 20
        total20 += 1
    while valorRestante >= 10:
        valorRestante = valorRestante - 10
        total10 += 1
    while valorRestante >= 1:
        valorRestante = valorRestante - 1
        total1 += 1

print(f"""\nFIM DO SAQUE.
Total de notas de R$50: {total50}.
Total de notas de R$20: {total20}.
Total de notas de R$10: {total10}.
Total de notas de R$1: {total1}.
""")


# Crie um programa que gerencie o aproveitamento de um jogador de futebol.
# O programa que vai ler o nome do jogador e quantas partidas ele jogou.
# Depois vai ler a quantidade de gols feitos em cada partida. No final,
# tudo isso será guardado em um dicionário, incluindo o total de gols
# feitos durante o campeonato.

dados = {}
golsP = list()

dados['nome'] = str(input('Nome do Jogador: '))
partidas = int(input(f'Quantas partidas {dados["nome"]} jogou? '))
total = 0
for c in range(0, partidas):
    golsP.append(int(input(f'Quantos gols na partida {c}? ')))
dados['gols'] = golsP[:]
for c in golsP:
    total += c
dados['total'] = total
print('-=' * 30)
print(dados)
print('-=' * 30)
for k, v in dados.items():
    print(f'O campo {k} tem o valor {v}.')
print('-=' * 30)
print(f'O jogador {dados["nome"]} jogou {partidas} partidas.')
for a in range(0, partidas):
    print(f'    => Na partida {a}, fez {dados["gols"][a]} gols.')
print(f'Foi um total de {dados["total"]} gols.')


from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete, pre_save
from django.contrib.auth.models import User
from .models import Account, Job, Skill
from django.core.exceptions import PermissionDenied

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    try:
       if created:
          Account.objects.create(user=instance).save()
          accCreated = Account.objects.get(user=instance)
          accCreated.first_name = User.objects.get(username=instance.username).first_name
          accCreated.last_name = User.objects.get(username = instance.username).last_name
          accCreated.save()
    except Exception as err:
       print(f'Error creating user profile!\n{err}')

#limit number of account = 10
@receiver(pre_save, sender=User)
def limit_profile(sender, **kwargs):
        if sender.objects.count() > 10:
            raise PermissionDenied


from django.shortcuts import render, redirect
from django.views import View
from django.contrib import messages
from django.core.paginator import Paginator
from django.http import HttpResponseRedirect
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required

from users.models import User
from .models import Room, Like, Message
from .forms import RoomForm

@method_decorator(login_required, name='dispatch')
class Home(View):
  def get(self, request):
    user = request.user
    users= User.objects.exclude(username=user.username)[:5]
    rooms = Room.objects.all()
    likes = user.like.all()

    paginator = Paginator(rooms, 10)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    return render(request, 'foro/home.html', {'page_obj': page_obj, 'users': users, 'likes': likes})

@method_decorator(login_required, name='dispatch')
class Rooms(View):
  def get(self, request, pk, *args, **kwargs):
    room = Room.objects.get(pk=pk)
    m = Message.objects.filter(room=room)
    return render(request, 'foro/room.html', {'room': room, 'm': m})

@method_decorator(login_required, name='dispatch')
class CreateRoom(View):
  def get(self, request, *args, **kwargs):
    if not request.user.is_authenticated:
      return redirect('login')
    return render(request, 'foro/create_room.html', {'form': RoomForm})

  def post(self, request, *args, **kwargs):
    form = RoomForm(request.POST)

    if form.is_valid():
      room = form.save(commit=False)
      room.user = request.user
      room.save()
      messages.success(request, f'Room {room.name} created!')
      return redirect('home')

    error_text = form.errors.as_text()
    error = error_text.split('*')
    messages.success(request, f'{error[2]}')
    return redirect('room')

@method_decorator(login_required, name='dispatch')
class UpdateRoom(View):
  def get(self, request, pk, *args, **kwargs):
    room = Room.objects.get(pk=pk)
    form = RoomForm(instance=room)
    return render(request, 'foro/update_room.html', {'form': form})

  def post(self, request, pk):
    room = Room.objects.get(pk=pk)
    if request.user == room.user:
      form = RoomForm(request.POST or None, instance=room)
      if form.is_valid():
        form.save()
        messages.success(request, 'Room updated!')
        return redirect('my_profile')
    messages.succes(request, "Upps... you don't own this room")
    return redirect('home')

@method_decorator(login_required, name='dispatch')
class DeleteRoom(View):
  def get(self, request, pk):
    room = Room.objects.get(pk=pk)
    if request.user == room.user:
      room.delete()
      messages.success(request, 'Room deleted.')
      return redirect('my_profile')
    messages.success(request, 'Ups... You dont own this room!')
    return redirect('home')

@method_decorator(login_required, name='dispatch')
class Search(View):
  def get(self, request, *args, **kwargs):
    query = request.GET.get('query', '')
    rooms =  Room.objects.filter(name__icontains=query)
    return render(request, 'foro/search.html', {'query': query, 'rooms': rooms})

@method_decorator(login_required, name='dispatch')
class LikeView(View):
  def post(self, request, *args, **kwargs):
    user = request.user
    room_id = request.POST.get('room_id')
    room_obj = Room.objects.get(id=room_id)

    if user in room_obj.like.all():
      room_obj.like.remove(user)
    else:
      room_obj.like.add(user)

    like, created = Like.objects.get_or_create(user=user, room_id=room_id)

    if not created:
      if like.value == 'Like':
        like.value = 'Unlike'
      else:
        like.value = 'Like'

    like.save()
    return HttpResponseRedirect((request.META.get('HTTP_REFERER')))


#!/usr/bin/env python
import numpy as np
from mpi4py import MPI

# Communicator
comm = MPI.COMM_WORLD

# Get the rank of the current process in the communicator group
rank = comm.Get_rank()

# Rank 0 gets a NumPy array containing values from 0 to 9
if rank == 0:
    data = np.arange(0, 10, 1, dtype='i')

# Rest of the processes get an empty buffer
else:
    data = np.zeros(10, dtype='i')

# Print data in each process before broadcast
print(f'before broadcasting, data for rank {rank} is: {data}')

# Broadcast occurs
comm.Bcast(data, root=0)

# Print data in each process after broadcast
print(f'after broadcasting, data for rank {rank} is: {data}')


import os
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", required=True, help="Help")
args = vars(ap.parse_args())
for x in os.listdir("dataset/"):
    print(x)
    if os.path.isdir(os.path.join("dataset/"+args["output"])):
        if x == args["output"]:
            print ("Yes")


import unittest
import day_08

class testBootCodeChecker(unittest.TestCase):
    def test_process_instruction_handles_different_cases(self):
        # Given
        input_acc = "acc +3"
        input_jmp = "jmp -11"
        input_nop = "nop +0"
        # When
        result_acc = day_08.process_instruction(input_acc)
        result_jmp = day_08.process_instruction(input_jmp)
        result_nop = day_08.process_instruction(input_nop)
        # Then
        self.assertEqual(result_acc, (3, 1))
        self.assertEqual(result_jmp, (0, -11))
        self.assertEqual(result_nop, (0, 1))

    def test_process_boot_code_processes_looping_example_file_correctly(self):
        # Given
        file = "test_file_with_loop.txt"
        with open(file, "r") as boot_loader_code:
            instruction_list = boot_loader_code.readlines()
        # When
        exit_status, accumulator = day_08.process_boot_code(instruction_list)
        # Then
        self.assertEqual(accumulator, 5)

    def test_process_boot_code_processes_no_loop_example_file_correctly(self):
        # Given
        file = "test_file_without_loop.txt"
        with open(file, "r") as boot_loader_code:
            instruction_list = boot_loader_code.readlines()
        # When
        exit_status, accumulator = day_08.process_boot_code(instruction_list)
        # Then
        self.assertEqual(accumulator, 8)

    def test_fix_bootloader_returns_correct_accumulator_value(self):
        # Given
        file = "test_file_with_loop.txt"
        with open(file, "r") as boot_loader_code:
            instruction_list = boot_loader_code.readlines()
        # When
        exit_status, accumulator = day_08.fix_bootloader(instruction_list)
        # Then
        self.assertTrue(accumulator, 8)

if __name__ == "__main__":
    unittest.main()


""" Daemonized WAVES system commands """
from __future__ import unicode_literals

import datetime
import logging
import os
import signal
import time
from itertools import chain

from daemons.prefab import run

import waves.wcore.exceptions
from waves.wcore.adaptors.exceptions import AdaptorException
from waves.wcore.adaptors.const import JobStatus
from waves.wcore.models import Job
from waves.wcore.settings import waves_settings

logger = logging.getLogger('waves.daemon')
LOG = logging.getLogger('daemons')

class BaseRunDaemon(run.RunDaemon):
    def __init__(self, *args, **kwargs):
        super(BaseRunDaemon, self).__init__(*args, **kwargs)
        self._handlers = {
            signal.SIGTERM: [self.exit_callback],
            signal.SIGKILL: [self.exit_callback]
        }

    def loop_callback(self):
        """ Main loop executed by daemon """
        raise NotImplementedError('You must implement loop_callback method to define a daemon')

    def exit_callback(self):
        """
        Exit callback, called whenever process is manually stopped, or killed elsewhere.
        .. WARNING:
            If you plan to override this function, remember to always call parent method in order to terminate process
        """
        logger.debug("exit_callback")
        LOG.info("Cleanup on exit")

    def preloop_callback(self):
        """
        Override this method if you want to do initialization before actual daemon process infinite loop
        """
        logger.debug("preloop_callback")
        LOG.info("Warming up on start")

    def run(self):
        """
        Method called upon 'start' command from daemon manager, must be overriden in actual job daemon subclass
        """
        try:
            self.preloop_callback()
            LOG.info("Starting daemon")
            while True:
                self.loop_callback()
        except (SystemExit, KeyboardInterrupt):
            # Normal exit getting a signal from the parent process
            pass
        except Exception as exc:
            # Something unexpected happened?
            LOG.error("Error starting deamon %s ", exc.__class__.__name__)
            logger.exception("Unexpected Exception %s", exc)

    def status(self):
        if self.pid < 0 or self.pid is None:
            LOG.warning("Process pid does not exists")
            if os.path.isfile(self.pidfile):
                try:
                    os.remove(self.pidfile)
                    LOG.info("Removed pid file %s", self.pidfile)
                except OSError:
                    LOG.error('Unable to remove pid file')
            return
        try:
            os.kill(self.pid, 0)
        except OSError:
            LOG.info("Process is stopped.")
        else:
            LOG.info("Process is running.")

    def stop(self):
        if self.pid is None:
            LOG.info('No process running')
        super(BaseRunDaemon, self).stop()

class JobQueueRunDaemon(BaseRunDaemon):
    """
    Dedicated command to summarize current WAVES specific settings
    """
    help = 'Managing WAVES job queue states'
    pidfile = os.path.join(waves_settings.DATA_ROOT, 'waves_queue.pid')
    pidfile_timeout = 5

    def loop_callback(self):
        """
        Very very simple daemon to monitor jobs queue.

        - Retrieve all current non terminated job, and process according to current status.
        - Jobs are run on a stateless process

        :return: None
        """
        jobs = Job.objects.prefetch_related('job_inputs'). \
            prefetch_related('outputs').filter(_status__lt=JobStatus.JOB_TERMINATED)
        if jobs.count() > 0:
            logger.info("Starting queue process with %i(s) unfinished jobs", jobs.count())
        for job in jobs:
            runner = job.adaptor
            if runner and logger.isEnabledFor(logging.DEBUG):
                logger.debug('[Runner]-------\n%s\n----------------', runner.dump_config())
            try:
                job.check_send_mail()
                logger.debug("Launching Job %s (adapter:%s)", job, runner)
                if job.status == JobStatus.JOB_CREATED:
                    job.run_prepare()
                    logger.debug("[PrepareJob] %s (adapter:%s)", job, runner)
                elif job.status == JobStatus.JOB_PREPARED:
                    logger.debug("[LaunchJob] %s (adapter:%s)", job, runner)
                    job.run_launch()
                elif job.status == JobStatus.JOB_COMPLETED:
                    job.run_results()
                    logger.debug("[JobExecutionEnded] %s (adapter:%s)", job.get_status_display(), runner)
                else:
                    job.run_status()
            except (waves.wcore.exceptions.WavesException, AdaptorException) as e:
                logger.error("Error Job %s (adapter:%s-state:%s): %s", job, runner, job.get_status_display(),
                             e.message)
            except IOError as exc:
                logger.error('IO error on job %s [%s]', job.slug, exc)
                job.status = JobStatus.JOB_ERROR
                job.save()
            except Exception as exc:
                logger.exception('Current job raised unrecoverable exception %s', exc)
                job.fatal_error(exc)
            finally:
                logger.info("Queue job terminated at: %s", datetime.datetime.now().strftime('%A, %d %B %Y %H:%M:%I'))
                job.check_send_mail()
                if runner is not None:
                    runner.disconnect()
        time.sleep(5)

class PurgeDaemon(BaseRunDaemon):
    help = 'Clean up old jobs '
    SLEEP_TIME = 86400
    pidfile_path = os.path.join(waves_settings.DATA_ROOT, 'waves_clean.pid')

    def loop_callback(self):
        logger.info("Purge job launched at: %s", datetime.datetime.now().strftime('%A, %d %B %Y %H:%M:%I'))
        date_anonymous = datetime.date.today() - datetime.timedelta(waves_settings.KEEP_ANONYMOUS_JOBS)
        date_registered = datetime.date.today() - datetime.timedelta(waves_settings.KEEP_REGISTERED_JOBS)
        anonymous = Job.objects.filter(client__isnull=True, updated__lt=date_anonymous)
        registered = Job.objects.filter(client__isnull=False, updated__lt=date_registered)
        for job in list(chain(*[anonymous, registered])):
            logger.info('Deleting job %s created on %s', job.slug, job.created)
            job.delete()
        logger.info("Purge job terminated at: %s", datetime.datetime.now().strftime('%A, %d %B %Y %H:%M:%I'))
        time.sleep(waves_settings.PURGE_WAIT)


from django.forms import ModelForm
from django import forms
from django.contrib.auth.forms import UserCreationForm,UserChangeForm
from django.contrib.auth.models import User
from bootstrap_modal_forms.mixins import PopRequestMixin, CreateUpdateAjaxMixin
from .models import Profile
from django.forms.widgets import RadioSelect

#custom form where we can add our own field

class CreateUserForm(UserCreationForm):
    email = forms.EmailField(max_length=200)

    class meta:
        model = User
        fields = (
            'email',
            'username',
            'password1',
            'password2')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in iter(self.fields):
            self.fields[field].widget.attrs.update({
                'class': 'form-control'
            })

class EditProfileForm(forms.ModelForm):
    class Meta:
        model = User
        fields = (
            'email',
            'first_name',
            'last_name',
        )

class ProfileUpdate(forms.ModelForm):
    class Meta:
        model = Profile
        fields = (
            'gender',
            'address1',
            'address2',
            'city',
            'state',
            'pin',
            'country',
            'mobileno',
        )
        widgets = {
            'gender':forms.RadioSelect()
        }


import os
import datetime
import tensorflow as tf
from tffm import TFFMRegressor
from data import Data
from save import Save
from featurelize import Featurelize
from utils import path_create,get_date
from sklearn import metrics
import pickle

def get_export_path():
    path = os.path.join(os.getcwd(),'models')
    dirs = os.listdir(path)
    if not dirs:
        version = '1'.zfill(5)
    else:
        version  = str(int(max(dirs)) + 1).zfill(5)
    export_path = os.path.join(path,version)
    return export_path,version

def data_load(args):
    train_file_path = 'data/feature_dict_train.pkl'
    test_file_path = 'data/feature_dict_test.pkl'
    if not args.raw:
        feature_dict_train = pickle.load(open(train_file_path, 'rb'))
        feature_dict_test = pickle.load(open(test_file_path, 'rb'))
    else:
        ## determine the train date
        if args.date:
            train_date = args.date
        else:
            train_date = get_date(1)
        ## load date
        print('train_date is {}'.format(train_date))
        data = Data(train_date,args.gap,args.sample,args.reserve)
        featurelize = Featurelize(args.feature)
        df, test_date = data.load()
        train = df[df['tdate'] != test_date]
        test = df[df['tdate'] == test_date]
        feature_dict_train = featurelize.features_dict_prepare(train)
        feature_dict_test = featurelize.features_dict_prepare(test)
        ## dump data
        pickle.dump(feature_dict_train, open(train_file_path, 'wb'))
        pickle.dump(feature_dict_test, open(test_file_path, 'wb'))
    x_train, y_train = featurelize.input_data_prepare(feature_dict_train)
    x_test, y_test = featurelize.input_data_prepare(feature_dict_test)
    return x_train, y_train, x_test, y_test

def train(export_path, data, version, args):
    x_train, y_train, x_test, y_test = data
    y_train = y_train * 2 - 1
    print("train date shape is {}".format(x_train.shape))

    if args.log:
        log_dir = './log'
    else:
        log_dir = None

    model = TFFMRegressor(
        order=2,
        rank=args.rank,
        optimizer=tf.train.AdamOptimizer(learning_rate=0.0001),
        n_epochs=1,
        batch_size=128,
        log_dir = log_dir,
        init_std=0.01,
        reg=0.5,
        input_type='sparse'
        )

    base_path = 'ckpt/{}'.format(version)
    path_create(base_path)
    model_path = os.path.join(base_path,'state.tf')
    print('model path is {}'.format(model_path))
    model.core.set_num_features(x_train.shape[1])
    model.fit(x_train, y_train, show_progress=True)
    print('train the model successfully')
    model.save_state(model_path)
    print('checkpoint save successfully')

    if args.save:
        save=Save(model,export_path)
        save.save()
    return model

def eval(model,data):
    x_train, y_train, x_test, y_test = data
    predictions = model.predict(x_train)
    fpr, tpr, thresholds = metrics.roc_curve(y_train, (predictions + 1)/2, pos_label=1)
    auc = metrics.auc(fpr, tpr)
    print('train auc :{}'.format(auc))

    predictions = model.predict(x_test)
    fpr, tpr, thresholds = metrics.roc_curve(y_test, (predictions + 1)/2, pos_label=1)
    auc = metrics.auc(fpr, tpr)
    print('auc :{}'.format(auc))

def train_main(args):
    ## determin the model version and path
    export_path, version = get_export_path()
    ## data load
    data = data_load(args)
    ## model train
    model = train(export_path, data, version, args)
    ## model eval
    eval(model,data)
    ## model destory
    model.destroy()


from tkinter import *

root = Tk()

def callback():
    pass
openvar=IntVar()
savevar=IntVar()
quitvar=IntVar()
mm = Menu(root)
fileMenu=Menu(mm,tearoff=False)
fileMenu.add_checkbutton(label='open',command=callback,variable=openvar)
fileMenu.add_checkbutton(label='save',command=callback,variable=savevar)
fileMenu.add_checkbutton(label='quit',command=callback,variable=quitvar)

editvar=IntVar()
editMunu=Menu(mm)
editMunu.add_radiobutton(label='copy',command=callback,value=1,variable=editvar)
editMunu.add_radiobutton(label='redo',command=callback,value=2,variable=editvar)
editMunu.add_radiobutton(label='del',command=callback,value=3,variable=editvar)

mm.add_cascade(label='file',menu=fileMenu)
mm.add_cascade(label='eidt',menu=editMunu)
root.config(menu=mm)
mainloop()


from functools import reduce
from typing import Sequence

import kanren

from ..commons import Clause, Variable

def _turn_clause_to_interim_repr(clause: Clause, suffix: str = "_x"):
    head_vars = dict(
        [(v, ind) for ind, v in enumerate(clause.get_head().get_variables())]
    )

    return [
        tuple([a.get_predicate()]
              + [
                  head_vars[t] if isinstance(t, Variable) and t in head_vars else t
                  for t in a.get_terms()
              ])
        for a in clause.get_literals()
    ]

def construct_recursive_rule(rules: Sequence[Clause]):
    base_cases = [x for x in rules if not x.is_recursive()]
    recursive_rules = [x for x in rules if x.is_recursive()]
    head_predicate = rules[0].get_head().get_predicate()

    # turn each clause into its body: [(predicate, arg1, arg2, ...)]
    base_cases = [_turn_clause_to_interim_repr(x) for x in base_cases]

    recursive_rules = [_turn_clause_to_interim_repr(x) for x in recursive_rules]

    # variables that need to be created in the generic function
    bc_vars = [set(reduce(lambda x, y: x + y, [[v for v in c if isinstance(v, Variable)] for c in x], [])) for x in base_cases]
    rc_vars = [set(reduce(lambda x, y: x + y, [[v for v in c if isinstance(v, Variable)] for c in x], [])) for x in recursive_rules]

    def generic_recusive_predicate(
            *args,
            head=head_predicate,
            bases=base_cases,
            recursion=recursive_rules,
            bvars=bc_vars,
            rvars=rc_vars
    ):
        bvars = [
            dict(
                [(c, kanren.var()) for c in v]
                + [(x, args[x]) for x in range(head.get_arity())]
            )
            for v in bvars
        ]
        rvars = [
            dict(
                [(c, kanren.var()) for c in v]
                + [(x, args[x]) for x in range(head.get_arity())]
            )
            for v in rvars
        ]

        base_cases = [
            [
                a[0].as_kanren()(
                    *[
                        bvars[ind][ar] if ar in bvars[ind] else ar.get_name()
                        for ar in a[1:]
                    ]
                )
                for a in cl
            ]
            for ind, cl in enumerate(bases)
        ]

        recursion = [
            [
                a[0].as_kanren()(
                    *[
                        rvars[ind][ar] if ar in rvars[ind] else ar.get_name()
                        for ar in a[1:]
                    ]
                )
                if a[0] != head_predicate
                else kanren.core.Zzz(
                    generic_recusive_predicate,
                    *[
                        rvars[ind][ar] if ar in rvars[ind] else ar.get_name()
                        for ar in a[1:]
                    ]
                )
                for a in cl
            ]
            for ind, cl in enumerate(recursion)
        ]

        all_for_conde = base_cases + recursion

        return kanren.conde(*all_for_conde)

    return generic_recusive_predicate


import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List

app = FastAPI()

class Article(BaseModel):
    title: str
    contenu: str
    tags: List[str] = []

articles: List[Article] = [
    Article(title="Test 3", contenu="Mon contenu de test", tags=["lol", "test", "mdr"]),
    Article(title="Test 2", contenu="Mon contenu de test", tags=["lol", "test", "mdr"]),
    Article(title="Test 1", contenu="Mon contenu de test", tags=["lol", "test", "mdr"]),
]

@app.get("/", response_model=List[Article])
def index():
    return articles

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)


import re
import pandas as pd
from collections import Counter

text = open("textfile.txt", "r", encoding='utf-8')
text = text.read()

spc_chars = '-.,\n()[]' #special chars

#remove special chars and numbers
for char in spc_chars:
    text = text.replace(char, '')
text = text.lower()
text = re.sub(r'[0-9]+', '', text)

text = text.split()

count = Counter(text).most_common()

wordSorted = sorted(count, key=lambda x: x[1], reverse=True)

df = pd.DataFrame.from_records(wordSorted, columns=['Word', 'Count'], index=None)
print(df)


#!/usr/bin/python3
"""
Contains the TestStateDocs classes
"""

from datetime import datetime
import inspect
from models import state
from models.base_model import BaseModel
import unittest
State = state.State

class TestState(unittest.TestCase):

    # Create an instance of State
    def test_instance_of_BaseModel(self):
        state = State()
        self.assertIsInstance(state, BaseModel)

    # Set the name attribute of an instance of State
    def test_set_name_attribute(self):
        state = State()
        state.name = "California"
        self.assertEqual(state.name, "California")

    # Call the to_dict method on an instance of State
    def test_to_dict_method(self):
        state = State()
        state.name = "California"
        state_dict = state.to_dict()
        self.assertEqual(state_dict['name'], "California")
        self.assertEqual(state_dict['__class__'], "State")

    # Create an instance of State with no arguments
    def test_no_arguments(self):
        state = State()
        self.assertIsNotNone(state.id)
        self.assertIsNotNone(state.created_at)
        self.assertIsNotNone(state.updated_at)

    # Create an instance of State with a created_at or updated_at
    def test_invalid_datetime_format(self):
        with self.assertRaises(ValueError):
            state = State(created_at="2022-01-01")


"""Etapa 5: Puntaje"""
# El responsable de esta etapa es Valentino Ceniceros.
# El responsable de su revision es Emilio Ontiveros.

import Etapa4

def acumulate_points(results,SUCCESS_POINTS = 10, FAIL_POINTS = -3):
    """
    Eecibe una tupla con aciertos y errores y devuelve el puntaje total del juego como entero
    """
    SUCCESS_INDEX = 0
    FAIL_INDEX = 1
    success_point = results[SUCCESS_INDEX] * SUCCESS_POINTS
    fail_point = results[FAIL_INDEX] * FAIL_POINTS
    return success_point + fail_point

def play_match():
    """
    Le pregunta al jugador si desea seguir jugando y luego almacena y muestra el puntaje final.
    """
    points = 0
    decision = True
    while decision:
        results = Etapa4.play_the_game()
        points += acumulate_points(results)
        print(f"Puntaje Final: {points}")
        play_again = str(input("si desea seguir jugando, escriba s, caso contrario el juego terminara "))
        if play_again != 's':
            decision = False

def main_etapa5():
    """
    ejecuta el juego
    """
    play_match()

#main_etapa5()


import configparser
import pdb
import wsgiref
from datetime import datetime
from urllib import parse
from wsgiref.simple_server import make_server

from stravalib.client import Client

import pandas as pd

config = configparser.ConfigParser()
config.read("config.ini")

client = Client()

class _RedirectWSGIApp(object):
    def __init__(self, success_message="Authorized!"):
        self.last_request_uri = None
        self._success_message = success_message

    def __call__(self, environ, start_response):
        start_response("200 OK", [("Content-type", "text/plain")])
        self.last_request_uri = wsgiref.util.request_uri(environ)
        return [self._success_message.encode("utf-8")]

def update_configfile(response):
    for k, v in response.items():
        config["strava"][k] = str(v)

    with open("config.ini", "w") as configfile:
        config.write(configfile)

def get_first_time_token():
    wsgi_app = _RedirectWSGIApp()

    with make_server("localhost", 5000, wsgi_app) as httpd:
        print("Serving on port 5000...")

        authorize_url = client.authorization_url(
            client_id=config["strava"]["client_id"],
            redirect_uri="http://localhost:5000/authorized",
            scope=config["strava"]["scopes"].split(","),
        )

        print(f"Go to {authorize_url}")

        httpd.handle_request()

    authorization_response = wsgi_app.last_request_uri

    qp = parse.parse_qs(parse.urlsplit(authorization_response).query)
    code = qp["code"][0]

    token_response = client.exchange_code_for_token(
        client_id=config["strava"]["client_id"],
        client_secret=config["strava"]["client_secret"],
        code=code,
    )
    update_configfile(token_response)

def authenticate():
    if "refresh_token" not in config["strava"]:
        get_first_time_token()

    elif "access_token" in config["strava"]:
        expires_at = config["strava"]["expires_at"]
        expires_at = datetime.fromtimestamp(int(expires_at))

        if expires_at < datetime.now():
            refresh_response = client.refresh_access_token(
                client_id=config["strava"]["client_id"],
                client_secret=config["strava"]["client_secret"],
                refresh_token=config["strava"]["refresh_token"],
            )
            update_configfile(refresh_response)

    client.access_token = config["strava"]["access_token"]
    client.refresh_token = config["strava"]["refresh_token"]
    client.expires_at = config["strava"]["expires_at"]

if __name__ == "__main__":
    authenticate()

    activities = list(client.get_activities(after="2020-07-01T00:00:00Z", limit=1000))
    acts = [{"id": a.id, **a.to_dict()} for a in activities]
    df = pd.DataFrame(acts)
    df2 = df[["type", "start_date", "name", "private", "distance", "id"]]
    df2 = df2[df2.type == "Ride"]
    df2.to_csv("private-rides.csv", index=False)


# time_ranges is list of lists
timez = [[10, 40], [38, 78], [46, 96], [60, 122]]

def create_pairs_intersection(time_ranges):
    tuples_list = []  # list of start and end tuples

    for t_range in time_ranges:
        start = (t_range[0], -1)
        mid = ((t_range[0] + t_range[1]) / 2), 0
        end = (t_range[1], +1)

        #         print(start)
        #         print(end)
        #         print()

        tuples_list.append(start)
        tuples_list.append(mid)
        tuples_list.append(end)

    return tuples_list

def intersection(time_ranges):
    M = len(time_ranges)
    lower, upper = 0, 0
    tuples_list = create_pairs_intersection(time_ranges)

    print("Number of intervals:", M)
    tuples_list = sorted(tuples_list, key=lambda x: (x[0], x[1]))
    print(tuples_list)

    # 0. [initialize best f]
    for f in range(int(M / 2)):
        tuples_list = sorted(tuples_list, key=lambda x: (x[0], x[1]))

        midcount, endcount = 0, 0

        # 2. [find lower endpoint]
        for pair in tuples_list:
            endcount -= type_p(pair)

            if endcount >= (M - f):
                print("low has been found", lower)
                break

            lower = offset(pair)

            if type_p(pair) == 0:
                midcount += 1

        endcount = 0

        for pair in tuples_list[::-1]:
            endcount += type_p(pair)

            if endcount >= M - f:
                print("high has been found", upper)
                break

            upper = offset(pair)

            if type_p(pair) == 0:
                midcount += 1

        if midcount <= f:
            break

    print("Correct quantity:", M - f)
    print("False quantity:", f)
    if lower > upper:
        return ("Error")
    return [lower, upper]

print(intersection(timez))


class Solution:
    def smallestCommonElement(self, mat: List[List[int]]) -> int:
        m, n = len(mat), len(mat[0])
        memo = collections.defaultdict(int)
        for row in mat:
            for num in row:
                memo[num] += 1
                if memo[num] == m:
                    return num

        return -1

Data Structure/Hash/Medium/LET_no1198_Find Smallest Common Element in All Rows.py


"""
The Fool role

The fool thinks that they are the seer! But they just get random answers instead.
"""
import logging
import random

from ..model import GameStage
from ..model import PlayerRole
from ..resolver import GameAction
from .common import RoleDescription
from .common import RoleDetails
from .common import StageAction
from .miller import ConfusedByMiller
from .seer import description as seer_description
from .seer import SeerAction
from .teams import Team
from .utility_mixins import TargetMustBeAlive
from .villager import description as villager
from .wolf import AffectedByWolves

if False:  # for typing
    from ..game import WurwolvesGame

description = RoleDescription(
    display_name="You are the Fool!",
    stages={
        # No stages are relevant here, since this RoleDescription
        # is only used in ENDED, where we don't want custom text.
        # I'll pass the Seer's controls through anyway though, to stop
        # the error validation in registration.py getting upset
        GameStage.NIGHT: seer_description.stages[GameStage.NIGHT]
    },
    team=Team.VILLAGERS,
    fallback_role=PlayerRole.VILLAGER,
    fallback_role_description=villager,
    masked_role_in_stages={
        GameStage.DAY: PlayerRole.SEER,
        GameStage.NIGHT: PlayerRole.SEER,
        GameStage.VOTING: PlayerRole.SEER,
    },
)

class FoolAction(ConfusedByMiller, AffectedByWolves, TargetMustBeAlive, GameAction):
    def execute(self, game: "WurwolvesGame"):
        from .registration import get_role_team

        fool_name = self.originator.model.user.name
        target_name = self.target.model.user.name

        target_is_wolf = bool(random.randint(0, 1))

        logging.info(f"Foor: {fool_name} checks {target_name}")

        if self.prevented:
            game.send_chat_message(
                f"You tried to check {target_name} but you couldn't concentrate",
                is_strong=True,
                player_list=[self.originator.model.id],
            )
        elif self.originator_attacked_by_wolf:
            game.send_chat_message(
                f"You checked {target_name} but were rudely interrupted",
                is_strong=True,
                player_list=[self.originator.model.id],
            )
        elif target_is_wolf:
            game.send_chat_message(
                f"You checked {target_name}... they are a wolf!",
                is_strong=True,
                player_list=[self.originator.model.id],
            )
        else:
            game.send_chat_message(
                f"You checked {target_name}... they are not a wolf",
                is_strong=True,
                player_list=[self.originator.model.id],
            )

def register(role_map):
    role_map.update(
        {
            PlayerRole.FOOL: RoleDetails(
                role_description=description, actions={GameStage.NIGHT: FoolAction}
            )
        }
    )


# 2. La forma para calcular cuantas maneras diferentes tengo para elegir r cosas distintas de un
# conjunto de n cosas es:
# C(n,r) = n! / (r!*(n-r)!)
#
# Donde la función factorial se define como
# n! = n *(n-1)*(n-2)*…*2*1
#
# Descubra una versión recursiva de la fórmula anterior y escriba una función recursiva que calcule el
# valor de dicha fórmula.

def elegir(n, r):
    if ((r == 0) | (n == r)):
        return (1)
    elif (r > n):
        return (0)
    else:
        return (elegir(n - 1, r - 1) + elegir(n - 1, r))

print(elegir(5, 2))


'''
7
5 0
1 1 1
3 1 2
6 1 1
1 2 2 4
8 2 2 4
4 3 3 5 6
'''

n = int(input())

works = [[0, 0]]

for _ in range(n):
    works.append(list(map(int, input().split())))

n += 1

dp = [0] * n
seq = [[] for _ in range(n)]

for i in range(len(works)):
    if len(works[i]) == 2: continue
    for j in range(2, len(works[i])):
        seq[i].append(works[i][j])

def get_time(work_num): return works[work_num][0]

for i in range(n):
    max_pre_time = 0
    for s in seq[i]:
        max_pre_time = max(max_pre_time, dp[s])
    dp[i] = max(dp[i], get_time(i) + max_pre_time)

print(max(dp))


from datetime import datetime

import numpy as np
import pandas as pd
import pandas_datareader.data as pdr
import plotly.graph_objs as go

def get(tickers, start_date, end_date):
    def data(ticker):
        return pdr.get_data_yahoo(ticker, start=start_date, end=end_date)

    datas = map(data, tickers)
    return pd.concat(datas, keys=tickers, names=["ticker", "date"])

def get_ema(window, prices):
    K = (2 / (window + 1))
    ma = prices.rolling(window=window).mean().dropna()

    data = pd.DataFrame(index=ma.index)
    data["Price"] = prices
    data["EMA"] = np.NaN
    data.EMA[0] = ma[1]

    for i in range(1, len(data)):
        data.EMA[i] = (data.Price[i] * K) + ((1 - K) * data.EMA[i - 1])

    return data

def simple_config_plot(fig, title):
    title={"text": title,
           "xanchor": "center",
           "yanchor": "bottom",
           "y": 0,
           "x": 0.5}

def simple_plot(data, title):
    fig = go.Figure(data=data)
    simple_config_plot(fig, title)
    fig.show()

tickers = ["AAPL", "MSFT", "^GSPC"]
start_date = datetime(year=2018, month=1, day=1,
                      hour=0, minute=0, second=0, microsecond=0)
end_date = datetime(year=2019, month=12, day=31,
                    hour=0, minute=0, second=0, microsecond=0)

data = get(tickers, start_date, end_date)
aapl = data.loc["AAPL"]["2018-01-01":"2018-12-31"].dropna()

window = 20
aapl_avg = aapl.Close.rolling(window=window).mean().dropna()
aapl_std = aapl.Close.rolling(window=window).std().dropna()

aapl_bollinger = pd.DataFrame(index=aapl_avg.index)
aapl_bollinger["mband"] = aapl_avg
aapl_bollinger["uband"] = aapl_avg + aapl_std.apply(lambda x: (x * 2))
aapl_bollinger["lband"] = aapl_avg - aapl_std.apply(lambda x: (x * 2))

aapl_prices = aapl[aapl_bollinger.index[0]:]

prices = go.Candlestick(x=aapl_prices.index,
                        open=aapl_prices.Open,
                        high=aapl_prices.High,
                        low=aapl_prices.Low,
                        close=aapl_prices.Close,
                        name="Prices")

uband = go.Scatter(x=aapl_bollinger.index,
                   y=aapl_bollinger.uband,
                   name="Upper Band",
                   line=dict(color="#17BEFC"),
                   opacity=1)

mband = go.Scatter(x=aapl_bollinger.index,
                   y=aapl_bollinger.mband,
                   name="Moving Average",
                   line=dict(color="#B22222"),
                   opacity=1)

lband = go.Scatter(x=aapl_bollinger.index,
                   y=aapl_bollinger.lband,
                   name="Lower Band",
                   line=dict(color="#17BEFC"),
                   opacity=1)

data = [prices, uband, lband, mband]
simple_plot(data, "Bollinger Bands")


def build_position_embedding(config, z_shape, x_shape, dim):
    if not config['position_embedding']:
        return None, None

    if config['position_type'] == 'sine':
        from .sine import SinePositionEmbedding

        with_branch_index = config['with_branch_index']

        return SinePositionEmbedding(dim, (z_shape[1], z_shape[0]), 0 if with_branch_index else None), \
               SinePositionEmbedding(dim, (x_shape[1], x_shape[0]), 1 if with_branch_index else None)
    elif config['position_type'] == 'learned':
        from .learned import Learned2DPositionalEncoder

        return Learned2DPositionalEncoder(dim, z_shape[0], z_shape[1]), \
               Learned2DPositionalEncoder(dim, x_shape[0], x_shape[1])
    else:
        raise ValueError('Unknown positional embedding type: {}'.format(config['position_type']))


from app import app, db
from flask import jsonify, request

def get_all_categories():
    from models import ticket_category
    categories = ticket_category.query.all()

    all_categories = []

    if categories:
        for category in categories:
            all_categories.append({
                'id': category.id,
                'category': category.category
            })
        return all_categories
    else:
        return None

@app.route('/categories', methods=['GET'])
def runCategories():
    # data = request.get_json()
    allCategories = get_all_categories()

    if allCategories:
        return jsonify(allCategories)
    else:
        return jsonify({'message': 'Error accessing categories'}), 404


import random

import matplotlib.pyplot as plt
import numpy as np

class Grid:

    def __init__(self, show_flag=False, dim_x=4, dim_y=4):
        self.show_flag = show_flag
        self.dim_x = dim_x
        self.dim_y = dim_y
        self.flat_grid_size = dim_x * dim_y
        self.grid = np.zeros((dim_x, dim_y), dtype=int)
        self.init_grid()

    def init_grid(self):
        flat_index1 = random.randrange(0, self.flat_grid_size)
        flat_index2 = random.randrange(0, self.flat_grid_size)
        while flat_index2 == flat_index1:
            flat_index2 = random.randrange(0, self.flat_grid_size)
        index1 = np.unravel_index(flat_index1, (self.dim_x, self.dim_y))
        index2 = np.unravel_index(flat_index2, (self.dim_x, self.dim_y))
        self.grid[index1] = 4 if random.uniform(0, 1) < 0 else 2
        self.grid[index2] = 4 if random.uniform(0, 1) < 0.25 else 2

    def project(self, direction):
        # Rotate to be come to the "up case"
        current_grid = np.rot90(self.grid, k=direction)

        # # Check that the projection is valid
        # condition = (current_grid[0:(current_grid.shape[0]-1), :] - current_grid[1:current_grid.shape[0], :]) == 0
        # similar_indexes = np.extract(condition, current_grid)
        # if similar_indexes.size > 0 and np.max(similar_indexes) == 0:
        #     return False

        # Apply projection
        new_grid = np.zeros_like(current_grid)
        for y in range(0, current_grid.shape[1]):
            previous = None
            previous_index = 0
            for x in range(0, current_grid.shape[0]):
                if current_grid[x, y] > 0:
                    if previous is None:
                        previous = current_grid[x, y]
                    else:
                        if current_grid[x, y] == previous:
                            new_grid[previous_index, y] = 2 * previous
                            previous = None
                        else:
                            new_grid[previous_index, y] = previous
                            previous = current_grid[x, y]
                        previous_index += 1
            if previous is not None:
                new_grid[previous_index, y] = previous

        # Check that the projection is valid
        if np.array_equal(current_grid, new_grid):
            return False

        # Rotate back to the initial orientation
        self.grid = np.rot90(new_grid, k=4 - direction)
        return True

    def add(self):
        # Define the value to add
        value = 4 if random.uniform(0, 1) < 0.125 else 2

        # Fine a location to add it
        condition = self.grid == 0
        index_array = np.arange(0, self.flat_grid_size).reshape((self.dim_x, self.dim_y))
        zeros_indexes = np.extract(condition, index_array)
        chosen_flat_index = zeros_indexes[random.randrange(0, zeros_indexes.size)]
        index = np.unravel_index(chosen_flat_index, (self.dim_x, self.dim_y))

        # Add the value
        self.grid[index] = value
        return True

    def show(self):
        plt.imshow(np.log(1 + self.grid), vmin=0, vmax=11)
        plt.pause(0.01)

    def random_step(self):
        is_projected = False
        direction = np.random.permutation(np.arange(4))
        direction_index = 0
        while not is_projected:
            is_projected = self.project(direction[direction_index])
            direction_index += 1
        is_success = self.add()
        if self.show_flag:
            self.show()
        return is_success

    def fcnn(self, weights):
        ns = [4 * 4, 64, 32, 16, 4]

        layer = np.reshape(np.log2(self.grid + 1), ns[0])
        layer = layer.astype(float)
        i2 = 0

        for i in range(1, len(ns)):
            i0 = i2
            i1 = i0 + ns[i] * ns[i - 1]
            i2 = i1 + ns[i]
            w = np.reshape(weights[i0:i1], (ns[i], ns[i - 1]))
            b = np.reshape(weights[i1:i2], ns[i])
            layer = np.matmul(w, layer) + b
            layer = np.maximum(layer, 0)

        direction = np.argmax(layer)
        return direction

    def step(self, weights):
        direction = self.fcnn(weights)
        is_projected = self.project(direction)
        if not is_projected:
            return False
        is_success = self.add()
        if self.show_flag:
            self.show()
        return is_success

    def play_until_end(self, weights):
        grid0 = self.grid
        do_continue = True
        step = 0
        while do_continue:
            do_continue = self.step(weights)
            step += 1
        self.grid = grid0
        return step

if __name__ == "__main__":
    # Add -m cProfile -s cumtime to profile
    grid = Grid(show_flag=True)
    do_continue = True
    step = 0
    while do_continue:
        do_continue = grid.random_step()
        step += 1
    print(step)


import socket
from threading import Thread

def recv_message(server, BUFFER_SIZE):
    while True:
        res, addr = server.recvfrom(BUFFER_SIZE)
        if res:
            print("收到%s的消息:%s"% (addr[1], res.decode("utf8")))
            str = input("回复消息:")
            server.sendto(str.encode("utf8"), addr)
            print("回复成功")

if __name__ == '__main__':
    ADDR = ("192.168.12.155", 50000)
    BUFFER_SIZE = 1024
    server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server.bind(ADDR)
    t1 = Thread(target=recv_message, args=(server, BUFFER_SIZE))
    t1.start()
    t1.join()
    server.close()


# import the necessary packages
import argparse
import pickle
from pathlib import Path
import cv2
from searcher import Searcher
from rgbhistogram import RGBHistogram
from indexmovie import index_movie

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,	help="Path to the image")
ap.add_argument("-m", "--movie", required=True, help="Path to the movie")
ap.add_argument("-s", "--skip", required=True, help="Duration to skip checking")
args = vars(ap.parse_args())

movie_index_filename = args["movie"].rsplit(".", 1)[0]+'.db'
index_file = Path(movie_index_filename)
if index_file.is_file():
	print("Loading movie index from "+movie_index_filename)
	movie_index = pickle.load(open(movie_index_filename, 'rb'))
else:
	print("Index file not found, indexing movie...")
	movie_index = index_movie(args["movie"], movie_index_filename)

print("Done")

movie_searcher = Searcher(movie_index)
# results = searcher.search(queryFeatures)

image = cv2.imread(args["image"])
movie_cap = cv2.VideoCapture(args["movie"])

desc = RGBHistogram([8, 8, 8])
skip_duration = float(args["skip"])

features = desc.describe(image)
results = movie_searcher.search(features, skip_duration)

movie_cap.set(1, int(results[0][1]))
ret, frame = movie_cap.read()
cv2.imwrite("result.jpg", frame)

print(results[0])


'''
Created on Jul 8, 2019

@author: skwok
'''

import datetime
import threading
import signal
import traceback
import time

from core.queues import Event_queue

# Server Task import
from core.server_task import DRPF_server_handler, start_http_server

from models.processing_context import Processing_context
from models.arguments import Arguments
from models.action import Action
from models.event import Event
from models.data_set import Data_set

from utils.DRPF_logger import getLogger

from config.framework_config import ConfigClass
from bokeh.client import push_session
from bokeh.io import curdoc
from bokeh.plotting.figure import figure
from bokeh.layouts import column

class Framework(object):
    '''
    This class implements the core of the framework.
    There are two threads: the event loop and the action loop.
    '''

    def __init__(self, pipeline, configFile):
        '''
        pipeline: a class containing recipes

        Creates the event_queue and the action queue
        '''
        self.config = ConfigClass (configFile)
        self.logger = getLogger (self.config.logger_config_file, name ="DRPF")
        pipeline.set_logger (self.logger)

        self.event_queue = Event_queue()
        self.event_queue_hi = Event_queue()

        self.pipeline = pipeline
        self.context = Processing_context (self.event_queue_hi, self.logger, self.config)
        self.keep_going = True
        self.init_signal ()

    def get_event (self):
        try:
            try:
                return self.event_queue_hi.get_nowait()
            except:
                return self.event_queue.get(True, self.config.event_timeout)
        except Exception as e:
            ev = self.config.no_event_event
            if ev is None:
                return None
            time.sleep(self.config.no_event_wait_time)
            ev.args = Arguments(name=ev.name, time=datetime.datetime.ctime(datetime.datetime.now()))
            return ev

    def _push_event (self, event_name, args):
        '''
        Pushes high priority events

        Normal events go to the lower priority queue
        Only used in execute.

        '''
        self.logger.info (f"Push event {event_name}, {args.name}")
        self.event_queue_hi.put(Event (event_name, args))

    def append_event (self, event_name, args):
        '''
        Appends low priority event to the end of the queue
        '''
        self.event_queue.put (Event (event_name, args))

    def event_to_action (self, event, context):
        '''
        Passes event.args to action.args

        Note that event.args comes from previous action.output.
        '''
        event_info = self.pipeline.event_to_action (event, context)
        self.logger.info (f'Event to action {event_info}')
        return Action (event_info, args=event.args)

    def execute (self, action, context):
        '''
        Executes one action
        The input for the action is in action.args.
        The action returns action_output and it is passed to the next event if action is successful.
        '''
        pipeline = self.pipeline
        action_name = action.name
        try:
            if pipeline.get_pre_action(action_name)(action, context):
                if self.config.print_trace:
                    self.logger.info ('Executing action ' + action.name)
                action_output = pipeline.get_action(action_name)(action, context)
                if pipeline.get_post_action(action_name)(action, context):
                    if not action.new_event is None:
                        new_args = Arguments() if action_output is None else action_output
                        self._push_event (action.new_event, new_args)
                    if not action.next_state is None:
                        context.state = action.next_state
                    if self.config.print_trace:
                        self.logger.info ('Action ' + action.name + ' done')
                    return
                else:
                    # post-condition failed
                    context.state = 'stop'
            else:
                # Failed pre-condition
                if self.config.pre_condition_failed_stop:
                    context.state = 'stop'
        except:
            self.logger.error ("Exception while invoking {}. Execution stopped.".format (action_name))
            context.state = 'stop'
            if self.config.print_trace:
                traceback.print_exc()

    def start_action_loop (self):
        '''
        This is a thread running the action loop.
        '''

        def loop ():
            #if self.context.config.instrument.interactive >=1:

            self.context.bokeh_session = push_session(curdoc())
            p=figure()
            c=column(children=[p])
            curdoc().add_root(c)
            self.context.bokeh_session.show(c)

            while self.keep_going:
                try:
                    action = ''
                    event = ''
                    event = self.get_event ()
                    if event is None:
                        self.logger.info ("No new events - do nothing")

                        if self.event_queue.qsize() == 0 and \
                            self.event_queue_hi.qsize() == 0:
                            self.logger.info (f"No pending events or actions, terminating")
                            self.keep_going = False
                    else:
                        action = self.event_to_action (event, self.context)
                        self.execute(action, self.context)
                    if self.context.state == 'stop':
                        break
                except Exception as e:
                    self.logger.error (f"Exception while processing action {action}, {e}")
                    break
            self.keep_going = False

        thr = threading.Thread (name='action_loop', target=loop)
        thr.setDaemon(True)
        thr.start()

    def init_signal (self):
        '''
        Captures keyboard interrupt
        '''

        def handler (*args):
            self.keep_going = False

        # signal.signal (signal.CTRL_BREAK_EVENT, handler)
        signal.signal (signal.SIGINT, handler)

    def start (self):
        '''
        Starts the event loop and the action loop
        '''
        self.start_action_loop ()

    def waitForEver (self):
        while self.keep_going:
            time.sleep (1)

    #
    # Methods for HTTP server
    #
    def http_server_loop (self):
        start_http_server(self, self.config, self.logger)

    def start_http_server (self):
        thr = threading.Thread (target=self.http_server_loop)
        thr.setDaemon(True)
        thr.start()

    def getPendingEvents (self):
        return self.event_queue.get_pending(), self.event_queue_hi.get_pending()

    #
    # Methods to handle data set
    #
    def ingesst_data (self, path, files=None):
        ds = self.context.data_set
        if ds is None:
            ds = Data_set (path, self.logger, self.config)

        if not files is None:
            for f in files:
                ds.append_item(f)

        self.context.data_set = ds


from flask import Flask, request, render_template ,url_for,redirect, jsonify
app = Flask(__name__)
import numpy as np
import pandas as pd
import nltk
import string
from nltk import word_tokenize
import time
import re
from nltk.corpus import stopwords
from gensim.models import word2vec
from sklearn.neural_network import MLPClassifier
from sklearn.externals import joblib
import csv

if __name__ == '__main__':
	app.run(debug=True, port=5000)

stopwords= set(nltk.corpus.stopwords.words('english'))

def div2word(raw_text, remove_stopwords=False):
	#Remove non-letters, but including numbers
	letters_only = re.sub("[^0-9a-zA-Z]", " ", raw_text)
	#Convert to lower case, split into individual words
	words = letters_only.lower().split()
	if remove_stopwords:
		stops = set(stopwords.words("english")) #searching a set is much faster
		meaningful_words = [w for w in words if not w in stops] # Remove stop words
		words = meaningful_words
	return words

def makeFeatureVec(words, model, wv_size):
	featureVec = np.zeros((wv_size,), dtype="float32")#preallocation of numpy array for speed
	nwords = 0

	index2word_set = set(model.wv.index2word)

	for word in words:
		if word in index2word_set:
			nwords = nwords + 1
			featureVec = np.add(featureVec, model[word])
# Dividing the result by the number of words to get the average
	if nwords == 0:
		nwords = 1
	featureVec = np.divide(featureVec, nwords)
	return featureVec

wv_size=300 #var for the size or the dimension of the word vector

w2v=joblib.load('w2v_fm')#word2vec model
TM=joblib.load('final_trained_model')#trained MLP model

@app.route("/",methods=['GET', 'POST'])
@app.route('/home', methods=['GET', 'POST'])
def home():
	if request.method == 'POST':
		data=[]
		data1=request.form.get('data1')
		data2=request.form.get('data2')
		d1_1=div2word(data1)
		d2_1=div2word(data2)
		d1_2=makeFeatureVec(d1_1,w2v,wv_size)
		d2_2=makeFeatureVec(d2_1,w2v,wv_size)
		data.append(d1_2)
		data.append(d2_2)
		tox_lvl=[]
		txc=(TM[0].predict_proba(data)[:,1])*100.000
		svr_txc=(TM[1].predict_proba(data)[:,1])*100.000
		obc=(TM[2].predict_proba(data)[:,1])*100.000
		thrt=(TM[3].predict_proba(data)[:,1])*100.000
		inslt=(TM[4].predict_proba(data)[:,1])*100.000
		idnt_ht=(TM[5].predict_proba(data)[:,1])*100.000
		tox1=round(float(txc[0]),2)
		tox2=round(float(txc[1]),2)
		svtox1=round(float(svr_txc[0]),2)
		svtox2=round(float(svr_txc[1]),2)
		obc1=round(float(obc[0]),2)
		obc2=round(float(obc[1]),2)
		thrt1=round(float(thrt[0]),2)
		thrt2=round(float(thrt[1]),2)
		inslt1=round(float(inslt[0]),2)
		inslt2=round(float(inslt[1]),2)
		idnt1=round(float(idnt_ht[0]),2)
		idnt2=round(float(idnt_ht[1]),2)
		return redirect(url_for('toxicity',tox1=tox1,tox2=tox2,svtox1=svtox1,svtox2=svtox2,obc1=obc1,obc2=obc2,thrt1=thrt1,thrt2=thrt2,inslt1=inslt1,inslt2=inslt2,idnt1=idnt1,idnt2=idnt2))
		#,sev_toxic=svr_txc,oscene=obc,threat=thrt,insult=inslt,identity=idnt_ht

	return '''<form method="POST" action=''>

Enter the first live input(comment1):
 <input type="text" name="data1" size=150>

Enter the second live input(comment2):
 <input type="text" name="data2" size=150>

					<input type="submit" value="Submit">

				</form>'''

@app.route('/toxicity/<tox1>/<tox2>/<svtox1>/<svtox2>/<obc1>/<obc2>/<thrt1>/<thrt2>/<inslt1>/<inslt2>/<idnt1>/<idnt2>',methods=['GET', 'POST'])
def toxicity(tox1,tox2,svtox1,svtox2,obc1,obc2,thrt1,thrt2,inslt1,inslt2,idnt1,idnt2):
    tox1=tox1
    tox2=tox2
    svtox1=svtox1
    svtox2=svtox2
    obc1=obc1
    obc2=obc2
    thrt1=thrt1
    thrt2=thrt2
    inslt1=inslt1
    inslt2=inslt2
    idnt1=idnt1
    idnt2=idnt2
    if (request.method =='POST'):
    	return redirect(url_for('home'))
    return render_template('toxicity.html',tox1=tox1,tox2=tox2,svtox1=svtox1,svtox2=svtox2,obc1=obc1,obc2=obc2,thrt1=thrt1,thrt2=thrt2,inslt1=inslt1,inslt2=inslt2,idnt1=idnt1,idnt2=idnt2)