# Stdlib imports
# Core Django imports
from crispy_forms.bootstrap import Field, FieldWithButtons, StrictButton
from crispy_forms.layout import (
    HTML,
    Button,
    ButtonHolder,
    Column,
    Hidden,
    Layout,
    Row,
    Submit,
)
from django import forms
from django.contrib.gis.geos import Point as geoPoint
from django.core.exceptions import ValidationError
from django.urls import reverse_lazy
from django.utils.http import urlencode

# Third-party app imports
# Imports from my apps
from src.campaigns.models import Campaign
from src.model_grids.models import ModelGrid
from src.plots.models import StationsPlot
from src.utils.mixins import CrispyMixin
from src.utils.util import M2MSelect
from src.utils.util import degree_sign as deg

from .models import Station

class StationForm(CrispyMixin, forms.ModelForm):
    current_url = "stations:create"

    longitude = forms.DecimalField(
        min_value=-180,
        max_value=180,
        help_text=f"{deg}E",
        max_digits=9,
        decimal_places=6,
    )
    latitude = forms.DecimalField(
        min_value=-90,
        max_value=90,
        help_text=f"{deg}N",
        max_digits=8,
        decimal_places=6,
        # Pb: 'step' force the number of mandatory decimal
        # widget=forms.NumberInput(
        #     attrs={'step': 0.01,}
        # ),
    )

    altitude = forms.DecimalField(
        min_value=0,
        help_text="m",
        max_digits=11,
        decimal_places=6,
    )

    plots = forms.ModelMultipleChoiceField(
        required=False,
        queryset=StationsPlot.objects.all(),
        widget=forms.CheckboxSelectMultiple(
            attrs={"class": "form-check", "style": "list-style:none;"}
        ),
    )

    campaigns = forms.ModelMultipleChoiceField(
        required=False,
        queryset=Campaign.objects.all(),
        widget=forms.CheckboxSelectMultiple(
            attrs={"class": "form-check", "style": "list-style:none;"}
        ),
    )

    start_datetime = forms.DateTimeField(
        widget=forms.widgets.DateTimeInput(attrs={"placeholder": "YYYY-MM-DD HH:MM"})
    )
    end_datetime = forms.DateTimeField(
        widget=forms.widgets.DateTimeInput(attrs={"placeholder": "YYYY-MM-DD HH:MM"})
    )

    class Meta:
        model = Station
        fields = [
            "name",
            "longitude",
            "latitude",
            "altitude",
            "is_active",
            "station_id",
            "wmo_id",
            "description",
            "margin",
            "plots",
            "campaigns",
            "uses_flexpart",
            "start_datetime",
            "end_datetime",
            "alt_lower",
            "alt_upper",
            "alt_unit",
            "numb_part",
            "xmass",
            "number_grid",
        ]

    def _init_helper_layout(self):
        """initialise crispy layout"""
        self.helper.error_text_inline = False
        self.helper.layout = Layout(
            Hidden("next", "{{ request.GET.path }}"),
            Row(
                Column(
                    Field("name"),
                    css_class="form-group col-md-4 mb-0",
                ),
                Column(
                    Field("station_id"),
                    css_class="form-group col-md-4 mb-0",
                ),
                Column(
                    Field("wmo_id"),
                    css_class="form-group col-md-4 mb-0",
                ),
                css_class="form-row",
            ),
            HTML("<legend>Coordinates</legend>"),
            Row(
                Column(
                    Field("latitude"),
                    css_class="form-group col-md-4 mb-0",
                ),
                Column(
                    Field("longitude"),
                    css_class="form-group col-md-4 mb-0",
                ),
                Column(
                    Field("altitude"),
                    css_class="form-group col-md-4 mb-0",
                ),
                css_class="form-row",
            ),
            # Field("name"),
            # Fieldset("Coordinates", "latitude", "longitude", "altitude"),
            FieldWithButtons(
                "margin",
                StrictButton(
                    "",
                    css_class="btn-success",
                    onclick='window.location.href = "{}?{}";'.format(
                        reverse_lazy("margins:list"),
                        urlencode({"next": reverse_lazy(self.current_url)}),
                    ),
                ),
            ),
            Field("is_active"),
            Field("uses_flexpart"),
            Field("description", rows="4"),
            HTML("
"),
            HTML("
"),
            Field("campaigns"),
            HTML("
"),
            HTML("
"),
            HTML("
"),
            Field("plots"),
            HTML("
"),
            HTML("
"),
            HTML("
"),
            Row(
                Column(
                    Field("start_datetime"),
                    css_class="form-group col-md-6 mb-0",
                ),
                Column(
                    Field("end_datetime"),
                    css_class="form-group col-md-6 mb-0",
                ),
                css_class="form-row",
            ),
            Row(
                Column(
                    Field("alt_lower"),
                    css_class="form-group col-md-4 mb-0",
                ),
                Column(
                    Field("alt_upper"),
                    css_class="form-group col-md-4 mb-0",
                ),
                Column(
                    Field("alt_unit"),
                    css_class="form-group col-md-4 mb-0",
                ),
                css_class="form-row",
            ),
            Row(
                Column(
                    Field("numb_part"),
                    css_class="form-group col-md-4 mb-0",
                ),
                Column(
                    Field("xmass"),
                    css_class="form-group col-md-4 mb-0",
                ),
                Column(
                    Field("number_grid"),
                    css_class="form-group col-md-4 mb-0",
                ),
                css_class="form-row",
            ),
            HTML("
"),
            # HTML(""),
            HTML("
"),
            ButtonHolder(
                Submit("submit", "Submit", css_class="btn-success"),
                Button(
                    "cancel",
                    "Cancel",
                    css_class="btn-primary",
                    onclick="history.go(-1);",
                ),
            ),
        )

    def _custom_helper(self):
        """customize crispy form"""
        # Sort plots alphabetically
        self.fields["plots"].label = "Available plots"
        self.fields["plots"].queryset = StationsPlot.objects.order_by("name")
        # rename label
        self.fields["start_datetime"].label = "Starting date"
        self.fields["end_datetime"].label = "Ending date"
        self.fields["alt_lower"].label = "Lower altitude"
        self.fields["alt_upper"].label = "Upper altitude"
        self.fields["alt_unit"].label = "Unit"
        self.fields["numb_part"].label = "Particles"
        self.fields["xmass"].label = "Total mass"
        # fields not required
        self.fields["start_datetime"].required = False
        self.fields["end_datetime"].required = False
        self.fields["alt_lower"].required = False
        self.fields["alt_upper"].required = False
        self.fields["alt_unit"].required = False
        self.fields["numb_part"].required = False
        self.fields["xmass"].required = False
        self.fields["number_grid"].required = False

    def clean(self):
        """check station is located inside one the registered forecast"""
        cleaned_data = super().clean()
        # capitalize name
        name = cleaned_data.get("name")
        lat = float(cleaned_data.get("latitude", 0))
        lon = float(cleaned_data.get("longitude", 0))
        alt = float(cleaned_data.get("altitude", 0))
        point = geoPoint(lon, lat, alt)

        model_grids = ModelGrid.objects.all()

        if len(model_grids) == 0:
            raise ValidationError(
                "No Weather Forecast registered. You must have registered at least one before assigning station"
            )

        for model_grid in model_grids:
            poly = model_grid.geom
            # valid = poly.contains(point)
            # valid = poly.covers(point)
            valid = point.intersects(poly)
            if valid:
                print(f"Station {name} is in {model_grid.name}")
                break

        if not valid:
            raise ValidationError(
                "Station is not inside any ModelGrid registered.",
            )

        uses_flexpart = cleaned_data.get("uses_flexpart")

        alt_lower = cleaned_data.get("alt_lower")
        alt_upper = cleaned_data.get("alt_upper")

        if uses_flexpart:
            if not alt_lower and alt_lower != 0:
                raise ValidationError("Lower altitude is a required field.")
            if not alt_upper and alt_upper != 0:
                raise ValidationError("Upper altitude is a required field.")
            if alt_upper < alt_lower:
                raise ValidationError(
                    "Flexpart release altitudes aren't sorted in ascending order.",
                )

        start_datetime = cleaned_data.get("start_datetime")
        end_datetime = cleaned_data.get("end_datetime")

        if uses_flexpart:
            if start_datetime and end_datetime and end_datetime < start_datetime:
                raise ValidationError(
                    "Flexpart release dates aren't sorted in ascending order.",
                )

        numb_part = cleaned_data.get("numb_part")
        if not isinstance(numb_part, int):
            raise ValidationError(
                "Flexpart number of particles must be an integer.",
            )

        xmass = cleaned_data.get("xmass")
        if not isinstance(xmass, int):
            raise ValidationError(
                "Flexpart xmass must be an integer.",
            )

        number_grid = cleaned_data.get("number_grid")
        if not isinstance(number_grid, int):
            raise ValidationError(
                "Flexpart number_grid must be an integer.",
            )

class StationUpdateForm(StationForm):
    def _custom_helper(self):
        """customize crispy form"""
        super()._custom_helper()
        # change some field
        self.fields["name"].disabled = True

class StationCampaignForm(CrispyMixin, forms.ModelForm):
    current_url = "stations:redirect"

    campaigns = forms.ModelMultipleChoiceField(
        required=False,
        queryset=Campaign.objects.all(),
        widget=M2MSelect,
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["campaigns"].empty_label = "---------"

    class Meta:
        model = Station
        fields = [
            "campaigns",
        ]

    def _init_helper_layout(self):
        """initialise crispy layout"""
        self.helper.attrs = {
            "data-change-campaign-url": reverse_lazy("stations:change_campaign"),
        }
        self.helper.error_text_inline = False
        self.helper.form_show_labels = False
        self.helper.layout = Layout(
            Hidden("next", "{{ request.GET.path }}"),
            FieldWithButtons(
                "campaigns",
                StrictButton(
                    "",
                    css_class="btn-success",
                    onclick='window.location.href = "{}?{}";'.format(
                        reverse_lazy("campaigns:list"),
                        urlencode({"next": reverse_lazy(self.current_url)}),
                    ),
                ),
                css_id="change_campaign_id",
            ),
        )


#!/usr/bin/env python3

# Created by: Aleksandr Ten
# Created on: April 2022
# This program calculates all the possible RGB combinations

def main():
    # this function has nested loops
    red = 0
    green = 0
    blue = 0

    # process & output
    for red in range(255 + 1):
        for green in range(255 + 1):
            for blue in range(255 + 1):
                print("RGB ({0}, {1}, {2})".format(red, green, blue))

    print("\nDone.")

if __name__ == "__main__":
    main()


from wpilib.command.subsystem import Subsystem
from wpilib.doublesolenoid import DoubleSolenoid
from ctre.wpi_talonsrx import WPI_TalonSRX
from wpilib.analoginput import AnalogInput
from subsystems.GrabberPosition import GrabberPosition
from robot_map import Grabber as GrabberMap, Compressor as CompressorMap
from wpilib.smartdashboard import SmartDashboard

class Grabber(Subsystem):
    NORMAL_MOVE_SPEED = 0.5
    open: bool
    pneumatic: DoubleSolenoid
    grabber_motor: WPI_TalonSRX
    distance: AnalogInput
    set_point: int
    auto_grab: bool
    grabber_position: GrabberPosition

    def __init__(self):
        super().__init__("Grabber")

        self.pneumatic = DoubleSolenoid(CompressorMap.compressor_port, GrabberMap.grabber_ports[0],
                                        GrabberMap.grabber_ports[1])
        self.grabber_motor = WPI_TalonSRX(GrabberMap.grabber_motor_port)
        self.grabber_motor.setInverted(GrabberMap.grabber_motor_inverted)
        self.grabber_motor.configSelectedFeedbackSensor(WPI_TalonSRX.FeedbackDevice.CTRE_MagEncoder_Relative, 0, 0)
        self.grabber_motor.setSensorPhase(GrabberMap.grabber_encoder_inverted)
        self.grabber_motor.setNeutralMode(WPI_TalonSRX.NeutralMode.Brake)
        self.grabber_motor.setSelectedSensorPosition(0, 0, 0)

        self.grabber_motor.configMaxIntegralAccumulator(0, 0, 0)
        self.pidf(0.6, 0.000003, 240, 0.025)

        self.distance = AnalogInput(GrabberMap.distance_sensor_port)

        self.auto_grab = True
        self.grabber_position = GrabberPosition.HIGH
        self.set_point = 0
        self.open = self.pneumatic.get() == DoubleSolenoid.Value.kForward

    def pidf(self, p, i, d, f):
        self.grabber_motor.config_kP(0, p, 0)
        self.grabber_motor.config_kI(0, i, 0)
        self.grabber_motor.config_kD(0, d, 0)
        self.grabber_motor.config_kF(0, f, 0)

    def get_distance(self):
        return (self.distance.getVoltage() * 60) + 50

    def calculate(self):
        if self.auto_grab and self.open and self.get_distance() < 75:
            self.capture_cube()
            self.auto_grab = False
        elif self.get_distance() > 180:
            self.auto_grab = True

    @staticmethod
    def fuzzy_equal(a, b) -> bool:
        return abs(a - b) < 250

    def reset(self):
        self.grabber_motor.setSelectedSensorPosition(0, 0, 0)

    def get_set_point(self):
        return self.set_point

    def capture_cube(self):
        self.pneumatic.set(DoubleSolenoid.Value.kReverse)
        self.open = False

    def release_cube(self):
        self.pneumatic.set(DoubleSolenoid.Value.kForward)
        self.open = True

    def go_to_set_point(self, set_point: int or GrabberPosition) -> None:
        if isinstance(set_point, GrabberPosition):
            self.grabber_position = set_point
            set_point = set_point.value
        self.grabber_motor.set(WPI_TalonSRX.ControlMode.Position, set_point)

    def set_speed(self, speed):
        self.grabber_motor.set(WPI_TalonSRX.ControlMode.PercentOutput, speed)

    def get_current_grabber_position(self) -> GrabberPosition:
        return self.grabber_position

    def get_current_position(self) -> int:
        return self.grabber_motor.getSelectedSensorPosition(0)

    def at_target_position(self) -> bool:
        return self.fuzzy_equal(self.set_point, self.get_current_position())

    def log(self):
        SmartDashboard.putBoolean("Grabber Open", self.open)
        SmartDashboard.putNumber("Grabber Current Position", self.get_current_position())
        SmartDashboard.putNumber("Grabber Speed", self.grabber_motor.get())
        SmartDashboard.putString("Grabber Distance", str(self.get_distance()))
        SmartDashboard.putString("Grabber Position", self.get_current_grabber_position().name)


import uuid

import pytest
from fakeredis.aioredis import FakeRedis
from fastapi import FastAPI
from httpx import AsyncClient
from starlette import status

@pytest.mark.anyio
async def test_setting_value(
    fastapi_app: FastAPI,
    fake_redis: FakeRedis,
    client: AsyncClient,
) -> None:
    """
    Tests that you can set value in redis.

    :param fastapi_app: current application fixture.
    :param fake_redis: fake redis instance.
    :param client: client fixture.
    """
    url = fastapi_app.url_path_for("handle_http_post")

    test_key = uuid.uuid4().hex
    test_val = uuid.uuid4().hex
    query = """
    mutation ($key: String!, $val: String!) {
        setRedisValue(data: { key: $key, value: $val }) {
            key
            value
        }
    }
    """
    response = await client.post(
        url,
        json={
            "query": query,
            "variables": {"key": test_key, "val": test_val},
        },
    )

    assert response.status_code == status.HTTP_200_OK
    actual_value = await fake_redis.get(test_key)
    assert actual_value == test_val

@pytest.mark.anyio
async def test_getting_value(
    fastapi_app: FastAPI,
    fake_redis: FakeRedis,
    client: AsyncClient,
) -> None:
    """
    Tests that you can get value from redis by key.

    :param fastapi_app: current application fixture.
    :param fake_redis: fake redis instance.
    :param client: client fixture.
    """
    test_key = uuid.uuid4().hex
    test_val = uuid.uuid4().hex
    await fake_redis.set(test_key, test_val)
    url = fastapi_app.url_path_for("handle_http_post")
    response = await client.post(
        url,
        json={
            "query": "query($key:String!){redis:getRedisValue(key:$key){key value}}",
            "variables": {
                "key": test_key,
            },
        },
    )

    assert response.status_code == status.HTTP_200_OK
    assert response.json()["data"]["redis"]["key"] == test_key
    assert response.json()["data"]["redis"]["value"] == test_val


SECONDS_IN_A_MINUTE = 60
SECONDS_IN_A_HOUR = 3600
SECONDS_IN_A_DAY = 86400

class UnableToParseError(Exception):
    pass

def parse_timestamp(timestamp: str):
    try:
        return int(timestamp)
    except ValueError:
        return parse_colon_timestamp(timestamp)

def parse_colon_timestamp(timestamp: str):
    if ":" not in timestamp:
        raise UnableToParseError("Unsupported time format.")

    else:
        time_parts = timestamp.split(":")
        try:
            time_parts_conv = [int(i) for i in time_parts]
        except TypeError:
            raise UnableToParseError("Invalid time input.")

        if len(time_parts_conv) == 2:
            minutes, seconds = time_parts_conv
            return minutes * SECONDS_IN_A_MINUTE + seconds

        elif len(time_parts_conv) == 3:
            hours, minutes, seconds = time_parts_conv
            return hours * SECONDS_IN_A_HOUR + minutes * SECONDS_IN_A_MINUTE + seconds

        elif len(time_parts_conv) == 4:
            days, hours, minutes, seconds = time_parts_conv
            return days * SECONDS_IN_A_DAY + hours * SECONDS_IN_A_HOUR + minutes * SECONDS_IN_A_MINUTE + seconds

        else:
            raise UnableToParseError("Unsupported time input. Supported time inputs are either in seconds or in colons (up to days).")

def parse_to_timestamp(time_seconds: int):
    if time_seconds < SECONDS_IN_A_MINUTE:
        if time_seconds < 10:
            time_seconds = f"0{time_seconds}"
        return f"0:{time_seconds}"
    elif time_seconds < SECONDS_IN_A_HOUR:
        return parse_seconds_to_minutes(time_seconds)
    elif time_seconds < SECONDS_IN_A_DAY:
        return parse_seconds_to_hours(time_seconds)
    else:
        return parse_seconds_to_days(time_seconds)

def parse_seconds_to_minutes(seconds: int):
    minutes = int(seconds / SECONDS_IN_A_MINUTE)
    remainder = seconds - minutes * SECONDS_IN_A_MINUTE

    if minutes < 10:
        minutes = f"0{minutes}"
    if remainder < 10:
        remainder = f"0{remainder}"
    return f"{minutes}:{remainder}"

def parse_seconds_to_hours(seconds: int):
    hours = int(seconds / SECONDS_IN_A_HOUR)
    remainder = seconds - hours * SECONDS_IN_A_HOUR

    if hours < 10:
        hours = f"0{hours}"
    return f"{hours}:{parse_seconds_to_minutes(remainder)}"

def parse_seconds_to_days(seconds: int):
    days = int(seconds / SECONDS_IN_A_DAY)
    remainder = seconds - days * SECONDS_IN_A_DAY

    return f"{days}:{parse_seconds_to_hours(remainder)}"

if __name__ == "__main__":
    print(parse_to_timestamp(10))
    print(parse_to_timestamp(100))
    print(parse_to_timestamp(10000))
    print(parse_to_timestamp(100000))


from genericpath import isdir
from PIL import Image
import numpy as np
import os

def get_paths(dir):
    paths = []
    for path in os.listdir(dir):
        if os.path.isdir(dir + '/' + path):
            print('\rReading {} ...'.format(dir + '/' + path), end = '')
            paths += get_paths(dir + '/' + path)
        else:
            paths.append(dir + '/' + path)
    return paths

class FaceDataset:
    def __init__(self, path_list):
        self.path_list = path_list
        self.maxlen = len(path_list)

    def __iter__(self):
        self.count = 0
        return self

    def __next__(self):
        if (self.count >= self.maxlen):
            raise StopIteration
        else:
            path = self.path_list[self.count]
            img = Image.open(path)
            pix = np.asarray(img)
            self.count += 1
            return path, pix

path_list = get_paths('Data/pic_load')
print('\nThe number of pictures:', len(path_list))

facedataset = FaceDataset(path_list[:10])
for pic_path, face in facedataset:
    try:
        print(pic_path, face.shape)
    except StopIteration:
        break


import sys
sys.stdin=open('bj1238.txt','r')

import sys
input=sys.stdin.readline
N,M,X=map(int,input().split())
Map=[[10**9]*N for _ in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    Map[a-1][b-1]=c
for k in range(N):
    Map[k][k]=0
    for i in range(N):
        if Map[i][k]==10**9:continue
        for j in range(N):
            Map[i][j]=min(Map[i][j],Map[i][k]+Map[k][j])
print(Map)
print(max(Map[i][X-1]+Map[X-1][i]for i in range(N)))


from sys import stdin, stdout
n = int(stdin.readline())
n1=n
for i in range(n1):
    r,s,p = [int(x) for x in stdin.readline().rstrip().split()]
    n=0
    su=1
    arr=[]
    arr.append(1)
    for i in range(n,p):
        arr.append(((arr[n]*r)%p))
        su= sum(arr)%p
        n+=1
        if(su== s):
            stdout.write(str(n+1)+"\n")
            break
    if( n==p):
        stdout.write(str(-1)+"\n")


#!/usr/bin/env python

"""
Test the calibtic helper fitting a PSP by integrating the COBA ODE
"""

import unittest
import numpy as np
from pycake.helpers import ode_psp_fit

class TestCalibticHelper(unittest.TestCase):
    def setUp(self):
        self.data = np.loadtxt("share/cake/test_data/ode_psp_fit_membrane.dat")

    def test_fit(self):
        t = np.arange(0, 60, 0.1)
        v = self.data[:, 1]

        orig_v_rest = -50
        orig_e_rev = 20
        orig_tau_rev = 2
        orig_tau_m = 5
        orig_cm = 0.2
        orig_w = 0.01

        # we pin the reversal potential to 20 (mV) as weight and
        # reversal potential are highly correlated
        bounds = [(-np.inf, orig_e_rev - 0.01, 0, 0, 0),
                  (np.inf, orig_e_rev + 0.01, np.inf, np.inf, np.inf)]

        # some very rough starting values
        p0 = [0, 20, 1, 1, 1]

        popt, pcov = ode_psp_fit.fit(t, v, p0, bounds=bounds)

        v_rest, e_rev, tau_rev, tau_m, w_per_cm = popt

        self.assertAlmostEqual(v_rest, orig_v_rest, 2)
        self.assertAlmostEqual(e_rev, orig_e_rev, 1)
        self.assertLess(np.abs(tau_rev - orig_tau_rev) / orig_tau_rev, 0.1)
        self.assertLess(np.abs(tau_m - orig_tau_m) / orig_tau_m, 0.1)
        self.assertAlmostEqual(w_per_cm, orig_w / orig_cm, 2)

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


from ops import *

class Generator:
    def __init__(self, name):
        self.name = name

    def __call__(self, inputs, train_phase):
        with tf.variable_scope(name_or_scope=self.name, reuse=tf.AUTO_REUSE):
            inputs = dense("dense", inputs, 256*4*4)
            inputs = tf.reshape(inputs, [-1, 4, 4, 256])
            inputs = G_Resblock("ResBlock1", inputs, 256, train_phase)
            inputs = G_Resblock("ResBlock2", inputs, 256, train_phase)
            inputs = G_Resblock("ResBlock3", inputs, 256, train_phase)
            inputs = relu(batchnorm(inputs, train_phase, "BN"))
            inputs = conv("conv", inputs, k_size=3, nums_out=3, strides=1)
        return tf.nn.tanh(inputs)

    def var_list(self):
        return tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, self.name)

class Discriminator:
    def __init__(self, name):
        self.name = name

    def __call__(self, inputs, update_collection=None):
        with tf.variable_scope(name_or_scope=self.name, reuse=tf.AUTO_REUSE):
            inputs = D_FirstResblock("ResBlock1", inputs, 128, update_collection, is_down=True)
            inputs = D_Resblock("ResBlock2", inputs, 128, update_collection, is_down=True)
            inputs = D_Resblock("ResBlock3", inputs, 128, update_collection, is_down=False)
            inputs = D_Resblock("ResBlock4", inputs, 128, update_collection, is_down=False)
            inputs = relu(inputs)
            inputs = global_sum_pooling(inputs)
            inputs = dense("dense", inputs, 1, update_collection, is_sn=True)
            return inputs

    def var_list(self):
        return tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, self.name)

if __name__ == "__main__":
    x = tf.placeholder(tf.float32, [None, 32, 32, 3])
    z = tf.placeholder(tf.float32, [None, 100])
    y = tf.placeholder(tf.float32, [None, 100])
    train_phase = tf.placeholder(tf.bool)
    G = Generator("generator")
    D = Discriminator("discriminator")
    fake_img = G(z, train_phase)
    fake_logit = D(fake_img)
    aaa = 0


import requests
import json
import pytest

url = 'https://petstore.swagger.io/v2/user'

def base(**kwargs):
    with open(str(kwargs['json_file'])) as f:
        user = json.load(f)[str(kwargs['user'])]

        obj_post = requests.post(url, json=user)
        assert obj_post.status_code == int(kwargs['post_status']), \
            f'ERROR POST: url {url}.\nData {user}.\nStatus error {obj_post.status_code}'

        obj_get = requests.get(f'{url}/{user["username"]}')
        assert obj_get.status_code == int(kwargs['get_status']), \
            f'ERROR GET: url {url}. \nUsername {user["username"]}. \nStatus error {obj_get.status_code}'

# POST REQUESTS
def test_create_user():
    base(
        json_file='post_response.json',
        user='create_user',
        post_status=200,
        get_status=200
    )

def test_create_user_null():
    base(
        json_file='post_response.json',
        user='create_user_null',
        post_status=500,
        get_status=404
    )

def test_create_user_id_str():
    base(
        json_file='post_response.json',
        user='create_user_id_str',
        post_status=500,
        get_status=404
    )

def test_create_user_id_minus():
    base(
        json_file='post_response.json',
        user='create_user_id_minus',
        post_status=500,
        get_status=404
    )

def test_create_user_all_info_int():
    base(
        json_file='post_response.json',
        user='create_user_all_info_int',
        post_status=500,
        get_status=404
    )

def test_create_user_more_then_limit_user_status():
    base(
        json_file='post_response.json',
        user='create_user_more_then_limit_userStatus',
        post_status=500,
        get_status=404
    )

def test_create_user_boolean_id():
    base(
        json_file='post_response.json',
        user='create_user_boolean_id',
        post_status=500,
        get_status=404
    )

def test_create_user_boolean_user_status():
    base(
        json_file='post_response.json',
        user='create_user_boolean_userStatus',
        post_status=500,
        get_status=404
    )

def test_create_user_empty_username():
    base(
        json_file='post_response.json',
        user='create_user_empty_username',
        post_status=400,
        get_status=404
    )

def test_create_user_only_username():
    base(
        json_file='post_response.json',
        user='create_user_only_username',
        post_status=400,
        get_status=404
    )

if __name__ == '__main__':
    test_create_user()
    # test_create_user_null()
    # test_create_user_id_str()
    # test_create_user_all_info_int()
    # test_create_user_more_then_limit_user_status()
    # test_create_user_boolean_id()
    # test_create_user_boolean_userStatus()
    # test_create_user_empty_username()
    # test_create_user_only_username()
    # pass


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        if head == None:
            return head
        cur = head
        prv = None
        while(cur != None):
            if head.val == val:
                head = head.next
                cur= head
            elif cur.val == val:
                prv.next = cur.next
                cur = cur.next
            else:
                prv = cur
                cur = cur.next
        return head


"""

 PACKNET  -  c0mplh4cks

 DATATYPE
    ADDR

"""

# === Importing Dependencies === #
from struct import pack, unpack
from . import IP, MAC, INT

# === ADDR === #
class ADDR:
    def __init__(self, ip="255.255.255.255", port=0, mac="ff:ff:ff:ff:ff:ff", version=4, size=2):
        self.__ip = IP( ip, version=version )
        self.__port = INT( port, size=size )
        self.__mac = MAC( mac )

    def __str__(self):
        return f"['{self.ip}', {self.port}, '{self.mac}']"

    def __getitem__(self, key):
        if key == 0 or key == "ip":
            return self.ip
        elif key == 1 or key == "port":
            return self.port
        elif key == 2 or key == "mac":
            return self.mac
        else:
            raise IndexError

    def __setitem__(self, key, value):
        if key == 0 or key == "ip":
            self.ip = value
        elif key == 1 or key == "port":
            self.port = value
        elif key == 2 or key == "mac":
            self.mac = value
        else:
            raise IndexError

    @property
    def ip(self):
        return self.__ip

    @ip.setter
    def ip(self, value):
        if type(value) == str:
            self.__ip.ip = value
        elif type(value) == IP:
            self.__ip = value

    @property
    def port(self):
        return self.__port

    @port.setter
    def port(self, value):
        if type(value) == str:
            self.__port.integer = value
        elif type(value) == INT:
            self.__port = value

    @property
    def mac(self):
        return self.__mac

    @mac.setter
    def mac(self, value):
        if type(value) == str:
            self.__mac.mac = value
        elif type(value) == MAC:
            self.__mac = value

    @property
    def addr(self):
        return [ str(self.ip), int(self.port), str(self.mac) ]

    @addr.setter
    def addr(self, value):
        self.ip, self.port, self.mac = value


import numpy as np
import cv2
import imutils

image= cv2.imread("/Users/sam/All-Program/App-DataSet/Image-Processing/sam.jpg")
cv2.imshow("Original_image", image)
cv2.waitKey(0)

## Our Image is too large, hence we first shorten it using resizing
resized=imutils.resize(image, height=500)  # Passing the new height of the image , the width is automatically calculated in resize method of the imutil package
cv2.imshow("resized image", resized)
cv2.waitKey(0)
image=resized

## Now we determine the pixels that contains the head
cropped= image[7:230, 25:235]  # basically we display the pixel in that particulae area
# nuply array takes array input as (height * width)
cv2.imshow("cropped image", cropped)
cv2.waitKey(0)

## Now we perform Masking from center
# Now we perform masking on the original image, Here we would manually provide the pixels
# First we create a rectangle, because faces are suppose to be of rectangle size
# First lets just define a rectangle white in color and mask it in the center
mask=np.zeros(image.shape[:2], dtype="uint8")  # we create a mask with same array size as that of our image
(cx, cy)= (image.shape[1]/2, image.shape[0]/2) # we take the center point
cv2.rectangle(mask, (cx-105,cy-111), (cx+105,cy+111), 255,-1)  # (width(cx) * height(cy)
cv2.imshow("Mask_rectangle", mask)
cv2.waitKey(0)

masked=cv2.bitwise_and(image, image,mask=mask)
cv2.imshow("Masked_from_center", masked)
cv2.waitKey(0)

## Now we perform Masking of face
# Now we will shift the center and extract the face
mask=np.zeros(image.shape[:2], dtype="uint8")  # we create a mask with same array size as that of our image
(cx, cy)= (105, 111) # we take the center point
cv2.rectangle(mask, (cx-(105-25),cy-(111-7)), (cx+(235-105),cy+(230-111)), 255,-1) # (width(cx) * height(cy)
cv2.imshow("Mask_rectangle", mask)
cv2.waitKey(0)

masked=cv2.bitwise_and(image, image,mask=mask)
cv2.imshow("Masked_from_center", masked)
cv2.waitKey(0)

cv2.destroyAllWindows()


#!/usr/bin/env python3
import os,sys,re,yaml,subprocess,pickle
import os.path as osp
import numpy as np
from easydict import EasyDict as edict

trainCommandTemplate = "./tools/train_net.py --iters {} --imdb mnist-{}-default --cfg ./experiments/cfgs/cls_mnist_al.yml --solver ./models/mnist/{}/solver.prototxt --solver_state ./mnist_al_net_{}_iter_{}.solverstate --new_path_to_imageset {} --snapshot_infix {}"
testCommandTemplate = "./tools/test_net.py --imdb mnist-test-default --cfg ./experiments/cfgs/cls_mnist.yml --def ./models/mnist/{}/test.prototxt --net ./output/classification/mnist/mnist_al_net_{}_{}_iter_{}.caffemodel"
trainOriginalCommandTemplate = "./tools/train_net.py --iters {} --imdb mnist-train-default --cfg ./experiments/cfgs/cls_mnist_al.yml --solver ./models/mnist/{}/solver.prototxt"
testOriginalModelCommandTemplate = "./tools/test_net.py --imdb mnist-test-default --cfg ./experiments/cfgs/cls_mnist.yml --def ./models/mnist/{}/test.prototxt --net {}"

def dbprint(item):
    print("DEBUG_PRINT")
    print(item)

def getAlInfo(fn):
    with open(fn, 'r') as f:
        yaml_cfg = edict(yaml.load(f))
    return yaml_cfg

def assignAccuracy(alIds,alSet,acc):
    alSubset = getAlSubsetInfo(alSet)
    for alId in alSubset:
        if alIds[alId] is None: alIds[alId] = []
        alIds[alId].append(acc)

def getIdList(imageSetPath):
    with open(imageSetPath,"r") as f:
        lines = f.readlines()
    ids = [int(line.strip()) for line in lines]
    return ids

def runCommandProcess(setCommand):
    modelProc = subprocess.Popen(setCommand.split(' '),stdout=subprocess.PIPE)
    output_b,isSuccess = modelProc.communicate()
    assert isSuccess is None, print("ERROR; command failed.")
    output = output_b.decode('utf-8')
    return output

def testOriginalModel(netPath,modelType):
    setTestCommand = testOriginalModelCommandTemplate.format(modelType,netPath)
    return testModel(setTestCommand)

def trainOriginalModel(nIters,modelType):
    setTrainCommand = trainOriginalCommandTemplate.format(nIters,modelType)
    output = trainModel(setTrainCommand)
    return findCaffemodelSnapshot(output)

def trainModel(setTrainCommand):
    print(setTrainCommand)
    return runCommandProcess(setTrainCommand)

def findCaffemodelSnapshot(output):
    findName = r"Wrote snapshot to: (?P<name>.*)"
    trainedModelName = re.findall(findName,output)[-1]
    return trainedModelName

def testModel(setTestCommand):
    print(setTestCommand)
    output = runCommandProcess(setTestCommand)
    findAcc = r"Accuracy: (?P<acc>[0-9]+\.[0-9]+)"
    acc = float(re.findall(findAcc,output)[0])
    return acc

def trainAlModel(alSet,nIters,originalIters,alSetDir,modelType):
    setTrainCommand = trainCommandTemplate.format(nIters,alSet,modelType,\
                                                  modelType,originalIters,alSetDir,alSet)
    output = trainModel(setTrainCommand)
    print("trainAlModel")
    print(output)
    return None

def testAlModel(alSet,nIters,modelType):
    setTestCommand = testCommandTemplate.format(modelType,modelType,alSet,nIters)
    return testModel(setTestCommand)

def getAlSubsetInfo(alSet):
    alSetFn = alSet + ".txt"
    alSetPath = osp.join(alSetDir,alSetFn)
    alSubset = getIdList(alSetPath)
    return alSubset

def saveStatePickle(saveALResultsFn,alIds,originalAcc,subsetIndex,cover):
    stateInfo = {"subsetIndex":subsetIndex,"cover":cover}
    saveALResultsFnPkl = saveALResultsFn+".pkl"
    print("saving AL state information to {}".format(saveALResultsFnPkl))
    with open(saveALResultsFnPkl, 'wb') as f:
        pickle.dump({'alIds':alIds,'originalAcc':originalAcc,'stateInfo':stateInfo}, f)

def restoreStatePickle(saveALResultsFn):
    saveALResultsFnPkl = saveALResultsFn+".pkl"
    with open(saveALResultsFnPkl, 'rb') as f:
        stateInfo = pickle.load(f)
    return stateInfo['alIds'],stateInfo['originalAcc'],stateInfo['stateInfo']

def savePickleFile(saveALResultsFn,alIds,originalAcc):
    saveALResultsFnPkl = saveALResultsFn+".pkl"
    print("saving raw AL results to {}".format(saveALResultsFnPkl))
    with open(saveALResultsFnPkl, 'wb') as f:
        pickle.dump({'alIds':alIds,'originalAcc':originalAcc}, f)

def saveCsvFile(saveALResultsFn,alIds,originalAcc):
    saveALResultsFnCsv = saveALResultsFn+".csv"
    print("saving summary stat AL results to {}".format(saveALResultsFnCsv))
    f = open(saveALResultsFnCsv,"w+")
    f.write("image_index,mean_acc,std_acc\n")
    for image_index,acc_list in alIds.items():
        mean = ""
        std = ""
        if acc_list is None:
            pass
            #print("warning: we actually have a none; very unlikely to occur")
        else:
            percent_diff = computePercentDifference(acc_list,originalAcc)
            print("% difference")
            print(percent_diff)
            print(acc_list)
            print(originalAcc)
            mean = np.mean(percent_diff)
            std = np.std(percent_diff)
        f.write("{},{},{}\n".format(image_index,mean,std))
    f.close()

def computePercentDifference(accuracy_list,originalAccuracy):
    acc = np.array(accuracy_list)
    oa = originalAccuracy
    return (acc - oa) / ((acc + oa) / 2.)

def handleTrainedModel(caffemodelPath):
    """
    the caffemodel needs to be copied to the solverstate dir &
    the solverstate needs to be copied to the local dir
    """
    cmDir = '/'.join(caffemodelPath.split("/")[:-1])
    trainModelNetNameBase = caffemodelPath.split("/")[-1].split(".")[0]
    ss = trainModelNetNameBase+".solverstate"
    cm = trainModelNetNameBase+".caffemodel"
    # copySolverstateDest = osp.join(cmDir,ss)
    copyCaffemodelDest = osp.join(cmDir,ss)
    copyCommand = "cp {} {}".format(caffemodelPath,copyCaffemodelDest)
    print(runCommandProcess(copyCommand))
    return ss

if __name__ == "__main__":

    # arguments
    oIters = 6000
    alInfoFn = "/home/gauenk/Documents/data/mnist/ImageSets/AL_single/alSet_info.txt"
    modelType = "lenet5"
    nPassesofAlSubsets = 1
    saveMod = 300
    restoreState = False

    #-----------------------
    # CREATE ORIGINAL MODEL
    #-----------------------

    # train original model
    # caffemodel = trainOriginalModel(oIters,modelType)
    # solverstate = handleTrainedModel(caffemodel)
    caffemodel = "./output/classification/mnist/mnist_al_net_lenet5_iter_6000.caffemodel"
    solverstate = "./mnist_al_net_lenet5_iter_6000.solverstate"

    # test original model
    originalAcc = testOriginalModel(caffemodel,modelType)
    print(originalAcc)

    #-------------------
    # ACTIVE LEARNING
    #-------------------

    # set al arguments from generating the al subsets
    alInfo = getAlInfo(alInfoFn)
    alSetOrigin = alInfo['alSetOrigin']
    alSetDir = alInfo['alSetDir']
    nIters = alInfo['subsetSize'] * nPassesofAlSubsets + oIters

    # get of ALL image indicies available for active learning
    alIds = dict.fromkeys(getIdList(alSetOrigin),None)

    # save active learning information
    saveALResultsFn = "resultsAL_{}_{}_{}_{}".format(nIters,
                                                     alInfo['valSize'],
                                                     alInfo['subsetSize'],
                                                     alInfo['numberOfCovers'])
    # load state information
    start_cover,start_subset = 0,0
    if restoreState:
        alIds,originalAcc,stateInfo = restoreStatePickle(saveALResultsFn)
        start_cover,start_subset = stateInfo['cover'],stateInfo['subsetIndex']

    # run the AL experiments
    for cover in range(start_cover,alInfo['numberOfCovers']):
        for subsetIndex in range(start_subset,alInfo['subsetsInCover']):
            alSet = "alSet_{}_{}_{}_{}".format(subsetIndex,cover,
                                               alInfo['subsetSize'],
                                               alInfo['valSize'])
            trainAlModel(alSet,nIters,oIters,alSetDir,modelType) # train the new model
            acc = testAlModel(alSet,nIters,modelType) # test the new model
            assignAccuracy(alIds,alSet,acc) # aggregate the accuracy
            # save the current state information
            if subsetIndex % saveMod == 0:
                saveStatePickle(saveALResultsFn,alIds,originalAcc,subsetIndex,cover)

    # save information
    savePickleFile(saveALResultsFn,alIds,originalAcc)
    saveCsvFile(saveALResultsFn,alIds,originalAcc)


import os
import sys, time, random

from zipfile import ZipFile

from PyQt6.QtWidgets import QApplication, QWidget, QFileDialog, QMessageBox, QDialog, QVBoxLayout, QLabel
from PyQt6.QtGui import QIcon
from PyQt6 import uic, QtGui, QtCore, QtWidgets

from threading import Thread
from pathlib import Path

sys.setrecursionlimit(5000)

uifile = 'sudoku.ui'
form, base = uic.loadUiType(uifile)

PATH = 'data'
print(os.path.isdir(PATH))
if not os.path.isdir(PATH):
	with ZipFile('data.zip', 'r') as zip_ref:
		zip_ref.extractall(PATH)

class DifficultyDialog(QDialog):
	def __init__(self):
		super().__init__()

		self.setWindowTitle("DifficultyDialog")
		self.setWindowIcon(QtGui.QIcon('assets/icons/question-mark.png'))

		self.difficulty = 'trivial'

		self.btn_trivial = QtWidgets.QPushButton("Trivial")
		self.btn_easy = QtWidgets.QPushButton("Easy")
		self.btn_medium = QtWidgets.QPushButton("Medium")
		self.btn_hard = QtWidgets.QPushButton("Hard")

		self.layout = QVBoxLayout()
		message = QLabel("Select a difficulty : ")
		self.layout.addWidget(message)
		self.layout.addWidget(self.btn_trivial)
		self.layout.addWidget(self.btn_easy)
		self.layout.addWidget(self.btn_medium)
		self.layout.addWidget(self.btn_hard)

		self.setLayout(self.layout)

		self.btn_trivial.clicked.connect(self.trivial)
		self.btn_easy.clicked.connect(self.easy)
		self.btn_medium.clicked.connect(self.medium)
		self.btn_hard.clicked.connect(self.hard)

	def trivial(self) -> None:
		self.close()
		self.difficulty = 'trivial'
	def easy(self) -> None:
		self.close()
		self.difficulty = 'easy'
	def medium(self) -> None:
		self.close()
		self.difficulty = 'medium'
	def hard(self) -> None:
		self.close()
		self.difficulty = 'hard'

class MainWindow(base, form):
	def __init__(self):
		super(base, self).__init__()
		self.setWindowIcon(QtGui.QIcon('assets/icons/sudoku.png'))

		self.PATHS = {
			"trivial": "data/puzzles0_kaggle",
			"easy": "data/puzzles2_17_clue",
			"medium": "data/puzzles3_magictour_top1465",
			"hard": "data/puzzles6_forum_hardest_1106",
		}
		self.DIFFICULTY = 'easy'

		self.board = None

		self.setupUi(self)
		self.setWindowTitle("Sudoku")

		self.setFixedHeight(500)
		self.setFixedWidth(440)

		self.actionNew_game.triggered.connect(self.resetBoard)

		self.actionSolve.triggered.connect(self.solveSudokuThread)
		self.actionShow_Backtracking_Slow.triggered.connect(self.solveSudokuThreadSlow)

		self.actionClear.triggered.connect(self.clearSudokuThread)
		self.actionAbout.triggered.connect(self.aboutSection)

		self.pushButtonReset.clicked.connect(self.resetSudokuThread)
		self.pushButtonCheck.clicked.connect(self.checkSudoku)

		self.createGame()

	def fixBox(self, i, j, index):
		getattr(self, f"comboBox_{i}_{j}").setCurrentIndex(index)
		getattr(self, f"comboBox_{i}_{j}").setEnabled(False)

	def enableBox(self, i, j):
		getattr(self, f"comboBox_{i}_{j}").setEnabled(True)

	def setBoxData(self, i, j, data):
		getattr(self, f"comboBox_{i}_{j}").setCurrentIndex(data)

	def getBoxDataIndex(self, i, j) -> int:
		return getattr(self, f"comboBox_{i}_{j}").currentIndex()

	def isBoxEnabled(self, i, j) -> bool:
		return getattr(self, f"comboBox_{i}_{j}").isEditable()

	def reset(self):
		self.clear()
		self.createSudoku(self.board)

	def clear(self):
		for i in range(9):
			for j in range(9):
				self.setBoxData(i, j, 0)
				self.enableBox(i, j)

	def solve(self, ms = 0) -> bool:
		for i in range(9):
			for j in range(9):
				if self.getBoxDataIndex(i, j) == 0:
					for val in range(1, 10):

						time.sleep(ms)
						if self.check(i, j, val):
							self.setBoxData(i, j, val)
							if self.solve(ms):
								return True
							self.setBoxData(i, j, 0)
					return False
		return True

	def check(self, i, j, val):
		if val == 0:
			return False

		for ii in range(9):
			if self.getBoxDataIndex(i, ii) == val:
				return False
			if self.getBoxDataIndex(ii, j) == val:
				return False
		x, y = i - i%3, j - j%3
		for ii in range(3):
			for jj in range(3):
				if self.getBoxDataIndex(x + ii, y + jj) == val:
					return False
		return True

	def showMessageBox(self, title, message):
		dlg = QMessageBox(self)
		dlg.setWindowTitle(title)
		dlg.setText(message)
		button = dlg.exec()

	def showDifficultySettings(self):
		diff = DifficultyDialog()
		diff.exec()
		if self.DIFFICULTY != diff.difficulty:
			self.DIFFICULTY = diff.difficulty
			self.boards = self.getSudokuData(self.PATHS[self.DIFFICULTY])
		self.boards = self.getSudokuData(self.PATHS[self.DIFFICULTY])

	def checkSudoku(self):
		solved = True
		for i in range(9):
			for j in range(9):

				val = self.getBoxDataIndex(i, j)
				self.setBoxData(i, j, 0)

				if not self.check(i, j, val):
					solved = False
					break

				self.setBoxData(i, j, val)

		if not solved:
			self.showMessageBox("Sudoku not solved", "Sudoku is either incomplete/incorrect !")
		else:
			self.showMessageBox("Congratulations !", "You have solved it correctly !")
			self.createGame()

	def solveSudoku(self, ms=0):
		self.pushButtonReset.setEnabled(False)
		self.actionClear.setEnabled(False)
		self.actionSolve.setEnabled(False)
		self.actionNew_game.setEnabled(False)
		self.actionShow_Backtracking_Slow.setEnabled(False)

		solvable = self.solve(ms)

		self.pushButtonReset.setEnabled(True)
		self.actionClear.setEnabled(True)
		self.actionSolve.setEnabled(True)
		self.actionShow_Backtracking_Slow.setEnabled(True)
		self.actionNew_game.setEnabled(True)

	def solveSudokuThread(self):
		self.setFocus()
		self.pushButtonReset.setEnabled(False)
		thread = Thread(target=self.solveSudoku)
		thread.start()

	def solveSudokuThreadSlow(self):
		self.setFocus()
		self.pushButtonReset.setEnabled(False)
		thread = Thread(target=self.solveSudoku, args = [0.001])
		thread.start()

	def clearSudokuThread(self):
		self.setFocus()
		thread = Thread(target=self.clear)
		thread.start()

	def resetSudokuThread(self):
		self.setFocus()
		thread = Thread(target=self.reset)
		thread.start()

	def resetBoard(self):
		self.createGame()

	def aboutSection(self):
		self.showMessageBox("About", """
This is a sudoku app made in Python using PyQt6.
(Currently under development)

Creator : Utkarsh Sharma
									 """)

	def createSudoku(self, board):
		if board is None: return None
		self.board = board
		dataList = self.toDataList(board)
		for row, col, val in dataList:
			self.fixBox(row, col, val)

	def toDataList(self, board):
		data = []
		for i in range(9):
			for j in range(9):
				if board[i][j] != ".":
					data.append( (i, j, int(board[i][j])) )
		return data

	def getSudokuData(self, path):
		sdkList = []
		with open(path) as f:
			for line in f:
				mat = [['.' for i in range(9)] for i in range(9)]
				line = line.replace('/n', '')
				if len(line)==82:
					for i in range(81):
						mat[i//9][i%9] = line[i]
				sdkList.append(mat)
		return sdkList

	def createGame(self):
		self.showDifficultySettings()
		self.clear()
		index = random.randint(0, len(self.boards))
		self.board = self.boards[index]
		self.createSudoku(self.board)

def main():
	app = QApplication(sys.argv)

	window = MainWindow()
	window.show()
	sys.exit(app.exec())

if __name__ == '__main__':
	main()


import os

import mock

from django.test import TestCase
from django.test import override_settings
from pyfakefs.fake_filesystem_unittest import TestCase as FSTestCase

from mii_common import tools
from mii_indexer.factories import MovieRelationFactory, MovieTaggingFactory
from mii_interface.factories import ReportFactory
from mii_interface.views import discrepancies
from mii_rating.models import QuestionAnswer, MovieQuestionSet
from mii_sorter.factories import EpisodeFactory, MovieFactory, SerieFactory, SeasonFactory
from mii_sorter.models import Movie

@override_settings(DESTINATION_FOLDER='/home/destination')
class TestViews(TestCase):
    @classmethod
    def setUpTestData(cls):
        MovieTaggingFactory.create_batch(3)
        MovieRelationFactory.create_batch(5)
        EpisodeFactory.create_batch(6)
        ReportFactory.create_batch(5)
        cls.episode = EpisodeFactory.create()
        cls.report = ReportFactory.create()

    def test_index(self):
        assert self.client.get('/').status_code == 200

    def test_movies(self):
        assert self.client.get('/movies').status_code == 200

    def test_series(self):
        assert self.client.get('/series').status_code == 200

    def test_rate(self):
        assert self.client.get('/rate').status_code == 200

    def test_reports(self):
        assert self.client.get('/reports').status_code == 200

    def test_report(self):
        assert self.client.get('/report/%s/' %
                               self.report.id).status_code == 200

    def test_report_missing(self):
        assert self.client.get('/report/39202390/').status_code == 200

    @mock.patch('mii_unpacker.views.unpack')
    def test_rpc_unpack(self, unpack):
        response = self.client.get('/rpc/unpack')
        assert response.status_code == 200
        assert unpack.delay.called

    @mock.patch('mii_sorter.views.sort')
    def test_rpc_sort(self, sort):
        response = self.client.get('/rpc/sort')
        assert response.status_code == 200
        assert sort.delay.called

    @mock.patch('mii_indexer.views.index_movies')
    def test_rpc_index(self, index):
        response = self.client.get('/rpc/index')
        assert response.status_code == 200
        assert index.delay.called

    @mock.patch('mii_rss.views.recheck_feed_and_download_torrents')
    def test_rpc_rerss(self, index):
        response = self.client.get('/rpc/recheck_rss')
        assert response.status_code == 200
        assert index.delay.called

    @mock.patch('mii_rss.views.check_feed_and_download_torrents')
    def test_rpc_rss(self, rss):
        response = self.client.get('/rpc/rss')
        assert response.status_code == 200
        assert rss.delay.called

    @mock.patch('mii_interface.views.index_movies')
    @mock.patch('mii_interface.views.unpack')
    @mock.patch('mii_interface.views.sort')
    def test_rpc_index(self, sort, unpack, index):
        response = self.client.get('/rpc/unpack_sort_index')
        assert response.status_code == 200
        assert index.si.called
        assert unpack.si.called
        assert sort.si.called

    def test_mii_rating(self):
        movie = MovieFactory.create(seen=None)
        data = {
            'action': 'not_seen',
            'movie_id': movie.id
        }
        response = self.client.post('/rate', data=data)
        assert Movie.objects.get(id=movie.id).seen is False
        assert response.status_code == 200

    @mock.patch('mii_interface.views.remote_play')
    def test_play(self, remote_play):
        self.client.post('/play', data={'episode_id': self.episode.id})
        remote_play.assert_called_with(self.episode.file_path)

@override_settings(DESTINATION_FOLDER='/home/destination')
class TestMiiRating(TestCase):
    def test_mii_rating_save_question(self):
        movie = MovieFactory.create(seen=None)
        data = {
            'action': 'save_movie',
            'movie_id': movie.id,
            'overall': ['9.4'],
        }
        response = self.client.post('/rate', data=data)
        assert MovieQuestionSet.objects.get(movie_id=movie.id)
        assert QuestionAnswer.objects.get(question_type='overall')
        assert response.status_code == 200

    def test_mii_rating_missing_movie(self):
        response = self.client.get('/rate?movie_id=1')
        assert response.status_code == 200

    def test_rate_with_info(self):
        movie = MovieFactory.create(seen=None)
        MovieRelationFactory.create_batch(5, movie=movie)
        MovieTaggingFactory.create_batch(5, movie=movie)
        response = self.client.get('/rate')
        assert response.status_code == 200

@override_settings(DESTINATION_FOLDER='/processed/')
class TestDiscrepancies(FSTestCase, TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.movie_without_path = MovieFactory.create(
            folder_path='dummy_path.mkv')
        cls.movie_with_path = MovieFactory.create(folder_path='path_exists')
        cls.movie_with_different_path = MovieFactory.create(
            title='Match', year=2000, folder_path='path_exists2')
        cls.serie_with_path = SerieFactory.create(name='Serie2')
        cls.season_with_path = SeasonFactory.create(
            serie=cls.serie_with_path, number=1)
        cls.season_without_path = SeasonFactory.create(
            serie=cls.serie_with_path, number=2)
        cls.episode_with_path = EpisodeFactory(
            season=cls.season_with_path, number=1)
        cls.episode_without_path = EpisodeFactory(
            season=cls.season_with_path, number=2)
        cls.serie_without_path = SerieFactory.create(name='Serie3')

    def setUp(self):
        self.setUpPyfakefs()
        dest_dir = tools.make_dir('/processed/')
        movie_dir = tools.make_dir(os.path.join(dest_dir, 'Movies'))
        serie_dir = tools.make_dir(os.path.join(dest_dir, 'Series'))
        all_movie_dir = tools.make_dir(os.path.join(movie_dir, 'All'))
        self.title_folder = tools.make_dir(
            os.path.join(all_movie_dir, 'Title (2014)'))
        self.match_folder = tools.make_dir(
            os.path.join(all_movie_dir, 'Match (2000)'))
        self.serie1_folder = tools.make_dir(os.path.join(serie_dir, 'Serie1'))
        self.serie1_season1_folder = tools.make_dir(
            os.path.join(self.serie1_folder, 'Season 1'))
        self.serie1_season1_ep1_path = self.fs.create_file(os.path.join(
            self.serie1_season1_folder, 'serie.name.s01e01.mkv'), contents='dummy')
        self.serie2_folder = tools.make_dir(os.path.join(serie_dir, 'Serie2'))
        self.season_with_path.folder_path = tools.make_dir(
            os.path.join(self.serie2_folder, 'Season 1'))
        self.season_with_path.save()
        self.episode_with_path.file_path = os.path.join(
            self.season_with_path.folder_path, str(self.episode_with_path))
        self.episode_with_path.save()
        self.fs.create_file(self.episode_with_path.file_path, contents='dummy')
        tools.make_dir('path_exists')
        tools.make_dir('path_exists2')

    @mock.patch('mii_interface.views.render')
    def test_discrepancies(self, render):
        request = mock.MagicMock()
        discrepancies(request)
        render.assert_called_with(
            request,
            mock.ANY,
            {
                'movie_discrepancy': [
                    {'title': self.movie_without_path.title, 'id': self.movie_without_path.id}],
                'folder_discrepancy': [
                    {'folder_exists': True,
                     'movie_folder_exists': True,
                     'movie_id': self.movie_with_different_path.id,
                     'folder': self.match_folder,
                     'movie_folder': self.movie_with_different_path.folder_path
                     },
                    {
                        'folder': self.title_folder
                    }
                ],
                'series_discrepancy': [{'name': 'Serie3', 'id': 2}],
                'seasons_discrepancy': [{'number': '2', 'id': 2}],
                'episodes_discrepancy': [{'title': 'Episode object (2)', 'file_path': '{destination_dir}serie_13.mkv', 'id': 2}],
                'serie_folder_discrepancy': [
                    {'name': 'Serie1',
                     'folder_path': '/processed/Series/Serie1',
                     'seasons': [{
                         'number': '1',
                         'folder_path': '/processed/Series/Serie1/Season 1',
                         'episodes': [
                             {'number': 1,
                              'file_path': '/processed/Series/Serie1/Season 1/serie.name.s01e01.mkv',
                              'file_size': 5}
                         ]
                     }
                     ]
                     }
                ],
            }
        )

    @mock.patch('mii_interface.views.render')
    def test_discrepancies_fixing(self, render):
        fixing_request = mock.MagicMock()
        fixing_request.method = 'POST'
        discrepancies(fixing_request)
        request = mock.MagicMock()
        request.method = 'GET'
        discrepancies(request)
        render.assert_called_with(
            request,
            mock.ANY,
            {
                'movie_discrepancy': [],
                'folder_discrepancy': [],
                'serie_folder_discrepancy': [],
                'episodes_discrepancy': [],
                'seasons_discrepancy': [],
                'series_discrepancy': [],
            }
        )


from time import time
from requests_html import HTMLSession
from reppy.robots import Robots

domain = 'py4you.com'
home_url = f'https://{domain}/'
robots_url = home_url + 'robots.txt'
robots = Robots.fetch(robots_url)

filename = domain.replace('.', '_') + '.csv'

result_file = open(filename, 'w', encoding='utf-8')
result_file.write('URL\tTITLE\tH1\tRESPONSE_TIME\tLEVEL\n')

parsed_urls, queue_urls = set(), dict()

queue_urls[home_url] = 1

while len(queue_urls) > 0:

    min_level = min(queue_urls.values())

    for key, value in queue_urls.items():
        if value == min_level:
            url = key
            level = queue_urls.pop(url)
            break

    print('Scan: ', url)

    parsed_urls.add(url)

    with HTMLSession() as session:
        t1 = time()
        response = session.get(url)
        t2 = time()
    try:
        title = response.html.xpath('//title')[0].text
    except Exception as e:
        title = 'Not found'

    try:
        h1 = response.html.xpath('//h1')[0].text
    except Exception as e:
        h1 = 'Not found'

    response_time = round(t2 - t1, 2)

    result = f'{url}\t{title}\t{h1}\t{response_time}\t{level}\n'

    result_file.write(result)

    for link in response.html.absolute_links:
        if '#' in link:
            link =link.split('#')[0]
            continue

        if link.endswith('.jpg'):
            continue

        if domain not in link:
            continue

        if link in parsed_urls:
            continue

        if not robots.allowed(link, '*'):
            continue

        if link not in queue_urls:
            queue_urls[link] = level + 1

print('Website is parsed!')


#!/usr/bin/env python3

import random, time

FLAG = "Hero{n0t_s0_r4nd0m_4ft3r_4ll}"
random.seed(time.time())
t = 0

print("Everybody loves a little guessing challenge right ? Guess the number \o/\n\t- Love, Mersenne\n")
while t < 1000:
    rand = random.getrandbits(32)
    try:
        guess = int(input("Guess me : "))
    except ValueError:
        print("I'm a number remember ?")
        exit()
    if rand == guess:
        print("how can you be so lucky... here you go... {}".format(FLAG))
    else:
        print("Nop, it was {} :) But you can try again\n".format(rand))

print("\nI can't take your mediocrity any longer")


import string
#create set with lowercased string
alphabets = set(string.ascii_lowercase)
#input strings
inp = ['How quickly daft jumping zebras vex','How quickly daft jumping zebras']

inp_nospace =[None]*inp.__len__()

for i in range(len(inp)):

    #replace space character (' ') with ('')
    inp_nospace[i] = inp[i].replace(' ', '')

    #check if every letter in the set alphabets is in the set created from input text
    output = ("is not a pangram","is a pangram")[set(inp_nospace[i].lower()) >= alphabets]

    print(inp[i]+' --> '+output)


# -*- coding:utf-8 -*-
from model_7th import *
from Cal_measurement import *
from random import random
from model_profiler import model_profiler
from color_to_gray import *

import matplotlib.pyplot as plt
import numpy as np
import easydict
import os

FLAGS = easydict.EasyDict({"img_height": 256,

                           "img_width": 320,

                           "classes": 8,

                           "batch_size": 6,

                           "epochs": 200,

                           "lr": 0.0001,

                           "tr_img_path": "/yuhwan/yuhwan/Dataset/Segmentation/SUIM/SUIM/Train/images",

                           "tr_lab_path": "/yuhwan/yuhwan/Dataset/Segmentation/SUIM/SUIM/Train/masks",

                           "te_img_path": "/yuhwan/yuhwan/Dataset/Segmentation/SUIM/SUIM/Test/images",

                           "te_lab_path": "/yuhwan/yuhwan/Dataset/Segmentation/SUIM/SUIM/Test/masks_fix",

                           "train": True,

                           "pre_checkpoint": False,

                           "pre_checkpoint_path": "",

                           "save_checkpoint": "/yuhwan/yuhwan/checkpoint/Segmenation/7th_paper/checkpoint",

                           "save_print": "/yuhwan/yuhwan/checkpoint/Segmenation/7th_paper/train_out.txt",

                           "sample_images": "/yuhwan/yuhwan/checkpoint/Segmenation/7th_paper/sample_images"})

optim = tf.keras.optimizers.Adam(FLAGS.lr)
color_map = np.array([[0,0,0], [0,0,255], [0, 255, 0], [0, 255, 255], [255, 0, 0], [255, 0, 255], [255, 255, 0], [255, 255, 255]], np.uint8)
def train_func(img_list, lab_list):

    img = tf.io.read_file(img_list)
    img = tf.image.decode_jpeg(img, 3)
    img = tf.image.resize(img, [FLAGS.img_height, FLAGS.img_width])
    img = tf.image.random_brightness(img, max_delta=50.)
    img = tf.image.random_saturation(img, lower=0.5, upper=1.5)
    img = tf.image.random_hue(img, max_delta=0.2)
    img = tf.image.random_contrast(img, lower=0.5, upper=1.5)
    img = tf.clip_by_value(img, 0, 255)
    # img = tf.image.rgb_to_grayscale(img)
    # img = tf.concat([img, img, img, img, img, img, img, img], -1)
    img = img / 255.

    lab = tf.io.read_file(lab_list)
    lab = tf.image.decode_bmp(lab)
    lab = tf.image.resize(lab, [FLAGS.img_height, FLAGS.img_width], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

    if random() > 0.5:
        img = tf.image.flip_left_right(img)
        lab = tf.image.flip_left_right(lab)

    return img, lab

def test_func(img_list, lab_list):

    img = tf.io.read_file(img_list)
    img = tf.image.decode_jpeg(img, 3)
    img = tf.image.resize(img, [FLAGS.img_height, FLAGS.img_width])
    img = tf.clip_by_value(img, 0, 255)
    # img = tf.image.rgb_to_grayscale(img)
    # img = tf.concat([img, img, img, img, img, img, img, img], -1)
    img = img / 255.

    lab = tf.io.read_file(lab_list)
    lab = tf.image.decode_bmp(lab)
    lab = tf.image.resize(lab, [FLAGS.img_height, FLAGS.img_width], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

    return img, lab

def true_dice_loss(y_true, y_pred):
    y_true = tf.cast(y_true, tf.float32)
    y_pred = tf.math.sigmoid(y_pred)
    numerator = 2 * tf.reduce_sum(y_true * y_pred)
    denominator = tf.reduce_sum(y_true + y_pred)

    return 1 - tf.math.divide(numerator, denominator)

def cal_loss(model, images, labels, imbal):

    with tf.GradientTape() as tape:

        output = model(images, True)
        output = tf.reshape(output, [-1, FLAGS.classes])
        labels = tf.reshape(labels, [-1, FLAGS.classes])

        # 0인지 아닌지, 1인지 아닌지, 2인지 아닌지...,...,...
        predict_0, labels_0 = output[:, 0], labels[:, 0]
        labels_0 = tf.where(labels_0 == 0, 1, 0)
        bin_0 = np.bincount(labels_0.numpy(), minlength=2)
        if bin_0[1] != 0:
            total_loss = true_dice_loss(labels_0, predict_0)
        else:
            total_loss = 0.

        predict_1, labels_1 = output[:, 1], labels[:, 1]
        labels_1 = tf.where(labels_1 == 1, 1, 0)
        bin_1 = np.bincount(labels_1.numpy(), minlength=2)
        if bin_1[1] != 0:
            total_loss += true_dice_loss(labels_1, predict_1)
        else:
            total_loss += 0.

        predict_2, labels_2 = output[:, 2], labels[:, 2]
        labels_2 = tf.where(labels_2 == 2, 1, 0)
        bin_2 = np.bincount(labels_2.numpy(), minlength=2)
        if bin_2[1] != 0:
            total_loss += true_dice_loss(labels_2, predict_2)
        else:
            total_loss += 0.

        predict_3, labels_3 = output[:, 3], labels[:, 3]
        labels_3 = tf.where(labels_3 == 3, 1, 0)
        bin_3 = np.bincount(labels_3.numpy(), minlength=2)
        if bin_3[1] != 0:
            total_loss += true_dice_loss(labels_3, predict_3)
        else:
            total_loss += 0.

        predict_4, labels_4 = output[:, 4], labels[:, 4]
        labels_4 = tf.where(labels_4 == 4, 1, 0)
        bin_4 = np.bincount(labels_4.numpy(), minlength=2)
        if bin_4[1] != 0:
            total_loss += true_dice_loss(labels_4, predict_4)
        else:
            total_loss += 0.

        predict_5, labels_5 = output[:, 5], labels[:, 5]
        labels_5 = tf.where(labels_5 == 5, 1, 0)
        bin_5 = np.bincount(labels_5.numpy(), minlength=2)
        if bin_5[1] != 0:
            total_loss += true_dice_loss(labels_5, predict_5)
        else:
            total_loss += 0.

        predict_6, labels_6 = output[:, 6], labels[:, 6]
        labels_6 = tf.where(labels_6 == 6, 1, 0)
        bin_6 = np.bincount(labels_6.numpy(), minlength=2)
        if bin_6[1] != 0:
            total_loss += true_dice_loss(labels_6, predict_6)
        else:
            total_loss += 0.

        predict_7, labels_7 = output[:, 7], labels[:, 7]
        labels_7 = tf.where(labels_7 == 7, 1, 0)
        bin_7 = np.bincount(labels_7.numpy(), minlength=2)
        if bin_7[1] != 0:
            total_loss += true_dice_loss(labels_7, predict_7)
        else:
            total_loss += 0.

        total_loss += tf.keras.losses.CategoricalCrossentropy(from_logits=True)(labels, output)

        # total_loss = loss_0 + loss_1 + loss_2 + loss_3 \
        #     + loss_4 + loss_5 + loss_6 + loss_7 \
        #     + tf.keras.losses.CategoricalCrossentropy(from_logits=True)(labels, output)

    grads = tape.gradient(total_loss, model.trainable_variables)
    optim.apply_gradients(zip(grads, model.trainable_variables))

    return total_loss

def main():
    model = Fix_model(input_shape=(FLAGS.img_height, FLAGS.img_width, 3), classes=FLAGS.classes)
    prob = model_profiler(model, FLAGS.batch_size)
    model.summary()
    print(prob)

    if FLAGS.pre_checkpoint:
        ckpt = tf.train.Checkpoint(model=model, optim=optim)
        ckpt_manager = tf.train.CheckpointManager(ckpt, FLAGS.pre_checkpoint_path, 5)
        if ckpt_manager.latest_checkpoint:
            ckpt.restore(ckpt_manager.latest_checkpoint)
            print("Restored!!!")

    if FLAGS.train:

        count = 0
        output_text = open(FLAGS.save_print, "w")

        tr_img_data = os.listdir(FLAGS.tr_img_path)
        tr_lab_data = [tr_img_data[i].split('.')[0] + ".bmp" for i in range(len(tr_img_data))]
        tr_img_data = [FLAGS.tr_img_path + "/" + data for data in tr_img_data]
        tr_lab_data = [FLAGS.tr_lab_path + "/" + data for data in tr_lab_data]

        tr_img_data, tr_lab_data = np.array(tr_img_data), np.array(tr_lab_data)

        te_img_data = os.listdir(FLAGS.te_img_path)
        te_lab_data = [te_img_data[i].split('.')[0] + ".bmp" for i in range(len(te_img_data))]
        te_img_data = [FLAGS.te_img_path + "/" + data for data in te_img_data]
        te_lab_data = [FLAGS.te_lab_path + "/" + data for data in te_lab_data]

        te_img_data, te_lab_data = np.array(te_img_data), np.array(te_lab_data)

        # print(tr_img_data[0], tr_lab_data[0])
        # print(te_img_data[0], te_lab_data[0])

        te_gener = tf.data.Dataset.from_tensor_slices((te_img_data, te_lab_data))
        te_gener = te_gener.map(test_func)
        te_gener = te_gener.batch(1)
        te_gener = te_gener.prefetch(tf.data.experimental.AUTOTUNE)

        for epoch in range(FLAGS.epochs):

            tr_gener = tf.data.Dataset.from_tensor_slices((tr_img_data, tr_lab_data))
            tr_gener = tr_gener.shuffle(len(tr_img_data))
            tr_gener = tr_gener.map(train_func)
            tr_gener = tr_gener.batch(FLAGS.batch_size)
            tr_gener = tr_gener.prefetch(tf.data.experimental.AUTOTUNE)

            tr_iter = iter(tr_gener)
            tr_idx = len(tr_img_data) // FLAGS.batch_size
            tr_miou = 0.
            for step in range(tr_idx):
                temp_batch_labels = tf.zeros([FLAGS.batch_size, FLAGS.img_height, FLAGS.img_width], tf.float32)
                batch_images, batch_labels = next(tr_iter)

                labels = tf.where(func22(batch_labels) & func23(batch_labels) & func24(batch_labels), 7, temp_batch_labels)
                labels = tf.where(func19(batch_labels) & func20(batch_labels) & func21(batch_labels), 6, labels)
                labels = tf.where(func16(batch_labels) & func17(batch_labels) & func18(batch_labels), 5, labels)
                labels = tf.where(func13(batch_labels) & func14(batch_labels) & func15(batch_labels), 4, labels)
                labels = tf.where(func10(batch_labels) & func11(batch_labels) & func12(batch_labels), 3, labels)
                labels = tf.where(func7(batch_labels) & func8(batch_labels) & func9(batch_labels), 2, labels)
                labels = tf.where(func4(batch_labels) & func5(batch_labels) & func6(batch_labels), 1, labels)
                labels = tf.where(func1(batch_labels) & func2(batch_labels) & func3(batch_labels), 0, labels)
                labels = tf.cast(labels, tf.uint8)

                class_imbal_labels = labels
                class_imbal_labels_buf = 0.
                for i in range(FLAGS.batch_size):
                    class_imbal_label = class_imbal_labels[i]
                    class_imbal_label = np.reshape(class_imbal_label, [FLAGS.img_height*FLAGS.img_width, ])
                    count_c_i_lab = np.bincount(class_imbal_label, minlength=FLAGS.classes)
                    class_imbal_labels_buf += count_c_i_lab
                class_imbal_labels_buf = (np.max(class_imbal_labels_buf / np.sum(class_imbal_labels_buf)) + 1 - (class_imbal_labels_buf / np.sum(class_imbal_labels_buf)))
                class_imbal_labels_buf = tf.nn.softmax(class_imbal_labels_buf[0:FLAGS.classes]).numpy()

                labels = tf.one_hot(labels, FLAGS.classes)

                loss = cal_loss(model, batch_images, labels, class_imbal_labels_buf)

                if count % 10 == 0:
                    print("Epoch: {} [{}/{}] loss = {}".format(epoch, step+1, tr_idx, loss))

                if count % 100 == 0:

                    output = model(batch_images, False)

                    for i in range(FLAGS.batch_size):
                        predict = tf.nn.softmax(output[i], -1)
                        predict = tf.argmax(predict, -1)
                        predict = tf.cast(predict, tf.int32).numpy()
                        gt_images = tf.argmax(labels[i], -1)
                        gt_images = tf.cast(gt_images, tf.int32).numpy()

                        ground_images = color_map[gt_images]
                        predict_images = color_map[predict]

                        plt.imsave(FLAGS.sample_images + "/" + "{}_{}_predict.png".format(count, i), predict_images)
                        plt.imsave(FLAGS.sample_images + "/" + "{}_{}_gt.png".format(count, i), ground_images)

                temp_out = model(batch_images, False)
                temp_predict = tf.nn.softmax(temp_out, -1)
                temp_predict = tf.argmax(temp_predict, -1)
                temp_predict = tf.cast(temp_predict, tf.int32)
                temp_labels = tf.argmax(labels, -1)
                temp_labels = tf.cast(temp_labels, tf.int32)

                miou_, cm = Measurement(predict=temp_predict,
                                    label=temp_labels,
                                    shape=[FLAGS.batch_size*FLAGS.img_height*FLAGS.img_width, ],
                                    total_classes=FLAGS.classes).MIOU()

                tr_miou += miou_

                count += 1

            print("=================================================================================================================================================")
            print("mIoU (train) = %.4f" % (tr_miou / tr_idx))
            output_text.write("Epoch: ")
            output_text.write(str(epoch))
            output_text.write("===================================================================")
            output_text.write("\n")
            output_text.write("train mIoU: ")
            output_text.write("%.4f" % (tr_miou / tr_idx))
            output_text.write("\n")

            te_iter = iter(te_gener)
            te_idx = len(te_img_data)
            te_miou = 0.
            for j in range(te_idx):
                temp_batch_labels = tf.zeros([FLAGS.batch_size, FLAGS.img_height, FLAGS.img_width], tf.float32)
                test_images, test_labels = next(te_iter)
                labels = tf.where(func22(test_labels) & func23(test_labels) & func24(test_labels), 7, temp_batch_labels)
                labels = tf.where(func19(test_labels) & func20(test_labels) & func21(test_labels), 6, labels)
                labels = tf.where(func16(test_labels) & func17(test_labels) & func18(test_labels), 5, labels)
                labels = tf.where(func13(test_labels) & func14(test_labels) & func15(test_labels), 4, labels)
                labels = tf.where(func10(test_labels) & func11(test_labels) & func12(test_labels), 3, labels)
                labels = tf.where(func7(test_labels) & func8(test_labels) & func9(test_labels), 2, labels)
                labels = tf.where(func4(test_labels) & func5(test_labels) & func6(test_labels), 1, labels)
                labels = tf.where(func1(test_labels) & func2(test_labels) & func3(test_labels), 0, labels)
                labels = tf.cast(labels, tf.int32)

                outputs = model(test_images, False)
                output = tf.nn.softmax(outputs[0], -1)
                output = tf.argmax(output, -1)
                output = tf.cast(output, tf.int32)
                label = labels[0]

                miou_, cm = Measurement(predict=output,
                                    label=label,
                                    shape=[FLAGS.img_height*FLAGS.img_width, ],
                                    total_classes=FLAGS.classes).MIOU()
                te_miou += miou_

            print("mIoU (test) = %.4f" % (te_miou / len(te_img_data)))
            print("=================================================================================================================================================")
            output_text.write("test mIoU: ")
            output_text.write("%.4f" % (te_miou / len(te_img_data)))
            output_text.write("\n")
            output_text.write("===================================================================")
            output_text.write("\n")
            output_text.flush()

            model_dir = "%s/%s" % (FLAGS.save_checkpoint, epoch)
            if not os.path.isdir(model_dir):
                print("Make {} folder to store the weight!".format(epoch))
                os.makedirs(model_dir)
            ckpt = tf.train.Checkpoint(model=model, optim=optim)
            ckpt_dir = model_dir + "/7th__model_{}.ckpt".format(epoch)
            ckpt.save(ckpt_dir)

if __name__ == "__main__":
    main()


import datetime
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
DB_PATH = "sqlite:///sochi_athletes.sqlite3"

from datetime import datetime, date, time

# базовый класс моделей таблиц
Base = declarative_base()

class User(Base):
    """
    Описывает структуру таблицы user для хранения регистрационных данных пользователей
    """
    # задаем название таблицы
    __tablename__ = 'user'
    # идентификатор пользователя, первичный ключ
    id = sa.Column(sa.Integer, primary_key=True)
    # имя пользователя
    first_name = sa.Column(sa.Text)
    # фамилия пользователя
    last_name = sa.Column(sa.Text)
    # пол атлета
    gender = sa.Column(sa.Text)
    # адрес электронной почты пользователя
    email = sa.Column(sa.Text)
    # дата рождения
    birthdate = sa.Column(sa.Text)
    # рост
    height = sa.Column(sa.REAL)

class Athelete(Base):
    """
    Описывает структуру таблицы user для хранения регистрационных данных пользователей ("id" integer primary key autoincrement, "age" integer,"birthdate" text,"gender" text,"height" real,"name" text,"weight" integer,"gold_medals" integer,"silver_medals" integer,"bronze_medals" integer,"total_medals" integer,"sport" text,"country" text)
    """
    # задаем название таблицы
    __tablename__ = 'athelete'
    # идентификатор пользователя, первичный ключ
    id = sa.Column(sa.Integer, primary_key=True)
    # возраст атлета
    age = sa.Column(sa.Integer)
    # дата рождения
    birthdate = sa.Column(sa.Text)
    # пол атлета
    gender = sa.Column(sa.Text)
    # рост
    height = sa.Column(sa.REAL)
    # вес
    # имя пользователя
    name = sa.Column(sa.Text)
    weight = sa.Column(sa.Integer)
    # кол-во медалей
    gold_medals = sa.Column(sa.Integer)
    silver_medals = sa.Column(sa.Integer)
    bronze_medals = sa.Column(sa.Integer)
    total_medals = sa.Column(sa.Integer)
    # вид спорта
    sport = sa.Column(sa.Text)
    # страна
    country = sa.Column(sa.Text)

def connect_db():
    """
    Устанавливает соединение к базе данных, создает таблицы, если их еще нет и возвращает объект сессии
    """
    # создаем соединение к базе данных
    engine = sa.create_engine(DB_PATH)
    # создаем описанные таблицы
    Base.metadata.create_all(engine)
    # создаем фабрику сессию
    session = sessionmaker(engine)
    # возвращаем сессию
    return session()

def is_date(date):
    '''
    # Функция принимает на вход строку с датой
    # Возвращает True, еслли дата корректна, False если не корректна
    # Для примера возьмём дату "1995-04-30"
    '''
    # Разбиваем дату разделителем "-", фукнция split возвращает список
    # Если дата корректна, список будет вида ["YYYY","MM","DD"]
    date_splitted = date.split("-")
    # Для начала, длина списка должна быть равной 3
    if len(date_splitted) == 3:
        # Теперь запишем в переменные year, month, day элементы списка date_splitted
        year, month, day = date_splitted
        # Теперь просто проверим, что строка с годом имеет длину 4, строки с месяцем и днём длину 2
        if len(year) == 4 and len(month) == 2 and len(day)==2 and int(month) < 13 and int(month) > 0 and int(day) > 0 and int(day) < 32:
            # Дата нам подходит
            return True
        else:
            # Иначе дата нам не подходит
            return False
    else:
        # Иначе дата не подходит
        return False

def request_data():
    """
    Запрашивает у пользователя данные и добавляет их в список users
    """
    # выводим приветствие
    print("Привет! Введите данные:")
    # запрашиваем у пользователя данные
    first_name = input("Введи имя: ")
    last_name = input("Введи фамилию: ")
    gender = input("Введите пол: ")
    email = input("Введите адрес электронной почты: ")
    # дата рождения
    while True:
        birthdate = input("Введите дату рождения в формате YYYY-MM-DD: ")
        if is_date(birthdate) == True:
            break
        else:
            print("Неверный формат даты")
    # рост
    height = input("Введите рост в формате М.СС: ")
    # создаем нового пользователя
    user = User(
        first_name=first_name,
        last_name=last_name,
        gender=gender,
        email=email,
        birthdate=birthdate,
        height=height
    )
    # возвращаем созданного пользователя
    return user

def find_user(user_id):
    # Создадим сессию
    session = connect_db()
    # Сделаем запрос
    user = session.query(User).filter(User.id == user_id).first()
    # Закроем сессию
    session.close()
    # Возращаем пользователя
    return user

def find_by_height(user_height):
# Функция, которая принимает как аргумент рост юзера
# Возвращает атлета с минимальной разницей в росте с юзером
    # Создадим сессию
    session = connect_db()
    # отфильтруем атлетов с ростом
    atheletes = session.query(Athelete).filter(Athelete.height > 0).all()
    # Закроем сессию
    session.close()
    # Переменная candidate - это атлет-кандидатю. У кандидата минимальная разница в росте с юзером
    # Пусть сначала кандидатом будет первый атлет
    candidate = atheletes[0]

    # В цикле пройдем по атлетам
    for athelete in atheletes:
        # Посчитаем разницу роста кандидата и юзера
        candidate_diff = abs(candidate.height - user_height)
        # Теперь посчитаем разницу роста для текущего атлета и юзера
        athelete_diff = abs(athelete.height - user_height)

        # Если у текущего атлета разница в росте меньше чем у кандидата
        if athelete_diff < candidate_diff:
            # То теперь этот атлет и есть новый кандидат
            candidate = athelete

    # Теперь когда мы просмотрели всех атлетов
    # У кандидата минимальная разница в росте с юзером
    # Поэтому вернем его
    return candidate

# Функция, которая возвращает разность дат
def date_diff(date_1, date_2):
    # Преобразуем строку date_1 в объект datetime
    datetime_1 = datetime.strptime(date_1, "%Y-%m-%d")
    # Аналогично для второй даты
    datetime_2 = datetime.strptime(date_2, "%Y-%m-%d")
    # Считаем модуль разности дат
    diff = abs(datetime_1-datetime_2)
    # Возращаем разность
    return diff

def find_by_date(user_birthdate):
# Поиск по дате рождения
    session = connect_db()
    atheletes1 = session.query(Athelete).all()
    session.close()
    candidate1 = atheletes1[0]

    # В цикле пройдем по атлетам
    for athelete1 in atheletes1:
        diff_age = date_diff(candidate1.birthdate, user_birthdate)
        diff_age_a = date_diff(athelete1.birthdate, user_birthdate)
        if diff_age > diff_age_a:
            candidate1 = athelete1
    return candidate1

def main():
    session = connect_db()
    mode = input("Выбери режим.\n1 - ввести нового пользователя\n2 - поиск в базе атлетов по номеру пользователя\n")
    if mode == "2":
        # выбран режим поиска, запускаем его
        user_id = int(input("Введите ID пользователя: "))
        # Находим юзера по ID
        user = find_user(user_id)
        if user_id:
            # Вызовем фукнцию, чтобы найти атлета с минимальной разницей по росту
            athelete_close_height = find_by_height(user.height)
            # Вызовем функцию поиска атлета по дате рождения
            athelete_close_birthdate = find_by_date(user.birthdate)
            # Выведем результат
            print(f'Найден пользователь {user.id}: {user.first_name} {user.last_name}. Рост - {user.height}, дата рождения - {user.birthdate}.')
            print("Ближайший по росту атлет: {} {}".format(athelete_close_height.name, athelete_close_height.height))
            print("Ближайший по дате рождения атлет: {} {}".format(athelete_close_birthdate.name, athelete_close_birthdate.birthdate))
        else:
            print("Пользователь с таким ID не найден")
    elif mode == "1":
        # запрашиваем данные пользоватлея
        user = request_data()
        # добавляем нового пользователя в сессию
        session.add(user)
        # сохраняем все изменения, накопленные в сессии
        session.commit()
        print("Спасибо, данные сохранены!")
    else:
        print("Некорректный режим:(")

if __name__ == "__main__":
    main()


from imgutil.compression.compressor import Compressor
from imgutil.files.manager import ImageDirectory

class FunctionController:
    def __init__(self):
        self.compressor = Compressor()

    def compress(self):
        print("Select a folder (ALL images in ALL subdirectories will be included as well)")
        directory_path = self._get_dir_path()

        print("Do you want to replace the images (Y/N)")
        wants_replace = True if input().lower() == 'y' else False
        # Open a directory selection dialog

        directory = ImageDirectory(directory_path)
        for item in directory.list_all_files():
            self.compressor.compress_img(item, replace=wants_replace)
        print("!!!Operation complete!!!")

    def lower_imgs(self):
        print("Select a folder (ALL images in ALL subdirectories will be included as well)")
        directory_path = self._get_dir_path()
        ImageDirectory(directory_path).lowercase_all_files()
        print("!!!Operation complete!!!")

    def change_idx(self):
        print("Select a folder (ALL images in ALL subdirectories will be included as well)")
        directory_path = self._get_dir_path()
        print("How many 0s do you want in from the the images (MUST be formatted <name>_0...<number>.<extension>")
        x = int(input())
        ImageDirectory(directory_path).reset_all_files_counter(x)
        print("!!!Operation complete!!!")

    @staticmethod
    def _get_dir_path():
        import tkinter as tk
        from tkinter import filedialog
        # Create a Tkinter root window (it won't be shown)
        root = tk.Tk()
        root.withdraw()  # Hide the root window
        return filedialog.askdirectory()


""" Example: plotting Zarrow's non-classical Schottky group.

    Robert Zarrow. "Classical and non-classical Schottky groups". In: Duke Mathematical Journal (42)4, 1975. pp.717--724.'
"""

from bella import cayley
import holoviews as hv
hv.extension('bokeh')
from mpmath import mp
from functools import reduce

class ZarrowGroup(cayley.GroupCache):
    def __init__(self):
        fix_det = lambda M: 1/mp.sqrt(mp.det(M)) * M
        self.S = fix_det(mp.matrix([[99801,0],[59000,5988060]]))
        self.T = fix_det(mp.matrix([[99900,10010000],[1001,99900]]))
        mp.nprint(cayley.simple_tr(self.S))
        mp.nprint(cayley.simple_tr(self.T))
        super().__init__([self.S,self.T])

# Just compute the gasket as the limit set of the μ=2i parabolic Riley group, and save it to an image file.
num_points = 10**6
G = ZarrowGroup()

seed = G.fixed_points((0,1))[0]
df = G.coloured_limit_set_fast(num_points, seed=seed)
scatter = hv.Scatter(df, kdims = ['x'], vdims = ['y','colour'])\
            .opts(marker = "dot", size = 0.1,  color = 'colour', frame_width=800, frame_height=800, data_aspect=1, cmap='Set1')\
              .redim(x=hv.Dimension('x', range=(-200,200)),y=hv.Dimension('y', range=(-200,200)))

#isometric circles of generators
isocircles = [G.isometric_circle(w) for w in G.free_cayley_graph_bfs(1)]
ellipses = [hv.Ellipse(float(centre.real), float(centre.imag), float(radius)*2).opts(color='gray') for (centre, radius) in isocircles] #<-the final parameter of hv.Ellipse is diameter, not radius
scatter = reduce(lambda a,b: a*b, ellipses, scatter)

hv.save(scatter, 'zarrow1.png', fmt='png')
print(f'    zarrow.py (1/2): drew images of {mp.nstr(seed)}')

seed = 100j
df = G.coloured_limit_set_fast(num_points, seed=seed)
scatter = hv.Scatter(df, kdims = ['x'], vdims = ['y','colour'])\
            .opts(marker = "dot", size = 0.1,  color = 'colour', frame_width=800, frame_height=800, data_aspect=1, cmap='Set1')\
              .redim(x=hv.Dimension('x', range=(-200,200)),y=hv.Dimension('y', range=(-200,200)))
hv.save(scatter, 'zarrow2.png', fmt='png')
print(f'    zarrow.py (2/2): drew images of {mp.nstr(seed)}')


from Queues import *
from Account import *
import json
import ConfigParser
import couchdb
import ast
import Pyro4
import random
import logging

__author__ = 'Stian Stroem Anderssen'

class Config:
    """
    This module aims to build and keep track of the manager objet during runtime. Every time a user creation is
    requested. This module is called.
    """
    queue_name = ""
    scriptpath = ""
    interval = 0
    configdbname = ""
    dbserver = ""
    queserver = ""
    configinstance = "None"
    account = Account()
    interpreterserver = Pyro4.Proxy("PYRONAME:interpreter")

    @staticmethod
    def convert_json_to_dictionary(self, data):
        """
        A function to convert JSON contents to a dict.
        :param data:
        :type data:
        :return:
        :rtype:
        """
        jsondict = json.loads(data)[0]
        return jsondict

    @staticmethod
    def request_user_creation(self, configobject):
        """
        Build dictionary containtaining courseinformation that is sent to the interpreter based on the content of the
        config object.
        :param configobject:
        :type configobject:
        :return listtosend:
        :rtype list:
        """
        listtosend = []
        teacherdict = {"teacher": [configobject.get_account().get_teacher()]}
        listtosend.append(teacherdict)
        semesterdict = {"semester": [configobject.get_account().get_semester()]}
        listtosend.append(semesterdict)
        coursedict = {"course": [configobject.get_account().get_course()]}
        listtosend.append(coursedict)
        groupsdict = {}
        groupsandmembers = configobject.get_account().get_groups()
        tempdict = groupsandmembers[0]
        groupsdict.update({"groups": tempdict})
        listtosend.append(groupsdict)
        return listtosend

    @staticmethod
    def send_users_to_queue(self, accountlist):
        """
        OUTDATED- This function converts the accountlist to a string that can be sent over a queue.
        :param accountlist:
        :type accountlist:
        :return:
        :rtype:
        """
        listtostring = accountlist
        queue = Queues()
        queue.create_queue("createuserq", str(listtostring))

    @staticmethod
    def find_groupnames(self, grouplist):
        """
        Return a list of the groupnames
        :param grouplist:
        :type grouplist:
        :return groupnames:
        :rtype:
        """
        groupnames = []
        for i, j in grouplist[0].iteritems():
            groupnames.append(i)
        return groupnames

    def write_config(self, configdata):
        """
        Write config from the database to the config.ini file
        :param configdata:
        :type configdata:
        :return:
        :rtype:
        """
        for key, value in configdata.iteritems():
            self.write_to_file(key, str(value))

    @staticmethod
    def write_to_file(self, key, value):
        """
        Write config to config.ini
        :param key:
        :type key:
        :param value:
        :type value:
        :return:
        :rtype:
        """
        with open('/root/uptime_challenge_master/manager/config.ini', 'r') as f:
            words = f.read().split()
            if key in words:
                logging.info("No need to write to file, key is present")
            else:
                file = open('/root/uptime_challenge_master/manager/config.ini', 'a')
                file.write(key + " = " + value + "\n")

    def read_config_from_file(self):
        """
        Read static config provided in config.ini and build an account object.
        :return configclass:
        :rtype Config:
        """
        config = ConfigParser.SafeConfigParser()
        config.read("/root/uptime_challenge_master/manager/config.ini")
        configclass = Config()
        configclass.set_account(self.account)
        configclass.get_account().set_course(config.get("Account", "course"))
        configclass.get_account().set_groups(json.loads(config.get("Account", "groups")))
        configclass.get_account().set_teacher(config.get("Account", "teacher"))
        configclass.get_account().set_semester(config.get("Account", "semester"))
        configclass.set_config_dbname(config.get("Global", "configdbname"))
        configclass.set_script_path(config.get("Global", "scriptpath"))
        configclass.set_dbserver(config.get("Global", "dbserver"))
        configclass.set_queserver(config.get("Global", "queueserver"))
        return configclass

    def init_db_config(self):
        """
        Build the final config object after the DbConfig is initialized along with the local config
        :return config :
        :rtype Config:
        """
        config = self.initiate_users()
        configdict = self.interpreterserver.fetchConfig(config.get_account().get_teacher())
        # self.write_config(configdict)
        configparser = ConfigParser.SafeConfigParser()
        configparser.read("/root/uptime_challenge_master/manager/config.ini")
        config.set_interval(configparser.get("Global", "interval"))
        config.set_queue_name(configparser.get("Global", "queue_name"))
        return config

    @staticmethod
    def create_user_list(self, usersdictlist):
        userlist = []
        for userdict in usersdictlist:
            userlist.append(userdict["group"])
        return userlist

    def initiate_users(self):
        usersdictlist = self.interpreterserver.getEnabledUsers()
        config = ConfigParser.SafeConfigParser()
        config.read("/root/uptime_challenge_master/manager/config.ini")
        userlist = self.create_user_list(usersdictlist)
        configclass = Config()
        configclass.set_account(self.account)
        configclass.get_account().set_course(config.get("Account", "course"))
        configclass.get_account().set_teacher(config.get("Account", "teacher"))
        configclass.get_account().set_semester(config.get("Account", "semester"))
        configclass.get_account().set_groups(userlist)
        configclass.set_config_dbname(config.get("Global", "configdbname"))
        configclass.set_script_path(config.get("Global", "scriptpath"))
        configclass.set_dbserver(config.get("Global", "dbserver"))
        configclass.set_queserver(config.get("Global", "queueserver"))
        return configclass

    @staticmethod
    def create_work_queue(self, queuename, joblist):
        """
        Function to create a workqueue that is used to put jobs on
        :param queuename:
        :type String:
        :param joblist:
        :type list:
        :return:
        :rtype:
        """
        queue = Queues()
        for group, job in joblist.iteritems():
            for j in job:
                jobdict = {}
                jobdict.update({group: j})
                queue.create_queue(queuename, jobdict)

    def get_queue_name(self):
        """
        Return queue_name
        :return:
        :rtype:
        """
        return self.__queue_name

    def get_interval(self):
        """
        Rrturn interval used in the dbconfig
        :return:
        :rtype:
        """
        return self.__interval

    def set_config_dbname(self, value):
        """
        Set the dbname of the couchdb
        :param value:
        :type value:
        :return:
        :rtype:
        """
        self.configdbname = value

    def get_config_dbname(self):
        """
        Get the dbname of the couchdb.
        :return:
        :rtype:
        """
        return self.configdbname

    def set_queue_name(self, value):
        """
        Set queuename to be used
        :param value:
        :type value:
        :return:
        :rtype:
        """
        self.__queue_name = value

    def get_account(self):
        """
        Return accountobject.
        :return:
        :rtype:
        """
        return self.account

    def set_account(self, a):
        """
        Set accountobject
        :param a:
        :type a:
        :return:
        :rtype:
        """
        self.account = a

    def set_interval(self, value):
        """
        Set interval
        :param value:
        :type value:
        :return:
        :rtype:
        """
        self.__interval = value

    def get_dbserver(self):
        """
        Return dbservername
        :return:
        :rtype:
        """
        return self.__dbserver

    def get_queserver(self):
        """
        Return queueservername
        :return:
        :rtype:
        """
        return self.__queserver

    def set_dbserver(self, value):
        """
        set dbservername
        :param value:
        :type value:
        :return:
        :rtype:
        """
        self.__dbserver = value

    def set_queserver(self, value):
        """
        set queueserver name
        :param value:
        :type value:
        :return:
        :rtype:
        """
        self.__queserver = value

    def set_config_instance(self, value):
        """
        Set the configobject before the database config is applied. This is used instead of using a global variable

        :param value:
        :type value:
        :return:
        :rtype:
        """
        self.configinstance = value

    def get_config_instance(self, value):
        """
        Return the config class object.
        :param value:
        :type value:
        :return:
        :rtype:
        """
        return self.configinstance

    def get_script_path(self):
        """
        Return the path to the executable location of the test on the LeeshoreWorker.
        :return:
        :rtype:
        """
        return self.scriptpath

    def set_script_path(self, path):
        """
        Set the script path on the LeeshoreWorker.
        :param path:
        :type path:
        :return:
        :rtype:
        """
        self.scriptpath = path

    @staticmethod
    def function(self, x):
        return -0.25 * x ** 2 + 50

    @staticmethod
    def derived_off_function(self, x):
        return -0.5 * x

    def return_load(self, x):
        answer = []
        load = self.function(x)
        derivative = self.derived_off_function(x)
        answer.append(float(load))
        answer.append(float(derivative))
        return answer


# Exercise 18: Calculate the sum of three numbers and include an equal comparison.

def sum_num(a,b,c):

    sum = a + b + c

    if a == b and a == c:
        sum *= 3

    return sum

print(sum_num(2,2,2))