import math
import sys
import time
from nba_api.stats.static import players
from nba_api.stats.endpoints import playergamelog
from nba_api.stats.library.parameters import SeasonAll
from nba_api.stats.endpoints import leaguegamefinder
import csv
import pandas as pd
from bs4 import BeautifulSoup
import requests
from dateutil.parser import parse
import datetime
from io import StringIO
import tqdm

def get_active_players(player_csv_name):
    active_players_array_of_dicts = players.get_active_players()
    field_names = active_players_array_of_dicts[0].keys()
    with open(player_csv_name, mode="w", newline="") as csv_file:
        writer = csv.DictWriter(csv_file, fieldnames=field_names)
        writer.writeheader()
        writer.writerows(active_players_array_of_dicts)

    return player_csv_name

def add_nba_api_data(players_csv, data_csv_name, testing, wait_seconds=1):
    data = pd.read_csv(data_csv_name)
    players = pd.read_csv(players_csv)

    counter = 0
    for _, row in players.iterrows():
        # cut short for testing
        if counter > 3 and testing:
            break
        counter += 1

        # get the data for the player
        player_id = row["id"]
        gamelogs = pd.concat(
            playergamelog.PlayerGameLog(
                player_id=player_id, season=SeasonAll.all
            ).get_data_frames()
        )
        if len(gamelogs) == 0:
            print("No data for", row["full_name"])

        # have the data, now to add it to the csv.
        for _, gamelog in gamelogs.iterrows():
            already_exists = (gamelog["Player_ID"] == player_id) & (
                data["game_id"] == gamelog["Game_ID"]
            )
            if not any(already_exists):
                to_add = gamelog
                to_add["player_id"] = to_add["Player_ID"]
                to_add["game_id"] = to_add["Game_ID"]
                data = pd.concat([data, pd.DataFrame([to_add])], ignore_index=True)

        # wait before next request to not get rate limited
        data.drop_duplicates(subset=["player_id", "game_id"], inplace=True)
        data.to_csv(data_csv_name, index=False)
        outtext = (
            "Added data for "
            + row["full_name"]
            + " "
            + str(counter)
            + "/"
            + str(len(players))
            + " "
        )
        print(" " * 80, end="\r", flush=True)
        print(outtext, end="\r", flush=True)
        time.sleep(wait_seconds)
    print()
    data = pd.read_csv(data_csv_name)
    data.drop_duplicates(subset=["player_id", "game_id"], inplace=True)
    data.to_csv(data_csv_name, index=False)
    return data_csv_name

def instantiate_data_if_needed(data_csv_name, columns):
    try:
        data = pd.read_csv(data_csv_name)
    except pd.errors.EmptyDataError:
        data = pd.DataFrame()
    for column in columns:
        if column not in data.columns:
            data[column] = 0
    data.to_csv(data_csv_name, index=False)
    return data_csv_name

def clean_date(text):
    return parse(text)

def find_matching_id(injury_row, keyword_df):
    for _, row in keyword_df.iterrows():
        if row["full_name"] in injury_row["Relinquished"]:
            if "DTD" in injury_row["Notes"]:
                return (row["id"], "DTD")
            else:
                return (row["id"], "Not DTD")
    return None, None

def add_injury_data(players_csv, data_csv_name, testing, wait_seconds=1):
    # get correct date data
    data = pd.read_csv(data_csv_name)
    data["GAME_DATE"] = data["GAME_DATE"].apply(clean_date)
    data["GAME_DATE"] = pd.to_datetime(data["GAME_DATE"])

    # make initial url request
    earliest_date = data["GAME_DATE"].min().strftime("%Y-%m-%d")
    players = pd.read_csv(players_csv)
    url = (
        "https://www.prosportstransactions.com/basketball/Search/SearchResults.php?Player=&Team=&BeginDate="
        + earliest_date
        + "&EndDate=&InjuriesChkBx=yes&Submit=Search"
    )
    response = requests.get(url)

    # get all page links
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, "html.parser")
        links = soup.find_all(
            "a", href=lambda href: href and "SearchResults.php?Player=&Team=" in href
        )
        urls = [link["href"] for link in links]
    else:
        print(f"Failed to retrieve content. Status code: {response.status_code}")
        raise ValueError("cant read main page!")

    # for each page, scrape relevant data
    counter = 0
    all_injuries = pd.DataFrame(columns=["Date", "matching_id", "injury_type"])
    for url in urls:
        out_string = ("Adding injury page " + str(counter + 1) + "/" + str(len(urls)) + " ")
        print(out_string, end="\r")
        if counter > 3 and testing:
            break
        counter += 1
        # https://www.prosportstransactions.com/basketball/Search/SearchResults.php?Player=&Team=&BeginDate=2022-12-21&EndDate=&InjuriesChkBx=yes&Submit=Search
        # call page, read table & add clean date
        url_to_call = "https://www.prosportstransactions.com/basketball/Search/" + url
        if response.status_code == 200:
            response = requests.get(url_to_call)
            soup = BeautifulSoup(response.content, "html.parser")
            table = soup.find("table", class_="datatable center")
            table_dataframes = pd.read_html(StringIO(str(table)), header=0)
            page_df = table_dataframes[0]
            page_df["Date"] = pd.to_datetime(page_df["Date"].apply(clean_date))
        else:
            print("failed to get data for", url)
        # If find player match in a row, add matching id and injury type in page_df
        page_df["matching_id"] = None
        page_df["injury_type"] = None
        for injury_index, injury_row in page_df.iterrows():
            for _, row in players.iterrows():
                if isinstance(injury_row["Relinquished"], str) and isinstance(
                    injury_row["Notes"], str
                ):
                    if row["full_name"] in injury_row["Relinquished"]:
                        if "DTD" in injury_row["Notes"]:
                            page_df.at[injury_index, "matching_id"] = row["id"]
                            page_df.at[injury_index, "injury_type"] = "DTD"
                        else:
                            page_df.at[injury_index, "matching_id"] = row["id"]
                            page_df.at[injury_index, "injury_type"] = "Not DTD"

        # add injuries to all_injuries
        page_df = page_df[page_df["matching_id"].notna()]
        if not all_injuries.empty:
            all_injuries = pd.concat([all_injuries, page_df], ignore_index=True)
        else:
            all_injuries = pd.concat([page_df], ignore_index=True)

        time.sleep(wait_seconds)

    # For row in all injuries, update relevant rows in data
    all_injuries = all_injuries.sort_values(by="Date")
    for _, injury_row in all_injuries.iterrows():
        # Find just the data rows matching the injury player_id AND are later:
        ref_date = pd.to_datetime(injury_row["Date"])
        ref_id = int(injury_row["matching_id"])
        ref_injury_type = injury_row["injury_type"]
        filtered_df = data[data["GAME_DATE"] > ref_date]
        filtered_df = data[data["player_id"] == ref_id]
        # Calculate the number of days since the reference date & update data
        print("Adding injuries to data...")
        for data_idx, data_row in filtered_df.iterrows():
            days_since_injury = (data_row["GAME_DATE"] - ref_date).days
            if days_since_injury < 0:
                data.loc[data_idx, "days_since_last_injury"] = math.inf
            else:
                data.loc[data_idx, "days_since_last_injury"] = days_since_injury
            data.loc[data_idx, "type_of_last_injury"] = ref_injury_type

    # fillna
    data["days_since_last_injury"] = data["days_since_last_injury"].fillna(math.inf)
    data["type_of_last_injury"] = data["type_of_last_injury"].fillna(math.inf)

    print()
    data.drop_duplicates(subset=["player_id", "game_id"], inplace=True)
    data.to_csv(data_csv_name, index=False)
    return data_csv_name

def make_time_idx(game_id, min_id):
    return game_id - min_id

def turn_game_id_to_time_idx(data_csv_name):
    data = pd.read_csv(data_csv_name)

    data["game_id_str"] = data["game_id"].astype(str).str[1:]

    data = data.sort_values(by="game_id_str")

    data["time_idx"] = range(len(data))

    del data["game_id_str"]
    data.to_csv(data_csv_name, index=False)

    return data_csv_name

def apply_home(matchup):
    if "vs." in matchup:
        return matchup.split(" vs. ")[0]
    elif "@" in matchup:
        return matchup.split(" @ ")[1]

def apply_away(matchup):
    if "vs." in matchup:
        return matchup.split(" vs. ")[1]
    elif "@" in matchup:
        return matchup.split(" @ ")[0]

def add_teams_playing_column(data_csv_name):
    """Add home and away team columns"""
    data = pd.read_csv(data_csv_name)
    data["home_team"] = data["MATCHUP"].apply(apply_home)
    data["away_team"] = data["MATCHUP"].apply(apply_away)

    data.drop_duplicates(subset=["player_id", "game_id"], inplace=True)
    data.to_csv(data_csv_name, index=False)
    return data_csv_name

def apply_which_team(matchup):
    if "vs." in matchup:
        return matchup.split(" vs. ")[0]
    elif "@" in matchup:
        return matchup.split(" @ ")[0]

def add_which_team_column(data_csv_name):
    data = pd.read_csv(data_csv_name)
    data["team"] = data["MATCHUP"].apply(apply_which_team)
    data["was_home"] = False
    data.loc[data["team"] == data["home_team"], "was_home"] = True

    data.drop_duplicates(subset=["player_id", "game_id"], inplace=True)
    data.to_csv(data_csv_name, index=False)
    return data_csv_name

def apply_game_type(season_id):
    first_digit = str(season_id)[0]
    switch_dict = {
        "1": "preseason",
        "2": "regular",
        "3": "allstar",
        "4": "postseason",
        "5": "playin",
    }

    return switch_dict.get(first_digit, "Unknown")

def add_game_type(data_csv_name):
    # https://github.com/swar/nba_api/issues/220
    # 1 = pre season
    # 2 = regular
    # 3 = all star
    # 4 = finals/playoffs, post-season
    # 5 = play-in

    data = pd.read_csv(data_csv_name)
    data["game_type"] = data["SEASON_ID"].apply(apply_game_type)

    data.to_csv(data_csv_name, index=False)
    return data_csv_name

def clean_up_data(data_csv_name):
    data = pd.read_csv(data_csv_name)
    data = data.drop("VIDEO_AVAILABLE", axis=1)

    data.to_csv(data_csv_name, index=False)
    return data_csv_name

if __name__ == "__main__":
    # only do 3 players for testing
    testing = False

    # get active players, store in a csv
    columns = [
        "player_id",
        "game_id",
        "days_since_last_injury",
        "type_of_last_injury",
        "home_team",
        "away_team",
        "team",
        "was_home",
    ]
    data_csv_name = "data.csv"
    player_csv_name = "players.csv"

    players_csv = get_active_players(player_csv_name)
    #data_csv_name = instantiate_data_if_needed(data_csv_name, columns)
    #data_csv_name = add_nba_api_data(players_csv, data_csv_name, testing)
    data_csv_name = add_injury_data(players_csv, data_csv_name, testing)

    data_csv_name = turn_game_id_to_time_idx(data_csv_name)
    data_csv_name = add_teams_playing_column(data_csv_name)
    data_csv_name = add_which_team_column(data_csv_name)
    data_csv_name = add_game_type(data_csv_name)
    # add was finals??
    data_csv_name = clean_up_data(data_csv_name)


# --------------
#Importing header files
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#Reading the file
data=pd.read_csv(path)

#Code starts here

# Step 1
#Reading the file

#Creating a new variable to store the value counts
loan_status=data["Loan_Status"].value_counts()

#Plotting bar plot
loan_status.plot(kind="bar")

# Step 2
#Plotting an unstacked bar plot
property_and_loan=data.groupby(["Property_Area","Loan_Status"]).size().unstack()
property_and_loan.plot(kind="bar",stacked=False)
plt.xlabel("Property Area")
plt.ylabel("Loan Status")
plt.xticks(rotation=45)

#Changing the x-axis label

#Changing the y-axis label

#Rotating the ticks of X-axis

# Step 3
#Plotting a stacked bar plot
education_and_loan=data.groupby(["Education","Loan_Status"]).size().unstack()
education_and_loan.plot(kind="bar",stacked=True)
plt.xlabel("Education Status")
plt.ylabel("Loan Status")
plt.xticks(rotation=45)

#Changing the x-axis label

#Changing the y-axis label

#Rotating the ticks of X-axis

# Step 4
#Subsetting the dataframe based on 'Education' column
graduate=data[data["Education"]=="Graduate"]
not_graduate=data[data["Education"]=="Not Graduate"]
dd=graduate["LoanAmount"]
dd=dd.fillna(0)
dd.plot(kind="density",label="Graduate")
ss=not_graduate["LoanAmount"]
ss=ss.fillna(0)
ss.plot(kind="density",label="not_Graduate")

#Subsetting the dataframe based on 'Education' column

#Plotting density plot for 'Graduate'

#Plotting density plot for 'Graduate'

#For automatic legend display

# Step 5
#Setting up the subplots
fig ,(ax_1,ax_2,ax_3)=plt.subplots(nrows = 3 , ncols = 1)

#Plotting scatter plot
data.plot.scatter(x='ApplicantIncome',y="LoanAmount",ax=ax_1,title="Applicant Income")
data.plot.scatter(x='CoapplicantIncome',y="LoanAmount",ax=ax_2,title="Coapplicant Income")

#Setting the subplot axis title
data["TotalIncome"]=data["ApplicantIncome"]+data["CoapplicantIncome"]

#Plotting scatter plot
data.plot.scatter(x="TotalIncome",y="LoanAmount",ax=ax_3,title="Total Income")

#Setting the subplot axis title

#Creating a new column 'TotalIncome'

#Plotting scatter plot

#Setting the subplot axis title


import logging

from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver

from timeline.models import Timeline

# Models
class SubscribeRecord(models.Model):
    is_read = models.BooleanField(
        default=False,
        verbose_name='Запись прочитана пользователем'
    )
    record = models.OneToOneField(
        'blog.Record',
        on_delete=models.CASCADE,
        verbose_name='Запись',
    )

    class Meta:
        verbose_name = 'Запись из блога на которую подписан пользователь'
        verbose_name_plural = 'Записи из блога на которую подписан пользователь'

    def __str__(self):
        return f'{self.id}'

class SubscribeByBlog(models.Model):
    timeline = models.ForeignKey(
        'timeline.Timeline',
        on_delete=models.CASCADE,
        verbose_name='Лента записей'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='Дата создания'
    )
    subscribes_record = models.ManyToManyField(
        'blog.SubscribeRecord',
        verbose_name='Подписка на новость',
    )

    class Meta:
        verbose_name = 'Подписка на блог'
        verbose_name_plural = 'Подписки на блоги'

    def __str__(self):
        return f'{self.created_at}'

class Blog(models.Model):
    name = models.fields.CharField(
        max_length=150,
        verbose_name='Название'
    )
    user = models.OneToOneField(
        'auth.User',
        on_delete=models.CASCADE,
        verbose_name='Пользователь'
    )
    subscribes_by_blog = models.ManyToManyField(
        'blog.SubscribeByBlog',
        verbose_name='Подписки на блог',
    )

    class Meta:
        verbose_name = 'Блог'
        verbose_name_plural = 'Блоги'

    def __str__(self):
        return f'{self.name}'

class Record(models.Model):
    title = models.fields.CharField(
        max_length=150,
        verbose_name='Заголовок'
    )
    text = models.TextField(
        verbose_name='Текст'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='Дата создания'
    )
    blog = models.ForeignKey(
        'blog.Blog',
        on_delete=models.CASCADE,
        verbose_name='Блог',
    )

    class Meta:
        verbose_name = 'Запись'
        verbose_name_plural = 'Записи'

    def __str__(self):
        return f'{self.title}'

# Signals for models
@receiver(post_save, sender=User)
def create_blog(sender, instance, created, **kwargs):
    if created:
        Blog.objects.create(
            name=f'Блог пользователя {instance.username}',
            user=instance,
        )

@receiver(post_save, sender=Record)
def create_subscribe_record(sender, instance, created, **kwargs):
    from manager_tasks.tasks import send_notify
    if created:
        subscribes_by_blog = instance.blog.subscribes_by_blog.all()
        if subscribes_by_blog:
            send_notify.delay(instance.id, instance.blog.id)
            for subscribe_by_blog in subscribes_by_blog:
                subscribe_by_blog.subscribes_record.add(SubscribeRecord.objects.create(record=instance))

@receiver(post_delete, sender=Record)
def delete_subscribe_record(sender, instance, **kwargs):
    subscribes_by_blog = instance.blog.subscribes_by_blog.all()
    if subscribes_by_blog:
        for subscribe_by_blog in subscribes_by_blog:
            subscribe_by_blog.subscribes_record.filter(record=instance).delete()


import json
from pprint import pprint
from time import ctime

def lambda_response(status_code, message, **kwargs) -> dict:
    '''Presents the custom OleTalk API Gateway/server responses in standard HTTP format, for all communications sent
    from the API to the client.    \n

    Parameters:
    -----------
        status_code (int, req'd):
            Standard http codes.    \n

        msg (str, req'd):
            String displaying the message response. \n

        kwargs (dict, opt):
            Other relevant data (used in debugging) such as Exceptions, etc.    \n

    Returns:
    --------
        return (dict):
            Response to the client's requests in standard HTTP form.  \n

    '''
    data = {"message": message}
    for key in kwargs:
        data.update({key: kwargs[key]})
    return {
        'statusCode': status_code,     #   cCode, sCode, conf_cde, stat_cde
        'body': json.dumps(data)
    }

def html_response(status_code, html) -> dict:
    '''Responds with html (or web page) as its payload instead of a json object.    \n

    Parameter:
    ----------
        status_code (int/str, req):
            Standard HTTP Status Codes.     \n

        html (txt, req.):
            HTML formatted text.    \n

    Returns:
    --------
        return (dict, req):
            A response with HTML formatted text in the body.  \n

    '''
    return {
        "headers": {
            "Content-Type": "text/html"
        },
        "statusCode": status_code,
        "body": html
    }

def error_response(error_code, error_message, **kwargs) -> dict:
    '''Presents the Error in the required standard format,
    or all error communications from the API to the client.    \n

    Parameters:
    -----------
        error_code (int, req'd):
            HTTP Status/Error code for this error-type.    \n

        error_message (str, req'd):
            HTTP code for the error.    \n

        kwargs (dict, opt):
            The error details in key - value pairs giving further eg. stack trace, error type etc.    \n

    Returns:
    --------
        return (dict):
            Error presented in the standard HTTP response format.  \n

        '''
    #   should trigger logging and custom debugging functions.
    error_data = kwargs
    error_data.update({"errorMessage": error_message})
    error_data.update({"timestamp": ctime()})
    return {
        'statusCode': error_code,           #   usually 400 or 500  follows the same convention as http error codes  \n
        'body': json.dumps(error_data)      #   including the 'call stack'
    }

#

def respond(err=None, res=None, html=None) -> dict:
    '''Customizes response using a consistant, standard format for AWS API Gateway/AWS Lambda Functions.     \n

    Parameters:
    -----------
        err (exception/error object, opt):
            Exception class based object. Must be set to None if not used.   \n

        res (dict, opt):
            Key-Value pairs containing the data being sent to a client.     \n

        html (text, opt):
            HTML formatted text.    \n

    Returns:
    --------
        return (dict):
            Custom OleTalk API response in the standard http format.

    '''
    return {
        "headers": {
            "Access-Control-Allow-Origin": '*',
            "Access-Control-Allow-Methods": 'GET,POST,OPTIONS',
            "Access-Control-Allow-Headers": 'Content-Type',
            # "Content-Type": "text/html" if html else "application/json"
            "Content-Type": "text/html" if html else "application/json"
        },
        "statusCode": '400' if err else '200',
        "body": err if err else html if html else json.dumps(res)
    }


"""
Fix binary png images so that they're in a consistent binary format
Used for ground truth (_fg) images which can be saved by some image programs as 8 bit RGB pngs
rather than binary ones.
"""
import cv2
import os
import io

for img in os.listdir("/home/carter/Desktop/our-data/training/imgs"):
    if "_fg" in img:
        print(img)
        #read image as grey scale
        img_grey = cv2.imread("/home/carter/Desktop/our-data/training/imgs/"+img, cv2.IMREAD_GRAYSCALE)

        # define a threshold, 128 is the middle of black and white in grey scale
        thresh = 128

        # threshold the image
        img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]

        #save image
        cv2.imwrite("/home/carter/Desktop/our-data/training/imgs/"+img, img_binary)


#! python 3
"""
Program that reads in text files and lets users replace some keywords. Lastly, save as new file
"""

from pathlib import Path
import re

### read in text file
file_name = input("Enter relative path of file (eg. folder/file.txt): ")
p = Path(Path.cwd() / file_name)
input_file = open(p)
txt = input_file.read()
print(txt)

### find following keywords
kwRegex = re.compile(r'ADJECTIVE|NOUN|VERB|ADVERB')
kwList = kwRegex.findall(txt)

### get user to input string to replace
for i in kwList:
    replaceWord = input(f'Enter a {i}:')
    txt = txt.replace(i, replaceWord, 1)

### save updated text to new file
print(txt)

# consider identifying how many different parents levels are there
out_file_name = input("Enter relative path of new file (eg. folder/file.txt): ")
new_file = open(f"{Path.cwd() / out_file_name}", 'w')
new_file.write(txt)

input_file.close()
new_file.close()


from __future__ import print_function
import wave
import numpy as np
from struct import unpack

class WaveWrap(object):
    def __init__(self, filename, output_window_size, window_overlap, unpack_fmt="<{}h"):
        self.wav_buffer_size = output_window_size - window_overlap
        self.wave_file = wave.open(filename, 'r')
        self.length = self.wave_file.getnframes()
        self.output_window_size = output_window_size
        self.unpack_fmt = unpack_fmt.format(self.wav_buffer_size)

        self.cursor = 1
        self.n_windows = self.length / self.wav_buffer_size

        self.wav_data = np.zeros(self.output_window_size)
        self.fft_input = np.zeros(self.output_window_size)
        self.hamming = np.hamming(self.output_window_size)

    def next_pcm_window(self):
        try:
            wave_data = self.wave_file.readframes(self.wav_buffer_size)
        except Exception as e:
            print(e.message)
            wave_data = np.zeros(self.wav_buffer_size)
        self.cursor += 1
        return unpack(self.unpack_fmt, wave_data)

    def next_fft_input_window(self):
        np.roll(self.wav_data, -self.wav_buffer_size)
        self.wav_data[-self.wav_buffer_size:] = self.next_pcm_window()
        return self.wav_data * self.hamming

    def next_fft_output_window(self):
        fft = np.abs(np.fft.rfft(self.next_fft_input_window()))
        max_energy = max(fft)
        fft /= max_energy
        return fft

window_size = 4096 * 4
overlap = (window_size / 4) * 3

wav_wrap = WaveWrap(filename='bee.wav', output_window_size=window_size, window_overlap=overlap)

bin_size = wav_wrap.wave_file.getframerate() / float(window_size)
print('bin size', bin_size)

d = np.array([wav_wrap.next_fft_output_window() for _ in range(wav_wrap.n_windows)])

print('len(d) = ', len(d))

import matplotlib.pyplot as plt

plt.imshow(np.flip(d.T, 0), interpolation='nearest', aspect='auto')

plt.show()


import os

from PySide6 import QtWidgets
from PySide6.QtGui import QFont, QIcon
from PySide6.QtWidgets import (
    QCheckBox,
    QComboBox,
    QFileDialog,
    QFrame,
    QLabel,
    QLineEdit,
    QPushButton,
    QRadioButton,
    QSizePolicy,
    QSpacerItem,
    QTabWidget,
    QTextEdit,
    QWidget,
)

from nadeocr.GUI.functions.utils.extra import edit_config_ini, get_data, read_config_ini
from nadeocr.GUI.widgets.input_hotkey_widget import MainHotkeyExecution

_ROOT = os.path.abspath(os.path.dirname(__file__))

class OptionsWidget(QWidget):
    def __init__(self, parent):
        self.parent = parent
        super(OptionsWidget, self).__init__()
        self.setWindowTitle("Opciones")
        self.setFixedSize(425, 310)
        icon_path = get_data(_ROOT, "../../resources/assets", "icon.ico")

        self.setWindowIcon(QIcon(icon_path))

        self.config_reader = read_config_ini()

        layout = QtWidgets.QGridLayout()
        self.setLayout(layout)

        self.tabs = QTabWidget()
        self.tab_general = QWidget()
        self.tab_advanced = QWidget()
        self.tab_about = QWidget()

        self.tabs.addTab(self.tab_general, "General")
        self.tabs.addTab(self.tab_advanced, "Avanzado")
        self.tabs.addTab(self.tab_about, "Acerca de")

        # Create first tab layout
        self.tab_general.layout = QtWidgets.QGridLayout(self)
        self.tab_advanced.layout = QtWidgets.QGridLayout(self)
        self.tab_about.layout = QtWidgets.QGridLayout(self)

        # Add tabs to main widget
        layout.addWidget(self.tabs)
        self.tab_general.setLayout(self.tab_general.layout)
        self.tab_advanced.setLayout(self.tab_advanced.layout)
        self.tab_about.setLayout(self.tab_about.layout)

        # General Tab
        self.label_interface_user = QLabel("Interfaz de usuario")
        self.label_interface_user.setFont(QFont("Arial", 9, weight=QFont.Bold))
        self.tab_general.layout.addWidget(self.label_interface_user, 0, 0, 1, 5)

        self.label_language = QLabel("Idioma")
        self.tab_general.layout.addWidget(self.label_language, 1, 0)

        self.box_language = QComboBox()
        self.box_language.addItem("Español")
        self.box_language.addItem("Inglés")
        self.tab_general.layout.addWidget(self.box_language, 1, 1, 1, 5)

        self.label_notification_pos = QLabel("Posición notificación")
        self.tab_general.layout.addWidget(self.label_notification_pos, 2, 0, 1, 2)

        spanish_to_pos_text = {
            "TopLeft": "Esquina superior izquierda",
            "TopRight": "Esquina superior derecha",
            "BottomLeft": "Esquina inferior izquierda",
            "BottomRight": "Esquina inferior derecha",
        }

        notification_pos = self.config_reader["user_settings"]["notification_pos"]
        self.box_notification_pos = QComboBox()
        self.box_notification_pos.addItem("Esquina superior izquierda")
        self.box_notification_pos.addItem("Esquina superior derecha")
        self.box_notification_pos.addItem("Esquina inferior izquierda")
        self.box_notification_pos.addItem("Esquina inferior derecha")
        self.box_notification_pos.setCurrentText(spanish_to_pos_text[notification_pos])
        self.tab_general.layout.addWidget(self.box_notification_pos, 2, 1, 1, 5)

        self.separatorLine = QFrame(frameShape=QFrame.HLine)
        self.separatorLine.setLineWidth(0)
        self.separatorLine.setMidLineWidth(5)
        self.separatorLine.setStyleSheet("font: 9pt; color: grey;")
        self.tab_general.layout.addWidget(self.separatorLine, 4, 0, 1, 6)

        self.label_preferences_user = QLabel("Preferencias")
        self.label_preferences_user.setFont(QFont("Arial", 9, weight=QFont.Bold))
        self.tab_general.layout.addWidget(self.label_preferences_user, 5, 0)

        self.checkbox_run_startup = QCheckBox("Ejecutar al arranque", self)
        self.tab_general.layout.addWidget(self.checkbox_run_startup, 6, 0, 1, 2)

        hotkey = self.config_reader["user_settings"]["shortcut_key"]

        self.label_preference_hotkey = QLabel("Atajo para escaneo")
        self.tab_general.layout.addWidget(self.label_preference_hotkey, 7, 0, 1, 2)
        self.parent.button_input_hotkey = QPushButton(self)
        self.parent.button_input_hotkey.setText(hotkey)
        self.tab_general.layout.addWidget(self.parent.button_input_hotkey, 7, 1, 1, 5)
        self.parent.button_input_hotkey.clicked.connect(self.on_button_hotkey_click)

        self.label_preference_scan = QLabel("Motor de escaneo preferido")
        self.tab_general.layout.addWidget(self.label_preference_scan, 8, 0, 1, 3)

        self.radio_button_google = QRadioButton("Google (recomendado)")
        self.tab_general.layout.addWidget(self.radio_button_google, 9, 0)

        self.label_separator = QLabel("           ")
        self.tab_general.layout.addWidget(self.label_separator, 9, 1)
        self.radio_button_mangaocr = QRadioButton("MangaOCR")
        self.tab_general.layout.addWidget(self.radio_button_mangaocr, 9, 2)
        self.radio_button_paddleocr = QRadioButton("PaddleOCR")
        self.tab_general.layout.addWidget(self.radio_button_paddleocr, 9, 3)
        self.radio_button_paddleocr.setDisabled(True)

        ocr_provider = self.config_reader["provider_settings"]["ocr_provider"]

        # OCR client selection based on the config file
        if ocr_provider == "Google":
            self.radio_button_google.setChecked(True)

        elif ocr_provider == "MangaOCR":
            self.radio_button_mangaocr.setChecked(True)

        self.spaceItem = QSpacerItem(100, 10, QSizePolicy.Expanding)
        self.tab_general.layout.addItem(self.spaceItem, 10, 0)

        self.button_accept = QPushButton("Aceptar", self)
        self.button_cancel = QPushButton("Cancelar", self)
        self.button_accept.clicked.connect(self.accept_button)
        self.button_cancel.clicked.connect(self.cancel_button)

        self.tab_general.layout.addWidget(self.button_accept, 16, 2, 2, 1)
        self.tab_general.layout.addWidget(self.button_cancel, 16, 3, 2, 2)

        # Tab Advanced
        self.label_paths_user = QLabel("Rutas de archivos")
        self.label_paths_user.setFont(QFont("Arial", 9, weight=QFont.Bold))
        self.tab_advanced.layout.addWidget(self.label_paths_user, 0, 0, 1, 4)

        self.label_path_google = QLabel("Credenciales Google")
        self.tab_advanced.layout.addWidget(self.label_path_google, 1, 0, 1, 2)

        self.line_edit_google = QLineEdit(self)
        self.tab_advanced.layout.addWidget(self.line_edit_google, 2, 0, 1, 5)

        self.button_accept_google = QPushButton("...", self)
        self.tab_advanced.layout.addWidget(self.button_accept_google, 2, 5, 1, 1)
        self.button_accept_google.clicked.connect(self.accept_button_path_google)

        path_string_google = self.config_reader["path_settings"]["credentials_google"]
        if path_string_google.strip() == "":
            self.line_edit_google.setText("No se ha definido")
        else:
            self.line_edit_google.setText(path_string_google)

        self.button_accept2 = QPushButton("Aceptar", self)
        self.button_cancel2 = QPushButton("Cancelar", self)
        self.button_accept2.clicked.connect(self.accept_button)
        self.button_cancel2.clicked.connect(self.cancel_button)

        self.tab_advanced.layout.addWidget(self.button_accept2, 16, 4, 2, 1)
        self.tab_advanced.layout.addWidget(self.button_cancel2, 16, 5, 2, 1)

        self.labelAbout = QLabel(
            "NadeOCR v1.0.1 (10/10/2022)
An easy and fast-to-use tool for scanning text anywhere with Google's Vision API and other third party services.

"
            "This project wouldn't be possible without:
- Google's Vision API for detecting and recognising a wide variety of languages including, but not limited to, English, Japanese and Spanish.
- The awesome Manga-OCR model by Maciej Budyś for recognizing Japanese characters in manga.

"
            "More information about this project can be found here."
        )
        self.labelAbout.setWordWrap(True)
        self.labelAbout.setOpenExternalLinks(True)
        self.tab_about.layout.addWidget(self.labelAbout, 0, 0, 1, 6)

        self.button_accept3 = QPushButton("Cerrar", self)
        self.button_accept3.clicked.connect(self.cancel_button)
        self.tab_about.layout.addWidget(self.button_accept3, 16, 5, 2, 1)

        self.tab_general.layout.setRowStretch(16, 5)
        self.tab_advanced.layout.setRowStretch(15, 5)
        self.tab_about.layout.setRowStretch(15, 5)

        self.show()

    def accept_button_path_google(self):
        path_google_fdialog = QFileDialog.getOpenFileName(
            self, "Abrir archivo", "", "Credenciales (*.json)", ""
        )
        self.line_edit_google.setText(path_google_fdialog[0])
        edit_config_ini("path_settings", "credentials_google", path_google_fdialog[0])

    def accept_button(self):
        if self.radio_button_google.isChecked():
            edit_config_ini("provider_settings", "ocr_provider", "Google")
        elif self.radio_button_mangaocr.isChecked():
            edit_config_ini("provider_settings", "ocr_provider", "MangaOCR")

        if self.box_notification_pos.currentText() == "Esquina superior izquierda":
            edit_config_ini("user_settings", "notification_pos", "TopLeft")
        elif self.box_notification_pos.currentText() == "Esquina superior derecha":
            edit_config_ini("user_settings", "notification_pos", "TopRight")
        elif self.box_notification_pos.currentText() == "Esquina inferior derecha":
            edit_config_ini("user_settings", "notification_pos", "BottomRight")
        elif self.box_notification_pos.currentText() == "Esquina inferior izquierda":
            edit_config_ini("user_settings", "notification_pos", "BottomLeft")

        self.close()

    def cancel_button(self):
        self.close()

    def on_button_hotkey_click(self):
        self.input_hotkey_window = MainHotkeyExecution(parent=self.parent)


# Imports
import bs4
import requests
from bs4 import BeautifulSoup

# URL Base
url_base = 'https://www.who.int'
def scrapingCovid():
    # URL com os dados do COVID-19 (coloque essa URL no seu navegador e compreenda os dados disponíveis)
    url = 'https://www.who.int/emergencies/diseases/novel-coronavirus-2019/situation-reports/'

    # Obtém os dados da URL
    response = requests.get(url)

    # Leitura do conteúdo
    soup = BeautifulSoup(response.content, 'lxml')

    # Lista temporária
    tempList = []

    # Loop pelos elementos da página para buscar as tags "a" que indicam URLs
    # Estamos buscando os links dos relatórios chamados 'situation-reports'
    for element in soup.find_all('a', href = True):
        if 'situation-reports' in str(element):
            tempList.append(str(element))

    # Visualiza alguns registros da lista
    # print(tempList[1:5])

    # Lista para as URLs dos arquivos em pdf
    pdfurls = []

    # Variável que indica se é o último relatório
    lastreport = None

    # Loop para limpar cada registro da lista temporária, extraindo somente a URL dos arquivos em pdf
    for url in tempList:

        # Replace da tag de link html
        x = url.replace('<a href="', '')

        # Busca pelo sinal de interrogação para usar como índice de final do endereço de cada arquivo pdf
        index = x.find('?')

        # Extrai tudo de 0 até o índice
        extraction = x[0:index]

        # Nas posições de 68:70 do endereço do arquivo pdf estão os dígitos do número do relatório
        reportnumber = extraction[68:70]

        # Verifica se o relatório é o último
        # Se não for, adicionamos a url_base ao nome do arquivo e gravamos na lista de URLs
        if reportnumber != lastreport:
            pdfurls.append(url_base + extraction)

        # Atualiza a variável com o número do último relatório (posições de 68:70)
        lastreport = extraction[68:70]

    # Total de arquivos pdf
    print(len(pdfurls))

    # Visualiza amostra da lista de URLs dos arquivos pdf
    return pdfurls


import os
from torchvision.transforms import CenterCrop
from PIL import Image

completed_processing = set()

source_dir = "C:/Users/jessi/Documents/Master_Courses/MIE1517_IDL/Project/dataset/dataset/"
output_dir = "C:/Users/jessi/Documents/Master_Courses/MIE1517_IDL/Project/dataset/processed_dataset/"

i = 0
nb_excluded = 0

with os.scandir(source_dir) as it:
    for item in it:
        i += 1
        if item.name.endswith('.jpg'):
            if item.name in completed_processing:
                continue

            completed_processing.add(item.name)

            with Image.open(source_dir + item.name) as image:

                width, height = image.size
                if width < 128 or height < 128:
                    nb_excluded += 1
                    continue

                crop_img = CenterCrop(128)(image)
                crop_img.save(output_dir + item.name)

        if i % 50 == 0:
            print(i)


# -*- coding: utf-8 -*-
"""Home dashboard layout."""

import locale

from apps import utils_dash

import dash_bootstrap_components as dbc

import dash_core_components as dcc

import pandas as pd

import plotly.express as px
import plotly.graph_objects as go

from portfolio.fii import FiiPortfolio
from portfolio.funds import FundsPortfolio
from portfolio.stocks import StocksPortfolio

print("locale: ", locale.setlocale(locale.LC_ALL, utils_dash.my_locale))

stocksportfolio = StocksPortfolio()
fiiportfolio = FiiPortfolio()
fundsportfolio = FundsPortfolio()

#############################################################################
# Figures
#############################################################################

fig_class_distribution_labels = ["Stocks", "FIIs", "Funds"]
fig_class_distribution_values = []
fig_class_distribution_values.append(stocksportfolio.total_invest()[1])
fig_class_distribution_values.append(fiiportfolio.total_invest[1])
fig_class_distribution_values.append(fundsportfolio.total_invest[1])

fig_class_distribution = go.Figure(
    data=[
        go.Pie(
            labels=fig_class_distribution_labels, values=fig_class_distribution_values
        )
    ]
)
fig_class_distribution.update_layout(title="Porfolio Class Distribution", title_x=0.5)

df_fii_money_monthly = fiiportfolio.fiitransactions.money_invested_monthly()
df_fii_money_monthly["Class"] = "FII"
df_fun_money_monthly = fundsportfolio.money_invested_monthly()
df_fun_money_monthly.rename(columns={"Value": "Operation Cost"}, inplace=True)
df_fun_money_monthly["Class"] = "Funds"
df_sto_money_monthly = stocksportfolio.stockstransactions.money_invested_monthly()
df_sto_money_monthly["Class"] = "Stocks"
fig_money_inv_monthly = px.bar(
    pd.concat([df_fii_money_monthly, df_fun_money_monthly, df_sto_money_monthly]),
    x="Date",
    y="Operation Cost",
    labels={"Operation Cost": "Amount Invested", "Date": "Month"},
    color="Class",
    title="Money Invested Monthly",
)
fig_money_inv_monthly.update_layout(
    title_x=0.5, yaxis={"tickprefix": utils_dash.graph_money_prefix}
)
fig_money_inv_monthly.update_xaxes(rangeslider_visible=True)
fig_money_inv_monthly.update_layout(
    title_x=0.5,
    xaxis={
        "rangeselector": {
            "buttons": [
                {"count": 1, "label": "1m", "step": "month", "stepmode": "backward"},
                {"count": 6, "label": "6m", "step": "month", "stepmode": "backward"},
                {"count": 1, "label": "YTD", "step": "year", "stepmode": "todate"},
                {"count": 1, "label": "1y", "step": "year", "stepmode": "backward"},
                {"count": 2, "label": "2y", "step": "year", "stepmode": "backward"},
                {"count": 5, "label": "5y", "step": "year", "stepmode": "backward"},
                {"step": "all"},
            ],
        },
        "rangeslider": {"visible": False},
        "type": "date",
    },
)

#############################################################################
# layout
#############################################################################

layout = dbc.Container(
    [
        dbc.Row(
            [
                dbc.Col(
                    [
                        dcc.Graph(
                            id="fig_class_distribution_id",
                            figure=fig_class_distribution,
                        )
                    ],
                ),
                dbc.Col(
                    [
                        dcc.Graph(
                            id="fig_money_inv_monthly_id",
                            figure=fig_money_inv_monthly,
                        )
                    ],
                ),
            ]
        ),
    ],
    fluid=True,
)

# vim: ts=4


#################################################################################
# Description:  Class for generating augmented data for training of
#               neural network and SVM
#
# Authors:      Petr Buchal         <petr.buchal@lachub.cz>
#               Martin Ivanco       <ivancom.fr@gmail.com>
#               Vladimir Jerabek    <jerab.vl@gmail.com>
#
# Date:     2019/04/13
#
# Note:     This source code is part of project created on UnIT extended 2019.
#################################################################################

import random
import argparse
from datetime import datetime

import cv2
import numpy as np
from matplotlib import pyplot

from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator

from image import Image
from tools import parse_data

def get_args():
    """
    method for parsing of arguments
    """
    parser = argparse.ArgumentParser()

    parser.add_argument('--batches', type="int", action="store", default=100,
                        help='Number of generated batch of size 32.')

    args = parser.parse_args()

    return args

#Class storing data for augmentation
class DataSet(object):
    def __init__(self, ImageList):
        self.Images = ImageList
        self.p_images, self.labels, self. grand_truths = DataSet.strip_futilities(ImageList)
        random.seed(datetime.now())

    #Function for augmentation images
    #@param: imageArr   - is numpy array of images of shape (n, 1, height, width)
    #                                                        ^-- number of images
    #@param: grandArr   - is numpy array of grand truths images of shape (n, 1, height, width)
    #@param: batchSize  - is integer, which is less or equal 'n'
    #@return: new_img   - array of augmented images of shape (batchSize, 1, height, width)
    #@return: new_g_t   - array of augmented grand truths images of shape (batchSize, 1, height, width)
    @staticmethod
    def augmentImage(imageArr, grandArr, batchSize):

        for imgs in [imageArr, grandArr]:
            for img in imgs:
                h, w = img[0].shape
                img[0,0,:] = 0
                img[0,h-1,:] = 0
                img[0,:,0] = 0
                img[0,:,w-1] = 0

        shift = 0.2
        data_gen_args = dict(   data_format="channels_first",
                                rotation_range=90,
                                height_shift_range=shift,
                                width_shift_range=shift,
                                zoom_range=0.2,
                                horizontal_flip=True,
                                vertical_flip=True)
        #creating Image generator same for image data and grand truths data
        img_dataGen = ImageDataGenerator(**data_gen_args)
        g_tr_dataGen = ImageDataGenerator(**data_gen_args)

        seed = random.randint(0,65535)
        for new_img in img_dataGen.flow(imageArr, seed=seed, batch_size=batchSize):
            new_img = np.uint16(new_img)
            break
        for new_g_t in g_tr_dataGen.flow(grandArr, seed=seed, batch_size=batchSize):
            new_g_t = np.uint16(new_g_t)
            break

        return new_img, new_g_t

    #Function for striping unneeded data from Image class
    #@param: data   - List of Image class       ^-- for more info see image.py
    #@return: images        - numpy array of images of shape (n, 1, height, width)      %note: 16-bit grayscale
    #@return: labels        - numpy array of booleans of shape (n,1),
    #                           - 0 mean, that i-th image is doesn't contain ellipse
    #                           - 1 mean, that i-th image contains ellipse
    #@return: grand_truths  - numpy array of images of shape (n, 1, height, width)      %note: 16-bit grayscale
    @staticmethod
    def strip_futilities(data):
        images = []
        labels = []
        grand_truths = []
        for i, item in enumerate(data):
            images.append(np.array([item.processed_image]))
            labels.append(np.array([item.ellipse]))
            grand_truths.append(np.array([item.processed_ground_truths]))

        return np.array(images), np.array(labels), np.array(grand_truths)

    #Method for getting augmented data for training neural netowrk
    #@param batchSize   - nuber of images in one training epoche
    def getBatch(self, batchSize, isClassNet=True):
        img, g_truths = DataSet.augmentImage(self.p_images, self.grand_truths, batchSize)
        labels = []

        if batchSize > len(self.p_images):
            while len(img) != batchSize:
                curSize = batchSize - len(img)
                img2, g_truths2 = DataSet.augmentImage(self.p_images, self.grand_truths, curSize)
                img = np.concatenate((img, img2), axis=0)
                g_truths = np.concatenate((g_truths, g_truths2), axis=0)

        #get labels
        for i in range(len(img)):
            unique, counts = np.unique(g_truths[i], return_counts=True)
            if unique[-1] > 60000:
                labels.append(np.array([1]))
            else:
                labels.append(np.array([0]))

        if isClassNet:
            return img, np.array(labels)
        else:
            return img, g_truths

if __name__ == "__main__":
    args = parse_args()

    trn_data = parse_data("./data_training/ground_truths_develop.csv", "./data_training/images/", "./data_training/ground_truths/")
    myData = DataSet(trn_data)

    if not os.path.exists("./images_png"):
        os.makedirs("./images_png")

    counter = 0
    for e in range(args.batches):
        x, y = myData.getBatch(32)
        for i, item in enumerate(x):
            print("Generated batches: {}" .format(counter))
            new = cv2.cvtColor(item[0], cv2.COLOR_GRAY2BGR)
            if y[i] == 0:
                cv2.imwrite("./images_png/{}_F.png" .format(counter), new)
            if y[i] == 1:
                cv2.imwrite("./images_png/{}_T.png" .format(counter), new)
            counter += 1


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

import abc
import importlib

from django.core.exceptions import ImproperlyConfigured
from django.conf import settings

class BaseEventsPushBackend(object, metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def emit_event(self, message:str, *, routing_key:str, channel:str="events"):
        pass

def load_class(path):
    """
    Load class from path.
    """

    mod_name, klass_name = path.rsplit('.', 1)

    try:
        mod = importlib.import_module(mod_name)
    except AttributeError as e:
        raise ImproperlyConfigured('Error importing {0}: "{1}"'.format(mod_name, e))

    try:
        klass = getattr(mod, klass_name)
    except AttributeError:
        raise ImproperlyConfigured('Module "{0}" does not define a "{1}" class'.format(mod_name, klass_name))

    return klass

def get_events_backend(path:str=None, options:dict=None):
    if path is None:
        path = getattr(settings, "EVENTS_PUSH_BACKEND", None)

        if path is None:
            raise ImproperlyConfigured("Events push system not configured")

    if options is None:
        options = getattr(settings, "EVENTS_PUSH_BACKEND_OPTIONS", {})

    cls = load_class(path)
    return cls(**options)


import json

filename = r'/home/xxm/下载/qlora/data/estate_qa.json'
with open(filename, 'r', encoding='utf-8') as f:
    lines = f.readlines()
    for line in lines:
        line = line.strip()
        example = json.loads(line)
        content = example['output']
        if content == '':
            print(example)


from flask import Flask
from flask import render_template, url_for, jsonify, request
from game import Game

app = Flask(__name__)

@app.route('/')
def main():
    return render_template('index.html')

game=0
@app.route('/init', methods=['POST'])
def init():
    global game
    game = Game()
    game.start()

    # I have no idea why this is necessary but without the list comprehension it
    # breaks.
    data = [int(x) for x in game.grid.flatten()]
    response = jsonify({'board': data})
    return response

directions = { 'U': (1,-1), 'D': (1, 1), 'L': (0,-1), 'R': (0, 1) }

@app.route('/move', methods=['POST'])
def move():
    d = request.args['direction']
    game.move(*directions[d.upper()])

    response = jsonify({ 'board': [int(x) for x in game.grid.flatten()] })
    return response


import numpy
from matplotlib import pyplot
from utils import timeit

@timeit
def get_data():
    data = []
    with open('input.txt') as input_file:
        for line in input_file:
            points = tuple((int(x), int(y)) for x, y in (point.split(',') for point in line.strip().split(' -> ')))
            data.append(points)
    return data

def build_grid(paths, infinite_floor=False):
    paths.append(((500, 0),))
    min_x = min(point[0] for path in paths for point in path)
    min_y = min(point[1] for path in paths for point in path)
    max_x = max(point[0] for path in paths for point in path)
    max_y = max(point[1] for path in paths for point in path)

    if infinite_floor:
        max_y += 2
        min_x = min(min_x, 500 - (max_y - min_y) - 2)
        max_x = max(max_x, 500 + (max_y - min_y) + 2)
        paths = [((min_x, max_y), (max_x, max_y))] + paths

    paths = tuple(tuple((x - min_x, max_y - y) for x, y in path) for path in paths)
    start_point = paths[-1][0]

    grid = numpy.full((max_x - min_x + 1, max_y - min_y + 1), '.')
    for path in paths:
        for (a_x, a_y), (b_x, b_y) in zip(path, path[1:]):
            a_x, b_x = min(a_x, b_x), max(a_x, b_x)
            a_y, b_y = min(a_y, b_y), max(a_y, b_y)

            grid[a_x:b_x + 1, a_y:b_y + 1] = '#'

    return grid, start_point

def get_resting_place(path, grid):
    x, y = path[-1]
    while True:
        if not 0 <= x < grid.shape[0] or not 0 <= y < grid.shape[1]:
            return None
        y -= 1
        if grid[x][y] == '.':
            pass
        elif grid[x - 1][y] == '.':
            x -= 1
        elif grid[x + 1][y] == '.':
            x += 1
        else:
            return path
        path.append((x, y))

@timeit
def part_1(paths, infinite_floor=False):
    grid, start_point = build_grid(paths, infinite_floor)

    path = [start_point]
    count = 0

    while path:
        path = get_resting_place(path, grid)
        if not path:
            break
        x, y = path.pop()
        grid[x][y] = 'o'
        count += 1

    pyplot.imshow(numpy.rot90(grid.view(numpy.uint32)))
    pyplot.show()
    return count

@timeit
def part_2(paths):
    return part_1.func(paths, infinite_floor=True)

def main():
    data = get_data()
    part_1(data)
    part_2(data)

if __name__ == "__main__":
    main()


import re
import keyword
import os
from collections import Counter

def add_keywords(python_kw, cpp_kw, java_kw):
    # keyword.kwlist is a list with python language keywords
    python_kw.extend(keyword.kwlist)
    # list of cpp keywords: http://www.cplusplus.com/doc/oldtutorial/variables/
    cpp_kw.extend(['asm', 'auto', 'bool', 'break', 'case', 'catch', 'char', 'class', 'const', 'const_cast', 'continue',
                   'default', 'delete', 'do', 'double', 'dynamic_cast', 'else', 'enum', 'explicit', 'export', 'extern',
                   'false', 'float', 'for', 'friend', 'goto', 'if', 'inline', 'int', 'long', 'mutable', 'namespace',
                   'new', 'operator', 'private', 'protected', 'public', 'register', 'reinterpret_cast', 'return',
                   'short', 'signed', 'sizeof', 'static', 'static_cast', 'struct', 'switch', 'template', 'this',
                   'throw', 'true', 'try', 'typedef', 'typeid', 'typename', 'union', 'unsigned', 'using', 'virtual',
                   'void', 'volatile', 'wchar_t', 'while'])
    # list of java keywords: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
    java_kw.extend(['abstract', 'continue', 'for', 'new', 'switch', 'assert', 'default',  'goto', 'package',
                    'synchronized', 'boolean', 'do', 'if', 'private', 'this', 'break', 'double', 'implements',
                    'protected', 'throw', 'byte', 'else', 'import', 'public', 'throws', 'case', 'enum', 'instanceof',
                    'return', 'transient', 'catch', 'extends', 'int', 'short', 'try', 'char', 'final', 'interface',
                    'static', 'void', 'class', 'finally', 'long', 'strictfp', 'volatile', 'const', 'float', 'native',
                    'super', 'while'])

def word_frequency(file):
    # separate words in the given file
    words = re.findall(r'\w+', open(file).read().lower())
    # returns a counter with the number of appearances of each word in the file
    word_counter = Counter(words)
    return word_counter

def guess_language(file):
    python_kw = []
    cpp_kw = []
    java_kw = []
    # fill the lists with each language keywords
    add_keywords(python_kw, cpp_kw, java_kw)
    # get words frequency
    word_counter = word_frequency(file)
    # count appearances of each language keywords in the given file
    kw_counter = {'python': 0, 'cpp': 0, 'java': 0}
    for kw in python_kw:
        kw_counter['python'] += word_counter[kw]
    for kw in cpp_kw:
        kw_counter['cpp'] += word_counter[kw]
    for kw in java_kw:
        kw_counter['java'] += word_counter[kw]
    # guess language depending on the number of each language keywords encountered
    language = max(kw_counter, key=kw_counter.get)
    return language

def main():
    # Put the files to test in directories called 'cpp', 'java' and 'python'. The output will show a summary of the
    # given files, the prediction success percentage and the files in which it failed.
    cpp_files = os.listdir('cpp/')
    java_files = os.listdir('java/')
    python_files = os.listdir('python/')
    total_files = cpp_files.__len__() + java_files.__len__() + python_files.__len__()
    total_hits = 0

    # Iterate over all the files
    for file in cpp_files:
        if guess_language('cpp/' + file) == 'cpp':
            total_hits = total_hits + 1
        else:
            print('Missed prediction: ', file, '\tLanguage: cpp\tPredicted: ', guess_language('cpp/' + file))
    for file in java_files:
        if guess_language('java/' + file) == 'java':
            total_hits = total_hits + 1
        else:
            print('Missed prediction: ', file, '\tLanguage: java\tPredicted: ', guess_language('java/' + file))
    for file in python_files:
        if guess_language('python/' + file) == 'python':
            total_hits = total_hits + 1
        else:
            print('Missed prediction: ', file, '\tLanguage: python\tPredicted: ', guess_language('python/' + file))

    # print summary
    print('\nTotal files: ', total_files)
    print('Files correctly classified: ', total_hits)
    print('Hit percentage: ', round(total_hits / total_files * 100, 2), '%')

if __name__ == "__main__":
    main()


import json

from tags.tests.base_test import TagsBaseTestCase

class TagNamespaceTestCase(TagsBaseTestCase):
    fixtures = ["test_authentication.yaml", "test_marketplace.yaml"]

    def test_create_scoped_namespace(self):
        ##############################
        # Creation of global namespace

        self.login("17admin")
        res = self.post(
            "/tags/namespaces/", {"scoped_to_model": "global", "name": "users"}
        )
        self.assertStatusCode(res, 201)

        # Check cannot create two global namespaces with the same name
        res = self.post(
            "/tags/namespaces/", {"scoped_to_model": "global", "name": "users"}
        )

        self.assertStatusCode(res, 400)

        ############################################
        # Creation of a namespace for an association

        self.login("17admin_pdm")
        res = self.post(
            "/tags/namespaces/",
            {"scoped_to_model": "association", "scoped_to_pk": "pdm", "name": "farine"},
        )
        self.assertStatusCode(res, 201)

        # Try to get all namespaces
        res = self.get("/tags/namespaces/")
        self.assertStatusCode(res, 200)
        self.assertEqual(len(res.data["results"]), 2)
        self.assertSetEqual(
            {n["name"] for n in res.data["results"]}, {"users", "farine"}
        )

        # Try to get only the namespaces for the association + globals
        res = self.get("/tags/namespaces/association/pdm/")
        self.assertStatusCode(res, 200)
        self.assertEqual(len(res.data["namespaces"]), 2)
        self.assertSetEqual(
            {n["name"] for n in res.data["namespaces"]}, {"farine", "users"}
        )

        # Try to get namespace forgetting the scope
        res = self.post(
            "/tags/namespaces/", {"scoped_to_model": "association", "name": "test_v"}
        )
        self.assertStatusCode(res, 400)

        res = self.post("/tags/namespaces/", {"scoped_to_pk": "pdm", "name": "test_w"})
        self.assertStatusCode(res, 400)

        # Try to get global namespace
        res = self.get("/tags/namespaces/?scoped_to_model=global")
        self.assertStatusCode(res, 200)
        self.assertEqual(len(res.data["results"]), 1)
        self.assertSetEqual({n["name"] for n in res.data["results"]}, {"users"})

        #######################################
        # Creation without proper authorization
        self.login("17admin_biero")
        res = self.post(
            "/tags/namespaces/",
            {"scoped_to_model": "association", "scoped_to_pk": "pdm", "name": "test_x"},
        )
        self.assertStatusCode(res, 403)

        self.login("17admin_biero")
        res = self.post(
            "/tags/namespaces/", {"scoped_to_model": "global", "name": "test_y"}
        )
        self.assertStatusCode(res, 403)

    def test_edit_namespace(self):
        ###########################
        # Create needed namespaces
        self.login("17admin")
        res = self.post(
            "/tags/namespaces/", {"scoped_to_model": "global", "name": "users"}
        )
        self.assertStatusCode(res, 201, user_msg=res.data)
        namespace_user = json.loads(res.content)

        self.login("17admin_pdm")
        res = self.post(
            "/tags/namespaces/",
            {"scoped_to_model": "association", "scoped_to_pk": "pdm", "name": "farine"},
        )
        self.assertStatusCode(res, 201)
        namespace_farine = json.loads(res.content)

        ###########################
        # Basic namespace edition

        self.login("17admin_biero")
        # Global namespace
        res = self.patch(
            "/tags/namespaces/{}/".format(namespace_user["id"]), {"name": "coucou"}
        )
        self.assertStatusCode(res, 403)

        res = self.delete("/tags/namespaces/{}/".format(namespace_user["id"]))
        self.assertStatusCode(res, 403)

        # Specific namespace that doesn't belong to us
        res = self.patch(
            "/tags/namespaces/{}/".format(namespace_farine["id"]), {"name": "coucou"}
        )
        self.assertStatusCode(res, 403)

        res = self.delete("/tags/namespaces/{}/".format(namespace_farine["id"]))
        self.assertStatusCode(res, 403)

        self.login("17admin")
        res = self.patch(
            "/tags/namespaces/{}/".format(namespace_user["id"]), {"name": "coucou"}
        )
        self.assertStatusCode(res, 200)

        self.login("17admin_pdm")
        res = self.patch(
            "/tags/namespaces/{}/".format(namespace_farine["id"]), {"name": "coucou"}
        )
        self.assertStatusCode(res, 200)

        ############################
        # Try to move the namespace
        self.login("17admin_pdm")
        res = self.patch(
            "/tags/namespaces/{}/".format(namespace_user["id"]),
            {"scoped_to_model": "global"},
        )
        self.assertStatusCode(res, 400)

        res = self.patch(
            "/tags/namespaces/{}/".format(namespace_user["id"]),
            {"scoped_to_model": "association", "scoped_to_pk": "biero"},
        )
        self.assertStatusCode(res, 400)

    def test_get_namespace_for_object(self):
        self.login("17admin_pdm")
        res = self.post(
            "/tags/namespaces/",
            {"scoped_to_model": "association", "scoped_to_pk": "pdm", "name": "farine"},
        )
        self.assertStatusCode(res, 201)

        namespace_id = res.data["id"]

        results = self.get("/tags/namespaces/?product=4").data[
            "results"
        ]  # get namespaces for a pdm product
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]["id"], namespace_id)

        results = self.get("/tags/namespaces/?product=3").data[
            "results"
        ]  # get namespaces for a biero product
        self.assertEqual(len(results), 0)


import os

from qgis.PyQt import QtWidgets, uic
from qgis.PyQt.QtCore import pyqtSignal
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QApplication
from qgis.utils import iface
from qgis.core import (
    QgsProject,
    QgsPointXY,
    QgsCoordinateReferenceSystem,
    QgsCoordinateTransform,
)

from qgis.gui import QgsVertexMarker

# using utils
from .utils import (
    draw_rect_bound,
    icon,
    get_nlp,
    bk_10000,
    bk_2500,
    bk_1000,
    bk_500,
    bk_250,
    dialogBox,
)
from .maptools import MapTool

FORM_CLASS, _ = uic.loadUiType(
    os.path.join(os.path.dirname(__file__), "../ui/gambar_nlp.ui")
)

# constants
skala = ["1:10000", "1:2500", "1:1000", "1:500", "1:250"]

# constants for NLP
x_origin = 32000
y_origin = 282000
grid_10rb = 6000
grid_2500 = 1500
grid_1000 = 500
grid_500 = 250
grid_250 = 125

class DrawNLPDialog(QtWidgets.QDialog, FORM_CLASS):
    """Dialog for NLP Dialog"""

    closingPlugin = pyqtSignal()

    def __init__(self, parent=iface.mainWindow()):
        self.iface = iface
        self.canvas = iface.mapCanvas()
        super(DrawNLPDialog, self).__init__(parent)
        self.setupUi(self)
        self.project = QgsProject()
        self.setWindowIcon(icon("icon.png"))

        copy_icon = QIcon(":/images/themes/default/mActionEditCopy.svg")

        self.project.instance().crsChanged.connect(self.set_epsg)

        # Clipboard
        self.clipboard = QApplication.clipboard()

        self.ambil_titik.checked = False
        self.point = None

        # setup map tool
        self.previousMapTool = self.canvas.mapTool()
        self.epsg = self.project.instance().crs().authid()
        self.crs_tm3.setText(self.project.instance().crs().description())

        # copy to clipboard
        self.copyTeksNLP.setIcon(copy_icon)
        self.copyTeksNLP.clicked.connect(self.copy_clicked)

        # self.skala_peta.currentIndexChanged.connect(self.get_nlp_text())
        self.ambil_titik.clicked.connect(self.on_pressed)

        self.skala_peta.addItems(skala)

    def closeEvent(self, event):
        try:
            self.closingPlugin.emit()
            self.iface.actionPan().trigger()
            MapTool(self.canvas, self.vm).clear_drawing()
            event.accept()
        except Exception as e:
            pass

    def copy_clicked(self, button_index):
        text = self.nlp.text()
        self.clipboard.setText(text)
        self.iface.statusBarIface().showMessage(
            "Nomor lembar peta berhasil disalin", 3000
        )

    def createMapTool(self):
        self.canvas.setMapTool(self.myMapTool)

    def deactivateMapTool(self):
        self.point_tool.isEmittingPoint = False
        self.point_tool.deleteLater()
        self.canvas.scene().removeItem(self.vm)
        self.canvas.setMapTool(self.previousMapTool)

    def set_epsg(self):
        self.epsg = self.project.instance().crs().authid()
        self.crs_tm3.setText(self.project.instance().crs().description())
        # print("changing epsg now into", self.epsg)

    def on_pressed(self):
        self.check_is_tm3()
        self.ambil_titik.checked = True
        try:
            self.canvas.scene().removeItem(self.vm)
            self.canvas.scene().removeItem(self.rb)
            MapTool(self.canvas, self.vm).clear_drawing()
        except:  # noqa
            pass
        self.vm = self.create_vertex_marker()
        self.point_tool = MapTool(self.canvas, self.vm)

        self.point_tool.map_clicked.connect(self.update_titik)

        self.point_tool.isEmittingPoint = True
        self.canvas.setMapTool(self.point_tool)

    def check_is_tm3(self):
        if int(self.epsg.split(":")[1]) in range(23830, 23846):
            return True
            # print("EPSG Tercatat", self.epsg.split(":")[1])
        else:
            dialogBox("Anda belum mengatur sistem proyeksi TM-3 Project")
            # print("EPSG Tercatat", self.epsg.split(":")[1])
            self.ambil_titik.checked = False
            return False

    def update_titik(self, x, y):
        self.ambil_titik.setChecked(False)
        self.point = QgsPointXY(x, y)

        # check point bounds against TM-3 Boundary
        source_crs = QgsCoordinateReferenceSystem("EPSG:4326")
        crs = QgsCoordinateReferenceSystem(self.epsg)
        transform = QgsCoordinateTransform(source_crs, crs, QgsProject.instance())
        crs_box = transform.transformBoundingBox(crs.bounds())
        if not crs_box.contains(self.point):
            dialogBox("Anda memilih titik di luar zona TM-3 Project")
        else:
            self.koordinat.setText(str(round(x, 3)) + "," + str(round(y, 3)))
            self.canvas.unsetMapTool(self.point_tool)
            self.deactivateMapTool()
            self.get_nlp_text()

    def create_vertex_marker(self, type="CROSS"):
        vm = QgsVertexMarker(self.canvas)

        if type == "BOX":
            icon_type = QgsVertexMarker.ICON_BOX
        elif type == "CIRCLE":
            icon_type = QgsVertexMarker.ICON_CIRCLE
        elif type == "CROSS":
            icon_type = QgsVertexMarker.ICON_CROSS
        else:
            icon_type = QgsVertexMarker.ICON_X

        vm.setIconType(icon_type)
        vm.setPenWidth(3)
        vm.setIconSize(7)
        return vm

    def get_nlp_text(self):
        skala_now = self.skala_peta.currentText()
        if self.point is not None:
            x, y = self.point
            self.nlp.setText(get_nlp(skala_now[2:], x, y))
        if self.checkBoxNLP.isChecked():
            self.draw_nlp()

    def draw_nlp(self):
        xMin = xMax = yMin = yMax = None
        skala_now = self.skala_peta.currentText()
        if self.point is not None:
            x, y = self.point

        def rect10rb():
            k_10rb, b_10rb = bk_10000(x, y)
            xMin = x_origin + (k_10rb - 1) * grid_10rb
            yMin = y_origin + (b_10rb - 1) * grid_10rb
            xMax = x_origin + (k_10rb) * grid_10rb
            yMax = y_origin + (b_10rb) * grid_10rb
            return [xMin, yMin, xMax, yMax]

        def rect2500():
            k_2500, b_2500 = bk_2500(x, y)
            ori_10rb_x, ori_10rb_y, p, q = rect10rb()
            xMin = ori_10rb_x + (k_2500 - 1) * grid_2500
            yMin = ori_10rb_y + (b_2500 - 1) * grid_2500
            xMax = ori_10rb_x + (k_2500) * grid_2500
            yMax = ori_10rb_y + (b_2500) * grid_2500
            return [xMin, yMin, xMax, yMax]

        def rect1000():
            k_1000, b_1000 = bk_1000(x, y)
            ori_2500_x, ori_2500_y, p, q = rect2500()
            xMin = ori_2500_x + (k_1000 - 1) * grid_1000
            yMin = ori_2500_y + (b_1000 - 1) * grid_1000
            xMax = ori_2500_x + (k_1000) * grid_1000
            yMax = ori_2500_y + (b_1000) * grid_1000
            return [xMin, yMin, xMax, yMax]

        def rect500():
            k_500, b_500 = bk_500(x, y)
            ori_1000_x, ori_1000_y, p, q = rect1000()
            xMin = ori_1000_x + (k_500 - 1) * grid_500
            yMin = ori_1000_y + (b_500 - 1) * grid_500
            xMax = ori_1000_x + (k_500) * grid_500
            yMax = ori_1000_y + (b_500) * grid_500
            return [xMin, yMin, xMax, yMax]

        def rect250():
            k_250, b_250 = bk_250(x, y)
            ori_500_x, ori_500_y, p, q = rect500()
            xMin = ori_500_x + (k_250 - 1) * grid_250
            yMin = ori_500_y + (b_250 - 1) * grid_250
            xMax = ori_500_x + (k_250) * grid_250
            yMax = ori_500_y + (b_250) * grid_250
            return [xMin, yMin, xMax, yMax]

        if skala_now == skala[0]:
            xMin, yMin, xMax, yMax = rect10rb()
        elif skala_now == skala[1]:
            xMin, yMin, xMax, yMax = rect2500()
        elif skala_now == skala[2]:
            xMin, yMin, xMax, yMax = rect1000()
        elif skala_now == skala[3]:
            xMin, yMin, xMax, yMax = rect500()
        elif skala_now == skala[4]:
            xMin, yMin, xMax, yMax = rect250()

        if xMin is not None:
            draw_rect_bound(xMin, yMin, xMax, yMax, self.epsg)


import os
import json

def get_ground_truth(filename):
    #0022000159342.jpg
    #0022000159342_1.jpg
    if filename.find("_")!=-1:
        return filename[0:filename.find("_")]
    else:
        return filename[0:filename.find(".")]

f = open("single_test.txt","r")
for line in f.readlines():
    data = line.strip().split(" ")
    filename = data[0]
    left = data[1]
    top = data[2]
    right = data[3]
    bottom = data[4]
    bbox = {}
    bbox["left"] = left
    bbox["top"] = top
    bbox["right"] = right
    bbox["bottom"] = bottom
    attrib = {}
    attrib["Type"] = "EAN13"
    result = {}
    result["attrib"] = attrib
    result["bbox"] = bbox
    result["text"] = get_ground_truth(filename)
    results = []
    results.append(result)
    fw = open(filename+".txt","w")
    fw.write(json.dumps(results))
    fw.close()
f.close()


'''
Prepare a word sequence (sentence) with padding
    - convert a sentence String to list of words in sentence
    - convert list of words in sentence to array of word_embedding and padded
'''
import numpy as np
from keras.preprocessing import sequence
from textblob import Sentence
from src.static_variable import load_word_embedding

# Example dialogs
dialogs = [
    "Beijing is a historical city that can be traced back to 3,000 years ago.",
    "The city's history dates back three millennia. As the last of the Four Great Ancient Capitals of China",
    "Beijing has been the political center of the country for much of the past eight centuries",
    "With mountains surrounding the inland city on three sides",
    "When you want to use a different language, select the same Keyboard & Character Viewer item in the menu bar -- although, when more than one language is enabled, the icon will now show the flag or symbol of the current language in use -- and click on the desired language. The Keyboard Viewer will then change layout to represent the keys of the desired language and allow you to insert native characters. To change back to your regular language, once again click on the Keyboard & Character Viewer menu bar item and select the desired input language to switch to."
]

# load pre-trained Word Embedding model
WORD_EMB = load_word_embedding(load_glove=False)

# constant of maximum number of word in sentence (important when padding)
MAX_WORD = 11
N_CHANEL = 1

def generate_wordlist_emb(txt):
    '''
    Process a text string to list of embedded word
    'I love you' -> ['I', 'love', 'you'] -> list([emb('I'), emb('love'), emb('you')])
    :param word_seq: text String
    :return: list of embedded word
    '''
    word_seq = Sentence(txt.lower()).words
    wordlist = []
    for word in word_seq:
        if word in WORD_EMB.vocab:
            wordlist.append(WORD_EMB[word])
        else:
            # shape is depend on embedding dimension
            # use [1., 1., 1., ...] represent a unknown word
            wordlist.append(np.full(shape=[300], fill_value=1.0, dtype=np.float32))
    return wordlist

def generate_sentence_embedding(sentence):
    # get list of embedded words
    new_sentence = generate_wordlist_emb(sentence)

    # padding a sentence by add 0 value at the front until sentence's length = max_word
    return sequence.pad_sequences([new_sentence], maxlen=MAX_WORD, dtype=np.float32)[0]

def generate_dialogs_embedding(dialogs):
    tmp_dialogs = []
    for i, sentence in enumerate(dialogs):
        tmp_dialogs.append(generate_sentence_embedding(sentence))

    # padding a word that lower than max_words
    tmp_dialogs = np.array(tmp_dialogs)

    # n_sample, max_word, emb_dim, = tmp_dialogs.shape[0], tmp_dialogs.shape[1], tmp_dialogs.shape[2]

    # # return with add 1 chanel in the last index
    # return np.reshape(tmp_dialogs, [n_sample, max_word, emb_dim, N_CHANEL])
    return tmp_dialogs

# dialogs_emb = generate_dialogs_embedding(dialogs)


"""
the Bot makes everyday posts with some picture+Belarusial legal word and its definition
"""

#!/usr/bin/python3

import telegram
import time

TOKEN = 'your_token_here'
bot = telegram.Bot(token=TOKEN)
chat_id='your_chat_id_here'
f = open('dictionary.txt', 'r', encoding='UTF-8')
words = f.read().split('\n')
f.close()
temp = ''
num=1
for word in words:
    temp += word + '\n'
    if word[-1] == '*':
        post = "#беларуская_лексіка #родныя_словы" + "\n" + "\n" + temp[:-2:]
        #pictures named 1.jpg, 2.jpg, 3.jpg, etc.
        name='folder_with_pictures'+str(num)+'.jpg'
        with open(name, 'rb') as photo:
            bot.send_photo(chat_id, photo, post)
        temp = ''
        num+=1
        time.sleep(86400)


import matplotlib.pyplot as plt
from matplotlib import ticker

# taken from https://scikit-learn.org/stable/auto_examples/manifold/plot_compare_methods.html#sphx-glr-auto-examples-manifold-plot-compare-methods-py

def plot_3d(points, points_color, title):
    x, y, z = points.T

    fig, ax = plt.subplots(
        figsize=(6, 6),
        facecolor="white",
        tight_layout=True,
        subplot_kw={"projection": "3d"},
    )
    fig.suptitle(title, size=16)
    col = ax.scatter(x, y, z, c=points_color, s=50, alpha=0.8)
    ax.view_init(azim=-60, elev=9)
    ax.xaxis.set_major_locator(ticker.MultipleLocator(4))
    ax.yaxis.set_major_locator(ticker.MultipleLocator(4))
    ax.zaxis.set_major_locator(ticker.MultipleLocator(4))

    fig.colorbar(col, ax=ax, orientation="horizontal", shrink=0.6, aspect=60, pad=0.01)
    plt.show()

def plot_2d(points, points_color, title):
    fig, ax = plt.subplots(figsize=(3, 3), facecolor="white", constrained_layout=True)
    fig.suptitle(title, size=16)
    plt.scatter(ax, points, points_color)
    plt.show()


'''PROGRAM DESCRIPITON:
HMTL form for registration is created and after filling the data, the student details like name,email, course are stored in the MongoDB "Registration" database.
'''

# PROGRAMMED BY: PULI SNEHITH REDDY
# MAIL ID : snehithreddyp@gmail.com

# DATE    : 23-09-2021

# VERSION : 3.7.9

# CAVEATS : None

# LICENSE : None

from flask import Flask
from flask import render_template
from flask import redirect
from flask import url_for
from flask import request
import json
from pymongo import MongoClient

connection = MongoClient("mongodb://localhost:27017")

def mongo_connection():
    if connection:
        return True
    else:
        return False

def mongodb_list():
   if mongo_connection() == True:
        return connection.list_database_names()

def db_exists( db_name):
    if db_name in mongodb_list():
        return True
    else:
        return False

def create_new_collection(db_name, new_collection):
    if connection:
        db_name = connection[db_name]
        new_collection = db_name[new_collection]
        return new_collection
    else:
        return("error")

# timestand for mongodb
def timestamp():
    import datetime as dt
    return dt.datetime.now()

def insert_data(db_name,collection_name,data):
    if connection:
        connection[db_name][collection_name].insert_one(data)
        return "success"
    else:
        return "error"

def display(db_name,collection_name):
    a=[]
    if connection:
        for i in connection[db_name][collection_name].find():
            a.append(i)

        for i in a:
            print(i)
            print("-----------------------------------------------")

app = Flask(__name__)

def func(name,email,course):
    data={}
    data["name"]=name
    data["email"]=email
    data["course"]=course
    insert_data("Registration","Data",data)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/submi',methods=['POST'])
def result():
    name=request.form['name']
    email=request.form['email']
    course=request.form['course']
    func(name,email,course)
    return render_template("submit.html")

@app.route('/user')
def user():
    return render_template('user.html')

if __name__ == '__main__':
    app.run(debug=True, port=5001)

Class Assignments/Storing deatils of HTML form in Mongodb/app.py


import datetime

from django.shortcuts import render
from django.db.models import Subquery, Count
from django.http import HttpResponseRedirect
from django.views.decorators.cache import cache_page

from port.models import Port
from stats.models import Submission, PortInstallation
from port.filters import PortFilterByMultiple
from utilities import old_search_redirect

@cache_page(60 * 10)
def index(request):
    # Support for old "?search=<QUERY>&search_by=<name,description>" links
    # Check if search query is present
    if request.GET.get('search'):
        return HttpResponseRedirect(old_search_redirect(request))

    ports_count = Port.objects.filter(active=True).count()
    submissions_unique = Submission.objects.filter(timestamp__gte=datetime.datetime.now(tz=datetime.timezone.utc) - datetime.timedelta(days=30)).order_by('user', '-timestamp').distinct('user')
    top_ports = PortInstallation.objects.filter(submission_id__in=Subquery(submissions_unique.values('id')), requested=True).exclude(port__icontains='mpstats').values('port').annotate(num=Count('port')).order_by('-num')[:10]
    recently_added = Port.objects.all().order_by('-created_at').only('name')[:10]

    return render(request, 'index.html', {
        'top_ports': top_ports,
        'recently_added': recently_added,
        'ports_count': ports_count
    })

# Respond to ajax-call triggered by the search box
def search(request):
    query = request.GET.get('search_text', '')
    search_by = request.GET.get('search_by', '')
    ports = PortFilterByMultiple(request.GET, queryset=Port.get_active.all()).qs[:50]

    return render(request, 'filtered_table.html', {
        'ports': ports,
        'query': query,
        'search_by': search_by
    })

def about_page(request):
    return render(request, 'about.html')


import socket
import json
import sys  # for exit

# Create a UDP socket at client side
UDPClientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Initialize variables
udp_port = 1234
local_host = socket.gethostname()
serverAddressPort = (local_host, udp_port)
IPAddr = socket.gethostbyname(local_host)
buffer_size = 1024

UDPClientSocket.connect(serverAddressPort)

def send_msg(sending_data):
    UDPClientSocket.sendto(sending_data.encode(), serverAddressPort)

def request_options():
    options = "Enter 1 -> Send text, 2-> Check for text, 3-> Quit: "
    c_option = input(options)
    return c_option

def receiving_data(data, opt, local_id):
    recv_data, server_add = UDPClientSocket.recvfrom(buffer_size * 2)
    recv_data = json.loads(recv_data.decode())
    if opt == "2":
        print("TEXT RECEIVED: To: {} From: {} Text: {}".format(local_id, recv_data["sender"], recv_data["message"]))
    else:
        print("MESSAGE RECEIVED FROM SERVER: {}".format(recv_data["message"]))

variables = ProcessData()
variables.client_IP = IPAddr

print("Enter your ID as a string")
variables.client_id = input("->")
option = request_options()

while option == "1" or option == "2":
    if option == "1":
        variables.client_code = "T"
        print("Enter dest ID as a string: ")
        variables.dest_id = input("-> ")
        request_send_text = "Enter your text: "
        variables.send_text = input(request_send_text)
        send_data = json.dumps(
            {"code": variables.client_code, "dest_id": variables.dest_id, "client_id": variables.client_id,
             "send_text": variables.send_text, "client_ip": variables.client_IP})
        print("MESSAGE TO SEND: {} To: {} From: {} Text: {}".format(variables.client_code, variables.dest_id,
                                                                    variables.client_id, variables.send_text))
        send_msg(send_data)
        receiving_data(variables, option, variables.client_id)

    elif option == "2":
        variables.client_code = "C"
        print("MESSAGE TO SEND: {} From: {} ".format(variables.client_code, variables.client_id))
        send_data = json.dumps({"code": variables.client_code, "client_id": variables.client_id})
        send_msg(send_data)
        receiving_data(variables, option, variables.client_id)

    option = request_options()

else:
    UDPClientSocket.close()
    sys.exit()


import pygame
import math

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (0, 0, 250)
RED = (255, 0, 0)
pygame.init()

# Set the width and height of the screen [width, height]
size = (800, 700)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    # --- Game logic should go here

    # --- Drawing code should go here

    # First, clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.
    screen.fill(WHITE)

    for i in range(200):
        radians_x = i / 20
        radians_y = i / 6
        x = int(75 * math.sin(radians_x)) + 200
        y = int(75 * math.cos(radians_y)) + 200
        pygame.draw.line(screen, BLACK, [x,y], [x+5,y], 5)

    for x_offset in range(30, 300, 30):
        pygame.draw.line(screen,BLACK,[x_offset,100],[x_offset-10,90],2)
        pygame.draw.line(screen,BLACK,[x_offset,90],[x_offset-10,100],2)

    # Draw a rectangle
    pygame.draw.rect(screen,BLACK,[320,20,250,100],2)
    # Draw an ellipse, using a rectangle as the outside boundaries
    pygame.draw.ellipse(screen, BLACK, [320,20,250,100], 2)

    # Draw an arc as part of an ellipse. Use radians to determine what
    # angle to draw.
    pygame.draw.arc(screen, GREEN, [100,300,250,200], math.pi/2, math.pi, 2)
    pygame.draw.arc(screen, BLACK, [100,300,250,200], 0, math.pi/2, 2)
    pygame.draw.arc(screen, RED, [100,300,250,200],3*math.pi/2, 2*math.pi, 2)
    pygame.draw.arc(screen, BLUE, [100,300,250,200], math.pi, 3*math.pi/2, 2)

    # This draws a triangle using the polygon command
    pygame.draw.polygon(screen, BLACK, [[300,300], [300,500], [500,500]], 5)

    # Select the font to use, size, bold, italics
    font = pygame.font.SysFont('Calibri', 25, True, False)
    # Render the text. "True" means anti-aliased text.
    # Black is the color. The variable BLACK was defined
    # above as a list of [0, 0, 0]
    # Note: This line creates an image of the letters,
    # but does not put it on the screen yet.
    text = font.render("My text", True, BLACK)
    # Put the image of the text on the screen at 250x250
    screen.blit(text, [500, 400])

    score = 100
    text = font.render("Score: ", str(score), True, BLACK)

    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    # --- Limit to 60 frames per second
    clock.tick(60)

# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()


import streamlit as st
import pandas as pd
import pickle
from random import randint

books = pd.read_csv('df_2.csv')
df_1 = pickle.load(open('df_1.pkl', 'rb'))

def recommendation(title):
    recommendations = pd.DataFrame(df_1.nlargest(11, title)['title'])
    recommendations = recommendations[recommendations['title'] != title]
    list_name = recommendations['title'].values.tolist()

    add_img = []
    for name in list_name:
        add_img.append(books[books['title'] == name]['image_url'].iloc[0])

    return list_name, add_img

st.title('Content Based Book Recommendation System ')

with st.container():
    select = st.selectbox(
        'Which book would you like to choose?',
        books['title'].values)

    st.write('You selected:', select)

    if st.button('Recommend'):
        col = ["a", "b", "c", "d", "e"]

        list_books, list_imgs = recommendation(select)

        col = st.columns(5)
        with st.container():
            for i in range(5):
                c = col[i]
                with c:
                    st.text(list_books[i])
                    st.image(list_imgs[i])

        with st.container():
            col = st.columns(5)
            for i in range(5):
                c = col[i]
                with c:
                    st.text(list_books[i+5])
                    st.image(list_imgs[i+5])

with st.container():
    st.header('Books')

    colb = ['a', 'b', 'c', 'd', 'e']
    for i in range(10):
        with st.container():
            colb = st.columns(5)
            for c in colb:
                index = randint(0, len(books))
                book = books.iloc[index]
                with c:
                    st.text(book.title)
                    st.image(book.image_url)


from telebot import types
from datetime import date, timedelta
from ...utils import constants

def get_days(room_name: str) -> types.InlineKeyboardMarkup:
    now = date.today()
    markup = types.InlineKeyboardMarkup()
    for day in range(7):
        """ add in InlineKeyboard """
        w_day = now + timedelta(days=day)
        if w_day.weekday() in range(5):
            if now == w_day:
                text = "Сегодня"
                callback_data = f"{constants.DAYS_OF_THE_WEEK[w_day.weekday()]} - " \
                       f"{room_name} - {w_day.strftime('%d-%m-%Y')}"
            else:
                text = f"{constants.DAYS_OF_THE_WEEK[w_day.weekday()]} - " \
                       f"{room_name} - {w_day.strftime('%d-%m-%Y')}"
                callback_data = f"{constants.DAYS_OF_THE_WEEK[w_day.weekday()]} - " \
                       f"{room_name} - {w_day.strftime('%d-%m-%Y')}"
            markup.add(
                types.InlineKeyboardButton(
                    text=text, callback_data=callback_data
                )
            )
    return markup


#!/usr/bin/env python3

import argparse
import sys

from KaSaAn.functions import prefixed_snapshot_analyzer

def main(args=None):
    if args is None:
        args = sys.argv[1:]
    parser = argparse.ArgumentParser(
        description='Get cumulative and mean distribution of complex sizes, plus distribution of number of species,'
                    ' based on snapshots sharing a common prefix, like "t_one_snap_7.ka" having the prefix "t_one_".'
                    ' Snapshots must contain the word "snap" and end in ".ka". Files will be produced in the same'
                    ' directory as the snapshots are. They will be prefixed accordingly, e.g.'
                    ' [prefix]distribution_cumulative.csv')
    parser.add_argument('-p', '--prefix', type=str, default='',
                        help='Prefix identifying snapshots to analyze; e.g. "foo_snap_" is the prefix for'
                             ' "foo_snap_76.ka". Files must end with [number].ka')
    parser.add_argument('-d', '--working_directory', type=str, default='./',
                        help='The directory where snapshots are held, and where distribution files will be saved to.')
    parser.add_argument('-v', '--verbosity', action='store_true',
                        help='Print extra information, like number of snapshots found, directory understood, and'
                             ' file names used for output.')
    args = parser.parse_args()
    prefixed_snapshot_analyzer(base_directory=args.working_directory, snap_prefix=args.prefix, verbosity=args.verbosity)

if __name__ == '__main__':
    main()


import argparse

import pytorch_lightning as pl
import torch
from torch.nn import functional as F

import constants
from lightling_wrapper import BaseTorchLightlingWrapper, SpeechCommandDataModule
from models.bc_resnet.bc_resnet_model import BcResNetModel
from models.bc_resnet.mel_spec_dataset import MelSpecDataSet, mel_collate_fn
from models.simple_conv.base_dataset import AudioArrayDataSet, simconv_collate_fn
from models.simple_conv.simple_conv_model import SimpleConv

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", type=str, default="conv")
    parser.add_argument("--pretrain", type=str)

    return parser.parse_args()

if __name__ == "__main__":
    args = parse_args()

    if args.model == "conv":
        core_model = SimpleConv(
            n_channel=constants.N_CHANNEL, kernel_size_l1=constants.KERNEL_SIZE_L1)
        loss_fn = F.nll_loss
        collate_fn = simconv_collate_fn
        dataset_fn = AudioArrayDataSet
    elif args.model == "bc_resnet":
        core_model = BcResNetModel(
            scale=constants.SCALE_BC_RESNET, dropout=constants.DROPOUT)
        loss_fn = F.nll_loss
        collate_fn = mel_collate_fn
        dataset_fn = MelSpecDataSet
    else:
        raise ValueError("Invalid model name")

    model = BaseTorchLightlingWrapper(
        core_model=core_model,
        loss_fn=loss_fn,
        learning_rate=constants.LEARNING_RATE,
    )

    data_module = SpeechCommandDataModule(
        dataset_fn, collate_fn, batch_size=constants.BATCH_SIZE
    )

    if torch.cuda.is_available():
        trainer = pl.Trainer(
            accelerator="gpu", devices=1
        )
    else:
        trainer = pl.Trainer()

    trainer.test(model, data_module, ckpt_path=args.pretrain)


#!/usr/bin/env python3

from argparse import ArgumentParser
from collections import namedtuple
import hashlib
import json
import os
import shutil

LayerInfo = namedtuple('LayerInfo', ['path', 'metadata', 'contenthash'])

def hash(data):
    m = hashlib.sha256()
    m.update(bytes(data, 'utf-8'))
    return m.hexdigest()

if __name__ == '__main__':
    parser = ArgumentParser(description='Build image manifest')
    parser.add_argument('layers', nargs='+')
    parser.add_argument('--config', required=True)
    parser.add_argument('--tag', required=True)
    parser.add_argument('--architecture', required=True)
    parser.add_argument('--os', required=True)
    parser.add_argument('--out', required=True)
    opts = parser.parse_args()

    non_empty_layers = []
    for layer_path in opts.layers:
        if os.path.exists(os.path.join(layer_path, 'metadata.json')):
            with open(os.path.join(layer_path, 'metadata.json')) as f:
                metadata = json.load(f)
            with open(os.path.join(layer_path, 'contentsha256')) as f:
                contenthash = f"sha256:{f.read().strip()}"
            non_empty_layers.append(
                LayerInfo(layer_path, metadata, contenthash))

    with open(opts.config) as f:
        container_config = json.load(f)

    config = json.dumps({
        'created': '1970-01-01T00:00:00Z',
        'rootfs': {
            'type': 'layers',
            'diff_ids': list(map(lambda x: x.contenthash, non_empty_layers))
        },
        'config': container_config,
        'architecture': opts.architecture,
        'os': opts.os,
    })

    config_hash = hash(config)

    manifest = json.dumps({
        'schemaVersion': 2,
        'config': {
            'mediaType': 'application/vnd.oci.image.config.v1+json',
            'size': len(config),
            'digest': 'sha256:{}'.format(config_hash)
        },
        'layers': list(map(lambda x: x.metadata, non_empty_layers))
    })

    manifest_hash = hash(manifest)

    metadata_dict = {
        'mediaType': 'application/vnd.oci.image.manifest.v1+json',
        'size': len(manifest),
        'digest': 'sha256:{}'.format(manifest_hash),
        'platform': {
            'architecture': opts.architecture,
            'os': opts.os,
        }
    }

    if opts.tag != '':
        metadata_dict['annotations'] = {
            'org.opencontainers.image.ref.name': opts.tag
        }

    blobs_path = os.path.join(opts.out, 'blobs', 'sha256')
    os.makedirs(blobs_path)

    def open_file(name):
        return open(os.path.join(opts.out, name), 'w', encoding='utf-8')

    with open_file('config.json') as f:
        f.write(config)
        os.symlink(f.name, os.path.join(blobs_path, config_hash))

    with open_file('manifest.json') as f:
        f.write(manifest)
        os.symlink(f.name, os.path.join(blobs_path, manifest_hash))

    with open_file('metadata.json') as f:
        json.dump(metadata_dict, f)

    for layer in non_empty_layers:
        layer_blobs_path = os.path.join(layer.path, 'blobs', 'sha256')
        for path in os.listdir(layer_blobs_path):
            shutil.copyfile(os.path.join(layer_blobs_path, path),
                            os.path.join(blobs_path, path),
                            follow_symlinks=False)


import os
import requests
import time
import pyfiglet
req = requests.get('https://google.com')
os.system('clear')
print ("Fuck Dunia Percintaan")
time.sleep(1)
os.system('clear')
print ("\33[36;1m")
text = pyfiglet.figlet_format("SantriXploiter")
print (text)
print
print ("[1] Tentang SantriXploiter")
print ("[2] Blog SantriXploiter")
print ("[3] Web SantriXploiter")
print ("[4] Instagram SntriXploiter")
print ("[5] Group Telegram ")
print ("[6] Channel Youtube ")
print ("[7] Fanspage Facebook")
print
pilih = int(input("pilih : "))
if pilih == 1:
  time.sleep(1)
  print("Komunitas dari Pelajar Biasa yang tertarik Pada Dunia IT Dan Blog Yang Membahas Seputar Dunia IT dan Agama")
elif pilih == 2:
  time.sleep(1)
  os.system('xdg-open https://santrixploiter.blogspot.com')
elif pilih == 3:
  time.sleep(1)
  os.system('xdg-open http://aliyajnck.000webhostapp.com')
elif pilih == 4:
  time.sleep(1)
  os.system('xdg-open https://www.instagram.com/santri.xploiter/')
elif pilih == 5:
  time.sleep(1)
  os.system('xdg-open http://t.me/santrixploiter')
elif pilih == 6:
  time.sleep(1)
  os.system('xdg-open https://www.youtube.com/channel/UCG8EAqQdcDzphpluzrJWt3Q')
elif pilih == 7:
  time.sleep(1)
  os.system('xdg-open https://www.facebook.com/santrixploiter/')
else:
  print ("Tidak Tersedia")