'''
tree.py
'''

import collections
import itertools

def get(tree, key):
    subtree = tree
    for name in key:
        subtree = subtree[name]
    return subtree

def set(tree, key, value):
    subtree = tree
    *key, base = key
    for name in key:
        if not name in subtree:
            subtree[name] = {}
        subtree = subtree[name]
    subtree[base] = value

def schema(tree, _schema=None):
    if _schema is None:
        _schema = {}
    if isinstance(tree, dict):
        for k, v in tree.items():
            if isinstance(v, list):
                _schema[k] = [{}]
                schema(v, _schema[k][0])
            else:
                _schema[k] = {}
                schema(v, _schema[k])

    elif isinstance(tree, list):
        for v in tree:
            schema(v, _schema)
    else:
        return
    return _schema

def keys(tree, _parent=None):
    if isinstance(tree, dict):
        for k, v in tree.items():
            if _parent:
                p = _parent + (k,)
            else:
                p = (k,)
            yield p
            yield from keys(v, p)
    elif isinstance(tree, list):
        for n, v in enumerate(tree):
            if _parent:
                p = _parent + (n,)
            else:
                p = (n,)
            yield p
            yield from keys(v, p)

def values(tree):
    for key in keys(tree):
        yield get(tree, key)

def items(tree):
    for key in keys(tree):
        yield key, get(tree, key)

def paths(schema, _parent=None):
    if isinstance(schema, dict):
        for k, v in schema.items():
            if _parent:
                p = _parent + (k,)
            else:
                p = (k,)
            yield p
            yield from paths(v, p)
    elif isinstance(schema, list):
        for v in schema:
            if _parent:
                p = _parent + (Ellipsis,)
            else:
                p = (Ellipsis,)
            yield p
            yield from paths(v, p)

def search(tree, name):
    for path in paths(tree):
        if path[-1] == name:
            return path

def find(tree, name):
    for path in paths(tree):
        if path[-1] == name:
            yield path

def expandpath(schema, path):
    path = list(path)

    n = 0
    while n < len(path):
        if isinstance(schema, list):
            if path[n] != Ellipsis:
                path.insert(n, Ellipsis)
            schema = schema[0]
        else:
            schema = schema[path[n]]
        n += 1

    return tuple(path)

def matchkey(expanded, key):
    if not len(expanded) == len(key):
        return False

    for p, k in zip(expanded, key):
        if isinstance(k, int) and p == Ellipsis:
            continue
        if k != p:
            return False

    return True

def pathkeys(tree, expanded):

    for key in keys(tree):
        if matchkey(expanded, key):
            yield key

def walk(tree, expanded):

    for key in pathkeys(tree, expanded):
        yield get(tree, key)

def flatten(tree, key):
    result = {}

    for n, k in enumerate(key):
        if isinstance(k, int):
            continue
        result[k] = get(tree, key[:n+1])

    return result

def branches(tree, expanded):

    for key in pathkeys(tree, expanded):
        yield flatten(tree, key)

class TreeDict:

    def __init__(self, d):
        self._tree = d
        self._schema = schema(self._tree)
        self._paths = list(paths(self._schema))
        self._keys = list(keys(self._tree))

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

    def __repr__(self):
        return 'TreeDict({!r})'.format(self._tree)

    def ispath(self, path):
        return path in self._paths

    def iskey(self, key):
        return key in self._keys

    def canonicalise(self, path):
        return expandpath(self._schema, path)

    def get(self, key):
        return get(self._tree, key)

    def keys(self):
        for key in self._keys:
            yield key

    def values(self):
        for key in self._keys:
            yield get(self._tree, key)

    def items(self):
        for key in self._keys:
            yield key, get(self._tree, key)

    def paths(self):
        for path in self._paths:
            yield path

    def schema(self):
        return self._schema

class Tree:

    def __init__(self, json_dict):
        self.__tree__ = TreeDict(json_dict)
        self.__path__ = tuple()

    def __str__(self):
        return 'Tree({})'.format(self.__tree__)

    def __repr__(self):
        if self.__path__:
            return 'Tree({}).{}'.format(
                self.__tree__, '.'.join(self.__path__))
        else:
            return 'Tree({})'.format(self.__tree__)

    def __iter__(self):
        yield from self.__tree__.get(self.__path__)

    @classmethod
    def __getter__(cls, tree, path):
        new = cls.__new__(cls)
        new.__tree__ = tree
        new.__path__ = path
        return new

    def __getattr__(self, name):
        path = self.__path__ + (name,)
        if self.__tree__.ispath(path):
            return self.__getter__(self.__tree__, path)
        else:
            raise AttributeError(
                'Tree: {} has no element {}'.format('.'.join(path[:-1]), name))


import sys
input = sys.stdin.readline

n, b = map(int, input().rstrip().split())
matrix = [list(map(int, input().rstrip().split())) for _ in range(n)]

def matrix_pow(mat, orig):
    new_mat = [[0 for _ in range(n)] for _ in range(n)]
    for i in range(n):
        for j in range(n):
            for k in range(n):
                new_mat[i][j] += mat[i][k] * orig[k][j]
            new_mat[i][j] %= 1000
    return new_mat

def solve(mat, now):
    if now == 1:
        return mat

    now_matrix = solve(mat, now // 2)
    if now % 2 == 1:
        return matrix_pow(matrix_pow(now_matrix, now_matrix), matrix)
    else:
        return matrix_pow(now_matrix, now_matrix)

matrix = solve(matrix, b)
for i in range(n):
    for j in range(n):
        matrix[i][j] %= 1000
for i in range(n): print(' '.join(map(str, matrix[i])))


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

import csv
import datetime
import yaml
import os

NORDEA_DATA_FOLDER = 'nordea_data'

def read_values(filename):
    with open(filename, 'r') as input_file:
        file_data = input_file.readlines()
        rows = [row.strip().split('\t') for row in file_data if row != '\r\n']
        return rows

def read_settings():
    with open('settings.yaml', 'r') as input_file:
        settings = yaml.load(input_file)
        return settings

class Account():
    def __init__(self, account_number, name, transactions=[], balance=0, balance_date=datetime.datetime.now().date()):
        self.account_number = account_number
        self.name = name
        self.transactions = []
        self.add_transactions(transactions)
        self.init_balance = balance
        self.init_balance_date = balance_date
        self.saaja_maksaja_includes = None
        self.viesti_includes = None
        # self.calculate_balances(balance, balance_date)

    def __str__(self):
        return '{} {} {}'.format(self.account_number, self.name, len(self.transactions))

    def add_transactions(self, transaction_list):
        transaction_list = [trans for trans in transaction_list if trans not in self.transactions]
        print('adding ', len(transaction_list))
        self.transactions = self.transactions + transaction_list
        print(len(self.transactions), 'transactions')
        dates = [trans.kirjauspaiva for trans in self.transactions]

    def calculate_balances(self):
        balance = self.init_balance
        balance_date = self.init_balance_date
        self.transactions = sorted(self.transactions, reverse=True, key=lambda transaction: transaction.kirjauspaiva)
        self.balances = []
        self.day_changes = []
        current_date = None
        current_date_change = 0
        for transaction in self.transactions:
            if current_date != transaction.kirjauspaiva:
                if current_date is not None:
                    self.day_changes.append({"date": current_date, "change": current_date_change})
                current_date = transaction.kirjauspaiva
                current_date_change = 0
            current_date_change = current_date_change + transaction.value
        self.day_changes.append({"date": current_date, "change": current_date_change})

        # self.balances.append({"amount": balance, "date": balance_date})
        date = balance_date
        for change in self.day_changes:
            change_date = change['date']
            while change_date != date:
                self.balances.append({"amount": balance, "date": date})
                try:
                    date = date - datetime.timedelta(days=1)
                except:
                    print(self.balances[-1])
                    raise
            balance = balance - change['change']
        self.balances.append({"amount": balance, "date": date})
        if self.name == 'Nordnet':
            print(self.balances)

    def add_transaction_if_included(self, transaction):
        if self.includes_transaction(transaction):
            opposite = transaction.opposite()
            if opposite not in self.transactions:
                self.transactions.append(opposite)

    def includes_transaction(self, transaction):
        if self.saaja_maksaja_includes is not None:
            if self.saaja_maksaja_includes.lower() in transaction.saaja_maksaja.lower():
                return True
        if self.viesti_includes is not None:
            if self.viesti_includes.lower() in transaction.viesti.lower():
                return True
        return False

class Transaction():
    def __init__(
        self, tili, row
        # kirjauspaiva, arvopaiva, maksupaiva, value, saaja_maksaja, tilinumero, bic,
        # tapahtuma, viite, maksan_viite, viesti, kortinnumero, kuitti
    ):
        while len(row) <= 12:
            row.append('')
        self.row = row
        self.tili = tili
        self.kirjauspaiva = self.parse_date(row[0])
        self.arvopaiva = self.parse_date(row[1])
        self.maksupaiva = self.parse_date(row[2])
        self.value = float(row[3].replace(',', '.'))
        self.saaja_maksaja = row[4].decode('utf-8')
        self.tilinumero = row[5]
        self.bic = row[6]
        self.tapahtuma = row[7]
        self.viite = row[8]
        self.maksan_viite = row[9]
        self.viesti = row[10]
        self.kortinnumero = row[11]
        self.kuitti = row[12]

    def __str__(self):
        return '{} {} {} {}'.format(self.tili, self.kirjauspaiva, self.value, self.saaja_maksaja)

    def parse_date(self, date_string):
        if not date_string:
            return ''
        date = datetime.datetime.strptime(date_string, '%d.%m.%Y').date()
        return date

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

    def opposite(self):
        opposite = Transaction(self.tili, self.row)
        opposite.value = -self.value
        return opposite

class Assets():
    def __init__(self):
        self.accounts = {}
        self.settings = read_settings()
        self.virtual_accounts = []

    def transactions_from_folder(self):
        filenames = os.listdir(NORDEA_DATA_FOLDER)
        for filename in filenames:
            self.transactions_from_file(NORDEA_DATA_FOLDER + '/' + filename)

    def calculate_balances(self):
        for account in self.get_accounts():
            account.calculate_balances()

    def get_accounts(self):
        return [self.accounts[key] for key in self.accounts.keys()] + self.virtual_accounts

    def transactions_from_file(self, filename):
        print(filename)
        data = read_values(filename)
        tilinumero = data[0][1]

        transactions = [Transaction(tilinumero, row) for row in data[2:]]
        self.add_transactions(transactions, tilinumero)

    def add_transactions(self, transactions, tilinumero):
        if tilinumero in self.accounts:
            self.accounts[tilinumero].add_transactions(transactions)
        else:
            account_name = tilinumero
            account = Account(tilinumero, account_name, transactions, balance=self.settings.get('balances', {}).get(tilinumero, 0))
            self.add_account(account)
        for account in self.virtual_accounts:
            for transaction in transactions:
                account.add_transaction_if_included(transaction)

    def add_account(self, account):
        self.accounts[account.account_number] = account

    def external_accounts_from_settings(self):
        external_accounts = self.settings.get('externalaccounts', {})
        for name in external_accounts.keys():
            ext_account = Account(name, name, balance=self.settings.get('balances', {}).get(name))
            account_settings = external_accounts[name]
            ext_account.saaja_maksaja_includes = account_settings.get('saaja_maksaja')
            ext_account.viesti_includes = account_settings.get('viesti')
            self.virtual_accounts.append(ext_account)

    def classifications_from_settings(self):
        self.classifications = self.settings.get('classification')

    def get_all_transactions(self):
        transactions = []
        for account in self.get_accounts():
            transactions = transactions + account.transactions
        return transactions

    def classify_transactions(self, start, end):
        classified = {}
        for transaction in self.get_all_transactions():
            if start <= transaction.kirjauspaiva < end:
                self.add_to_classified(classified, transaction)
        return classified

    def add_to_classified(self, classified, transaction):
        saaja_maksaja = transaction.saaja_maksaja
        type = self.classify_saaja_maksaja(saaja_maksaja)
        self.add_to_dict(classified, type, transaction.value)

    def add_to_dict(self, sum_dict, key, value):
        if key in sum_dict:
            sum_dict[key] += value
        else:
            sum_dict[key] = value

    def classify_saaja_maksaja(self, saaja_maksaja):
        for key in self.classifications:
            try:
                for search_value in self.classifications[key]:
                    if search_value.lower() in saaja_maksaja.lower():
                        saaja_maksaja = key
            except:
                print(saaja_maksaja)
                print(type(saaja_maksaja))
                raise
        return saaja_maksaja

# tilinumero = data[0][1]
# transactions = [Transaction(tilinumero, row) for row in data[2:]]
# transactions2 = [Transaction(tilinumero, row) for row in data[2:]]

# account = Account(tilinumero, 'Käyttötili', transactions, balance=settings.get('balance', 0))

assets = Assets()

assets.external_accounts_from_settings()
assets.transactions_from_folder()
assets.classifications_from_settings()
assets.calculate_balances()
today = datetime.datetime.utcnow().date()

min_date = datetime.datetime(2016, 1, 1).date()
date = datetime.date.today()
months = [date]
spendings = []
total = {}
while date >= min_date:
    if date.day == 1:
        months.append(date)
    date -= datetime.timedelta(days=1)
for i in range(len(months) - 1):
    classified = assets.classify_transactions(months[i + 1], months[i])
    spendings.append(classified)
    for key in classified:
        assets.add_to_dict(total, key, classified[key])
    print(months[i + 1].strftime("%B %Y"))
    print(sorted([[key, classified[key]] for key in classified], key=lambda a: a[1]))
sorted_pairs = sorted([[key, total[key]] for key in total], key=lambda a: a[1])
ordered_targets = [p[0] for p in sorted_pairs]
spending_matrix = []
spending_matrix.append([None] + [t.encode('utf-8') for t in ordered_targets])

for i, spending in enumerate(spendings):
    spending_matrix.append([months[i + 1]] + [spending.get(key, 0) for key in ordered_targets])

print(total)
print(sorted_pairs)

# print(external_names)
# for name in external_names:
#     print(external_accounts[name])
csv_headers = ['date', 'balance']

account_names = [acc.name for acc in assets.get_accounts()]

csv_matrix = []
i = 0
values = {}
while True:
    row = []
    no_data_count = 0
    date = None
    for account in assets.get_accounts():
        try:
            balance = account.balances[i]
            values[account.name] = balance['amount']
            date = balance['date']
        except IndexError:
            no_data_count += 1

        row.append(values.get(account.name, 0))
        value = balance
    i += 1
    value_list = [values[key] for key in values.keys()]
    total = sum(value_list)
    csv_matrix.append([date] + row + [total])
    if no_data_count == len(account_names):
        break
    if i > 10000:
        print('too much')
        break

csv_matrix = list(reversed(csv_matrix))
csv_matrix = [['date'] + account_names + ['total']] + csv_matrix

filtered_csv = []
for index, row in enumerate(csv_matrix):
    if index % 5 == 0:
        filtered_csv.append(row)

with open('out_data.csv', 'w') as out_csv:
    writer = csv.writer(out_csv)
    writer.writerows(csv_matrix)

with open('spending.csv', 'w') as out_csv:
    writer = csv.writer(out_csv)
    writer.writerows(spending_matrix)


__author__ = "an.luzgarev"

import random

from django.db import connection
from .models import Word
from .models import WordTraining

"""
Module contains functions for creating new trainings
"""

def get_array_of_words(letter, quantity, training_words=False, user=None):
	total_array = []
	if training_words:
		with connection.cursor() as cursor:
			query = """SELECT
			work_app_word.word AS word,
			work_app_word.translation AS translation,
			work_app_word.transcription AS transcription,
			work_app_word.id AS id
			FROM
			work_app_word
			INNER JOIN
			wordtraining
			ON work_app_word.id = wordtraining.word_id
			WHERE wordtraining.user_id = %s
			"""
			if letter == "all":
				cursor.execute(query, [user.id])
			else:
				query = query + " AND work_app_word.first_letter = %s"
				cursor.execute(query,[user.id, letter])
			selector = cursor.fetchall()
			for x in selector:
				word = {'word': x[0],
			            'translation': x[1],
						'transcription': x[2],
						'id': x[3]}
				total_array.append(word)
	else:
		if letter == "all":
			selection = Word.objects.all().values()
		else:
			selection = Word.objects.filter(first_letter=letter).values()
		total_array = [x for x in selection]
	random.shuffle(total_array)
	total_array = total_array[0:min(quantity, len(total_array))]
	return total_array

def words_of_category(category):
	words_list = []
	words = Word.objects.filter(category=category).distinct()
	for x in words:
		words_list.append(x.word)
	return words_list

def get_list_of_categories():
	categories_list = []
	objects = Word.objects.values("category").distinct()
	for x in objects:
		if x['category'] != 'nan' and x['category'] != 'Phrasal verb':
			categories_list.append(x['category'])
	return categories_list

def get_phrasal_verbs(word):
	query = """SELECT
	work_app_word.*
	FROM work_app_word
	WHERE work_app_word.category =  'Phrasal verb'"""
	selector = Word.objects.raw(query)
	words_array = []
	for x in selector:
		arr = x.word.split(" ")
		if arr[0] == word:
			words_array.append({"word": x.word, "translation": x.translation})
	return words_array
	return words_array

def get_phrasal_verbs_list():
	query = "SELECT work_app_word.* FROM work_app_word WHERE work_app_word.category =  'Phrasal verb'"
	selector = Word.objects.raw(query)
	selector = Word.objects.filter(category='Phrasal verb').distinct()
	words_array = [x.word for x in selector]
	words_array = process_phrasal_verbs_array(words_array)
	words_array = prosses_phrasal_verbs_doubles(words_array)
	return words_array

def process_phrasal_verbs_array(array):
	array_words = []
	for x in array:
		arr = x.split(" ")
		curr_word = arr[0]
		array_words.append(curr_word)
	return array_words

def prosses_phrasal_verbs_doubles(array):
	array_2 = []
	for x in array:
		try:
			array_2.index(x)
		except:
			array_2.append(x)
	return array_2

def get_amount_of_words(letter):
	words = Word.objects.filter(first_letter=letter)
	amount_of_words = len(words)
	return amount_of_words

def process_amount_of_words(request,letter):
	if request.POST.get("words_amount", 0) != 0:
		return get_amount_of_words(letter)
	else:
		return 0

def clear_all_trainings(user=None):
	if user == None:
		WordTraining.objects.all().delete()
	else:
		WordTraining.objects.filter(user_id=user.id).delete()


#!/usr/bin/env python3
"""Create resource tarball, including trained DNN models and feedback media files and upload it to storage server."""
import os
import subprocess
import tarfile

import boto
import boto.s3.connection
import configparser
from logzero import logger

def _make_tarfile(output_filename, file_and_dir_list):
    with tarfile.open(output_filename, 'w:gz', dereference=True) as tar:
        for item in file_and_dir_list:
            tar.add(item)

def create_asset_tarball():
    asset_file_list = [
        'model/model.caffemodel',
        'model/faster_rcnn_test.pt',
        'model/labels.txt',
        'feedbacks/images',
        'feedbacks/videos'
    ]
    output_file = 'asset.tgz'
    if os.path.exists(output_file):
        os.remove(output_file)
    _make_tarfile(output_file, asset_file_list)
    return output_file

def upload_to_storage_server(host, access_key, secret_key, bucket_name, key_name, file_path):
    conn = boto.connect_s3(
        aws_access_key_id=access_key,
        aws_secret_access_key=secret_key,
        host=host,
        is_secure=True,
        calling_format=boto.s3.connection.OrdinaryCallingFormat(),
    )

    bucket = conn.get_bucket(bucket_name)

    if not bucket:
        logger.debug('Bucket {} does not exist. Creating...'.format(bucket_name))
        bucket = conn.create_bucket(bucket_name)
        bucket.add_user_grant('FULL_CONTROL', 'backup')
        logger.debug('Creation finished.')

    logger.debug('uploading {} to {} under key {}'.format(file_path, host, key_name))
    key = bucket.new_key(key_name)
    key.set_contents_from_filename(file_path)
    key.set_canned_acl('public-read')

def md5(file_path):
    p = subprocess.run(["md5sum", file_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    return p.stdout

if __name__ == '__main__':
    config = configparser.ConfigParser()
    config.read(os.path.expanduser('~/.cmusatyalab'))
    host = config['DEFAULT']['s3_host']
    access_key = config['DEFAULT']['s3_access_key']
    secret_key = config['DEFAULT']['s3_secret_key']
    file_path = create_asset_tarball()
    logger.debug('Creating asset file to {}'.format(file_path))
    upload_to_storage_server(host, access_key, secret_key, 'gabriel-model', 'disktray', file_path)
    logger.debug('upload finished! Remember to update the md5 hash in the documentation.')
    md5sum = md5(file_path)
    logger.info('The md5sum for {} is: {}'.format(file_path, md5sum))


from django.http import HttpResponse
from django.views import View
from django.shortcuts import render

def function_view(request):
    return HttpResponse('response from function view')

class ExampleClassBased(View):
    def get(self, request):
        return HttpResponse('response fom class based view')

class ExampleView(View):
    def get(self, request):
        return render(request, 'main.html', {
            'my_var': 'i came from views.py',
            'list': [
                'one',
                'two',
                'five',
                'many...'
            ]
        })

class OrdersView(View):
    def get(self, request):
        data = {
            'orders': [
                {'title': 'Первый заказ', 'id': 1, 'element': 'element 1'},
                {'title': 'Второй заказ', 'id': 2, 'element': 'element 2'},
                {'title': 'Третий заказ', 'id': 3, 'element': 'element 3'}
            ]
        }
        return render(request, 'orders.html', data)

class OrderView(View):
    def get(self, request, id):
        data = {
            'order': {
                'id': id
            }
        }
        return render(request, 'order.html', data)


from gcscanner import parser, check, scope

_buckets_publicly_readable = []
_buckets_authenticated_readable = []

def scan_projects(options):
    projects = scope.get_projects_scope(options.project)

    # Iterate on projects
    for project in projects:

        parser.display_project(project, options)

    resume(options.resume)

def resume(resume):
    if resume:
        check.list_buckets_authenticated_readable(_buckets_authenticated_readable)
        check.list_buckets_publicly_readable(_buckets_publicly_readable)

if __name__ == "__main__":
    # Execute when the module is not initialized from an import statement.
    parser.args()


import logging
logger = logging.getLogger(__name__)
ERROR = logger.error
WARN = logger.warn
INFO = logger.info
DEBUG = logger.debug

from six import text_type, string_types

from six import PY2, PY3
if PY3:
    from collections.abc import Mapping
elif PY2:
    from collections import Mapping

import weakref

from partycrasher.api.search import Search
from partycrasher.api.report_type import ReportType

cached_types = weakref.WeakKeyDictionary()

def get_cached_types(search):
    global cached_types
    if search in cached_types:
        DEBUG("HIT")
        return cached_types[search]
    else:
        DEBUG("MISS " + repr(search._d))
        r = search(size=0)
        types = {
            t: ReportType(search=search, report_type=t)
            for t in r['counts']['type'].keys()
            }
        cached_types[search] = types
        return types

class Types(Mapping):
    """
    Represents the types available under a certain search.
    """
    def __init__(self, search):
        self.search = search
        # Lazy-load types
        self._d = get_cached_types(search)

    def __getitem__(self, key):
        return self._d.__getitem__(key)

    def __iter__(self):
        return self._d.__iter__()

    def __len__(self):
        return self._d.__len__()

    def restify(self):
        return self._d


def main():
    # open file for reading
    inFile = open('GirlNames.txt', 'r')
    inFile2 = open('BoyNames.txt', 'r')
    girl_list = readContents(inFile)
    boy_list = readContents(inFile2)

    name_search = input("Enter a name to search or type QUIT to exit:\n").lower()
    new_name = name_search.capitalize()

    while name_search.upper() != "QUIT":
        if searchList(girl_list, new_name) and not searchList(boy_list, new_name):
            print("The name '", new_name, "' was found in popular girl names list (line ", girl_list.index(new_name) + 1, ").", sep='')

        elif searchList(boy_list, new_name) and not searchList(girl_list, new_name):
            print("The name '", new_name, "' was found in popular boy names list (line ", boy_list.index(new_name) + 1, ").", sep='')

        elif searchList(girl_list, new_name) and searchList(boy_list, new_name):
            print("The name '", new_name, "' was found in both lists: boy names (line ", boy_list.index(new_name) + 1, ") and girl names (line ", girl_list.index(new_name) + 1, ").", sep='')

        else:
            print("The name '", new_name, "' was not found in either list.", sep='')

        name_search = input("Enter a name to search or type QUIT to exit:\n").lower()
        new_name = name_search.capitalize()

def readContents(filename):
    new_list = []
    new_list = filename.readlines()

    index = 0
    while index < len(new_list):
        new_list[index] = new_list[index].rstrip('\n')
        index += 1

    filename.close()
    return new_list

def searchList(name_list, search_value):
    if search_value in name_list:
        return True
    else:
        return False

main()


import threading
import time
import traceback

from consts.proj_vars import ProjVar
from utils.clients.ssh import SSHClient, ControllerClient, NATBoxClient
from utils.exceptions import ThreadingError
from utils.tis_log import LOG

TIMEOUT_ERR = "Thread did not terminate within timeout. " \
              "Thread details: {} {} {}"
EVENT_TIMEOUT = "Event did not occur within timeout."
INFINITE_WAIT_EVENT_EXPECTED = "There is only one thread (Main Thread), " \
                               "waiting is likely to wait indefinitely"

class MThread(threading.Thread):
    """
    Multi threading class. Allows multiple threads to be run simultaneously.
    e.g. nova_helper.create_flavor('threading', 'auto', vcpus=2, ram=1024) is
    equivalent to...
        thread_1 = MThread(nova_helper.create_flavor, 'threading', 'auto',
        vcpus=2, ram=1024)
        thread_1.start_thread()
        thread_1.wait_for_thread_end()

    Other commands can be run between start_thread and wait_for_thread_end
    The function's output can be retrieved from thread_1.get_output()
    name should NOT be changed
    """
    total_threads = 0
    running_threads = []

    def __init__(self, func, *args, **kwargs):
        """

        Args:
            func (runnable): name of function to run. e.g.
            nova-helper.create_flavor. NOT nova_helper.create_flavor()
            *args:
            **kwargs:
        """
        threading.Thread.__init__(self)
        MThread.total_threads += 1
        self.thread_id = MThread.total_threads
        self.func = func
        self.args = args
        self.kwargs = kwargs
        self._output = None
        self._output_returned = threading.Event()
        self.timeout = None
        self._err = None

    def get_output(self, wait=True, timeout=None):
        """
        Get return value of self.func

        Args:
            wait (bool): Whether or not to wait for the output from self.func
            timeout:

        Returns: return value from self.func

        """
        if wait:
            if timeout is None:
                timeout = self.timeout

            if timeout:
                end_time = time.time() + timeout
                while time.time() < end_time:
                    if self._err:
                        raise ThreadingError(str(self._err))
                    if self._output_returned.is_set():
                        # LOG.info("{}: {} returned: {}".format(self.name,
                        # self.func.__name__, self._output.__str__))
                        break

        return self._output

    def get_error_info(self):
        return self._err

    def start_thread(self, timeout=3600):
        """
        Starts a thread.
        Test must wait for thread to terminate or it can continue running
        during other tests.

        Args:
            timeout (int): how long to wait for the thread to finish

        Returns:

        """
        self.timeout = timeout
        self.__start_thread_base()

    def __start_thread_base(self):
        self.start()

    def run(self):
        """
        Do not run this command. Start threads from start_thread functions
        Returns:

        """
        LOG.info("Starting {}".format(self.name))
        # run the function
        try:
            MThread.running_threads.append(self)
            LOG.info("Connecting to lab fip in new thread...")
            lab = ProjVar.get_var('lab')
            lab_fip = lab['floating ip']
            con_ssh = SSHClient(lab_fip)
            con_ssh.connect(use_current=False)
            ControllerClient.set_active_controller(con_ssh)

            if ProjVar.get_var('IS_DC'):
                LOG.info("Connecting to subclouds fip in new thread...")
                ControllerClient.set_active_controller(con_ssh, 'RegionOne')
                con_ssh_dict = ControllerClient.get_active_controllers_map()
                for name in con_ssh_dict:
                    if name in lab:
                        subcloud_fip = lab[name]['floating ip']
                        subcloud_ssh = SSHClient(subcloud_fip)
                        try:
                            subcloud_ssh.connect(use_current=False)
                            ControllerClient.set_active_controller(subcloud_ssh,
                                                                   name=name)
                        except:
                            if name == ProjVar.get_var('PRIMARY_SUBCLOUD'):
                                raise
                            LOG.warning('Cannot connect to {}'.format(name))

            LOG.info("Connecting to NatBox in new thread...")
            NATBoxClient.set_natbox_client()

            LOG.info("Execute function {}({}, {})".format(self.func.__name__,
                                                          self.args,
                                                          self.kwargs))
            self._output = self.func(*self.args, **self.kwargs)
            LOG.info("{} returned: {}".format(self.func.__name__,
                                              self._output.__str__()))
            self._output_returned.set()
        except:
            err = traceback.format_exc()
            # LOG.error("Error found in thread call {}".format(err))
            self._err = err
            raise
        finally:
            LOG.info("Terminating thread: {}".format(self.thread_id))
            if ProjVar.get_var('IS_DC'):
                ssh_clients = ControllerClient.get_active_controllers(
                    current_thread_only=True)
                for con_ssh in ssh_clients:
                    con_ssh.close()
            else:
                ControllerClient.get_active_controller().close()

            natbox_ssh = NATBoxClient.get_natbox_client()
            if natbox_ssh:
                natbox_ssh.close()

            LOG.debug("{} has finished".format(self.name))
            MThread.running_threads.remove(self)

    def wait_for_thread_end(self, timeout=3600, fail_ok=False):
        """
        Waits for thread (self) to finish executing.
        All tests should wait for threads to end before proceeding to
        teardown, unless it is expected to continue,
        e.g. LOG.tc_step will not work during setup or teardown
        Raise error if thread is still running after timeout
        Args:
            timeout (int): how long to wait for the thread to finish.
            self.timeout is preferred.
            fail_ok (bool): fail_ok=False will raise error if wait times out
            or fails test if thread exited due to error

        Returns (bool): True if thread is not running, False/exception otherwise

        """
        if not self.is_alive():
            LOG.info("{} was already finished".format(self.name))
            if self._err:
                if not fail_ok:
                    raise ThreadingError(
                        "Error in thread: {}".format(self._err))
                LOG.error("Error found in thread call {}".format(self._err))
            return True, self._err

        if not timeout:
            timeout = self.timeout

        LOG.info("Wait for {} to finish".format(self.name))
        self.join(timeout)

        if not fail_ok:
            assert not self._err, "{} ran into an error: {}".format(self.name,
                                                                    self._err)

        if not self.is_alive():
            LOG.info("{} has finished".format(self.name))
        else:
            # Thread didn't finish before timeout
            LOG.error("{} did not finish within timeout".format(self.name))
            if fail_ok:
                return False, self._err
            raise ThreadingError(
                TIMEOUT_ERR.format(self.func, self.args, self.kwargs))

        return True, self._err

def get_multi_threads():
    return MThread.running_threads

def is_multi_thread_active():
    if len(get_multi_threads()) == 0:
        return False
    return True

class Events(threading.Event):
    """
    Blocks thread progress when wait_for_event is called
    (Main Thread)               (Thread-1)
    event = Events()
    event.wait_for_event()
    # waits
                                event.set()
    # continues
    """

    def __init__(self, message="Simple event"):
        threading.Event.__init__(self)
        self.message = message
        self.msg_lock = threading.Lock()

    def set(self):
        """
        Set event flag to true. Allows threads waiting for the event to continue
        """
        threading.Event.set(self)
        LOG.info("Event \"{}\" flag set to True".format(self.message))

    def clear(self):
        threading.Event.clear(self)
        LOG.info("Event \"{}\" flag set to False".format(self.message))

    def wait_for_event(self, timeout=3600, fail_ok=False):
        """
        Waits for this Events object to have its flag set to True
        Args:
            timeout:
            fail_ok:

        Returns (bool): True if event flag set to true, False/exception
        otherwise

        """
        if self.is_set():
            LOG.info("Event \"{}\" has already been set".format(self.message))
            return True
        if not timeout:
            LOG.warning(
                "No timeout was specified. This can lead to waiting "
                "indefinitely")

        if not is_multi_thread_active() and not timeout:
            LOG.error(
                "There are no other running threads that can set this event "
                "to true. "
                "This would wait indefinitely")
            raise ThreadingError(INFINITE_WAIT_EVENT_EXPECTED)

        LOG.info(
            "Waiting for event \"{}\" flag set to true".format(self.message))
        if not threading.Event.wait(self, timeout):
            if fail_ok:
                LOG.error(
                    "Timed out waiting for event \"{}\" flag set to "
                    "True".format(
                        self.message))
                return False
            raise ThreadingError(EVENT_TIMEOUT)

        if self.msg_lock.acquire(False):
            # only one message should appear once event is set
            LOG.info("Threads continuing")
            time.sleep(1)
            self.msg_lock.release()
        return True

class TiSBarrier(threading.Barrier):
    """
    Blocks threads calling wait() until specified number (parties) of threads
    are waiting or timeout expires.
    (Main Thread)               (Thread-1)              (Thread-2)
    barr = TiSBarrier(2)
                                barr.wait()
                                # Thread-1 waits
                                                        barr.wait()
                                # both continue
    """

    def __init__(self, parties, action=None, timeout=180):
        """
        Args:
            parties (int): number of threads to wait for
            action (function): additional function to call when barrier breaks
            timeout (int): max wait time
        """
        if len(get_multi_threads()) + 1 < parties:
            LOG.warning(
                "This barrier will wait for more threads than are currently "
                "running")
        threading.Barrier.__init__(self, parties, action, timeout)

        self.timeout = timeout
        LOG.info("Created a barrier waiting for {} threads".format(parties))

    def wait(self, timeout=None):
        """
        Block thread until barrier breaks
        Exception is raised if timeout expires
        Args:
            timeout (int): max wait time at barrier. Preferred over
            self.timeout, if given

        Returns (int): number from 0 to (parties - 1), randomly assigned and
        unique for each thread that waited.
                       -1 if thread did not wait

        """
        self.barrier_status()
        if self.broken:
            return -1
        if not timeout:
            if not self.timeout:
                LOG.warning("This thread does not have a timeout for wait()")
            else:
                timeout = self.timeout

        try:
            LOG.info("Start waiting at barrier")
            id_ = threading.Barrier.wait(self, timeout)
            # only one thread will report the barrier being passed
            if id_ == 0:
                self._barrier_passed()
            return id
        except:
            LOG.info("Barrier broke before {} threads were waiting".format(
                self.parties))
            raise

    def barrier_status(self):
        if not self.broken:
            LOG.info(
                '{} threads are waiting... Barrier waiting for {} '
                'threads'.format(
                    self.n_waiting, self.parties))
        else:
            LOG.info("This barrier has already been passed.")

    def _barrier_passed(self):
        LOG.info("Barrier has been passed. {} threads continuing".format(
            self.parties))

class TiSLock:
    """
    RLock class with added logging
    lock can be acquired using 'lock.acquire()' or 'with lock as have_lock'
    lock released using 'lock.release()' or exiting with scope
    Do Not call __enter__ or __exit__ explicitly

    (Main Thread)               (Thread-1)
    lock = TiSLock()
    lock.acquire()
    # edit test.txt
                                lock.acquire()
                                # waits
    lock.release()
                                # continues
    """

    def __init__(self, blocking=True, timeout=180):
        """
        Create a locking object
        Args:
            blocking (bool): wait for lock to be released if it is locked
            during first acquire attempt
            timeout (int): how long to wait for lock to be released. Should
            set to -1 if blocking is false
                           -1 with blocking True will have no timeout
        """
        self._lock = threading.RLock()
        self.blocking = blocking
        self.timeout = timeout if blocking else -1

    def acquire(self, **kwargs):
        if 'blocking' in kwargs:
            self.blocking = kwargs['blocking']
        if 'timeout' in kwargs:
            self.timeout = kwargs['timeout']
        return self.__enter__()

    def release(self):
        return self.__exit__()

    def __enter__(self):
        """
        Make sure that True is returned.
        Threads will continue if timeout is reached even though lock was not
        acquired

        Returns (bool): True if lock was acquired, False otherwise

        """
        blocking_msg = "" if self.blocking else "not "
        timeout_msg = self.timeout if self.timeout >= 0 else "None"
        msg = "Attempting to acquire lock, {}blocking, timeout - {}".format(
            blocking_msg, timeout_msg)
        LOG.debug(msg)
        got_lock = False
        try:
            got_lock = self._lock.acquire(self.blocking, self.timeout)
        finally:
            if got_lock:
                LOG.debug("Acquired lock")
            else:
                LOG.debug("Could not acquire lock")
            return got_lock

    def __exit__(self, *args):
        LOG.debug("Releasing lock")
        try:
            return self._lock.release()
        except RuntimeError:
            LOG.error("Lock did not release, lock was unlocked already")
            raise
        except:
            LOG.error("An unexpected error was caught when unlocking lock")
            raise


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import os.path
import sys

import numpy as np
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf

import input_data
import models
from tensorflow.python.platform import gfile

FLAGS = None

def main(_):
  print(FLAGS.model_size_info)
  # We want to see all the logging messages for this tutorial.
  tf.logging.set_verbosity(tf.logging.INFO)

  # Start a new TensorFlow session.
  sess = tf.InteractiveSession()
  mag_squared_bool = FLAGS.spectrogram_magnitude_squared == 'True'
  # Begin by making sure we have the training data we need. If you already have
  # training data of your own, use `--data_url= ` on the command line to avoid
  # downloading.
  model_settings = models.prepare_model_settings(
      len(input_data.prepare_words_list(FLAGS.wanted_words.split(','))),
      FLAGS.sample_rate, FLAGS.clip_duration_ms, FLAGS.window_size_ms,
      FLAGS.window_stride_ms, FLAGS.dct_coefficient_count,100)
  audio_processor = input_data.AudioProcessor(
      FLAGS.data_url, FLAGS.data_dir, FLAGS.silence_percentage,
      FLAGS.unknown_percentage,
      FLAGS.wanted_words.split(','), FLAGS.validation_percentage,
      FLAGS.testing_percentage, model_settings,FLAGS.input_type, mag_squared_bool)
  fingerprint_size = model_settings['fingerprint_size']
  label_count = model_settings['label_count']
  time_shift_samples = int((FLAGS.time_shift_ms * FLAGS.sample_rate) / 1000)
  # Figure out the learning rates for each training phase. Since it's often
  # effective to have high learning rates at the start of training, followed by
  # lower levels towards the end, the number of steps and learning rates can be
  # specified as comma-separated lists to define the rate at each stage. For
  # example --how_many_training_steps=10000,3000 --learning_rate=0.001,0.0001
  # will run 13,000 training loops in total, with a rate of 0.001 for the first
  # 10,000, and 0.0001 for the final 3,000.
  training_steps_list = list(map(int, FLAGS.how_many_training_steps.split(',')))
  learning_rates_list = list(map(float, FLAGS.learning_rate.split(',')))
  if len(training_steps_list) != len(learning_rates_list):
    raise Exception(
        '--how_many_training_steps and --learning_rate must be equal length '
        'lists, but are %d and %d long instead' % (len(training_steps_list),
                                                   len(learning_rates_list)))

  fingerprint_input = tf.placeholder(
      tf.float32, [None, fingerprint_size], name='fingerprint_input')

  logits, dropout_prob = models.create_model(
      fingerprint_input,
      model_settings,
      FLAGS.model_architecture,
      FLAGS.model_size_info,
      is_training=True)

  # Define loss and optimizer
  ground_truth_input = tf.placeholder(
      tf.int64, [None], name='groundtruth_input')

  # Optionally we can add runtime checks to spot when NaNs or other symptoms of
  # numerical errors start occurring during training.
  control_dependencies = []
  if FLAGS.check_nans:
    checks = tf.add_check_numerics_ops()
    control_dependencies = [checks]

  # Create the back propagation and training evaluation machinery in the graph.
  with tf.name_scope('cross_entropy'):
    cross_entropy_mean = tf.losses.sparse_softmax_cross_entropy(
        labels=ground_truth_input, logits=logits)
  tf.summary.scalar('cross_entropy', cross_entropy_mean)
  with tf.name_scope('train'), tf.control_dependencies(control_dependencies):
    learning_rate_input = tf.placeholder(
        tf.float32, [], name='learning_rate_input')
    train_step = tf.train.GradientDescentOptimizer(
        learning_rate_input).minimize(cross_entropy_mean)
  predicted_indices = tf.argmax(logits, 1)
  correct_prediction = tf.equal(predicted_indices, ground_truth_input)
  confusion_matrix = tf.confusion_matrix(
      ground_truth_input, predicted_indices, num_classes=label_count)
  evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  tf.summary.scalar('accuracy', evaluation_step)

  global_step = tf.train.get_or_create_global_step()
  increment_global_step = tf.assign(global_step, global_step + 1)

  saver = tf.train.Saver(tf.global_variables())

  # Merge all the summaries and write them out to /tmp/retrain_logs (by default)
  merged_summaries = tf.summary.merge_all()
  train_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/train',
                                       sess.graph)
  validation_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/validation')

  tf.global_variables_initializer().run()

  start_step = 1

  if FLAGS.start_checkpoint:
    models.load_variables_from_checkpoint(sess, FLAGS.start_checkpoint)
    start_step = global_step.eval(session=sess)

  tf.logging.info('Training from step: %d ', start_step)

  set_size = audio_processor.set_size('testing')
  tf.logging.info('set_size=%d', set_size)
  total_accuracy = 0
  total_conf_matrix = None
  for i in xrange(0, set_size, FLAGS.batch_size):
    test_fingerprints, test_ground_truth = audio_processor.get_data(
        FLAGS.batch_size, i, model_settings,  FLAGS.background_frequency,
        FLAGS.background_volume, 0, 'testing', sess)
    test_accuracy, conf_matrix = sess.run(
        [evaluation_step, confusion_matrix],
        feed_dict={
            fingerprint_input: test_fingerprints,
            ground_truth_input: test_ground_truth,
            dropout_prob: 1.0
        })
    batch_size = min(FLAGS.batch_size, set_size - i)
    total_accuracy += (test_accuracy * batch_size) / set_size
    if total_conf_matrix is None:
      total_conf_matrix = conf_matrix
    else:
      total_conf_matrix += conf_matrix
  tf.logging.info('Confusion Matrix:\n %s' % (total_conf_matrix))
  tf.logging.info('Final test accuracy = %.1f%% (N=%d)' % (total_accuracy * 100,
                                                           set_size))

  text_file = open(FLAGS.result_path+'/accuracy_'+FLAGS.noise_set+".txt", "a")
  text_file.write(str(total_accuracy) + ',')
  text_file.close()

  text_file = open(FLAGS.result_path + '/conf_' + FLAGS.noise_set + "_"+FLAGS.SNR+".txt", "w")
  text_file.write(str(total_conf_matrix))
  text_file.close()

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--data_url',
      type=str,
      # pylint: disable=line-too-long
      default='',
      # pylint: enable=line-too-long
      help='Location of speech training data archive on the web.')
  parser.add_argument(
      '--data_dir',
      type=str,
      default='/tmp/speech_dataset/',
      help="""\
      Where to download the speech training data to.
      """)
  parser.add_argument(
      '--background_volume',
      type=float,
      default=0.1,
      help="""\
      How loud the background noise should be, between 0 and 1.
      """)
  parser.add_argument(
      '--background_frequency',
      type=float,
      default=0.8,
      help="""\
      How many of the training samples have background noise mixed in.
      """)
  parser.add_argument(
      '--silence_percentage',
      type=float,
      default=10.0,
      help="""\
      How much of the training data should be silence.
      """)
  parser.add_argument(
      '--unknown_percentage',
      type=float,
      default=10.0,
      help="""\
      How much of the training data should be unknown words.
      """)
  parser.add_argument(
      '--time_shift_ms',
      type=float,
      default=100.0,
      help="""\
      Range to randomly shift the training audio by in time.
      """)
  parser.add_argument(
      '--testing_percentage',
      type=int,
      default=10,
      help='What percentage of wavs to use as a test set.')
  parser.add_argument(
      '--validation_percentage',
      type=int,
      default=10,
      help='What percentage of wavs to use as a validation set.')
  parser.add_argument(
      '--sample_rate',
      type=int,
      default=16000,
      help='Expected sample rate of the wavs',)
  parser.add_argument(
      '--clip_duration_ms',
      type=int,
      default=1000,
      help='Expected duration in milliseconds of the wavs',)
  parser.add_argument(
      '--window_size_ms',
      type=float,
      default=30.0,
      help='How long each spectrogram timeslice is',)
  parser.add_argument(
      '--window_stride_ms',
      type=float,
      default=10.0,
      help='How long each spectrogram timeslice is',)
  parser.add_argument(
      '--dct_coefficient_count',
      type=int,
      default=40,
      help='How many bins to use for the MFCC fingerprint',)
  parser.add_argument(
      '--how_many_training_steps',
      type=str,
      default='15000,3000',
      help='How many training loops to run',)
  parser.add_argument(
      '--eval_step_interval',
      type=int,
      default=400,
      help='How often to evaluate the training results.')
  parser.add_argument(
      '--learning_rate',
      type=str,
      default='0.001,0.0001',
      help='How large a learning rate to use when training.')
  parser.add_argument(
      '--batch_size',
      type=int,
      default=100,
      help='How many items to train with at once',)
  parser.add_argument(
      '--summaries_dir',
      type=str,
      default='/tmp/retrain_logs',
      help='Where to save summary logs for TensorBoard.')
  parser.add_argument(
      '--wanted_words',
      type=str,
      default='yes,no,up,down,left,right,on,off,stop,go',
      help='Words to use (others will be added to an unknown label)',)
  parser.add_argument(
      '--train_dir',
      type=str,
      default='/tmp/speech_commands_train',
      help='Directory to write event logs and checkpoint.')
  parser.add_argument(
      '--save_step_interval',
      type=int,
      default=100,
      help='Save model checkpoint every save_steps.')
  parser.add_argument(
      '--start_checkpoint',
      type=str,
      default='',
      help='If specified, restore this pretrained model before any training.')
  parser.add_argument(
      '--model_architecture',
      type=str,
      default='conv',
      help='What model architecture to use')
  parser.add_argument(
      '--check_nans',
      type=bool,
      default=False,
      help='Whether to check for invalid numbers during processing')
  parser.add_argument(
      '--SNR',
      type=str,
      default='',
      help='SNR of test data')
  parser.add_argument(
      '--noise_set',
      type=str,
      default='',
      help='If there are unknown noise tyoes included')
  parser.add_argument(
      '--result_path',
      type=str,
      default='',
      help='path to where results should be put')
  parser.add_argument(
      '--input_type',
      type=str,
      default='MFCC',
      help='MFCC if DCT should be applied, log_mel if not')
  parser.add_argument(
      '--model_size_info',
      type=int,
      nargs="+",
      default=[128,128,128],
      help='Model dimensions - different for various models')
  parser.add_argument(
      '--spectrogram_magnitude_squared',
      type=str,
      default='True',
      help='Whether to use squared magnitude spectrogram or not')

  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)


inputData = open("Q_B_input.txt", "r")
outputData = open("Q_B_output.txt", "w")
outputLines = []

def output (string):
	string = str(string)
	print(string)
	outputLines.append(string)

def case (x, y):
	output("Case #" + str(x) + ": " + str(y))

def secondLargest (l):
	maxm = max(l)
	for (i, n) in enumerate(l):
		if n == maxm:
			l.pop(i)
			break
	return max(l)

def timeLeft (plates, cost, upper):
	if cost >= upper:
		return cost
	maxm = max(plates)
	if maxm <= 1:
		return maxm
	else:
		options = [maxm]
		for num in range(1, maxm // 2 + 1):
			move = plates[:]
			move[move.index(max(move))] -= num
			move.append(num)
			options.append(1 + timeLeft(move, cost + 1, upper))
		return min(options)

for (number, line) in enumerate(inputData.read().splitlines()[1:]):
	if number % 2 == 0:
		continue
	plates = list(map(int, line.split(" ")))
	case(number // 2 + 1, timeLeft(plates, 0, max(plates)))

outputData.write("\n".join(outputLines))
inputData.close()
outputData.close()


from django.template.defaulttags import register

from zweb_utils.format import currency_format, decimal_format, number_js_format

@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)

@register.filter
def money(val):
    try:
        return currency_format(val)
    except TypeError:
        return currency_format(0)

@register.filter
def js_format(val):
    return number_js_format(val)

@register.filter
def obra_codigo(obras, form):
    try:
        if form.initial:
            return "{}".format(obras.get(pk=form.initial["obra"]))
        else:
            return "{}".format(obras.get(pk=form.cleaned_data["obra"].pk))
    except:
        return ""

@register.filter
def nombre_familia(familias, form):
    try:
        if form.initial:
            return "{}".format(familias.get(pk=form.initial["familia"]))
        else:
            return "{}".format(familias.get(pk=form.cleaned_data["familia"].pk))
    except:
        return ""


#!/usr/bin/env python
from __future__ import unicode_literals
from prompt_toolkit.application import Application
from prompt_toolkit.layout import Layout
from prompt_toolkit.layout.dimension import D
from prompt_toolkit.widgets import Dialog
from ptterm import Terminal

def main():
    def done():
        application.exit()

    term = Terminal(
        width=D(preferred=60),
        height=D(preferred=25),
        done_callback=done)

    application = Application(
        layout=Layout(
            container=Dialog(
                title='Terminal demo',
                body=term,
                with_background=True
            ),
            focused_element=term
        ),
        full_screen=True,
        mouse_support=True,
    )
    application.run()

if __name__ == '__main__':
    main()


import datetime

import pandas as pd

import numpy as np

#atomman imports
from atomman.tools import uber_open_rmode
from DataModelDict import DataModelDict as DM

class Log(object):
    """Object for representing a LAMMPS log output"""

    def __init__(self, log_info=None):
        """Intitializes a Log object."""

        #Initialize simulation properties
        self.__simulations = []
        self.__lammps_version = None
        self.__lammps_date = None

        #Read log data if supplied
        if log_info is not None:
            self.read(log_info)

    def read(self, log_info, append=True):
        """Parses a LAMMPS screen output/log file for thermodynamic data."""

        #Rset properties and values if append is False
        if append is False:
            self.__simulations = []
            self.__lammps_version = None
            self.__lammps_date = None

        #Strings found before run and mimize simulations
        sim_trigger = ['Memory usage per processor =','Per MPI rank memory allocation (min/avg/max) =']

        #Handle file names, strings and open file-like objects equivalently
        with uber_open_rmode(log_info) as log_info:

            headers = []
            footers = []
            i = 0

            #for all lines in file/output
            for line in log_info:

                #skip blank lines
                if len(line.split()) == 0:
                    continue

                #Save the LAMMPS version information
                if line[:8] == 'LAMMPS (' and self.lammps_version is None:
                    month = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4,
                             'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8,
                             'Sep': 9, 'Oct': 10,'Nov': 11,'Dec': 12}
                    self.__lammps_version = line.strip()[8:-1]
                    d = self.lammps_version.split('-')[0].split()
                    self.__lammps_date = datetime.date(int(d[2]), month[d[1]], int(d[0]))

                #Check for strings listed before run and minimize simulations
                if any([trigger in line for trigger in sim_trigger]):
                    headers.append(i+1)

                #This follows both run and minimize simulations
                elif 'Loop time of' in line:
                    footers.append(i-1)

                i += 1

            #Add last line to footers for incomplete logs
            footers.append(i)

            #Reset file pointer
            log_info.seek(0)

            #for all lines in file/output
            for header, footer in zip(headers, footers):

                #Initialize simulation data dictionary
                sim = {}

                #Read thermo data and reset file pointer
                sim['thermo'] = pd.read_csv(log_info, header=header, nrows=footer-header, sep='\s+', engine='python', skip_blank_lines=True)
                log_info.seek(0)

                #Append simulation results
                self.__simulations.append(sim)

    @property
    def simulations(self):
        return self.__simulations

    @property
    def lammps_version(self):
        return self.__lammps_version

    @property
    def lammps_date(self):
        return self.__lammps_date

    def flatten(self, style='last'):
        """Combines all simulations into one

        Keyword Arguments:
        style -- specifies which values to use for duplicate time steps
            - first uses the values from the earlier simulation
            - last uses the values from the later simulation
        """

        for sim in self.simulations:
            if 'thermo' in sim and len(sim['thermo']) > 0:
                assert 'Step' in sim['thermo'], 'All simulation thermos must have Step key in order to flatten'
        merged_df = self.simulations[0]['thermo']
        for i in xrange(1, len(self.simulations)):
            if 'thermo' in self.simulations[i]:
                thermo = self.simulations[i]['thermo']
                if   style == 'first':
                    merged_df = pd.concat([merged_df, thermo[thermo.Step > merged_df.Step.max()]], ignore_index=True)
                elif style == 'last':
                    merged_df = pd.concat([merged_df[merged_df.Step < thermo.Step.min()], thermo], ignore_index=True)
                else:
                    raise ValueError('Unsupported style')
        self.__simulations = [{'thermo':merged_df}]

    def model(self, flatten=None):
        """Returns a DataModelDict containing the information."""
        if flatten is not None:
            self.flatten(flatten)

        #Create DataModelDict root
        log_model = DM()
        log_model['LAMMPS-log-thermo-data'] = DM()

        #Loop over all simulations
        for sim in self.simulations:
            sim_model = DM()

            #Convert to DataModelDict
            sim_model['thermo'] = DM()
            thermo = sim['thermo']
            for j in thermo:
                sim_model['thermo'][str(j)] = thermo[j].values.tolist()

            #Append simulation results to DataModelDict root
            log_model['LAMMPS-log-thermo-data'].append('simulation', sim_model)

        return log_model


from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import render_to_string
from django.conf import settings
from django.core.mail import  EmailMultiAlternatives

def send_login_mail(request,user):
    print('---------------->send_signup_otp_mail')
    current_site = get_current_site(request)
    subject = "Greetings From Test"
    signup = 'Test'
    to_email = [user.email]
    from_email = settings.EMAIL_HOST_USER
    # for production plese change http to https
    url =  "http://{}/user-login/{}".format(current_site,user.id)
    context =  ( {
        'username':user.fullname,
        'url':url
        } )
    html_content = render_to_string('accounts/send_user_login_mail.html', context)

    msg = EmailMultiAlternatives(subject=subject, body=signup, from_email=from_email, to=to_email)
    msg.attach_alternative( html_content , "text/html")
    email_sent_status = msg.send()

    return email_sent_status


#创建列表
list01=[]
list01=list()

#创建具有默认元素的列表
list01=[1,"大","true"]
print(list01)#[1, '大', 'true']

list01=list(range(5))
print(list01)#[0, 1, 2, 3, 4]

list01=list("大小小大")
print(list01)#['大', '小', '小', '大']

#1.添加元素
#append   #追加
list01=list(range(5))
print(list01)#[0, 1, 2, 3, 4]

list01.append("小大")
print(list01)#[0, 1, 2, 3, 4, '小大']

#2.insert    #插入
list01.insert(1,"大小")
print(list01)#[0, '大小', 1, 2, 3, 4, '小大']

#3.remove     #删除
#   del 列表名[索引或切片]
list01.remove("大小")  #del list01[1]
print(list01)#[0, 1, 2, 3, 4, '小大']

#4.修改
list01[1]="大大"
print(list01)#[0, '大大', 2, 3, 4, '小大']

#5.索引和切片
#(1)通过切片获取新的列表
list02=list01[:3]
print(list02)#[0, '大大', 2]

#(2)通过切片可以修改元素
list01[:3]=["a"]
print(list01)#['a', 3, 4, '小大']

#6.查询
#获取列表中的每个元素
#	正向：
#	for 变量名 in 列表名:
#		变量名就是元素
#	反向：
#	for 索引名 in range(len(列表名)-1,-1,-1):
#		列表名[索引名]就是元素
#

#练习：在控制台中录入所有学生的成绩
#   "请录入学生总数＂
#   "请录入第一个学生的成绩＂
#   (1)获取总分(2)获取最高分数(3)获取最低分
list_student_score=[]
student_count=int(input("请录入学生总数："))
for i in range(student_count):
    student_score=float(input("请录入第%d个学生成绩:"%(i+1)))
    list_student_score.append(student_score)
print("总分：%.1f"%sum(list_student_score))
print("最高分：%.1f"%max(list_student_score))
print("最低分：%.1f"%min(list_student_score))

#list01是变量，存储列表对象的地址
#引用列表
list01=["张无忌","赵敏","周芷若"]
list02=list01
#修改的是列表第一个元素存储的对象地址
list01[0]="老张"
print(list02[0])#老张
print(list02)#['老张', '赵敏', '周芷若']

list01=["张无忌","赵敏","周芷若"]
list02=list01
#修改的是变量存储的对象地址
list01="老张"
print(list02[0])#张无忌
print(list01)#老张

list01=["张无忌","赵敏","周芷若"]
#通过切片复制列表(拷贝了列表中的变量，而没有变量指向的对象)
list02=list01[:]
list01[0]="老张"
print(list02[0])#张无忌
print(list01)#['老张', '赵敏', '周芷若']
print(list02)#['张无忌', '赵敏', '周芷若']

list01=["张无忌",["赵敏","周芷若"]]
list02=list01
#修改列表第二个元素的第二个元素
list01[1][1]="芷若"
print(list01[1][1])#芷若
print(list01)#['张无忌', ['赵敏', '芷若']]
print(list02)#['张无忌', ['赵敏', '芷若']]

list01=["张无忌",["赵敏","周芷若"]]
list02=list01.copy()#浅拷贝
list01[1][1]="芷若"
print(list01[1][1])#芷若
print(list01)#['张无忌', ['赵敏', '芷若']]
print(list02)#['张无忌', ['赵敏', '芷若']]

#练习：在控制台中录入所有学生姓名，如果有重复的姓名不存入姓名
#       如果输入esc,则停止录入，在每行打印学生姓名
list_student=[]
while True:
    student_name=str(input("录入学生姓名"))
    if student_name =="esc":
        break
    if student_name in list_student:
        continue
    list_student.append(student_name)
#正向
for item in list_student:
    print(item)
#倒序获取列表中元素，通过索引获取元素
for i in range(len(list_student)-1,-1,-1):
    print(list_student[i])

#练习：将list_score列表中大于６０的元素存入list01中
#       list_score=[60,85,35,26,20,90]
list_score=[60,85,35,20,26,90]
list01=[]
for i in list_score:
    if i>60:
        list01.append(i)
print(list01)

#练习：获取list_score列表中的最大值
#       list_score=[60,85,35,26,20,90]

list_score=[60,85,35,20,26,90]
max_value=list_score[0]
for i in range(1,len(list_score)):
    if max_value<list_score[i]:
        max_value=list_score[i]
print(max_value)

#函数：
#   将多个字符串拼接为一个。
#   result = "连接符".join(列表)
#
#   将一个字符串拆分为多个。
#   列表 = “a-b-c-d”.split(“分隔符”)

#在控制台中重复录入字符串，直到输入esc为止，
#   最后打印拼接后的字符串
list=[]
while True:
    str_number=input("录入字符串")
    if str_number == "esc":
        break
    list.append(str_number)
str_result="".join(list)
print(str_result)

#练习：英文单词反转
# how are you-->you are how
english="how are you"
list_english=english.split(" ")
str_english=" ".join(list_english[::-1])
print(str_english)

#列表推导式
# 语法：
#   变量 = [表达式 for 变量 in 可迭代对象]
#   变量 = [表达式 for 变量 in 可迭代对象 if 条件]
#如果if真值表达式的布尔值为False,则可迭代对象生成的数据将被丢弃。
#列表推导式嵌套

#练习：将list01=[56,36,68,44,868]中所有元素的个位存储到lsit02中
list01=[56,36,68,44,868]
list02=[]
for i in list01:
        list02.append(i%10)
print(list02)#传统方法

list02=[i%10 for i in list01]
print(list02)#推导方法


from time import perf_counter
from fastapi import APIRouter
from fastapi import Response
from rio_tiler.errors import TileOutsideBounds
from rasterio.errors import RasterioIOError
from rio_tiler.utils import render
from rio_tiler.io import COGReader
from decouple import config
from cogtiler.classify import ftl, hfi, elevation, aspect, slope
from cogtiler import utils

logger = utils.getLogger()

router = APIRouter(
    prefix="/v0.0.1"
)

cache_control = config('CACHE-CONTROL', 'max-age=604800')
cache_expiry_seconds = 604800  # 10,080 minutes, 168 hours, 7 days

@router.get('/tile/{z}/{x}/{y}')
def tile_xyz(z: int, x: int, y: int, path: str, source: str, filter: str = None, tilesize: int = 512) -> Response:
    """
    Prepare your images:
    ```bash
    gdal_translate ftl2018.bin ftl_2018.tif -co TILED=YES -co COMPRESS=DEFLATE
    gdaladdo -r nearest ftl_2018.tif 2 4 8 16 32
    gdal_translate ftl_2018.tif ftl_2018_cloudoptimized.tif -co TILED=YES -co COMPRESS=LZW -co COPY_SRC_OVERVIEWS=YES
    ```
    """
    start = perf_counter()
    s3_url = f's3://{path}'
    key = f'/tile/{z}/{x}/{y}?path={path}&source={source}&filter={filter}&tilesize={tilesize}'
    cache = utils.create_redis()
    logger.info('%s ; s3_url: %s', key, s3_url)
    try:
        cached_data = cache.get(key)
    except Exception as error:
        cached_data = None
        logger.error(error, exc_info=error)
    if cached_data:
        logger.info('redis cache hit %s', key)
        response = Response(content=cached_data, media_type="image/png")
        response.headers["Cache-Control"] = cache_control
        return response
    else:
        logger.info('redis cache miss %s', key)
    try:
        with COGReader(s3_url) as cog:
            try:
                img = cog.tile(x, y, z, tilesize=tilesize)
            except TileOutsideBounds:
                response = Response(status_code=404)
                response.headers["Cache-Control"] = cache_control
                return response
        if source == 'ftl':
            data, mask = ftl.classify(img.data, filter)
        elif source == 'hfi':
            data, mask = hfi.classify(img.data)
        elif source == 'elevation':
            data, mask = elevation.classify(img.data)
        elif source == 'slope':
            data, mask = slope.classify(img.data)
        elif source == 'aspect':
            data, mask = aspect.classify(img.data)
    except RasterioIOError as e:
        # If the file is not found, return 404
        logger.error(e, exc_info=True)
        return Response(status_code=500)
    except Exception as e:
        logger.error(e, exc_info=True)
        return Response(status_code=500)
    # TODO: This part (`render`) gives an error:
    # "ERROR 4: `/vsimem/xxxx.tif' not recognized as a supported file format."
    # We can replace this with a different method. It's just taking an array of rgba values
    # and turning it into a png file.
    rendered = render(data, mask)
    response = Response(content=rendered, media_type="image/png")
    try:
        cache.set(key, rendered, ex=cache_expiry_seconds)
    except Exception as error:  # pylint: disable=broad-except
        logger.error(error, exc_info=error)
    # cache it for a week - we probably only need to cache for a day or two, but this is on
    # the client, so we don't care.
    response.headers["Cache-Control"] = cache_control
    end = perf_counter()
    delta = end - start
    logger.info('/ftl/%s/%s/%s?path=%s&source=%s took %f seconds', z, x, y, path, source, delta)
    return response

@router.get('/value/{band}/{lat}/{lon}')
def value(band: int, lat: float, lon: float, path: str) -> Response:
    s3_url = f's3://{path}'
    try:
        with COGReader(s3_url) as cog:
            point = cog.point(lon, lat, indexes=band)
        # TODO: put in nice json response. This could even be a nice geojson thing
        return point[band - 1]
    except RasterioIOError:
        # If the file is not found, return 404
        response = Response(status_code=404)
        return response


import sys
from collections import defaultdict
from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import Optional
from typing import Set
from typing import Type

from xsdata.exceptions import XmlContextError
from xsdata.formats.bindings import T
from xsdata.formats.dataclass.compat import class_types
from xsdata.formats.dataclass.models.builders import XmlMetaBuilder
from xsdata.formats.dataclass.models.elements import XmlMeta
from xsdata.models.enums import DataType
from xsdata.utils.constants import return_input

class XmlContext:
    """
    The service provider for binding operations' metadata.

    :param element_name_generator: Default element name generator
    :param attribute_name_generator: Default attribute name generator
    :param class_type: Default class type `dataclasses`
    :param models_package: Restrict auto locate to a specific package
    """

    __slots__ = (
        "element_name_generator",
        "attribute_name_generator",
        "class_type",
        "cache",
        "xsi_cache",
        "sys_modules",
        "models_package",
    )

    def __init__(
        self,
        element_name_generator: Callable = return_input,
        attribute_name_generator: Callable = return_input,
        class_type: str = "dataclasses",
        models_package: Optional[str] = None,
    ):
        self.element_name_generator = element_name_generator
        self.attribute_name_generator = attribute_name_generator
        self.class_type = class_types.get_type(class_type)

        self.cache: Dict[Type, XmlMeta] = {}
        self.xsi_cache: Dict[str, List[Type]] = defaultdict(list)
        self.models_package = models_package
        self.sys_modules = 0

    def reset(self):
        self.cache.clear()
        self.xsi_cache.clear()
        self.sys_modules = 0

    def get_builder(
        self, globalns: Optional[Dict[str, Callable]] = None
    ) -> XmlMetaBuilder:
        return XmlMetaBuilder(
            class_type=self.class_type,
            element_name_generator=self.element_name_generator,
            attribute_name_generator=self.attribute_name_generator,
            globalns=globalns,
        )

    def fetch(
        self,
        clazz: Type,
        parent_ns: Optional[str] = None,
        xsi_type: Optional[str] = None,
    ) -> XmlMeta:
        """
        Fetch the model metadata of the given dataclass type, namespace and xsi
        type.

        :param clazz: The requested dataclass type
        :param parent_ns: The inherited parent namespace
        :param xsi_type: if present it means that the given clazz is
            derived and the lookup procedure needs to check and match a
            dataclass model to the qualified name instead
        """
        meta = self.build(clazz, parent_ns)
        subclass = None
        if xsi_type and meta.target_qname != xsi_type:
            subclass = self.find_subclass(clazz, xsi_type)

        return self.build(subclass, parent_ns) if subclass else meta

    def build_xsi_cache(self):
        """Index all imported dataclasses by their xsi:type qualified name."""
        if len(sys.modules) == self.sys_modules:
            return

        self.xsi_cache.clear()
        builder = self.get_builder()
        for clazz in self.get_subclasses(object):
            if self.is_binding_model(clazz):
                meta = builder.build_class_meta(clazz)

                if meta.target_qname:
                    self.xsi_cache[meta.target_qname].append(clazz)

        self.sys_modules = len(sys.modules)

    def is_binding_model(self, clazz: Type[T]) -> bool:
        if not self.class_type.is_model(clazz):
            return False

        return not self.models_package or (
            hasattr(clazz, "__module__")
            and isinstance(clazz.__module__, str)
            and clazz.__module__.startswith(self.models_package)
        )

    def find_types(self, qname: str) -> List[Type[T]]:
        """
        Find all classes that match the given xsi:type qname.

        - Ignores native schema types, xs:string, xs:float, xs:int, ...
        - Rebuild cache if new modules were imported since last run

        :param qname: Qualified name
        """
        if not DataType.from_qname(qname):
            self.build_xsi_cache()
            if qname in self.xsi_cache:
                return self.xsi_cache[qname]

        return []

    def find_type(self, qname: str) -> Optional[Type[T]]:
        """
        Return the most recently imported class that matches the given xsi:type
        qname.

        :param qname: Qualified name
        """
        types: List[Type] = self.find_types(qname)
        return types[-1] if types else None

    def find_type_by_fields(self, field_names: Set[str]) -> Optional[Type[T]]:
        """
        Find a dataclass from all the imported modules that matches the given
        list of field names.

        :param field_names: A unique list of field names
        """

        def get_field_diff(clazz: Type) -> int:
            meta = self.cache[clazz]
            local_names = {var.local_name for var in meta.get_all_vars()}
            return len(local_names - field_names)

        self.build_xsi_cache()
        choices = [
            (clazz, get_field_diff(clazz))
            for types in self.xsi_cache.values()
            for clazz in types
            if self.local_names_match(field_names, clazz)
        ]

        choices.sort(key=lambda x: (x[1], x[0].__name__))
        return choices[0][0] if len(choices) > 0 else None

    def find_subclass(self, clazz: Type, qname: str) -> Optional[Type]:
        """
        Compare all classes that match the given xsi:type qname and return the
        first one that is either a subclass or shares the same parent class as
        the original class.

        :param clazz: The search dataclass type
        :param qname: Qualified name
        """
        types: List[Type] = self.find_types(qname)
        for tp in types:
            # Why would an xml node with have an xsi:type that points
            # to parent class is beyond me but it happens, let's protect
            # against that scenario <node xsi:type="nodeAbstract" />
            if issubclass(clazz, tp):
                continue

            for tp_mro in tp.__mro__:
                if tp_mro is not object and tp_mro in clazz.__mro__:
                    return tp

        return None

    def build(
        self,
        clazz: Type,
        parent_ns: Optional[str] = None,
        globalns: Optional[Dict[str, Callable]] = None,
    ) -> XmlMeta:
        """
        Fetch from cache or build the binding metadata for the given class and
        parent namespace.

        :param clazz: A dataclass type
        :param parent_ns: The inherited parent namespace
        """
        if clazz not in self.cache:
            builder = self.get_builder(globalns)
            self.cache[clazz] = builder.build(clazz, parent_ns)
        return self.cache[clazz]

    def build_recursive(self, clazz: Type, parent_ns: Optional[str] = None):
        """Build the binding metadata for the given class and all of its
        dependencies."""
        if clazz not in self.cache:
            meta = self.build(clazz, parent_ns)
            for var in meta.get_all_vars():
                types = var.element_types if var.elements else var.types
                for tp in types:
                    if self.class_type.is_model(tp):
                        self.build_recursive(tp, meta.namespace)

    def local_names_match(self, names: Set[str], clazz: Type) -> bool:
        try:
            meta = self.build(clazz)
            local_names = {var.local_name for var in meta.get_all_vars()}
            return not names.difference(local_names)
        except (XmlContextError, NameError):
            # The dataclass includes unsupported typing annotations
            # Let's remove it from xsi_cache
            builder = self.get_builder()
            target_qname = builder.build_class_meta(clazz).target_qname
            if target_qname and target_qname in self.xsi_cache:
                self.xsi_cache[target_qname].remove(clazz)

            return False

    @classmethod
    def is_derived(cls, obj: Any, clazz: Type) -> bool:
        """
        Return whether the given obj is derived from the given dataclass type.

        :param obj: A dataclass instance
        :param clazz: A dataclass type
        """
        if obj is None:
            return False

        if isinstance(obj, clazz):
            return True

        return any(x is not object and isinstance(obj, x) for x in clazz.__bases__)

    @classmethod
    def get_subclasses(cls, clazz: Type):
        try:
            for subclass in clazz.__subclasses__():
                yield from cls.get_subclasses(subclass)
                yield subclass
        except TypeError:
            pass


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

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

_URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'

zip_dir = tf.keras.utils.get_file('cats_and_dogs_filterted.zip', origin=_URL, extract=True)

base_dir = os.path.join(os.path.dirname(zip_dir), 'cats_and_dogs_filtered')
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')

train_cats_dir = os.path.join(train_dir, 'cats')  # directory with our training cat pictures
train_dogs_dir = os.path.join(train_dir, 'dogs')  # directory with our training dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')  # directory with our validation cat pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')  # directory with our validation dog pictures

num_cats_tr = len(os.listdir(train_cats_dir))
num_dogs_tr = len(os.listdir(train_dogs_dir))

num_cats_val = len(os.listdir(validation_cats_dir))
num_dogs_val = len(os.listdir(validation_dogs_dir))

total_train = num_cats_tr + num_dogs_tr
total_val = num_cats_val + num_dogs_val

print('total training cat images:', num_cats_tr)
print('total training dog images:', num_dogs_tr)

print('total validation cat images:', num_cats_val)
print('total validation dog images:', num_dogs_val)
print("--")
print("Total training images:", total_train)
print("Total validation images:", total_val)

BATCH_SIZE = 100
IMG_SHAPE  = 150 # Our training data consists of images with width of 150 pixels and height of 150 pixels

# Data Augmentation

## Flipping the image horizontally
image_gen = ImageDataGenerator(rescale=1./255, horizontal_flip=True)

train_data_gen = image_gen.flow_from_directory(batch_size=BATCH_SIZE,
                                               directory=train_dir,
                                               shuffle=True,
                                               target_size=(IMG_SHAPE,IMG_SHAPE))

## Rotating the image
image_gen = ImageDataGenerator(rescale=1./255, rotation_range=45)

train_data_gen = image_gen.flow_from_directory(batch_size=BATCH_SIZE,
                                               directory=train_dir,
                                               shuffle=True,
                                               target_size=(IMG_SHAPE, IMG_SHAPE))

## Applying Zoom
image_gen = ImageDataGenerator(rescale=1./255, zoom_range=0.5)

train_data_gen = image_gen.flow_from_directory(batch_size=BATCH_SIZE,
                                               directory=train_dir,
                                               shuffle=True,
                                               target_size=(IMG_SHAPE, IMG_SHAPE))

## Putting it all together
image_gen_train = ImageDataGenerator(
      rescale=1./255,
      rotation_range=40,
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest')

train_data_gen = image_gen_train.flow_from_directory(batch_size=BATCH_SIZE,
                                                     directory=train_dir,
                                                     shuffle=True,
                                                     target_size=(IMG_SHAPE,IMG_SHAPE),
                                                     class_mode='binary')

# Creating Validation Data generator
image_gen_val = ImageDataGenerator(rescale=1./255)

val_data_gen = image_gen_val.flow_from_directory(batch_size=BATCH_SIZE,
                                                 directory=validation_dir,
                                                 target_size=(IMG_SHAPE, IMG_SHAPE),
                                                 class_mode='binary')

# Define the model
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),

    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(2, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

print(model.summary())
"""
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d (Conv2D)              (None, 148, 148, 32)      896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 74, 74, 32)        0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 72, 72, 64)        18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 36, 36, 64)        0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 34, 34, 128)       73856
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 17, 17, 128)       0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 15, 15, 128)       147584
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 7, 7, 128)         0
_________________________________________________________________
dropout (Dropout)            (None, 7, 7, 128)         0
_________________________________________________________________
flatten (Flatten)            (None, 6272)              0
_________________________________________________________________
dense (Dense)                (None, 512)               3211776
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 1026
=================================================================
Total params: 3,453,634
Trainable params: 3,453,634
Non-trainable params: 0
_________________________________________________________________
"""

# Train the model
epochs=100
history = model.fit_generator(
    train_data_gen,
    steps_per_epoch=int(np.ceil(total_train / float(BATCH_SIZE))),
    epochs=epochs,
    validation_data=val_data_gen,
    validation_steps=int(np.ceil(total_val / float(BATCH_SIZE)))
)

# Visualizing results of the training
acc = history.history['acc']
val_acc = history.history['val_acc']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.savefig('./l05c02_augmentation.png')


# https://leetcode.com/problems/removing-stars-from-a-string/

class Solution:
    def removeStars(self, s: str) -> str:
        """
        Proceses string in a way that removes all starts and letter before star
        :param s:
        :return:

        loop over the string
        add letters to stack if * pop letter
        loop over a stack to create string and return it.
        """

        stack = []

        for char in s:
            if char == '*':
                stack.pop()  # Pops char
            else:
                stack.append(char)  # Adds char

        return "".join(stack)  # Merge chars


# noqa D100
import mock

def test_touch_setup(cap1xxx):
    """Test that touch initialises Cap1166."""
    from gfxhat import touch

    touch.setup()

    cap1xxx.Cap1166.assert_called_with(i2c_addr=touch.I2C_ADDR)

def test_touch_names():
    """Test that get_name returns a sensible value."""
    from gfxhat import touch

    assert touch.get_name(0) == 'up'

def test_touch_set_led(cap1xxx):
    """Test that set_led calls Cap1166.set_led_state."""
    from gfxhat import touch

    touch.set_led(0, True)

    cap1xxx.Cap1166().set_led_state.assert_called_with(touch.LED_MAPPING[0], True)

def test_touch_high_sensitivity(cap1xxx):
    """Test that high sensitivity calls custom commands.

    Tests should be updated if/when this behaviour is formalised in Cap1xxx.

    """
    from gfxhat import touch

    touch.high_sensitivity()

    cap1xxx.Cap1166()._write_byte.assert_has_calls([
        mock.call(0x00, 0b11000000),
        mock.call(0x1f, 0b00000000)
    ])

def test_touch_repeat(cap1xxx):
    """Test that touch repeat calls Cap1166.enable_repeat()."""
    from gfxhat import touch

    touch.enable_repeat(True)
    cap1xxx.Cap1166().enable_repeat.assert_called_with(0b11111111)

    touch.enable_repeat(False)
    cap1xxx.Cap1166().enable_repeat.assert_called_with(0b00000000)

def test_touch_repeat_rate(cap1xxx):
    """Test that touch repeat rate calls Cap1166.set_repeat_rate()."""
    from gfxhat import touch

    touch.set_repeat_rate(35)
    cap1xxx.Cap1166().set_repeat_rate.assert_called_with(35)

def test_attach_handler(cap1xxx):
    """Test that both handler modes call Cap1166.on()."""
    from gfxhat import touch

    decorator = touch.on([1])

    def handler():
        pass

    decorator(handler)

    cap1xxx.Cap1166().on.assert_has_calls([
        mock.call(channel=1, event='press', handler=handler),
        mock.call(channel=1, event='release', handler=handler),
        mock.call(channel=1, event='held', handler=handler),
    ])

    touch.on([0], handler=handler)

    cap1xxx.Cap1166().on.assert_has_calls([
        mock.call(channel=0, event='press', handler=handler),
        mock.call(channel=0, event='release', handler=handler),
        mock.call(channel=0, event='held', handler=handler),
    ])


from src import utils
from os import getenv
from redis import Redis
import config
import messagebird

redis = Redis().from_url(getenv("REDIS_URL"))
msg_client = messagebird.Client(getenv("MESSAGEBIRD"))

def send_sms(src, dst, text, key=None):
	if key is not None:
		key = key.encode()
		keys = redis.lrange("keys", 0, -1)
		if key not in keys:
			return utils.error("Invalid key")

	fixed_src = utils.fix_number(src)
	if fixed_src.replace(config.phone_prepend, "").isdecimal() and len(fixed_src) == config.phone_total_length:
		src = fixed_src
	dst = utils.fix_number(dst)

	if len(dst) != config.phone_total_length:
		return utils.error("Destination number is not 8 characters long")

	if len(text) == 0 or (len(text.encode()) > 150 and key is not None):
		return utils.error("Invalid message length")

	if src.lower() == "nicesms" and key is not None:
		return utils.error("Reserved sender!")

	try:
		message = msg_client.message_create(src, dst, text)
		if key is not None:
			redis.lrem("keys", 0, key)
	except messagebird.ErrorException as e:
		print(e)
		return utils.error("Unknown Error! Contact the admin. (Key not used)")

	if key:
		print(f"Sent sms | {src} => {dst} | Key: {key.decode()} | Text: {text}")
	else:
		print(f"Sent sms | {src} => {dst} | Text: {text}")

	return message

def make_call(src, dst, text, key=None):
	if key is not None:
		key = key.encode()
		keys = redis.lrange("keys", 0, -1)
		if key not in keys:
			return utils.error("Invalid key")

	src = utils.fix_number(src)
	dst = utils.fix_number(dst)

	if len(dst) != 11 or len(src) != 11:
		return utils.error("Destination and Source numbers are not 8 characters long")

	if len(text) == 0 or len(text.encode()) > 140:
		return utils.error("Invalid tts message length")

	try:
		result = msg_client.voice_message_create(dst, text, params={"originator": src, "language": "da-DK"})
		if key is not None:
			redis.lrem("keys", 0, key)
	except messagebird.ErrorException:
		return utils.error("Unknown Error! Contact the admin. (Key not used)")

	if key:
		print(f"Made call | {src} => {dst} | Key: {key.decode()} | Text: {text}")
	else:
		print(f"Made call | {src} => {dst} | Text: {text}")

	return result

def get_msg(msg_id):
	try:
		return msg_client.message(msg_id)
	except messagebird.ErrorException:
		try:
			return msg_client.voice_message(msg_id)
		except messagebird.ErrorException:
			return utils.error("Message not found!")


# %%
import os
import pandas as pd
from sklearn import tree
from sklearn import model_selection
from sklearn import metrics
from sklearn import preprocessing
from sklearn import ensemble
from sklearn import pipeline
from sklearn import decomposition
from sklearn.model_selection import RandomizedSearchCV

import random

import xgboost as xgb

from feature_engine.imputation import ArbitraryNumberImputer
from feature_engine.discretisation import DecisionTreeDiscretiser

# %%
ORACLE_DIR = os.path.dirname(os.path.abspath(__file__))
#ORACLE_DIR = os.path.abspath(".")
SRC_DIR = os.path.dirname(ORACLE_DIR)
BASE_DIR = os.path.dirname(SRC_DIR)
DATA_DIR = os.path.join(BASE_DIR, "data")

DATA_TRAIN_DIR = os.path.join(DATA_DIR, "train")
TABLE_PATH = os.path.join(DATA_TRAIN_DIR, "tb_abt_oracle")

file_path = [i for i in os.listdir(TABLE_PATH) if i.endswith(".csv")][-1]
file_path = os.path.join(TABLE_PATH, file_path)

# %%

# Importando o dataset
print("Importando dataset...")
df_full = pd.read_csv(file_path)

columns = df_full.columns.tolist()
features =list(set(columns) - set(['match_id', 'dt_start_time', 'radiant_win']))
target = 'radiant_win'

df_full[features] = df_full[features].dropna(how="all")
print("ok.")

# %%
# Separa entre treinamento e teste

print("Separando em base de treino e teste...")
X_train, X_test, y_train, y_test = model_selection.train_test_split( df_full[features],
                                                                     df_full[target],
                                                                     random_state=42,
                                                                     test_size=0.1 )

print("ok.")

# %%

print("Ajustando modelo em nosso pipeline...")
arbitrary_imputer = ArbitraryNumberImputer(arbitrary_number=-999,
                                           variables=features)

disc = DecisionTreeDiscretiser(cv=3,
                               scoring='roc_auc',
                               variables=features,
                               regression=False,
                               random_state=42)

pca = decomposition.PCA(n_components=120,
                        random_state=42)

best_pars = {'subsample': 0.7, 'n_estimators': 100, 'max_depth': 5, 'learning_rate': 0.2}

clf_xgb = xgb.XGBClassifier(nthread=8,
                            eval_metric='auc',
                            random_state=42,
                            **best_pars)

"""
params = {
    "n_estimators":[100,500,600,700,800],
    "max_depth":[4,5,6,7,10],
    "subsample":[0.1,0.2,0,3, 0.7, 0.75,0.8,0.9, 0.99],
    "learning_rate":[0.1, 0.2, 0.5, 0.7, 0.8, 0.9, 0.99],
}

random_search = RandomizedSearchCV(clf_xgb,
                                   params,
                                   scoring="roc_auc",
                                   n_jobs=1,
                                   random_state=42,
                                   n_iter=20,
                                   cv=4)
"""

my_pipe = pipeline.Pipeline( [("imputer", arbitrary_imputer),
                              ("discretizator", disc),
                              ("pca", pca),
                              ("model", clf_xgb)])

my_pipe.fit(X_train, y_train)

print("ok.")

# %%
print("Analisando performance na base de teste...")
y_test_pred = my_pipe.predict(X_test)
y_test_prob = my_pipe.predict_proba(X_test)
auc_score = metrics.roc_auc_score(y_test, y_test_prob[:,1])
print("AUC:", auc_score)
print("ok.")

# %%

print("Salvando o modelo...")
model_dict = {"model": my_pipe,
              "features"  : X_train.columns.tolist(),
              "auc_test" : auc_score
              }

model = pd.Series(model_dict)
model.to_pickle(os.path.join(ORACLE_DIR, "models", "model_ale.pkl"))
print("Ok.")


def main():
    i = 0
    list1 = []
    list1 = getInput()
    for i in list1:
        print(i, end = " ")

    print("\n", id(list1))
    cubeList(list1)

def getInput():
    i = 0
    x = []
    for i in range(10):
        y = float(input("Please enter a Variable: "))
        x.append(y)
    return x

def cubeList(x):
    i = 0
    for i in range(len(x)):
        x[i] = x[i]**3
    for i in x:
        print(i, end = " ")

    print("\n", id(x))

main()


#!/usr/bin/python3
""" Defines lockboxes """

def canUnlockAll(boxes):
    n = len(boxes)
    opened = [False] * n
    opened[0] = True
    queue = [0]

    while queue:
        current_box = queue.pop(0)
        for key in boxes[current_box]:
            if key < n and not opened[key]:
                opened[key] = True
                queue.append(key)

    return all(opened)