from django.shortcuts import render, redirect
from dashboard.form import Formkomik, Formdaftarkomik
from dashboard.models import komik, daftarkomik
from django.contrib import messages

# Create your views here.

def Komik_View(request):
    komiks=komik.objects.all()

    konteks={
        'komiks':komiks,
    }
    return render(request,'Tampil_komik.html',konteks)

def Daftarkomik_View(request):
    daftarkomiks=daftarkomik.objects.all()

    konteks={
        'daftarkomiks':daftarkomiks,
    }
    return render(request,'Tampil_daftarkomik.html',konteks)

def tambah_komik(request):
    if request.POST:
        form=Formkomik(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request,"Data Berhasil Ditambahkan")
            form = Formkomik()
            konteks={
                'form':form,
            }
            return render(request,'tambah_komik.html',konteks)
    else:
        form=Formkomik()
        konteks = {
            'form':form,
        }
        return render(request,'tambah_komik.html',konteks)

def ubah_Komik(request,id_komik):
    komiks=komik.objects.get(id=id_komik)
    if request.POST:
        form=Formkomik(request.POST,instance=komiks)
        if form.is_valid():
            form.save()
            messages.success(request,"Data Berhasil Diubah")
            return redirect('ubah_komik',id_komik=id_komik)
    else:
        form=Formkomik(instance=komiks)
        konteks = {
            'form':form,
            'komiks':komiks
        }
        return render(request,'ubah_komik.html',konteks)

def hapuskomik(request,id_komik):
    komiks=komik.objects.filter(id=id_komik)
    komiks.delete()
    messages.success(request,'Data Terhapus')
    return redirect('/tabelkomik')

def tambah_daftarkomik(request):
    if request.POST:
        form=Formdaftarkomik(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request,"Data Berhasil Ditambahkan")
            form = Formdaftarkomik()
            konteks={
                'form':form,
            }
            return render(request,'tambah_daftarkomik.html',konteks)
    else:
        form=Formdaftarkomik()
        konteks = {
            'form':form,
        }
        return render(request,'tambah_daftarkomik.html',konteks)

def ubahdaftarkomik(request,id_daftarkomik):
    daftarkomiks=daftarkomik.objects.get(id=id_daftarkomik)
    if request.POST:
        form=Formdaftarkomik(request.POST,instance=daftarkomiks)
        if form.is_valid():
            form.save()
            messages.success(request,"Data Berhasil Diubah")
            return redirect('ubahdaftarkomik',id_daftarkomik=id_daftarkomik)
    else:
        form=Formdaftarkomik(instance=daftarkomiks)
        konteks = {
            'form':form,
            'daftarkomiks':daftarkomiks
        }
        return render(request,'ubah_daftarkomik.html',konteks)

def hapusdaftarbarang(request,id_daftarkomik):
    daftarkomiks=daftarkomik.objects.filter(id=id_daftarkomik)
    daftarkomiks.delete()
    messages.success(request,'Data Terhapus')
    return redirect('/daftarkomik')


from __future__ import annotations
import ezdxf
from ezdxf.math import Vec2
from ezdxf.enums import MTextEntityAlignment
from ezdxf.layouts import Modelspace

SIZE_X = 20
SIZE_Y = 20
GAP_X = 5
GAP_Y = 5
CHAR_HEIGHT = 3
TEXT_OFFSET = Vec2(3, 3)
TEXT_COLOR = 255
TEXT_BG_COLOR = (10, 10, 10)
GRADIENT = [9, 7, 5, 3, 1, 0, 2, 4, 6, 8]
ORDERED = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

def add_index(msp: Modelspace, location: Vec2, aci: int):
    mtext = msp.add_mtext(
        str(aci),
        dxfattribs={
            "style": "OpenSans",
            "char_height": CHAR_HEIGHT,
            "layer": "TEXT",
        },
    )
    mtext.set_location(
        location + TEXT_OFFSET,
        attachment_point=MTextEntityAlignment.BOTTOM_LEFT,
    )
    mtext.set_bg_color(TEXT_BG_COLOR)

def add_color(msp: Modelspace, location: Vec2, aci: int):
    points = [
        location,
        location + Vec2(SIZE_X, 0),
        location + Vec2(0, SIZE_Y),
        location + Vec2(SIZE_X, SIZE_X),
    ]
    msp.add_solid(points, dxfattribs={"color": aci, "layer": "ACI"})

def add_row(msp: Modelspace, x: int, y: int, colors: list[int]):
    for color in colors:
        location = Vec2(x, y)
        add_color(msp, location, color)
        add_index(msp, location, color)
        x += SIZE_X + GAP_X

def draw_aci_color_table(msp: Modelspace, order: list[int], x: int, y: int):
    add_row(msp, x, y, [1, 2, 3, 4, 5, 6, 7, 8, 9])
    y -= SIZE_Y + GAP_Y
    for start_index in range(10, 250, 10):
        colors = [start_index + offset for offset in order]
        add_row(msp, x, y, colors)
        y -= SIZE_Y + GAP_Y
    add_row(msp, x, y, [250, 251, 252, 253, 254, 255])

def main():
    doc = ezdxf.new()
    doc.styles.add("OpenSans", font="OpenSans-Regular.ttf")
    doc.layers.add("SOLID")
    doc.layers.add("TEXT", color=TEXT_COLOR)
    msp = doc.modelspace()
    draw_aci_color_table(msp, order=ORDERED, x=0, y=700)
    draw_aci_color_table(msp, order=GRADIENT, x=300, y=700)
    doc.set_modelspace_vport(800, center=(300, 400))
    doc.saveas("aci_color_table.dxf")

if __name__ == "__main__":
    main()


import os
import datetime as dt

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from scrapy.utils.project import get_project_settings

from orflaedi.models import Model, Retailer, VehicleClassEnum

def get_or_create_retailer(db, slug):
    retailer = db.query(Retailer).filter(Retailer.slug == slug).first()
    if retailer is None:
        retailer = Retailer(slug=slug, name=slug)
        db.add(retailer)
        db.commit()
    return retailer

def get_connection_string():
    database_url = get_project_settings().get("DATABASE_URL")
    if database_url is not None:
        return database_url
    return os.environ.get("DATABASE_URL", "postgresql://orflaedi:@localhost/orflaedi")

class DatabasePipeline(object):
    def __init__(self):
        self.scraped_skus = set()

    def open_spider(self, spider):
        connection_string = spider.settings.get("DATABASE_URL")
        if connection_string is None:
            connection_string = get_connection_string()
        engine = create_engine(connection_string, connect_args={})
        SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
        self.db = SessionLocal()
        self.retailer = get_or_create_retailer(self.db, slug=spider.name)

    def close_spider(self, spider):
        # we can deactivate these since they are no longer available
        if not getattr(spider, "skip", False):
            self.db.query(Model).filter(
                ~Model.sku.in_(self.scraped_skus), Model.retailer == self.retailer
            ).update({"active": False}, synchronize_session=False)
        self.db.commit()
        self.db.close()

    def process_item(self, item, spider):
        model = (
            self.db.query(Model)
            .filter(Model.sku == item["sku"], Model.retailer == self.retailer)
            .first()
        )
        if model is None:
            model = Model(retailer=self.retailer, sku=item["sku"])
        model.name = item["name"]
        model.make = item.get("make") or model.make
        model.last_scraped = dt.datetime.utcnow()
        model.scrape_url = item["scrape_url"]
        model.price = item["price"]
        model.image_url = len(item["file_urls"]) and item["file_urls"][0] or None
        model.active = True
        model.name = model.name or item.get("name")
        if not model.motor_model:
            model.motor_model = item.get("motor_model") or None
        if model.name and model.make:
            if model.name.startswith(model.make):
                model.name = model.name[len(model.make) :]
        model.name = model.name.strip()
        model.classification = item.get(
            "classification", model.classification or VehicleClassEnum.bike_b
        )
        self.db.add(model)
        self.db.commit()
        self.scraped_skus.add(item["sku"])
        return item


# Sebastian Thomas (coding at sebastianthomas dot de)

# https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list
#
# Reverse a doubly linked list

class DoublyLinkedListNode:
    def __init__(self, node_data):
        self.data = node_data
        self.next = None
        self.prev = None

def reverse(head):
    if head is None:
        return None

    while True:
        head.prev, head.next = head.next, head.prev
        if head.prev:
            head = head.prev
        else:
            return head


"""
Get the best ROI for a given currency based on current market board
prices for Exodus.
"""

import numpy as np
import os
import pandas as pd
import streamlit as st

from ffxiv_shugo.constants import DATA_DIR
from ffxiv_shugo.utilities import lookup_prices

# TODO: clean this code up

COLS_TO_NORMALIZE = [
    "current_average_price",
    # "min_price",
    "average_recent_price",
]
NORMALIZED_COLS = [f"normalized_{col}" for col in COLS_TO_NORMALIZE]
ALL_KEYS = [
    # from items_by_currency
    "id",
    "item_name",
    "currency_cost",
    # from lookup_prices
    *COLS_TO_NORMALIZE,
    "sale_velocity",
    "average_recent_stack_size",
    # from computation here
    *NORMALIZED_COLS,
]

def load_data() -> pd.DataFrame:
    data_path = os.path.join(DATA_DIR, "items_by_currency.csv")
    data = pd.read_csv(data_path)
    data = data.loc[data["is_untradable"] == 0].drop("is_untradable", axis=1)
    return data

def main():
    data = load_data()
    available_currencies = data["currency_type"].unique().tolist()

    st.title("FFXIV: Fetch Prices")
    st.info(
        "This app looks up current market prices so you can spend your currencies "
        "on the best ROI."
    )
    with st.form("Options", clear_on_submit=False):
        currency = st.selectbox(
            label="Currency",
            options=available_currencies,
        )
        keys_to_show = st.multiselect(
            label="Final columns to show in output",
            options=ALL_KEYS,
            default=ALL_KEYS,
        )
        sort_by_column = st.selectbox(
            label="Column to sort the output by",
            options=ALL_KEYS, # narrow this down
        )
        sort_ascending = st.selectbox(
            label="Sort output by column ascending?",
            options=["False", "True"],
        )
        submit = st.form_submit_button(label="Submit")
        display = st.empty()
        if submit:
            fetch_prices(
                data.loc[data["currency_type"] == currency],
                display,
                keys_to_show,
                sort_by_column,
                sort_ascending,
            )

def fetch_prices(
    data: pd.DataFrame,
    display: st.empty,
    keys_to_show: st.multiselect,
    sort_by_column: st.selectbox,
    sort_ascending: st.selectbox,
) -> None:
    ids = data["id"].values.tolist()
    col_map = {
        "currentAveragePriceNQ": "current_average_price",
        "nqSaleVelocity": "sale_velocity",
        # "minPriceNQ": "min_price",
        "averageRecentHistoryStackSize": "average_recent_stack_size",
        "averageRecentPrice": "average_recent_price",
    }
    # data[col_map.values()] = np.nan
    for new_col in col_map.values():
        data[new_col] = np.nan

    price_results = lookup_prices(ids)
    for price_data in price_results:
        item_id = price_data["item_id"]
        idx = data.loc[data["id"] == item_id].index[0]
        for price_col, data_col in col_map.items():
            data[data_col][idx] = price_data[price_col]

    for col, normalized_col in zip(COLS_TO_NORMALIZE, NORMALIZED_COLS):
        data[normalized_col] = data[col] / data["currency_cost"]

    display.write(
        data.sort_values(
            by=[sort_by_column],
            ascending=str(sort_ascending).lower() == "true",
        )[keys_to_show]
    )

if __name__ == "__main__":
    main()


import requests
from PIL import Image
from io import BytesIO
import os
import threading
import time

class EenaduEpaper:
	def __init__(self,no,filename,quality):
		self.id=no
		self.data=self.allPageDetails()
		self.name=filename
		self.quality=quality
		self.emptyImage=[]
		for i in self.data:
			self.emptyImage.append(None)

	def allPageDetails(self):
		print(time.strftime("%T"))
		res=requests.get("https://epaper.eenadu.net/Home/GetAllpages?editionid="+str(self.id)+"&editiondate="+time.strftime("%d")+"%2F"+time.strftime("%m")+"%2F"+time.strftime("%Y"),timeout=8000).json()
		data=[]
		for i in res:
			data.append(i["XHighResolution"])
		return data

	def formCompleteImg(self,url,no):
		headers={'Referer': 'https://epaper.eenadu.net/Home/Index', 'Sec-Fetch-Mode': 'no-cors', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'}
		url1=url
		url2=url[:-3]+'png'
		img = Image.open(BytesIO(requests.get(url2,headers=headers,timeout=8000).content)).convert("RGBA")
		background = Image.open(BytesIO(requests.get(url1,headers=headers,timeout=8000).content))
		background.paste(img, (0, 0), img)
		self.emptyImage[no]=background

	def DownloadPaper(self):
		arr=[]
		for no,i in enumerate(self.data):
			t=threading.Thread(target=self.formCompleteImg,args=(i,no))
			arr.append(t)
			t.start()
		no=1
		for i in arr:
			print(no)
			no+=1
			i.join()
		img=self.emptyImage[0]
		self.emptyImage.pop(0)
		img.save(self.name+'.pdf',save_all=True,optimize=True,quality=int(self.quality),append_images=self.emptyImage)
		#optimize=True,quality=quality

def downloadPaper(no,name,quality):
	pap=EenaduEpaper(no,name,quality)
	pap.DownloadPaper()


"""
    Title: harper_hobbies.py
    Author: James Harper
    Date: 9 September 2023
    Description: Python Conditionals, Lists, and Loops.
"""

#List containing 5 hobbies.
hobbies = ["Moutain Biking", "Brewing", "Gaming", "Wood Working", "Coding"]

#Loop that iterates through each item in the hobbies list.
for item in hobbies:
    print(item)

#List of days of the week.
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

#Loop through each item in days, prints the day to the terminal with a custom message.
for item in days:
    if item == "Sunday" or item == "Saturday":
        print(str(item) + ". Day off! Time to enjoy some hobbies.")
    else:
        print(str(item) + ". Uhg! It's a work day.")


from tkinter import Frame, Canvas, Button, Label
from os.path import isfile

class LeaderBoard():
    def getSortedListOfLeaders(self):
        list_of_leaders = []
        if isfile("leaderboard.txt"):
            leaderboard_file = open("leaderboard.txt", 'r')
            leaderboard_list = leaderboard_file.read().strip().split('\n')
            for key_val in leaderboard_list:
                key_val_split = key_val.split(':')
                for combo in list(list_of_leaders):
                    if int(key_val_split[1]) > int(combo[1]):
                        insert_index = list_of_leaders.index(combo)
                        list_of_leaders.insert(insert_index, key_val_split)
                        break
                else:
                    list_of_leaders.append(key_val_split)
            leaderboard_file.close()
        return list_of_leaders[:10]  # only the TOP 10

    def leaderList2String(self, leader_list):
        output = ""
        for i in range(len(leader_list)):
            output += ("%d. %s\n") % (i+1, ":  ".join(leader_list[i]))
        return output

    def __init__(self, frame, windowManager):
        self.frame = frame
        self.windowManager = windowManager
        self.create()

    def create(self):
        canvas = Canvas(self.frame,
                        width=self.windowManager.getResolution()[0],
                        height=self.windowManager.getResolution()[1])
        canvas.pack()

        canvas.create_image(0, 0, image=self.windowManager.menu_image,
                            anchor="nw")
        title = canvas.create_text(self.windowManager.getResolution()[0]/2,
                                   60, text="LEADERBOARD",
                                   font=('Arial', 40, 'bold'))

        back_button = Button(canvas, text="BACK",
                             command=self.windowManager.menu,
                             bg='white', fg='black', padx=30, pady=20)
        back_button.place(relx=0.2, rely=0.15)

        list_of_leaders = self.getSortedListOfLeaders()
        grid_of_leaders = Canvas(canvas, width=500, height=500, bg='white')
        grid_of_leaders.place(relx=0.5, rely=0.45, anchor="center")
        for leader_i in range(len(list_of_leaders)):
            placed = Label(grid_of_leaders, text=str(leader_i+1),
                           font=("Arial", 24), bg='white')
            placed.grid(row=leader_i, column=0, padx=5, sticky='e')
            name = Label(grid_of_leaders, text=list_of_leaders[leader_i][0][:20],
                         font=("Arial", 24), bg='white')
            name.grid(row=leader_i, column=1, sticky='w')
            score = Label(grid_of_leaders, text=list_of_leaders[leader_i][1],
                          font=("Arial", 24), bg='white')
            score.grid(row=leader_i, column=2, padx=15)


""" You are given a String X and a string containing single character Y.
Your task is to find the largest distance between any two occurrences of the character
Y in string X.

The Distance is defined by the no of distinct characters between any
two occurrences of char Y in string X (Exclude whitespaces).

If there is no occurrences or only one occurrences of the given character,
the function should return -1.

Example:

Input1: my name is ganary

Input2: a

Output: 7"""

s1 = "my name is ranary lmao popalola"
s2 = "a"
s1 = s1.replace(" ", "")  # remove whitespaces

idxlist = []
counts2 = s1.count(s2)

if not counts2 or counts2 == 1:
    print(-1)
else:

    for a, b in enumerate(s1):
        if b == s2:
            idxlist.append(a)
    print(idxlist)
    maxdist, mindist = max(idxlist), min(idxlist)
    print(maxdist - mindist - 1)


import os
import h5py
import pandas as pd
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from collections import Counter

GRANULARITY = 0.025
SIZE_FINAL_MATRIX = 256

calocell_file = "data/calocell/user.cantel.33075755._000001.calocellD3PD_mc16_JZW4.r14423.h5"

def normalization_method(val, method, min=0, max=0, mean=0, std=0):
    if method == 0:
        return val
    elif method == 1:
        return np.log(val)
    elif method == 2:
        return 2*((val-min)/(max-min))-1
    elif method == 3:
        return ((val-min)/(max-min))
    elif method == 4:
        if val > max:
            val = max
        return ((val-min)/(max-min))
    elif method == 5:
        return (val-mean)/std
    elif method == 6:
        return val/max
    elif method == 7:
        return 1-(mean+std)/((mean+std)+val)
    elif method == 8:
        return (np.power(2, val)-1)/(2-1)

    return 0

########################################################################
# Deal generated matrices
########################################################################

matrices_training = []
matrices_validation = []
matrices_test = []

########################################################################
# Transform data
########################################################################

# Compute the len of phi and eta based on a given granularity
len_phi = len(np.arange(start=-3.15, stop=3.15+GRANULARITY, step=GRANULARITY))
len_eta = len(np.arange(start=-2.4, stop=2.4+GRANULARITY, step=GRANULARITY))

print("Size eta : " + str(len_eta))
print("Size phi : " + str(len_phi))

f = h5py.File(calocell_file, "r")

data_calo = f["caloCells"]["2d"][('cell_E', 'cell_Sigma', 'cell_eta', 'cell_phi', 'cell_pt')][0]

f.close()

df_calo = pd.DataFrame(data_calo,  columns=['cell_E', 'cell_Sigma', 'cell_eta', 'cell_phi', 'cell_pt'])

# Check if the cell_E is above 2 sigma (that defined that the energy is interesting)
# We keep the negative values
df_calo['cell_EoverSigma'] = (df_calo["cell_E"]/df_calo['cell_Sigma'])
df_calo = df_calo[df_calo['cell_EoverSigma'] > 2]
# df_calo[df_calo['cell_EoverSigma'] > 6] = 6

# df_calo['cell_eta_rounded'] = df_calo['cell_eta'].apply(lambda x : round(x*40)/40)

# df_calo['cell_phi_rounded'] = df_calo['cell_phi'].apply(lambda x : round(x*40)/40)

# matrix = np.zeros((3, len_phi, len_eta))

# for j in range(len(df_calo['cell_eta'])):
#     if np.absolute(df_calo['cell_eta'].iloc[j]) <= 2.4:
#         matrix[0][round((df_calo['cell_phi_rounded'].iloc[j] + 3.15) / (3.15*2) * (len_phi-1))][round((df_calo['cell_eta_rounded'].iloc[j] + 2.4) / (2.4*2) * (len_eta-1))] += df_calo["cell_EoverSigma"].iloc[j]
#         matrix[1][round((df_calo['cell_phi_rounded'].iloc[j] + 3.15) / (3.15*2) * (len_phi-1))][round((df_calo['cell_eta_rounded'].iloc[j] + 2.4) / (2.4*2) * (len_eta-1))] += df_calo["cell_pt"].iloc[j]

# val_min = matrix[0].min()
# val_max = matrix[0].max()
# val_mean = matrix[0].mean()
# val_std = matrix[0].std()

# val_min1 = matrix[1].min()
# val_max1 = matrix[1].max()
# val_mean1 = matrix[1].mean()
# val_std1 = matrix[1].std()

# for k in range(len(matrix[0])):
#     for j in range(len(matrix[0][k])):
#         matrix[0][k][j] = normalization_method(matrix[0][k][j], 7, val_min, val_max, val_mean, val_std)
#         matrix[1][k][j] = normalization_method(matrix[1][k][j], 7, val_min1, val_max1, val_mean1, val_std1)

# matrix = np.ascontiguousarray(matrix.transpose(1,2,0))

# matrix *= 255
# matrix = matrix.astype(np.uint8)

# im = Image.fromarray(matrix, 'RGB')

hist,bins = np.histogram(df_calo["cell_EoverSigma"])

print(len(hist))
print(len(bins))

print((df_calo['cell_EoverSigma'].lt(5)).sum())
print((df_calo['cell_EoverSigma'].ge(5)).sum())

print(df_calo["cell_EoverSigma"].mean())
print(df_calo["cell_EoverSigma"].median())
print(df_calo["cell_EoverSigma"].std())

plt.hist(df_calo["cell_EoverSigma"], 600)
plt.yscale('log')
plt.xscale('log')
plt.xlabel("Energie sur bruit")
plt.ylabel("Occurences")
plt.grid(axis='both', linestyle='--')
plt.show()

# plt.imshow(im)
# plt.show()


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import AddGameForm
from library.views import library
from library.models import Post

@login_required
def add_game(request):
    if request.method == 'POST':
        form = AddGameForm(request.POST, request.FILES)
        if form.is_valid():
            form.instance.user = request.user
            form.save()
            messages.success(request, "Game successfully added")
            return redirect(library)
        else:
            messages.error(
                request,
                "There was a problem adding this game. Please try again")
    form = AddGameForm()
    template = 'add_game/add_game.html'
    context = {
        'form': form
    }
    return render(request, template, context)

@login_required
def update_game(request, id):
    game = get_object_or_404(Post, id=id)
    if not game.user == request.user:
        messages.error(request, "You are not authorised")
        return redirect(library)

    if request.method == 'POST':
        form = AddGameForm(request.POST, request.FILES, instance=game)
        if form.is_valid():
            form.instance.user = request.user
            form.save()
            messages.success(request, "Game successfully updated")
            return redirect(library)
        else:
            messages.error(
                request,
                "There was a problem updated this game. Please try again")
    form = AddGameForm(instance=game)
    template = 'add_game/update_game.html'
    context = {
        'form': form,
        'game': game,
    }
    return render(request, template, context)

@login_required
def delete_game(request, id):
    game = get_object_or_404(Post, id=id)
    if not game.user == request.user:
        messages.error(request, "You are not authorised")
        return redirect(library)

    messages.success(request, "Game successfully deleted")
    game.delete()
    return redirect(library)


from bamboo.core.parser import Parser
from bamboo.lib.mongo import MONGO_ID, MONGO_ID_ENCODED
from bamboo.lib.query_args import QueryArgs
from bamboo.lib.schema_builder import make_unique

def parse_columns(dataset, formula, name, dframe=None, no_index=False):
    """Parse a formula and return columns resulting from its functions.

    Parse a formula into a list of functions then apply those functions to
    the Data Frame and return the resulting columns.

    :param formula: The formula to parse.
    :param name: Name of the formula.
    :param dframe: A DataFrame to apply functions to.
    :param no_index: Drop the index on result columns.
    """
    functions = Parser.parse_functions(formula)
    dependent_columns = Parser.dependent_columns(formula, dataset)

    # make select from dependent_columns
    if dframe is None:
        select = {col: 1 for col in dependent_columns or [MONGO_ID]}

        dframe = dataset.dframe(
            query_args=QueryArgs(select=select),
            keep_mongo_keys=True).set_index(MONGO_ID_ENCODED)

        if not dependent_columns:
            # constant column, use dummy
            dframe['dummy'] = 0

    return __build_columns(dataset, dframe, functions, name, no_index)

def __build_columns(dataset, dframe, functions, name, no_index):
    columns = []

    for function in functions:
        column = dframe.apply(function, axis=1, args=(dataset,))
        column.name = make_unique(name, [c.name for c in columns])

        if no_index:
            column = column.reset_index(drop=True)

        columns.append(column)

    return columns


from matplotlib.pyplot import disconnect
import numpy as np
import pandas as pd
import pickle
import random

from helpers import thresholding_algo, find_changes_from_thresholding_algo, format_for_pandas, real_time_peak_detection, thresholding_algo_only_break_points

def calculate_score(online_data, offline_data):
    score = 0

    if len(online_data) == 0:
        return 100000000000000
    elif len(offline_data) == 0:
        return 0

    online_data_index = 0
    for offline_data_point in offline_data:

        distances = []

        for online_data_point in online_data[online_data_index:]:
            if offline_data_point > online_data_point:
                online_data_index +=1
            else:
                break

        if online_data_index > 0:
            distance_to_lower_bound = np.power((online_data[online_data_index-1] - offline_data_point) / 100, 2)
            distances.append(distance_to_lower_bound)

        if online_data_index < len(online_data):
            distance_to_upper_bound = np.power((online_data[online_data_index] - offline_data_point) / 100, 2)
            distances.append(distance_to_upper_bound)

        score += np.min(distances)

    return score

DATA_PATH = r"C:\borsa\boxanalyse\Box Analysis\inputs\DAX_from_2020.csv"

print("Started reading the file.")
custom_date_parser = lambda x,y: pd.to_datetime(x+" "+y, format='%m/%d/%Y %H:%M:%S')
df = pd.read_csv(DATA_PATH, header=0, parse_dates={'datetime':[0,1]}, date_parser=custom_date_parser)
print(df)
print(df.info())
print("Started analysing.")

with open(r"Box Analysis\peak_find_datas\offline_data_all.pickle", "rb") as f:
    offline_data = pickle.load(f)

lag_range = [245]
treshold_range = [3]
influence_range = [0.06]

grid_search_results = []

random.seed(10)
indexes_to_analyse = random.sample(range(len(df.groupby(df.datetime.dt.date))), 25)

for lag in lag_range:
    lag = int(lag)
    for threshold in treshold_range:
        for influence in influence_range:

            total_score_for_current_parameters = 0
            i = -1
            for idx, day in df.groupby(df.datetime.dt.date):
                i += 1
                if len(day)<200 or i not in indexes_to_analyse:
                    continue

                y = day["Price"].to_numpy()
                algo_result = thresholding_algo_only_break_points(y, lag=lag, threshold=threshold, influence=influence)
                score = calculate_score(algo_result, offline_data[idx])
                print(f"--RESULTS--  day: {idx}, lag: {lag:.2f}, threshold: {threshold:.2f}, influence: {influence:.2f}, result: {score}")
                total_score_for_current_parameters += score
                print

            grid_search_results.append({"lag":lag,
                                        "treshhold":threshold,
                                        "influence":influence,
                                        "total_score":total_score_for_current_parameters
                                        })

            headers = ["lag", "treshhold", "influence", "total_score"]
            pandas_dict = format_for_pandas(headers, grid_search_results)
            result_df = pd.DataFrame(pandas_dict)
            print(result_df)
            result_df.to_csv(r"C:\borsa\boxanalyse\Box Analysis\peak_find_datas\grid_search_results.csv", mode='w', header=True)


def readit_all(filename = "penguins.csv"):

  with open(filename, 'r') as file:
    mylist = file.read().rstrip().split("\n")

  keys = mylist.pop(0).split(",")
  data = []
  for line in mylist:
    values = line.split(",")
    datum = {keys[i]: value for i, value in enumerate(values)}
    data.append(datum)

  return data

import matplotlib.pyplot as plt

data = readit_all()

x_label = "bill_length_mm"
y_label = "bill_depth_mm"

for datum in data:
  if datum[x_label] == "NA" or datum[y_label] == "NA":
    continue
  plt.scatter(float(datum[x_label]), float(datum[y_label]), c="steelblue")

data[0]
plt.show()

z_label = "species"
species = set([d[z_label] for d in data])
colormap = {s: ['tab:blue', 'tab:orange', 'tab:green'][i] for i,s in enumerate(species)}

for d in data:
    if d[x_label] == "NA" or d[y_label] == "NA":
        continue

    color = colormap[d[z_label]]
    plt.scatter(float(d[x_label]), float(d[y_label]), color=color)

plt.xlabel(x_label)
plt.ylabel(y_label)
plt.show()

def process(data, x = "bill_length_mm", y = "flipper_length_mm", z = "species"):
    bad_count = 0
    X = []
    Y = []
    Z = []
    for d in data:

      if "NA" in [d[x], d[y], d[z]]:
          bad_count += 1
          print("Skipping NA --", bad_count, "lines skipped")
          continue

      X.append(float(d[x]))
      Y.append(float(d[y]))
      Z.append(d[z])

    return X, Y, Z

X, Y, Z = process(data)

for s in species:
  X_subset=[x_sub for x_sub,z_sub in zip(X,Z) if z_sub==s]
  Y_subset=[y_sub for y_sub,z_sub in zip(Y,Z) if z_sub==s]

  X_subset = [d for i, d in enumerate(X) if s == Z[i]]
  Y_subset = [d for i, d in enumerate(Y) if s == Z[i]]

  plt.scatter(X_subset, Y_subset, label=s)

plt.legend()
plt.show()


import warnings

warnings.simplefilter('ignore')

import os
import logging
import torch
from torchaudio.backend import soundfile_backend

from os import remove
from os.path import dirname, abspath

import telebot
from utils import init_jit_model, apply_tts, replace_accents, pluses_to_accents

TOKEN = os.environ['TOKEN']

if not TOKEN:
    print('You must set the TOKEN environment variable')
    exit(1)

SYMBOLS = '_~абвгдеёжзийклмнопрстуфхцчшщъыьэюя +.,!?…:;–'
SAMPLE_RATE = 16000

START_MSG = '''Привет!

Этот бот создан для тестирования Silero TTS: https://github.com/snakers4/silero-models
'''

FIRST_STEP = '''Использовать бот просто:

1) Добавьте ударения в свой текст с помощью https://morpher.ru/accentizer/

2) Отправьте текст и ждите аудиосообщение
'''

device = torch.device('cpu')

jit_model = dirname(__file__) + '/model/v1_kseniya_16000.jit'
model = init_jit_model(jit_model, device=device)

bot = telebot.TeleBot(TOKEN, parse_mode=None)

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
    bot.reply_to(message, START_MSG)
    bot.reply_to(message, FIRST_STEP)

@bot.message_handler(func=lambda message: True)
def process_voice_message(message):
    if not message.text:
        bot.reply_to(message, 'А где текст??!')
        return

    # normalize the input

    # convert pluses into accents
    text_with_accents = pluses_to_accents(message.text)

    # replace accents into pluses
    text_normalized = replace_accents(text_with_accents)

    if not text_normalized:
        bot.reply_to(message, 'Ошибка: а где текст?')
        return

    # add a dot if it is not set
    last_char = text_normalized[-1]
    if last_char not in ['.', '!', '?']:
        text_normalized = text_normalized + '.'

    text_len = len(text_normalized)
    if text_len > 150:
        bot.reply_to(message, f'Ошибка: ваш текст больше 150 символов, а именно {text_len}')
        return

    bot.reply_to(message, f'Текст c ударениями:\n\n{text_with_accents}\n\nНормализованный текст:\n\n{text_normalized}')

    # do the synthesizing
    audios = apply_tts(texts=[text_normalized],
                       model=model,
                       sample_rate=SAMPLE_RATE,
                       symbols=SYMBOLS,
                       device=device)

    s = 0
    for n, audio_tensor in enumerate(audios):
        # form the filename
        filename = dirname(abspath('__file__')) + f'/files/file_{n}.wav'

        # save to the disk
        soundfile_backend.save(filename, audio_tensor, SAMPLE_RATE)

        # send back to the user
        audio = open(filename, 'rb')
        bot.send_voice(message.chat.id, audio)

        # remove WAV file
        remove(filename)

        # increment the counter
        s = s + 1

    if s == 0:
        bot.reply_to(message, "Ничего не смог синтезировать :(")

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)

    bot.polling()


from pathlib import Path
from htmlStruct import HtmlStruct

def main():

    tests = []
    tests.append(("test1.1.html", "test1.2.html"))
    tests.append(("test2.1.html", "test2.2.html"))
    tests.append(("test3.1.html", "test3.2.html"))
    tests.append(("test4.1.html", "test4.2.html"))
    tests.append(("test5.1.html", "test5.2.html"))
    tests.append(("test6.1.html", "test6.2.html"))
    tests.append(("test7.1.html", "test7.1.html"))

    for old, new in tests:
        oldfn = Path("./tests/" + old)
        newfn = Path("./tests/" + new)

        with open(oldfn, "r", encoding="utf-8") as oldf:
            with open(newfn, "r", encoding="utf-8") as newf:
                oldStr = oldf.read()
                newStr = newf.read()
                #diagnose(newStr)
                oldStruc = HtmlStruct()
                oldStruc.parseHtml(oldStr, keepAttributes=True)

                newStruc = HtmlStruct()
                newStruc.parseHtml(newStr, keepAttributes=True)

                diff = oldStruc.structDiff(newStruc)

                print ("===============================================\n")
                print("    ---------- old document ----------")
                print(HtmlStruct.presentNodes(oldStruc.getNodes()))
                print("\n    ---------- new document ----------")
                print(HtmlStruct.presentNodes(newStruc.getNodes()))
                if diff != None:
                    print("\n    ---------- number of injected structures: {} ----------\n".format(len(diff)))
                else:
                    print("    ---------- document changed beyond simple injections ----------\n")
                i = 1
                if diff:
                    for insertedStruct in diff:
                        print("    ---------- injected structure {} ----------\n".format(i))
                        i += 1
                        print (HtmlStruct.presentNodes(insertedStruct))
                        print("\n")

if __name__ == '__main__':
    main()


from __future__ import print_function
from pyspark import SparkConf, SparkContext
import sys

conf = SparkConf().setAppName("task1").setMaster("local[2]")
sc = SparkContext(conf=conf)
sc.setLogLevel("WARN")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: task1 <file>", file=sys.stderr)
        exit(-1)

    tweets = sc.textFile(sys.argv[1])
    # Used smaller amount of data for testing
    tweetsTest = tweets.sample(False, 0.1, 5)
    tweets = tweets.map(lambda line: line.split("\t"))

    # Task 1A: Total number of tweets
    numberOfTweets = tweets.count()

    # Task 1B: Number of distinct usernames
    distinctUsernames = tweets.map(lambda x: x[6]).distinct().count()

    # Task 1C: Number of distinct countries
    distinctCountryNames = tweets.map(lambda x: x[1]).distinct().count()

    # Task 1D: Number of distinct places
    distinctPlaceNames = tweets.map(lambda x: x[4]).distinct().count()

    # Task 1E: Number of distinct languages
    distinctLanguages = tweets.map(lambda x: x[5]).distinct().count()

    # Task 1F and 1G: Minimal latitude and minimal longitude
    minLatitude = tweets.map(lambda x: x[11]).reduce(lambda a, b: min(a,b))
    minLongitude = tweets.map(lambda x: x[12]).reduce(lambda a, b: min(a,b))

    # Task 1H and 1I: Maximal latitude and maximal longitude
    maxLatitude = tweets.map(lambda x: x[11]).reduce(lambda a, b: max(a,b))
    maxLongitude = tweets.map(lambda x: x[12]).reduce(lambda a, b: max(a,b))

    # Task 1J: Average number of characters in tweet texts
    totalLength = tweets.map(lambda line: len(line[10])).sum()
    avg_char = totalLength/(numberOfTweets)

    # Task 1K: Average number of words in tweets texts
    totalWords = tweets.map(lambda line: len(line[10].split(" "))).sum()
    avg_words = totalWords/(numberOfTweets)

    results = sc.parallelize([numberOfTweets, distinctUsernames, distinctCountryNames, distinctPlaceNames, distinctLanguages,
        minLatitude, minLongitude, maxLatitude, maxLongitude, avg_char, avg_words])
    results.repartition(1).saveAsTextFile("data/result_1.tsv")

TDT4305 - Espen Lindstad - Phase 1/source_code/task_1.py


from hypothesis import given, settings
from hypothesis import strategies as st

from gattaca.fast_non_dominated_sort import dominates, fast_non_dominated_sort

@st.composite
def list_of_tuple_floats(draw, tuple_len=st.integers(min_value=1, max_value=5)):
    tuple_len = draw(tuple_len)
    int_list = [st.floats() for _ in range(tuple_len)]
    return draw(st.lists(elements=st.tuples(*int_list), max_size=100, min_size=1))

@settings(max_examples=200)
@given(tuple_list=list_of_tuple_floats())
def test_sort_levels_dominate_correctly(tuple_list):
    pareto_fronts = fast_non_dominated_sort(tuple_list)
    pareto_len = len(pareto_fronts)
    assert pareto_len <= len(tuple_list)
    if pareto_len > 1:
        for i in range(pareto_len - 1):
            front_1 = pareto_fronts[i]
            front_2 = pareto_fronts[i + 1]
            for i_2 in front_2:
                assert any(
                    [dominates(tuple_list[i_1], tuple_list[i_2]) for i_1 in front_1]
                )


from sklearn.utils import resample
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import normalize
from sklearn.ensemble import RandomForestRegressor
from timeit import default_timer as timer
from sklearn.utils import resample

import shap

from stratx.featimp import *
from support import *

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

figsize = (3.5, 3.0)
use_oob=False
metric = mean_absolute_error
n = 20_000

X, y, _ = load_flights(n=n)

fig, ax = plt.subplots(1,1,figsize=(3.5, 2.7))
plot_stratpd(X, y, colname='SCHEDULED_DEPARTURE', targetname='Flight arrival delay (min)',
             show_slope_lines=False,
             show_impact=True,
             ticklabel_fontsize=10,
             pdp_marker_size=1,
             show_impact_dots=False,
             show_impact_line=False,
             ax=ax
             )
plt.tight_layout()
plt.savefig("../images/flights-impact-SCHEDULED_DEPARTURE.pdf", bbox_inches="tight", pad_inches=0)
plt.show()

fig, ax = plt.subplots(1,1,figsize=(3.5, 2.7))
plot_stratpd(X, y, colname='DEPARTURE_TIME', targetname='Flight arrival delay (min)',
             show_slope_lines=False,
             show_impact=True,
             ticklabel_fontsize=10,
             pdp_marker_size=1,
             show_impact_dots=False,
             show_impact_line=False,
             ax=ax
             )
plt.tight_layout()
plt.savefig("../images/flights-impact-DEPARTURE_TIME.pdf", bbox_inches="tight", pad_inches=0)
plt.show()


import sys
sys.path.append("../..")
sys.path.append("..")

from limits.limits import *
from hypothesis.strategies import *
from hypothesis import given
from timeout import run_with_timeout
from typing import *

@composite
def create(draw):
  n = draw(integers(min_value=1, max_value=MAX_SEQUENCE_LEN))
  arr = draw(lists(integers(), min_size=n, max_size=n))
  i = draw(integers(min_value=0, max_value=n-1))
  return arr, i

arr = shared(create(), key='eval').map(lambda x: x[0])
i = shared(create(), key='eval').map(lambda x: x[1])

strategy = arr, i
if not isinstance(strategy, tuple):
    strategy = (strategy,)

def check_min_heap_helper(arr, i):
    if 2 * i + 2 > len(arr):
        return True
    left_child = (arr[i] <= arr[2 * i + 1]) and check_min_heap_helper(arr, 2 * i + 1)
    right_child = (2 * i + 2 == len(arr)) or (arr[i] <= arr[2 * i + 2]
                                      and check_min_heap_helper(arr, 2 * i + 2))
    return left_child and right_child

def check_min_heap(arr):
  return check_min_heap_helper(arr, 0)

@given(tuples(*strategy))
def test_fuzz(args):
    run_with_timeout(0.3, check_min_heap_helper, *args)


class Solution:
    def numberOfPairs(self, nums: List[int]) -> List[int]:
        dict = {}
        result = [0, 0]

        for i in range(len(nums)):
            if nums[i] not in dict:
                dict[nums[i]] = 1
            else:
                dict[nums[i]] += 1

        for i in dict.items():
            result[0] += i[1] // 2
            result[1] += i[1] % 2

        return result


from enum import Enum

class EC2InstanceState:
    pending = "pending"
    running = "running"
    stopping = "stopping"
    stopped = "stopped"
    terminated = "terminated"
    shutting_down = "shutting-down"
    rebooting = "rebooting"

    LIVE_STATES = [pending, running, rebooting]
    DEAD_STATES = [stopping, stopped, terminated, shutting_down]

    @staticmethod
    def is_dead(state):
        return state in EC2InstanceState.DEAD_STATES

    @staticmethod
    def is_live(state):
        return state in EC2InstanceState.LIVE_STATES

class EC2Instance:
    def __init__(self, cores: int, ram: int):
        self.cores = cores
        self.ram = ram

    def __repr__(self):
        return f"{self.cores} cores, {self.ram} Gb RAM"

class EC2InstanceCategory:
    GENERAL = "general"
    MEMORY = "memory"
    COMPUTE = "compute"
    TINY = "tiny"

EC2_INSTANCE_SPECS = {
    EC2InstanceCategory.TINY: {
        "t3.micro": EC2Instance(1, 1),
        "t3.small": EC2Instance(1, 2),
        "t3.medium": EC2Instance(1, 4),
    },
    EC2InstanceCategory.GENERAL: {
        "m6i.large": EC2Instance(1, 4),
        "m6i.xlarge": EC2Instance(2, 8),
        "m6i.2xlarge": EC2Instance(4, 16),
        "m6i.4xlarge": EC2Instance(8, 32),
        "m6i.8xlarge": EC2Instance(16, 64),
        "m6i.12xlarge": EC2Instance(24, 96),
        "m6i.16xlarge": EC2Instance(32, 128),
        "m6i.24xlarge": EC2Instance(48, 192),
        "m6i.32xlarge": EC2Instance(64, 256),
    },
    EC2InstanceCategory.MEMORY: {
        "r6i.large": EC2Instance(1, 16),
        "r6i.xlarge": EC2Instance(2, 32),
        "r6i.2xlarge": EC2Instance(4, 64),
        "r6i.4xlarge": EC2Instance(8, 128),
        "r6i.8xlarge": EC2Instance(16, 256),
        "r6i.12xlarge": EC2Instance(24, 384),
        "r6i.16xlarge": EC2Instance(32, 512),
        "r6i.24xlarge": EC2Instance(48, 768),
        "r6i.32xlarge": EC2Instance(64, 1024),
    },
    EC2InstanceCategory.COMPUTE: {
        "c6i.large": EC2Instance(1, 4),
        "c6i.xlarge": EC2Instance(2, 8),
        "c6i.2xlarge": EC2Instance(4, 16),
        "c6i.4xlarge": EC2Instance(8, 32),
        "c6i.8xlarge": EC2Instance(16, 64),
        "c6i.12xlarge": EC2Instance(24, 96),
        "c6i.16xlarge": EC2Instance(32, 128),
        "c6i.24xlarge": EC2Instance(48, 192),
        "c6i.32xlarge": EC2Instance(64, 256),
    },
}

AWS_PROFILE = "autumn"
AWS_REGION = "ap-southeast-2"
EC2_SPOT_MAX_PRICE = "1.2"
EC2_AMI = {
    "36venv": "ami-0d27c531f813ff1cf",
    "310conda": "ami-0dbd9adffb9f07a62",
    "springboard310": "ami-0260a3a2d3c12744d",
    "springboardtest": "ami-06294e062db0533bd",
}
EC2_SECURITY_GROUP = "sg-0b2fe230ac8853538"
EC2_IAM_INSTANCE_PROFILE = "worker-profile"
S3_BUCKET = "autumn-data"
EC2_MAX_HOURS = 24


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

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import os
from image_util import resize
from PIL import Image

tf.app.flags.DEFINE_string('checkpoint_dir', 'checkpoint/', """チェックポイント保存先""")
tf.app.flags.DEFINE_integer('max_step', 50000, """訓練回数""")
tf.app.flags.DEFINE_string('data_dir', './data', """訓練データパス""")
tf.app.flags.DEFINE_string('log_dir', 'logs/', """学習ログ出力先""")
tf.app.flags.DEFINE_boolean('eval', False, """評価モードフラグ""")
tf.app.flags.DEFINE_string('photo_file', "", """評価対象画像""")

FLAGS = tf.app.flags.FLAGS

LABEL2NAME = {0: "T-shirt/top", 1: "Trouser", 2: "Pullover", 3: "Dress",
              4: "Coat", 5: "Sandal", 6: "Shirts", 7: "Sneaker", 8: "Bag", 9: "Ankle boot"}

def training():
  data = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)

  x = tf.placeholder(tf.float32, [None, 784])
  y = tf.placeholder(tf.float32, [None, 10])
  kb = tf.placeholder(tf.float32, [])

  convolution_op = convolution(x, kb)
  loss_op = loss(convolution_op, y)
  train_op = train(loss_op)
  accuracy_op = accuracy(convolution_op, y)
  init_op = tf.global_variables_initializer()

  with tf.Session() as sess:
    saver = tf.train.Saver()
    load_checkpoint(sess, saver)
    summary_writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph)
    summary_op = tf.summary.merge_all()

    for i in range(FLAGS.max_step):
      images, labels = data.train.next_batch(32)
      _, _loss, _accuracy = sess.run((train_op, loss_op, accuracy_op), feed_dict={x: images, y: labels, kb: 0.5})
      if i % 10 == 0:
        print('step: %03d loss: %0.06f accuracy: %0.06f' % (i, _loss, _accuracy))
      if i % 500 == 0 or i == FLAGS.max_step - 1:
        test_image, test_label = data.test.next_batch(3000)
        _accuracy, _ = sess.run([accuracy_op, summary_op], feed_dict={x: test_image, y: test_label, kb: 1.0})
        print('Test accuracy: %s' % _accuracy)
        saver.save(sess, FLAGS.checkpoint_dir, global_step=i)

def convolution(images, keep_prob):
  batch_size = tf.shape(images)[0]
  reshape = tf.reshape(images, [batch_size, 28, 28, 1])

  output = tf.layers.conv2d(reshape, filters=32, kernel_size=5, strides=(2,2), padding='SAME')
  output = tf.nn.relu(output)

  output = tf.layers.conv2d(reshape, filters=64, kernel_size=5, padding='SAME')
  output = tf.nn.relu(output)

  output = tf.layers.max_pooling2d(output, pool_size=(2, 2), strides=1)

  output = tf.layers.dropout(output, rate=keep_prob)

  output = tf.layers.conv2d(reshape, filters=32, kernel_size=5, strides=(2,2), padding='SAME')
  output = tf.nn.relu(output)

  output = tf.layers.conv2d(reshape, filters=64, kernel_size=5, padding='SAME')
  output = tf.nn.relu(output)

  output = tf.layers.max_pooling2d(output, pool_size=(2, 2), strides=1)

  output = tf.layers.dropout(output, rate=keep_prob)

  output = tf.contrib.layers.flatten(output)

  output = tf.layers.dense(output, 512)
  output = tf.nn.relu(output)

  output = tf.layers.dropout(output, rate=keep_prob)

  output = tf.layers.dense(output, 10)
  output = tf.nn.softmax(output)
  return output

def load_checkpoint(sess, saver):
  if os.path.exists(FLAGS.checkpoint_dir + 'checkpoint'):
    print('restore parameters...')
    saver.restore(sess, tf.train.latest_checkpoint(FLAGS.checkpoint_dir))
  else:
    print('initilize parameters...')
    init_op = tf.global_variables_initializer()
    sess.run(init_op)

def loss(logits, labels):
  loss = tf.reduce_mean(-tf.reduce_sum(labels * tf.log(logits + 1e-10), reduction_indices=[1]))
  tf.summary.scalar('loss', loss)
  return loss

def train(loss):
  return tf.train.AdadeltaOptimizer().minimize(loss)

def accuracy(logits, labels):
  correct_prediction = tf.equal(tf.argmax(logits,1), tf.argmax(labels,1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  tf.summary.scalar('accuracy', accuracy)
  return accuracy

def evaluate():
  img = Image.open(FLAGS.photo_file)
  gray_img = img.convert('L')
  img = [int(e) / 255.0 for e in resize(gray_img).tobytes()]
  x = tf.placeholder(tf.float32, [None, 784])
  kb = tf.placeholder(tf.float32, [])
  convolution_op = convolution(x, kb)
  with tf.Session() as sess:
    saver = tf.train.Saver()
    load_checkpoint(sess, saver)
    logits = sess.run([convolution_op], feed_dict={x: [img], kb: 1.0})
    print(LABEL2NAME[np.argmax(logits[0])])

def main():
  if FLAGS.eval:
    evaluate()
  else:
    training()

if __name__ == '__main__':
  main()


#!/bin/python3

import os

roundingMinimum = 38

def gradingStudents(grades):
    for i, grade in enumerate(grades):
        if grade < roundingMinimum: continue
        dif = (grade//5 + 1)*5 - grade
        if dif < 3:
            grades[i] += dif
    return grades

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    grades_count = int(input().strip())
    grades = []

    for _ in range(grades_count):
        grades_item = int(input().strip())
        grades.append(grades_item)

    result = gradingStudents(grades)

    fptr.write('\n'.join(map(str, result)))
    fptr.write('\n')

    fptr.close()


def fit(self, X, y=None, **kwargs):
        """Fit encoder according to X and y.

        Parameters
        ----------

        X : array-like, shape = [n_samples, n_features]
            Training vectors, where n_samples is the number of samples
            and n_features is the number of features.
        y : array-like, shape = [n_samples]
            Target values.

        Returns
        -------

        self : encoder
            Returns self.

        """

        # first check the type
        X = util.convert_input(X)

        self._dim = X.shape[1]

        # if columns aren't passed, just use every string column
        if self.cols is None:
            self.cols = util.get_obj_cols(X)
        else:
            self.cols = util.convert_cols_to_list(self.cols)

        X_temp = self.transform(X, override_return_df=True)
        self.feature_names = X_temp.columns.tolist()

        # drop all output columns with 0 variance.
        if self.drop_invariant:
            self.drop_cols = []
            generated_cols = util.get_generated_cols(X, X_temp, self.cols)
            self.drop_cols = [x for x in generated_cols if X_temp[x].var() <= 10e-5]
            try:
                [self.feature_names.remove(x) for x in self.drop_cols]
            except KeyError as e:
                if self.verbose > 0:
                    print("Could not remove column from feature names."
                          "Not found in generated cols.\n{}".format(e))

        return self


from imageai import Detection

class Recognizer:
    def __init__(self) -> None:
        self.recognizer = Detection.ObjectDetection()
        self.recognizer.setModelTypeAsYOLOv3()
        self.recognizer.setModelPath("yolo.h5") # altrimenti yolo-tiny.h5, considerare che E' PIU' VELOCE MA MENO PRECISO
        self.recognizer.loadModel()

    #restituisci frame con tag, nome oggetto e probabilità
    def run(self, img):
        img, preds = self.recognizer.detectCustomObjectsFromImage(input_image=img,
                        custom_objects=None, input_type="array",
                        output_type="array",
                        minimum_percentage_probability=40,
                        display_percentage_probability=True,
                        display_object_name=True)
        return img, preds


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger("boto3").setLevel(logging.WARNING)

import boto3
import os
import feedparser
from boto3 import Session
from boto3 import resource
from botocore.exceptions import BotoCoreError, ClientError
from contextlib import closing
from HTMLParser import HTMLParser
from feedgen.feed import FeedGenerator
from bs4 import BeautifulSoup
import datetime
import dateutil.parser
import hashlib

MAX_TPS = 10
MAX_CONCURENT_CONNECTIONS = 20
TASKS = 100
REQUEST_LIMIT = 1200

def split_content_by_dot(soup, max_len):
    """
    split HTML soup into parts not bigger than max_len may break prosody where
    dot is not at the end of the sentence (like "St. Louis") in some cases may
    be synthesized as two separate sentences
    """
    text = soup.get_text(" ", strip=True)
    start = 0
    while start < len(text):
        if len(text) - start <= max_len:
            yield text[start:]
            return
        max = start + max_len
        index = text.rfind(".", start, max)
        if index == start:
            start += 1
        elif index < 0:
            yield text[start:max]
            start = max
        else:
            yield text[start:index]
            start = index

def get_entries(feed):
    NEW_POST = u"""New post, author {author}, title {title} {content}"""
    for entry in feed.entries:
        if "http" in entry.id:
            nid = hashlib.md5(str(entry.id))
            entry.id = nid.hexdigest()
        entry_content = entry.content[0].value
        soup = BeautifulSoup(entry_content, 'html.parser')
        chunks = split_content_by_dot(soup, REQUEST_LIMIT-len(NEW_POST))
        chunks = list(chunks)
        published = dateutil.parser.parse(entry.published)
        for i, chunk in enumerate(chunks):
            if i == 0:
                chunk = NEW_POST.format(
                        author=entry.author,
                        title=entry.title,
                        content=chunk)
            yield dict(
                content=chunk,
                id="%s_%d" % (entry.id, i),
                title=entry.title,
                published=published - datetime.timedelta(0, i),
            )
            remaining = chunk

def lambda_handler(event, context):
    rss = event['rss']
    bucket_name = event['bucket']
    logging.info("Processing url: %s" % rss)
    logging.info("Using bucket: %s" % bucket_name)

    session = Session(region_name="us-west-2")
    polly = session.client("polly")
    s3 = resource('s3')
    bucket = s3.Bucket(bucket_name)

    logging.info("getting list of existing objects in the given bucket")
    files = set(o.key for o in bucket.objects.all())

    feed = feedparser.parse(rss)

    title = feed['feed']['title']
    fg = FeedGenerator()
    fg.load_extension('podcast')
    fg.title('Audio podcast based on: %s' % title)
    fg.link(href=feed.feed.link, rel='alternate')
    fg.subtitle(feed.feed.description)

    ENTRY_URL = "http://{bucket}.s3-website.{region}.amazonaws.com/{filename}"

    for entry in get_entries(feed):
        filename = "%s.mp3" % entry['id']
        fe = fg.add_entry()
        fe.id(entry['id'])
        fe.title(entry['title'])
        fe.published(entry['published'])
        entry_url = ENTRY_URL.format(bucket=bucket_name, filename=filename, region=os.environ["AWS_REGION"])
        fe.enclosure(entry_url, 0, 'audio/mpeg')
        if filename in files:
            logging.info('Article "%s" with id %s already exist, skipping.'
                         % (entry['title'], entry['id']))
            continue
        try:
            logging.info("Next entry, size: %d" % len(entry['content']))
            logging.debug("Content: %s" % entry['content'])
            response = polly.synthesize_speech(
                    Text=entry['content'],
                    OutputFormat="mp3",
                    VoiceId="Joanna")
            with closing(response["AudioStream"]) as stream:
                bucket.put_object(Key=filename, Body=stream.read())
        except BotoCoreError as error:
            logging.error(error)
    bucket.put_object(Key='podcast.xml', Body=fg.rss_str(pretty=True))


import unittest

from numpy import matrix
from numpy.linalg import norm

from spline import (
    bezier,
    bezier6,
    curve_constraints,
    exact_cubic,
    polynom,
    spline_deriv_constraint,
)

class ParametricCurvesTests(unittest.TestCase):
    def test_all(self):
        waypoints = matrix([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]).transpose()
        waypoints6 = matrix(
            [[1.0, 2.0, 3.0, 7.0, 5.0, 5.0], [4.0, 5.0, 6.0, 4.0, 5.0, 6.0]]
        ).transpose()
        time_waypoints = matrix([0.0, 1.0])

        # testing bezier curve
        a = bezier6(waypoints6)
        a = bezier(waypoints, -1.0, 3.0)

        self.assertEqual(a.degree, a.nbWaypoints - 1)
        a.min()
        a.max()
        a(0.4)
        self.assertTrue((a.derivate(0.4, 0) == a(0.4)).all())
        a.derivate(0.4, 2)
        a = a.compute_derivate(100)

        prim = a.compute_primitive(1)

        for i in range(10):
            t = float(i) / 10.0
            self.assertTrue((a(t) == prim.derivate(t, 1)).all())
        self.assertTrue((prim(0) == matrix([0.0, 0.0, 0.0])).all())

        prim = a.compute_primitive(2)
        for i in range(10):
            t = float(i) / 10.0
            self.assertTrue((a(t) == prim.derivate(t, 2)).all())
        self.assertTrue((prim(0) == matrix([0.0, 0.0, 0.0])).all())

        # testing bezier with constraints
        c = curve_constraints()
        c.init_vel = matrix([0.0, 1.0, 1.0])
        c.end_vel = matrix([0.0, 1.0, 1.0])
        c.init_acc = matrix([0.0, 1.0, -1.0])
        c.end_acc = matrix([0.0, 100.0, 1.0])

        a = bezier(waypoints, c)
        self.assertLess(norm(a.derivate(0, 1) - c.init_vel), 1e-10)
        self.assertLess(norm(a.derivate(1, 2) - c.end_acc), 1e-10)

        # testing polynom function
        a = polynom(waypoints)
        a = polynom(waypoints, -1.0, 3.0)
        a.min()
        a.max()
        a(0.4)
        self.assertTrue(((a.derivate(0.4, 0) == a(0.4)).all()))
        a.derivate(0.4, 2)

        # testing exact_cubic function
        a = exact_cubic(waypoints, time_waypoints)
        a.min()
        a.max()
        a(0.4)
        self.assertTrue(((a.derivate(0.4, 0) == a(0.4)).all()))
        a.derivate(0.4, 2)

        # testing spline_deriv_constraints
        c = curve_constraints()
        c.init_vel
        c.end_vel
        c.init_acc
        c.end_acc

        c.init_vel = matrix([0.0, 1.0, 1.0])
        c.end_vel = matrix([0.0, 1.0, 1.0])
        c.init_acc = matrix([0.0, 1.0, 1.0])
        c.end_acc = matrix([0.0, 1.0, 1.0])

        a = spline_deriv_constraint(waypoints, time_waypoints)
        a = spline_deriv_constraint(waypoints, time_waypoints, c)

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


from settings import CSV_CITY, CSV_SCHOOL, INSEE_CODE
from database import db
from apps.cities.models import City
from apps.schools.models import School
from apps.doctors.models import Doctor
import pandas as pd
import matplotlib.pyplot as plt
import apps.cities.city as ct
import apps.schools.school as sch
import apps.doctors.doctor as dc
import apps.data as dt
import argparse

if __name__ == '__main__':

    db.connect(reuse_if_open=True)

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "action",
        help="Choose an action to execute",
        nargs="?",
        choices=[
            "compute_city",
            "compute_school",
            "compute_doctor",
            "show_city_chart",
            "show_school_chart",
            "show_doctor_chart"
        ],
    )
    args = parser.parse_args()

    if args.action == "compute_city":
        # compute and store result in sql
        cities = ct.extract_and_prepare_cities_from_csv()
        cities_rename = ct.rename_city_column(cities)
        dt.save_data_into_database(cities_rename, City)

    if args.action == "compute_school":
        # compute and store result in sql
        cities = ct.extract_and_prepare_cities_from_csv()
        schools = sch.extract_and_prepare_schools_from_csv(cities)
        schools_rename = sch.rename_school_column(schools)
        dt.save_data_into_database(schools_rename, School)

    if args.action == "compute_doctor":
        # compute and store result in sql
        cities = ct.extract_and_prepare_cities_from_csv()
        doctors = dc.extract_and_prepare_doctors_from_csv(cities)
        doctors_rename = dc.rename_doctor_column(doctors)
        dt.save_data_into_database(doctors_rename, Doctor)

    if args.action == "show_city_chart":
        # show result in a chart
        cities = pd.DataFrame(City.select().order_by('population').limit(10).dicts())
        print(cities)
        city_rank = dt.show_data_in_graph(cities, 10, 'name', 'population', 'barh')
        plt.show()

    if args.action == "show_school_chart":
        # show result in a chart
        schools = pd.DataFrame(School.select(City, School).join(City).order_by(School.success_rate_spread.desc()).dicts())
        print(schools)
        school_rank = dt.show_data_in_graph(schools, 50, 'name', 'success_rate_spread', 'bar')
        plt.show()

    if args.action == "show_doctor_chart":
        # show result in a chart
        doctors = pd.DataFrame(Doctor.select(City, Doctor).join(City).order_by(Doctor.citizens_per_doctor.desc()).dicts())
        doctor_rank = dt.show_data_in_graph(doctors, 20, 'name', 'citizens_per_doctor', 'barh')
        plt.show()

    db.close()


from gvcore.dataset.transforms import parse_transform_config
from gvcore.dataset.dataloader import BasicDataloader
from gvcore.dataset.sampler import GroupedBatchSampler, InfiniteSampler, SequentialSampler
from gvcore.dataset import DATASET_REGISTRY

def build_dataloader(cfg, seed=669):
    # 1. Construct transforms
    img_transforms = parse_transform_config(cfg.img_transforms)
    batch_transforms = parse_transform_config(cfg.batch_transforms)

    # 2. Construct dataset.
    dataset = DATASET_REGISTRY[cfg.dataset](cfg.root, subset=cfg.subset, img_transforms=img_transforms)

    # 3. Construct sampler
    if not cfg.infinity_sampler:
        data_sampler = SequentialSampler(size=len(dataset))
    else:
        data_sampler = InfiniteSampler(size=len(dataset), seed=seed, shuffle=cfg.shuffle)

    if cfg.grouped_sampler:
        batch_sampler = GroupedBatchSampler(data_sampler, group_ids=dataset.group, batch_size=cfg.batch_size)
    else:
        batch_sampler = None

    # 4. Construct dataloader
    data_loader = BasicDataloader(
        dataset,
        batch_size=cfg.batch_size,
        sampler=data_sampler,
        batch_sampler=batch_sampler,
        num_workers=cfg.get("num_workers", 4),
        pin_memory=True,
        prefetch=cfg.get("prefetch", False),
        batch_transforms=batch_transforms,
        drop_last=cfg.get("drop_last", True),
    )

    return data_loader


#CodeChefFlow005
l=[100,50,10,5,2,1]
n=int(input())
for i in range(n):
    m=int(input())
    count=0
    j=0
    for num in l:
        j=int(m/num)
        count+=j
        m=int(m%num)
        if m==0:
            break
    print(count)


import struct
import numpy as np      # 支持大量的维度数组与矩阵运算
import matplotlib.pyplot as plt     # Python的绘图库

train_images_path = "E:\\zxy\\svm_learn\\minist\\train-images-idx3-ubyte\\train-images.idx3-ubyte"
train_labels_path = "E:\\zxy\\svm_learn\\minist\\train-labels-idx1-ubyte\\train-labels.idx1-ubyte"
test_images_path = "E:\\zxy\\svm_learn\\minist\\t10k-images-idx3-ubyte\\t10k-images.idx3-ubyte"
test_labels_path = "E:\\zxy\\svm_learn\\minist\\t10k-labels-idx1-ubyte\\t10k-labels.idx1-ubyte"

def read_images(inputFile):
    binaryData = open(inputFile, 'rb').read()
    offest = 0
    head = '>iiii'
    _, imageNum, imageRow, imageCol = struct.unpack_from(head, binaryData, offest)
    print("读取文件：{}， 图片数：{}，规格：{}*{}".format(inputFile, imageNum, imageRow, imageCol))

    iamgeSize = imageCol * imageRow  # 28*28 = 784
    offest += struct.calcsize(head)
    imageFmt = '>' + str(iamgeSize) + 'B'  # >784B
    # imageFmt = ">784B"
    images = np.empty((imageNum, imageRow * imageCol))
    for i in range(imageNum):
        image = struct.unpack_from(imageFmt, binaryData, offest)
        tmp = np.array(image)  # 1*784
        #plt.imshow(tmp.reshape((28,28)), cmap='binary')
        #plt.show()
        #tmp = tmp.reshape((imageRow, imageCol)) # 28*28
        images[i] = tmp
        offest += struct.calcsize(imageFmt)
    print('Load image done')
    return images

def read_labels(inputFile):
    binaryData = open(inputFile, 'rb').read()
    head = '>ii'
    offset = 0
    _, labelNum = struct.unpack_from(head, binaryData, offset)
    print('读取文件：{}， 标签数：{}'.format(inputFile, labelNum))
    labelHead = '>B'
    offset += struct.calcsize(head)
    labels = np.empty(labelNum)
    for i in range(labelNum):
        labels[i] = struct.unpack_from(labelHead, binaryData, offset)[0]
        offset += struct.calcsize(labelHead)
    return labels

def ReadTrainImages():
    return read_images(train_images_path)

def ReadTrainLabels():
    return read_labels(train_labels_path)

def ReadTestImages():
    return read_images(test_images_path)

def ReadTestLabels():
    return read_labels(test_labels_path)

if __name__ == "__main__":
    train_images = ReadTrainImages()
    train_labels = ReadTrainLabels()
    for i in range(60000):
        print("这个图片里的数字是{}".format(train_labels[i]))
        plt.imshow(np.array(train_images[i]).reshape((28,28)), cmap="binary")
        plt.show()


"""
Applies a rule-based approach for labeling unambiguous animated entities for the training data.
It uses an actor noun list and an inanimate adjective list as patterns.
NER is used to identify organizations and people, and a name database is used for labeling names.
"""

import os
import spacy
from spacy.matcher import Matcher
from names_dataset import NameDataset

nlp = spacy.load("de_core_news_lg")
matcher = Matcher(nlp.vocab)
name_dataset = NameDataset()

with open("gold_actor_10_0.7.txt", "r", encoding="utf-8") as f:
    for line in f:
        words = line.strip().lower().split()
        for word in words:
            if word:
                pattern = [{"LOWER": word}]
                matcher.add(word, [pattern])

inanimated_adjectives = set()
with open("inanimated_adjectives_5_0.9.txt", "r", encoding="utf-8") as f:
    for line in f:
        adjective = line.strip().lower()
        inanimated_adjectives.add(adjective)

def should_animate_chunk(chunk_tokens):
    doc = nlp(" ".join(chunk_tokens))
    matches = matcher(doc)
    contains_inanimated_adjective = any(token.lower_ in inanimated_adjectives for token in doc if token.pos_ == "ADJ")
    contains_person_entity = any(ent.label_ == "PERSON" for ent in doc.ents)
    if matches and not contains_inanimated_adjective:
        for match_id, start, end in matches:
            span = doc[start:end]
            if span.root.pos_ == "NOUN":
                return True
    if contains_person_entity:
        return True
    if any(token.lower() in ['wir', 'ich', 'man', 'jemand'] for token in chunk_tokens):
        return True
    if any(ent.label_ == "ORG" for ent in doc.ents):
        return True
    first_name_count = 0
    last_name_count = 0
    for token in doc:
        token_lower = token.text.lower()
        name_info = name_dataset.search(token_lower)
        if 'first_name' in name_info and token.pos_ == "PROPN":
            first_name_count += 1
        if 'last_name' in name_info and token.pos_ == "PROPN":
            last_name_count += 1
    if first_name_count >= 2 and last_name_count >= 2:
        return True
    return False

def process_chunk(chunk):
    chunk_tokens = [token_tuple[2] for token_tuple in chunk]
    is_animated = should_animate_chunk(chunk_tokens)
    if is_animated:
        for i in range(len(chunk)):
            try:
                chunk[i][3] = 'Animated' if chunk[i][4] in ['B', 'I'] else '_'
            except IndexError:
                continue
    else:
        for i in range(len(chunk)):
            try:
                chunk[i][3] = '_'
            except IndexError:
                continue
    return ['\t'.join(token_tuple) for token_tuple in chunk]

input_folder = 'C:/Users/fredh/Documents/BA/Labeling/Chunks'
output_folder = 'C:/Users/fredh/Documents/BA/Labeling/Animated'

file_list = os.listdir(input_folder)

for filename in file_list:
    if os.path.isfile(os.path.join(input_folder, filename)):
        input_file = os.path.join(input_folder, filename)
        output_file = os.path.join(output_folder, filename.rsplit('.', 1)[0] + '_animated.csv')

        output_lines = []

        with open(input_file, 'r', encoding="utf-8") as file:
            chunk = []
            inside_text_segment = False
            line_count = 0
            for line in file:
                line = line.strip()
                line_count += 1
                if line_count <= 5:
                    output_lines.append(line)
                    continue
                if line.startswith("#Text="):
                    if inside_text_segment and chunk:
                        output_lines.extend(process_chunk(chunk))
                        chunk = []
                    inside_text_segment = True
                    output_lines.append(line)
                    continue
                if not line:
                    inside_text_segment = False
                    if chunk:
                        output_lines.extend(process_chunk(chunk))
                        chunk = []
                    output_lines.append(line)
                    continue
                if inside_text_segment:
                    parts = line.split('\t')
                    chunk_label = parts[4] if len(parts) > 4 else "_"
                    if chunk_label == 'B' and chunk:
                        output_lines.extend(process_chunk(chunk))
                        chunk = []
                    chunk.append(parts)
            if chunk:
                output_lines.extend(process_chunk(chunk))
        with open(output_file, 'w', encoding="utf-8") as out_file:
            for line in output_lines:
                out_file.write(line + '\n')


from tkinter import *
from tkinter import ttk

#A class that takes care of the graphical user interface

class GUI(Tk):
    """ Handles all the graphical user interface """
    def __init__(self, itemMngr, extraReceiptHeight = 0):
        """ Creates a GUI instance
        Arguments: self(GUI), itemMngr(ItemManager) and extraReceiptHeight(int, deafualt 0)
        Returns: nothing"""
        super().__init__()
        self.itemMngr = itemMngr
        self.extraReceiptHeight = extraReceiptHeight
        self.initFirstFrame()
        self.initSecondFrame()

    def initFirstFrame(self):
        """ Calls the Initialization methods for frame one, also grids frame one
        Arguments: self(GUI)
        Returns: nothing """
        self.initFontAndColor()
        self.initWindow()
        self.initMenuBar()

        self.frameOne = Frame(self, bg=self.COLORONE)
        self.frameOne.grid()

        self.fillGrids(self.frameOne)
        self.initId()
        self.initAmount()
        self.initItem()
        self.initErrorMessage()
        self.initButtons()
        self.initTreeView()

    def initSecondFrame(self):
        """ Calls the Initialization methods for frame two, also grids frame two
        Arguments: self(GUI)
        Returns: nothing """
        self.frameTwo = Frame(self, bg=self.COLORONE)
        self.option_add("*Font", "Roboto 10")
        self.fillGrids(self.frameTwo)
        self.initReceipt()

    def initFontAndColor(self):
        """ Initializes the font and color
        Arguments: self(GUI)
        Returns: nothing """
        self.option_add("*Font", "Roboto 12")
        self.COLORONE = "lightgrey"
        self.COLORTWO = "grey"

    def initWindow(self):
        """ Initializes the window
        Arguments: self(GUI)
        Returns: nothing """
        self.resizable(width=False, height=False)
        self.winHeight = IntVar(value=370)
        self.resizeWindow("Scan Items")

    def initMenuBar(self):
        """ Initializes the menu bar
        Arguments: self(GUI)
        Returns: nothing """
        self.helpWinOpen = BooleanVar(value=False)
        self.itemWinOpen = BooleanVar(value=False)
        self.menuBar = Menu(self)
        self.menuBar.add_command(label="Help", command=self.showHelpWindow)
        self.menuBar.add_command(label="Items", command=self.showItemWindow)
        self.config(menu=self.menuBar)

    def initId(self):
        """ Initializes the id entry and label part of the gui
        Arguments: self(GUI)
        Returns: nothing """
        self.itemId = IntVar(value=0)
        valID = self.register(self.validateIdEntry)
        self.idLabel = Label(self.frameOne, text="ID", bg=self.COLORONE)
        self.idEntry = Entry(self.frameOne, width=3, borderwidth=0, highlightthickness=1, highlightbackground="black", validate="key", validatecommand=(valID, "%P"))
        self.idLabel.grid(row=2, column=2, sticky=W)
        self.idEntry.grid(row=3, column=2, sticky=W)

    def initAmount(self):
        """ Initializes the amount entry and label part of the gui
        Arguments: self(GUI)
        Returns: nothing """
        self.itemAmount = IntVar(value=0)
        valAmount = self.register(self.checkAmount)
        self.amountLabel = Label(self.frameOne, text="Qty", bg=self.COLORONE)
        self.amountEntry = Entry(self.frameOne, width=3, borderwidth=0, highlightthickness=1, highlightbackground="black", validate="key", validatecommand=(valAmount, "%P"))
        self.amountLabel.grid(row=2, column=3, sticky=W)
        self.amountEntry.grid(row=3, column=3, sticky=W)

    def initItem(self):
        """ Initializes the item label and entry part of the gui
        Arguments: self(GUI)
        Returns: nothing"""
        self.itemName = StringVar(value="")
        self.nameLabel = Label(self.frameOne, text="Item", bg=self.COLORONE)
        self.itemNameLabel = Label(self.frameOne, width=10, textvariable=self.itemName, anchor=W, bg="darkgrey", borderwidth=1, relief=SOLID)
        self.nameLabel.grid(row=2, column=4, ipadx=1, sticky=W)
        self.itemNameLabel.grid(row=3, column=4, sticky=W)

    def initErrorMessage(self):
        """ Initializes the error message part of the gui
        Arguments: self(GUI)
        Returns: nothing """
        self.idErrorMessage = StringVar(value="X")
        self.idErrorLabel = Label(self.frameOne, textvariable=self.idErrorMessage, bg=self.COLORONE, fg="red")
        self.idErrorLabel.grid(row=4, column=2)

        self.amountErrorMessage = StringVar(value="X")
        self.amountErrorLabel = Label(self.frameOne, textvariable=self.amountErrorMessage, bg=self.COLORONE, fg="red")
        self.amountErrorLabel.grid(row=4, column=3)

    def initButtons(self):
        """ Initializes the button part of the gui
        Arguments: self(GUI)
        Returns: nothing """
        self.option_add("*Button.Font", "Roboto 10")

        self.enterButton = Button(self.frameOne, text="Enter", command=self.addItem)
        self.enterButton.grid(row=3, column=5)

        self.printButton = Button(self.frameOne, text="Print", command=self.changeFrame)
        self.printButton.grid(row=6, column=7)

    def initTreeView(self):
        """ Initializes the Treeview part of the gui
        Arguments: self(GUI)
        Returns: nothing """
        font = ttk.Style().configure("Treeview", rowheight=15)
        self.treeTitle = Label(self.frameOne, text="Shopping cart", style=font, anchor=S, bg=self.COLORONE)
        self.treeTitle.grid(row=5, column=2, columnspan=4)

        self.tree = ttk.Treeview(self.frameOne, columns=(0, 1, 2, 3), show="headings", height=10, style=font)
        self.tree.column(0, anchor=W, width=31)
        self.tree.heading(0, text="ID       ")
        self.tree.column(1, anchor=W, width=31)
        self.tree.heading(1, text="Qty      ")
        self.tree.column(2, anchor=W, width=97)
        self.tree.heading(2, text="Item                    ")
        self.tree.column(3, anchor=W, width=44)
        self.tree.heading(3, text="Price    ")
        self.tree.grid(row=6, column=2, columnspan=4)

        self.scrollBar = ttk.Scrollbar(self.frameOne, orient="vertical", command=self.tree.yview)
        self.tree.configure(yscrollcommand=self.scrollBar.set)
        self.scrollBar.grid(row=6, column=6, sticky=NS)

    def initReceipt(self):
        """ Initializes the receipt part of the gui
        Arguments: self(GUI)
        Returns: nohting"""
        self.receiptFrame = Frame(self.frameTwo, background=self.COLORONE, border=3, highlightthickness=3, highlightcolor=self.COLORTWO, highlightbackground=self.COLORTWO)
        self.receiptFrame.grid(row=1, rowspan=4, column=2, columnspan=4)
        Label(self.receiptFrame, width=1, height=1, bg=self.COLORONE).grid(row=0, column=0)
        Label(self.receiptFrame, width=1, height=1, bg=self.COLORONE).grid(row=4, column=4)

        self.receiptHeaderLabel = Label(self.receiptFrame, text="RECEIPT", font=(None, 18, "italic bold"), background=self.COLORONE)
        self.receiptHeaderLabel.grid(row=0, column=1, columnspan=3)

        self.receiptHeight = IntVar()
        self.receiptName = StringVar()
        self.receiptAmount = IntVar()
        self.receiptPrice = StringVar()
        self.receiptAmountBox = Listbox(self.receiptFrame, listvariable=self.receiptAmount, justify=RIGHT, width=5, height=self.receiptHeight.get(), border=0, highlightthickness=0, background=self.COLORONE)
        self.receiptNameBox = Listbox(self.receiptFrame, listvariable=self.receiptName, justify=LEFT, width=15, border=0, height=self.receiptHeight.get(), highlightthickness=0, background=self.COLORONE)
        self.receiptPriceBox = Listbox(self.receiptFrame, listvariable=self.receiptPrice, justify=RIGHT, width=10, border=0, height=self.receiptHeight.get(), highlightthickness=0, background=self.COLORONE)
        self.receiptAmountBox.grid(row=1, column=1)
        self.receiptNameBox.grid(row=1, column=2)
        self.receiptPriceBox.grid(row=1, column=3, sticky=NW)

        self.receiptTotal = StringVar()
        self.receiptTotalLabel = Label(self.receiptFrame, textvariable=self.receiptTotal, anchor=E, width=10, border=0, highlightthickness=0, background=self.COLORONE)
        self.receiptTotalLabel.grid(row=2, column=1, columnspan=3, sticky=EW)

    def makeReceipt(self):
        """ Shows the receipt
        Arguments: self(GUI)
        Returns: nothing """
        receipt, total, receiptLenght = self.itemMngr.makeReceiptDict()
        self.receiptHeight.set(receiptLenght)
        self.receiptTotal.set("\nTotal: {:>35.2f}".format(total))

        if self.receiptHeight.get() != 0:
            self.winHeight.set((self.receiptHeight.get() + 1) * 17 + 193 + self.extraReceiptHeight)
        else:
            self.winHeight.set(230 + self.extraReceiptHeight)

        self.receiptName.set(receipt["name"])
        self.receiptAmount.set(receipt["amount"])
        self.receiptPrice.set(receipt["price"])

    def fillGrids(self, frame):
        """ Makes 18 empty labels to make the gui look a bit nicer
        Arguments: self(GUI) and frame(Frame)
        Returns: nothing"""
        for i in range(9):
            Label(frame, width=2, height=1, bg=self.COLORONE).grid(row=0, column=i, sticky=NSEW)
            Label(frame, width=2, height=1, bg=self.COLORONE).grid(row=i, column=0, sticky=NSEW)

    def setNameAndID(self, inp, items):
        """ Sets the itemName, itemId and idErrorMessage variable classes depending on the inp argument.
        Arguments: self(GUI), inp(int) and items(dict)
        Returns: item(Item)"""
        item = items[inp]
        self.itemName.set(item.getName())
        self.amountErrorMessage.set("")
        self.idErrorMessage.set("")
        self.itemId.set(inp)

        return item

    def checkId(self, inp):
        """ Checks the id input. If the input is bad, an error message (a red X) will show under the Entry.
        Arguments: self(GUI) and inp(any)s
        Returns: nothing"""
        items = self.itemMngr.getItems()
        try:
            inp = int(inp)
            if inp in items.keys():
                item = self.setNameAndID(inp, items)
                if item.getAmount() == 0:
                    self.amountErrorMessage.set("X")
            else:
                self.amountErrorMessage.set("")
                self.idErrorMessage.set("X")
                self.itemName.set("")

        except ValueError:
            self.amountErrorMessage.set("")
            self.idErrorMessage.set("X")
            self.itemName.set("")

    def validateIdEntry(self, inp):
        """ Takes care of the id input. If it's accepted the method returns True.
        Arguments: self(GUI) and inp(any)
        Returns: True or False (boolean)"""
        self.itemId.set(0)
        if inp == "0":
            return False

        self.checkId(inp)
        self.amountEntry.delete(0, END)
        self.amountEntry.insert(0, 1)

        return True

    def validateAmountEntry(self, inp):
        """ Validates the amount input. If the input is bad, an error message (a red X) will show under the Entry.
        Arguments: self(GUI) and inp(any)
        Returns: nothing"""
        items = self.itemMngr.getItems()
        try:
            inp = int(inp)
            if self.itemId.get() != 0:
                if inp <= items[self.itemId.get()].getAmount() and inp > 0:
                    self.amountErrorMessage.set("")
                    self.itemAmount.set(inp)
                elif abs(inp) <= items[self.itemId.get()].getBuying() and inp < 0:
                    self.amountErrorMessage.set("")
                    self.itemAmount.set(inp)
                else:
                    self.itemAmount.set(0)
                    self.amountErrorMessage.set("X")

        except ValueError:
            self.itemAmount.set(0)
            self.amountErrorMessage.set("X")

    def checkAmount(self, inp):
        """ Validates the amount input. If accepted the method returns True.
        Arguments: self(GUI) and inp(any)
        Returns: True or False (boolean) """
        if inp == "0":
            return False

        self.validateAmountEntry(inp)

        return True

    def changeFrame(self):
        """ Removes frameOne(Frame) and shows frameTwo(Frame).
        Shows the receipt.
        Saves the items inside of the itemMngr-object to the file.
        Arguments: self(GUI)
        Returns: nothing"""
        self.frameOne.destroy()
        self.frameTwo.grid()
        self.makeReceipt()
        self.resizeWindow("Receipt")
        self.itemMngr.writeToDataBase()

    def resizeWindow(self, title):
        """ Rezizes the gui window size and sets the title to the title argument
        Arguments: self(GUI) and title(str)
        Returns: nothing """
        self.title(title)
        self.geometry("{}x{}".format(340, self.winHeight.get()))

    def updateTree(self):
        """ Shows the scannedItems in Treeview.
        Arguments: self(GUI)
        Returns: nothing"""
        scannedItems = self.itemMngr.getScannedItems()
        total = 0
        scannedItems.sort()
        self.tree.delete(*self.tree.get_children())
        for item in scannedItems:
            total += item.getTotalPrice()
            self.tree.insert("", "end", values=(str(item.getCode()), str(item.getBuying()), item.getName(), str(item.getTotalPrice())))

    def addItem(self):
        """ Takes the input from the user.
        Updates the scannedItems list.
        Arguments: self(GUI)
        Returns: nothing """
        items = self.itemMngr.getItems()
        scannedItems = self.itemMngr.getScannedItems()

        code = self.itemId.get()
        amount = self.itemAmount.get()
        code = int(code)
        amount = int(amount)
        if code != 0 and amount != 0:
            item = items[code]
            if item not in scannedItems:
                scannedItems.append(item)
            self.itemMngr.buy(amount, self, item)
            self.itemMngr.setScannedItems(scannedItems)
            self.amountEntry.delete(0, END)
            self.amountEntry.insert(0, 1)
            self.itemAmount.set(1)
            self.updateTree()

    def showHelpWindow(self):
        """ Shows a help window with instructions.
        Arguments: self(GUI)
        Returns: nothing """
        if not self.helpWinOpen.get():
            self.helpWinOpen.set(True)
            self.helpWindow = Toplevel(self)
            self.helpWindow.title("Help")
            self.helpWindow.geometry("{}x{}".format(530, 200))
            self.helpWindow.resizable(False, False)
            self.helpWindow.protocol("WM_DELETE_WINDOW", lambda: (self.helpWindow.destroy(), self.helpWinOpen.set(False)))
            self.helpWindow.grid()

            helpMessage = "\
            ID is row number in the database.\n\
            'Qty' for how many.\n\
            Use negative 'Qty' to remove that many items of that ID from your cart\n\
            'X' for error.\n\
            If the ID doesn't exist the program an 'X' will appear under ID\n\
            If the Qty is more than in stock an 'X' will appear under the Qty\n\
            If you try to remove more items than you have an 'X' will appear under Qty"
            helpMessageLabel = Label(self.helpWindow, text=helpMessage, justify=LEFT)
            helpMessageLabel.grid()

    def showItemWindow(self):
        """ Shows a window with all the items with their id, name and nr in stock
        Arguments: self(GUI)
        Returns: nothing """
        if not self.itemWinOpen.get():
            self.itemWinOpen.set(True)
            self.itemWindow = Toplevel(self)
            self.itemWindow.title("Items")
            self.itemWindow.geometry("{}x{}".format(530, 400))
            self.itemWindow.protocol("WM_DELETE_WINDOW", lambda: (self.itemWindow.destroy(), self.itemWinOpen.set(False)))
            self.itemWindow.grid()

            itemsStr = self.itemMngr.getStrOfItems()
            itemsStrLabel = Label(self.itemWindow, text=itemsStr, justify=LEFT)
            itemsStrLabel.grid()


import time
import random

class Task:
   def __init__(self, completion_time, callback_method, task_result=None):
      self.completion_time = completion_time
      self.task_result = task_result
      self.callback_method = callback_method

   def perform(self):
      if self.task_result is not None:
         print(f"I have finished {self.callback_method}.")
         self.callback_method(self.task_result)
      else:
         print(f"I am going to start {self.callback_method}.")
         self.callback_method()
         print(f"I am performing {self.callback_method}.")

class Bot:
   """This is the blueprint for our bots"""
   def __init__(
         self,
         foo_mined,
         bar_mined,
         combination_result,
         busy_until=0):
      self.busy_until = busy_until
      self.foo_mined = foo_mined
      self.bar_mined = bar_mined
      self.combination_result = combination_result
      self.task = None

   def is_same_task(self, callback_method):
         return self.task is not None and self.task.callback_method == callback_method

   def mine_foo(self, current_time, execution_time=1):
      print("Received order : Mine foo !")
      if self.busy_until > current_time:
         print("Foo? I am busy.")
         return
      self.complete_task(current_time)

      self.busy_until = current_time + execution_time
      completion_time = self.busy_until if self.is_same_task(self.foo_mined) else self.busy_until + 5
      self.task = Task(completion_time, self.foo_mined)

   def mine_bar(self,current_time, execution_time=(random.uniform(0.5, 2))):
      if self.busy_until > current_time:
         print("Bar? I am busy.")
         return
      self.complete_task(current_time)

      self.busy_until = current_time + execution_time
      print(f"I will be busy until {self.busy_until}.")

      completion_time = self.busy_until if self.is_same_task(self.bar_mined) else self.busy_until + 5
      print(f"Required completion time = {completion_time}.")

      self.task = Task(completion_time, self.bar_mined)

   def combine(self, current_time, execution_time=2):

      if self.busy_until > current_time:
         print("I am busy.")
         return
      self.complete_task(current_time)

      self.busy_until = current_time + execution_time
      print(f"I will be busy until {self.busy_until}.")

      completion_time = self.busy_until if self.is_same_task(self.combination_result) else self.busy_until + 5
      print(f"Required completion time = {completion_time}.")

      self.task = Task(completion_time, self.combination_result, random.randrange(100) < 60)

   def complete_task(self, current_time):
      if self.task and self.task.completion_time > current_time :
         self.task.perform()
         self.task = None


import json

from novaclient.exceptions import NotFound
from pony.orm import db_session, commit

import utils
from core.log import logger
from core.models import VmImageInfoMapper
from core.openstack_utils import create_nova_client
from internal.resourcemanager import resourcemanager_pb2_grpc
from internal.resourcemanager.resourcemanager_pb2 import CreateVmResponse, QueryVmResponse, DeleteVmResponse, \
    OperateVmResponse
from task.image_task import start_check_image_status

LOG = logger

class VmService(resourcemanager_pb2_grpc.VmManagerServicer):
    """

    """

    def createVm(self, request, context):
        """

        Args:
            request:
            context:

        Returns:

        """
        LOG.info('received create vm message')
        host_ip = utils.validate_input_params(request)
        if host_ip is None:
            return CreateVmResponse(status='{"data": null, "retCode": 500, "message": "Failure"}')

        nova = create_nova_client(host_ip, request.tenantId)

        availability_zone = None
        config_drive = None
        security_groups = None
        userdata = None
        networks = None

        if request.server.networks and len(request.server.networks) > 0:
            networks = []
            for network in request.server.networks:
                if utils.validate_ipv4_address(network.fixedIp):
                    networks.append({
                        'net-id': network.network,
                        'v4-fixed-ip': network.fixedIp
                    })
                elif network.fixedIp:
                    networks.append({
                        'net-id': network.network,
                        'v6-fixed-ip': network.fixedIp
                    })
                else:
                    networks.append({
                        'net-id': network.network
                    })

        if request.server.availabilityZone:
            availability_zone = request.server.availabilityZone
        if request.server.configDrive:
            config_drive = request.server.configDrive
        if request.server.securityGroups:
            security_groups = request.server.securityGroups
        if request.server.userData:
            userdata = request.server.userData

        server = nova.servers.create(request.server.name,
                                     request.server.image,
                                     request.server.flavor,
                                     availability_zone=availability_zone,
                                     config_drive=config_drive,
                                     security_groups=security_groups,
                                     nics=networks,
                                     userdata=userdata
                                     )
        LOG.info('success boot server %s', server.id)
        return CreateVmResponse(status='{"data": null, "retCode": 0, "message": "Create Success"}')

    def queryVm(self, request, context):
        """

        Args:
            request:
            context:

        Returns:

        """
        LOG.info('received query vm message')
        host_ip = utils.validate_input_params(request)
        if host_ip is None:
            return QueryVmResponse(response='{"data": null, "retCode":400, "message":"Params invalid"}')

        nova = create_nova_client(host_ip, request.tenantId)

        resp_data = {
            'data': None,
            'retCode': 200,
            'message': 'Success'
        }
        if request.vmId:
            try:
                server = nova.servers.get(request.vmId)
                resp_data['data'] = {
                    'id': server.id,
                    'name': server.name,
                    'status': server.status,
                    'addresses': server.addresses,
                    'flavor': server.flavor,
                    'image': server.image,
                    'securityGroups': server.security_groups
                }
            except NotFound:
                resp_data = {
                    'data': None,
                    'retCode': 404,
                    'message': 'server %s not found' % request.vmId
                }
        else:
            resp_data['data'] = []
            servers = nova.servers.list()
            for server in servers:
                resp_data['data'].append({
                    'id': server.id,
                    'name': server.name,
                    'status': server.status,
                    'addresses': server.addresses,
                    'flavor': server.flavor,
                    'image': server.image,
                    'securityGroups': server.security_groups
                })

        LOG.info('success query vm')
        return QueryVmResponse(response=json.dumps(resp_data))

    def operateVm(self, request, context):
        """

        Args:
            request:
            context:

        Returns:

        """
        LOG.info('received operate vm message')
        host_ip = utils.validate_input_params(request)
        if host_ip is None:
            return OperateVmResponse(response='{"data": null, "retCode": 400, "message": "hostIp is needed"}')

        nova = create_nova_client(host_ip, request.tenantId)

        resp_data = {
            'data': None,
            'retCode': 200,
            'message': 'Success'
        }

        if request.action == 'reboot':
            reboot_type = 'SOFT'
            if request.reboot is not None and request.reboot.type is not None:
                reboot_type = request.reboot.type
            nova.servers.reboot(request.vmId, reboot_type=reboot_type)
        elif request.action == 'createImage':
            metadata = None
            if request.createImage.metadata:
                metadata = dict(request.createImage.metadata)
            resp_data['data'] = nova.servers.create_image(request.vmId,
                                                          request.createImage.name,
                                                          metadata=metadata)
            with db_session:
                VmImageInfoMapper(
                    image_id=resp_data['data'],
                    image_name=request.createImage.name,
                    disk_format='raw',
                    status=utils.QUEUED,
                    host_ip=host_ip,
                    tenant_id=request.tenantId,
                    compress_task_status=utils.WAITING
                )
                commit()
            start_check_image_status(resp_data['data'], host_ip)
            return OperateVmResponse(response=json.dumps(resp_data))
        elif request.action == 'pause':
            nova.servers.pause(request.vmId)
        elif request.action == 'unpause':
            nova.servers.unpause(request.vmId)
        elif request.action == 'suspend':
            nova.servers.suspend(request.vmId)
        elif request.action == 'resume':
            nova.servers.resume(request.vmId)
        elif request.action == 'stop':
            nova.servers.stop(request.vmId)
        elif request.action == 'start':
            nova.servers.start(request.vmId)
        elif request.action == 'createConsole':
            resp_data['data'] = nova.servers.get_console_url(request.vmId, console_type='novnc')
            return OperateVmResponse(response=json.dumps(resp_data))
        else:
            LOG.info('not support action %s', request.action)
            return OperateVmResponse(response='{"data": null, "retCode": 400,"message": "not support action %s"}'
                                              % request.action)

        LOG.info('success operate vm')
        return OperateVmResponse(response='{"data": null, "retCode": 200, "message": "success"}')

    def deleteVm(self, request, context):
        """

        Args:
            request:
            context:

        Returns:

        """
        LOG.info('received delete vm message')
        host_ip = utils.validate_input_params(request)
        if host_ip is None:
            return DeleteVmResponse(status='{"data": null, "retCode": 500, "message": "Failure"}')

        nova = create_nova_client(host_ip, request.tenantId)

        try:
            nova.servers.delete(request.vmId)
        except NotFound:
            LOG.debug('skip not found server %s', request.vmId)

        LOG.info('success delete vm')
        return DeleteVmResponse(status='{"data": null, "retCode":200, "message":"success"}')