from Tkinter import *
import sys
sys.path.append('../')
from Card import *

sys.path.append('../TK_UIv2/')
from TKPlayerBase import TKPlayerBase

from UITools import BoardTools

class TKSimulatorScreen:
    def __init__(self, actions, canvas, game_board, xoffset, yoffset):
        self.actions = actions
        self.canvas = canvas
        self.game_board = game_board
        self.xoffset = xoffset
        self.yoffset = yoffset

        self.screen_items = []
        self.casting_hexes_dict = {}
        self.casting_buttons = []

    def paint(self, cards, player_id_to_casting_hexes_dict):
        self.casting_hexes_dict = player_id_to_casting_hexes_dict
        self.paint_controls()
        self.paint_cards(cards)

    def clear(self):
        for item in self.screen_items:
            self.canvas.delete(item)
        self.screen_items = []

    def paint_controls(self):
        x = self.xoffset
        y = self.yoffset
        txt1 = "Next"
        txt2 = "Reset"

        next_opts = { "anchor":W, "window": Button(text=txt1, \
                command=self.actions.next) }
        next_btn = self.canvas.create_window(x, y, **next_opts)
        self.screen_items.append(next_btn)

        reset_opts = { "anchor":W, "window": Button(text=txt2, \
                command=self.actions.reset) }
        reset_btn = self.canvas.create_window(x+140, y, **reset_opts)
        self.screen_items.append(reset_btn)

    def paint_cards(self, cards):
        def card_button_command(card):
            return lambda: self.card_clicked(card)

        x = self.xoffset
        y = self.yoffset + 50

        button_offset = 130

        for i, card in enumerate(cards):
            btn_text = ''.join( [card.name, " $", str(card.cost)] )
            card_opts = { "anchor":W, "window": Button(text=btn_text, \
                    command=card_button_command(card)) }

            button = self.canvas.create_window(x, y, **card_opts)
            self.screen_items.append(button)

            if i % 7 == 6:
                y += 25
                x = self.xoffset
            else:
                x += button_offset

    def card_clicked(self, card):
        self.clear_casting_buttons()

        if isinstance(card, SpellCard):
            self.paint_spell_casting_buttons(card)
        elif isinstance(card, BuildingCard):
            self.paint_building_casting_buttons(card)
        else:
            self.paint_unit_casting_buttons(card)

    def paint_unit_casting_buttons(self, card):
        def play_unit_command(pid, c, l):
            return lambda: self.play_unit_card(pid, c, l)

        for player_id, casting_hexes_list in self.casting_hexes_dict.iteritems():
            for i, pos in enumerate(casting_hexes_list):
                pix = self.game_board.get_center_pixel_for_battlefield_position(pos)

                btn = Button(text=str(i),
                        command=play_unit_command(player_id, card, pos))
                btn_opts = { "window": btn }
                self.casting_buttons.append(self.canvas.create_window(pix[0], pix[1], **btn_opts))

    def paint_spell_casting_buttons(self, card):
        def play_spell_command(pid, c, s):
            return lambda: self.play_spell_card(pid, c, s)

        self._paint_base_casting_buttons(card, TKPlayerBase.SPELL_COLUMN_INDEX,
                play_spell_command)

    def paint_building_casting_buttons(self, card):
        def play_building_command(pid, c, s):
            return lambda: self.play_building_card(pid, c, s)

        self._paint_base_casting_buttons(card, TKPlayerBase.BUILDING_COLUMN_INDEX,
                play_building_command)

    def _paint_base_casting_buttons(self, card, slot_column, on_click):
        for player_id in self.casting_hexes_dict.iterkeys():
            for i in xrange(self.game_board.bf_height):
                slot = (slot_column, i)
                pix = self.game_board.get_center_pixel_for_player_base_slot( \
                        player_id, slot)
                btn = Button(text=str(i), command=on_click(player_id, card, i))
                btn_opts = { "window": btn }
                self.casting_buttons.append(self.canvas.create_window(pix[0], pix[1], **btn_opts))

    #this indirection is necessary due to the way closure works
    #in other words, removing the middle step sets these variables equal to
    #last value of the iteration
    def play_unit_card(self, player_id, card, location):
        self.clear_casting_buttons()
        self.actions.play_unit_card(player_id, card, location)

    def play_spell_card(self, player_id, card, slot):
        self.clear_casting_buttons()
        self.actions.play_spell_card(player_id, card, slot)

    def play_building_card(self, player_id, card, slot):
        self.clear_casting_buttons()
        self.actions.play_building_card(player_id, card, slot)

    def clear_casting_buttons(self):
        for casting_button in self.casting_buttons:
            self.canvas.delete(casting_button)


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

from threading import Thread
from math import factorial, sin

eps = .0000001

def inf_sum(x, check, num):
    summa = 1
    i = 1
    prev = 0
    while abs(summa - prev) > eps:
        prev = summa
        summa += ((-1)**i * x**(2*i + 1)) / factorial(2 * i + 1)
        i += 1
    print(f"The sum number {num} is: {summa}")
    print(f"Check: sin{x} = {-check}")

if __name__ == '__main__':
    checksum1 = sin(1)
    thread1 = Thread(target=inf_sum, args=(1, checksum1, 1))
    thread1.start()
    checksum2 = sin(4)
    thread2 = Thread(target=inf_sum, args=(4, checksum2, 2))
    thread2.start()


import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats

# asset parameters
expected_return = 0.12
volatility = 0.20

# risk-free rate
risk_free_rate = 0.05

# standardize the risk-free rate
z = (risk_free_rate - expected_return) / volatility

# compute the probability of underperformance
prob_underperformance = stats.norm.cdf(z)

# Define the distribution
dist = stats.norm(loc=expected_return, scale=volatility)

# Generate values
values = np.linspace(expected_return - 3*volatility, expected_return + 3*volatility, 100)

# Generate pdf values
pdf_values = dist.pdf(values)

# Create the plot
plt.figure(figsize=(10,6))
plt.plot(values, pdf_values, lw=2)

# Customize the title and labels
plt.title('Distribution of Returns and Underperformance', fontsize=14)
plt.xlabel('Returns', fontsize=12)
plt.ylabel('Probability Density', fontsize=12)

# Customize the grid and spines
plt.grid(alpha=0.5)
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)

# Shade the underperformance region
underperformance_values = np.linspace(expected_return - 3*volatility, risk_free_rate, 100)
underperformance_pdf = dist.pdf(underperformance_values)
plt.fill_between(underperformance_values, underperformance_pdf, color='red', alpha=0.3)

# Annotate the risk-free rate
plt.axvline(x=risk_free_rate, color='red', linestyle='--', lw=1.5)
plt.annotate(f'Risk-free rate: {risk_free_rate*100:.1f}%', (risk_free_rate, max(pdf_values)/2),
             textcoords="offset points", xytext=(-15,10), ha='center', fontsize=9, color='red',
             bbox=dict(facecolor='white', edgecolor='red', boxstyle='round,pad=0.2'))

# Annotate the probability of underperformance
plt.text(expected_return - 3*volatility, max(pdf_values)*0.9, f'Prob. of Underperformance: {prob_underperformance*100:.2f}%', fontsize=10, bbox=dict(facecolor='white', alpha=0.8))

# Annotate asset parameters
plt.text(expected_return - 3*volatility, max(pdf_values)*0.8, f'Expected Return: {expected_return*100:.2f}%\nVolatility: {volatility*100:.2f}%', fontsize=10, bbox=dict(facecolor='white', alpha=0.8))

plt.show()


import csv
import json

csvFilePath = 'death_natl.csv'
jsonFilePath = 'death_data.json'

def read_CSV(csvFilePath, jsonFilePath):
    csv_rows = []
    with open(csvFilePath) as csvfile:
        reader = csv.DictReader(csvfile)
        field = reader.fieldnames
        for row in reader:
            csv_rows.extend([{field[i]:row[field[i]] for i in range(len(field))}])
            convert_write_json(csv_rows, jsonFilePath)

#Convert csv data into json and format it to make it human readable
def convert_write_json(data, jsonFilePath):
    with open(jsonFilePath, "w") as f:
        f.write(json.dumps(data, sort_keys=False, indent=4, separators=(',', ': ')))

read_CSV(csvFilePath, jsonFilePath)


liczba = int(input("Podaj liczbe dziesietna: "))
tab = []
i = 0
while liczba != 0:
    tab.append(liczba % 2)
    liczba = liczba // 2
print("Liczba po zamianie na postac binarna: ", end="")
for j in range(len(tab) - 1, -1, -1):
    print(tab[j], end="")
print()


def prepareVideoResponsePacket(video,videotopics):
    respVideo={}
    topics=''
    for videotopic in videotopics:
        if topics=='':
            topics=str(videotopic.topic_id)
        else:
            topics=topics+','+str(videotopic.topic_id)
    respVideo={
        'Id': video.video_id,
        'Name':video.name,
        'Description':video.description,
        'Url':video.url,
        'Thumbnail':video.thumbnail,
        'Language':video.lang,
        'UploadDate':video.upload_date,
        'topics':topics}

    return respVideo


from . import BaseCollector, entry
import re
import os

class Collector(BaseCollector):
    def postinit(self):
        if not os.path.exists(os.path.join(self.config.procfs, 'diskstats')):
            raise NotImplementedError
        self.re_ignored_devices = re.compile(self.config.diskstats_ignored_devices)
        self.re_whitespace = re.compile('\s+')

    def run(self):
        out = {}
        metrics_info = [
            ('node_disk_reads_completed', 'counter', 'The total number of reads completed successfully.'),
            ('node_disk_reads_merged', 'counter', 'The total number of reads merged. See https://www.kernel.org/doc/Documentation/iostats.txt.'),
            ('node_disk_sectors_read', 'counter', 'The total number of sectors read successfully.'),
            ('node_disk_read_time_ms', 'counter', 'The total number of milliseconds spent by all reads.'),
            ('node_disk_writes_completed', 'counter', 'The total number of writes completed successfully.'),
            ('node_disk_writes_merged', 'counter', 'The number of writes merged. See https://www.kernel.org/doc/Documentation/iostats.txt.'),
            ('node_disk_sectors_written', 'counter', 'The total number of sectors written successfully.'),
            ('node_disk_write_time_ms', 'counter', 'This is the total number of milliseconds spent by all writes.'),
            ('node_disk_io_now', 'counter', 'The number of I/Os currently in progress.'),
            ('node_disk_io_time_ms', 'counter', 'Total Milliseconds spent doing I/Os.'),
            ('node_disk_io_time_weighted', 'counter', 'The weighted # of milliseconds spent doing I/Os. See https://www.kernel.org/doc/Documentation/iostats.txt.'),
            ('node_disk_bytes_read', 'counter', 'The total number of bytes read successfully.'),
            ('node_disk_bytes_written', 'counter', 'The total number of bytes written successfully.'),
        ]
        values = {}
        for (metric, metric_type, metric_help) in metrics_info:
            values[metric] = []
        diskstats = []
        with open(os.path.join(self.config.procfs, 'diskstats')) as f:
            for l in f:
                diskstats.append(self.re_whitespace.split(l.strip()))
        for l in diskstats:
            if self.re_ignored_devices.match(l[2]):
                continue
            labels = {
                'device': l[2],
            }
            values['node_disk_reads_completed'].append((labels, int(l[3])))
            values['node_disk_reads_merged'].append((labels, int(l[4])))
            values['node_disk_sectors_read'].append((labels, int(l[5])))
            values['node_disk_read_time_ms'].append((labels, int(l[6])))
            values['node_disk_writes_completed'].append((labels, int(l[7])))
            values['node_disk_writes_merged'].append((labels, int(l[8])))
            values['node_disk_sectors_written'].append((labels, int(l[9])))
            values['node_disk_write_time_ms'].append((labels, int(l[10])))
            values['node_disk_io_now'].append((labels, int(l[11])))
            values['node_disk_io_time_ms'].append((labels, int(l[12])))
            values['node_disk_io_time_weighted'].append((labels, int(l[13])))
            values['node_disk_bytes_read'].append((labels, (int(l[5]) * 512)))
            values['node_disk_bytes_written'].append((labels, (int(l[9]) * 512)))
            values['node_disk_bytes_written'].append((labels, (int(l[9]) * 512)))

        for (metric, metric_type, metric_help) in metrics_info:
            out[metric] = entry(values[metric], metric_type, metric_help)
        self.output = out

    def parser_config(self, parser):
        parser.add_argument(
            '-collector.diskstats.ignored-devices', '--collector.diskstats.ignored-devices', type=str,
            default='^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$',
            dest='diskstats_ignored_devices',
            help='Regexp of devices to ignore for diskstats.',
        )


import itertools
import logging
import os
import numpy as np

import pytest
import cocotb_test.simulator

import cocotb
from cocotb.clock import Clock
from cocotb.triggers import RisingEdge, FallingEdge
from cocotb.regression import TestFactory

import matplotlib.pyplot as plt

class TB:
    def __init__(self, dut):
        self.dut = dut
        self.signal_a = []
        self.signal_b = []
        self.signal_c = []

        self.getbit = [2048,1024,512,256,128,64,32,16,8,4,2,1]

        self.log = logging.getLogger("cocotb.tb")
        self.log.setLevel(logging.DEBUG)

        cocotb.cocotb.start_soon(Clock(dut.i_clk, 10,units="ns").start())

    async def reset(self):
        self.dut.i_rstn.setimmediatevalue(0)
        self.dut.phase_a.setimmediatevalue(0)
        self.dut.phase_b.setimmediatevalue(0)
        self.dut.phase_c.setimmediatevalue(0)

        self.dut.i_ld.setimmediatevalue(0)
        self.dut.i_step.setimmediatevalue(1)
        self.dut.i_lgcoeff.setimmediatevalue(11)
        #self.dut.m_axis_tready.setimmediatevalue(0)
        # self.dut.EnableSampleGeneration.setimmediatevalue(0)
        await RisingEdge(self.dut.i_clk)
        await RisingEdge(self.dut.i_clk)
        self.dut.i_rstn.value = 0
        await RisingEdge(self.dut.i_clk)
        await RisingEdge(self.dut.i_clk)
        self.dut.i_ce.value = 1
        self.dut.i_rstn.value = 1
        #self.dut.m_axis_tready.value = 1
        await RisingEdge(self.dut.i_clk)
        await RisingEdge(self.dut.i_clk)

    async def write_sig_a_thead(self, signal):
        for i in range(len(signal)):
            data = int(signal[i])
            await FallingEdge(self.dut.i_clk)
            self.dut.phase_a.value  = data

    async def write_sig_b_thead(self, signal):
        for i in range(len(signal)):
            data = int(signal[i])
            await FallingEdge(self.dut.i_clk)
            self.dut.phase_b.value  = data

    async def write_sig_c_thead(self, signal):
        for i in range(len(signal)):
            data = int(signal[i])
            await FallingEdge(self.dut.i_clk)
            self.dut.phase_c.value  = data

    def toSigned32(self, n):
        n = n & 0xffffffff
        return n | (-(n & (1<<31)))

    def toSigned8(self, n):
        n = n & 0xff
        return n | (-(n & (1<<7)))

    def toSigned16(self, n):
        n = n & 0xffff
        return n | (-(n & (1<<15)))

    def generateSin(self, freq, time, amp,  sample_rate, random, random_range, channel):
        samples = np.arange(0, time, 1/sample_rate)
        noise = 0
        if random == 1:
              noise = np.random.randint(random_range, size=(len(samples)))
        if channel == 0:
            self.signal_a = amp/2 * np.sin(2 * np.pi * freq * samples)  + amp/2  + noise
            self.signal_a = np.int16(self.signal_a)
        elif channel == 1:
            self.signal_b = amp/2 * np.sin(2 * np.pi * freq * samples - (2*np.pi/3))  + amp/2 + noise
            self.signal_b = np.int16(self.signal_b)
        elif channel == 2:
            self.signal_c = amp/2 * np.sin(2 * np.pi * freq * samples + (2*np.pi/3)) + amp/2  + noise
            self.signal_c = np.int16(self.signal_c)

async def run_test(dut):

    tb = TB(dut)

    #id_count = 2**len(tb.source.bus.tid)

    cur_id = 1

    for i in range(3):
        tb.generateSin(0.06,100,3500,1e3,1,500,i)

    #plt.plot(tb.signal_a)
    #plt.plot(tb.signal_b)
    #plt.show()
    await tb.reset()

    #await tb.write(20,12)

    # Run reset_dut concurrently
    write_thread_0 = cocotb.start_soon(tb.write_sig_a_thead(tb.signal_a))
    write_thread_1 = cocotb.start_soon(tb.write_sig_b_thead(tb.signal_b))
    write_thread_2 = cocotb.start_soon(tb.write_sig_c_thead(tb.signal_c))

    assert 1 == 1
    # await RisingEdge(dut.CLK100MHz)
    # await RisingEdge(dut.CLK100MHz)

    # Wait for the other thread to complete
    await write_thread_0
    await write_thread_1
    await write_thread_2

if cocotb.SIM_NAME:
    factory = TestFactory(run_test)
    factory.generate_tests()

# cocotb-test

tests_dir = os.path.abspath(os.path.dirname(__file__))
hdl_dir = os.path.abspath(os.path.join(tests_dir, '..',  'hdl'))
pll_dir = os.path.abspath(os.path.join(hdl_dir, '..',  'pll'))

@pytest.mark.parametrize("data_width", [12])
def test_pll(request, data_width):
    dut = "pll"
    module = os.path.splitext(os.path.basename(__file__))[0]
    toplevel = dut

    verilog_sources = [
        os.path.join(hdl_dir, f"{dut}.v"),
        #os.path.join(hdl_dir, "pi_controller.v"),
        #os.path.join(pll_dir, "fd.v"),
        #os.path.join(pll_dir, "lp.v"),
        #os.path.join(pll_dir, "pd.v"),
        #os.path.join(pll_dir, "vcodiv.v"),
    ]

    parameters = {}

    parameters['ADC_DATA_WIDTH'] = data_width

    extra_env = {f'PARAM_{k}': str(v) for k, v in parameters.items()}

    sim_build = os.path.join(tests_dir, "sim_build",
        request.node.name.replace('[', '-').replace(']', ''))

    cocotb_test.simulator.run(
        python_search=[tests_dir],
        verilog_sources=verilog_sources,
        toplevel=toplevel,
        module=module,
        parameters=parameters,
        sim_build=sim_build,
        extra_env=extra_env,
    )


import datetime
from django.contrib.auth.models import User
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.utils import timezone

class Deal(models.Model):
  CATEGORY_LABEL = (
    ('poc', 'おこずかい'),
    ('sal', '給与'),
    ('oti', 'その他所得'),
    ('foo', '食費'),
    ('boo', '教育'),
    ('tra', '交通費'),
    ('sho', 'ショッピング'),
    ('prs', 'プレゼント'),
    ('ent', '交際費'),
    ('fee', '手数料'),
    ('oto', 'その他消費'),
  )

  # define
  date = models.DateField('日付', default=timezone.now)
  in_out = models.IntegerField('収支', validators=[MinValueValidator(-999999), MaxValueValidator(999999)])
  category = models.CharField(
    'カテゴリー',
    choices=CATEGORY_LABEL,
    default='foo',
    max_length=3,
  )
  description = models.CharField('内容', blank=True, max_length=50)

  user = models.ForeignKey(
    User,
    on_delete=models.SET_NULL,
    null=True,
  )

  def __str__(self):
    return self.description

class Wishlist(models.Model):
  name = models.CharField('名前', max_length=100)
  image = models.ImageField('画像', upload_to='images/', blank=True)
  price = models.IntegerField('価格',
    validators=[
      MinValueValidator(0),
      MaxValueValidator(9999999)
    ],
    blank=True,
  )
  term = models.IntegerField('期間',
    validators=[
      MinValueValidator(1),
      MaxValueValidator(3653)
    ],
    blank=True,
  )
  url = models.CharField('URL', max_length=255, blank=True)
  created_at = models.DateTimeField('作成日', auto_now_add=True)
  memo = models.TextField(
    '備考',
    blank=True,
    max_length=300,
    )

  user = models.ForeignKey(
    User,
    on_delete=models.SET_NULL,
    null=True,
  )


#Falling body simulation
#Author Ronald Jasper

import math

#earth's acceleration due to gravity, a constant in m/(sec square)
g=9.80665

def distanceTraveledWhileFall(t):
    #distance traveled by a body while falling for t seconds
    #d is in meters, t is in seconds
    d=0.5*g*t*t
    return(d)

def timeTakenToFall(d):
    #Time taken to fall d distance
    #d is in meters, t is in seconds
    t=math.sqrt(2*d/g)
    return(t)

def instantaneousVelocityAfterFallingElapsedTime(t):
    v=g*t
    return(v)

def instantaneousVelocityAfterFallingDistance(d):
    v=math.sqrt(2*g*d)
    return(v)

def mps2kmphr(mpc):
    #convert m/s to km/hr
    return(mpc/1000*3600)

c=input("choice: enter time or distance. t or d:")

if (c=="t"):
    time=float(input("Enter time in seconds:"))
    distance=distanceTraveledWhileFall(time)
    velocity=instantaneousVelocityAfterFallingElapsedTime(time)
    print("")
    print("Input time of fall:", time, " seconds or ", time/60.0, " minutes")
    print("Distance fell:", distance, " meters or ", distance/1000, " km")
    print("Velocity at that time:", velocity, " meters/second or ", mps2kmphr(velocity), " km/hour")
    print("")

if (c=="d"):
    distance=float(input("Enter distance of fall in meters:"))
    time=timeTakenToFall(distance)
    velocity=instantaneousVelocityAfterFallingDistance(distance)
    print("")
    print("Distance of fall:", distance, " meters or ", distance/1000, " km")
    print("Time of fall:", time, " seconds or ", time/60.0, " minutes")
    print("Velocity at that distance:", velocity, " meters/second or ", mps2kmphr(velocity), " km/hour")
    print("")


# -*- coding: utf-8 -*-
"""
Created on Thu Jul 27 10:44:28 2017

@author: datkin10
"""

from loggerhelpers import LoggerHelper
import datetime
import json
import pandas
import smtplib
import re
# This class is a singleton instance
class NotificationManager:

    __instance = []

    @staticmethod
    def getInstance(userID):
        for instance in NotificationManager.__instance:
            if instance.userID == userID:
                return instance

        instance = NotificationManager(userID)
        return instance

    @staticmethod
    def removeInstance(userID):
        for instance in NotificationManager.__instance:
            if instance.userID == userID:
                NotificationManager.__instance.remove(instance)

    def __init__(self, userID):
        self.userID = userID
        self.notificationList = []
        self.typeList =[]
        self.fromList = []
        self.indexList = []
        self.index = 0
        self.EMAIL_HOST = "mailo2.uhc.com"
        self.EMAIL_PORT = 25
        self.FROM = "parkinsontracker@optum.com"
        self.newStartGiven = False
        lh = LoggerHelper().getInstance()
        self.logger = lh.createLoggerWorker("Notification Manager","DEBUG",2)
        self.logger.info("Instantiated for user: {}".format(self.userID))

        # Appends the users online singleton list
        NotificationManager.__instance.append(self)

    # This function sets a notification for a user
    def addNotification(self, notification,nType,nFrom):

        if nType == 'deterioration':
            for t in self.typeList:
                if t == 'deterioration':
                    return
        time =  datetime.datetime.now()
        timeStamp = time.strftime("%H:%M:%S - ")
        self.indexList.append(self.index)
        self.index+=1
        self.notificationList.append(timeStamp + notification)
        self.typeList.append(nType)
        self.fromList.append(nFrom)

        self.logger.debug("Added Notifcicatio: {}\n {}\n {}\n".format(notification,nType,nFrom))

    # not tested yet
    # This function sends an email to a Doctor.
    def sendDoctorNotification(self,message,doctorEmail):
        try:
            server = smtplib.SMTP(self.EMAIL_HOST,self.EMAIL_PORT)
            server.sendmail(self.FROM,doctorEmail,message)
            server.quit()
        except Exception as e:
            return False

    # This function gets a notification by creating a JSON and returning that JSON back.
    def getUserNotifications(self):

        if len(self.notificationList) == 0:
            return json.dumps("No notifications to display")
        else:
            message = pandas.Series(data = self.notificationList,index=self.indexList,name="message")
            ntype = pandas.Series(data = self.typeList,index=self.indexList,name="type")
            nfrom = pandas.Series(data = self.fromList,index=self.indexList,name="from")

            frame = pandas.concat([message,ntype,nfrom], axis=1)

        if re.search("welcome to PAPA",self.notificationList[0]):
            self.notificationList.remove(self.notificationList[0])
            self.typeList.remove(self.typeList[0])
            self.fromList.remove(self.fromList[0])

        frame.to_json('static/json/notifications.json')
        file = open('static/json/notifications.json','r')
        jsonstring = file.read()
        return jsonstring


from io import StringIO
import unittest
from unittest.mock import patch

from src.utils.cli import ask_yes_or_no

class TestCli(unittest.TestCase):
    @patch('sys.stdout', new_callable=StringIO)
    def test_valid_message_print(self, mock_stdout):
        with patch('builtins.input', return_value='yes'):
            respuesta = ask_yes_or_no('Continue? (yes/no): ')
        self.assertEqual(respuesta, True)
        self.assertEqual(mock_stdout.getvalue(), 'Continue? (yes/no): \n')

    def test_valid_response_yes(self):
        with patch('builtins.input', return_value='yes'):
            respuesta = ask_yes_or_no('Continue? (yes/no): ')
        self.assertEqual(respuesta, True)

    def test_valid_response_no(self):
        with patch('builtins.input', return_value='no'):
            respuesta = ask_yes_or_no('Continue? (yes/no): ')
        self.assertEqual(respuesta, False)

    @patch('sys.stdout', new_callable=StringIO)
    def test_invalid_response(self, mock_stdout):
        with patch('builtins.input', side_effect=['something else', 'yes']):
            respuesta = ask_yes_or_no('a ')
        self.assertEqual(respuesta, True)
        self.assertEqual(mock_stdout.getvalue(), 'a \nPlease enter \'yes\' or \'no\'\na \n')


#5> Write a program to find the sum of digits of a number

class Sumodig:
    def SumOfDigits(self, num):
        sum_digits = lambda number: 0 if number == 0 else (number % 10) + sum_digits (number // 10)
        return sum_digits(num)

a=int(input("Enter the number : "))
tst = Sumodig()
sum1 = tst.SumOfDigits(a)
print("The sum of digits of a number is: ",sum1)


import json
import os
from itertools import chain
from operator import itemgetter

import requests
from pymongo import MongoClient

leagues_file = 'leagues.json'

def get_db():
    client = MongoClient('localhost', 27017)
    return client['fotmob']

class API:
    def __init__(self):
        self.host = 'https://www.fotmob.com/api'

    def get_all_leagues(self):
        response = requests.get(f'{self.host}/allLeagues')
        response.raise_for_status()
        return response.json()

    def get_league(self, league_id):
        response = requests.get(f'{self.host}/leagues', params={'id': league_id})
        response.raise_for_status()
        return response.json()

    def get_fixtures(self, league_id, season):
        response = requests.get(f'{self.host}/fixtures', params={'id': league_id, 'season': season})
        response.raise_for_status()
        return response.json()

    def get_player(self, player_id):
        response = requests.get(f'{self.host}/playerData', params={'id': player_id})
        response.raise_for_status()
        return response.json()

def get_all_leagues():
    if os.path.exists(leagues_file):
        with open(leagues_file) as f:
            return json.load(f)

    leagues = API().get_all_leagues()
    leagues = list(chain(*itemgetter('international', 'countries')(leagues)))
    with open(leagues_file, 'w') as f:
        json.dump(leagues, f, indent=4)

    return leagues

def get_league(league_id):
    ids = list(chain(*[[j['id'] for j in i['leagues']] for i in get_all_leagues()]))
    if league_id in ids:
        table = get_db()['leagues']
        league = table.find_one({'id': league_id})
        if not league:
            league = API().get_league(league_id)
            seasons = league['allAvailableSeasons']
            stats = league['stats'] or {}
            stat_links = stats.get('seasonStatLinks', [])
            league = {'id': league_id, 'seasons': seasons, 'stat_links': stat_links}
            table.insert_one(league)
        return league

def get_totw_rounds(league_id, season):
    league = get_league(league_id)
    if league:
        table = get_db()['totw_rounds']
        query = {'league_id': league_id, 'season': season}
        totw_rounds = table.find_one(query)
        if not totw_rounds:
            totw_info = [i for i in league['stat_links'] if i['Name'] == season]
            if len(totw_info) == 1:
                url = totw_info[0]['TotwRoundsLink']
                response = requests.get(url)
                response.raise_for_status()
                totw_rounds = response.json()
                totw_rounds = {**query, 'rounds': totw_rounds['rounds']}
                table.insert_one(totw_rounds)
        return totw_rounds

def get_totw_teams(league_id, season):
    totw_rounds = get_totw_rounds(league_id, season)['rounds']
    table = get_db()['totw_team']
    query = {'league_id': league_id, 'season': season}
    totw_teams = table.find(query)
    missing_rounds = {i['roundId'] for i in totw_rounds} - {i['round_id'] for i in totw_teams}
    if len(missing_rounds) > 0:
        teams = []
        for i in totw_rounds:
            round_id, link = itemgetter('roundId', 'link')(i)
            if round_id in missing_rounds:
                print(f'Fetching team from {link}')
                team = requests.get(link).json()
                teams.append({**query, 'round_id': round_id, 'players': team['players']})
        table.insert_many(teams)
    return list(table.find(query))

def get_totw_players(league_id, season):
    teams = get_totw_teams(league_id, season)
    players = list(chain(*[[j for j in i['players']] for i in teams]))
    return players


from torch import nn

class Second2Last(object):
    def __call__(self, model: nn.Module):
        l = 0
        layers_dict = {}

        for _ in model.modules():
            l += 1

        for i, layer in enumerate(model.modules()):
            if i == (l - 1):
                layers_dict[layer] = i

        return layers_dict


# Step 1 receive user input, which will be amount of elements in second line.

n = int(input())

# Step 2  create a list to store numbers

temp = list

# Step 3 store next input into list where each element is separated by ' '

temp = input().split(' ')

# Step 4 for each element run if less than 0 and add one to count.

count = 0

for i in range(n):
    a = int(temp[i])
    if a < 0:
        count += 1
        i += 1
    else:
        i += 1

print(count)


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

import sys
import os
import shlex
import re
import subprocess

from os.path import abspath, isfile

# http://docs.readthedocs.io/en/latest/getting_started.html#in-markdown
from recommonmark.parser import CommonMarkParser
source_parsers = { '.md': CommonMarkParser, }

sys.path.insert(0, abspath('.'))
sys.path.insert(0, abspath('_extensions'))

# -- General configuration ------------------------------------------------

needs_sphinx = '1.5'

extensions = [
    # Standard Sphinx extensions
    'sphinx.ext.extlinks',
    'sphinx.ext.intersphinx',
    'sphinx.ext.todo',
    'sphinx.ext.graphviz',
    'sphinx.ext.mathjax',
    'sphinx.ext.ifconfig',
    'sphinx.ext.viewcode',
    # 'sphinx.ext.githubpages',
    # SphinxContrib extensions
    # 'sphinxcontrib.textstyle',
    # 'sphinxcontrib.spelling',
]

source_suffix = ['.rst', '.md']

master_doc = 'index'

project = u'BTD'
copyright = u'2018, Build The Docs'
author = u''

version = "0.0"    # The short X.Y version.
release = version  # The full version, including alpha/beta/rc tags.

language = None

# There are two options for replacing |today|: either, you set today to some non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

exclude_patterns = []

pygments_style = 'stata-dark'

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True
todo_link_only = True

# reST settings
prologPath = "prolog.inc"
try:
    with open(prologPath, "r") as prologFile:
        rst_prolog = prologFile.read()
except Exception as ex:
    print("[ERROR:] While reading '{0!s}'.".format(prologPath))
    print(ex)
    rst_prolog = ""

# -- Options for HTML output ----------------------------------------------

html_theme_path = ["."]
html_theme = "btd.sphinx.theme"

html_context = {
    # Override default css
    # 'css_files': [
    #     '_static/theme_overrides.css',
    # ]
}

html_context = {
    'use_gfonts': True,
    'display_github': True,
    'slug_user': 'buildthedocs',
    'slug_repo': 'btd',
    'slug_path': 'master/doc/',
    'description': 'Build the Docs',
    'current_version': 'master'
}

# Add display_github and VERSIONING data
if isfile('context.json'):
    from json import loads
    data = loads(open('context.json').read())
    html_context.update(data)

html_theme_options = {
    'style_nav_header_background': '#e7e7e7ff',
    'logo_only': True,
    'home_breadcrumbs': False,
    # 'home_logo': False,
    'prevnext_location': 'bottom'
}

# html_title = None # Defaults to "<project> v<release> documentation".
# html_short_title = None
# html_logo = None
# html_favicon = None

html_static_path = ['_static']

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
html_last_updated_fmt = '%b %d, %Y'

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# Output file base name for HTML help builder.
htmlhelp_basename = 'BTDdoc'

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
    'papersize': 'a4paper',
    'pointsize': '10pt',
    # 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto, manual, or own class]).
latex_documents = [
  ('index', 'BTD.tex', u'BTD Documentation', [author], 'manual'),
]

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'btd', u'BTD Documentation', [author], 1)
]

# If true, show URL addresses after external links.
# man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author, dir menu entry, description, category)
texinfo_documents = [
  (master_doc, 'BTD', u'BTD Documentation', author, 'BTD', 'Build The Docs.', 'Miscellaneous'),
]

# -- Sphinx.Ext.ExtLinks --------------------------------------------------

extlinks = {
   'wikipedia': ('https://en.wikipedia.org/wiki/%s', None),
   'btdsharp': ('https://github.com/buildthedocs/btd/issues/%s', '#'),
   'btdissue': ('https://github.com/buildthedocs/btd/issues/%s', 'issue #'),
   'btdpull':  ('https://github.com/buildthedocs/btd/pull/%s', 'pull request #'),
   'btdrepo':   ('https://github.com/buildthedocs/btd/blob/master/%s', None)
}


import importlib

import re

from datetime import datetime

from typing import Any

from xml.etree.ElementTree import Element

import xml.etree.ElementTree as ET

#from gcs_jsonlogger.gcs_logger import GcsLogger

from jsonpath_ng.ext import parse

import json

#from layers.common.error.exceptions import ExceptionHandler

#LOGGER = GcsLogger("common.mapping.mapping_framework")

TARGET_ELEMENT = "targetElement"

TARGET_COLUMN_NAME = "targetColumnName"

TARGET_STRUCT_NAME = "targetStructName"

TARGET_DATA_TYPE = "targetDataType"

NUMBERING_PLAN = "numberingPlan"

PHONE = "phone"

CURRENCY = "currency"

ISO4217CODE = "iso4217Code"

USECOMMAS = "useCommas"

FORMAT = "format"

USD = "USD"

EUR = "EUR"

GBP = "GBP"

SEPARATOR = "separator"

ATTRIBUTES = "attributes"

ARTIFACT = "artifact"

CHILDREN = "children"

MAPPINGS = "mappings"

SOURCE = "source"

ACQUIRED_COLUMN_NAME = "acquiredColumnName"

ACQUIRED_COLUMN_KEY_NAME = "acquiredColumnKeyName"

TAXONOMY = "taxonomy"

PARTIAL_MATCH = "partialMatch"

RECORD = "record"

SPLIT = "split"

CONSTANT = "constantValue"

CONVERTER = "converter"

CONVERTER_FUNCTION = "converterFunction"

CONCAT = "concat"

TAXONOMY_NAME = "taxonomyName"

CUSTOM = "custom"

DATE_CONVERSION = "dateConversion"

ISO_DATE = "isoDate"

ROOT_ELEMENT = "root_element"

JSON_PATH = "jsonPath"

CONFIGURATION = "configuration"

INPUT_FORMAT = "inputFormat"

MULTIPLY = "multiply"

STRING_REPLACE = "stringReplace"

TARGET_COLUMN_STRUCT_NAME = "targetColumnStructName"

ARRAY = "array"

Record_file="XML Automation\\FBN Dallas\\record.json"
Mapping_file="XML Automation\\FBN Dallas\\mapping.json"
records = json.load(open("Record_file"))
#print(records)
mapping = json.load(open("Mapping_file"))

class MappingFramework:

    @staticmethod

    #@ExceptionHandler.error_handler

    def handle_mapping(record: Any, mapping_dict: dict, taxonomies: dict) -> dict:

        response = {}

        target_record = {}

        #LOGGER.info(f'handle_mapping: record={record}')

        array_of_structs = []

        root_element = MappingFramework.create_root_element(mapping_dict.get(ROOT_ELEMENT))

        config = MappingFramework.get_configuration(mapping_dict)

        input_format = MappingFramework.get_config_value(config, INPUT_FORMAT)

        # If the input data is xml, we want to convert it to an ElementTree once

        # if input_format == "xml":

        #     xml_record: ElementTree = ET.ElementTree(ET.fromstring(record))

        #     record = xml_record

        mappings = mapping_dict.get(MAPPINGS)

        for mapping in mappings:

            if TARGET_ELEMENT in mapping:

                target_element_name = mapping[TARGET_ELEMENT]

                if ATTRIBUTES in mapping:

                    target_element_attr = MappingFramework.handle_iso_date_conversion(record, mapping[ATTRIBUTES])

                    target_element_attr = target_element_attr if target_element_attr else mapping[ATTRIBUTES]

                    target_element = Element(target_element_name, target_element_attr)

                else:

                    target_element = Element(target_element_name)

                if SOURCE in mapping:

                    print(target_element)

                    MappingFramework.handle_source(mapping[SOURCE], target_element, record, taxonomies)

                    target_element_value = MappingFramework.handle_data_conversion(target_element.text,

                                                                                mapping.get(TARGET_DATA_TYPE))

                    if FORMAT in mapping:

                        target_element_value = MappingFramework.format_data(target_element_value, mapping[FORMAT])

                    if target_element.text is not None and len(target_element.text) > 0:

                        target_element.text = str(target_element_value)

                        root_element.append(target_element)

                    if TARGET_COLUMN_NAME in mapping:

                        target_column_name = mapping[TARGET_COLUMN_NAME]

                        target_record[target_column_name] = target_element_value

                if CHILDREN in mapping:

                    if ARRAY == mapping.get(TARGET_DATA_TYPE):

                        if TARGET_COLUMN_STRUCT_NAME in mapping:

                            struct_dict = {mapping.get(TARGET_COLUMN_STRUCT_NAME): {}}

                        MappingFramework.handle_array(mapping.get(CHILDREN), target_element, record, taxonomies,

                                                      target_record, array_of_structs, mapping[TARGET_COLUMN_NAME], struct_dict, False)

                        array_of_structs.append(struct_dict)

                        if struct_dict.values():

                            target_record[mapping[TARGET_COLUMN_NAME]] = array_of_structs

                    else:

                        MappingFramework.handle_children(mapping.get(CHILDREN), target_element, record, taxonomies,

                                                         target_record)

                    if MappingFramework.has_children(target_element):

                        root_element.append(target_element)

        response[ARTIFACT] = root_element

        response[RECORD] = target_record

        return response

    @staticmethod

    def handle_data_conversion(value, _format):

        if not _format:

            return value

        if not value:

            return value

        try:

            if _format == 'int':

                return int(value)

            elif 'datetime' in _format:

                try:

                    _format = _format.split('~')[1]

                    if len(value) > 10:

                        return datetime.strptime(value, _format)

                    else:

                        formatted_date = datetime.strptime(value, _format)

                        return formatted_date.date()

                except ValueError as ve:

                    return value

            return value

        except Exception as ex:

            raise ex

    @staticmethod

    def format_data(value, format_dict):

        if 'phone' in format_dict:

            return MappingFramework.handle_phone_format(value, format_dict[PHONE][NUMBERING_PLAN],

                                                        format_dict[PHONE][SEPARATOR])

        elif 'currency' in format_dict:

            return MappingFramework.handle_currency_format(value, format_dict[CURRENCY][ISO4217CODE],

                                                           format_dict[CURRENCY][USECOMMAS])

    @staticmethod

    def handle_phone_format(phone_number, _format, split_char=' '):

        try:

            ph = re.sub('[^0-9]+', '', phone_number)

            formatted_phone_number = re.sub("(\d)(?=(\d{3})+(?!\d))", fr"\1{split_char}", "%s" % ph[:-1]) + ph[-1]

            return formatted_phone_number if _format == 'NANP' else '+' + formatted_phone_number.replace(split_char,

                                                                                ' ', 1)

        except Exception as ex:

            raise ex

    @staticmethod

    def handle_currency_format(currency_amount, _format, use_commas=True):

        try:

            if _format == USD:

                _format = "$"

            elif _format == EUR:

                _format = "€"

            elif _format == GBP:

                _format = "£"

            else:

                _format = ""

            currency_amount, decimal_count = (float(currency_amount), 2) if '.' in str(currency_amount) else (

                int(currency_amount), 0)

            prefix = '-' if currency_amount < 0 else ''

            comma = ',' if use_commas else ''

            formatted_amount = '%s%s%s' % (

                prefix, _format, '{:{}.{}f}'.format(abs(currency_amount), comma, decimal_count))

            return formatted_amount

        except Exception as ex:

            raise ex

    @staticmethod

    #@ExceptionHandler.error_handler

    def get_configuration(mapping: dict):

        if "configuration" in mapping:

            return mapping.get("configuration")

        return None

    @staticmethod

    def get_config_value(config: dict, key: str):

        if config is not None:

            if key in config:

                return str(config.get(key)).lower()

        return None

    @staticmethod

    #@ExceptionHandler.error_handler

    def handle_children(children, parent_element: Element, record: dict, taxonomies: dict, target_record: dict):

        for child in children:

            if TARGET_ELEMENT in child:

                target_element_name = child[TARGET_ELEMENT]

                if ATTRIBUTES in child:

                    target_element_attr = MappingFramework.handle_iso_date_conversion(record, child[ATTRIBUTES])

                    target_element_attr = target_element_attr if target_element_attr else child[ATTRIBUTES]

                    target_element = Element(target_element_name, target_element_attr)

                else:

                    target_element = Element(target_element_name)

                if SOURCE in child:

                    # has_source = True

                    MappingFramework.handle_source(child[SOURCE], target_element, record, taxonomies)

                    target_element_value = MappingFramework.handle_data_conversion(target_element.text,

                                                                                child.get(TARGET_DATA_TYPE))

                    if FORMAT in child:

                        target_element_value = MappingFramework.format_data(target_element_value, child[FORMAT])

                    if target_element.text is not None and len(target_element.text) > 0:

                        target_element.text = str(target_element_value)

                        parent_element.append(target_element)

                        # has_children = True

                    if TARGET_COLUMN_NAME in child:

                        target_column_name = child[TARGET_COLUMN_NAME]

                        target_record[target_column_name] = target_element_value

                if CHILDREN in child:

                    MappingFramework.handle_children(child[CHILDREN], target_element, record, taxonomies,

                                                     target_record)

                    if MappingFramework.has_children(target_element):

                        parent_element.append(target_element)

    @staticmethod

   # @ExceptionHandler.error_handler

    def handle_array(children, parent_element: Element, record: dict, taxonomies: dict, target_record: dict, array: list, target_column_name, struct_dict: dict, nested_children: bool):

        for child in children:

            if TARGET_ELEMENT in child:

                target_element_name = child[TARGET_ELEMENT]

                if ATTRIBUTES in child:

                    target_element_attr = MappingFramework.handle_iso_date_conversion(record, child[ATTRIBUTES])

                    target_element_attr = target_element_attr if target_element_attr else child[ATTRIBUTES]

                    target_element = Element(target_element_name, target_element_attr)

                else:

                    target_element = Element(target_element_name)

                if TARGET_COLUMN_STRUCT_NAME in child:

                    if child[TARGET_COLUMN_STRUCT_NAME] not in struct_dict:

                        child_struct = {child[TARGET_COLUMN_STRUCT_NAME]: {}}

                        struct_dict[parent_element.tag].update(child_struct)

                if SOURCE in child:

                    # has_source = True

                    MappingFramework.handle_source(child[SOURCE], target_element, record, taxonomies)

                    target_element_value = MappingFramework.handle_data_conversion(target_element.text,

                                                                                child.get(TARGET_DATA_TYPE))

                    if FORMAT in child:

                        target_element_value = MappingFramework.format_data(target_element_value, child[FORMAT])

                    if target_element.text is not None and len(target_element.text) > 0:

                        target_element.text = str(target_element_value)

                        parent_element.append(target_element)

                        # has_children = True

                    if TARGET_STRUCT_NAME in child:

                        target_struct_name = child[TARGET_STRUCT_NAME]

                        if target_element_value:

                            key = list(struct_dict.keys())[0]

                            # if key == parent_element.tag:

                            if key == parent_element.tag or not nested_children:

                                struct_dict[key][target_struct_name] = target_element_value

                            else:

                                struct_dict[key][parent_element.tag][target_struct_name] = target_element_value

                                # struct_dict['individual']['person'][target_struct_name] = target_element_value

                if CHILDREN in child:

                    nested_children = True

                    MappingFramework.handle_array(child[CHILDREN], target_element, record, taxonomies, target_record, array, target_column_name, struct_dict, nested_children)

                    if MappingFramework.has_children(target_element):

                        parent_element.append(target_element)

        # if struct_dict:

           # target_record[target_column_name] = array

    @staticmethod

    #@ExceptionHandler.error_handler

    def has_children(element: Element) -> bool:

        if len(element.findall('*')) > 0:

            return True

        else:

            return False

    @staticmethod

    #@ExceptionHandler.error_handler

    def handle_source(source_dict, element: Element, record: dict, taxonomies: dict):

        if ACQUIRED_COLUMN_NAME in source_dict:

            MappingFramework.handle_acquired_column_name(record, element, source_dict[ACQUIRED_COLUMN_NAME])

        elif TAXONOMY in source_dict:

            MappingFramework.handle_taxonomy(source_dict[TAXONOMY], element, taxonomies, record)

        elif SPLIT in source_dict:

            MappingFramework.handle_split(record, element, source_dict[SPLIT])

        elif CONVERTER in source_dict:

            MappingFramework.handle_converter(record, element, source_dict[CONVERTER])

        elif CONCAT in source_dict:

            MappingFramework.handle_concat(record, element, source_dict[CONCAT], taxonomies)

        elif CONSTANT in source_dict:

            MappingFramework.handle_constant(element, source_dict[CONSTANT])

        elif CUSTOM in source_dict:

            MappingFramework.handle_custom_converter(record, element, source_dict[CUSTOM], taxonomies)

        elif JSON_PATH in source_dict:

            print(element)

            MappingFramework.handle_json_path_expression(record, element, source_dict[JSON_PATH])

        elif MULTIPLY in source_dict:

            MappingFramework.handle_multiply(record, element, source_dict[MULTIPLY])

        elif STRING_REPLACE in source_dict:

            MappingFramework.handle_string_replace(record, element, source_dict[STRING_REPLACE])

    @staticmethod

    def handle_constant(element: Element, constant: str):

        element.text = constant

    @staticmethod

    def handle_taxonomy(taxonomy_info: dict, element, taxonomies: dict, record: dict):

        element.text = MappingFramework.get_taxonomy_value(taxonomy_info, taxonomies, record)

    @staticmethod

   # @ExceptionHandler.error_handler

    def get_taxonomy_value(taxonomy_info: dict, taxonomies: dict, record: dict) -> str:

        taxonomy_value = ''

        taxonomy_table_name = taxonomy_info[TAXONOMY_NAME]

        taxonomy_key_column_name = taxonomy_info[ACQUIRED_COLUMN_KEY_NAME]

        if taxonomy_key_column_name in record:

            if PARTIAL_MATCH in taxonomy_info:

                for key in taxonomies[taxonomy_table_name]:

                    if record[taxonomy_key_column_name].find(key) > 0:

                        taxonomy_value = taxonomies[taxonomy_table_name].get(key)

            else:

                taxonomy_value = taxonomies[taxonomy_table_name].get(str(record[taxonomy_key_column_name]).strip())

        return taxonomy_value

    @staticmethod

    #@ExceptionHandler.error_handler

    def handle_split(record: dict, element: Element, split: dict):

        key_column_name = split[ACQUIRED_COLUMN_NAME]

        split_char = split["splitChar"]

        index_pos = split["indexPos"]

        if key_column_name in record:

            element.text = MappingFramework.split(record[key_column_name], split_char, index_pos)

    @staticmethod

   # @ExceptionHandler.error_handler

    def split(value: str, separator: chr, index: int):

        if value is not None and separator in value:

            split_list = value.split(separator)

            if index < len(split_list):

                return value.split(separator)[index]

        else:

            return ''

    @staticmethod

   #@ExceptionHandler.error_handler

    def handle_concat(record: dict, element: Element, concat_config: dict, taxonomies: dict):

        concat_value = ''

        for concat_item in concat_config:

            if TAXONOMY in concat_item:

                concat_value = concat_value + MappingFramework.get_taxonomy_value(concat_item[TAXONOMY], taxonomies,

                                                                                record)

            elif ACQUIRED_COLUMN_NAME in concat_item:

                concat_value = concat_value + MappingFramework.get_acquired_column_value(record, concat_item[

                    ACQUIRED_COLUMN_NAME])

        element.text = concat_value

    @staticmethod

    #@ExceptionHandler.error_handler

    def handle_converter(record: dict, parent_element: Element, converter: dict):

        acquired_column_name = converter[ACQUIRED_COLUMN_NAME]

        converter_function = converter[CONVERTER_FUNCTION]

        if converter_function == "yesno" and acquired_column_name in record:

            parent_element.text = MappingFramework.yesno_converter(str(record[acquired_column_name]))

        elif converter_function == "streetCleanup" and acquired_column_name in record:

            parent_element.text = MappingFramework.street_converter(str(record[acquired_column_name]))

    @staticmethod

    #@ExceptionHandler.error_handler

    def yesno_converter(status: str) -> str:

        temp_status = status.lower().strip()

        if temp_status == 'y':

            status = 'YES'

        elif temp_status == 'n':

            status = 'NO'

        else:

            status = 'NO'

        return status

    @staticmethod

   # @ExceptionHandler.error_handler

    def street_converter(street: str) -> str:

        # add spaces around # and -

        street = re.sub(r"(#)", r" \1 ", street)

        # add spaces between certain dwelling types and alpha-numberic

        street = re.sub(r"\b(APT|UNIT|BSMT|BLDG?|DEPT|FRNT|HNGR|LBBY|LOT|LOWR|OFFICE|OFC|PIER|ROOM|RM|SPC|STOP|SUITE|TRLR|UPPR|SIDE|SLIP|REAR|KEY|STE|FL|PH|HWY)\s?(\W*|\w*)", r"\1 \2", street, 2)

        # cleanup multiple spaces added by previous regex's

        street = re.sub(' +', ' ', street).rstrip()

        return street

    @staticmethod

    #@ExceptionHandler.error_handler

    def handle_acquired_column_name(record: dict, element: Element, acquired_column_name):

        if acquired_column_name in record:

            element.text = MappingFramework.get_acquired_column_value(record, acquired_column_name)

    @staticmethod

   # @ExceptionHandler.error_handler

    def handle_custom_converter(record: dict, element: Element, custom_config: dict, taxonomies: dict):

        module_name = custom_config['moduleName']

        class_name = custom_config['className']

        method_name = custom_config['methodName']

        custom_data = custom_config.get('customData')

        element.text = MappingFramework.call_custom_method(module_name, method_name, class_name, record, element,

                                                           taxonomies, custom_data)

    @staticmethod

  #  @ExceptionHandler.error_handler

    def call_custom_method(module_name, method_name, class_name, record: dict, element: Element, taxonomies: dict,

                           custom_data: str):

        module = importlib.import_module(module_name)

        clazz = getattr(module, class_name)

        func = getattr(clazz, method_name)

        if custom_data:

            record['custom_config_data'] = custom_data

        return func(record, element, taxonomies)

    @staticmethod

   # @ExceptionHandler.error_handler

    def get_acquired_column_value(record, acquired_column_name):

        if acquired_column_name in record:

            return MappingFramework.handle_zero_value(str(record[acquired_column_name]).strip())

        else:

            return ''

    @staticmethod

    def handle_zero_value(value):

        if value == '0':

            return None

        else:

            return value

    @staticmethod

   # @ExceptionHandler.error_handler

    def handle_date_conversion(value):

        iso_date = datetime.strptime(value, '%m/%d/%Y').date()

        return iso_date

    @staticmethod

  #  @ExceptionHandler.error_handler

    def create_root_element(config: dict):

        root = Element("artifact")

        root.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')

        # need to make these as parameter in the configuration

        for k, v in config.items():

            root.set(k, v)

        return root

    @staticmethod

  #  @ExceptionHandler.error_handler

    def handle_json_path_expression(record: dict, element, json_path):

        json_path_list = []

        try:

            if isinstance(json_path, str):

                json_path_list.append(json_path)

            elif isinstance(json_path, list):

                json_path_list = json_path

            for path in json_path_list:

                jsonpath_expr = parse(path)

                get_match = jsonpath_expr.find(record)

                if len(get_match) > 0:

                    if get_match[0].value and get_match[0].value != 'None':

                        element.text = str(get_match[0].value)

                        break

                # else:

                #     element.text = None

        except Exception:

            #LOGGER.exception(f'Failed to convert record using json expression {json_path}', exc_info=True)

            element.text = None

            raise

    @staticmethod

  #.error_handler

    def handle_multiply(record: dict, element: Element, multiply: dict):

        acquired_column_name = multiply[ACQUIRED_COLUMN_NAME]

        if acquired_column_name in record:

            value = int(record[acquired_column_name])

            factor = int(multiply[CONSTANT])

            element.text = str(value * factor)

    @staticmethod

  #  @ExceptionHandler.error_handler

    def handle_string_replace(record: dict, element: Element, replace_string: dict):

        acquired_column_name = replace_string[ACQUIRED_COLUMN_NAME]

        if acquired_column_name in record:

            element.text = str(record[acquired_column_name].replace(

                replace_string['searchString'],

                replace_string['replaceString'])).strip()

    @staticmethod

   #.error_handler

    def handle_iso_date_conversion(record: dict, attr):

        if DATE_CONVERSION in attr:

            if ISO_DATE in attr[DATE_CONVERSION]:

                source_date = record[attr[DATE_CONVERSION][ISO_DATE]]

                conv_iso_date = MappingFramework.handle_date_conversion(source_date)

                return {ISO_DATE: str(conv_iso_date).replace('-', '')}

artifact = MappingFramework.handle_mapping(records,mapping,{})["artifact"]

print(artifact)

tree = ET.ElementTree(artifact)

tree.write('output1.xml')


M = int(input())
N = int(input())
List = []
cnt = 0
if M == 1 :
    M = 2

for num in range(M,N+1):

    for k in range(2,num):
        if num % k == 0 :
            cnt += 1

    if cnt == 0 :
        List.append(num)
    cnt = 0

if List == [] :
    print(-1)
else:

    print(sum(List))
    print(min(List))


from asyncio import Queue

async def async_generator_from_queue(queue: Queue, stop_value=None):
    while True:
        value = await queue.get()

        if value is stop_value:
            return
        else:
            yield value
            queue.task_done()


# Board games in store
board_games = {"Ticket to Ride", "Catan",
               "7 Wonders", "Articulate!", "Azul"}

# Card Games in store
card_games = {"Monikers", "Scrimish", "Coup",
              "Death on the Cards", "Skyjo", "Duet"}

def add_board_game(game):
    if game in board_games:
        print("This game is already registered")
    else:
        board_games.add(game)
        print(game + " was added to the collection")

def add_card_game(game):
    if game in card_games:
        print("This game is already registered")
    else:
        card_games.add(game)
        print(game + " was added to the collection")

def list_all_games():
    all_games = board_games.union(card_games)
    for game in all_games:
        print(game)

new_board_game = input("Enter new board game name: ")
add_board_game(new_board_game)

new_card_game = input("Enter new card game name: ")
add_card_game(new_card_game)

list_all_games()


import os
import time
import requests
import bs4
from urllib.parse import urljoin
import pymongo
from dotenv import load_dotenv
from datetime import datetime
import locale
locale.setlocale(locale.LC_ALL, 'ru_RU')

class ParseError(Exception):
    def __init__(self, text):
        self.text = text

class MagnitParser:
    _headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15'
    }
    _params = {
        'geo': 'moskva'
    }
    _cookies = {'mg_geo_id': '2398'}

    def __init__(self, start_url, data_client):
        self.start_url = start_url
        self.data_client = data_client
        self.data_base = self.data_client["magnit_parse_db"]

    @staticmethod
    def _get_response(url, *args, **kwargs):
        while True:
            try:
                response = requests.get(url, *args, **kwargs)
                if response.status_code > 399:
                    raise ParseError(response.status_code)
                time.sleep(0.1)
                return response
            except (requests.RequestException, ParseError):
                time.sleep(0.5)
                continue

    @staticmethod
    def _get_soup(response):
        return bs4.BeautifulSoup(response.text, 'lxml')

    def run(self):
        for product in self.parse(self.start_url):
            self.save(product)

    def parse(self, url) -> dict:
        resp = self._get_response(url, params=self._params, headers=self._headers, cookies=self._cookies)
        print(resp)
        soup = self._get_soup(resp)
        # print(soup)
        catalog_main = soup.find('div', attrs={'class': 'сatalogue__main'})
        # print(catalog_main)
        for product_tag in catalog_main.find_all('a', attrs={'class': 'card-sale'}):
            yield self._get_product_data(product_tag)

    @staticmethod
    def get_price(product_tag, attr: str) -> float or None:
       try:
           price: list = (product_tag.find('div', attrs={'class' : attr}).text)[1 : -1].split('\n')
           return float('.'.join(price))
       except:
           return None

    @staticmethod
    def get_date(product_tag, binary: bin) -> datetime or None:
        try:
            date = product_tag.find('div', class_='card-sale__date').findChildren()[binary].text[2:].strip()
            return datetime.strptime(f"{date} {datetime.now().year}", "%d %B %Y")
        except:
            return None

    @property
    def data_template(self) -> dict:
        return {
            'url': lambda tag: urljoin(self.start_url, tag.attrs.get('href')),
            'promo_name':lambda tag: tag.find('div', attrs={'class' : 'card-sale__header'}).text,
            'product_name': lambda tag: tag.find('div', attrs={'class': 'card-sale__title'}).text,
            'old_price': lambda tag: self.get_price(tag, 'label__price_old'),
            'new_price': lambda tag: self.get_price(tag, 'label__price_new'),
            'image_url': lambda tag: urljoin(self.start_url, tag.find('source').attrs.get('data-srcset')),
            'date_from': lambda tag: self.get_date(tag, 0),
            'date_to': lambda tag: self.get_date(tag, 1)
        }

    def _get_product_data(self, product_tag: bs4.Tag) -> dict:
        data = {}
        for key, pattern in self.data_template.items():
            try:
                data[key] = pattern(product_tag)
            except AttributeError:
                pass
        return data

    def save(self, data):
        print(data)
        collection = self.data_base["magnit"]
        collection.insert_one(data)
        pass

if __name__ == '__main__':
    load_dotenv(".env")
    data_base_url = os.getenv('DATA_BASE_URL')
    data_client = pymongo.MongoClient(data_base_url)
    url = 'https://magnit.ru/promo/'
    parser = MagnitParser(url, data_client)
    parser.run()


'''
use pnn to predict the real values of states
using uncetainty
the input length of Bayes is 10 (6 is worse than 10 and 8)
For planning, the node pruning method is employed in the decision-making tree

'''
import argparse
import numpy as np
import fixed_env as env
import load_trace
import matplotlib.pyplot as plt
import itertools
# import bandwidth_pred as bp
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.utils.data as Data
from torch.autograd import Variable
# import test as bw_pre
# import mcts_abr_pnn as mcts
import pandas
from collections import Counter
# import lstm_net as net

S_INFO = 5  # bit_rate, buffer_size, rebuffering_time, bandwidth_measurement, chunk_til_video_end
S_LEN = 10  # take how many frames in the past
A_DIM = 6
MPC_FUTURE_CHUNK_COUNT = 3
SIMULATION_NUM = 8
ACTOR_LR_RATE = 0.0001
CRITIC_LR_RATE = 0.001
VIDEO_BIT_RATE = [300,750,1200,1850,2850,4300]  # Kbps
BITRATE_REWARD = [1, 2, 3, 12, 15, 20]
BUFFER_NORM_FACTOR = 10.0
CHUNK_TIL_VIDEO_END_CAP = 48.0
TOTAL_VIDEO_CHUNKS = 48
M_IN_K = 1000.0
# REBUF_PENALTY = 4.3  # 1 sec rebuffering -> 3 Mbps
SMOOTH_PENALTY = 1
DEFAULT_QUALITY = 1  # default video quality without agent
RANDOM_SEED = 42
RAND_RANGE = 1000000

VIDEO_SIZE_FILE = './video_size/ori/video_size_'
# log in format of time_stamp bit_rate buffer_size rebuffer_time chunk_size download_time reward
# NN_MODEL = './models/nn_model_ep_5900.ckpt'

TIME_STEP = 8
# TARGET_SIZE = 5
INPUT_SIZE = 1
HIDDEN_SIZE = 128

BATCH_SIZE = 1
LR = 0.001
EPOCH = 20

CHUNK_COMBO_OPTIONS = []

parser = argparse.ArgumentParser(description='BayesMPC')
parser.add_argument('--lin', action='store_true', help='QoE_lin metric')
parser.add_argument('--log', action='store_true', help='QoE_log metric')
parser.add_argument('--FCC', action='store_true', help='Test in FCC dataset')
parser.add_argument('--HSDPA', action='store_true', help='Test in HSDPA dataset')
parser.add_argument('--Oboe', action='store_true', help='Test in Oboe dataset')
parser.add_argument('--cb', action='store_true', help='Compare the lower bound')

# past errors in bandwidth
past_errors = []
past_bandwidth_ests = []

#size_video1 = [3155849, 2641256, 2410258, 2956927, 2593984, 2387850, 2554662, 2964172, 2541127, 2553367, 2641109, 2876576, 2493400, 2872793, 2304791, 2855882, 2887892, 2474922, 2828949, 2510656, 2544304, 2640123, 2737436, 2559198, 2628069, 2626736, 2809466, 2334075, 2775360, 2910246, 2486226, 2721821, 2481034, 3049381, 2589002, 2551718, 2396078, 2869088, 2589488, 2596763, 2462482, 2755802, 2673179, 2846248, 2644274, 2760316, 2310848, 2647013, 1653424]
# size_video1 = [2354772, 2123065, 2177073, 2160877, 2233056, 1941625, 2157535, 2290172, 2055469, 2169201, 2173522, 2102452, 2209463, 2275376, 2005399, 2152483, 2289689, 2059512, 2220726, 2156729, 2039773, 2176469, 2221506, 2044075, 2186790, 2105231, 2395588, 1972048, 2134614, 2164140, 2113193, 2147852, 2191074, 2286761, 2307787, 2143948, 1919781, 2147467, 2133870, 2146120, 2108491, 2184571, 2121928, 2219102, 2124950, 2246506, 1961140, 2155012, 1433658]
# size_video2 = [1728879, 1431809, 1300868, 1520281, 1472558, 1224260, 1388403, 1638769, 1348011, 1429765, 1354548, 1519951, 1422919, 1578343, 1231445, 1471065, 1491626, 1358801, 1537156, 1336050, 1415116, 1468126, 1505760, 1323990, 1383735, 1480464, 1547572, 1141971, 1498470, 1561263, 1341201, 1497683, 1358081, 1587293, 1492672, 1439896, 1139291, 1499009, 1427478, 1402287, 1339500, 1527299, 1343002, 1587250, 1464921, 1483527, 1231456, 1364537, 889412]
# size_video3 = [1034108, 957685, 877771, 933276, 996749, 801058, 905515, 1060487, 852833, 913888, 939819, 917428, 946851, 1036454, 821631, 923170, 966699, 885714, 987708, 923755, 891604, 955231, 968026, 874175, 897976, 905935, 1076599, 758197, 972798, 975811, 873429, 954453, 885062, 1035329, 1026056, 943942, 728962, 938587, 908665, 930577, 858450, 1025005, 886255, 973972, 958994, 982064, 830730, 846370, 598850]
# size_video4 = [668286, 611087, 571051, 617681, 652874, 520315, 561791, 709534, 584846, 560821, 607410, 594078, 624282, 687371, 526950, 587876, 617242, 581493, 639204, 586839, 601738, 616206, 656471, 536667, 587236, 590335, 696376, 487160, 622896, 641447, 570392, 620283, 584349, 670129, 690253, 598727, 487812, 575591, 605884, 587506, 566904, 641452, 599477, 634861, 630203, 638661, 538612, 550906, 391450]
# size_video5 = [450283, 398865, 350812, 382355, 411561, 318564, 352642, 437162, 374758, 362795, 353220, 405134, 386351, 434409, 337059, 366214, 360831, 372963, 405596, 350713, 386472, 399894, 401853, 343800, 359903, 379700, 425781, 277716, 400396, 400508, 358218, 400322, 369834, 412837, 401088, 365161, 321064, 361565, 378327, 390680, 345516, 384505, 372093, 438281, 398987, 393804, 331053, 314107, 255954]
# size_video6 = [181801, 155580, 139857, 155432, 163442, 126289, 153295, 173849, 150710, 139105, 141840, 156148, 160746, 179801, 140051, 138313, 143509, 150616, 165384, 140881, 157671, 157812, 163927, 137654, 146754, 153938, 181901, 111155, 153605, 149029, 157421, 157488, 143881, 163444, 179328, 159914, 131610, 124011, 144254, 149991, 147968, 161857, 145210, 172312, 167025, 160064, 137507, 118421, 112270]

# def create_DataTensor(dataset, window):
#     # Input: dataset, flattened numpy array
#     # Output: Tensor_x [batch, time_step, features]
#     #         Tensor_y [batch, target_size, features]
#     # Each dataset has one output
#     data_x, data_y = [], []
#     for i in range(window, len(dataset)):
#         batch_x = dataset[i - window: i]
#         batch_y = dataset[i]
#         data_x.append(batch_x[:, np.newaxis])
#         data_y.append(np.array([[batch_y]]))

#     data_x, data_y = np.asarray(data_x), np.asarray(data_y)

#     Tensor_x = torch.from_numpy(data_x)
#     Tensor_y = torch.from_numpy(data_y)
#     return Tensor_x, Tensor_y

# torch.cuda.device(0)
# torch.cuda.get_device_name(torch.cuda.current_device())

def to_variable(var=(), cuda=True, volatile=False):
    out = []
    for v in var:

        if isinstance(v, np.ndarray):
            v = torch.from_numpy(v).type(torch.FloatTensor)

        if not v.is_cuda and cuda:
            v = v.cuda()

        if not isinstance(v, Variable):
            v = Variable(v, volatile=volatile)

        out.append(v)
    return out

def log_gaussian_loss(output, target, sigma, no_dim, sum_reduce=True):
    exponent = -0.5*(target - output)**2/sigma**2
    log_coeff = -no_dim*torch.log(sigma) - 0.5*no_dim*np.log(2*np.pi)

    if sum_reduce:
        return -(log_coeff + exponent).sum()
    else:
        return -(log_coeff + exponent)

def get_kl_divergence(weights, prior, varpost):
    prior_loglik = prior.loglik(weights)

    varpost_loglik = varpost.loglik(weights)
    varpost_lik = varpost_loglik.exp()

    return (varpost_lik*(varpost_loglik - prior_loglik)).sum()

class gaussian:
    def __init__(self, mu, sigma):
        self.mu = mu
        self.sigma = sigma

    def loglik(self, weights): # log Guassian value
        exponent = -0.5*(weights - self.mu)**2/self.sigma**2
        log_coeff = -0.5*(np.log(2*np.pi) + 2*np.log(self.sigma))

        return (exponent + log_coeff).sum()

class BayesLinear_Normalq(nn.Module):
    def __init__(self, input_dim, output_dim, prior):
        super(BayesLinear_Normalq, self).__init__()
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.prior = prior

        # scale = (2/self.input_dim)**0.5
        # rho_init = np.log(np.exp((2/self.input_dim)**0.5) - 1)
        self.weight_mus = nn.Parameter(torch.Tensor(self.input_dim, self.output_dim).uniform_(-0.01, 0.01))
        self.weight_rhos = nn.Parameter(torch.Tensor(self.input_dim, self.output_dim).uniform_(-3, -3))

        self.bias_mus = nn.Parameter(torch.Tensor(self.output_dim).uniform_(-0.01, 0.01))
        self.bias_rhos = nn.Parameter(torch.Tensor(self.output_dim).uniform_(-4, -3))

    def forward(self, x, sample = True):

        if sample:
            # sample gaussian noise for each weight and each bias
            weight_epsilons = Variable(self.weight_mus.data.new(self.weight_mus.size()).normal_())
            bias_epsilons =  Variable(self.bias_mus.data.new(self.bias_mus.size()).normal_())

            # calculate the weight and bias stds from the rho parameters
            weight_stds = torch.log(1 + torch.exp(self.weight_rhos))
            bias_stds = torch.log(1 + torch.exp(self.bias_rhos))

            # calculate samples from the posterior from the sampled noise and mus/stds
            weight_sample = self.weight_mus + weight_epsilons*weight_stds
            bias_sample = self.bias_mus + bias_epsilons*bias_stds

            output = torch.mm(x, weight_sample) + bias_sample ## Performs a matrix multiplication of the matrices input and mat2.

            # computing the KL loss term
            prior_cov, varpost_cov = self.prior.sigma**2, weight_stds**2
            KL_loss = 0.5*(torch.log(prior_cov/varpost_cov)).sum() - 0.5*weight_stds.numel()
            KL_loss = KL_loss + 0.5*(varpost_cov/prior_cov).sum()
            KL_loss = KL_loss + 0.5*((self.weight_mus - self.prior.mu)**2/prior_cov).sum()

            prior_cov, varpost_cov = self.prior.sigma**2, bias_stds**2
            KL_loss = KL_loss + 0.5*(torch.log(prior_cov/varpost_cov)).sum() - 0.5*bias_stds.numel()
            KL_loss = KL_loss + 0.5*(varpost_cov/prior_cov).sum()
            KL_loss = KL_loss + 0.5*((self.bias_mus - self.prior.mu)**2/prior_cov).sum()

            return output, KL_loss

        else:
            output = torch.mm(x, self.weight_mus) + self.bias_mus
            return output, KL_loss

    def sample_layer(self, no_samples):
        all_samples = []
        for i in range(no_samples):
            # sample gaussian noise for each weight and each bias
            weight_epsilons = Variable(self.weight_mus.data.new(self.weight_mus.size()).normal_())

            # calculate the weight and bias stds from the rho parameters
            weight_stds = torch.log(1 + torch.exp(self.weight_rhos))

            # calculate samples from the posterior from the sampled noise and mus/stds
            weight_sample = self.weight_mus + weight_epsilons*weight_stds

            all_samples += weight_sample.view(-1).cpu().data.numpy().tolist()

        return all_samples

class BBP_Heteroscedastic_Model_Wrapper:
    def __init__(self, network, learn_rate, batch_size, no_batches):

        self.learn_rate = learn_rate
        self.batch_size = batch_size
        self.no_batches = no_batches

        self.network = network
        self.network.cuda()

        self.optimizer = torch.optim.Adam(self.network.parameters(), lr = self.learn_rate)
        self.loss_func = log_gaussian_loss

    def fit(self, x, y, no_samples):
        x, y = to_variable(var=(x, y), cuda=True)

        # reset gradient and total loss
        self.optimizer.zero_grad()
        fit_loss_total = 0

        for i in range(no_samples):
            output, KL_loss_total = self.network(x)

            # calculate fit loss based on mean and standard deviation of output
            fit_loss = self.loss_func(output[:, :1], y, output[:, 1:].exp(), 1) ## sigma.exp() make sure the sigma > 0
            fit_loss_total = fit_loss_total + fit_loss

        KL_loss_total = KL_loss_total/self.no_batches
        total_loss = (fit_loss_total + KL_loss_total)/(no_samples*x.shape[0])
        total_loss.backward()
        self.optimizer.step()

        return fit_loss_total/no_samples, KL_loss_total

    def get_loss_and_rmse(self, x, y, no_samples):
        x, y = to_variable(var=(x, y), cuda=True)

        means, stds = [], []
        for i in range(no_samples):
            output, KL_loss_total = self.network(x)
            means.append(output[:, :1, None])
            stds.append(output[:, 1:, None].exp())

        means, stds = torch.cat(means, 2), torch.cat(stds, 2)
        mean = means.mean(dim=2)
        std = (means.var(dim=2) + stds.mean(dim=2)**2)**0.5

        # calculate fit loss based on mean and standard deviation of output
        logliks = self.loss_func(output[:, :1], y, output[:, 1:].exp(), 1, sum_reduce=False)
        rmse = float((((mean - y)**2).mean()**0.5).cpu().data)

        return logliks, rmse

class BBP_Heteroscedastic_Model_UCI(nn.Module):
    def __init__(self, input_dim, output_dim, num_units):
        super(BBP_Heteroscedastic_Model_UCI, self).__init__()

        self.input_dim = input_dim
        self.output_dim = output_dim

        # network with two hidden and one output layer
        self.layer1 = BayesLinear_Normalq(input_dim, num_units, gaussian(0, 1))
        self.layer2 = BayesLinear_Normalq(num_units, num_units, gaussian(0, 1))
        self.layer3 = BayesLinear_Normalq(num_units, 2*output_dim, gaussian(0, 1))

        # activation to be used between hidden layers
        self.activation = nn.ReLU(inplace = True)

    def forward(self, x):

        KL_loss_total = 0
        x = x.view(-1, self.input_dim)

        x, KL_loss = self.layer1(x)
        KL_loss_total = KL_loss_total + KL_loss
        x = self.activation(x)

        x, KL_loss = self.layer2(x)
        KL_loss_total = KL_loss_total + KL_loss
        x = self.activation(x)

        x, KL_loss = self.layer3(x)
        KL_loss_total = KL_loss_total + KL_loss

        return x, KL_loss_total

def throughput_pre(data, model, horizon):
    # best_net = torch.load("./throughput_v0.pkl")
    best_net = model
    H_step = horizon
    in_dim = 10
    x_test = data
    throughput_mean = []
    throughput_std = []
    x, y = to_variable(var=(x_test, x_test), cuda=True)

    for step in range(H_step):
        means, stds = [], []
        no_samples = SIMULATION_NUM
        for i in range(no_samples):
            output, KL_loss_total = best_net(x)
            means.append(output[:, :1, None])
            stds.append(output[:, 1:, None].exp())

        means, stds = torch.cat(means, 2), torch.cat(stds, 2)
        mean = means.mean(dim=2)
        std = (means.var(dim=2) + stds.mean(dim=2)**2)**0.5
        # std = stds.mean(dim=2)
        throughput_mean.append(mean.cpu().detach().numpy()[0].tolist()[0])
        throughput_std.append(std.cpu().detach().numpy()[0].tolist()[0])

        x_test = np.roll(x_test, -1, axis=0)
        x_test[-1] = mean.cpu().detach().numpy()
        x, y = to_variable(var=(x_test, x_test), cuda=True)

    return throughput_mean, throughput_std

# def get_chunk_size(quality, index):
#     if ( index < 0 or index > 48 ):
#         return 0
#     # note that the quality and video labels are inverted (i.e., quality 4 is highest and this pertains to video1)
#     sizes = {5: size_video1[index], 4: size_video2[index], 3: size_video3[index], 2: size_video4[index], 1: size_video5[index], 0:size_video6[index]}
#     return sizes[quality]
class video_size(object):
    def __init__(self):
        self.video_sizes = {}

    def store_size(self):
        for bitrate in range(A_DIM):
            self.video_sizes[bitrate] = []
            with open(VIDEO_SIZE_FILE + str(bitrate)) as f:
                for line in f:
                    self.video_sizes[bitrate].append(int(line.split()[0]))

    def get_chunk_size(self, quality, index):
        if ( index < 0 or index > 47 ):
            return 0
        # note that the quality and video labels are inverted (i.e., quality 4 is highest and this pertains to video1)
        # sizes = {5: size_video1[index], 4: size_video2[index], 3: size_video3[index], 2: size_video4[index], 1: size_video5[index], 0:size_video6[index]}
        return self.video_sizes[quality][index]

def main():
    args = parser.parse_args()
    if args.cb or args.lin:
        qoe_metric = 'results_lin'
    elif args.log:
        qoe_metric = 'results_log'
    else:
        print('Please select the QoE Metric!')

    if args.FCC:
        dataset = 'fcc'
    elif args.HSDPA:
        dataset = 'HSDPA'
    elif args.Oboe:
        dataset = 'Oboe'
    else:
        print('Please select the dataset!')

    dataset_path = './traces_' + dataset + '/'
    if args.cb:
        Log_file_path = './' + qoe_metric + '/cb_' + dataset + '/log_sim_bayesmpc'
    else:
        Log_file_path = './' + qoe_metric + '/' + dataset + '/log_sim_bayesmpc'

    np.random.seed(RANDOM_SEED)

    assert len(VIDEO_BIT_RATE) == A_DIM

    all_cooked_time, all_cooked_bw, all_file_names = load_trace.load_trace(dataset_path)

    net_env = env.Environment(all_cooked_time=all_cooked_time,
                              all_cooked_bw=all_cooked_bw)

    log_path = Log_file_path + '_' + all_file_names[net_env.trace_idx]
    log_file = open(log_path, 'w')

    model = torch.load("./throughput_v0.pkl")

    chunk_size_info = video_size()
    chunk_size_info.store_size()

    time_stamp = 0

    last_bit_rate = DEFAULT_QUALITY
    bit_rate = DEFAULT_QUALITY

    action_vec = np.zeros(A_DIM)
    action_vec[bit_rate] = 1

    s_batch = [np.zeros((S_INFO, S_LEN))]
    a_batch = [action_vec]
    r_batch = []
    future_bandwidth = [0]
    future_bandwidth_r = [0]
    predicted_mean = [0]
    predicted_unc = [0]
    # entropy_record = []

    video_count = 0

    # make chunk combination options
    # for combo in itertools.product([0,1,2,3,4,5], repeat=MPC_FUTURE_CHUNK_COUNT):
    #     CHUNK_COMBO_OPTIONS.append(combo)

    while True:  # serve video forever
        # the action is from the last decision
        # this is to make the framework similar to the real
        delay, sleep_time, buffer_size, rebuf, \
        video_chunk_size, _,\
        end_of_video, video_chunk_remain = \
            net_env.get_video_chunk(bit_rate)

        time_stamp += delay  # in ms
        time_stamp += sleep_time  # in ms

        # reward is video quality - rebuffer penalty
        if qoe_metric == 'results_lin':
            REBUF_PENALTY = 4.3
            reward = VIDEO_BIT_RATE[bit_rate] / M_IN_K \
                    - REBUF_PENALTY * rebuf \
                    - SMOOTH_PENALTY * np.abs(VIDEO_BIT_RATE[bit_rate] -
                                            VIDEO_BIT_RATE[last_bit_rate]) / M_IN_K
        else:# log scale reward
            REBUF_PENALTY = 2.66
            log_bit_rate = np.log(VIDEO_BIT_RATE[bit_rate] / float(VIDEO_BIT_RATE[0]))
            log_last_bit_rate = np.log(VIDEO_BIT_RATE[last_bit_rate] / float(VIDEO_BIT_RATE[0]))

            reward = log_bit_rate \
                    - REBUF_PENALTY * rebuf \
                    - SMOOTH_PENALTY * np.abs(log_bit_rate - log_last_bit_rate)

        # reward = BITRATE_REWARD[bit_rate] \
        #          - 8 * rebuf - np.abs(BITRATE_REWARD[bit_rate] - BITRATE_REWARD[last_bit_rate])

        r_batch.append(reward)

        last_bit_rate = bit_rate

        # log time_stamp, bit_rate, buffer_size, reward
        log_file.write(str(time_stamp / M_IN_K) + '\t' +
                       str(VIDEO_BIT_RATE[bit_rate]) + '\t' +
                       str(buffer_size) + '\t' +
                       str(rebuf) + '\t' +
                       str(video_chunk_size) + '\t' +
                       str(delay) + '\t' +
                       str(reward) + '\t' +
                       str(predicted_mean[0]/8) + '\t' + str(predicted_unc[0]/8) + '\t' +
                       str(future_bandwidth_r[0]/8) + '\t' +
                       str(float(video_chunk_size) / float(delay) / M_IN_K) + '\n')
        log_file.flush()

        # retrieve previous state
        if len(s_batch) == 0:
            state = [np.zeros((S_INFO, S_LEN))]
        else:
            state = np.array(s_batch[-1], copy=True)

        # dequeue history record
        state = np.roll(state, -1, axis=1)

        # this should be S_INFO number of terms
        state[0, -1] = VIDEO_BIT_RATE[bit_rate] / float(np.max(VIDEO_BIT_RATE))  # last quality
        state[1, -1] = buffer_size / BUFFER_NORM_FACTOR
        state[2, -1] = rebuf
        state[3, -1] = float(video_chunk_size) / float(delay) / M_IN_K * 8# kilo bits / ms
        state[4, -1] = np.minimum(video_chunk_remain, CHUNK_TIL_VIDEO_END_CAP) / float(CHUNK_TIL_VIDEO_END_CAP)
        # state[5: 10, :] = future_chunk_sizes / M_IN_K / M_IN_K

        # ================== MPC =========================
        curr_error = 0 # defualt assumes that this is the first request so error is 0 since we have never predicted bandwidth
        # curr_error_for_net = 0
        if ( len(past_bandwidth_ests) > 0 ):
            curr_error  = abs(past_bandwidth_ests[-1]-state[3,-1])/float(state[3,-1])
        # curr_error_for_net = past_bandwidth_ests[-1]-state[3,-1]
        past_errors.append(curr_error)

        # pick bitrate according to MPC
        # first get harmonic mean of last 5 bandwidths
        # past_bandwidths = [0 for i in range(10)]
        past_bandwidths = state[3,-S_LEN:]

        # simulation for many times
        est_current = []
        predicted_mean, predicted_unc = throughput_pre(past_bandwidths, model, MPC_FUTURE_CHUNK_COUNT)
        future_bandwidth = predicted_mean
        # last_bandwidth = state[3, -1]
        # for i in range(MPC_FUTURE_CHUNK_COUNT):
        #     future_bandwidth.append(last_bandwidth + prediction_output[i])
        #     last_bandwidth = last_bandwidth + prediction_output[i]
        # while past_bandwidths[0] == 0.0:
        #     past_bandwidths = past_bandwidths[1:]
        # #if ( len(state) < 5 ):
        # #    past_bandwidths = state[3,-len(state):]
        # #else:
        # #    past_bandwidths = state[3,-5:]
        # bandwidth_sum = 0
        # for past_val in past_bandwidths:
        #     bandwidth_sum += (1/float(past_val))
        # harmonic_bandwidth = 1.0/(bandwidth_sum/len(past_bandwidths))

        # future bandwidth prediction
        # divide by 1 + max of last 5 (or up to 5) errors
        max_error = 0
        error_pos = -5
        if ( len(past_errors) < 5 ):
            error_pos = -len(past_errors)
        max_error = float(np.max(past_errors[error_pos:]))
        future_bandwidth_r = []
        for i in range(MPC_FUTURE_CHUNK_COUNT): #MPC_FUTURE_CHUNK_COUNT
            # future_bandwidth_r.append(future_bandwidth[i]/(1+max_error))  # robustMPC here
            future_bandwidth_r.append(max(future_bandwidth[i] - 1.2*predicted_unc[i], 0.01))  # robustMPC hered
            # future_bandwidth_r.append(max(future_bandwidth[i] - (2.2-np.log10(buffer_size))*predicted_unc[i], 0.001))
        est_current.append(future_bandwidth[0])
        past_bandwidth_ests.append(future_bandwidth[0])

        # future chunks length (try 4 if that many remaining)
        last_index = int(CHUNK_TIL_VIDEO_END_CAP - video_chunk_remain - 1)
        future_chunk_length = MPC_FUTURE_CHUNK_COUNT
        if ( TOTAL_VIDEO_CHUNKS - last_index < MPC_FUTURE_CHUNK_COUNT ):
            future_chunk_length = TOTAL_VIDEO_CHUNKS - last_index

        # all possible combinations of 5 chunk bitrates (9^5 options)
        # iterate over list and for each, compute reward and store max reward combination
        max_reward = -100000000
        # best_combo = ()
        start_buffer = buffer_size
        #start = time.time()
        download_time_every_step = []
        for position in range(future_chunk_length):
            download_time_current = []
            for action in range(0, A_DIM):
                index = last_index + position + 1 # e.g., if last chunk is 3, then first iter is 3+0+1=4
                download_time = (chunk_size_info.get_chunk_size(action, index)/1000000.)/future_bandwidth_r[0] * 8 # this is MB/MB/s --> seconds
                download_time_current.append(download_time)
            download_time_every_step.append(download_time_current)

        reward_comparison = False
        send_data = 0
        parents_pool = [[0.0, start_buffer, int(bit_rate)]]
        for position in range(future_chunk_length):
            if position == future_chunk_length-1:
                reward_comparison = True
            children_pool = []
            for parent in parents_pool:
                action = 0
                curr_buffer = parent[1]
                last_quality = parent[-1]
                curr_rebuffer_time = 0
                chunk_quality = action
                download_time = download_time_every_step[position][chunk_quality]
                if ( curr_buffer < download_time ):
                    curr_rebuffer_time += (download_time - curr_buffer)
                    curr_buffer = 0.0
                else:
                    curr_buffer -= download_time
                curr_buffer += 4

                # reward
                bitrate_sum = VIDEO_BIT_RATE[chunk_quality]
                smoothness_diffs = abs(VIDEO_BIT_RATE[chunk_quality] - VIDEO_BIT_RATE[last_quality])
                reward = (bitrate_sum/1000.) - (REBUF_PENALTY*curr_rebuffer_time) - (SMOOTH_PENALTY*smoothness_diffs/1000.)
                reward += parent[0]

                children = parent[:]
                children[0] = reward
                children[1] = curr_buffer
                children.append(action)
                children_pool.append(children)
                if (reward >= max_reward) and reward_comparison:
                    if send_data > children[3] and reward == max_reward:
                        send_data = send_data
                    else:
                        send_data = children[3]
                    max_reward = reward

                # criterion terms
                # theta = SMOOTH_PENALTY * (VIDEO_BIT_RATE[action+1]/1000. - VIDEO_BIT_RATE[action]/1000.)
                rebuffer_term = REBUF_PENALTY * (max(download_time_every_step[position][action+1] - parent[1], 0) - max(download_time_every_step[position][action] - parent[1], 0))
                if (action + 1 <= parent[-1]):
                    High_Maybe_Superior = ((1.0 + 2 * SMOOTH_PENALTY)*(VIDEO_BIT_RATE[action]/1000. - VIDEO_BIT_RATE[action+1]/1000.) + rebuffer_term < 0.0)
                else:
                    High_Maybe_Superior = ((VIDEO_BIT_RATE[action]/1000. - VIDEO_BIT_RATE[action+1]/1000.) + rebuffer_term < 0.0)

                # while REBUF_PENALTY*(download_time_every_step[position][action+1] - parent[1]) <= ((VIDEO_BIT_RATE[action+1]/1000. - VIDEO_BIT_RATE[action]/1000.)-(abs(VIDEO_BIT_RATE[action+1] - VIDEO_BIT_RATE[parent[-1]]) - abs(VIDEO_BIT_RATE[action] - VIDEO_BIT_RATE[parent[-1]]))/1000.):
                while High_Maybe_Superior:
                    curr_buffer = parent[1]
                    last_quality = parent[-1]
                    curr_rebuffer_time = 0
                    chunk_quality = action + 1
                    download_time = download_time_every_step[position][chunk_quality]
                    if ( curr_buffer < download_time ):
                        curr_rebuffer_time += (download_time - curr_buffer)
                        curr_buffer = 0
                    else:
                        curr_buffer -= download_time
                    curr_buffer += 4

                    # reward
                    bitrate_sum = VIDEO_BIT_RATE[chunk_quality]
                    smoothness_diffs = abs(VIDEO_BIT_RATE[chunk_quality] - VIDEO_BIT_RATE[last_quality])
                    reward = (bitrate_sum/1000.) - (REBUF_PENALTY*curr_rebuffer_time) - (SMOOTH_PENALTY*smoothness_diffs/1000.)
                    reward += parent[0]

                    children = parent[:]
                    children[0] = reward
                    children[1] = curr_buffer
                    children.append(chunk_quality)
                    children_pool.append(children)
                    if (reward >= max_reward) and reward_comparison:
                        if send_data > children[3] and reward == max_reward:
                            send_data = send_data
                        else:
                            send_data = children[3]
                        max_reward = reward

                    action += 1
                    if action + 1 == A_DIM:
                        break
                    # criterion terms
                    # theta = SMOOTH_PENALTY * (VIDEO_BIT_RATE[action+1]/1000. - VIDEO_BIT_RATE[action]/1000.)
                    rebuffer_term = REBUF_PENALTY * (max(download_time_every_step[position][action+1] - parent[1], 0) - max(download_time_every_step[position][action] - parent[1], 0))
                    if (action + 1 <= parent[-1]):
                        High_Maybe_Superior = ((1.0 + 2 * SMOOTH_PENALTY)*(VIDEO_BIT_RATE[action]/1000. - VIDEO_BIT_RATE[action+1]/1000.) + rebuffer_term < 0)
                    else:
                        High_Maybe_Superior = ((VIDEO_BIT_RATE[action]/1000. - VIDEO_BIT_RATE[action+1]/1000.) + rebuffer_term < 0)

            parents_pool = children_pool
        bit_rate = send_data
        # for item in L:
        #     if L[item] > max_time:
        #         max_time = L[item]
        #         bit_rate = item
        #     elif L[item] == max_time:
        #         if bit_rate < item:
        #             bit_rate = item
        # bit_rate = send_data
        # hack
        # if bit_rate == 1 or bit_rate == 2:s
        #    bit_rate = 0

        # ================================================

        # Note: we need to discretize the probability into 1/RAND_RANGE steps,
        # because there is an intrinsic discrepancy in passing single state and batch states

        s_batch.append(state)
        past_bandwidth_ests.append(np.mean(est_current))

        if end_of_video:
            log_file.write('\n')
            log_file.close()

            last_bit_rate = DEFAULT_QUALITY
            bit_rate = DEFAULT_QUALITY  # use the default action here

            del s_batch[:]
            del a_batch[:]
            del r_batch[:]
            del past_bandwidth_ests[:]

            time_stamp = 0

            action_vec = np.zeros(A_DIM)
            action_vec[bit_rate] = 1

            s_batch.append(np.zeros((S_INFO, S_LEN)))
            a_batch.append(action_vec)
            entropy_record = []
            # past_errors_for_net = []

            print("video count", video_count)
            video_count += 1

            if video_count >= len(all_file_names):
                break

            log_path = Log_file_path + '_' + all_file_names[net_env.trace_idx]
            log_file = open(log_path, 'w')

if __name__ == '__main__':
    main()


import streamlit as st
import pickle
import nltk
import sklearn
import string
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from tensorflow.keras.models import load_model
from keras.utils import pad_sequences

ps = PorterStemmer()

def transform_text(text):
  text  = text.lower()
  text = nltk.word_tokenize(text)
  y =[]
  for i in text:
    if i.isalnum():
      y.append(i)

  text = y[:]
  y.clear()

  for i in text:
    if i not in stopwords.words('english') and i not in string.punctuation:
      y.append(i)

  text = y[:]
  y.clear()
  for i in text:
    y.append(ps.stem(i))
  return " ".join(y)

tfidf = pickle.load(open("vectorizer.pkl", 'rb'))
model = pickle.load(open('model.pkl', 'rb'))
tokenizer = pickle.load(open('tokenizer.pkl','rb'))
lstm_model = load_model("lstm_model.h5")

st.title("Email/Spam Classifier")

input_sms = st.text_input("Enter the message")

st.title("Prediction Through ML")
button1 = st.button("Predict by ML")
if button1:

  transform_sms = transform_text(input_sms)

  vector_input = tfidf.transform([transform_sms]).toarray()

  result  = model.predict(vector_input)[0]

  if result == 1:
    st.header("Spam")

  else:
    st.header("Not Spam")

st.title("Prediction Through RNN_LSTM")
button2 = st.button("Button by LSTM")
if button2:
  max_len = 150
  transform_sms = transform_text(input_sms)

  txt_sms = tokenizer.texts_to_sequences(transform_sms)
  vector_input = pad_sequences(txt_sms, maxlen=max_len)

  result = lstm_model.predict(vector_input)[0]

  if result ==1:
    st.header("Spam")

  else:
    st.header("Not Spam")


from django.shortcuts import render
from django.http import HttpResponseRedirect
from django import forms
from django.urls import reverse

tasks = []#this will take the tasks to do

class NewTaskForm(forms.Form):
    task = forms.CharField(label="New Task")#the form field will take characters

def index(request):
    if "tasks" not in request.session:
        request.session["tasks"] = []

    return render(request, 'tasks/indexT.html', {
        "tasks": request.session["tasks"]
    })

def add(request):
    if request.method == "POST":
        form = NewTaskForm(request.POST)#this will input the inserted data into form using "request.POST"
        if form.is_valid():#if data entered is valid
            task = form.cleaned_data["task"]#this is var to hold task entered in field or hold all data entered
            tasks.append(task)#if the task is valid we put the task in the list
            return render(request, 'tasks/indexT.html', {"tasks": tasks})#after text is added return us to the index page
        else:
            #if the data is not valid we shall serve up the same form they had so they can correct errors
            return render(request, "tasks/add.html", {
                "form": form
            })

    return render(request, 'tasks/add.html', {
        "form": NewTaskForm()
    })


# NOTE: copy data from file and paste it to another file
print(30*'*', 'Copy Data', 30*'*')
file_ = open('students.txt', 'r')
new_file = open('students1.txt', 'w')
readed_data = file_.read()
content = new_file.write(readed_data)
print(content)
file_.close()
new_file.close()