"""
Initialy populate an empty django database with the required groups, and possibly dummy data for testing.

"""
import argparse
import os
import sys

import django

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Populate the database with initial values")
    parser.add_argument('--mode', nargs='?', const=1, type=str, default='debug', help='debug/production')
    parser.add_argument('--create-dummy-data', dest='createDummyData', action='store_true',
                        help='if activated dummy data is generated')
    parser.set_defaults(createDummyData=False)
    DUMMY, MODE = parser.parse_args().createDummyData, parser.parse_args().mode

    if MODE not in ["debug", "production"]:
        print("Please use --mode debug or --mode production")
        sys.exit(1)
else:
    MODE = 'debug'
    DUMMY = False

if MODE == 'debug':
    os.environ['DJANGO_SETTINGS_MODULE'] = 'BepMarketplace.settings_development'
elif MODE == 'production':
    os.environ['DJANGO_SETTINGS_MODULE'] = 'BepMarketplace.settings'

django.setup()

from django.contrib.auth.models import Group, User
from index.models import Track, UserMeta
from proposals.models import Proposal
from students.models import Application
from support.models import CapacityGroup
from timeline.models import TimePhase, TimeSlot
import random
from timeline.utils import get_timeslot
from datetime import date
from django.conf import settings
from proposals.utils import get_all_proposals

tracks = [
    ("Automotive", "AU"),
    ("Smart&Sustainable Society", "SSS"),
    ("Connected World", "CW"),
    ("Care&Cure", "C&C"),
]

Groups = (
    ("EES", "Electrical Energy Systems"),
    ("ECO", "Electro-Optical Communications"),
    ("EPE", "Electromechanics and Power Electronics"),
    ("ES", "Electronic Systems"),
    ("IC", "Integrated Circuits"),
    ("CS", "Control Systems"),
    ("SPS", "Signal Processing Systems"),
    ("PHI", "Photonic Integration"),
    ("EM", "Electromagnetics")
)
for group in Groups:
    c, created = CapacityGroup.objects.get_or_create(ShortName=group[0], FullName=group[1])
    if created:
        print("creating {}-{}".format(group[0], group[1]))

print("populating tables with production values")

# setup tracks
trackobjs = []
for track in tracks:
    t, created = Track.objects.get_or_create(Name=track[0])
    if created:
        print("creating track {}".format(track[0]))
        t.ShortName = track[1]
        t.save()
    trackobjs.append(t)

# setup all groups with permissions
g, created = Group.objects.get_or_create(name='type1staff')
if created:
    print("creating type1staff")
    # perms = Permission.objects.filter(content_type=ContentType.objects.get_for_model(Proposal))
    # for p in perms:
    #     g.permissions.add(p)
    # #perms = Permission.objects.filter(content_type=ContentType.objects.get_for_model(CategoryResult))
    # g.permissions.add(perms[0])
    # g.permissions.add(perms[1])
    g.save()

g, created = Group.objects.get_or_create(name='type2staff')
if created:
    print("creating type2staff")
    # #perms = Permission.objects.filter(content_type=ContentType.objects.get_for_model(Proposal))
    # g.permissions.add(perms[0])
    # g.permissions.add(perms[1])
    # g.permissions.add(perms[2])
    g.save()

g, created = Group.objects.get_or_create(name='type2staffunverified')
if created:
    print("creating type2staffunverified")
    # perms = Permission.objects.filter(content_type=ContentType.objects.get_for_model(Proposal))
    # g.permissions.add(perms[0])
    # g.permissions.add(perms[1])
    # g.permissions.add(perms[2])
    g.save()

g, created = Group.objects.get_or_create(name='type3staff')
if created:
    print("creating type3staff")
    # for model in all_models:
    #     perms = Permission.objects.filter(content_type=ContentType.objects.get_for_model(model))
    #     for p in perms:
    #         g.permissions.add(p)
    g.save()

g, created = Group.objects.get_or_create(name='type4staff')
if created:
    print("creating type4staff")

g, created = Group.objects.get_or_create(name='type5staff')
g.save()
if created:
    print("creating type5staff")

g, created = Group.objects.get_or_create(name='type6staff')
g.save()
if created:
    print("creating type6staff")

if not DUMMY:
    # End of non dummy generation
    sys.exit(0)

# generate test users, proposals and applictions for debug purpose
print("populating tables with debug objects")

NUMPROFS = 20
NUMPHDS = 30
NUMSTDS = 50
NUMPROPOSALS = 75

type1staff = Group.objects.get(name="type1staff")
type2staff = Group.objects.get(name="type2staff")
type3staff = Group.objects.get(name="type3staff")

profs = list(type1staff.user_set.all())
phds = list(type2staff.user_set.all())

print("creating timeslot and timephase")
ts = TimeSlot(Name="semester", Begin=date.today(), End=date.today().replace(year=date.today().year+1))
ts.save()
tp = TimePhase(Description=4, TimeSlot=ts, Begin=date.today(), End=date.today().replace(month=date.today().month + 3))
tp.save()

print("creating {} professors".format(NUMPROFS))
for i in range(0, NUMPROFS):
    try:
        prof = User.objects.create_user('professor{}'.format(i), 'professor{}@tue.nl'.format(i), 'marketplace')
        prof.first_name = "professor"
        prof.last_name = str(i)
        prof.groups.add(type1staff)
        prof.save()
        profs.append(prof)
    except:
        print(str(i) + " not created")
    prof = User.objects.get(username='professor{}'.format(i))
    try:
        mta = UserMeta()
        mta.Fullname = "Professor-" + str(i)
        mta.Studentnumber = 0
        mta.User = prof
        mta.save()
        print("usermeta prof" + str(i))
    except:
        print(str(i) + " prof usermeta not created")

print("creating {} phders".format(NUMPHDS))
for i in range(0, NUMPHDS):
    try:
        phd = User.objects.create_user('phd{}'.format(i), 'phd{}@tue.nl'.format(i), 'marketplace')
        phd.first_name = "phd"
        phd.last_name = str(i)
        phd.groups.add(type2staff)
        phd.save()
        phds.append(phd)
    except:
        print(str(i) + " not created")
    phd = User.objects.get(username='phd{}'.format(i))
    try:
        mta = UserMeta()
        mta.Fullname = "phd-" + str(i)
        mta.Studentnumber = 0
        mta.User = phd
        mta.save()
        print("usermeta phd" + str(i))
    except:
        print(str(i) + " phd usermeta not created")

stds = []
print("creating {} students".format(NUMSTDS))
for i in range(0, NUMSTDS):
    try:
        std = User.objects.create_user('std{}'.format(i), 'std{}@tue.nl'.format(i), 'marketplace')
        std.first_name = "std"
        std.last_name = str(i)
        std.save()
        stds.append(std)
    except:
        print(str(i) + " not created")
    std = User.objects.get(username='std{}'.format(i))
    try:
        mta = UserMeta()
        mta.Fullname = "student-" + str(i)
        mta.Studentnumber = str(i) + str(i) + str(i) + str(i) + str(i)
        mta.User = std
        mta.EnrolledBEP = True
        mta.save()
        mta.TimeSlot.add(ts)
        mta.save()
        print("usermeta" + str(i))
    except:
        print(str(i) + " usermeta not created")

print("creating the support user")
try:
    supp = User.objects.create_user('janedoe', 'j.doe.1@tue.nl', 'marketplace')
    supp.first_name = "Jane"
    supp.last_name = "Doe"
    supp.groups.add(type3staff)
    supp.save()
except:
    print("no type3 created")
supp = User.objects.get(username='janedoe')
try:
    mta = UserMeta()
    mta.Fullname = 'Jane Doe'
    mta.User = supp
    mta.save()
except:
    print('Supp usermeta not created')

print("creating track Head and assigning him")
try:
    trackh = User.objects.create_user('johndoe', 'j.doe.2@tue.nl', 'marketplace')
    trackh.first_name = "John"
    trackh.last_name = "Doe"
    trackh.groups.add(type1staff)
    trackh.save()
except:
    print("no trackhead created")
supp = User.objects.get(username='johndoe')
try:
    mta = UserMeta()
    mta.Fullname = 'John Doe'
    mta.User = supp
    mta.save()
except:
    print('Track usermeta not created')

for track in tracks:
    try:
        t = Track.objects.get(Name=track[0])
        t.Head = trackh
        t.save()
    except:
        print("no track created")

def flip(x):
    """

    :param x:
    :return:
    """
    return True if random.random() < x else False

# create usermeta's
for user in User.objects.all():
    try:
        meta = user.usermeta
    except UserMeta.DoesNotExist:
        meta = UserMeta(User=user)
        meta.save()
    meta.TimeSlot.add(ts)
    meta.save()

print("creating proposals")
for i in range(0, NUMPROPOSALS):
    try:
        p = Proposal()
        p.Title = "project{}".format(i)
        p.ResponsibleStaff = random.choice(profs)
        p.Group = CapacityGroup.objects.order_by('?').first()
        p.NumStudentsMin = random.randint(1, 2)
        p.NumStudentsMax = random.randint(p.NumStudentsMin, 5)
        p.GeneralDescription = "stuff about project. autogenerated with number {}".format(i)
        p.StudentsTaskDescription = "students have to do stuff woop woop"
        p.Track = random.choice(trackobjs)
        # p.Private = None
        # p.Image = random.choice(["niels.png", "crying.png"])
        # p.Status = random.choice(Proposal.StatusOptions)[0]
        p.Status = 4
        p.TimeSlot = get_timeslot()
        p.save()  # save already to activate the manytomany field of assistants
        numphd = random.choice([1, 2])
        ass1 = random.choice(phds)
        p.Assistants.add(ass1)
        if numphd == 2:
            phds.remove(ass1)
            ass2 = random.choice(phds)
            p.Assistants.add(ass2)
            phds.append(ass1)
        p.save()
        print('{} created'.format(p))
    except:
        print(str(i) + "th proposal not created")

print("generating applications")
secure_random = random.SystemRandom()
projects = get_all_proposals()
for i in range(0, NUMSTDS):
    for c in range(1, settings.MAX_NUM_APPLICATIONS+1):
        app = Application(Priority=c,
                          Proposal=secure_random.choice(projects),
                          Student=User.objects.get(username='std{}'.format(i)))
        app.save()


#!/usr/bin/env python
# coding=utf-8
import requests
import json
from bson import ObjectId

#######
from .. import db

#预约智能柜
def book_cabinet_door_yunwei(cabinet_station_code = '',book_num = 1,cabinet_temperature_type = 0,cabinet_order_id = ''):
    res = requests.Session()
    url = "http://yc.wiselong.com:8080/psi/psi/eleme/elemeOrderSlot.do?method=orderSlotNum&storeId=%s&slotNum=%d&machineType=%d&orderNo=%s" % (cabinet_station_code,book_num,cabinet_temperature_type,cabinet_order_id)
    response = res.get(url)
    data = json.loads(response.text)
    return data

#获取所预约智能柜信息
def get_book_task_info_yunwei(cabinet_order_id):
    res = requests.Session()
    url = "http://yc.wiselong.com:8080/psi/psi/eleme/elemeOrderSlot.do?method=queryOrderInfo&orderNo=%s" % (cabinet_order_id)
    response = res.get(url)
    data = json.loads(response.text)
    return data

#取消所预约的智能柜
def cancel_book_task_yunwei(cabinet_order_id):
    cinfo = db.con.cabinet_order.find_one({'_id':ObjectId(cabinet_order_id)})
    storeId = str(cinfo['cabinet_json']['cabinet_station_code'])
    machineNo = str(cinfo['cabinet_json']['cabinet_code'])
    slotNo = str(cinfo["cabinet_json"]['slotNo'])
    orderNo = cabinet_order_id
    slotList = []
    slotList = [{'orderNo':orderNo,'machineNo':machineNo,'slotNo':slotNo}]

    res = requests.Session()
    url = "http://yc.wiselong.com:8080/psi/psi/eleme/elemeOrderSlot.do?method=cancelOrderSlot&storeId=%s&slotList=%s" % (storeId,slotList)
    response = res.get(url)
    data = json.loads(response.text)
    return data

#获取云味所有智能柜门店信息
def get_nearby_cabinet_yunwei():
    res = requests.Session()
    url = "http://yc.wiselong.com:8080/psi/psi/eleme/elemeOrderSlot.do?method=queryStoreList"
    response = res.get(url)
    data = json.loads(response.text)
    return data


#!/usr/bin/python

import click
import pandas as pd
from src import DataPreprocessing
from src import CategoryPrediction
from src import SentenceSimilarity

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

'''
1. Preprocessing (print lemma, token, stopword)
2. Classify tokenized question to category (Doc2vec category model)
3. find closest question tag (Doc2vec question model of specific category)
4. find closest question tag from all questions (not category specific)
'''
@cli.command()
@click.option('--question', "question", prompt='Question to process', help='Question to process')
def run(question):
    click.echo("\n" + "The question is: " + question + "\n")

    '''
    Tokenize input question
    '''
    dp = DataPreprocessing()
    tokens = dp.clean_sentence(question)
    for token in tokens:
        click.echo("Token: " + " " + token.get("token") +
                   ", Lemma: " + " " + token.get("lemma") +
                   ", is stop word: " + " " + str(token.get("is_stop")))

    tokens_for_pred = [token.get("lemma") for token in tokens]

    '''
    Predict category from tokenized question
    '''
    click.echo("\n" + "Predicting Category... ")
    cp = CategoryPrediction()
    prediction = cp.predict(tokens_for_pred, model_to_use="doc2vec")
    predicted_category = prediction.get("category")
    click.echo("Predicted Category is: " + predicted_category +
               ", Prediction Score: " + str(prediction.get("score")))

    '''
    Predict Question and Answer from the predicted Category.
    '''
    click.echo("\n" + "Predicted From " + predicted_category + "Category: ")
    ss1 = SentenceSimilarity(predicted_category)
    similar_sentence_for_category = ss1.find_similar_sentence(tokens_for_pred)

    # get answer and question from tag
    data = pd.read_pickle('resources/processed_data/category_' + predicted_category + '.pkl')
    question = data.iloc[similar_sentence_for_category.get("sentence")]["Name"]
    answer = data.iloc[similar_sentence_for_category.get("sentence")]["FullDescription"]

    click.echo("\n" + "Question: " + question +
               "\n" + "Answer: " + answer +
               "\n" + "Score: " + str(similar_sentence_for_category.get("score")) + "\n")

    '''
    Predict Question and Answer from all Categories.
    Used for reference
    '''
    ss = SentenceSimilarity()
    similar_sentence = ss.find_similar_sentence(tokens_for_pred)
    click.echo("Predicted From all Categories: (USED FOR REFERENCE)")

    # get answer and question from tag
    data = pd.read_pickle('resources/processed_data/category_' + "all" + '.pkl')
    question = data.iloc[similar_sentence.get("sentence")]["Name"]
    answer = data.iloc[similar_sentence.get("sentence")]["FullDescription"]

    click.echo("\n" + "Question: " + question +
               "\n" + "Answer: " + answer +
               "\n" + "Score: " + str(similar_sentence.get("score")) + "\n")

if __name__ == "__main__":
    cli()


"""
    html-pdf-webservice

    Copyright 2014 Nathan Jones
    See LICENSE for more details
"""

import re
from locust import HttpLocust, TaskSet

wikipedia_main_page_html = "wikipedia-main-page.html"

def print_document(l, html_file_name):
    with open(html_file_name, "r") as html_file:
        l.client.post(
            url="/",
            files={
                "html": html_file
            }
        )

def print_simple_document(l):
    print_document(l, "sample.html")

def print_wikipedia_home_page(l):
    print_document(l, wikipedia_main_page_html)

def download_wikipedia_home_page(l):
    response = l.client.get("https://en.wikipedia.org/wiki/Main_Page")
    # External CSS and image links will be in scheme-relative form, such as
    # "//host/path". These URLs will break when the page is loaded with the
    # file:// scheme by wkhtmltopdf. Replace them with https:// URLs.
    html = re.sub('([^:])//', r'\1https://', response.text)
    with open(wikipedia_main_page_html, "w") as f:
        f.write(html.encode('utf8'))

class UserBehavior(TaskSet):
    tasks = {
        print_simple_document: 1,
        print_wikipedia_home_page: 1,
    }

    def on_start(self):
        download_wikipedia_home_page(self)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000


# nwes.daum.net 에서 JTBC 뉴스의 헤드라인, 간략뉴스를 추출해서
# jtbcnews 컬렉션에 저장
# 필드명 : news, desc

from bs4 import BeautifulSoup
import requests
from pymongo import MongoClient

url = 'https://media.daum.net/cp/310?regDate=20190502'

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
    AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}

res = requests.get(url, headers=headers)
html = BeautifulSoup(res.text, 'lxml')

news = []
desc = []

# 뉴스 헤드라인
for item in html.select('strong.tit_thumb a'):
    news.append(item.text.strip())
news.pop(); news.pop()

# 간략 뉴스
for item in html.select('div.desc_thumb span'):
    desc.append(item.text.strip())

# print(news)
# print(desc)

######################################################################

client = MongoClient('mongodb://192.168.180.133:27017')

db = client.bigdata
coll = db.jtbcnews

coll.remove({})

for i in range(len(news)):
    newOne = { 'news': news[i], 'desc': desc[i] }
    coll.insert_one(newOne)

client.close()

2. [Python_Codes] Fabio Ko's Python Programming (파이썬 프로그래밍)/mongo/04JTBC_news_mongo.py


"""
Created on 2011-02-10

@author: Bohdan Mushkevych
"""
import time, os, psutil

from system.repeat_timer import RepeatTimer
from settings import settings

class FootprintCalculator(object):
    def __init__(self):
        self.pid = os.getpid()

    def group(self, number):
        """ method formats number and inserts thousands separators """
        s = '%d' % number
        groups = []
        while s and s[-1].isdigit():
            groups.append(s[-3:])
            s = s[:-3]
        return s + '\''.join(reversed(groups))

    def get_snapshot_as_list(self):
        ps = psutil.Process(self.pid)
        return (self.group(ps.get_memory_info()[0]),
                self.group(ps.get_memory_info()[1]),
                '%02d' % ps.get_cpu_percent(),
                self.group(psutil.phymem_usage().free),
                self.group(psutil.virtmem_usage().free))

    def get_snapshot(self):
        resp = 'Footprint: RSS=%r VMS=%r CPU=%r; Available: PHYS=%r VIRT=%r' % self.get_snapshot_as_list()
        return resp

class WorkerPerformanceTicker(object):
    SECONDS_IN_24_HOURS = 86400
    TICKS_BETWEEN_FOOTPRINTS = 10

    def __init__(self, logger):
        self.logger = logger
        self.posts_per_24_hours = 0
        self.posts_per_tick = 0
        self.interval = settings['perf_ticker_interval']
        self.mark_24_hours = time.time()
        self.mark_footprint = time.time()
        self.footprint = FootprintCalculator()

    def start(self):
        self.timer = RepeatTimer(self.interval, self._run_tick_thread)
        self.timer.start()

    def cancel(self):
        self.timer.cancel()

    def _print_footprint(self):
        if time.time() - self.mark_footprint > self.TICKS_BETWEEN_FOOTPRINTS * self.interval:
            self.logger.info(self.footprint.get_snapshot())
            self.mark_footprint = time.time()

    def _run_tick_thread(self):
        self._print_footprint()
        self.logger.info('Processed: %d in last %d seconds; %d in last 24 hours;' \
                    % (self.posts_per_tick,
                       self.interval,
                       self.posts_per_24_hours))

        self.posts_per_tick = 0
        if time.time() - self.mark_24_hours > self.SECONDS_IN_24_HOURS:
            self.mark_24_hours = time.time()
            self.posts_per_24_hours = 0

    def increment(self):
        self.posts_per_tick += 1
        self.posts_per_24_hours += 1

class SessionPerformanceTicker(WorkerPerformanceTicker):

    def __init__(self, logger):
        super(SessionPerformanceTicker, self).__init__(logger)
        self.updates_per_24_hours = 0
        self.updates_per_tick = 0

    def _run_tick_thread(self):
        self._print_footprint()
        self.logger.info('Inserts/Updates. In last {0:d} seconds: {1:d}/{2:d}. In last 24 hours: {3:d}/{4:d};' \
                        .format(self.interval,
                                self.posts_per_tick,
                                self.updates_per_tick,
                                self.posts_per_24_hours,
                                self.updates_per_24_hours))

        self.posts_per_tick = 0
        self.updates_per_tick = 0
        if time.time() - self.mark_24_hours > self.SECONDS_IN_24_HOURS:
            self.mark_24_hours = time.time()
            self.posts_per_24_hours = 0
            self.updates_per_24_hours = 0

    def increment_insert(self):
        super(SessionPerformanceTicker, self).increment()

    def increment_update(self):
        self.updates_per_tick += 1
        self.updates_per_24_hours += 1

class AggregatorPerformanceTicker(WorkerPerformanceTicker):
    STATE_IDLE = 'state_idle'
    STATE_PROCESSING = 'state_processing'

    def __init__(self, logger):
        super(AggregatorPerformanceTicker, self).__init__(logger)
        self.state = self.STATE_IDLE
        self.posts_per_job = 0
        self.uow_obj = None
        self.state_triggered_at = time.time()

    def _run_tick_thread(self):
        self._print_footprint()
        if self.state == self.STATE_PROCESSING or self.posts_per_tick > 0 or self.posts_per_24_hours > 0:
            msg = 'State: %s for %d sec; processed: %d in %d sec. %d in this uow; %d in 24 hours;' \
                    % (self.state,
                       time.time() - self.state_triggered_at,
                       self.posts_per_tick,
                       self.interval,
                       self.posts_per_job,
                       self.posts_per_24_hours,)
        else:
            msg = 'State: %s for %d sec;' % (self.state, time.time() - self.state_triggered_at)
        self.logger.info(msg)

        self.posts_per_tick = 0
        if time.time() - self.mark_24_hours > self.SECONDS_IN_24_HOURS:
            self.mark_24_hours = time.time()
            self.posts_per_24_hours = 0

    def increment(self):
        self.posts_per_tick += 1
        self.posts_per_24_hours += 1
        self.posts_per_job += 1

    def start_uow(self, uow_obj):
        self.state = self.STATE_PROCESSING
        self.uow_obj = uow_obj
        self.state_triggered_at = time.time()

    def finish_uow(self):
        _id = self.uow_obj.get_document()['_id']
        self.logger.info('Success: unit_of_work %s in timeperiod %s; processed %d entries in %d seconds' \
                    % (_id,
                       self.uow_obj.get_timestamp(),
                       self.posts_per_job,
                       time.time() - self.state_triggered_at))
        self.cancel_uow()

    def cancel_uow(self):
        self.state = self.STATE_IDLE
        self.uow_obj = None
        self.state_triggered_at = time.time()
        self.posts_per_job = 0


"""
data.json - файл с данными о некоторых математиках прошлого.
1. Необходимо написать функцию, которая считает эти данные из файла. Параметр функции - имя файла.
2. Написать функцию сортировки данных по ФАМИЛИИ в поле "name" (у тех у кого она есть).
Например для Rene Descartes фамилия это Descartes, у Pierre de Fermat - Fermat и т.д.
Если фамилии нет, то использовать имя, например Euclid.
3. Написать функцию сортировки по дате смерти из поля "years".
Обратите внимание на сокращение BC. - это означает до н.э.
4. Написать функцию сортировки по количеству слов в поле "text"
"""
import json

# 1. Необходимо написать функцию, которая считает эти данные из файла. Параметр функции - имя файла.

def read_json(filename):
    with open(filename, "r") as json_file:
        data = json.load(json_file)
        return data

print(f'Json data: {read_json("data.json")}')

# 2. Написать функцию сортировки данных по ФАМИЛИИ в поле "name" (у тех у кого она есть).
# Если фамилии нет, то использовать имя, например Euclid.

def split_name(path):
    dicts_list = read_json(path)
    for dict in dicts_list:
        splited_name = dict['name'].split(' ')
        dict['name'] = splited_name
    return dicts_list

def sort_by_last_name(last_name_dict):
    last_name = last_name_dict['name']
    last_name = last_name[-1]
    first_name = last_name_dict['name']
    first_name = first_name[0]
    return last_name, first_name

def join_name(path):
    sorted_by_last_name_list = sorted(split_name(path), key=sort_by_last_name)
    for dict in sorted_by_last_name_list:
        joined_name = ' '.join(dict['name'])
        dict['name'] = joined_name
    return sorted_by_last_name_list

print(f'Sorted by last name: {join_name("data.json")}')

# 3. Написать функцию сортировки по дате смерти из поля "years".

def split_year(path):
    dicts_list = read_json(path)
    for dict in dicts_list:
        splited_years = dict['years'].split(' ')
        dict['years'] = splited_years
        if dict['years'][-1] == 'BC.':
            dict['years'][-2] = f'-{dict["years"][-2]}'
            dict['years'].pop(-1)
    return dicts_list

def sort_by_year(year_dict):
    year = float(year_dict['years'][-1])
    return year

def join_year(path):
    sorted_by_year_list = sorted(split_year(path), key=sort_by_year)
    for dict in sorted_by_year_list:
        if float(dict['years'][-1]) < 0:
            dict['years'][-1] = dict['years'][-1].replace('-', '')
            dict['years'].append('BC.')
        joined_year = ' '.join(dict['years'])
        dict['years'] = joined_year
    return sorted_by_year_list

print(f'Sorted by last year: {join_year("data.json")}')

# 4. Написать функцию сортировки по количеству слов в поле "text"

def split_text(path):
    dicts_list = read_json(path)
    for dict in dicts_list:
        splited_text = dict['text'].split(' ')
        dict['text'] = splited_text
    return dicts_list

def join_text(path):
    sorted_by_words_number_list = sorted(split_text(path), key=lambda x: len(x.get('text')))
    for dict in sorted_by_words_number_list:
        joined_text = ' '.join(dict['text'])
        dict['text'] = joined_text
    return sorted_by_words_number_list

print(f'Sorted by words number in text field: {join_text("data.json")}')


__author__ = 'alefur'
from PyQt5.QtWidgets import QGridLayout, QComboBox

from spsClient.widgets import Coordinates, ValueGB, ControlPanel, CommandsGB, CmdButton, DoubleSpinBoxGB, CustomedCmd, AbortButton

class CoordBoxes(QGridLayout):
    def __init__(self):
        QGridLayout.__init__(self)
        self.widgets = [DoubleSpinBoxGB('X', -10, 10, 5),
                        DoubleSpinBoxGB('Y', -10, 10, 5),
                        DoubleSpinBoxGB('Z', -10, 10, 5),
                        DoubleSpinBoxGB('U', -2, 2, 5),
                        DoubleSpinBoxGB('V', -2, 2, 5),
                        DoubleSpinBoxGB('W', -2, 2, 5)]

        for i, spinbox in enumerate(self.widgets):
            self.addWidget(spinbox, i // 3, i % 3)

class MoveCmd(CustomedCmd):
    def __init__(self, controlPanel):
        CustomedCmd.__init__(self, controlPanel=controlPanel, buttonLabel='MOVE')

        self.combo = QComboBox()
        self.combo.addItems(['absolute', 'relative'])
        self.combo.currentIndexChanged.connect(self.resetCoords)

        self.addWidget(self.combo, 0, 1)

    @property
    def spinboxes(self):
        return self.controlPanel.commands.coordBoxes.widgets

    def resetCoords(self, ind):
        if ind == 0:
            vals = [float(valueGB.value.text()) for valueGB in self.controlPanel.coordinates.widgets]
        else:
            vals = 6 * [0]

        for spinbox, val in zip(self.spinboxes, vals):
            spinbox.setValue(val)

    def buildCmd(self):
        labels = ['X', 'Y', 'Z', 'U', 'V', 'W']
        values = [spinbox.getValue() for spinbox in self.spinboxes]

        cmdStr = '%s slit move %s ' % (self.controlPanel.actorName, self.combo.currentText())
        cmdStr += (" ".join(['%s=%.5f' % (label, value) for label, value in zip(labels, values)]))

        return cmdStr

class SetRepCmd(CustomedCmd):
    def __init__(self, controlPanel):
        CustomedCmd.__init__(self, controlPanel=controlPanel, buttonLabel='SET')

        self.combo = QComboBox()
        self.combo.addItems(['work', 'tool'])
        self.combo.currentIndexChanged.connect(self.resetCoords)

        self.addWidget(self.combo, 0, 1)

    def resetCoords(self, ind):
        for spinbox in self.spinboxes:
            spinbox.setValue(0)

    @property
    def spinboxes(self):
        return self.controlPanel.commands.coordBoxes.widgets

    def buildCmd(self):
        labels = ['X', 'Y', 'Z', 'U', 'V', 'W']
        values = [spinbox.getValue() for spinbox in self.spinboxes]

        cmdStr = '%s slit set %s ' % (self.controlPanel.actorName, self.combo.currentText().lower())
        cmdStr += (" ".join(['%s=%.5f' % (label, value) for label, value in zip(labels, values)]))

        return cmdStr

class SlitCommands(CommandsGB):
    def __init__(self, controlPanel):
        CommandsGB.__init__(self, controlPanel)
        self.statusButton = CmdButton(controlPanel=controlPanel, label='STATUS',
                                      cmdStr='%s slit status' % controlPanel.actorName)
        self.connectButton = CmdButton(controlPanel=controlPanel, label='CONNECT',
                                       cmdStr='%s connect controller=slit' % controlPanel.actorName)
        self.initButton = CmdButton(controlPanel=controlPanel, label='INIT',
                                    cmdStr='%s slit init' % controlPanel.actorName)
        self.abortButton = AbortButton(controlPanel=controlPanel, cmdStr='%s slit abort' % controlPanel.actorName)
        self.goHomeButton = CmdButton(controlPanel=controlPanel, label='GO HOME',
                                      cmdStr='%s slit move home' % controlPanel.actorName)
        self.coordBoxes = CoordBoxes()

        self.moveCmd = MoveCmd(controlPanel=controlPanel)
        self.setRepCmd = SetRepCmd(controlPanel=controlPanel)

        self.grid.addWidget(self.statusButton, 0, 0)
        self.grid.addWidget(self.connectButton, 0, 1)
        self.grid.addWidget(self.initButton, 1, 0)
        self.grid.addWidget(self.abortButton, 1, 1)
        self.grid.addLayout(self.coordBoxes, 2, 0, 2, 3)
        self.grid.addLayout(self.moveCmd, 4, 0, 1, 2)
        self.grid.addLayout(self.setRepCmd, 5, 0, 1, 2)
        self.grid.addWidget(self.goHomeButton, 6, 0, 1, 1)

    @property
    def buttons(self):
        return [self.statusButton, self.connectButton, self.initButton, self.abortButton,
                self.goHomeButton, self.moveCmd.button, self.setRepCmd.button]

class SlitPanel(ControlPanel):
    def __init__(self, controlDialog):
        ControlPanel.__init__(self, controlDialog)

        self.mode = ValueGB(self.moduleRow, 'slitMode', 'Mode', 0, '{:s}')
        self.state = ValueGB(self.moduleRow, 'slitFSM', '', 0, '{:s}')
        self.substate = ValueGB(self.moduleRow, 'slitFSM', '', 1, '{:s}')
        self.info = ValueGB(self.moduleRow, 'slitInfo', 'Info', 0, '{:s}')
        self.location = ValueGB(self.moduleRow, 'slitLocation', 'Location', 0, '{:s}')

        self.coordinates = Coordinates(self.moduleRow, 'slit', title='Position')
        self.work = Coordinates(self.moduleRow, 'slitWork', title='Work')
        self.tool = Coordinates(self.moduleRow, 'slitTool', title='Tool')

        self.commands = SlitCommands(self)

        self.grid.addWidget(self.mode, 0, 0)
        self.grid.addWidget(self.state, 0, 1)
        self.grid.addWidget(self.substate, 0, 2)
        self.grid.addWidget(self.location, 0, 3)

        self.grid.addWidget(self.info, 1, 0, 1, 6)
        self.grid.addWidget(self.coordinates, 2, 0, 1, 6)
        self.grid.addWidget(self.work, 3, 0, 1, 6)
        self.grid.addWidget(self.tool, 4, 0, 1, 6)

        self.grid.addWidget(self.commands, 0, 7, 5, 4)

    @property
    def customWidgets(self):
        return self.coordinates.widgets + self.work.widgets + self.tool.widgets


# -*- coding: utf-8 -*-
"""
Created on Mon Feb 25 11:47:36 2019

@author: harshvardhan
"""

import urllib.request
import requests

cityName = input("Enter the city name please: ")

url = "http://api.openweathermap.org/data/2.5/weather?q="+cityName+"&appid=e9185b28e9969fb7a300801eb026de9c"
resp = urllib.request.urlopen(url)   # GET request to REST API
print(resp.read())

print(" * "*20)

resp = requests.get(url)
print (resp.text)


import numpy as np
from scipy.stats import rankdata
from functools import partial
from nptyping import Array
from sklearn.metrics import pairwise_distances
from sklearn.base import BaseEstimator, TransformerMixin

class BoostedSURF(BaseEstimator, TransformerMixin):

    """sklearn compatible implementation of the boostedSURF algorithm

    Gediminas Bertasius, Delaney Granizo-Mackenzie, Ryan Urbanowicz, Jason H. Moore.
    Boosted Spatially Uniform ReliefF Algorithm for Genome-Wide Genetic Analysis.

    author: Jernej Vivod

    """

    def __init__(self, n_features_to_select=10, phi=5,
            dist_func=lambda w, x1, x2: np.sum(w*np.logical_xor(x1, x2), 1), learned_metric_func=None):

        self.n_features_to_select = n_features_to_select  # Number of features to select.
        self.phi = phi                                    # the phi parameter (update weights when iteration_counter mod phi == 0)
        self.dist_func = dist_func                        # Distance function to use.
        self.learned_metric_func = learned_metric_func    # learned metric function.

    def fit(self, data, target):

        """
        Rank features using BoostedSURF feature selection algorithm

        Args:
            data : Array[np.float64] -- matrix of examples
            target : Array[np.int] -- vector of target values of examples

        Returns:
            self
        """

        # Run Boosted SURF feature selection algorithm.
        if self.learned_metric_func != None:
            self.rank, self.weights = self._boostedSURF(data, target, self.phi, self.dist_func,
                    learned_metric_func=self.learned_metric_func(data, target))
        else:
            self.rank, self.weights = self._boostedSURF(data, target, self.phi, self.dist_func)

        return self

    def transform(self, data):

        """
        Perform feature selection using computed feature ranks

        Args:
            data : Array[np.float64] -- matrix of examples on which to perform feature selection

        Returns:
            Array[np.float64] -- result of performing feature selection
        """

        msk = self.rank <= self.n_features_to_select  # Compute mask.
        return data[:, msk]  # Perform feature selection.

    def fit_transform(self, data, target):

        """
        Compute ranks of features and perform feature selection
        Args:
            data : Array[np.float64] -- matrix of examples on which to perform feature selection
            target : Array[np.int] -- vector of target values of examples

        Returns:
            Array[np.float64] -- result of performing feature selection
        """

        self.fit(data, target)
        return self.transform(data)

    def _boostedSURF(self, data, target, phi, dist_func, **kwargs):

        """Compute feature scores using boostedSURF algorithm

        Args:
            data : Array[np.float64] -- Matrix containing examples' data as rows
            target : Array[np.int] -- matrix containing the example's target variable value
            phi: int -- parameter specifying number of iterations before recomputing distance weights
            dist_func : Callable[[Array[np.float64], Array[np.float64]], Array[np.float64]] -- function for evaluating
            distances between examples. The function should acept two examples or two matrices of examples and return the dictances.
            **kwargs: can contain argument with key 'learned_metric_func' that maps to a function that accepts a distance
            function and indices of two training examples and returns the distance between the examples in the learned
            metric space.

        Returns:
            Array[np.int], Array[np.float64] -- Array of feature enumerations based on the scores, array of feature scores

        """

        # Initialize distance weights.
        dist_weights = np.ones(data.shape[1], dtype=np.float)

        # weighted distance function
        dist_func_w = partial(dist_func, dist_weights)

        # Initialize weights.
        weights = np.zeros(data.shape[1], dtype=np.int)

        for idx in np.arange(data.shape[0]):

            # Recompute distance matrix.
            if np.mod(idx, phi) == 0:
                dist_weights = np.maximum(weights, np.ones(data.shape[1], dtype=np.float))
                dist_func_w = partial(dist_func, dist_weights)
                if 'learned_metric_func' in kwargs:
                    dist_func_w_learned = partial(kwargs['learned_metric_func'], dist_func_w)

            # Compute distances from current examples to all other examples.
            if 'learned_metric_func' in kwargs:
                dists = dist_func_w_learned(idx, np.arange(data.shape[0]))
            else:
                dists = dist_func_w(data[idx, :], data)

            # Compute mean and standard deviation of distances and set thresholds.
            t_next = np.mean(dists[np.arange(data.shape[0]) != idx])
            sigma_nxt = np.std(dists[np.arange(data.shape[0]) != idx])
            thresh_near = t_next - sigma_nxt/2.0
            thresh_far = t_next + sigma_nxt/2.0

            # Get mask of examples that are close.
            msk_close = dists < thresh_near
            msk_close[idx] = False

            # Get mask of examples that are far.
            msk_far = dists > thresh_far

            # Get examples that are close.
            examples_close = data[msk_close, :]
            target_close = target[msk_close]

            # Get examples that are far.
            examples_far = data[msk_far, :]
            target_far = target[msk_far]

            # Get considered features of close examples.
            features_close = data[idx, :] != examples_close
            # Get considered features of far examples.
            features_far = data[idx, :] == examples_far

            # Get mask for close examples with same class.
            msk_same_close = target_close == target[idx]

            # Get mask for far examples with same class.
            msk_same_far = target_far == target[idx]

            ### WEIGHTS UPDATE ###

            # Get penalty weights update values for close examples.
            wu_close_penalty = np.sum(features_close[msk_same_close, :], 0)
            # Get reward weights update values for close examples.
            wu_close_reward = np.sum(features_close[np.logical_not(msk_same_close), :], 0)

            # Get penalty weights update values for far examples.
            wu_far_penalty = np.sum(features_far[msk_same_far, :], 0)
            # Get reward weights update values for close examples.
            wu_far_reward = np.sum(features_far[np.logical_not(msk_same_far), :], 0)

            # Update weights.
            weights = weights - (wu_close_penalty + wu_far_penalty) + (wu_close_reward + wu_far_reward)

            ### /WEIGHTS UPDATE ###

        # Create array of feature enumerations based on score.
        rank = rankdata(-weights, method='ordinal')
        return rank, weights


import numpy as np
import pytest

from pandas.core.dtypes.common import is_float_dtype

from pandas import isna

class TestSetValue:
    def test_set_value(self, float_frame):
        for idx in float_frame.index:
            for col in float_frame.columns:
                float_frame._set_value(idx, col, 1)
                assert float_frame[col][idx] == 1

    def test_set_value_resize(self, float_frame):

        res = float_frame._set_value("foobar", "B", 0)
        assert res is None
        assert float_frame.index[-1] == "foobar"
        assert float_frame._get_value("foobar", "B") == 0

        float_frame.loc["foobar", "qux"] = 0
        assert float_frame._get_value("foobar", "qux") == 0

        res = float_frame.copy()
        res._set_value("foobar", "baz", "sam")
        assert res["baz"].dtype == np.object_

        res = float_frame.copy()
        res._set_value("foobar", "baz", True)
        assert res["baz"].dtype == np.object_

        res = float_frame.copy()
        res._set_value("foobar", "baz", 5)
        assert is_float_dtype(res["baz"])
        assert isna(res["baz"].drop(["foobar"])).all()
        msg = "could not convert string to float: 'sam'"
        with pytest.raises(ValueError, match=msg):
            res._set_value("foobar", "baz", "sam")


# coding: utf-8

from flask import Blueprint, render_template, redirect, url_for, request
from apps.blog.models import Post
from apps.blog.admins.forms import PostForm
from auth import admin_required

mod = Blueprint(
    'admins.blog',
    __name__,
    url_prefix='/admins/blog',
    template_folder='templates'
)

@admin_required
@mod.route('/')
def index():
    posts = Post.query().order(-Post.created)
    return render_template(
        'blog/admins/index.html',
        posts=posts
    )

@admin_required
@mod.route('/post/new/', methods=['GET', 'POST'])
def post_new():
    form = PostForm()
    if form.validate_on_submit():
        post = Post()
        form.populate_obj(post)
        post.put()
        return redirect(url_for('admins.blog.index'))
    return render_template(
        'blog/admins/post_new.html',
        form=form
    )

@admin_required
@mod.route('/post/<int:post_id>/', methods=['GET', 'POST'])
def post(post_id):
    post = Post.get_by_id(post_id)
    if not post:
        return redirect(url_for('admins.blog.index'))
    form = PostForm(obj=post)
    if request.method == 'POST' and 'delete' in request.form:
        post.key.delete()
        return redirect(url_for('admins.blog.index'))
    if form.validate_on_submit():
        form.populate_obj(post)
        post.put()
        return redirect(url_for('admins.blog.index'))
    return render_template(
        'blog/admins/post.html',
        form=form
    )


import os
import random

from flask import (render_template, send_from_directory, request, abort,
    redirect)
from flask.helpers import safe_join
from flask_wtf.csrf import CSRFError
from werkzeug.exceptions import NotFound

from . import app, tryton, cache, util
from .compat import HTTPStatus

Product = tryton.pool.get('product.product')
PriceList = tryton.pool.get('product.price_list')

@app.errorhandler(CSRFError)
@tryton.transaction()
def handle_csrf_error(e):
    return render_template('csrf_error.html', reason=e.description), 400

@app.route('/robots.txt')
@cache.cached(timeout=24 * 60 * 60)
@tryton.transaction()
def static_from_root():
    return render_template('robots.txt')

@app.route('/images/<type>/<code>/<size>/<name>.jpg')
def static_image(type, code, name, size):
    index = int(request.args.get('index', 0))
    if type not in {'product', 'category'}:
        abort(HTTPStatus.NOT_FOUND)
    try:
        return send_from_directory(
            app.static_folder,
            '%s-images/%s/%s%s.jpg' % (type, size, code, index or ''))
    except NotFound:
        if not index:
            if type == 'product':
                return redirect(
                    util.static_url_for(
                        'static', filename='images/default-%s.jpg' % size))
            else:
                return redirect(
                    util.static_url_for(
                        'static',
                        filename='images/all-categories-%s.jpg' % size))
        else:
            return ''

@app.route('/static/product-images/<name>')
@app.route('/static/category-images/<name>')
def images(name):
    abort(HTTPStatus.FORBIDDEN)

if not app.debug:

    @app.url_defaults
    def hashed_url_for_image_file(endpoint, values):
        if endpoint == 'static_image':
            type = values.get('type')
            code = values.get('code')
            size = values.get('size')
            index = values.get('index')
            if type and code and size:
                filename = '%s-images/%s/%s%s.jpg' % (
                    type, size, code, index or '')
                filepath = safe_join(app.static_folder, filename)
                try:
                    values['t'] = int(os.path.getmtime(filepath))
                except OSError:
                    pass

@app.route('/_warmup')
@tryton.transaction()
def warmup():
    util.get_shipping_distribution()
    products = Product.search([
            ('salable', '=', True),
            ('type', '=', 'goods'),
            ('webshop_categories', '!=', None),
            ], order=[])
    products = Product.browse(
        random.sample(products, 20))
    util.get_all_sale_prices(products)
    return '', HTTPStatus.NO_CONTENT


import json
import os

def loadjsondata(filename):
    path = os.getcwd()
    with open(filename) as data_file:
        data = json.load(data_file)
    return data

ENV_FILE = os.getcwd()+'/env.json'
env = loadjsondata(ENV_FILE)


import ndimage as ndi
import numpy
import math
from scipy._lib.six import string_types

def gaussianFilter(input, sigma = 4, order=0, output=None,
                  mode="reflect", cval=0.0, truncate=4.0):
    """Multidimensional Gaussian filter.

    Parameters
    ----------
    %(input)s
    sigma : scalar or sequence of scalars
        Standard deviation for Gaussian kernel. The standard
        deviations of the Gaussian filter are given for each axis as a
        sequence, or as a single number, in which case it is equal for
        all axes.
    order : {0, 1, 2, 3} or sequence from same set, optional
        The order of the filter along each axis is given as a sequence
        of integers, or as a single number.  An order of 0 corresponds
        to convolution with a Gaussian kernel. An order of 1, 2, or 3
        corresponds to convolution with the first, second or third
        derivatives of a Gaussian. Higher order derivatives are not
        implemented
    %(output)s
    %(mode)s
    %(cval)s
    truncate : float
        Truncate the filter at this many standard deviations.
        Default is 4.0.

    Returns
    -------
    gaussian_filter : ndarray
        Returned array of same shape as `input`.

    Notes
    -----
    The multidimensional filter is implemented as a sequence of
    one-dimensional convolution filters. The intermediate arrays are
    stored in the same data type as the output. Therefore, for output
    types with a limited precision, the results may be imprecise
    because intermediate results may be stored with insufficient
    precision.

    """
    input = numpy.asarray(input)
    output, return_value = _get_output(output, input)
    orders = _normalize_sequence(order, input.ndim)
    if not set(orders).issubset(set(range(4))):
        raise ValueError('Order outside 0..4 not implemented')
    sigmas = _normalize_sequence(sigma, input.ndim)
    axes = list(range(input.ndim))
    axes = [(axes[ii], sigmas[ii], orders[ii])
                        for ii in range(len(axes)) if sigmas[ii] > 1e-15]
    if len(axes) > 0:
        for axis, sigma, order in axes:
            gaussian_filter1d(input, sigma, axis, order, output,
                              mode, cval, truncate)
            input = output
    else:
        output[...] = input[...]
    return return_value

def gaussian_filter1d(input, sigma, axis=-1, order=0, output=None,
                      mode="reflect", cval=0.0, truncate=4.0):
    """One-dimensional Gaussian filter.

    Parameters
    ----------
    %(input)s
    sigma : scalar
        standard deviation for Gaussian kernel
    %(axis)s
    order : {0, 1, 2, 3}, optional
        An order of 0 corresponds to convolution with a Gaussian
        kernel. An order of 1, 2, or 3 corresponds to convolution with
        the first, second or third derivatives of a Gaussian. Higher
        order derivatives are not implemented
    %(output)s
    %(mode)s
    %(cval)s
    truncate : float, optional
        Truncate the filter at this many standard deviations.
        Default is 4.0.

    Returns
    -------
    gaussian_filter1d : ndarray

    """
    if order not in range(4):
        raise ValueError('Order outside 0..3 not implemented')
    sd = float(sigma)
    # make the radius of the filter equal to truncate standard deviations
    lw = int(truncate * sd + 0.5)
    weights = [0.0] * (2 * lw + 1)
    weights[lw] = 1.0
    sum = 1.0
    sd = sd * sd
    # calculate the kernel:
    for ii in range(1, lw + 1):
        tmp = math.exp(-0.5 * float(ii * ii) / sd)
        weights[lw + ii] = tmp
        weights[lw - ii] = tmp
        sum += 2.0 * tmp
    for ii in range(2 * lw + 1):
        weights[ii] /= sum
    # implement first, second and third order derivatives:
    if order == 1:  # first derivative
        weights[lw] = 0.0
        for ii in range(1, lw + 1):
            x = float(ii)
            tmp = -x / sd * weights[lw + ii]
            weights[lw + ii] = -tmp
            weights[lw - ii] = tmp
    elif order == 2:  # second derivative
        weights[lw] *= -1.0 / sd
        for ii in range(1, lw + 1):
            x = float(ii)
            tmp = (x * x / sd - 1.0) * weights[lw + ii] / sd
            weights[lw + ii] = tmp
            weights[lw - ii] = tmp
    elif order == 3:  # third derivative
        weights[lw] = 0.0
        sd2 = sd * sd
        for ii in range(1, lw + 1):
            x = float(ii)
            tmp = (3.0 - x * x / sd) * x * weights[lw + ii] / sd2
            weights[lw + ii] = -tmp
            weights[lw - ii] = tmp
    return ndi.correlate1d(input, weights, axis, output, mode, cval, 0)

def medianFilter(input, size=3, footprint=None, output=None,
                  mode="reflect", cval=0.0, origin=0):
    """
    Calculates a multidimensional median filter.

    Parameters
    ----------
    %(input)s
    %(size_foot)s
    %(output)s
    %(mode)s
    %(cval)s
    %(origin)s

    Returns
    -------
    median_filter : ndarray
        Return of same shape as `input`.

    """
    return ndi._rank_filter(input, 0, size, footprint, output, mode, cval,
                        origin, 'median')

def _normalize_sequence(input, rank, array_type=None):
    """If input is a scalar, create a sequence of length equal to the
    rank by duplicating the input. If input is a sequence,
    check if its length is equal to the length of array.
    """
    if hasattr(input, '__iter__'):
        normalized = list(input)
        if len(normalized) != rank:
            err = "sequence argument must have length equal to input rank"
            raise RuntimeError(err)
    else:
        normalized = [input] * rank
    return normalized

def _get_output(output, input, shape=None):
    if shape is None:
        shape = input.shape
    if output is None:
        output = numpy.zeros(shape, dtype=input.dtype.name)
        return_value = output
    elif type(output) in [type(type), type(numpy.zeros((4,)).dtype)]:
        output = numpy.zeros(shape, dtype=output)
        return_value = output
    elif type(output) in string_types:
        output = numpy.typeDict[output]
        output = numpy.zeros(shape, dtype=output)
        return_value = output
    else:
        if output.shape != shape:
            raise RuntimeError("output shape not correct")
        return_value = None
    return output, return_value

def _check_axis(axis, rank):
    if axis < 0:
        axis += rank
    if axis < 0 or axis >= rank:
        raise ValueError('invalid axis')
    return axis

def _extend_mode_to_code(mode):
    """Convert an extension mode to the corresponding integer code.
    """
    if mode == 'nearest':
        return 0
    elif mode == 'wrap':
        return 1
    elif mode == 'reflect':
        return 2
    elif mode == 'mirror':
        return 3
    elif mode == 'constant':
        return 4
    else:
        raise RuntimeError('boundary mode not supported')


# Import pour lecture et utilisation de la mediane
import numpy as np
# Lecture des donnees
t,u = np.loadtxt('ouverture_appareil_photo.txt',unpack=True,skiprows=1)

# Pour que le dessin donne quelque chose
t_propre = [1,2,3]
u_propre = [1,3,1]

# Avant de pouvoir appliquer le meme principe que pour le flash, il faut
# proprifier votre signal (stocker les valeurs correspondantes dans les listes
# t_propre et u_propre).

# Le code suivant vous permet de visualiser si votre procede de nettoyage
# marche correctement. Vous aurez dans le dossier un fichier image
# ouverture_propre.png qui devrait se creer.

import matplotlib.pyplot as plt

plt.plot(t_propre,u_propre)
plt.savefig('ouverture_propre.png')

# Ne reste plus qu'a proceder comme pour le flash pour obtenir le temps total
# d'ouverture

temps_ouverture = 'pas ca...'

# Affichage du resultat
print(temps_ouverture)


from ..core import *
from .models import Darknet
from .utils.utils import *
from .utils.datasets import VideoDataset
from torchvision.transforms.transforms import ToTensor
from imageio import get_reader
from torch.utils.data import DataLoader
from matplotlib.ticker import NullLocator
import matplotlib
# matplotlib.use('agg')
import random
from PIL import Image
import pkg_resources

from . import config
from . import weights

class VideoPipeline:
    def __init__(self, img_size=416, batch_sz=8, nms_thres=0.4, conf_thres=0.8, device=None, n_cpu=0):
        """
        Paramaters
        ----------
        img_size:       Size to resize each frame to before inference
        batch_sz:       Batch Size for batch processing
        nms_thres:      IOU Threshold for Non Maximum Supression
        conf_thres:     Confedence Threshold for Non Max Supression
        device:         device to use eg. torch.device('cuda:0')
        """
        self.img_size = img_size
        self.n_cpu = n_cpu

        self.config_path = pkg_resources.resource_filename(
                            config.__name__, 'yolov3.cfg')

        self.weights_path = pkg_resources.resource_filename(
                            weights.__name__, 'yolov3.weights')

        self.classes_path = pkg_resources.resource_filename(
                            config.__name__, 'coco.names')

        self.classes = load_classes(self.classes_path)
        self.batch_sz = batch_sz
        self.nms_thres,self.conf_thres = nms_thres,conf_thres

        # Set up model
        self.model = Darknet(self.config_path, img_size=self.img_size)
        self.model.load_weights(self.weights_path)
        if device is None: device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.model.to(device)
        self.model.eval()

    def vid2vid(self, source_path, dest_folder):
        """
        Paramaters
        -----------
        source_path:    Path of the source video for object detection
        dest_path:      Path of the final video woth bounding boxes

        Returns
        ------------
        dest_path
        """

        assert not os.path.isfile(dest_folder), "dest_folder should be a directory not a file"

        os.makedirs(dest_folder, exist_ok=True)
        dest_filename = os.path.split(source_path)[1]
        dest_filename = os.path.join(dest_folder, dest_filename)

        classes = self.classes
        img_detections = []

        dataloader = DataLoader(
        VideoDataset(source_path, img_size=self.img_size),
        batch_size=self.batch_sz,
        shuffle=False,
        num_workers=self.n_cpu,
        )
        model = self.model
        # Inference
        for imgs in tqdm.tqdm(dataloader, leave=False):
            with torch.no_grad():
                imgs = imgs.float().to(device)
                detections = model(imgs)
                detections = non_max_suppression(detections, self.conf_thres, self.nms_thres)
            img_detections.extend(detections)

        # Bounding-box colors
        cmap = plt.get_cmap('tab20b')
        colors = [cmap(i) for i in np.linspace(0, 1, 20)]

        video_writer = imageio.get_writer(dest_filename, fps=25)
        for i, img in tqdm.tqdm(enumerate(get_reader(source_path)), leave=False):
            detections = img_detections[i]
            fig, ax = plt.subplots(1)
            ax.imshow(img)

            size = self.img_size

            if detections is not None:
                # Rescale boxes to original image
                detections = rescale_boxes(detections, self.img_size, img.shape[:2])
                unique_labels = detections[:, -1].cpu().unique()
                n_cls_preds = len(unique_labels)
                bbox_colors = random.sample(colors, n_cls_preds)
                for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections:

                    box_w = x2 - x1
                    box_h = y2 - y1

                    color = bbox_colors[int(np.where(unique_labels == int(cls_pred))[0])]
                    # Create a Rectangle patch
                    bbox = patches.Rectangle((x1, y1), box_w, box_h, linewidth=2, edgecolor=color, facecolor="none")
                    # Add the bbox to the plot
                    ax.add_patch(bbox)
                    # Add label
                    plt.text(
                        x1,
                        y1,
                        s=classes[int(cls_pred)],
                        color="white",
                        verticalalignment="top",
                        bbox={"color": color, "pad": 0},
                    )

            # Save generated image with detections
            plt.axis("off")
            plt.gca().xaxis.set_major_locator(NullLocator())
            plt.gca().yaxis.set_major_locator(NullLocator())
            fig.tight_layout(pad=0)
            fig.canvas.draw()
            data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
            data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
            video_writer.append_data(data)
            plt.close()
            # filename = os.path.join(dest_path, 'tmp', str(i).zfill(3))+'.png'
            # plt.savefig(filename, bbox_inches="tight", pad_inches=0.0)
            # plt.close()

        video_writer.close()
        with open(f"{dest_filename[:-3]}_predictions", "wb") as f:
            pickle.dump(img_detections, f)
        return dest_filename


import face_recognition
import numpy as np
import cv2
import urllib
import io

def url_to_image(url):
	# download the image, convert it to a NumPy array, and then read
	# it into OpenCV format
	resp = urllib.request.urlopen(url)
	image = np.asarray(bytearray(resp.read()), dtype="uint8")
	image = cv2.imdecode(image, cv2.IMREAD_COLOR)

	# return the image
	return image

def compareFaces(base_photo, target_photo):

    bse_image = face_recognition.load_image_file(base_photo)

    try:
        bse_encoding = face_recognition.face_encodings(bse_image)[0]
        tgt_encoding = face_recognition.face_encodings(url_to_image(target_photo))[0]
    except:
        return (-1)

    known_encodings = [bse_encoding]

    face_distances = face_recognition.face_distance(known_encodings, tgt_encoding)

    return face_distances[0]


import json
import os
import stripe

from flask import Flask, render_template, jsonify, request
from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())
stripe.api_key = os.getenv('STRIPE_SECRET_KEY')
stripe.api_version = os.getenv('STRIPE_API_VERSION')

app = Flask(__name__, template_folder='client')

@app.route('/')
def order():
    return render_template('order.html')

@app.route('/order_success')
def success():
    return render_template('order_success.html')

@app.route('/config', methods=['GET'])
def get_public_key():
    return jsonify({
      'publicKey': os.getenv('STRIPE_PUBLIC_KEY'),
      'basePrice': os.getenv('BASE_PRICE'),
      'currency': os.getenv('CURRENCY')
    })

@app.route('/checkout-session', methods=['GET'])
def get_checkout_session():
    id = request.args.get('sessionId')
    checkout_session = stripe.checkout.Session.retrieve(id)
    return jsonify(checkout_session)

@app.route('/create-checkout-session', methods=['POST'])
def create_checkout_session():
    data = json.loads(request.data)
    domain_url = os.getenv('DOMAIN')

    try:
        checkout_session = stripe.checkout.Session.create(
            success_url=domain_url + "/order_success?session_id={CHECKOUT_SESSION_ID}",
            cancel_url=domain_url + "/",
            payment_method_types=["card"],
            line_items=[
                {
                    "name": data['name'],
                    "images": [domain_url + data['img']],
                    "quantity": data['quantity'],
                    "currency": data['currency'],
                    "amount": int(data['amount'])
                }
            ]
        )
        return jsonify({'sessionId': checkout_session['id']})
    except Exception as e:
        return jsonify(e), 40

@app.route('/webhook', methods=['POST'])
def webhook_received():
    webhook_secret = os.getenv('STRIPE_WEBHOOK_SECRET')
    request_data = json.loads(request.data)

    if webhook_secret:
        signature = request.headers.get('stripe-signature')
        try:
            event = stripe.Webhook.construct_event(
                payload=request.data, sig_header=signature, secret=webhook_secret)
            data = event['data']
        except Exception as e:
            return e
        event_type = event['type']
    else:
        data = request_data['data']
        event_type = request_data['type']
    data_object = data['object']

    if event_type == 'payment_intent.failed' or \
        data_object['status'] != 'succeeded':
            return jsonify({
                'status': 'failed',
                'amount_received': None,
            }), 200

    return jsonify({
                'status': 'succeeded',
                'amount_received': data_object['amount_received'],
            }), 200

if __name__ == '__main__':
    app.run()


#!/usr/bin/env python2
import sys
import time

from argparse import ArgumentParser

import yaml
import logging

from certification_tool import fuel_rest_api
from certification_tool.main import login

fuel_rest_api.set_logger(logging.getLogger())
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def parse_command_line(argv):
    parser = ArgumentParser("usage: %prog [options]")

    parser.add_argument('-a', '--auth',
                        help='keystone credentials in format '
                             'tenant_name:username:password',
                        dest="auth", default='admin:admin:admin')

    parser.add_argument('-u', '--fuelurl', help="fuel rest url",
                        dest='fuelurl', required=True)

    parser.add_argument('config_file',
                        help='yaml configuration file',
                        metavar="CLUSTER_CONFIG")

    return parser.parse_args(argv)

def match_nodes(conn, cluster):
    controller_count = int(cluster['controller'])
    compute_nodes = int(cluster['compute'])
    cinder = int(cluster.get('cinder', 0))
    ceph_osd = int(cluster.get('ceph_osd', 0))

    min_nodes = ceph_osd + cinder + compute_nodes + controller_count

    while True:
        nodes = [node for node in fuel_rest_api.get_all_nodes(conn)
                 if node.cluster is None]

        if len(nodes) < min_nodes:
            templ = "Waiting till {} nodes will be available"
            logger.info(templ.format(min_nodes))
            time.sleep(10)
            continue

        if len(nodes) <= 1:
            raise ValueError("Nodes amount should be not less, than 2")

        cpu_disk = []
        for node in nodes:
            info = node.get_info()

            cpu_count = int(info['meta']['cpu']['real'])
            disk_size = 0

            for disk in info['meta']['disks']:
                disk_size += int(disk['size'])

            cpu_disk.append((disk_size, cpu_count, node))

        cpu_disk.sort()
        nodes = [tpl[2] for tpl in cpu_disk]

        for i, node in enumerate(nodes[:controller_count]):
            descr = {'roles': ["controller"],
                     'name': "controller_{}".format(i)}
            yield (descr, node)
        nodes = nodes[controller_count:]

        for i, node in enumerate(nodes[:cinder]):
            descr = {'roles': ["cinder"],
                     'name': "cinder_{}".format(i)}
            yield (descr, node)
        nodes = nodes[cinder:]

        for i, node in enumerate(nodes[:ceph_osd]):
            descr = {'roles': ["ceph-osd"],
                     'name': "ceph_{}".format(i)}
            yield (descr, node)
        nodes = nodes[ceph_osd:]

        for i, node in enumerate(nodes):
            descr = {'roles': ["compute"],
                     'name': "compute_{}".format(i)}
            yield (descr, node)

        break

def str2ip_range(ip_str):
    ip1, ip2 = ip_str.split("-")
    return [ip1.strip(), ip2.strip()]

def set_networks_params(cluster, net_settings):
    configuration = cluster.get_networks()
    curr_config = configuration['networking_parameters']

    if 'floating' in net_settings:
        curr_config['floating_ranges'] = \
            [str2ip_range(net_settings['floating'])]

    if 'public' in net_settings:
        pub_settings = net_settings['public']
        for net in configuration['networks']:
            if net['name'] == 'public':

                if 'ip_ranges' in pub_settings:
                    ip_range = str2ip_range(pub_settings['ip_ranges'])
                    net['ip_ranges'] = [ip_range]

                if 'cidr' in pub_settings:
                    net['cidr'] = pub_settings['cidr']

                if 'gateway' in pub_settings:
                    net['gateway'] = pub_settings['gateway']

    cluster.configure_networks(**configuration)

def create_cluster(conn, cluster):

    nodes_iter = match_nodes(conn, cluster['nodes'])

    cluster_obj = fuel_rest_api.create_empty_cluster(conn, cluster)

    if 'network' in cluster:
        set_networks_params(cluster_obj, cluster['network'])

    for node_desc, node in nodes_iter:
        node.set_node_name(node_desc['name'])
        cluster_obj.add_node(node, node_desc['roles'])

def main(argv):
    args = parse_command_line(argv)
    conn = login(args.fuelurl, args.auth)
    cluster = yaml.load(open(args.config_file).read())

    for cluster_obj in fuel_rest_api.get_all_clusters(conn):
        if cluster_obj.name == cluster['name']:
            cluster_obj.delete()
            wd = fuel_rest_api.with_timeout(60, "Wait cluster deleted")
            wd(lambda co: not co.check_exists())(cluster_obj)

    create_cluster(conn, cluster)

if __name__ == "__main__":
    exit(main(sys.argv[1:]))


# In the name of God
# -*- coding: utf-8 -*-
# Windows and Panels main Frame File
#! /usr/bin/env python

import wx
import wx.aui as wxaui
from Config.Init import *

import GUI.proman as pro

_ = wx.GetTranslation

class BGPanel(wx.Panel):
	def __init__( self,parent,BGfile ):
		wx.Panel.__init__ ( self, parent=parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.TAB_TRAVERSAL )

		#bSizer2 = wx.BoxSizer( wx.VERTICAL )

		self.BGfile = BGfile

		self.SetBackgroundStyle(wx.BG_STYLE_ERASE)
		self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

		#self.setmenu()

		#bSizer2.Add( self.m_bitmap4, 5, wx.ALIGN_CENTER|wx.ALL, 5 )

		#self.SetSizer( bSizer2 )
		#self.Layout()

		# Connect Events

		#self.Bind( wx.EVT_RIGHT_DOWN, self.m_bitmap4OnContextMenu )
		#self.Bind( wx.EVT_CONTEXT_MENU, self.setmenu)
		self.Bind(wxaui.EVT_AUI_RENDER, self.OnEraseBackground)

	def __del__( self ):
		pass

	# Virtual event handlers, overide them in your derived class

	def m_bitmap4OnContextMenu( self, event ):
		self.PopupMenu(self.m1)

		#print (self.itm3.IsChecked(),self.itm2.IsChecked())
		#print self.itm3.IsChecked(),self.itm4.IsChecked()
	'''
	def setmenu(self,event):
		self.MnuDic = { 1: [_(u'Menu Change'),9999] ,2:[_(u'Toolbar Change'),9998],3:[_(u'Panes Change'),9997],4:[u'',0],
					    5:[_(u'Databases...'),9996],6:[_(u'Programs...'),9995],7:[_(u'Add Tools...'),9994],8:[u'',0],
						9:[_(u'ML Design...'),9990],10:[u'',0],11:[_(u'Settings...'),9992] }
		self.m1 = wx.Menu()

		self.itms = []
		i = 0
		for itm in self.MnuDic :
			self.Bind(wx.EVT_MENU, self.OnPopupOne, id=itm)
			if self.MnuDic[itm][0] == u'':
				self.m1.AppendSeparator()
			else:
				#self.itms.append( wx.MenuItem(self.m1, wx.ID_ANY, MnuDic[itm][0], wx.EmptyString) )
				self.m1.Append(itm, self.MnuDic[itm][0])
				#self.m1.Append(itm, self.itms[i])
				#self.Bind(wx.EVT_MENU, self.OnPopupOne, id=MnuDic[itm][1])
				i = i + 1

		self.PopupMenu(self.m1)
		self.m1.Destroy()

	def OnPopupOne(self, event):
		#print(event,event.GetId())
		pmid = event.GetId()
		a = pro.DoProgram(self.MnuDic[pmid][1],'A')
		s = a.size() if 'size' in dir(a) else ()
		win1 = wx.Frame(self, -1)
		win1.SetSize(s)
		a.main(win1)
    '''
	def OnEraseBackground(self, evt):
		# yanked from ColourDB.py
		#print(evt)
		dc = evt.GetDC()
		if not dc:
			dc = wx.ClientDC(self)
			rect = self.GetUpdateRegion().GetBox()
			#dc.SetClippingRect(rect)
			dc.SetClippingRegion(rect)
		dc.Clear()
		#bmp = wx.Bitmap(PICS_PATH+self.BGfile)
		bmp = wx.Bitmap(self.BGfile)
		img = bmp.ConvertToImage()
		img2 = img.Scale(wx.GetDisplaySize()[0],wx.GetDisplaySize()[1])
		bmp2 = img2.ConvertToBitmap()
		#print(dir(img2))
		#print(bmp2.GetWidth(),bmp2.GetHeight())
		#print(wx.GetDisplaySize())
		dc.DrawBitmap(bmp2, 0, 0)

	def ChangeBackGround(self):
		dc = wx.ClientDC(self)
		rect = self.GetUpdateRegion().GetBox()
		dc.SetClippingRegion(rect)
		dc.Clear()
		bmp = wx.Bitmap(self.BGfile)
		img = bmp.ConvertToImage()
		img2 = img.Scale(wx.GetDisplaySize()[0], wx.GetDisplaySize()[1])
		bmp2 = img2.ConvertToBitmap()
		dc.DrawBitmap(bmp2, 0, 0)
		self.Refresh()


# taken from pyramid.compat

from __future__ import absolute_import
import sys
import six

# True if we are running on Python 3.
PY3 = sys.version_info[0] == 3

if PY3:  # pragma: no cover
    text_type = str
else:
    text_type = six.text_type

if PY3:
    string_types = (str,)
else:
    string_types = (six.string_types,)


import techController
import mm

def getOutputtedPrediction(stock_name):
    stock_data = techController.getStockData(stock_name)

    lastTime = stock_data[0].pop()
    lastOpen = stock_data[1].pop()
    lastClose = stock_data[4].pop()
    lastHigh = stock_data[2].pop()
    lastLow = stock_data[3].pop()

    roc = mm.getRateOfChange(stock_data)  # array
    stoch_os = mm.getStochasticOscillator(stock_data)  # array
    asi = mm.getASI(stock_data)  # array
    arima_prediction = mm.getARIMA(stock_data[4])  # double
    fourier_prediction = mm.getFourier(stock_data[4])[0]

    prediction = mm.aggregatePrediction(roc, stoch_os, asi, arima_prediction, fourier_prediction)
    return [prediction, arima_prediction,fourier_prediction,lastClose]

def getPerr(predicted, actual):
    return abs((predicted - actual)/actual*100)

def getAvgError(perrs):
    total = 0
    for num in perrs:
        total += num
    total /= len(perrs)
    return total

listOfStockNames = ["AAPL","AA", "A", "AAL" , "AABA", "AAC",  "AAOI", "AAON", "AAP",
                    "AAT", "AAU", "AAWW", "AAXN", "AB", "ABB", "ABBV", "ABC", "ABCB",
                    "ABDC", "ABEO", "ABEV", "ABG", "ABIL", "ABIO", "ABM", "ABMD", "ABR",
                    "NFLX"]

predictions = []
arima_predictions = []
fourier_predictions = []
actualPrices = []

for name in listOfStockNames:
    print(name)
    data = getOutputtedPrediction(name)
    predictions.append(data[0])
    arima_predictions.append(data[1])
    fourier_predictions.append(data[2])
    actualPrices.append(data[3])

print("predictions are.........")
print(predictions)
print(arima_predictions)
print(fourier_predictions)
print("actual prices are.....")
print(actualPrices)

percentErrors = []
arima_perrs = []
fourier_perrs = []

for i in range(len(predictions)):
    percentErrors.append(getPerr(predictions[i],actualPrices[i]))
    arima_perrs.append(getPerr(arima_predictions[i],actualPrices[i]))
    fourier_perrs.append(getPerr(fourier_predictions[i],actualPrices[i]))

print("percent errors are...")
print(percentErrors)
print(arima_perrs)
print(fourier_perrs)
print("average ARIMA error")
print(getAvgError(arima_perrs))
print("average Fourier first extrapolation error")
print(getAvgError(fourier_perrs))


import asyncio
import sys, traceback
from threading import Lock
import json
from uuid import uuid4
from copy import deepcopy

from flask import Flask, request, jsonify

from root import ROOT_DIR
from src.python.chess.chess_interop import format_move
from src.python.chess.chess_state import fen_to_state
from src.python.chess.chess_agency import ChessAgent
from src.python.primitive import highest

serve_folder = ROOT_DIR + "/serve"
app = Flask(__name__, static_url_path="/static", static_folder=serve_folder)
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 0

analysis_lock = Lock()
loop = asyncio.get_event_loop()
shared = {
    "agent": None
}

@app.route("/")
def root():
    return app.send_static_file("chess.html")

@app.route("/api/analysis", methods=["POST"])
def create_analysis():
    request_id = str(uuid4())

    if analysis_lock.acquire(timeout=100):
        body = request.get_json()
        print(f"Request {request_id[:6]} for POST /api/analysis")
        print(json.dumps(body))
        out = {
            "result": None
        }
        chess_state = fen_to_state(body["fen"])
        shared["agent"] = ChessAgent(int(body["depth"]))

        def anaylsis():
            out["result"] = shared["agent"].start_game(chess_state)
        try:
            future = loop.run_in_executor(None, anaylsis)
            loop.run_until_complete(future)
        except Exception:
            print("Failed create_analysis: ")
            traceback.print_exc(file=sys.stdout)
        finally:
            analysis_lock.release()

        (score, best_path) = out["result"]
        next_move = next(best_path)
        next_state = deepcopy(shared["agent"].game)
        next_state.apply(next_move)
        next_fen = next_state.to_fen()
        resp = {
            "score": score,
            "move": format_move(next_move),
            "fen": next_fen,
            "resign": abs(score) == highest - 1,
            "done": next_state.is_done
        }
        print(f"Reponse {request_id[:6]} for POST /api/analysis")
        print(json.dumps(resp))
        return jsonify(resp)

@app.route("/api/analysis", methods=["DELETE"])
def stop_analysis():
    request_id = str(uuid4())
    print(f"Request {request_id[:6]} for DELETE /api/analysis")
    if shared["agent"] is not None:
        shared["agent"].stop()
    print(f"Request {request_id[:6]} for DELETE /api/analysis")
    return "Ok"

if __name__ == "__main__":
    app.run(debug=True)


import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from sklearn.utils import shuffle
from database import get_data_cache, get_all_food_data
import ETL
import random
from app_setup import app, colors

def description():
    return html.Div(children=[dcc.Markdown('''
        # CaloTracker
        This is an interactive tool for you to search and log different foods from our existing
        database of foods. Your caloric total will be divided between food and exercise, with labels
        for each individual entry to really help you break your intake down! Just search for a food
        or exercise using the dropdown bars, and the full info will appear in the table. Change
        your serving quantity and time spend working out straight in the table, as well; the total
        calories will automatically update in the table and in the graph. And, if you can't find the
        food or exercise that you're looking for, query it yourself from our API, and the dropdown
        bars will contain any new results. Happy logging!''',
        style={'paddingLeft': '5%', 'paddingRight': '5%', 'marginTop': 50, 'marginBottom': 25
        })])

def food_log():
    df = shuffle(get_all_food_data(False))
    opts = df['food_name'].unique()
    #data = df.to_dict('records')
    return html.Div([
        dash_table.DataTable(
            id='foods-table',
            columns=[{
                "name": 'Food Name', "id": 'food_name'},
                {"name": 'Brand Name', "id": 'brand_name'},
                {"name": 'Serving Unit', "id": 'serving_unit'},
                {"name": 'Serving Quantity', "id": 'serving_qty', 'editable': True},
                {"name": 'Total Calories', "id": 'nf_calories'}],
            data=[],
            row_deletable=True,
            fixed_rows={ 'headers': True, 'data': 0 },
            style_cell={'minWidth': '65px',
                'width': '65px',
                'maxWidth': '65px',
                'overflow': 'hidden',
                'textOverflow': 'ellipsis'},
            style_table={
                'maxHeight': '300px',
                'overflowY': 'scroll',
                'margin-left': '7px',
                'padding-right': '7%'
                },
            filter_action="native",
            sort_action="native",
            sort_mode='multi',
            css=[{ 'selector': 'td.cell--selected, td.focused',
                'rule': 'background-color: #D9D9DA !important;'},
                { 'selector': 'td.cell--selected *, td.focused *',
                'rule': 'color: #FC6D41 !important;'}],
            style_header={'backgroundColor': '#A9BA9C',
                'fontWeight': 'bold',
                'textAlign': 'center'},
            style_data_conditional=[{
                'if': {'row_index': 'odd'},
                'backgroundColor': '#D9D9DA'}
                ]),
        html.Br(),
        html.Div([dcc.Dropdown(
            id='food-log',
            options=[{'label': i, 'value': i} for i in opts],
            placeholder="Select a food")],
            style={'margin-left': '11%', 'color': '#274228', 'width': '80%'}),
        html.Div([html.Button('Add Food',
            id='adding-food-button',
            n_clicks=0)],
            style={'margin-left': '11%', 'display': 'inline-block', 'color': '#274228'})],
        style={'width': '48%', 'display': 'inline-block'})

def exer_log():
    db = get_data_cache(allow_cached=True)
    df = shuffle(db[2])
    #data = df.to_dict('records')
    opts = df['name'].unique()
    return html.Div([
        dash_table.DataTable(
            id='exer-table',
            columns=[{
                "name": 'Exercise Name', "id": 'exer_name'},
                {"name": 'Duration (Mins)', "id": 'duration_min', 'editable': True},
                {"name": 'Calories Burned', "id": 'cals_burned'}],
            data=[],
            fixed_rows={ 'headers': True, 'data': 0 },
            style_cell={'minWidth': '65px',
                'width': '65px',
                'maxWidth': '65px',
                'overflow': 'hidden',
                'textOverflow': 'ellipsis',
                'fontColor': '#274228'},
            style_table={
                'maxHeight': '300px',
                'overflowY': 'scroll',
                'margin-right': '5%',
                'padding-left': '9%'
                },
            row_deletable=True,
            filter_action="native",
            sort_action="native",
            sort_mode='multi',
            style_data_conditional=[{
                'if': {'row_index': 'odd'},
                'backgroundColor': '#D9D9DA'}
                ],
            style_header={'backgroundColor': '#A9BA9C',
                'fontWeight': 'bold',
                'textAlign': 'center'},
            css=[{ 'selector': 'td.cell--selected, td.focused',
            'rule': 'background-color: #D9D9DA !important;'},
            { 'selector': 'td.cell--selected *, td.focused *',
            'rule': 'color: #FC6D41 !important;'}]
        ),
        html.Br(),
        html.Div([dcc.Dropdown(
            id='exer-log',
            options=[{'label': i, 'value': i} for i in opts],
            placeholder="Select an exercise")],
            style={'margin-left': '17%', 'color': '#274228'}),
        html.Div([html.Button('Add Exercise',
            id='adding-exercise-button',
            n_clicks=0)],
            style={'margin-left': '17%',
            'display': 'inline-block', 'color': '#274228', 'fontColor': '#A9BA9C'})],
        style={'width': '48%', 'display': 'inline-block'})

def food_search():
    """"""
    return html.Div([
        dcc.Input(id='food-search', type='text', value=''),
        html.Button('Find my food!', id='food-submit', n_clicks=0,
        style={'margin-left': '5px'}),
        html.P(id='food-output')],
        style={'display': 'inline-block',
        'align-items': 'center',
        'margin-left': '14%',
        'margin-right': '5%'}
    )

def exer_search():
    """"""
    return html.Div([
        dcc.Input(id='exer-name-search', type='text', value=''),
        html.Button('Find my exercise!', id='exer-submit', n_clicks=0,
        style={'margin-left': '5px'}),
        html.P(id='exer-output')],
        style={'display': 'inline-block',
        'align-items': 'center',
        'margin-right': '14%',
        'margin-left': '5%'}
    )

def dynamic_layout():
    return html.Div([
        description(),
        dcc.Graph(id='log-graph', style={'width': '90%', 'margin-left': '5%', 'margin-right': '5%'}),
        food_log(),
        exer_log(),
        html.Br(),
        html.Br(),
        dcc.Markdown('''Couldn't find what you were looking for? Try searching the API with the name of a food,
        exercise,or a list of either separated by a common and space (and please be patient)! Foods go in the
        left search bar, and exercises on the right.''', style={'paddingLeft': '5%', 'paddingRight': '5%'}),
        food_search(),
        exer_search(),
        html.Br(),
        dcc.Link('Return Home', href='/app',
        style={'color': '#274228'}),
        html.Br(),
        dcc.Link('Go to CaloPlanner', href='/apps/app2',
        style={'color': '#274228'})],
        id='app1-content')

layout = dynamic_layout()

@app.callback(
    Output('foods-table', 'data'),
    [Input('adding-food-button', 'n_clicks'),
    Input('foods-table', 'data_timestamp')],
    [State('food-log', 'value'),
    State('foods-table', 'data'),
    State('foods-table', 'columns')])
def update_food_row(n_clicks, serving, food, rows, columns):

    df = get_all_food_data(True)
    info = df[df['food_name']==food]

    def add_row(r):
        if len(info) == 0:
            pass
        else:
            rowdict = info.to_dict('records')[0]
            vals = rowdict.values()
            r.append({c['id']: v for (c,v) in zip(columns, vals)})
        return r

    if len(rows) == 0:
        add_row(rows)

    else:
        test = [list(r.values()) for r in rows]
        flat_list = [item for sublist in test for item in sublist]
        if food in flat_list:
            pass
        else:
            add_row(rows)

    for r in rows:
        update = df[df['food_name'] == r['food_name']]
        rowdict = update.to_dict('records')[0]
        temp1 = rowdict['nf_calories']
        temp2 = rowdict['serving_qty']
        r['nf_calories'] = float(temp1)/float(temp2) * float(r['serving_qty'])
    return rows

@app.callback(
    Output('exer-table', 'data'),
    [Input('adding-exercise-button', 'n_clicks'),
    Input('exer-table', 'data_timestamp')],
    [State('exer-log', 'value'),
    State('exer-table', 'data'),
    State('exer-table', 'columns')])
def update_exer_row(n_clicks, duration, exercise, rows, columns):

    db = get_data_cache(allow_cached=True)
    df = db[2]
    info = df[df['name']==exercise]

    def add_erow(r):
        if len(info) == 0:
            pass
        else:
            rowdict = info.to_dict('records')[0]
            vals = rowdict.values()
            r.append({c['id']: v for (c,v) in zip(columns, vals)})
        return r

    if len(rows) == 0:
        add_erow(rows)

    else:
        test = [list(r.values()) for r in rows]
        flat_list = [item for sublist in test for item in sublist]
        if exercise in flat_list:
            pass
        else:
            add_erow(rows)

    for r in rows:
        update = df[df['name'] == r['exer_name']]
        rowdict = update.to_dict('records')[0]
        temp1 = rowdict['nf_calories']
        temp2 = rowdict['duration_min']
        r['cals_burned'] = float(temp1)/float(temp2) * float(r['duration_min'])
    return rows

@app.callback(
    Output('log-graph', 'figure'),
    [Input('foods-table', 'derived_viewport_data'),
     Input('exer-table', 'derived_viewport_data')],
     [State('foods-table', 'columns'),
     State('exer-table', 'columns')])
def display_output(frows, erows, fcols, ecols):

    fcals = []
    ecals = []
    ftraces = []
    etraces = []

    if frows is None:
        pass
    else:
        for f in frows:
            trace = go.Bar(name = f['food_name'],
                x = ['Food'],
                y = [f['nf_calories']],
                marker_color = colors[random.randint(1,9)])
            ftraces.append(trace)
            fcals.append(f['nf_calories'])

    if erows is None:
        pass
    else:
        for e in erows:
            trace = go.Bar(name = e['exer_name'],
                x = ['Exercise'],
                y = [e['cals_burned']],
                marker_color = colors[random.randint(1,9)])
            etraces.append(trace)
            ecals.append(e['cals_burned'])

    total = np.sum(fcals) - np.sum(ecals)
    totaltrace = [go.Bar(name = 'Total Caloric Intake',
                    x = ['Total'],
                    y = [total],
                    marker_color = colors[random.randint(1,9)])]

    traces = ftraces + etraces + totaltrace

    return {
        'data': traces,
        'layout': go.Layout(barmode='stack',
                    title='Personal Calorie Log',
                    font = dict(color = '#274228'),
                    yaxis = dict(title='Number of Calories'),
                    showlegend=False)}

@app.callback(
    Output('food-output', 'children'),
    [Input('food-submit', 'n_clicks')],
    [State('food-search', 'value')]
)
def foodapi(click, term):
    if click == 0:
        return
    else:
        hits = ETL.user_food_query(term)
        return u'Your query returned {} results!'.format(hits)

@app.callback(
    Output('exer-output', 'children'),
    [Input('exer-submit', 'n_clicks')],
    [State('exer-name-search', 'value')]
)
def exerapi(click, term):
    if click == 0:
        return
    else:
        hits = ETL.user_exercise_query(term)
        return u'Your query returned {} results!'.format(hits)

@app.callback(
    Output('exer-log', 'options'),
    [Input('exer-output', 'children')]
)
def update_exer_drop(refresh):
    df = shuffle(get_data_cache()[2])
    opts = df['name'].unique()
    return [{'label': i, 'value': i} for i in opts]

@app.callback(
    Output('food-log', 'options'),
    [Input('food-output', 'children')]
)
def update_food_drop(refresh):
    df = shuffle(get_all_food_data(False))
    opts = df['food_name'].unique()
    return [{'label': i, 'value': i} for i in opts]

# if __name__ == '__main__':
#     app.run_server(debug=True, port=1050, host='0.0.0.0')


#Fischer Gleichung in 1D
import numpy as np
import matplotlib.pyplot as plt

#Definitionen
def rk4(nk,kx,ky,gammar,sigma,mu,h):
    k_1 = DGL(nk,kx,ky,gammar,sigma,mu)
    k_2 = DGL(nk + h/2*k_1,kx,ky,gammar,sigma,mu)
    k_3 = DGL(nk + h/2*k_2,kx,ky,gammar,sigma,mu)
    k_4 = DGL(nk + h*k_3,kx,ky,gammar,sigma,mu)
    nk = nk + h/6*(k_1 + 2*(k_2 + k_3) + k_4)
    return nk

#Benötigen unsere DGL mit nk ist n im k-Raum
def DGL(nk,kx,ky,gammar,sigma,mu):
    return (gammar/sigma - mu)*nk + 0.01*(-kx**2 - ky**2)*nk - np.fft.fft(np.fft.ifft2(Deal(nk))**2)

def Deal(nk):
    kxmax = np.max(kx)
    kymax = np.max(ky)
    for i in range(len(nk)):
        for l in range(len(nk)):
            if kxmax < 2/3*kx[i][l] or kymax < 2/3*ky[i][l]:
                nk[i][j] = 0
    return nk

#Anfangsbedingungen
def n_0(x,y,L_x,L_y):
    return 0.2*np.exp(-((x-L_x/2)**2 + (y-L_y/2)**2)/0.5)

#Parameter
N_t = 1000
N_x = 256
N_y = 256
h = 0.005
L_x = 4*np.pi
L_y = 4*np.pi
mu = 0.01
gammar = 1
sigma = 1

#Funktionen in denen Werte reingeschrieben werden
T = np.arange(N_t)*h
n = np.zeros((N_t,N_x,N_y))

#Räume und deren Gestalt
x,y = np.meshgrid(np.arange(N_x)*L_x/N_x, np.arange(N_y)*L_y/N_y)
kx,ky = np.meshgrid(np.fft.fftfreq(N_x,L_x/(N_x*2*np.pi)), np.fft.fftfreq(N_y,L_y/(N_y*2*np.pi)))

#Bezug auf Anfansdaten
n[0] = n_0(x,y,L_x,L_y)

#Berechnen/ Prinzip des Pseudospektralverfahrens
#for i in range(1,N_t):

#Plotten
plt.ion
plt.axis([x.min(), x.max(), y.min(), y.max()])
plt.pcolormesh(x,y,n[0],vmin = 0, vmax = 1)
plt.colorbar()

for i in range(1,N_t):
    #Berchnung des Pseudospektralverfahrens
    nk = np.fft.fft2(n[i-1])
    nk_new = rk4(nk,kx,ky,gammar,sigma,mu,h)
    n[i] = np.fft.ifft2(nk_new).real

    if i%10 == 0:#Modulo somit Bewegung
        plt.cla()
        plt.pcolormesh(x,y,n[i],vmin = 0, vmax = 1)#Ortsbewegung
        plt.plot()
        plt.pause(0.01)

plt.ioff
#Ende


# GameSpace.py

# Thomas Deranek and Chris Clark
# CSE 30332
# 05/07/15

import pygame
from pygame.locals import *

from Player import Player
from Laser import Laser

class GameSpace:
	def __init__(self):
		# 1) basic init
		pygame.init()
		self.size = self.width, self.height = 1200,800
		self.black = 0,0,0
		self.screen = pygame.display.set_mode(self.size)
		pygame.key.set_repeat(1,50)

		# 2) set up game objects
		self.Player1 = Player(self)	#Player1
		self.Player1.choosePlayer("player1.png")
		self.Player2 = Player(self)	#Player2
		self.Player2.choosePlayer("player2.png")
		self.laserList = []	#List of laser objects
		self.Victor = 0		#The winner of the game (1 or 2, or 0 if no winner yet)

		# This image is displayed to the user when the other player is yet to connect
		self.wait_image = None
		self.wait_rect = None

		# This is the scene in which the game is played on
		self.back_image = pygame.image.load("media/background.png")
		self.back_rect = self.back_image.get_rect()

		# Two different victory screens
		self.p1v_image = pygame.image.load("media/p1v.png")
		self.p1v_rect = self.p1v_image.get_rect()
		self.p2v_image = pygame.image.load("media/p2v.png")
		self.p2v_rect = self.p2v_image.get_rect()

	# Draws all the objects to the screen
	def draw_game(self):
		# 7) animation
		self.screen.fill(self.black)
		self.screen.blit(self.back_image, self.back_rect)

		# Display the laser images
		for laser in self.laserList:
			laser.tick()	#move laser in correct direction
			#check if enemy laser has hit a player
			if laser.playerNum == 1 and laser.rect.colliderect(self.Player2.rect) and laser.hasHit == 0:
				self.Player2.health = self.Player2.health - 1
				laser.hasHit = 1
			elif laser.playerNum == 2 and laser.rect.colliderect(self.Player1.rect) and laser.hasHit == 0:
				self.Player1.health = self.Player1.health - 1
				laser.hasHit = 1
			self.screen.blit(laser.image, laser.rect)

		# Tick both players and display them
		self.Player1.tick()
		self.Player2.tick()
		self.screen.blit(self.Player1.image, self.Player1.rect)
		self.screen.blit(self.Player2.image, self.Player2.rect)

		# Display end screen if one of the players won
		if self.Player1.isDead == 1 and (self.Victor == 0 or self.Victor == 2):
			self.Victor = 2
			self.screen.blit(self.p2v_image, self.p2v_rect)
		elif self.Player2.isDead == 1 and (self.Victor == 0 or self.Victor == 1):
			self.gameOver = 1
			self.screen.blit(self.p1v_image, self.p1v_rect)

		pygame.display.flip()


import os

import fabrika
import yaml
from fabric import api as fabric_api, operations
from fabric.decorators import roles
from fabrika import deploy_tools
from fabrika.tasks.docker import SetupLoadBalancerBehindGateway

import configuration
from fabfile.app_configuration import configured_for

CONTAINER_HOSTNAME = 'node'
DEPENDENCIES = 'dependencies'
DNS_SERVER_2 = 'dns2'
DNS_SERVER_1 = 'dns1'
LOAD_BALANCER_NODES = 'nodes'
LOAD_BALANCER_PORT = 'load_balancer_port'
LOAD_BALANCER_HOST = 'load_balancer_host'
LOAD_BALANCER_MODE = 'mode'
NODE_PORT = 'node_port'
ENABLE_NSCD = 'enable_nscd'

FABRIC_ROLE_SEED_SANITY_TEST_FILE = 'seed_sanity_test_file'
FABRIC_ROLE_DOCKER_HOST = 'docker_host'
FABRIC_ROLE_LOAD_BALANCER = 'lb_config_node'
FABRIC_ROLE_ELASTIC_SEARCH_LOAD_BALANCER = 'elastic_search_lb_config_node'

NSCD_SOCKET_SOURCE = '/var/run/nscd'
NSCD_SOCKET_TARGET = '/tmp/nscd-extern'

load_balancer_task = SetupLoadBalancerBehindGateway()

@fabric_api.task()
def assert_deploying_newer_image(environment, full_image_name):
    _initialize_environment(environment)
    fabric_api.execute(deploy_tools.assert_deploying_newer_image, full_image_name, role=FABRIC_ROLE_DOCKER_HOST)

def _initialize_environment(environment):
    _parse_deployment_yaml(environment)
    env_properties = """
    roledefs  : {roledefs}
    hosts     : {hosts}
    properties: {properties}
    """
    print(env_properties.format(roledefs=fabric_api.env.roledefs, hosts=fabric_api.env.hosts,
                                properties=fabric_api.env.host_role_properties))

@fabric_api.task()
def full_deploy(environment, full_image_name, configuration_dir, run_migrate=False, enable_nscd=None):
    _initialize_environment(environment)
    fabric_api.execute(deploy_docker_image, environment, full_image_name, configuration_dir, enable_nscd=enable_nscd)
    fabric_api.execute(setup_elastic_search_service_load_balancer)
    fabric_api.execute(setup_lls_service_load_balancer)
    fabric_api.execute(seed_test_file)

def _associate_host_to_a_role(host, role):
    if role not in fabric_api.env.roledefs:
        fabric_api.env.roledefs[role] = []
    fabric_api.env.roledefs[role].append(host)

def _parse_deployment_yaml(environment):
    fabric_api.env.host_role_properties = {}
    deploy_cfg_file = os.path.join(os.getcwd(), "fabfile/deploy/{}.yml".format(environment))
    yaml_config = yaml.load(open(deploy_cfg_file, 'r'))
    for host in yaml_config:
        for role in yaml_config[host]['roles']:
            _associate_host_to_a_role(host, role)
            fabric_api.env.host_role_properties[(host, role)] = yaml_config[host]['roles'][role]

def set_load_balancer(role):
    load_balancer_config = fabric_api.env.host_role_properties[(fabric_api.env.host, role)]
    fabric_api.execute(load_balancer_task,
                       load_balancer_config[LOAD_BALANCER_HOST],
                       load_balancer_config[LOAD_BALANCER_PORT],
                       load_balancer_config[LOAD_BALANCER_NODES],
                       load_balancer_config[NODE_PORT],
                       load_balancer_config.get(LOAD_BALANCER_MODE))

@roles(FABRIC_ROLE_SEED_SANITY_TEST_FILE)
def seed_test_file():
    print(">>>>>>> seeding sanity test file on host {}".format(fabric_api.env.host_string))
    local_csv_file = os.path.join(os.getcwd(), "tests/samples/normal.csv")
    remote_sanity_test_file = os.path.join(configuration.data.VOLUME_MAPPINGS_FILE_UPLOAD_SOURCE, 'normal.csv')
    operations.put(local_csv_file, remote_sanity_test_file)

@roles(FABRIC_ROLE_LOAD_BALANCER)
def setup_lls_service_load_balancer():
    print(">>>>>>> setting up load balancer on host {}".format(fabric_api.env.host_string))
    set_load_balancer(FABRIC_ROLE_LOAD_BALANCER)

@roles(FABRIC_ROLE_ELASTIC_SEARCH_LOAD_BALANCER)
def setup_elastic_search_service_load_balancer():
    print(">>>>>>> setting up search service load balancer on host {}".format(fabric_api.env.host_string))
    set_load_balancer(FABRIC_ROLE_ELASTIC_SEARCH_LOAD_BALANCER)

def _process_nscd_mapping(volume_mappings, configuration, override):
    option_overridden = override is not None

    if option_overridden:
        if override:
            volume_mappings[NSCD_SOCKET_SOURCE] = NSCD_SOCKET_TARGET
        return volume_mappings

    if configuration.get(ENABLE_NSCD):
        volume_mappings[NSCD_SOCKET_SOURCE] = NSCD_SOCKET_TARGET
    return volume_mappings

@roles(FABRIC_ROLE_DOCKER_HOST)
def deploy_docker_image(environment, fully_qualified_image_name, app_container_config_path, enable_nscd=None,
                        stop_existing=True):
    print(">>>>>>> deploy_docker_image on host {}".format(fabric_api.env.host))
    node_deploy_configuration = fabric_api.env.host_role_properties[(fabric_api.env.host, FABRIC_ROLE_DOCKER_HOST)]

    volume_mappings = _process_nscd_mapping({}, node_deploy_configuration, enable_nscd)

    with configured_for(environment):
        file_upload_source = configuration.data.VOLUME_MAPPINGS_FILE_UPLOAD_SOURCE
        file_upload_target = configuration.data.VOLUME_MAPPINGS_FILE_UPLOAD_TARGET
        volume_mappings[file_upload_source] = file_upload_target

    deploy_task = fabrika.tasks.docker.DeployTask('list_loading_service', volume_mappings=volume_mappings)
    fabric_api.execute(deploy_task,
                       fully_qualified_image_name,
                       node_deploy_configuration.get(DEPENDENCIES),
                       node_deploy_configuration.get(DNS_SERVER_1),
                       node_deploy_configuration.get(DNS_SERVER_2),
                       node_deploy_configuration.get(CONTAINER_HOSTNAME),
                       app_container_config_path,
                       stop_existing=stop_existing)