from matplotlib import cm, rcParams
import matplotlib as matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math as math
import random as rand
import os
import csv
import argparse

# TO DO : Rewrite this code to make it more readable.
# USAGE : Run in terminal "python auto_to_py2.py beta=0.1/bif_points_1.dat  beta=0.1/bif_points_2.dat  beta=0.1/bif_points_3.dat beta_range/beta_0.2.dat beta_range/beta_0.15.dat beta_range/beta_0.1.dat beta_range/beta_0.05.dat beta_range/line.dat beta_range/stable1.dat beta_range/stable2.dat".

plt.rcParams['axes.xmargin'] = 0
fig, ax = plt.subplots(1, 2, figsize=(14,5.4))
#matplotlib.pyplot.xkcd(scale=.4, length=100, randomness=2)

matplotlib.rc('xtick', labelsize=11)
matplotlib.rc('ytick', labelsize=11)

p = argparse.ArgumentParser()
p.add_argument('files', type=str, nargs='*')
args = p.parse_args()

def row_count(filename):
    with open(filename) as in_file:
        return sum(1 for _ in in_file)

c = ['#aa3863', '#d97020', '#ef9f07', '#449775', '#3b7d86']
s = ['-', '--']

I = [[]]
phi = [[]]
stability = []

for filename in args.files :
    with open(filename, newline='') as file:
        datareader = csv.reader(file, delimiter=' ')

        last_line_nb = row_count(filename)

        last_I = -999
        last_phi = -999
        last_stability = 0

        # seperate by checking if two consecutive values are duplicates
        for row in datareader:

            # this last condition avoids a list with one value when two consecutive values are duplicates,
            # but the values phi defer (e.g. at the fork bifurcation)
            # however sometimes xppaut duplicates lines when outputting files, in which case we ignore
            if last_I == float(row[0]) and last_phi != float(row[1]) and len(I[-1]) > 2 :
                    I.append([])
                    phi.append([])
                    if last_stability != 0 :
                        stability.append(last_stability)

            if last_I != -999 :
                I[-1].append(last_I)
                phi[-1].append(last_phi)

            if last_stability != int(row[3]) and len(I[-1]) > 1:
                I.append([])
                phi.append([])
                if last_stability != 0 :
                    stability.append(last_stability)

            # if at last line, then stop checking for consecutive values and just add the remaining data
            if last_line_nb == datareader.line_num:
                I[-1].append(float(row[0]))
                phi[-1].append(float(row[1]))
                stability.append(int(row[3]))

            last_I = float(row[0])
            last_phi = float(row[1])
            last_stability = int(row[3])

print(len(I))
# regime delimiter to make things more visual
ax[0].axvspan(1, 1.4942, facecolor='0.2', alpha=0.1)

legend_stable, legend_unstable = False, False
for k in range(6) :
    if stability[k] == 1 :
        if legend_stable == False :
            ax[0].plot(I[k], phi[k], color='black', linestyle=s[stability[k]-1], label='stable')
            legend_stable = True
        else :
            ax[0].plot(I[k], phi[k], color='black', linestyle=s[stability[k]-1])
    if stability[k] == 2 :
        if legend_unstable == False :
            ax[0].plot(I[k], phi[k], color='black', linestyle=s[stability[k]-1], label='unstable')
            legend_unstable = True
        else :
            ax[0].plot(I[k], phi[k], color='black', linestyle=s[stability[k]-1])

# remove duplicate legend
ax[0].legend(loc='upper right', prop={'size': 11}, bbox_to_anchor=(1, 0.95))

b = [0.2, 0.15, 0.1, 0.05]
for k in range(6,len(I)) :
    if k < 14 :
        ax[1].plot(I[k], phi[k], color=c[int((k-6)/2)], linestyle=s[stability[k]-1], label=f'$\\beta$={b[int((k-6)/2)]}')

    else :
        if stability[k] == 1 :
            ax[1].plot(I[k], phi[k], color='black', linestyle=s[stability[k]-1])
        if stability[k] == 2 :
            ax[1].plot(I[k], phi[k], color='black', linestyle=s[stability[k]-1])

plt.suptitle('Bifurcation diagrams for two weakly coupled neurons', fontsize=16)
ax[1].set_xlabel('Current $I$', fontsize=14, labelpad=10)
ax[0].set_xlabel('Current $I$', fontsize=14, labelpad=10)
ax[0].set_ylabel('Phase Difference $\phi$', fontsize=14)

# remove duplicate legend
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
ax[1].legend(by_label.values(), by_label.keys(), loc='upper right', prop={'size': 11}, bbox_to_anchor=(1, 0.95))

plt.tight_layout()
plt.savefig('bifs_in_one.svg')
plt.show()


#!/usr/bin/env python
"""
This performs a simple test of the HLSVDPROPY module to see that it's working
and producing sane results. It uses stored SVS FID data as input and pre-
calculated singular values, and frequencies, dampings, amplitudes and phases
data as comparisons.

"""

#
#  (C) Brian J Soher, 2020
#

# Python modules
from __future__ import division, print_function

import os
import cProfile
import pprint
pp=pprint.pprint

# 3rd party modules
import numpy as np

# Our modules
import hlsvdpropy

DTOR = np.pi / 180.0

def test(plot_flag=True):
    """
    This code can be run at the command line to test the internal methods for
    a real MRS SVS data set by running the following command. You may need to
    move it out of the hlsvdpropy install directory for proper functioning:

        $ python simple_test.py

    This should print out results that show that the current hlsvd calculated
    singular values are the same as 'known' values previously calculated and
    stored in this module. It should also plot out for the time domain and
    spectral domain 1) raw data, 2) fitted model and 3) raw - fit plots.

    """
    indat = hlsvdpropy.TESTDATA
    datt = hlsvdpropy.get_testdata()

    dwell = float(indat["step_size"])
    nsv_sought  = indat['n_singular_values']

    reps = 1 if plot_flag else 20

    result1 = []
    result2 = [nsv_sought,indat['sing0'],indat['freq0'],indat['damp0'],indat['ampl0'],indat['phas0']]

    for i in range(reps):
        result1 = hlsvdpropy.hlsvd(datt, nsv_sought, dwell)

    if result1: pp_result_compare(result1, result2, 'hlsvdpropy', 'actual')

    if plot_flag:

        fitts = []
        for indx,res in enumerate([result1, result2]):

            if res:
                fitt = hlsvdpropy.create_hlsvd_fids(res, len(datt), dwell, sum_results=True, convert=False)
                fitt[0] *= 0.5
                fitts.append(fitt)
                dat = np.real(np.array(datt))   # only concerned with the real data
                fit = np.real(fitt)
                rmse = np.sqrt(np.sum((dat - fit) ** 2) / len(dat))
                nrmse = rmse / max(dat) - min(dat)
                print(str(indx)+" - RMSE = %.2f, normalized RMSE = %.2f%%" % (rmse, nrmse / 100))

        import matplotlib.pyplot as plt

        datt[0] *= 0.5
        datf = np.fft.fft(_chop(datt))
        labels = ['HLSVDProPy vs Data Time', 'HLSVDProPy vs Data Freq',
                  'Actual Values vs Data Time', 'Actual Values vs Data Freq']

        for i,fit in enumerate(fitts):

            fitt = np.array(fit)
            fitf = np.fft.fft(_chop(fitt))

            # The raw data values are always noisier, so plotting them first allows
            # the cleaner estimation to be displayed on top.

            plt.subplot(3,2,i*2+1)
            plt.plot(datt.real, color='b')
            plt.plot(fitt.real, color='r')
            plt.plot((datt-fitt).real, color='g')
            plt.title(labels[i*2+0])

            plt.subplot(3,2,i*2+2)
            plt.plot(datf.real, color='b')
            plt.plot(fitf.real, color='r')
            plt.plot((datf-fitf).real, color='g')
            plt.title(labels[i*2+1])

        if len(fitts) >=2:
            fita, fitb = fitts
            fitt = fita - fitb
            fitaf = np.fft.fft(_chop(fita))
            fitbf = np.fft.fft(_chop(fitb))
            fitf = fitaf - fitbf

            plt.subplot(3,2,5)
            plt.plot(fita.real, color='b')
            plt.plot(fitb.real, color='r')
            plt.plot(fitt.real, color='g')
            plt.title('Both Fits Compared Time')

            plt.subplot(3,2,6)
            plt.plot(fitaf.real, color='b')
            plt.plot(fitbf.real, color='r')
            plt.plot(fitf.real,  color='g')
            plt.title('Both Fits Compared Freq')

        plt.show()

def _chop(data):
    return data * ((((np.arange(len(data)) + 1) % 2) * 2) - 1)

def pp_result_compare(res1, res2, id1, id2):

    labl = ['nsv_found', 'singular_values', 'frequencies', 'damping_terms', 'amplitudes', 'phases']

    # NB. The following comparisons come out in different order, thus the need to
    #     do the np.argsort() gymnastics.

    _, _, frequencies1, _, _, _ = res1
    _, _, frequencies2, _, _, _ = res2

    indices1 = np.argsort(frequencies1)
    indices2 = np.argsort(frequencies2)

    for labl, item1, item2 in zip(labl, res1, res2):

        if labl == 'nsv_found':
            print('')
            print(labl, id1+' = '+str(item1), id2+' = '+str(item2))
        elif labl == 'singular_values':
            print('')
            pp(labl+' - '+id1)
            pp(np.array(item1))
            pp(labl+' - '+id2)
            pp(np.array(item2))
            pp(labl+ ' delta'+'('+id1+' - '+id2+')')
            pp(np.array([val1-val2 for val1,val2 in zip(np.array(item1),np.array(item2))]))
        else:
            print('')

#------------------------------------------------------------------------------

if __name__ == "__main__":

    if os.path.exists("profile.data"):
        os.remove("profile.data")

    cProfile.run('test(plot_flag=False)', 'profile.data')
    import pstats as ps
    p = ps.Stats('profile.data')
    p.strip_dirs().sort_stats('cumulative').print_stats()

    test(plot_flag=True)


import pygame
import random

from board import *
from snake import *
from food import *

pygame.init()

pygame.mixer.init()
devour_sound = pygame.mixer.Sound('sounds/devour.wav')

pygame.display.set_caption('Snake')

clock = pygame.time.Clock()

board = Board(0, 0, 500, 700, 20)
snake = Snake(board)
food = Food(board, snake)

screen = pygame.display.set_mode([500, 700])
snake.screen = screen
snake.devour_sound = devour_sound
food.screen = screen
board.screen = screen

snake.place()
food.place()

game_running = True

while game_running:

    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN and prev_y_speed != -1:
                snake.x_speed = 0.
                snake.y_speed = 1.
            if event.key == pygame.K_UP and prev_y_speed != 1:
                snake.x_speed = 0.
                snake.y_speed = -1.
            if event.key == pygame.K_LEFT and prev_x_speed != 1:
                snake.x_speed = -1.
                snake.y_speed = 0.
            if event.key == pygame.K_RIGHT and prev_x_speed != -1:
                snake.x_speed = 1.
                snake.y_speed = 0.

    # game loop
    snake.slither()

    if snake.collided():
        snake.place()

    if food.eaten():
        food.place()
        snake.devour()

    prev_x_speed = snake.x_speed
    prev_y_speed = snake.y_speed

    # draw loop
    screen.fill([0,0,0])

    board.draw()
    snake.draw()
    food.draw()

    pygame.display.flip()

    clock.tick(board.height/board.scale//4)

pygame.quit()


import unittest
import random

from tqdm import tqdm
import IPython
import torch
import numpy as np
import pandas as pd
import pytorch_lightning as pl
from pytorch_lightning import loggers as pl_loggers
from torch.utils.data import DataLoader

from models import BERTModelLightning
from dataset import ToyDataset

def generate_dataset(n=100, isLeft=True, dimension=32):
    outputs, images, sequences = [], [], []
    halved = int(dimension/2)
    if isLeft:
        names= ['left', 'right']
    else:
        names = ['top', 'bottom']
    for idx in range(n):
        input_npy = np.zeros((dimension, dimension))
        if random.random() < 0.5:
            if isLeft:
                input_npy[:,:halved] = np.random.randint(2, size=((dimension,halved)))
            else:
                input_npy[:halved,:] = np.random.randint(2, size=((halved, dimension)))
            true = 0
        else:
            if isLeft:
                input_npy[:,halved:] = np.random.randint(2, size=((dimension,halved)))
            else:
                input_npy[halved:,:] = np.random.randint(2, size=((halved,dimension)))
            true = 1
        for idx_d, direction in enumerate(names):
            images.append(input_npy)
            sequences.append(direction)
            outputs.append(int(idx_d == true))
    return np.stack(images, axis=0), np.array(sequences), np.array(outputs)

def predict(bbm, dataloader, save_path='logs/train_results.csv', device='cpu'):
    bbm = bbm.to(device)
    all_zeros, all_ones, all_outs = [], [], []
    for seq_ids, imgs, outs in dataloader:

        probs = bbm(seq_ids.to(device), imgs.to(device))
        tostore= bbm.pred_softmax(probs).tolist()
        zeros, ones = map(list, zip(*tostore))

        # save
        all_zeros += zeros
        all_ones += ones
        all_outs += outs.argmax(dim=1).tolist()
    df = pd.DataFrame({'prob_zero': all_zeros, 'prob_one': all_ones, 'correct': all_outs})
    df.to_csv(save_path)

class TestAdjConv(unittest.TestCase):
    def test_basic(self):
        dims = [16, 32, 64, 128]
        # ns = [100, 200, 300, 400]
        ns = [400, 400, 400, 400]

        for dim, n in zip(dims, ns):
            bbm = BERTModelLightning([1, 16, 32, 64, 128], output_channels=64, last_size=dim//2, device='cuda')
            bbm.to('cuda')
            train_ds = ToyDataset(*generate_dataset(n, dimension=dim))
            valid_ds = ToyDataset(*generate_dataset(n, dimension=dim))

            train_dl = DataLoader(train_ds, batch_size=16)
            valid_dl = DataLoader(valid_ds, batch_size=16)

            tb_logger = pl_loggers.CSVLogger('logs/', name=f'dim{dim}')
            trainer = pl.Trainer(max_epochs=10, logger=tb_logger, log_every_n_steps=1, gpus=1)

            trainer.fit(bbm, train_dl, valid_dl)

            predict(bbm, train_dl, save_path=f'logs/dim{dim}/train_results.csv', device='cuda')
            predict(bbm, valid_dl, save_path=f'logs/dim{dim}/valid_results.csv', device='cuda')

        # train on top/bottom
        # test_ds = ToyDataset(*generate_dataset(isLeft=False, n=10))
        # test_dl = DataLoader(test_ds, batch_size=1)
        #
        # test_ds2 = ToyDataset(*generate_dataset(isLeft=False, n=50))
        # test_dl2 = DataLoader(test_ds2, batch_size=1)
        #
        # test_logger = pl_loggers.CSVLogger('test_logs/')
        # test_trainer = pl.Trainer(max_epochs=5, logger=test_logger, log_every_n_steps=1)
        # test_trainer.fit(bbm, test_dl, test_dl2)
        # predict(bbm, test_ds, save_path='test_logs/test_results.csv')

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


import numpy
from keras.datasets import imdb
from keras.layers import Dense, Flatten, SpatialDropout1D
from keras.layers.embeddings import Embedding
from keras.models import Sequential
from keras.preprocessing.sequence import pad_sequences

seed = 7
numpy.random.seed(seed)
top_words = 5000
(X1_train, Y1_train), (X1_test, Y1_test) = imdb.load_data(num_words=top_words)

max_words = 500
X1_train = pad_sequences(X1_train, maxlen=max_words)
X1_test = pad_sequences(X1_test, maxlen=max_words)

print(X1_train.shape)

model = Sequential()
model.add(Embedding(top_words, 32, input_length=max_words))
model.add(SpatialDropout1D(0.4))
model.add(Flatten())
model.add(Dense(250, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='adam', metrics=['accuracy'])
print(model.summary())

model.fit(X1_train, Y1_train, validation_data=(X1_test, Y1_test), epochs=2, verbose=2)

scores = model.evaluate(X1_test, Y1_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))


NASA_URL                = "http://www.nasa.gov"
USTREAM_URL             = 'http://www.ustream.tv/%s'
USTREAM_CHANNEL_URL     = 'http://www.ustream.tv/channel/%s'
PODCAST_URL             = 'http://www.nasa.gov/multimedia/podcasting/index.html'
ITUNES_NAMESPACE        = {'itunes':'http://www.itunes.com/dtds/podcast-1.0.dtd'}
TITLE                   = 'NASA'

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

def Start():

  ObjectContainer.title1 = TITLE
  HTTP.CacheTime = CACHE_1HOUR

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

@handler('/video/nasa', TITLE)
def MainMenu():
  oc = ObjectContainer()
  oc.add(DirectoryObject(key=Callback(NASATV), title='NASA TV'))
  oc.add(DirectoryObject(key=Callback(PodcastChooser, mediaType='video'), title='Video Podcasts'))
  return oc

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

@route('/video/nasa/podcast-chooser')
def PodcastChooser(mediaType):
  oc = ObjectContainer(title2='Video Podcasts')

  page = HTML.ElementFromURL(PODCAST_URL)

  for feed in page.xpath('//a[contains(@href,"vodcast.rss")]'):
    Log(XML.StringFromElement(feed))

    try:
      url = NASA_URL + feed.xpath('./following-sibling::a')[0].get('href')
      rss_url = NASA_URL + feed.get('href')
      title = feed.xpath('./following-sibling::a')[0].text.strip()
    except:
      continue
      # try:
      #   url = feed.get('href')
      # except:
      #   Log('Found a vodcast.rss but couldn\'t extract the url.')
      #   continue # If we can't extract these from following-sibling, it's not a podcast we want to list.

    Log('RSS URL: ' + url)
    podcast = RSS.FeedFromURL(rss_url)
    thumb = podcast.feed.image.href

    oc.add(DirectoryObject(key=Callback(PodcastEpisodes, title=title, url=url, rss_url=rss_url), title=title, thumb=Resource.ContentsOfURLWithFallback(thumb)))

  return oc

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

@route('/video/nasa/podcast-episodes')
def PodcastEpisodes(title, url, rss_url):
  # Shows available episodes in the selected podcast

  Log('Listing PodcastEpisodes for URL: ' + url)
  podcast = XML.ObjectFromURL(rss_url)

  try:
    t2 = podcast.xpath('/rss/channel/').get('title')
  except:
    t2 = title
  oc = ObjectContainer(title1='Video Podcasts', title2=t2)

  try:
    thumb = podcast.xpath('/rss/channel/itunes:image', namespaces=ITUNES_NAMESPACE)[0].get('href')
  except:
    thumb = None

  for entry in podcast.xpath('//rss/channel/item'):
    title = entry.xpath('./title')[0].text
    # url = entry.xpath('./enclosure')[0].get('url')
    url = entry.xpath('./link')[0].text

    try:
     summary = HTML.ElementFromString(entry.description)[0].text_content()
    except:
      summary = ''

    try:
      date = Datetime.ParseDate(entry.date).strftime('%a %b %d, %Y')
    except:
      date = None

    # Durations appear to be bogus on these items...
    duration = 0

    oc.add(VideoClipObject(url=url, title=title, summary=summary, duration=0, thumb=Resource.ContentsOfURLWithFallback(thumb)))

  return oc

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

@route('/video/nasa/nasatv')
def NASATV():
  oc = ObjectContainer(title2=L('nasatv'))

  oc.add(URLService.MetadataObjectForURL(USTREAM_URL % 'nasahdtv'))
  oc.add(URLService.MetadataObjectForURL(USTREAM_CHANNEL_URL % 'live-iss-stream'))

  return oc


'''Продолжить работу над заданием. В программу должна попадать строка из слов, разделенных пробелом.
Каждое слово состоит из латинских букв в нижнем регистре.
Сделать вывод исходной строки, но каждое слово должно начинаться с заглавной буквы.
Необходимо использовать написанную ранее функцию int_func().'''

from HW3_6 import int_func  # это еще не проходили, но сделал так, чтобы не дублировать функцию сюда

res = []

string = input("Введите строку: ").split(" ")
for word in string:
    res.append(int_func(word))

print(" ".join(res))


import re, nltk, string, glob, os

#Opens a text file and clears it of any html tags
def processfile(direction):

    current_read = open(direction, 'rb')
    raw_data = current_read.read().decode('ISO-8859-1')
    cleanr = re.compile('<.*?>')
    cleantext = re.sub(cleanr, '', raw_data)
    return ' '.join(raw_data.split())

#Finds all 4-word phrases beginning with sound(s) of xx xx xx xx
def findsounds(filename):
    new_data = processfile(filename)
    pattern = 'sounds?\sof((\s\S+){4})'
    results = re.findall(pattern, new_data)
    results = map(lambda x: x[0], results)
    return results

#Classifies phrase into one of 6 POS tags or returns false
def checktagger(tags):

    justtags = map(lambda x: x[1], tags)
    justtags = list(justtags)

    if justtags[0] == 'VBG':
        if justtags[1] == 'NN' or justtags[1] == 'NNS':
            return (True, 2) #(DT) VBG NN(S)
        else:
            return (True, 1) # VBG

    elif justtags[0] == 'NN' or justtags[0] == 'NNS':
        if (justtags[1] =='NN' or justtags[1] == 'NNS') and justtags[0] == 'NN':
            return (True, 2) # (DT) NN NN(S)
        elif justtags[1] == 'VBG':
            return (True, 2) # (DT) NN(S) VBG
        else:
            return (True, 1) # (DT) NN(S)

    elif justtags[0] == 'JJ':
        if justtags[1] =='NN' or justtags[1] == 'NNS':
            return (True, 2) # (DT) JJ NN(S)

    return (False, 0) # did not match

allphrases = []

# tokenizes phrases and does POS tagging
def getsoundlist(directory, num_files):

    allphrases = []
    for i in range(num_files):

        phrases = findsounds(directory + str(i))
        for phrase in phrases:

            try:

                splitphrase = nltk.word_tokenize(phrase)
                tagger = nltk.pos_tag(splitphrase)

                if tagger[0][1] == 'DT':
                    tagger.pop(0)
                    splitphrase.pop(0)

                correct_fit = checktagger(tagger)
                if correct_fit[0]:
                    good_fit = ' '.join(splitphrase[0:correct_fit[1]])
                    good_fit = good_fit.lower()
                    translator = str.maketrans('','',string.punctuation)
                    good_fit = good_fit.translate(translator)
                    allphrases.append(good_fit)

            except:
                pass

    return allphrases

# top level function
def usemydata():
    results = getsoundlist('../Downloads/blogs/', 19320)
    results += getsoundlist('../Downloads/Gutenberg/txt/', 3036)
    results =  list(set(results))

    return results

# renames all files in this directory to numbers
path1 = '/home/radu/Downloads/Gutenberg/txt'
count = 0
for filename in glob.iglob(os.path.join(path1, '*.txt')):
    title, ext = os.path.splitext(os.path.basename(filename))
    os.rename(filename, os.path.join(path1, str(count)))
    count += 1

# renames all files in this directory to numbers
path1 = '/home/radu/Downloads/blogs'
count = 0
for filename in glob.iglob(os.path.join(path1, '*.txt')):
    title, ext = os.path.splitext(os.path.basename(filename))
    os.rename(filename, os.path.join(path1, str(count)))
    count += 1

dataset = usemydata()

# uncomment to write results to file

'''
X = open('X', 'w')

for phrase in dataset:
    X.write(phrase + '\n')
X.close()
'''


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('chart', '0002_point'),
    ]

    operations = [
        migrations.RenameField(
            model_name='point',
            old_name='x',
            new_name='x1',
        ),
        migrations.RenameField(
            model_name='point',
            old_name='y',
            new_name='x2',
        ),
        migrations.AddField(
            model_name='point',
            name='y1',
            field=models.DecimalField(default=2, decimal_places=2, max_digits=5),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='point',
            name='y2',
            field=models.DecimalField(default=4, decimal_places=2, max_digits=5),
            preserve_default=False,
        ),
    ]


from flask import Flask
from flask import render_template

app = Flask(__name__)

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

@app.route('/products')
def products():
    # haal de afbeeldingen op
    import os
    list_afbeeldingen = os.listdir(os.path.dirname(os.path.abspath(__file__))+'/static/images')
    # haal de productinfo op uit de tekstfile
    list_products = lees_producten('products.txt')
    return render_template('products.html', images=list_afbeeldingen, products = list_products)

@app.route('/productOverview/<productname>')
def product_overview(productname):
    list_products = lees_producten('products.txt')

    result = ""

    for product in list_products:
        if product[0] == productname:
            result = product

    if result=="":
        return render_template('dummy.html', tekst='product niet gevonden')
    else:
        return render_template('productOverview.html', product=result)

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

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

# errorhandlers
@app.errorhandler(404)
def page_not_found(error):
    return render_template('errors/404.html', error=error)

@app.errorhandler(500)
def internal_error(error):
    return render_template('errors/505.html', error=error)

def lees_producten(bestand):
    import os
    bestandsnaam = os.path.dirname(os.path.abspath(__file__)) + "/static/" + bestand
    list_products = []
    try:
        fp = open(bestandsnaam, 'r')
    except FileNotFoundError:
        print("File not Found!")
        return False
    lijn = fp.readline()
    while lijn != "":
        list_lijn = lijn.split(":")
        list_products.append(list_lijn)
        lijn = fp.readline()
    fp.close()
    return list_products

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


from django.contrib import admin
from . import models
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User

class CustomerInline(admin.StackedInline):
    model = models.Customer
    can_delete = False
    verbose_name_plural = 'customer'

class StaffInline(admin.StackedInline):
    model = models.Staff
    can_delete = False
    verbose_name_plural = 'staff'

class UserAdmin(BaseUserAdmin):
    inlines = (CustomerInline,StaffInline)

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

# @admin.register(models.Customer)
# class CustomerAdmin(admin.ModelAdmin):
#     list_display = ['user_type', 'is_approved']
#     class Meta:
#         model=models.Customer

# @admin.register(models.Staff)
# class CustomerAdmin(admin.ModelAdmin):
#     list_display = ['user_type', 'is_approved']
#     class Meta:
#         model=models.Staff

#admin.site.register(models.Review)


__author__ = 'binarycat.ru'

import platform
import os
import os.path
import getpass
from datetime import datetime

class Logger(object):
	"""Logger"""

	currentUser = getpass.getuser();

	def __init__(self, logFile, test):
		self.test = test
		self.msg = ""

		nSystem = platform.system().lower()
		if(nSystem == "linux"  or nSystem == "java"):
			nFileName = logFile.replace('~/',os.path.expanduser('~/'))
		else:
			nFileName = logFile

		if logFile is None:
			self.logFile = None
		elif(os.path.exists(nFileName)):
			if(os.path.isdir(nFileName)):
				self.logFile = None
			else:
				self.logFile = nFileName
		else:
			self.logFile = nFileName

		return;

	def LogStart(self):
		self.__LogToFile(self.logFile, "run")

		return;

	def LogEnd(self):
		self.__LogToFile(self.logFile, "finish")

		return;

	def Log(self,msg):
		self.__LogToFile(self.logFile, msg)

		return;

	def __LogToFile(self, logFile, msg):

		msg = str(datetime.now()) + " | user: "+Logger.currentUser+" | " + msg

		if(self.test is True):
			print(msg)
		elif(logFile is None):
			print(msg)

		try:
			mode = 'a' if os.path.exists(logFile) else 'w'
			f = open(logFile, mode)
			f.write(msg+'\n')
			f.close()
		except Exception as e:
			print(str(e))

		return;


def mutate_list(a_list, index_num, v):
    ''' Inserts v at index index_num in a_list'''
    a_list.insert(index_num, v)

def remove_ch(a_list, index_num):
    ''' Removes character at index_num from a_list'''
    a_list.pop(index_num)

def get_list():
    ''' Reads in values for a list and returns the list '''
    user_list = input()
    user_list = user_list.split(",")
    user_list = [int(i) for i in user_list]
    return user_list

# Main program starts here
my_list = get_list()
print(my_list)
choice = input("Enter choice (m,r): ")
if choice.lower() == "m":
    index_number = input("Enter where you want to add a number and which number: ")
    index, number = index_number.split(",")
    index_int = int(index)
    number_int = int(number)
    mutate_list(my_list, index_int, number_int)
elif choice.lower() == "r":
    placement = int(input())
    remove_ch(my_list, placement)
print(my_list)


# This module can be used as a startup script.
#
# It will load all the other modules that offer
# the ldmud_efuns entry point.

import pkg_resources, traceback
import ldmud

for entry_point in pkg_resources.iter_entry_points('ldmud_efuns'):
    try:
        entry_point.load()()
    except:
        traceback.print_exc()


from ops import *
from utils import *
import os

class ResNet(object):
    def __init__(self, sess, args):
        self.model_name = 'ResNet'
        self.sess = sess
        self.dataset_name = args.dataset

        if self.dataset_name == 'myself':
            test_image_dir = ["test1.png", "test2.png", "test3.png"]
            """测试使用照片,test_img_dir 存放在列表中"""
            self.test_x = load_myself(test_image_dir)
            self.img_size = 200
            self.c_dim = 1
            self.label_dim = 3

        self.checkpoint_dir = args.checkpoint_dir
        self.res_n = 18
        self.batch_size = len(self.test_x)
        self.init_lr = args.lr

    def network(self, x, is_training=True, reuse=False):
        with tf.variable_scope("network", reuse=reuse):
            residual_block = resblock

            residual_list = get_residual_layer(self.res_n)

            ch = 32
            x = conv(x, channels=ch, kernel=3, stride=1, scope='conv')

            for i in range(residual_list[0]):
                x = residual_block(x, channels=ch, is_training=is_training, downsample=False, scope='resblock0_' + str(i))

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

            x = residual_block(x, channels=ch*2, is_training=is_training, downsample=True, scope='resblock1_0')

            for i in range(1, residual_list[1]):
                x = residual_block(x, channels=ch*2, is_training=is_training, downsample=False, scope='resblock1_' + str(i))

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

            x = residual_block(x, channels=ch*4, is_training=is_training, downsample=True, scope='resblock2_0')

            for i in range(1, residual_list[2]):
                x = residual_block(x, channels=ch*4, is_training=is_training, downsample=False, scope='resblock2_' + str(i))

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

            x = residual_block(x, channels=ch*8, is_training=is_training, downsample=True, scope='resblock_3_0')

            for i in range(1, residual_list[3]):
                x = residual_block(x, channels=ch*8, is_training=is_training, downsample=False, scope='resblock_3_' + str(i))

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

            x = batch_norm(x, is_training, scope='batch_norm')
            x = relu(x)

            x = global_avg_pooling(x)
            x = fully_conneted(x, units=self.label_dim, scope='logit')

            return x

    def build_model(self):

        self.train_inptus = tf.placeholder(tf.float32, [self.batch_size, self.img_size, self.img_size, self.c_dim], name='train_inputs')
        self.train_labels = tf.placeholder(tf.float32, [self.batch_size, self.label_dim], name='train_labels')

        self.test_inptus = tf.placeholder(tf.float32, [self.batch_size, self.img_size, self.img_size, self.c_dim], name='test_inputs')
        self.predict_labels = tf.placeholder(tf.float32, self.batch_size, name='predict_labels')
        self.probability = tf.placeholder(tf.float32, self.batch_size, name='probability')

        self.lr = tf.placeholder(tf.float32, name='learning_rate')

        """ Model """
        self.train_logits = self.network(self.train_inptus)
        self.test_logits = self.network(self.test_inptus, is_training=False, reuse=True)

        self.predict_labels, self.probability= classification_loss(logit=self.test_logits)

    def load(self, checkpoint_dir):
        ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
            self.saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name))

    def test(self):
        tf.global_variables_initializer().run()

        self.saver = tf.train.Saver()
        self.load(self.checkpoint_dir)

        test_feed_dict = {
            self.test_inptus: self.test_x,
        }

        predict_labels, probability = self.sess.run([self.predict_labels, self.probability], feed_dict=test_feed_dict)

        for i in range(len(predict_labels)):
            print("image", i+1, ": predict_labels: ", predict_labels[i], ",probability :%.4f"% (max(probability[i])))


# (C) Datadog, Inc. 2019
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os

import pytest
import requests

from datadog_checks.dev.conditions import CheckEndpoints
from datadog_checks.dev.kube_port_forward import port_forward
from datadog_checks.dev.terraform import terraform_run
from datadog_checks.dev.utils import get_here

try:
    from contextlib import ExitStack
except ImportError:
    from contextlib2 import ExitStack

DEPLOYMENTS = [
    ('istio-citadel', 15014),
    ('istio-galley', 15014),
    ('istio-pilot', 15014),
    ('istio-telemetry', 15014),
    ('istio-telemetry', 42422),
    ('istio-ingressgateway', 80),
]

@pytest.fixture(scope='session')
def dd_environment():
    with terraform_run(os.path.join(get_here(), 'terraform')) as outputs:
        kubeconfig = outputs['kubeconfig']['value']
        with ExitStack() as stack:
            ip_ports = [
                stack.enter_context(port_forward(kubeconfig, 'istio-system', deployment, port))
                for (deployment, port) in DEPLOYMENTS
            ]
            instance = {
                'citadel_endpoint': 'http://{}:{}/metrics'.format(*ip_ports[0]),
                'galley_endpoint': 'http://{}:{}/metrics'.format(*ip_ports[1]),
                'pilot_endpoint': 'http://{}:{}/metrics'.format(*ip_ports[2]),
                'mixer_endpoint': 'http://{}:{}/metrics'.format(*ip_ports[3]),
                'istio_mesh_endpoint': 'http://{}:{}/metrics'.format(*ip_ports[4]),
            }
            page = 'http://{}:{}/productpage'.format(*ip_ports[5])
            # Check a bit to make sure it's available
            CheckEndpoints([page], wait=5)()
            for _ in range(5):
                # Generate some traffic
                requests.get(page)
            yield instance


# -*- coding: utf-8 -*-
r"""
Created on Mon Aug 20 11:23:26 2018

@author: F. Obersteiner, florian\obersteiner\\kit\edu
"""

import numpy as np
from numba import njit
from scipy.interpolate import interp1d

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

def filter_jumps_simple(v, max_delta, no_val=-1, add_v1=None, add_v2=None,
                        delete_vals=False, use_abs_delta=True):

    max_delta = abs(max_delta)

    result = {'filtered': None,
              'ix_del': [],
              'ix_rem': [],
              'add_v1': add_v1,
              'add_v2': add_v2}

    for ix, v_ix in enumerate(v):
        if ix == 0:
            buffer = v_ix
            result['ix_rem'].append(ix)
            continue

        if use_abs_delta:
            delta = abs(v_ix-buffer)
        else:
            delta = v_ix-buffer

        if delta > max_delta or delta < 0.:
            v[ix] = no_val
            result['ix_del'].append(ix)
        else:
            buffer = v_ix
            result['ix_rem'].append(ix)

    if delete_vals:
        result['filtered'] = [element for element in v if element != no_val]

    if delete_vals and add_v1:
        result['add_v1'] = []
        for ix in result['ix_rem']:
            result['add_v1'].append(add_v1[ix])

    if delete_vals and add_v2:
        result['add_v2'] = []
        for ix in result['ix_rem']:
            result['add_v2'].append(add_v2[ix])

    return result

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

def mask_repeated_elems(arr):
    """
    use numpy diff to mask repeated (adjacent) element in a numpy 1d array.
    - for small array sizes (<10000), a looped solution with
      numba njit is faster.
    """
    # n_el = arr.shape[0]
    # mask = np.ones(arr.shape).astype(np.bool_)
    # i = 0
    # while i < n_el-1:
    #     if arr[i] == arr[i+1]:
    #         cur = arr[i]
    #         for j in range(i+1, n_el):
    #             i += 1
    #             if cur == arr[j]:
    #                 mask[j] = False
    #             else:
    #                 break
    #     i += 1
    mask = np.ones(arr.shape).astype(np.bool_)
    mask[1:] = np.diff(arr).astype(np.bool_)
    return mask

@njit
def mask_jumps(arr, thrsh, look_ahead, abs_delta=False):
    """
    check the elements of array "arr" if the delta between element and
    following element(s) exceed a threshold "trsh". How many elements to
    look ahead is defined by "look_ahead"
    """
    n_el = arr.shape[0]
    mask = np.ones(arr.shape).astype(np.bool_)
    i = 0
    while i < n_el-1:
        cur, nxt = arr[i], arr[i+1]
        delta_0 = np.absolute(nxt-cur) if abs_delta else nxt-cur
        if delta_0 > thrsh:
            for value in arr[i+1:i+look_ahead+1]:
                delta_1 = np.absolute(value-cur) if abs_delta else value-cur
                if delta_1 > thrsh:
                    mask[i+1] = False
                    i += 1
                else:
                    break
        i += 1
    return mask

def filter_jumps_v2(arr, thrsh, look_ahead,
                    abs_delta=False,
                    vmiss=np.nan,
                    remove_repeated=False,
                    interpol_jumps=False, interpol_kind='linear'):
    """
    wrapper around mask_jumps()
    ! interpolation assumes equidistant spacing of the independent variable of
      which arr depends !
    """
    if not isinstance(arr, np.ndarray):
        raise ValueError("input array must be of class numpy ndarray.")
    if arr.ndim > 1:
        raise ValueError("input array must be numpy 1d array.")
    if not isinstance(look_ahead, int):
        raise ValueError("parameter look_ahead must be an integer.")
    if look_ahead >= arr.shape[0] or look_ahead < 1:
        raise ValueError(f"parameter look_ahead must be >=1 and <{arr.shape[0]}.")

    result = arr.copy() # do not touch the input...
    if not np.isnan(vmiss):
        result[vmiss] = np.nan
    if remove_repeated:
        result[~mask_repeated_elems(result)] = np.nan
    mask = mask_jumps(result, thrsh, look_ahead, abs_delta=abs_delta)
    result[~mask] = np.nan
    if interpol_jumps:
        f_ip = interp1d(np.arange(0, result.shape[0])[mask], result[mask],
                        kind=interpol_kind, fill_value='extrapolate')
        result = f_ip(np.arange(0, result.shape[0]))
        return (result, mask)
    return (result, mask)

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

def filter_jumps_np(v, max_delta, no_val=np.nan, use_abs_delta=True,
                    reset_buffer_after=3, remove_doubles=False,
                    # if v is dependent on another variable x (e.g. time),
                    # IF x is not equidistant, do NOT use interpolation:
                    interpol_jumps=False, interpol_kind='linear'):

    len_v = len(v)

    ix_del = np.full(len_v, -1, dtype='int32')  # deletion index
    ix_rem = np.full(len_v, -1, dtype='int32')  # remaining index

    buffer = [False, 0]

    for ix, v_ix in enumerate(v):
        if any([~np.isfinite(v_ix), v_ix == no_val, np.isnan(v_ix)]):
            ix_rem[ix] = ix
            continue  # skip line if value is np.nan

        if not buffer[0]:
            buffer[0] = v_ix
            ix_rem[ix] = ix
            continue  # fill buffer if not done so yet

        if use_abs_delta:
            delta = abs(v_ix-buffer[0])
        else:
            delta = v_ix-buffer[0]

        if delta > max_delta:  # jump found!
            v[ix] = no_val
            ix_del[ix] = ix
            buffer[1] += 1
            if reset_buffer_after:
                if buffer[1] == reset_buffer_after:
                    buffer = [v_ix, 0]
        else:  # no jump,...
            buffer[0] = v_ix
            if remove_doubles:  # check for double values...
                if delta == 0.:  # double found!
                    v[ix] = no_val
                    ix_del[ix] = ix
                else:  # no double
                    ix_rem[ix] = ix
            else:
                ix_rem[ix] = ix

    w_valid = np.where(ix_del != -1)
    ix_del = ix_del[w_valid]

    w_valid = np.where(ix_rem != -1)
    ix_rem = ix_rem[w_valid]

    if interpol_jumps:
        tmp_x = (np.arange(0, len_v))[ix_rem]
        tmp_y = v[ix_rem]
        f_ip = interp1d(tmp_x, tmp_y,
                        kind=interpol_kind, fill_value='extrapolate')
        filtered = f_ip(np.arange(0, len_v))
    else:
        w_valid = np.where(v != no_val)
        filtered = v[w_valid]

    return {'filtered': filtered,
            'ix_del': ix_del,
            'ix_rem': ix_rem}

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

def del_at_edge(v, n_cut, add=2, out_len='same'):
    """
    assume v to be a 1D array which contains blocks of NaNs.
    returns: v with "more NaNs", i.e. range of NaN-blocks is extended by n_cut.
    """

    tf = np.isfinite(v)*1.

    mask = np.convolve(tf, np.ones((int(n_cut+add),))/int(n_cut+add),
                       mode=out_len)

    if tf[0] > 0.9:
        mask[0] = 1.
    if tf[-1] > 0.9:
        mask[-1] = 1.

    mask[np.where(mask < 0.999)] = np.nan

    return v * mask

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


import os
from shutil import copyfile
import sys

def create_dir(parrent_dir = os.getcwd(), nums = 0 ):
    """
    Функция для создания директорий dir_1 - dir_nums в папке parrent_dir
    """
    for i in range(nums):
        try:
            dir_path = os.path.join(parrent_dir, 'dir' + str(i))
            os.mkdir(dir_path)
        except FileExistsError:
            print('Директория уже существует')

def remove_dir(dir_name):
    """
    Функция, для удаления указаной директории
    :param dir_name:
    :return:
    """
    try:
        os.removedirs(dir_name)
    except FileNotFoundError:
        print('Директория отсутствует')
    except PermissionError:
        print('Нет доступа для удаления')

def get_folders_cuttent_dir(path=os.getcwd()):
    """
    :param path: Дирректория
    :return: список папок дирректории
    """
    return [f for f in os.listdir(path) if not os.path.isfile(os.path.join(path, f))]

def copy_this(file_path = sys.argv[0]):
    """
    :param file_path: путь до файла, который тредуется скопировать
    """
    try:
        os.mkdir(os.path.join(os.getcwd(),"copy"))
    except FileExistsError:
        print('Директория уже существует')
    try:
        print(os.path.join(os.getcwd(),sys.argv[0]))
        new_file_name = str.split(sys.argv[0],".")[0] + "(copy)" +".py"
        copyfile(file_path, os.path.join(file_path, new_file_name))
    except FileNotFoundError:
        print('Файл не найден')
    except PermissionError:
        print('Нет доступа.')

def test():
    """
    Функция для тестирования работы описаныйх выше методов
    """
    create_dir(parrent_dir=os.getcwd(), nums=10)
    for i in range(10):
        remove_dir(os.path.join(os.getcwd(), 'dir' + str(i)))
    print(get_folders_cuttent_dir())
    copy_this()


from datetime import datetime
from dateutil.relativedelta import relativedelta
import pandas as pd
import numpy as np

def list_for_calendar(month=None, year=None):
    today = datetime.strftime(datetime.now(), '%Y-%m-%d')
    first_of_month = f'{year}-{month}-01'
    table_dates = pd.DataFrame(index=[i for i in range(6)], columns=[i for i in range(7)])
    first = pd.to_datetime(first_of_month) - relativedelta(days=pd.to_datetime(first_of_month).dayofweek)
    for i in range(table_dates.shape[0]):
        for j in range(table_dates.shape[1]):
            table_dates.iloc[i, j] = first + relativedelta(days=i * 7 + j)
    final_list = []
    for i in range(table_dates.shape[0]):
        week_list = []
        for j in range(table_dates.shape[1]):
            day_list = []
            day_list.append(table_dates.iloc[i, j].strftime("%d"))  # day of month (two digits)
            day_list.append(table_dates.iloc[i, j].strftime("%B"))  # month (full name)
            day_list.append(table_dates.iloc[i, j].strftime("%Y"))  # year (four digits)
            day_list.append(table_dates.iloc[i, j].strftime("%A"))  # day of week (full name)
            day_list.append(datetime.strftime(table_dates.iloc[i, j], '%Y-%B-%d'))  # full day
            week_list.append(day_list)
        final_list.append(week_list)
    next_nonth = pd.to_datetime(first_of_month) + relativedelta(months=1)
    prev_nonth = pd.to_datetime(first_of_month) - relativedelta(months=1)
    return final_list, next_nonth.strftime("%B"), next_nonth.strftime("%Y"),\
           prev_nonth.strftime("%B"), prev_nonth.strftime("%Y")


from django.conf.urls import patterns, include, url
import views

urlpatterns = patterns('',
    url(r'^main/',views.main, name="main" ),

    url(r'^maingm/', views.maingm, name="maingm" ),
    url(r'^create/crtskill/$', views.crtSkill, name="createskill"),
    url(r'^create/crtprof/$', views.crtProf, name="createprofession"),
    url(r'^create/crteffect/$', views.crtEff, name="createeffect"),
    url(r'^login/$', views.char_login, name="login"),
    url(r'^logout/$', views.char_logout, name="logout"),
    url(r'^log/$', views.show_log, name="log"),
    url(r'^roll/$', views.roll, name="roll"),
    url(r'^statup/(\w+)/$', views.statup, name="statup"),
)


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('yuesao_choice', '0003_yuesaoinfo_company'),
    ]

    operations = [
        migrations.AlterField(
            model_name='yuesaoinfo',
            name='company',
            field=models.ForeignKey(default=True, to='yusao_user.CompanyInfo'),
        ),
    ]


import os
import torch
import torch.nn as nn
import torch.optim as optim
import pytorch_lightning as pl

from models import get_resnet
from dataset import get_imagenet_loader
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning import loggers as pl_loggers

from datetime import datetime
from argparse import ArgumentParser
from omegaconf import OmegaConf

class BoilerNet(pl.LightningModule):
    def __init__(self, args):
        super().__init__()
        self.save_hyperparameters(args)

        cfg = OmegaConf.load(args.config)
        self.cfg = cfg

        self.model = get_resnet(cfg.model.ic, cfg.model.oc, cfg.model.model_type)
        self.loss = nn.CrossEntropyLoss()

    def forward(self, x):
        return self.model(x)

    def training_step(self, batch, batch_idx):
        x, y = batch
        pred = self(x)
        loss = self.loss(pred, y)

        output = {"loss": loss}

        self.logger.experiment.add_scalar("Training Loss", loss.item(), self.global_step)

        return output

    def validation_step(self, batch, batch_idx):
        x, y = batch
        pred = self(x)
        loss = self.loss(pred, y)

        acc = (pred.argmax(1) == y).sum() / float(x.shape[0])

        output = {
            "batch_val_loss": loss,
            "batch_val_acc": acc
        }

        return output

    def validation_epoch_end(self, outputs):
        avg_loss = torch.stack([o["batch_val_loss"] for o in outputs]).mean()
        avg_acc = torch.stack([o["batch_val_acc"] for o in outputs]).mean()

        output = {
            "val_loss": avg_loss,
            "val_acc": avg_acc
        }

        self.logger.experiment.add_scalar("Validation Loss", avg_loss.item(), self.global_step)
        self.logger.experiment.add_scalar("Validation Acc", avg_acc.item(), self.global_step)

        return output

    def configure_optimizers(self):
        lr = self.cfg.training.lr
        beta1 = self.cfg.training.beta1
        beta2 = self.cfg.training.beta2
        weight_decay = self.cfg.training.weight_decay
        opt_type = self.cfg.training.opt_type

        if opt_type == 'adam':
            opt = optim.Adam(self.model.parameters(), lr=lr, betas=(beta1, beta2), weight_decay=weight_decay)
        elif opt_type == 'sgd':
            opt = optim.SGD(self.model.parameters(), lr=lr, momentum=beta1, weight_decay=weight_decay)
        return opt

    def train_dataloader(self):
        num_gpus = self.hparams.num_gpus
        num_nodes = self.hparams.num_nodes
        dist_mode = self.hparams.dist_mode
        grad_acc = self.hparams.grad_acc
        bs, n_workers = self.cfg.training.bs, self.cfg.training.n_workers
        bs = inv_effective_bs(bs, num_gpus, num_nodes, dist_mode, grad_acc)

        train_dir = self.cfg.data.train_dir
        aug_type = self.cfg.augmentation.type
        dl = get_imagenet_loader(bs, n_workers, train_dir, aug_type, mode='train')
        return dl

    def val_dataloader(self):
        num_gpus = self.hparams.num_gpus
        num_nodes = self.hparams.num_nodes
        dist_mode = self.hparams.dist_mode
        grad_acc = self.hparams.grad_acc
        bs, n_workers = self.cfg.training.bs, self.cfg.training.n_workers
        bs = inv_effective_bs(bs, num_gpus, num_nodes, dist_mode, grad_acc)

        val_dir = self.cfg.data.val_dir
        aug_type = self.cfg.augmentation.type
        dl = get_imagenet_loader(bs, n_workers, val_dir, aug_type, mode='val')
        return dl

def effective_bs(bs, num_gpus, num_nodes, dist_mode, grad_acc):
    if dist_mode == 'dp':
        eff_bs = bs
    elif dist_mode == 'ddp' or dist_mode == 'horovod':
        eff_bs = bs * num_gpus * num_nodes
    elif dist_mode == 'ddp2':
        eff_bs = bs * num_nodes

    eff_bs *= grad_acc
    return eff_bs

def inv_effective_bs(eff_bs, num_gpus, num_nodes, dist_mode, grad_acc):
    if dist_mode == 'dp':
        bs = eff_bs
    elif dist_mode == 'ddp' or dist_mode == 'horovod':
        bs = eff_bs // num_gpus // num_nodes
    elif dist_mode == 'ddp2':
        bs = eff_bs // num_nodes

    bs //= grad_acc
    return bs

parser = ArgumentParser()
parser.add_argument('-c', '--config', type=str, default='config.yaml', help='.yaml config file')
parser.add_argument('-g', '--num-gpus', type=int, default=2, help='gpus')
parser.add_argument('-n', '--num-nodes', type=int, default=1, help='nodes')
parser.add_argument('-d', '--dist-mode', type=str, default='ddp', help='distributed modes')
parser.add_argument('-a', '--grad-acc', type=int, default=1, help='accumulated gradients')

parser.add_argument('-m', '--model-path-ckpt', type=str, help='model checkpoint path')
parser.add_argument('-r', '--resume-path-ckpt', type=str, default=None, help='resume training checkpoint path')
parser.add_argument('-e', '--experiment-name', type=str, default=None, help='experiment name')
parser.add_argument('-t', '--top-k-save', type=int, default=5, help='save top k')
parser.add_argument('-f', '--fast-dev-run', action='store_true', help='perform fast dev run')

args = parser.parse_args()
model = BoilerNet(args)
cfg = OmegaConf.load(args.config)

experiment_name = args.experiment_name
if experiment_name is None:
    experiment_name = datetime.now().strftime("%m%d%Y-%H:%M:%S")

ckpt_pth = os.path.join(args.model_path_ckpt, experiment_name)
log_dir = os.path.join(cfg.logging.log_dir, experiment_name)

os.makedirs(ckpt_pth, exist_ok=True)
ckpt_callback = ModelCheckpoint(
    filepath=ckpt_pth,
    monitor='val_loss',
    verbose=True,
    save_top_k=args.top_k_save
)

trainer = Trainer(
    logger=pl_loggers.TensorBoardLogger(log_dir),
    checkpoint_callback=ckpt_callback,
    weights_save_path=ckpt_pth,
    gpus=args.num_gpus,
    accumulate_grad_batches=args.grad_acc,
    distributed_backend='ddp',
    resume_from_checkpoint=args.resume_path_ckpt,
    gradient_clip_val=cfg.training.gradient_clip,
    fast_dev_run=args.fast_dev_run,
    max_epochs=cfg.training.epoch_num
)

trainer.fit(model)


# // https://practice.geeksforgeeks.org/problems/maximize-sum-after-k-negations/0

import heapq

T = int(input())

for t in range(T):
	N, k = map(int, input().split())
	array = list(map(int, input().split()))
	heapq.heapify(array)
	for i in range(k):
		small = heapq.heappop(array)
		heapq.heappush(array, -small)

	print(sum(array))


import clr, sys, types
import System
from UnityEngine import *

def log(item):
    if item == None:
        Debug.Log("None\n")
        return
    res = item
    itemType = type(item)
    if isinstance(item, str) or isinstance(item, types.FunctionType) or isinstance(item, types.ModuleType):
        pass
    elif isinstance(item, dict):
        items = [k.ToString() + " = " + x.ToString() for k, x in item.items()]
        res = "\n".join(items)
    elif isinstance(item, list) or item.GetType().GetMember("GetEnumerator") != None:
        items = [x.ToString() for x in item]
        res = "\n".join(items)
    Debug.Log("{0}\n{1}\n".format(itemType, res))

def logMembers(x):
    log(x.GetType().GetMembers())

def toPython(item):
    if item.GetType().GetMember("GetEnumerator") != None:
        res = []
        for x in item:
            res.append(x)
        return res
        # return [x for x in item]

def getClasses(moduleName=None, inheritedType=None):
    if not moduleName:
        moduleName = __name__

    def isClassObj(obj):
        try:
            issubclass(obj, object)
        except TypeError:
            return False
        else:
            return True

    allClasses = {}
    moduleObj = sys.modules[moduleName]
    for name in dir(moduleObj):
        obj = getattr(moduleObj, name)
        if isClassObj(obj):
            allClasses[name] = obj
    if not inheritedType:
        return allClasses
    else:
        res = {}
        for className in allClasses:
            classType = allClasses[className]
            if issubclass(classType, inheritedType):
                res[className] = classType
        return res

def getAllClasses(inheritedType=None):
    res = {}
    for moduleName in sys.modules:
        classes = getClasses(moduleName, inheritedType)
        for k in classes:
            res[k] = classes[k]
    return res

def getFiles(path, ext=None):
    files = System.IO.Directory.GetFiles(path, ext, System.IO.SearchOption.AllDirectories)
    return [f.replace("\\", "/") for f in files]

def printGlobalClasses():
    moduleNames = sys.modules.keys()
    moduleNames.sort()
    res = ""
    for mod in moduleNames:
        modContent = mod + "\n\t"
        allClasses = [key for key in getClasses(mod)]
        modContent += "\n\t".join(allClasses)
        res += "\n" + modContent
    log(res)


'''
Application factory methods
'''
import os
from datetime import datetime
from flask import Flask, Blueprint, jsonify, request, send_from_directory
from flask_cors import CORS
import tensorflow as tf

from .context import set_obj
from .images import save_image, images_dir, get_image_stats, set_image_stat, set_image_label
from .files import upload_file, upload_file_chunk, files_dir
from .models import upload_model, delete_model, get_models, update_model
from .datasets import upload_dataset, delete_dataset, get_datasets, update_dataset
from .trainings import add_training, delete_training, get_trainings, update_training, process_training
from .classifier import predict_face
from .generator import generate_image
from .entities import set_setting, get_settings

def build():
  '''
  Build flask application
  '''
  app = Flask(__name__, instance_relative_config=True)
  CORS(app)

  # ensure the instance folder exists
  os.makedirs(app.instance_path, exist_ok=True)

  # register blueprints
  app.register_blueprint(build_backend(), url_prefix='/backend')

  set_obj('app', app)
  return app

def build_backend():
  '''
  Build backend APIs blueprint
  '''
  backend = Blueprint('backend', __name__)

  # check status
  @backend.route('/status')
  def status():
    return jsonify({
      'status': 'ok',
      'tensorflow': {
        'version': tf.__version__,
        'gpu': tf.config.list_physical_devices('GPU') if tf.__version__.startswith('2.') else tf.test.gpu_device_name()
      },
      'now': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    })

  # get system settings
  @backend.route('/settings')
  def settings_get():
    return jsonify(get_settings())

  # set system settings
  @backend.route('/settings', methods=['POST'])
  def settings_post():
    req = request.get_json()
    count = 0
    for key in req:
      set_setting(key, req[key])
      count += 1
    return jsonify({'count': count})

  # retrieve images
  @backend.route('/image/<path:path>')
  def image_get(path):
    idx = path.rindex('/')
    folder = images_dir(path[0:idx])
    filename = path[idx+1:]
    return send_from_directory(folder, filename)

  # store images
  @backend.route('/image', methods=['POST'])
  def image_post():
    if request.files:
      image = request.files['image']
      result = save_image(image)
      return jsonify(result)
    else:
      return jsonify({'error': 'no image'})

  # image classification
  @backend.route('/image/classify', methods=['POST'])
  def image_classify():
    req = request.get_json()
    usecase = req['type']

    if(usecase == 'face'):
      img_uuid = req['image']['uuid']
      img_type = req['image']['type']

      mdl_name = None
      mdl_version = None
      if req['model'] is not None:
        mdl_name = req['model']['name']
        mdl_version = req['model']['version']

      result = predict_face(img_uuid, img_type, mdl_name, mdl_version)
      return jsonify(result)
    else:
      return jsonify({'error': 'invalid type'})

  # image generation
  @backend.route('/image/generate', methods=['POST'])
  def image_generate():
    req = request.get_json()
    if req['model'] is None:
      return jsonify({'error': 'missing model'})
    else:
      mdl_name = req['model']['name']
      mdl_version = req['model']['version']
      result = generate_image(mdl_name, mdl_version)
      return jsonify(result)

  # label image
  @backend.route('/image/label', methods=['POST'])
  def image_label_post():
    req = request.get_json()
    try:
      image = req['image']
      set_image_label(image['uuid'], image['type'], req['class_label'])
      return jsonify({})
    except KeyError as e:
      return jsonify({'error': f"missing {e.args[0]}"})

  # query image statistics
  @backend.route('/image/stats')
  def image_stats():
    return jsonify(get_image_stats())

  # update image statistics
  @backend.route('/image/stats', methods=['POST'])
  def image_stats_post():
    req = request.get_json()
    try:
      image = req['image']
      model = req['model']
      count = 0
      for stat in req['stats']:
        set_image_stat(image['uuid'], image['type'], model['name'], model['version'], stat['name'], stat['delta'])
        count += 1
      return jsonify({'count': count})
    except KeyError as e:
      return jsonify({'error': f"missing {e.args[0]}"})

  # handle large file uploading (init)
  @backend.route('/file/upload', methods=['POST'])
  def file_upload_init():
    req = request.get_json()
    try:
      file_uuid = req['uuid']
      file_type = req['type']
      upload_file(file_uuid, file_type)
      return jsonify({'uuid': file_uuid})
    except KeyError as e:
      return jsonify({'error': f"missing {e.args[0]}"})

  # handle large file uploading (chunk)
  @backend.route('/file/upload/<uuid>/<offset>', methods=['POST'])
  def file_upload_chunk(uuid, offset):
    if not request.files:
      return jsonify({'error': 'no files'})
    chunk = request.files['chunk']
    upload_file_chunk(uuid, offset, chunk.stream.read())
    return jsonify({'uuid': uuid})

  # get all models of type
  @backend.route('/model/<type>')
  def models_get(type):
    return jsonify(get_models(type))

  # adding model
  @backend.route('/model', methods=['POST'])
  def model_add():
    req = request.get_json()
    try:
      filename = f"{req['file']['uuid']}{req['file']['type']}"
      filepath = os.path.join(files_dir('upload'), filename)

      type = req['type']
      name = req['name']
      label = req['label']
      result = upload_model(type, name, label, filepath)
      return jsonify(result)
    except KeyError as e:
      return jsonify({'error': f"missing {e.args[0]}"})

  # updating model status
  @backend.route('/model/<uuid>/status/<status>', methods=['PATCH'])
  def model_update_status(uuid, status):
    return jsonify(update_model(uuid, status=status))

  # deleting model
  @backend.route('/model/<uuid>', methods=['DELETE'])
  def model_delete(uuid):
    return jsonify(delete_model(uuid))

  # get all datasets of type
  @backend.route('/dataset/<type>')
  def datasets_get(type):
    return jsonify(get_datasets(type))

  # adding dataset
  @backend.route('/dataset', methods=['POST'])
  def dataset_add():
    req = request.get_json()
    try:
      filename = f"{req['file']['uuid']}{req['file']['type']}"
      filepath = os.path.join(files_dir('upload'), filename)

      type = req['type']
      name = req['name']
      label = req['label']
      result = upload_dataset(type, name, label, filepath)
      return jsonify(result)
    except KeyError as e:
      return jsonify({'error': f"missing {e.args[0]}"})

  # updating dataset status
  @backend.route('/dataset/<uuid>/status/<status>', methods=['PATCH'])
  def dataset_update_status(uuid, status):
    return jsonify(update_dataset(uuid, status=status))

  # deleting dataset
  @backend.route('/dataset/<uuid>', methods=['DELETE'])
  def dataset_delete(uuid):
    return jsonify(delete_dataset(uuid))

  # get all trainings of type
  @backend.route('/training/<type>')
  def trainings_get(type):
    return jsonify(get_trainings(type))

  # adding training
  @backend.route('/training', methods=['POST'])
  def training_add():
    req = request.get_json()
    try:
      type = req['type']
      name = req['name']
      ensemble = req['ensemble']
      base_models = req['base_models']
      tr_settings = req['settings']
      tr_datasets = req['datasets']
      result = add_training(model_type=type, model_name=name, ensemble=ensemble, base_models=base_models, settings=tr_settings, datasets=tr_datasets)
      return jsonify(result)
    except KeyError as e:
      return jsonify({'error': f"missing {e.args[0]}"})

  # updating training status
  @backend.route('/training/<uuid>/status/<status>', methods=['PATCH'])
  def training_update_status(uuid, status):
    return jsonify(update_training(uuid, status=status))

  # deleting training
  @backend.route('/training/<uuid>', methods=['DELETE'])
  def training_delete(uuid):
    return jsonify(delete_training(uuid))

  # process training queue
  @backend.route('/training/process', methods=['POST'])
  def training_process():
    return jsonify(process_training())

  return backend


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

'''
You are given a string representing an attendance record for a student. The record only contains the following three characters:

'A' : Absent.
'L' : Late.
'P' : Present.
A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).

You need to return whether the student could be rewarded according to his attendance record.

Example 1:
Input: "PPALLP"
Output: True
Example 2:
Input: "PPALLL"
Output: False
'''

class Solution(object):
    def checkRecord(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if (s.count("A")>1):
            return False
        last1 = ""
        last2 = ""
        for t in s:
            if (t == "L" and last1 == "L" and last2 == "L"):
                return False
            last2 = last1
            last1 = t
        return True

def main():
    so = Solution()
    s = "PPALLPLLA"
    print(so.checkRecord(s))

if __name__ == '__main__':
    main()


# Databricks notebook source
# MAGIC %md
# MAGIC COPYRIGHT:  Columbia Sportswear 2019

# MAGIC DESCRIPTION: This notebook creates return quotation files in datalake that are to be sent to Salesforce.
# MAGIC
# MAGIC -----------------------------------------------------------------
# MAGIC ###### MODIFICATION LOG
# MAGIC | Programmmer          | Change Request  | Date       | Change Description                                                 |
# MAGIC |----------------------|-----------------|------------|--------------------------------------------------------------------|
# MAGIC | Bob Socolich | Enhancement | 11/20/2019|Initial Development|

# COMMAND ----------

dbutils.widgets.text("schmNm", "", "")
dbutils.widgets.text("tblNm", "", "")

dbutils.widgets.text("deltaTS", "", "")
dbutils.widgets.text("initFlg", "", "")

dbutils.widgets.get("deltaTS")
deltaTS = getArgument("deltaTS")

dbutils.widgets.get("initFlg")
initFlg = getArgument("initFlg")

dbutils.widgets.get("schmNm")
schmNm = getArgument("schmNm")

dbutils.widgets.get("tblNm")
tblNm = getArgument("tblNm")

deltaTS = deltaTS.replace('T',' ')

# COMMAND ----------

sqlQuery = """CREATE OR REPLACE TEMPORARY VIEW src_read
AS
SELECT
DESC,
FQR_NBR ,
RTN_QTTN_ID,
DEL_IND,
CASE WHEN INTGN_REPRCS = true THEN false ELSE INTGN_REPRCS END as INTGN_REPRCS,
LAST_MDFY_BY_ID,
LAST_MDFY_DT,
NM,
PRCS_AMT,
PRCS_UNITS,
RQ_AMT,
RQ_UNITS,
SLS_FRC_AGRMT_NM,
SLS_FRC_AGRMT_NBR
FROM ENTPR_SFA_VIEW.RTN_QTTN
WHERE EDW_UPDT_TS >= '{0}' AND DEL_IND = false""".format(deltaTS)
spark.sql(sqlQuery)

# COMMAND ----------

#select data to send back to SalesForce
df_updates = spark.sql("""
SELECT
RTN_QTTN_ID AS ID,
PRCS_AMT  AS PROCESSEDAMOUNT__C,
PRCS_UNITS   AS PROCESSEDUNITS__C,
RQ_AMT AS RQAMOUNT__C,
RQ_UNITS AS RQUNITS__C
FROM src_read""")

# COMMAND ----------

df_updates.write.format('parquet').mode('overwrite').save('/mnt/entadls/curated/eim/retail/internal/sfa/rtn_qttn_updates')


import os
import sys

import dash

from . import themes  # noqa
from ._version import __version__  # noqa

_current_path = os.path.dirname(os.path.abspath(__file__))

METADATA_PATH = os.path.join(_current_path, "metadata.json")

# Dash automatic class creation from `metadata.json` file.

_js_dist = [
    {
        "relative_package_path": "bundle.js",
        "namespace": "dash_bootstrap_components",
    }
]

_css_dist = []

def _setup_js_components(module, path_to_metadata):
    components = dash.development.component_loader.load_components(
        path_to_metadata, "dash_bootstrap_components"
    )
    for component in components:
        setattr(module, component.__name__, component)
        component._js_dist = _js_dist
        component._css_dist = _css_dist

_setup_js_components(sys.modules[__name__], METADATA_PATH)


from django.shortcuts import render
from django.views.generic import ListView, DetailView
from core.models import Movie, Person, Vote
from .forms import VoteForm, MovieImageForm
from django.shortcuts import reverse, redirect
from django.contrib.auth.mixins import LoginRequiredMixin
from  django.views.generic import UpdateView, CreateView
from django.core.exceptions import PermissionDenied
from django.contrib.auth.decorators import login_required
from django.urls import reverse_lazy

# Create your views here.

class MovieList(ListView):
    model = Movie
    paginate_by = 1

class UpdateVote(LoginRequiredMixin, UpdateView):
    form_class = VoteForm
    queryset = Vote.objects.all()

    def get_object(self, queryset=None):
        vote = super().get_object(
            queryset)
        user = self.request.user
        if vote.user != user:
            raise PermissionDenied(
                "can't change another user's vote") #вызывается ответ со статусом 403
        return vote

    def get_success_url(self):
        movie_id = self.object.movie.id
        return reverse(
            "core:MovieDetail",
            kwargs={'pk': movie_id})

    def render_to_response(self, context, **response_kwargs):
        movie_id = context['object'].id
        movie_detail_url = reverse(
            'core:MovieDetail',
            kwargs={'pk': movie_id})
        return redirect(
            to=movie_detail_url)

class CreateVote(LoginRequiredMixin, CreateView):
    form_class = VoteForm

    def get_initial(self):
        initial = super().get_initial()
        initial['user'] = self.request.user.id
        print(self.kwargs)
        initial['movie'] = self.kwargs[
            'movie_id']
        return initial

    def get_success_url(self):
        movie_id = self.object.movie.id
        return reverse(
            'core:MovieDetail',
            kwargs={
                'pk': movie_id})

    def render_to_response(self, context, **response_kwargs):
        movie_id = context['object'].id
        movie_detail_url = reverse(
            'core:MovieDetail',
            kwargs={'pk': movie_id})
        print(movie_detail_url)
        return redirect(
            to=movie_detail_url)

class MovieDetail(DetailView):
    queryset = Movie.objects.all_with_related_persons_and_score()

    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        ctx["image_form"] = self.movie_image_form()
        if self.request.user.is_authenticated:
            vote = Vote.objects.get_vote_or_unsaved_blank_vote(
                movie=self.object,
                user=self.request.user
            )
            if vote.id:
                vote_form_url = reverse_lazy(
                    'core:UpdateVote',
                    kwargs={
                        'movie_id': vote.movie.id,
                        'pk': vote.id})
            else:
                vote_form_url = (
                    reverse_lazy(
                        'core:CreateVote',
                        kwargs={
                            'movie_id': self.object.id}
                    )
                )
            vote_form = VoteForm(instance=vote)
            ctx['vote_form'] = vote_form
            ctx['vote_form_url'] = vote_form_url
        return ctx

    def movie_image_form(self):
        if self.request.user.is_authenticated:
            return MovieImageForm()
        return None

class PersonDetail(DetailView):
    queryset = Person.objects.all_with_prefetch_movies()

class MovieImageUpload(LoginRequiredMixin, CreateView):
    form_class = MovieImageForm

    def get_initial(self):
        initial = super().get_initial()
        initial['user'] = self.request.user.id
        initial['movie'] = self.kwargs['movie_id']
        return initial

    def render_to_response(self, context, **response_kwargs):
        movie_id = self.kwargs['movie_id']
        movie_detail_url = reverse(
            'core:MovieDetail',
            kwargs={'pk': movie_id})
        return redirect(
            to=movie_detail_url)

    def get_success_url(self):
        movie_id = self.kwargs['movie_id']
        movie_detail_url = reverse(
            'core:MovieDetail',
            kwargs={'pk': movie_id})
        return movie_detail_url

class ImageFileUpload(LoginRequiredMixin, CreateView):
    form_class = VoteForm
    queryset = Vote.objects.all()

    def get_object(self, queryset=None):
        vote = super().get_object(
            queryset)
        user = self.request.user
        if vote.user != user:
            raise PermissionDenied(
                'cannot change another users vote')
        return vote

    def get_success_url(self):
        movie_id = self.object.movie.id
        return reverse(
            'core:MovieDetail',
            kwargs={'pk': movie_id})

    def render_to_response(self, context, **response_kwargs):
        movie_id = context['object'].id
        movie_detail_url = reverse(
            'core:MovieDetail',
            kwargs={'pk': movie_id})
        return redirect(to=movie_detail_url)


from rest_framework import serializers
from api import models

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Category
        fields = 'id','name'

class ArticleSerialize(serializers.Serializer):
    id = serializers.IntegerField(required=False)
    category = CategorySerializer()
    title = serializers.CharField()
    image = serializers.CharField()
    text = serializers.CharField()


def isprime(n):
    if n> 1:
        for i in range(2, n// 2 + 1):
            if (n % i) == 0:
                return False
        else:
            return True
    else:
        return False

if n == 1:
    print("Fibonacci sequence : ", n, ":")
    print(a)
else:
    while count < n:
        if not isprime(n1) and a % 5 != 0:
            print(a, end=' ')
        else:
            print(0, end=' ')
        sums = a + b
        a = b
        b = sums
        count += 1


from app import app, socketio
from flask import render_template
from flask_socketio import emit
import uuid
import paho.mqtt.client as mqtt

# global mqtt-client instance
client = mqtt.Client(client_id=str(uuid.uuid4()), protocol='MQTTv311', clean_session=True)
thread = None
messages = [
"ok",
"connected",
"init",
"stop",
"wait",
"error",
"colorQuery",
"queryOk",
"brickAccepted",
"storageQuery",
"brickPushed",
"startConveyor",
"stopConveyor",
"ready",
"brickStored",
"finalPosition",
"goInitialPosition",
"disconnected"]

# decorators for different paths to be rendered
@app.route('/')
@app.route('/index')
def reactpanel():
    return render_template('reactpanel.html', async_mode=socketio.async_mode)

@app.route('/debug')
def index():
    return render_template('index.html', async_mode=socketio.async_mode)

# websocket message handlers
@socketio.on('connect')
def connect():
    global thread
    if thread is None:
        # start background thread for mqtt-message receiving
        thread = socketio.start_background_task(target=serverthread)

@socketio.on('mqtt_sender')
def mqtt_sender(msg):
    global client
    topic = 'listen/' + msg['rx']
    try:
        raw = [1, int(msg['type']), int(msg['ack']), int(msg['par1']), int(msg['par2'])]
        client.publish(topic, bytearray(raw))
    except ValueError:
        client.publish('error', 'invalid values')

@socketio.on('userMessage')
def userMessage(msg):
    global client
    topic = 'send/user'
    try:
        client.publish(topic, msg)
    except ValueError:
        client.publish('error', 'invalid values')

# emit mqtt debug messages to browser log view
def logger(msg):
    socketio.emit('mqttlog', {'data':msg})

# emit mqtt messages to the control panel
def inboundMessage(msg):
    socketio.emit('inbound', {'data':msg})

# background function to start and maintain mqtt connection
def serverthread():

    # connection callback
    def on_connect(client, userdata, rc):
        # listen all channels
        client.subscribe('#')

    # incoming message handler callback
    def on_message(client, userdata, msg):
        if msg.payload is None:
            logger('Topic: ' + msg.topic + ' -- Message: None')
        elif 'ui' in msg.topic:
            inboundMessage(msg.payload)
        elif 'user' not in msg.topic:
            p = bytearray(msg.payload)
            idx = msg.topic.index("/")
            beg = msg.topic[0:idx]
            if beg == "send":
                beg += "__"
            end = msg.topic[idx+1:]
            if end == "sorter":
                end += "__"
            topaz = beg+"/"+end
            #logger('Topic: ' + topaz + ' -- Message: ' + ",".join(map(lambda b: format(b, "d"), p)) + ' -- Length: ' + str(len(p)))
            logger('Topic: ' + topaz + ' -- Message: ' + format(p[0], "d") + ","+ messages[p[1]-1] + ","+format(p[2], "d") + ","+format(p[3], "d") + "," + format(p[4], "d"))

    # attach callbacks
    client.on_connect = on_connect
    client.on_message = on_message

    # connect to broker
    client.connect('192.168.42.1', 1883, 60)
    while True:
        # give time to socketio functions
        socketio.sleep(0.1)
        # maintain mqtt connection and receive messages
        client.loop(0.1)