# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render, redirect, HttpResponse
from .models import *
import datetime
from django.contrib import messages

def index(request):
    course_list = Course.objects.all()
    context = {
        'courses' : course_list
    }
    return render(request, 'courses/index.html', context)

def add_course(request):
    errors = Course.objects.course_validator(request.POST)
    if len(errors):
        for error, error_message in errors.iteritems():
            messages.error(request, error_message, extra_tags = error)
        return redirect('/')
    else:
        name = request.POST['name']
        description = request.POST['desc']
        new_course = Course.objects.create(name = name, desc = description)
        new_course.save()
    return redirect('/')

def remove_page(request, course_id):
    this_course = Course.objects.get(id = course_id)
    context = {
        'course' : this_course
    }
    return render(request, 'courses/delete_check.html', context)

def delete_course(request, course_id):
    this_course = Course.objects.get(id = course_id)
    this_course.delete()
    return redirect('/')


from functools import partial
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QSlider, QComboBox, QPushButton, QGroupBox, QGridLayout

class PresetPanel(QGroupBox):
	clicked = pyqtSignal(int)
	def __init__(self, n_buttons=9): #TODO make n a tuple, and populate a grid with x columns and y rows
		super().__init__("Presets")
		self.n_buttons = n_buttons
		self.setFixedHeight(100)
		self.grid = QGridLayout()
		self.setLayout(self.grid)
		self.buttons = []
		for i in range(n_buttons):
			button = QPushButton(f"Preset {i}")
			button.setFixedSize(100, 50) # set fixed size
			self.buttons.append(button)
			self.grid.addWidget(button, 0, i)
			button.clicked.connect(partial(self.clicked.emit, i))

	def animateClick(self, n):
		if n < self.n_buttons:
			self.buttons[n].animateClick()

class VariationPanel(QGroupBox):
	clicked = pyqtSignal(int)
	def __init__(self, n_buttons=9):
		super().__init__("Variations")
		self.n_buttons = n_buttons
		self.setFixedHeight(100)
		self.grid = QGridLayout()
		self.setLayout(self.grid)
		self.buttons = []
		# self.buttons_LUT = {"q": 0, "w": 1, "e": 2, "r": 3, "t": 4, "z": 5, "u": 6, "i": 7, "o": 8}
		for i in range(n_buttons):
			button = QPushButton(f"Variation {i}")
			button.setFixedSize(100, 50)
			self.buttons.append(button)
			self.grid.addWidget(button, 0, i)
			button.clicked.connect(partial(self.clicked.emit, i))

	def animateClick(self, n):
		if n < self.n_buttons:
			self.buttons[n].animateClick()

class HotbuttonPanel(QGroupBox):
	updated = pyqtSignal(list)
	def __init__(self, n_buttons=8):
		super().__init__("Hotbuttons")
		self.n_buttons = n_buttons
		self.setFixedHeight(100)
		self.grid = QGridLayout()
		self.setLayout(self.grid)
		self.buttons = []
		self.button_states = [0] * n_buttons
		for i in range(n_buttons):
			button = QPushButton(f"Hotbutton {i}")
			button.setFixedSize(100, 50)
			self.buttons.append(button)
			self.grid.addWidget(button, 0, i)
			button.pressed.connect(partial(self.pressed, i))
			button.released.connect(partial(self.released, i))

	def pressed(self, i):
		self.button_states[i] = 255
		self.updated.emit(self.button_states)

	def released(self, i):
		self.button_states[i] = 0
		self.updated.emit(self.button_states)

	def animatePress(self, i):
		if i < self.n_buttons:
			self.buttons[i].setDown(True)
			self.pressed(i)

	def animateRelease(self, i):
		if i < self.n_buttons:
			self.buttons[i].setDown(False)
			self.released(i)

class PalettePanel(QGroupBox):
	clicked = pyqtSignal(int)
	def __init__(self, n_buttons=5):
		super().__init__("Palettes")
		self.n_buttons = n_buttons
		self.setFixedHeight(100)
		self.grid = QGridLayout()
		self.setLayout(self.grid)
		self.buttons = []
		for i in range(n_buttons):
			button = QPushButton(f"Palette {i}")
			button.setFixedSize(100, 50)
			self.buttons.append(button)
			self.grid.addWidget(button, 0, i)
			button.clicked.connect(partial(self.clicked.emit, i))

	def animateClick(self, n):
		if n < self.n_buttons:
			self.buttons[n].animateClick()


import torch
from data.CustomDataset import CustomDataset
from random import shuffle
import pickle
import pathlib

class Dataloader:
    def __init__(self, path, image_size, batch_size=16, image_format='png',
                 validation_required=(False, 0.2, 'train_valid_split')):
        self.path = path
        self.image_size = image_size
        self.image_format = image_format
        self.validation_req = validation_required
        self.batch_size = batch_size

    def get_data_loader(self, load_indexes=True, save_indexes=False):
        dataset = CustomDataset(self.path, self.image_size, self.image_format)
        print(dataset)
        size = len(dataset)

        train_index = list(range(size))
        valid_index = []

        if self.validation_req[0]:
            file_path = '{}/{}'.format(self.path, self.validation_req[2])
            path = pathlib.Path(file_path)
            if path.exists() and load_indexes:
                with open(file_path, 'rb') as file:
                    save_dict = pickle.load(file)
                    train_index = save_dict['train']
                    valid_index = save_dict['valid']
                    print('Index files have been loaded!')
            else:
                train_size = int(size * (1 - self.validation_req[1]))
                indexes = list(range(size))
                shuffle(indexes)
                train_index = indexes[0:train_size]
                valid_index = indexes[train_size:]

                if save_indexes:
                    with open(file_path, 'wb') as file:
                        save_dict = {'train': train_index, 'valid': valid_index}
                        pickle.dump(save_dict, file)
                        print('Index files have been saved!')

        train_sampler = torch.utils.data.SubsetRandomSampler(train_index)
        valid_sampler = torch.utils.data.SubsetRandomSampler(valid_index)

        trainloader = None
        validloader = None
        if len(valid_index) == 0:
            trainloader = torch.utils.data.DataLoader(dataset, shuffle=True, batch_size=self.batch_size)
        else:
            trainloader = torch.utils.data.DataLoader(dataset, sampler=train_sampler, batch_size=self.batch_size)
            validloader = torch.utils.data.DataLoader(dataset, sampler=valid_sampler, batch_size=1)

        return trainloader, validloader


import os

import pytest

from topaz.objects.ioobject import W_IOObject

from ..base import BaseTopazTest

class TestIO(BaseTopazTest):
    def test_constants(self, space):
        assert space.int_w(space.execute("return IO::SEEK_CUR")) == os.SEEK_CUR
        assert space.int_w(space.execute("return IO::SEEK_END")) == os.SEEK_END
        assert space.int_w(space.execute("return IO::SEEK_SET")) == os.SEEK_SET

    def test_new_from_file(self, space, tmpdir):
        contents = "foo\nbar\nbaz\n"
        f = tmpdir.join("file.txt")
        f.write(contents)

        w_res = space.execute("""
        f = File.new('%s')
        io = IO.new(f)
        return io.read
        """ % f)
        assert space.str_w(w_res) == contents

    def test_new_from_fd(self, space):
        w_res = space.execute("return IO.new(1)")
        assert isinstance(w_res, W_IOObject)

    def test_write(self, space, capfd, tmpdir):
        content = "foo\n"
        space.execute('return IO.new(1, "w").write("%s")' % content)
        out, err = capfd.readouterr()
        assert out == content
        content = "foo\n"

        f = tmpdir.join("file.txt")
        with self.raises(space, "IOError", "closed stream"):
            space.execute("""
            io = File.new('%s', "w")
            io.close
            io.write("")
            """ % f)

    def test_push(self, space, capfd, tmpdir):
        space.execute('return IO.new(1, "w") << "hello" << "world"')
        out, err = capfd.readouterr()
        assert out == "helloworld"

        f = tmpdir.join("file.txt")
        with self.raises(space, "IOError", "closed stream"):
            space.execute("""
            io = File.new('%s', "w")
            io.close
            io << ""
            """ % f)

    def test_read(self, space, tmpdir):
        contents = "foo\nbar\nbaz\n"
        f = tmpdir.join("file.txt")
        f.write(contents)

        w_res = space.execute("return File.new('%s').read" % f)
        assert space.str_w(w_res) == contents

        w_res = space.execute("return File.new('%s').read(4)" % f)
        assert space.str_w(w_res) == contents[:4]

        w_res = space.execute("""
        a = 'hello world'
        File.new('%s').read(10, a)
        return a
        """ % f)
        assert space.str_w(w_res) == contents[:10]

        with self.raises(space, "ArgumentError"):
            space.execute("File.new('%s').read(-1)" % f)

        with self.raises(space, "IOError", "closed stream"):
            space.execute("""
            io = File.new('%s')
            io.close
            io.read
            """ % f)

    def test_simple_print(self, space, capfd, tmpdir):
        space.execute('IO.new(1, "w").print("foo")')
        out, err = capfd.readouterr()
        assert out == "foo"

        f = tmpdir.join("file.txt")
        with self.raises(space, "IOError", "closed stream"):
            space.execute("""
            io = File.new('%s', "w")
            io.close
            io.print ""
            """ % f)

    def test_multi_print(self, space, capfd):
        space.execute('IO.new(1, "w").print("This", "is", 100, "percent")')
        out, err = capfd.readouterr()
        assert out == "Thisis100percent"

    def test_print_globals(self, space, capfd):
        space.globals.set(space, "$,", space.newstr_fromstr(":"))
        space.globals.set(space, "$\\", space.newstr_fromstr("\n"))
        space.execute('IO.new(1, "w").print("foo", "bar", "baz")')
        space.globals.set(space, "$_", space.newstr_fromstr('lastprint'))
        space.execute('IO.new(1, "w").print')
        out, err = capfd.readouterr()
        assert out == "foo:bar:baz\nlastprint\n"

    def test_non_string_print_globals(self, space, capfd):
        space.globals.set(space, "$,", space.w_nil)
        space.globals.set(space, "$\\", space.w_nil)
        space.execute('IO.new(1, "w").print("foo", "bar", "baz")')
        space.globals.set(space, "$_", space.w_nil)
        space.execute('IO.new(1, "w").print')
        out, err = capfd.readouterr()
        assert out == "foobarbaz"

    def test_puts(self, space, capfd, tmpdir):
        space.execute("IO.new(1, 'w').puts('This', 'is\n', 100, 'percent')")
        out, err = capfd.readouterr()
        assert out == "This\nis\n100\npercent\n"

        f = tmpdir.join("file.txt")
        with self.raises(space, "IOError", "closed stream"):
            space.execute("""
            io = File.new('%s', "w")
            io.close
            io.puts ""
            """ % f)

    def test_flush(self, space, capfd, tmpdir):
        space.execute("IO.new(1, 'w').flush.puts('String')")
        out, err = capfd.readouterr()
        assert out == "String\n"

        f = tmpdir.join("file.txt")
        with self.raises(space, "IOError", "closed stream"):
            space.execute("""
            io = File.new('%s', "w")
            io.close
            io.flush
            """ % f)

    def test_globals(self, space, capfd):
        w_res = space.execute("""
        STDOUT.puts("STDOUT")
        $stdout.puts("$stdout")
        $>.puts("$>")
        STDERR.puts("STDERR")
        $stderr.puts("$stderr")
        return STDIN.read, $stdin.read
        """)
        out, err = capfd.readouterr()
        assert out == "STDOUT\n$stdout\n$>\n"
        assert err == "STDERR\n$stderr\n"
        assert self.unwrap(space, w_res) == [None, None]

    def test_rewind(self, space, tmpdir):
        f = tmpdir.join("file.txt")
        f.write("content")
        w_res = space.execute("""
        f = File.new('%s', "r+")
        c = f.read
        f.rewind
        return c, f.read
        """ % f)
        assert self.unwrap(space, w_res) == ["content", "content"]
        with self.raises(space, "IOError", "closed stream"):
            space.execute("""
            io = File.new('%s')
            io.close
            io.rewind
            """ % f)

    def test_seek(self, space, tmpdir):
        f = tmpdir.join("file.txt")
        f.write("content")
        w_res = space.execute("""
        res = []
        f = File.new('%s', "r+")
        f.seek(2, IO::SEEK_SET)
        res << f.read
        f.seek(2)
        res << f.read
        f.seek(-3, IO::SEEK_CUR)
        res << f.read
        f.seek(-2, IO::SEEK_END)
        res << f.read
        return res
        """ % f)
        assert self.unwrap(space, w_res) == [
            "ntent", "ntent", "ent", "nt"
        ]
        with self.raises(space, "IOError", "closed stream"):
            space.execute("""
            io = File.new('%s')
            io.close
            io.seek 2
            """ % f)

    def test_pos(self, space, tmpdir):
        f = tmpdir.join("file.txt")
        f.write("words in here")
        w_res = space.execute("""
        f = File.new('%s', "r+")
        f.seek(2, IO::SEEK_SET)
        pos0 = f.pos
        f.seek(8, IO::SEEK_SET)
        return pos0, f.pos
        """ % f)
        assert self.unwrap(space, w_res) == [2, 8]

    def test_pipe(self, space):
        w_res = space.execute("""
        return IO.pipe
        """)
        w_read, w_write = space.listview(w_res)
        assert isinstance(w_read, W_IOObject)
        assert isinstance(w_write, W_IOObject)
        w_res = space.execute("""
        r, w, r_c, w_c = IO.pipe do |r, w|
          r.close
          [r, w, r.closed?, w.closed?]
        end
        return r.closed?, w.closed?, r_c, w_c
        """)
        assert self.unwrap(space, w_res) == [True, True, True, False]

    def test_singleton_readlines(self, space, tmpdir):
        tmpdir.join("x.txt").write("abc")
        w_res = space.execute("return IO.readlines('%s')" % tmpdir.join("x.txt"))
        assert self.unwrap(space, w_res) == ["abc"]

    def test_to_io(self, space, tmpdir):
        f = tmpdir.join("file.txt")
        f.write("")
        w_res = space.execute("""
        f = File.new '%s'
        return f.eql? f.to_io
        """ % f)
        assert w_res == space.w_true

    def test_to_i(self, space, tmpdir):
        f1 = tmpdir.join("file1.txt")
        f1.write("")
        f2 = tmpdir.join("file2.txt")
        f2.write("")

        w_res = space.execute("""
        f1 = File.new '%s'
        f2 = File.new '%s'
        return f1.to_i, f2.to_i
        """ % (f1, f2))
        fds = self.unwrap(space, w_res)
        assert fds[0] != fds[1]

        w_res = space.execute("return STDIN.to_i")
        assert space.int_w(w_res) == 0
        w_res = space.execute("return STDOUT.to_i")
        assert space.int_w(w_res) == 1
        w_res = space.execute("return STDERR.to_i")
        assert space.int_w(w_res) == 2

    def test_reopen_stdout_in_closed_io(self, space, tmpdir):
        f = tmpdir.join("file.txt")
        f.write('')
        with self.raises(space, "IOError", "closed stream"):
            space.execute("""
            f = File.new('%s')
            f.close
            f.reopen($stdout)
            """ % f)

    def test_reopen_closed_io(self, space, tmpdir):
        f = tmpdir.join("file.txt")
        f.write('')
        with self.raises(space, "IOError", "closed stream"):
            space.execute("""
            f = File.new('%s')
            f.close
            $stderr.reopen(f)
            """ % f)

    def test_reopen(self, space, tmpdir):
        content = "This is line one"
        f = tmpdir.join("testfile")
        f.write(content + "\n")
        w_res = space.execute("""
        res = []
        class A
          def to_io
            File.new("%s")
          end
        end
        f1 = A.new
        f2 = File.new("%s")
        res << f2.readlines[0]
        f2.reopen(f1)
        res << f2.readlines[0]
        res << f2.readlines[0]
        return res
        """ % (f, f))
        assert self.unwrap(space, w_res) == [content, content, ""]

    def test_reopen_path(self, space, tmpdir):
        content = "This is line one"
        f = tmpdir.join("testfile")
        f.write(content + "\n")
        w_res = space.execute("""
        res = []
        f = File.new("%s")
        res << f.readlines[0]
        f.reopen("%s")
        res << f.readlines[0]
        res << f.readlines[0]
        return res
        """ % (f, f))
        assert self.unwrap(space, w_res) == [content, content, ""]

    def test_reopen_with_invalid_arg(self, space):
        with self.raises(space, "TypeError", "can't convert Fixnum into String"):
            space.execute("$stderr.reopen(12)")

    def test_popen_read(self, space):
        w_res = space.execute("""
        io = IO.popen("echo foo", "r")
        return io.pid.is_a?(Fixnum), io.read
        """)
        assert self.unwrap(space, w_res) == [True, "foo\n"]

    @pytest.mark.xfail
    def test_popen_write(self, space, capfd):
        space.execute("""
        IO.popen("cat", "w") do |io|
          io.write 'foo\n'
        end
        """)
        out, err = capfd.readouterr()
        assert out == "foo\n"


from collections import Counter

def findAnagrams(s, p):
    result = []
    p_count = Counter(p)
    s_count = Counter(s[:len(p)])

    if s_count == p_count:
        result.append(0)

    for i in range(len(p), len(s)):
        s_count[s[i - len(p)]] -= 1
        if s_count[s[i - len(p)]] == 0:
            del s_count[s[i - len(p)]]

        s_count[s[i]] += 1

        if s_count == p_count:
            result.append(i - len(p) + 1)

    return result
#Driver code
s = "cbaebabacd"
p = "abc"
result = findAnagrams(s, p)
print(result)


import abc
import sys

from DsContent import DsContent
from FilterBase import FilterBase

class FilterCharsPerSecondRange(FilterBase):
    def get_readable_name(self):
        return "charspersecondrange"

    def print_filter_help(self):
        print("Filter:", self.get_readable_name())
        print(" Include elements whom chars_per_seconds is between the given values.")
        print("Parameters:")
        print(" -fp  (--filter_parameter) : minimum border of chars_per_seconds to include")
        print(" -fp2 (--filter_parameter2): maximum border of chars_per_seconds to include")

    def perform(self, ds_content, filter_parameter=None, additional_filter_parameter=None):

        if filter_parameter is None:
            raise Exception("You must provide a minimum chars_per_seconds border")
        if additional_filter_parameter is None:
            raise Exception("You must provide a maximum chars_per_seconds border")

        min_cps = float(filter_parameter)
        max_cps = float(additional_filter_parameter)

        if min_cps > max_cps:
            raise Exception("Minimum chars_per_seconds border must not be higher than maximum chars_per_seconds border")

        for i in range(0, ds_content.size()):
            item = ds_content.get(i)
            include = min_cps <= item["chars_per_sec"] <= max_cps
            self._add_newcontent_item(item, include)
        return self.new_content


'''
Thoughts:

Iterate all the characters in board, if board[r][c] equal to word[0:], enter helper function with r, c and word[1:]
When r or c exceeds the edges, exit helper function
If the first letter of current word can be found by extending position [r][c] in any direction, enter helper function with updated r, c and word
Valid array is used to record whether a certain character has been used already when searching for the word

'''

class Solution:
	def exist(self, board, word):
		if not word:
			return True
		if not board:
			return False
		row, col = len(board), len(board[0])
		self.row, self.col = row, col
		valid = [[0] * col for _ in range(row)]
		for r in range(row):
			for c in range(col):
				if board[r][c] == word[0]:
					tmp = valid[:r] + [valid[r][:c] + [1] + valid[r][c + 1:]] + valid[r + 1:]
					# print(tmp)
					if self.helper(board, word[1:], tmp, r, c):
						return True
		return False

	def helper(self, board, word, valid, r, c):
		if not word:
			return True
		if r < 0 or c < 0 or r >= self.row or c >= self.col:
			return False
		if r > 0:
			if board[r - 1][c] == word[0] and valid[r - 1][c] != 1:
				tmp = valid[:r - 1] + [valid[r - 1][:c] + [1] + valid[r - 1][c + 1:]] + valid[r:]
				if self.helper(board, word[1:], tmp, r - 1, c):
					return True
		if r < self.row - 1:
			if board[r + 1][c] == word[0] and valid[r + 1][c] != 1:
				tmp = valid[:r + 1] + [valid[r + 1][:c] + [1] + valid[r + 1][c + 1:]] + valid[r + 2:]
				if self.helper(board, word[1:], tmp, r + 1, c):
					return True
		if c > 0:
			if board[r][c - 1] == word[0] and valid[r][c - 1] != 1:
				tmp = valid[:r] + [valid[r][:c - 1] + [1] + valid[r][c:]] + valid[r + 1:]
				if self.helper(board, word[1:], tmp, r, c - 1):
					return True
		if c < self.col - 1:
			if board[r][c + 1] == word[0] and valid[r][c + 1] != 1:
				tmp = valid[:r] + [valid[r][:c + 1] + [1] + valid[r][c + 2:]] + valid[r + 1:]
				if self.helper(board, word[1:], tmp, r, c + 1):
					return True
		return False

board = ['ABCE', 'SFCS', 'ADEE']
words = ['ABCCED', 'SFCS', 'EEDA', 'ABCB']
inst = Solution()
for word in words:
	print(word, inst.exist(board, word))


from abc import abstractmethod, ABC
from typing import Optional, TYPE_CHECKING

# if TYPE_CHECKING:
from ..core import ActionMap
from ..agent import Agent, RLAgent, DRLAgent

from ..game import Game
from ..learning import RewardToken

from copy import deepcopy

from pickle import Pickler, Unpickler
import os
from multiprocessing import Lock

class RLAlgorithm(ABC):
    """
    Base Class for a Reinforcement Learning algorithm.
    """

    def __init__(
        self,
        game: Game,
        student: RLAgent,
        action_map: ActionMap,
    ):
        assert isinstance(
            student, RLAgent
        ), f"Student agent to coach has to be of type '{RLAgent}'. Given type '{type(self.student).__name__}'"
        self.student = student

        self.action_map = action_map
        self.game = game

    @abstractmethod
    def run(self, *args, **kwargs):
        """
        Train the reinforcement learning agent according to the method defined in the concrete class.
        """
        raise NotImplementedError

    @staticmethod
    def reward_agent(agent: Agent, reward: RewardToken):
        if isinstance(agent, RLAgent):
            agent.add_reward(reward)

    def _new_state(self, lock: Optional[Lock] = None):
        """
        Resets the game and returns a deepcopy of the new state in a thread-safe manner, if the lock is provided.
        """
        gen_state = lambda: deepcopy(self.game.reset().state)
        if lock is not None:
            lock.lock()
            state = gen_state()
            lock.unlock()
        else:
            state = gen_state()
        return state

class DRLAlgorithm(RLAlgorithm, ABC):
    """
    Base Class for a Deep Reinforcement Learning algorithm.
    """

    def __init__(
        self,
        game: Game,
        student: DRLAgent,
        action_map: ActionMap,
        model_folder: str = "./checkpoints/models",
        train_data_folder: str = "./checkpoints/data",
    ):
        super().__init__(game, student, action_map)
        self.model_folder = model_folder
        self.train_data_folder = train_data_folder
        if not os.path.exists(model_folder):
            os.makedirs(model_folder)
        if not os.path.exists(train_data_folder):
            os.makedirs(train_data_folder)

        assert isinstance(
            student, DRLAgent
        ), f"Student agent to coach has to be of type '{DRLAgent}'. Given type '{type(self.student).__name__}'"

        self.action_map = action_map
        self.game = game

    def load_train_data(self, filename: str):
        data_filepath = os.path.join(self.train_data_folder, filename, ".data")
        with open(data_filepath, "rb") as f:
            train_data = Unpickler(f).load()
        return train_data

    def save_train_data(self, data, filename: str):
        folder = self.train_data_folder
        filename = os.path.join(folder, filename, ".data")
        with open(filename, "wb+") as f:
            Pickler(f).dump(data)


import numpy as np

# read and parse subtitles file
def get_data_from_file(path):
    sentences = []
    timings = []

    # 0 - index
    # 1 - time
    # 2 - sentence
    read = 0

    with open(path) as file:
        for line in file:
            line = line.strip()

            if read == 2:
                sentences.append(line)
                read = 0
            if read == 1:
                timings.append(line)
                read = 2
            if line.isdigit():
                read = 1

    return sentences, timings

# write indexes, timings and translations to file
# overwrite file if exists
def build_translated_file(srt_file_path, predictions, timings, y_tk):
    y_id_to_word = { value: key for key, value in y_tk.word_index.items() }
    y_id_to_word[0] = '<PAD>'

    with open(srt_file_path, 'w') as srt_file:
        for i in range(0, len(predictions)):
            srt_file.write(str(i + 1) + '\n')
            srt_file.write(timings[i] + '\n')
            srt_file.write(' '.join([y_id_to_word[np.argmax(x)] for x in predictions[i]]) + '\n')
            srt_file.write('\n')


import io
import os

#be mahze inke file ra ba mode w baz konim tamame mohtavaye an pak mishavad
#sepas ba methode write mitavanim mohtavaye jadid ra dar an benevisim
#agar file vojud nadashte bashad an ra misazad
fileObj = open("texts.txt", "w")
fileObj.write("Hello Python ...............")
print("File writing finished!")

#be positione 13 miravim
fileObj.seek(13)

#az khane 13 be bad pak mishavad va mohtavaye jadid dar an neveshte mishavad
fileObj.write("\n!!!!!!!!!!!!!!\n")
fileObj.close()

"""Note that if the file is opened for appending using either 'a' or 'a+',
any seek() operations will be undone at the next write."""
fileObj = open("texts.txt", "a")
fileObj.write("New Line :)))))))))))))")
print("Current Pos:", fileObj.tell())
fileObj.close()

fileObj = open("texts.txt", "r")
contents = fileObj.read()
print("--------------")
print(contents)
print("---------------")

#hazf va rename file ha
#os.rename("texts.txt", "newname.txt")
#os.remove("newname.txt")

#sakhte dir
#os.mkdir("texts")

print("Current dir: ")
print(os.getcwd())


import importlib
import os
from types import ModuleType
from typing import Any, Callable, Dict, List, Tuple, cast

import sublime
import sublime_plugin
from lsp_utils import ServerNpmResource

from .patcher import AlreadyPatchedException, Patcher, PatcherUnsupportedException, json_dumps, restore_directory
from .plugin_message import console_msg, error_box, info_box
from .utils import get_command_name

def restart_intelephense_server() -> None:
    view = sublime.active_window().active_view()
    if view:
        view.run_command("lsp_restart_server", {"config_name": "LSP-intelephense"})

def st_command_run_precheck(func: Callable) -> Callable:
    def wrapped(self: sublime_plugin.Command, *args, **kwargs) -> None:
        def checker() -> Tuple[ModuleType, ServerNpmResource]:
            try:
                plugin_module = importlib.import_module("LSP-intelephense.plugin")
                lsp_plugin = plugin_module.LspIntelephensePlugin  # type: ignore
            except (ImportError, AttributeError):
                raise RuntimeError("LSP-intelephense is not installed...")

            server_resource = lsp_plugin.get_server()  # type: ServerNpmResource

            if not os.path.isfile(server_resource.binary_path):
                raise RuntimeError(
                    "The intelephense server has not been installed yet... "
                    + "Open a PHP project to install it and then retry."
                )

            return plugin_module, server_resource

        try:
            _, server_resource = checker()
        except Exception as e:
            return error_box("[{_}] {}", e)

        return func(self, server_resource, *args, **kwargs)

    return wrapped

class LspIntelephensePatcherPatchCommand(sublime_plugin.ApplicationCommand):
    @st_command_run_precheck
    def run(
        self,
        server_resource: ServerNpmResource,
        allow_unsupported: bool = False,
        is_direct: bool = True,
    ) -> None:
        binary_path = server_resource.binary_path

        is_already_patched = False
        is_success = False

        try:
            is_success, occurrences = Patcher.patch_file(binary_path, allow_unsupported)

            if is_success and occurrences > 0:
                info_box('[{_}] "{}" is patched with {} occurrences!', binary_path, occurrences)
            else:
                error_box("[{_}] Unfortunately, somehow the patching failed.")
        except AlreadyPatchedException:
            is_already_patched = True
            is_success = True
        except PatcherUnsupportedException as e:
            is_success = False
            error_box("[{_}] {}", e)

        if not is_success:
            return

        patch_info = Patcher.extract_patch_info(binary_path)

        if is_already_patched:
            msg = '[{_}] "{bin}" had been already patched...'

            if Patcher.VERSION > patch_info["version"]:
                msg += "\n\nBut the current patcher ({v_new}) is newer than the patching one ({v_old})."

            info_box(msg, bin=binary_path, v_old=patch_info["version"], v_new=Patcher.VERSION)

        if is_direct:
            restart_intelephense_server()

        console_msg("[{_}] Patch info: {}", json_dumps(patch_info))

class LspIntelephensePatcherUnpatchCommand(sublime_plugin.ApplicationCommand):
    @st_command_run_precheck
    def run(
        self,
        server_resource: ServerNpmResource,
        is_direct: bool = True,
    ) -> None:
        binary_path = server_resource.binary_path

        restored_files = restore_directory(os.path.dirname(binary_path))

        if not restored_files:
            return error_box("[{_}] No file has been restored...")

        restored_files_len = len(restored_files)

        for idx, file in enumerate(restored_files):
            console_msg("[{_}] {}/{} file restored: {}", idx + 1, restored_files_len, file)

        if is_direct:
            restart_intelephense_server()

        info_box("[{_}] {} files have been restored.", restored_files_len)

class LspIntelephensePatcherRepatchCommand(sublime_plugin.ApplicationCommand):
    @st_command_run_precheck
    def run(self, server_resource: ServerNpmResource) -> None:
        sublime.run_command(get_command_name(LspIntelephensePatcherUnpatchCommand), {"is_direct": False})
        sublime.run_command(get_command_name(LspIntelephensePatcherPatchCommand), {"is_direct": False})
        restart_intelephense_server()

class LspIntelephensePatcherOpenServerBinaryDirCommand(sublime_plugin.WindowCommand):
    @st_command_run_precheck
    def run(self, server_resource: ServerNpmResource) -> None:
        self.window.run_command("open_dir", {"dir": os.path.dirname(server_resource.binary_path)})

class LspIntelephensePatcherShowMenuCommand(sublime_plugin.WindowCommand):
    menu_items = [
        # title, cmd_class, cmd_arg
        ("Patch Intelephense", LspIntelephensePatcherPatchCommand, {}),
        ("Patch Intelephense (Allow Unsupported)", LspIntelephensePatcherPatchCommand, {"allow_unsupported": True}),
        ("Un-patch Intelephense", LspIntelephensePatcherUnpatchCommand, {}),
        ("Re-patch Intelephense", LspIntelephensePatcherRepatchCommand, {}),
        ("Open Server Binary Directory", LspIntelephensePatcherOpenServerBinaryDirCommand, {}),
    ]  # type: List[Tuple[str, type, Dict[str, Any]]]

    def run(self) -> None:
        titles, cmd_classes, cmd_args = cast(
            # make stupid type deduction tool happy
            Tuple[Tuple[str, ...], Tuple[type, ...], Tuple[Dict[str, Any], ...]],
            zip(*self.menu_items),
        )

        def on_select(idx: int) -> None:
            if idx < 0:
                return

            self.window.run_command(get_command_name(cmd_classes[idx]), cmd_args[idx])

        self.window.show_quick_panel(titles, on_select=on_select)


from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse, HttpResponseNotFound, HttpResponseBadRequest, HttpResponse

from basicInfo.models import account, examination, takeup, teach, course, room, learn, student,attrib,major,discipline
import traceback

@csrf_exempt
def api_student_info(request):
    if request.method == "GET":
        try:
            print("----")
            account_id = request.GET["account_id"]

            student_info=student.objects.get(student_id=account_id)

            student_attrib_info=attrib.objects.get(account_id=account_id)

            major_info=major.objects.get(student_id=account_id)
            dis_id=major_info.discipline_id

            dis_info=discipline.objects.get(discipline_id=dis_id)

            ret={}

            ret["name"]=student_info.name
            ret["nick"]=student_attrib_info.nickname
            ret["major"]=dis_info.name
            ret["grade"]=student_info.grade
            ret["email"]=student_attrib_info.email
            ret["dorm"]=student_info.dorm

            return JsonResponse(ret)

        except Exception as e:
            print(e)
            ret = {}

            ret["name"] = "获取失败"
            ret["nick"] = "获取失败"
            ret["major"] = "获取失败"
            ret["grade"] = "获取失败"
            ret["email"] = "获取失败"
            ret["dorm"] = "获取失败"
            return JsonResponse(ret)
    return HttpResponseBadRequest()

@csrf_exempt
def api_student_exam(request):
    if request.method == "GET":
        try:
            account_id = request.GET["account_id"]

            exam = examination.objects.filter(student_id=account_id)

            exam_list = []
            for e in exam:
                tmp = {}
                takeup_id = e.takeup_id
                teach_id = takeup_id.teach_id
                course_id = teach_id.course_id
                tmp["name"] = course_id.name
                if course_id.exam_date:
                    tmp["time"] = course_id.exam_date.strftime("%Y-%m-%d %H:%M")
                else:
                    continue
                room_id = takeup_id.room_id
                tmp["room"] = room_id.location
                tmp["seat"] = e.position
                exam_list.append(tmp)

            return JsonResponse(exam_list,safe=False)

        except Exception as e:
            print(e)
            traceback.print_exc()
            return JsonResponse([], safe=False)
    return HttpResponseBadRequest()

@csrf_exempt
def api_student_grade(request):
    if request.method == "GET":
        try:
            account_id = request.GET["account_id"]

            student_learn = learn.objects.filter(student_id=account_id)

            course_list = []
            for l in student_learn:
                tmp = {}
                course_id = l.course_id
                tmp["name"] = course_id.name
                tmp["credit"] = course_id.credit
                tmp["grade"] = l.grade
                tmp["course_id"]=l.course_id.course_id
                if l.grade!=None:
                    course_list.append(tmp)

            return JsonResponse(course_list,safe=False)

        except Exception as e:
            print(e)
            traceback.print_exc()
            return JsonResponse([], safe=False)
    return HttpResponseBadRequest()


"""
Author: Henry Powell
Institution: Institute of Neuroscience and Psychology, Glasgow University, Scotland.

High level script to train a denoising autoencoder on a given data set. Different autoencoder models
can be imported from the 'autoencoder_architectures' directory.
"""

import torch
import numpy as np
import matplotlib.pyplot as plt
from torch.utils import data
from data_loader import DataSet
from RMSE_loss import RMSE_loss
from autoencoder_architectures.CNNLSTM import CNNLSTMAE
from functions import load_data, apply_missing_cmu_val, crop_to_missing, denormalize_series

# set script params here
architecture = 'CNNLSTMAE'
# path_to_ground_truth = '/home/henryp/PycharmProjects/MoGap/filtered_tensors'
batch_size = 17
params = {'batch_size': batch_size,
          'shuffle': True,
          'num_workers': 6}
max_epochs = 90
# pick whether you want to train the network on GPU or CPU
run_on = 'GPU'
# change to false if you want to preload the network with saved weights
train_network = True

# get mean pose from data set.
mean_pose = torch.as_tensor(np.load('./mean_pose.npy')).cuda()

# get max val from dataset
max_val = torch.as_tensor(np.load('./data_max.npy')).cuda()

# where to save the saved network weights after training
PATH = './MoGapSaveState' + architecture + '_test.pth'

# path to data that has been cropped into smaller same-sized chucks
cropped_ground_truth_path_train = '/run/media/henryp/HenryHDD/DataSets/CMU/Formatted/Train/Cropped_data/sliced_'

cropped_ground_truth_path_test = '/run/media/henryp/HenryHDD/DataSets/CMU/Formatted/Test/Cropped_data/'

if run_on == 'GPU':
    device = torch.device('cuda:0')
else:
    device = torch.device('cpu')

print('============')
print('Training on: ' + str(device))
print('============')

training_partition, training_set_size = load_data(ground_truth_dir=cropped_ground_truth_path_train,
                                                  type='train')

training_set = DataSet(list_IDS=training_partition['train'],
                       data_dir=cropped_ground_truth_path_train,
                       clip_length=64)
training_generator = data.DataLoader(training_set, **params)

testing_partition, testing_set_size = load_data(ground_truth_dir=cropped_ground_truth_path_test,
                                                type='test')

testing_set = DataSet(list_IDS=testing_partition['test'],
                      data_dir=cropped_ground_truth_path_test,
                      clip_length=64)

testing_generator = data.DataLoader(testing_set, **params)

# load the network class
# input size is the number of features in your input data
net = CNNLSTMAE(num_frames=64, num_layers=2)
net = net.cuda()

if train_network:

    # select network hyper parameters
    criterion = RMSE_loss()
    optimizer = torch.optim.Adam(net.parameters(), lr=0.0002)
    # scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer=optimizer,
    #                                                        patience=5, verbose=True)

    # track loss per epoch to plot how the network learns over the number of epochs
    loss_values = []
    best_loss = 1000

    # training loop
    for epoch in range(max_epochs):
        epoch_acc = torch.Tensor().cuda()
        for local_batch in training_generator:

            # apply missing markers and split missing and ground truth data
            local_batch = local_batch.float()
            local_batch_missing, _ = apply_missing_cmu_val(time_series_tensor=local_batch,
                                                           erasures_perc=10,
                                                           missing_val=1.0001)
            # move to GPU
            local_batch = local_batch.to(device)
            local_batch_missing = local_batch_missing.to(device)

            # training loop
            outputs = net(local_batch_missing)

            # get only missing data
            y, y_hat = crop_to_missing(local_batch, local_batch_missing, outputs, nan_val=1.0001)

            # loss = criterion(outputs, local_batch)
            loss = criterion(y, y_hat)
            epoch_acc = torch.cat((epoch_acc, loss.data.unsqueeze(0)))
            # backwards step
            optimizer.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_value_(net.parameters(), 10)
            optimizer.step()
        # scheduler.step(loss)
        print('epoch [{}/{}], mean RMSE_loss for epoch:{:.8f}'
              .format(epoch + 1, max_epochs, torch.mean(epoch_acc)))

        loss_values.append(torch.Tensor.cpu(torch.mean(epoch_acc)).numpy())

        # save best epoch
        if loss.data < best_loss:
            best_loss = loss.data
            torch.save(net.state_dict(), PATH)

    # plot loss over training
    plt.plot([i for i in range(max_epochs)], loss_values)
    plt.title(architecture + ' loss || Final RMSEloss: ' + str(loss_values[-1]))
    plt.savefig(architecture + '_loss')

else:
    print('Loading saved network state...')
    net.load_state_dict(torch.load(PATH))

# test on testing_set
criterion = RMSE_loss()
testing_loss = []
with torch.no_grad():
    for local_batch in testing_generator:
        # preprocess data
        local_batch = local_batch.float()
        local_batch_missing, _ = apply_missing_cmu_val(time_series_tensor=local_batch,
                                                       erasures_perc=10,
                                                       missing_val=1.0001)

        # move to GPU
        local_batch = local_batch.to(device)
        local_batch_missing = local_batch_missing.to(device)

        outputs = net(local_batch_missing)
        loss = criterion(local_batch, outputs)
        testing_loss.append(torch.Tensor.cpu(loss).data)

    print('Mean RMSE_loss for testing set:{:.8f}'.format(torch.mean(torch.as_tensor(testing_loss))))


# Import system modules
import os
import arcpy
from arcpy import env
from arcpy.sa import *
# Necessary data folders
originFolder = r'B:\files\data\night_light_data\allraster\origin_stable'
radianceFolder = r'B:\files\data\night_light_data\Radiance Calibration\tar'
saturatedFolder = r'B:\files\data\night_light_data\allraster\calibrated'
maskFolder = r'B:\files\data\MGISDATA\mgisdata\World\other_countries'
workFolder = r'C:\Users\GIS336\Desktop\files\tmp'

# specify files to be applied in Extractions
def SpecifyFiles(folder, postfix):
	return filter(lambda name:name[-len(postfix):] == postfix, os.listdir(folder))

# Execute ExtractByMask and Save the output
def ExtractByMask(inRaster,inMaskData,outputPath, outputPostfix):
	arcpy.CheckOutExtension("Spatial")
	outExtractByMask = ExtractByMask(inRaster,inMaskData)
	outExtractByMask.save(outputPath)

def BatchExtraction(rasterFolder,rasterPostfix,maskFolder,maskPostfix,workFolder):
	rasters = SpecifyFiles(rasterFolder,rasterPostfix)
	masks = SpecifyFiles(maskFolder,maskPostfix)
	for raster in rasters:
		outputDir = workFolder+'\\'+raster.split(".")[0]
		os.mkdir(outputDir)
		for mask in masks:
			outputPath = outputDir + "\\" + mask[:-(len(maskPostfix)+1)] + "." + rasterPostfix
			ExtractByMask(rasterFolder+"\\"+raster, maskFolder+"\\"+mask, outputPath, rasterPostfix)
if __name__ == "__main__":
	BatchExtraction(originFolder,"tif",maskFolder,"shp",workFolder)


start=int(input("Enter the start of the range to find STRONG NUMBER: "))
end=int(input("Enter the end of the range to find STRONG NUMBER: "))
for n in range(start,end+1):
    b=n
    sum=0
    if(n==0):   #special case n=0
        sum=1
    while(b>0):
        dig=b%10
        b=b//10
        a=1
        for j in range(2,dig+1):
            a=a*j
        sum=sum+a
    if(n==sum):
        print(n,"is a STRONG Number.")


import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','rango_project.settings')
import django
django.setup()
from rango.models import Category,Page
import random
def add_cat(lst):
    c = Category.objects.get_or_create(name=lst[0])[0]
    c.view = lst[1]
    c.likes = lst[2]
    c.save()
    return c
lst = [["Python",5,7],["Django",2,1],["Other Frameworks",7,8]]
for l in lst:
    add_cat(l)
for i in range(1,9):
    p = Page.objects.get_or_create(id=i)[0]
    r = random.randint(50,100)
    p.views = r
    p.save()


#
#	Nick Quinn - Compiler Construction
#
#   This file contains support functions and classes that are commonly
#   used in the C, X, and R languages.

### Support Classes ###

########################## Node #########################################################
# -- Node Class for Linked List in Env (Enviroment) --

class TypeCheckError(Exception):
	pass

########################## Node #########################################################
# -- Node Class for Linked List in Env (Enviroment) --

class node():
	def __init__(self, var, num):
		self._var = var
		self._num = num
		self.next = None

########################## Env ##########################################################
# -- Class env (enviroment) --

class env():
	def __init__(self):
		self._head = None

	def find_var(self, var):
		temp = self._head
		while temp != None:
			if (temp._var == var):
				return temp._num;
			temp = temp.next;
		#print (" No mapping found. ")
		return None;

	def add_var(self, var, x):
		new_node = node(var, x)
		if (self._head == None):
			self._head = new_node
		else:
			temp = self._head
			new_node.next = temp
			self._head = new_node

########################## Uniquify #####################################################
# -- Looks for the var passed in the enviroment, and creates a new unique var if it already
# -- exists
def uniquify(var, enviroment):
	unique_count = 1
	temp = enviroment.find_var(var)
	new_var = var
	while(temp != None):
		new_var = var + str(unique_count)
		temp = enviroment.find_var(new_var)
		unique_count += 1

	return new_var;

########################## Create Unique Var ############################################
# -- Creates a new unique variable based of the enviroment. These variables are distinguished
# -- because they start with "_u"
def create_unique_var(enviroment):
	default_var = "_u"
	unique_var = uniquify(default_var, enviroment)
	return unique_var;


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
GPLv3 license (ASTRA toolbox)

Script to reconstruct tomographic X-ray data (ice cream crystallisation process)
obtained at Diamond Light Source (UK synchrotron), beamline I13

Dependencies:
    * astra-toolkit, install conda install -c astra-toolbox astra-toolbox
    * CCPi-RGL toolkit (for regularisation), install with
    conda install ccpi-regulariser -c ccpi -c conda-forge
    or conda build of  https://github.com/vais-ral/CCPi-Regularisation-Toolkit
    * TomoRec https://github.com/dkazanc/TomoRec

<<<
IF THE SHARED DATA ARE USED FOR PUBLICATIONS/PRESENTATIONS etc., PLEASE CITE:
E. Guo et al. 2018. Revealing the microstructural stability of a
three-phase soft solid (ice cream) by 4D synchrotron X-ray tomography.
Journal of Food Engineering, vol.237
>>>
@author: Daniil Kazantsev: https://github.com/dkazanc
"""

import h5py
import numpy as np
import matplotlib.pyplot as plt
#%%
# loading data
h5f = h5py.File('data/data_icecream_1_slice_35_frames.h5','r')
data_norm = h5f['icecream_normalised'][:]
data_raw = h5f['icecream_raw'][:]
angles_rad = h5f['angles'][:]
h5f.close()
#%%

#%%
# loading FBP images
h5f = h5py.File('data/FBP_35timeframes.h5','r')
FBP_timeframes = h5f['FBP_timeframes'][:]
h5f.close()

# visualise FBP reconstructed sequence
"""
timeframe_no = np.size(FBP_timeframes,2)
for i in range(0, timeframe_no):
    plt.gray()
    plt.figure(1)
    plt.imshow(FBP_timeframes[:,:,i],vmin=0, vmax=1)
    plt.show()
    plt.pause(0.1)
"""
plt.figure()
plt.imshow(FBP_timeframes[:,:,15], vmin=0, vmax=1, cmap="gray")
plt.title('FBP reconstruction, time frame 15')
plt.show()
#%%
# loading Iteratively reconstructed images
h5f = h5py.File('data/FISTATV_35timeframes.h5','r')
FISTA_TV_timeframes = h5f['FISTA_TV_timeframes'][:]
h5f.close()

"""
timeframe_no = np.size(FISTA_TV_timeframes,2)
for i in range(0, timeframe_no):
    plt.gray()
    plt.figure(1)
    plt.imshow(FISTA_TV_timeframes[:,:,i],vmin=0, vmax=0.15)
    plt.show()
    plt.pause(0.1)
"""
plt.figure()
plt.imshow(FISTA_TV_timeframes[:,:,15], vmin=0, vmax=0.15, cmap="gray")
plt.title('FISTA TV reconstruction, time frame 15')
plt.show()
#%%
"""
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print ("%%%%%%%%%%%%Reconstructing with FBP method %%%%%%%%%%%%%%%%%")
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
from tomorec.methodsDIR import RecToolsDIR

N_size = 2500
timeframe_no = np.size(data_norm,2)
det_y_crop = [i for i in range(0,2374)]
FBP_timeframes = np.zeros((N_size, N_size, timeframe_no), dtype='float32')

for i in range(0, timeframe_no):
    RectoolsDIR = RecToolsDIR(DetectorsDimH = np.size(det_y_crop),  # DetectorsDimH # detector dimension (horizontal)
                    DetectorsDimV = None,  # DetectorsDimV # detector dimension (vertical) for 3D case only
                    AnglesVec = angles_rad[i,:], # array of angles in radians
                    ObjSize = N_size, # a scalar to define reconstructed object dimensions
                    device='gpu')
    FBP_timeframes[:,:,i] = RectoolsDIR.FBP(np.transpose(data_norm[det_y_crop,:,i]))
"""
#%%
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print ("Reconstructing with FISTA PWLS-OS-TV method % %%%%%%%%%%%%%%")
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
from tomorec.methodsIR import RecToolsIR

N_size = 2500
det_y_crop = [i for i in range(0,2374)]
timeframe_no = np.size(data_norm,2)
FISTA_TV_timeframes = np.zeros((N_size, N_size, timeframe_no), dtype='float32')

for i in range(0, timeframe_no):
    # set parameters and initiate a class object
    Rectools = RecToolsIR(DetectorsDimH = np.size(det_y_crop),  # DetectorsDimH # detector dimension (horizontal)
                    DetectorsDimV = None,  # DetectorsDimV # detector dimension (vertical) for 3D case only
                    AnglesVec = angles_rad[:,i], # array of angles in radians
                    ObjSize = N_size, # a scalar to define reconstructed object dimensions
                    datafidelity='PWLS',# data fidelity, choose LS, PWLS, GH (wip), Student (wip)
                    OS_number = 12, # the number of subsets, NONE/(or > 1) ~ classical / ordered subsets
                    tolerance = 1e-08, # tolerance to stop outer iterations earlier
                    device='gpu')

    lc = Rectools.powermethod(np.transpose(data_raw[det_y_crop,:,i])) # calculate Lipschitz constant (run once to initilise)

    FISTA_TV_timeframes[:,:,i] = Rectools.FISTA(np.transpose(data_norm[det_y_crop,:,i]), \
                              np.transpose(data_raw[det_y_crop,:,i]), \
                              iterationsFISTA = 10, \
                              regularisation = 'FGP_TV', \
                              regularisation_parameter = 0.0015,\
                              regularisation_iterations = 200,\
                              lipschitz_const = lc)
    """
    # Run FISTA-PWLS-OS reconstrucion algorithm
    RecFISTA_PWLS = Rectools.FISTA(np.transpose(data_norm[det_y_crop,:,0]), \
                              np.transpose(data_raw[det_y_crop,:,0]), \
                              iterationsFISTA = 3, \
                              lipschitz_const = lc)
    """

#%%


"""
This runs any of the forcing jobs.

It can be run for a single forecast or over a range of past days.

- Test on mac from ipython:
run driver_forcing3 -g cas6 -0 2019.07.04 -1 2019.07.05 -test True -f [FRC]
where [FRC] = riv00, etc.

- Test on mac from command line:
python driver_forcing3.py -g cas6 -0 2019.07.04 -test True -f [FRC]

As of 2022.06.08 the default start_type is perfect, which is the new default.
Not relevant in most cases. A start_type of "new" may be the only important use.

NEW: this "3" version uses the new organizational system where the forcing goes to a [gridname]
folder instead of [gtag].

NEW: 2023.05.14 Added new optional arguments for ocnN capability.

"""

import sys
import argparse
from datetime import datetime, timedelta
import subprocess

from lo_tools import Lfun, zfun

parser = argparse.ArgumentParser()
# arguments without defaults are required
parser.add_argument('-g', '--gridname', type=str)   # e.g. cas2
parser.add_argument('-f', '--frc', type=str)        # e.g. tide
parser.add_argument('-r', '--run_type', type=str, default='backfill')   # forecast or backfill
parser.add_argument('-s', '--start_type', type=str, default='perfect') # new, continuation, or perfect
parser.add_argument('-0', '--ds0', type=str)        # e.g. 2019.07.04
parser.add_argument('-1', '--ds1', type=str, default='') # is set to ds0 if omitted
parser.add_argument('-test', '--testing', default=False, type=zfun.boolean_string)

# optional arguments used only for ocnN, to determine what to nest inside
parser.add_argument('-gtx', '--gtagex', default='cas6_traps2_x2b', type=str) # e.g. cas6_traps2_x2b
parser.add_argument('-ro', '--roms_out_num', type=int, default=0) # 1 = Ldir['roms_out1'], etc.
parser.add_argument('-do_bio', default=False, type=Lfun.boolean_string) # True to add bio vars to ocnN forcing

args = parser.parse_args()

# check for required arguments
argsd = args.__dict__
for a in ['gridname', 'frc']:
    if argsd[a] == None:
        print('*** Missing required argument for driver_forcing3.py: ' + a)
        sys.exit()

if args.testing:
    from importlib import reload
    reload(Lfun)

# get Ldir
Ldir = Lfun.Lstart(gridname=args.gridname)

# set time range to process
if args.run_type == 'forecast':
    ds0 = datetime.now().strftime(Lfun.ds_fmt)
    dt0 = datetime.strptime(ds0, Lfun.ds_fmt)
    dt1 = dt0
    ds1 = dt1.strftime(Lfun.ds_fmt)
elif args.run_type == 'backfill':
    ds0 = args.ds0
    if len(args.ds1) == 0:
        ds1 = ds0
    else:
        ds1 = args.ds1
    dt0 = datetime.strptime(ds0, Lfun.ds_fmt)
    dt1 = datetime.strptime(ds1, Lfun.ds_fmt)
else:
    print('Error: Unknown run_type')
    sys.exit()

if args.run_type == 'backfill':
    print(('Forcing: %s, %s, %s-%s ' % (args.frc, args.run_type, ds0, ds1)).center(60,'-'))
else:
    pass # don't clutter up the screen output for forecasts

# loop over all days
dt = dt0
while dt <= dt1:

    # fix start_type bug
    if (dt != dt0) and (args.start_type == 'new'):
        args.start_type = 'continuation'

    # make clean output directories
    out_dir = Ldir['LOo'] / 'forcing' / Ldir['gridname'] / ('f' + dt.strftime(Lfun.ds_fmt)) / args.frc
    Lfun.make_dir(out_dir, clean=True)
    Lfun.make_dir(out_dir / 'Data')
    Lfun.make_dir(out_dir / 'Info')

    # run the code
    f_fn = Ldir['LO'] / 'forcing' / args.frc / 'make_forcing_main.py'
    # If the user has this forcing in LO_user, then default to that:
    f_fn_alt = Ldir['LOu'] / 'forcing' / args.frc / 'make_forcing_main.py'
    if f_fn_alt.is_file():
        f_fn = f_fn_alt

    cmd_list = ['python3', str(f_fn),
                '-g', args.gridname, '-f', args.frc,
                '-r', args.run_type, '-s', args.start_type,
                '-d', dt.strftime(Lfun.ds_fmt), '-test', str(args.testing),
                '-gtx', args.gtagex, '-ro', str(args.roms_out_num), '-do_bio', str(args.do_bio)]
    proc = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    with open(out_dir / 'Info' / 'screen_output.txt', 'w') as fout:
        fout.write(stdout.decode())
    if len(stderr) > 0:
        with open(out_dir / 'Info' / 'subprocess_error.txt', 'w') as ffout:
            ffout.write(stderr.decode())

    # this is intended to end up in the log that the cron job makes
    res_fn = out_dir / 'Info' / 'results.txt'
    if res_fn.is_file():
        with open(res_fn, 'r') as fout:
            for line in fout:
                print(line.replace('\n',''))
    else:
        print('ERROR: missing results.txt file')

    dt += timedelta(days=1)
    print('')


from django.test import TestCase
from .models import Books
from django.urls import reverse
# Create your tests here.

class BookTests(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.book = Books.objects.create(
            title="A good title",
            subtitle="An excellent subtitle",
            author="Tom Christie",
            isbn="1234567890123",
        )
    def test_book_content(self):
        self.assertEqual(self.book.title, "A good title")
        self.assertEqual(self.book.subtitle, "An excellent subtitle")
        self.assertEqual(self.book.author, "Tom Christie")
        self.assertEqual(self.book.isbn, "1234567890123")
    def test_book_listview(self):
        response = self.client.get(reverse("books"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "excellent subtitle")
        self.assertTemplateUsed(response, "home.html")


import math

from django.views.generic.list import ListView
from django.conf import settings

from .forms import SearchForm

from talks.models import Talk
from search.search import search_talks

class SearchTalksView(ListView):
    model = Talk
    template_name = 'search.html'
    paginate_by = settings.PAGE_SIZE

    def get_context_data(self, **kwargs):
        context = super(SearchTalksView, self).get_context_data(**kwargs)

        search_form = SearchForm(self.request.GET)
        context['search_form'] = search_form

        if search_form.is_valid():
            query = search_form.cleaned_data['q']
            context['search_query'] = query

            page = 1
            if "page" in self.request.GET:
                page = int(self.request.GET["page"])

            sort = "relevance"
            if "sort" in self.request.GET:
                sort = self.request.GET["sort"]

            results_total, results_ids = search_talks(q=query, page=page, sort=sort)
            search_results = Talk.published_objects.filter(pk__in=results_ids)

            num_pages = math.ceil(results_total / self.paginate_by)
            if num_pages > 500:
                num_pages = 500
            pagination = {
                "is_paginated": True if results_total > self.paginate_by else False,
                "number": page,
                "num_pages": num_pages,
                "has_previous": True if page > 1 else False,
                "previous_page_number": page - 1,
                "has_next": True if page < num_pages else False,
                "next_page_number": page + 1,
            }
            context['pagination'] = pagination
            context['sort'] = sort
            context['object_list'] = search_results

        return context


from dataclasses import dataclass, field
from typing import Tuple, Type

import torch
import torchvision

try:
    import clip
except ImportError:
    assert False, "clip is not installed, install it with `pip install clip`"

from lerf.encoders.image_encoder import BaseImageEncoder, BaseImageEncoderConfig

@dataclass
class CLIPNetworkConfig(BaseImageEncoderConfig):
    _target: Type = field(default_factory=lambda: CLIPNetwork)
    clip_model_type: str = "ViT-B/16"
    clip_n_dims: int = 512
    negatives: Tuple[str] = ("object", "things", "stuff", "texture")

class CLIPNetwork(BaseImageEncoder):
    def __init__(self, config: CLIPNetworkConfig):
        super().__init__()
        self.config = config
        self.process = torchvision.transforms.Compose(
            [
                torchvision.transforms.Resize((224, 224)),
                torchvision.transforms.Normalize(
                    mean=[0.48145466, 0.4578275, 0.40821073],
                    std=[0.26862954, 0.26130258, 0.27577711],
                ),
            ]
        )
        model, _ = clip.load(self.config.clip_model_type)
        model.eval()
        self.tokenizer = clip.tokenize
        self.model = model.to("cuda")
        self.clip_n_dims = self.config.clip_n_dims

        self.positives = ["hand sanitizer"]
        self.negatives = self.config.negatives
        with torch.no_grad():
            tok_phrases = torch.cat([self.tokenizer(phrase) for phrase in self.positives]).to("cuda")
            self.pos_embeds = model.encode_text(tok_phrases)
            tok_phrases = torch.cat([self.tokenizer(phrase) for phrase in self.negatives]).to("cuda")
            self.neg_embeds = model.encode_text(tok_phrases)
        self.pos_embeds /= self.pos_embeds.norm(dim=-1, keepdim=True)
        self.neg_embeds /= self.neg_embeds.norm(dim=-1, keepdim=True)

        assert (
            self.pos_embeds.shape[1] == self.neg_embeds.shape[1]
        ), "Positive and negative embeddings must have the same dimensionality"
        assert (
            self.pos_embeds.shape[1] == self.clip_n_dims
        ), "Embedding dimensionality must match the model dimensionality"

    @property
    def name(self) -> str:
        return "clip_openai_{}".format(self.config.clip_model_type)

    @property
    def embedding_dim(self) -> int:
        return self.config.clip_n_dims

    def set_positives(self, text_list):
        self.positives = text_list
        with torch.no_grad():
            tok_phrases = torch.cat([self.tokenizer(phrase) for phrase in self.positives]).to("cuda")
            self.pos_embeds = self.model.encode_text(tok_phrases)
        self.pos_embeds /= self.pos_embeds.norm(dim=-1, keepdim=True)

    def get_relevancy(self, embed: torch.Tensor, positive_id: int) -> torch.Tensor:
        phrases_embeds = torch.cat([self.pos_embeds, self.neg_embeds], dim=0)
        p = phrases_embeds.to(embed.dtype)  # phrases x 512
        output = torch.mm(embed, p.T)  # rays x phrases
        positive_vals = output[..., positive_id : positive_id + 1]  # rays x 1
        negative_vals = output[..., len(self.positives) :]  # rays x N_phrase
        repeated_pos = positive_vals.repeat(1, len(self.negatives))  # rays x N_phrase

        sims = torch.stack((repeated_pos, negative_vals), dim=-1)  # rays x N-phrase x 2
        softmax = torch.softmax(10 * sims, dim=-1)  # rays x n-phrase x 2
        best_id = softmax[..., 0].argmin(dim=1)  # rays x 2
        return torch.gather(softmax, 1, best_id[..., None, None].expand(best_id.shape[0], len(self.negatives), 2))[
            :, 0, :
        ]

    def encode_image(self, input):
        processed_input = self.process(input).half()
        return self.model.encode_image(processed_input)


# https://gombru.github.io/2019/04/03/ranking_loss/

# Ranking Loss: The objective of Ranking Losses is to predict relative distances between inputs.
# 1. Pair Ranking Loss
# 2. Triplet Ranking Loss

# Pair Ranking Loss Formulation (MarginRankingLoss)
# L(r0, r1, y) = max(0, margin - y*||r0 - r1||)
# Where r0, and r1 are the input features and
# y = 1, and -1 for positive and negative examples respectively.

# Triplet Ranking Loss Formulation (TripletMarginLoss)
# L(a, p, n) = max(|| a - p || - || a - n || + margin, 0)
# where a (anchor), p (positive examples), and n (negative examples)
import torch
from torch import nn

input1 = torch.rand(3, 512, requires_grad=True)
input2 = torch.rand(3, 512, requires_grad=True)
input3 = torch.rand(3, 512, requires_grad=True)

loss_fn_pair = nn.MarginRankingLoss()
loss_fn_triplet = nn.TripletMarginLoss()
target = torch.randn(3,1).sign()
output_pair = loss_fn_pair(input1, input2, target)
output_pair.backward()
output_triplet = loss_fn_triplet(input1, input2, input3)
output_triplet.backward()

print(input1)
print(input2)
print(input3)
print(output_pair)
print(output_triplet)


import tensorflow as tf
from tensorflow.python.framework import graph_util
from hparams import hparams
from models import create_model

if __name__ == '__main__':
    model_folder = 'logs-tacotron'
    checkpoint = tf.train.get_checkpoint_state(model_folder)
    input_checkpoint = checkpoint.model_checkpoint_path
    print(input_checkpoint)
    output_graph = 'frozen_model.pb'
    output_node_names = ['model/inference/dense/BiasAdd']

    tf.reset_default_graph()
    with tf.variable_scope('datafeeder') as scope:
        inputs = tf.placeholder(tf.int32, [None, None], 'inputs')
        input_lengths = tf.placeholder(tf.int32, [None], 'input_lengths')
    with tf.variable_scope('model') as scope:
        model = create_model('tacotron', hparams)
        model.initialize(inputs, input_lengths)

    graph = tf.get_default_graph()
    saver = tf.train.Saver()

    with tf.Session(graph=graph) as sess:
        sess.run(tf.global_variables_initializer())
        saver.restore(sess, input_checkpoint)
        input_graph_def = graph.as_graph_def()
        output_graph_def = graph_util.convert_variables_to_constants(
            sess,
            input_graph_def,
            output_node_names
        )
        with tf.gfile.GFile(output_graph, 'wb') as f:
            f.write(output_graph_def.SerializeToString())
        with open('operations.txt', 'w') as wf:
            for op in graph.get_operations():
                wf.write(op.name)
                wf.write('\n')
        print('%d ops in the final graph.' % len(output_graph_def.node))


"""
Management command to recreate the Elasticsearch index
"""
import logging

from django.core.management.base import BaseCommand

from search.indexing_api import (
    recreate_index,
    __name__ as indexing_api_name,
)

class Command(BaseCommand):
    """
    Command for recreate_index
    """
    help = "Clears existing Elasticsearch indices and creates a new index and mapping."

    def add_arguments(self, parser):  # pylint: disable=no-self-use
        """Configure command args"""
        parser.add_argument(
            '--profile',
            action='store_true',
            dest='profile',
            default=False,
        )

    def handle(self, *args, **kwargs):  # pylint: disable=unused-argument
        """
        Recreates the index
        """
        log = logging.getLogger(indexing_api_name)
        console = logging.StreamHandler(self.stderr)
        console.setLevel(logging.DEBUG)
        log.addHandler(console)
        log.level = logging.INFO

        if kwargs['profile']:
            import cProfile
            import uuid
            profile = cProfile.Profile()
            profile.enable()
            recreate_index()
            profile.disable()
            filename = 'recreate_index_{}.profile'.format(uuid.uuid4())
            profile.dump_stats(filename)
            self.stdout.write('Output profiling data to: {}'.format(filename))
        else:
            recreate_index()


""" Упражнение 1
Дополните приведенный код так, чтобы он вывел произведение элементов кортежа numbers.
"""
from math import prod
numbers = (2, 3, 5, 7, -11, 13, 17, 19, 23, 29, 31, -6, 41, 43, 47, 53, 59, 61, -96, 71, 1000, -1)
print(prod(numbers))

""" Упражнение 2
Дополните приведенный код так, чтобы он преобразовал строку в кортеж символов строки и вывел результат.
"""
data = 'Python для продвинутых!'
print(tuple(data))

""" Упражнение 3
Программист Тимур написал программу для работы с биографическими данными русских поэтов. Данные содержатся в кортежах
вида (фамилия, год рождения, город рождения). В процессе работы программы в некотором кортеже poet_data обнаружилась
ошибка: ('Пушкин', 1799, 'Санкт-Петербург'), неверно указано место рождения, ведь Александр Пушкин родился в Москве.
Дополните приведенный код так, чтобы в переменной poet_data находился правильный кортеж (с исправленным значением),
а затем выведите его содержимое.
"""
poet_data = ('Пушкин', 1799, 'Санкт-Петербург')
poet_data = poet_data[:-1] + ('Москва',)
print(poet_data)

""" Упражнение 4
Дополните приведенный код так, чтобы он вывел список, содержащий средние арифметические значения чисел каждого
вложенного кортежа в заданном кортеже кортежей numbers.
"""
numbers = ((10, 10, 10, 12), (30, 45, 56, 45), (81, 80, 39, 32), (1, 2, 3, 4), (90, 10))
print([sum(x) / len(x) for x in numbers])

""" Упражнение 5
Уравнение параболы имеет вид y =ax^2 + bx + c, где a !=0. Напишите программу, которая по введенным значениям a,b,c
определяет и выводит вершину параболы.
Формат входных данных
На вход программе подаются три целых числа, каждое на отдельной строке.
Формат выходных данных
Программа должна вывести координаты вершины параболы.
Sample Input 1:
    -2
    6
    1
Sample Output 1:
    (1.5, 5.5)
Sample Input 2:
    -5
    2
    0
Sample Output 2:
    (0.2, 0.2)
"""
def comp_h(a, b, c):
    return -(b/(2*a)), (4*a*c-b**2)/(4*a)
a, b, c = [int(input()) for _ in range(3)]
print(comp_h(a, b, c))

""" Упражнение 6
Напишите программу, которая выводит список хорошистов и отличников в классе.
Формат входных данных
На вход программе подается натуральное число n, далее следует n строк с фамилией школьника и его оценкой на каждой из
них.
Формат выходных данных
Программа должна вывести сначала все введённые строки с фамилиями и оценками учеников в том же порядке.
Затем следует пустая строка, а затем выводятся строки с фамилиями и оценками хорошистов и отличников (в том же порядке).
Примечание 1. Оценка ученика – это натуральное число от 1 до 5.
Примечание 2. Гарантируется, что в классе есть хотя бы один хорошист – обладатель оценки 4, или отличник – получивший 5.
Sample Input 1:
    3
    Гуев 3
    Чаниев 5
    Барсуков 2
Sample Output 1:
    Гуев 3
    Чаниев 5
    Барсуков 2

    Чаниев 5
"""
l = [input().split() for _ in range(int(input()))]
for i in l:
    print(*i)
print()
for i in l:
    if int(i[1]) > 3:
        print(*i)

""" Упражнение 7
Напишите программу, которая считывает натуральное число n и выводит первые n чисел последовательности Трибоначчи.
Формат входных данных
На вход программе подается одно число n (n≤100) – количество членов последовательности.
Формат выходных данных
Программа должна вывести члены последовательности Трибоначчи, отделенные символом пробела.
Примечание. Последовательность Трибоначчи – последовательность натуральных чисел, где каждое последующее число является
суммой трех предыдущих:
1, 1, 1, 3, 5, 9, 17, 31, 57, 105 …
"""
n = int(input())
tr = [1]
while len(tr) < n:
    if len(tr) < 3:
        tr.append(1)
    else:
        tr.append(tr[-3]+tr[-2]+tr[-1])
print(*tr)

3) Поколение Python - курс для продвинутых/06) Кортежи/6.3 Основы работы с кортежами. Часть 2.py


from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from model_utils import Choices
import reversion

from sapl.base.models import Autor
from sapl.materia.models import TipoMateriaLegislativa, UnidadeTramitacao
from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES, texto_upload_path,
                        get_settings_auth_user_model,
                        OverwriteStorage)

@reversion.register()
class TipoDocumentoAdministrativo(models.Model):
    sigla = models.CharField(max_length=5, verbose_name=_('Sigla'))
    descricao = models.CharField(max_length=50, verbose_name=_('Descrição'))

    class Meta:
        verbose_name = _('Tipo de Documento Administrativo')
        verbose_name_plural = _('Tipos de Documento Administrativo')
        ordering = ['descricao']

    def __str__(self):
        return self.descricao

"""
uuid4 + filenames diversos apesar de tornar url de um arquivo praticamente
impossível de ser localizado não está controlando o acesso.
Exemplo: o SAPL está configurado para ser docs adm restritivo porém
alguem resolve perga o link e mostrar o tal arquivo para um amigo, ou um
vizinho de departamento que não possui acesso... ou mesmo alguem que nem ao
menos está logado... este arquivo estará livre

outro caso, um funcionário bem intencionado, mas com um computador infectado
que consegue pegar todos os links da página que ele está acessando e esse
funcionário possui permissão para ver arquivos de docs administrativos.
Consequentemente os arquivos se tornarão públicos pois podem ser acessados
via url sem controle de acesso.

* foi aberta uma issue no github para rever a questão de arquivos privados:
https://github.com/interlegis/sapl/issues/751

a solução dela deverá dar o correto tratamento a essa questão.

def texto_upload_path(instance, filename):
    return '/'.join([instance._meta.model_name, str(uuid4()), filename])
"""

@reversion.register()
class Protocolo(models.Model):
    numero = models.PositiveIntegerField(
        blank=False,
        null=False,
        verbose_name=_('Número de Protocolo'))
    ano = models.PositiveSmallIntegerField(
        blank=False,
        null=False,
        choices=RANGE_ANOS,
        verbose_name=_('Ano do Protocolo'))
    data = models.DateField(
        null=True,
        blank=True,
        verbose_name=_('Data do Protocolo'),
        help_text=_('Informado manualmente'))
    hora = models.TimeField(
        null=True,
        blank=True,
        verbose_name=_('Hora do Protocolo'),
        help_text=_('Informado manualmente'))
    timestamp_data_hora_manual = models.DateTimeField(default=timezone.now)
    user_data_hora_manual = models.CharField(
        max_length=256,
        blank=True,
        verbose_name=_('IP'),
        help_text=_('Usuário que está realizando Protocolo e informando data e hora manualmente.'))
    ip_data_hora_manual = models.CharField(
        max_length=256,
        blank=True,
        verbose_name=_('IP'),
        help_text=_('Endereço IP da estação de trabalho do usuário que está realizando Protocolo e '
                    'informando data e hora manualmente.'))
    # Não foi utilizado auto_now_add=True em timestamp porque ele usa datetime.now que não é timezone aware.
    timestamp = models.DateTimeField(
        null=True,
        blank=True,
        default=timezone.now)
    tipo_protocolo = models.PositiveIntegerField(
        blank=True,
        null=True,
        verbose_name=_('Tipo de Protocolo'))
    tipo_processo = models.PositiveIntegerField()
    interessado = models.CharField(
        max_length=200,
        blank=True,
        verbose_name=_('Interessado'))
    autor = models.ForeignKey(
        Autor,
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    assunto_ementa = models.TextField(blank=True)
    tipo_documento = models.ForeignKey(
        TipoDocumentoAdministrativo,
        blank=True,
        null=True,
        on_delete=models.PROTECT,
        verbose_name=_('Tipo de Documento'))
    tipo_materia = models.ForeignKey(
        TipoMateriaLegislativa,
        blank=True,
        null=True,
        on_delete=models.PROTECT,
        verbose_name=_('Tipo de Matéria'))
    numero_paginas = models.PositiveIntegerField(
        blank=True,
        null=True,
        verbose_name=_('Número de Páginas'))
    observacao = models.TextField(blank=True, verbose_name=_('Observação'))
    anulado = models.BooleanField(default=False)
    user_anulacao = models.CharField(max_length=20, blank=True)
    ip_anulacao = models.CharField(max_length=15, blank=True)
    justificativa_anulacao = models.CharField(
        max_length=260,
        blank=True,
        verbose_name=_('Motivo'))
    timestamp_anulacao = models.DateTimeField(blank=True, null=True)

    class Meta:
        verbose_name = _('Protocolo')
        verbose_name_plural = _('Protocolos')
        permissions = (
            ('action_anular_protocolo', _('Permissão para Anular Protocolo')),
        )
        unique_together = ('numero', 'ano',)

    def __str__(self):
        return _('%(numero)s/%(ano)s') % {
            'numero': self.numero, 'ano': self.ano
        }

@reversion.register()
class DocumentoAdministrativo(models.Model):
    tipo = models.ForeignKey(
        TipoDocumentoAdministrativo, on_delete=models.PROTECT,
        verbose_name=_('Tipo Documento'))
    numero = models.PositiveIntegerField(verbose_name=_('Número'))

    complemento = models.CharField(max_length=10, blank=True,
        verbose_name=_('Complemento'))

    ano = models.PositiveSmallIntegerField(verbose_name=_('Ano'),
                                           choices=RANGE_ANOS)
    protocolo = models.ForeignKey(
        Protocolo,
        blank=True,
        null=True,
        on_delete=models.PROTECT,
        verbose_name=_('Protocolo'))
    data = models.DateField(verbose_name=_('Data'))

    interessado = models.CharField(
        max_length=50, blank=True, verbose_name=_('Interessado'))
    autor = models.ForeignKey(Autor, blank=True, null=True,
                              on_delete=models.PROTECT)
    dias_prazo = models.PositiveIntegerField(
        blank=True, null=True, verbose_name=_('Dias Prazo'))
    data_fim_prazo = models.DateField(
        blank=True, null=True, verbose_name=_('Data Fim Prazo'))
    tramitacao = models.BooleanField(
        verbose_name=_('Em Tramitação?'),
        choices=YES_NO_CHOICES,
        default=False)
    assunto = models.TextField(verbose_name=_('Assunto'))
    numero_externo = models.PositiveIntegerField(
        blank=True,
        null=True,
        verbose_name=_('Número Externo'))
    observacao = models.TextField(
        blank=True, verbose_name=_('Observação'))
    texto_integral = models.FileField(
        max_length=300,
        blank=True,
        null=True,
        storage=OverwriteStorage(),
        upload_to=texto_upload_path,
        verbose_name=_('Texto Integral'))
    restrito = models.BooleanField(default=False,
                                   verbose_name=_('Acesso Restrito'),
                                   blank=True)

    anexados = models.ManyToManyField(
        'self',
        blank=True,
        through='Anexado',
        symmetrical=False,
        related_name='anexo_de',
        through_fields=(
            'documento_principal',
            'documento_anexado'
        )
    )

    user = models.ForeignKey(
        get_settings_auth_user_model(),
        verbose_name=_('Usuário'),
        on_delete=models.PROTECT,
        null=True,
        blank=True
    )
    ip = models.CharField(
        verbose_name=_('IP'),
        max_length=60,
        blank=True,
        default=''
    )
    ultima_edicao = models.DateTimeField(
        verbose_name=_('Data e Hora da Edição'),
        blank=True, null=True
    )

    class Meta:
        verbose_name = _('Documento Administrativo')
        verbose_name_plural = _('Documentos Administrativos')

    def __str__(self):
        return _('%(tipo)s - %(assunto)s') % {
            'tipo': self.tipo, 'assunto': self.assunto
        }

    def delete(self, using=None, keep_parents=False):
        texto_integral = self.texto_integral
        result = super().delete(using=using, keep_parents=keep_parents)

        if texto_integral:
            texto_integral.delete(save=False)

        return result

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):

        if not self.pk and self.texto_integral:
            texto_integral = self.texto_integral
            self.texto_integral = None
            models.Model.save(self, force_insert=force_insert,
                              force_update=force_update,
                              using=using,
                              update_fields=update_fields)
            self.texto_integral = texto_integral

        return models.Model.save(self, force_insert=force_insert,
                                 force_update=force_update,
                                 using=using,
                                 update_fields=update_fields)

@reversion.register()
class DocumentoAcessorioAdministrativo(models.Model):
    documento = models.ForeignKey(DocumentoAdministrativo,
                                  on_delete=models.PROTECT)
    tipo = models.ForeignKey(
        TipoDocumentoAdministrativo,
        on_delete=models.PROTECT,
        verbose_name=_('Tipo'))
    nome = models.CharField(max_length=30, verbose_name=_('Nome'))
    arquivo = models.FileField(
        max_length=300,
        blank=True,
        null=True,
        upload_to=texto_upload_path,
        storage=OverwriteStorage(),
        verbose_name=_('Arquivo'))
    data = models.DateField(blank=True, null=True, verbose_name=_('Data'))
    autor = models.CharField(
        max_length=200, blank=True, verbose_name=_('Autor'))
    assunto = models.TextField(
        blank=True, verbose_name=_('Assunto'))
    indexacao = models.TextField(blank=True)

    class Meta:
        verbose_name = _('Documento Acessório')
        verbose_name_plural = _('Documentos Acessórios')

    def __str__(self):
        return self.nome

    def delete(self, using=None, keep_parents=False):
        arquivo = self.arquivo
        result = super().delete(using=using, keep_parents=keep_parents)

        if arquivo:
            arquivo.delete(save=False)

        return result

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):

        if not self.pk and self.arquivo:
            arquivo = self.arquivo
            self.arquivo = None
            models.Model.save(self, force_insert=force_insert,
                              force_update=force_update,
                              using=using,
                              update_fields=update_fields)
            self.arquivo = arquivo

        return models.Model.save(self, force_insert=force_insert,
                                 force_update=force_update,
                                 using=using,
                                 update_fields=update_fields)

@reversion.register()
class StatusTramitacaoAdministrativo(models.Model):
    INDICADOR_CHOICES = Choices(
        ('F', 'fim', _('Fim')),
        ('R', 'retorno', _('Retorno')),
    )

    sigla = models.CharField(max_length=10, verbose_name=_('Sigla'))
    descricao = models.CharField(max_length=60, verbose_name=_('Descrição'))
    # TODO make specific migration considering both ind_fim_tramitacao,
    # ind_retorno_tramitacao
    indicador = models.CharField(
        max_length=1, verbose_name=_('Indicador da Tramitação'),
        choices=INDICADOR_CHOICES)

    class Meta:
        verbose_name = _('Status de Tramitação')
        verbose_name_plural = _('Status de Tramitação')

    def __str__(self):
        return self.descricao

@reversion.register()
class TramitacaoAdministrativo(models.Model):
    status = models.ForeignKey(
        StatusTramitacaoAdministrativo,
        on_delete=models.PROTECT,
        verbose_name=_('Status'))
    documento = models.ForeignKey(DocumentoAdministrativo,
                                  on_delete=models.PROTECT)
    timestamp = models.DateTimeField(default=timezone.now)
    data_tramitacao = models.DateField(
        verbose_name=_('Data Tramitação'))
    unidade_tramitacao_local = models.ForeignKey(
        UnidadeTramitacao,
        related_name='adm_tramitacoes_origem',
        on_delete=models.PROTECT,
        verbose_name=_('Unidade Local'))
    data_encaminhamento = models.DateField(
        blank=True, null=True, verbose_name=_('Data Encaminhamento'))
    unidade_tramitacao_destino = models.ForeignKey(
        UnidadeTramitacao,
        related_name='adm_tramitacoes_destino',
        on_delete=models.PROTECT,
        verbose_name=_('Unidade Destino'))
    urgente = models.BooleanField(verbose_name=_('Urgente ?'),
                                  choices=YES_NO_CHOICES,
                                  default=False)
    texto = models.TextField(verbose_name=_('Texto da Ação'))
    data_fim_prazo = models.DateField(
        blank=True, null=True, verbose_name=_('Data Fim do Prazo'))
    user = models.ForeignKey(get_settings_auth_user_model(),
                             verbose_name=_('Usuário'),
                             on_delete=models.PROTECT,
                             null=True,
                             blank=True)
    ip = models.CharField(verbose_name=_('IP'),
                          max_length=60,
                          blank=True,
                          default='')
    ultima_edicao = models.DateTimeField(
        verbose_name=_('Data e Hora da Edição'),
        blank=True, null=True
    )

    class Meta:
        verbose_name = _('Tramitação de Documento Administrativo')
        verbose_name_plural = _('Tramitações de Documento Administrativo')

    def __str__(self):
        return _('%(documento)s - %(status)s') % {
            'documento': self.documento, 'status': self.status
        }

@reversion.register()
class Anexado(models.Model):
    documento_principal = models.ForeignKey(
        DocumentoAdministrativo, related_name='documento_principal_set',
        on_delete=models.CASCADE,
        verbose_name=_('Documento Principal')
    )
    documento_anexado = models.ForeignKey(
        DocumentoAdministrativo, related_name='documento_anexado_set',
        on_delete=models.CASCADE,
        verbose_name=_('Documento Anexado')
    )
    data_anexacao = models.DateField(verbose_name=_('Data Anexação'))
    data_desanexacao = models.DateField(
        blank=True, null=True, verbose_name=_('Data Desanexação')
    )

    class Meta:
        verbose_name = _('Anexado')
        verbose_name_plural = _('Anexados')

    def __str__(self):
        return _('Anexado: %(documento_anexado_tipo)s %(documento_anexado_numero)s'
                 '/%(documento_anexado_ano)s\n') % {
                     'documento_anexado_tipo': self.documento_anexado.tipo,
                     'documento_anexado_numero': self.documento_anexado.numero,
                     'documento_anexado_ano': self.documento_anexado.ano
        }

@reversion.register()
class AcompanhamentoDocumento(models.Model):
    usuario = models.CharField(max_length=50)
    documento = models.ForeignKey(
        DocumentoAdministrativo, on_delete=models.CASCADE)
    email = models.EmailField(
        max_length=100, verbose_name=_('E-mail'))
    data_cadastro = models.DateField(auto_now_add=True)
    hash = models.CharField(max_length=8)
    confirmado = models.BooleanField(default=False)

    class Meta:
        verbose_name = _('Acompanhamento de Documento')
        verbose_name_plural = _('Acompanhamentos de Documento')

    def __str__(self):
        if self.data_cadastro is None:
            return _('%(documento)s - %(email)s') % {
                'documento': self.documento,
                'email': self.email
            }
        else:
            return _('%(documento)s - %(email)s - Registrado em: %(data)s') % {
                'documento': self.documento,
                'email': self.email,
                'data': str(self.data_cadastro.strftime('%d/%m/%Y'))
            }


# O(n) time | O(n) space
def nextGreaterElement(array):
    # to solve this problem, we could perform a brute-force approach solution; however this would take O(n^2) time.
	# so we can create a solution which uses a bit of space: use of stacks. we can provide two solutions:
	# FIRST SOLUTION: initialise an array with -1's and length the original array. Then what we do is create a stack where we push the elements of the array in the following way:
	# - i = 0: push the first index of the element, in this case 0.
	# - If array[i] < top of the stack, then we push the index i to the top of the stack.
	# - If array[i] > top of the stack, then the update results at the index of the top of the stack to be array[i] and pop the top of the stack. Traverse the stack until finding is not bigger or empty.
	# for either case, we will push the index i.
	# since the array is circular (the end connects with the beginning), we need to do a for loop of double range, and use the mod operator to take the correct index. When that for loop ends we simply return result.

	result = [-1] * len(array)
	stack = []

	for idx in range(2* len(array)):
		# when we reach the second iteration of the circular array, we want to go back using the mod operator
		circularIdx = idx % len(array)

		# we will have a while loop that iterates through the stack for every index i, only if the stack is not empty
		# AND the array[top of the stack], where the top is at the end of the array (len(array) minus one) is less than the element at idx:
		while len(stack) > 0 and array[stack[len(stack) - 1]] < array[circularIdx]:
			# the next element is the next greater element, so we pop the element (which is the index),
			# and we give the result output.
			top = stack.pop()
			result[top] = array[circularIdx]

		stack.append(circularIdx)
	return result


import os
import sys
import tempfile
import yaml
import zlib
import numpy as np
import simplejson as js
import subprocess as sb
from time import time,sleep
from os import path
from scipy.stats.mstats import mquantiles

try:
    from sklearn.lda import LDA
    from sklearn.svm import SVC
    from sklearn.neighbors import KNeighborsClassifier as KNN
    from sklearn.feature_selection import SelectKBest, f_classif

    import samcnet.mh as mh
    import samcnet.samc as samc
    from samcnet.mixturepoisson import *
    from samcnet.lori import *
except ImportError as e:
    sys.exit("Make sure LD_LIBRARY_PATH is set correctly and that the build"+\
            " directory is populated by waf.\n\n %s" % str(e))

if 'WORKHASH' in os.environ:
    try:
        server = os.environ['SERVER']
    except:
        sys.exit("ERROR in worker: Need SERVER environment variable defined.")

if 'PARAM' in os.environ:
    params = yaml.load(os.environ['PARAM'])
else:
    params = {}

def setv(p,s,d,conv=None):
    if s not in p:
        p[s] = str(d)
        return d
    elif conv is not None:
        return conv(p[s])
    else:
        p[s]

iters = setv(params, 'iters', int(1e4), int)

num_feat = setv(params, 'num_feat', 4, int)
#seed = setv(params, 'seed', 1234, int)
seed = setv(params, 'seed', np.random.randint(10**8), int)
rseed = setv(params, 'rseed', np.random.randint(10**8), int)

Ntrn = setv(params, 'Ntrn', 3, int)
Ntst = setv(params, 'Ntst', 3000, int)
f_glob = setv(params, 'f_glob', 2, int)
subclasses = setv(params, 'subclasses', 2, int)
f_het = setv(params, 'f_het', 1, int)
f_rand = setv(params, 'f_rand', 0, int)
rho = setv(params, 'rho', 0.6, float)
f_tot = setv(params, 'f_tot', f_glob+f_het*subclasses+f_rand, int)
blocksize = setv(params, 'blocksize', 1, int)
mu0 = setv(params, 'mu0', -1.2, float)
mu1 = setv(params, 'mu1', -0.2, float)
sigma0 = setv(params, 'sigma0', 0.5, float)
sigma1 = setv(params, 'sigma1', 0.2, float)
S = setv(params, 'S', 10.0, float)

lowd = setv(params, 'lowd', 9.0, float)
highd = setv(params, 'highd', 11.0, float)

numlam = setv(params, 'numlam', 20, int)

output = {}
output['errors'] = {}
errors = output['errors']
np.seterr(all='ignore') # Careful with this

t1 = time()

#trn_data, trn_labels, tst_data, tst_labels = data_yj(params)
trn_data, trn_labels, tst_data, tst_labels = data_jason(params)

norm_trn_data = norm(trn_data)
norm_tst_data = norm(tst_data)

norm_trn_data0, norm_trn_data1 = split(norm_trn_data)
norm_tst_data0, norm_tst_data1 = split(norm_tst_data)
trn_data0, trn_data1 = split(trn_data)
tst_data0, tst_data1 = split(tst_data)

#################### CLASSIFICATION ################
sklda = LDA()
skknn = KNN(3)
sksvm = SVC()
sklda.fit(norm_trn_data, trn_labels)
skknn.fit(norm_trn_data, trn_labels)
sksvm.fit(norm_trn_data, trn_labels)
errors['lda'] = (1-sklda.score(norm_tst_data, tst_labels))
errors['knn'] = (1-skknn.score(norm_tst_data, tst_labels))
errors['svm'] = (1-sksvm.score(norm_tst_data, tst_labels))

bayes0 = GaussianBayes(np.zeros(num_feat), 1, 8, np.eye(num_feat)*3, norm_trn_data0)
bayes1 = GaussianBayes(np.zeros(num_feat), 1, 8, np.eye(num_feat)*3, norm_trn_data1)

# Gaussian Analytic
gc = GaussianCls(bayes0, bayes1)
errors['gauss'] = gc.approx_error_data(norm_tst_data, tst_labels)

# MPM Model
#d0 = np.asarray(mquantiles(trn_data0, 0.75, axis=1)).reshape(-1)
#d1 = np.asarray(mquantiles(trn_data1, 0.75, axis=1)).reshape(-1)
#dist0 = MPMDist(trn_data0,kmax=1,priorkappa=150,lammove=0.01,mumove=0.08,d=d0)
#dist1 = MPMDist(trn_data1,kmax=1,priorkappa=150,lammove=0.01,mumove=0.08,d=d1)

up = True
kappa = 10.0
S = np.eye(4) * 0.4 * (kappa - 1 - 4)
dist0 = MPMDist(trn_data0,kmax=1,priorkappa=200,lammove=0.05,mumove=0.08,usepriors=up,
        kappa=kappa, S=S)
dist1 = MPMDist(trn_data1,kmax=1,priorkappa=200,lammove=0.05,mumove=0.08,usepriors=up,
        kappa=kappa, S=S)
mpm1 = MPMCls(dist0, dist1)
mhmc1 = mh.MHRun(mpm1, burn=2000, thin=50)
mhmc1.sample(iters,verbose=False)
errors['mpm'] = mpm1.approx_error_data(mhmc1.db, tst_data, tst_labels,numlam=numlam)
print("")
print("skLDA error: %f" % errors['lda'])
print("skKNN error: %f" % errors['knn'])
print("skSVM error: %f" % errors['svm'])
print("gauss error: %f" % errors['gauss'])
print("my MP error: %f" % errors['mpm'])

#n,gext,grid = get_grid_data(np.vstack(( trn_data0, trn_data1 )), positive=True)

#def myplot(ax,g,data0,data1,gext):
    #ax.plot(data0[:,0], data0[:,1], 'g.',label='0', alpha=0.5)
    #ax.plot(data1[:,0], data1[:,1], 'r.',label='1', alpha=0.5)
    #ax.legend(fontsize=8, loc='best')

    ##im = ax.imshow(g, extent=gext, aspect=1.0, origin='lower')
    ##p.colorbar(im,ax=ax)
    #ax.contour(g, [0.0], extent=gext, aspect=1.0, origin='lower', cmap = p.cm.gray)

#p.close("all")
#gavg = mpm1.calc_gavg(mhmc1.db, grid, numlam=numlam).reshape(-1,n)
##myplot(p.subplot(3,1,1),gavg,trn_data0,trn_data1,gext)

#g0 = mpm1.dist0.calc_db_g(mhmc1.db, mhmc1.db.root.object.dist0, grid)
#g1 = mpm1.dist1.calc_db_g(mhmc1.db, mhmc1.db.root.object.dist1, grid)

##def jitter(x):
    ##rand = np.random.rand
    ##n = x.shape[0]
    ##return (x.T + rand(n)).T
#def jitter(x):
    #rand = np.random.rand
    #return x + rand(*x.shape)-0.5

##myplot(p.subplot(3,1,3),err.reshape(-1,n),jitter(tst_data0),jitter(tst_data1),gext)

#def plot_all(n, gext, grid, data0, data1, g0, g1, gavg):
    #Z = np.exp(g0)+np.exp(g1)
    #eg0 = np.exp(g0)/Z
    #eg1 = np.exp(g1)/Z
    #err = np.minimum(eg0,eg1)
    #err = err.reshape(-1,n)

    #lx,hx,ly,hy = gext
    #asp = float(hx-lx) / (hy-ly)
    #alp = 1.0
    #ms = 8

    #p.figure()
    #p.subplot(2,2,1)
    #p.plot(data0[:,0], data0[:,1], 'g^',label='0', markersize=ms, alpha=alp)
    #p.plot(data1[:,0], data1[:,1], 'ro',label='1', markersize=ms, alpha=alp)
    #p.legend(fontsize=8, loc='best')
    ##p.contour(gavg, extent=gext, aspect=1, origin='lower', cmap = p.cm.gray)
    ##p.contour(gavg, [0.0], extent=gext, aspect=1, origin='lower', cmap = p.cm.gray)
    ##p.imshow(gavg, extent=gext, aspect=1, origin='lower')
    ##p.imshow(g0.reshape(-1,n), extent=gext, aspect=asp, origin='lower')
    ##p.colorbar()
    #p.contour(g0.reshape(-1,n), extent=gext, aspect=asp, origin='lower', cmap = p.cm.Greens)

    #p.subplot(2,2,2)
    #p.plot(data0[:,0], data0[:,1], 'g^',label='0', markersize=ms, alpha=alp)
    #p.plot(data1[:,0], data1[:,1], 'ro',label='1', markersize=ms, alpha=alp)
    #p.legend(fontsize=8, loc='best')
    ##p.contour(g0.reshape(-1,n), extent=gext, aspect=1, origin='lower', cmap = p.cm.Greens)
    ##p.contour(g1.reshape(-1,n), extent=gext, aspect=1, origin='lower', cmap = p.cm.Reds)
    ##p.contour((g1-g0).reshape(-1,n), [0.0], extent=gext, aspect=1, origin='lower', cmap = p.cm.gray)
    ##p.imshow((g1-g0).reshape(-1,n), extent=gext, aspect=1, origin='lower')
    ##p.imshow(g1.reshape(-1,n), extent=gext, aspect=asp, origin='lower')
    ##p.colorbar()
    #p.contour(g1.reshape(-1,n), extent=gext, aspect=asp, origin='lower', cmap = p.cm.Reds)

    #p.subplot(2,2,3)
    #p.plot(data0[:,0], data0[:,1], 'g^',label='0', markersize=ms, alpha=alp)
    #p.plot(data1[:,0], data1[:,1], 'ro',label='1', markersize=ms, alpha=alp)
    #p.legend(fontsize=8, loc='best')
    ##p.imshow(err, extent=gext, origin='lower', aspect=asp)
    ##p.colorbar()
    #p.contour((g1-g0).reshape(-1,n), [0.0], extent=gext, aspect=asp, origin='lower', cmap = p.cm.gray)
    ##p.contour(eg0.reshape(-1,n), extent=gext, aspect=1, origin='lower', cmap = p.cm.Greens)
    ##p.contour(eg1.reshape(-1,n), extent=gext, aspect=1, origin='lower', cmap = p.cm.Reds)

    #p.subplot(2,2,4)
    #p.plot(data0[:,0], data0[:,1], 'g^',label='0', markersize=ms)
    #p.plot(data1[:,0], data1[:,1], 'ro',label='1', markersize=ms)
    #p.legend(fontsize=8, loc='best')
    #p.contour((g1-g0).reshape(-1,n), [0.0], extent=gext, aspect=asp, origin='lower', cmap = p.cm.gray)
    #CS = p.contour(err, [0.4, 0.3, 0.2, 0.1, 0.05], extent=gext, aspect=asp, origin='lower')
    #p.clabel(CS, inline=1, fontsize=10, aspect=asp)
    #p.show()

#def plot_concise(n, gext, grid, data0, data1, g0, g1, gavg):
    #p.figure()
    #Z = np.exp(g0)+np.exp(g1)
    #eg0 = np.exp(g0)/Z
    #eg1 = np.exp(g1)/Z
    #err = np.minimum(eg0,eg1)
    #err = err.reshape(-1,n)
    #ms=8

    #lx,hx,ly,hy = gext
    #asp = float(hx-lx) / (hy-ly)
    #p.plot(data0[:,0], data0[:,1], 'g^',label='0', markersize=ms)
    #p.plot(data1[:,0], data1[:,1], 'ro',label='1', markersize=ms)
    #p.legend(fontsize=8, loc='best')

    #cont = (g0.max() + g1.max()) / 2.0 - 0.6
    #p.contour(g0.reshape(-1,n), [cont], extent=gext, aspect=asp, origin='lower', cmap = p.cm.gray)
    #p.contour(g1.reshape(-1,n), [cont], extent=gext, aspect=asp, origin='lower', cmap = p.cm.gray)
    #p.imshow(err, extent=gext, origin='lower', aspect=asp, alpha=0.4, cmap = p.cm.Reds)
    #p.contour((g1-g0).reshape(-1,n), [0.0], extent=gext, aspect=asp, origin='lower', cmap = p.cm.gray, linewidth=15.0)
    #CS = p.contour(err, [0.4, 0.3, 0.2, 0.1, 0.05], extent=gext, aspect=asp, origin='lower')
    #p.clabel(CS, inline=1, fontsize=10, aspect=asp)
    #p.show()

#plot_all(n, gext, grid, trn_data0, trn_data1, g0,g1,gavg)
#plot_concise(n, gext, grid, trn_data0, trn_data1, g0,g1,gavg)

##n,gext,grid = get_grid_data(np.vstack(( norm_trn_data0, norm_trn_data1 )), positive=False)
##myplot(p.subplot(3,1,3),sksvm.decision_function(grid).reshape(-1,n),norm_trn_data0,norm_trn_data1,gext)

#p.figure()
#myplot(p.subplot(1,1,1),gavg,jitter(tst_data0),jitter(tst_data1),gext)
#p.axis(gext)
#mpm1.dist0.plot_traces(mhmc1.db, '/object/dist0', ['sigma'])
#p.show()

output['seed'] = seed
output['time'] = time()-t1
output['acceptance'] = float(mhmc1.accept_loc)/mhmc1.total_loc

if 'WORKHASH' in os.environ:
    import zmq
    ctx = zmq.Context()
    socket = ctx.socket(zmq.REQ)
    socket.connect('tcp://'+server+':7000')

    wiredata = zlib.compress(js.dumps(output))
    #wiredata = s.read_db()
    socket.send(os.environ['WORKHASH'], zmq.SNDMORE)
    socket.send(wiredata)
    socket.recv()
    socket.close()
    ctx.term()

mhmc1.db.close()