"""Tests."""

import pytest
from pyramid.interfaces import IRoutesMapper

@pytest.mark.parametrize('config_fixture, route_count', (
    ('simplerouting_config', 3),
    ('moduled_config', 6),
    ('two_moduled_config', 9),
    ('prefixed_config', 9)
))
def test_read_count(request, config_fixture, route_count):
    """A test to read routes from python package."""
    config = request.getfixturevalue(config_fixture)

    # actually needed to be able tu use getUtility
    config.commit()

    mapper = config.registry.getUtility(IRoutesMapper)
    routes = mapper.get_routes()
    assert routes
    assert len(routes) == route_count

@pytest.mark.parametrize('config_fixture, route_number, name, pattern', (
    ('simplerouting_config', 0, 'index', '/'),
    ('moduled_config', 0, 'module_index', 'module/'),
    ('moduled_config', 3, 'index', '/'),
    ('two_moduled_config', 0, 'module_index', 'module/'),
    ('two_moduled_config', 3, 'second_index', 'second/'),
    ('two_moduled_config', 4, 'second_secret', 'second/secret'),
    ('two_moduled_config', 6, 'index', '/'),
    ('prefixed_config', 0, 'module_index', '{var}/'),
    ('prefixed_config', 1, 'module_secret', '{var}/secret'),
    ('prefixed_config', 3, 'second_index', '{var}/subpath/'),
    ('prefixed_config', 4, 'second_secret', '{var}/subpath/secret'),
))
def test_routename(request, config_fixture, route_number, name, pattern):
    """A test to check whether index is the first route."""
    config = request.getfixturevalue(config_fixture)

    # actually needed to be able tu use getUtility
    config.commit()

    mapper = config.registry.getUtility(IRoutesMapper)
    routes = mapper.get_routes()
    assert routes[route_number].name == name
    assert routes[route_number].pattern == pattern

def test_by_hand_only(clean_config):
    """Run includeme by hand."""
    config = clean_config
    config.commit()
    # pylint:disable=import-outside-toplevel
    from tzf.pyramid_routing import routes_from_package

    routes_from_package(config, 'tests.routes_definitions.routing')

    config.commit()

def test_includeme_and_by_hand(simplerouting_config):
    """config.include and by hand in app."""
    config = simplerouting_config
    config.commit()
    # pylint:disable=import-outside-toplevel
    from tzf.pyramid_routing import routes_from_package

    routes_from_package(config, 'tests.routes_definitions.routing_moduled')

    config.commit()

def test_includeme_and_by_hand_with_includeme_ab(simplerouting_config):
    """config.include in app, and two includeme's in other 'plugin'."""
    config = simplerouting_config
    config.commit()

    config.include('tzf.pyramid_routing')

    config.include('tests.routes_definitions.module_to_include_1')
    config.include('tests.routes_definitions.module_to_include_2')

    config.commit()

def test_includeme_and_by_hand_with_includeme(simplerouting_config):
    """config.include in app, and includeme in other 'plugin'."""
    config = simplerouting_config
    config.commit()

    def includeme_test(config):
        """Configure routes in test."""
        # pylint:disable=import-outside-toplevel
        from tzf.pyramid_routing import routes_from_package
        routes_from_package(config, 'tests.routes_definitions.routing_moduled')

    config.include(includeme_test)
    config.commit()
    config.include('tzf.pyramid_routing')
    config.commit()


from fanstatic import Library, Resource

from implugin.fanstatic.resources import Resources as BaseResources

library = Library('impex', '.')

bootstrap_js = Resource(
    library,
    'bootstrap/js/bootstrap.min.js',
    bottom=True,
)
bootstrap = Resource(
    library,
    'bootstrap/css/bootstrap.min.css',
    depends=[
        bootstrap_js,
    ]
)
custom_js = Resource(
    library,
    'bootstrap/js/custom.js',
    bottom=True,
)
custom = Resource(
    library,
    'bootstrap/css/custom.min.css',
    depends=[
        bootstrap_js,
        custom_js,
    ]
)
datepicker_js = Resource(
    library,
    'bootstrap/js/bootstrap-datepicker.min.js',
    bottom=True,
)
datepicker_locale_pl = Resource(
    library,
    'bootstrap/locales/bootstrap-datepicker.pl.min.js',
    bottom=True,
)
datepicker = Resource(
    library,
    'bootstrap/css/datepicker.min.css',
    depends=[
        datepicker_js,
        datepicker_locale_pl,
    ]
)
timepicker_js = Resource(
    library,
    'bootstrap/js/bootstrap-timepicker.min.js',
    bottom=True,
)
timepicker = Resource(
    library,
    'bootstrap/css/bootstrap-timepicker.min.css',
    depends=[
        timepicker_js,
    ]
)
bracket_css = Resource(
    library,
    'bracket/jquery.bracket.min.css',
)

fontawesome = Resource(
    library,
    'fontawesome/css/font-awesome.min.css',
)

select2_js = Resource(
    library,
    'select2/js/select2.min.js',
    bottom=True,
)

select2_locale_pl = Resource(
    library,
    'select2/js/i18n/pl.js',
    bottom=True,
)

select2 = Resource(
    library,
    'select2/css/select2.min.css',
    depends=[
        select2_js,
        select2_locale_pl,
    ]
)

scoretable = Resource(
    library,
    'js/scoretable.js',
    bottom=True,
)

gamelist = Resource(
    library,
    'js/gamelist.js',
    bottom=True,
)

bracket = Resource(
    library,
    'bracket/jquery.bracket.min.js',
    depends=[
        bracket_css,
    ],
    bottom=True,
)

sliders_css = Resource(
    library,
    'css/sliders.css',
)

ajax_queue = Resource(
    library,
    'js/jquery.ajaxQueue.min.js',
    bottom=True,
)

sliders = Resource(
    library,
    'js/sliders.js',
    depends=[
        sliders_css,
        ajax_queue,
    ],
    bottom=True,
)

class Resources(BaseResources):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.add_resource('home', 'impex.home.resources:home')
        self.add_resource(
            'bootstrap',
            'impex.application.resources:bootstrap',
        )
        self.add_resource(
            'custom',
            'impex.application.resources:custom',
        )
        self.add_resource(
            'datepicker',
            'impex.application.resources:datepicker',
        )
        self.add_resource(
            'fontawesome',
            'impex.application.resources:fontawesome',
        )
        self.add_resource(
            'timepicker',
            'impex.application.resources:timepicker',
        )
        self.add_resource(
            'select2',
            'impex.application.resources:select2',
        )
        self.add_resource(
            'scoretable',
            'impex.application.resources:scoretable',
        )
        self.add_resource(
            'gamelist',
            'impex.application.resources:gamelist',
        )
        self.add_resource(
            'bracket',
            'impex.application.resources:bracket',
        )
        self.add_resource(
            'sliders',
            'impex.application.resources:sliders',
        )


import unittest
from mock import patch, MagicMock

from cloner.utils import prepare_inject, get_ticket_IDs, \
    get_ticket_IDs_specific

class TestUtils(unittest.TestCase):

    def test_prepare_inject(self):
        """Test that fields are prepared to be injected."""
        input = {
            "PAV": "spam-1.0",
            "keywords": ["bicycle", "repair", "man"],
            "labels": ["Red Leicester", "Tilsit", "Emmental"],
            "assignee": "Mr. Creosote",
            "milestone": "nudge"
        }
        expected = {
            "customfield_11911": [{"value": "spam-1.0"}],
            "customfield_12700": [
                {"value": "bicycle"},
                {"value": "repair"},
                {"value": "man"}
            ],
            "labels": ["Red Leicester", "Tilsit", "Emmental"],
            "assignee": {"name": "Mr. Creosote"},
            "customfield_12000": [{"value": "nudge"}]
        }
        output = prepare_inject(input)
        self.assertEqual(expected, output)

    @patch('cloner.ticket.Ticket.search')
    @patch('cloner.ticket.Ticket._create_requests_session')
    def test_get_ticket_IDs_query_formatting(self, mock_session, mock_search):
        """Test that get_ticket_IDs_specific() properly formats query before
        search.
        """
        mock_session.return_value = MagicMock()
        get_ticket_IDs_specific('spam-1.0')
        mock_search.assert_called_with('project=RCMTEMPL and '
                                       '"Product Affects Version"="spam-1.0"')
        get_ticket_IDs_specific('spam-1.0', keywords=['spam', 'eggs'])
        mock_search.assert_called_with(
            'project=RCMTEMPL and "Product Affects Version"="spam-1.0" and '
            '((issuetype="Sub-task") or (issuetype!="Sub-task" and '
            '"Keyword"="spam" and "Keyword"="eggs"))')

    @patch('cloner.ticket.Ticket.search')
    @patch('cloner.ticket.Ticket._create_requests_session')
    def test_get_tickets_query_formatting(self, mock_session, mock_search):
        """Test that get_ticket_IDs() properly formats query before search."""
        mock_session.return_value = MagicMock()
        get_ticket_IDs(None)
        mock_search.assert_called_with('project=RCMTEMPL')
        get_ticket_IDs(None, pav='spam-1.0')
        mock_search.assert_called_with('project=RCMTEMPL and '
                                       '"Product Affects Version"="spam-1.0"')
        get_ticket_IDs(None, keywords=['spam', 'eggs'])
        mock_search.assert_called_with('project=RCMTEMPL '
                                       'and "Keyword"="spam" '
                                       'and "Keyword"="eggs"')
        get_ticket_IDs(None, pav='spam-1.0',
                       keywords=['spam', 'eggs'])
        mock_search.assert_called_with('project=RCMTEMPL and '
                                       '"Product Affects Version"="spam-1.0"'
                                       ' and "Keyword"="spam"'
                                       ' and "Keyword"="eggs"')

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


"""Advent of Code Day 15

https://adventofcode.com/2020/day/15

COUNTING AND HASHMAPS
"""

from collections import defaultdict

initials = [
    [0, 3, 6],
    [1, 3, 2],
    [2, 1, 3],
    [1, 2, 3],
    [2, 3, 1],
    [3, 2, 1],
    [3, 1, 2],
    [5, 1, 9, 18, 13, 8, 0]
]

# Brute Force.
limit = 30000000  # or 2020 for part 1

for initial in initials:
    print("Initial: ", initial)
    # What number are we on?
    n = 1
    # What did we say last?
    last_spoken = 0
    # When have we said each number before?
    last_speak_map = defaultdict(list)
    while n <= limit:
        # If it's an initial number, speak it.
        if n <= len(initial):
            speak = initial[n - 1]
        else:
            # Work out what to speak based on the last number.
            # Have we said it twice before?
            spoken_history = last_speak_map[last_spoken]
            if len(spoken_history) >= 2:
                speak = spoken_history[-1] - spoken_history[-2]
            else:
                speak = 0
        # Keep track of what we speak
        last_spoken = speak
        last_speak_map[last_spoken].append(n)
        n += 1
    print("Spoke", last_spoken, "at turn", n - 1)
    # Part 1 answer: 376
    # Is there an elegant answer to part 2? Maybe, if
    # so it probably involves finding a loop in the pattern
    # and then exploiting the size of that loop.
    # Part 2 answer: 323780


import subprocess
import threading
import numpy as np
import argparse

def launch_sensor():
    p = subprocess.Popen(["python2", "launch_sensor.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    p.wait()
    print(p.stdout.read())

if __name__ == "__main__":
    parser = argparse.ArgumentParser("SensorLauncher")
    parser.add_argument('--random', type=bool,
                        help='random number of sensors (1 - 10)', default=True)
    parser.add_argument('--n', type=int,
                        help='n simulated sensors', default=-1)
    kwargs = vars(parser.parse_args())
    if kwargs.get("n") > 0:
        count = kwargs.get("n")
    elif kwargs.get("n") < 0 and kwargs.get("random"):
        count = np.random.randint(1, 10)
    else:
        count = 1
    threads = []

    for i in range(count):
        threads.append(threading.Thread(target=launch_sensor))
        threads[-1].start()


from django.utils import dateparse

from django.core import validators
from django.core.exceptions import ValidationError
from django.utils.deconstruct import deconstructible

@deconstructible
class InlineVideoJSONValidator(object):
    '''
    Validates inline JSON for video field.
    '''
    error_messages = {
        'invalid': 'Enter a valid value: { vendor, id, startTime?, endTime?, meta?: { published?, author?, title?, duration? } }',
        'vendor_not_allowed': 'Vendor \'%s\' is not allowed.',
        'invalid_vendor_id': 'Video ID is invalid.',
        'invalid_meta_field': 'Meta \'%s\' field is invalid.',
        'invalid_time': '\'%s\' field is invalid time.',
        'invalid_time_delta': '\'endTime\' must be greater than \'startTime\'.',
    }
    allowed_vendors = None
    vendor_id_validators = []

    def __init__(self, allowed_vendors=None, vendor_id_validators=None):
        if allowed_vendors:
            self.allowed_vendors = allowed_vendors
        if vendor_id_validators:
            self.vendor_id_validators = vendor_id_validators

    def _duration_to_str(self, duration_timedelta):
        if not duration_timedelta.days:
            duration_str = str(duration_timedelta)
        else:
            no_days_duration = dateparse.datetime.timedelta(seconds=duration_timedelta.seconds)
            no_days_duration_str = str(no_days_duration)
            hours, minutes_seconds = no_days_duration_str.split(':', 1)
            duration_str = str(int(hours) + duration_timedelta.days*24) + ':' + minutes_seconds
        return duration_str

    def __call__(self, value):
        if not isinstance(value, dict):
            self._raise_error('invalid')

        #check for valid keys:
        if len(set(value.keys()) - {'vendor', 'id', 'startTime', 'endTime', 'meta',}):
            self._raise_error('invalid')

        #check vendor is allowed:
        video_vendor = value.get('vendor', '')
        if self.allowed_vendors is not None:
            if video_vendor not in self.allowed_vendors:
                self._raise_error('vendor_not_allowed', (video_vendor,))

        #check vendor id:
        vendor_id_validator = self.vendor_id_validators.get(video_vendor)
        video_id = value.get('id', '')
        if vendor_id_validator:
            if not vendor_id_validator(video_id):
                self._raise_error('invalid_vendor_id')

        #check startTime:
        video_start_time = value.get('startTime')
        video_start_timedelta = None
        if video_start_time:
            #validate and clean time string:
            video_start_timedelta = dateparse.parse_duration(video_start_time)
            if video_start_timedelta is None:
                self._raise_error('invalid_time', ('startTime',))
            video_start_time = self._duration_to_str(video_start_timedelta)
            value['startTime'] = video_start_time

        #check endTime:
        video_end_time = value.get('endTime')
        if video_end_time:
            #validate and clean time string:
            video_end_timedelta = dateparse.parse_duration(video_end_time)
            if video_end_timedelta is None:
                self._raise_error('invalid_time', ('endTime',))
            video_end_time = self._duration_to_str(video_end_timedelta)
            value['endTime'] = video_end_time

            #validate timedelta between end and start times:
            if video_start_timedelta and video_end_timedelta:
                if video_end_timedelta <= video_start_timedelta:
                    self._raise_error('invalid_time_delta')

        #check meta, in case exists:
        if 'meta' in value:
            if len(set(value['meta'].keys()) - {'published', 'author', 'title', 'duration',}):
                self._raise_error('invalid')
            if 'published' in value['meta'] and not dateparse.parse_datetime(value['meta']['published']):
                self._raise_error('invalid_meta_field', ('published',))
            for f in ['author', 'title']:
                if f in value['meta'] and (not isinstance(value['meta'][f], basestring) or len(value['meta'][f]) > 255):
                    self._raise_error('invalid_meta_field', (f,))
            if 'duration' in value['meta']:
                video_duration_timedelta = dateparse.parse_duration(value['meta']['duration'])
                if video_duration_timedelta is None:
                    self._raise_error('invalid_meta_field', ('duration',))
                value['meta']['duration'] = self._duration_to_str(video_duration_timedelta)

    def _raise_error(self, error_code, msg_args=tuple()):
        raise ValidationError(self.error_messages[error_code] % msg_args, code=error_code)

@deconstructible
class InlineTeacherFileJSONValidator(object):
    '''
    Validates inline JSON for teacher file field.
    '''
    error_messages = {
        'invalid': 'Enter a valid value: { url, size, name, time }',
        'invalid_field': 'Field \'%s\' is invalid.',
    }

    def __call__(self, value):
        if not isinstance(value, dict):
            self._raise_error('invalid')

        #check all keys exist:
        if set(value.keys()) != {'url', 'size', 'name', 'time',}:
            self._raise_error('invalid')

        #check url:
        try:
            validators.URLValidator()(value['url'])
        except Exception:
            self._raise_error('invalid_field', ('url',))
        if len(value['url']) > 500:
            self._raise_error('invalid_field', ('url',))

        #check size:
        if type(value['size']) is not int or value['size'] < 0:
            self._raise_error('invalid_field', ('size',))

        #check name:
        if not isinstance(value['name'], basestring) or not value['name'] or len(value['name']) > 255:
            self._raise_error('invalid_field', ('name',))

        #check time:
        if not dateparse.parse_datetime(value['time']):
            self._raise_error('invalid_field', ('time',))

    def _raise_error(self, error_code, msg_args=tuple()):
        raise ValidationError(self.error_messages[error_code] % msg_args, code=error_code)

@deconstructible
class InstructablesJSONValidator(object):
    '''
    Validates JSON for instructables field.
    '''
    error_messages = {
        'invalid': 'Enter a valid value: { urlId }',
        'invalid_field': 'Field \'%s\' is invalid.',
    }

    def __call__(self, value):
        if not isinstance(value, dict):
            self._raise_error('invalid')

        #check all keys exist:
        if set(value.keys()) != {'urlId',}:
            self._raise_error('invalid')

        #check urlId (validate as url slug):
        try:
            validators.validate_slug(value['urlId'])
        except ValidationError:
            self._raise_error('invalid_field', ('urlId',))

    def _raise_error(self, error_code, msg_args=tuple()):
        raise ValidationError(self.error_messages[error_code] % msg_args, code=error_code)

@deconstructible
class InstructionJSONValidator(object):
    '''
    Validates inline JSON for video field.
    '''
    error_messages = {
        'invalid': 'Enter a valid values: { description, image, hint, id, order }',
        'invalid_field': 'Field \'%s\' is invalid.',
    }

    def __call__(self, value):
        if not isinstance(value, dict):
            self._raise_error('invalid')

        #check description keys exist:
        if not 'description' in set(value.keys()) or not value.get('description'):
            self._raise_error('invalid')

        #check all keys exist:
        if len(set(value.keys()) - { 'description', 'image', 'hint', 'id', 'order' }) > 0:
            self._raise_error('invalid')

        #check image:
        if value.get('image'):
            try:
                validators.URLValidator()(value['image'])
            except ValidationError:
                self._raise_error('invalid_field', ('image',))
            if len(value['image']) > 500:
                self._raise_error('invalid_field', ('image',))

    def _raise_error(self, error_code, msg_args=tuple()):
        raise ValidationError(self.error_messages[error_code] % msg_args, code=error_code)

@deconstructible
class SeparatorJSONValidator(object):
    '''
    Validates JSON for separator field.
    '''
    error_messages = {
        'invalid': 'Enter a valid value: { before, label }',
        'invalid_field': 'Field \'%s\' is invalid.',
    }

    def __call__(self, value):
        if not isinstance(value, dict):
            self._raise_error('invalid')

        #check all keys exist:
        if set(value.keys()) != {'before', 'label',}:
            self._raise_error('invalid')

        #check before:
        if type(value['before']) is not int or value['before'] < 0:
            self._raise_error('invalid_field', ('before',))

        #check label:
        if not isinstance(value['label'], basestring) or not value['label'] or len(value['label']) > 140:
            self._raise_error('invalid_field', ('label',))

    def _raise_error(self, error_code, msg_args=tuple()):
        raise ValidationError(self.error_messages[error_code] % msg_args, code=error_code)


# Shlomi Ben-Shushan

import matplotlib.pyplot as plt

def watch_data_info(data):
    """
    This function prints useful information about the given data.

    :param data: a tuple of data from ratings.csv and movies_subset.csv files.
    :return: None. Prints information instead.
    """
    for d in data:
        print(d.head())
        print(d.info())
        print(d.describe(include='all').transpose())

def print_data(data):
    """
    This function prints the required information to answer the questions in the
    first part of the exercise.

    :param data: a tuple of data from ratings.csv and movies_subset.csv files.
    :return: None. Prints information instead.
    """
    ratings = data[0]

    # Get initial information.
    n_ratings = len(ratings)
    n_movies = len(ratings['movieId'].unique())
    n_users = len(ratings['userId'].unique())

    # Count ratings for each user and movie in the dataset.
    u_counters, m_counters = {}, {}
    for u, m in zip(ratings['userId'], ratings['movieId']):
        if u in u_counters.keys():
            u_counters[u] += 1
        else:
            u_counters[u] = 1
        if m in m_counters.keys():
            m_counters[m] += 1
        else:
            m_counters[m] = 1

    # Calculate the keys of the minimum and maximum values in each dictionary.
    u_min_k = min(m_counters.keys(), key=lambda k: m_counters[k])
    u_max_k = max(m_counters.keys(), key=lambda k: m_counters[k])
    m_min_k = min(u_counters.keys(), key=lambda k: u_counters[k])
    m_max_k = max(u_counters.keys(), key=lambda k: u_counters[k])

    # Print the required information to answer questions 1, 2 and 3.
    print(f"The number of unique users is {n_users}.")
    print(f"The number of unique movies is {n_movies}.")
    print(f"The number of ratings is {n_ratings}.")
    print(f"The minimum number of ratings that given to a movie is {m_counters[u_min_k]}.")
    print(f"The maximum number of ratings that given to a movie is {m_counters[u_max_k]}.")
    print(f"The minimum number of ratings that a user gave is {u_counters[m_min_k]}.")
    print(f"The maximum number of ratings that a user gave is {u_counters[m_max_k]}.")

def plot_data(data, plot=True):
    """
    This function plots the rating distribution and save it to a file.

    :param data: a tuple of data from ratings.csv and movies_subset.csv files.
    :param plot: a boolean that tells if to plot the graph or not.
    :return: None. Create plot file and show it if needed instead.
    """
    ratings = data[0]['rating'].sort_values(ascending=True)

    # Count votes for each rating.
    counters = {}
    for x in ratings:
        xs = str(x)
        if xs in counters.keys():
            counters[xs] += 1
        else:
            counters[xs] = 0

    # Create a plot and save it as an image in a folder named "plot".
    rates = list(counters.keys())
    count = list(counters.values())
    plt.figure()
    plt.bar(rates, count)
    plt.title("Rating Distribution")
    plt.xlabel("Rating")
    plt.ylabel("Count")
    try:
        plt.savefig("plot/Rating Distribution.png")
    except FileNotFoundError:
        print("Error: plot folder could not be found so the figure was not saved.")

    # Show plot if required.
    if plot:
        plt.show()


"""
All Admin configuration related to drf_user

Author: Himanshu Shankar (https://himanshus.com)
"""
from django.contrib import admin
from django.contrib.auth.admin import Group
from django.contrib.auth.admin import GroupAdmin
from django.contrib.auth.admin import UserAdmin
from django.utils.text import gettext_lazy as _

from .models import AuthTransaction
from .models import OTPValidation
from .models import Role
from .models import User

class DRFUserAdmin(UserAdmin):
    """
    Overrides UserAdmin to show fields name & mobile and remove fields:
    first_name, last_name

    Author: Himanshu Shankar (https://himanshus.com)
    """

    fieldsets = (
        (None, {"fields": ("username", "password")}),
        (_("Personal info"), {"fields": ("name", "profile_image", "email", "mobile")}),
        (
            _("Permissions"),
            {
                "fields": (
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "groups",
                    "user_permissions",
                )
            },
        ),
        (
            _("Important dates"),
            {"fields": ("last_login", "date_joined", "update_date")},
        ),
    )
    add_fieldsets = (
        (
            None,
            {
                "classes": ("wide",),
                "fields": ("username", "email", "mobile", "password1", "password2"),
            },
        ),
    )
    list_display = ("username", "email", "name", "mobile", "is_staff")
    search_fields = ("username", "name", "email", "mobile")
    readonly_fields = ("date_joined", "last_login", "update_date")

class OTPValidationAdmin(admin.ModelAdmin):
    """OTP Validation Admin"""

    list_display = ("destination", "otp", "prop")

class AuthTransactionAdmin(admin.ModelAdmin):
    """AuthTransaction Admin"""

    list_display = ("created_by", "ip_address", "create_date")

    def has_add_permission(self, request):
        """Limits admin to add an object."""

        return False

    def has_change_permission(self, request, obj=None):
        """Limits admin to change an object."""

        return False

    def has_delete_permission(self, request, obj=None):
        """Limits admin to delete an object."""

        return False

# UnRegister default Group & register proxy model Role
# This will also remove additional display of application in admin panel.
# Source: https://stackoverflow.com/a/32445368
admin.site.unregister(Group)
admin.site.register(Role, GroupAdmin)

admin.site.register(User, DRFUserAdmin)
admin.site.register(OTPValidation, OTPValidationAdmin)
admin.site.register(AuthTransaction, AuthTransactionAdmin)


import pandas as pd
import os
import csv
import datetime

def limpieza_datos_retabilidades(resultados_fondos):
    """
    Esta función crea un dataframe con la información de los fondos de inversión como salida. Extrae los diccionarios de rentabilidades por días y años para luego posicionarlos en columnas nuevas.

    Parameters:

        resultados_fondos: pandas.DataFrame: Los resultados de los fondos de inversion de bancolombia.

    Returns:

        pandas.DataFrame: DataFrame limpio con la información de los resultados de los fondos de inversion.
    """

    Totaldf= pd.DataFrame()

    for nit in resultados_fondos:
        df = pd.DataFrame(nit).reset_index(drop=True)
        Totaldf = pd.concat([Totaldf,df],axis=0)

    Totaldf.reset_index(drop=True, inplace=True)

    anual = Totaldf["rentabilidad"].apply(pd.Series).iloc[:,:4].copy()
    anual.dropna(inplace=True)
    anual.reset_index(inplace=True,drop=True)

    diarios = Totaldf["rentabilidad"].apply(pd.Series).iloc[:,4:].reset_index(drop=True).copy()
    diarios.dropna(inplace=True)
    diarios.reset_index(inplace=True,drop=True)

    rentabilidades = pd.concat([anual,diarios],axis=1)
    # rentabilidades

    # limpiar dataset original
    Totaldf.drop(["rentabilidad"],axis=1, inplace=True)

    Totaldf.drop_duplicates(inplace=True)
    Totaldf.reset_index(inplace=True, drop=True)
    Totaldf

    #unificar datasets
    rentabilidades_fondos_clean = pd.concat([Totaldf,rentabilidades],axis=1)

    return rentabilidades_fondos_clean

def act_df_rentabilidades(ruta_lectura,df_renta):

    df_renta = df_renta.query("~fechaCierre.isna()").copy()
    df_renta["fechaCierre"] = pd.to_datetime(df_renta["fechaCierre"].astype(int), format = "%Y%m%d")
    df_renta["fechaCierre"] = df_renta["fechaCierre"].dt.strftime("%Y-%m-%d")

    if os.path.exists(ruta_lectura):

        version1_df = pd.read_csv(ruta_lectura)
        print(version1_df.shape[0])
        full_df_renta = pd.concat([version1_df,df_renta],axis=0)

        full_df_renta.drop_duplicates(subset=["nit","fechaCierre"], inplace = True)
        print(full_df_renta.shape[0])
        return full_df_renta
    else:
        return df_renta

def escribir_resultados(ruta, rutalog, act_df_rent):
    fecha_actual = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    with open(ruta, "r") as archivo:
        lineas_antes = len(list(csv.reader(archivo)))
    act_df_rent.to_csv(ruta, index=False)
    with open(ruta, "r") as archivo:
        lineas_despues = len(list(csv.reader(archivo)))
    lineas_nuevas = lineas_despues - lineas_antes
    with open(rutalog, 'a', newline='') as f:
        writer = csv.writer(f)
        writer.writerow([fecha_actual, lineas_nuevas])


from django.conf.urls import url
from django.urls import path
from django.conf import settings

from job import views

def trigger_error(request):
    division_by_zero = 1 / 0

urlpatterns = [
    # 职位列表
    path("job_list/", views.job_list, name="job_list"),

    # 管理员创建 HR 账号的 页面:
    # path('create_hr_user/', views.create_hr_user, name='create_hr_user'),

    # 职位详情
    url(r'^job_detail/(?P<job_id>\d+)/$', views.job_detail, name='detail'),
    # path('job_detail/<int:job_id>/', views.detail, name='job_detail'),

    # 简历提交
    path('resume/add/', views.ResumeCreateView.as_view(), name='resume-add'),
    # 简历详情
    path('resume/<int:pk>/', views.ResumeDetailView.as_view(), name='resume-detail'),

    # path('sentry-debug/', trigger_error),

    # 首页自动跳转到 职位列表
    url(r"^$", views.job_list, name="name"),
    # path("", views.job_list, name="name"),

]

if settings.DEBUG :
    # 有 XSS 漏洞的视图页面，
    urlpatterns += [url(r'^detail_resume/(?P<resume_id>\d+)/$', views.detail_resume, name='detail_resume'),]


"""invoices URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
from django.contrib.sitemaps.views import sitemap
from .sitemaps import StaticViewSitemap

sitemaps = {
    'static': StaticViewSitemap,
}

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/', include('djoser.urls')),
    path('api/v1/', include('djoser.urls.authtoken')),
    path('api/v1/', include('apps.client.urls')),
    path('api/v1/', include('apps.team.urls')),
    path('api/v1/', include('apps.invoice.urls')),
    path('ads.txt', TemplateView.as_view(template_name='ads.txt', content_type="text/plain"), name='ads'),
    path('robots.txt', TemplateView.as_view(template_name='robots.txt', content_type="text/plain"), name='robots'),
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
    path('', TemplateView.as_view(template_name='application.html'), name='application'),
]


import rdkit.Chem as Chem
from .mol_graph import bond_fdim, bond_features
import numpy as np

BOND_TYPE = ["NOBOND", Chem.rdchem.BondType.SINGLE, Chem.rdchem.BondType.DOUBLE, Chem.rdchem.BondType.TRIPLE, Chem.rdchem.BondType.AROMATIC]
N_BOND_CLASS = len(BOND_TYPE)
binary_fdim = 4 + bond_fdim
INVALID_BOND = -1

def get_bin_feature(r, max_natoms):
    comp = {}
    for i, s in enumerate(r.split('.')):
        mol = Chem.MolFromSmiles(s)
        for atom in mol.GetAtoms():
            comp[atom.GetIntProp('molAtomMapNumber') - 1] = i
    n_comp = len(r.split('.'))
    rmol = Chem.MolFromSmiles(r)
    n_atoms = rmol.GetNumAtoms()
    bond_map = {}
    for bond in rmol.GetBonds():
        a1 = bond.GetBeginAtom().GetIntProp('molAtomMapNumber') - 1
        a2 = bond.GetEndAtom().GetIntProp('molAtomMapNumber') - 1
        bond_map[(a1,a2)] = bond_map[(a2,a1)] = bond

    features = []
    for i in range(max_natoms):
        for j in range(max_natoms):
            f = np.zeros((binary_fdim,))
            if i >= n_atoms or j >= n_atoms or i == j:
                features.append(f)
                continue
            if (i,j) in bond_map:
                bond = bond_map[(i,j)]
                f[1:1+bond_fdim] = bond_features(bond)
            else:
                f[0] = 1.0
            f[-4] = 1.0 if comp[i] != comp[j] else 0.0
            f[-3] = 1.0 if comp[i] == comp[j] else 0.0
            f[-2] = 1.0 if n_comp == 1 else 0.0
            f[-1] = 1.0 if n_comp > 1 else 0.0
            features.append(f)
    return np.vstack(features).reshape((max_natoms,max_natoms,binary_fdim))

def get_all_batch(smiles_list):
    max_natoms = 0
    mol_list = []
    for r in smiles_list:
        rmol = Chem.MolFromSmiles(r)
        mol_list.append(rmol)
        if rmol.GetNumAtoms() > max_natoms:
            max_natoms = rmol.GetNumAtoms()

    validity = np.ones( (len(mol_list), max_natoms, max_natoms) )
    eye = np.eye(max_natoms)
    for i,mol in enumerate(mol_list):
        n = mol.GetNumAtoms()
        validity[i, :, n:] = 0
        validity[i, n:, :] = 0
        validity[i] -= eye

    features = []
    for r in smiles_list:
        features.append(get_bin_feature(r, max_natoms))

    return np.array(features), validity


# -*- coding: utf-8 -*-
from string import Template
import csv
import os
import json
# import nestedlookup
###############################################################################
# El script extrae del archivo entrada.csv las frases y el Expected Json Result
# generando un archivo salida.json, válido para ser ejecutado con terrier upload
# Archivos necesarios.
# arma_upload_test.py, que exista entrada.csv
# En la carpeta source deben estar las plantillas necesarias, acondicionadas para cada dominio, ya que no son todas iguales.
# Los otros archivos son prescindibles, se pueden eliminar
# -----------------------------------------------------------------------------
# carpeta source: contiene las plantillan necesarias
#   En plantilla cabecera se deben colocar los dominios a utilizar
# archivo de entrada = entrada.csv
#   formato csv delimitado por tabuladores
#   Debe contener la primer fila con el nombre de los campos.
#   En el ejemplo está tomado de un kpi del cliente.
#   Nombre de los campos indispensables
#     Query         = Frase a testear (sin comillas)
#     Expected Json Result = Resultado esperado (debe contener las llaves de apertura y cierre)
# archivo de salida = salida.json
#   Ahora no se debe eliminar manualmente la ultima coma
#   graba 2 archivos de salida para poder comparar luego de regenerar.
# archivo de errores = json_invalido.txt
#   aqui registra los numereros de consulta y las frases que tienen expected mal conformados.
# Se puede ejecutar con:
# time te run-uploaded-domain-tests -regenerate salida.json
# time te run-uploaded-domain-tests -regenerate salida.json | tee $jorge/test_results/udt_ums_kpi$(date +%F_%H%M).txt
###############################################################################

# Funcion que valida objeto json
def json_validator(data):
    try:
        json.loads(data)
        return True
    except ValueError:
        return False

# Variables de configuración
sigla_dom = "rcl" # Sigla del dominio
sigla_dom_acond = "_" + sigla_dom # Sigla del dominio acondicionada.
sigla_dom_acond = sigla_dom_acond + "_psa" # Se agrega si es para psa, puede tener cambios.

# variables de trabajo
source_f = "entrada" # archivo .csv con las frases a procesar
target_f = "salida" # archivo .csv con las frases a procesar
error = "Frases con Expected mal conformados\n"
reg_tot=reg_ok=reg_err=0

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

# Limpiar pantalla
os.system ("clear")

# Abre las plantillas y las guarda en variables para trabajarlas
with open("./source/plantilla_input"+sigla_dom_acond+".json") as raw_f:
  pl_input = raw_f.read()
with open("./source/plantilla_cabecera"+sigla_dom_acond+".json") as raw_f:
  pl_cabecera = raw_f.read()
with open("./source/plantilla_metadata"+sigla_dom_acond+".json") as raw_f:
  pl_metadata = raw_f.read()
with open("./source/plantilla_pie"+sigla_dom_acond+".json") as raw_f:
  pl_pie = raw_f.read()

temp = pl_cabecera #agrega cabecera
temp = temp + pl_metadata #agrega plantilla metadata.
# Abre archvio csv y lo recorre
with open('./'+source_f+".csv") as csvfile:
  reader = csv.DictReader(csvfile,delimiter='\t')
  for row in reader:
    if reg_tot > 0:
      temp = temp  + "\t\t\t\t\t,\n" #agrega una coma , para separar los test. Al comienzo del segundo test y en los siguientes.
    reg_tot+=1
    json_valido = json_validator(row['Expected Json Result']) # Verifica que el Expected esté bien conformado
    if json_valido: # Sustituir campos de la plantilla input por datos de las variables
      reg_ok+=1
      t = Template(pl_input)
      ### Se debe acondicionar el Expected Result, porque no viene exactamente como lo muestra el udt
      # acond_expected=row['Expected Json Result']
      # acond_expected = acond_expected.replace('{"AllResults":[{','[{"ServerResponse":{')
      # acond_expected = acond_expected.rstrip('}]}\n') # Al replace no se le puede colocar caracteres no visibles."
      ### Probando otro acondicionamiento
      acond_expected=row['Expected Json Result']
      if sigla_dom == "csh":
        acond_expected = acond_expected.replace('"AllResults":[{','"ServerResponse":{')
        acond_expected = acond_expected.replace('}]}','}}')
      else: # acondicionando Expected de forma general
        acond_expected = acond_expected.replace('AllResults','ServerResponse')
        acond_expected = acond_expected.replace('[','')
        acond_expected = acond_expected.replace(']','')
      #
      s = t.safe_substitute(frase=row['Query'], expected=acond_expected, qry_nro=row['Query#'], level=row['Level'])
      temp = temp + s #agrega input reemplazado
    else: # registra frases con errores
      reg_err+=1
      error = error + row['Query#'] + " " + row['Query'] + " " + row['Expected Json Result']

temp = temp + pl_pie #agrega pie

# Graba en archivos los resultados
with open ("./error_json_invalido.txt", "w") as pre_proc_f: # Graba en archivo las frases que contienen expected mal conformados
  pre_proc_f.write(error)
with open ("./"+target_f+".json", "w") as pre_proc_f: # Graba el resultado a un archivo
  pre_proc_f.write(temp)
with open ("./"+target_f+".json", "w") as pre_proc_f: # Graba 2 veces para no perder el original al ejecutar el regenerate
  pre_proc_f.write(temp)

# Resumen de lo realizado
print(f"Totales: {reg_tot}")
print(f"Proces.: {reg_ok}")
print(f"Errores: {reg_err}")
print("Archivo resultante: salida.json")


'''<Reformats .p data file format into .swc file format.>
   <Output .swc file>
    Copyright (C) <2019>  <Zhi Cheng Wu>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.'''

#Usage:
#Output: filenameconvert.p

#George Mason University
#Jonathan Reed and Zhi Cheng Wu
#March 1, 2019

import argparse
import datetime
from convert_swc_pfile import read_data_comments
PARENT=1
SELF=0
DIAM=5

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument("--file")
    args = parser.parse_args()
    filename = args.file

    lines = open(filename, 'r').readlines()

    data_lines,comment_lines=read_data_comments(lines)

    comment_lines.append('# Original .p file : '+filename + '\n')
    comment_lines.append('# Modified to .swc file on : '+str(datetime.datetime.now()) + '\n')

    all_new_lines = []
    #rearranges values for .p format
    for num, line in enumerate(data_lines):
        newline = [line[x] for x in range(6)]
        newline[5] = round(float(line[5])/2,4) #replace diameter with radius
        #very first line has no parent, and is of type soma
        if num == 0:
            newline.append(-1) #parent
            newline[PARENT] = 1  #type
        #splicing underscore out and choosing portion of the value desired
        else:
            newline.append( newline[PARENT].split('_')[0]) #parent
            newline[PARENT] = newline[0].split('_')[1] #type
        newline[SELF] = newline[SELF].split('_')[0] #self
        #creates list of newline that can be recorded to search for previous neurons
        all_new_lines.append(newline)
        parentneuron = int(newline[6])
        #print(num,'*** relative:', newline,'parent=',parentneuron)
        if int(parentneuron) > 0:
            for x in range(2,5):
                #convert from relative to absolute coordinates, but not for first line
                #print(newline[x], 'add to', all_new_lines[parentneuron-1][x])
                newline[x] = round(float(newline[x])+float(all_new_lines[parentneuron-1][x]),4)
            #print('   abs',newline,'\n','  parent',parentneuron,' :',all_new_lines[parentneuron-1])

    out_name = filename.split('.p')[0] + '.swc'
    outfile = open(out_name,'w')
    for line in comment_lines:
        outfile.write(line)
    for line in all_new_lines:
        write_line = ' '.join([str(val) for val in line]) #converts from list of strings to single string
        outfile.write(write_line + '\n')
    outfile.close()

    print('Converted ' + str(len(data_lines)) + ' compartments from original file : ' + str(filename))
    print('Modified file created : ' + str(out_name))


# guilds.py
"""Provides access to the table "guilds" in the database"""

from dataclasses import dataclass
import itertools
import sqlite3
from typing import List, Tuple, Union

import discord
from discord.ext import commands

from database import errors
from resources import exceptions, settings, strings

# Containers
@dataclass()
class Guild():
    """Object that represents a record from table "guilds"."""
    guild_id: int
    prefix: str

    async def refresh(self) -> None:
        """Refreshes guild data from the database."""
        new_settings = await get_guild(self.guild_id)
        self.prefix = new_settings.prefix

    async def update(self, **kwargs) -> None:
        """Updates the guild record in the database. Also calls refresh().

        Arguments
        ---------
        kwargs (column=value):
            prefix: str
        """
        await _update_guild(self.guild_id, **kwargs)
        await self.refresh()

# Miscellaneous functions
async def _dict_to_guild(record: dict) -> Guild:
    """Creates a Guild object from a database record

    Arguments
    ---------
    record: Database record from table "guilds" as a dict.

    Returns
    -------
    Guild object.

    Raises
    ------
    LookupError if something goes wrong reading the dict. Also logs this error to the database.
    """
    function_name = '_dict_to_guild'
    try:
        guild = Guild(
            guild_id = record['guild_id'],
            prefix = record['prefix'],
        )
    except Exception as error:
        await errors.log_error(
            strings.INTERNAL_ERROR_DICT_TO_OBJECT.format(function=function_name, record=record)
        )
        raise LookupError(error)

    return guild

async def _get_mixed_case_prefixes(prefix: str) -> List[str]:
    """Turns a string into a list of all mixed case variations of said string

    Returns
    -------
    All mixed case variations: List[str]
    """
    mixed_prefixes = []
    all_prefixes = map(''.join, itertools.product(*((char.upper(), char.lower()) for char in prefix)))
    for prefix in list(all_prefixes):
        mixed_prefixes.append(prefix)
    return mixed_prefixes

# Read data
async def get_prefix(ctx_or_message: Union[commands.Context, discord.Message]) -> str:
    """Check database for stored prefix. If no prefix is found, the default prefix is used"""
    table = 'guilds'
    function_name = 'get_prefix'
    sql = f'SELECT prefix FROM {table} WHERE guild_id=?'
    guild_id = ctx_or_message.guild.id
    try:
        cur=settings.DATABASE.cursor()
        cur.execute(sql, (guild_id,))
        record = cur.fetchone()
        prefix = record['prefix'].replace('"','') if record else settings.DEFAULT_PREFIX
    except sqlite3.Error as error:
        await errors.log_error(
            strings.INTERNAL_ERROR_SQLITE3.format(error=error, table=table, function=function_name, sql=sql),
            ctx_or_message
        )

    return prefix

async def get_all_prefixes(bot: commands.Bot, ctx: commands.Context) -> Tuple:
    """Gets all prefixes. If no prefix is found, a record for the guild is created with the
    default prefix.

    Returns
    -------
    A tuple with the current server prefix, all "rpg" prefixes and the pingable bot

    Raises
    ------
    sqlite3.Error if something happened within the database.  Also logs this error to the database.
    """
    table = 'guilds'
    function_name = 'get_all_prefixes'
    sql = f'SELECT prefix FROM {table} WHERE guild_id=?'
    guild_id = ctx.guild.id
    try:
        cur = settings.DATABASE.cursor()
        cur.execute(sql, (guild_id,))
        record = cur.fetchone()
        prefixes = []
        if record:
            prefix_db = record['prefix'].replace('"','')
            prefix_db_mixed_case = await _get_mixed_case_prefixes(prefix_db)
            for prefix in prefix_db_mixed_case:
                prefixes.append(prefix)
        else:
            sql = f'INSERT INTO {table} (guild_id, prefix) VALUES (?, ?)'
            cur.execute(sql, (guild_id, settings.DEFAULT_PREFIX))
            prefix_default_mixed_case = await _get_mixed_case_prefixes(settings.DEFAULT_PREFIX)
            for prefix in prefix_default_mixed_case:
                prefixes.append(prefix)
    except sqlite3.Error as error:
        await errors.log_error(
            strings.INTERNAL_ERROR_SQLITE3.format(error=error, table=table, function=function_name, sql=sql),
            ctx
        )
        raise

    return commands.when_mentioned_or(*prefixes)(bot, ctx)

async def get_guild(guild_id: int) -> Guild:
    """Gets all guild settings.

    Returns
    -------
    Guild object

    Raises
    ------
    sqlite3.Error if something happened within the database.
    exceptions.NoDataFoundError if no guild was found.
    LookupError if something goes wrong reading the dict.
    Also logs all errors to the database.
    """
    table = 'guilds'
    function_name = 'get_guild'
    sql_select = f'SELECT * FROM {table} WHERE guild_id=?'
    try:
        cur = settings.DATABASE.cursor()
        cur.execute(sql_select, (guild_id,))
        record = cur.fetchone()
    except sqlite3.Error as error:
        await errors.log_error(
            strings.INTERNAL_ERROR_SQLITE3.format(error=error, table=table, function=function_name, sql=sql_select)
        )
        raise
    if not record:
        sql = f'INSERT INTO {table} (guild_id, prefix) VALUES (?, ?)'
        try:
            cur.execute(sql, (guild_id, settings.DEFAULT_PREFIX))
            sql = sql_select
            cur.execute(sql, (guild_id,))
            record = cur.fetchone()
        except sqlite3.Error as error:
            await errors.log_error(
                strings.INTERNAL_ERROR_SQLITE3.format(error=error, table=table, function=function_name, sql=sql)
            )
            raise
    guild = await _dict_to_guild(dict(record))

    return guild

# Write Data
async def _update_guild(guild_id: int, **kwargs) -> None:
    """Updates guild record. Use Guild.update() to trigger this function.

    Arguments
    ---------
    kwargs (column=value):
        prefix: str

    Raises
    ------
    sqlite3.Error if something happened within the database.
    NoArgumentsError if no kwargs are passed (need to pass at least one)
    Also logs all errors to the database.
    """
    table = 'guilds'
    function_name = '_update_guild'
    if not kwargs:
        await errors.log_error(
            strings.INTERNAL_ERROR_NO_ARGUMENTS.format(table=table, function=function_name)
        )
        raise exceptions.NoArgumentsError('You need to specify at least one keyword argument.')
    try:
        cur = settings.DATABASE.cursor()
        sql = f'UPDATE {table} SET'
        for kwarg in kwargs:
            sql = f'{sql} {kwarg} = :{kwarg},'
        sql = sql.strip(",")
        kwargs['guild_id'] = guild_id
        sql = f'{sql} WHERE guild_id = :guild_id'
        cur.execute(sql, kwargs)
    except sqlite3.Error as error:
        await errors.log_error(
            strings.INTERNAL_ERROR_SQLITE3.format(error=error, table=table, function=function_name, sql=sql)
        )
        raise


from mohawk import Sender
import requests
import os
import sys

def get_or_throw(key_name):
    res = os.getenv(key_name, None)
    if res is None:
        print("Environment variable '" + key_name + "' is required.")
        sys.exit(1)
    return res

HOST = os.getenv("TS_HOST", 'api.threatstack.com')
USER_ID = get_or_throw("TS_USER_ID")
ORGANIZATION_ID = get_or_throw("TS_ORGANIZATION_ID")
API_KEY = get_or_throw("TS_API_KEY")

BASE_PATH = 'https://' + HOST
URI_PATH = '/help/hawk/self-test'

credentials = {
    'id': USER_ID,
    'key': API_KEY,
    'algorithm': 'sha256'
}
URL = BASE_PATH + URI_PATH
sender = Sender(credentials, URL, "GET", always_hash_content=False, ext=ORGANIZATION_ID)

response = requests.get(URL, headers={'Authorization': sender.request_header})
print(response.text)
# Note a warning is logged out during the authenticate call:
# seen_nonce was None; not checking nonce. You may be vulnerable to replay attacks
# This is not an issue because the nonce is randomly generated above and a different
# nonce is used for each request.
sender.accept_response(response.headers['Server-Authorization'],
                       content=response.text,
                       content_type=response.headers['Content-Type'])

# accept_response will throw if the response is not authentic
# after this call we know the response is authentic
print('Response is authentic')


from collections import deque
import sys
input=sys.stdin.readline

N,M=map(int,input().split())
MAP=[list(input()) for _ in range(N)]

def bfs(x,y,a):
    dq=deque()
    dq.append([x,y])
    while dq:
        X,Y=dq.popleft()
        MAP[X][Y]=0
        if a=='-':
            if 0<=X<N and 0<=Y+1<M:
                if MAP[X][Y+1]=='-':
                    MAP[X][Y+1]=0
                    dq.append([X,Y+1])
        elif a=='|':
            if 0<=X+1<N and 0<=Y<M:
                if MAP[X+1][Y]=='|':
                    MAP[X+1][Y]=0
                    dq.append([X+1,Y])

cnt=0
for i in range(N):
    for j in range(M):
        if MAP[i][j]=='-' or MAP[i][j]=='|':
            result=bfs(i,j,MAP[i][j])
            cnt+=1

print(cnt)


from django.shortcuts import render,render_to_response

from django.http import HttpResponse
from django.template import RequestContext, loader

from .models import NextMeeting, Officers

def index(request):
    next_meetings = NextMeeting.objects.order_by('-meeting_date')[:5]
    officers = Officers.objects.order_by('-president')
    template = loader.get_template('meeting/index.html')
    context = {
    	'meeting_time':next_meetings.reverse()[0].meeting_time,
    	'meeting_date':next_meetings.reverse()[0].meeting_date,
    	'president':officers[0].president,
    	'vice_president':officers[0].vice_president,
    	'treasurer':officers[0].treasurer,
    	'secretary':officers[0].secretary,
    	'webmaster':officers[0].webmaster,
    	'president_email':officers[0].president_email,
    	'vice_president_email':officers[0].vice_president_email,
    	'treasurer_email':officers[0].treasurer_email,
    	'secretary_email':officers[0].secretary_email,
    	'webmaster_email':officers[0].webmaster_email,
    }
    return render_to_response('index.html', context)


#!/usr/bin/env python
#SBATCH --mem=8000
#SBATCH --time=4-18:20
#SBATCH --mail-type=ALL
#SBATCH --mail-user=brando90@mit.edu
#SBATCH --gres=gpu:1

#from __future__ import #print_function
#tensorboard --logdir=/tmp/mdl_logs
#

print('#!/usr/bin/env python')
print('#!/usr/bin/python')

import os
import sys

#import pickle
import namespaces as ns
import argparse
import pdb
import functools

import numpy as np
import tensorflow as tf

import my_tf_pkg as mtf
from my_tf_pkg import main_hp
from my_tf_pkg import main_large_hp_checkpointer as large_main_hp
import sgd_lib

##
#print('In batch script', flush=True)
#print = functools.partial(print, flush=True)
#print(ns)
###
arg = ns.Namespace(printing=False)
#
arg.get_errors_from = mtf.get_errors_based_on_train_error
#arg.get_errors_from = mtf.get_errors_based_on_validation_error
#

#arg.type_job, arg.nb_array_jobs = 'serial', 1 #careful when this is on and GPU is NOT on
arg.type_job = 'slurm_array_parallel'
#arg.type_job, arg.nb_array_jobs = 'main_large_hp_ckpt', 200
#arg.save_checkpoints = True
arg.save_checkpoints = False
#arg.save_last_mdl = True
arg.save_last_mdl = False

## debug mode
arg.data_dirpath = './data/' # path to datasets
prefix_path_sim_results = './tmp_simulation_results_scripts/%s/%s/' # folder where the results from script is saved
prefix_path_ckpts = './tmp_all_ckpts/%s/%s/' # folder where the results from script is saved
## to run locally: python batch_main.py -sj sj
#arg.data_dirpath = '../data/' # path to datasets
#prefix_path_sim_results = '../../../simulation_results_scripts/%s/%s/' # folder where the results from script is saved
#prefix_path_ckpts = '../../../all_ckpts/%s/%s/' # folder where the results from script is saved
## to run in docker
#arg.data_dirpath = '/home_simulation_research/hbf_tensorflow_code/tf_experiments_scripts/data/' # path to datasets
#prefix_path_sim_results = '/home_simulation_research/simulation_results_scripts/%s/%s/' # folder where the results from script is saved
#prefix_path_ckpts = '/home_simulation_research/all_ckpts/%s/%s/' # folder where the results from script is saved

# prefix_path_sim_results = '../../simulation_results_scripts/%s/%s'
# prefix_path_ckpts = '../../all_ckpts/%s/%s' # folder where the results from script is saved
arg.get_path_root =  lambda arg: prefix_path_sim_results%(arg.experiment_root_dir,arg.experiment_name)
arg.get_path_root_ckpts =  lambda arg: prefix_path_ckpts%(arg.experiment_root_dir,arg.experiment_name)

arg.prefix_ckpt = 'mdl_ckpt'
####
arg.data_filename = 'basin_expt'
#arg.data_filename = 'f_64D_product_binary'
#arg.task_folder_name = mtf.get_experiment_folder(arg.data_filename) #om_f_4d_conv
#arg.type_preprocess_data = None
#
#arg.N_frac = 60000

#arg.classificaton = mtf.classification_task_or_not(arg)

arg.experiment_name = 'task_Mar_12_1D_basin_dgx1'
#arg.job_name = 'BT_debug1'
arg.job_name = 'basin_1D'
arg.job_name = 'basin_2D'
#
arg.experiment_root_dir = mtf.get_experiment_folder(arg.data_filename)
#
arg.mdl = 'basin_1D'
arg.mdl = 'basin_2D'
arg.mdl = 'basin_3D'
#arg.mdl = 'basin_4D'
#arg.mdl = 'basin_8D'
#arg.mdl = 'basin_16D'
#arg.mdl = 'basin_32D'
if arg.mdl == 'debug_mdl':
    arg.act = tf.nn.relu
    arg.dims = None
    arg.get_dims = lambda arg: arg.dims
    arg.get_x_shape = lambda arg: [None,arg.D]
    arg.get_y_shape = lambda arg: [None,arg.D_out]
elif arg.mdl == 'basin_1D':
    arg.printing = True
    #arg.printing = False
    #
    arg.mdl_scope_name = arg.mdl
    D = 1
    arg.D = D
    arg.get_x_shape = lambda arg: arg.D
    #
    arg.compact = True
    #arg.compact = False
    arg.B = 18
    #
    arg.init_std = lambda: tf.constant([1.0,2.0])
    arg.init_mu = lambda: [ tf.constant( 4.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]), tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]) ]
    arg.init_W = lambda: tf.constant(6.972*np.ones([D,1]),dtype=np.float32,shape=[D,1])
    #
    arg.iter = 1*int(1.4*1001)
    #
    arg.start_learning_rate = 1.0
    arg.gdl_mu_noise = 0.0
    # 13.8
    arg.frac = 1.0
    #arg.gdl_stddev_noise = arg.frac*13.8
    arg.gdl_stddev_noise = arg.frac*0.1
    arg.noise_amplitude = 7.0
    #
    p_filename = 'W_hist_data_%sD_lr%s_amp_%s_%sstd_%snoise_iter%s'%(arg.D,arg.start_learning_rate,arg.noise_amplitude,arg.frac,arg.gdl_stddev_noise,arg.iter)
    arg.p_filename = p_filename.replace('.','p')+'.p'
    arg.p_filename = 'current_test.p'
    print(arg.p_filename)
    def get_basins(arg):
        #pdb.set_trace()
        W = tf.get_variable(name='W', initializer=arg.init_W(), trainable=True)
        print('==> W.name', W.name)
        tf.summary.histogram('Weights', W)
        #tf.summary.scalar('Weights_scal', W)
        #
        init_std = arg.init_std()
        init_mu = arg.init_mu()
        #
        basin1 = sgd_lib.get_basin(W,init_std[0],init_mu[0],str(1))
        basin2 = sgd_lib.get_basin(W,init_std[1],init_mu[1],str(2))
        basins = [basin1, basin2]
        return basins
    arg.get_basins = get_basins
elif arg.mdl == 'basin_2D':
    arg.printing = True
    #arg.printing = False
    #
    arg.mdl_scope_name = arg.mdl
    D = 2
    arg.D = D
    arg.get_x_shape = lambda arg: arg.D
    #
    arg.compact = True
    #arg.compact = False
    arg.B = 18
    #
    arg.init_std = lambda: tf.constant([1.0,2.0])
    arg.init_mu = lambda: [ tf.constant( 4.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]), tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]) ]
    arg.init_W = lambda: tf.constant(6.9*np.ones([D,1]),dtype=np.float32,shape=[D,1])
    #
    arg.iter = 1*int(1.3*1001)
    #
    arg.start_learning_rate = 0.1
    arg.gdl_mu_noise = 0.0
    # 13.8
    arg.frac = 1.0
    #arg.gdl_stddev_noise = arg.frac*13.8
    arg.gdl_stddev_noise = arg.frac*0.01
    arg.noise_amplitude = 8.0
    #
    p_filename = 'W_hist_data_%sD_lr%s_amp_%s_%sstd_%snoise_iter%s'%(arg.D,arg.start_learning_rate,arg.noise_amplitude,arg.frac,arg.gdl_stddev_noise,arg.iter)
    arg.p_filename = p_filename.replace('.','p')+'.p'
    arg.p_filename = 'current_test.p'
    print(arg.p_filename)
    def get_basins(arg):
        #pdb.set_trace()
        W = tf.get_variable(name='W', initializer=arg.init_W(), trainable=True)
        print('==> W.name', W.name)
        tf.summary.histogram('Weights', W)
        #tf.summary.scalar('Weights_scal', W)
        #
        init_std = arg.init_std()
        init_mu = arg.init_mu()
        #
        basin1 = sgd_lib.get_basin(W,init_std[0],init_mu[0],str(1))
        basin2 = sgd_lib.get_basin(W,init_std[1],init_mu[1],str(2))
        basins = [basin1, basin2]
        return basins
    arg.get_basins = get_basins
elif arg.mdl == 'basin_3D':
    arg.printing = True
    #arg.printing = False
    #
    arg.mdl_scope_name = arg.mdl
    D = 3
    arg.D = D
    arg.get_x_shape = lambda arg: arg.D
    #
    arg.compact = True
    #arg.compact = False
    arg.B = 18
    #
    arg.init_std = lambda: tf.constant([1.0,4.0])
    arg.init_mu = lambda: [ tf.constant( 4.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]), tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]) ]
    arg.init_W = lambda: tf.constant(4.0*np.ones([D,1]),dtype=np.float32,shape=[D,1])
    #
    arg.iter = 1*int(1.5*1.5*1001)
    #
    arg.start_learning_rate = 0.07
    arg.gdl_mu_noise = 0.0
    # 13.8
    arg.frac = 1.0
    #arg.gdl_stddev_noise = arg.frac*13.8
    arg.gdl_stddev_noise = arg.frac*1.0
    arg.noise_amplitude = 0.004
    #
    p_filename = 'W_hist_data_%sD_lr%s_amp_%s_%sstd_%snoise_iter%s'%(arg.D,arg.start_learning_rate,arg.noise_amplitude,arg.frac,arg.gdl_stddev_noise,arg.iter)
    arg.p_filename = p_filename.replace('.','p')+'.p'
    arg.p_filename = 'current_test.p'
    print(arg.p_filename)
    def get_basins(arg):
        #pdb.set_trace()
        W = tf.get_variable(name='W', initializer=arg.init_W(), trainable=True)
        print('==> W.name', W.name)
        tf.summary.histogram('Weights', W)
        #tf.summary.scalar('Weights_scal', W)
        #
        init_std = arg.init_std()
        init_mu = arg.init_mu()
        #
        basin1 = sgd_lib.get_basin(W,init_std[0],init_mu[0],str(1))
        basin2 = sgd_lib.get_basin(W,init_std[1],init_mu[1],str(2))
        basins = [basin1, basin2]
        return basins
    arg.get_basins = get_basins
elif arg.mdl == 'basin_4D':
    arg.printing = True
    #arg.printing = False
    #
    arg.mdl_scope_name = arg.mdl
    D = 4
    arg.D = D
    arg.get_x_shape = lambda arg: arg.D
    #
    arg.compact = True
    #arg.compact = False
    arg.B = 18
    #
    arg.init_std = lambda: tf.constant([1.0,2.0])
    arg.init_mu = lambda: [ tf.constant( 4.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]), tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]) ]
    arg.init_W = lambda: tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1])
    #
    arg.iter = 1*int(1.0*1001)
    #
    arg.start_learning_rate = 0.01
    arg.gdl_mu_noise = 0.0
    # 13.8
    arg.frac = 1.0
    #arg.gdl_stddev_noise = arg.frac*13.8
    arg.gdl_stddev_noise = arg.frac*0.01
    arg.noise_amplitude = 1.0
    #
    p_filename = 'W_hist_data_%sD_lr%s_amp_%s_%sstd_%snoise_iter%s'%(arg.D,arg.start_learning_rate,arg.noise_amplitude,arg.frac,arg.gdl_stddev_noise,arg.iter)
    arg.p_filename = p_filename.replace('.','p')+'.p'
    arg.p_filename = 'current_test.p'
    print(arg.p_filename)
    def get_basins(arg):
        #pdb.set_trace()
        W = tf.get_variable(name='W', initializer=arg.init_W(), trainable=True)
        print('==> W.name', W.name)
        tf.summary.histogram('Weights', W)
        #tf.summary.scalar('Weights_scal', W)
        #
        init_std = arg.init_std()
        init_mu = arg.init_mu()
        #
        basin1 = sgd_lib.get_basin(W,init_std[0],init_mu[0],str(1))
        basin2 = sgd_lib.get_basin(W,init_std[1],init_mu[1],str(2))
        basins = [basin1, basin2]
        return basins
    arg.get_basins = get_basins
elif arg.mdl == 'basin_8D':
    arg.printing = True
    #arg.printing = False
    #
    arg.mdl_scope_name = arg.mdl
    D = 8
    arg.D = D
    arg.get_x_shape = lambda arg: arg.D
    #
    arg.compact = True
    #arg.compact = False
    arg.B = 18
    #
    arg.init_std = lambda: tf.constant([1.0,2.0])
    arg.init_mu = lambda: [ tf.constant( 4.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]), tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]) ]
    arg.init_W = lambda: tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1])
    #
    arg.iter = 1*int(1.0*1001)
    #
    arg.start_learning_rate = 0.01
    arg.gdl_mu_noise = 0.0
    # 13.8
    arg.frac = 1.0
    #arg.gdl_stddev_noise = arg.frac*13.8
    arg.gdl_stddev_noise = arg.frac*0.01
    arg.noise_amplitude = 1.0
    #
    p_filename = 'W_hist_data_%sD_lr%s_amp_%s_%sstd_%snoise_iter%s'%(arg.D,arg.start_learning_rate,arg.noise_amplitude,arg.frac,arg.gdl_stddev_noise,arg.iter)
    arg.p_filename = p_filename.replace('.','p')+'.p'
    arg.p_filename = 'current_test.p'
    print(arg.p_filename)
    def get_basins(arg):
        #pdb.set_trace()
        W = tf.get_variable(name='W', initializer=arg.init_W(), trainable=True)
        print('==> W.name', W.name)
        tf.summary.histogram('Weights', W)
        #tf.summary.scalar('Weights_scal', W)
        #
        init_std = arg.init_std()
        init_mu = arg.init_mu()
        #
        basin1 = sgd_lib.get_basin(W,init_std[0],init_mu[0],str(1))
        basin2 = sgd_lib.get_basin(W,init_std[1],init_mu[1],str(2))
        basins = [basin1, basin2]
        return basins
    arg.get_basins = get_basins
elif arg.mdl == 'basin_16D':
    arg.printing = True
    #arg.printing = False
    #
    arg.mdl_scope_name = arg.mdl
    D = 16
    arg.D = D
    arg.get_x_shape = lambda arg: arg.D
    #
    arg.compact = True
    #arg.compact = False
    arg.B = 18
    #
    arg.init_std = lambda: tf.constant([1.0,2.0])
    arg.init_mu = lambda: [ tf.constant( 4.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]), tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]) ]
    arg.init_W = lambda: tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1])
    #
    arg.iter = 1*int(1.0*1001)
    #
    arg.start_learning_rate = 0.01
    arg.gdl_mu_noise = 0.0
    # 13.8
    arg.frac = 1.0
    #arg.gdl_stddev_noise = arg.frac*13.8
    arg.gdl_stddev_noise = arg.frac*0.01
    arg.noise_amplitude = 1.0
    #
    p_filename = 'W_hist_data_%sD_lr%s_amp_%s_%sstd_%snoise_iter%s'%(arg.D,arg.start_learning_rate,arg.noise_amplitude,arg.frac,arg.gdl_stddev_noise,arg.iter)
    arg.p_filename = p_filename.replace('.','p')+'.p'
    arg.p_filename = 'current_test.p'
    print(arg.p_filename)
    def get_basins(arg):
        #pdb.set_trace()
        W = tf.get_variable(name='W', initializer=arg.init_W(), trainable=True)
        print('==> W.name', W.name)
        tf.summary.histogram('Weights', W)
        #tf.summary.scalar('Weights_scal', W)
        #
        init_std = arg.init_std()
        init_mu = arg.init_mu()
        #
        basin1 = sgd_lib.get_basin(W,init_std[0],init_mu[0],str(1))
        basin2 = sgd_lib.get_basin(W,init_std[1],init_mu[1],str(2))
        basins = [basin1, basin2]
        return basins
    arg.get_basins = get_basins
elif arg.mdl == 'basin_32D':
    arg.printing = True
    #arg.printing = False
    #
    arg.mdl_scope_name = arg.mdl
    D = 32
    arg.D = D
    arg.get_x_shape = lambda arg: arg.D
    #
    arg.compact = True
    #arg.compact = False
    arg.B = 18
    #
    arg.init_std = lambda: tf.constant([1.0,2.0])
    arg.init_mu = lambda: [ tf.constant( 4.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]), tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1]) ]
    arg.init_W = lambda: tf.constant(12.0*np.ones([D,1]),dtype=np.float32,shape=[D,1])
    #
    arg.iter = 1*int(1.0*1001)
    #
    arg.start_learning_rate = 0.01
    arg.gdl_mu_noise = 0.0
    # 13.8
    arg.frac = 1.0
    #arg.gdl_stddev_noise = arg.frac*13.8
    arg.gdl_stddev_noise = arg.frac*0.01
    arg.noise_amplitude = 1.0
    #
    p_filename = 'W_hist_data_%sD_lr%s_amp_%s_%sstd_%snoise_iter%s'%(arg.D,arg.start_learning_rate,arg.noise_amplitude,arg.frac,arg.gdl_stddev_noise,arg.iter)
    arg.p_filename = p_filename.replace('.','p')+'.p'
    arg.p_filename = 'current_test.p'
    print(arg.p_filename)
    def get_basins(arg):
        #pdb.set_trace()
        W = tf.get_variable(name='W', initializer=arg.init_W(), trainable=True)
        print('==> W.name', W.name)
        tf.summary.histogram('Weights', W)
        #tf.summary.scalar('Weights_scal', W)
        #
        init_std = arg.init_std()
        init_mu = arg.init_mu()
        #
        basin1 = sgd_lib.get_basin(W,init_std[0],init_mu[0],str(1))
        basin2 = sgd_lib.get_basin(W,init_std[1],init_mu[1],str(2))
        basins = [basin1, basin2]
        return basins
    arg.get_basins = get_basins

#arg.get_y_shape = lambda arg: [None, arg.D_out]
# float type
arg.float_type = tf.float32
#steps
#arg.steps_low = int(2.5*60000)
#arg.steps_low = 1*int(1.3*10001) # 1D
arg.steps_low = arg.iter # 2D
arg.steps_high = arg.steps_low+1
arg.get_steps = lambda arg: int( np.random.randint(low=arg.steps_low ,high=arg.steps_high) )

arg.M_low = 32
arg.M_high = 33
arg.get_batch_size = lambda arg: int(np.random.randint(low=arg.M_low , high=arg.M_high))
#arg.potential_batch_sizes = [16,32,64,128,256,512,1024]
#arg.potential_batch_sizes = [4]
def get_power2_batch_size(arg):
    i = np.random.randint( low=0, high=len(arg.potential_batch_sizes) )
    batch_size = arg.potential_batch_sizes[i]
    return batch_size
#arg.get_batch_size = get_power2_batch_size
arg.report_error_freq = 2

arg.low_log_const_learning_rate, arg.high_log_const_learning_rate = -0.5, -4
arg.get_log_learning_rate =  lambda arg: np.random.uniform(low=arg.low_log_const_learning_rate, high=arg.high_log_const_learning_rate)
arg.get_start_learning_rate = lambda arg: 10**arg.log_learning_rate
## decayed_learning_rate = learning_rate * decay_rate ^ (global_step / decay_steps)
#arg.decay_rate_low, arg.decay_rate_high = 0.1, 1.0
#arg.get_decay_rate = lambda arg: np.random.uniform(low=arg.decay_rate_low, high=arg.decay_rate_high)
arg.get_start_learning_rate = lambda arg: arg.start_learning_rate
arg.get_decay_rate = lambda arg: 1.0

#arg.decay_steps_low, arg.decay_steps_high = arg.report_error_freq, arg.M
#arg.get_decay_steps_low_high = lambda arg: arg.report_error_freq, arg.M
#arg.get_decay_steps = lambda arg: np.random.randint(low=arg.decay_steps_low, high=arg.decay_steps_high)
def get_decay_steps(arg):
    #arg.decay_steps_low, arg.decay_steps_high = arg.report_error_freq, arg.M
    arg.decay_steps_low, arg.decay_steps_high = 1000, 15000
    decay_steos = np.random.randint(low=arg.decay_steps_low, high=arg.decay_steps_high)
    return decay_steos
arg.get_decay_steps = get_decay_steps # when stair case, how often to shrink

#arg.staircase = False
arg.staircase = True

#optimization_alg = 'GD'
#optimization_alg = 'Momentum'
#optimization_alg = 'Adadelta'
#optimization_alg = 'Adagrad'
#optimization_alg = 'Adam'
#optimization_alg = 'RMSProp'
optimization_alg = 'GDL'
arg.optimization_alg = optimization_alg

if optimization_alg == 'GD':
    pass
elif optimization_alg=='Momentum':
    #arg.get_use_nesterov = lambda: False
    arg.get_use_nesterov = lambda: True
    arg.momentum_low, arg.momontum_high = 0.01, 0.99
    arg.get_momentum = lambda arg: np.random.uniform(low=arg.momentum_low,high=arg.momontum_high)
elif optimization_alg == 'Adadelta':
    arg.rho_low, arg.rho_high = 0.1, 0.99
    arg.get_rho = lambda arg: np.random.uniform(low=arg.rho_low,high=arg.rho_high)
elif optimization_alg == 'Adagrad':
    #only has learning rate
    pass
elif optimization_alg == 'Adam':
    arg.beta1 = 0.99
    arg.beta2 = 0.999
    arg.get_beta1 = lambda arg: arg.beta1 # m = b1m + (1 - b1)m
    arg.get_beta2 = lambda arg: arg.beta2 # v = b2 v + (1 - b2)v
    #arg.beta1_low, arg.beta1_high = beta1_low=0.7, beta1_high=0.99 # m = b1m + (1 - b1)m
    #arg.beta2_low, arg.beta2_high = beta2_low=0.8, beta2_high=0.999 # v = b2 v + (1 - b2)v
elif optimization_alg == 'RMSProp':
    arg.decay_loc, arg.decay_high = 0.75, 0.99
    arg.get_decay = lambda arg: np.random.uniform(low=arg.decay_loc,high=arg.decay_high)
    arg.momentum_low, arg.momontum_high = 0.0, 0.99
    arg.get_momentum = lambda arg: np.random.uniform(low=arg.momentum_low,high=arg.momontum_high)
elif optimization_alg == 'GDL':
    arg.get_gdl_mu_noise =  lambda arg: arg.gdl_mu_noise
    arg.get_gdl_stddev_noise = lambda arg: arg.gdl_stddev_noise
else:
    pass

#saving pickle histogram data
arg.nb_bins = 35
#arg.p_path = './tmp_pickle'
arg.p_path = './tmp_om_pickle'
#arg.p_path = './folder_pickle_W_hist'
#arg.p_filename = p_filename
#arg.save_hist = False
arg.save_hist = True
arg.display_hist = False
#arg.display_hist = True

#arg.bn = True
#arg.trainable_bn = True #scale, shift BN
arg.bn = False
arg.trainable_bn = False #scale, shift BN

# NORMALIZE UNIT CIRCLE
arg.data_normalize='normalize_input'
arg.data_normalize='dont_normalize'

re_train = None
arg.re_train = re_train
#
arg.save_config_args = False
#arg.debug = False
# arg.slurm_jobid = os.environ['SLURM_JOBID']
# arg.slurm_array_task_id = os.environ['SLURM_ARRAY_TASK_ID']

####
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", help="debug mode: loads the old (pickle) config file to run in debug mode", action='store_true')
parser.add_argument("-tj", "--type_job", help="type_job for run")
parser.add_argument("-sj", "--SLURM_JOBID", help="SLURM_JOBID for run")
parser.add_argument("-stid", "--SLURM_ARRAY_TASK_ID", help="SLURM_ARRAY_TASK_ID for run.")
parser.add_argument("-sca", "--save_config_args", help="save_config_args saves the current config file to a picke file ")

parser.add_argument("-tb", "--tensorboard", dest='tensorboard', help="tensorboard mode: save tensorboard data", action='store_true')
parser.add_argument('-notb', "--no-tensorboard", dest='tensorboard', help="tensorboard mode: save tensorboard data", action='store_false')
parser.set_defaults(tensorboard=False)

parser.add_argument("-pr", "--path_root", help="path_root: specifies the path root to save hyper params")
#parser.add_argument("-hp", "--hyper_parameter", help="hyper_parameter: when searching for hp on needs to specify were to store the results of experiment or it will default.")

cmd_args = parser.parse_args()
# (do if first if statment is true) if (return true when cmd_args is initialized to None) else (do this)
cmd_args.type_job = cmd_args.type_job if cmd_args.type_job else arg.type_job
# if the flag is initialized (not None) then use it, otherwise use the flag from environment veriable
arg.slurm_jobid = cmd_args.SLURM_JOBID if cmd_args.SLURM_JOBID else os.environ['SLURM_JOBID']

######## try: TODO: fix this
#     #this should only succeed when slurm is being called in parallel with an array id i.e. if (arg.type_job == 'slurm_array_parallel') is true.
#     arg.slurm_array_task_id = cmd_args.SLURM_ARRAY_TASK_ID if cmd_args.SLURM_ARRAY_TASK_ID else os.environ['SLURM_ARRAY_TASK_ID']
# except:
#     if arg.type_job == 'slurm_array_parallel':
#         # stry should have been succesful: arg.slurm_array_task_id = cmd_args.SLURM_ARRAY_TASK_ID if cmd_args.SLURM_ARRAY_TASK_ID else os.environ['SLURM_ARRAY_TASK_ID']
#         pass
#     elif arg.type_job == 'slurm_array_parallel':
#         arg.slurm_array_task_id = arg.type_job
if arg.type_job == 'slurm_array_parallel':
    arg.slurm_array_task_id = cmd_args.SLURM_ARRAY_TASK_ID if cmd_args.SLURM_ARRAY_TASK_ID else os.environ['SLURM_ARRAY_TASK_ID']
else:
    arg.slurm_array_task_id = arg.type_job
#print('--> arg ', arg.slurm_jobid, arg.slurm_array_task_id)
#########

if cmd_args.save_config_args:
    # flag to save current config files to pickle file
    #print(cmd_args.save_config_args)
    arg.save_config_args = cmd_args.save_config_args
if cmd_args.debug:
    #arg.debug = cmd_args.debug
    # load old pickle config
    # pickled_arg_dict = pickle.load( open( "pickle-slurm-%s_%s.p"%(int(arg.slurm_jobid)+int(arg.slurm_array_task_id),arg.slurm_array_task_id), "rb" ) )
    # #print( pickled_arg_dict )
    # # values merged with the second dict's values overwriting those from the first.
    # arg_dict = {**dict(arg), **pickled_arg_dict}
    # arg = ns.Namespace(arg_dict)
    print('EMPTY IF STATEMENT') #TODO fix line above that gives syntax error
# if cmd_args.path_root:
#     arg.path_root = cmd_args.path_root
#     arg.get_path_root =  lambda arg: arg.path_root

#
arg.mdl_save = False
#arg.mdl_save = True

#
arg.use_tensorboard = cmd_args.tensorboard
arg.tensorboard_data_dump_train = './tmp/train'

arg.max_to_keep = 1

arg.get_dataset = lambda arg: (arg.X_train, arg.Y_train, arg.X_cv, arg.Y_cv, arg.X_test, arg.Y_test)

#arg.act_name = arg.act.__name__
arg.restore = False

#
arg.print_func = print
if arg.slurm_array_task_id == '1':
    print = functools.partial(print, flush=True)
    arg.print_func = print
#pickle.dump( dict(arg), open( "pickle_file" , "wb" ) )
#pdb.set_trace()
print(arg)
if __name__ == '__main__':
    #print('In __name__ == __main__')
    if cmd_args.type_job == 'serial':
        # jobs one job. No slurm array
        #arg.start_stid = 1
        #arg.end_stid = arg.nb_array_jobs
        #large_main_hp.run_hyperparam_search2(arg)
        raise ValueError('this cmd_args.type_job is not valid cuz it was %s'%(cmd_args.type_job))
    elif cmd_args.type_job == 'slurm_array_parallel':
        # run one single job according to slurm array command
        sgd_lib.main_basin(arg)
    elif cmd_args.type_job == 'main_large_hp_ckpt':
        #large_main_hp.main_large_hp_ckpt(arg)
        raise ValueError('this cmd_args.type_job is not valid cuz it was %s'%(cmd_args.type_job))


import torch
from torchvision import models
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
import cv2
import numpy as np

device = "cuda" if torch.cuda.is_available() else "cpu"
num_classes = 4
net = models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
in_features = net.roi_heads.box_predictor.cls_score.in_features
net.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
net.load_state_dict(torch.load("D:/workspace/NathanTrain/model.pth"))
net.eval()
net = net.to(device)

camera = cv2.VideoCapture(0)
while camera.isOpened():
    success, frame = camera.read()
    if not success:
        break

    img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    img = np.array(img, dtype=np.float32) / 255
    img = torch.from_numpy(img)
    img = torch.permute(img, (2, 0, 1))
    img = torch.unsqueeze(img, 0)
    img = img.to(device)
    # print(img.shape)
    h = net(img)
    # print(h)
    o_boxes = h[0]["boxes"]
    o_labels = h[0]["labels"]
    o_scores = h[0]["scores"]
    for box, label, score in zip(o_boxes, o_labels, o_scores):
        if score < 0.7: continue
        x1, y1, x2, y2 = map(int, box)
        color = (0, 255, 0)
        if label == 2: color = (0, 0, 255)
        elif label == 3: color = (0, 255, 255)
        cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
        label_names = ["with mask", "without mask", "incorrect"]
        cv2.putText(frame, label_names[label - 1], (x1 + 10, y1 + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)

    cv2.imshow("frame", frame)
    key_code = cv2.waitKey(1)
    if key_code in [27, ord('q')]:
        break
camera.release()
cv2.destroyAllWindows()


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

import numpy as np
import pygame
import random
import Player

class Environment:
    def __init__(self, m, n):
        self.observation_space_n = m*n
        self.action_space_n = 4  # 8 for 8 sides

        self.n = n
        self.m = m
        self.width = m*10
        self.height = n*10

        self.startY = int(input("Enter start position Y: "))
        self.targetY = int(input("Enter target position Y: "))

        self.player = Player.Player(0, self.startY)

        self.map = np.zeros((n, m))
        self.rewards = np.zeros((n, m))
        self.drawWall(self.map, self.rewards)

        self.cordmemory = {}
        self.players = pygame.sprite.Group()
        self.players.add(self.player)
        self.time = 0
        pygame.init()

        pygame.font.init()
        self.myfont = pygame.font.SysFont('Helvetica', 30)

    def getState(self):
        return self.player.getX()*self.n+self.player.getY()

    def reset(self):
        self.time = 0
        self.player = Player.Player(20, self.startY)
        self.players = pygame.sprite.Group()
        self.players.add(self.player)
        self.cordmemory = {}
        return self.getState()

    def step(self, action):
        self.cordmemory[(self.player.getX(), self.player.getY())] = 1

        self.player.update(action)
        self.time += 1

        done = False
        try:
            reward = self.rewards[self.player.getY()][self.player.getX()]
        except IndexError:
            pass

        if self.player.getX() > self.m-1 or self.player.getX() < 0 or \
                self.player.getY() < 0 or self.player.getY() > self.n-1 or \
                self.map[self.player.getY()][self.player.getX()] == 1:
            if action == 0 or action == 1:
                self.player.update(-action+1)
            elif action == 2 or action == 3:
                self.player.update(-action+5)
            elif action == 4 or action == 5:
                self.player.update(-action+9)
            elif action == 6 or action == 7:
                self.player.update(-action+13)
            reward = -5
            done = True

        if (self.player.getX() == self.m-1 and self.player.getY() == self.targetY) or self.time == 3600:
            done = True

        next_state = self.getState()
        return next_state, reward, done

    def drawWall(self, map, rewards):
        cords = [(i, j) for j in range(self.m) for i in range(self.n)]
        random.shuffle(cords)

        for i in range(int(self.m*self.n*0.3)):
            coordinate = cords.pop()
            map[coordinate[0]][coordinate[1]] = 1

        for i in range(self.n):
            for j in range(self.m):
                rewards[i][j] = -1 if map[i][j] == 0 else -5

        map[self.startY][20] = 0
        map[self.targetY][self.m-1] = 0
        rewards[self.startY][20] = -1
        rewards[self.targetY][self.m-1] = 5

        file = open('engel.txt', 'w')

        for i in range(self.n):
            for j in range(self.m):

                if self.player.getY() == i and self.player.getX() == j:
                    file.write('  O')
                elif self.targetY == i and j == self.m:
                    file.write('  H')
                elif map[i][j] == 0:
                    file.write('  D')
                elif map[i][j] == 1:
                    file.write('  Y')

            file.write('\n')

    def initer(self):
        pygame.display.set_caption("Q Learning")
        self.screen = pygame.display.set_mode((self.width, self.height))
        self.render(1)

    def render(self, episode):
        self.screen.fill((255, 255, 255))
        pygame.draw.rect(self.screen, (0, 0, 0), (0,0, self.width, self.height))

        for i in range(self.m):
            for j in range(self.n):
                if self.map[j][i] == 0:
                    pygame.draw.rect(self.screen,(127,140,141),(i*10+1,j*10+1,9,9))
                elif self.map[j][i] == 1:
                    pygame.draw.rect(self.screen, (44, 62, 80), (i * 10 + 1, j * 10 + 1, 9, 9))

        for cord in self.cordmemory:
            pygame.draw.rect(self.screen, (0, 0, 128), (cord[0] * 10 + 1, cord[1] * 10 + 1, 9, 9))

        self.players.draw(self.screen)
        pygame.draw.rect(self.screen, (255, 0, 0), (20 * 10 + 1, self.startY * 10 + 1, 9, 9))
        pygame.draw.rect(self.screen, (255,0,0), ((self.m-1)*10+1,self.targetY*10+1,9,9))
        self.screen.blit(self.myfont.render('Episode: '+str(episode), True, (255, 255, 255)),(20,20))
        pygame.display.flip()


from importlib import import_module
import config
from blueprints import qa_bp,user_bp
from flask import Flask, render_template, Response,session, g
import os
from exts import  db,mail
from flask_migrate import Migrate
from webmodels import UserModel

if os.environ.get('CAMERA'):
    Camera=import_module('camera_'+os.environ['CAMERA']).Camera
else:
    from camera import Camera

app = Flask(__name__)
app.config.from_object(config)

db.init_app(app)
mail.init_app(app)
migrate=Migrate(app,db)

app.register_blueprint(qa_bp)
app.register_blueprint(user_bp)

@app.before_request
def before_request():
    user_id=session.get("user_id")
    if user_id:
        try:
            user=UserModel.query.get(user_id)
            g.user=user
        except:
            g.user=None

@app.context_processor
def context_processor():
    if hasattr(g,"user"):
        return {"user":g.user}
    else:
        return {}

@app.route('/detect')
def detect_mi():
    #获取用户登录状态
    user_id = session.get("user_id")
    print(user_id)
    #如果用户为登录状态，进入检测页面，else进入登录页面
    if user_id:
        return render_template("detect.html")
    return render_template("login.html")
    #return render_template('detect.html')

@app.route('/')
def hello_world():  # put application's code here
    # return render_template('detect.html')   #用于渲染页面
    return render_template('home.html')   #用于渲染页面

def gen(camera):
    """"视频流生成"""
    while True:
        frame=camera.get_frame()
        yield (b'--frame\r\n'
               b'Content=Type:image/jpeg\r\n\r\n'+frame+b'\r\n')  #设置请求媒体信息

@app.route('/video_feed')
def video_feed():
    """视频流的路线，将其放在img标记的src属性中"""
    return Response(gen(Camera()),mimetype='multipart/x-mixed-replace;boundary=frame')

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

    app.run(host='0.0.0.0',port=5001,threaded=True) #threaded=True为开启多线程


"""
---------------------------
Post Processing - OLS
---------------------------

This class computes the post processing of the simulation.

---------------------------
@autor: Davide Strassera
@first release: 2019-12-21
by Python 3.7
---------------------------

"""
# Import packages
import matplotlib.pyplot as plt
import numpy as np
import scipy.interpolate as interp

class PostProc:

    def __init__(self, accEnvDict, lapSimTimeDict):
        self.size = 10
        # inputs
        self.GGVfull = accEnvDict["GGVfull"]
        self.GGVacc = lapSimTimeDict["GGVacc"]
        self.GGVdec = lapSimTimeDict["GGVdec"]

        self.vxvect = accEnvDict["vxvect"]
        self.ay = accEnvDict["ay"]
        self.axacc = accEnvDict["axacc"]
        self.axdec = accEnvDict["axdec"]
        self.dist = lapSimTimeDict["dist"]
        self.vcar = lapSimTimeDict["vcar"]
        self.laptime = lapSimTimeDict["laptime"]
        self.vcarmax = lapSimTimeDict["vcarmax"]
        self.vxacc = lapSimTimeDict["vxacc"]
        self.vxdec = lapSimTimeDict["vxdec"]
        self.vxcor = lapSimTimeDict["vxcor"]
        # extra channels (AccEnv)
        self.nGear = accEnvDict["nGear"]
        self.EngNm = accEnvDict["EngNm"]
        self.EngRpm = accEnvDict["EngRpm"]
        self.Fzaero = accEnvDict["Fzaero"]
        self.Fxaero = accEnvDict["Fxaero"]
        self.Fxgrip = accEnvDict["Fxgrip"]
        self.Fxdrive = accEnvDict["Fxdrive"]

    def plotAccEnv(self):
        plt.figure(1, figsize=(self.size/2, self.size/2))
        plt.title("Acceleration Envelope")
        plt.plot(self.ay, self.vxvect, 'c-', label="ay")
        plt.plot(self.axacc, self.vxvect, 'm-', label="axacc")
        plt.plot(self.axdec, self.vxvect, 'r-', label="axdec")
        plt.legend()
        plt.xlabel('acceleration [m/s^2]')
        plt.ylabel('velocity [m/s]')
        plt.grid(b=True, which='major', linestyle=':')
        plt.ylim(0, self.vcarmax*1.2)

    def plotGGV(self, bPlotGGVfull=0):
        GGVacc = self.GGVacc
        GGVdec = self.GGVdec
        GGVfull = self.GGVfull

        xyz1 = GGVacc
        X1 = xyz1[:, 0]
        Y1 = xyz1[:, 1]
        Z1 = xyz1[:, 2]
        # Griddata
        ploty1, plotz1, = np.meshgrid(np.linspace(np.min(Y1), np.max(Y1), 30),
                                      np.linspace(np.min(Z1), np.max(Z1), 30))
        plotx1 = interp.griddata((Y1, Z1), X1, (ploty1, plotz1),
                                 method='linear', fill_value=0.0)

        xyz2 = GGVdec
        X2 = xyz2[:, 0]
        Y2 = xyz2[:, 1]
        Z2 = xyz2[:, 2]
        # Griddata
        ploty2, plotz2, = np.meshgrid(np.linspace(np.min(Y2), np.max(Y2), 30),
                                      np.linspace(np.min(Z2), np.max(Z2), 30))
        plotx2 = interp.griddata((Y2, Z2), X2, (ploty2, plotz2),
                                 method='linear', fill_value=0.0)

        xyz3 = GGVfull
        X3 = xyz3[:, 0]
        Y3 = xyz3[:, 1]
        Z3 = xyz3[:, 2]

        fig = plt.figure(5)
        ax = fig.add_subplot(111, projection='3d')
        surf1 = ax.plot_surface(plotx1, ploty1, plotz1,
                                cstride=1, rstride=1, cmap='coolwarm',
                                edgecolor='black', linewidth=0.2,
                                antialiased=True)
        surf2 = ax.plot_surface(plotx2, ploty2, plotz2,
                                cstride=1, rstride=1, cmap='coolwarm',
                                edgecolor='black', linewidth=0.2,
                                antialiased=True)
        if bPlotGGVfull == 1:
            ax.scatter(X3, Y3, Z3, color="black", label="GGVfull sparse")
            ax.legend()

        # Add a color bar which maps values to colors.
        fig.colorbar(surf1, shrink=0.5, aspect=5)

        plt.title("OpenLapSim - Performance Envelope")
        plt.xlabel('ax [m/s^2]')
        plt.ylabel('ay [m/s^2]')
        ax.set_zlabel('velocity [m/s]')

    def plotAccEnvExtra(self):
        f, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4,
                                               figsize=(self.size*1.5,
                                                        self.size/2))
        ax1.set_title("Forces[N] (vcar[m/s])")
        ax1.plot(self.Fzaero, self.vxvect, 'c-', label="Fzaero")
        ax1.plot(self.Fxaero, self.vxvect, 'm-', label="Fxaero")
        ax1.plot(self.Fxgrip, self.vxvect, 'r-', label="FxGrip")
        ax1.plot(self.Fxdrive, self.vxvect, 'g-', label="FxDrive")
        ax1.legend()
        ax1.grid(b=True, which='major', linestyle=':')

        ax2.set_title("Gear (vcar[m/s])")
        ax2.plot(self.nGear, self.vxvect, 'c-', label="nGear")
        ax2.legend()
        ax2.grid(b=True, which='major', linestyle=':')

        ax3.set_title("EngNm (vcar[m/s])")
        ax3.plot(self.EngNm, self.vxvect, 'c-', label="EngNm")
        ax3.legend()
        ax3.grid(b=True, which='major', linestyle=':')

        ax4.set_title("EngRpm (vcar[m/s])")
        ax4.plot(self.EngRpm, self.vxvect, 'c-', label="EngRpm")
        ax4.legend()
        ax4.grid(b=True, which='major', linestyle=':')

    def plotLapTimeSim(self):
        plt.figure(2, figsize=(self.size, self.size/2))
        plt.title("OpenLapSim - Lap Time Simulation")
        plt.plot(self.dist, self.vcar, 'b-', linewidth=2, label="vcar")
        plt.xlabel('distance [m]')
        plt.ylabel('velocity [m/s]')
        plt.legend()
        plt.grid(b=True, which='major', linestyle=':')
        plt.ylim(0, self.vcarmax*1.2)
        plt.xlim(0, max(self.dist))

    def plotLapTimeSimExtra(self):
        plt.figure(3, (self.size, self.size/2))
        plt.title("Lap Time Simulation - Extra")
        plt.plot(self.dist, self.vxcor, 'c-', label="vxcor")
        plt.plot(self.dist, self.vxacc, 'm-', label="vxacc")
        plt.plot(self.dist, self.vxdec, 'r-', label="vxdec")
        plt.plot(self.dist, self.vcar, 'b-', linewidth=2, label="vcar")
        plt.xlabel('distance [m]')
        plt.ylabel('velocity [m/s]')
        plt.legend()
        plt.grid(b=True, which='major', linestyle=':')
        plt.ylim(0, self.vcarmax*1.2)
        plt.xlim(0, max(self.dist))

    def printData(self):
        print("PostProc completed")
        print("---------------------------")
        print("LapTime: ", self.laptime, "[s]")
        print("TopSpeed: ", np.round(self.vcarmax*3.6, 1), "[Km/h]")
        print("---------------------------")