#-*- coding=utf-8 -*-

from django.db import models
from django.utils.translation import ugettext_lazy as _

from biz.instance.models import Instance
from biz.volume.models import Volume
from biz.backup.settings import BACKUP_TYPE_CHOICES, BACKUP_TYPE_FULL, \
                BACKUP_STATES, BACKUP_STATE_WAITING, \
                BACKUP_STATE_PENDING_DELETE, BACKUP_STATE_PENDING_RESTORE

class Backup(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(_("Name"), max_length=64)
    backup_type = models.IntegerField(_("Back Type"),
            choices=BACKUP_TYPE_CHOICES, default=BACKUP_TYPE_FULL)

    volumes = models.CharField(_("Volumes"), max_length=255,
                                null=True, blank=True, default=None)
    instance = models.IntegerField(_("Instance"),
                        null=True, blank=True, default=None)

    user_data_center = models.ForeignKey('idc.UserDataCenter')
    user = models.ForeignKey('auth.User')

    create_date = models.DateTimeField(_("Create Date"), auto_now_add=True)
    delete_date = models.DateTimeField(_("Delete Date"), auto_now=True)
    deleted = models.BooleanField(_("Deleted"), default=False)
    status = models.IntegerField(_("Status"), choices=BACKUP_STATES,
                                default=BACKUP_STATE_WAITING)

    @property
    def backup_type_desc(self):
        return self.get_backup_type_display()

    @property
    def instance_name(self):
        if self.instance:
            try:
                ins = Instance.objects.get(pk=self.instance)
                return ins.name
            except:
                return '---'
        else:
            return 'N/A'

    @property
    def volume_name(self):
        if not self.instance and self.volumes:
            try:
                vol = Volume.objects.get(pk=self.volumes)
                return vol.name
            except:
                return '---'
        else:
            return 'N/A'

    def mark_delete(self):
        self.status = BACKUP_STATE_PENDING_DELETE
        self.save()
        self.items.all().update(status=BACKUP_STATE_PENDING_DELETE)

    def mark_restore(self, item_id=None):
        self.status = BACKUP_STATE_PENDING_RESTORE
        self.save()
        backup_items = self.items.all()
        if item_id:
            backup_items.filter(pk=item_id).update(status=BACKUP_STATE_PENDING_RESTORE)
        else:
            backup_items.update(status=BACKUP_STATE_PENDING_RESTORE)

    def create_items(self):
        if self.instance:
            try:
                ins = Instance.objects.get(pk=self.instance)
                BackupItem.objects.create(
                    backup=self,
                    resource_id=ins.id,
                    resource_uuid=ins.uuid,
                    resource_size=ins.sys_disk,
                    resource_type=Instance.__name__,
                    resource_name=ins.name,
                    user_data_center=self.user_data_center,
                    user=self.user,
                    status=self.status
                )
            except:
                pass
        if self.volumes:
            try:
               for vol_id in self.volumes.split(','):
                    volume = Volume.objects.get(pk=vol_id)
                    BackupItem.objects.create(
                        backup=self,
                        resource_id=volume.id,
                        resource_uuid=volume.volume_id,
                        resource_size=volume.size,
                        resource_type=Volume.__name__,
                        resource_name=volume.name,
                        user_data_center=self.user_data_center,
                        user=self.user,
                        status=self.status
                    )
            except:
                pass

    class Meta:
        db_table = "backup"
        verbose_name = _("Backup")
        verbose_name_plural = _("Backup")
        ordering = ['-create_date']

class BackupItem(models.Model):
    id = models.AutoField(primary_key=True)
    backup = models.ForeignKey(Backup, related_name="items")

    resource_id = models.IntegerField(_("Resource ID"),
                        null=True, blank=True, default=0)
    resource_uuid = models.CharField(_("Backend UUID"),
                        max_length=64, null=True, blank=True)
    resource_type = models.CharField(_("Resource"),
                        max_length=64, null=True, blank=True)
    resource_name = models.CharField(_("Resource Name"),
                            max_length=64, null=True, blank=True)
    resource_size = models.IntegerField(_("Size(GB)"),
                        null=True, blank=True, default=0)

    rbd_image = models.CharField(_("RBD Image"), max_length=256,
                        null=True, blank=True, default=None)

    user_data_center = models.ForeignKey('idc.UserDataCenter')
    user = models.ForeignKey('auth.User')
    create_date = models.DateTimeField(_("Create Date"), auto_now_add=True)
    delete_date = models.DateTimeField(_("Delete Date"), auto_now=True)
    deleted = models.BooleanField(_("Deleted"), default=False)
    status = models.IntegerField(_("Status"), choices=BACKUP_STATES,
                                default=BACKUP_STATE_WAITING)

    class Meta:
        db_table = "backup_item"
        verbose_name = _("Backup Item")
        verbose_name_plural = _("Backup Item")
        ordering = ['-create_date']


import argparse
from datetime import datetime, timedelta, date
from enum import Enum
import pandas as pd
import requests
from pathlib import Path

outfolder =Path.cwd().joinpath('output')

class AustralianState(Enum):
    NSW = "nsw"
    VIC = "vic"
    QLD = "qld"
    WA = "wa"
    SA = "sa"
    TAS = "tas"
    ACT = "act"
    NT = "nt"

def get_annual_holidays(jurisdiction: AustralianState) -> list:
    """
    Get a list of annual holidays for the specified jurisdiction.

    Parameters:
    jurisdiction (AustralianState): The Australian state or territory to retrieve the holidays for.

    Returns:
    list: A list of `date` objects representing the annual holidays for the specified jurisdiction.

    Raises:
    HTTPError: If there was an error retrieving the holiday data from the API.
    """
    url = f'https://data.gov.au/data/api/3/action/datastore_search_sql?sql=SELECT%20*%20from%20%22d256f989-8f49-46eb-9770-1c6ee9bd2661%22%20WHERE%20%22Jurisdiction%22%20LIKE%20%27{jurisdiction}%27'
    try:
        response = requests.get(url)
        holidays = []
        for holiday in response.json()['result']['records']:
            holidays.append(datetime.strptime(holiday['Date'], '%Y%m%d').date())
        return holidays
    except requests.exceptions.RequestException as e:
        raise requests.exceptions.HTTPError(f"Error retrieving holidays data from API for {jurisdiction}: {e}")

def convert_strings_to_dates(str_list: list) -> list:
    """
    Convert a list of date strings in 'YYYYmmdd' format to a list of date objects.

    Parameters:
    str_list (list): A list of date strings in 'YYYYmmdd' format.

    Returns:
    list: A list of date objects corresponding to the input strings.

    """
    date_list = []
    for date_str in str_list:
        date = datetime.strptime(date_str, '%Y%m%d').date()
        date_list.append(date)
    return date_list

def get_next_business_day(datein: date = None, holidays: list = None) -> dict:
    """
    Get the next business day after a given date, accounting for weekends and holidays.

    Parameters:
    - datein (date): The date to start from. If not provided, the current date will be used.
    - holidays (list): A list of date objects representing holidays. If not provided, no holidays will be used.

    Returns:
    - A dictionary with two keys: 'Date' and 'Next Business Day'. 'Date' is the input date, and 'Next Business Day'
      is the date of the next business day after the input date.

    """
    if datein is None:
        datein = date.today()
    if holidays is None:
        holidays = []
    nbd_dict = {'Date': datein, 'Next Business Day': None}
    next_day = datein + timedelta(days=1)
    # Check if the next day is a weekend day or a holiday
    while next_day.weekday() >= 5 or next_day in holidays:
        next_day += timedelta(days=1)
    nbd_dict['Next Business Day'] = next_day
    return nbd_dict

def save_dates_to_excel(nbd_list, filename):
    """
    Save a dictionary of dates to an Excel file.

    Parameters:
    - dates_dict: A dictionary containing dates as keys and values.
    - filename: The filename to save the Excel file to.

    Returns:
    - None.
    """
    df = pd.DataFrame(nbd_list)
    df.to_excel(filename, index=False)

def generate_annual_nbd_lookuptable(year: int, holiday_list: list)-> list:
    """
    Iterate through all days of the specified year and print each day's date.

    Parameters:
    year (int): The year to iterate through.

    Returns:
    None
    """
    nbd_list = []
    start_date = date(year, 1, 1)
    end_date = date(year, 12, 31)
    delta = timedelta(days=1)
    current_date = start_date
    while current_date <= end_date:
        day_pair=get_next_business_day(current_date, holiday_list)
        nbd_list.append(day_pair)
        current_date += delta

    return nbd_list

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description="Process an Australian state")
    parser.add_argument("state", type=str, help="The Australian state to process")
    args = parser.parse_args()
    state_map = {
        "NSW": "New South Wales",
        "VIC": "Victoria",
        "QLD": "Queensland",
        "WA": "Western Australia",
        "SA": "South Australia",
        "TAS": "Tasmania",
        "NT": "Northern Territory"
    }

    action = "Generating NBD lookup for "
    if args.state in state_map:
        print(f"{action} {state_map[args.state]}")
    else:
        print("Invalid state entered")
        exit(1)
    state = args.state.lower()
    hols = get_annual_holidays(state)
    nbd_lookup_list=generate_annual_nbd_lookuptable(2023, hols)
    outfolder.mkdir(parents=True, exist_ok=False)
    outfile = outfolder.joinpath(f"{state}_nbd_lookup.xlsx")
    print(f"saving data to {state}_nbd_lookup.xlsx")
    save_dates_to_excel(nbd_lookup_list, outfile)
    # Print the results


from collections import defaultdict
from typing import List, Dict, DefaultDict, Tuple

from file_tools import load_img, save_images

import numpy as np
import itertools as it

def deallocate_img(data: Dict[str, any], new_name: str, axis: int = 0) -> List[np.ndarray]:
    """
    Divides image into parts based on given name and assembles it to a new image based on new name
    :param data: Object consisting of name and directory
    :param new_name: Name
    :param axis: 0 for horizontal and 1 for vertical
    :return: New image representation as a numpy array
    """
    new_name: str = new_name.lower()
    name: str = data['name']
    directory: str = data['dir']
    img: np.ndarray = load_img(directory)
    divisions: Dict[str, List[np.ndarray]] = get_divisions(img, name, axis)
    return assemble_img(new_name, divisions, axis)

def get_divisions(img: np.ndarray, name: str, axis: int = 0) -> Dict[str, List[np.ndarray]]:
    """
    Divides the image, based on the character which is allocated to. This handles also the special case, where not all
    chars in the string are unique.
    :param img: Image representation as a numpy array
    :param name: Name
    :param axis: 0 for horizontal and 1 for vertical
    :return: Divisions based on position of char. Returned as List of dictionaries
    """
    ret: DefaultDict = defaultdict(list)
    for key, value in zip(list(name), np.array_split(img, len(name), axis=axis)):
        ret[key].append(value)
    return dict(ret)

def assemble_img(new_name: str, locator: Dict[str, List[np.ndarray]], axis: int = 0) -> List[np.ndarray]:
    """
    Assembles parts of the old image to a new image based on the new name.
    :param new_name: New name, which only consists of characters from the original name
    :param locator: Dictionary of the divided parts into the posiitional characters
    :param axis: 0 for horizontal and 1 for vertical
    :return: New image representation as a numpy array
    """
    new_name: str = new_name.lower()
    if not set(new_name) <= set(locator.keys()):
        new_name: str = ''.join(locator.keys()).lower()
    indices: List[Tuple[any]] = [i for i in it.product(*[list(range(0, len(locator[c]))) for c in new_name])]
    return [np.concatenate([locator[c][index[idx]] for idx, c in enumerate(new_name)], axis=axis) for index in indices]


# 3rd party import
import pygame
# class imports for game
from Graphics.Ball import Ball
from Graphics.Paddles import Paddles
from Graphics.Score import Score

pygame.init()

BLACK = (0,0,0)
WHITE = (255,255,255)

WIDTH = 1280
HEIGT = 720
screen = pygame.display.set_mode((WIDTH,HEIGT))

enemy = Paddles(WHITE, 20, 100)
enemy.rect.x = WIDTH-1200
enemy.rect.y = HEIGT/2-50

player = Paddles(WHITE, 20, 100)
player.rect.x = WIDTH-80
player.rect.y = HEIGT/2-50

ball = Ball(WHITE, 15, 15)
ball.rect.x = WIDTH/2
ball.rect.y = HEIGT/2

score = Score()
score.textrect.centerx = screen.get_rect().centerx
score.textrect.centery = screen.get_rect().top + 100

all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(ball)
all_sprites_list.add(player)
all_sprites_list.add(enemy)

pads = [player.rect, enemy.rect]
clock = pygame.time.Clock()

def run():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        # movment for player
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP] and player.rect.y > 0:
            player.moveUp(5)
        if keys[pygame.K_DOWN] and player.rect.y < HEIGT-100:
            player.moveDown(5)
        # enemy "AI"
        if enemy.rect.y >= ball.rect.y and enemy.rect.y > 0:
            enemy.moveUp(5)
        if enemy.rect.y <= ball.rect.y and enemy.rect.y < HEIGT-100:
            enemy.moveDown(5)
        # see if the ball has scored and update score
        if ball.rect.x >= WIDTH-15:
            ball.reset()
            score.update_score(enemy_scored = True)
        if ball.rect.x <= 0:
            ball.reset()
            score.update_score(player_scored = True)
        # collision for borders
        if ball.rect.y > HEIGT-15:
            ball.velocity[1] = -ball.velocity[1]
        if ball.rect.y < 0:
            ball.velocity[1] = -ball.velocity[1]
        # collision for pads
        if ball.rect.collidelist(pads) >= 0:
            ball.bounce()
        # updates game
        all_sprites_list.update()
        screen.fill(BLACK)
        screen.blit(score.text, score.textrect)
        all_sprites_list.draw(screen)
        clock.tick(60)
        pygame.display.flip()

if __name__ == "__main__":
    run()


import sklearn.base
import sklearn.impute
import sklearn.compose
import sklearn.tree
import sklearn.pipeline
import sklearn.ensemble
import lightgbm

class CategoricalEncoder(sklearn.base.BaseEstimator, sklearn.base.TransformerMixin):

    def __init__(self,  columns):
        self.columns = columns
        self.encoder = sklearn.preprocessing.OrdinalEncoder(
            handle_unknown='use_encoded_value', unknown_value=-1
        )
        self.feature_names_in_ = []

    def fit(self, x, y):
        self.feature_names_in_ = x.columns
        xc = x.loc[:, self.columns].values
        self.encoder.fit(xc, y)
        return self

    def transform(self, x, y=None):
        xc = x.loc[:, self.columns].values
        x2 = self.encoder.transform(xc)
        result = x.copy()
        result.loc[:, self.columns] = x2
        return result

    def get_feature_names_out(self, input_feature=None):
        return self.feature_names_in_

class ProbPos(sklearn.base.BaseEstimator, sklearn.base.ClassifierMixin):

    def __init__(self, base_classifer):
        self.base_classifier = base_classifer

    def __repr__(self, N_CHAR_MAX=700):
        return f"ProbPos({self.base_classifier})"

    def fit(self, x, y):
        self.base_classifier.fit(x, y)
        return self

    def predict(self, x):
        return self.base_classifier.predict(x)

    def predict_proba(self, x):
        prob_matrix = self.base_classifier.predict_proba(x)
        return prob_matrix[:, 1]

categorical_columns = [
    'workclass',
    'education',
    'marital-status',
    'occupation',
    'relationship',
    'race',
    'sex',
    'native-country',
]
preps = {
    'TreePreprocessing': [
        ('encode', CategoricalEncoder(categorical_columns)),
        ('impute', sklearn.impute.SimpleImputer(fill_value=0.0)),
    ],
}
classifiers = {
    'DecisionTree': sklearn.tree.DecisionTreeClassifier(),
    'RandomForest': sklearn.ensemble.RandomForestClassifier(),
    'LGBMClassifier': lightgbm.LGBMClassifier(n_estimators=50),
}


'''
Luis Ferrufino
G#00997076
HW#4
4/15/20
lof.py
'''

import random as rnd
import numpy as np
import matplotlib.pyplot as plt

dist = np.load('./distMatrix.npy')
kRange = range(1, 81)
avgAuc = np.zeros(len(kRange))
numRuns = 100 # number of trials we run each test for k

for k in kRange:

  for j in range(1, numRuns + 1):

    #create two random sets of indeces
    trainIndeces = []
    for i in range(50):
      indexA = rnd.randint(0, 99)
      while trainIndeces.count(indexA) > 0:
        indexA = rnd.randint(0, 99)
      indexB = rnd.randint(100, 199)
      while trainIndeces.count(indexB) > 0:
        indexB = rnd.randint(100, 199)
      indexC = rnd.randint(200, 299)
      while trainIndeces.count(indexC) > 0:
        indexC = rnd.randint(200, 299)
      indexD = rnd.randint(300, 398)
      while trainIndeces.count(indexD) > 0:
        indexD = rnd.randint(300, 398)

      trainIndeces.append(indexA)
      trainIndeces.append(indexB)
      trainIndeces.append(indexC)
      trainIndeces.append(indexD)

    testIndeces = []

    for i in range(36):
      indexA = rnd.randint(0, 99)
      while trainIndeces.count(indexA) > 0 or testIndeces.count(indexA) > 0:
        indexA = rnd.randint(0, 99)
      indexB = rnd.randint(100, 199)
      while trainIndeces.count(indexB) > 0 or testIndeces.count(indexB) > 0:
        indexB = rnd.randint(100, 199)
      indexC = rnd.randint(200, 299)
      while trainIndeces.count(indexC) > 0 or testIndeces.count(indexC) > 0:
        indexC = rnd.randint(200, 299)
      indexD = rnd.randint(300, 398)
      while trainIndeces.count(indexD) > 0 or testIndeces.count(indexD) > 0:
        indexD = rnd.randint(300, 398)
      indexM = rnd.randint(399, 497)
      while trainIndeces.count(indexM) > 0 or testIndeces.count(indexM) > 0:
        indexM = rnd.randint(399, 497)

      testIndeces.append(indexA)
      testIndeces.append(indexB)
      testIndeces.append(indexC)
      testIndeces.append(indexD)
      testIndeces.append(indexM)

    #use these to create a new training set and test set:
    notTests = [x for x in list(range(0,498)) if x not in testIndeces]
    notTrains = [x for x in list(range(0,498)) if x not in trainIndeces]

    testDist = np.delete(dist, notTests, 0)
    testDist = np.delete(testDist, notTrains, 1)
    trainDist = np.delete(dist, notTrains, 0)
    trainDist = np.delete(trainDist, notTrains, 1)

    #now create the neighbours matrix for each set:

    trainNeighb = np.zeros(trainDist.shape)

    for i in range(0, trainNeighb.shape[0]):

      temp = np.argsort(trainDist[i])

      for l in range(0, temp.size):

        trainNeighb[i][l] = trainIndeces[temp[l]]

    testNeighb = np.zeros(testDist.shape)

    for i in range(0, testNeighb.shape[0]):

      temp = np.argsort(testDist[i])

      for l in range(0, temp.size):

        testNeighb[i][l] = trainIndeces[temp[l]]

    #now create the strangeness training list by calculating the LOF between points in the training set:

    trainRd = np.zeros(trainDist.shape) #the reachability distances between points

    for i in range(0, trainDist.shape[0]):

      for l in range(0, trainDist.shape[1]):

        trainRd[i][l] = max( dist[trainIndeces[i]][int(trainNeighb[i][k])], dist[trainIndeces[i]][trainIndeces[l]] )

    trainLrd = np.zeros(trainDist.shape[0]) # the lrd (local reachability density) for each point

    for i in range(0, trainLrd.size):

      summation = 0

      for l in range(1, k + 1):

        summation += trainRd[i][trainIndeces.index(int(trainNeighb[i][l]))]

      trainLrd[i] = k / summation

    trainLof = np.zeros(trainDist.shape[0])

    for i in range(0, trainLof.size):

      summation = 0

      for l in range(1, k + 1):

        summation += trainLrd[trainIndeces.index(int(trainNeighb[i][l]))] / trainLrd[i]

      trainLof[i] = summation / k

    trainLof = np.sort(trainLof) #this is now my strangeness training list

    #in a very similar manner, we compute the strangeness test list:

    testRd = np.zeros(testDist.shape) #reach-dist

    for i in range(0, testDist.shape[0]):

      for l in range(0, testDist.shape[1]):

        testRd[i][l] = max( dist[testIndeces[i]][int(testNeighb[i][k])], dist[testIndeces[i]][trainIndeces[l]] )

    testLrd = np.zeros(testDist.shape[0]) #lrd

    for i in range(0, testLrd.size):

      summation = 0

      for l in range(1, k + 1):

        summation += testRd[i][trainIndeces.index(int(testNeighb[i][l]))]

      testLrd[i] = k / summation

    testLof = np.zeros(testDist.shape[0]) #WON'T be sorted; will become my strangeness test list

    for i in range(0, testLof.size):

      summation = 0

      for l in range(1, k + 1):

        summation += trainLrd[trainIndeces.index(int(testNeighb[i][l]))] / testLrd[i]

      testLof[i] = summation / k

    #next we calculate the p-values:

    pValues = np.zeros(testLof.shape)

    for i in range(0, pValues.size):

      pValues[i] = np.where(trainLof>=testLof[i])[0].size / ( trainLof.size + 1 ) # b / ( N + 1 )

    #finally, we compute the ROC's AUC:

    clDelta = 0.05 #intervals for confidence level
    numIntervals = int(1 / clDelta + 1)
    cl = 0
    tpr = np.zeros(numIntervals) # y-coördinates (true positive rate)
    fpr = np.zeros(numIntervals) # x-coördinates (false positive rate)
    labels = np.load('./labelVector.npy')
    normalLabels = ['A', 'B', 'C', 'D']

    for i in range(numIntervals):

      predictions = np.zeros(pValues.shape, '<U1')

      for l in range(pValues.size):

        predictions[l] = 'M' if pValues[l] < ( 1 - cl ) else 'N' # N for normal
      tp = 0 #true positive
      fp = 0 #false positive
      tn = 0 #true negative
      fn = 0 #false negative

      for l in range(pValues.size): #here, an anomaly is a positive, and a normal point is a negative

        if ( normalLabels.count(labels[testIndeces[l]]) > 0 and predictions[l] == 'N' ):
          tn += 1
        elif ( labels[testIndeces[l]] == 'M' and predictions[l] == 'M' ):
          tp += 1
        elif ( normalLabels.count(labels[testIndeces[l]]) > 0 and predictions[l] == 'M' ):
          fp += 1
        else:
          fn += 1

      tpr[i] = tp / ( tp + fn )
      fpr[i] = fp / ( tn + fp )

      cl += clDelta

    order = np.argsort(fpr)
    tpr = tpr[order]
    fpr = fpr[order]
    auc = np.trapz(y=tpr, x=fpr)
    print('Pass: k=' + str(k) + ' , j=' + str(j) + ' with ' + str(auc))
    avgAuc[k - 1] += auc
  avgAuc[k - 1] /= numRuns
  print('*During k=' + str(k) + ', the average auc was ' + str(avgAuc[k - 1]))

print('**The best k values were ' + str(np.argsort(avgAuc) + 1))

#generate a graph:

plt.plot(list(range(1,81)), avgAuc.tolist())
plt.ylabel('AUC')
plt.xlabel('K-VALUES')
plt.show()


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

from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation

# Image set read and shape settings
def read_images(dataset , pixels):
    image_list = []
    for image in dataset['image']:
        path = './cetaceous_images/' + image
        img = cv2.imread(path)
        image = cv2.resize(img, (pixels, pixels))
        image_list.append(image)
    return image_list

# Train, validation and test data
def train_test(dataset , image_list):

    X = ((np.array(image_list)) / 255)
    y = to_categorical(dataset['categories'])

    X_train , X_test , y_train , y_test = train_test_split(X , y , test_size= 0.2 , random_state= 98)

    X_val = X_train[-600:]
    y_val = y_train[-600:]
    X_train = X_train[:-600]
    y_train = y_train[:-600]

    return X_train , X_val , X_test , y_train , y_val , y_test

def model_params(pixel , pool_size_1 , pool_size_2 , pool_size_3, n_neurons):
    model = Sequential()

    model.add(Conv2D(32, (3,3), activation='relu',
                input_shape=(pixel, pixel, 3)))
    model.add(MaxPooling2D(pool_size=pool_size_1))
    model.add(Dropout(0.25))

    model.add(Conv2D(64, (3,3), activation='relu'))
    model.add(MaxPooling2D(pool_size=pool_size_2))
    model.add(Dropout(0.25))

    model.add(Conv2D(128, (3,3), activation='relu'))
    model.add(MaxPooling2D(pool_size=pool_size_3))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(n_neurons, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(3,activation='softmax'))

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

    print('-----------Modelo ==> ' , pixel , pool_size_1 , pool_size_2 , pool_size_3 , n_neurons , '---------------')

    return model

# Model training

def model_training(model , epoch , batch , X_train , y_train , X_val , y_val):

    history = model.fit(X_train,
          y_train,
          epochs=epoch,
          batch_size=batch,
          validation_data = (X_val,y_val))
    print('---------------------------Training params ==> ' , epoch , batch , '---------------------------------------')

    return history , model


"""
Contains functions that affect Google Calendar
"""
from dataclasses import dataclass

from .buildservice import build_google_service

@dataclass
class CalendarEvent:
    """Represents a calendar event.

    - id (str): The id of the calendar event
    - name (str): The name of the caledar event, or empty string if the event has no
                  name.
    - start_time (str): The start time of the calendar event. Recurring calendar events
                        have the start time of the original calendar event.
    - end_time (str): The end time of the calendar event. Recurring calendar events
                      have the end time of the original calendar event.

    """

    name: str
    id: str  # pylint: disable=invalid-name
    start_time: str
    end_time: str

def get_calendar_events(calendar_id: str) -> list[CalendarEvent]:
    """
    This function takes a Google Calendar ID and returns a list of all events in the
    calendar.

    Args:
        calendar_id (str): The ID of the Google Calendar to fetch events from

    Raises:
        googleapiclient.errors.HttpError: If the function fails.
    """
    service = build_google_service("calendar", "v3")

    events_result = (
        service.events()  # pylint: disable=no-member
        .list(calendarId=calendar_id, singleEvents=False)
        .execute()
    )
    event_items = events_result.get("items", [])
    events: list[CalendarEvent] = []
    for item in event_items:
        events.append(
            CalendarEvent(
                id=item["id"],
                name=item.get("summary", ""),
                start_time=item["start"]["dateTime"],
                end_time=item["end"]["dateTime"],
            )
        )

    return events

def update_calendar_event_participants(
    calendar_id: str, event_id: str, participants: list[str]
) -> None:
    """
    This function updates the participants of a Google Calendar event.

    Participants will receive notifications that they have been invited to the event.

    Args:
        calendar_id (str): The calendar ID of the calendar to update
        event_id (str): The ID of the event to update
        participants (list[str]): List of participant email addresses

    Raises:
        googleapiclient.errors.HttpError: If the function fails.
    """
    participants_body = [{"email": email} for email in participants]

    service = build_google_service("calendar", "v3")
    service.events().patch(  # pylint: disable=no-member
        calendarId=calendar_id,
        eventId=event_id,
        body={"attendees": participants_body},
        sendUpdates="all",
    ).execute()


import pyaudio
import sqlite3
import wave
import threading
import time

con = sqlite3.connect("record.db")
cur = con.cursor()
cur.executescript(open("setup.sql", "r").read())

recBefore = 5
recAfter = 5
recMax = 600
rate = 44100
audio = pyaudio.PyAudio()
frames = [None]*int(rate / 1024 * recMax)
i = 0
recStop = False

def rec_thread_func():
    global i
    global recStop
    global frames
    global audio
    stream = audio.open(format=pyaudio.paInt16, channels=1, rate=rate, input=True, frames_per_buffer=1024)
    while not recStop:
        while i in range(0, int(rate / 1024 * recMax)):
            frames[i] = stream.read(1024)
            i+=1

    stream.close()

# Setup and start recording thread
rec_thread = threading.Thread(target=rec_thread_func)
rec_thread.start()

while True:
    input("Start")
    startFrame = i
    startTime = int(time.time())
    input("Stop")
    time.sleep(recAfter)
    #recStop = True
    endFrame = i
    endTime = int(time.time())

    outputFile = "out.wav"

    file = wave.open(outputFile, "wb")
    file.setnchannels(1)
    file.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
    file.setframerate(rate)
    if(startFrame == endFrame):
        print("Threading put")
    elif(startFrame < endFrame):
        startFrame = int(startFrame-(rate/1024*recBefore))
        cur.execute("INSERT INTO audio(startT, endT, len, freq, audio) VALUES (?,?,?,?,?);", (startTime, endTime, startTime-endTime, rate, b"".join(frames[startFrame:endFrame])))
        con.commit()
        file.writeframes(b"".join(frames[startFrame:endFrame]))
    elif(startFrame > endFrame):
        file.writeframes(b"".join(frames[startFrame-recBefore:int(rate / 1024 * recMax)]+frames[0:endFrame]))
    else:
        print("dafuq")

    file.close()


"""
JSON (JavaScript Object Notation) = este o sintaxa asemanatoare cu dictionarele din python,
folosita adesea in comunicarea over the internet. Este un format text pentru schimbul de date.

In formatul JSON, avem obiecte (care sunt cuprinse intre acolade) si Arrays (listele din Python).
Pe langa acestea, avem toate tipurile basic de date : string, int, float, boolean

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": {
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
}}

In JSON, pentru stringuri, folosim mereu double quotes " (ghilimele duble)
Un JSON valid este de fapt un obiect, adica este cuprins intre acolade

 CTR + ALT + L - modalitatea de a forma in pychat exemplu in json
"""
import json

with open('files/employees.json') as my_json:
    # putem citi fisierul json si linie cu linie, daca vrem, DAR pierdem toata informatia primita din formatarea sa
    employees = json.load(my_json)
    print(type(employees))
    for e in employees['employees']:
        print(e['name'])

"""
La fel cum putem "incarca" un JSON intr-un dictionar, putem face si operatiunea inversa, adica putem scrie
din dictionar, intr-un fisier de tip JSON.
"""

dict_complex = {
    'id': 1,
    'name': ['Madalin', 'Chelu'],
    'height': 1.88,
    'course': {
        'name': 'Python + Testare automata',
        'start date': '27 martie 2023'
    }
}

with open('files/newj.json', 'w') as newme:
    json.dump(dict_complex, newme, indent=4)
# indent asezare
"""
CSV = comma separated values

id,name,age
1,Adela,31
2,Ionut,25
3,Ion Popescu,45
"""
# import csv
# with open('files/file.csv', 'r') as read:
#     csv.reader(read)


"""
This is a base service class implementation.
Please override set_cache_xxx methods with extra logic for Redis in the inherited class.
    * xxx - create/update/delete
"""
import typing

import pydantic

from fastapi import BackgroundTasks
from app.repositories.base_db_repository import CRUDBaseRepository, ModelType
from app.repositories.redis_repository import RedisBaseRepository

class BaseService:
    """Base abstract service class."""
    MSG_NOT_IMPLEMENTED = "Method or function hasn't been implemented yet."

    def __init__(self, db: CRUDBaseRepository, redis: RedisBaseRepository, bg_tasks: BackgroundTasks | None = None):
        self.db = db
        self.redis = redis
        self.bg_tasks = bg_tasks

    async def _add_bg_task(self, method, obj: ModelType | list[ModelType]) -> None:
        if obj:
            self.bg_tasks.add_task(method, obj) if self.bg_tasks is not None else await method(obj)

    async def set_cache(self, obj: ModelType | list[ModelType]) -> None:
        if obj:
            await self.redis.set_all(obj) if isinstance(obj, list) else await self.redis.set_obj(obj)

    async def __get(self, method_name: str | None = None, pk: int | None = None) -> ModelType | None:
        obj = (await self.redis.get_all() if pk is None else
               await self.redis.get_obj(pk))
        if obj:
            return obj
        obj = (await self.db.get_all() if pk is None else
               await self.db.__getattribute__(method_name)(pk))
        await self._add_bg_task(self.set_cache, obj)
        return obj

    async def get(self, pk: int) -> ModelType | None:
        return await self.__get('get', pk)

    async def get_or_404(self, pk: int) -> ModelType:
        return await self.__get('get_or_404', pk)

    async def get_all(self, exception: bool = False) -> list[ModelType] | None:
        return await self.__get()

    async def set_cache_create(self, obj) -> None:
        raise NotImplementedError(self.MSG_NOT_IMPLEMENTED)

    async def set_cache_update(self, obj) -> None:
        raise NotImplementedError(self.MSG_NOT_IMPLEMENTED)

    async def set_cache_delete(self, obj) -> None:
        raise NotImplementedError(self.MSG_NOT_IMPLEMENTED)

    async def create(self,
                     payload: pydantic.BaseModel,
                     extra_data: typing.Any | None = None) -> ModelType:
        """Base class provides database create method and put a setting
        cache task to background."""
        obj = await self.db.create(payload, extra_data=extra_data)
        await self._add_bg_task(self.set_cache_create, obj)
        return obj

    async def update(self,
                     pk: int,
                     payload: pydantic.BaseModel) -> ModelType:
        """Base class provides database update method."""
        obj = await self.db.update(pk, payload)
        await self._add_bg_task(self.set_cache_update, obj)
        return obj

    async def delete(self, pk: int) -> ModelType:
        """Base class provides database delete method."""
        obj = await self.db.delete(pk)
        await self._add_bg_task(self.set_cache_delete, obj)
        return obj


import re

# -------------------------------------------------------------------------
#
# Gramps modules
#
# -------------------------------------------------------------------------
from ..lib.date import Date
from ._dateparser import DateParser
from ._datedisplay import DateDisplay
from ._datehandler import register_datehandler

# -------------------------------------------------------------------------
#
# Slovenian parser
#
# -------------------------------------------------------------------------
class DateParserSL(DateParser):
    """
    Converts a text string into a Date object
    """

    modifier_to_int = {
        "pred": Date.MOD_BEFORE,
        "pr.": Date.MOD_BEFORE,
        "po": Date.MOD_AFTER,
        "okoli": Date.MOD_ABOUT,
        "okrog": Date.MOD_ABOUT,
        "okr.": Date.MOD_ABOUT,
        "ok.": Date.MOD_ABOUT,
        "cca.": Date.MOD_ABOUT,
        "cca": Date.MOD_ABOUT,
        "circa": Date.MOD_ABOUT,
        "ca.": Date.MOD_ABOUT,
        "približno": Date.MOD_ABOUT,
        "pribl.": Date.MOD_ABOUT,
        "~": Date.MOD_ABOUT,
        "from": Date.MOD_FROM,
        "to": Date.MOD_TO,
    }

    quality_to_int = {
        "ocenjeno": Date.QUAL_ESTIMATED,
        "oc.": Date.QUAL_ESTIMATED,
        "po oceni": Date.QUAL_ESTIMATED,
        "izračunano": Date.QUAL_CALCULATED,
        "izrač.": Date.QUAL_CALCULATED,
        "po izračunu": Date.QUAL_CALCULATED,
    }

    bce = [
        "pred našim štetjem",
        "pred Kristusom",
        "p.n.š.",
        "p. n. š.",
        "pr.Kr.",
        "pr. Kr.",
    ] + DateParser.bce

    def init_strings(self):
        """
        compiles regular expression strings for matching dates
        """

        DateParser.init_strings(self)
        # match 'Day. MONTH year.' format with or without dots
        self._text2 = re.compile(
            r"(\d+)?\.?\s*?%s\.?\s*((\d+)(/\d+)?)?\s*\.?$" % self._mon_str,
            re.IGNORECASE,
        )
        # match Day.Month.Year.
        self._numeric = re.compile(r"((\d+)[/\.-])?\s*((\d+)[/\.-])?\s*(\d+)\.?$")

        self._span = re.compile(
            r"od\s+(?P<start>.+)\s+do\s+(?P<stop>.+)", re.IGNORECASE
        )
        self._range = re.compile(
            r"med\s+(?P<start>.+)\s+in\s+(?P<stop>.+)", re.IGNORECASE
        )
        self._jtext2 = re.compile(
            r"(\d+)?.?\s+?%s\s*((\d+)(/\d+)?)?" % self._jmon_str, re.IGNORECASE
        )

# -------------------------------------------------------------------------
#
# Slovenian display
#
# -------------------------------------------------------------------------
class DateDisplaySL(DateDisplay):
    """
    Slovenian language date display class.
    """

    # TODO fix BUG 7064: non-Gregorian calendars wrongly use BCE notation for negative dates
    # not refactoring _bce_str into base class because it'll be gone under #7064
    _bce_str = "%s pr.Kr."

    display = DateDisplay.display_formatted

# -------------------------------------------------------------------------
#
# Register classes
#
# -------------------------------------------------------------------------
register_datehandler(
    ("sl_SI", "sl", "SL", "slovenščina", "slovenian", "Slovenian", ("%d. %m. %Y",)),
    DateParserSL,
    DateDisplaySL,
)


import sys
import datetime
import requests as re
from bs4 import BeautifulSoup as bs

def requests_url(url, headers):
    flipkart_api = re.get(url, headers=headers)
    return flipkart_api

print(f"Program started : {datetime.datetime.now()}")
flipkart_url = "https://www.flipkart.com/search?q=iphone&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off"
flipkart_headers = {'content-type': 'text/html'}

get_api = requests_url(flipkart_url, flipkart_headers)

iPhone_model = []
iPhone_rating = []
iPhone_rating_count = []
iPhone_review = []
iPhone_original_price = []
iPhone_discount_price = []
try:
    if get_api.status_code == 200:
        print("API STATUS RESPONSE SUCCESSFUL")
    else:
        sys.exit()
except Exception as e:
    print(f"Error : {e}")

try:
    soup = bs(get_api.text, "html.parser")

    mobile_name_rating = soup.findAll(class_="col col-7-12")

    for details in mobile_name_rating:
        mobile_name = details.find_all(class_="_4rR01T")
        for name in mobile_name:
            name = name.text
            iPhone_model.append(name)
            # print(f"Mobile Name : {name}")

        mobile_rating = details.find_all(class_="gUuXy-")
        for rating in mobile_rating:
            rating = rating.find_all(class_="_1lRcqv")
            for rate in rating:
                rate = rate.text
                iPhone_rating.append(rate)
            # print(f"Mobile rating : {rate}")
        for rating in mobile_rating:
            rating_count = rating.find_all(class_="_2_R_DZ")
            for rate_count in rating_count:
                rate_count = rate_count.text
                iPhone_rating_count.append(rate_count)
            # print(f"Mobile rating count and reviews : {rate_count}")
    print("Extracted Mobile Name,Rating,Rating Count,Reviews")

    mobile_price_details = soup.findAll(class_="col col-5-12 nlI3QM")

    for price_details in mobile_price_details:
        discount_mobile_price = price_details.find_all(class_="_30jeq3 _1_WHN1")
        for discount_price in discount_mobile_price:
            discount_price = discount_price.text
            iPhone_discount_price.append(discount_price)
            # print(f"Mobile discount_price : {discount_price}")

        original_mobile_price = price_details.find_all(class_="_3I9_wc _27UcVY")
        for original_price in original_mobile_price:
            original_price = original_price.text
            iPhone_original_price.append(original_price)
            #print(f"Mobile original_price : {original_price}")
    print("Extracted Mobile Original Price and Discount Price ")
    print(f"Program Ended : {datetime.datetime.now()}")
except Exception as e:
    print(f"Error : {e}")


import contextlib
import io
import os

import pytest

from shop.shop import Item

def test_get_attributes(item):
    """Проверка получения атрибутов экземпляра класса Item"""
    assert item.name == 'товар 1'
    assert item.price == 10000
    assert item.quantity == 20
    assert item.pay_rate == 0.85
    assert len(Item.all) == 1

def test_change_attributes(item):
    """Проверка изменения атрибутов"""
    item.name = 'товар 1_'
    item.price = 10
    item.quantity = 10
    item.pay_rate = 0.5

    assert item.name == 'товар 1_'
    assert item.price == 10
    assert item.quantity == 10
    assert item.pay_rate == 0.5

def test_calculate_total_price(item):
    """Проверка расчета общей стоимости товаров"""
    assert item.calculate_total_price() == 200000

def test_apply_discount(item):
    """Проверка применения скидки на товар"""
    item.pay_rate = 0.8
    assert item.apply_discount() == 8000
    assert item.price == 8000

def test_object_name_str(item):
    """Проверка отображения информации об объекте класса Item для пользователей"""
    assert str(item) == 'товар 1'

def test_object_name_repr(item):
    """Проверка отображения информации об объекте класса Item для разработчиков"""
    assert repr(item) == 'Item(name=товар 1, price=10000, quantity=20)'

def test_exception_long_name(item):
    """Проверка вызова исключения, если название товара будет превышать 10 символов"""
    with pytest.raises(Exception):
        item.name = 'длина названия товара больше 10 символов'

def test_instantiate_from_csv():
    """
    Проверка атрибутов экземпляров, созданных из файла csv.
    Проверка, что создано правильное количество экземпляров.
    """
    Item.instantiate_from_csv(path=os.path.join('tests', 'test.csv'))
    item_1 = Item.all[0]
    item_2 = Item.all[1]
    item_3 = Item.all[2]

    assert len(Item.all) == 3
    assert item_1.name == 'товар 1'
    assert item_1.price == 100
    assert item_1.quantity == 1
    assert item_2.name == 'товар 2'
    assert item_2.price == 55.5
    assert item_2.quantity == 3
    assert item_3.name == 'товар 3'
    assert item_3.price == 2000
    assert item_3.quantity == 6

def test_exception_file_not_found():
    """Проверка вызова исключения, если файл не найден"""
    s = io.StringIO()
    with contextlib.redirect_stdout(s):
        Item.instantiate_from_csv(path=os.path.join('tests', 'file.csv'))
    assert s.getvalue() == 'FileNotFoundError: Отсутствует файл tests\\file.csv\n'

def test_exception_file_damaged_1():
    """Проверка вызова исключения, если файл повреждён"""
    s = io.StringIO()
    with contextlib.redirect_stdout(s):
        Item.instantiate_from_csv(path=os.path.join('tests', 'bad_file_1.csv'))
    assert s.getvalue() == 'InstantiateCSVError: Файл item.csv поврежден\n'

def test_exception_file_damaged_2():
    """Проверка вызова исключения, если файл повреждён"""
    s = io.StringIO()
    with contextlib.redirect_stdout(s):
        Item.instantiate_from_csv(path=os.path.join('tests', 'bad_file_2.csv'))
    assert s.getvalue() == 'InstantiateCSVError: Файл item.csv поврежден\n'

def test_attributes_price_and_quantity():
    """
    Проверка того, что при инициализации объекта класса Item
    нельзя задать атрибуты цена и количество меньше 0
    """
    with pytest.raises(AttributeError):
        Item(name='товар 1', price=-10000, quantity=-1)

def test_add(item):
    """
    Проверка сложения двух экземпляров класса Item.
    Сложение происходит по атрибуту quantity.
    """
    assert item + item == 40

def test_add_exception(item):
    """
    Проверка вызова исключения, если правый операнд не является
    экземпляром класса Item
    """
    with pytest.raises(TypeError):
        result = item + 10


import numpy as np
from splines import latex_matrix

data = np.matrix([(4, 8, 1), (0, 2, -2), (3, 6, 7)])

def qr(A):
    m, n = A.shape

    r = np.zeros((m, n))
    q = np.zeros((n, n))

    for j in range(n):
        y = A[:, j]

        r[j, j] = np.linalg.norm(y.T)
        q[:, j] = (y / r[j, j]).flatten()

        for i in range(j - 1):
            r[j, i] = q[:, j].T * y
            y = y - r[j, i] * q[:, i]

    return q, r

if __name__ == "__main__":
    q, r = qr(data)
    print(latex_matrix(q))
    print(latex_matrix(r))


import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QSpinBox, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt

class QLabeledSpinBox(QWidget):
    def __init__(self, label='label for slider', range=(1,30), initValue:int = 20):
        super().__init__()

        self.SpinBox = QSpinBox()
        self.SpinBox.setFixedSize(70,40)
        font = self.SpinBox.font()
        font.setPointSize(12)
        self.SpinBox.setFont(font)
        self.SpinBox.setRange(*range)
        self.SpinBox.setValue(initValue)
        self.Label = QLabel()
        self.Label.setStyleSheet("""QLabel {
                                    font-size : 10pt;
                                    }""")
        self.Label.setText(label)
        self.Label.setAlignment(Qt.AlignCenter)

        self.Box = QVBoxLayout()
        self.Box.addWidget(self.Label, alignment=Qt.AlignCenter)
        self.Box.addWidget(self.SpinBox, alignment=Qt.AlignBottom | Qt.AlignCenter)

        self.setLayout(self.Box)

    def connect(self, changeValueFunc):
        self.SpinBox.valueChanged.connect(changeValueFunc)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = QLabeledSpinBox()
    ex.show()
    sys.exit(app.exec_())


import requests
from functools import wraps

from flask import request

from src.config import Config

class TokenValidator:

    def check_token(f):

        @wraps(f)
        def decorated(*args, **kwargs):
            if Config.DENY_ACCESS:
                print('Access denied, API_SECURITY_URL does not exist')
                return '', requests.codes.unauthorized
            token_header = request.headers['authorization']
            print('TOKEN: ' + token_header)
            response = requests.get(Config.API_SECURITY_URL, headers={"Authorization": token_header, "Content-Type": "application/json"})
            if response.status_code != requests.codes.ok:
                print('Access denied, token is invalid')
                return '', requests.codes.unauthorized
            return f(*args, **kwargs)
        return decorated


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

from typing import List, Optional, Tuple

import torch
import torch.nn.functional as F
from torch import Tensor

@torch.jit.script
def fourier_conv2d(
    inputs: Tensor,
    kernel: Tensor,
    stride: Tuple[int, int] = (1, 1),
    padding: Tuple[int, int] = (0, 0),
    padding_mode: str = "constant",
    fill_value: float = 0,
    bias: Optional[Tensor] = None,
) -> Tensor:
    r"""Performs 2D convolution as a multiplication in the frequency domain. This
    approach is more efficient for large kernel sizes and small strides.

    Args:
        inputs (:class:`torch.Tensor`):
            Tensor to convolve over

        kernel (:class:`torch.Tensor`):
            Convolution kernel

        stride (tuple of int, int):
            Convolution stride

        padding (tuple of int, int):
            Padding to be applied to inputs

        padding_mode (str):
            Same as :func:`torch.nn.functional.pad` ``padding_mode``

        fill_value (float):
            Same as :func:`torch.nn.functional.pad` ``fill_value``

        bias (:class:`torch.Tensor`):
            Bias term

    Shape
        * ``input`` - :math:`(N, C, H_i, W_i)`
        * ``kernel`` - :math:`(N, C, H_k, W_k)`
        * ``bias`` - :math:`(C)`
    """
    original_shape = inputs.shape
    spatial_ndim = 2

    # expand kernel.ndim to match inputs.ndim
    while kernel.ndim < inputs.ndim:
        kernel.unsqueeze_(0)
    assert kernel.ndim == inputs.ndim

    # pad inputs and fft
    _pad = (padding[1], padding[1], padding[0], padding[0])
    inputs = F.pad(inputs, _pad, padding_mode, fill_value)
    fft_inputs = torch.fft.fft2(inputs, s=inputs.shape[2:])

    # get padding to place kernel in tensor of shape input.shape
    kernel_padding: List[int] = []
    for i in range(spatial_ndim):
        size = inputs.shape[-(i + 1)] - kernel.shape[-(i + 1)]
        assert size >= 0
        kernel_padding += [0, size]
    assert len(kernel_padding) == 2 * spatial_ndim

    # pad kernel and fft
    _ = F.pad(kernel, kernel_padding)
    fft_kernel = torch.fft.fft2(_, s=inputs.shape[2:])

    # do convolution as multiplication in frequency domain and inverse fourier
    result = torch.fft.irfft2(fft_inputs * fft_kernel.conj(), inputs.shape[2:])
    result = result[..., :: stride[0], :: stride[1]]

    # crop to match expected output shape
    crop: List[int] = []
    for i, (s, p) in enumerate(zip(stride, padding)):
        l = original_shape[2 + i]
        k = kernel.shape[2 + i]
        crop.append((l - k + 2 * p) // s + 1)
    result = result[..., : crop[0], : crop[1]]
    result = result.contiguous()

    if bias is not None:
        result += bias

    return result


# -*- coding: utf-8 -*-
"""
Created on Fri Jun 21 15:02:49 2019

@author: admin
"""
import pickle
import scipy.io as sio
import scipy.sparse as ss
import numpy as np
from ut import IO, Eval, Misc
from load_dataset import *
DIR = 'data/'+'ml-10m'+'/'
model_pickle = open("./RCE/data/ml-10m/ml-10m32.pkl",'rb')
params = pickle.load(model_pickle, encoding='latin1')
#data_generator = Data(train_file=DIR+'train.mat', test_file=DIR+'test.mat',batch_size=1024,num_neg=5)
train_file=DIR+'train.mat'
test_file=DIR+'test.mat'
R = sio.loadmat(train_file)['train'].tocsr()
test = sio.loadmat(test_file)['test'].tocsr()
U = params[0]
V = params[1]
user_for_test = sio.loadmat("./RCE/data/ml-10m/user.mat")['user'][0]
topk_mat = Eval.topk_search(R[user_for_test,:], U[user_for_test,:], V, 200)
#print(topk_mat.shape)
rerank_mat = np.zeros((69662,100))
user_embed = np.load('./RCE/U_model.npy')
item_embed = np.load('./RCE/V_model.npy')
for id_,u in enumerate(user_for_test):
    user = u
    item = topk_mat[id_,:]
    rerank_score = np.matmul(user_embed[u,:], item_embed[np.squeeze(item),:].T)
    rerank_score = np.squeeze(rerank_score)
#    print(rerank_score.shape)
#    print(rerank_mat[u,:].shape)
#    print(item[(-rerank_score).argsort()[:100]].shape)
    rerank_mat[u,:] = item[(-rerank_score).argsort()[:100]]
rerank = Eval.evaluate_topk(R[user_for_test,:], test[user_for_test,:], rerank_mat[user_for_test,:], 100)
print(Eval.format(rerank))


import email
from pickle import TRUE
from django.db import models

class Persona(models.Model):
    """Model definition for Persona."""

    full_name = models.CharField(max_length=50)
    pais = models.CharField(max_length=50)
    pasaporte = models.CharField(max_length=50)
    edad = models.IntegerField()
    apelativo = models.CharField(max_length=10)

    class Meta:
        """Meta definition for Persona."""

        verbose_name = 'Persona'
        verbose_name_plural = 'Personas'

        # Se utiliza este atributo en el class Meta para indicarle a django que de esta manera
        # tiene que aparecer nuestro modelo en el gestor de la base de datos.
        db_table = 'persona'

        # Se usa este campo para hacer una validacion directamente en el modelo
        # Se usa unique_toguether para que no se repitan registros juntos con esas caracteristicas
        unique_together = ['pais', 'apelativo']

        # Se utiliza el atributo constraints para hacer una validacion
        # En este caso que edad sea mayor a 18
        constraints = [
            models.CheckConstraint(
                check= models.Q(edad__gte = 18),
                name='edad_mayor_18'
            )
        ]

        # Con este atributo le indicamos a django que queremos que se considere un modelo
        # pero que no se cree en nuestra base de datos ya qu lo vamos a utilizar solo para herencia
        abstract = True

    def __str__(self):
        """Unicode representation of Persona."""
        return self.full_name

# Construimos un modelo Empleado heredando los atributos del modelo Persona
class Empleados(Persona):
    empleo = models.CharField(max_length=50)

# Construimos un modelo Cliente heredando los atributos del modelo Persona
class Cliente(Persona):
    email = models.EmailField('Email', max_length=254)


import json

from django.core.serializers.json import DjangoJSONEncoder
from wagtail.core import blocks

from apps.events.models import EventPage

class EventsTeaserBlock(blocks.StructBlock):
    heading = blocks.CharBlock(required=False)

    def get_context(self, value, parent_context=None):
        context = super().get_context(value, parent_context=parent_context)
        events = EventPage.objects.live()
        js_events = events.values('date', 'title', 'short_description')
        js_data = json.dumps(list(js_events), cls=DjangoJSONEncoder)

        context.update({
            'events': events,
            'js_data': js_data
        })

        return context

    class Meta:
        template = 'events/blocks/eventsteaser_block.html'


import sqlite3
import json

def main():
    allShopInfo = json.load(open('all_shop.json'))

    con = sqlite3.connect('all_shop.sqlite')
    cur = con.cursor()
    cur.execute('''DROP TABLE IF EXISTS `info`''')
    cur.execute('''CREATE TABLE `info` (`version` TEXT)''')
    cur.execute('''INSERT INTO `info` (`version`) VALUES (?)''', (allShopInfo['version'],))
    fieldnames = allShopInfo['shopList'][0].keys()
    shopTableCreateStatement = []
    shopTableCreateStatement.append('CREATE TABLE `shops` (')
    typeMapping = {'px': 'REAL', 'py': 'REAL'}
    prefix = ""
    for fieldname in fieldnames:
        if fieldname[0].isalpha() and fieldname.isalnum():
            shopTableCreateStatement.append(prefix + '`' + fieldname + '` ' + typeMapping.get(fieldname, 'TEXT'))
            prefix = ", "
    shopTableCreateStatement.append(')')
    cur.execute('DROP TABLE IF EXISTS `shops`')
    cur.execute(''.join(shopTableCreateStatement))

    shopInsertData = []
    for shop in allShopInfo['shopList']:
        shopData = []
        for fieldname in fieldnames:
            shopData.append(shop.get(fieldname, ''))
        shopInsertData.append(tuple(shopData))

    shopTableInsertStatement = []
    shopTableInsertStatement.append('INSERT INTO `shops` (')
    prefix = ""
    for fieldname in fieldnames:
        shopTableInsertStatement.append(prefix + '`' + fieldname + '`')
        prefix = ", "
    shopTableInsertStatement.append(') VALUES (')
    shopTableInsertStatement.append('?' + ' ,?' * (len(fieldnames) - 1))
    shopTableInsertStatement.append(')')

    cur.executemany(''.join(shopTableInsertStatement), shopInsertData)

    cur.close()
    con.commit()
    con.close()

if __name__ == '__main__':
    main()


# author : Philippe Vo
# date   : Mon 02 May 2022 07:24:29 PM

# 3rd party imports
import logging
import os

logger = logging.getLogger(__name__)

def get_user_choice(options: list, msg: str):
    """show list of options for user and ask them to choose an option
    """
    validIdxs = []
    for idx, val in enumerate(options):
        print("{} : {}".format(idx, val))
        validIdxs.append(idx)

    validChoice = False
    while not validChoice:
        try:
            idxChoose = int(input(msg))
            logging.info("selected : {}".format(options[idxChoose]))
            validChoice = True

        except (ValueError, IndexError):
            logging.warning("invalid choice")

    return idxChoose


import json

if __name__ == '__main__':
    test_movie_ids = json.load(open('data/test.json'))
    data = json.load(open('data/features.json'))

    data_to_submit = {}
    for movie_id in test_movie_ids:
        data_to_submit[movie_id] = data[str(movie_id)]

    with open('data/data.json', 'w') as outfile:
        outfile.write(json.dumps(data_to_submit, indent=4, encoding="utf-8"))


from fastapi import APIRouter
from apis.schemas.requests.label_studio.project_request import (
    TransformAnnotation,
)
from apis.schemas.response.label_studio.project_response import (
    TransformAnnotationResponse,
)
from controllers.label_studio.label_studio_controller import ProjectController

project_router = APIRouter()

@project_router.post(
    "/label_studio/transform_annotations", response_model=TransformAnnotationResponse
)
def transform_annotations(transform_annotation_request: TransformAnnotation):
    response = ProjectController().transform_annotations_controller(
        transform_annotation_request
    )
    return TransformAnnotationResponse(**response)


#__init__ config might missing,
# so the parent module was not able to found,
# many exceptions had been raised,
# absolute and realtive import did not work neither,
# so this solution was the only one that actually worked
import sys
import sqlalchemy as db
import pandas as pd
import psycopg2
import sqlite3
import numpy as np
sys.path.append('/Users/attilakiss/Desktop/project_HaHU_KA/Project-HaHU_KA/module1_data_download')
import catalog_collect_class
import SQL_load_class
import URL_validation_class

#creating the list of the missing catalogURLs
missing_catalog_df = pd.DataFrame()
missing_catalog_df = pd.read_excel(
    '/Users/attilakiss/Desktop/project_HaHU_KA/Project-HaHU_KA/ad_id.xlsx',
    'missing_catalogs',
    header = None)

sql_database = '/Users/attilakiss/Desktop/project_HaHU_KA/Project-HaHU_KA/DB/test_db.db'
conn = sqlite3.connect(sql_database)
cur = conn.cursor()

missing_catalogs = missing_catalog_df.iloc[:,0].to_list()  #creating a list from the df
#catalog_validation = True  #manual input for the function / otherwise it would be evaluated
url_validation = URL_validation_class.URL_Validation()
for catalog in missing_catalogs:
    #validating the missing URL
    url_validation.catalog_url_validation(catalog, cur)

    #gathering the catalog data
    car_catalog = catalog_collect_class.CatalogDownload()
    # downloading the raw catalog data
    car_catalog.catalog_raw_download(catalog, url_validation.catalog_validation)
    car_catalog.catalog_data_retrive(cur, catalog)

    #compiling the catalog data
    car_catalog.catalog_attributes['catalog_url'] = catalog

    #loading the cleaned catalog data into the SQL DB
    sql_load = SQL_load_class.SQL_load()
    sql_load.sql_load_catalog(car_catalog.catalog_attributes,
                              cur,
                              url_validation.catalog_validation)
    conn.commit()


from typing import Mapping, MutableMapping, MutableSequence, Sequence

from .classes import classCastFuncs, classSchema

def setItem(subject, key, item, *, itemCast=None):
    if itemCast is not None:
        item = itemCast(item)
    if isinstance(subject, (MutableMapping, MutableSequence)):
        subject[key] = item
    else:
        setattr(subject, key, item)

def delAttr(subject, key, *, itemCast=None):
    if isinstance(subject, Sequence):
        raise TypeError("can't call delattr on list")
    elif isinstance(subject, MutableMapping):
        del subject[key]
    else:
        delattr(subject, key)

def delItems(subject, index, deleteCount=1, itemCast=None):
    spliceItems(subject, index, deleteCount)

def insertItems(subject, index, *items, itemCast=None):
    spliceItems(subject, index, 0, *items, itemCast=itemCast)

def spliceItems(subject, index, deleteCount, *items, itemCast=None):
    if itemCast is not None:
        items = [itemCast(item) for item in items]
    subject[index : index + deleteCount] = items

baseChangeFunctions = {
    "=": setItem,
    "d": delAttr,
    "-": delItems,
    "+": insertItems,
    ":": spliceItems,
}

# TODO: Refactor. These don't really belong here,
# and should ideally be registered from outside
changeFunctions = {
    **baseChangeFunctions,
    "=xy": lambda path, pointIndex, x, y: path.setPointPosition(pointIndex, x, y),
    "insertContour": lambda path, contourIndex, contour: path.insertContour(
        contourIndex, contour
    ),
    "deleteContour": lambda path, contourIndex: path.deleteContour(contourIndex),
    "deletePoint": lambda path, contourIndex, contourPointIndex: path.deletePoint(
        contourIndex, contourPointIndex
    ),
    "insertPoint": lambda path, contourIndex, contourPointIndex, point: path.insertPoint(
        contourIndex, contourPointIndex, point
    ),
}

#
# A "change" object is a simple JS object containing several
# keys.
#
# "p": an array of path items, eg. ["glyphs", "Aring"]
# Optional: can be omitted if empty.
#
# "f": function name, to be looked up in the changeFunctions dict
# Optional: can be omitted if the change has children
#
# "a": "arguments", an array of arguments for the change function
# Optional: if omitted, defaults to an empty array
#
# "c": Array of child changes. Optional.
#

def applyChange(subject, change):
    """Apply `change` to `subject`."""
    _applyChange(subject, change)

def _applyChange(subject, change, *, itemCast=None):
    path = change.get("p", [])
    functionName = change.get("f")
    children = change.get("c", [])

    for pathElement in path:
        itemCast = None
        if isinstance(subject, (Mapping, Sequence)):
            subject = subject[pathElement]
        else:
            itemCast = getItemCast(subject, pathElement, "subtype")
            subject = getattr(subject, pathElement)

    if functionName is not None:
        changeFunc = changeFunctions[functionName]
        args = change.get("a", [])
        if functionName in baseChangeFunctions:
            if itemCast is None and args:
                itemCast = getItemCast(subject, args[0], "type")
            changeFunc(subject, *args, itemCast=itemCast)
        else:
            changeFunc(subject, *args)

    for subChange in children:
        _applyChange(subject, subChange, itemCast=itemCast)

def getItemCast(subject, attrName, fieldKey):
    classFields = classSchema.get(type(subject))
    if classFields is not None:
        fieldDef = classFields[attrName]
        subtype = fieldDef.get(fieldKey)
        if subtype is not None:
            return classCastFuncs.get(subtype)
    return None

_MISSING = object()

def matchChangePattern(change, matchPattern):
    """Return `True` or `False`, depending on whether the `change` matches
    the `matchPattern`.

    A `matchPattern` is tree in the form of a dict, where keys are change path
    elements, and values are either nested pattern dicts or `None`, to indicate
    a leaf node.
    """
    node = matchPattern
    for pathElement in change.get("p", []):
        childNode = node.get(pathElement, _MISSING)
        if childNode is _MISSING:
            return False
        if childNode is None:
            # leaf node
            return True
        node = childNode

    for childChange in change.get("c", []):
        if matchChangePattern(childChange, node):
            return True

    return False

def filterChangePattern(change, matchPattern, inverse=False):
    """Return a subset of the `change` according to the `matchPattern`, or `None`
    if the `change` doesn't match `matchPattern` at all. If there is a match,
    all parts of the change that do not match are not included in the returned
    change object.

    A `matchPattern` is tree in the form of a dict, where keys are change path
    elements, and values are either nested pattern dicts or `None`, to indicate
    a leaf node.

    If `inverse` is True, `matchPattern` is used to exclude the change items
    that match from the return value.
    """
    node = matchPattern
    for pathElement in change.get("p", []):
        childNode = node.get(pathElement, _MISSING)
        if childNode is _MISSING:
            return change if inverse else None
        if childNode is None:
            # leaf node
            return None if inverse else change
        node = childNode

    filteredChildren = []
    for childChange in change.get("c", []):
        childChange = filterChangePattern(childChange, node, inverse)
        if childChange is not None:
            filteredChildren.append(childChange)

    result = {**change, "c": filteredChildren}
    if not inverse:
        # We've at most matched one or more children, but not the root change
        result.pop("f", None)
        result.pop("a", None)

    return _normalizeChange(result)

def _normalizeChange(change):
    children = change.get("c", ())

    if "f" not in change and len(children) == 1:
        # Turn only child into root change
        result = {**children[0]}
        # Prefix child path with original root path
        result["p"] = change.get("p", []) + result.get("p", [])
    else:
        result = {**change}

    if not result.get("p"):
        # Remove empty path
        result.pop("p", None)

    if not result.get("c"):
        # Remove empty children list
        result.pop("c", None)

    if len(result) == 1 and "p" in result:
        # Nothing left but a path: no-op change
        result.pop("p", None)

    if not result:
        result = None

    return result

def patternFromPath(matchPath):
    """Given a list of path elements, return a pattern dict."""
    pattern = {}
    if matchPath:
        pattern[matchPath[0]] = (
            None if len(matchPath) == 1 else patternFromPath(matchPath[1:])
        )
    return pattern

def patternUnion(patternA, patternB):
    """Return a pattern which is the union of `patternA` and `patternB`:
    the result will match everything from `patternA` and `patternB`.
    """
    result = {**patternA}
    for key, valueB in patternB.items():
        valueA = patternA.get(key, _MISSING)
        if valueA is _MISSING or valueB is None:
            result[key] = valueB
        elif valueA is not None:
            result[key] = patternUnion(valueA, valueB)
        else:
            # valueA is None -- patternA already matches a prefix of
            # patternB: nothing to do
            pass
    return result

def patternDifference(patternA, patternB):
    """Return a pattern which is `patternA` minus `patternB`: the result will
    only match the items from `patternA` that are not included in `patternB`.
    """
    result = {**patternA}
    for key, valueB in patternB.items():
        valueA = patternA.get(key, _MISSING)
        if valueA is _MISSING:
            pass
        elif valueB is None:
            del result[key]
        elif valueA is None:
            pass
        else:
            result[key] = patternDifference(valueA, valueB)
            if not result[key]:
                del result[key]
    return result

def patternIntersect(patternA, patternB):
    """Return the intersection of `patternA` and `patternB`. The resulting pattern
    will only match items that are included in both patterns.
    """
    result = {}
    for key, valueA in patternA.items():
        valueB = patternB.get(key, _MISSING)
        if valueB is _MISSING:
            continue
        if valueA is None:
            result[key] = valueB
        elif valueB is None:
            result[key] = valueA
        else:
            childResult = patternIntersect(valueA, valueB)
            if childResult:
                result[key] = childResult
    return result

def collectChangePaths(change, depth):
    """Return a sorted list of paths of the specified `depth` that the `change`
    includes."""
    return sorted(set(_iterateChangePaths(change, depth)))

def _iterateChangePaths(change, depth, prefix=()):
    path = prefix + tuple(change.get("p", ()))
    if len(path) >= depth:
        yield path[:depth]
        return
    for childChange in change.get("c", []):
        yield from _iterateChangePaths(childChange, depth, path)


import os
import re
from datetime import datetime
import boto3
from botocore.exceptions import ClientError

s3_client = boto3.client('s3')

# Function to upload the latest CSV file from a folder to an S3 bucket
def data_ingestion(folder_path, s3_bucket_name):
    csv_files = [f for f in os.listdir(folder_path) if f.endswith('.csv')]
    latest_date = None
    latest_csv_file = None

    for csv_file in csv_files:
        date_match = re.match(r'^(\d{8})', csv_file)
        if date_match:
            date_str = date_match.group(1)
            try:
                file_date = datetime.strptime(date_str, '%Y%m%d')
                if not latest_date or file_date > latest_date:
                    latest_date = file_date
                    latest_csv_file = csv_file
            except ValueError:
                print(f"Invalid date format in CSV file {csv_file}")

    if latest_csv_file:
        local_csv_path = os.path.join(folder_path, latest_csv_file)
        try:
            s3_client.upload_file(local_csv_path, s3_bucket_name, latest_csv_file)
            print(f"Latest CSV file {local_csv_path} uploaded to S3 bucket {s3_bucket_name}")
        except ClientError as e:
            print(f"Error uploading latest CSV file {local_csv_path} to S3 bucket {s3_bucket_name}: {e}")
    else:
        print("No valid CSV files found in the folder")

# Upload the latest local CSV file to the input S3 bucket
data_ingestion('/Users/maximkiesel/batch_processing_IaC_AWS/data/', 'data-ingestion-bucket')


import os
import subprocess
from pathlib import PurePath

def get(remote_path, local_path, rootfs):
    subprocess.check_call(["debugfs", "-R", "dump -p %s %s" % (remote_path, local_path), rootfs],
                          stderr=subprocess.STDOUT)

def put(local_path, remote_path, rootfs, remote_path_mkdir_p=False):
    proc = subprocess.Popen(["debugfs", "-w", rootfs], stdin=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
    if remote_path_mkdir_p:
        # Create parent directories sequencially, to simulate a "mkdir -p" on the final dir
        parent_dirs = list(PurePath(remote_path).parents)[::-1][1:]
        for parent in parent_dirs:
            proc.stdin.write(("mkdir %s\n" % parent).encode())
    proc.stdin.write(("cd %s\n" % os.path.dirname(remote_path)).encode())
    proc.stdin.write(("rm %s\n" % os.path.basename(remote_path)).encode())
    proc.stdin.write(("write %s %s\n" % (local_path, os.path.basename(remote_path))).encode())
    proc.stdin.close()
    ret = proc.wait()
    assert ret == 0

def extract_ext4(img, rootfs):
    return _manipulate_ext4(img=img, rootfs=rootfs, write=False)

def insert_ext4(img, rootfs):
    return _manipulate_ext4(img=img, rootfs=rootfs, write=True)

def _manipulate_ext4(img, rootfs, write):
    # calls partx with --show --bytes --noheadings, sample output:
    #
    # $ partx -sbg core-image-full-cmdline-vexpress-qemu.sdimg
    # NR  START    END SECTORS      SIZE NAME UUID  <-- NOTE: this is not shown
    # 1  49152  81919   32768  16777216      a38e337d-01
    # 2  81920 294911  212992 109051904      a38e337d-02
    # 3 294912 507903  212992 109051904      a38e337d-03
    # 4 507904 770047  262144 134217728      a38e337d-04
    output = subprocess.check_output(["partx", "-sbg", img])
    for line in output.decode().split('\n'):
        columns = line.split()
        # This blindly assumes that rootfs is on partition 2.
        if columns[0] == "2":
            if write:
                subprocess.check_call(["dd", "if=%s" % rootfs, "of=%s" % img,
                                       "seek=%s" % columns[1],
                                       "count=%d" % (int(columns[3])),
                                       "conv=notrunc"],
                                      stderr=subprocess.STDOUT)
            else:
                subprocess.check_call(["dd", "if=%s" % img, "of=%s" % rootfs,
                                       "skip=%s" % columns[1],
                                       "count=%d" % (int(columns[3]))],
                                      stderr=subprocess.STDOUT)
            break
    else:
        raise Exception("%s not found in fdisk output: %s" % (img, output))


from django.db import models
from django.utils.translation import gettext_lazy as _
from django.utils import timezone
from access.managers import ReadAccessControlManager, WriteAccessControlManager

from events.models import Event

class AccessControl(models.Model):
    event = models.ForeignKey(
        Event, related_name="access", verbose_name=_("event"), on_delete=models.CASCADE
    )

    name = models.CharField(_("name"), max_length=200)
    slug = models.SlugField(unique=True)

    location = models.CharField(_("location"), max_length=500, blank=True, null=True)
    mark_used = models.BooleanField(_("mark used"), default=True)

    read_objects = ReadAccessControlManager()
    objects = WriteAccessControlManager()

    class Meta:
        verbose_name = _("access control point")
        verbose_name_plural = _("access control points")

    def __str__(self):
        return self.name

    def checked_today(self, date, status=None):
        if not date:
            date = timezone.now()
        today = self.log_access.filter(
            date__year=date.year, date__month=date.month, date__day=date.day
        )
        if status:
            today = today.filter(status=status)
        return today.count()

AC_TYPES = (
    ("ok", _("ok")),
    ("right", _("ok")),
    ("wrong", _("wrong")),
    ("incorrect", _("incorrect")),
    ("used", _("used")),
    ("maybe", _("maybe")),
)

class LogAccessControl(models.Model):
    access_control = models.ForeignKey(
        AccessControl,
        related_name="log_access",
        verbose_name=_("access control"),
        on_delete=models.CASCADE,
    )
    date = models.DateTimeField(auto_now_add=True)
    status = models.CharField(
        _("status"), max_length=10, choices=AC_TYPES, default="right"
    )

    class Meta:
        verbose_name = _("access control point log")
        verbose_name_plural = _("access control point logs")


import urllib.request # url libraries
import json # import json libraries
import re # regex for cleaning entries

# creating dynamic time variable for yesterday
from datetime import datetime, timedelta # importing for date calculation
yesterday = (datetime.now() - timedelta(1)).strftime('%Y-%m-%d') # yesterday's date

# reading in data
url = 'https://data.nsw.gov.au/data/api/3/action/datastore_search?resource_id=21304414-1ff1-4243-a5d2-f52778048b29&q=%22'+yesterday+'%22'
fileobj = urllib.request.urlopen(url)
dict_data = fileobj.read()
dict_data2 = json.loads(dict_data) # loading string returned from endpoint into dictionary

## Note: data is stored as nested dictionary e.g. {meta data{results}}

# print total cases
total_cases = dict_data2['result']['total']

# print local government areas as a list
lga_list = [] # initialise list
record_list = dict_data2['result']['records']
for dict_entry in record_list:
    if dict_entry['lga_name19'] != None:
        modified_entry = re.sub(r"\([^()]*\)", "", dict_entry['lga_name19'])
        lga_list.append(modified_entry + '(' + (dict_entry['postcode']) + ')')
    else:
        lga_list.append("Not Available")

# helper function to return string from list
def listtostring(input_list):
    final_string = ""
    final_string = '\n'.join([str(item) for item in input_list])
    return final_string

# sending out SMS to numbers
from twilio.rest import Client

# Twilio account details
account_sid = "" # Insert account id here
auth_token = "" #Insert authentication token here
numbers = ["+61"] # add numbers you want to send here
# karl, mum, alison, james, brandon, donald, hillman, daniel

client = Client(account_sid, auth_token)
message = client.api.account.messages.create(

    to=numbers,
    from_="+123456789",
    body="Welcome to Karl's COVID-19 SMS notification service - you are a beloved friend of Karl's and so he has provided you this service free of charge to keep you safe during these tough times. On the " + str(yesterday) +
         " there had been a total of " + str(total_cases) + " cases confirmed at these locations/post codes: \n\n"
        +

        listtostring(lga_list)

        +
        "\n\nData in this notification has been taken from the data.NSW web API (provided by NSW Health). \n\nThis script is running on a Raspberry Pi 4 Model B 24/7 - notifications will be sent out daily at 6pm AEST everyday regarding coronavirus cases in the previous day. \n\nStay Safe.")


import cx_Oracle

conn = cx_Oracle.connect('scott','tiger','192.168.1.11:1521/XE')
cursor = conn.cursor()
# sql = "insert into dept values(50,'databse','seoul')"
# cursor.execute(sql)
sql = "insert into dept values(:1,:2,:3)"
code = input('부서코드를 입력하세요(2자리 숫자) >>> ')
dname = input('부서명을 입력하세요  >>>')
city = input('위치를 입력하세요 >>> ')
data = (int(code),dname,city)
cursor.execute(sql,data)
cursor.close()
conn.commit()
conn.close()


# The contents of this file are taken and modified from https://github.com/ialhashim/DenseDepth.
# pylint: disable=missing-function-docstring
from typing import Tuple
import torch
import torch.nn.functional as F
import torchvision.models as M  # type: ignore
from torch import nn

class DenseDepthEncoder(nn.Module):
    """
    The DenseDepth encoder uses DenseNet to obtain an encoding of shape [dim, 320, 240] from an
    input of size [3, 640, 480] for a given output dimension.
    """

    def __init__(self, out_dim: int, *, dropout: float = 0.0):
        super().__init__()
        self.feature_extractor = FeatureExtractor()

        in_features = 2208
        out_features = in_features // 2
        self.conv_in = nn.Sequential(
            nn.Conv2d(in_features, out_features, kernel_size=1, stride=1, padding=1),
            nn.Dropout2d(dropout),
        )
        self.up1 = Upsample(out_features + 384, out_features // 2, dropout=dropout)
        self.up2 = Upsample(out_features // 2 + 192, out_features // 4, dropout=dropout)
        self.up3 = Upsample(out_features // 4 + 96, out_features // 8, dropout=dropout)
        self.up4 = Upsample(out_features // 8 + 96, out_dim)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        block0, block1, block2, block3, block4 = self.feature_extractor(x)
        x0 = self.conv_in(block4)
        x1 = self.up1(x0, block3)
        x2 = self.up2(x1, block2)
        x3 = self.up3(x2, block1)
        return self.up4(x3, block0)

class FeatureExtractor(nn.Module):
    """
    The feature extractor of the DenseDepth encoder, based on DenseNet.
    """

    def __init__(self):
        super().__init__()
        densenet = M.densenet161(pretrained=True)
        features = densenet.features
        self.encoder0 = nn.Sequential(
            features.conv0, features.norm0, features.relu0  # type: ignore
        )
        self.encoder1 = nn.Sequential(features.pool0)  # type: ignore
        self.encoder2 = nn.Sequential(features.denseblock1, features.transition1)  # type: ignore
        self.encoder3 = nn.Sequential(features.denseblock2, features.transition2)  # type: ignore
        self.encoder4 = nn.Sequential(
            features.denseblock3, features.transition3, features.denseblock4  # type: ignore
        )

    def forward(
        self, x: torch.Tensor
    ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
        block0 = self.encoder0(x)
        block1 = self.encoder1(block0)
        block2 = self.encoder2(block1)
        block3 = self.encoder3(block2)
        block4 = self.encoder4(block3)
        return block0, block1, block2, block3, block4

class Upsample(nn.Module):
    """
    The upsample module increases the size of an image.
    """

    def __init__(self, in_channels: int, out_channels: int, dropout: float = 0.0):
        super().__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1),
            nn.LeakyReLU(0.2),
            nn.Dropout2d(dropout),
            nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),
            nn.LeakyReLU(0.2),
            nn.Dropout2d(dropout),
        )

    def forward(self, x: torch.Tensor, concat: torch.Tensor) -> torch.Tensor:
        z = F.interpolate(
            x, size=[concat.size(2), concat.size(3)], mode="bilinear", align_corners=True
        )
        z = torch.cat([z, concat], dim=1)
        return self.conv(z)


from opentrons import labware
from opentrons import instruments
# from opentrons.data_storage import database

tiprack = labware.load('opentrons-tiprack-300ul', '1')

plate = labware.load('96-flat', '2')
#create custom plate if it does not currently exist in robot software
plate_agar='agar_plate_9_9_9'
if plate_agar not in labware.list():
	#print('hi')
	custom_plate = labware.create(
	        plate_agar,                    # name of you labware
	        grid=(12, 8),                    # specify amount of (columns, rows)
	        spacing=(9, 9),               # distances (mm) between each (column, row)
	        diameter=9,                     # diameter (mm) of each well on the plate
	        depth=5,                       # depth (mm) of each well on the plate
	        volume=200)

plate2 = labware.load(plate_agar, '5')

pipette = instruments.P300_Single(
	mount = 'right',
	tip_racks = [tiprack])

# Testing single drop
# pipette.pick_up_tip(tiprack.wells('A1'))
# pipette.aspirate(20, plate.wells('A1'))
# pipette.dispense(20, plate2.wells('A1'))

#Testing drops along single column
# pipette.transfer(7,
# 				plate.wells('A1', 'B1'),
# 				plate2.wells('A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1'))

# Testing drops along single row
# pipette.transfer(20,
# 				plate.wells('A1', 'B1', 'C1'),
# 				plate2.wells('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11', 'A12'))

# Testing drops along first row & column
pipette.transfer(14,
				plate.wells('A1'),
				plate2.wells('B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1'))
pipette.transfer(14,
				plate.wells('B1', 'C1'),
				plate2.wells('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11', 'A12'))


from urllib.request import urlopen
from bs4 import BeautifulSoup
import pyexcel
# 1. Tạo một kết nôi Create conection
url = "https://dantri.com.vn"
conn = urlopen(url)

# 2. Download page
raw_data = conn.read()

page_content  = raw_data.decode("utf8")

# print(page_content)
# 3. Find ro Region of interest
soup = BeautifulSoup(page_content, "html.parser")
ul = soup.find("ul", "ul1 ulnew")

# 4. Extra ROI
li_list = ul.find_all("li")

# print(li_list)
news_list = []
for li in li_list:
    h4 = li.h4
    a = h4.a
    link = url + a["href"]
    title = a.string
    news = {
        "link": link,
        "title": title
    }
    news_list.append(news)
    print(link)

    print(title)

# print(li)
# print(ul.prettify())
# print(soup.prettify())
# 5. Save
pyexcel.save_as(records=news_list, dest_file_name="your_file.xlsx")


__author__ = 'seamus tuohy'
__date__ = '2020-12-05'
__version__ = '0.00.1'

import logging

FORMAT = "%(levelname)s [%(filename)s:%(lineno)s - %(funcName)s() ] %(message)s"
formatter = logging.Formatter(FORMAT)
default_handler = logging.StreamHandler()
default_handler.setFormatter(formatter)

logger = logging.getLogger(__name__)
logger.addHandler(default_handler)
logger.setLevel(logging.WARNING)

from RTFDE.deencapsulate import DeEncapsulator


from django.contrib import messages
from django.contrib.auth import login, logout
from django.contrib.auth.forms import AuthenticationForm
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.detail import DetailView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.edit import FormView
from django.urls import reverse, reverse_lazy
from django.http import HttpResponseRedirect
from . import forms
from projects.forms import UserCompletedProjectFormset
from skills.forms import SkillFormset
from .models import User
from skills.models import Skill
from applications.models import Application
from projects.models import UserCompletedProject

class ProfileView(LoginRequiredMixin, UpdateView):
    model = User
    success_url = reverse_lazy('accounts:profile',
                               kwargs={'option': None}
                               )
    form_class = forms.ProfileCreateForm
    context_object = 'user'

    def get_object(self):
        return User.objects.get(pk=self.request.user.id)

    def get_template_names(self):
        """this method checks the option,
        if its not view it shows the edit template"""
        option = self.kwargs.get('option')
        if option is not None:
            return 'accounts/profile.html'
        else:
            return 'accounts/edit_profile.html'

    def get_context_data(self, *args, **kwargs):
        data = super().get_context_data(**kwargs)
        if self.request.POST:
            data['CompletedProjectsFormset'] = UserCompletedProjectFormset(
                                            self.request.POST,
                                            prefix='completed_projects')
            data['SkillsFormset'] = SkillFormset(
                                            self.request.POST,
                                            prefix='skills')
        else:
            data['accepted_applications'] = Application.objects.filter(
                                        employee=self.get_object(),
                                        accepted=True
                                    )
            data['CompletedProjectsFormset'] = UserCompletedProjectFormset(
                    prefix='completed_projects',
                    queryset=UserCompletedProject.objects.filter(
                                             user=self.get_object())
                                            )
            data['SkillsFormset'] = SkillFormset(
                    prefix='skills',
                    queryset=Skill.objects.filter(users=self.get_object())
                )
            # check if there are any completed projects for the user
            projects = UserCompletedProject.objects.filter(
                                             user=self.get_object()
                                            )
            # check if the user has added skills
            skills = Skill.objects.filter(users=self.get_object())
            if not projects:
                # if there are no projects send a extra form through
                data['CompletedProjectsFormset'].extra = 1
            if not skills:
                # if there are no skills send a extra form through
                data['SkillsFormset'].extra = 1
        return data

    def get_form(self, *args, **kwargs):
        form = super(ProfileView, self).get_form(*args, **kwargs)
        form.fields['full_name'].widget.attrs['class'] = 'circle--input--h1'
        form.fields['full_name'].widget.attrs['placeholder'] = 'Firstname'
        form.fields['full_name'].label = ''
        form.fields['bio'].widget.attrs['placeholder'] = (
                                            '''
                                              Tell us about yourself...
                                            '''
                                        )
        form.fields['bio'].label = ''
        return form

    def form_valid(self, form):
        data = self.get_context_data()
        instance = form.save(commit=False)
        completed_projects = data['CompletedProjectsFormset']
        skills_formset = data['SkillsFormset']
        if completed_projects.is_valid() and skills_formset.is_valid():
            projects = completed_projects.save(commit=False)
            skills = skills_formset.save(commit=False)
            for project in projects:
                project.user = instance
                project.save()
            for obj in skills_formset.deleted_objects:
                obj.users.remove(instance)
            for skill in skills:
                skill_instance, created = Skill.objects.get_or_create(
                                            name=skill.name
                                        )
                skill_instance.save()
                skill_instance.users.add(instance)
                skill_instance.save()
            instance.save()
            skills_formset.save_m2m()
            completed_projects.save()
        return super().form_valid(form)

class ApplicantView(LoginRequiredMixin, DetailView):
    model = User
    context_object = 'user'
    template_name = 'accounts/applicant.html'

class loginView(FormView):
    template_name = 'accounts/login.html'
    form_class = AuthenticationForm
    success_url = reverse_lazy('accounts:profile',
                               kwargs={'option': None})

    def form_valid(self, form):
        login(self.request, form.get_user())
        return super().form_valid(form)

class SignupView(CreateView):
    template_name = 'accounts/signup.html'
    success_url = reverse_lazy('accounts:login')
    form_class = forms.UserCreateForm

    def form_valid(self, form):
        user = form.save(commit=False)
        user.set_password(user.password)
        user.save()
        return super().form_valid(form)

def sign_out(request):
    logout(request)
    messages.success(request, "You've been signed out. Come back soon!")
    return HttpResponseRedirect(reverse('home'))