"""
Example of using reduce to analyze car trends
- calculate average profits of cars by mileage as well as fuel efficiency
"""
from functools import reduce

xs = {}

def get_profit(d):
    return d.get('price-sell', 0) - d.get('price-buy', 0)

def low_med_hi(d, key, low, high):
    if d[key] < low:
        return 'low'
    elif d[key] < high:
        return 'med'
    return 'high'

def clean_entry(d):
    r = dict()
    r['profit'] = get_profit(d)
    r['mpg'] = low_med_hi(d, 'mpg', (18, 35))
    r['odo'] = low_med_hi(d, 'odo', (60000, 105000))
    return r

def acc_average(acc, profit):
    acc['total'] = acc.get('total', 0) + profit
    acc['count'] = acc.get('count', 0) + 1
    acc['average'] = acc['total'] / acc['count']
    return acc

def sort_and_add(acc, nxt):
    profit = nxt('profit')
    acc['mpg'][nxt['mpg']] = acc_average(acc['mpg'].get(nxt['mpg'], {}), profit)
    acc['odo'][nxt['odo']] = acc_average(acc['odo'].get(nxt['odo'], {}), profit)
    return acc

reduce(sort_and_add, map(clean_entry, xs), {})


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""SVG Example Program 2: Koch Snowflake."""

from math import cos, pi, sin

def line(xs, ys, length, angle, iter):
    """Draw a line with given length and angle, using iter as number of subdivisions to perform."""
    x = xs[-1]
    y = ys[-1]

    if iter < 1:
        xs.append(x+length*sin(angle))
        ys.append(y+length*cos(angle))
    else:
        line(xs, ys, length / 3.0, angle, iter-1)
        line(xs, ys, length / 3.0, angle+pi/3, iter-1)
        line(xs, ys, length / 3.0, angle-pi/3, iter-1)
        line(xs, ys, length / 3.0, angle, iter-1)

def snowflake(pagesize, size, iterations):
    """Generate a Koch Snowflake to fit the given dimensions."""
    # Starting point.
    xs = [pagesize/2 - size/2]
    ys = [pagesize/2 - size/3]
    angle = pi/2

    # Draw the triangle.
    line(xs, ys, size, angle, iterations)
    line(xs, ys, size, angle - pi*2/3, iterations)
    line(xs, ys, size, angle - pi*4/3, iterations)

    return xs, ys

def svg(xs, ys, pagesize, stroke_width=2):
    """Return the SVG representation of this object."""
    points = " ".join([str(x)+" "+str(y) for (x, y) in zip(xs, ys)])

    return '<?xml version="1.0" standalone="no"?>\n' + \
           '<svg width="{0}" height="{0}" version="1.1" xmlns="http://www.w3.org/2000/svg">\n'.format(pagesize) + \
           '  <rect x="0" y="0" width="{0}" height="{0}" fill="white" stroke-width="0"/>\n'.format(pagesize) + \
           '  <polygon points="{}" stroke="forestgreen" fill="transparent" stroke-width="{}"/>\n'.format(points, stroke_width) + \
           '</svg>\n'

pagesize = 500
size = 250

for i in range(6):
    xs, ys = snowflake(pagesize, size, i)

    with open("02_koch_snowflake_{}.svg".format(i), 'w') as f:
        f.write(svg(xs, ys, pagesize))


# Import Flask modules
from flask import Flask, request, render_template
from flaskext.mysql import MySQL
import os

# Create an object named app
app = Flask(__name__)

# Configure mysql database
app.config['MYSQL_DATABASE_HOST'] = os.getenv('MYSQL_DATABASE_HOST')
app.config['MYSQL_DATABASE_PASSWORD'] = os.getenv('MYSQL_PASSWORD')
app.config['MYSQL_DATABASE_DB'] = os.getenv('MYSQL_DATABASE')
app.config['MYSQL_DATABASE_USER'] = os.getenv('MYSQL_USER')
#app.config['MYSQL_DATABASE_PORT'] = 3306
mysql = MySQL()
mysql.init_app(app)
connection = mysql.connect()
connection.autocommit(True)
cursor = connection.cursor()

# Write a function named `find_persons` which finds persons' record using the keyword from the phonebook table in the db,
# and returns result as list of dictionary
# `[{'id': 1, 'name':'XXXX', 'number': 'XXXXXX'}]`.
def find_persons(keyword):
    query = f"""
    SELECT * FROM phonebook WHERE name like '%{keyword.strip().lower()}%';
    """
    cursor.execute(query)
    result = cursor.fetchall()
    persons =[{'id':row[0], 'name':row[1].strip().title(), 'number':row[2]} for row in result]
    if len(persons) == 0:
        persons = [{'name':'No Result', 'number':'No Result'}]
    return persons

# Write a function named `find_records` which finds phone records by keyword using `GET` and `POST` methods,
# using template files named `index.html` given under `templates` folder
# and assign to the static route of ('/')
@app.route('/', methods=['GET', 'POST'])
def find_records():
    if request.method == 'POST':
        keyword = request.form['username']
        persons = find_persons(keyword)
        return render_template('index.html', persons=persons, keyword=keyword, show_result=True, developer_name='Hasan Kaval')
    else:
        return render_template('index.html', show_result=False, developer_name='Hasan Kaval')

# Add a statement to run the Flask application which can be reached from any host on port 80.
if __name__== '__main__':
    #app.run(debug=True)
    app.run(host='0.0.0.0', port=80)


from django.contrib.auth.models import User
from django.views.generic import ListView
from django.views.generic.base import TemplateView
from django_tables2 import SingleTableView
import django_tables2 as tables
from django_tables2.config import RequestConfig
from django.shortcuts import render
import csv
from django.http import HttpResponse

from django.shortcuts import render, get_object_or_404

from .models import DailyRecord, Participant
from .tables import DailyRecordTable, DailyTable
# Create your views here.

def index(request):
	return render(request, 'dailyrecord/index.html')

def export_daily_record_csv(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="nja_daily_record.csv"'

    writer = csv.writer(response)
    writer.writerow(['S.n.', 'Ogransied Date', 'Program Name',	'Venue', 'Organiser'])

    dailyrecords = DailyRecord.objects.all().values_list('id', 'date_organised', 'program_name', 'venue', 'organiser')
    for dailyrecord in dailyrecords:
        writer.writerow([u''+ str(row)+' ' for row in dailyrecord])

    return response

def dailyrecord_list(request):
	dailyrecords = DailyRecord.public.all()
	context  = {'dailyrecords': dailyrecords}
	return render(request, 'dailyrecord/programs.html', context)

#test method
# def daily_home(request):
# 	 return render(request, 'dailyrecord/daily-home.html', {'dailyrecords': DailyRecord.public.all()})

def daily_home(request):
	table = DailyTable(DailyRecord.objects.all())
	RequestConfig(request).configure(table)
	return render(request, 'dailyrecord/daily-home.html', {'dailyrecords': table})

class ProgramListAll(ListView):
	model = DailyRecord
	template_name = "dailyrecord/programs.html"

class HomePageView(TemplateView):
	template_name = "dailyrecord/index.html"

class AboutPageView(TemplateView):
	template_name = "dailyrecord/about.html"

qs = DailyRecord.public.all()

class DailyActivityPageView(SingleTableView):
	table_class = DailyRecordTable
	queryset = DailyRecord.public.all()
	# queryset = Participant(DailyRecord.objects.all())
	# RequestConfig(request).configure(queryset)
	template_name = 'dailyrecord/daily-activity-record.html'

	def get_context_data(self, **kwargs):
		context = super(DailyActivityPageView, self).get_context_data(**kwargs)
		context['table_participant'] =  Participant(DailyRecord.public.all()),
		return context

class ExtraCuricularActivityPageView(TemplateView):
	template_name = "dailyrecord/extra-curicular-activity.html"


from django.db import models

from django.core.validators import MaxLengthValidator
from django.core.validators import MinValueValidator

class Tag(models.Model):
    name = models.CharField(
        max_length=50,
        unique=True,
        blank=False,
        validators=[MaxLengthValidator(50)]
    )

    def __str__(self):
        return self.name

class Event(models.Model):
    title = models.CharField(
        max_length=100,
        unique=True,
        blank=False,
        validators=[MaxLengthValidator(100)]
    )

    description = models.TextField(
        max_length=500,
        blank=True,
        validators=[MaxLengthValidator(500)]
    )

    price = models.DecimalField(
        max_digits=6,
        decimal_places=2,
        validators=[MinValueValidator(0.00)]
    )

    tags = models.ManyToManyField(Tag)

    def __str__(self):
        return ('%(title)s (%(price)s €)' %
                {'title': self.title, 'price': self.price})


from sys import stdin

def checkRedundantBrackets(expression):
    stk = list()
    for i in range(len(expression)) :
        if (expression[i] == '(') or (find(expression[i])) :
            stk.append(expression[i])
        elif expression[i] == ')' :
            hasOperator = False
            while not isEmpty(stk) and top(stk) != '(' :
                stk.pop()
                hasOperator = True
            if not hasOperator :
                return True
            if not isEmpty(stk) :
                stk.pop()
    return False

def find(ch) :
    if ch =='+' or ch=='-' or ch=='*' or ch=='/':
        return True
    return False

def isEmpty(stack) :
    return len(stack) == 0

def top(stack) :
    return stack[len(stack) - 1]

#main
expression = stdin.readline().strip()

if checkRedundantBrackets(expression) :
	print("true")

else :
	print("false")


import io
import datetime
from decimal import Decimal

from django.core import management
from django.test import TestCase
from django.urls import reverse

from cinnabar.test_helper import TestHelper

from .models import Connection
from .models import Project
from .models import DataAcquisition
from .models import RedmineUser
from .models import ProjectVersion
from .models import CustomField
from .models import IssueTracker
from .models import IssueStatus
from .models import IssuePriority
from .models import Issue
from .models import IssueCustomField
from .models import TimeEntryActivity
from .models import TimeEntry

from .forms import ConnectionFormSet
from .forms import ProjectFormSet

class TestsCmdRedmineAcquireData(TestCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        TestHelper.acquire_projects()
        TestHelper.enable_all_projects()
        TestHelper.acquire_data()

    def setUp(self):
        self.TEST_REDMINE_USER = {
                'redmine_id': 1,
                'login_name': 'testuser',
                'first_name': 'UserName',
                'last_name': 'LastName',
                }
        self.TEST_CUSTOM_FIELDS = [
                {
                    'redmine_id': 1,
                    'name': '備考',
                    'customized_type': 'issue',
                    'field_format': 'string',
                    'multiple': False,
                    },
                {
                    'redmine_id': 2,
                    'name': '費用',
                    'customized_type': 'issue',
                    'field_format': 'int',
                    'multiple': False,
                    },
                {
                    'redmine_id': 3,
                    'name': 'ポイント',
                    'customized_type': 'issue',
                    'field_format': 'float',
                    'multiple': False,
                    },
                {
                    'redmine_id': 4,
                    'name': '関連バージョン',
                    'customized_type': 'issue',
                    'field_format': 'version',
                    'multiple': True,
                    },
                {
                    'redmine_id': 5,
                    'name': '備考',
                    'customized_type': 'project',
                    'field_format': 'string',
                    'multiple': False,
                    },
                ]
        self.TEST_ISSUE_TRACKER = {
                'redmine_id': 2,
                'name': 'Feature',
                }
        self.TEST_ISSUE_STATUS = {
                'redmine_id': 5,
                'name': 'Closed',
                'is_closed': True,
                }
        self.TEST_ISSUE_PRIORITY = {
                'redmine_id': 3,
                'name': 'High',
                }
        self.TEST_PROJECT_VERSION = {
                'redmine_id': 1,
                'name': '2017年3Q',
                }
        self.TEST_ISSUES = [
                {
                    'redmine_id': 1,
                    'project__redmine_id': 1,
                    'tracker__redmine_id': 2,
                    'status__redmine_id': 1,
                    'priority__redmine_id': 2,
                    'parent_id': None,
                    'assigned_user__redmine_id': 1,
                    'version__redmine_id': 1,
                    'name': '材料を用意する',
                    'due_date': None,
                    'done_ratio': 17,
                    'estimated_hours': None,
                    },
                {
                    'redmine_id': 2,
                    'project__redmine_id': 1,
                    'tracker__redmine_id': 2,
                    'status__redmine_id': 5,
                    'priority__redmine_id': 2,
                    'parent_id': 1,
                    'assigned_user__redmine_id': None,
                    'version__redmine_id': None,
                    'name': '冷蔵庫の中身を確認する',
                    'due_date': None,
                    'done_ratio': 100,
                    'estimated_hours': Decimal('0.11'),
                    },
                {
                    'redmine_id': 3,
                    'project__redmine_id': 1,
                    'tracker__redmine_id': 2,
                    'status__redmine_id': 2,
                    'priority__redmine_id': 2,
                    'parent_id': 1,
                    'assigned_user__redmine_id': None,
                    'version__redmine_id': None,
                    'name': 'レシピを確認する',
                    'due_date': None,
                    'done_ratio': 50,
                    'estimated_hours': Decimal('0.25'),
                    },
                ]
        self.TEST_ISSUE_CUSTOM_FIELDS = [
                {
                    'issue__redmine_id': 4,
                    'custom_field__redmine_id': 1,
                    'value_text': 'ついでに牛乳も買ってくる',
                    },
                {
                    'issue__redmine_id': 4,
                    'custom_field__redmine_id': 2,
                    'value_text': '1000',
                    },
                {
                    'issue__redmine_id': 4,
                    'custom_field__redmine_id': 3,
                    'value_text': '0.123456789',
                    },
                ]
        self.TEST_TIME_ENTRY_ACTIVITIES = [
                {
                    'redmine_id': 8,
                    'name': 'Design',
                    },
                {
                    'redmine_id': 9,
                    'name': 'Development',
                    },
                ]
        self.TEST_TIME_ENTRIES = [
                {
                    'redmine_id': 1,
                    'project__name': '肉じゃが',
                    'issue_is_none': False,
                    'issue__redmine_id': 2,
                    'activity__redmine_id': 9,
                    'user__redmine_id': 1,
                    'hours': Decimal('0.05'),
                    },
                {
                    'redmine_id': 2,
                    'project__name': '肉じゃが',
                    'issue_is_none': True,
                    'issue__redmine_id': None,
                    'activity__redmine_id': 9,
                    'user__redmine_id': 1,
                    'hours': Decimal('1.23'),
                    },

                ]

        self.today = datetime.date.today()

    def test_acquisition(self):
        # Check DataAcquisition
        today_da = DataAcquisition.objects.filter(
                date=datetime.date.today())
        self.assertEqual(today_da.count(), 1)

    def test_acquisition_of_redmine_user(self):
        # Check RedmineUser
        for test_cnct in TestHelper.CONNECTIONS:
            testuser = RedmineUser.objects.get(
                    connection__name=test_cnct['name'],
                    redmine_id=self.TEST_REDMINE_USER['redmine_id'])
            self.assertEqual(
                    testuser.login_name,
                    self.TEST_REDMINE_USER['login_name'])
            self.assertEqual(
                    testuser.first_name,
                    self.TEST_REDMINE_USER['first_name'])
            self.assertEqual(
                    testuser.last_name,
                    self.TEST_REDMINE_USER['last_name'])

    def test_acquisition_of_custom_field(self):
        # Check CustomField
        for test_cnct in TestHelper.CONNECTIONS:
            for test_cf in self.TEST_CUSTOM_FIELDS:
                cf = CustomField.objects.get(
                        connection__name=test_cnct['name'],
                        redmine_id=test_cf['redmine_id'])
                self.assertEqual(
                        cf.name,
                        test_cf['name'])
                self.assertEqual(
                        cf.customized_type,
                        test_cf['customized_type'])
                self.assertEqual(
                        cf.field_format,
                        test_cf['field_format'])
                self.assertEqual(
                        cf.multiple,
                        test_cf['multiple'])

    def test_acquisition_of_issue_tracker(self):
        # Check IssueTracker
        for test_cnct in TestHelper.CONNECTIONS:
            issue_tracker = IssueTracker.objects.get(
                    connection__name=test_cnct['name'],
                    redmine_id=self.TEST_ISSUE_TRACKER['redmine_id'])
            self.assertEqual(
                    issue_tracker.name,
                    self.TEST_ISSUE_TRACKER['name'])

    def test_acquisition_of_issue_status(self):
        # Check IssueStatus
        for test_cnct in TestHelper.CONNECTIONS:
            issue_status = IssueStatus.objects.get(
                    connection__name=test_cnct['name'],
                    redmine_id=self.TEST_ISSUE_STATUS['redmine_id'])
            self.assertEqual(
                    issue_status.name,
                    self.TEST_ISSUE_STATUS['name'])

    def test_acquisition_of_issue_priority(self):
        # Check IssuePriority
        for test_cnct in TestHelper.CONNECTIONS:
            issue_priority = IssuePriority.objects.get(
                    connection__name=test_cnct['name'],
                    redmine_id=self.TEST_ISSUE_PRIORITY['redmine_id'])
            self.assertEqual(
                    issue_priority.name,
                    self.TEST_ISSUE_PRIORITY['name'])

    def test_acquisition_of_project_version(self):
        # Check Project Version
        for test_cnct in TestHelper.CONNECTIONS:
            project_version = ProjectVersion.objects.get(
                    project__connection__name=test_cnct['name'],
                    project__redmine_id=TestHelper.SUB_PROJECT['redmine_id'],
                    redmine_id=self.TEST_PROJECT_VERSION['redmine_id'])
            self.assertEqual(
                    project_version.name,
                    self.TEST_PROJECT_VERSION['name'])

    def test_acquisition_of_issue(self):
        # Check Issue
        for test_cnct in TestHelper.CONNECTIONS:
            for test_issue in self.TEST_ISSUES:
                issue = Issue.objects.get(
                        project__connection__name=test_cnct['name'],
                        project__redmine_id=test_issue['project__redmine_id'],
                        redmine_id=test_issue['redmine_id'])
                self.assertEqual(
                        issue.tracker.redmine_id,
                        test_issue['tracker__redmine_id'])
                self.assertEqual(
                        issue.status.redmine_id,
                        test_issue['status__redmine_id'])
                self.assertEqual(
                        issue.priority.redmine_id,
                        test_issue['priority__redmine_id'])
                self.assertEqual(
                        issue.parent_id,
                        test_issue['parent_id'])

                if issue.assigned_user is None:
                    assigned_user__redmine_id = None
                else:
                    assigned_user__redmine_id = issue.assigned_user.redmine_id
                self.assertEqual(
                        assigned_user__redmine_id,
                        test_issue['assigned_user__redmine_id'])

                if issue.version is None:
                    issue_version__redmine_id = None
                else:
                    issue_version__redmine_id = issue.version.redmine_id
                self.assertEqual(
                        issue_version__redmine_id,
                        test_issue['version__redmine_id'])

                self.assertEqual(
                        issue.name,
                        test_issue['name'])
                self.assertEqual(
                        issue.due_date,
                        test_issue['due_date'])
                self.assertEqual(
                        issue.done_ratio,
                        test_issue['done_ratio'])
                self.assertEqual(
                        issue.estimated_hours,
                        test_issue['estimated_hours'])

    def test_acquisition_of_issue_custom_field(self):
        # Check IssueCustomField
        for test_cnct in TestHelper.CONNECTIONS:
            for test_icf in self.TEST_ISSUE_CUSTOM_FIELDS:
                icf = IssueCustomField.objects.get(
                        custom_field__connection__name=test_cnct['name'],
                        issue__redmine_id=test_icf['issue__redmine_id'],
                        custom_field__redmine_id=test_icf['custom_field__redmine_id'])
                self.assertEqual(icf.value_text, test_icf['value_text'])

    def test_acquisition_of_time_entry_activities(self):
        # Check TimeEntryActivity
        for test_cnct in TestHelper.CONNECTIONS:
            for test_tea in self.TEST_TIME_ENTRY_ACTIVITIES:
                tea = TimeEntryActivity.objects.get(
                        connection__name=test_cnct['name'],
                        redmine_id=test_tea['redmine_id'])
                self.assertEqual(tea.name, test_tea['name'])

    def test_acquisition_of_time_entries(self):
        # Check TimeEntry
        for test_cnct in TestHelper.CONNECTIONS:
            for test_te in self.TEST_TIME_ENTRIES:
                te = TimeEntry.objects.get(
                        project__connection__name=test_cnct['name'],
                        redmine_id=test_te['redmine_id'])
                self.assertEqual(te.project.name, test_te['project__name'])
                if test_te['issue_is_none']:
                    self.assertEqual(te.issue, None)
                else:
                    self.assertEqual(te.issue.redmine_id, test_te['issue__redmine_id'])
                self.assertEqual(te.user.redmine_id, test_te['user__redmine_id'])
                self.assertEqual(te.activity.redmine_id, test_te['activity__redmine_id'])
                self.assertEqual(te.hours, test_te['hours'])

    def test_call_twice(self):
        sio = io.StringIO()
        management.call_command('redmine_acquire_data', stdout=sio)
        self.assertIn(
                'Succeess:redmine_acquire_data', sio.getvalue())
        sio = io.StringIO()
        management.call_command('redmine_acquire_data', stdout=sio)
        self.assertIn(
                'Succeess:redmine_acquire_data', sio.getvalue())
        # Check DataAcquisition
        today_da = DataAcquisition.objects.filter(
                date=datetime.date.today())
        self.assertEqual(today_da.count(), 1)


"""OpenAPI core contrib falcon handlers module"""
from json import dumps
from typing import Any
from typing import Dict
from typing import Iterable
from typing import Type

from falcon import status_codes
from falcon.constants import MEDIA_JSON
from falcon.request import Request
from falcon.response import Response

from openapi_core.templating.media_types.exceptions import MediaTypeNotFound
from openapi_core.templating.paths.exceptions import OperationNotFound
from openapi_core.templating.paths.exceptions import PathNotFound
from openapi_core.templating.paths.exceptions import ServerNotFound
from openapi_core.templating.security.exceptions import SecurityNotFound

class FalconOpenAPIErrorsHandler:
    OPENAPI_ERROR_STATUS: Dict[Type[BaseException], int] = {
        ServerNotFound: 400,
        SecurityNotFound: 403,
        OperationNotFound: 405,
        PathNotFound: 404,
        MediaTypeNotFound: 415,
    }

    @classmethod
    def handle(
        cls, req: Request, resp: Response, errors: Iterable[Exception]
    ) -> None:
        data_errors = [cls.format_openapi_error(err) for err in errors]
        data = {
            "errors": data_errors,
        }
        data_str = dumps(data)
        data_error_max = max(data_errors, key=cls.get_error_status)
        resp.content_type = MEDIA_JSON
        resp.status = getattr(
            status_codes,
            f"HTTP_{data_error_max['status']}",
            status_codes.HTTP_400,
        )
        resp.text = data_str
        resp.complete = True

    @classmethod
    def format_openapi_error(cls, error: BaseException) -> Dict[str, Any]:
        if error.__cause__ is not None:
            error = error.__cause__
        return {
            "title": str(error),
            "status": cls.OPENAPI_ERROR_STATUS.get(error.__class__, 400),
            "type": str(type(error)),
        }

    @classmethod
    def get_error_status(cls, error: Dict[str, Any]) -> int:
        return int(error["status"])


import discord
from discord.ext import commands
import re
import json
from pyNyaav2 import SearchTorrent

def setup(bot):
	bot.add_cog(Nyaav2(bot))

with open('conf.json') as fp:
	conf = json.load(fp)
prefix = conf['prefix']
NyaaUser = conf['nyaausername']
NyaaPass = conf['nyaapassword']

def checkCategory(b):
	categorylist = ['all', 'amv', 'anime_eng', 'anime_non-eng', 'anime_raw', 'audio_lossless', 'audio_lossy', 'books_eng', 'books_non-eng', 'books_raw', 'la_eng', 'la_idolpv', 'la_non-eng', 'la_raw', 'pics_graphics', 'pics_photos', 'sw_apps', 'sw_games']
	if b not in categorylist:
		return False
	return True

def searchTor(keyword, category, number):
	number = number - 1
	parsedQuery = []
	search = SearchTorrent(NyaaUser, NyaaPass, keyword, category, 1)
	for query in search:
		NAME = query['name']
		ID = query['id']
		SUBMIT = query['submitter']
		if SUBMIT is None:
			SUBMIT = 'Anonymous'
		else:
			pass
		CREADATE = query['creation']
		SIZE = query['filesize']
		CATEG = query['category'] + ' (' + query['category_id'] + ')'
		STATS = '**Seeders**: {} || **Leechers**: {} || **Completed**: {}'.format(query['seeders'], query['leechers'], query['completed'])
		DLLINK = 'https://nyaa.si{}'.format(query['download_link'])
		MAGNET = query['magnet_link']
		TORURL = query['url']
		TORHASH = query['hash']

		TRUST = query['is_trusted']
		REMAKE = query['is_remake']

		if TRUST:
			color = '0x72ee6f'
		elif REMAKE:
			color = '0xf16b6f'
		else:
			color = '0x73e1ee'

		temptext = {'name': NAME, 'id': ID, 'size': SIZE, 'submitter': SUBMIT, 'category': CATEG, 'stats': STATS, 'DL': DLLINK, 'magnet': MAGNET, 'URL': TORURL, 'hash': TORHASH, 'date': CREADATE, 'tortype': color, 'dataLength': str(len(search))}
		parsedQuery.append(temptext)

	return parsedQuery[number]

class Nyaav2:
	def __init__(self, bot):
		self.bot = bot

	def __local_check(self, ctx):
		if not ctx.guild:
			raise commands.NoPrivateMessage('This command cannot be used in a private message.')
		return True

	async def __error(self, ctx, error):
		if not isinstance(error, commands.UserInputError):
			raise error

		try:
			await ctx.send(error)
		except discord.Forbidden:
			pass

	@commands.command(pass_context=True)
	async def nyaacategory(self, ctx):
		listcategory = {'all': 'All category', 'amv': 'Anime music video', 'anime_eng': 'Anime - English-translated', 'anime_non-eng': 'Anime Non-english-translated', 'anime_raw': 'Anime - Raw', 'audio_lossless': 'Audio - Lossless', 'audio_lossy': 'Audio - Lossy', 'books_eng': 'Literature - English', 'books_non-eng': 'Literature - Non-english', 'books_raw': 'Literature - Raw', 'la_eng': 'Live action - English', 'la_idolpv': 'Live action - Idol/PV', 'la_non-eng': 'Live action - Non-english', 'la_raw': 'Live action - Raw', 'pics_graphics': 'Pictures - Graphics', 'pics_photos': 'Pictures - Photos', 'sw_apps': 'Software - Apps', 'sw_games': 'Software - Games'}
		text = '**Nyaa v2 Category List**\n'
		for kc, vc in listcategory:
			temptext = '- **{}** == {}'.format(kc, vc)
			text += temptext

		m = await self.bot.say(text)

		def checkReaction(reaction, user):
			e = str(reaction.emoji)
			return e.startswith('✅')
		await self.bot.add_reaction(m, '✅')

		mm = await self.bot.wait_for_reaction(message=m, user=ctx.message.author, timeout=10, check=checkReaction)
		if mm is None:
			await self.bot.delete_message(ctx.message)
			await self.bot.delete_message(m)
		elif '✅' in str(mm.reaction.emoji):
			await self.bot.delete_message(ctx.message)
			await self.bot.delete_message(m)

	@commands.command(pass_context=True, aliases=['nyaav2'])
	async def nyaasearch(self, ctx, category, *, keyword):
		"""Search nyaa torrent"""
		check = checkCategory(category)
		if not check:
			await self.bot.say('Category doesn\'t exist, please refer to {}nyaacategory'.format(prefix))
			raise ValueError('nyaav2: category are not exist')
		else:
			pass

		init = searchTor(keyword, category, 1)

		maxPage = int(init['dataLength'])
		firstRun = True
		while True:
			if firstRun:
				firstRun = False
				num = 1
				#temptext = {'name': NAME, 'id': ID, 'size': SIZE, 'submitter': SUBMIT, 'category': CATEG, 'stats': STATS, 'DL': DLLINK, 'magnet': MAGNET, 'URL': TORURL, 'hash': TORHASH, 'date': CREADATE, 'tortype': color, 'dataLength': str(len(search))}
				find = searchTor(keyword, category, 1)
				constrctInfo = '**ID**: {}\n**Name**: {}\n**Size**: {}\n**Category**: {}\n**Statistics**: {}\n**Submitted by: {}'.format(find['id'], find['name'],  find['size'], find['category'], find['stats'], find['submitter'])
				constrctDownload = '📥 || [URL]({}) || [Download]({}) || [Magnet]({})'.format(find['URL'], find['DL'], find['magnet'])
				constrctFoot = '⏲ || Posted at {} || {}'.format(find['date'], find['hash'])
				embed=discord.Embed(title="Nyaa v2 Search", url='https://nyaa.si', color=int(find['tortype']))
				embed.set_thumbnail(url="https://i2.wp.com/nyaa.si/static/img/avatar/default.png?ssl=1")
				embed.add_field(name='Information', value=constrctInfo, inline=False)
				embed.add_field(name='Link', value=constrctDownload, inline=False)
				embed.set_footer(text=constrctFoot)
				msg = await self.bot.say(embed=embed)

			if maxPage == 1 and num == 1:
				toReact = ['✅']
			elif num == 1:
				toReact = ['⏩', '✅']
			elif num == maxPage:
				toReact = ['⏪', '✅']
			elif num > 1 and num < maxPage:
				toReact = ['⏪', '⏩', '✅']
			for reaction in toReact:
				await self.bot.add_reaction(msg, reaction)
			#feel free to change ✅ to 🆗 or the opposite
			def checkReaction(reaction, user):
				e = str(reaction.emoji)
				return e.startswith(('⏪', '⏩', '✅'))

			res = await self.bot.wait_for_reaction(message=msg, user=ctx.message.author, timeout=10, check=checkReaction)
			if res is None:
				await self.bot.delete_message(ctx.message)
				await self.bot.delete_message(msg)
				break
			elif '⏪' in str(res.reaction.emoji):
				num = num - 1
				find = searchTor(keyword, category, 1)
				constrctInfo = '**ID**: {}\n**Name**: {}\n**Size**: {}\n**Category**: {}\n**Statistics**: {}\n**Submitted by: {}'.format(find['id'], find['name'],  find['size'], find['category'], find['stats'], find['submitter'])
				constrctDownload = '📥 || [URL]({}) || [Download]({}) || [Magnet]({})'.format(find['URL'], find['DL'], find['magnet'])
				constrctFoot = '⏲ || Posted at {} || {}'.format(find['date'], find['hash'])
				embed=discord.Embed(title="Nyaa v2 Search", url='https://nyaa.si', color=int(find['tortype']))
				embed.set_thumbnail(url="https://i2.wp.com/nyaa.si/static/img/avatar/default.png?ssl=1")
				embed.add_field(name='Information', value=constrctInfo, inline=False)
				embed.add_field(name='Link', value=constrctDownload, inline=False)
				embed.set_footer(text=constrctFoot)
				await self.bot.clear_reactions(msg)
				msg = await self.bot.edit_message(msg, embed=embed)
			elif '⏩' in str(res.reaction.emoji):
				num = num + 1
				find = searchTor(keyword, category, 1)
				constrctInfo = '**ID**: {}\n**Name**: {}\n**Size**: {}\n**Category**: {}\n**Statistics**: {}\n**Submitted by: {}'.format(find['id'], find['name'],  find['size'], find['category'], find['stats'], find['submitter'])
				constrctDownload = '📥 || [URL]({}) || [Download]({}) || [Magnet]({})'.format(find['URL'], find['DL'], find['magnet'])
				constrctFoot = '⏲ || Posted at {} || {}'.format(find['date'], find['hash'])
				embed=discord.Embed(title="Nyaa v2 Search", url='https://nyaa.si', color=int(find['tortype']))
				embed.set_thumbnail(url="https://i2.wp.com/nyaa.si/static/img/avatar/default.png?ssl=1")
				embed.add_field(name='Information', value=constrctInfo, inline=False)
				embed.add_field(name='Link', value=constrctDownload, inline=False)
				embed.set_footer(text=constrctFoot)
				await self.bot.clear_reactions(msg)
				msg = await self.bot.edit_message(msg, embed=embed)
			elif '✅' in str(res.reaction.emoji):
				await self.bot.delete_message(ctx.message)
				await self.bot.delete_message(msg)
				break

	@nyaasearch.error
	async def nyaasearch_handler(self, error, ctx):
		if isinstance(error, commands.MissingRequiredArgument):
			await self.bot.say("Argumen yang benar adalah:\n**{}nyaasearch <category> <keyword>**\nUntuk melihat kategori: {}nyaacategory".format(prefix, prefix))
		if isinstance(error, Exception):
			await self.bot.say("Terjadi kesalahan di bagian bot, tolong perbaiki dan restart bot")
		print(error)


import time
from typing import List, Dict

from app.Redis.utils import encode_ok_response

def ping(rest: List[str], *args, **kwargs) -> bytes:
    if not rest:
        return encode_ok_response("PONG")
    else:
        return encode_ok_response(rest[0])

def echo(rest: List[str], *args, **kwargs) -> bytes:
    if not rest:
        raise ValueError("ECHO needs a parameter")
    else:
        return encode_ok_response(rest[0], bulk_str=True)

kv_map: Dict[str, str] = dict()
expiry_time: Dict[str, float] = dict()

def set(rest: List[str], *args, **kwargs) -> bytes:
    if len(rest) < 2:
        raise ValueError("SET needs 2 parameters")

    key, val = rest[0], rest[1]

    if len(rest) == 4 and rest[2].lower() == 'px':
        # We need to set expiry as well
        offset = int(rest[3])
        expiry_time[key] = time.time() + offset / 1000
    else:
        expiry_time[key] = -1

    kv_map[key] = val

    return encode_ok_response("OK")

def get(rest: List[str], *args, **kwargs) -> bytes:
    if not rest:
        raise ValueError("GET needs 1 parameters")

    key = rest[0]

    exp_time = expiry_time.get(key, -1)

    # Check if this key was expired.
    if exp_time != -1 and exp_time < time.time():
        del expiry_time[key]
        del kv_map[key]

    return encode_ok_response(kv_map.get(key, None), bulk_str=True)


# coding: utf-8

# ### Notebook to configure model

# In[1]:

import time
import math
import copy

import numpy as np
import pandas as pd

import matplotlib
import matplotlib.pyplot as plt

import seaborn as sns
sns.set_context(context="talk")

import torch
import torch.nn as nn
import tensorflow as tf
import torch.nn.functional as F
from torchvision import datasets
import torchvision.transforms as transforms
from torch.autograd import Variable
from torch.utils import data

# In[2]:

isRetrain = False

# Hyperparameter

QUERY_SIZE = 10
EMBEDDING_SIZE = 50

# HIDDEN_SIZE = 512
# ATTENTION_SIZE = 64

parameter_name = 'lr5em2_lm1em4_batch100_q1_e50'

LEARNING_RATE = 5*1e-2
BATCH_SIZE = 100
LAMBDA = 1e-4

num_epochs = 100

VOCAB_SIZE = 5
NUM_CLASSES = 2

# Data-specific

READ_LENGTH = 100

# # KRAS
# GENOME_START = 25204789
# GENOME_END = 25250936

# # NOTCH1
# GENOME_START = 136494433
# GENOME_END = 136545786

# NRAS
file_name = 'NRAS-germline'
GENOME_START = 114704464
GENOME_END = 114716894

GENOME_LENGTH = GENOME_END - GENOME_START + 1
CONTEXT_SIZE = GENOME_LENGTH

ref_names = ["class", "ref", "sequence"]
VOCAB = ['N','A','T','C','G']

# In[3]:

def load_ref_data(file_name, sample_ratio= 1, n_class=2, names=ref_names):

    csv_file = pd.read_csv(file_name, names=ref_names)
    shuffle_csv = csv_file.sample(frac=sample_ratio).reset_index()
#     x = pd.Series(shuffle_csv["sequence"])
    x = list(shuffle_csv["sequence"])
#     ref = pd.Series(shuffle_csv["ref"])
    ref = list(shuffle_csv["ref"])
    y = pd.Series(shuffle_csv["class"])
    y = to_one_hot(y, n_class)
    print(y.shape)
#     print(type(x))
#     print(type(y))
#     print(type(ref))

    return x, ref, y

# In[4]:

def create_synthetic_data(file_name, cancer_genes, benign_genes, num_patients=10, num_reads_per_patients=3, read_length=READ_LENGTH, genome_length=GENOME_LENGTH, vocab=VOCAB, isSomatic=True, print_seq=False):

    seq_list = np.random.choice(vocab, [num_patients, genome_length], replace=True)
    backup_seq_list = seq_list

    for loc, mutation in cancer_genes.items():
        seq_list[np.random.choice(num_patients, int(num_patients*mutation[1]), replace=False), loc] = mutation[0]

    genomes = []
    for r in range(seq_list.shape[0]):
        seq = ''.join(seq_list[r,:])
        if print_seq:
            print(seq)
        genomes.append(seq)

    locs = np.random.choice(genome_length-read_length, num_patients*num_reads_per_patients)

    file = open('./tumor-genome-'+file_name+'.txt','w')
    count = 0
    reads = []
    for genome in genomes:
        for t in range(num_reads_per_patients):
            index = count*num_reads_per_patients+t
            reads.append(genome[locs[index]:locs[index]+read_length])
        file.write("%s\n" % genome)
        count = count + 1
    file.close()

    file = open('./tumor-syn-'+file_name+'.csv','w')
    for r in range(num_patients*num_reads_per_patients):
        file.write("1, %d, %s\n" % (locs[r], reads[r]))
    file.close()

    tumor_locs = locs
    tumor_reads = reads

    if isSomatic:
        seq_list = backup_seq_list
    else:
        seq_list = np.random.choice(vocab, [num_patients, genome_length], replace=True)

    for loc, mutation in benign_genes.items():
        seq_list[np.random.choice(num_patients, int(num_patients*mutation[1]), replace=False), loc] = mutation[0]

    genomes = []
    for r in range(seq_list.shape[0]):
        seq = ''.join(seq_list[r,:])
        if print_seq:
            print(seq)
        genomes.append(seq)

    locs = np.random.choice(genome_length-read_length, num_patients*num_reads_per_patients)

    file = open('./normal-genome-'+file_name+'.txt','w')
    count = 0
    reads = []
    for genome in genomes:
        for t in range(num_reads_per_patients):
            index = count*num_reads_per_patients+t
            reads.append(genome[locs[index]:locs[index]+read_length])
        file.write("%s\n" % genome)
        count = count + 1
    file.close()

    file = open('./normal-syn-'+file_name+'.csv','w')
    for r in range(num_patients*num_reads_per_patients):
        file.write("0, %d, %s\n" % (locs[r], reads[r]))
    file.close()

    normal_locs = locs
    normal_reads = reads

    file = open('./syn-'+file_name+'.csv','w')
    for r in range(num_patients*num_reads_per_patients):
        file.write("1,%d,%s\n" % (tumor_locs[r], tumor_reads[r]))
        file.write("0,%d,%s\n" % (normal_locs[r], normal_reads[r]))
    file.close()

    return './syn-'+file_name+'.csv'

# In[5]:

def to_one_hot(y, n_class):

    return np.eye(n_class)[y.astype(int)]

# In[6]:

def split_ref_dataset(x_test, y_test, ref_test, dev_ratio):

    test_size = len(x_test)
    print(test_size)
    dev_size = (int)(test_size * dev_ratio)
    print(dev_size)

    x_dev = x_test[:dev_size]
    x_test = x_test[dev_size:]
    y_dev = y_test[:dev_size]
    y_test = y_test[dev_size:]
    ref_dev = ref_test[:dev_size]
    ref_test = ref_test[dev_size:]

    return x_test, x_dev, y_test, y_dev, ref_test, ref_dev, dev_size, test_size - dev_size

# In[7]:

class TensorizedReadDataset(torch.utils.data.DataLoader):
    'Characterizes a Tensorized dataset for genome reads in PyTorch'

    def __init__(self, reads, ref_locs, labels, read_length=100, genome_start=0, genome_end=0):
#         super(TensorizedReadDataset, self).__init__()

        # self.read_length = read_length
        self.labels = labels
        self.reads = reads
        self.ref_locs = ref_locs
        self.genome_start = genome_start
        self.genome_end = genome_end

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

    def __getitem__(self, index):

        vals = list(self.reads[index])
        read_length = len(vals)
        locs = list(np.arange(self.ref_locs[index]-self.genome_start,self.ref_locs[index]+read_length-self.genome_start))

#         print(len(vals))
#         print(len(locs))

        vals2idx = {'N': 0, 'A': 1, 'C': 2, 'T': 3, 'G': 4}
#         read = torch.LongTensor(np.array([vals2idx[val]+loc*len(vals2idx) for val, loc in zip(vals, locs)], dtype=int), requires_grad=False)

        read = torch.autograd.Variable(torch.LongTensor(np.array([vals2idx[val]+loc*len(vals2idx) for val, loc in zip(vals, locs)], dtype=int)), requires_grad=False)

        X = read
        Y = self.labels[index,:]

#         torch.LongTensor(self.labels[index,:])

        return X, Y

# In[8]:

class SequenceAttentionClassifier(nn.Module):

    def __init__(self, genome_length, vocab_size=5, query_size=10, embedding_size=128, num_classes=2):

        super(SequenceAttentionClassifier, self).__init__()

        self.genome_length = genome_length
        self.vocab_size = vocab_size
        self.query_size = query_size
        self.embedding_size = embedding_size
        self.num_classes = num_classes
        self.K = nn.Embedding(vocab_size*genome_length, embedding_size)
        self.V = nn.Embedding(vocab_size*genome_length, query_size)
        self.W = nn.Linear(query_size, num_classes)
        self.Q = nn.Linear(embedding_size, query_size)

    def forward(self, read):

        # 'read' here should be mapped to a flattened form where X_ij = 1 maps to i*vocab_size + j
        K_lookup = self.K(read) # Get the relevant keys
        V_lookup = self.V(read) # Get the relevant values

        # Get the attention weights
        logits = self.Q(K_lookup) / math.sqrt(self.embedding_size)
        probs = F.softmax(logits, dim = 0)

        # Calculate the covariates for the logistic regression
#         X = torch.matmul(probs.transpose(1,2), V_lookup)
#         X = probs * V_lookup
        X = (probs * V_lookup).sum(dim=1)

        # Right now we can just ignore the fact that we're doing a linear-transform.
        # In the future we'll add nonlinearities

        # Return the logits for the classifier
        return self.W(X), K_lookup, V_lookup

# In[9]:

def GetTensorBatch(reads, ref_locs, labels, batch_size=100, genome_start=0, genome_end=0):

    batches = {}
    set_size = len(ref_locs)
    for batch in range(set_size // batch_size):

        x_batch = []
        y_batch = []

        for index in range(batch_size):

            vals = list(reads[index])
            read_length = len(vals)
            locs = list(np.arange(ref_locs[index]-genome_start,ref_locs[index]+read_length-genome_start))

            vals2idx = {'N': 0, 'A': 1, 'C': 2, 'T': 3, 'G': 4}
            read = torch.autograd.Variable(torch.LongTensor(np.array([vals2idx[val]+loc*len(vals2idx) for val, loc in zip(vals, locs)], dtype=int)), requires_grad=False)

            X = read
            Y = labels[index,:]
            x_batch.append(X)
            y_batch.append(Y)

        batches[batch] = [x_batch, y_batch]

    return batches

# In[14]:

# load data

x_train, refs_train, y_train = load_ref_data("../data/ref-germline-NRAS-train39000.csv", sample_ratio=1)
x_hardtest, refs_hardtest, y_hardtest = load_ref_data("../data/ref-germline-NRAS-test1000.csv", sample_ratio=1)

# split dataset to train and validation
x_train, x_test, y_train, y_test, refs_train, refs_test, test_size, train_size = split_ref_dataset(x_train, y_train, refs_train, 0.2)

# split dataset to test and dev
x_softtest, x_val, y_softtest, y_val, refs_softtest, refs_val, val_size, softtest_size = split_ref_dataset(x_test, y_test, refs_test, 0.5)

print("Training size: ", train_size)
print("Soft Test size: ", softtest_size)
print("Hard Test size: ", len(y_hardtest))
print("Validation size: ", val_size)

# In[15]:

# Generators
# train_dataset = TensorizedReadDataset(reads=x_train,
#                                       ref_locs=refs_train,
#                                       labels=y_train,
#                                       read_length=READ_LENGTH,
#                                       genome_start=GENOME_START,
#                                       genome_end=GENOME_END)

# hardval_dataset = TensorizedReadDataset(reads=x_test,
#                                         ref_locs=refs_test,
#                                         labels=y_test,
#                                         read_length=READ_LENGTH,
#                                         genome_start=GENOME_START,
#                                         genome_end=GENOME_END)

# softval_dataset = TensorizedReadDataset(reads=x_softval,
#                                         ref_locs=refs_softval,
#                                         labels=y_softval,
#                                         read_length=READ_LENGTH,
#                                         genome_start=GENOME_START,
#                                         genome_end=GENOME_END)

# # Input pipeline
# train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
#                                            batch_size=BATCH_SIZE,
#                                            shuffle=True)

# hardtest_loader = torch.utils.data.DataLoader(dataset=hardval_dataset,
#                                              batch_size=BATCH_SIZE,
#                                              shuffle=True)

# softval_loader = torch.utils.data.DataLoader(dataset=softval_dataset,
#                                              batch_size=BATCH_SIZE,
#                                              shuffle=True)

train_loader = GetTensorBatch(reads=x_train,
                                      ref_locs=refs_train,
                                      labels=y_train,
                                      batch_size=BATCH_SIZE,
                                      genome_start=GENOME_START,
                                      genome_end=GENOME_END)

val_loader = GetTensorBatch(reads=x_val,
                                      ref_locs=refs_val,
                                      labels=y_val,
                                      batch_size=BATCH_SIZE,
                                      genome_start=GENOME_START,
                                      genome_end=GENOME_END)

hardtest_loader = GetTensorBatch(reads=x_hardtest,
                                        ref_locs=refs_hardtest,
                                        labels=y_hardtest,
                                        batch_size=BATCH_SIZE,
                                        genome_start=GENOME_START,
                                        genome_end=GENOME_END)

softtest_loader = GetTensorBatch(reads=x_softtest,
                                        ref_locs=refs_softtest,
                                        labels=y_softtest,
                                        batch_size=BATCH_SIZE,
                                        genome_start=GENOME_START,
                                        genome_end=GENOME_END)

# In[16]:

# isRetrain = True

# In[17]:

model = SequenceAttentionClassifier(genome_length=GENOME_LENGTH,

                                    vocab_size=VOCAB_SIZE,
                                    query_size=QUERY_SIZE,
                                    embedding_size=EMBEDDING_SIZE,
                                    num_classes=NUM_CLASSES)

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=LEARNING_RATE)

if isRetrain:
    model.load_state_dict(torch.load('./'+file_name+'_model.pth'))

# In[18]:

f1 = open('cv_acc_each_epoch_'+parameter_name+'.txt', 'a+')
f2 = open('cv_loss_each_batch_'+parameter_name+'.txt', 'a+')
f3 = open('cv_loss_val_each_batch_'+parameter_name+'.txt', 'a+')
f4 = open('cv_acc_each_batch_'+parameter_name+'.txt', 'a+')

lam = LAMBDA

# Training process
for epoch in range(num_epochs):
#     b = 0 # count batch
    for b, [x_batch, y_batch] in train_loader.items():

        full_loss = 0
        full_data_loss = 0
        optimizer.zero_grad()
        for c in range(BATCH_SIZE):

            x_input = x_batch[c].view(1, x_batch[c].shape[0])
            y_input = torch.Tensor(y_batch[c]).type(torch.float64).view(1, y_batch[c].shape[0])

            outputs, K, V = model(x_input)
            data_loss = criterion(outputs, torch.max(y_input, 1)[1])

            reg_loss = (torch.norm(K,2,1).mean() + torch.norm(V,2,1).mean())*lam
            loss = data_loss + reg_loss
            full_loss = full_loss + loss
            full_data_loss = full_data_loss + data_loss

            loss.backward()
            optimizer.step()
            f2.write(repr(data_loss)+"\n")

#         full_loss = full_loss / 2
        # full_loss.backward()
        # optimizer.step()

        # if (b + 1) % 1 == 0:
        print("Epoch {}, Batch {}, loss :{}".format(epoch + 1, b + 1, full_loss))
#         b = b + 1

        # loss_each_batch.append(full_loss)
        # f2.write(repr(full_data_loss)+"\n")

        full_loss = 0
        full_data_loss = 0

        correct = 0
        total = 0
        for b, [x_batch, y_batch] in val_loader.items():
            full_vloss = 0
            for c in range(BATCH_SIZE):

                x_input = x_batch[c].view(1, x_batch[c].shape[0])
                y_input = torch.Tensor(y_batch[c]).type(torch.float64).view(1, y_batch[c].shape[0])

                outputs, K, V = model(x_input)
                _, predicted = torch.max(outputs.data, 1)

                data_vloss = criterion(outputs, torch.max(y_input, 1)[1])
                full_vloss = full_vloss + data_vloss

                total += len(y_input)
                correct += (predicted == torch.max(y_input.type(torch.LongTensor), 1)[1]).sum()

                f3.write(repr(data_vloss)+"\n")
            # f3.write(repr(full_vloss)+"\n")

        acc = int(correct) / total
        print('Validation Accuracy: {}%'.format(100 * acc))
        # acc_each_epoch.append(acc)
        f4.write(repr(acc)+"\n")

    correct = 0
    total = 0
    for b, [x_batch, y_batch] in val_loader.items():
        for c in range(BATCH_SIZE):

            x_input = x_batch[c].view(1, x_batch[c].shape[0])
            y_input = torch.Tensor(y_batch[c]).type(torch.float64).view(1, y_batch[c].shape[0])

            outputs, K, V = model(x_input)
            _, predicted = torch.max(outputs.data, 1)
            total += len(y_input)
            correct += (predicted == torch.max(y_input.type(torch.LongTensor), 1)[1]).sum()
    acc = int(correct) / total
    print('Validation Accuracy: {}%'.format(100 * acc))
    # acc_each_epoch.append(acc)
    f1.write(repr(acc)+"\n")

f1.close()
f2.close()
f3.close()
f4.close()

# In[202]:

torch.save(model, './full_' + file_name + '_' + parameter_name + '_model.pt')
torch.save(model.state_dict(), './'+file_name + '_' + parameter_name + '_model.pth')

# In[ ]:

# plt.rcParams['figure.figsize'] = [30, 5]

# plt.plot(np.arange(len(loss_each_batch))+1, loss_each_batch)
# plt.xlim(1, len(loss_each_batch))
# plt.title('loss vs. batchs')
# plt.show()

# plt.plot(np.arange(len(acc_each_batch))+1, acc_each_batch)
# plt.xlim(1, len(acc_each_batch))
# plt.title('accuracy vs. batchs')
# plt.show()

# plt.plot(np.arange(len(acc_each_epoch))+1, acc_each_epoch)
# plt.xlim(1, len(acc_each_epoch))
# plt.title('accuracy vs. epochs')
# plt.show()

# In[10]:

# model = torch.load('./full_'+file_name+'_model.pt')
# model.load_state_dict(torch.load('./'+file_name+'_model.pth'))

# In[16]:

correct = 0
total = 0
for b, [x_batch, y_batch] in softtest_loader.items():
    for c in range(BATCH_SIZE):

        x_input = x_batch[c].view(1, x_batch[c].shape[0])
        y_input = torch.Tensor(y_batch[c]).type(torch.float64).view(1, y_batch[c].shape[0])

        outputs, K, V = model(x_input)
        _, predicted = torch.max(outputs.data, 1)
        total += len(y_input)
        correct += (predicted == torch.max(y_input.type(torch.LongTensor), 1)[1]).sum()
acc = int(correct) / total
print('Soft Test Accuracy: {}%'.format(100 * acc))

correct = 0
total = 0
for b, [x_batch, y_batch] in hardtest_loader.items():
    for c in range(BATCH_SIZE):

        x_input = x_batch[c].view(1, x_batch[c].shape[0])
        y_input = torch.Tensor(y_batch[c]).type(torch.float64).view(1, y_batch[c].shape[0])

        outputs, K, V = model(x_input)
        _, predicted = torch.max(outputs.data, 1)
        total += len(y_input)
        correct += (predicted == torch.max(y_input.type(torch.LongTensor), 1)[1]).sum()
acc = int(correct) / total
print('Hard Test Accuracy: {}%'.format(100 * acc))


# import cv2
# img = cv2.imread("face.jpg",1)

# # resize the image to smaller image
# resized = cv2.resize(img, (int(img.shape[1]/2),int(img.shape[0]/2)))
# cv2.imshow("legend", resized)
# # window will stay open for 2000 mil/sec
# cv2.waitKey(2000)

# cv2.destroyAllWindows()

import cv2

# create a cascadeclassifier
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

# read our image
img = cv2.imread("face.jpg")
# read the image as a gray scale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# search for the cordinates of the image, set scale factor = 1.05 and minnighbors = 5
faces = face_cascade.detectMultiScale(gray_img,  1.05, 5)

print(type(faces))
print(faces)

color = (0,255,0)
# for loop to display a box around the face

for x,y,w,h in faces:
    img = cv2.rectangle(img, (x,y), (x+w, y+h), color , 3 )
cv2.imshow("gray", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

face and motion detector/face detection/face_detection.py


import logging
from dataclasses import asdict
from flask import Blueprint, request
from app.models.signup import SignupFormRequest
from app.models.responses.signup import SignupResponse, SignupErrorResponse
from exceptions.validation import ValidationError

LOG = logging.getLogger(__name__)

__all__ = ["signup"]

signup = Blueprint('signup', __name__, url_prefix="/v1/signup")

def _error_response(sfr: SignupFormRequest, exc: ValidationError) -> SignupResponse:
    """Returns correct error response.

    If the validation error is on the honeypot field then we return a successfull message
    with an error to tell the front-end to show a successful message but the signup
    did not actually go through.

    sfr (SignupFormRequest): Form data instance.

    returns: SignupResponse
    """
    error = SignupErrorResponse(
        field_name=exc.field_name, message=exc.message
    )

    response = SignupResponse(
        success=False,
        error=error,
        message="Signup form error.",
        request=sfr
    )
    return response

def _success_response(sfr: SignupFormRequest) -> SignupResponse:
    """Returns success response.

    sfr (SignupFormRequest): Form data istance.

    returns: SignupResponse
    """
    return SignupResponse(
        success=True,
        error=SignupErrorResponse(),
        message="Successfully signed up",
        request=sfr
    )

@signup.route("", methods=["POST"])
def signup_form():
    """Handler for signup form."""
    sfr = SignupFormRequest(**request.get_json())
    try :
        sfr.validate()
    except ValidationError as exc:
        LOG.exception(exc)
        return asdict(_error_response(sfr, exc)), 400

    return asdict(_success_response(sfr)), 200


#!/usr/bin/python3
"""
City
"""
from flask import jsonify, abort, request
from api.v1.views import app_views
from models import storage
from models.place import Place
from models.city import City

@app_views.route('/cities/<city_id>/places',
                 methods=['GET'], strict_slashes=False)
def show_places(city_id):
    """This functions lists all the users"""
    list_t = []
    cities = storage.all("City")
    c_id = "City." + city_id
    if cities.get(c_id) is None:
        abort(404)
    else:
        places = storage.all("Place")
        for place in places.values():
            if place.city_id == city_id:
                list_t.append(place.to_dict())
    return jsonify(list_t)

@app_views.route('places/<place_id>', methods=['GET'], strict_slashes=False)
def get_place(place_id):
    """This functions get a specific state by id"""
    places = storage.all("Place")
    p_id = "Place." + place_id
    if places.get(p_id) is None:
        abort(404)
    place = places.get(p_id).to_dict()
    return place

@app_views.route('/places/<place_id>',
                 methods=['DELETE'], strict_slashes=False)
def delete_place(place_id):
    """This function delete a state by id"""
    places = storage.all(Place)
    p_id = "Place." + place_id
    to_del = places.get(p_id)
    if to_del is None:
        abort(404)
    storage.delete(to_del)
    storage.save()
    return jsonify({}), 200

@app_views.route('/cities/<city_id>/places',
                 methods=['POST'], strict_slashes=False)
def create_place(city_id):
    """This function creates a new place"""
    data = request.get_json()
    c_id = storage.get("City", city_id)
    if c_id is None:
        abort(404)
    if not data:
        abort(400, 'Not a JSON')
    if data.get('user_id') is None:
        abort(400, 'Missing user_id')
    u_id = data['user_id']
    look_user = storage.get("User", u_id)
    if look_user is None:
        abort(404)
    if data.get('name') is None:
        abort(400, 'Missing name')
    data['city_id'] = city_id
    place = Place(**data)
    storage.new(place)
    storage.save()
    return jsonify(place.to_dict()), 201

@app_views.route('/places/<place_id>', methods=['PUT'], strict_slashes=False)
def update_place(place_id):
    """This function update a state by id"""
    data = request.get_json()
    places = storage.all("Place")
    p_id = "Place." + place_id
    if places.get(p_id) is None:
        abort(404)
    if not data:
        abort(400, 'Not a JSON')
    else:
        place = places.get(p_id)
        for key, value in data.items():
            if key != "id" and key != "created_at" \
                    and key != "updated_at" and key != 'city_id'\
                    and key != "user_id":
                setattr(place, key, value)
        place.save()
        place = place.to_dict()
    return jsonify(place), 200


def decimalToBinary(n):
    temp = bin(n).replace("0b", "")
    if(len(temp) < 4):
        for i in range(0, 4 - len(temp)):
            temp = "0" + temp
    return temp

def toBcd(data):
    data = str(data)
    temp = ""
    for i in range(0, len(data)):
        temp += decimalToBinary(int(data[i]))
    return temp

def setXorGate():
    '''set xor gate'''
    count = 0
    tempIsXor = [0] * (len(divisor)-1)
    for i in range(len(tempIsXor), 0, -1):
        if(int(divisor[i]) == 1):
            tempIsXor[count] = 1
        count += 1
    #print(tempIsXor)
    return tempIsXor

def setCode():

    code = toBcd(data) + toCode()
    return code

def DFF(dataIn):
    '''set'''
    if(int(dataIn) == 1):
        return 1
    else:
        return 0
    '''reset'''

def xor(a, b):
    if(int(a) is not int(b)):
        return 1
    else:
        return 0

def toCode():
    temp = ""
    for i in range(0, (len(str(divisor))-1)):
        temp = "0" + temp
    return temp

def reminder(code):
    print(data, divisor)
    remind = ""
    #q = [1, 0, 1, 0]
    for i in range(0, len(code)):
        if(i == 0):
            jkState = [0] * (len(divisor)-1)
            #jkState = [1, 0, 1, 0]
        else:
            jkState = q

        tempState = jkState
        q = [0] * (len(divisor)-1)
        #q = [1, 0, 1, 0]

        for j in range(0, len(divisor)-1):
            if(int(isXor[j]) == 1):
                #print("xor", j)
                if(j == 0):
                    q[j] = DFF(xor(code[i], tempState[len(divisor)-2]))
                else:
                    q[j] = DFF(xor(tempState[j-1], tempState[len(divisor)-2]))
            else:
                if(j == 0):
                    q[j] = DFF(code[i])
                else:
                    q[j] = DFF(tempState[j-1])

        #print("xor", xor(code[i], tempState[3]))
        #q[0] = jkFF(xor(code[i], tempState[3]), jkState[0])
        #q[1] = jkFF(tempState[0], jkState[1])
        #q[2] = jkFF(tempState[1], jkState[2])
        #q[3] = jkFF(xor(tempState[2], tempState[3]), jkState[3])

        if(i == len(code)-1):
            remind = q
            #print(remind)

        print(code[i], q)

    temp = remind
    remind = ""
    for i in range(len(temp)-1, -1, -1):
        remind += str(temp[i])

    global codeWord
    codeWord = toBcd(data) + remind

    print("remind =", remind)

def setData(newData):
    global data
    global code
    data = str(newData)
    code = setCode()

def setDivisor(newDivisor):
    global divisor
    global isXor
    divisor = str(newDivisor)
    isXor = setXorGate()

def decode():
    reminder(codeWord)

'''
422
11001
'''
data = 0
divisor = 0
isXor = list()

setData(422)
setDivisor(11001)
code = setCode()
codeWord = ""
reminder(code)
decode()


from grapher.scatterplot import Scatterplot

class Stemplot(Scatterplot):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def _plot(self, plt=None):

        point_label, line_label = self._label()

        for k, (i, j) in enumerate(zip(self.x, self.y)):
            if k == 0:
                plt.scatter(i, j, label=point_label, **self.point_style)
                plt.plot([i, i], [0, j], label=point_label, **self.line_style)
            else:
                plt.scatter(i, j, **self.point_style)
                plt.plot([i, i], [0, j], **self.line_style)

        return self

if __name__ == '__main__':
    import numpy as np

    x = np.linspace(-np.pi, np.pi, 40)
    y = np.sin(x)

    s = Stemplot(x, y)
    s.set_line_style()
    s.set_labels('a', 'b', label=['points a', 'line b'])
    s.plot()


# -*- encoding: utf-8 -*-
############################################################################
#    Module for OpenERP, Open Source Management Solution
#
#    Copyright (c) 2014 OpenBias - http://www.bias.com.mx/
#    All Rights Reserved.
#---------------------------------------------------------------------------
#    Coded by: Ing. Miguel Miguel miguel.miguel@bias.com.mx
#    Manager: Eduardo Bayardo eduardo.bayardo@bias.com.mx
#---------------------------------------------------------------------------
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
import re
import StringIO
import cStringIO
import csv
import codecs
from openerp import pooler, tools
from openerp.report import report_sxw
from openerp.report.interface import report_rml
from openerp.tools import to_xml
from openerp.tools.translate import _
from bias_tools import unicode_writer_csv
from bias_tools import xlsxwriter_document

class fiscal_statements_by_periods_reports(report_rml):

    def get_header_rows_data_1(self, cr, uid, ids, datas, context):
        fiscal_obj = pooler.get_pool(cr.dbname).get('fiscal.statements')
        header = []
        header_line = []
        rows = []
        indx = 0
        indx_1 = 1
        self.indx_merge = 1
        lines_datas = fiscal_obj.get_lines(cr, uid, [datas['report_id']], datas, context=context)
        self.indx_title = []
        self.indx_format = []
        for line in lines_datas:
            header_line.append(' CONCEPTOS ')
            self.indx_format.append('title')
            for period_line in line['period_name']:
                self.indx_title.append({'inicio': (indx_1+1), 'final': (indx_1+4), 'name': period_line })
                indx_1 += 4
                header_line.append(period_line)
                header_line.append(' % ')
                header_line.append(period_line.replace("Periodo: ", "Acumulado: "))
                header_line.append(' % ')
                format = ['number_left', 'percent', 'number', 'percent_right']
                self.indx_format += format
                if line['col_branch_name']:
                    header_line.append(' ')
                    header_line += line['col_branch_name']
                    header_line.append(' ')
            if indx == 0:
                break
                indx = indx + 1
        header.append(tuple(header_line))
        for line in lines_datas:
            rows_line = []
            rows_line.append(line['name'])
            for indx, period_line in enumerate(line['period_name']):
                p_1 = float(line['p_1'].strip().replace(" %", "") or 0.0)
                p_2 = float(line['p_2'].strip().replace(" %", "") or 0.0)
                line_amount_1 = len(line['amount_1']) > 1 and line['amount_1'][indx] or ' '
                rows_line.append(line_amount_1 or 0.0 )
                rows_line.append((p_1 and p_1 > 0.0 and p_1 / 100) or '')

                line_amount_2 = len(line['amount_2']) > 1 and line['amount_2'][indx] or ' '
                rows_line.append(line_amount_2 or 0.0 )
                rows_line.append((p_2 and p_2 > 0.0 and p_2 / 100) or '')
                for indx_b, line_branch in enumerate(line['col_branch'][indx]):
                    rows_line.append(line_branch)
            rows.append(tuple(rows_line))
        return header, rows

    def get_header_rows_data_2(self, cr, uid, ids, datas, context):
        fiscal_obj = pooler.get_pool(cr.dbname).get('fiscal.statements')
        header = []
        header_line = []
        rows = []
        indx = 0
        indx_1 = 1
        lines_datas = fiscal_obj.get_lines(cr, uid, [datas['report_id']], datas, context=context)
        self.indx_merge = 0
        self.indx_title = []
        self.indx_format = []
        for line in lines_datas:
            for period_line in line['period_name']:
                header_line.append(period_line)
                header_line.append(' % ')
                self.indx_merge = self.indx_merge + 2
                format = ['number_left', 'percent_right']
                self.indx_format += format
            self.indx_format.append('title')
            header_line.append(' CONCEPTOS ')
            for period_line in line['period_name']:
                header_line.append(period_line)
                header_line.append(' % ')
                format = ['number_left', 'percent_right']
                self.indx_format += format
                if line['col_branch_name']:
                    header_line.append(' ')
                    header_line += line['col_branch_name']
                    header_line.append(' ')
            if indx == 0:
                break
                indx = indx + 1
        header.append(tuple(header_line))
        for line in lines_datas:
            rows_line = []
            p_1 = float(line['p_1'].strip().replace(" %", "") or 0.0)
            p_2 = float(line['p_2'].strip().replace(" %", "") or 0.0)
            for indx, period_line in enumerate(line['period_name']):
                line_amount_1 = len(line['amount_1']) > 1 and line['amount_1'][indx] or ' '
                rows_line.append(line_amount_1 or 0.0 )
                rows_line.append((p_1 and p_1 > 0.0 and p_1 / 100) or '')
            rows_line.append(line['name'])
            for indx, period_line in enumerate(line['period_name']):
                line_amount_2 = len(line['amount_2']) > 1 and line['amount_2'][indx] or ' '
                rows_line.append(line_amount_2 or 0.0 )
                rows_line.append((p_2 and p_2 > 0.0 and p_2 / 100) or '')
                for indx_b, line_branch in enumerate(line['col_branch'][indx]):
                    rows_line.append(line_branch)
            rows.append(tuple(rows_line))
        return header, rows
    def create(self, cr, uid, ids, datas, context):
        if context is None:
            context = {}
        self.pool   = pooler.get_pool(cr.dbname)
        report_type = datas.get('report_type', 'pdf')
        pdf         = None
        fiscal_obj = pooler.get_pool(cr.dbname).get('fiscal.statements')
        for fiscal in fiscal_obj.browse(cr, uid, [datas['report_id']], context={} ):
            company_name = fiscal_obj._get_company_name(cr, uid, context=None)
            title_name   = fiscal_obj._get_title(cr, uid, fiscal.id)
            date_name    = fiscal_obj._get_date_end(cr, uid, datas['period_id'][0] )
            user_name    = fiscal_obj._get_responsible_name(cr, uid, context=None)
            if fiscal.report_format == '' or fiscal.report_format == 'format_2':
                header, rows = self.get_header_rows_data_2(cr, uid, ids, datas, context=context)
            elif fiscal.report_format == 'format_1':
                header, rows = self.get_header_rows_data_1(cr, uid, ids, datas, context=context)
            if not self.indx_merge:
                self.indx_merge = 0
            if report_type == 'xls':
                context_data = {'in_memory':True}
                workbook     = xlsxwriter_document.workbook_xlsxwriter(headings=header[0], datas=rows, indx_format=self.indx_format, context=context_data, col=6)
                worksheet    = workbook.add_worksheet('balance')
                workbook.worksheet_set_column(worksheet, 'A','A',30)
                workbook.worksheet_set_column(worksheet, 'B','ZZ',20)
                workbook.workbook_properties(title='Reporte Financiero', author='Ing. Miguel Miguel', company='OpenBias', comments='Reportes Financieros')
                workbook.worksheet_write_rows(worksheet, {'cell':'A1','content': company_name,'format':'general'})
                workbook.worksheet_write_rows(worksheet, {'cell':'A2','content': (title_name + '  AL ') ,'format':'general'})
                workbook.worksheet_write_rows(worksheet, {'cell':'A3','content': 'IMPRESO POR: ','format':'general'})
                workbook.worksheet_write_rows(worksheet, {'cell':'B2','content': date_name,'format':'title_header'})
                workbook.worksheet_write_rows(worksheet, {'cell':'B3','content': user_name,'format':'title_header'})
                if fiscal.report_format == '' or fiscal.report_format == 'format_2':
                    workbook.worksheet_write_merge_range(worksheet, merge_row_a=5, merge_range_a=1, merge_row_b=5, merge_range_b=(self.indx_merge or 1) , merge_text=' MENSUAL ', merge_format='merge_format' )
                    workbook.worksheet_write_merge_range(worksheet, merge_row_a=5, merge_range_a=(self.indx_merge and (self.indx_merge + 2) or 1), merge_row_b=5, merge_range_b=(len(header[0]) or 1) , merge_text=' ACUMULADO ', merge_format='merge_format' )
                    workbook.worksheet_write_rows_cols(worksheet, {'row':4, 'col':(self.indx_merge or 1), 'content': ' HISTORICO ', 'format':'merge_format'})
                elif fiscal.report_format == 'format_1':
                    workbook.worksheet_write_indx_title(worksheet, merge_row_a=5, indx_title=self.indx_title)
                workbook.worksheet_write_headings(worksheet, 'A6', 'header', None)
                workbook.worksheet_write_cols(worksheet, 'currency_format')
                xls = workbook.close()
                return (xls, 'xlsx')
            elif report_type == 'csv':
                data_rows = []
                data_rows += [(company_name,)]
                data_rows += [(title_name + ' AL ',date_name)]
                data_rows += [('IMPRESO POR:',user_name)]
                data_rows += header
                data_rows += rows
                file_data = StringIO.StringIO()
                try:
                    writer = unicode_writer_csv.AccountUnicodeWriter(file_data)
                    writer.writerows(data_rows)
                    file_value = file_data.getvalue()
                finally:
                    file_data.close()
        return (file_value, report_type)

fiscal_statements_by_periods_reports('report.fiscal.statements.by.periods.reports', 'fiscal.statements','','')

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:


from .base import Resource
from .compute import *
from .storage import *

def get_all_resources(skil):
    '''Get all current SKIL resources as a list of
    skil.resources.base.Resource instances.
    '''
    resource_list = skil.api.get_resources()
    resources = []

    for resource_obj in resource_list:
        resource = Resource(skil)
        resource.resource_id = resource_obj.resource_id
        resources.append(resource)
    return resources

def get_resource_by_id(skil, resource_id):
    '''Get a skil.resources.base.Resource object
    by ID.
    '''
    api_response = skil.api.get_resource_by_id(resource_id)
    return Resource(skil, api_response.resource_id)

def get_resources_by_type(skil, resource_type):
    '''Get a list of skil.resources.base.Resource objects
    by type ('compute' or 'storage').
    '''
    resource_list = skil.api.get_resource_by_type(resource_type)
    resource_ids = [resource.resource_id for resource in resource_list]
    return [get_resource_by_id(skil, resource_id)
            for resource_id in resource_ids]

def get_resources_by_sub_type(skil, sub_type):
    '''Get a list of resources by string sub_type, namely
        - EMR                   # AWS Elastic Map Reduce(Compute)
        - S3                    # AWS Simple Storage Service
        - GoogleStorage         # Google Cloud Storage
        - DataProc              # Google Big Data Compute Engine
        - HDInsight             # Azure Compute
        - AzureStorage          # Azure Blob Storage
        - HDFS                  # in house Hadoop (Storage)

    For instance, choosing 'EMR' sub_type, you'll get all
    skil.resources.compute.EMR resource instances in a Python list.
    '''
    subtype_list = skil.api.get_resource_by_sub_type(sub_type)
    return [get_resource_details_by_id(
        skil, obj.resource_id) for obj in subtype_list]

def get_resource_details_by_id(skil, resource_id):
    '''Get a concrete resource implementation of
    skil.resources.base.Resource by resource ID. For instance, if
    your resource ID corresponds to a resource of subtype "HDFS",
    this will return a skil.resources.storage.HDFS object.
    '''
    resource = skil.api.get_resource_by_id(resource_id)
    res_type = resource.sub_type

    details = skil.api.get_resource_details_by_id(resource_id)
    if res_type == 'EMR':
        return EMR(skil, resource.name, details['region'], None,
                   details['clusterId'], resource_id, False)
    elif res_type == 'S3':
        return S3(skil, resource.name,
                  details['bucket'], details['region'], None, resource_id, False)
    elif res_type == 'GoogleStorage':
        return GoogleStorage(
            skil, resource.name, details['projectId'], details['bucketName'], None, resource_id, False)
    elif res_type == 'DataProc':
        return DataProc(skil, resource.name, details['projectId'], details['region'],
                        details['sparkClusterName'], None, resource_id, False)
    elif res_type == 'HDInsight':
        return HDInsight(skil, resource.name, details['subscriptionId'], details['resourceGroupName'],
                         details['clusterName'], None, resource_id, False)
    elif res_type == 'AzureStorage':
        return AzureStorage(
            skil, resource.name, details['containerName'], None, resource_id, False)
    elif res_type == 'HDFS':
        return HDFS(skil, resource.name, details['nameNodeHost'],
                    details['nameNodePort'], None, resource_id, False)
    elif res_type == 'YARN':
        return YARN(skil, resource.name,
                    details['localSparkHome'], None, resource_id, False)


#! -*- coding:utf-8 -*-
import tornado.ioloop
import os
from handlers import *
import config
from tornado.options import options, parse_command_line, define

define("port", default=8080, help="run on the given port", type=int)

settings = {
    "static_path": os.path.join(os.path.dirname(__file__), "static"),
    "cookie_secret": config.COOKIE_SECRET,
    "template_path":os.path.join(os.path.dirname(__file__), "template"),
    "login_url": "/login",
    "xsrf_cookies": config.XSRF_COOKIES,
    "debug":config.DEBUG
}

application = tornado.web.Application([
    (r"/", IndexHandler),
    (r"/login", LoginHandler),
    (r"/logout", LogoutHandler),
], **settings)

if __name__ == "__main__":
    parse_command_line()
    application.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

import pandas as pd
import os

from src.etl_script import User, Transaction

DATABASE_STRING = 'postgresql://luke:password@localhost:5432/revolut_data'

def features_for_user(session, user_id):
    """
    This function assumes the database is populated as described in task 1
    :param db_string:
    :return:
    """
    # this is an O(n^2) solution which is particularly slow
    user = session.query(User).filter(User.id == user_id).one()
    user_transaction_history = session.query(Transaction).filter(Transaction.user_id == user_id).all()

    if len(user_transaction_history) > 0:
        number_of_card_payments = len([x for x in user_transaction_history if x.type == "CARD_PAYMENT"])
        number_of_atm_uses = len([x for x in user_transaction_history if x.type == "ATM"])
        number_of_bank_transfers = len([x for x in user_transaction_history if x.type == "BANK_TRANSFER"])
    else:
        number_of_card_payments = 0
        number_of_atm_uses = 0
        number_of_bank_transfers = 0

    # fraudsters = session.query(User).filter(User.is_fraudster).all()

    return pd.DataFrame({"birth_year": [user.birth_year],
                         "has_email": [user.has_email],
                         "kyc_passed": [user.kyc == "PASSED"],
                         "failed_sign_in_attempts": [user.failed_sign_in_attempts],
                         "user_signed_terms": [user.terms_version != None],
                         "user_is_from_uk": [user.country == "GB"],
                         "number_of_transactions": [len(user_transaction_history)],
                         "number_of_card_payments": [number_of_card_payments],
                         "number_of_atm_uses": [number_of_atm_uses],
                         "number_of_bank_transfers": [number_of_bank_transfers]},
                        index=[user_id])

def get_all_features(db_string):
    """

    :param db_string:
    :return:
    """
    db = create_engine(db_string)
    Session = sessionmaker(db)
    session = Session()
    users = session.query(User)

    feature_vec = pd.DataFrame()
    for i, user in enumerate(users):
        feature_vec = feature_vec.append(features_for_user(session, user.id))
        if i % 100 == 0:
            print(i)

    feature_vec = feature_vec.sort_index()
    feature_vec.to_csv(os.path.join("results", "training_features.csv"))

if __name__ == "__main__":
    get_all_features(DATABASE_STRING)


import os
import sys
d= {}

def convert(s):
	return int(s[-2:]) + 60*int(s[-5:-3]) + 3600*int(s[0:-6])

for subdir, dirs, files in os.walk(os.getcwd()):
    for file in files:
        filepath = subdir + os.sep + file
        if file.startswith( 'stoptiming' ):
            # print filepath
            fp = open(filepath,'r')
            for line in fp:
                line = ' '.join(line.split())
                line = line.strip()
                line = line.replace(' ',',')
                line = line.split(',')
                # print line
                if (line[-3],line[-2]) not in d:
                    d[(line[-3],line[-2])] = 0
                d[(line[-3],line[-2])] = int(convert(line[-1])) + d[(line[-3],line[-2])]

f = open("stopdata.txt","w+")
for key,value in sorted(d.iteritems()):
	f.write( "%s %s %s \n" %(key[0], key[1], value) )

f.close()


# -*- coding: utf-8 -*-
# @Author: YangZhou
# @Date:   2017-06-14 22:16:16
# @Last Modified by:   YangZhou
# @Last Modified time: 2018-01-11 15:18:39
from aces.tools import *
from aces.graph import fig, setLegend, pl, fit
import numpy as np

from ase import io

kas = []
with fig('chi.eps', figsize=(7, 8)):
    markers = ['s', '8', '^', 'D', '>']
    colors = "firebrick,r,b,k,g,hotpink,orange,r".split(
        ',')
    text_style = dict(horizontalalignment='left', verticalalignment='center',
                      fontsize=14, fontdict={'family': 'serif'})
    fi, axes = pl.subplots(1, 1, sharex=True, figsize=(7, 4))
    ax1 = axes
    ks = []
    vs = '2l1,2l2,2lhex,3l1,4l1,5l1,6l1,8l1'.split(',')
    for i, v in enumerate(vs):

        dir = "%s/0/SHENG" % v
        if v == "2l1":
            dir = "2l1/0/sheng.1"
        kappa = np.loadtxt(
            dir + '/T300K/BTE.cumulative_kappa_tensor')  # [::3]
        # m = markers[i]
        c = colors[i]
        l = kappa[:, 0]
        atoms = io.read(v + "/0/secondorder/POSCAR")
        nlayer = int(v.split('l')[0])
        ss = np.linalg.norm(atoms.cell[2]) / (
            3.34 * nlayer)

        z = kappa[:, 1] * ss
        a = kappa[:, 5] * ss
        opts = dict(
            # markeredgecolor='w',
            # marker=m,
            color=c,
            # markersize=1 + 2,
            lw=2,
            ls='-')
        o1 = "-"
        ka = np.abs(z - a) / np.max(np.c_[z, a], axis=1) * 100
        if v == '2lhex':
            v = '2l3'
        label = v
        ax1.semilogx(l, ka, label=label.replace('l', 'L'), **opts)
        ax1.set_xlabel("Cutoff Mean Free Path for Phonons (${ \\AA }$)")
        # ax1.set_ylim([0, 15])
        ax1.set_xlim([10, 1e5])
        ax1.set_ylim([0, 100])
        ax1.set_ylabel("Thermal Conductivity Anisotropy (%)")
        # ax1.text(-.15, 1, 'b)', transform=ax1.transAxes, **text_style)
    setLegend(ax1, ncol=4, loc=0, fontsize=12)
    fi.subplots_adjust(
        left=None,
        bottom=None,
        right=None,
        top=None,
        wspace=0,
        hspace=0)


import sys
from diylisp.parser import parse
from diylisp.evaluator import evaluate
from diylisp.types import Environment

program = """(
(define fact
;; Factorial function
(lambda (n)
    (if (eq n 0)
        1 ; Factorial of 0 is 1
        (* n (fact (- n 1))))))

(print (fact 3))
(print (fact 6))
)
"""

evaluate(parse(program), Environment())


# -*- encoding:utf-8 -*-
# Copyright (c) Alibaba, Inc. and its affiliates.
import logging

import tensorflow as tf

from easy_rec.python.utils.load_class import load_by_path

if tf.__version__ >= '2.0':
  tf = tf.compat.v1

class DNN:

  def __init__(self, dnn_config, l2_reg, name='dnn', is_training=False):
    """Initializes a `DNN` Layer.

    Args:
      dnn_config: instance of easy_rec.python.protos.dnn_pb2.DNN
      l2_reg: l2 regularizer
      name: scope of the DNN, so that the parameters could be separated from other dnns
      is_training: train phase or not, impact batchnorm and dropout
    """
    self._config = dnn_config
    self._l2_reg = l2_reg
    self._name = name
    self._is_training = is_training
    logging.info('dnn activation function = %s' % self._config.activation)
    self.activation = load_by_path(self._config.activation)

  @property
  def hidden_units(self):
    return self._config.hidden_units

  @property
  def dropout_ratio(self):
    return self._config.dropout_ratio

  def __call__(self, deep_fea, hidden_layer_feature_output=False):
    hidden_units_len = len(self.hidden_units)
    hidden_feature_dict = {}
    for i, unit in enumerate(self.hidden_units):
      deep_fea = tf.layers.dense(
          inputs=deep_fea,
          units=unit,
          kernel_regularizer=self._l2_reg,
          activation=None,
          name='%s/dnn_%d' % (self._name, i))
      if self._config.use_bn:
        deep_fea = tf.layers.batch_normalization(
            deep_fea,
            training=self._is_training,
            trainable=True,
            name='%s/dnn_%d/bn' % (self._name, i))
      deep_fea = self.activation(
          deep_fea, name='%s/dnn_%d/act' % (self._name, i))
      if len(self.dropout_ratio) > 0 and self._is_training:
        assert self.dropout_ratio[
            i] < 1, 'invalid dropout_ratio: %.3f' % self.dropout_ratio[i]
        deep_fea = tf.nn.dropout(
            deep_fea,
            keep_prob=1 - self.dropout_ratio[i],
            name='%s/%d/dropout' % (self._name, i))

      if hidden_layer_feature_output:
        hidden_feature_dict['hidden_layer' + str(i)] = deep_fea
        if (i + 1 == hidden_units_len):
          hidden_feature_dict['hidden_layer_end'] = deep_fea
          return hidden_feature_dict
    else:
      return deep_fea