from django.shortcuts import render
from .models import Product, Company
from django.http import HttpResponseRedirect, HttpResponseNotFound
from .forms import ProductForm

def index(request):
    products = Product.objects.all()
    return render(request, 'index.html', {'products': products})

def create(request):
    create_companies()
    form = ProductForm()
    if request.method == 'POST':
        form = ProductForm(request.POST)
        if form.is_valid():
            form.save()
        return HttpResponseRedirect('/')

    return render(request, 'create.html', {'form': form})

def edit(request, id):
    try:
        product = Product.objects.get(id=id)
        form = ProductForm(instance=product)
        if request.method == "POST":
            form = ProductForm(request.POST, instance=product)
            if form.is_valid():
                form.save()
            return HttpResponseRedirect("/")
        else:
            return render(request, "edit.html", {"form": form})
    except Product.DoesNotExist:
        return HttpResponseNotFound(" <h2›Product not found
")

def delete(request, id):
    try:
        product = Product.objects.get(id=id)
        product.delete()
        return HttpResponseRedirect("/")
    except Product.DoesNotExist:
        return HttpResponseNotFound("
Product not found
")

def create_companies():
    if Company.objects.all().count() == 0:
        Company.objects.create(name="Apple")
        Company.objects.create(name="Asus")
        Company.objects.create(name="MSI")


import math
import random

from logger import logger
import graphTraverser

class Infecter():

	def __init__(self):
		pass

	def infect_single_user(self, user):
		user.infected = True

	def infect_random(self, users, numToInfect):
		users = users[:]
		for i in range(numToInfect):
			user = random.choice(users)
			user.infected = True
			users.remove(user)

	def infect_all(self, users):
		for user in users:
			user.infected = True

	def infect_from(self, user):
		graphTraverser.breadth_first_apply_function(user, self.infect_single_user)

	def infect_limited(self, users, numUsersToInfect, acceptableErrorPercentage, debug=False):
		numOptionalUsers = self.get_int_percentage_of(numUsersToInfect, acceptableErrorPercentage)
		minNumToInfect = numUsersToInfect - numOptionalUsers
		maxNumToInfect = numUsersToInfect + numOptionalUsers
		remaining = minNumToInfect

		subgraphs = graphTraverser.get_subgraphs_sorted(users)

		finalSubgraph = None
		for subgraph in subgraphs:
			if remaining <= 0:
				break

			bestStartCandidate = self.find_user_with_fewest_connections(subgraph)
			self.infect_limited_from(bestStartCandidate, remaining)

			remaining -= self.get_num_infected(subgraph)
			finalSubgraph = subgraph

		optionalRemaining = maxNumToInfect - minNumToInfect
		self.infect_while_improving(finalSubgraph, optionalRemaining)

	def get_int_percentage_of(self, value, percentage):
		return math.floor(value * (percentage / 100))

	def infect_limited_from(self, user, numUsersToInfect):
		user.epicenter = True
		self.infect_single_user(user)
		infectedUsers = [user]
		while len(infectedUsers) < numUsersToInfect:

			nextToInfect = self.find_next_to_infect(infectedUsers)
			if nextToInfect is None:
				return

			self.infect_single_user(nextToInfect)
			infectedUsers.append(nextToInfect)

	def find_next_to_infect(self, infectedUsers):
		nextToInfect = None
		fewestConnections = math.inf

		for user in infectedUsers:

			neighborWithFewest = self.find_neighbor_with_fewest_connections(user)
			if neighborWithFewest is None:
				continue
			elif neighborWithFewest.numConnections < fewestConnections:
				fewestConnections = neighborWithFewest.numConnections
				nextToInfect = neighborWithFewest

		return nextToInfect

	def infect_while_improving(self, users, limit):
		quality_level = self.get_solution_quality(users)
		remainingUsers = self.get_all_infected(users)
		while len(remainingUsers) > 0:
			user = remainingUsers.pop()
			for neighbor in self.get_clean_neighbors(user):
				if limit <= 0: return
				neighbor.infected = True
				new_quality_level = self.get_solution_quality(users)
				if new_quality_level > quality_level:
					neighbor.infected = False
				else:
					remainingUsers.append(neighbor)
					limit -= 1
					quality_level = new_quality_level

	def find_neighbor_with_fewest_connections(self, user):
		cleanNeighbors = self.get_clean_neighbors(user)
		fewestNeighbors = math.inf
		neighborWithFewest = None
		for neighbor in cleanNeighbors:
			numNeighbors = len(self.get_clean_neighbors(neighbor))
			if numNeighbors < fewestNeighbors:
				neighbor.numConnections = numNeighbors
				fewestNeighbors = numNeighbors
				neighborWithFewest = neighbor

		return neighborWithFewest

	def find_user_with_fewest_connections(self, users):
		fewestConnections = math.inf
		userWithFewest = None
		for user in users:
			numConnections = len(user.neighbors)
			if numConnections < fewestConnections and user.infected == False:
				fewestConnections = numConnections
				userWithFewest = user

		return userWithFewest

	def get_all_infected(self, users):
		return [user for user in users if user.infected]

	def get_solution_quality(self, users):
		cleanNeighbors = []
		infected = [user for user in users if user.infected]
		for user in infected:
			cleanNeighbors.extend(self.get_clean_neighbors(user))
		return len(cleanNeighbors)

	def get_clean_neighbors(self, user):
		return [ neighbor for neighbor in user.neighbors if not neighbor.infected ]

	def get_num_infected(self, users):
		numInfected = 0
		for user in users:
			if user.infected:
				numInfected += 1
		return numInfected


import quantstats as qs
# import streamlit as st
# import streamlit.components.v1 as components
# import quantstats as qs
# import pandas as pd
# import numpy as np
# from bokeh.plotting import figure
# from PIL import Image
# from patterns_list import candlestick_patterns

def create_benchmark_report(dataname,benchmark):

    stock = qs.utils.download_returns(dataname)
    bench = qs.utils.download_returns(benchmark)

    stock.index = stock.index.tz_convert(None)
    bench.index = bench.index.tz_convert(None)
    print(stock)
    qs.reports.html(stock, mode='full', benchmark=bench ,output='output_plot/test12_report.html',title=f"{dataname} VS {benchmark}")


from typing import List, Set, Dict, Optional

class Transaction:
    def __init__(self, buyer_id: str,
                 seller_id: str,
                 amount: int,
                 price: int) -> None:
        self.buyer_id = buyer_id
        self.seller_id = seller_id
        self.amount = amount
        self.price = price

class Order:
    def __init__(self, trader_id: str,
                 amount: int,
                 price: int) -> None:
        self.trader_id = trader_id
        self.amount = amount
        self.price = price

class Stock:
    def __init__(self) -> None:
        self.history: List[Transaction] = []
        self.buyers: List[Order] = []
        self.sellers: List[Order] = []

StockExchange = Dict[str, Stock]

def add_new_stock(stock_exchange: StockExchange, ticker_symbol: str) -> bool:
    if ticker_symbol in stock_exchange:
        return False
    else:
        stock_exchange[ticker_symbol] = Stock()
        return True

def deal(stock_exchange: StockExchange, ticker_symbol: str) -> None:
    s_e = stock_exchange[ticker_symbol]
    len_sellers = len(s_e.sellers)
    len_buyers = len(s_e.buyers)

    if len_sellers > 0:

        for i in range(len_buyers - 1, -1, -1):

            for j in range(len_sellers - 1, -1, -1):
                seller = s_e.sellers[j]
                buyer = s_e.buyers[i]

                if buyer.price >= seller.price \
                        and seller.amount > 0 \
                        and buyer.amount > 0:

                    if seller.amount > buyer.amount:
                        s_e.history.append(Transaction
                                           (buyer.trader_id,
                                            seller.trader_id,
                                            buyer.amount,
                                            buyer.price))
                        seller.amount -= buyer.amount
                        buyer.amount = 0

                    else:
                        s_e.history.append(Transaction
                                           (buyer.trader_id,
                                            seller.trader_id,
                                            seller.amount,
                                            buyer.price))
                        buyer.amount -= seller.amount
                        seller.amount = 0

    for i in range(len_buyers - 1, -1, -1):
        if s_e.buyers[i].amount == 0:
            del s_e.buyers[s_e.buyers.index(s_e.buyers[i])]

    for i in range(len_sellers - 1, -1, -1):
        if s_e.sellers[i].amount == 0:
            del s_e.sellers[s_e.sellers.index(s_e.sellers[i])]

def buy_sell(stock_exchange: StockExchange, ticker_symbol: str,
             trader_id: str, amount: int, price: int, sell: bool) -> None:
    add_new_stock(stock_exchange, ticker_symbol)
    s_e = stock_exchange[ticker_symbol]
    len_buyers = len(s_e.buyers)
    len_sellers = len(s_e.sellers)

    if len_buyers == 0 and sell is False:
        s_e.buyers.append(Order(trader_id, amount, price))
    elif len_sellers == 0 and sell is True:
        s_e.sellers.append(Order(trader_id, amount, price))

    else:
        if sell is False:
            x = s_e.buyers
            ranged = len_buyers
        else:
            x = s_e.sellers
            ranged = len_sellers

        for index in range(ranged):
            if sell is False and x[index].price >= price:
                x.insert(index, Order(trader_id, amount, price))
                break
            if sell is True and x[index].price <= price:
                x.insert(index, Order(trader_id, amount, price))
                break
            if index == ranged - 1:
                x.append(Order(trader_id, amount, price))

    len_sellers = len(s_e.sellers)
    len_buyers = len(s_e.buyers)
    if (len_sellers > 0 and sell is False) \
            or (len_buyers > 0 and sell is True):
        deal(stock_exchange, ticker_symbol)

def place_buy_order(stock_exchange: StockExchange, ticker_symbol: str,
                    trader_id: str, amount: int, price: int) -> None:
    buy_sell(stock_exchange, ticker_symbol, trader_id, amount, price, False)

def place_sell_order(stock_exchange: StockExchange, ticker_symbol: str,
                     trader_id: str, amount: int, price: int) -> None:
    buy_sell(stock_exchange, ticker_symbol, trader_id, amount, price, True)

def stock_owned(stock_exchange: StockExchange, trader_id: str) \
        -> Dict[str, int]:
    s_e = stock_exchange
    dict_result = {}

    for i in s_e.keys():

        actual_amount = 0
        for a in range(len(s_e[i].history)):

            if trader_id == s_e[i].history[a].seller_id:
                actual_amount -= s_e[i].history[a].amount

            if trader_id == s_e[i].history[a].buyer_id:
                actual_amount += s_e[i].history[a].amount

            if actual_amount != 0:
                dict_result[i] = actual_amount

    return dict_result

def all_traders(stock_exchange: StockExchange) -> Set[str]:
    s_e = stock_exchange
    result = []

    for i in s_e.keys():

        for a in range(len(s_e[i].history)):
            result.append(s_e[i].history[a].seller_id)
            result.append(s_e[i].history[a].buyer_id)

        for b in range(len(s_e[i].sellers)):
            result.append(s_e[i].sellers[b].trader_id)

        for c in range(len(s_e[i].buyers)):
            result.append(s_e[i].buyers[c].trader_id)

    return set(result)

def transactions_by_amount(stock_exchange: StockExchange,
                           ticker_symbol: str) -> List[Transaction]:
    s_e = stock_exchange[ticker_symbol]
    result_tr: List[Transaction] = list(s_e.history)
    result_tr.sort(key=lambda x: x.amount, reverse=True)
    return result_tr

def process_batch_commands(stock_exchange: StockExchange,
                           commands: List[str]) -> Optional[int]:
    num = 0
    for i in commands:

        check_add = check = False
        if 'ADD ' in i and len(i.split()) == 2:
            new_stock = i.split()
            if len(new_stock) == 2 \
                    and new_stock[0] == 'ADD':
                if add_new_stock(stock_exchange, new_stock[1]) is False:
                    return num
                num += 1
                check_add = True

        list_of_i = i.split(':', maxsplit=1)
        if len(list_of_i) == 2 \
                and len(list_of_i[0]) > 0 \
                and check_add is False:
            action = list_of_i[1].split(' ')
            if len(action) == 6 \
                    and action[0] == '' \
                    and action[2].isdigit() \
                    and action[5].isdigit():
                check = True

            if check is True \
                    and action[1] == 'BUY' \
                    and action[4] == 'AT':
                place_buy_order(stock_exchange, action[3], list_of_i[0],
                                int(action[2]), int(action[5]))

            elif check is True \
                    and action[1] == 'SELL' \
                    and action[4] == 'AT':
                place_sell_order(stock_exchange, action[3], list_of_i[0],
                                 int(action[2]), int(action[5]))

            else:
                return num
            num += 1
        if check == check_add is False:
            return num
    return None

def print_stock(stock_exchange: StockExchange, ticker_symbol: str) -> None:
    assert ticker_symbol in stock_exchange

    stock = stock_exchange[ticker_symbol]
    print(f"=== {ticker_symbol} ===")
    print("     price amount  trader")
    print("  -------------------------------------------------------------")

    for order in stock.sellers:
        print(f"    {order.price:6d} {order.amount:6d} ({order.trader_id})")
    print("  -------------------------------------------------------------")

    for order in reversed(stock.buyers):
        print(f"    {order.price:6d} {order.amount:6d} ({order.trader_id})")
    print("  -------------------------------------------------------------")

    for transaction in stock.history:
        print(f"    {transaction.seller_id} -> {transaction.buyer_id}: "
              f"{transaction.amount} at {transaction.price}")

def check_order(order: Order, trader_id: str, amount: int, price: int) -> None:
    assert order.trader_id == trader_id
    assert order.amount == amount
    assert order.price == price

def check_transaction(transaction: Transaction, buyer_id: str, seller_id: str,
                      amount: int, price: int) -> None:
    assert transaction.buyer_id == buyer_id
    assert transaction.seller_id == seller_id
    assert transaction.amount == amount
    assert transaction.price == price

def test_scenario1() -> None:
    duckburg_se: StockExchange = {}
    add_new_stock(duckburg_se, 'ACME')

    place_sell_order(duckburg_se, 'ACME', 'Strýček Skrblík', 50, 120)
    place_buy_order(duckburg_se, 'ACME', 'Rampa McKvák', 100, 90)
    place_sell_order(duckburg_se, 'ACME', 'Hamoun Držgrešle', 70, 110)
    place_sell_order(duckburg_se, 'ACME', 'Kačer Donald', 20, 120)

    acme = duckburg_se['ACME']
    assert acme.history == []

    assert len(acme.buyers) == 1
    check_order(acme.buyers[0], 'Rampa McKvák', 100, 90)

    assert len(acme.sellers) == 3
    check_order(acme.sellers[0], 'Kačer Donald', 20, 120)
    check_order(acme.sellers[1], 'Strýček Skrblík', 50, 120)
    check_order(acme.sellers[2], 'Hamoun Držgrešle', 70, 110)

    place_buy_order(duckburg_se, 'ACME', 'Paní Čvachtová', 90, 110)

    assert len(acme.history) == 1
    check_transaction(acme.history[0], 'Paní Čvachtová', 'Hamoun Držgrešle',
                      70, 110)

    assert len(acme.buyers) == 2
    check_order(acme.buyers[0], 'Rampa McKvák', 100, 90)
    check_order(acme.buyers[1], 'Paní Čvachtová', 20, 110)

    assert len(acme.sellers) == 2
    check_order(acme.sellers[0], 'Kačer Donald', 20, 120)
    check_order(acme.sellers[1], 'Strýček Skrblík', 50, 120)

    place_buy_order(duckburg_se, 'ACME', 'Magika von Čáry', 60, 130)

    assert len(acme.history) == 3
    check_transaction(acme.history[0], 'Paní Čvachtová', 'Hamoun Držgrešle',
                      70, 110)
    check_transaction(acme.history[1], 'Magika von Čáry', 'Strýček Skrblík',
                      50, 130)
    check_transaction(acme.history[2], 'Magika von Čáry', 'Kačer Donald',
                      10, 130)

    assert len(acme.buyers) == 2
    check_order(acme.buyers[0], 'Rampa McKvák', 100, 90)
    check_order(acme.buyers[1], 'Paní Čvachtová', 20, 110)

    assert len(acme.sellers) == 1
    check_order(acme.sellers[0], 'Kačer Donald', 10, 120)

    for name, amount in [
        ('Kačer Donald', -10),
        ('Strýček Skrblík', -50),
        ('Hamoun Držgrešle', -70),
        ('Paní Čvachtová', 70),
        ('Magika von Čáry', 60),
    ]:
        assert stock_owned(duckburg_se, name) == {'ACME': amount}

    assert stock_owned(duckburg_se, 'Rampa McKvák') == {}
    assert stock_owned(duckburg_se, 'Šikula') == {}

    assert all_traders(duckburg_se) == {
        'Kačer Donald',
        'Strýček Skrblík',
        'Hamoun Držgrešle',
        'Paní Čvachtová',
        'Magika von Čáry',
        'Rampa McKvák',
    }

    all_transactions = transactions_by_amount(duckburg_se, 'ACME')
    check_transaction(all_transactions[0],
                      'Paní Čvachtová', 'Hamoun Držgrešle',
                      70, 110)
    check_transaction(all_transactions[1],
                      'Magika von Čáry', 'Strýček Skrblík',
                      50, 130)
    check_transaction(all_transactions[2],
                      'Magika von Čáry', 'Kačer Donald',
                      10, 130)

def test_scenario2() -> None:
    duckburg_se: StockExchange = {}
    result = process_batch_commands(duckburg_se, [
        "ADD ACME",
        "Uncle Scrooge: SELL 50 ACME AT 120",
        "Launchpad McQuack: BUY 100 ACME AT 90",
        "Flintheart Glomgold: SELL 70 ACME AT 110",
        "Donald Duck: SELL 20 ACME AT 120",
        "Mrs. Beakley: BUY 90 ACME AT 110",
        "Magica De Spell: BUY 60 ACME AT 130",
    ])
    assert result is None
    assert 'ACME' in duckburg_se
    acme = duckburg_se['ACME']

    assert len(acme.history) == 3
    check_transaction(acme.history[0], 'Mrs. Beakley', 'Flintheart Glomgold',
                      70, 110)
    check_transaction(acme.history[1], 'Magica De Spell', 'Uncle Scrooge',
                      50, 130)
    check_transaction(acme.history[2], 'Magica De Spell', 'Donald Duck',
                      10, 130)

    assert len(acme.buyers) == 2
    check_order(acme.buyers[0], 'Launchpad McQuack', 100, 90)
    check_order(acme.buyers[1], 'Mrs. Beakley', 20, 110)

    assert len(acme.sellers) == 1
    check_order(acme.sellers[0], 'Donald Duck', 10, 120)

def test_scenario3() -> None:
    nnyse: StockExchange = {}
    result = process_batch_commands(nnyse, [
        "ADD Momcorp",
        "Mom: SELL 1000 Momcorp AT 5000",
        "Walt: BUY 10 Momcorp AT 5600",
        "Larry: BUY 7 Momcorp AT 5000",
        "Igner: BUY 1 Momcorp AT 4000",
        "ADD PlanetExpress",
        "Mom: BUY 1000 PlanetExpress AT 100",
        "Zoidberg: BUY 1000 PlanetExpress AT 199",
        "Professor Farnsworth: SELL 1020 PlanetExpress AT 100",
        "Bender B. Rodriguez: BUY 20 Momcorp AT 100",
        "Fry: INVALID COMMAND",
        "Leela: BUY 500 PlanetExpress AT 150",
    ])

    assert result == 10

    assert set(nnyse) == {'Momcorp', 'PlanetExpress'}

    momcorp = nnyse['Momcorp']
    pe = nnyse['PlanetExpress']

    assert len(momcorp.history) == 2
    check_transaction(momcorp.history[0], 'Walt', 'Mom', 10, 5600)
    check_transaction(momcorp.history[1], 'Larry', 'Mom', 7, 5000)

    assert len(momcorp.sellers) == 1
    check_order(momcorp.sellers[0], 'Mom', 983, 5000)

    assert len(momcorp.buyers) == 2
    check_order(momcorp.buyers[0], 'Bender B. Rodriguez', 20, 100)
    check_order(momcorp.buyers[1], 'Igner', 1, 4000)

    assert len(pe.history) == 2
    check_transaction(pe.history[0], 'Zoidberg', 'Professor Farnsworth',
                      1000, 199)
    check_transaction(pe.history[1], 'Mom', 'Professor Farnsworth',
                      20, 100)

    assert pe.sellers == []
    assert len(pe.buyers) == 1
    check_order(pe.buyers[0], 'Mom', 980, 100)

if __name__ == '__main__':
    test_scenario1()
    test_scenario2()
    test_scenario3()


# source
# https://itsfoss.com/could-not-get-lock-error/

# author : strongpapazola

import subprocess
from os import system
import sys

def banner():
	print('usage: python3 '+sys.argv[0]+' [--id/--rmfile]')

try:
	if sys.argv[1] == "--id":
		print('Killing Process apt')
		getProcess = subprocess.Popen('ps aux | grep apt', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8').splitlines()
		getProcess2 = []
		for i in getProcess:
			if i:
				getProcess2.append(i)
		result = []
		for i in getProcess2:
			getProcess4 = []
			i = i.split(' ')
			for j in i:
				if j:
					getProcess4.append(j)
			result.append(getProcess4)

		pid = []
		for i in result:
			pid.append(i[1])

		for i in pid:
			system('kill -9 %s &>/dev/null&' % (i,))
	elif sys.argv[1] == "--rmfile":
		print('remove apt file')
		system('sudo rm /var/lib/apt/lists/lock')
		system('sudo rm /var/cache/apt/archives/lock')
		system('sudo rm /var/lib/dpkg/lock')
		system('sudo dpkg --configure -a')
	else:
		banner()
except:
	banner()


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

# Create your views here.

from django.shortcuts import render

# relative import of forms

def create_view(request):
    # dictionary for initial data with
    # field names as keys
    context ={}

    # add the dictionary during initialization
    form = GeeksForm(request.POST or None)
    if form.is_valid():
        form.save()

    context['form']= form
    return render(request, "hello.html", context)


import os
import sys
import subprocess
import argparse
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_market_calendars as mcal

from scipy import signal
from matplotlib import style
from matplotlib.patches import Rectangle
from sklearn.preprocessing import StandardScaler
from alpaca_trade_api.rest import REST, TimeFrame

class CommandLineArgs:
    def __init__(self):
        self.args = self._parse_arguments()

    def _parse_arguments(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("--symbol", "-s", type=str)
        parser.add_argument("--num_days", "-n", type=int, default=8)
        parser.add_argument("--filter_window", "-f", type=int)
        parser.add_argument("--plot_switch", "-p", type=int, default=1)
        parser.add_argument("--std_dev", "-d", type=int, default=0)
        parser.add_argument(
            "--detect_factor", "-df", type=float, default=0.10
        )
        parser.add_argument("--num_samples", "-ns", type=int, default=1000)
        return parser.parse_args()

    @property
    def symbol(self):
        return self.args.symbol

    @property
    def num_days(self):
        return self.args.num_days

    @property
    def filter_window(self):
        return self.args.filter_window

    @property
    def plot_switch(self):
        return self.args.plot_switch

    @property
    def std_dev(self):
        return self.args.std_dev

    @property
    def detect_factor(self):
        return self.args.detect_factor

    @property
    def num_samples(self):
        return self.args.num_samples

class StockAnalyzer:
    def __init__(self, args):
        self._initialize_plotting()
        self._initialize_api_keys()
        self._initialize_parameters(args)
        self._set_company_name()

    def _initialize_plotting(self):
        plt.rcParams["figure.figsize"] = [17.0, 8.0]
        plt.rcParams["figure.dpi"] = 100
        style.use("dark_background")

    def _initialize_api_keys(self):
        self.API_KEY_ID = os.environ["APCA_API_KEY_ID"]
        self.SECRET_KEY_ID = os.environ["APCA_API_SECRET_KEY"]
        self.BASE_URL = os.environ["APCA_API_BASE_URL"]
        self.rest_api = REST(
            self.API_KEY_ID, self.SECRET_KEY_ID, self.BASE_URL
        )

    def _initialize_parameters(self, args):
        self.symbol = args.symbol.upper()
        self.detect_factor = args.detect_factor
        self.num_days = args.num_days
        self.filter_window = args.filter_window
        self.plot_switch = args.plot_switch
        self.std_dev = args.std_dev
        self.num_samples = args.num_samples
        self.bashCommand = f"awk -F'|' '{{if($1==\"{self.symbol}\"){{print $2}}}}' ./tickers.txt"
        self.df_temp = pd.DataFrame(
            index=[self.symbol],
            columns=[
                "num_samples",
                "filter_length",
                "mean_value",
                "std_dev",
                "last_velocity",
                "detect_value",
                "price_now",
                "trend_diff",
                "action",
                "action_price",
                "current_price",
                "isamp_ago",
            ],
        )
        self.nyse = mcal.get_calendar("NYSE")
        self.end_dt = pd.Timestamp.now(tz="America/New_York")
        self.start_dt = self.end_dt - pd.Timedelta(f"{self.num_days} days")
        self._from = self.start_dt.strftime("%Y-%m-%d")
        self.to_end = self.end_dt.strftime("%Y-%m-%d")
        self.nyse_schedule = self.nyse.schedule(
            start_date=self._from, end_date=self.to_end
        )
        self._to_date = str(self.nyse_schedule.index[-1]).split(" ")[0]

    def _set_company_name(self):
        try:
            self.co_name = subprocess.run(
                ["bash", "-c", self.bashCommand],
                capture_output=True,
                text=True,
            ).stdout.strip()
        except Exception:
            self.co_name = "N/A"

    def group_consecutives(self, data, stepsize=1):
        diff = np.diff(data)
        split_indices = np.flatnonzero(diff != stepsize) + 1
        return np.split(data, split_indices)

    def normalize(self, data_close):
        scaler = StandardScaler()
        data_close_normalized = scaler.fit_transform(
            data_close.reshape(-1, 1)
        ).flatten()
        return data_close_normalized

    def remove_trend_method(self, data_close, num_samples):
        gradient = (data_close[-1] - data_close[0]) / (num_samples - 1)
        intercept = data_close[0]
        x = np.arange(num_samples)
        trend = (gradient * x) + intercept
        remove_trend = data_close - trend
        return remove_trend, gradient, intercept

    def apply_tolgi_filter(
        self, remove_trend, gradient, intercept, num_samples, filter_window
    ):
        computed_filter = signal.windows.hann(filter_window)
        filter_result = signal.convolve(
            remove_trend, computed_filter, mode="same"
        ) / sum(computed_filter)
        trend = (gradient * np.arange(num_samples)) + intercept
        data_filter = filter_result + trend
        return data_filter

    def compute_derivatives(self, data_filter, data_close):
        first_derivative = signal.savgol_filter(
            data_filter, delta=1, window_length=3, polyorder=2, deriv=1
        )
        first_derivative_raw = signal.savgol_filter(
            data_close, delta=1, window_length=3, polyorder=2, deriv=1
        )
        return first_derivative, first_derivative_raw

    def compute_avo_attributes(self, data):
        num_samples = len(data)
        hundred = 100.0
        first_price = data[0]
        norm_data = (data - first_price) / first_price * hundred
        z_array = np.arange(1, num_samples + 1)
        trans_matrix = np.column_stack((z_array, np.ones(num_samples)))
        norm_gradient, norm_intercept = np.linalg.lstsq(
            trans_matrix, norm_data, rcond=None
        )[0]
        gradient, intercept = np.linalg.lstsq(trans_matrix, data, rcond=None)[
            0
        ]
        return gradient, intercept, norm_gradient

    def find_extrema(self, data, height_threshold=5):
        padded = np.pad(data, (0, 1), "constant")
        mean = np.mean(data)
        std_dev = np.std(data)
        peaks, _ = signal.find_peaks(
            padded, height=height_threshold * std_dev
        )
        troughs, _ = signal.find_peaks(
            -padded, height=height_threshold * std_dev
        )
        return padded, peaks, troughs

    def analyze_stock(self):
        if self.plot_switch == 1:
            print("Parameter values:")
            print(" ")
            print("Symbol = ", self.symbol)
            print("Velocity detect factor = ", self.detect_factor)
            print(
                "Number of days to collect historical prices = ",
                self.num_days,
            )

        try:
            bars = self.rest_api.get_bars(
                self.symbol,
                TimeFrame.Minute,
                self._from,
                self._to_date,
                adjustment="split",
            ).df[["close"]]
            self.data_close = bars.close.values[-self.num_samples :]
            self.current_price = self.rest_api.get_latest_trade(
                self.symbol
            ).price
            if self.current_price != self.data_close[-1]:
                self.data_close[-1] = float(self.current_price)
            self.num_samples = len(self.data_close)
        except:
            print(
                "Historical data not found, is the symbol correct? --> exiting"
            )
            sys.exit(0)

        if self.filter_window is None or self.filter_window == 0:
            self.filter_window = self.num_samples // 10
            if (self.filter_window % 2) == 0:
                self.filter_window += 1

        print("Filter window = ", self.filter_window)
        print(" ")

        self.remove_trend = self.data_filter = np.zeros(self.num_samples)

        self.data_close[-1] = self.price_now = self.current_price
        self.data_orig = self.data_close

        self.data_close = (
            self.normalize(self.data_close)
            if self.std_dev == 1
            else self.data_close
        )

        (
            self.remove_trend,
            self.gradient,
            self.intercept,
        ) = self.remove_trend_method(self.data_close, self.num_samples)

        self.data_filter = self.apply_tolgi_filter(
            self.remove_trend,
            self.gradient,
            self.intercept,
            self.num_samples,
            self.filter_window,
        )

        mean_value = np.mean(self.data_close)
        std_dev = np.std(self.data_close)

        (
            self.first_derivative,
            self.first_derivative_raw,
        ) = self.compute_derivatives(self.data_filter, self.data_close)

        self.padded, self.peaks, self.troughs = self.find_extrema(
            self.first_derivative_raw
        )

        mean = np.mean(self.first_derivative)
        std = np.std(self.first_derivative)
        a = (self.first_derivative - mean) / std
        velocity = self.first_derivative
        self.first_derivative = a / np.max(a)

        (self.min_,) = np.where(self.first_derivative < -self.detect_factor)
        (self.max_,) = np.where(self.first_derivative > self.detect_factor)

        gap_min = self.group_consecutives(self.min_)
        gap_max = self.group_consecutives(self.max_)

        gap_array = [
            ind
            for group in gap_min
            for ind in [group[0], group[-1]]
            if group.size > 0
        ]
        gap_array.extend(
            [
                ind
                for group in gap_max
                for ind in [group[0], group[-1]]
                if group.size > 0
            ]
        )

        gap_array = np.append(gap_array, 1)
        gap_array = np.append(gap_array, self.num_samples)

        gap_array = np.unique(np.sort(gap_array).astype(int))

        diff_array = np.diff(np.sign(self.first_derivative))

        (mina_,) = np.where(diff_array < 0)
        (maxa_,) = np.where(diff_array > 0)

        final_min = []
        final_max = []
        last = "None"

        for val1, val2 in zip(gap_array[:-1], gap_array[1:]):
            index_min = max(
                mina_[(val1 <= mina_) & (mina_ <= val2)], default=0
            )
            index_max = max(
                maxa_[(val1 <= maxa_) & (maxa_ <= val2)], default=0
            )

            if index_min > 0 or index_max > 0:
                if index_min > index_max:
                    if last != "min":
                        final_min.append(index_min)
                        last = "min"
                else:
                    if last != "max":
                        final_max.append(index_max)
                        last = "max"

        self.final_min = np.array(final_min, dtype=int)
        self.final_max = np.array(final_max, dtype=int)

        isamp = 0
        action_price = 0.0
        if self.final_min[-1] > self.final_max[-1]:
            isamp = int(self.final_min[-1])
            self.action = "Sell"
        elif self.final_min[-1] < self.final_max[-1]:
            isamp = int(self.final_max[-1])
            self.action = "Buy"
        else:
            isamp = int(len(self.data_close))
            self.action = "None"

        self.isamp_ago = self.num_samples - isamp
        self.action_price = self.data_close[isamp]
        if self.std_dev == 1:
            self.price_now = self.data_orig[-1]

        gradient, intercept, norm_gradient = self.compute_avo_attributes(
            self.data_close
        )

        trend_diff = (
            (self.data_close[isamp] - (gradient * isamp + intercept))
            / self.data_close[isamp]
        ) * 100

        mean_value = np.mean(self.data_orig)
        std_dev = np.std(self.data_orig)

        detect_value = (self.data_orig[-1] - mean_value) / abs(std_dev)
        if self.std_dev == 1:
            self.current_price = detect_value

        self.df_temp.loc[self.symbol] = [
            self.num_samples,
            self.filter_window,
            mean_value,
            std_dev,
            velocity[-1],
            detect_value,
            self.price_now,
            trend_diff,
            self.action,
            self.action_price,
            self.current_price,
            self.isamp_ago,
        ]

    def plot_results(self):
        if self.plot_switch == 1:
            fig, ax = plt.subplots()

            ax.plot(
                self.data_close,
                linewidth=1,
                alpha=0.8,
                linestyle="solid",
                color="turquoise",
                label="Close Prices",
            )
            ax.plot(
                self.data_filter,
                linewidth=6,
                alpha=0.6,
                color="yellow",
                label="Filtered Data",
            )
            ax.plot(
                self.padded + np.min(self.data_close),
                linewidth=1,
                alpha=0.6,
                color="white",
                label="Padded Data",
            )
            ax.plot(
                self.data_filter,
                linewidth=3,
                alpha=0.6,
                linestyle="dashed",
                color="black",
                label="Filtered Data (dashed)",
            )
            ax.scatter(
                self.min_,
                self.data_filter[self.min_],
                c="r",
                s=100,
                label="Local Minima",
            )
            ax.scatter(
                self.max_,
                self.data_filter[self.max_],
                c="g",
                s=100,
                label="Local Maxima",
            )

            if len(self.final_min) > 0:
                ax.scatter(
                    self.final_min,
                    self.data_close[self.final_min],
                    c="r",
                    s=400,
                    edgecolor="white",
                    label="Final Minima",
                )

            if len(self.final_max) > 0:
                ax.scatter(
                    self.final_max,
                    self.data_close[self.final_max],
                    c="g",
                    s=400,
                    edgecolor="white",
                    label="Final Maxima",
                )

            plot_peaks = ax.plot(
                self.peaks,
                self.padded[self.peaks] + np.min(self.data_close),
                "x",
                color="green",
                label="Peaks",
            )
            plot_troughs = ax.plot(
                self.troughs,
                self.padded[self.troughs] + np.min(self.data_close),
                "x",
                color="red",
                label="Troughs",
            )

            plot_vlines = ax.vlines(
                self.peaks,
                np.min(self.data_close),
                np.max(self.data_close),
                linestyle="--",
                linewidth=0.75,
                alpha=0.8,
                color="green",
            )
            plot_vlines = ax.vlines(
                self.troughs,
                np.min(self.data_close),
                np.max(self.data_close),
                linestyle="--",
                linewidth=0.75,
                alpha=0.8,
                color="red",
            )
            ax.fill_between(
                range(self.num_samples),
                np.min(self.data_close) + np.min(self.padded),
                np.max(self.padded) + np.min(self.data_close),
                facecolor="darkblue",
                alpha=0.25,
            )
            ax.grid(linestyle="--", linewidth=0.4)
            ax.set_xlabel("Samples (Trading Minutes)")
            ax.set_ylabel("Price ($)")
            ax.set_title(
                f"Close prices for symbol = {self.symbol} / {self.co_name}\n filter length = {self.filter_window} current price = {self.current_price} action price = {self.action_price} action = {self.action} samples ago = {self.isamp_ago}"
            )
            plt.tight_layout()
            plt.show()
        else:
            print(self.df_temp)

def main():
    args = CommandLineArgs()
    if args.symbol is not None:
        if args.symbol.upper() == "HELP":
            print(
                "Program useage:  bars symbol num_days filter_window plot_switch std_dev num_samples"
            )
            sys.exit(0)

    analyzer = StockAnalyzer(args)

    try:
        analyzer.analyze_stock()
        analyzer.plot_results()
    except Exception as e:
        print("Exception occurred: ", e)
        print(f"Symbol: {analyzer.symbol} caused an error")
        analyzer.df_temp.drop([analyzer.symbol], inplace=True)
        print(analyzer.df_temp)

if __name__ == "__main__":
    main()


#!/usr/bin/env python

## 2021/06/21. Levi King. From the blog,
## https://naturallanguagepuzzling.blogspot.com
## This code was written to assist with the 2021/05/30 Sunday Puzzle:
## https://www.npr.org/2021/06/20/1008352915/sunday-puzzle-hidden-cities
## That puzzle:
"""
This week's challenge comes from listener Iva Allen in Canada. Name a make of
car. Write it in all capital letters. Rotate one of the letters 90 degrees and
another letter 180 degrees to make a woman's name. What is it?
"""

import itertools
import string
from slugify import slugify

## mapping upright capital letters to 90degree rotated letters
upper_90d = {
    'C': ['U'],
    'E': ['M', 'W'],
    'H': ['I'],
    'I': ['H'],
    'M': ['E'],
    'N': ['Z'],
    'U': ['C'],
    'W': ['E'],
    'Z': ['N']
    }

## mapping upright capital letters to 180degree rotated letters
upper_180d = {
    'A': 'V',
    'M': 'W',
    'U': 'A',
    'V': 'A',
    'W': 'M',
    }

## You can use the text file lists of cars makers and girls names I've prepared;
## download them from GitHub and store it as I have:
car_makers_file = 'resources/car-makers.txt'
girls_names_file = 'resources/girls-names.txt'

## clean up each name to suit this puzzle: remove any whitespace,
## convert to upppercase, slugify (convert to ASCII), remove any non-letters
def clean_name(somename):
    s = somename.strip()
    s = slugify(s)
    s = "".join([l.upper() for l in s if l.isalpha()])
    return s

## takes in agenda (see get_car_variants); for each pair of indices in the
## agenda, does the rotations to produce variant(s)
def get_variants_by_agenda(car, agenda):
    car = "".join(car)
    variants = []
    for ag in agenda:
        agvars = []
        i90 = ag[0]
        i180 = ag[1]
        v90s = upper_90d[car[i90]]
        v180 = upper_180d[car[i180]]
        for v90 in v90s:
            carlist = list(car)
            carlist[i90] = v90
            carlist[i180] = v180
            agvars.append("".join(carlist))
        variants += agvars
    return variants

## takes a car maker (string), determines which positions in string can be
## rotated 90 or 180 degrees; produces a list (agenda) of pairs of positions
## to rotate in order to produce all variant strings; each pair is:
## (i90, i80), where i90 is index of letter to be rotated 90 degrees; i90 != i80
def get_car_variants(car, keys90, keys180):
    cvs = []
    car = list(car)
    i90s = []  ## index in car string for letters rotatable 90 degrees
    i180s = []  ## index in car string for letters rotatable 180 degrees
    for lx in car:
        if lx in keys90:
            i90s.append(car.index(lx))
        if lx in keys180:
            i180s.append(car.index(lx))
    if 0 not in [len(i90s), len(i180s)]:
        agenda = [ii for ii in itertools.product(i90s, i180s) if ii[0] != ii[1]]
        agenda = list(set(agenda))
        cvs += get_variants_by_agenda(car,agenda)
    else:
        pass
    return cvs

## iterate through car makers, expand each car maker to variants using rotated
## letters; compare variants against list of girl names; print solution
def cars_vs_girls(cars, girls, keys90, keys180):
    for car in cars:
        car_variants = get_car_variants(car, keys90, keys180)
        print(car)
        print(car_variants)
        for cv in car_variants:
            for g in girls:
                if cv.strip() == g.strip():
                    print(car, g)

## prepare lists of 90degree rotatable and 180degree rotatable letters
def solve_puzzle(cars, girls):
    keys90 = list(upper_90d.keys())
    keys180 = list(upper_180d.keys())
    cars_vs_girls(cars, girls, keys90, keys180)

def main():
    girl_file = open(girls_names_file, "r")
    girl_list = girl_file.readlines()
    girl_file.close()
    girls = [clean_name(g) for g in girl_list]
    girls.sort()
    car_file = open(car_makers_file, "r")
    car_list = car_file.readlines()
    car_file.close()
    cars = [clean_name(c) for c in car_list]
    cars.sort()
    solve_puzzle(cars, girls)

if __name__ == "__main__":
    main()

"""
Solution:
MAZDA --> WANDA
"""


import base64
import os
from io import BytesIO
import time
import datetime

import streamlit as st
import pandas as pd
import openpyxl

from .postxml_ema02 import prettyxmlfile, postxml_ema

def xmldownload(xmlfile):
    buffered = BytesIO()
    file = open(xmlfile,'rb').read()
    buffered.write(file)
    b64 = base64.b64encode(buffered.getvalue()).decode()
    href = f'Download XML file'
    return href

def add_block(checklist,label,inexcel,showlevel=4):
    #checklist.append('NONE')
    st.write(f'## Section of {label}')
    dfindex = len(checklist)-1
    temp = st.selectbox(label,checklist,index=dfindex)
    groups = 'test'
    if temp != 'NONE':
        df = pd.read_excel(inexcel,temp,dtype='object')
        if showlevel==4:
            groups = st.multiselect("Groups",df.columns,key='g_'+temp)
            total = st.multiselect('Total',df.columns,key='t_'+temp)
            sets = st.multiselect('Analysis Sets',df.columns,key='s_'+temp)
        elif showlevel == 3:
            groups = st.multiselect("Groups",df.columns,key='g_'+temp)
            sets = st.multiselect('Analysis Sets',df.columns,key='s_'+temp)
        elif showlevel == 2:
            groups = st.multiselect("Groups",df.columns,key='g_'+temp)
        else:
            pass
        st.write('The data is:')
        st.dataframe(df)
        if showlevel==4:
            return df, groups,total, sets
        elif showlevel==3:
            return df, groups,sets
        elif showlevel==2:
            return df, groups
        else:
            return df
    else:
        return None

def app():
    inxml = st.file_uploader('Upload XML file',key='Upload_xml')
    incountry = st.file_uploader('Upload Eucat contry excel file',key='Upload_country',type='XLSX')
    if incountry:
        country_sheets = openpyxl.load_workbook(incountry).sheetnames
        country=st.selectbox("Country:",country_sheets)
    else:
        incountry='euctid_country.xlsx'
        country='country'

    country_sheets = openpyxl.load_workbook(incountry).sheetnames
    country=st.selectbox("Country:",country_sheets)
    eucat_id=pd.read_excel(incountry,country)
    st.write('The Country EuCAT ID is:')
    st.dataframe(eucat_id)

    inexcel = st.file_uploader('Upload Excel created base on TLF meta',key='Upload_excel',type='XLSX')
    if inexcel:
        insheets = openpyxl.load_workbook(inexcel).sheetnames
        if isinstance(insheets,str):
            insheets = [insheets]
        if 'NONE' not in insheets:
            insheets=insheets+['NONE']
        country_input = add_block(insheets,'Country',inexcel,1)
        age_input = add_block(insheets,'Age',inexcel,2)
        disp_input = add_block(insheets,'Disposition',inexcel,2)
        age_n_input = add_block(insheets,'Age continue',inexcel,3)
        age_c_input = add_block(insheets,'Age category',inexcel)
        gender_input = add_block(insheets,'Gender',inexcel)
        demo_c_input = add_block(insheets,'Demography category',inexcel)
        demo_n_input = add_block(insheets,'Demography continue',inexcel,3)
        endpoints_input = add_block(insheets,'Endpoints',inexcel,3)
        stats_input = add_block(insheets,'Stat. analysis',inexcel,1)
        if st.button('Update XML'):
            test = postxml_ema(inxml)
            if len(country_input)>0:
                test.add_country_count(country_input,incountry,country)
            if age_input:
                test.add_agegroup(age_input[0],age_input[1][0])
            if disp_input:
                test.add_patientflow(disp_input[0],disp_input[1])
            if gender_input:
                test.add_gender_cat_characters(gender_input[0],gender_input[1],gender_input[2],
                gender_input[3])
            if age_n_input:
                test.add_age_cont_characters(age_n_input[0],age_n_input[1],age_n_input[2])
            if age_c_input:
                test.add_age_cat_characters(age_c_input[0],age_c_input[1],age_c_input[2],
                age_c_input[3])
            if demo_c_input:
                test.add_baseline_cat_characters(demo_c_input[0],demo_c_input[1],demo_c_input[2],
                demo_c_input[3])
            if demo_n_input:
                test.add_baseline_cont_characters(demo_n_input[0],demo_n_input[1],demo_n_input[2])
            if endpoints_input:
                test.add_endpoints(endpoints_input[0],endpoints_input[1],endpoints_input[2],
                stats_input)
            filename = 'sp'+datetime.datetime.now().strftime("%Y%m%d%h%m%c").replace(':','').replace(' ','')+'.xml'
            test.tofile(filename,pretty=False)
            prettyxmlfile(filename)
            st.markdown(xmldownload(filename),unsafe_allow_html=True)
            os.remove(filename)


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

import sys
import os
import csv
import math
import numpy
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot
import joblib

def calculate_n0(r_e):
	size = int(math.ceil(r_e))
	n0 = 0.0
	for y in range(-size, size+1):
		for x in range(-size, size+1):
			r = math.sqrt(x*x + y*y)
			if((0 < r) and (r < r_e)):
				w = r_e/r - 1
				n0 += w
	return n0

def output(dirname, filename, r_e, beta, R, p_theoretical, dt):
	with open(dirname + "/" + filename, "r") as f:
		data = [[math.sqrt(float(line["x"])**2 + float(line["z"])**2), float(line["p"]), float(line["n"])] for line in csv.DictReader(f, skipinitialspace="True")]

	data = numpy.array(data)

	t = dt * int(filename[10:15])
	pyplot.xlim([0, 0.06])
	pyplot.ylim([0, 600])
	pyplot.xlabel("r [m]")
	pyplot.ylabel("p [Pa]")
	pyplot.title("%4.2f [s]" % t)
	pyplot.grid(which="minor", color="lightgray", linestyle="-")
	pyplot.grid(which="major", color="black", linestyle="-", b=True)
	pyplot.minorticks_on()
	pyplot.plot(data.T[0], data.T[1], ".", markersize=1)
	pyplot.plot([0, R], [p_theoretical, 0], "-")
	pyplot.savefig("png/" + filename + ".png")
	pyplot.clf()

	n0 = calculate_n0(r_e)
	n_surface = n0 * beta
	surface = [d[0] for d in data if d[2] < n_surface]
	r_surface_max = max(surface)
	r_surface_min = min(surface)
	roundness = 1.0-(r_surface_max - r_surface_min)/R

	i = data.T[0].argmin() # 中心に一番近いやつ
	p_center = data[i][1]

	return [roundness*100, p_center]

def main(dirname, r_e, beta, dt, L):
	R = L/math.sqrt(math.pi)
	p_theoretical = 1000*9.8*R

	data = joblib.Parallel(n_jobs=-1, verbose=1)([joblib.delayed(output)(dirname, filename, r_e, beta, R, p_theoretical, dt) for filename in sorted(os.listdir(dirname))])
#	data = [output(dirname, filename, r_e, beta, R, p_theoretical, dt) for filename in sorted(os.listdir(dirname))]

	data = numpy.array(data).T
	n = data.shape[1]
	maxT = n * dt
	t = numpy.array(range(n)) * dt

	pyplot.plot(t, data[0])
	pyplot.xlim([0, maxT])
	pyplot.ylim([0, 100])
	pyplot.xlabel("t [s]")
	pyplot.ylabel("Roundness [%]")
	pyplot.grid(which="minor", color="lightgray", linestyle="-")
	pyplot.grid(which="major", color="black", linestyle="-", b=True)
	pyplot.minorticks_on()
	pyplot.savefig("roundness.svg")
	pyplot.clf()

	pyplot.plot(t, data[1])
	pyplot.plot([0, maxT], [p_theoretical, p_theoretical])
	pyplot.xlim([0, maxT])
	pyplot.ylim([0, 600])
	pyplot.xlabel("t [s]")
	pyplot.ylabel("p [Pa]")
	pyplot.grid(which="minor", color="lightgray", linestyle="-")
	pyplot.grid(which="major", color="black", linestyle="-", b=True)
	pyplot.minorticks_on()
	pyplot.savefig("pressure.svg")
	pyplot.clf()

if __name__ == "__main__":
	main(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]), float(sys.argv[5]))


import os

from athletelist import AthleteList
from athletemodel import *

def get_data(file_name):
    try:
        with open (file_name) as f_open:
            data = f_open.readline()

        data = data.strip().split(',')

        #new_data = []
        #for each_t in data:
            # new_data.append(sanitize(each_t))

        new_data = sorted( set( [sanitize(each_t) for each_t in data]) )

        return new_data

    except IOError as ioerr:
        print("IO Error: ", str(ioerr)  )

        return None

r_james = get_coach_data("james2.txt")
print (r_james.name, r_james.dob, r_james.get_top3())

the_files =["james2.txt", "mikey2.txt", "julie2.txt", "sarah2.txt"]
data = put_to_store(the_files)

for each_ath in data:
    print(data[each_ath].name   + " " + data[each_ath].dob)

#print (julie)
#print (sarah)
#print (mikey)


##
##

import logging
import googleapiclient.errors
from couchformation.gcp.driver.base import CloudBase, GCPDriverError

logger = logging.getLogger('couchformation.gcp.driver.instance')
logger.addHandler(logging.NullHandler())
logging.getLogger("googleapiclient").setLevel(logging.ERROR)

class Instance(CloudBase):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.logger = logging.getLogger(self.__class__.__name__)

    def run(self,
            name: str,
            image_project: str,
            image_name: str,
            sa_email,
            zone: str,
            vpc: str,
            subnet: str,
            username: str,
            ssh_key: str,
            swap_disk,
            data_disk,
            root_size="256",
            disk_type: str = "pd-ssd",
            machine_type="n2-standard-2"):
        operation = {}
        instance_body = {
            "name": name,
            "zone": zone,
            "networkInterfaces": [
                {
                    "network": f"projects/{self.gcp_project}/global/networks/{vpc}",
                    "subnetwork": f"regions/{self.gcp_region}/subnetworks/{subnet}",
                    "accessConfigs": [
                        {
                            "name": "external-nat",
                            "type": "ONE_TO_ONE_NAT",
                            "networkTier": "PREMIUM"
                        }
                    ]
                }
            ],
            "serviceAccounts": [
                {
                    "email": sa_email,
                    "scopes": [
                        "https://www.googleapis.com/auth/cloud-platform"
                    ]
                }
            ],
            "metadata": {
                "items": [
                    {
                        "key": "ssh-keys",
                        "value": f"{username}:{ssh_key}"
                    }
                ]
            },
            "disks": [
                {
                    "boot": True,
                    "initializeParams": {
                        "sourceImage": f"projects/{image_project}/global/images/{image_name}",
                        "diskType": f"zones/{zone}/diskTypes/{disk_type}",
                        "diskSizeGb": str(round(float(root_size)))
                    },
                    "autoDelete": True
                },
                {
                    "source": f"zones/{zone}/disks/{swap_disk}"
                },
                {
                    "source": f"zones/{zone}/disks/{data_disk}"
                }
            ],
            "machineType": f"zones/{zone}/machineTypes/{machine_type}"
        }

        try:
            request = self.gcp_client.instances().insert(project=self.gcp_project, zone=zone, body=instance_body)
            operation = request.execute()
            self.wait_for_zone_operation(operation['name'], zone)
        except googleapiclient.errors.HttpError as err:
            error_details = err.error_details[0].get('reason')
            if error_details != "alreadyExists":
                raise GCPDriverError(f"can not create instance: {err}")
        except Exception as err:
            raise GCPDriverError(f"error creating instance: {err}")

        return operation.get('targetLink')

    def details(self, instance: str, zone: str) -> dict:
        try:
            request = self.gcp_client.instances().get(project=self.gcp_project, zone=zone, instance=instance)
            response = request.execute()
            return response
        except Exception as err:
            raise GCPDriverError(f"error getting instance details: {err}")

    def terminate(self, instance: str, zone: str) -> None:
        try:
            request = self.gcp_client.instances().delete(project=self.gcp_project, zone=zone, instance=instance)
            operation = request.execute()
            self.wait_for_zone_operation(operation['name'], zone)
        except googleapiclient.errors.HttpError as err:
            error_details = err.error_details[0].get('reason')
            if error_details != "notFound":
                raise GCPDriverError(f"can not terminate instance: {err}")
        except Exception as err:
            raise GCPDriverError(f"error terminating instance: {err}")


from itune_radio import *
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error
import requests
from PIL import Image
from StringIO import StringIO
import os

def get_song_from_file(name_of_song):
  audio = MP3(name_of_song, ID3=ID3)
  return audio

def add_album_to_ID3(song,image_name,image_format):
  # add ID3 tag if it doesn't exist
  try:
      song.add_tags()
  except error:
      pass

  song.tags.add(
      APIC(
          encoding=3, # 3 is for utf-8
          mime='image/'+image_format, # image/jpeg or image/png
          type=3, # 3 is for the cover image
          desc=u'Cover',
          data=open(os.getcwd() + '/' + image_name).read()
      )
  )
  song.save()

def get_album_art_from_web(key):

  #getting track,artist and album using itune_radio.py
  track,artist = get_song_and_artist()
  album = get_album()

  #generating the api url for lastfm
  url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=%s&artist=%s&album=%s&format=json" %(key,artist,album)

  #getting the relevant album art url from lastfm
  r = requests.get(url)
  data = r.json()
  content = data['album']
  all_img = content['image']
  extralarge = all_img[3]
  img_url = extralarge['#text'].encode('ascii','ignore')

  #returning the obtained image
  i = requests.get(img_url)
  img = Image.open(StringIO(i.content))
  img_format = img.format.lower()

  img_name = album + "." + img_format
  img.save(img_name)

  return img_name, img_format

def delete_art_after_link(image_name):
  album_art_path = os.getcwd() + '/' + image_name
  os.remove(album_art_path)


import numpy as np
from tmtools import tm_align
from openfold.np import protein, residue_constants
import os
import argparse
import logging
from tqdm import tqdm
from statistics import mean

def tm_score_from_pdbs(fp1, fp2):
    coords1, seq1 = coord_seq_from_file(fp1)
    coords2, seq2 = coord_seq_from_file(fp2)
    res = tm_align(coords1, coords2, seq1, seq2)
    return res.tm_norm_chain1

def coord_seq_from_file(path):
    with open(path, 'r') as f:
        pdb_str = f.read()
    protein_object = protein.from_pdb_string(pdb_str)
    ca_coords = protein_object.atom_positions[:, 1, :]
    seq = _aatype_to_str_sequence(protein_object.aatype)
    return ca_coords, seq

def _aatype_to_str_sequence(aatype):
    return ''.join([
        residue_constants.restypes_with_x[aatype[i]]
        for i in range(len(aatype))
    ])

def gather_job(gt_dir, predict_dir):
    pdb_paths = []
    for f_path in os.listdir(gt_dir):
        if f_path.endswith('.pdb'):
            pdb_path_to_verify = os.path.join(predict_dir, f_path)
            if os.path.exists(pdb_path_to_verify):
                pdb_paths.append(f_path)
    return pdb_paths

def TMalign_dir(path1, path2):
    jobs = gather_job(path1, path2)
    logging.info(f'got {len(jobs)} jobs...')

    tm_list = []
    tm_dict = {}
    for job in tqdm(jobs):
        fp1 = os.path.join(path1, job)
        fp2 = os.path.join(path2, job)
        name = job.split(".")[0]
        tm = tm_score_from_pdbs(fp1, fp2)
        tm_list.append(tm)
        tm_dict[name] = tm
    return tm_list, tm_dict, mean(tm_list), len(jobs)

def main(args):
    tm_list, tm_dict, mean, num_jobs = TMalign_dir(args.gt_dir, args.predict_dir)
    with open(args.log_dir, 'w') as f:
        f.write(str(num_jobs) + " jobs: ")
        f.write(str(mean))
    # print(mean)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--gt_dir", type=str, default=None,
        help="Path to model parameters."
    )
    parser.add_argument(
        "--predict_dir", type=str, default=os.getcwd(),
        help="Name of the directory in which to output the prediction",
    )
    parser.add_argument(
        "--log_dir", type=str, default=os.getcwd(),
        help="Name of the directory in which to output the prediction",
    )
    args = parser.parse_args()
    main(args)


# Сравнение чисел, если первое больше второго вывод - 2, если второе больше - 1,равны - 0
def COM_NN_D(A, B):
    i = 0
    if len(A) > len(B):
        return 2
    elif len(A) < len(B):
        return 1
    else:
        while 1:
            if A[i] > B[i]:
                return 2
            elif A[i] < B[i]:
                return 1
            elif (A[i] == B[i]) & (i == (len(A) - 1)):
                return 0
            i += 1

# Проверка на ноль, если ноль - нет, если не ноль - да
def NZER_N_B(A):
    if A[0] != 0:
        return 'Да'
    else:
        return 'Нет'

# N-3
# Добавление 1 к натуральному числу:
# Если младший разряд = 9, то заменяем на 0
# Есди все цифры = 9, то создаем лист размером len(A) + 1 с 1 в начале
# Если не 9, то +1 в разряд
def ADD_1N_N(A):
    k = len(A) - 1
    while A[k] == 9:
        if k == 0:
            L = [0] * (len(A) + 1)
            L[0] = 1
            return L
        else:
            A[k] = 0
            k -= 1
    A[k] += 1
    return A

# N-4
# Сложение натуральных чисел:
# Добавляем в начало разряд, дополняя меньшее чсило разрядами до большего
# Если сумма цирф в разряде > 10 то переносим 1 в старший
# Если len(ответ) == len(большего числа), то удаляем 0
def ADD_NN_N(A, B):
    if COM_NN_D(A, B) == 2:
        C = B
        B = A
        A = C
    k = len(B) - len(A)
    A.reverse()
    for i in range(0, k + 1, 1):
        A.append(0)
    A.reverse()
    B.reverse()
    B.append(0)
    B.reverse()
    for i in range(len(B) - 1, 0, -1):
        if B[i] + A[i] >= 10:
            B[i] = (B[i] + A[i]) % 10
            B[i - 1] += 1
        else:
            B[i] = B[i] + A[i]
    if B[0] == 0:
        B.remove(0)
    return B

# N - 5 Вычитание натуральных чисел
def SUB_NN_N(A, B):
    if COM_NN_D(A, B) == 1:
        temp = A
        A = B
        B = temp
    k = len(A) - len(B)
    B.reverse()
    for i in range(0, k, 1):
        B.append(0)
    B.reverse()
    for i in range(len(B) - 1, -1, -1):
        if A[i] >= B[i]:
            A[i] = A[i] - B[i]
        else:
            k = i - 1
            A[i] = (A[i] + 10) - B[i]
            while A[k] == 0:
                A[k] = 9
                k -= 1
            A[k] -= 1
    i = 0
    while (A[i] == 0) & (len(A) != 1):
        A.remove(0)
    return A

# N-6
# Умножение натурального числа на цифру:
# Добавляем доп. разряд в начало
# k - перенос на другой разряд
# Если после умножения и + k число в разряде больше 10, то A[i] = A[i] % 10; k += 1
def MUL_ND_N(A, n):
    A.reverse()
    A.append(0)
    A.reverse()
    k = 0
    for i in range(len(A) - 1, 0, -1):
        tmp = A[i]
        A[i] = A[i] * n % 10
        A[i] += k
        k = int(tmp * n / 10)
        if A[i] >= 10:
            A[i] = A[i] % 10
            k += 1
    A[0] = k
    if A[0] == 0:
        A.remove(0)
    return A

# N-7
# Умножаем на 10^k: добавляем k нулей в конец массива
def MUL_Nk_N(A, k):
    B = A[:]
    for i in range(k):
        B.append(0)

    return B

# N-8
# Умножение натуральных чисел
def MUL_NN_N(A, B):
    p = []
    B.reverse()
    B.append(0)
    for i in range(len(B)):
        # t - текущий разряд на который умножаем число
        t = B[i]
        # Умножаем исходное число на t
        c = MUL_ND_N(list(map(int, A)), t)
        # Умноженаем результат на 10^k, чтобы "сдвинуть" его
        c = MUL_Nk_N(list(map(int, c)), i)
        # добавляем к результату
        p = ADD_NN_N(list(map(int, p)), list(map(int, c)))
    if p[0] == 0:
        del p[0]
    return p

# N-9 Вычитание из натурального другого натурального, умноженного на цифру для случая с неотрицательным результатом.
# Сначала находим произведение B и n. Если результат неотрицательный, то находим разность двух натуральных чисел,
# иначем возвращаем -1
def SUB_NDN_N(A, B, n):
    B = MUL_ND_N(B, n)

    if COM_NN_D(A, B) == 2 or COM_NN_D(A, B) == 0:

        A = SUB_NN_N(A, B)

        return A
    else:
        return "error in SUB_NDN_N"
# N-10
# Вычисление первой цифры деления, умноженное на 10^k, k - позиция этой цифры.
# Преобразование массивов в числа, перемещение большего числа на первое место,
# нахождение самого числа, ее позиции и результата.
def DIV_NN_Dk(A, B):
    count = 1
    if COM_NN_D(A, B) == 0:
        return 1, 0
    if COM_NN_D(A, B) == 1:
        big = B
        small = A
    else:
        big = A
        small = B
    k = 0
    n3 = small
    tm = small.copy()
    while COM_NN_D(big, n3) == 2:
        k += 1
        n3 = MUL_Nk_N(tm, k)
        tm = small.copy()
    k -= 1
    tm = small.copy()
    n3 = MUL_Nk_N(tm, k)
    small = n3
    tm = small.copy()
    while COM_NN_D(big, n3) == 2:
        count += 1
        n3 = MUL_ND_N(tm, count)
        tm = small.copy()
    if COM_NN_D(big, n3) != 0:
        count -= 1
    if count == 10:
        count = 1
        k += 1
    return count, k

# N-11 Частное от деления натуральных чисел
def DIV_NN_N(A, B):
    temp1 = A.copy()
    temp2 = B.copy()
    tempD1 = A.copy()
    tempD2 = B.copy()
    res = [0] * (DIV_NN_Dk(tempD1, tempD2)[1] + 1)
    while COM_NN_D(temp1, temp2) != 1:
        a, b = DIV_NN_Dk(temp1, temp2)
        res[b] = a
        c = MUL_Nk_N(temp2, b)
        temp2 = B.copy()
        temp1 = SUB_NDN_N(temp1, c, a)
    res.reverse()
    return res

# N-12 Остаток от деления
def MOD_NN_N(A, B):
    temp1 = A.copy()
    temp2 = B.copy()
    tempD1 = A.copy()
    tempD2 = B.copy()
    res = [0] * (DIV_NN_Dk(tempD1, tempD2)[1] + 1)
    while COM_NN_D(temp1, temp2) != 1:
        a, b = DIV_NN_Dk(temp1, temp2)
        res[b] = a
        c = MUL_Nk_N(temp2, b)
        temp2 = B.copy()
        temp1 = SUB_NDN_N(temp1, c, a)
    return temp1

# N-13
# НОД натуральных чисел
# Если одно число больше другого -> меняем местами
# Находим остатки от деления пока B не станет = 0
def GCF_NN_N(A, B):
    if COM_NN_D(A, B) == 0:
        return A
    if COM_NN_D(A, B) == 1:
        A, B = B, A
    if NZER_N_B(B) == 'Нет':
        return A
    else:
        A = MOD_NN_N(A, B)
        return GCF_NN_N(A, B)

# N - 14
# НОК натуральных чисел
# Делим произведение А и В на их НОД
def LCM_NN_N(A, B):
    GCF = GCF_NN_N(A, B)
    MUL = MUL_NN_N(A, B)
    return DIV_NN_N(MUL, GCF)


from config import Instance
from typing import List
from paths import get_training_metrics_path, get_cross_validation_scores_path
import h5py
import pandas as pd

def get_last_epoch(keys):
    epochs = [int(s.split('epoch')[1]) for s in keys]
    last_epoch = max(epochs)
    return last_epoch

def compute_score_for_each_model(instances: List[Instance]):
    cv_k = instances[0].cv_k
    df = Instance.get_projections(instances, hyperparameter_names=instances[0].get_dependencies_for_resources(
        'cross_validated_model'))
    df_scores = pd.DataFrame(columns=['cross_validated_model_hash', 'average_validation_loss'])
    for _, row in df.iterrows():
        d = row.to_dict()
        filtered_instances = Instance.get_filtered_instances(instances, d)
        assert len(filtered_instances) == cv_k
        assert set(Instance.get_projections(filtered_instances, ['cv_fold']).cv_fold.to_list()) == set(range(cv_k))
        average_validation_loss = 0
        for instance in filtered_instances:
            path = get_training_metrics_path(instance)
            with h5py.File(path, 'r') as f5:
                keys = f5.keys()
                # keys are like ['epoch10', 'epoch20', ...]
                last_epoch = get_last_epoch(keys)
                metrics = f5[f'epoch{last_epoch}']
                validation_loss = metrics['validation_loss'][...].item()
                average_validation_loss += validation_loss
        average_validation_loss /= cv_k
        instance_hash = filtered_instances[0].get_instance_hash(resource_name='cross_validated_model')
        assert len(
            set([instance.get_instance_hash(resource_name='cross_validated_model') for instance in filtered_instances]))
        df_scores = df_scores.append(
            {'cross_validated_model_hash': instance_hash, 'average_validation_loss': average_validation_loss},
            ignore_index=True)
    path = get_cross_validation_scores_path()
    df_scores.to_csv(path)

if __name__ == '__main__':
    from config import instances

    compute_score_for_each_model(instances)


v = list(map(int, input().split()))
d = {}

for i in v:
    d[i] = d.get(i, 0) + 1

ans = 0
for key, value in d.items():
    if value == 1:
        ans += key
print(ans)


from sksurv.metrics import (
    concordance_index_censored,
    brier_score,
    concordance_index_ipcw,
    cumulative_dynamic_auc,
    integrated_brier_score,
)

import numpy as np

class MyCIndex:
    def __init__(self, tied_tol=1e-8):
        self.tied_tol = tied_tol

    def __call__(self, y_test, y_pred):
        event_indicator = [y[0] for y in y_test]
        event_time = [y[1] for y in y_test]
        estimate = y_pred[:1800]

        score = concordance_index_censored(event_indicator=event_indicator,
                                           event_time=event_time,
                                           estimate=estimate,
                                           tied_tol=self.tied_tol)

        return score[0]

    @property
    def name(self):
        return 'C-index censored'

class MyBrierScore:
    def __init__(self, times):
        self.times = times

    def __call__(self, survival_train, survival_test, surv_func):
        estimate = [f(self.times) for f in surv_func]
        times, score = brier_score(survival_train=survival_train,
                                   survival_test=survival_test,
                                   estimate=estimate,
                                   times=self.times)

        return score[0]

    @property
    def name(self):
        return 'Brier score'

class MyCIndexIPCW:
    def __init__(self, tau=None, tied_tol=1e-08):
        self.tau = tau
        self.tied_tol = tied_tol

    def __call__(self, survival_train, survival_test, estimate):
        score = concordance_index_ipcw(survival_train=survival_train,
                                       survival_test=survival_test,
                                       estimate=estimate,
                                       tau=self.tau,
                                       tied_tol=self.tied_tol)
        return score[0]

    @property
    def name(self):
        return 'C-index ipcw'

class MyCumulativeDynamicAuc:
    def __init__(self, times, tied_tol=1e-08):
        self.times = times
        self.tied_tol = tied_tol

    def __call__(self, survival_train, survival_test, surv_func):
        estimate = [f(self.times) for f in surv_func]
        score = cumulative_dynamic_auc(survival_train,
                                       survival_test,
                                       estimate,
                                       self.times,
                                       self.tied_tol)
        return score

    @property
    def name(self):
        return 'Cumulative dynamic auc'

class MyIntegratedBrierScore:
    def __init__(self, times, tied_tol=1e-08):
        self.times = times
        self.tied_tol = tied_tol

    def __call__(self, survival_train, survival_test, func_surv):

        times = np.arange(self.times[0], self.times[1])
        estimate = np.asarray([[fn(t) for t in times] for fn in func_surv])

        score = integrated_brier_score(survival_train=survival_train[:720],
                                       survival_test=survival_test[:720],
                                       estimate=estimate[:720],
                                       times=times)

        return score

    @property
    def name(self):
        return 'Integrated brier score'


#Number of occurence of 2 as a digit in numbers from 1 to n

num = int(input("Enter num: "))
lis = []
c=0
for i in range(1,num+1):
    lis.append(i)

for num1 in lis:
    while num1>0:
        rem = num1%10
        if rem==2:
            c=c+1
        num1 = num1//10
print(c)

Logical Questions/Number occurence of 2 as an digit-imp.py


import sys
import os
import tensorflow as tf

# pylint: disable=unused-import
from tensorflow.contrib.framework.python.ops import audio_ops as contrib_audio
# pylint: enable=unused-import

class Labeller(object):

  def __init__(self, graph_file, label_file, input_name=None, output_name=None, how_many_labels=None ):
    """
        graph_file : graph file
        lable_file : label file. Assumtion : labels order should be in sync with confusion matrix order
        input_name, output_name, how_many_labels are needed for searching in trained graph
    """
    self.__graph_file = graph_file
    self.__label_file = label_file
    self.__input_name = input_name
    self.__output_name = output_name
    self.__how_many_labels = how_many_labels
    self.__initialize()

  def __initialize(self):
    """Loads the model and labels, and sets other required values"""
    self.__loadGraph()
    self.__loadLabels()

    if self.__input_name is None:
      self.__input_name = 'wav_data:0'

    if self.__output_name is None:
      self.__output_name = 'labels_softmax:0'

    if self.__how_many_labels is None:
      self.__how_many_labels = 1

    self._tf_session = tf.Session()
    self._session_tensor = self._tf_session.graph.get_tensor_by_name( self.__output_name )

  def __loadGraph(self):
    """Unpersists graph from file as default graph."""
    path = os.path.join(os.path.dirname(__file__), self.__graph_file)
    with tf.gfile.FastGFile(path, 'rb') as f:
      graph_def = tf.GraphDef()
      graph_def.ParseFromString( f.read() )
      tf.import_graph_def (graph_def, name='' )

  def __loadLabels(self):
    """Read in labels, one label per line."""
    path = os.path.join(os.path.dirname(__file__), self.__label_file)
    self.__readble_labels = [ line.rstrip() for line in tf.gfile.GFile(path) ]

  def analyze(self, a_wav_file_raw_data):
    """Runs the wave form of audio data through the graph and return the predictions."""
    _input = { self.__input_name : a_wav_file_raw_data }

    # Getting predictions values
    predictions, = self._tf_session.run( self._session_tensor, _input )

    # Sort to show labels in order of confidence
    top_k = predictions.argsort()[-self.__how_many_labels:][::-1]

    # preparing output
    data_to_return = [ ( self.__readble_labels[i], predictions[i] ) for i in top_k ]
    data_to_return = self.__readble_labels[top_k[0]]

    return data_to_return


import json

class Driver:
    def __init__(self):
        self.firstName = ""
        self.lastName = ""
        self.shortName = ""
        self.driverCategory = 0
        self.playerID = ""

class Team:

    def __init__(self):
        self.drivers = []
        self.carId = 0 # used for internal processing
        self.raceNumber = -1
        self.defaultGridPosition = -1
        self.forcedCarModel = -1
        self.overrideDriverInfo = -1
        self.isServerAdmin = 0
        self.ballastKg = 0
        self.configVersion = 1
        self.lap_cnt = 0
        self.cross_line_order = -1

class Event(object):
    name = ""
    teams = []

def _driver_to_json(d: Driver):
    return dict({
        'firstName': d.firstName,
        'lastName': d.lastName,
        'shortName': d.shortName,
        'driverCategory': d.driverCategory,
        'playerID': d.playerID,
    })

def _json_to_driver(json):
    d = Driver()

    d.firstName = json['firstName']
    d.lastName = json['lastName']
    d.shortName = json['shortName']
    d.driverCategory = json['driverCategory']
    d.playerID = json['playerID']

    return d

def _team_to_json(t: Team):
    d =  dict({
        'raceNumber': t.raceNumber,
        'defaultGridPosition': t.defaultGridPosition,
        'forcedCarModel': t.forcedCarModel,
        'overrideDriverInfo': t.overrideDriverInfo,
        'isServerAdmin': t.isServerAdmin,
        'ballastKg': t.ballastKg,
        'configVersion': t.configVersion
    })

    # serialize list of drivers
    d['drivers'] = []
    for drv in t.drivers:
        d['drivers'].append(_driver_to_json(drv))

    return d

def _json_to_team(json):
    t = Team()

    t.raceNumber = int(json['raceNumber'])
    t.defaultGridPosition = int(json['defaultGridPosition'])
    t.forcedCarModel = int(json['forcedCarModel'])
    t.overrideDriverInfo = int(json['overrideDriverInfo'])
    t.isServerAdmin = int(json['isServerAdmin'])
    t.ballastKg = int(json['ballastKg'])
    t.configVersion = int(json['configVersion'])

    t.drivers = []
    for driver in json['drivers']:
        t.drivers.append(_json_to_driver(driver))

    return t

def _event_to_json(e: Event):
    d = dict({
        'name': e.name
    })

    # serialize the list of teams
    d['entries'] = []
    for t in e.teams:
        d['entries'].append(_team_to_json(t))

    return d

def _json_to_event(json):
    e = Event()

    e.name = json['name']

    e.teams = []
    for team in json['entries']:
        e.teams.append(_json_to_team(team))

    return e


from bs4 import BeautifulSoup
from urllib.request import urlopen
import pyexcel

url = 'http://s.cafef.vn/bao-cao-tai-chinh/VNM/IncSta/2017/3/0/0/ket-qua-hoat-dong-kinh-doanh-cong-ty-co-phan-sua-viet-nam.chn'
conn = urlopen(url)
raw_data = conn.read()
web_text = raw_data.decode('utf-8')
soup = BeautifulSoup(web_text,'html.parser')
table = soup.find('table',{'style':'border-top: solid 1px #e6e6e6;border-left:solid 1px #cccccc;border-bottom:solid 1px #cccccc'})
rows = table.find_all('tr')
list1 =[]

for i in range (0,72):
    row = rows[i]
    data_list = row.find_all('td')
    data = {}
    for td in data_list:
        name = data_list[0].string
        data = {
            'Content':name,
            'Quý 4 - 2016':data_list[1].string,
            'Quý 1 - 2017':data_list[2].string,
            'Quý 2 - 2017':data_list[3].string,
            'Quý 3 - 2017':data_list[4].string
        }
    list1.append(data)

pyexcel.save_as(records = list1, dest_file_name = 'holy.xlsx')


import random
import string
import os

from PanaderiaVegana.Dominio.Bedidas import Bebida
from PanaderiaVegana.Dominio.Inventario import Inventario
from PanaderiaVegana.Dominio.Mesero import Mesero
from PanaderiaVegana.Dominio.Pan import Pan
from PanaderiaVegana.Dominio.Panadero import Panadero
from PanaderiaVegana.Dominio.Postre import Postre
from PanaderiaVegana.Infraestructura.Persistencia import Persistencia

if __name__ == '__main__':
    saver = Persistencia()
    saver.connect()
    nombreBebidas = ['Jugo Natural', 'limonada', 'agua']
    clasesPan = ['Croissant', 'Agridulce', 'Hawaiano']
    postres = ['Chocolate', 'Leche Vegetal', 'Dulce de Almendras']
    nombres = ['Juan', 'Carlos', 'Fernanda', 'Nathalia', 'Victoria', 'Andres']
    apellidos = ['Bedoya', 'Cardenas', 'Salazar', 'Restrepo', 'Fernandez']
    barrios = ['La fachada', 'Paraiso', 'Ciudad Dorada', 'Providencia', 'La Grecia',
               'Mercedes de Norte', 'Los Profesionales']
    # BEBIDA
    nombreBebida = random.choice(nombreBebidas)
    bebida = Bebida(nombreBebida)
    saver.guardar_bebida(bebida)
    Persistencia.save_json_bebida(bebida)
    # MESERO
    nombreMesero = random.choice(nombres)
    apellidoMesero = random.choice(apellidos)
    cedulaMesero = random.randint(1000000, 9999999)
    telefonoMesero = random.randint(1000000, 9999999)
    direccionMesero = "Barrio " + random.choice(barrios) + " Manzana " + random.choice(
        string.ascii_uppercase) + " Casa " + str(random.randint(0, 99))
    mesero = Mesero(nombreMesero, apellidoMesero, cedulaMesero, telefonoMesero, direccionMesero)
    saver.guardar_mesero(mesero)
    Persistencia.save_json_mesero(mesero)
    # PAN
    clasePan = random.choice(clasesPan)
    pan = Pan(clasePan)
    saver.guardar_pan(pan)
    Persistencia.save_json_pan(pan)
    # PANADERO
    nombrePanadero = random.choice(nombres)
    apellidoPanadero = random.choice(apellidos)
    cedulaPanadero = random.randint(1000000, 9999999)
    telefonoPanadero = random.randint(1000000, 9999999)
    direccionPanadero = "Barrio " + random.choice(barrios) + " Manzana " + random.choice(
        string.ascii_uppercase) + " Casa " + str(random.randint(0, 99))
    panadero = Panadero(nombrePanadero, apellidoPanadero, cedulaPanadero, telefonoPanadero, direccionPanadero)
    saver.guardar_panadero(panadero)
    Persistencia.save_json_panadero(panadero)
    # POSTRE
    nombrePostre = random.choice(postres)
    postre = Postre(nombrePostre)
    saver.guardar_postre(postre)
    Persistencia.save_json_postre(postre)
    inventario_json = Inventario()

    for file in os.listdir("./files"):
        if '.json' in file:
            inventario_json.agregar_mesero(Persistencia.load_json_mesero(file))
            inventario_json.agregar_bebida(Persistencia.load_json_bebida(file))
            inventario_json.agregar_pan(Persistencia.load_json_pan(file))
            inventario_json.agregar_panadero(Persistencia.load_json_panadero(file))
            inventario_json.agregar_postre(Persistencia.load_json_postre(file))


import torch
import numpy as np
from thop import profile
from thop import clever_format
if torch.__version__ >= "1.8":
    import torch_npu

def clip_gradient(optimizer, grad_clip):
    """
    For calibrating misalignment gradient via cliping gradient technique
    :param optimizer:
    :param grad_clip:
    :return:
    """
    for group in optimizer.param_groups:
        for param in group['params']:
            if param.grad is not None:
                param.grad.data.clamp_(-grad_clip, grad_clip)

def adjust_lr(optimizer, init_lr, epoch, decay_rate=0.1, decay_epoch=30):
    decay = decay_rate ** (epoch // decay_epoch)
    for param_group in optimizer.param_groups:
        param_group['lr'] *= decay

class AvgMeter(object):
    def __init__(self, num=40):
        self.num = num
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0
        self.losses = []

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count
        self.losses.append(val)

    def show(self):
        return torch.mean(torch.stack(self.losses[np.maximum(len(self.losses)-self.num, 0):]))

def CalParams(model, input_tensor):
    """
    Usage:
        Calculate Params and FLOPs via [THOP](https://github.com/Lyken17/pytorch-OpCounter)
    Necessarity:
        from thop import profile
        from thop import clever_format
    :param model:
    :param input_tensor:
    :return:
    """
    flops, params = profile(model, inputs=(input_tensor,))
    flops, params = clever_format([flops, params], "%.3f")
    print('[Statistics Information]\nFLOPs: {}\nParams: {}'.format(flops, params))

class AverageMeter(object):
    """Computes and stores the average and current value"""

    def __init__(self, name, fmt=':f', start_count_index=2):
        self.name = name
        self.fmt = fmt
        self.reset()
        self.start_count_index = start_count_index

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        if self.count == 0:
            self.N = n

        self.val = val
        self.count += n
        if self.count > (self.start_count_index * self.N):
            self.sum += val * n
            self.avg = self.sum / (self.count - self.start_count_index * self.N)

    def __str__(self):
        fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
        return fmtstr.format(**self.__dict__)

class Conv2dForAvgPool2d(torch.nn.Conv2d):
    def __init__(self, channels, kernel_size, stride, padding):
        super(Conv2dForAvgPool2d, self).__init__(channels, channels,
                                                 kernel_size, stride,
                                                 padding, groups=channels,
                                                 bias=False)
        self.weight.data = torch.ones_like(self.weight.data) / (kernel_size * kernel_size)
        self.requires_grad_(False)
        self.npu()
    def forward(self, x):
        return super().forward(x)


from pic2jal_environment import check_and_set_environment
base, mplabxversion = check_and_set_environment()    # obtain environment variables
if (base == ""):
   exit(1)

import sys
import os
import glob
import fnmatch
import subprocess
import platform

platform_name = platform.system()

# --- system dependent paths
if (platform_name == "Linux"):
   olddir = os.path.join("/", "media", "nas", "picdevices", "test")  # previous device files
   kdiff  = "kdiff3"                                                 # assumed to be in path
   cpy    = os.path.join(base, "comparejal_copy.sh")                 # copy commandfile
elif (platform_name == "Windows"):
   olddir = os.path.join("D:\\", "jallib-master", "include", "device")      # previous device files
   kdiff  = os.path.join("C:\\", "Program Files", "KDiff3", "kdiff3.exe")    # full path
   cpy    = os.path.join(base, "comparejal_copy.cmd")                # copy commandfile
elif (platform_name == "Darwin"):                                    # Mac
   olddir = os.path.join("/", "media", "nas", "picdevices", "test")  # previous device files
   kdiff  = "kdiff3"                                                 # assumed to be in path
   cpy    = os.path.join(base, "comparejal_copy.sh")                 # copy commandfile
else:
   print("Please add platform specific info to this script!")
   exit(1)

newdir = os.path.join(base, "test")                   # new device files
log    = os.path.join(base, "comparejal.log")         # list with change device files

# -----------------------------------------------------

def compare_devs(newdevs, selection):

   comparecount = 0
   diffcount = 0
   newcount = 0
   visual_compare = "ON"

   fl = open(log, "w")                                # log file
   fc = open(cpy, "w")                                # commandlist with copy commands

   newdevs.sort()
   for newdev in newdevs:                             # all new device files

      picname = os.path.split(newdev)[1][:-4]
      if not fnmatch.fnmatch(picname, selection):     # not within selection
         continue

      print(picname)                                  # progress signal

      olddev = os.path.join(olddir, picname +  ".jal")   # pathspec old device file
      if not os.path.exists(olddev):
         print("   Seems a new device")
         fl.write("++ " + newdev +  "\n")
         if (platform_name == "Linux"):
            fc.write("cp ")
         elif (platform_name == "Windows"):
            fc.write("copy ")
         elif (platform_name == "Darwin"):
            fc.write("cp ")
         else:                                        # other platform
            pass
         fc.write(os.path.join(newdir, newdev) + " " + olddev + "\n")
         newcount += 1
         continue

      fn = open(newdev, "r")
      if (fn == None):
         print("   Failed to open new:", newdev, ", terminating!")
         break
      fo = open(os.path.join(olddev, olddev), "r")
      if (fo == None):
         print("   Failed to open old:", olddev, ", terminating!")
         break

      # skip header part of new and old device files
      newl = " "
      while not newl.startswith("include chipdef_jallib"):
         newl = fn.readline()
      oldl = " "
      while not oldl.startswith("include chipdef_jallib"):
         oldl = fo.readline()

      comparecount += 1
      diff_flag = 0                                   # no differences (yet)
      x = "Y"                                         # default answer

      while (True):                                   # all lines

         newl = fn.readline()
         if (newl == ""):                             # EOF
            break

         oldl = fo.readline()
         if (oldl == ""):                             # EOF
            break

         if (newl == oldl):                           # lines are equal
            continue                                  # continue with next line

         print("   new:", newl.strip())
         print("   old:", oldl.strip())
         fo.close()                                   # unequal pair of lines: no more reading
         fn.close()
         diff_flag = 1
         diffcount = diffcount + 1                    # count files which are different
         fl.write("!! " + os.path.join(olddir, olddev) + "\n")
         if (platform_name == "Linux"):
            fc.write("cp ")
         elif (platform_name == "Windows"):
            fc.write("copy ")
         elif (platform_name == "Darwin"):
            fc.write("cp ")
         else:                                        # other platform
            pass
         fc.write(os.path.join(newdir, newdev) + " " + olddev + "\n")
         if (visual_compare != 'ON'):
            break                             #
         sys.stdout.write("Press N to skip this file, U to only list different files, Q to quit entirely:")
         sys.stdout.flush()
         x = sys.stdin.readline().strip().upper()
         if (x == "U"):                               # skip visual compare of remaining files
            visual_compare = "OFF"
            break
         elif ((x == "N") or (x == "Q")):             # no (more) compares
            break
         else:
            subprocess.call([kdiff, newdev, os.path.join(olddir, olddev)], shell=False)
            break

      if (diff_flag == 0):                            # no differences
         fl.write('== ' + newdev + "\n")

      if (x == "Q"):                                  # skip all following files in list
         break

   fl.close()                                         # close log
   fc.close()                                         # close list of copy commands

   return [comparecount, diffcount, newcount]

# === E N T R Y   P O I N T ===

if (__name__ == "__main__"):

   if (len(sys.argv) > 1):                            # any commandline argument(s)
      selection = sys.argv[1]                         # first is PIC name selection (wildcards)
   else:
      selection = "1*"                                # all device files

   print("Comparing 2 collections of include files:")
   print("  ", newdir, "<-->", olddir)

   newdevs = glob.glob(os.path.join(newdir, selection))
   if (len(newdevs) == 0):
      print("No files matching", "'" + selection + "'", "in directory", newdir)
      sys.exit(1)

   print("   Comparing", len(newdevs), "device files in", newdir)

   comparecount, diffcount, newcount = compare_devs(newdevs, selection)   # pass list of device files

   if ((diffcount > 0) | (newcount > 0)):
      print("\nOf the", len(newdevs), "new device files", comparecount, "were compared,", diffcount, "did NOT match!")
      print("There are", newcount, "new device files to be added to", olddir)
      print("See", log, "for a list of files with different files.\n")
   else:
      print("\nNo differences found in", comparecount, "device files.\n")


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

import inspect

from ..exc import *

def public(name=None):
    """Set RPC name on function.

    This function decorator will set the ``_rpc_public_name`` attribute on a
    function, causing it to be picked up if an instance of its parent class is
    registered using
    :py:func:`~tinyrpc.dispatch.RPCDispatcher.register_instance`.

    ``@public`` is a shortcut for ``@public()``.

    :param name: The name to register the function with.
    """
    # called directly with function
    if callable(name):
        f = name
        f._rpc_public_name = f.__name__
        return f

    def _(f):
        f._rpc_public_name = name or f.__name__
        return f

    return _

class RPCDispatcher(object):
    """Stores name-to-method mappings."""

    def __init__(self):
        self.method_map = {}
        self.subdispatchers = {}

    def add_subdispatch(self, dispatcher, prefix=''):
        """Adds a subdispatcher, possibly in its own namespace.

        :param dispatcher: The dispatcher to add as a subdispatcher.
        :param prefix: A prefix. All of the new subdispatchers methods will be
                       available as prefix + their original name.
        """
        self.subdispatchers.setdefault(prefix, []).append(dispatcher)

    def add_method(self, f, name=None):
        """Add a method to the dispatcher.

        :param f: Callable to be added.
        :param name: Name to register it with. If ``None``, ``f.__name__`` will
                     be used.
        """
        assert callable(f), "method argument must be callable"
                            # catches a few programming errors that are
                            # commonly silently swallowed otherwise
        if not name:
            name = f.__name__

        if name in self.method_map:
            raise RPCError('Name %s already registered')

        self.method_map[name] = f

    def dispatch(self, request):
        """Fully handle request.

        The dispatch method determines which method to call, calls it and
        returns a response containing a result.

        No exceptions will be thrown, rather, every exception will be turned
        into a response using :py:func:`~tinyrpc.RPCRequest.error_respond`.

        If a method isn't found, a :py:exc:`~tinyrpc.exc.MethodNotFoundError`
        response will be returned. If any error occurs outside of the requested
        method, a :py:exc:`~tinyrpc.exc.ServerError` without any error
        information will be returend.

        If the method is found and called but throws an exception, the
        exception thrown is used as a response instead. This is the only case
        in which information from the exception is possibly propagated back to
        the client, as the exception is part of the requested method.

        :py:class:`~tinyrpc.RPCBatchRequest` instances are handled by handling
        all its children in order and collecting the results, then returning an
        :py:class:`~tinyrpc.RPCBatchResponse` with the results.

        :param request: An :py:func:`~tinyrpc.RPCRequest`.
        :return: An :py:func:`~tinyrpc.RPCResponse`.
        """
        if hasattr(request, 'create_batch_response'):
            results = [self._dispatch(req) for req in request]

            response = request.create_batch_response()
            if response != None:
                response.extend(results)

            return response
        else:
            return self._dispatch(request)

    def _dispatch(self, request):
        try:
            try:
                method = self.get_method(request.method)
            except KeyError as e:
                return request.error_respond(MethodNotFoundError(e))

            # we found the method
            try:
                result = method(*request.args, **request.kwargs)
            except Exception as e:
                # an error occured within the method, return it
                return request.error_respond(e)

            # respond with result
            return request.respond(result)
        except Exception as e:
            # unexpected error, do not let client know what happened
            return request.error_respond(ServerError())

    def get_method(self, name):
        """Retrieve a previously registered method.

        Checks if a method matching ``name`` has been registered.

        If :py:func:`get_method` cannot find a method, every subdispatcher
        with a prefix matching the method name is checked as well.

        If a method isn't found, a :py:class:`KeyError` is thrown.

        :param name: Callable to find.
        :param return: The callable.
        """
        if name in self.method_map:
            return self.method_map[name]

        for prefix, subdispatchers in self.subdispatchers.iteritems():
            if name.startswith(prefix):
                for sd in subdispatchers:
                    try:
                        return sd.get_method(name[len(prefix):])
                    except KeyError:
                        pass

        raise KeyError(name)

    def public(self, name=None):
        """Convenient decorator.

        Allows easy registering of functions to this dispatcher. Example:

        .. code-block:: python

            dispatch = RPCDispatcher()

            @dispatch.public
            def foo(bar):
                # ...

            class Baz(object):
                def not_exposed(self):
                    # ...

                @dispatch.public(name='do_something')
                def visible_method(arg1)
                    # ...

        :param name: Name to register callable with
        """
        if callable(name):
            self.add_method(name)
            return name

        def _(f):
            self.add_method(f, name=name)
            return f

        return _

    def register_instance(self, obj, prefix=''):
        """Create new subdispatcher and register all public object methods on
        it.

        To be used in conjunction with the :py:func:`tinyrpc.dispatch.public`
        decorator (*not* :py:func:`tinyrpc.dispatch.RPCDispatcher.public`).

        :param obj: The object whose public methods should be made available.
        :param prefix: A prefix for the new subdispatcher.
        """
        dispatch = self.__class__()
        for name, f in inspect.getmembers(
            obj, lambda f: callable(f) and hasattr(f, '_rpc_public_name')
        ):
            dispatch.add_method(f, f._rpc_public_name)

        # add to dispatchers
        self.add_subdispatch(dispatch, prefix)


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

'''
Measure the harmonic distortion (HD2 and HD3) of the DAC and ADC combined.
Connect DAC0 to ADC0 to perform the measurement.
'''

import numpy as np
import os
import time
import matplotlib.pyplot as plt
from fft import FFT
from koheron import connect

host = os.getenv('HOST', '192.168.1.16')
client = connect(host, 'fft', restart=False)
driver = FFT(client)

# Use Rectangular FFT window
driver.set_fft_window(0)

n_pts = driver.n_pts
fs = 250e6

fmin = 0.1e6
fmax = 40e6

freqs = np.linspace(fmin, fmax ,num=500)
freqs = np.round(freqs / fs * n_pts) * fs / n_pts

hd1 = 0.0 * freqs
hd2 = 0.0 * freqs
hd3 = 0.0 * freqs

for i, freq in enumerate(freqs):
    n = np.uint32(freq / fs * n_pts)
    driver.set_dds_freq(0, freq)
    time.sleep(0.5)
    psd = driver.read_psd()
    psd_db = 10*np.log10(psd)

    hd1[i] = psd_db[n-1]
    hd2[i] = psd_db[2*n-1] - hd1[i]
    hd3[i] = psd_db[3*n-1] - hd1[i]

    print(freq, hd1[i], hd2[i], hd3[i])

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111)

ax.set_xlabel('Frequency (MHz)')
ax.set_ylabel('Harmonic distortion (dB)')
ax.semilogx(freqs*1e-6, hd2, label='HD2')
ax.semilogx(freqs*1e-6, hd3, label='HD3')
ax.set_xlim([fmin * 1e-6, fmax * 1e-6])
ax.legend(loc=2)

plt.show()

np.save('alpha250_dac_distortion.npy', (freqs, hd2, hd3))


import json
import time
import random
from datetime import datetime
from parameters import TRANSACTIONS_TOPIC, DELAY
from streaming_kafka.confluent_utils import create_producer

_id = 0
producer = create_producer()

if producer is not None:

    fake_data = ["Noor", "Victor", "Yusuf", "Bayang", 123, 152, 555]
    while True:
        # Generate some wrong good case to simulate normal and abnormal behavior
        for_pred = random.choice(fake_data)

        X_test = for_pred

        current_time = datetime.utcnow().isoformat()

        record = {"id": _id, "data": X_test, "current_time": current_time}
        print(f"producing event_{_id}: {record}")
        record = json.dumps(record).encode("utf-8")

        producer.produce(topic=TRANSACTIONS_TOPIC,
                         value=record)
        producer.flush()

        _id += 1
        time.sleep(DELAY)