import sympy as sp
from importlib import reload
import subprocess

import signal
import time
import yaml

assert_call = '__VERIFIER_assert'
def set_timeout(num, callback):
  def wrap(func):
    def handle(signum, frame):
      raise RuntimeError

    def to_do(*args, **kwargs):
      try:
        signal.signal(signal.SIGALRM, handle)
        signal.alarm(num)
        r = func(*args, **kwargs)
        signal.alarm(0)
        return r
      except RuntimeError as e:
        callback()

    return to_do

  return wrap

def to_PRS(inits, recurrence):
    res = ''.join('%s = %s;\n' % (var, inits[var]) for var in inits) + '\n'
    conds = {}
    # print(recurrence[0][0])
    if recurrence[1][0] == True:
        body = ''.join('%s = %s;\n' % (var, recurrence[0][0][var]) for var in recurrence[0][0])
        res += r'if (true) {%s}' % body
    else:
        for body, cond in zip(*recurrence):
            if isinstance(cond, sp.Eq):
                if isinstance(cond.lhs, sp.Mod):
                    res += 'if (%s) {\n%s} else ' % ('%s %% %s == %s' % (cond.lhs.args[0], cond.lhs.args[1], cond.rhs), dict2c(body))
                else:
                    res += 'if (%s) {\n%s} else ' % ('%s == %s' % (cond.lhs, cond.rhs), dict2c(body))
            else:
                res += 'if (%s) {\n%s} else ' % (cond, dict2c(body))
        res += '{\n%s}' % dict2c(recurrence[0][-1])
    return res

def dict2c(d):
    return ''.join('%s = %s;\n' % (k, d[k]) for k in d)

def z3query(query):
    with open('z3q.py', 'w') as fp:
        fp.write(query)
        fp.write('def query():\n')
        fp.write('    global s\n')
        fp.write('    solver = Solver()\n')
        fp.write('    res = solver.check(s)\n')
        fp.write('    s = set()\n')
        fp.write('    return res\n')
        fp.write('print(query())')
    out = subprocess.check_output(['python', 'z3q.py'])
    return out.decode().strip()
    # import z3q as q
    # q = reload(q)
    # return q.query()

def closed_form2z3(closed_form, order, var, index):
    conds, closed = closed_form[0], closed_form[1]
    closed_form_index = order.index(var)
    res = 'If(%s, %s, %s)'
    for i in range(len(conds)-1):
        cond_str = 'And(%s <= %s, %s < %s)' % (conds[i], index, index, conds[i+1])
        if i < len(conds) - 2:
            res = res % (cond_str, closed[i][closed_form_index], 'If(%s, %s, %s)')
        else:
            res = res % (cond_str, closed[i][closed_form_index], '%s')
    if len(conds) == 1:
        res = closed[0][closed_form_index]
    else:
        res = res % closed[-1][closed_form_index]
    return res
        # res = res % ('And(%s <= %s, %s <  %s)' % (conds[i]))

def analyze_loop(loop_guard, loop_body):
    assignments = [[{}, {}], [sp.true]]
    it = 0
    assertion = None
    for statement in loop_body:
        it += 1
        if is_assignment(statement):
            lhs = get_assignment_lhs(statement)
            rhs = get_assignment_rhs(statement)
            for branch in assignments[0]:
                updated = rhs.subs(branch, simultaneous=True)
                branch[lhs] = updated
        elif is_selection(statement):
            if assignments[1][0]:
                values = assignments[0][0]
                bodies_conds = selection_bodies_conds(statement)
                new_bodies = []
                new_conds = []
                for seq_body in bodies_conds[0]:
                    body = sequence2parallel(seq_body)
                    for var in body:
                        body[var] = body[var].subs(values)
                    for var in values:
                        if var not in body:
                            body[var] = values[var]
                    new_bodies.append(body)
                for i, cond in enumerate(bodies_conds[1]):
                    new_conds.append(cond.subs(values))
                assignments = new_bodies, new_conds
            else:
                raise Exception('Only one sequence of if-else is supported now')
        elif is_assertion(statement) and it == len(loop_body):
            _, assertion = function_name_args(statement)
            assertion = assertion[0]
        # elif is_assertion(statement):
        #     print(statement)
    involved_variables = set()
    for body in assignments[0]:
        for var in body:
            involved_variables.add(var)
    for body in assignments[0]:
        for var in involved_variables:
            if var not in body:
                body[var] = var

    return ('iteration', assignments, sp.true if loop_guard == 1 else loop_guard, assertion)

def read_answer(filename):
    with open(filename) as fp:
        yml = yaml.load(fp, Loader=yaml.FullLoader)
    properties = yml['properties']
    for prop in properties:
        if 'unreach-call' in prop['property_file'] or 'no-overflow' in prop['property_file']:
            return prop['expected_verdict']
    else:
        print('property not found')

def get_assertion_in_loop(statement):
    return statement[1]

def is_assertion(statement):
    name, *args = function_name_args(statement)
    return name.name == assert_call

def is_return(statement):
    try:
        return statement[0] == 'return'
    except:
        return False

def is_assignment(statement):
    return statement[0] == 'assignment'

def is_assignment_list(statement):
    return statement[0][0] == 'assignment'

def is_iteration(statement):
    return statement[0] == 'iteration'

def is_for_iteration(statement):
    return statement[0] == 'for-iteration'

def is_selection(statement):
    return statement[0] == 'selection'

def is_function_call(statement):
    return statement[0] == 'call'

def function_name_args(statement):
    if len(statement) > 2:
        return statement[1], statement[2:]
    else:
        return (statement[1],)

def relation2str(relation):
    if isinstance(relation, sp.Eq) and isinstance(relation.lhs, sp.Mod):
        return '%s == %s' % (relation.lhs, relation.rhs)
    if isinstance(relation, sp.Eq):
        return '%s == %s' % (relation2str(relation.lhs), relation2str(relation.rhs))
    elif isinstance(relation, sp.Ne):
        return '%s != %s' % (relation2str(relation.lhs), relation2str(relation.rhs))
    # elif isinstance(relation, sp.Or):
    #     return 'Or(%s, %s)' % (relation2str(relation.args[0]), relation2str(relation.args[1]))
    # elif isinstance(relation, sp.And):
    #     return 'And(%s, %s)' % (relation2str(relation.args[0]), relation2str(relation.args[1]))
    elif isinstance(relation, sp.Mod):
        return '%s > 0' % relation
    else:
        return str(relation)

def assignment2dict(assignment):
    return {assignment[1]: assignment[2]}

def get_assignment_lhs(assignment):
    return assignment[1]

def get_assignment_rhs(assignment):
    return assignment[2]

def get_selection_cond(selection):
    return selection[3]

def create_assignment(lhs, rhs):
    return ('assignment', lhs, rhs)

def selection_bodies_conds(selection):
    if selection[2][0] != 'selection':
        return [selection[1], selection[2]], [selection[3]]
    else_branch = selection_bodies_conds(selection[2])
    return [selection[1]] + else_branch[0], [selection[3]] + else_branch[1]

def sequence2parallel(sequence):
    values = {}
    for i, statement in enumerate(sequence):
        lhs = get_assignment_lhs(statement)
        rhs = get_assignment_rhs(statement)
        rhs = rhs.subs(values, simultaneous=True)
        values[lhs] = rhs
    return values


"""
POSTS to the API

Author: quangdungluong
Date: February 20, 2023
"""
import json

import requests

data = {
    'age': 33,
    'workclass': 'Local-gov',
    'fnlgt': 198183,
    'education': 'Bachelors',
    'education_num': 13,
    'marital_status': 'Never-married',
    'occupation': 'Prof-specialty',
    'relationship': 'Not-in-family',
    'race': 'White',
    'sex': 'Female',
    'capital_gain': 0,
    'capital_loss': 0,
    'hours_per_week': 50,
    'native_country': 'United-States'
}

request = requests.post("https://udacity-project-3.onrender.com", data=json.dumps(data))
print(f"Status code:", request.status_code)
print(f"Inference result:", request.json())


# Create your views here.

from django.views.generic import TemplateView, FormView, DetailView, ListView
from django.shortcuts import render,render_to_response
from .models import Post
from django.core.urlresolvers import reverse_lazy
from django.template import RequestContext
from .forms import ConfessForm
# from uuslug import uuslug
# Import PILLOW to render and image of the text
# from PIL import Image,ImageDraw
from endless_pagination.decorators import page_template

class PostListView(ListView):
	model = Post
	template_name="post_list.html"
	paginate_by = 15

@page_template('post_page.html')  # just add this decorator
def postList(
               request, template='confess.html', extra_context=None):

    context = {}
    if extra_context is not None:
        context.update(extra_context)

    obj = Post.objects.all()
    context['objects']=obj
    return render_to_response(
        template, context, context_instance=RequestContext(request))

@page_template('post_page.html')  # just add this decorator
def popularList(
               request, template='confess.html', extra_context=None):

    context = {}
    if extra_context is not None:
        context.update(extra_context)

    obj = Post.objects.filter(category='PO')
    context['objects']=obj
    return render_to_response(
        template, context, context_instance=RequestContext(request))
@page_template('post_page.html')  # just add this decorator
def newList(
               request, template='confess.html', extra_context=None):

    context = {}
    if extra_context is not None:
        context.update(extra_context)

    obj = Post.objects.filter(category='NE')
    context['objects']=obj
    return render_to_response(
        template, context, context_instance=RequestContext(request))

class ConfessForm(FormView):
	template_name="post_form.html"
	form_class=ConfessForm
	success_url=reverse_lazy('list')
	def form_valid(self,form):
		category ='NE'
		 # every post will be set to the new category,
		# the popular will be handpicked by admin
		post=Post()
		post.title=form.cleaned_data['title']
		post.text=form.cleaned_data['text']
		post.total_likes = 1
		post.category = category
		# This piece of code can turn any text to image
		# transform text to an image
		# img= Image.new('RGB',(500,500),(255,255,255))
		# d=ImageDraw.Draw(img)
		# d.text((20,20),post.text,fill=(255,0,0))
		# # save image to the uploads directory for media
		# img.save(settings.MEDIA_ROOT+"/uploads/"+post.text+".png","PNG")
		# # # save it to the model
		# post.image="/uploads/"+post.text+".png"
		post.save()

		return super(ConfessForm,self).form_valid(form)

class PostDetailView(DetailView):
 	template_name = 'post_detail.html'
 	model= Post


import keras
from tensorflow import keras
from keras.models import Sequential
from keras_NN import *
from Plot_Quantities import *
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import train_test_split
from atlasify import atlasify
import pickle

#From Adversarial code
# Import(s)
import os
import h5py

# Set Keras backend
os.environ['KERAS_BACKEND'] = 'tensorflow'

# Keras import(s)
import keras.backend as K
from keras.models import Model
from keras.layers import Input, Dense, Concatenate
from keras.optimizers import Adam

# Local import(s) -- after Keras backend is set
import ops
import plots
import layers

#Total data set provided (signal/backgorund event numberss)
samples = [447834, 2111964]

#Retrieve transformed data
with open("C:/Users/jamie/OneDrive/Documents/Python Scripts/SHproject2/transformed_data.data", 'rb') as f:
    # read the data as binary data stream
    a = pickle.load(f)
df = pd.DataFrame(a[:1000000])#895668

#Reproducability
np.random.seed(5321)

#Standardise data
for i in range(df.shape[1]-2):
        df[i] = (df[i] - df[i].mean()) / (df[i].std())

#Shuffle then separate into training, validation, and test sets
x_train, x_test, y_train, y_test, m_train, m_test = train_test_split(df.iloc[:, :-2].values, df.iloc[:, -1].values,
                                                                     df.iloc[:, -2].values, test_size=0.2, shuffle = True)

#Data metrics
nb_samples = x_train.shape[0]
nb_features = x_train.shape[1]

# Adversary factory method
def adversary_model (nb_gmm):

    # Input(s)
    i = Input(shape=(1,))
    m = Input(shape=(1,))

    # Hidden layer(s)
    x = Dense(4,  activation='relu')(i)
    x = Dense(8,  activation='relu')(x)
    x = Dense(16, activation='relu')(x)
    x = Dense(32, activation='relu')(x)

    # Gaussian mixture model (GMM) components
    coeffs = Dense(nb_gmm, activation='softmax') (x)  # GMM coefficients sum to one
    means  = Dense(nb_gmm, activation='sigmoid') (x)  # Means are on [0, 1]
    widths = Dense(nb_gmm, activation='softplus')(x)  # Widths are positive

    # Posterior p.d.f.
    pdf = layers.PosteriorLayer(nb_gmm)([coeffs, means, widths, m])

    # Build model
    return Model(inputs=[i,m], outputs=pdf, name='adversary')

# Combined factory method
def combined_model (clf, adv, lambda_reg, lr_ratio):

    # Classifier
    input_clf  = Input(shape=clf.layers[0].input_shape[1:])
    input_m    = Input(shape=(1,))
    output_clf = clf(input_clf)

    # Connect with gradient reversal layer
    gradient_reversal = layers.GradientReversalLayer(lambda_reg * lr_ratio)(output_clf)

    # Adversary
    output_adv = adv([gradient_reversal, input_m])

    # Build model
    return Model(inputs=[input_clf, input_m], outputs=[output_clf, output_adv], name='combined')

def KL (y_true, y_pred):
    """
    Kullback-Leibler loss; maximises posterior p.d.f.
    """
    return -K.log(y_pred)

#Compile and fit standard netral network
clf = define_model(nb_features)
hist_clf = clf.fit(x_train, y_train, epochs=5, batch_size=1000, validation_split=0.15)

#Make class predictions with the model
y_pred = clf.predict(x_test).flatten()

sig_smp, bkg_smp = len(y_test[y_test == 1]), len(y_test[y_test == 0])
s_fac_trim = [samples[0]/sig_smp, samples[1]/bkg_smp]
print('Cut Signal: ', sig_smp*8.1330010*(10**-5)*s_fac_trim[0])
print('Cut Background: ', bkg_smp*1.4058503*(10**-4)*s_fac_trim[1])

#Print various metrics and graphs based on standard neural network
#report_NN(clf, hist_clf, y_pred, x_test, y_test, m_test, s_fac_trim)

lam = 100                     # Regularisation parameter, lambda
nb_gmm = 6                   # Number of GMM components
loss_weights = [1.0E-08, 1.]   # Relative learning rates for classifier and adversary, resp.
lr_ratio = loss_weights[0] / loss_weights[1]
optimiser = 'adam'             # Using the Adam optimiser

# Prepare sample weights (i.e. only do mass-decorrelation for signal)
sample_weight = [np.ones(nb_samples, dtype=float), (y_train == 0).astype(float)]
sample_weight[1] *= np.sum(sample_weight[0]) / np.sum(sample_weight[1])

# Rescale mass to [0,1]
mt_train  = m_train - m_train.min()
mt_train /= mt_train.max()

# Get classifier predictions
z_train = clf.predict(x_train).flatten()
z_train_rounded = np.rint(z_train)

# Construct adversary model and fit
adv = adversary_model(nb_gmm)
adv.compile(optimiser, loss=KL)
hist_adv = adv.fit([z_train, mt_train], np.ones_like(mt_train), sample_weight=sample_weight[1], batch_size = 60, epochs=7, validation_split=0.15)

#Demonstrate learned mass ability
plots.posterior(adv, m_train[y_train == 0], z_train[y_train == 0], title='Before adversarial training')
plt.show()

# Construct combined model and fit
cmb = combined_model(clf, adv, lam, lr_ratio)
cmb.compile(optimiser, loss=['binary_crossentropy', KL], loss_weights=loss_weights)
hist_cmb = cmb.fit([x_train, mt_train], [y_train, np.ones_like(mt_train)], sample_weight=sample_weight, batch_size=500, epochs=60, validation_split=0.15)

#Predict using new classifier on train/test sets
z_train    = clf.predict(x_train).flatten()
y_pred_ANN = clf.predict(x_test).flatten()
y_pred_ANN_rounded = np.rint(y_pred_ANN)
plots.posterior(adv, m_train[y_train == 0], z_train[y_train == 0], title='After adversarial training')
plt.show()

#Mass Correlation Graph
plots.profile(m_test[y_test==0], [y_pred[y_test==0], y_pred_ANN[y_test==0]], labels=['NN classifier', 'ANN classifier'])
plt.show()

#Predictions
distribution(y_test, y_pred_ANN, xlabel="Classifier Output")
plt.show()

#Evaluate the naive model
loss, accuracy, precision, recall= clf.evaluate(x_test, y_test)
print('Accuracy: %.2f' % (accuracy*100))
print('Loss: %.2f' % (loss))
print('Precision: %.2f' % (precision))
print('Recall: %.2f' % (recall))
print('F1: ', metrics.f1_score(y_test, y_pred_ANN_rounded))

sig_smp, bkg_smp = len(y_test[y_test == 1]), len(y_test[y_test == 0])
s_fac_trim = [samples[0]/sig_smp, samples[1]/bkg_smp]
print('Cut Signal: ', sig_smp*8.1330010*(10**-5)*s_fac_trim[0])
print('Cut Background: ', bkg_smp*1.4058503*(10**-4)*s_fac_trim[1])

#Scaling constant
SBSF = 24.838677879576565

#Sensitivity for different cuts with ANN
ss = []
t = np.linspace(0.1,0.9,20,endpoint=True)
for i in t:
    true_pos, false_pos, true_neg, false_neg = calc_Truths(y_pred_ANN, y_test, m_test, threshold = i)
    eff_sig = len(true_pos)/(len(true_pos)+len(false_neg))
    eff_bkg = len(false_pos)/(len(false_pos)+len(true_neg))
    sensitivity = Sensitivity(true_pos, false_pos, s_fac_trim, SBSF)
    ss.append(sensitivity)
    print('Sensitivity on cut {}: '.format(i), sensitivity, ' with efsig: ', eff_sig, ' with rj: ', eff_bkg)
print('Best at: ', t[ss.index(max(ss))])

#Plot sensitivities
plt.plot(t, ss, label = 'Sensitivity on Cut')
plt.xlabel('Prediction Cut')
plt.ylabel('Sensitivity')
plt.legend()
plt.show()

#Print graphs for the best cut
true_pos, false_pos, true_neg, false_neg = calc_Truths(y_pred_ANN, y_test, m_test, threshold =t[ss.index(max(ss))])
eff_sig = len(true_pos)/(len(true_pos)+len(false_neg))
eff_bkg = len(false_pos)/(len(false_pos)+len(true_neg))
sensitivity = Sensitivity(true_pos, false_pos, s_fac_trim, SBSF)
print('Best effsig: ', eff_sig)
print('Best rj: ', eff_bkg)
print('Best Sensitivity: ', sensitivity)

# Mass distribution sculpting
plots.sculpting(m_test, y_test, [y_pred, y_pred_ANN], ['NN classifier', 'ANN classifier'], effsig = eff_sig)
plt.show()

plot_Sculpting(true_pos, false_pos, true_neg, false_neg)
plt.show()
fit_Exp(false_pos)

#Plot metrics
plot_ROC(y_test, y_pred_ANN)
plot_PR(y_test, y_pred_ANN)
plot_Seq_Loss(hist_cmb)


from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .models import Expense, Category
from django.contrib.auth.models import User
from django.contrib import messages
from django.core.paginator import Paginator
import json, csv, xlwt
from django.http import JsonResponse, HttpResponse
from userprefernces.models import UserPrefernce
from userprefernces.views import account_details
import datetime
from django.template.loader import render_to_string
from .forms import ExpenseUploadForm
from django.views.decorators.csrf import csrf_exempt
# import tempfile
# from weasyprint import HTML
# from django.db.models import Sum

# Create your views here.
def search_expenses(request):
    if request.method == "POST":
        search_str = json.loads(request.body).get('searchText')
        expenses = Expense.objects.filter(amount__istartswith=search_str, owner=request.user) | Expense.objects.filter(date__istartswith=search_str, owner=request.user) | Expense.objects.filter(description__icontains=search_str, owner=request.user) | Expense.objects.filter(category__icontains=search_str, owner=request.user)

        data = expenses.values()
        return JsonResponse(list(data), safe=False)

@login_required(login_url='/authentication/login')
def index(request):
    categories = Category.objects.all()
    expenses = Expense.objects.filter(owner=request.user)
    paginator = Paginator(expenses,5)
    page_number = request.GET.get('page')
    page_obj = Paginator.get_page(paginator, page_number)
    # Check if UserPreference exists for the current user
    try:
        user_preference = UserPrefernce.objects.get(user=request.user)
        currency = user_preference.currency
    except UserPrefernce.DoesNotExist:
        # Handle the case where UserPreference doesn't exist (new user)
        currency = None

    context = {
        'expenses': expenses,
        'page_obj': page_obj,
        'currency': currency,
    }
    return render(request, 'expenses/index.html', context)

@login_required(login_url='/authentication/login')
def add_expense(request):
    categories = Category.objects.all()
    context = {
        'categories': categories,
        'values': request.POST
    }
    # Check if the user has set their preferences
    try:
        user_preference = UserPrefernce.objects.get(user=request.user)
    except UserPrefernce.DoesNotExist:
        messages.error(request, "Please set your preferences before adding an expense.")
        return redirect('prefernces')
    if request.method == 'GET':
        return render(request, 'expenses/add_expense.html', context)

    if request.method == 'POST':
        amount = request.POST['amount']
        description = request.POST['description']
        date = request.POST['expense_date']
        category = request.POST['category']
        if not amount:
            messages.error(request, 'Amount required.')
            return render(request, 'expenses/add_expense.html', context)

        if not description:
            messages.error(request, 'Description required.')
            return render(request, 'expenses/add_expense.html', context)

        Expense.objects.create(owner = request.user, amount=amount, description=description, date=date, category=category)
        messages.success(request, "Expense saved successfully")
        return redirect('expenses')

@login_required(login_url='/authentication/login')
def edit_expense(request, id):
    expense = Expense.objects.get(pk=id)
    categories = Category.objects.all()
    context = {
        'expense': expense,
        'values': expense,
        'categories': categories
    }
    if request.method == 'GET':
        return render(request, 'expenses/edit_expense.html', context)
    if request.method == 'POST':
        amount = request.POST['amount']
        if not amount:
            messages.error(request, 'Amount required.')
            return render(request, 'expenses/edit_expense.html', context)

        description = request.POST['description']
        date = request.POST['expense_date']
        category = request.POST['category']

        if not description:
            messages.error(request, 'description is required.')
            return render(request, 'expenses/edit_expense.html', context)

        expense.owner = request.user
        expense.amount=amount
        expense.description=description
        expense.date=date
        expense.category=category
        expense.save()
        messages.success(request, "Expense updated successfully")
        return redirect('expenses')

@login_required(login_url='/authentication/login')
def delete_expense(request, id):
    expense = Expense.objects.get(pk=id)
    expense.delete()
    messages.success(request, "Expense deleted.")
    return redirect('expenses')

# def add_category(request):
#     print('view connected')
#     if request.method == 'POST':
#         category = request.POST["category"]
#     Category.objects.create(name = category)
#     messages.success(request, "Category added successfully")
#     return redirect(request, 'account-details')
@login_required(login_url='/authentication/login')
def expense_category_summary(request, time_interval):
    # Convert the time_interval to the number of days (e.g., '1m' for 1 month)
    if time_interval == '1m':
        num_days = 30
    elif time_interval == '3m':
        num_days = 90
    elif time_interval == '6m':
        num_days = 180
    elif time_interval == '1y':
        num_days = 365
    else:
        # Handle an invalid time_interval here
        num_days = 0
    # print(num_days,"--time interval")
    todays_date = datetime.date.today()
    start_date = todays_date - datetime.timedelta(days=num_days)
    # print(datetime.timedelta(days=num_days))
    # print(todays_date, start_date, "-----time period")
    # # Filter expenses based on the start_date
    expenses = Expense.objects.filter(
        owner=request.user,
        date__range=[start_date, todays_date]
    )
    finalrep = {}
    def get_category(expense):
        return expense.category
    category_list = list(set(map(get_category, expenses)))
    def get_expense_category_amount(category):
        amount = 0
        filtered_by_category = expenses.filter(category=category)
        for item in filtered_by_category:
            amount += item.amount
        return amount
    for x in expenses:
        for y in category_list:
            finalrep[y] = get_expense_category_amount(y)

    return JsonResponse({'expense_category_data': finalrep}, safe=False)
@login_required(login_url='/authentication/login')
def stats_view(request):
    return render (request, 'expenses/stats.html')
@login_required(login_url='/authentication/login')
def export_csv(request):
    response = HttpResponse(content_type='text/csv')
    username = request.user.username
    response['Content-Disposition']='attachment; filename=Expenses-For-'+ username +'-'+ datetime.datetime.now().strftime("%Y-%m-%d")+'.csv'
    writer = csv.writer(response)
    writer.writerow(['Amount', 'Description', 'Category', 'Date'])

    expenses = Expense.objects.filter(owner=request.user)
    for expense in expenses:
        writer.writerow([expense.amount, expense.description, expense.category, expense.date])

    return response
@login_required(login_url='/authentication/login')
def export_excel(request):
    response = HttpResponse(content_type='application/ms-excel')
    username = request.user.username
    response['Content-Disposition']='attachment; filename=Expenses-For-'+ username +'-'+ datetime.datetime.now().strftime("%Y-%m-%d")+'.xls'

    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('Expenses')
    row_num = 0
    font_style = xlwt.XFStyle()
    font_style.font.bold = True

    columns = ['Amount', 'Description', 'Category', 'Date']

    for col_num in range(len(columns)):
        ws.write(row_num, col_num, columns[col_num], font_style)

    font_style = xlwt.XFStyle()

    rows = Expense.objects.filter(owner=request.user).values_list(
        'amount', 'description', 'category', 'date')

    for row in rows:
        row_num += 1

        for col_num in range(len(row)):
            ws.write(row_num, col_num, str(row[col_num]), font_style)

    wb.save(response)
    return response

def upload_expense_csv(request):
    if request.method == 'POST':
        form = ExpenseUploadForm(request.POST, request.FILES)
        if form.is_valid():
            csv_file = form.cleaned_data['csv_file']

            if csv_file:
                # Process the uploaded CSV file
                csv_data = csv.reader(csv_file.read().decode('utf-8').splitlines())

                for row in csv_data:
                    # Assuming your CSV structure is: amount, date, description, category
                    amount, date, description, category = row
                    # Create an Expense record
                    Expense.objects.create(
                        owner=request.user,
                        amount=amount,
                        date=date,
                        description=description,
                        category=category
                    )

                return redirect('expenses')

    else:
        form = ExpenseUploadForm()

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

# def export_pdf(request):
#     response = HttpResponse(content_type='application/pdf')
#     username = request.user.username
#     response['Content-Disposition']='attachment; filename=Expenses-For-'+ username +'-'+ datetime.datetime.now().strftime("%Y-%m-%d")+'.pdf'
#     response['Content-Transfer-Encoding']= 'binary'

#     html_string = render_to_string('expenses/pdf-output.html', {'expenses':[],'total':0})
#     html = HTML(string=html_string)

#     result = html.write_pdf()

#     with tempfile.NamedTemporaryFile(delete=True) as output:
#         output.write(result)
#         output.flush()

#         output = open(output.name, 'rb')
#         response.write(output.read())

#     return response


import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

def main():
    # image's path
    img_rgb = cv.imread('mario.png')

    # converting image to grayscale
    img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)

    # path to template image and turned it to grayscale too
    template = cv.imread('mario_coin.png',0)

    # defining weight and height to enclose the template (in rectangle)
    w, h = template.shape[::-1]

    # indicating the detection method with matchTemplate(image,template,the method)
    res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)

    # defining the threshold value
    threshold = 0.8

    # finding the location of the template in the image
    loc = np.where( res >= threshold)

    for pt in zip(*loc[::-1]):
        # drawing a rectangle around the template
        cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 1)

    # saving the result image
    cv.imwrite('res.png',img_rgb)

if __name__ == "__main__":
    main()


import torch
import os
import torchvision # torch package for vision related things
import torch.nn.functional as F  # Parameterless functions, like (some) activation functions
import torchvision.datasets as datasets  # Standard datasets
import torchvision.transforms as transforms  # Transformations we can perform on our dataset for augmentation
from torch import optim  # For optimizers like SGD, Adam, etc.
from torch import nn  # All neural network modules
from torch.utils.data import DataLoader  # Gives easier dataset managment by creating mini batches etc.
from tqdm import tqdm  # For nice progress bar!

# Simple CNN
class CNN(nn.Module):
    def __init__(self, in_channels=1, num_classes=10):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(
            in_channels=in_channels,
            out_channels=8,
            kernel_size=(3, 3),
            stride=(1, 1),
            padding=(1, 1),
        )
        self.pool = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
        self.conv2 = nn.Conv2d(
            in_channels=8,
            out_channels=16,
            kernel_size=(3, 3),
            stride=(1, 1),
            padding=(1, 1),
        )
        self.fc1 = nn.Linear(16 * 7 * 7, num_classes)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = self.pool(x)
        x = F.relu(self.conv2(x))
        x = self.pool(x)
        x = x.reshape(x.shape[0], -1)
        x = self.fc1(x)
        return x

# Set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Hyperparameters
in_channels = 1
num_classes = 10
learning_rate = 0.001
batch_size = 64
num_epochs = 3
load_model = True
checkpoint_name = os.path.join("./checkpoints", "cnn_checkpoint.pth.tar" )
# checkpoint_name = "cnn_checkpoint.pth.tar"

# Save Checkpoint
def save_checkpoint(state, filename=checkpoint_name):
    print("=> Saving Checkpoint")
    torch.save(state, filename)

# Load Checkpoint
def load_checkpoint(checkpoint):
    print("=> Loading checkpoint")
    model.load_state_dict(checkpoint["model"])
    optimizer.load_state_dict(checkpoint["optimizer"])

# Load Data train
train_dataset = datasets.MNIST(root="dataset/", train=True, transform=transforms.ToTensor(), download=True)
test_dataset = datasets.MNIST(root="dataset/", train=False, transform=transforms.ToTensor(), download=True)
# Load Data test
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=True)

# Initialize network
model = CNN(in_channels=in_channels, num_classes=num_classes).to(device)

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)

if load_model:
    load_checkpoint(torch.load(checkpoint_name))

# Train Network
for epoch in range(num_epochs):
    print(f"Training epoch: {epoch}")
    losses = []

    if epoch %2 == 0:
        checkpoint = {"model" : model.state_dict(), "optimizer": optimizer.state_dict()}
        save_checkpoint(checkpoint)
    for batch_idx, (data, targets) in enumerate(tqdm(train_loader)):
        # Get data to cuda if possible
        data = data.to(device=device)
        targets = targets.to(device=device)

        # forward
        scores = model(data)
        loss = criterion(scores, targets)
        losses.append(loss.item())

        # backward
        optimizer.zero_grad()
        loss.backward()

        # gradient descent or adam step
        optimizer.step()
    # for debugging purpose
    print(f"Loss at epoch {epoch}: {loss.item()}")

# Check accuracy on training & test to see how good our model
def check_accuracy(loader, model):
    if loader.dataset.train:
        print("Checking accuracy on train data")
    else:
        print("Checking accuracy on test data")

    num_correct = 0
    num_samples = 0
    model.eval()

    with torch.no_grad():
        for x, y in loader:
            x = x.to(device=device)
            y = y.to(device=device)

            scores = model(x)
            _, predictions = scores.max(1)
            num_correct += (predictions == y).sum()
            num_samples += predictions.size(0)

    model.train()
    return num_correct/num_samples

# Printing the accuracy
print(f"Accuracy on training set: {check_accuracy(train_loader, model)*100:.2f}")
print(f"Accuracy on test set: {check_accuracy(test_loader, model)*100:.2f}")


#! /usr/bin/dev python
# -*- coding utf-8 -*-
# @Time: 2018-06-20 20:54
# @Author: Wang Ya wen
# @Site:
# @File: reader.py

from flask import Flask, render_template, session

app = Flask(__name__)
app.secret_key = '234567'

@app.route('/')
def home():
    user_id = session.get('user_id')
    if user_id is None:
        return render_template('login.html')
    return render_template('index.html')

if __name__ == '__main__':
    app.run()


#!/usr/bin/env python

import os

from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from whoosh.index import create_in
from whoosh.fields import ID, TEXT, Schema

class Command(BaseCommand):
    help = 'Crear índice whoosh'

    def handle(self, *args, **kwargs):
        self.stdout.write('Creadon índice', ending=' ')
        schema = Schema(
            id_documento=ID(stored=True),
            name=TEXT(stored=True),
            labels=TEXT(stored=True),
            content=TEXT()
            )
        if not os.path.isdir(settings.INDEX_DIR):
            os.makedirs(settings.INDEX_DIR)
            self.stdout.write('D', ending='')
        index = create_in(settings.INDEX_DIR, schema)
        self.stdout.write('I', ending='')
        self.stdout.write(' [ok]')


"""
	* @ date: 15/05/2022
	* @ author: Almanza Martínez Francisco Alejadro
	* @ subject: Criptography
	* @ teacher: Sandra Diaz Santiago
	* @ Description: Practice 5. AES encryption. Usage python aes.py <operation_mode>
	*	0: "CBC"
	*	1: "CTR"
	*	2: "OFB"
	*	3: "CFB"
	*
"""
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad
import base64, os, sys

if __name__ == '__main__':
	if not len(sys.argv) > 1:
		print("[E] Operation mode is missing: 0: CBC, 1: CTR, 2: OFB, 3: CFB")
		exit()
	op_mode = int(sys.argv[1])
	key = get_random_bytes(16)

	f = open(f"./keys/AES_key{sys.argv[1]}.txt", "wb")
	b64_key = base64.b64encode(key)
	f.write(b64_key)
	f.close()
	random_bytes_file = open('/dev/random', 'rb')
	padding = random_bytes_file.read(1000000)
	random_bytes_file.close()

	if op_mode == 0:
		cipher = AES.new(key, AES.MODE_CBC)
	if op_mode == 1:
		cipher = AES.new(key, AES.MODE_CTR)
		nonce = cipher.nonce
	if op_mode == 2:
		cipher = AES.new(key, AES.MODE_OFB)
	if op_mode == 3:
		cipher = AES.new(key, AES.MODE_CFB)

	f = open(f"./messages/AES_message{sys.argv[1]}.txt", "wb")
	plaintext = b"The name of the ship was the Billy of tea... "

	ciphertext = cipher.encrypt(pad(plaintext, 256))

	b64 = base64.b64encode(ciphertext)
	padding = base64.b64encode(padding)
	f.write(b64+padding)
	f.close()

	print(plaintext)


# coding: utf-8
# Le neuronal network actuel que j'utilise est un 2 inputs (Input_A & Input_B ) donc les 2 entrées.
# Ensuite les 2 entrées sont redirigés par les 3 synapses qui sont reliées aux 3 "hidden Layer"
# Qui ensuite sont redirigés par les 3 synapses --> output Layer
# --
# Pour le tableau, j'ai décidé de prendre pour exemple un tableau avec 10 valeurs & 3 colonnes
# Pour le context il y aura un système de voiture & camion. [ 0 pour la voiture & 1 pour le camion ]
# Ainsi que leur dimensions donc longeur du vehicule et largeur

# Importations des modules
import numpy as np
import matplotlib.pyplot as plt
import mplcyberpunk
import time as t
import os

# Input_A = Longueur & Largeur (valeur dans le tableau)
# Input_B = 0 -> voiture | 1 -> camion

# Définir les entrées avec les valeurs du tableau
input_dimension = np.array(([3.5,1.5], [9.3,2.3], [5.5,2.7], [7.3,2.5], [2.3,1.7], [6.9,2.9], [2.7,1.6], [3.9,1.8], [8.3,3], [6.4,2.2]), dtype=float)
# Dans l'input B il n'y a que 9 valeurs car il nous faut trouver la 10ème valeur (celle manquante)
input_type = np.array(([0], [1], [1], [1], [0], [1], [0], [0], [1]), dtype=float)

# On divise par le plus grand chiffre pour nous permettre d'obtenir des valeurs entre 0 & 1
# Axis est égal a l'axe dont on veut calculer nos valeurs
input_a_divided = input_dimension/np.amax(input_dimension, axis=0)

# On split les valeurs donc on recupère seulement les valeurs avec un résulat donc les 9 première
split_value = np.split(input_a_divided,[9])[0]
split_prediction = np.split(input_a_divided,[9])[1]

# Création d'une classe pour le reseaux neuronal
class neuronal_network:
    def __init__(self):
        # Les valeurs ci-dessous sont celles retrouvé sur l'image qui illustre ce code.
        self.inputNeuronal = 2
        self.hiddenNeuronal = 3
        self.outputNeuronal = 1

        self.weight1 = np.random.randn(self.inputNeuronal, self.hiddenNeuronal)
        self.weight2 = np.random.randn(self.hiddenNeuronal, self.outputNeuronal)

    def feedforward(self, split_value):
        # La variable self.multi multiplie la valeur d'entrer donc qui est maintenant split_value, et on la multiplie par self.weight1
        # Et donc ensuite avec self.sigmoid_cal on effectue la fonction sigmoid dessus (j'ai du mal avec)
        self.multi_1 = np.dot(split_value, self.weight1)
        self.sigmoid_cal_1 = self.sigmoid_equation(self.multi_1)
        self.multi_2 = np.dot(self.sigmoid_cal_1, self.weight2)
        output_sigmoid = self.sigmoid_equation(self.multi_2)
        return output_sigmoid

    # On créer une fonction pour écrire le calcule de la fonction sigmoid afin de l'appliquer au dessus
    def sigmoid_equation(self, S):
        return 1/(1+np.exp(-S))

# Output de notre calcul matriciel
output_result = neuronal_network().feedforward(split_value)

# J'ai crée ici un graphique pour mieux se visualiser le tableau & comparer
plt.style.use("cyberpunk")
plt.plot(split_value, marker='o', label='Bleu = Largeur & Rose = Longueur')
plt.legend(loc='lower left')
mplcyberpunk.add_glow_effects()

def question_choice():
    while True:
        print("""
1. Afficher les données de l'IA
2. Afficher le graphique
3. Afficher les deux

        """)
        input_question = input(str("Veuillez choisir: "))
        if input_question == '1':
            print("La sortie prédite par l'IA: ", str(output_result))
            print("La sortie réel: ", str(input_type))
            break
        elif input_question == '2':
            print(plt.show())
            break
        elif input_question == '3':
            print(output_result, plt.show())
        else:
            os.system("clear")
            print('[ERREUR] Veuillez choisir un élément ci-dessus !')
            t.sleep(2)
            os.system("clear")

if __name__ == "__main__":
    question_choice()

# A ce stade la l'AI fait juste crée un tableau avec des valeurs, leur soumettre la fonction d'activation qui est la fonction sigmoid, ainsi que input_a_divided qui va garder toute nos valeurs
# entre 0 & 1 donc actuellement on a des valeurs qui sont sortie grâce au calcul matriciel dans la class neuronal_network mais le soucis est que les valeurs des weights donc des poids/synaps
# sont actuellement générer aléatoirement ce qui fait qu'il faut utilisé la "rétropropagation" afin d'obtenir des résultats proche de la réalité, malheureusement étant donné que j'apprend
# en auto didacte sur ce sujet là il est compliqué de comprendre plein chose tel que certaine fonction mathématique ou autre.


from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
# Initialize Qt resources from file resources.py
import resources
import utils
# Import the code for the dialog
from plotstatisticsdialog import PlotStatisticsDialog
import numpy as np
import matplotlib.pylab as plt

class PlotStatistics:

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface

    def initGui(self):

        self.canvas = self.iface.mapCanvas()
        self.mainWindow = self.iface.mainWindow()

        # Create action that will start plugin configuration
        self.action = QAction(QIcon(":/plugins/plotstatistics/icon.png"), \
            "plot statistics", self.mainWindow)
        # connect the action to the run method
        QObject.connect(self.action, SIGNAL("triggered()"), self.run)

        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.action)
        self.iface.addPluginToMenu("&plot statistics", self.action)

    def unload(self):
        # Remove the plugin menu item and icon
        self.iface.removePluginMenu("&plot statistics",self.action)
        self.iface.removeToolBarIcon(self.action)

    # run method that performs all the real work
    def run(self):
        """
        This initialise process called when QGIS started
        """
        # create and show the dialog
        self.dlg = PlotStatisticsDialog()
        # show the dialog
        self.dlg.show()
        QObject.connect(self.dlg, SIGNAL("okClicked(QString, QString)"), self.readData)

    def readData(self, layerName, outFile):
        """
        selected layer and output file name are passed in from dialog box, from the layer
        its attribute values are extracted to plot diagrams.
        Note this function currently extract data from Vector layer only.
        """
        dateTimeList = []
        magList = []
        depList = []

        vlayer = utils.getVectorLayerByName( layerName )

        vprovider = vlayer.dataProvider()
        feat = QgsFeature()
        allAttrs = vprovider.attributeIndexes()
        vprovider.select( allAttrs )

        # field description
        myFields = vprovider.fields()

        while vprovider.nextFeature(feat):
            atmap = feat.attributeMap()
            for key, value in atmap.iteritems():
                if key == 6:
                    dateTime = "%-22s" % atmap[key].toString()
                    dateTimeList.append(dateTime)
                elif key == 3:
                    #mag = "%-22s" % atmap[key].toString()
                    mag = "%-22s" % atmap[key].toString()
                    magList.append(mag)
                elif key == 4:
                    dep = "%-22s" % atmap[key].toString()
                    depList.append(dep)
                else:
                    pass

        self.plotTimes(dateTimeList,magList, depList, outFile)

    def plotTimes(self, dateTimeList,magList, depList, outFile):
        """
        This function plots following diagrams calculating Bin/histogram values
        - date vs Frequency
        - time vs Frequency
        - magnitude vs Frequency
        - depth vs Frequency
        """
        outputfile = str(outFile) + "_date_freq.png"
        timeoutputfile = str(outFile) + "_time_freq.png"
        magoutputfile = str(outFile) + "_mag_freq.png"
        depoutputfile = str(outFile) + "_dep_freq.png"

        yearList = []
        timeList = []
        magOutList = []
        depOutList = []

        for mag in magList:
            mag_en = float(mag)
            magOutList.append(mag_en)

        for dep in depList:
            dep_en = float(dep)
            depOutList.append(dep_en)

        for dateTime in dateTimeList:
            year = dateTime[:4]
            time = dateTime[11:13]

            year_e = int(year)
            time_e = int(time)

            yearList.append(year_e)
            timeList.append(time_e)

        # Frequency vs Year plot
        startYear = min(yearList)
        endYear = max(yearList)

        step = 1 #bin width

        bins = np.arange(startYear, endYear, step)
        hist, edges = np.histogram(yearList, bins)

        plt.figure()
        plt.bar(bins[:-1]+0.5,hist, width=1, color='k')
        #plt.bar(bins[:-1]+0.02,hist, width=0.8, color='k')
        plt.axis([startYear, endYear, 0, 100])
        plt.ylabel('Number of Eruptions per Year')
        plt.xlabel('Year')
        plt.savefig(outputfile)

        # Frequency vs Time plot
        startTime = min(timeList)
        endTime = max(timeList)
        step = 1 #bin width

        bins = np.arange(startTime, endTime, step)
        hist, edges = np.histogram(timeList, bins)

        plt.figure()
        plt.bar(bins[:-1]+0.5,hist, width=1, color='k')
        plt.axis([startTime, endTime, 0, 100])
        plt.ylabel('Number of Eruptions per Time')
        plt.xlabel('Time')
        plt.savefig(timeoutputfile)

        # Frequency vs Mag plot
        startMag = min(magOutList)
        endMag = max(magOutList)

        step = 1 #bin width

        bins = np.arange(startMag, endMag, step)
        hist, edges = np.histogram(magOutList, bins)

        plt.figure()
        plt.bar(bins[:-1]+0.5,hist, width=0.8, color='k')
        plt.axis([startMag, endMag, 0, 50])
        plt.ylabel('Number of Eruptions ')
        plt.xlabel('Mag')
        plt.savefig(magoutputfile)

        # Frequency vs Dep plot
        startDep = min(depOutList)
        endDep = max(depOutList)
        step = 1 #bin width

        bins = np.arange(startDep, endDep, step)
        hist, edges = np.histogram(depOutList, bins)

        plt.figure()
        plt.bar(bins[:-1]+0.5,hist, width=0.8, color='k')
        #plt.bar(bins[:-1]+0.02,hist, width=0.8, color='k')
        plt.axis([startDep, endDep, 0, 50])
        plt.ylabel('Number of Eruptions')
        plt.xlabel('Depth')
        plt.savefig(depoutputfile)


"""Add milestone to issues

Revision ID: 36386a60b3fd
Revises: 350efb3f6baf
Create Date: 2016-09-14 11:03:45.673932

"""

# revision identifiers, used by Alembic.
revision = '36386a60b3fd'
down_revision = '350efb3f6baf'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ''' Add the column milestone to the table issues.
    '''
    op.add_column(
        'issues',
        sa.Column('milestone', sa.String(255), nullable=True)
    )

def downgrade():
    ''' Add the column milestone to the table issues.
    '''
    op.drop_column('issues', 'milestone')


"""
code by NduX
"""

import requests
import time
import datetime
from wolfpak import influxdb_hauler, convert_to_influx_time
from influxdb import InfluxDBClient

def influx_run_query(query, db):

    ip_address = "172.24.7.10"
    client = InfluxDBClient(ip_address, 8086,"","",db)
    client.query(query)

def pulse_api_call(ip, api_call, auth1):
    '''
    http://10.200.170.132/api/2.0/config/protocolprocessorserver

    '''
    status_code = 'none'
    url = r'http://' + ip + api_call
    r = requests.get(url, auth=(auth1[0], auth1[1]))
    status_code = r.status_code

    pp_dict = r.json()

    while "next" in r.json()['meta']:
        next_api_call = r.json()['meta']['next']
        url = r'http://' + ip + next_api_call
        r = requests.get(url, auth=(auth1[0], auth1[1]))
        pp_dict['data']= pp_dict['data'] + r.json()['data']

        if status_code == 200: status_code = r.status_code #dont see where he stores new status code

    if status_code != 200:
        print(f"One of the API calls failed: {status_code}")

    print(f"Size of API call Response: {r.text.__sizeof__()} Bytes")

    return pp_dict
#### end of function pulse_api_call using requests#############################

def get_obj_names(obj):
    '''
    This function will return a dict of objectnames
    Key = obj_id and the value is the obj_name
   '''
    #return_dict = {}

    ip_address = "10.200.170.132"
    api_call = "/api/2.0/config/" + obj
    auth = ('ndugod', 'Intelsat#2')

    result = pulse_api_call(ip_address, api_call, auth)

    return result
#### End of Function get_obj_names ############################################

def cluster_name_formatter(cluster_name):
    cluster_name_splitted_list=cluster_name.split('-')
    cluster_name_abb=""
    for i in cluster_name_splitted_list:

        i=i.upper()
        if (i.startswith('PCL')) or (i.startswith('FLX')):
            continue
        else:
            cluster_name_abb+=i
    cluster_name_abb='PP-'+cluster_name_abb

    return cluster_name_abb
#### End of Function get_obj_names ############################################

if __name__=="__main__":

    epoch_time = datetime.datetime.fromtimestamp(time.time())
    influx_time= convert_to_influx_time(epoch_time.strftime('%H:%M:%S'))
    influx_list=[]

    pp_server_result = get_obj_names('protocolprocessorserver')
    pp_cluster_result=get_obj_names('protocolprocessorcluster')
    site_result = get_obj_names('site')
    satellite_result= get_obj_names('satellite')
    pp_objid_server_dict = {}
    for server_record in pp_server_result['data']:
        server_obj_id=server_record.get('obj_parentid')
        id_check=pp_objid_server_dict.get(server_obj_id, None)
        if id_check!=None:
            pp_objid_server_dict[server_obj_id].append({server_record['obj_name']:server_record["obj_attributes"]['adminaddress']})
        else:
            pp_objid_server_dict[server_obj_id]=[{server_record['obj_name']:server_record["obj_attributes"]['adminaddress']}]
    # print(pp_objid_server_dict)

    pp_objid_cluster_dict = {}
    for cluster_record in pp_cluster_result['data']:
        cluster_parent_id=cluster_record.get('obj_parentid')
        id_check=pp_objid_cluster_dict.get(cluster_parent_id, None)
        if id_check!=None:
            pp_objid_cluster_dict[cluster_parent_id].append({cluster_record["obj_id"]:cluster_record['obj_name']})
        else:
            pp_objid_cluster_dict[cluster_parent_id]=[{cluster_record["obj_id"]:cluster_record['obj_name']}]
    # print(pp_objid_cluster_dict)

    cluster_id_to_cluster_name_dict={}
    for cluster_record in pp_cluster_result['data']:
        cluster_id=cluster_record.get('obj_id')
        cluster_name=cluster_record.get('obj_name')
        cluster_id_to_cluster_name_dict[cluster_id]=cluster_name_formatter(cluster_name)
    # print(cluster_id_to_cluster_name_dict)

    site_dict = {}
    for site_record in site_result['data']:
        site_obj_id=site_record.get('obj_id')
        site_dict[site_obj_id]={site_record["obj_name"]:site_record["obj_attributes"]["satellite_id"]}
    # print(site_dict)

    sat_dict = {}
    for sat_record in satellite_result['data']:
        sat_obj_id=sat_record.get('obj_id')
        sat_dict[sat_obj_id]={sat_record["obj_attributes"]["domainname"]:sat_record["obj_attributes"]["longitude"]}
    #print(sat_dict)

    for cluster_parent_id in pp_objid_cluster_dict:
        site=site_dict.get(cluster_parent_id)
        for site_name_sat_id in site.items():
            sat_info=sat_dict.get(site_name_sat_id[1])
            pp_objid_cluster_dict[cluster_parent_id].append(sat_info)
    #print(pp_objid_cluster_dict)

    for server_parent_id in pp_objid_server_dict:
        for cluster_parent_id in pp_objid_cluster_dict:
            cluster_info_list=pp_objid_cluster_dict.get(cluster_parent_id)
            if cluster_info_list[0].get(server_parent_id)!=None:
                cluster_sat_info=cluster_info_list[1]
                pp_objid_server_dict[server_parent_id].append(cluster_sat_info)
            else:
                continue
    #print(pp_objid_server_dict)

    for server_parent_id in pp_objid_server_dict:
    #     influx_dicts['tags']['server_parent_id']=server_parent_id
        servers_list=pp_objid_server_dict.get(server_parent_id)
        sat_info_dict = servers_list[-1]
        if sat_info_dict is None:
            continue
    #     print(sat_info_dict)
        sat_name=None
        sat_long=None
        for sat_element in sat_info_dict:
            sat_name=sat_element
            sat_long=sat_info_dict[sat_element]
        # epoch_time=epoch_time
        for count in range(len(servers_list)-1):
            single_server_dict=servers_list[count]
            influx_dicts={'measurement':'clusters',
                                          'tags':{"server_name":'',"server_parent_id":'',
                                                   "cluster":'', "sat":''},
                                          'time':influx_time,  'fields':{'server_address':'', 'sat_long':''}}
            for server_element in single_server_dict:
                influx_dicts['tags']['cluster']=cluster_id_to_cluster_name_dict.get(server_parent_id)
                influx_dicts['tags']['server_parent_id']=server_parent_id
                influx_dicts['tags']["server_name"]=server_element
                influx_dicts['tags']["sat"]=sat_name
                influx_dicts['fields']["server_address"]=single_server_dict[server_element]
                influx_dicts['fields']["sat_long"]=float(sat_long)
            influx_list.append(influx_dicts)
    print(influx_list)
    print(len(influx_list))
    db='clusters'
    query="delete from clusters"
    deleting_old_rrecords=influx_run_query(query, db) #deleting all old cluster records before loading new ones
    influxdb_hauler(influx_list, db, host='all clusters')


#!/usr/local/bin/python3.3

import json, codecs, sys, csv
from pprint import pprint
import geoip2.database
import geoip2.webservice

DB_FNAME = '/g/darkwebpaper/logs/GeoIP2-Domain.mmdb'
db = geoip2.database.Reader( DB_FNAME )

user_id = 42
license_key = None
client = geoip2.webservice.Client( user_id, license_key )

ips = None
with open('logs/owned_ips.log') as f:
	ips = [ x.strip() for x in f.readlines() ]

#pprint(ips)

for ip in ips:
	r1 = db.domain( ip )
	print( "domain=%s" % (r1.domain) )
#r2 = client.city('128.101.101.101')

#print( r1 )

#print( r2.location.latitude )
#print( r2.location.longitude )

db.close()


from booking.models import *
from users import db_communication as users_db
from trips import db_communication as trips_db
from booking.exceptions import *
import asyncio
from loguru import logger

def book(token, id_, seats) -> int:
    trip = trips_db.get_target_trip(id=id_)
    if not trips_db.is_exists(id_):
        raise NotExistException
    if seats:
        for seat in seats:
            if seat in get_taken_seats(trips_db.get_target_trip(id=id_)):
                raise SeatIsTakenException
        booking = Booking(
            owner=users_db.get_user(token=token),
            trip=trip
        )
        booking.set_seat(seats)
    else:
        booking = Booking(
            owner=users_db.get_user(token=token),
            trip=trip
        )
    if seats:
        trips_db.push_notify(trip.owner.fcm_token, 'Поездка', f'У вас новая бронь. Поездка'
                                                              f' {trip.departure_set.first().name}-'
                                                              f'{trip.stops_set.last().name}')
    else:
        trips_db.push_notify(trip.owner.fcm_token, 'Поездка', f'Бронирование передачи посылки. Поездка'
                                                              f' {trip.departure_set.first().name}-'
                                                              f'{trip.stops_set.last().name}')
    asyncio.run(trips_db.notify(trip.owner.id, 'new booking'))
    booking.save()
    return booking.id

def cancel_booking(token: str, id_: int):
    user = users_db.get_user(token=token)
    bookings = Booking.objects.filter(owner=user, trip=trips_db.get_target_trip(id=id_))
    if not bookings:
        raise NotExistException
    for booking in bookings:
        booking.delete()
    trips_db.push_notify(user.fcm_token, 'cancel booking', 'cancel booking')
    asyncio.run(trips_db.notify(booking.trip.owner.id, 'cancel booking'))
    return True

def cancel_booking_for_driver(token: str, trip_id: int, user_id):
    driver = users_db.get_user(token=token)
    user = users_db.get_user(id=user_id)
    trip = trips_db.get_target_trip(id=trip_id)
    if trip.owner != driver:
        raise Exception('Khui tebe')
    bookings = Booking.objects.filter(owner_id=user_id, trip=trip)
    for booking in bookings:
        booking.delete()
    trips_db.push_notify(user.fcm_token, 'cancel booking', 'cancel booking')
    asyncio.run(trips_db.notify(user.id, 'cancel booking'))
    return True

def get_passengers(id_: int):
    return list(set([
        i.owner for i in Booking.objects.filter(trip_id=id_).all()
    ]))

def get_passengers_and_bookings(id_: int):
    return [
        (i.owner, i.id) for i in Booking.objects.filter(trip_id=id_).all()
    ]

def get_user_seat(user, trip):
    list_ = []
    for i in get_passengers_and_bookings(trip.id):
        if i[0] == user:
            list_.extend(get_booking(id=i[1]).get_seat())
    if list_:
        return list_
    else:
        raise NotInTripException

def get_taken_seats(trip):
    taken = []
    for booking in Booking.objects.filter(trip=trip):
        taken.extend(booking.get_seat())
    return taken

def get_booking(**kwargs) -> Booking:
    return Booking.objects.filter(
        **kwargs
    ).first()

def get_all_booking(owner):
    return Booking.objects.filter(
        owner=owner
    ).all()


import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

@pytest.fixture(scope="session")
def browser():
    #driver = webdriver.Chrome("../chromedriver")
    options = Options()
    options.headless = True
    driver = webdriver.Chrome(chrome_options=options)

    yield driver
    driver.quit()


__all__ = ["HDFMapMSIMagneticField"]

import h5py
import numpy as np

from warnings import warn

from bapsflib._hdf.maps.msi.templates import HDFMapMSITemplate
from bapsflib.utils.exceptions import HDFMappingError

class HDFMapMSIMagneticField(HDFMapMSITemplate):
    """
    Mapping class for the 'Magnetic field' MSI diagnostic.

    Simple group structure looks like:

    .. code-block:: none

        +-- Magnetic field
        |   +-- Magnet power supply currents
        |   +-- Magnetic field profile
        |   +-- Magnetic field summary
    """

    def __init__(self, group: h5py.Group):
        """
        :param group: the HDF5 MSI diagnostic group
        :type group: :class:`h5py.Group`
        """
        # initialize
        HDFMapMSITemplate.__init__(self, group)

        # populate self.configs
        self._build_configs()

    def _build_configs(self):
        """Builds the :attr:`configs` dictionary."""
        # look for required datasets
        for dset_name in [
            "Magnet power supply currents",
            "Magnetic field profile",
            "Magnetic field summary",
        ]:
            if dset_name not in self.group:
                why = f"dataset '{dset_name}' not found"
                raise HDFMappingError(self.info["group path"], why=why)

        # initialize general info values
        pairs = [("calib tag", "Calibration tag"), ("z", "Profile z locations")]
        for pair in pairs:
            try:
                val = self.group.attrs[pair[1]]
                if isinstance(val, (list, tuple, np.ndarray)):
                    self._configs[pair[0]] = val
                else:
                    self._configs[pair[0]] = [val]
            except KeyError:
                self._configs[pair[0]] = []
                warn(
                    f"Attribute '{pair[1]}' not found for MSI diagnostic "
                    f"'{self.device_name}', continuing with mapping"
                )

        # initialize 'shape'
        # - this is used by HDFReadMSI
        self._configs["shape"] = ()

        # initialize 'shotnum'
        self._configs["shotnum"] = {
            "dset paths": (),
            "dset field": ("Shot number",),
            "shape": (),
            "dtype": np.int32,
        }

        # initialize 'signals'
        # - there are two signal fields
        #   1. 'magnet ps current'
        #   2. 'magnetic field'
        #
        self._configs["signals"] = {
            "magnet ps current": {
                "dset paths": (),
                "dset field": (),
                "shape": (),
                "dtype": np.float32,
            },
            "magnetic field": {
                "dset paths": (),
                "dset field": (),
                "shape": (),
                "dtype": np.float32,
            },
        }

        # initialize 'meta'
        self._configs["meta"] = {
            "shape": (),
            "timestamp": {
                "dset paths": (),
                "dset field": ("Timestamp",),
                "shape": (),
                "dtype": np.float64,
            },
            "data valid": {
                "dset paths": (),
                "dset field": ("Data valid",),
                "shape": (),
                "dtype": np.int8,
            },
            "peak magnetic field": {
                "dset paths": (),
                "dset field": ("Peak magnetic field",),
                "shape": (),
                "dtype": np.float32,
            },
        }

        # ---- update configs related to 'Magnetic field summary'   ----
        # - dependent configs are:
        #   1. 'shotnum'
        #   2. all of 'meta'
        #
        dset_name = "Magnetic field summary"
        dset = self.group[dset_name]

        # define 'shape'
        expected_fields = [
            "Shot number",
            "Timestamp",
            "Data valid",
            "Peak magnetic field",
        ]
        if dset.ndim == 1 and all(field in dset.dtype.names for field in expected_fields):
            self._configs["shape"] = dset.shape
        else:
            why = "'/Magnetic field summary' does not match expected shape"
            raise HDFMappingError(self.info["group path"], why=why)

        # update 'shotnum'
        self._configs["shotnum"]["dset paths"] = (dset.name,)
        self._configs["shotnum"]["shape"] = dset.dtype["Shot number"].shape

        # update 'meta/timestamp'
        self._configs["meta"]["timestamp"]["dset paths"] = (dset.name,)
        self._configs["meta"]["timestamp"]["shape"] = dset.dtype["Timestamp"].shape

        # update 'meta/data valid'
        self._configs["meta"]["data valid"]["dset paths"] = (dset.name,)
        self._configs["meta"]["data valid"]["shape"] = dset.dtype["Data valid"].shape

        # update 'meta/peak magnetic field'
        self._configs["meta"]["peak magnetic field"]["dset paths"] = (dset.name,)
        self._configs["meta"]["peak magnetic field"]["shape"] = dset.dtype[
            "Peak magnetic field"
        ].shape

        # update configs related to 'Magnet power supply currents'  ----
        # - dependent configs are:
        #   1. 'signals/magnet ps current'
        #
        dset_name = "Magnet power supply currents"
        dset = self.group[dset_name]
        self._configs["signals"]["magnet ps current"]["dset paths"] = (dset.name,)

        # check 'shape'
        _build_success = True
        if dset.dtype.names is not None:
            # dataset has fields (it should not have fields)
            _build_success = False
        elif dset.ndim == 2:
            if dset.shape[0] == self._configs["shape"][0]:
                self._configs["signals"]["magnet ps current"]["shape"] = (dset.shape[1],)
            else:
                _build_success = False
        else:
            _build_success = False
        if not _build_success:
            why = "'/Magnet power supply currents' does not match expected shape"
            raise HDFMappingError(self.info["group path"], why=why)

        # update configs related to 'Magnetic field profile'        ----
        # - dependent configs are:
        #   1. 'signals/magnetic field'
        #
        dset_name = "Magnetic field profile"
        dset = self.group[dset_name]
        self._configs["signals"]["magnetic field"]["dset paths"] = (dset.name,)

        # check 'shape'
        _build_success = True
        if dset.dtype.names is not None:
            # dataset has fields (it should not have fields)
            _build_success = False
        elif dset.ndim == 2:
            if dset.shape[0] == self._configs["shape"][0]:
                self._configs["signals"]["magnetic field"]["shape"] = (dset.shape[1],)
            else:
                _build_success = False
        else:
            _build_success = False
        if not _build_success:
            why = "'/Magnetic field profile' does not match expected shape"
            raise HDFMappingError(self.info["group path"], why=why)


#! /usr/bin/python3

"""
A command-line script to take the existing images in a given directory, and
store copies in the following configurations:
horz flip
rotated 90 degrees clockwise * 3
Every valid non-duplicated combination of the above transforms

see fastai for other good transforms.
"""

import os
import PIL
import sys
from PIL import Image
from PIL import ImageDraw

#def filepaths
r90 = "r90"
r180 = "r180"
r270 = "r270"

# ARG FORMAT IS DIR, THEN NOTHING
imgdir = sys.argv[1]

def dupeAndFlip(image, imgdir, filename):
  image.save(f"{imgdir}/normal/{filename}")
  flip = image.transpose(PIL.Image.FLIP_LEFT_RIGHT)
  flip.save(f"{imgdir}/hflip/{filename}")

for file in os.listdir(imgdir):
  try:
    filesplit = os.path.split(file)
    print(file)

    img = Image.open(file)
    r90 = img.transpose(PIL.Image.ROTATE_90)
    r180 = img.transpose(PIL.Image.ROTATE_180)
    r270 = img.transpose(PIL.Image.ROTATE_270)
    dupeAndFlip(img, f"{imgdir}/../orig", file)
    dupeAndFlip(r90, f"{imgdir}/../r90", file)
    dupeAndFlip(r180, f"{imgdir}/../r180", file)
    dupeAndFlip(r270, f"{imgdir}/../r270", file)
  except Exception:
    print("that ain't no image")
    print()


import pypyodbc

class DataBase:
        def __init__(self):

            self.conn = pypyodbc.connect(
            'Driver={SQL Server};'
            'Server=DESKTOP-9NMO9KA;'
            'Database=ogrenciKayit;'
            'Trusted_Connection=True;')

            self.cursor = self.conn.cursor()

        def add_Student(self, studentNo, name, lastName, bornDate, clas, gender):
            try:
                self.cursor.execute("SELECT COUNT(*) FROM Student WHERE StudentNo=?", (studentNo,))
                row = self.cursor.fetchone() #mevcut konumda bir kayıt varsa, bu kaydı bir tuple olarak döndürür ve cursor konumunu bir sonraki kayda taşır.
                if row[0] > 0:
                    print("-------")
                else:
                    self.cursor.execute("INSERT INTO Student (StudentNo, Name, Surname, BornTime, Class, Gender) VALUES (?, ?, ?, ?, ?, ?)",
                    (studentNo, name, lastName, bornDate, clas, gender))
                    self.conn.commit()
                    print("Öğrenci başarıyla eklendi!")

            except pypyodbc.Error as e:
                print("hata : ", e)
            finally:
                self.showStudent()
                self.conn.close()

        def showStudent(self):
            self.cursor.execute('SELECT * FROM Student')
            students = self.cursor.fetchall()
            for student in students:
                print(student)

        def control(self, no):
            self.cursor.execute("SELECT COUNT(*) FROM Student WHERE StudentNo=?", (no,))
            var = self.cursor.fetchone()
            if var[0] > 0 :
                return True


################################################################################
# fetches data from cepea database
#
# Obs:
# for clarification in regards to the olefile module check
# this stackoverflow thread: https://stackoverflow.com/questions/
# 58336366/compdocerror-when-importing-xls-file-format-to-python-using-pandas-read-excel
################################################################################

# import form the system
from concurrent.futures import ThreadPoolExecutor as executor
from typing import List
import logging, logging.config

# import from packages
import requests
import olefile
import pandas as pd

#app imports
from DB.transactions import add_series

# logging
logging.config.fileConfig('./logging/logging.conf')
logger = logging.getLogger('dbLoaders')

# series to be included
info = [("milho", 77),
        ("acucar", 53),
        ("etanol-diario-paulinia", 111),
        ("boi-gordo", 2)]

tickers = [f"cepea.{n}" for _, n in info]

def build_url(name:str, number:int) -> str:
    """
    builds the relevant url for the ticker in case
    """
    return f'https://www.cepea.esalq.usp.br/br/indicador/series/{name}.aspx?id={number}'

def process(resp:requests.models.Response) -> pd.DataFrame:
    """
    fetchs the series related to the url used as input. Return a
    dataframe
    """
    if resp.ok:
        ole = olefile.OleFileIO(resp.content)
        return  pd.read_excel(ole.openstream("Workbook"))
    logger.error(f"Unable to fetch data from {resp.url}")

def ingest_series(info: List[tuple]) -> None:
    """
    adds series information into the database
    """
    with requests.session() as session:
        with executor() as e:
            urls = []
            for p in info:
                urls.append(build_url(*p))
            dfs = list(e.map(lambda u: process(session.get(u)), urls, timeout=60))

    for n,tck in enumerate(tickers):
        add_series(tck, dfs[n].columns[0], "CEPEA", "AGRI", "SERIES-TEMPORAIS")

if __name__ == '__main__':
    ingest_series(info)


"""use text_to_4lang to process tsv-formatted dependencies
(instead of parsing ourselves)"""
import json
import logging
import os
import sys

from fourlang.text_to_4lang import TextTo4lang
from fourlang.utils import get_cfg

def main():
    logging.basicConfig(
        level="INFO",
        format="%(asctime)s : " +
        "%(module)s (%(lineno)s) - %(levelname)s - %(message)s")
    cfg_file = sys.argv[1]
    cfg = get_cfg(cfg_file)
    text_to_4lang = TextTo4lang(cfg)
    fn = cfg.get('text', 'input_sens')
    base_fn = os.path.basename(fn)
    deps_fn = os.path.join(text_to_4lang.deps_dir, "{0}.deps".format(base_fn))

    deps = [[]]
    for line in open(fn):
        dep_str = line.strip()
        if not dep_str:
            deps.append([])
        else:
            deps[-1].append(dep_str)

    with open(deps_fn, 'w') as out_f:
        out_f.write("{0}\n".format(json.dumps({
            "deps": deps,
            "corefs": []})))

    text_to_4lang.process_deps(deps_fn)

if __name__ == "__main__":
    main()


from typing import Any, Dict, Tuple

import torch
from lightning import LightningModule
from torchmetrics import MinMetric, MeanMetric
from src.data.components.ocr_vocab import Vocab
from src.models.components.labelsmoothingloss import LabelSmoothingLoss
from src.models.vietocr_utils.download_ckpt import download_weights
# from src.models.components.cer import CER

class OCRLitModule(LightningModule):
    def __init__(
        self,
        net: torch.nn.Module,
        pretrain: str,
        optimizer: torch.optim.Optimizer,
        scheduler: torch.optim.lr_scheduler,
        compile: bool,
        vocab: Vocab
    ) -> None:
        super().__init__()

        # this line allows to access init params with 'self.hparams' attribute
        # also ensures init params will be stored in ckpt
        self.save_hyperparameters(logger=False)

        self.vocab = vocab
        self.net = net(vocab_size=len(vocab))
        if pretrain:
            print("USING PRETRAINED MODEL")
            weight_file = download_weights(pretrain)
            state_dict = torch.load(weight_file, map_location=torch.device(self.device))
            for name, param in self.net.named_parameters():
                if name not in state_dict:
                    print('{} not found'.format(name))
                elif state_dict[name].shape != param.shape:
                    print('{} missmatching shape, required {} but found {}'.format(name, param.shape, state_dict[name].shape))
                    del state_dict[name]
            try:
                self.net.load_state_dict(state_dict, strict=False)
            except:
                print("Do not use pretrained model")

        # loss function
        self.criterion = LabelSmoothingLoss(
            classes=len(vocab),
            padding_idx=vocab.pad,
            smoothing=0.1
        )

        # for averaging loss across batches
        self.train_loss = MeanMetric()
        self.val_loss = MeanMetric()
        self.test_loss = MeanMetric()

        # # for averaging CER across batches
        # self.cer = CER()
        # self.train_cer = MeanMetric()
        # self.val_cer = MeanMetric()
        # self.test_cer = MeanMetric()

        # for tracking best so far validation accuracy
        self.val_loss_best = MinMetric()
        # self.val_cer_best = MinMetric()

    def forward(self, x: torch.Tensor, tgt_input, tgt_padding_mask) -> torch.Tensor:
        """Perform a forward pass through the model `self.net`.

        :param x: A tensor of images.
        :return: A tensor of logits.
        """
        return self.net(x, tgt_input, tgt_padding_mask)

    def on_train_start(self) -> None:
        """Lightning hook that is called when training begins."""
        # by default lightning executes validation step sanity checks before training starts,
        # so it's worth to make sure validation metrics don't store results from these checks
        self.val_loss.reset()
        # self.val_cer.reset()

    def model_step(
        self, batch: Tuple[torch.Tensor, torch.Tensor]
    ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        """Perform a single model step on a batch of data.

        :param batch: A batch of data (a tuple) containing the input tensor of images and target labels.

        :return: A tuple containing (in order):
            - A tensor of losses.
            - A tensor of predictions.
            - A tensor of target labels.
        """
        img, tgt_input, tgt_output, tgt_padding_mask = batch['img'], batch['tgt_input'], batch['tgt_output'], batch['tgt_padding_mask']
        output = self.forward(img, tgt_input= tgt_input, tgt_padding_mask=tgt_padding_mask)
        output = output.flatten(0,1)
        tgt_output = tgt_output.flatten(0,1)

        loss = self.criterion(output, tgt_output)

        return loss, output,tgt_output

    def training_step(
        self, batch: Tuple[torch.Tensor, torch.Tensor], batch_idx: int
    ) -> torch.Tensor:
        """Perform a single training step on a batch of data from the training set.

        :param batch: A batch of data (a tuple) containing the input tensor of images and target
            labels.
        :param batch_idx: The index of the current batch.
        :return: A tensor of losses between model predictions and targets.
        """
        loss, preds, targets = self.model_step(batch)

        # update and log metrics
        self.train_loss(loss)
        self.log("train/loss", self.train_loss, on_step=False, on_epoch=True, prog_bar=True)

        # # cer_score = self.cer(preds, targets)
        # # self.train_cer(cer_score)
        # # self.log("train/cer", self.train_cer, on_step=False, on_epoch=True, prog_bar=True)

        # return loss or backpropagation will fail
        return loss

    def on_train_epoch_end(self) -> None:
        "Lightning hook that is called when a training epoch ends."
        pass

    def validation_step(self, batch: Tuple[torch.Tensor, torch.Tensor], batch_idx: int) -> None:
        """Perform a single validation step on a batch of data from the validation set.

        :param batch: A batch of data (a tuple) containing the input tensor of images and target
            labels.
        :param batch_idx: The index of the current batch.
        """
        loss, preds, targets = self.model_step(batch)

        # update and log metrics
        self.val_loss(loss)
        self.log("val/loss", self.val_loss, on_step=False, on_epoch=True, prog_bar=True)

        # # cer_score = self.cer(preds, targets)
        # # self.val_cer(cer_score)
        # # self.log("val/cer", self.val_cer, on_step=False, on_epoch=True, prog_bar=True)

    def on_validation_epoch_end(self) -> None:
        "Lightning hook that is called when a validation epoch ends."
        loss = self.val_loss.compute()  # get current val acc
        self.val_loss_best(loss)  # update best so far val acc
        # log `val_acc_best` as a value through `.compute()` method, instead of as a metric object
        # otherwise metric would be reset by lightning after each epoch
        self.log("val/loss_best", self.val_loss_best.compute(), sync_dist=True, prog_bar=True)

        # # cer_score = self.val_cer.compute()
        # # self.val_cer_best(cer_score)
        # # self.log("val/cer_best", self.val_cer_best.compute(), sync_dist=True, prog_bar=True)

    def test_step(self, batch: Tuple[torch.Tensor, torch.Tensor], batch_idx: int) -> None:
        """Perform a single test step on a batch of data from the test set.

        :param batch: A batch of data (a tuple) containing the input tensor of images and target
            labels.
        :param batch_idx: The index of the current batch.
        """
        loss, preds, targets = self.model_step(batch)

        # update and log metrics
        self.test_loss(loss)
        self.log("test/loss", self.test_loss, on_step=False, on_epoch=True, prog_bar=True)

    def on_test_epoch_end(self) -> None:
        """Lightning hook that is called when a test epoch ends."""
        pass

    def setup(self, stage: str) -> None:
        """Lightning hook that is called at the beginning of fit (train + validate), validate,
        test, or predict.

        This is a good hook when you need to build models dynamically or adjust something about
        them. This hook is called on every process when using DDP.

        :param stage: Either `"fit"`, `"validate"`, `"test"`, or `"predict"`.
        """
        if self.hparams.compile and stage == "fit":
            self.net = torch.compile(self.net)

    def configure_optimizers(self) -> Dict[str, Any]:
        """Choose what optimizers and learning-rate schedulers to use in your optimization.
        Normally you'd need one. But in the case of GANs or similar you might have multiple.

        Examples:
            https://lightning.ai/docs/pytorch/latest/common/lightning_module.html#configure-optimizers

        :return: A dict containing the configured optimizers and learning-rate schedulers to be used for training.
        """
        optimizer = self.hparams.optimizer(params=self.parameters())
        if self.hparams.scheduler is not None:
            scheduler = self.hparams.scheduler(optimizer=optimizer)
            return {
                "optimizer": optimizer,
                "lr_scheduler": {
                    "scheduler": scheduler,
                    "monitor": "val/loss",
                    "interval": "epoch",
                    "frequency": 1,
                },
            }
        return {"optimizer": optimizer}

if __name__ == "__main__":
    print("Here")


import discord
import asyncio
import os
from colorama import Fore
from colorama import Back
from colorama import Style

token = input("Insert Token: ")
os.system("cls")
os.system(f'mode 125,18')
os.system("title DM/SERVER PURGER -  CODED BY mbn evil#9731")

print(f"""{Fore.LIGHTBLUE_EX}{Style.BRIGHT} ▄▀▀█▄▄   ▄▀▀▄ ▄▀▄      ▄▀▀▄▀▀▀▄  ▄▀▀▄ ▄▀▀▄  ▄▀▀▄▀▀▀▄  ▄▀▀▀▀▄   ▄▀▀█▄▄▄▄  ▄▀▀▄▀▀▀▄
█ ▄▀   █ █  █ ▀  █     █   █   █ █   █    █ █   █   █ █        ▐  ▄▀   ▐ █   █   █
▐ █    █ ▐  █    █     ▐  █▀▀▀▀  ▐  █    █  ▐  █▀▀█▀  █    ▀▄▄   █▄▄▄▄▄  ▐  █▀▀█▀
  █    █   █    █         █        █    █    ▄▀    █  █     █ █  █    ▌   ▄▀    █
 ▄▀▄▄▄▄▀ ▄▀   ▄▀        ▄▀          ▀▄▄▄▄▀  █     █   ▐▀▄▄▄▄▀ ▐ ▄▀▄▄▄▄   █     █
█     ▐  █    █        █                    ▐     ▐   ▐         █    ▐   ▐     ▐
▐        ▐    ▐        ▐                                        ▐
type $purgedm if u wanna purge a dm type $purgeserver if u wanna purge server
                                         code by mbn evil#9731                                                       """)

class MyClient(discord.Client):
  async def on_message(self, message):
      if(message.author!=self.user):
          return
      channels=[]
      if(message.content=="$purgeserver"):
          channels=message.channel.guild.channels
      elif(message.content=="$purgedm"):
          channels.append(message.channel)
      else:
          return
      for channel in channels:
          print(channel)
          try:
              async for mss in channel.history(limit=None):
                  if(mss.author==self.user):
                      print(f'{Fore.LIGHTWHITE_EX}[ {Fore.LIGHTBLUE_EX}DELETED {Fore.LIGHTWHITE_EX}] ' + mss.content)
                      try:
                          await mss.delete()
                      except:
                          print(f'{Fore.RED}Failed to delete message.')
          except:
              print(f'{Fore.RED}Failed to read history.')

client=MyClient(heartbeat_timeout=86400, guild_subscriptions=False)
client.run(token, bot=False)


from django.contrib import admin
from django.urls import include, path
from . import views

urlpatterns = [
    path('', views.HomeListView.as_view(), name='home'),
    path('find', views.FindListView.as_view(), name='find'),
    path('detail/<int:pk>', views.HomeDetailView.as_view(), name='detail_page'),
    path('accounts/register', views.RegisterCreateView.as_view(), name='register'),
    path('add_remove_bookmark/<int:pk>',
         views.add_remove_bookmark, name='add_remove_bookmark'),
    path('favourites', views.FavouriteListView.as_view(), name='favourites'),
]

urlpatterns += [
    path('accounts/', include('django.contrib.auth.urls')),
]


import pygame
import pickle
import os
import constants

class Checkpoint():
    def __init__(self):
        self.filename = 'checkpoint.dat'
        # defaults (these are the equivalent values at the start of a game)
        # allow a checkpoint to be loaded even if the file does not exist.
        self.data = {
            'map_number' : 0,
            'game_percent' : 0,
            'player_lives' : 10,
            'player_ammo' : 10,
            'player_keys' : 0,
            'player_TNT' : 0,
            'player_oxygen' : constants.MAX_OXYGEN,
            'player_stacked_TNT' : False,
            'player_facing_right' : True,
            'player_rect' : pygame.Rect(16,112, constants.TILE_SIZE, constants.TILE_SIZE),
            'player_score' : 0,
            'hotspot_data' : constants.HOTSPOT_DATA,
            'gate_data' : constants.GATE_DATA
        }

    # generates a new 'checkpoint.dat' with the current game data
    def save(self):
        with open(self.filename, "wb") as f:
            pickle.dump(self.data, f)

    # loads data from 'checkpoint.dat' if file exists
    def load(self):
        if os.path.exists(self.filename):
            with open(self.filename, "rb") as f:
                self.data = pickle.load(f)


import discord, json
from checks import *
from typing import Optional
from discord.ext import commands

# Management cog
class Management(commands.Cog, name="Management", command_attrs=dict(case_insensitive=True)):
    "Bot managing commands (prefix changes, etc.)."

    def __init__(self, bot):
        self.bot = bot

    # Get/set prefixes
    @commands.group(name="prefix", aliases=["pre", "p"])
    async def prefix(self, ctx):
        """Various prefix commands!
        If run without a subcommand, sends the prefixes you can use here."""

        if ctx.invoked_subcommand is None:
            prefix_results = await self.bot.get_prefixes(self.bot, ctx.message)

            await ctx.send(
                "You can use the following prefixes here: **"
                + "**, **".join(prefix_results)
                + "**."
            )

    # Get/set custom guild prefix
    @prefix.command(name="server", aliases=["s"])
    @commands.guild_only()
    async def guild_prefix(self, ctx, prefix: Optional[str]=None):
        """Sets the prefix for the server!
        (Setting the server prefix requires the "Manage Server" permission.)
        This overrides the global prefix ($) and will be permanent through any crashes/downtime.

        Setting the prefix to "none" (or any capitalization thereof) instead removes the prefix. If run without a prefix, it instead sends the current server prefix (does not require "Manage Server")."""

        if prefix is None:
            if ctx.guild.id in self.bot.data["prefixes"]["guild"].keys():
                await ctx.send(
                    "**{}** is my custom prefix for **{}**.".format(
                        self.bot.data["prefixes"]["guild"][str(ctx.guild.id)], ctx.guild.name
                    )
                )
            else:
                await ctx.send(f"**{ctx.guild.name}** does not have a custom prefix yet.")
        else:
            perms = ctx.message.channel.permissions_for(ctx.message.author)

            if perms.manage_guild:
                self._guild_prefix(ctx.guild.id, prefix)
                await ctx.send(
                    "Done! Your server's custom prefix {}.".format(
                        f"is now {prefix}" if prefix.lower() != "none" else "has been unset"
                    )
                )

            else:
                raise commands.MissingPermissions(["manage_guild"])

    # Set custom guild prefix
    def _guild_prefix(self, _id, prefix):
        if prefix.lower() == "none":
            self.bot.data["prefixes"]["guild"].pop(str(_id))

        else:
            self.bot.data["prefixes"]["guild"][str(_id)] = prefix

        bot.update_json()

    # Get/set custom user prefix
    @prefix.command(name="user", aliases=["u"])
    async def user_prefix(self, ctx, prefix: Optional[str]=None):
        """Sets your custom user prefix!
        Your custom prefix will persist across servers and will be permanent through any crashes/downtime.
        This does not override the global prefix ($) or any server prefixes. Instead, this is just a prefix you can use in addition to the global/server prefix.

        Setting the prefix to "none" (or any capitalization thereof) instead removes the prefix. If run without a prefix, it instead sends your current custom prefix."""

        if prefix is None:
            if ctx.message.author.id in self.bot.data["prefixes"]["user"].keys():
                await ctx.send(
                    "**{}** is your custom prefix.".format(
                        self.bot.data["prefixes"]["user"][str(ctx.message.author.id)]
                    )
                )

            else:
                await ctx.send("You don't have a custom prefix... yet!")

        else:
            if prefix.lower() == "none":
                self.bot.data["prefixes"]["user"].pop(str(ctx.message.author.id))

            else:
                self.bot.data["prefixes"]["user"][str(ctx.message.author.id)] = prefix

            bot.update_json()

            await ctx.send(
                "Done! Your custom prefix {}.".format(
                    f"is now {prefix}" if prefix.lower() != "none" else "has been unset"
                )
            )

    @commands.command(name="ignchans", aliases=["ignorechannels", "ignore", "ic"], hidden=True)
    @commands.has_guild_permissions(manage_channels=True)
    async def ignore_channels(self, ctx, channel: discord.TextChannel):
        pass

async def setup(bot):
    await bot.add_cog(Management(bot))


# coding=utf-8
"""
Created on 2020, April 16th
@author: orion
"""
import dash
import dash_core_components as dcc
import dash_html_components as html

from dash.dependencies import Output, Input

import mydcc

app = dash.Dash()
app.layout = html.Div([
    mydcc.Listener( id = "uuu", aim = 'graph' ),
    dcc.Graph( id = 'graph',
               figure = { 'data': [  {'x': [1, 2, 3], 'y': [4, 1, 2]}  ]  }
              ),
    html.Div( id = 'text' )
])

@app.callback(
    Output('text', 'children'),
    [Input('uuu', 'data')])
def myfun(ddd):
    print(ddd)
    # return str(ddd['y']) + ' and ' + str(ddd['y'])
    return 'ttttttt'

if __name__ == '__main__':
    app.run_server()