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

import os.path
import re
from tkinter import ttk
import traceback

from thonny import memory
from thonny.common import ToplevelCommand, parse_shell_command
from thonny.misc_utils import running_on_mac_os, shorten_repr
from thonny.ui_utils import EnhancedTextWithLogging
import tkinter as tk
from thonny.globals import get_workbench, get_runner
from thonny.codeview import EDIT_BACKGROUND

class ShellView (ttk.Frame):
    def __init__(self, master, **kw):
        ttk.Frame.__init__(self, master, **kw)

        self.vert_scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL)
        self.vert_scrollbar.grid(row=0, column=2, sticky=tk.NSEW)
        self.text = ShellText(self,
                            font=get_workbench().get_font("EditorFont"),
                            #foreground="white",
                            #background="#666666",
                            highlightthickness=0,
                            #highlightcolor="LightBlue",
                            borderwidth=0,
                            yscrollcommand=self.vert_scrollbar.set,
                            padx=4,
                            insertwidth=2,
                            height=10,
                            undo=True,
                            autoseparators=False)

        get_workbench().event_generate("ShellTextCreated", text_widget=self.text)
        get_workbench().add_command("clear_shell", "edit", "Clear shell",
                                    self.clear_shell,
                                    group=200)

        self.text.grid(row=0, column=1, sticky=tk.NSEW)
        self.vert_scrollbar['command'] = self.text.yview
        self.columnconfigure(1, weight=1)
        self.rowconfigure(0, weight=1)

    def focus_set(self):
        self.text.focus_set()

    def add_command(self, command, handler):
        self.text.add_command(command, handler)

    def submit_command(self, cmd_line):
        self.text.submit_command(cmd_line)

    def clear_shell(self):
        self.text._clear_shell()

class ShellText(EnhancedTextWithLogging):

    def __init__(self, master, cnf={}, **kw):
        if not "background" in kw:
            kw["background"] = EDIT_BACKGROUND

        EnhancedTextWithLogging.__init__(self, master, cnf, **kw)
        self.bindtags(self.bindtags() + ('ShellText',))

        self._before_io = True
        self._command_history = [] # actually not really history, because each command occurs only once
        self._command_history_current_index = None

        self.text_mode = "toplevel"

        """
        self.margin = tk.Text(self,
                width = 4,
                padx = 4,
                highlightthickness = 0,
                takefocus = 0,
                bd = 0,
                #font = self.font,
                cursor = "dotbox",
                background = '#e0e0e0',
                foreground = '#999999',
                #state='disabled'
                )
        self.margin.grid(row=0, column=0)
        """

        self.bind("<Up>", self._arrow_up, True)
        self.bind("<Down>", self._arrow_down, True)
        self.bind("<KeyPress>", self._text_key_press, True)
        self.bind("<KeyRelease>", self._text_key_release, True)

        vert_spacing = 10
        io_indent = 16

        self.tag_configure("toplevel", font=get_workbench().get_font("EditorFont"))
        self.tag_configure("prompt", foreground="purple", font=get_workbench().get_font("BoldEditorFont"))
        self.tag_configure("command", foreground="black")
        self.tag_configure("version", foreground="DarkGray")
        self.tag_configure("automagic", foreground="DarkGray")
        #self.tag_configure("value", foreground="DarkGreen")
        #self.tag_configure("value", foreground="#B25300")
        self.tag_configure("value", foreground="DarkBlue") # TODO: see also _text_key_press and _text_key_release
        self.tag_configure("error", foreground="Red")

        self.tag_configure("io", lmargin1=io_indent, lmargin2=io_indent, rmargin=io_indent,
                                font=get_workbench().get_font("IOFont"))
        self.tag_configure("stdin", foreground="Blue")
        self.tag_configure("stdout", foreground="Black")
        self.tag_configure("stderr", foreground="Red")
        self.tag_configure("hyperlink", foreground="#3A66DD", underline=True)
        self.tag_bind("hyperlink", "<ButtonRelease-1>", self._handle_hyperlink)
        self.tag_bind("hyperlink", "<Enter>", self._hyperlink_enter)
        self.tag_bind("hyperlink", "<Leave>", self._hyperlink_leave)

        self.tag_configure("vertically_spaced", spacing1=vert_spacing)
        self.tag_configure("inactive", foreground="#aaaaaa")

        # create 3 marks: input_start shows the place where user entered but not-yet-submitted
        # input starts, output_end shows the end of last output,
        # output_insert shows where next incoming program output should be inserted
        self.mark_set("input_start", "end-1c")
        self.mark_gravity("input_start", tk.LEFT)

        self.mark_set("output_end", "end-1c")
        self.mark_gravity("output_end", tk.LEFT)

        self.mark_set("output_insert", "end-1c")
        self.mark_gravity("output_insert", tk.RIGHT)

        self.active_object_tags = set()

        self._command_handlers = {}

        self._last_configuration = None

        get_workbench().bind("InputRequest", self._handle_input_request, True)
        get_workbench().bind("ProgramOutput", self._handle_program_output, True)
        get_workbench().bind("ToplevelResult", self._handle_toplevel_result, True)
        get_workbench().bind("BackendReady", self._backend_ready, True)

        self._init_menu()

    def _init_menu(self):
        self._menu = tk.Menu(self, tearoff=False)
        self._menu.add_command(label="Clear shell", command=self._clear_shell)

    def add_command(self, command, handler):
        self._command_handlers[command] = handler

    def submit_command(self, cmd_line):
        assert self._get_state() == "waiting_toplevel_command"
        self.delete("input_start", "end")
        self.insert("input_start", cmd_line, ("automagic",))
        self.see("end")

    def _handle_input_request(self, msg):
        self.text_mode = "io"
        self["font"] = get_workbench().get_font("IOFont") # otherwise the cursor is of toplevel size
        self.focus_set()
        self.mark_set("insert", "end")
        self.tag_remove("sel", "1.0", tk.END)
        self._current_input_request = msg
        self._try_submit_input() # try to use leftovers from previous request
        self.see("end")

    def _handle_program_output(self, msg):
        self.text_mode = "io"
        self["font"] = get_workbench().get_font("IOFont")

        # mark first line of io
        if self._before_io:
            self._insert_text_directly(msg.data[0], ("io", msg.stream_name, "vertically_spaced"))
            self._before_io = False
            self._insert_text_directly(msg.data[1:], ("io", msg.stream_name))
        else:
            self._insert_text_directly(msg.data, ("io", msg.stream_name))

        self.mark_set("output_end", self.index("end-1c"))
        self.see("end")

    def _handle_toplevel_result(self, msg):
        self.text_mode = "toplevel"
        self["font"] = get_workbench().get_font("EditorFont")
        self._before_io = True
        if hasattr(msg, "error"):
            self._insert_text_directly(msg.error + "\n", ("toplevel", "error"))

        if hasattr(msg, "value_info"):
            value_repr = shorten_repr(msg.value_info.repr, 10000)
            if value_repr != "None":
                if get_workbench().in_heap_mode():
                    value_repr = memory.format_object_id(msg.value_info.id)
                object_tag = "object_" + str(msg.value_info.id)
                self._insert_text_directly(value_repr + "\n", ("toplevel",
                                                               "value",
                                                               object_tag))
                if running_on_mac_os():
                    sequence = "<Command-Button-1>"
                else:
                    sequence = "<Control-Button-1>"
                self.tag_bind(object_tag, sequence,
                                   lambda _: get_workbench().event_generate(
                                        "ObjectSelect", object_id=msg.value_info.id))

                self.active_object_tags.add(object_tag)

        self.mark_set("output_end", self.index("end-1c"))
        self._insert_prompt()
        self._try_submit_input()
        self.see("end")

    def _insert_prompt(self):
        # if previous output didn't put a newline, then do it now
        if not self.index("output_insert").endswith(".0"):
            # TODO: show a symbol indicating unfinished line
            self._insert_text_directly("\n", ("io",))

        prompt_tags = ("toplevel", "prompt")

        # if previous line has value or io then add little space
        prev_line = self.index("output_insert - 1 lines")
        prev_line_tags = self.tag_names(prev_line)
        if "io" in prev_line_tags or "value" in prev_line_tags:
            prompt_tags += ("vertically_spaced",)
            #self.tag_add("last_result_line", prev_line)

        self._insert_text_directly(">>> ", prompt_tags)
        self.edit_reset();

    def _get_state(self):
        return get_runner().get_state()

    def intercept_insert(self, index, txt, tags=()):
        if (self._editing_allowed()
            and self._in_current_input_range(index)):
            #self._print_marks("before insert")
            # I want all marks to stay in place
            self.mark_gravity("input_start", tk.LEFT)
            self.mark_gravity("output_insert", tk.LEFT)

            if self._get_state() == "waiting_input":
                tags = tags + ("io", "stdin")
            else:
                tags = tags + ("toplevel", "command")

            # when going back and fixing in the middle of command and pressing ENTER there
            if txt == "\n":
                self.mark_set("insert", "insert lineend")
                index = "insert"

            super().intercept_insert(index, txt, tags)

            # tag first char of io separately
            if self._get_state() == "waiting_input" and self._before_io:
                self.tag_add("vertically_spaced", index)
                self._before_io = False

            self._try_submit_input()
        else:
            self.bell()

    def intercept_delete(self, index1, index2=None, **kw):
        if (self._editing_allowed()
            and self._in_current_input_range(index1)
            and (index2 is None or self._in_current_input_range(index2))):
            self.direct_delete(index1, index2, **kw)
        else:
            self.bell()

    def on_secondary_click(self, event):
        self._menu.post(event.x_root, event.y_root)

    def _in_current_input_range(self, index):
        return self.compare(index, ">=", "input_start")

    def _insert_text_directly(self, txt, tags=()):
        # I want the insertion to go before marks
        #self._print_marks("before output")
        self.mark_gravity("input_start", tk.RIGHT)
        self.mark_gravity("output_insert", tk.RIGHT)
        tags = tuple(tags)

        if "stderr" in tags or "error" in tags:
            # show lines pointing to source lines as hyperlinks
            for line in txt.splitlines(True):
                parts = re.split(r'(File .* line \d+.*)$', line, maxsplit=1)
                if len(parts) == 3 and "<pyshell" not in line:
                    self._insert_text_drctly(parts[0], tags)
                    self._insert_text_drctly(parts[1], tags + ("hyperlink",))
                    self._insert_text_drctly(parts[2], tags)
                else:
                    self._insert_text_drctly(line, tags)
        else:
            self._insert_text_drctly(txt, tags)

        #self._print_marks("after output")
        # output_insert mark will move automatically because of its gravity

    def _insert_text_drctly(self, txt, tags):
        if txt != "":
            self.direct_insert("output_insert", txt, tags)

    def _try_submit_input(self):
        # see if there is already enough inputted text to submit
        input_text = self.get("input_start", "end-1c")
        submittable_text = self._extract_submittable_input(input_text)

        if submittable_text is not None:
            # user may have pasted more text than necessary for this request
            # leftover text will be kept in widget, waiting for next request.
            start_index = self.index("input_start")
            end_index = self.index("input_start+{0}c".format(len(submittable_text)))
            # apply correct tags (if it's leftover then it doesn't have them yet)
            if self._get_state() == "waiting_input":
                self.tag_add("io", start_index, end_index)
                self.tag_add("stdin", start_index, end_index)
            else:
                self.tag_add("toplevel", start_index, end_index)
                self.tag_add("command", start_index, end_index)

            # update start mark for next input range
            self.mark_set("input_start", end_index)

            # Move output_insert mark after the requested_text
            # Leftover input, if any, will stay after output_insert,
            # so that any output that will come in before
            # next input request will go before leftover text
            self.mark_set("output_insert", end_index)

            # remove tags from leftover text
            for tag in ("io", "stdin", "toplevel", "command"):
                # don't remove automagic, because otherwise I can't know it's auto
                self.tag_remove(tag, end_index, "end")

            self._submit_input(submittable_text)
            # tidy up the tags
            #self.tag_remove("pending_input", "1.0", "end")
            #self.tag_add("submitted_input", start_index, end_index)
            #self.tag_add("pending_input", end_index, "end-1c")

    def _editing_allowed(self):
        return self._get_state() in ('waiting_toplevel_command', 'waiting_input')

    def _extract_submittable_input(self, input_text):

        if self._get_state() == "waiting_toplevel_command":
            # TODO: support also multiline commands
            if "\n" in input_text:
                return input_text[:input_text.index("\n")+1]
            else:
                return None
        elif self._get_state() == "waiting_input":
            input_request = self._current_input_request
            method = input_request.method
            limit = input_request.limit
            # TODO: what about EOF?
            if isinstance(limit, int) and limit < 0:
                limit = None

            if method == "readline":
                # TODO: is it correct semantics?
                i = 0
                if limit == 0:
                    return ""

                while True:
                    if i >= len(input_text):
                        return None
                    elif limit is not None and i+1 == limit:
                        return input_text[:i+1]
                    elif input_text[i] == "\n":
                        return input_text[:i+1]
                    else:
                        i += 1
            else:
                raise AssertionError("only readline is supported at the moment")

    def _backend_ready(self, event):
        configuration = get_workbench().get_option("run.backend_configuration")

        if (configuration != self._last_configuration
            and not (self._last_configuration is None and not configuration)):
                self._insert_text_directly("Python " + event.python_version, ("version",))

        self._last_configuration = get_workbench().get_option("run.backend_configuration")

    def _submit_input(self, text_to_be_submitted):
        if self._get_state() == "waiting_toplevel_command":
            # register in history and count
            if text_to_be_submitted in self._command_history:
                self._command_history.remove(text_to_be_submitted)
            self._command_history.append(text_to_be_submitted)
            self._command_history_current_index = None # meaning command selection is not in process

            try:
                if text_to_be_submitted.startswith("%"):
                    command, _ = parse_shell_command(text_to_be_submitted)
                    get_workbench().event_generate("MagicCommand", cmd_line=text_to_be_submitted)
                    if command in self._command_handlers:
                        self._command_handlers[command](text_to_be_submitted)
                        get_workbench().event_generate("AfterKnownMagicCommand", cmd_line=text_to_be_submitted)
                    else:
                        self._insert_text_directly("Unknown magic command: " + command)
                        self._insert_prompt()
                else:
                    get_runner().send_command(
                        ToplevelCommand(command="execute_source",
                                        source=text_to_be_submitted))

            except:
                #raise # TODO:
                get_workbench().report_exception()
                self._insert_prompt()

            get_workbench().event_generate("ShellCommand", command_text=text_to_be_submitted)
        else:
            get_runner().send_program_input(text_to_be_submitted)
            get_workbench().event_generate("ShellInput", input_text=text_to_be_submitted)

    def _arrow_up(self, event):
        if not self._in_current_input_range("insert"):
            return

        if len(self._command_history) == 0 or self._command_history_current_index == 0:
            # can't take previous command
            return "break"

        if self._command_history_current_index is None:
            self._command_history_current_index = len(self._command_history)-1
        else:
            self._command_history_current_index -= 1

        self._propose_command(self._command_history[self._command_history_current_index].strip("\n"))
        return "break"

    def _arrow_down(self, event):
        if not self._in_current_input_range("insert"):
            return

        if (len(self._command_history) == 0
            or self._command_history_current_index == len(self._command_history)-1):
            # can't take next command
            return "break"

        if self._command_history_current_index is None:
            self._command_history_current_index = len(self._command_history)-1
        else:
            self._command_history_current_index += 1

        self._propose_command(self._command_history[self._command_history_current_index].strip("\n"))
        return "break"

    def _propose_command(self, cmd_line):
        self.delete("input_start", "end")
        self.intercept_insert("input_start", cmd_line)

    def _text_key_press(self, event):
        # TODO: this underline may confuse, when user is just copying on pasting
        # try to add this underline only when mouse is over the value
        """
        if event.keysym in ("Control_L", "Control_R", "Command"):  # TODO: check in Mac
            self.tag_configure("value", foreground="DarkBlue", underline=1)
        """

    def _text_key_release(self, event):
        if event.keysym in ("Control_L", "Control_R", "Command"):  # TODO: check in Mac
            self.tag_configure("value", foreground="DarkBlue", underline=0)

    def _clear_shell(self):
        end_index = self.index("output_end")
        self.direct_delete("1.0", end_index)

    def compute_smart_home_destination_index(self):
        """Is used by EnhancedText"""

        if self._in_current_input_range("insert"):
            # on input line, go to just after prompt
            return "input_start"
        else:
            return super().compute_smart_home_destination_index()

    def _hyperlink_enter(self, event):
        self.config(cursor="hand2")

    def _hyperlink_leave(self, event):
        self.config(cursor="")

    def _handle_hyperlink(self, event):
        try:
            line = self.get("insert linestart", "insert lineend")
            matches = re.findall(r'File "([^"]+)", line (\d+)', line)
            if len(matches) == 1 and len(matches[0]) == 2:
                filename, lineno = matches[0]
                lineno = int(lineno)
                if os.path.exists(filename) and os.path.isfile(filename):
                    # TODO: better use events instead direct referencing
                    get_workbench().get_editor_notebook().show_file(filename, lineno)
        except:
            traceback.print_exc()

    def _invalidate_current_data(self):
        """
        Grayes out input & output displayed so far
        """
        end_index = self.index("output_end")

        self.tag_add("inactive", "1.0", end_index)
        self.tag_remove("value", "1.0", end_index)

        while len(self.active_object_tags) > 0:
            self.tag_remove(self.active_object_tags.pop(), "1.0", "end")


from fct.fct_etl import *

#Loading the datasets
df_test = create_df(raw_folder, 'application_test.csv.zip')
df_train = create_df(raw_folder, 'application_train.csv.zip')

#Cleaning the data
corr_rank = df_train.corr()['TARGET'].sort_values()

#Exporting the processed datasets
export_df(df_train, processed_folder, 'processed_train.csv')
export_df(df_test, processed_folder, 'processed_test.csv')


import numpy as np
import matplotlib.pyplot as plt
from torch import nn
from torch.optim.lr_scheduler import _LRScheduler

def count_parameters(model):
    return sum([p.numel() for p in model.parameters() if p.requires_grad])

def initialize_weights(m):
    if hasattr(m, 'weight') and m.weight.dim() > 1:
        nn.init.xavier_uniform_(m.weight.data)

def epoch_time(start_t, end_t):
    t = end_t - start_t
    m = int(t / 60)
    s = int(t - m*60)
    return m, s

def plot(history, skip_start=10, skip_end=5, log_lr=True,
         show_lr=None, ax=None, suggest_lr=True):
    lrs = history["lr"][skip_start:-skip_end]
    losses = history["loss"][skip_start:-skip_end]

    fig = None
    if ax is None:
        fig, ax = plt.subplots()

    ax.plot(lrs, losses)

    if suggest_lr:
        print("LR suggestion: steepest gradient")
        min_grad_idx = None
        min_grad_idx = (np.gradient(np.array(losses))).argmin()
        if min_grad_idx is not None:
            print("Suggested LR: {:.2E}".format(lrs[min_grad_idx]))
            ax.scatter(
                lrs[min_grad_idx],
                losses[min_grad_idx],
                s=75,
                marker="o",
                color="red",
                zorder=3,
                label="steepest gradient",
            )
            ax.legend()
    if log_lr:
        ax.set_xscale("log")

    ax.set_xlabel("lr")
    ax.set_ylabel("loss")

    plt.show()

class ExponentialLR(_LRScheduler):
    def __init__(self, optimizer, end_lr, num_iter, last_epoch=-1):
        self.end_lr = end_lr
        self.num_iter = num_iter
        super(ExponentialLR, self).__init__(optimizer, last_epoch)

    def get_lr(self):
        r = self.last_epoch / (self.num_iter - 1)

        return [base_lr * (self.end_lr / base_lr) ** r
                for base_lr in self.base_lrs]


# 1 Basic
for i in range(1, 150, 1):
    print(i)

# 2 Multiples of 5
for i in range(5, 1000, 5):
    print(i)

# 3 Counting, The Dojo Way
for num in range(100):
    if num % 5 == 0:
        print("Coding")
    elif num % 10 == 0:
        print("Coding Dojo")
    else:
        print(num)

# 4 Whoa. That Sucker's Huge
final_sum = 0
for i in range(1, 500000, 2):
    final_sum += i
    print(final_sum)

# 5 Countdown by fours
for count in range(2018, 0, -4):
    print(count)

# 6 Flexible Counter
Low_Num, Mult, High_Num = 2, 3, 9
for Mult in range (3, 12, 3):
    print(Mult)


from canvas_sdk import client, utils

def list_assignment_groups(request_ctx, course_id, include, override_assignment_dates=None, per_page=None, **request_kwargs):
    """
    Returns the list of assignment groups for the current context. The returned
    groups are sorted by their position field.

        :param request_ctx: The request context
        :type request_ctx: :class:RequestContext
        :param course_id: (required) ID
        :type course_id: string
        :param include: (required) Associations to include with the group. both "discussion_topic" and "all_dates" is only valid are only valid if "assignments" is also included.
        :type include: string
        :param override_assignment_dates: (optional) Apply assignment overrides for each assignment, defaults to true.
        :type override_assignment_dates: boolean or None
        :param per_page: (optional) Set how many results canvas should return, defaults to config.LIMIT_PER_PAGE
        :type per_page: integer or None
        :return: List assignment groups
        :rtype: requests.Response (with array data)

    """

    if per_page is None:
        per_page = request_ctx.per_page
    include_types = ('assignments', 'discussion_topic', 'all_dates')
    utils.validate_attr_is_acceptable(include, include_types)
    path = '/v1/courses/{course_id}/assignment_groups'
    payload = {
        'include' : include,
        'override_assignment_dates' : override_assignment_dates,
        'per_page' : per_page,
    }
    url = request_ctx.base_api_url + path.format(course_id=course_id)
    response = client.get(request_ctx, url, payload=payload, **request_kwargs)

    return response

def get_assignment_group(request_ctx, course_id, assignment_group_id, include, override_assignment_dates=None, **request_kwargs):
    """
    Returns the assignment group with the given id.

        :param request_ctx: The request context
        :type request_ctx: :class:RequestContext
        :param course_id: (required) ID
        :type course_id: string
        :param assignment_group_id: (required) ID
        :type assignment_group_id: string
        :param include: (required) Associations to include with the group. "discussion_topic" is only valid if "assignments" is also included.
        :type include: string
        :param override_assignment_dates: (optional) Apply assignment overrides for each assignment, defaults to true.
        :type override_assignment_dates: boolean or None
        :return: Get an Assignment Group
        :rtype: requests.Response (with AssignmentGroup data)

    """

    include_types = ('assignments', 'discussion_topic')
    utils.validate_attr_is_acceptable(include, include_types)
    path = '/v1/courses/{course_id}/assignment_groups/{assignment_group_id}'
    payload = {
        'include' : include,
        'override_assignment_dates' : override_assignment_dates,
    }
    url = request_ctx.base_api_url + path.format(course_id=course_id, assignment_group_id=assignment_group_id)
    response = client.get(request_ctx, url, payload=payload, **request_kwargs)

    return response

def create_assignment_group(request_ctx, course_id, name=None, position=None, group_weight=None, rules=None, **request_kwargs):
    """
    Create a new assignment group for this course.

        :param request_ctx: The request context
        :type request_ctx: :class:RequestContext
        :param course_id: (required) ID
        :type course_id: string
        :param name: (optional) The assignment group's name
        :type name: string or None
        :param position: (optional) The position of this assignment group in relation to the other assignment groups
        :type position: integer or None
        :param group_weight: (optional) The percent of the total grade that this assignment group represents
        :type group_weight: float or None
        :param rules: (optional) The grading rules that are applied within this assignment group See the Assignment Group object definition for format
        :type rules: string or None
        :return: Create an Assignment Group
        :rtype: requests.Response (with AssignmentGroup data)

    """

    path = '/v1/courses/{course_id}/assignment_groups'
    payload = {
        'name' : name,
        'position' : position,
        'group_weight' : group_weight,
        'rules' : rules,
    }
    url = request_ctx.base_api_url + path.format(course_id=course_id)
    response = client.post(request_ctx, url, payload=payload, **request_kwargs)

    return response

def edit_assignment_group(request_ctx, course_id, assignment_group_id, **request_kwargs):
    """
    Modify an existing Assignment Group.
    Accepts the same parameters as Assignment Group creation

        :param request_ctx: The request context
        :type request_ctx: :class:RequestContext
        :param course_id: (required) ID
        :type course_id: string
        :param assignment_group_id: (required) ID
        :type assignment_group_id: string
        :return: Edit an Assignment Group
        :rtype: requests.Response (with AssignmentGroup data)

    """

    path = '/v1/courses/{course_id}/assignment_groups/{assignment_group_id}'
    url = request_ctx.base_api_url + path.format(course_id=course_id, assignment_group_id=assignment_group_id)
    response = client.put(request_ctx, url, **request_kwargs)

    return response

def destroy_assignment_group(request_ctx, course_id, assignment_group_id, move_assignment_to, **request_kwargs):
    """
    Deletes the assignment group with the given id.

        :param request_ctx: The request context
        :type request_ctx: :class:RequestContext
        :param course_id: (required) ID
        :type course_id: string
        :param assignment_group_id: (required) ID
        :type assignment_group_id: string
        :param move_assignment_to: (required) The ID of an active Assignment Group to which the assignments that are currently assigned to the destroyed Assignment Group will be assigned. NOTE: If this argument is not provided, any assignments in this Assignment Group will be deleted.
        :type move_assignment_to: string
        :return: Destroy an Assignment Group
        :rtype: requests.Response (with AssignmentGroup data)

    """

    path = '/v1/courses/{course_id}/assignment_groups/{assignment_group_id}'
    payload = {
        'move_assignment_to' : move_assignment_to,
    }
    url = request_ctx.base_api_url + path.format(course_id=course_id, assignment_group_id=assignment_group_id)
    response = client.delete(request_ctx, url, payload=payload, **request_kwargs)

    return response


import csv, json

school_map = {}

with open('zone2.csv', 'r') as csvfile:
	mapreader = csv.reader(csvfile, delimiter=',', quotechar='|')
	zone = []
	for row in mapreader:
		zone.append([x for x in row])
	school_map["zone2"] = zone

with open('zone3.csv', 'r') as csvfile:
	mapreader = csv.reader(csvfile, delimiter=',', quotechar='|')
	zone = []
	for row in mapreader:
		zone.append([x for x in row])
	school_map["zone3"] = zone

json_map = {}

for zone in school_map:
	json_map[zone] = []
	for row in school_map[zone]:
		new_row = []
		for cell in row:
			if cell == "0":
				item = {"type": "empty"}
			elif cell == "2":
				item = {"type": "table"}
			else:
				item = {"type": "computer", "host": cell}
			new_row.append(item)
		json_map[zone].append(new_row)

print(json.dumps(json_map))


import codecs
import os , sys
from SingleByteXOR import best_decryption

### Read the file line by line
with open(os.path.join(sys.path[0], "4.txt"), "r") as f:
    lines = f.readlines()
    n = len(lines)
### For each line, look at the most likely decryption and their Englishness scores
for i in range(n):
    ### Ignore the last character of each line, which is just '\n'
    secret_hex = lines[i].strip()
    secret_bytes = codecs.decode(secret_hex , 'hex')
    lines[i] = best_decryption(secret_bytes)
### Sort the strings by their Englishness scores and choose the decrypted message with the best(lowest) score
lines.sort()
decrypted_text = codecs.decode(lines[0][2] , 'latin-1')

"""
print(decrypted_text)
"""


from tkinter import*
from tkinter import ttk
import pygame
from pygame import mixer
import sys
from tinytag import tinytag, TinyTag

root=Tk()
root.title("reproductor")
root.geometry('300x100')
root.iconbitmap(r'C:\Users\xgian\Desktop\programa\botones\favicon.ico')
mixer.init()

#funciones
def playboton():
    mixer.music.load(fr"C:\Users\xgian\Desktop\programa\canciones\Laura Pergolizzi - Lost on You\Lost On You.mp3")
    mixer.music.play()
paused=FALSE
def pausaboton():
    mixer.music.pause()

def atrasboton():
    print("hola")
def siguienteboton():
    print("holaa")

#botones
photoatras=PhotoImage(file='atras.png')
atras=Button(root, image=photoatras, command=atrasboton)
atras.pack(side=LEFT)

photoplay=PhotoImage(file='play.png')
play=Button(root, image=photoplay, command=playboton)
play.pack(side=LEFT)

photopausa=PhotoImage(file='pausa.png')
pausa=Button(root, image=photopausa, command=pausaboton)
pausa.pack(side=LEFT)

photosiguiente=PhotoImage(file='siguiente.png')
siguiente=Button(root, image=photosiguiente, command=siguienteboton)
siguiente.pack(side=LEFT)

photoshuffle=PhotoImage(file='shuffle.png')
shuffle=Button(root, image=photoshuffle, command=siguienteboton)
shuffle.pack(side=LEFT)

photosonido=PhotoImage(file='sonido.png')
sonido=Button(root, image=photosonido, command=siguienteboton)
sonido.pack(side=LEFT)

buscador=Entry(root,width=30)
buscador.pack(side=LEFT)
buscador.pack(side=BOTTOM)

root.mainloop()


def Cp(compound_string, T, mass=1.0):
    """
    Calculate the heat capacity of the compound for the specified temperature
    and mass.

    :param compound_string: Formula and phase of chemical compound, e.g.
      'Fe2O3[S1]'.
    :param T: [°C] temperature
    :param mass: [kg]

    :returns: [kWh/K] Heat capacity.
    """

    formula, phase = _split_compound_string_(compound_string)
    TK = T + 273.15
    compound = compounds[formula]
    result = compound.Cp(phase, TK)

    return _finalise_result_(compound, result, mass)


import json
import datetime
from typing import Optional, List, Set, Dict

from spotify_client.spotify.playlist_pull import get_playlist_total_seconds
from spotify_client.spotify.utils import authenticated_request
from spotify_client.settings import spotify_settings, general_settings
from spotify_client.models import PlaylistStats, DatedPlaylistStats, SongPull

def get_playlist_stats(current_songs: Optional[List[SongPull]] = None) -> PlaylistStats:
    """Fetch stats from the playlist, as a PlaylistStats object.
    List of SongPull can be given; if not, the playlist is pulled."""
    playlist_id = spotify_settings.get_required("playlist_id")
    url = f"https://api.spotify.com/v1/playlists/{playlist_id}"

    r = authenticated_request("GET", url)
    js = r.json()

    followers = js["followers"]["total"]
    songs = js["tracks"]["total"]
    total_seconds = get_playlist_total_seconds(current_songs)

    return PlaylistStats(followers=followers, songs=songs, total_seconds=total_seconds)

def append_stats_to_file(stats: PlaylistStats):
    """Append the given PlaylistStats object to the stats file configured.
    The stats file is an ndjson file, where each line corresponds to the JSON representation
    of a DatedPlaylistStats object, with the stats for a single day."""
    stats_file = general_settings.get_required("playlist_stats_file")
    dated_stats = DatedPlaylistStats(**stats.dict())

    with open(stats_file, "a") as f:  # open file in append mode
        f.write("\n")
        f.write(dated_stats.json())

def clear_repeated_stats_from_file():
    """Load the playlist stats file lines, clear repeated lines and save it again.
    Lines are considered duplicates if their "date" key is the same. The last line found is kept."""
    stats_file = general_settings.get_required("playlist_stats_file")

    with open(stats_file, "r") as f:
        lines = f.readlines()

    dates_strs: Set[str] = set()
    for line in lines:
        line_stats = DatedPlaylistStats.parse_raw(line)
        dates_strs.add(line_stats.date)

    dates_lines: Dict[str, List[int]] = dict()  # example: { "2022-10-01": [14, 15] } (lines where each date is found)
    for i, line in enumerate(lines):
        for date_str in dates_strs:
            if date_str in line:
                if date_str in dates_lines:
                    dates_lines[date_str].append(i)
                else:
                    dates_lines[date_str] = [i]
                break

    lines_delete: List[int] = list()
    for lines_delete_iter in dates_lines.values():
        if len(lines_delete_iter) < 2:
            continue
        lines_delete_iter.sort()
        lines_delete.extend(lines_delete_iter[:-1])  # add all elements from `lines_delete_iter` except the last one

    lines_delete.sort(reverse=True)
    for line_delete in lines_delete:
        lines.pop(line_delete)

    with open(stats_file, "w") as f:
        data = "\n".join([line.strip() for line in lines])
        f.write(data)

def load_stats() -> Dict[str, DatedPlaylistStats]:
    """Load the persisted stats from the stats ndjson file, and returns them as dict of {date(str), DatedPlaylistStats} objects,
    returned in the same order as read from the file (considering Python >= 3.6 keeps insertion order on dicts).
    For repeated dates, the last loaded line is used."""
    stats_file = general_settings.get_required("playlist_stats_file")
    parsed_dates_stats: Dict[str, DatedPlaylistStats] = dict()

    with open(stats_file, "r") as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            try:
                line_js = json.loads(line)
                line_stats = DatedPlaylistStats(**line_js)
                parsed_dates_stats[line_stats.date] = line_stats
            except:
                # TODO proper except and log
                continue

    return parsed_dates_stats

def filter_stats_monthly(dates_stats: Dict[str, DatedPlaylistStats]):
    """Filter the given `dates_stats` dict, leaving only the last data point for each month.
    The modifications are performed in-place on the dict."""
    months_dates: Dict[str, List[datetime.date]] = dict()  # example: { "2022-01": ["2022-01-10", "2022-01-17"] }
    for date_str in dates_stats.keys():
        date = datetime.date.fromisoformat(date_str)
        month_str = date.strftime("%Y-%m")

        if month_str in months_dates:
            months_dates[month_str].append(date)
        else:
            months_dates[month_str] = [date]

    delete_dates_strs: Set[str] = set()
    for month_str, dates in months_dates.items():
        dates.sort()
        delete_dates = dates[:-1]  # get all elements from `dates` except the last one
        delete_dates_strs.update(d.isoformat() for d in delete_dates)

    for delete_date_str in delete_dates_strs:
        dates_stats.pop(delete_date_str)

def export_chart():
    """Load the persisted stats from the stats file, and generate a chart using plotly, exporting it as a picture.
    More info about plotly export and supported output files: https://plotly.com/python/static-image-export/
    """
    import plotly.graph_objects as go
    import pandas as pd

    dates_stats = load_stats()
    filter_stats_monthly(dates_stats)
    chart_file = general_settings.get_required("playlist_chart_file")

    dataframe_followers = dict(date=[], value=[])
    dataframe_songs = dict(date=[], value=[])
    dataframe_duration = dict(date=[], value=[])

    for date, stats in dates_stats.items():
        dataframe_followers["date"].append(date)
        dataframe_songs["date"].append(date)
        dataframe_followers["value"].append(stats.followers)
        dataframe_songs["value"].append(stats.songs)
        dataframe_duration["date"].append(date)
        dataframe_duration["value"].append(float(stats.total_seconds / 60 / 60))  # duration in hours

    dataframe_followers = pd.DataFrame(dataframe_followers)
    dataframe_songs = pd.DataFrame(dataframe_songs)

    fig = go.Figure()
    fig.add_trace(go.Scatter(
        x=dataframe_followers["date"],
        y=dataframe_followers["value"],
        mode="lines+markers+text",
        name="Followers",
        line=dict(color="blue"),
        text=[str(v) for v in dataframe_followers["value"]],
        textposition="top center",
    ))
    fig.add_trace(go.Scatter(
        x=dataframe_songs["date"],
        y=dataframe_songs["value"],
        mode="lines+markers+text",
        name="Songs",
        line=dict(color="orange"),
        text=[str(v) for v in dataframe_songs["value"]],
        textposition="top center",
    ))
    fig.add_trace(go.Scatter(
        x=dataframe_duration["date"],
        y=dataframe_duration["value"],
        mode="lines+markers+text",
        name="Length (hours)",
        line=dict(color="green"),
        text=[str(round(v, 1)) for v in dataframe_duration["value"]],
        textposition="top center",
    ))

    fig.write_image(chart_file)


#!/usr/bin/env python
# coding: utf-8
# 选项的冲突处理conflict_handler

import argparse
"""
默认的conflict_handler,当某个选项出现两次就会抛出ArgumentError异常
"""
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--foo', help='old foo help')
try:
    parser.add_argument('--foo', help='new foo help')
except argparse.ArgumentError as e:
    print("{}: argument --foo: conflicting option string(s): --foo".format(e.__class__.__name__))

"""
有时(例如使用parents从一个parser中继承选项参数)需要重写旧的使用相同选项字符串的参数,这时就可以使用'resolve'来解决选项冲突的问题
usage: demo4.py [-h] [-f FOO] [--foo FOO]

optional arguments:
  -h, --help  show this help message and exit
  -f FOO      old foo help
  --foo FOO   new foo help
"""
parser = argparse.ArgumentParser(conflict_handler='resolve')
parser.add_argument('-f', '--foo', help='old foo help')
parser.add_argument('--foo', help='new foo help')
parser.print_help()


from django.urls import path
from django.contrib.auth import views as auth_views
from ..crm.views import LeadListView, LeadDetailView, LeadCreateView, LeadUpdateView, DeleteLead

urlpatterns = [
    path('lead', LeadListView.as_view(), name='lead'),
    path('lead/add/', LeadCreateView.as_view(), name='lead-add'),
    path('lead/<int:pk>/', LeadDetailView.as_view(), name='lead-detail'),
    path('lead/<int:pk>/update', LeadUpdateView.as_view(), name='lead-update'),
    path('lead/<int:pk>/delete/', DeleteLead, name='lead-delete'),
]


from PySide2.QtWidgets import QApplication,QWidget,QPlainTextEdit,QProgressBar,QMainWindow
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import Signal,QObject
from threading import Thread
import tkinter as tk
from tkinter import filedialog
import utils
from mainUI import Ui_mainUI

class DownloadUpdateSignals(QObject):
    dSignal = Signal(QPlainTextEdit, QProgressBar, str, int)

class interactiveWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.pdfDir = ''
        self.pdfPaper = None
        self.ui = Ui_mainUI()
        self.ui.setupUi(self)
        self.ui.pdfSelectButton.clicked.connect(self.ChoosePdf)
        self.ui.startSearchButton.clicked.connect(self.DownloadRefsBib)
        self.ui.progressBar.setValue(0)
        self.dSignal = DownloadUpdateSignals()
        self.dSignal.dSignal.connect(self.ReceiveDownloadSignal)

    def ChoosePdf(self):
        root = tk.Tk()
        root.withdraw()
        self.pdfDir = filedialog.askopenfilename()
        if not self.pdfDir[-4::1] == '.pdf':
            self.pdfDir = ''
            self.ui.pdfDirShower.setPlainText("Please Select PDF file.")
        else:
            self.ui.pdfDirShower.setPlainText(self.pdfDir)
            self.pdfPaper = utils.pdfPaper(self.pdfDir)
            self.ui.paperInfoShower.setPlainText(self.pdfPaper.title)
            self.ui.cmdOutput.setPlainText(self.pdfPaper.ShowRefs())

    def DownloadRefsBib(self):
        if self.pdfDir == '' or self.pdfPaper == None:
            return
        #Maybe the pdf is normal state now.
        thread = Thread(target=self.DownloadRefsBibThreadFunc)
        thread.start()
        ### Non-multithreading
        #tempCMDOutput = 'Start downloading bib files of refs......\n'
        #self.ui.cmdOutput.setPlainText(tempCMDOutput)
        #for i in range(len(self.pdfPaper.refs)):
        #    tempCMDOutput += utils.SearchForPaper(self.pdfPaper.refs[i])
        #    self.ui.cmdOutput.setPlainText(tempCMDOutput)
        #    self.ui.progressBar.setValue(100 * (i+1) / len(self.pdfPaper.refs))
        ###

    def DownloadRefsBibThreadFunc(self):
        #Thread for updating download info
        tempCMDOutput = 'Start downloading bib files of refs......\n'
        tempLen = len(self.pdfPaper.refs)
        successNum = 0
        self.dSignal.dSignal.emit(self.ui.cmdOutput, self.ui.progressBar, tempCMDOutput, 0)
        for i in range(tempLen):
            tempCMDOutput += utils.SearchForPaper(self.pdfPaper.refs[i])
            if tempCMDOutput[-5:-1:1] == '.bib':
                successNum += 1
            self.dSignal.dSignal.emit(self.ui.cmdOutput, self.ui.progressBar, tempCMDOutput, 100 * (i+1) / tempLen)
        tempCMDOutput += '----------------------------------------\nDownload Finished:\n'
        tempCMDOutput += 'Total: '+str(tempLen)+'    Success:'+str(successNum)+'    Fail:'+str(tempLen-successNum)+'    SDRatio:'+str(int(100*successNum/tempLen))+"%\n"
        self.dSignal.dSignal.emit(self.ui.cmdOutput, self.ui.progressBar, tempCMDOutput, 100)

    def ReceiveDownloadSignal(self, pt, pb, textStr, progInt):
        pt.setPlainText(textStr)
        pb.setValue(progInt)

def InitWindow():
    app = QApplication([])
    iWin = interactiveWindow();
    iWin.show()
    app.exec_()
    return iWin


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 17 11:53:14 2019

@author: zqy5086@AD.PSU.EDU
"""

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May  5 17:59:38 2019

@author: zqy5086@AD.PSU.EDU
"""

import multiprocessing as mp
import numpy as np
import datetime
from six.moves import cPickle
import time
from sklearn.neighbors import KDTree
import matplotlib.pyplot as plt
#from gp import gp,gp_prep
from state_space import ball, gridsearch_construct, to_string,state

from car_dynamics import car_true,car,car_disturbance
#from safe_plot import progression_plot
#from safe_plot import progression
from state_space import State_Space,grid,d
from car_dynamics import car_execution_disturbance
from safety_control import safety_iteration_GP, SI, SI_GP
import pickle

from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
def policy_search(center,X_safe,X_safe_tree,Xfree, control_history):
        ind1 = X_safe_tree.query(center.reshape(1,-1),1,dualtree=False, breadth_first=False)
        if center[2]<0:
            ind2 = X_safe_tree.query((center+np.array([0,0,2])).reshape(1,-1),1)
        else:
            ind2 = X_safe_tree.query((center-np.array([0,0,2])).reshape(1,-1),1)
        if ind1[0][0][0]<ind2[0][0][0]:
            ind=ind1
        else:
            ind=ind2
        coordinate = X_safe[ind[1][0][0]]
        key = to_string(coordinate)
        z = Xfree.table[key]
        if z.u is None:
            print(z.state)
        else:

            index=min(z.U,key=z.U.get)
            indices=[k for k in z.U if z.U[k]==z.U[index]]
            if len(indices)>1:
                new_ind=np.argmin([control_history[k] for k in indices])
                u=control_history[indices[new_ind]]
                control_history[new_ind]+=1
            else:
                u=z.U[index]
                z.U[index]+=1
                control_history[index]+=1

        return u,z,control_history

def progression(car_model,center,X_safe,X_safe_tree,Xfree,hp,ball_obs,Up):
    print('hi')
    t_end = 730.
    t_start = 0.
    t_step=0.01
    ZZ=[]
    ys=[]
    z1=None
    z2=None
    control_history=dict()
    for u in Up:
        control_history[u]=0
    while t_start<t_end:
        x = state(center)
        if not d(ball_obs,x):
            return 0,z1,z2,control_history#,U
        ys.append(center)

        u,zz,control_history=policy_search(center,X_safe,X_safe_tree,Xfree,control_history)
        ZZ.append((center,zz.state))
        z1,z2,_=np.vstack(ys).T
#        U.append(u)
        center=center+t_step*car_model(center,u)
        if center[0]<-0.01 or center[0]>1.01 or center[1]<-0.01 or center[1]>1.01:
            return 1, z1,z2,control_history

        t_start=t_start+t_step
    z11,z22,z=np.vstack(ys).T
    print(control_history)
    return 1, z11,z22,control_history #,U #,ZZ

def testing(i,X,Y):
    ZZ=[]
    for j in range(size):
        center = np.array([X[i][j],Y[i][j], angle])
        if len(X_safe)==0:
            ZZ.append(-1)
        else:
            z,z1,z2 = progression(car_model,center,X_safe,X_safe_tree,Xfree,hp,ball_obs,Up)
            ZZ.append(z)
    return (i,ZZ)
def collect_result(result):
        global results
        results.append(result)

def parent_update(s,need_update):
    for u in s.parents:
        for x in s.parents[u]:
            if not x.unsafe:
                index=np.where(x.u==u)
                if len(index[0])>0:
                    x.u=np.delete(x.u,index[0][0])
                if len(x.u)==0:
                    x.unsafe=True
#                    X_unsafe.append(x)
                    need_update.append(x)

if __name__=="__main__":
    save=True
    num=4
    size=50
    results=[]
    print(datetime.datetime.now())
    print('save is', save)
    P=5
    M = 0.005
    l = 0.005
    Time=0
    Up = np.linspace(-0.3,0.3,5)

    ratio=2.5

    obs_1 = State_Space(3,[0.47,0.52,0.25,0.75,-1,1])
    Xfree = grid([[0,1],[0,1],[-1,1]])
    Xfree.construct(P,Up)
    plot_progress=False
    plot_control=True

    left = obs_1.parameter[0]
    down = obs_1.parameter[2]
    width = obs_1.parameter[1]-obs_1.parameter[0]
    height = obs_1.parameter[3]-obs_1.parameter[2]

    X_unsafe=[]
    model=car
    car_model=car_disturbance
    theta = np.linspace(-1,1,2**(P-1)+1)
    angle = theta[-1]
    initial=np.array([1,0.8, angle])

    T_end=10
#    kernel = C(0.1, (1e-3, 1)) * RBF(0.001, (1e-5, 1e-2))
    kernel = C(1e-4, (1e-6, 1e-3)) * RBF(0.05, (1e-2, 1))
    Y, X = np.meshgrid(np.linspace(0, 1, size), np.linspace(0, 1, size))
    _,Z = np.meshgrid(np.linspace(0, 1, size), np.linspace(0, 1, size))
    ball_obs = ball(obs_1,0)
    leaf_size=200

    control_history=dict()
    for u in Up:
        control_history[u]=0

    print('computing backup controller')
    ######################################################################################################
    #######################################backup controller phase ########################################
    start = time.time()
    if save:
        for p in range(1,P+1):
            print(p)
            hp = 2**(-p)
            eps_p = (hp/(ratio*M*l))**(1/2)
            alpha_p=hp+l*eps_p*hp/2+M*l*eps_p**2
            ball_obs_1 = ball(obs_1,M*eps_p+hp)

            #table construction based on dynamics
            need_update=[]
            X_temp,state_temp,X_unsafe = gridsearch_construct(Xfree,p,ball_obs_1)
            X_temp_tree=KDTree(X_temp, leaf_size=leaf_size, metric='chebyshev')
#            cnt=0

            for s in state_temp:
#                print(cnt)
#                cnt+=1
                if not s.unsafe:
                    SI(model,s,Xfree,Up,eps_p,X_temp_tree,X_temp,alpha_p)
                    if s.unsafe:
#                        X_unsafe.append(s)
                        need_update.append(s)

            for x in need_update:
                parent_update(x,need_update)

        X_safe = [x.state for x in state_temp if not x.unsafe]
#        X_safe_control=[x.u for x in state_temp if not x.unsafe]

        if len(X_safe)>0:
            X_safe_tree=KDTree(X_safe, leaf_size=leaf_size, metric='manhattan')
#        with open("safe_tree_backup.save", "wb") as f:
#            cPickle.dump(X_safe_tree, f)
#        with open("X_safe_backup.save", "wb") as f:len(X_safe_)/(2**p+1)**3>0.2
#            cPickle.dump(X_safe, f)
#        with open("X_safe_control_backup.save","wb") as f:
#            cPickle.dump(X_safe_control,f)
#    else:
#        print('loading...')
#        with open("safe_tree_backup.save", "rb") as fp:   # Unpickling
#            X_safe_tree = cPickle.load(fp)
#        with open("X_safe_backup.save", "rb") as fp:   # Unpickling
#            X_safe = cPickle.load(fp)
#        with open("X_safe_control_backup.save","rb") as fp:
#            X_safe_control = cPickle.load(fp)
#        for x,u in zip(X_safe,X_safe_control):
#            key=to_string(x)
#            Xfree.table[key].u=u

    if plot_control:
            print('plotting control grid...')
            end = time.time()
            t = end-start
            Time = Time+t
            plt.figure()
            T=[]
            for angle in theta:
                X_temp_,state_temp,_ = gridsearch_construct(Xfree,P,fix=[2,angle])
                if save:
                    T.append([float(not x.unsafe) for x in state_temp])
                else:
                    for x in state_temp:
                        if x.u is None:
                            x.unsafe=True
                        else:
                            x.unsafe=False
                    T = np.array([float(not x.unsafe) for x in state_temp])
            T=np.stack(T)
            T=np.sum(T,axis=0)/len(theta)
            T[T==0]=-1
            X1 = [float(x.state[0]) for x in state_temp]
            X2 = [float(x.state[1]) for x in state_temp]
            center=initial
            z,z1,z2,control_record = progression(car_model,center,X_safe,X_safe_tree,Xfree,hp,ball_obs,Up)
            X_backup=X_safe
            X_backup_tree=X_safe_tree
            Xbackup=Xfree
            plt.scatter(X1,X2, c=T, vmin=0, vmax=1)
            plt.plot(z1,z2,color='red')
            plt.colorbar()
            rectangle=plt.Rectangle((left,down),width,height,fc='red')
            plt.gca().add_patch(rectangle)
            plt.xlabel('Time elapsed: '+str(round(Time*1000)/1000)+', '+ str(len(X_temp))+' points',fontsize=16)# subgrid construction time: '+str(round((end_-start)*1000)/1000),fontsize=16)
            plt.title(' Safe points: '+str(len(X_safe)),fontsize=16)

    t_end=T_end
    t_step=0.1
    t_start=0
    #initial
    car_x = initial

    record=[]
    train=[]
    train_f=[]
    record.append(car_x)

    success, record, train_new,train_f_new,car_x,control_history=car_execution_disturbance(car_x,t_end,t_step,X_safe_tree,X_safe,ball_obs,record,Xfree,control_history)
    car_trajectory=np.stack(record)

    if plot_progress:
        results=[]
        print('computing backup progression map')
        plt.figure()
        pool = mp.Pool(mp.cpu_count())
        for i in range(size):
    #        print(i)
        #    testing(i,X,Y)
            pool.apply_async(testing, args=(i,X,Y),callback=collect_result)
        pool.close()
        pool.join()
        results.sort(key=lambda x: x[0])
        for i in range(size):
            for j in range(size):
                Z[i][j]=results[i][1][j]
        plt.pcolormesh(X,Y,Z,vmin=-1, vmax=1)
#        plt.title('Initial Value: ['+str(initial[0])+','+str(initial[1])+','+str(initial[2])+']')

#        plt.plot(z1,z2)
        plt.colorbar()
        rectangle=plt.Rectangle((left,down),width,height,fc='red')
        plt.gca().add_patch(rectangle)
        plt.plot(car_trajectory[:,0],car_trajectory[:,1],color='blue')
#####################################################################################################################################3
############################################Learning dynamic phase####################################################################

    train=np.stack(train_new)
    train_f=np.stack(train_f_new)
#    mu_prep,cov_prep=gp_prep(kernel,train, train_f,sigma,car_true)
    gpr = GaussianProcessRegressor(kernel=kernel, normalize_y=True).fit(train, train_f)
    M_=np.max(train_f)
    M=max([M,M_])

    Xfree_ = grid([[0,1],[0,1],[-1,1]])
    Xfree_.construct(P,Up)
    switch=[]
    k_=0
    safe_index=0;
    safe_index_old=0;
    tol=0.1
    p_=1
   ##################################################################################################################################
    #################################################iterations of progression plots #################################################
    for k in range(num):
#        print(k)
#        filename='X_safe_GP_'+str(k)+'.save'
#        filename2='safe_tree_GP_'+str(k)+'.save'
#        filename3='X_safe_control_GP_'+str(k)+'.save'
        #safety controller
        print('computing safety controller...')
        ############################################################################################################
        ##################################################GP controller phase########################################
        start = time.time()
        Time=0
        t=0

        if save:
            for p in range(p_,P+1):
                print(k,p)
                hp = 2**(-p)
                eps_p = (hp/(ratio*M*l))**(1/2)
                ball_obs_1 = ball(obs_1,M*eps_p+hp)
                alpha_p=2*hp+l*eps_p*hp+M*l*eps_p**2
                #table construction based on dynamics
                need_update=[]
                X_temp,state_temp,X_unsafe = gridsearch_construct(Xfree_,p,ball_obs_1)
                X_temp_tree=KDTree(X_temp,leaf_size=leaf_size,metric='chebyshev')
                for s in state_temp:
                    if not s.unsafe:
                            SI_GP(s,Xfree_,Up,eps_p,X_temp_tree,X_temp,gpr,hp,M,l)
                            if s.unsafe:
#                                X_unsafe.append(s)
                                need_update.append(s)
                for x in need_update:
                    parent_update(x,need_update)

                X_safe_ = [x.state for x in state_temp if not x.unsafe]
                sp=len(X_safe_)/len(X_temp)

                if sp>safe_index:
                    print('switching')
                    safe_index=sp
                            ######################################################################################################################
        #################################################33#progression plot###################################################3
                    end = time.time()
                    t = end-start-t
                    if plot_control:
                        print('plotting controller plot...')
                        Time = Time+t
                        plt.figure()
                        theta = np.linspace(-1,1,2**(p-1)+1)
                        T=[]
                        for angle in theta:
                            X_temp_,state_temp,_ = gridsearch_construct(Xfree_,p,fix=[2,angle])
                            if save:
                                T.append([float(not x.unsafe) for x in state_temp])
                            else:
                                for x in state_temp:
                                    if x.u is None:
                                        x.unsafe=True
                                    else:
                                        x.unsafe=False
                                T = np.array([float(not x.unsafe) for x in state_temp])
                        T=np.stack(T)
                        T=np.sum(T,axis=0)/len(theta)
                        T[T==0]=-1
                        X1 = [float(x.state[0]) for x in state_temp]
                        X2 = [float(x.state[1]) for x in state_temp]
                        plt.scatter(X1,X2, c=T, cmap='viridis', vmin=0, vmax=1)
                        plt.plot(car_trajectory[:,0],car_trajectory[:,1],color='blue')
                        plt.colorbar()
                        rectangle=plt.Rectangle((left,down),width,height,fc='red')
                        plt.gca().add_patch(rectangle)
                        plt.xlabel('Time elapsed: '+str(round(Time*1000)/1000)+', '+ str(len(X_temp))+' points',fontsize=16)# subgrid construction time: '+str(round((end_-start)*1000)/1000),fontsize=16)
                        plt.title(' Safe points: '+str(len(X_safe_)),fontsize=16)

                    ball_obs = ball(obs_1,0)

                    print('in safe controller car execution...')
                    t_end=t

                    success, record, _,_,car_x,control_history=car_execution_disturbance(car_x,t_end,t_step,X_safe_tree,X_safe,ball_obs,record,Xfree,control_history)

                    if not success:
                        print('collision!!!!')
                    X_safe=X_safe_
                    X_safe_tree=KDTree(X_safe, leaf_size=leaf_size,metric='manhattan')
                    car_trajectory_=np.stack(record)
                    Xfree=Xfree_
                    switch.append(car_trajectory_[-1,:2])

                    print('car execution...')

                    t_end=T_end
                    success, record, train_new,train_f_new,car_x,control_history=car_execution_disturbance(car_x,t_end,t_step,X_safe_tree,X_safe,ball_obs,record,Xfree,control_history)
                    print('model updating...')
                    train=np.concatenate((train,train_new))
                    train_f=np.concatenate((train_f,train_f_new))
                    car_trajectory=np.stack(record)

                    if plot_progress:
                        print('plotting progression plot...')
                        results=[]
                        print('progression map...')
                        plt.figure()
                        pool = mp.Pool(mp.cpu_count())
                        for i in range(size):
                #            print(i)        ######################################################################################################################
        #################################################33#progression plot###################################################3
                            pool.apply_async(testing, args=(i,X,Y),callback=collect_result)
                        pool.close()
                        pool.join()
                        results.sort(key=lambda x: x[0])
                        for i in range(size):
                            for j in range(size):
                                Z[i][j]=results[i][1][j]
                        plt.pcolormesh(X,Y,Z,vmin=-1, vmax=1)
#                        plt.title('Initial Value: ['+str(initial[0])+','+str(initial[1])+','+str(initial[2])+']')
                    #    plt.plot(z1,z2)
                        plt.colorbar()
                        rectangle=plt.Rectangle((left,down),width,height,fc='red')
                        plt.gca().add_patch(rectangle)
                        plt.plot(car_trajectory[:,0],car_trajectory[:,1],color='blue')

                        plt.plot(car_trajectory_[:,0],car_trajectory_[:,1],color='black')
                        switch_points=np.stack(switch)
                        plt.scatter(switch_points[:,0],switch_points[:,1], color='black')

                        ########################################################################################
                        ################################ learning ###################################################
                    print('update uncertainty...')
                    gpr = GaussianProcessRegressor(kernel=kernel, normalize_y=True).fit(train, train_f)
                    M_=np.max(train_f)
                    M=max([M,M_])

                    if safe_index-safe_index_old<tol and safe_index>tol:
                        print('refine grid ...')
                        safe_index_old=safe_index
                        p_=p+1
                        break
                    else:
                        safe_index_old=safe_index
                        start=time.time()
                        p_=p
                        break
    print('safety test...')
    t_end=430
    success, record, _,_,car_x,control_history=car_execution_disturbance(car_x,t_end,t_step,X_safe_tree,X_safe,ball_obs,record,Xfree,control_history)
    car_trajectory=np.stack(record)

    plt.figure()
    plt.pcolormesh(X,Y,Z,vmin=-1, vmax=1)

    plt.colorbar()
    rectangle=plt.Rectangle((left,down),width,height,fc='red')
    plt.gca().add_patch(rectangle)
    plt.plot(car_trajectory[:,0],car_trajectory[:,1],color='blue')

    plt.plot(car_trajectory_[:,0],car_trajectory_[:,1],color='black')
    switch_points=np.stack(switch)
    plt.scatter(switch_points[:,0],switch_points[:,1], color='black')

#            with open(filename, "wb") as fp:   #Pickling
#                cPickle.dump(X_safe, fp)
#            with open(filename2, "wb") as fp:   #Pickling
#                cPickle.dump(X_safe_tree, fp)
#            with open(filename3,"wb") as fp:
#                cPickle.dump(X_safe_control,fp)
#        else:
#            print('loading...')
#            with open(filename, "rb") as fp:   # Unpickling
#                X_safe = cPickle.load(fp)
#            with open(filename2, 'rb') as f:
#                X_safe_tree = cPickle.load(f)
#            with open(filename3,'rb') as f:
#                X_safe_control=cPickle.load(f)
#            for x,u in zip(X_safe,X_safe_control):
#                key=to_string(x)
#                Xfree.table[key].u=u

#        plt.plot(car_trajectory[:,0],car_trajectory[:,1])
        #learn model

    #######################################################################################################################################
    #########################################################ends iterations#################################################################3
#    print('test trajectory')
#
#    left = obs_1.parameter[0]
#    down = obs_1.parameter[2]
#    width = obs_1.parameter[1]-obs_1.parameter[0]
#    height = obs_1.parameter[3]-obs_1.parameter[2]
#    if len(X_safe)>0:
#        X_safe_tree=KDTree(X_safe)
#    center=np.array([1,0.7,-angle])
#    z,z1,z2= progression(car_disturbance,center,X_safe,X_safe_tree,Xfree,hP,ball_obs)
#
#    plt.figure()
#    plt.plot(car_trajectory[:,0],car_trajectory[:,1])
#    plt.pcolormesh(X,Y,Z,vmin=-1, vmax=1)
#    plt.title('Initial Value: ['+str(initial[0])+','+str(initial[1])+','+str(initial[2])+']')
#    plt.colorbar()
#    rectangle=plt.Rectangle((left,down),width,height,fc='red')
#    plt.gca().add_patch(rectangle)
##    plt.plot(z1,z2)
#
#    plt.figure()
#    X_temp_,state_temp,_ = gridsearch_construct(Xfree,P,fix=[2,angle])
#    if save:
#        T = [float(not x.unsafe) for x in state_temp]
#    else:
#        for x in state_temp:
#            if x.u is None:
#                x.unsafe=True
#            else:
#                x.unsafe=False
#        T = [float(not x.unsafe) for x in state_temp]
#
#    X1 = [float(x.state[0]) for x in state_temp]
#    X2 = [float(x.state[1]) for x in state_temp]
#
#    plt.scatter(X1,X2, c=T)
#    plt.colorbar()
#    rectangle=plt.Rectangle((left,down),width,height,fc='red')
#    plt.gca().add_patch(rectangle)
#    plt.plot(z1,z2)
#


from grouptheory.set import Set
from grouptheory.operation import Operation

class Group:

    def __init__(self, group_set, op):

        if not isinstance(group_set, Set):
            raise TypeError("First parameter must be an instance of Set")

        if not isinstance(op, Operation):
            raise TypeError("Second parameter must be an instance of Operation")

        if op.src != group_set * group_set:
            raise ValueError("Operation source must be a Cartesian product of %s" % type(group_set))

        if op.dst != group_set:
            raise ValueError("Operation destination must be a %s" % type(group_set))

        if not Group.is_associative(group_set, op):
            raise ValueError("Group operation is not associative")

        id = Group.identity(group_set, op)

        if id is None:
            raise ValueError("Group set identity is missing")

        if not Group.has_invertibility(group_set, op, id):
            raise ValueError("Some element in group doesn't have an inverse")

        self.set = group_set
        self.op = op
        self.elems = Set(Element(g, self) for g in self.set)
        self.id = Element(id, self)
        self.is_abelian = Group.if_abelian(self.set, self.op)

    def __len__(self):
        return len(self.set)

    def __iter__(self):
        yield self.id

        for g in self.elems:
            if g != self.id:
                yield g

    def __contains__(self, item):
        return item in self.elems

    def __eq__(self, other):
        if not isinstance(other, Group):
            return False

        return id(self) == id(other) or (
            self.set == other.set and self.op == other.op
        )

    def __ne__(self, other):
        return not (self == other)

    def __le__(self, other):
        if not isinstance(other, Group):
            raise TypeError("Other is not an instance of Group")
        for a in self.elems:
            for b in self.elems:
                bi = self.inverse(b)
                if a * bi not in self.elems:
                    return False
        return self.set <= other.set and len(self.set) > 0

    def is_normal_subgroup(self, other):
        return self <= other and all(Set(h * g for h in self) == Set(g * h for h in self) for g in other)

    def inverse(self, x):
        if x not in self.elems:
            raise ValueError("%s doesn't belong to this set")
        for g in self:
            if x * g == self.id:
                return g
        return None

    def orbit(self, m, M):
        if not isinstance(M, Set):
            raise TypeError("M must be an instance of Set")
        if m not in M:
            raise ValueError("%s element doesn't belong to set")
        orb = Set()
        for g in self.set:
            gm = self.op((g, m))
            if M.issubset([gm]):
                orb = Set(orb.union([gm]))
        return orb

    def stabilizer(self, m, M):
        if not isinstance(M, Set):
            raise TypeError("M must be an instance of Set")
        if m not in M:
            raise ValueError("%s element doesn't belong to set")
        subset = Set()

        for g in self.set:
            if self.op((g, m)) == m:
                subset = Set(subset.union([g]))
        return subset

    def __mul__(self, other):
        if not isinstance(other, Group):
            raise TypeError("Other must be a Group")
        op = Operation((self.set * other.set) * (self.set * other.set), (self.set * other.set),
                       lambda x: (self.op((x[0][0], x[1][0])), self.op((x[0][1], x[1][1]))))
        return Group(self.set * other.set, op)

    def __str__(self):
        letters = "exyzabcd"
        if len(self) > len(letters):
            return "Can't print a Cayley table"

        to_letter = {}
        to_elem = {}
        for letter, elem in zip(letters, self):
            to_letter[elem] = letter
            to_elem[letter] = elem
        letters = letters[:len(self)]

        result = "\n".join("%s: %s" % (l, to_elem[l]) for l in letters) + "\n\n"

        def print_tables():
            nonlocal result
            nonlocal letters

            head_letters = "   | " + " | ".join(l for l in letters) + " |"
            head_values = "   | " + " | ".join(str(to_elem[l]) for l in letters) + " |"

            border = (len(self) + 1) * "---+" + "\t" + (len(self) + 1) * "---+" + "\n"
            result += head_letters + "\t" + head_values + "\n" + border
            result += border.join(" %s | " % l +
                                  " | ".join(to_letter[to_elem[l] * to_elem[l1]]
                                             for l1 in letters) + "\t\t" +
                                  " %s | " % to_elem[l] +
                                  " | ".join(str(to_elem[l] * to_elem[l1])
                                             for l1 in letters) +
                                  " | \n" for l in letters)
            result += border

        # def values_mode():
        #
        #     result += head + "\n" + border
        #     result += border.join(" %s | " % to_elem[l] +
        #                           " | ".join(str(to_elem[l] * to_elem[l1])
        #                                      for l1 in letters) +
        #                           " |\n" for l in letters)
        #     result += border

        print_tables()
        return result

    @staticmethod
    def identity(group_set, op):
        for x in group_set:
            found = True
            for y in group_set:
                if op((x, y)) != y:
                    found = False
                    continue
            if found:
                return x
        return None

    @staticmethod
    def is_associative(group_set, op):
        for a in group_set:
            for b in group_set:
                for c in group_set:
                    if not op((a, op((b, c)))) == op((op((a, b)), c)):
                        return False
        return True

    @staticmethod
    def has_invertibility(group_set, op, id):
        for x in group_set:
            if not any(op((x, y)) == id for y in group_set):
                return False
        return True

    @staticmethod
    def if_abelian(group_set, op):
        return all(op((x, y)) == op((y, x)) for x in group_set for y in group_set)

class Element:

    def __init__(self, x, group):
        if not isinstance(group, Group):
            raise TypeError("group is not an instance of Group")
        if x not in group.set:
            raise ValueError("Element %s is not in group" % x)
        self.x = x
        self.group = group

    def __eq__(self, other):
        if not isinstance(other, Element):
            raise TypeError("Other element is not an instance of Element")
        return self.x == other.x

    def __ne__(self, other):
        return not (self == other)

    def __mul__(self, other):

        if not isinstance(other, Element):
            raise TypeError("Other must be an Element")

        try:
            return Element(self.group.op((self.x, other.x)), self.group)
        except TypeError:
            return other.__rmul__(self)

    def __rmul__(self, other):

        if not isinstance(other, Element):
            raise TypeError("Other must be an Element")

        return Element(self.group.op((other.x, self.x)), self.group)

    def __add__(self, other):
        if self.group.is_abelian:
            return self * other
        raise TypeError("%s is not an element of abelian group")

    def __hash__(self):
        return hash(self.x)

    def __str__(self):
        return str(self.x)


import webapp2
import jinja2
from google.appengine.api import users
from google.appengine.ext import ndb
from create import Create
import os
from myuser import MyUser
from eleveh import ELEVEH
from information import Information
from compare import Compare
import math

JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True
)

class MainPage(webapp2.RequestHandler):
    def get(self):

        self.response.headers['Content-type']='text/html'
        url=''
        url_string=''
        ele = None
        user=users.get_current_user()
        myuser=None

        if user:
            url=users.create_logout_url(self.request.uri)
            url_string='logout'

            myuser_key=ndb.Key('MyUser',user.email())
            myuser=myuser_key.get()
            if myuser == None:
                myuser= MyUser(id=user.email(),email_address=user.email())
                myuser.put()
        else:
            url= users.create_login_url(self.request.uri)
            url_string= 'login'

        button = self.request.get('button')
        if button == 'Query ev':
            year = 0
            name= self.request.get('name').strip()
            manufacturer = self.request.get('manufacturer').strip()
            minyear = self.request.get('minyear').strip()
            minbatterySize=self.request.get('minbatterySize').strip()
            minWLTPRange=self.request.get('minWLTPRange').strip()
            mincost=self.request.get('mincost').strip()
            minpower=self.request.get('minpower').strip()
            maxyear = self.request.get('maxyear').strip()
            maxbatterySize=self.request.get('maxbatterySize').strip()
            maxWLTPRange=self.request.get('maxWLTPRange').strip()
            maxcost=self.request.get('maxcost').strip()
            maxpower=self.request.get('maxpower').strip()

            ele = ELEVEH.query()
            ele2 = ele
            ele3 = ELEVEH.query().fetch(keys_only=True)
            i = 0
            z = 0

            if name != '':
                i=1
                z=1
                elea = ele.filter(ELEVEH.name == name).fetch(keys_only=True)
                ele3=set(ele3).intersection(elea)
            if manufacturer != '':
                i=1
                z=1
                eleb = ele.filter(ELEVEH.manufacturer == manufacturer).fetch(keys_only=True)
                ele3=set(ele3).intersection(eleb)
            if minyear != '' or maxyear != '':
                i=1
                if minyear != '' and maxyear != '':
                    z=1
                    elec = ele.filter(ELEVEH.year >= int(math.floor(float(minyear)))).fetch(keys_only=True)
                    eled = ele.filter(ELEVEH.year <= int(math.floor(float(maxyear)))).fetch(keys_only=True)
                    ele3 = set(ele3).intersection(elec)
                    ele3 = set(ele3).intersection(eled)
            if minbatterySize != '' or maxbatterySize != '':
                i=1
                if minbatterySize != '' and maxbatterySize != '':
                    z=1
                    elee = ele.filter(ELEVEH.batterySize >= float(minbatterySize)).fetch(keys_only=True)
                    elef = ele.filter(ELEVEH.batterySize <= float(maxbatterySize)).fetch(keys_only=True)
                    ele3 = set(ele3).intersection(elee)
                    ele3 = set(ele3).intersection(elef)
            if minWLTPRange != '' or maxWLTPRange != '':
                i=1
                if minWLTPRange != '' and maxWLTPRange != '':
                    z=1
                    eleg = ele.filter(ELEVEH.WLTPRange >= float(minWLTPRange)).fetch(keys_only=True)
                    eleh = ele.filter(ELEVEH.WLTPRange <= float(maxWLTPRange)).fetch(keys_only=True)
                    ele3 = set(ele3).intersection(eleg)
                    ele3 = set(ele3).intersection(eleh)
            if mincost != '' or maxcost != '':
                i=1
                if mincost != '' and maxcost != '':
                    z=1
                    elei = ele.filter(ELEVEH.cost >= float(mincost)).fetch(keys_only=True)
                    elej = ele.filter(ELEVEH.cost <= float(maxcost)).fetch(keys_only=True)
                    ele3 = set(ele3).intersection(elei)
                    ele3 = set(ele3).intersection(elej)
            if minpower != '' or maxpower != '':
                i=1
                if minpower != '' and maxpower != '':
                    z=1
                    elek = ele.filter(ELEVEH.power >= float(minpower)).fetch(keys_only=True)
                    elel = ele.filter(ELEVEH.power <= float(maxpower)).fetch(keys_only=True)
                    ele3 = set(ele3).intersection(elek)
                    ele3 = set(ele3).intersection(elel)

            if z==1:
                ele=ndb.get_multi(ele3)
            elif i==1 and z==0:
                ele=None
            elif i==0:
                ele = ele2
                ele = ele.fetch()

        template_values= {
            'url': url,
            'url_string': url_string,
            'user': user,
            'myuser': myuser,
            'ele' : ele
        }
        template= JINJA_ENVIRONMENT.get_template('main.html')
        self.response.write(template.render(template_values))

    def post(self):
        button = self.request.get('button')
        if button == 'Create Ev':
            self.redirect('/create')
        elif button == 'Compare Ev':
            self.redirect('/compare')

app = webapp2.WSGIApplication([('/',MainPage),
('/create',Create),
('/information', Information),
('/compare', Compare)
],debug=True)


from django.core.management.base import BaseCommand
from django.core.management import call_command
from django.db import transaction
from django.db import connection
from django.conf import settings as main_settings

from opds_catalog import opdsdb
from opds_catalog import models
from opds_catalog.models import Counter

from constance import config

class Command(BaseCommand):
    help = 'Utils for SOPDS.'
    verbose = False

    def add_arguments(self, parser):
        parser.add_argument('command', action="store", nargs='*', help='Use [ clear | info | save_mygenres | load_mygenres | setconf | getconf | pg_optimize ]')
        parser.add_argument('--verbose',action='store_true', dest='verbose', default=False, help='Set verbosity level for books collection scan.')
        parser.add_argument('--nogenres',action='store_true', dest='nogenres', default=False, help='Not install genres fom fixtures.')

    def handle(self, *args, **options):
        action = options['command'][0]

        self.verbose = options['verbose']
        self.nogenres = options['nogenres']

        if action=='clear':
            self.stdout.write('Clear book database.')
            self.clear()
        elif action == "info":
            self.info()
        elif action == "save_mygenres":
            self.save_mygenres()
        elif action == "load_mygenres":
            self.load_mygenres()
        elif action == "setconf":
            self.confparam = options['command'][1] if len(options['command'])>1 else None
            self.confvalue = options['command'][2] if len(options['command'])>2 else None
            self.setconf(self.confparam, self.confvalue)
        elif action == "getconf":
            self.confparam = options['command'][1] if len(options['command'])>1 else None
            self.getconf(self.confparam)
        elif action == "pg_optimize":
            self.pg_optimize()

    def clear(self):
        with transaction.atomic():
            opdsdb.clear_all(self.verbose)
        if not self.nogenres:
            call_command('loaddata', 'genre.json')
        Counter.objects.update_known_counters()
        opdsdb.pg_optimize(False)

    def info(self):
        Counter.objects.update_known_counters()
        self.stdout.write('Books count    = %s'%Counter.objects.get_counter(models.counter_allbooks))
        self.stdout.write('Catalogs count = %s'%Counter.objects.get_counter(models.counter_allcatalogs))
        self.stdout.write('Authors count  = %s'%Counter.objects.get_counter(models.counter_allauthors))
        self.stdout.write('Genres count   = %s'%Counter.objects.get_counter(models.counter_allgenres))
        self.stdout.write('Series count   = %s'%Counter.objects.get_counter(models.counter_allseries))

    def save_mygenres(self):
        call_command('dumpdata', 'opds_catalog.genre','--output','opds_catalog/fixtures/mygenres.json')
        self.stdout.write('Genre dump saved in opds_catalog/fixtures/mygenres.json')

    def load_mygenres(self):
        opdsdb.clear_genres(self.verbose)
        call_command('loaddata', 'mygenres.json')
        Counter.objects.update_known_counters()
        self.stdout.write('Genres load from opds_catalog/fixtures/mygenres.json')

    def setconf(self, confparam, confvalue):
        if confparam and confvalue:
            call_command('constance', 'set',confparam, confvalue)
            self.stdout.write('Config parameter %s set to %s'%(confparam, confvalue))

    def getconf(self, confparam):
        if confparam:
            call_command('constance', 'get', confparam)
        else:
            call_command('constance', 'list')

    def pg_optimize(self):
        opdsdb.pg_optimize(True)


from posixpath import split
from .dataset import ImageDataset, DataSetContainer
from .transform import build_transforms
from torch.utils.data import DataLoader
from .collate_batch import train_collate_fn, val_collate_fn, test_collate_fn

def make_data_loader(cfg, phase='train'):
    transfrom = build_transforms(cfg)
    dataset = DataSetContainer(cfg)
    if phase == 'train':
        att_label_mapper = dataset.att_label_mapper
    elif phase == 'test':
        att_label_mapper = {}

    train_dataset = ImageDataset(dataset, transform=transfrom, phase='train')
    val_dataset = ImageDataset(dataset, transform=transfrom, phase='val')
    test_dataset = ImageDataset(dataset, transform=transfrom, phase='test')

    train_loader = DataLoader(train_dataset, batch_size=cfg.Train_Batch_Size,shuffle=True, num_workers=cfg.Num_Workers, collate_fn=train_collate_fn)
    val_loader = DataLoader(val_dataset, batch_size=cfg.Val_Batch_Size, shuffle=False, num_workers=cfg.Num_Workers, collate_fn=val_collate_fn)
    test_loader = DataLoader(test_dataset, batch_size=cfg.Test_Batch_Size, shuffle=False, num_workers=cfg.Num_Workers, collate_fn=test_collate_fn)

    return train_loader, val_loader, test_loader, att_label_mapper


import datetime
from django.db import models
from profiles.models import Profile
from django.contrib.auth.models import User

class PostalCodes(models.Model):
    town = models.CharField(max_length=70)

    def __str__(self):
        return f"{self.id} {self.town}"

class House(models.Model):
    address = models.CharField(max_length=255)
    street_nr = models.CharField(max_length=10, blank=True)
    price = models.BigIntegerField()
    p_code = models.ForeignKey(PostalCodes, on_delete=models.CASCADE)
    seller = models.ForeignKey(User, on_delete=models.CASCADE)
    on_sale = models.BooleanField(default=False)
    sellingdate = models.DateField("Date", default=datetime.date.today)

class HouseType(models.Model):
    type = models.CharField(max_length=70)

    def __str__(self):
        return f"{self.type}"

class HouseInfo(models.Model):
    house = models.ForeignKey(House, on_delete=models.CASCADE)
    description = models.CharField(max_length=1500)
    rooms = models.SmallIntegerField()
    size = models.SmallIntegerField()
    type = models.ForeignKey(HouseType, on_delete=models.CASCADE)
    garage = models.BooleanField(default=False)
    extra_apartment = models.BooleanField(default=False)
    new_building = models.BooleanField(default=False)
    elevator = models.BooleanField(default=False)
    entrance = models.BooleanField(default=False)
    buildyear = models.CharField(max_length=4, blank=True)

    def __str__(self):
        return "Þetta hús er {}, hefur {} herbergi og er {} fm.".format(self.type, self.rooms, self.size)

class HouseImage(models.Model):
    image = models.CharField(max_length=500)
    house = models.ForeignKey(House, on_delete=models.CASCADE)

    def __str__(self):
        return f"{self.image}"

class OnHold(models.Model):
    house = models.ForeignKey(House, on_delete=models.CASCADE)

class WishList(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    house = models.ForeignKey(House, on_delete=models.CASCADE)

class History(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    house = models.ForeignKey(House, on_delete=models.CASCADE)

class Offers(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="buyer")
    seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name="seller")
    house = models.ForeignKey(House, on_delete=models.CASCADE)
    price = models.BigIntegerField()
    date = models.DateTimeField(auto_now_add=True, blank=True)
    state = models.BooleanField(default=False)
    seen = models.BooleanField(default=False)
    accepted = models.BooleanField(default=False)