#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 21 21:54:38 2018

@author: kkwang
"""

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

## filter the enrichment gene
gene_12_chart=pd.read_table('common_12_chart.txt',sep='\t',header=0)
choice_chart=gene_12_chart[gene_12_chart['Fold Enrichment']>2]
choice_chart=choice_chart[choice_chart['Benjamini']<0.05]
choice_chart=choice_chart[choice_chart['Count']>20]

## get of specific pathway
gene_want=choice_chart.loc[37,'Genes']
gene_want=gene_want.replace(' ','').split(',')
gene_want_10=gene_want[:10]

## imput specific tissue_data
tissue='AMY'
tissue_data=pd.read_csv('gene_array_matrix_csv/{}_log2_fiter_matrix.tsv'.format(tissue),sep='\t',header=0)

tissue_genes=tissue_data[tissue_data['gene_symbol'].isin(gene_want)].iloc[:,:-1]
tissue_diff=tissue_genes.diff(periods=1, axis=1).abs()
tissue_diff[tissue_diff<2]=0.01
tissue_diff.fillna(0.2,inplace=True)
tissue_diff_10=tissue_diff.iloc[:10,:]
##impot age_data
col_info=pd.read_csv('gene_array_matrix_csv/columns_metadata.csv',header=0)
age=col_info[col_info['column_num'].isin(map(int,tissue_data.columns.values[:-1]))]['age']

fig=plt.figure(figsize=(24,10),dpi=300,facecolor=None)
fig.patch.set_alpha(0.5)
ax1 = fig.add_subplot(121, projection='3d')

xlabels = age
ylabels = gene_want_10
_x = np.arange(len(age))
_y = np.arange(len(gene_want_10))
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()

top=tissue_diff_10.values.flatten()
bottom = np.zeros_like(top)
width = depth = 0.5

#ax1.view_init(30, 120)
ax1.w_xaxis.set_ticks(_x + width/2.)
ax1.w_xaxis.set_ticklabels(xlabels,rotation=90)

ax1.w_yaxis.set_ticks(_y + depth/2.)
ax1.w_yaxis.set_ticklabels(ylabels,rotation=-45)
ax1.set_zlim3d(0, top.max())
ax1.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax1.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
values = np.linspace(0.2, 1., x.ravel().shape[0])
colors = plt.cm.rainbow(values)
ax1.grid(False)
ax1.set_zticks([])
ax1.bar3d(x, y, bottom, width, depth, top, shade=False,color=colors)
#ax1.set_title('Ion_channel_enrichmen_gene')
plt.savefig('AMY_neuron_cell_body_10_r.png',transparent=True)


import PyPDF2
import os
os.chdir('pdf')

pdfFile1 = open('meetingminutes1.pdf','rb')
pdfFile2 = open('meetingminutes2.pdf','rb')

reader1 = PyPDF2.PdfFileReader(pdfFile1)
reader2 = PyPDF2.PdfFileReader(pdfFile2)

writer = PyPDF2.PdfFileWriter()
for pageNum in range(reader1.numPages):
    page = reader1.getPage(pageNum)
    writer.addPage(page)

for pageNum in range(reader2.numPages):
    page = reader2.getPage(pageNum)
    writer.addPage(page)

ouputFile = open('combinedMinutes.pdf','wb')
writer.write(ouputFile)
ouputFile.close()
pdfFile1.close()
pdfFile2.close()


import torch

def perm_dim(dim, d):
    perm = list(range(d + 1, dim)) + list(range(d + 1))

    return perm

def gen_mask(x, msr):
    mask = torch.zeros_like(x)
    obs_num = round(torch.numel(x) * (1. - msr))
    obs_idx = torch.randperm(torch.numel(x))[:obs_num]
    mask = mask.reshape(-1)
    mask[obs_idx] = 1.0
    mask = mask.reshape(x.shape)
    return mask

def rse_metric(x, x_hat):
    rse = torch.norm(x - x_hat, p=2) / torch.norm(x)
    return rse

def psnr_metric(x, x_hat):
    mse = torch.norm(x - x_hat, p=2) ** 2 / torch.numel(x)
    psnr = 10 * torch.log10(torch.max(x) ** 2 / mse)
    return psnr

def get_batch_data(x, idx, batch_size):
    shuffle = torch.randperm(idx.shape[0])
    x = x[shuffle]
    idx = idx[shuffle]
    x_batch = torch.split(x, split_size_or_sections=batch_size)
    idx_batch = torch.split(idx, split_size_or_sections=batch_size)

    return x_batch, idx_batch


from ROOT import TFile,TTree,TCanvas,TH1I,TH1D,TH1F,TH2D,TH2F,TLatex,TMath,\
    TColor,TLegend,TEfficiency,TGraphAsymmErrors,gROOT,gPad,TF1,gStyle,\
    kBlack,kWhite,TH1
import ROOT
import os
import optparse
import copy
import myStyle as ms
import myNameFormat as nf
import mySumFunctions as sf
from array import array

gROOT.SetBatch( True )
gStyle.SetOptFit(1011)

## Defining Style
ms.force_style()
# font=ms.get_font()
tsize=ms.get_size()

def get_required_ffit(cuts):
    r_ffit = "0"
    if "Left" in cuts:
        r_ffit = "1"

    return r_ffit

def get_input_info(l_keys):
# Retrieve fit information from list of keys, returning a tuple with:
# list of names, list fit parameters, list correction methods
    l_kname = []
    s_npar = set()
    s_meth = set()

    for key in l_keys:
        obj_type = key.ReadObj().Class_Name()
        kname = key.GetName()
        if "TH1D" not in obj_type:
            continue

        l_kname.append(kname)
        s_npar.add(nf.get_hist_hnpar(kname))
        s_meth.add(nf.get_hist_hmeth(kname))

    return (l_kname, list(s_npar), list(s_meth))

def draw_axes(canvas, href, targ):
# ["par", "norm", "ratio"]
    npar = nf.get_hist_hnpar(canvas.GetName())

    # Define y limits
    y_limit = sf.get_parameters_limits(d_tp_bool, npar, my_xvar_init)
    ymin, ymax = y_limit

    # Create histogram with correct axes
    b1 = href.GetBinCenter(1)
    bN = href.GetBinCenter(href.GetXaxis().GetNbins())
    xtitle = href.GetXaxis().GetTitle()
    ytitle = sf.yaxis_label(npar, my_tp_nameS, targ)
    htitle = ";%s;%s"%(xtitle, ytitle)
    haxis = TH1D("haxis", htitle, 1, b1-0.05, bN+0.05)

    haxis.SetMinimum(ymin)
    haxis.SetMaximum(ymax)
    haxis.SetLabelSize(tsize-20,"xy")
    haxis.SetTitleSize(tsize-16,"xy")
    haxis.SetTitleOffset(1.0,"x")
    haxis.SetTitleOffset(1.8,"y")

    canvas.cd(0)
    # Get the list of primitives drawn in the canvas
    primitives = canvas.GetListOfPrimitives()

    # Loop over the list to retrieve the pads
    pads = []
    for primitive in primitives:
        if isinstance(primitive, ROOT.TPad):
            pads.append(primitive)

    # Define figures to add in every pad
    for pad in pads:
        pad.cd(0)
        # Draw axes
        haxis.Draw("AXIS")
        gPad.RedrawAxis("g")

        # Draw explicit x and y bin range
        px, py = sf.get_pad_coord(pad.GetName())
        axis_txt = draw_bin_aside(px, py)

        # Draw reference line at 1 for ratio
        if d_tp_bool["ratio"]:
            line = draw_line(haxis)

    return haxis

def create_d_th1(hvalues, href, l_bincodes, padx_var, pady_var, xaxis_var):
# Create dictionary with pad indices associated to histogram
# as in: {(x,y) : hist}
    d_pad = {}
    kname, targ = hvalues.GetName().split("-")
    for bincode in l_bincodes:
        # Get pad indices
        idx_x = ms.get_bincode_varidx(bincode, padx_var)
        idx_y = ms.get_bincode_varidx(bincode, pady_var)
        pcoord = (idx_x, idx_y)
        if pcoord not in d_pad:
            # Uses same name as pad
            new_name = sf.pad_name(kname, idx_x, idx_y)
            new_name+= "-%s"%(targ)
            htemp = href.Clone(new_name)
            d_pad[pcoord] = htemp
        # Retrieve values from input
        hvalues_bin = hvalues.GetXaxis().FindBin(bincode)
        value = hvalues.GetBinContent(hvalues_bin)
        error = hvalues.GetBinError(hvalues_bin)
        # Fill pad histogram
        hbin = ms.get_bincode_varbin(bincode, xaxis_var)
        this_hist = d_pad[pcoord]
        this_hist.SetBinContent(hbin, value)
        this_hist.SetBinError(hbin, error)

    return d_pad

def fill_canvas(canvas, dict_th1):
# Draw each histogram in its respective pad with proper color
    l_hist = []
    canvas.cd(0)
    for px,py in dict_th1:
        hist = dict_th1[(px,py)]
        pname, targ = hist.GetName().split("-")
        pad = gROOT.FindObject(pname)
        pad.cd(0)
        pad.SetGrid(0,1) # Is it needed?

        # Choose style
        # hist.SetLabelSize(tsize-20,"xy")
        # hist.SetTitleSize(tsize-16,"xy")
        # hist.SetTitleOffset(1.0,"x")
        # hist.SetTitleOffset(1.8,"y")
        hist.SetLineWidth(2)
        hist.SetLineColor(ms.color_target[targ])
        hist.SetMarkerStyle(4)
        hist.SetMarkerColor(ms.color_target[targ])

        # gPad.RedrawAxis("g")
        hist.Draw("hist L X0 SAME")
        # Draw error bars too
        hist.Draw("e X0 SAME")

        l_hist.append(hist)

    return l_hist

def create_legend(kname, def_position = True):
    plx, ply = 2, 0
    pad_name = sf.pad_name(kname, plx, ply)
    pad = gROOT.FindObject(pad_name)
    pad.cd(0)
    x1, x2 = sf.x_pad(0.1), sf.x_pad(0.9)
    y1, y2 = sf.y_pad(0.8), sf.y_pad(1.0)
    if not def_position:
        x1, x2 = sf.x_pad(0.1), sf.x_pad(0.9)
        y1, y2 = sf.y_pad(0.7), sf.y_pad(0.9)

    legend = TLegend(x1, y1, x2, y2)
    legend.SetBorderSize(0)
    legend.SetTextFont(ms.get_font())
    legend.SetTextSize(tsize-14)
    legend.SetFillStyle(0)
    legend.SetTextAlign(22)
    legend.SetNColumns(3)

    # Get the list of primitives in the pad
    primitives = pad.GetListOfPrimitives()
    # Filter and extract the histograms from the list
    histograms = {}
    for obj in primitives:
        if isinstance(obj, ROOT.TH1) and ("haxis" not in obj.GetName()):
            _, targ = obj.GetName().split("-")
            if targ in histograms:
                continue
            histograms[targ] = obj
    # Print the list of histograms
    for targ in l_tar_input:
        hist = histograms[targ]
        legend.AddEntry(hist, targ)

    legend.Draw()

    return legend

# Define extra stylistic functions
def draw_bin_aside(x, y):
# Check if x,y correspond to one of the boundary pads and write the bin info
    x,y = int(x), int(y)
    pady_drawx = 0
    padx_drawy = npx - 1

    if (x != padx_drawy) and (y != pady_drawx):
        return 0

    text = ROOT.TLatex()
    text.SetTextSize(tsize-14)
    text.SetTextAlign(23)

    # Draw bin info in x-axis
    if (y == pady_drawx):
        title = ms.get_bintxt("%s%s"%(vpx,x), d_bin["nBin"])
        text.DrawLatexNDC(sf.x_pad(0.50),sf.y_pad(-0.20), title)

    # Draw bin info in y-axis
    if (x == padx_drawy):
        text.SetTextAngle(90)
        title = ms.get_bintxt("%s%s"%(vpy,y), d_bin["nBin"])
        text.DrawLatexNDC(sf.x_pad(1.05),sf.y_pad(0.5), title)

    return text

def draw_line(hist):
# Draw line at 1.0
    line1 = ROOT.TLine(0.0,1.0, 1.0,1.0)
    line1.SetLineColorAlpha(ROOT.kRed, 0.5)
    line1.SetLineWidth(1)
    line1.SetLineStyle(9)

    xmin = hist.GetXaxis().GetXmin()
    xmax = hist.GetXaxis().GetXmax()
    line1.DrawLine(xmin,1.0, xmax,1.0)

    return line1

# Construct the argument parser
parser = optparse.OptionParser("usage: %prog [options]\n")
parser.add_option('-D', dest='Dataset', default = "",
                  help="Dataset in format <binType>_<Ndims>")
parser.add_option('-J', dest='isJLab', action='store_true', default = False,
                  help="Use folder from JLab_cluster")
parser.add_option('-i', dest='inputCuts', default = "",
                  help="Add input cuts Xf_Yb_Z/P...")
parser.add_option('-o', dest='outputCuts', default = "",
                  help="Add output cuts FE_...")

# parser.add_option('-y', dest='y_symmetric', action='store_true', default = False,
#                   help="Use symmetric y-limits (default False)")
# parser.add_option('-s', dest='solidTargets', action='store_true', default = False,
#                   help="Draw only solid targets (default adds D)")
# parser.add_option('-a', dest='allDSplit', action='store_true', default = False,
#                   help="Draw only deuterium for different solid targets")
parser.add_option('-t', dest='type', default = "parameters",
                  help="Choose parameter type: Pars, Norm, Ratio")
parser.add_option('-O', dest='Overwrite', action='store_true', default = False,
                  help="Overwrite if file already exists")

options, args = parser.parse_args()

dataset = options.Dataset
isJLab = options.isJLab
par_type = options.type
ovr = options.Overwrite

input_cuts = options.inputCuts
plots_cuts = options.inputCuts +"_"+ options.outputCuts

d_bin = ms.get_name_dict(dataset)
m_fit = ms.get_fit_method(input_cuts)

# Revisit this. Maybe plots_cuts might work better / be more general
initials = ms.get_plot_initials(d_bin["nBin"], input_cuts)
d_init_nbins = ms.get_bincode_nbins_dict(d_bin["nBin"], initials)

# Define type of plot
d_tp_bool, my_tp_nameS, my_tp_nameL = sf.get_parameters_type(par_type)

# Define Input
l_inputfile = []
l_tar_input = ms.summary_targ_type(plots_cuts)
# Skip ratio plot of liquid target
if (d_tp_bool["ratio"] and ("D" in l_tar_input[0])):
    ms.error_msg("Ratio", "Skip deuterium for ratio.")
first = True
for tar in l_tar_input:
    temp_dset = "%s_%s"%(tar, dataset)
    in_obj = nf.naming_format(my_tp_nameL, temp_dset, cuts=input_cuts,
                            is_JLab=isJLab)
    inputfile = TFile(in_obj.get_path(),"READ")

    if first:
        l_keys = inputfile.GetListOfKeys()
        l_kname, l_npar, l_meth = get_input_info(l_keys)
        first = False

    l_inputfile.append(inputfile)

# Create canvases
out_obj = nf.naming_format(my_tp_nameL, dataset, cuts=plots_cuts,
                           is_JLab=isJLab)
# outputfile = TFile(out_obj.get_path_summary(True, ovr),"RECREATE")
# TODO: Check how to generalize this for new binning with integrated Q, N, etc.

# Define number of pads and their coordinates
vpx = "Q"
vpy = "N" if "N" in d_init_nbins else "X"
npx = d_init_nbins[vpx]
npy = d_init_nbins[vpy]
l_canvas = sf.create_l_canvas(l_kname, npx, npy)

# Create generic/standard histogram to use in each pad
my_xvar = ms.get_xaxis(input_cuts)
my_xvar_init = ms.d_var_initial[my_xvar]
my_xaxis = ms.axis_label(my_xvar_init,"LatexUnit")
x_limits = ms.get_l_limits(d_bin["nBin"], my_xvar_init)
x_title = ";%s;"%(my_xaxis)
h_tmp = TH1D("h_tmp", x_title, len(x_limits)-1, array('d',x_limits))

# Fill canvases
l_bincodes = ms.get_bincode_list(d_bin["nBin"], input_cuts)
for cv in l_canvas:
    cv.cd(0)
    # Get input histograms
    kname = cv.GetName()
    first = True
    l_l_hist = []
    for i,file in enumerate(l_inputfile):
        hist = file.Get(kname)
        itarg = l_tar_input[i]
        # Avoid overwriting warning
        hist.SetName("%s-%s"%(kname,itarg))

        # Draw same axes in every pad
        if first:
            h_axis = draw_axes(cv, h_tmp, itarg)
            first = False

        # Get histograms
        d_padth1 = create_d_th1(hist, h_tmp, l_bincodes, vpx, vpy, my_xvar)

        # Draw histograms
        l_hist = fill_canvas(cv, d_padth1)
        l_l_hist.append(l_hist)

    cv.cd(0)
    # Draw legend
    legend = create_legend(kname, True)

    cv.cd(0)
    # Define canvas style
    ms.draw_summary("%s"%(my_tp_nameL))
    top_label_text = ms.summary_targ_type_legend(plots_cuts)
    ms.draw_targetinfo(top_label_text, "Data")

    out_obj.updt_name(nf.get_hist_hfullname(kname))
    out_obj.updt_acc_method(nf.get_hist_hmeth(kname))
    out_obj.updt_bin_code("")
    out_obj.updt_extension("png")

    cv.SaveAs(out_obj.get_path_summary(create=True, overwrite=True))
    # out_obj.get_file_name_summary()

    # Avoid memory leak warning
    h_axis.Delete()
    legend.Delete()

# Close input files
for file in l_inputfile:
    file.Close()


import tensorflow as tf
import pandas as pd
import numpy as np
import sys

from keras import regularizers

import math
import tensorflow as tf
tf.random.set_seed(7)

from Configs.pipeline_config import *
from Configs.namespaces import *
from Configs.nn_pipeline_config import *

from Misc.dataset_utils import *

from keras.callbacks import ModelCheckpoint, EarlyStopping

from time import process_time

from Models.neural_networks import *

from keras.models import Model

from sklearn.metrics import f1_score, accuracy_score

# class EarlyStopping(tf.keras.callbacks.Callback):
#   def __init__(self, accuracy_threshold):
#     super().__init__()
#     self.accuracy_treshold = accuracy_threshold

#   def on_epoch_end(self, epoch, logs={}):
#     if(logs.get('accuracy') > self.accuracy_treshold):
#       print("\nAccuracy threshold reached. Training stopped.")
#       self.model.stop_training = True

class TrainingCancellationCallback(tf.keras.callbacks.Callback):
  def __init__(self, f1_threshold, n_epochs):
    super().__init__()
    self.f1_threshold = f1_threshold
    self.n_epochs = n_epochs

  def on_epoch_end(self, epoch, logs={}):
    if(logs.get('val_f1') < self.f1_threshold):
      if epoch > 20:
        print("\nTraining loss exceeds threshold. Training cancelled.")
        self.model.stop_training = True

    if(logs.get('val_f1') < self.f1_threshold - 0.1):
      if epoch > 10:
        print("\nTraining loss exceeds threshold. Training cancelled.")
        self.model.stop_training = True

    # if (logs.get("val_f1")) < self.f1_treshold:
    #   if epoch > 20:
    #     print(f"\nTraining f1 scores below threshold {self.f1_treshold} after more than 10 epochs. Training cancelled.")
    #     self.model.stop_training = True

    # if (logs.get("f1")) < self.f1_treshold + 0.1:
    #   if epoch > 20:
    #     print(f"\nTraining f1 scores below threshold {self.f1_treshold + 0.1} after more than 20 epochs. Training cancelled.")
    #     self.model.stop_training = True

    # if np.isnan(logs.get("loss")):
    #   print("Training loss is nan.")
    #   self.model.stop_training = True

class TrainingCallback(tf.keras.callbacks.Callback):
  def __init__(self, model_output_path):
    '''
    :param lr_file: save the learning rate over time.
    '''
    self.model_output_path = model_output_path
    super().__init__()

  def on_epoch_end(self, epoch, logs=None):
    if epoch > 0:
      model_history = self.model.history.history
      save_history_plots(model_history, self.model_output_path)

    return super().on_epoch_end(epoch, logs)

  def on_train_begin(self, logs=None):
    return super().on_train_begin(logs)

  def on_train_end(self, logs=None):
    return super().on_train_end(logs)

  def on_epoch_begin(self, epoch, logs=None):
    print(f"LR at epoch {epoch} is " + str(self.model.optimizer.lr.numpy()))
    return super().on_epoch_begin(epoch, logs)

def ce_loss(y_true, y_pred):
  log_y_pred = tf.math.log(y_pred + sys.float_info.epsilon)
  elements = -tf.math.multiply_no_nan(x = log_y_pred, y = y_true)

  ce_loss_val = tf.reduce_mean(tf.reduce_sum(elements, axis = 1))

  '''
  if math.isinf(ce_loss_val):
    print("\n\n\nCE loss is infinity.")
    print("True labels")
    print(y_true)
    print("\nPredicted labels")
    print(y_pred)

  if math.isnan(ce_loss_val):
    print("\n\n\nCE loss is nan.")
    print("True labels")
    print(y_true)
    print("\nPredicted labels")
    print(y_pred)
    print("\n\n\n\n")
  '''

  return ce_loss_val

def predict(model , X_val):
  '''
  Predict the class labels for the data in val_X

  :param model: the (fitted) sklearn model
  :param val_X: data to be predicted (val or test )
  '''

  prediction = model.predict(X_val)

  return prediction

def predict_probability(model, X_data):
  '''
  If the classifier was created with the optional "probability = True" you can call this method
  to calculate the probability per class

  :return: df with integers (class names) as column names
  '''
  prediction_probability = model.predict_proba(X_data)

  return pd.DataFrame(data = prediction_probability, columns=(model.classes_).astype("int32"))

def f1(y_true, y_pred, training = True): #, labels = None):
  '''
  If training is True, y_true and y_pred are one hot encoded
  meaning shape (num_samples, num_classes) --> they have to be converted
  to a not one hot encoded shape of (num_samples, ) in order to compute
  the f1 score
  '''
  if training:
    y_true = np.array(y_true)
    y_pred = np.array(y_pred)

    y_true = np.argmax(y_true, axis = 1)
    y_pred = np.argmax(y_pred, axis = 1)
    f1_score_macro = f1_score(y_true, y_pred, average='macro', zero_division='warn')

  return f1_score_macro

def get_lr_metric(optimizer):
    def lr(y_true, y_pred):
      return optimizer.lr
    return lr

def get_y_shape_metric():
  def y_type(y_true, y_pred):
    return 0
  return y_type

def get_compiled_model(input_shape, n_classes_out, model_type, pipeline_configuration, network_configuration = None, use_val_data = False):
  '''
  :param input_shape: (n_timesteps, n_signals)
  :param n_classes_out: int
  :param model_type: str, one of the implementations defined in the neural network config
  :param network_configuration: if None --> select default configuration
  '''
  loss = tf.keras.losses.CategoricalCrossentropy(from_logits=False)
  # loss = "categorical_crossentropy"
  optimizer = tf.keras.optimizers.Adam(learning_rate=pipeline_configuration["LR"])
  lr_metric = get_lr_metric(optimizer)

  metrics = ["accuracy", f1]

  if model_type == "LSTM":
      model = get_lstm(input_shape = input_shape, n_classes_out = n_classes_out, lstm_network_config=network_configuration)
  elif model_type == "CNN":
      model = get_cnn(input_shape = input_shape, n_classes_out = n_classes_out, cnn_network_config=network_configuration)
  elif model_type == "CNN_LSTM":
      model = get_cnn_lstm(input_shape = input_shape, n_classes_out = n_classes_out, cnn_lstm_network_config=network_configuration)
  elif model_type == "LSTM_SIMPLE":
      model = get_lstm_simple(input_shape = input_shape, n_classes_out = n_classes_out, lstm_simple_network_config=network_configuration)
  elif model_type == "CNN_SIMPLE":
      model = get_cnn_simple(input_shape = input_shape, n_classes_out = n_classes_out, cnn_simple_network_config=network_configuration)
  elif model_type == "CNN_LSTM_SIMPLE":
      model = get_cnn_lstm_simple(input_shape = input_shape, n_classes_out = n_classes_out, cnn_lstm_simple_network_config=network_configuration)
  else:
      print(f"Unknown neural network architecture {model_type}.")

  model.compile(loss=loss,
                  optimizer=optimizer,
                  metrics=metrics,
                  run_eagerly = True)
                  # loss_weights=regularizers.l2(pipeline_configuration["REGULARIZATION_STRENGTH"]))
  return model

def train_model(model, train_set, epochs, pipeline_configuration, model_output_path, activity_code_type_dict, val_set = None, early_stopping_acc_threshold = 0.99, loss_treshold = 5.0, f1_threshold = 0.45, class_weight_dict = None):

    '''
    Create a classificator, train and predict
    '''
    if type(class_weight_dict) == type(None):
      class_weight_dict = {}
      for _code, _type in activity_code_type_dict.items():
        class_weight_dict[_code] = 1
    class_weight_dict = encode_classweight_dict(class_weight_dict, activity_dict = activity_code_type_dict)

    # Callback for stopping training when accuracy exceeds a threshold
    # early_stopping_acc_threshold_callback = EarlyStopping(early_stopping_acc_threshold)
    early_stopping_callback = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10)

    # Callback for cancelling training when loss exceeds a threshold or when f1 score is below a threshold after a certain amount of epochs
    n_epochs = 20
    training_cancellation_callback = TrainingCancellationCallback(f1_threshold, n_epochs)

    # Callback to monitor training process
    training_callback = TrainingCallback(model_output_path)

    # save best model checkpoint
    monitor = "loss" if type(val_set) == type(None) else "val_loss"
    model_checkpoint_callback = ModelCheckpoint(filepath=model_output_path,
                             monitor=monitor,
                             verbose=1,
                             save_best_only=True,
                             mode="min")

    # Learning Rate Scheduler
    def learning_rate_schedule(epoch, lr):
      if (epoch % pipeline_configuration["LR_DECAY_AFTER_EPOCHS"] == 0) & (epoch != 0):
        return lr * pipeline_configuration["LR_DECAY"]
      else:
        return lr
    learning_rate_callback = tf.keras.callbacks.LearningRateScheduler(learning_rate_schedule)

    t1_start = process_time()
    print(f"Fitting model for {epochs} epochs.")
    callbacks = [learning_rate_callback, model_checkpoint_callback, training_callback, early_stopping_callback]#, training_cancellation_callback] #, training_callback, training_cancellation_callback
    # history = model.fit(train_set,  class_weight = class_weight_dict, verbose = 1)
    history = model.fit(train_set, validation_data = val_set, callbacks = callbacks, epochs=epochs, verbose = 2)
    t1_stop = process_time()
    print("Fitting model time: ", t1_stop-t1_start, "s")

    return model

def save_history_plots(model_history, classifier_output_path):
  '''
  :param model_history: a trained models's .history.history object
  :param classifier_output_path: the path to where the trained model is saved, in h5 format --> has to end on h5
  '''
  loss = model_history["loss"]
  f1_score = model_history["f1"]
  accuracy = model_history["accuracy"]

  loss_plot_f = classifier_output_path.replace(".h5", "_loss.png")
  f1_score_f = classifier_output_path.replace(".h5", "_f1.png")
  accuracy_f = classifier_output_path.replace(".h5", "_acc.png")

  plt.plot(loss, color = METRIC_COLOR_DICT["LOSS"], label = "loss")
  plt.xlabel("epoch")
  plt.ylabel("cross entropy loss")
  if "val_loss" in list(model_history.keys()):
    plt.plot(model_history["val_loss"], color = METRIC_COLOR_DICT["VAL_LOSS"], label = "val_loss")
    plt.legend()
  plt.tight_layout()
  plt.savefig(loss_plot_f)
  plt.clf()

  plt.plot(f1_score, color = METRIC_COLOR_DICT["F1_SCORE"], label = "f1_score")
  plt.xlabel("epoch")
  plt.ylabel("f1 score (multiclass)")
  if "val_f1" in list(model_history.keys()):
    plt.plot(model_history["val_f1"], color = METRIC_COLOR_DICT["VAL_F1_SCORE"], label = "val_f1_scores")
    plt.legend()
  plt.tight_layout()
  plt.savefig(f1_score_f)
  plt.clf()

  plt.plot(accuracy, color = METRIC_COLOR_DICT["ACCURACY"], label = "train_accuracy")
  plt.xlabel("epoch")
  plt.ylabel("accuracy")
  if "val_accuracy" in list(model_history.keys()):
    plt.plot(model_history["val_accuracy"], color = METRIC_COLOR_DICT["VAL_ACCURACY"], label = "val_accuracy")
    plt.legend()
  plt.tight_layout()
  plt.savefig(accuracy_f)
  plt.clf()

  if "lr" in model_history.keys():
    lr = model_history["lr"]
    lr_f = classifier_output_path.replace(".h5", "_lr.png")
    plt.plot(lr, color = METRIC_COLOR_DICT["LR"])
    plt.xlabel("epoch")
    plt.ylabel("learning rate")
    plt.tight_layout()
    plt.savefig(lr_f)
    plt.clf()


def countdown(n):
    if n <= 0:
        print('Blustoff')
    else:
        print(n)
        countdown(n - 1)

#

def vertical(n):
    '''prints digits of
    n vertically
    '''
    if n < 10:
        print(n)
    else:
        vertical(n // 10)
        print(n % 10)

#

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

#print(factorial(5))

def cheers():
    n = int(n)
    if n == 0:
        print('Hurray!!!')
    else:
        print('hip', end='')

#print(cheers(5))
if __name__ == '__main__':
    countdown(5)
    vertical(3154)


import sys
import argparse
import os
import json

import re

###### MY IMPORT #########
import html
import spacy
import string
##########################

indir = '/u/cs401/A1/data/';

abbr = [line.rstrip('\n') for line in open("/u/cs401/Wordlists/abbrev.english")]
abbr2 = [line.rstrip('\n') for line in open("/u/cs401/Wordlists/pn_abbrev.english")]
abbr_all = abbr + abbr2
stopwords_2811 = [line.rstrip('\n') for line in open("/u/cs401/Wordlists/StopWords")]
nlp_2811 = spacy.load('en', disable=['parser','ner'])

def preproc1( comment , steps=range(1,11)):
    ''' This function pre-processes a single comment

    Parameters:
        comment : string, the body of a comment
        steps   : list of ints, each entry in this list corresponds to a preprocessing step

    Returns:
        modComm : string, the modified comment
    '''

    modComm = ''
    if 1 in steps:
        modComm = comment.replace('\n',' ').replace('\r',' ')
    if 2 in steps:
        if min(steps) == 2:
            chg = comment
        else:
            chg = modComm
        modComm = html.unescape(chg)
    if 3 in steps:
        if min(steps) == 3:
            chg = comment
        else:
            chg = modComm
        chg = chg.replace('\n','')
        pattern1 = r"http\S+"
        chg = re.sub(pattern1,"",chg)
        pattern2 = r"www\.\S+"
        modComm = re.sub(pattern2,"",chg)
    if 4 in steps:
        if min(steps) == 4:
            chg = comment
        else:
            chg = modComm
        modList = chg.strip().split(" ")
        modList = re.findall(r"[\wn't]+|[!\"#$%&\'()*+,-./:;<=>?@[\\\]^_`{|}~]*", chg)
        newList = []

        i = 0
        while i < len(modList):
            if ((i+2) <= len(modList)-1) and modList[i] == '.' and modList[i+2] == '.':
                checker = modList[i-1]+modList[i]+modList[i+1]+modList[i+2]
                for item in abbr_all:
                    if checker == item:
                        newList.pop()
                        newList.append(checker)
                        i += 3
            newList.append(modList[i])
            i += 1
        modComm = ' '.join(newList).strip()

    if 5 in steps:
        if min(steps) == 5:
            chg = comment
        else:
            chg = modComm

        chg = chg.split(" ")
        for i in range(len(chg)):
            if len(chg[i]) >= 3:
                if chg[i][-2] == "'" and chg[i][-1] == "t":
                    chg[i] =" ".join(re.split(r"(n't)",chg[i])[:-1])
                elif chg[i][-1] == "'":
                    chg[i] =" ".join(re.split(r"(')",chg[i])[:-1])
                elif (chg[i][-2] == "'") and (chg[i][-1] == "s"):
                    chg[i] = " ".join(re.split(r"('s)",chg[i])[:-1])
                elif (chg[i][-2] == "'") and (chg[i][-1] == "m"):
                    chg[i] = " ".join(re.split(r"('m)",chg[i])[:-1])
                elif (chg[i][-2] == "'") and (chg[i][-1] == "d"):
                    chg[i] = " ".join(re.split(r"('d)",chg[i])[:-1])
                elif (chg[i][-2] == "'") and (chg[i][-1] == "n"):
                    chg[i] = " ".join(re.split(r"('n)",chg[i])[:-1])
                elif (chg[i][-3] == "'") and (chg[i][-2:] == "re"):
                    chg[i] = " ".join(re.split(r"('re)",chg[i])[:-1])
                elif (chg[i][-3] == "'") and (chg[i][-2:] == "ve"):
                    chg[i] = " ".join(re.split(r"('ve)",chg[i])[:-1])
                elif (chg[i][-3] == "'") and (chg[i][-2:] == "ll"):
                    chg[i] = " ".join(re.split(r"('ll)",chg[i])[:-1])
                elif chg[i][1] == "'" and (chg[i][0] == "t"):
                    chg[i] = " ".join(re.split(r"(t')",chg[i]))
                elif chg[i][1] == "'" and (chg[i][0] == "y"):
                    chg[i] = " ".join(re.split(r"(y')",chg[i]))
                else:
                    continue
        modComm = ' '.join(chg).strip()
    if 6 in steps:
        if min(steps) == 6:
            chg = comment
        else:
            chg = modComm
        punc = [item for item in string.punctuation]
        ut = nlp_2811(chg)
        newList = []
        for i in range(len(ut)):
            token = ut[i].text
            tag = ut[i].tag_
            if i>0 and token in punc and ut[i-1].text in punc and tag == ut[i-1].tag_:
                position = newList[-1].rfind("/")
                if position == -1:
                    last_text = newList[-1]
                last_text = newList[-1][:position]
                newList.pop()
                new = last_text + token +'/'+ tag
            elif token == ' ':
                continue
            else:
                new = token + '/'+ tag
            newList.append(new)

        modComm = ' '.join(newList)
        #########
    if 7 in steps:
        if min(steps) == 7:
            chg = comment
        else:
            chg = modComm

        modList = chg.split(" ")
        for stop in stopwords_2811:
            i = len(modList) - 1
            while i >= 0:
                if modList[i] == "":
                    i-=1
                    continue
                position = modList[i].rfind("/")
                if position == -1:
                    w = modList[i]
                w = modList[i][:position]
                if len(w)!= 0 and w.lower() == stop:
                    modList.pop(i)
                i -= 1
        modComm = ' '.join(modList)

    if 8 in steps:
        if min(steps) == 8:
            chg = comment
        else:
            chg = modComm
        modList = chg.replace("\r"," ").replace("\n"," ").split(" ")
        newList = []
        punc = [item for item in string.punctuation]
        for i in range(len(modList)):
            if modList[i] == '':
                continue
            position = modList[i].rfind("/")
            if position == -1:
                fore = modList[i]
                back = '/'
            else:
                fore = modList[i][:position]
                back = modList[i][position:]
            #'/'
            if len(fore) == 0 or fore[0] in punc:
                newList.append(modList[i])
                continue
            #if fore is word
            utt = nlp_2811(fore)
            if utt[0].lemma_[0] == "-" and utt[0].text[0] != "-":
                newList.append(fore+back)
            else:
                newList.append(utt[0].lemma_+back)

        modComm = ' '.join(newList)

    if 9 in steps:
        if min(steps) == 9:
            chg = comment
        else:
            chg = modComm

        modList = chg.split(" ")
        newList = []
        punc = [".","!","?"]
        for i in range(len(modList)):
            if (modList[i] == '') or (modList[i][0] not in punc):
                newList.append(modList[i])
            else:
                utt = modList[i-1].split("/")
                if len(utt) == 0:
                    continue
                token = utt[0]+ '.'
                if token in abbr_all:
                    newList.append(modList[i])
                else:
                    newList.append(modList[i])
                    newList.append("\n")
        modComm = " ".join(newList)
    if 10 in steps:
        if min(steps) == 10:
            chg = comment
        else:
            chg = modComm
        modComm = chg.lower()
    return modComm

def main( args ):

    allOutput = []
    for subdir, dirs, files in os.walk(indir):
        for file in files:
            fullFile = os.path.join(subdir, file)
            #print(file)
            print( "Processing " + fullFile)
            #print(args.max)

            data = json.load(open(fullFile))
            count = 0
            for line in data:
                # if count % 1000 == 0:
                #     print(count)
                j = json.loads(line)
                new = {}
                result = preproc1(j["body"])
                new["id"],new["body"],new["cat"] = j["id"], result, file
                allOutput.append(new)
                count += 1
                if count == int(args.max):
                    break
            # TODO: select appropriate args.max lines
            # TODO: read those lines with something like `j = json.loads(line)`
            # TODO: choose to retain fields from those lines that are relevant to you
            # TODO: add a field to each selected line called 'cat' with the value of 'file' (e.g., 'Alt', 'Right', ...)
            # TODO: process the body field (j['body']) with preproc1(...) using default for `steps` argument
            # TODO: replace the 'body' field with the processed text
            # TODO: append the result to 'allOutput'

    fout = open(args.output, 'w')
    fout.write(json.dumps(allOutput))
    fout.close()

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='Process each .')
    parser.add_argument('ID', metavar='N', type=int, nargs=1,
                        help='your student ID')
    parser.add_argument("-o", "--output", help="Directs the output to a filename of your choice", required=True)
    parser.add_argument("--max", help="The maximum number of comments to read from each file", default=10000)
    args = parser.parse_args()
    #print(args)

    if (int(args.max) > 200272):
        print( "Error: If you want to read more than 200,272 comments per file, you have to read them all." )
        sys.exit(1)

    main(args)


from fastapi import APIRouter, Depends
from app.config import get_settings, Settings

router = APIRouter()

@router.get(
    "/",
    summary="Retrieve deployment information",
    response_description="""A JSON object containing deployment information:

- version: API version
- status: run-status of deployment
- environment: name of deployment environment
- testing: Boolean indicating if testing options are enabled
    """,
)
async def status(settings: Settings = Depends(get_settings)):
    """
### Retrieve API deployment status

- version: API version
- status: run-status of deployment
- environment: name of deployment environment
- testing: Boolean indicating if testing options are enabled
    """
    return {
        "version": settings.version,
        "status": "running",
        "environment": settings.environment,
        "testing": settings.testing,
    }


import os
import sys

import typer
from pydantic.main import BaseModel

ROBUSTA_BACKEND_PROFILE = os.environ.get("ROBUSTA_BACKEND_PROFILE", "")

class BackendProfile(BaseModel):
    robusta_cloud_api_host: str = ""
    robusta_ui_domain: str = ""
    robusta_relay_ws_address: str = ""
    robusta_relay_external_actions_url: str = ""
    robusta_telemetry_endpoint: str = ""
    robusta_store_token_url: str = ""
    custom_profile: bool = False

    @classmethod
    def fromDomain(cls, domain: str):
        return cls(
            robusta_cloud_api_host=f"https://api.{domain}",
            robusta_ui_domain=f"https://platform.{domain}",
            robusta_relay_ws_address=f"wss://relay.{domain}",
            robusta_relay_external_actions_url=f"https://api.{domain}/integrations/generic/actions",
            robusta_telemetry_endpoint=f"https://api.{domain}/telemetry",
            robusta_store_token_url=f"https://api.{domain}/auth/server/tokens",
        )

# default values
backend_profile = BackendProfile(
    robusta_cloud_api_host="https://api.robusta.dev",
    robusta_ui_domain="https://platform.robusta.dev",
    robusta_store_token_url="https://api.robusta.dev/auth/server/tokens",
)

if ROBUSTA_BACKEND_PROFILE:
    typer.secho(
        f"Using Robusta backend profile: {ROBUSTA_BACKEND_PROFILE}",
        color="blue",
    )
    backend_profile = BackendProfile.parse_file(ROBUSTA_BACKEND_PROFILE)
    backend_profile.custom_profile = True

    profile_dict = backend_profile.dict()
    for attribute, val in profile_dict.items():
        if not profile_dict.get(attribute):
            typer.secho(f"Illegal profile. Missing {attribute}. Aborting!")
            sys.exit(1)


# -*- coding: utf-8 -*-
from odoo import http
from odoo.http import request

class Empresas(http.Controller):

    @http.route('/api/empresas/getAll', type="json", auth="public", csrf=True, cors='*')
    def list(self):
        empresas_rec = request.env['empresas.empresas'].sudo().search([])
        empresas = []
        for rec in empresas_rec:
            vals = {
                'id': rec.id,
                'name': rec.name,
                'imagen': rec.imagen,
                'description': rec.description,
                'ingresos': rec.ingresos,
                'gastos': rec.gastos,
                'beneficios': rec.beneficios,
                'rentabilidad': rec.rentabilidad
            }
            empresas.append(vals)
        return {'status': 200, 'response': empresas, 'message': 'Success'}

    @http.route('/api/empresas/get/<int:rec_id>', type='json', auth='public', csrf=True, cors='*')
    def listOne(self, rec_id):
        model_to_get = request.env['empresas.empresas']
        rec = model_to_get.browse(rec_id).sudo().ensure_one()
        val = {
            'id': rec.id,
            'name': rec.name,
            'imagen': rec.imagen,
            'description': rec.description,
            'ingresos': rec.ingresos,
            'gastos': rec.gastos,
            'beneficios': rec.beneficios,
            'rentabilidad': rec.rentabilidad
        }
        data = {'status': 200, 'response': val, 'message': 'Success'}
        return data

    @http.route('/api/empresas/findByName', type="json", auth="public", csrf=True, cors='*')
    def findByBrand(self, **kw):
        data = kw["data"]
        reg_exp = '%' + data['name'] + '%'
        empresas_rec = request.env['empresas.empresas'].sudo().search([('name', '=ilike', reg_exp)])
        empresas = []
        for rec in empresas_rec:
            vals = {
                'id': rec.id,
                'name': rec.name,
                'imagen': rec.imagen,
                'description': rec.description,
                'ingresos': rec.ingresos,
                'gastos': rec.gastos,
                'beneficios': rec.beneficios,
                'rentabilidad': rec.rentabilidad
            }
            empresas.append(vals)
        return {'status': 200, 'response': empresas, 'message': 'Success'}

    @http.route('/api/empresas/create', type='json', auth='public', csrf=True, cors='*')
    def create(self, **kw):
        data = kw["data"]
        model_to_post = request.env["empresas.empresas"]
        record = model_to_post.sudo().create(data)
        return record.id

    @http.route('/api/empresas/update/<int:rec_id>', type='json', auth='public', csrf=True, cors='*')
    def update(self, rec_id, **kw):
        data = kw["data"]
        model_to_put = request.env["empresas.empresas"]
        rec = model_to_put.browse(rec_id).sudo().ensure_one()
        record = rec.write(data)
        data = {'status': 200, 'response': record, 'message': 'Success'}
        return data

    @http.route('/api/empresas/remove/<int:rec_id>', type='json', auth='public', csrf=True, cors='*')
    def delete(self, rec_id):
        model_to_del_rec = request.env["empresas.empresas"]
        rec = model_to_del_rec.browse(rec_id).sudo().ensure_one()
        is_deleted = rec.unlink()
        res = {
            "result": is_deleted
        }
        data = {'status': 200, 'response': res, 'message': 'Success'}
        return data

    @http.route('/api/empresas/removeAll', type='json', auth='public', csrf=True, cors='*')
    def deleteAll(self):
        model_to_del = request.env["empresas.empresas"].sudo()

        # .with_context(active_test=False) to also find inactive records.
        all_empresas = model_to_del.with_context(active_test=False).search([])
        is_deleted = all_empresas.unlink()
        res = {
            "result": is_deleted
        }
        data = {'status': 200, 'response': res, 'message': 'Success'}
        return data


from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from rest_framework.response import Response
from rest_framework.views import APIView
from .serializers import *
from .models import *
from rest_framework import status
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.permissions import IsAuthenticated
from django.db.models import Q

class MealsAPI(APIView):
    # authentication_classes = [JWTAuthentication]
    # permission_classes = [IsAuthenticated]

    def get(self, request):
        meals = Meals.objects.all()
        serializer = MealsSerializer(meals, many=True)
        return Response(serializer.data)

    def post(self, request):
        serializer = MealsSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(
                {
                    "status": status.HTTP_201_CREATED,
                    "message": "Meals added!",
                    "data": serializer.data,
                },
                status=status.HTTP_200_OK,
            )
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class MealDetailsAPIView(APIView):
    def get(self, request, id):
        try:
            print("id = ", id)
            meal = Meals.objects.get(id=id)
            serializer = MealsSerializer(meal)
            return Response(
                {
                    "status": status.HTTP_200_OK,
                    "message": "Meal details retrieved successfully",
                    "data": serializer.data,
                },
                status=status.HTTP_200_OK,
            )

        except Meals.DoesNotExist:
            return Response(
                {
                    "status": status.HTTP_404_NOT_FOUND,
                    "message": "Meal not found",
                },
                status=status.HTTP_404_NOT_FOUND,
            )

class OrderedMealsAPI(APIView):
    # authentication_classes = [JWTAuthentication]
    # permission_classes = [IsAuthenticated]

    def get(self, request, name):
        orders = OrderedMeals.objects.filter(order_user__name=name)
        if not orders:
            return Response(
                {
                    "status": status.HTTP_404_NOT_FOUND,
                    "message": "No orders found!",
                    "data": None,
                },
                status=status.HTTP_404_NOT_FOUND,
            )

        order_user = orders.first().order_user
        order_user_serializer = CustomerSerializer(order_user)

        serializer = OrderedMealsSerializer(orders, many=True)

        data = {
            "status": status.HTTP_200_OK,
            "message": "Successfully retrieved Orders!",
            "data": {
                "user_info": order_user_serializer.data,
                "orders": serializer.data,
            },
        }
        return Response(data, status=status.HTTP_200_OK)

    def post(self, request):
        ordered_items = request.data.get("ordered_items", [])
        user_info_data = request.data.get("user_info", {})

        if not ordered_items:
            return Response(
                {"detail": "No Order Items"}, status=status.HTTP_400_BAD_REQUEST
            )

        customer_serializer = CustomerSerializer(data=user_info_data)
        customer_serializer.is_valid(raise_exception=True)
        customer = customer_serializer.save()

        ordered_meals = []
        for item in ordered_items:
            meal = Meals.objects.filter(name=item["name"]).first()
            print("meal = ", meal)
            if not meal:
                return Response(
                    {
                        "status": status.HTTP_400_BAD_REQUEST,
                        "message": "No meals Found!",
                        "data": None,
                    },
                    status=status.HTTP_400_BAD_REQUEST,
                )

            ordered_meal = OrderedMeals(
                order_meals=meal, order_user=customer, amount=item["amount"]
            )
            ordered_meals.append(ordered_meal)

        OrderedMeals.objects.bulk_create(ordered_meals)

        response_data = {
            "status": status.HTTP_201_CREATED,
            "message": "Order placed successfully!",
            "data": {
                "user_info": {
                    "name": customer.name,
                    "email": customer.email,
                    "street": customer.street,
                    "postal_code": customer.postal_code,
                    "city": customer.city,
                },
                "ordered_items": [
                    {
                        "name": item["name"],
                        "amount": item["amount"],
                    }
                    for item in ordered_items
                ],
            },
        }
        return Response(response_data, status=status.HTTP_201_CREATED)

class SearchAPIView(APIView):
    def get(self, request, name):
        searched_meal = Meals.objects.all().filter(Q(name__icontains=name))

        if searched_meal.exists():
            serializer = MealsSerializer(searched_meal, many=True)
            return Response(
                {
                    "status": status.HTTP_200_OK,
                    "message": "Meals FOUND successfully",
                    "data": serializer.data,
                },
                status=status.HTTP_200_OK,
            )
        else:
            return Response(
                {
                    "status": status.HTTP_404_NOT_FOUND,
                    "message": "No similar meal found!",
                },
                status=status.HTTP_404_NOT_FOUND,
            )

class ReviewRatingAPI(APIView):
    def get(self, request):
        review = ReviewRating.objects.all()
        serializer = ReviewRatingSerializer(review, many=True)
        return Response(serializer.data)

    def post(self, request):
        serializer = ReviewRatingSerializer(
            data=request.data, context={"request": request}
        )
        if serializer.is_valid():
            serializer.save(user=request.user)
            return Response(
                {
                    "status": status.HTTP_201_CREATED,
                    "message": "Review ratings added!",
                    "data": serializer.data,
                },
                status=status.HTTP_201_CREATED,
            )

        return Response(
            {
                "status": status.HTTP_404_NOT_FOUND,
                "message": "ERROR while posting review Ratings!",
            },
            status=status.HTTP_404_NOT_FOUND,
        )

def admin_only(func):
    def wrapper(self, request, *args, **kwargs):
        if request.user.is_authenticated and request.user.role == "Admin":
            return func(self, request, *args, **kwargs)
        return Response(
            {"message": "Unauthorized user"}, status=status.HTTP_401_UNAUTHORIZED
        )

    return wrapper

class TestimonialsAPI(APIView):
    def get(self, request):
        testimonials = Testimonials.objects.all()
        serializer = TestimonialsSerializer(testimonials, many=True)
        if len(serializer.data) != 0:
            return Response(
                {
                    "status": status.HTTP_200_OK,
                    "message": "Got all the testimonials",
                    "data": serializer.data,
                },
                status=status.HTTP_200_OK,
            )
        else:
            return Response(
                {
                    "status": status.HTTP_404_NOT_FOUND,
                    "message": "ERROR in getting the Testimonials",
                },
                status=status.HTTP_404_NOT_FOUND,
            )

    @admin_only
    def post(self, request):
        serializer = TestimonialsSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(
                {
                    "status": status.HTTP_201_CREATED,
                    "message": "Testimonial added successfully!",
                    "data": serializer.data,
                },
                status=status.HTTP_201_CREATED,
            )
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    @admin_only
    def put(self, request, id):
        try:
            testimonial = Testimonials.objects.get(id=id)
        except Testimonials.DoesNotExist:
            return Response(
                {"message": "Testimonial not found"}, status=status.HTTP_404_NOT_FOUND
            )

        serializer = TestimonialsSerializer(testimonial, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(
                {
                    "status": status.HTTP_200_OK,
                    "message": "Testimonial updated successfully!",
                    "data": serializer.data,
                }
            )
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    @admin_only
    def delete(self, request, id):
        try:
            testimonial = Testimonials.objects.get(id=id)
        except Testimonials.DoesNotExist:
            return Response(
                {"message": "Testimonial not found"}, status=status.HTTP_404_NOT_FOUND
            )

        testimonial.delete()
        return Response(
            {"message": f"Testimonial{id} deleted successfully"},
            status=status.HTTP_204_NO_CONTENT,
        )


from lists import get_list_of_cities, get_list_of_places

class city_info:
    def __init__(self):
        pass

def get_places(city_name):
    list_of_cities = get_list_of_cities()
    list_of_places = get_list_of_places()
    list_of_places_per_city = {list_of_cities[index]: [] for index in range(len(list_of_cities))}

    if city_name not in list_of_cities:
        return None
    for place in list_of_places:
        words = []
        word = ''
        i = 0
        # print(place)
        for i in range(len(place)):
            if place[i] == ' ':
                words.append(word)
                word = ''
            else:
                word += place[i]
        words.append(word)
        if words[-1] in list_of_cities:
            city = words[-1]
            list_of_places_per_city[city].append(place)
        elif len(words) > 1 and words[-2] + ' ' + words[-1] in list_of_cities:
            city = words[-2] + ' ' + words[-1]
            list_of_places_per_city[city].append(place)

        # print(words)

    return list_of_places_per_city[city_name]


import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

PORT = 8080
IP = '127.0.0.1'
s.connect((IP, PORT))
msg = s.recv(1024)
print(msg.decode())

while True:
    cmd = input("-> ")
    if cmd == "quit":
        s.close()
        sys.exit()
    elif cmd == "":
        print("INVALID COMMAND")
    else:
        s.send(cmd.encode("utf-8"))
        print(s.recv(2048).decode())


import csv
import glob
import json
from dataclasses import dataclass
from typing import List

import progressbar as progressbar
from dataclasses_json import dataclass_json

RELATIVE_PATH = ''
# RELATIVE_PATH = '../'
DATA_DIR = f'{RELATIVE_PATH}../pagexml'
SCANS_FILE = f'{RELATIVE_PATH}data/scans_20210403.csv'
INVENTORY_FILE = f'{RELATIVE_PATH}data/werkvoorraad.csv'
TAGS_FILE = f'{RELATIVE_PATH}data/scan_tags.json'
ALL_PAGEXML_LST = f'{RELATIVE_PATH}data/all-pagexml.lst'

@dataclass_json
@dataclass
class ScanData:
    """Simple class for scan data"""
    id: int
    title: str
    number: int
    previewImage: str
    xmlKey: str
    imageURL: str

@dataclass_json
@dataclass
class WorkData:
    """Data from werkvoorraad"""
    rubric: int
    inventoryNumber: str
    serialNumber: str
    title: str
    writer: str
    numberOfScans: int
    status: str

@dataclass_json
@dataclass
class Writer:
    id: str
    name: str

@dataclass_json
@dataclass
class Archive:
    inventoryNumber: int
    serialNumber: str
    title: str
    numberOfScans: int
    status: str
    writer_id: int

def read_scan_data():
    with open(SCANS_FILE) as f:
        reader = csv.reader(f)
        data = [tuple(row) for row in reader]
    return [ScanData(*sd) for sd in data[1:]]

def read_werkvoorraad():
    with open(INVENTORY_FILE) as f:
        reader = csv.reader(f, delimiter=';')
        data = [tuple(row) for row in reader]

    return [WorkData(*wd) for wd in data[1:]]

def read_scan_tags():
    with open(TAGS_FILE) as f:
        return json.load(f)

def extract_writer_index(work_data_list):
    return {
        t[0]: Writer(*t)
        for t in {(wd.rubric, wd.writer) for wd in work_data_list}
    }

def extract_archive_index(work_data_list):
    return {wd.inventoryNumber: Archive(wd.inventoryNumber, wd.serialNumber, wd.title, wd.numberOfScans, wd.status,
                                        wd.rubric) for wd in work_data_list}

def transkribus_url(archive_id: str, page_index: int) -> str:
    return f'https://transkribus.eu/r/amsterdam-city-archives/#/documents/{archive_id}/pages/{page_index}'

def scan_paths(scan_dir: str):
    return [x.replace('\\', '/') for x in glob.glob(f'{scan_dir}/*.xml')]

def get_xml_paths():
    archive_dirs = [x.replace('\\', '/') for x in glob.glob(f'{DATA_DIR}/*[0-9]')]
    return {dir.split('/')[-1]: scan_paths(dir) for dir in archive_dirs}

def file_url(scan_path: str):
    return f'file:///C:/workspaces/golden-agents/pagexml{scan_path.replace(DATA_DIR, "")}'

def all_page_xml_file_paths() -> List[str]:
    with open(ALL_PAGEXML_LST) as f:
        paths = f.readlines()
    return [p.strip() for p in paths]

def default_progress_bar(max_value):
    widgets = [' [',
               progressbar.Timer(format='elapsed time: %(elapsed)s'),
               '] ',
               progressbar.Bar('*'),
               ' (',
               progressbar.ETA(),
               ') ',
               ]
    return progressbar.ProgressBar(max_value=max_value,
                                   widgets=widgets).start()


#Desenvolva um programa que leia quatro valores pelo teclado e guarde-os em uma tupla. No final, mostre: A) Quantas vezes apareceu o valor 9. B) Em que posição foi digitado o primeiro valor 3. C) Quais foram os números pares.
cont9 = 0
a = int(input('Digite o 1° número: '))
b = int(input('Digite o 2° número: '))
c = int(input('Digite o 3° número: '))
d = int(input('Digite o 4° número: '))
e = (a, b, c, d)
print(f'voce digitou os valores {e}')
print(f'O número 9 apareceu {e.count(9)} vezes.')
if 3 in e:
    print(f'O número 3 apareceu a primeira vez na posição {e.index(3)}.')
else:
    print('O valor 3 não foi encontrado.')
print(f'Os valores pares digitados foram: ', end='')
for c in e:
    if c % 2 == 0:
        print(c, end=' ')

Curso em Vídeo/075 Análise de dados em uma Tupla.py


### 处理 SIGCHLD 信号
### 子进程退出时系统会向父进程发送 SIGCHLD 信号，父进程可以通过注册 SIGCHLD 信号处理程序，在信号处理程序中调用 wait 系统调用来清理僵尸进程

import errno
from multiprocessing import Process, current_process
import logging
import os
import signal
import time

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)-15s - %(levelname)s - %(message)s'
)

def run():
    exitcode = 3
    logging.info('exit child process %s with exitcode %s',
                 current_process().pid, exitcode)
    os._exit(exitcode)

def wait_child(signum, frame):
    logging.info('receive SIGCHLD')
    try:
        while True:
            # -1 表示任意子进程
            # os.WNOHANG 表示如果没有可用的需要 wait 退出状态的子进程，立即返回不阻塞
            cpid, status = os.waitpid(-1, os.WNOHANG)
            if cpid == 0:
                logging.info('no child process was immediately available')
                break
            exitcode = status >> 8
            logging.info('child process %s exit with exitcode %s', cpid, exitcode)
    except OSError as e:
        if e.errno == errno.ECHILD:
            logging.warning('current process has no existing unwaited-for child processes.')
        else:
            raise
    logging.info('handle SIGCHLD end')

signal.signal(signal.SIGCHLD, wait_child)

p = Process(target=run)
p.start()

while True:
    time.sleep(100)


from pydantic import BaseModel
from queue import Queue
from threading import Thread
from typing import List

from ..scrap_worker.models.event_models import CandidacyEvent
from ..scrap_worker.app_state_machine import ScrapWorkerSubsystemConfigurations
from ..scrap_worker.state_machine.models import StateFlag, TimeoutFlag
from .mocks.kafka import (
    KafkaConsumerBaseMock,
    KafkaMessageWrapperMock,
    KafkaProducerBaseMock,
)
from .testables.subsystems import ElectionSubsystemTestable

messages: Queue = Queue(maxsize=100)
leader_uid: str = "00000000-0000-0000-0000-000000000000"

def test_election_subsystem_exit_flag():
    exit_flag = StateFlag("test_exit_flag")
    all_configs = ScrapWorkerSubsystemConfigurations(global_exit_flag=exit_flag)
    c = all_configs.election_subsystem_configuration
    c.ranking = 1000
    c.countdown_start = 10
    c.countdown_delay_ms = 500
    c.campaign_ttl_ms = 5000
    subsystem = ElectionSubsystemTestable(c)
    mock_producer = KafkaProducerBaseMock(output=messages)
    subsystem._set_mock_producer(mock_producer)
    mock_consumer = KafkaConsumerBaseMock(input=messages)
    subsystem._set_mock_consumer(mock_consumer)
    thread = Thread(target=subsystem.start)
    timeout = TimeoutFlag(3000, "Test timeout")

    thread.start()
    while not c.started_flag.get_value():
        assert (
            not timeout.get_value()
        ), "Test timed out waiting for subsystem to mark itself as started."

    ready_to_exit = TimeoutFlag(500, "Letting subsystem run for a bit.")
    while not ready_to_exit.get_value():
        assert (
            not timeout.get_value()
        ), "Test timed out while letting subsystem run. (May mean start up took longer than it should.)"

    assert (
        not c.exited_flag.get_value()
    ), "Subsystem exited before the global exit flag was set."

    exit_flag.set_value()

    while not c.exited_flag.get_value():
        assert (
            not timeout.get_value()
        ), "Test timed out waiting for subsystem to mark itself as exited"
    while thread.is_alive():
        thread.join(10)
        assert (
            not timeout.get_value()
        ), "Test timed out waiting for subsystem thread to die."

    assert not c.exception_flag.get_value(), "Subsystem threw an unhandled exception."

def test_election_subsystem_elected_new_leader():
    voted_for_us = StateFlag("voted_for_us")
    exit_flag = StateFlag("test_exit_flag")
    all_configs = ScrapWorkerSubsystemConfigurations(global_exit_flag=exit_flag)
    c = all_configs.election_subsystem_configuration

    class KafkaProducerDetectVoteMock(KafkaProducerBaseMock):
        def produce(
            self, message: BaseModel, topic: str = None, debug_mode: bool = False
        ):
            super(KafkaProducerDetectVoteMock, self).produce(
                message=message, topic=topic, debug_mode=debug_mode
            )
            assert isinstance(
                message, CandidacyEvent
            ), f"Election subsystem must only produce messages of type {CandidacyEvent.__name__}"
            candidacy: CandidacyEvent = message
            assert candidacy.worker_id == str(
                c.worker_id
            ), "Expected configured worker id to be specified in broadcasted candidacy."
            if candidacy.withdrawn_and_nominating_worker_id == leader_uid:
                voted_for_us.set_value()

    initial_ranking = 1000
    c.ranking = initial_ranking
    c.countdown_start = 10
    c.countdown_delay_ms = 500
    c.campaign_ttl_ms = 5000
    subsystem = ElectionSubsystemTestable(c)
    mock_producer = KafkaProducerDetectVoteMock(output=messages)
    subsystem._set_mock_producer(mock_producer)
    mock_consumer = KafkaConsumerBaseMock(input=messages)
    subsystem._set_mock_consumer(mock_consumer)
    thread = Thread(target=subsystem.start)
    timeout = TimeoutFlag(3000, "Test timeout")

    thread.start()
    while not c.started_flag.get_value():
        assert (
            not timeout.get_value()
        ), "Test timed out waiting for subsystem to mark itself as started."

    ready_to_exit = TimeoutFlag(500, "Letting subsystem run for a bit.")
    while not ready_to_exit.get_value():
        assert (
            not timeout.get_value()
        ), "Test timed out while letting subsystem run. (May mean start up took longer than it should.)"

    messages.put(
        KafkaMessageWrapperMock(
            CandidacyEvent(
                worker_id=leader_uid,
                # Don't try to test ties here.
                ranking=initial_ranking + 1,
            )
            .json()
            .encode("utf-8")
        )
    )

    while not voted_for_us.get_value():
        assert (
            not timeout.get_value()
        ), "Test timed out waiting for election subsystem to vote for us."

    messages.put(
        KafkaMessageWrapperMock(
            CandidacyEvent(
                worker_id=leader_uid,
                ranking=initial_ranking + 1,
                acceptance_countdown=0,
            )
            .json()
            .encode("utf-8")
        )
    )

    while not c.elected_new_leader_flag.get_value():
        assert (
            not timeout.get_value()
        ), "Test timed out waiting for subsystem accept us as a leader."
    while not c.exited_flag.get_value():
        assert (
            not timeout.get_value()
        ), "Test timed out waiting for subsystem to mark itself as exited"
    while thread.is_alive():
        thread.join(10)
        assert (
            not timeout.get_value()
        ), "Test timed out waiting for subsystem thread to die."
    assert (
        c.winning_candidacy is not None
    ), "Election subsystem did not provide the winning candidacy."
    assert (
        c.winning_candidacy.worker_id == leader_uid
    ), "Election subsystem did not provide our candidacy as the winning candidacy."
    assert not c.exception_flag.get_value(), "Subsystem threw an unhandled exception."


import cv2
import numpy as np

inpimg = cv2.imread('stock-snap.jpg')

l = len(inpimg)
b = len(inpimg[0])

grey = np.zeros((l, b), dtype=np.uint8)

for i in range(0, l):
    for j in range(0, b):
        grey[i][j] = int(inpimg[i][j][0] * 0.2126 + inpimg[i][j][1] * 0.7152 + inpimg[i][j][2] * 0.0722)

sb = np.zeros((l, b), dtype=np.uint8)

for i in range(0, l):
    for j in range(0, b):
        sb[i][j] = int(inpimg[i][j][0] * 0.2126 + inpimg[i][j][1] * 0.7152 + inpimg[i][j][2] * 0.0722)

for i in range(200, l-200):
    for j in range(200, b-200):
        sb[i][j] = int(inpimg[i][j][0] * 0 + inpimg[i][j][1] * 0 + inpimg[i][j][2] * 0)

cv2.imshow('Input Image', inpimg)
cv2.imshow('Grey Image', grey)
cv2.imshow('Inner Black', sb)
cv2.imshow('Sub',grey-sb)
cv2.waitKey(0)


import functions
import torch
import torch.optim as optim
import os
import AuTransformerMaxPool

pathOrigin = "/media/jonas/B41ED7D91ED792AA/Arbeit_und_Studium/Kognitionswissenschaft/Semester_5/masterarbeit#/data_Code"
device = "cpu"

model = AuTransformerMaxPool.AE_Transformer(9680, 10, 10, 1, 1, 1, 1, 1,torch.device(device), True, 5)
optimizer = optim.Adam(model.parameters(), lr=0.001, weight_decay = 0.01)
"""
#path = pathOrigin + "/conditionedOutputSoftmax/conditionedOutputSoftmax"
#checkpoint = torch.load(path)

#model.load_state_dict(checkpoint['state_dict'])
"""
# load dataset
os.chdir(pathOrigin + "/datasets")
sceneDataAletsch = functions.openData("test")
for i in range(len(sceneDataAletsch)):
    print(torch.nonzero(torch.isnan(sceneDataAletsch[i][0][1])))
    print(torch.nonzero(torch.isnan(sceneDataAletsch[i][1][1])))

#print(sceneDataAletsch)
# move to cuda
#sceneDataAletsch = list(map(lambda x: functions.moveToCuda(x, torch.device(device)), sceneDataAletsch))

# train on scenes
functions.fullSceneTrain(model, "transformerScenes", optimizer,
                                 sceneDataAletsch,
                                 3,
                                 50, 50,
                                 (1, 600 ,600),
                                device,
                                True)


import os

x = 'C:\\Users\\NCA\\Desktop\\Logfiles(1) - Copy\\Asokwa\\Voice'

for file in os.listdir(x):
    file_name, file_ext = os.path.splitext(file)
    full_path = os.path.join(x).split('\\')[-1]+'_'+ os.path.join(x).split('\\')[-2]
    if file_name[-2:] == '.1':
        os.rename(file, full_path+' MTN MOC '+file)


import numpy as np
import pickle

class Model:

    def __init__(self, prefix_len = 2) -> None:
        self.nxt: dict[str, dict[str, int]] = dict()
        self.prefix_len = prefix_len

    def get_next(self, n_gramm: list[str]) -> str:
        for i in range(len(n_gramm) + 1):
            prev = ' '.join(n_gramm[i:])
            if prev not in self.nxt.keys() or not len(self.nxt[prev]): continue

            p = np.array(list(self.nxt[prev].values()))
            # for t in range(i): n_gramm.pop(0)
            return np.random.choice(list(self.nxt[prev].keys()), p=p/p.sum())

    def fit(self, n_gramm: list[str], word: str) -> None:
        for i in range(len(n_gramm) + 1):
            prev = ' '.join(n_gramm[i:])
            if prev not in self.nxt.keys():
                self.nxt[prev] = dict()
            if word not in self.nxt[prev].keys():
                self.nxt[prev][word] = 0
            self.nxt[prev][word] += 1

def save(path: str, model: Model) -> None:
    try:
        with open(path, "wb") as f:
            pickle.dump(model, f)
    except:
        print("Can't open save path.")

def load(path: str) -> Model:
    try:
        with open(path, "rb") as f:
            return pickle.load(f)
    except:
        print("Can't load model file.")
        return None


# https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/chunky-monkey
# Reprak11
import math

def chunkArrayInGroups(arr, size):
    arreglo = []
    contadora = 0
    for i in range(0,math.floor(len(arr)/size)):
        arreglo.append(arr[contadora:contadora+size])
        contadora += size
    if (len(arr)%2 != 0):
        arreglo.append(arr[contadora:])
    return arreglo

if __name__ == "__main__":
    print(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3))


"""Add User.is_auth

Revision ID: 367b19bd951b
Revises: dede16ebc06b
Create Date: 2022-12-14 15:29:53.643470

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '367b19bd951b'
down_revision = 'dede16ebc06b'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('is_auth', sa.Boolean(), nullable=True))
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('users', 'is_auth')
    # ### end Alembic commands ###


from typing import Optional
# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def bstFromPreorder(self, preorder: list[int]) -> Optional[TreeNode]:
        # 根左右，第一个是根，比根大的是右子树，根和右子树之间是左子树
        left = 0
        right = len(preorder) - 1
        return self.helper(preorder, left, right)

    def helper(self, preorder, left, right):
        if left > right: return None
        rootVal = preorder[left]
        root = TreeNode(rootVal)
        if left == right: return root
        splitIndex = left + 1
        found = False
        for i in range(left, right + 1):
            if preorder[i] > rootVal:
                splitIndex = i
                found = True
                break
        if found:
            root.left = self.helper(preorder, left + 1, splitIndex - 1)
            root.right = self.helper(preorder, splitIndex, right)
        else:
            root.left = self.helper(preorder, left + 1, right)
            root.right = None
        return root

'''
1008. Construct Binary Search Tree from Preorder Traversal
Medium
Given an array of integers preorder, which represents the preorder traversal of a BST (i.e., binary search tree), construct the tree and return its root.
It is guaranteed that there is always possible to find a binary search tree with the given requirements for the given test cases.
A binary search tree is a binary tree where for every node, any descendant of Node.left has a value strictly less than Node.val, and any descendant of Node.right has a value strictly greater than Node.val.
A preorder traversal of a binary tree displays the value of the node first, then traverses Node.left, then traverses Node.right.
Example 1:
Input: preorder = [8,5,1,7,10,12]
Output: [8,5,10,1,7,null,12]
Example 2:
Input: preorder = [1,3]
Output: [1,null,3]
Constraints:
1 <= preorder.length <= 100
1 <= preorder[i] <= 1000
All the values of preorder are unique.
'''


import uuid

def uuid_generator(template_id_array):
    uuid_template_id_array = []
    for template_id in template_id_array:
        if template_id == 'nan' or template_id == '<Will be given by AppCS Onboarding Team>' or template_id == '<This will be filled up by AppCS Onboarding Team>':
            template_id = '<table_name>-' + str(uuid.uuid4())
            uuid_template_id_array.append(template_id)
        else:
            uuid_template_id_array.append(template_id)
    return uuid_template_id_array

def pop_svc_converter(pop_svc_array):
    clean_pop_svc_array = []
    for item in pop_svc_array:
        item = item.split('[')[-1].split(']')[0]
        if item != 'Template ID':
            clean_pop_svc_array.append(item)
    return clean_pop_svc_array

def main_name_converter(table_name_array):
    table_name_array = list(map(lambda x: x.lower(), table_name_array))
    converted_table_name_array = []
    for table_name in table_name_array:
        table_name = table_name.replace('_main', '')
        if table_name == 'audit_code' or table_name == 'audit_infotype':
            converted_table_name_array.append(table_name)
        elif table_name.split('_')[0].lower() == 'cd':
            table_name = table_name.replace('_', '')
            converted_table_name_array.append(table_name + '_main')
        else:
            converted_table_name_array.append(table_name + '_main')
    return converted_table_name_array

def field_array_splitter(table_name_column_array, field_column_array):
    field_arrays = []
    field_array = []
    i = 0
    while i < len(field_column_array):
        if i == 0:
            field_array.append(field_column_array[i])
        elif table_name_column_array[i] == 'nan' and table_name_column_array[i - 1] == 'nan' or table_name_column_array[i] == 'nan' and table_name_column_array[i - 1] != 'nan':
            field_array.append(field_column_array[i])
        else:
            field_arrays.append(field_array)
            field_array = []
            field_array.append(field_column_array[i])
        i += 1
    field_arrays.append(field_array)
    return field_arrays


import curses
from termcolor import colored

class Card():

    ranks = {'A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6,
             '7': 7, '8': 8, '9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10}

    suits = {'CLUBS': '♣', 'DIAMONDS': '♦', 'HEARTS': '♥', 'SPADES': '♠'}

    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit
        self.full_name = self.generate_full_name()

    def generate_full_name(self):
        names = {'A': 'ACE', 'J': 'JACK', 'Q': 'QUEEN', 'K': 'KING'}
        name = ""
        if str(self.rank) in names:
            name = names[self.rank]
        else:
            name = str(self.rank)

        full_name = name + " OF " + str(self.suit)
        return full_name

    def get_value(self):
        return self.ranks[self.rank]

    def draw(self):

        print("┌───────┐")
        print(f"│ {self.rank:<2}    │")
        print("│       │")
        print(f"│   {self.suits[self.suit]}   │")
        print("│       │")
        print(f"│    {self.rank:>2} │")
        print("└───────┘")


import hydra
import os, sys
from typing import List, Union, Dict
import omegaconf
import pytorch_lightning as pl
import ray
from pytorch_lightning.loggers import WandbLogger
from ray.tune.integration.pytorch_lightning import TuneReportCallback
from ray import tune

import Models.Utils as Utils
from Models.DataModules.ModMLMData import ModMLMDataModule
from Models.Modules.ModifiedLangModelingModule import ModifiedLMModule

import logging

logger = logging.getLogger(__name__)

def prepare_and_feed_config(updates: Dict, base_config: omegaconf.dictconfig.DictConfig):
    config = _get_updated_config(base_config, updates)
    config.pop('ray')

    # tune.report(loss=0.1)
    _run_modlm_train_test(config)

def _get_updated_config(base_config, updates):
    base_dict = Utils.flatten_config(base_config)
    base_dict.update(updates)
    base_config_dict = Utils.unflatten_config(base_dict)

    config = omegaconf.dictconfig.DictConfig(content=base_config_dict)
    # d = dict(base_config).update(updates)
    # config = Namespace(**update_config(dict(base_config), update_dict))
    return config

def tune_modlm_asha(config: omegaconf.dictconfig.DictConfig):
    assert "ray" in config, f'config does not have `ray`: {config}'
    # sweep_dict = {
    #     "train_setup.learning_rate": tune.loguniform(1e-6, 1e-4),
    #     # "data_module": {
    #     #     'train_batch_size': tune.choice([2, 4]),
    #     #     'max_seq_length': tune.choice([200, 300, 400, 450]),
    #     # },
    # }
    # setup the sweep values based on config
    sweep_dict = {}
    for k, v in dict(config.ray.sweep_dict).items():
        exec(f'sweep_dict[k] = {v}')

    scheduler = tune.schedulers.ASHAScheduler(
        max_t=1,
        grace_period=1,
        reduction_factor=2)

    reporter = tune.CLIReporter(
        # parameter_columns=["learning_rate", "train_batch_size", "max_seq_length"],
        metric_columns=["loss"])

    analysis = tune.run(
        tune.with_parameters(
            prepare_and_feed_config,
            base_config=config
        ),
        resources_per_trial={
            "cpu": 1,
            "gpu": 4
        },
        metric="loss",
        mode="min",
        config=sweep_dict,
        num_samples=config.ray.num_samples,
        scheduler=scheduler,
        progress_reporter=reporter,
        name="tune_modlm_asha")

    logger.warning("Best hyperparameters found were: ", analysis.best_config)

def _run_modlm_train_test(config: omegaconf.dictconfig.DictConfig):
    # print('TUNE_EX\n\n', sys.path, '\n\n')
    logger.warning(f'Running _run_modlm_train_test')
    # ------------
    # data
    # ------------
    data_module = ModMLMDataModule(config)

    # ------------
    # model
    # ------------
    lmmodel = ModifiedLMModule(config)

    # ------------
    # training
    # ------------
    callbacks_list = [
        # pl.callbacks.EarlyStopping(monitor="val_loss", min_delta=1e-4),
    ]
    if 'no_hyper_tune' not in config:
        callbacks_list.append(
            TuneReportCallback({
                "loss": "train_loss",
            }, on="batch_start")
        )

    trainer = pl.Trainer(
        gradient_clip_val=0,
        gpus=config['hardware']['gpus'],
        auto_select_gpus=True,
        max_epochs=1,
        min_epochs=1,
        resume_from_checkpoint=None,
        distributed_backend=None,
        accumulate_grad_batches=config['trainer_args']['accumulate_grad_batches'],
        limit_train_batches=config['trainer_args']['limit_train_batches'],
        logger=pl.loggers.WandbLogger(name=f"Mod-MLM-Trial3", project="CQ++"),
    )

    trainer.fit(lmmodel, datamodule=data_module)
    trainer.save_checkpoint('Checkpoint/ModifiedLMModule.ckpt')
    trainer.test(lmmodel, datamodule=data_module)

@hydra.main(config_path="../Configs", config_name="modified_lm_config")
def main(config: omegaconf.dictconfig.DictConfig):
    # tune_modlm_asha(config)
    _run_modlm_train_test(config)

if __name__ == '__main__':
    main()


#!/bin/bash
##############################################################
# File Name:
# Version: V1.0
# Author: shujingping
# Organization: https://www.cnblogs.com/jinggs/
# Created Time : 2020-05-28 15:32:36
# Description:
##############################################################
'''
设计一个函数返回给定文件名的后缀名
'''

def get_suffix(filename):
	suffix = filename.split('.')[-1]
	print(suffix)
	return suffix

if __name__ == '__main__':
	filename = 'http_post.tmp'
	get_suffix(filename)


"""
"""
import logging

from datetime import datetime
from optparse import make_option

from django.utils.translation import ugettext as _

from ..models import UpdateProgressLog
from fle_utils.django_utils.command import LocaleAwareCommand
from functools import wraps

logger = logging.getLogger(__name__)

def skip_if_no_progress_log(func):
    @wraps(func)
    def check(self, *args, **kwargs):
        if not self.progress_log:
            return
        return func(self, *args, **kwargs)

    return check

class UpdatesCommand(LocaleAwareCommand):
    """
    Abstract class for sharing code across Dynamic and Static versions
    """

    option_list = LocaleAwareCommand.option_list + (
        # WARNING: Because of issues having foreground as a default option, we
        # are overwriting this with a --background option in
        # retrievecontentpack
        make_option(
            "", "--foreground",
            action="store_true",
            dest="foreground",
            default=False,
            help=(
                "Run the command without job scheduler and  database-backed "
                "progress. Used when you retrieve a content pack before "
                "initializing KA Lite's db."
            )
        ),
    )

    def __init__(self, process_name=None, *args, **kwargs):

        self.process_name = process_name or self.__class__.__module__.split(".")[-1]

        super(UpdatesCommand, self).__init__(*args, **kwargs)

    def setup(self, options):

        # Should be set by command's handle() method since there's no clear
        # call structure defined. Respected by the methods that update stuff
        # in the database.
        self.foreground = options.get("foreground", False)
        self.background = options.get("background", True)

        if not self.foreground and self.background:
            self.progress_log = UpdateProgressLog.get_active_log(process_name=self.process_name)
        else:
            self.progress_log = None
        if self.progress_log and self.progress_log.current_stage:
            self.progress_log.cancel_progress(notes=_("Starting fresh."))
            self.progress_log = UpdateProgressLog.get_active_log(process_name=self.process_name)

    @skip_if_no_progress_log
    def display_notes(self, notes, ignore_same=True):
        if notes and (not ignore_same or notes != self.progress_log.notes):
            logger.info(notes)

    @skip_if_no_progress_log
    def ended(self):
        return self.progress_log.end_time is not None

class UpdatesDynamicCommand(UpdatesCommand):
    """
    Command that updates the UpdateProgressLog table,
    without having a full, static list of stages.

    Updates by knowing the current total number of stages (which can change),

    """
    def __init__(self, num_stages=None, *args, **kwargs):
        super(UpdatesDynamicCommand, self).__init__(*args, **kwargs)
        if num_stages:
            self.set_stages(num_stages=num_stages)

    @skip_if_no_progress_log
    def start(self, stage_name=None, notes=None):
        if not self.progress_log.total_stages:
            raise Exception("Must set num-stages (through __init__ or set_stages()) before starting.")
        self.check_if_cancel_requested()
        self.display_notes(notes, ignore_same=False)
        self.progress_log.update_stage(stage_name=stage_name, stage_percent=0, notes=notes)

    @skip_if_no_progress_log
    def started(self):
        self.check_if_cancel_requested()
        return self.progress_log.total_stages and not self.progress_log.completed

    @skip_if_no_progress_log
    def set_stages(self, num_stages=None, notes=None):
        """
        Allow dynamic resetting of stages.
        """
        self.check_if_cancel_requested()
        self.display_notes(notes)
        self.progress_log.update_total_stages(num_stages)

    @skip_if_no_progress_log
    def next_stage(self, stage_name=None, notes=None):
        assert self.started(), "Must call start() before moving to a next stage!"
        self.check_if_cancel_requested()
        self.display_notes(notes or "")  # blank out old notes, if necessary
        self.update_stage(stage_name=stage_name, stage_percent=0., notes=notes)

    @skip_if_no_progress_log
    def update_stage(self, stage_name=None, stage_percent=None, stage_status=None, notes=None):
        self.check_if_cancel_requested()
        self.display_notes(notes)
        self.progress_log.update_stage(stage_name=stage_name, stage_percent=stage_percent, stage_status=stage_status, notes=notes)

    @skip_if_no_progress_log
    def cancel(self, stage_status=None, notes=None):
        self.check_if_cancel_requested()
        self.display_notes(notes)
        self.progress_log.cancel_progress(stage_status=stage_status, notes=notes)

    @skip_if_no_progress_log
    def complete(self, notes=None):
        self.check_if_cancel_requested()
        self.display_notes(notes)
        self.progress_log.mark_as_completed(notes=notes)

    @skip_if_no_progress_log
    def check_if_cancel_requested(self):
        if self.progress_log.id:
            self.progress_log = UpdateProgressLog.objects.get(id=self.progress_log.id)
        if self.progress_log.cancel_requested:
            self.progress_log.end_time = datetime.now()
            self.progress_log.save()
            raise RuntimeError("Process cancelled.")

class UpdatesStaticCommand(UpdatesCommand):
    """
    Command that updates the UpdateProgressLog table,
    having a well-defined, sequential static list of stages.
    """
    def __init__(self, *args, **kwargs):
        super(UpdatesStaticCommand, self).__init__(*args, **kwargs)

    @skip_if_no_progress_log
    def start(self, notes=None):
        assert self.stages, "Stages must be set before starting."
        assert self.progress_log.current_stage is None, "Must not call start while already in progress."
        self.progress_log.update_total_stages(len(self.stages))
        self.display_notes(notes, ignore_same=False)
        self.progress_log.update_stage(stage_name=self.stages[0], stage_percent=0, notes=notes)

    @skip_if_no_progress_log
    def restart(self, notes=None):
        self.progress_log.restart()
        self.display_notes(notes)
        self.start(notes=notes)

    @skip_if_no_progress_log
    def started(self):
        return self.progress_log.current_stage is not None

    @skip_if_no_progress_log
    def next_stage(self, notes=None):
        assert self.progress_log.current_stage is not None, "Must call start function before next_stage()"
        assert self.progress_log.current_stage < len(self.stages), "Must not be at the last stage already."
        self.display_notes(notes)
        self.progress_log.update_stage(stage_name=self.stages[self.progress_log.current_stage], stage_percent=0, notes=notes)

    @skip_if_no_progress_log
    def update_stage(self, stage_percent, stage_status=None, notes=None):
        """
        :param: stage_percent: 0.0 - 1.0 (so not actually percent!?)
        """
        self.display_notes(notes)
        self.progress_log.update_stage(stage_name=self.stages[self.progress_log.current_stage - 1], stage_percent=stage_percent, stage_status=stage_status, notes=notes)

    @skip_if_no_progress_log
    def cancel(self, stage_status=None, notes=None):
        self.display_notes(notes)
        self.progress_log.cancel_progress(stage_status=stage_status, notes=notes)

    @skip_if_no_progress_log
    def complete(self, notes=None):
        self.display_notes(notes)
        self.progress_log.mark_as_completed(notes=notes)


import numpy as np
from util import *

class Camera:
    ACTION_NONE = 0
    ACTION_ORBIT = 1
    ACTION_PAN = 2
    ACTION_ZOOM = 3

    def __init__(self):
        self.action = Camera.ACTION_NONE
        self.pitch = 45.0
        self.yaw = 0.0
        self.distance = 3.0
        self.target = (0.0, 0.0, 0.0)
        self.up = np.array([0.0, 1.0, 0.0])

    def compute_look_at(self):
        pos = self.camera_pos()
        l = look_at(pos, self.target, self.up)
        return l

    def camera_pos(self):
        rot_x = rot_mat_x(deg2rad(-self.pitch))
        rot_y = rot_mat_y(deg2rad(self.yaw))
        dir = np.matmul(np.matmul(rot_y, rot_x), np.array([0.0, 0.0, 1.0, 0.0]))[:3]
        pos = self.target + self.distance * dir

        return pos

    def modify_with_mouse_pos(self, dx, dy):
        if self.action == Camera.ACTION_ZOOM:
            self.distance -= dx * 0.05
            if self.distance < 0.1:
                self.distance = 0.1

        if self.action == Camera.ACTION_PAN:
            look_at = self.compute_look_at().T
            x = look_at[:3, 0]
            y = look_at[:3, 1]
            target = self.target + (-dx * x + dy * y) * 0.01
            self.target = (target[0], target[1], target[2])

        if self.action == Camera.ACTION_ORBIT:
            self.pitch += dy
            self.yaw -= dx
            if self.pitch > 89:
                self.pitch = 89
            elif self.pitch < -89:
                self.pitch = -89


# Central Limit Theorem I

# A large elevator can transport a maximum of 9800 pounds.
# Suppose a load of cargo containing 49 boxes must be transported via the elevator.
# The box weight of this type of cargo follows a distribution with a mean of 205 pounds and a standard deviation of 15 pounds.
# Based on this information, what is the probability that all 49 boxes can be safely loaded into the freight elevator and transported?

#!/bin/python3

import math

def pnorm(x, mean, std):
    return (0.5 * (1 + math.erf((x - mean) / (std * (2 ** 0.5)))))

if __name__ == '__main__':
	a = float(input()) #  maximum weight the elevator can transport
	b = float(input()) #  number of boxes in the cargo
	c = float(input()) #  mean weight of a cargo box
	d = float(input()) #  standard deviation of cargo box

	result = pnorm(a, b * c, math.sqrt(b) * d)
	print("{:.4f}".format(result))
	# 0.0098


# 模式的额外信息 - 例子
from typing import Optional

from fastapi import FastAPI, Body
from pydantic import BaseModel, Field

app = FastAPI()

# 使用 Config 与 schema_extra
# 在 Field, Path, Query, Body 以及之后将会看到的工厂函数可以为 JSON 模式声明额外信息，也可以通过给工厂函数传递其他的任意参数来给JSON 模式声明额外信息
# 传递的那些额外参数不会添加任何验证，只会添加注释，用于文档的目的。
class Item(BaseModel):
    name: str = Field(..., example="Foo")
    description: Optional[str] = Field(None, example="A very nice Item")
    price: float = Field(..., example=35.4)
    tax: Optional[float] = Field(None, example=3.2)

    class Config:
        schema_extra = {
            "example": {
                "name": "Foo",
                "description": "A very nice Item",
                "price": 35.4,
                "tax": 3.2,
            }
        }

@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

# Body 额外参数
# 可以通过传递额外信息给 Field 同样的方式操作 Path, Query, Body等
@app.put("/items2/{item_id}")
async def update_item2(
    item_id: int,
    item: Item = Body(
        ...,
        example={
            "name": "Foo",
            "description": "A very nice Item",
            "price": 35.4,
            "tax": 3.2,
        },
    ),
):
    results = {"item_id": item_id, "item": item}
    return results


""" Implement the UCC-like circuit exploring all basis states. """

import numpy as np
np.set_printoptions(3, suppress=True)
from numpy import pi as π

import scipy.stats
import scipy.linalg

import cirq
import openfermion

def statevector(t, α):
    """ Construct statevector from parameterization. """
    N = 1 + len(t)                              # NUMBER OF COMPLEX VALUES

    # FILL IN MODULI
    r = np.empty(N)
    Π_cos = 1
    for n in range(N-1):
        r[n]   = np.sin(π/2 * t[n]) * Π_cos
        Π_cos *= np.cos(π/2 * t[n])
    r[N-1] = Π_cos

    # FILL IN PHASE ANGLES
    γ = np.zeros(N)
    γ[1:] = 2*π * α

    # CONSTRUCT STATEVECTOR
    return r * np.exp(1j*γ)

def projector(n,µ,ν):
    """ Pauli sum for the projector |µ⟩⟨ν|. """
    from openfermion import QubitOperator as Pauli

    µ = np.array(list(np.binary_repr(µ, n)), dtype=int)
    ν = np.array(list(np.binary_repr(ν, n)), dtype=int)

    Π = Pauli(f'')
    for q in range(n):
        s = 0.5 * Pauli(f'')
        if ν[q]:        s *= Pauli(f'X{q}')
        if µ[q]!=ν[q]:  s *= Pauli(f'X{q}')
        s *= Pauli(f'') + Pauli(f'Z{q}')
        if ν[q]:        s *= Pauli(f'X{q}')

        Π *= s
    return Π

def projectormatrix(N,i,j):
    """ Return the compact basis matrix operator for a†[i] a[j]. """
    Π = np.zeros((N,N), dtype=complex)
    Π[i,j] = 1
    return Π

def generator(ϕ,l):
    """ Return the generator T[l] = ∑ ϕ[i] a†[i] a[l] as a matrix. """
    N = len(ϕ)
    T = np.zeros((N,N), dtype=complex)
    for i in range(l):
        T += projectormatrix(N,i,i)
    for i in range(l,N):
        T += ϕ[i] * projectormatrix(N,i,l)
    return T

def operator(t,α,l):
    """ Return the operator Ω[l] = exp[-𝑖π (T[l]+T[l]†)/2] as a matrix. """
    T = generator(statevector(t,α), l)
    H = (T + T.T.conjugate())/2

    # We need to subtract off constant from H to match quantum implementation,
    #   to eliminate an awkward global phase term we can't actually implement.
    N = len(t) + 1
    I = np.eye(N)
    H -= np.trace(H)/N * I

    return scipy.linalg.expm(-1j*π * H)

def hamiltonian(n, t, α, l):
    """ Pauli sum for the operator (T + T†)/2 """
    N = 2**n

    # FILL IN MODULI
    r = np.empty(N)
    Π_cos = 1
    for n in range(N-1):
        r[n]   = np.sin(π/2 * t[n]) * Π_cos
        Π_cos *= np.cos(π/2 * t[n])
    r[N-1] = Π_cos

    # FILL IN PHASE ANGLES
    γ = np.zeros(N)
    γ[1:] = 2*π * α

    H = 0
    for i in range(l):
        H += projector(n,i,i)
    for i in range(l,N):
        H += r[i]/2 * np.exp( 1j*γ[i]) * projector(n,i,l)
        H += r[i]/2 * np.exp(-1j*γ[i]) * projector(n,l,i)
    return H

def trottercircuit(qreg, H, r, τ):
    """ Construct Trotterized circuit for exp(-𝑖Hτ). """
    p = 2   # Suzuki "order". I see no good reason not to hard-code 2 here.

    # LOP OFF CONSTANT TERM (only different by a global phase)
    H = H - H.constant

    # CONSTRUCT THE CIRCUIT
    ct = cirq.Circuit()

    if H.isclose(openfermion.QubitOperator()):  return ct   # IDENTITY CIRCUIT

    for qubitop in openfermion.trotter_operator_grouping(H, r, p, k_exp=-τ):
        paulisum = openfermion.qubit_operator_to_pauli_sum(qubitop, qreg)
        op = cirq.ops.PauliSumExponential(paulisum)
        ct += op
    return ct

def pauliword(n, i):
    """ Construct the pauli word corresponding to integer i.

    Ordering is made by representing I->0, X->1, etc. and writing i in base 4.
    """
    # CONVERT i TO BASE 4 REPRESENTATION
    i4 = np.base_repr(i, 4)         # DECOMPOSE INTO BASE 4
    i4 = "0"*(n-len(i4)) + i4       # PAD 0's ON LEFT

    # CONSTRUCT PAULI WORD
    qreg = cirq.LineQubit.range(n)
    pauli = 1
    for q in range(n):
        if i4[q] == "0":  pauli *= cirq.PauliString( cirq.I(qreg[q]) )
        if i4[q] == "1":  pauli *= cirq.PauliString( cirq.X(qreg[q]) )
        if i4[q] == "2":  pauli *= cirq.PauliString( cirq.Y(qreg[q]) )
        if i4[q] == "3":  pauli *= cirq.PauliString( cirq.Z(qreg[q]) )
    return pauli


from django.urls import include, path, re_path
from rest_framework import routers

from .views import CollectStatisticView, CureViewSet, PhotoViewSet, ScheduleViewSet, TakeViewSet, TimeTableViewSet,CureViewDateSet

router = routers.DefaultRouter()
router.register('cure', CureViewSet, basename='cure')
router.register('cure_date', CureViewDateSet, basename='cure')
router.register('schedule', ScheduleViewSet, basename='schedule')
router.register('time', TimeTableViewSet, basename='time')
router.register('photos', PhotoViewSet, basename='photos')

data_pattern = "(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$"

urlpatterns = [
    path('stat/', CollectStatisticView.as_view()),
    path('take/cure/<int:pk>/', TakeViewSet.as_view()),
]
urlpatterns += router.urls


'''
Module containing the base type: TypeStr
'''

from ... import classes
from .. import utils

import re

class TypeStr(str, metaclass = classes.BuiltinMeta):
    '''
    Base Type: TypeStr.

    Base string type.

    Attributes:
        _pattern: Regular expression pattern used to extract data
    '''

    _pattern: str = '^(?P<data>.*)$'

    def __new__(cls: type, value: str) -> object:
        '''
        Create a new class instance.

        Args:
            cls: This class
            value: Value to initialise class with

        Returns:
            New class instance
        '''

        parsed = cls._parse(value)
        data   = parsed.get('data')

        if not parsed:
            raise TypeError \
            (
                'Invalid {class_name}: {value}'.format \
                (
                    class_name = cls.__name__,
                    value      = repr(str(value)),
                )
            )

        return super().__new__(cls, data)

    def __repr__(self: object) -> str:
        '''
        Create a string representation of the class.

        The representation is of the form: <{class_name}({value})>

        Args:
            self: Class instance

        Returns:
            String representation of the class
        '''

        return '<{class_name}({value})>'.format \
        (
            class_name = self.__class__.__name__,
            value      = repr(utils.truncate(str(self))),
        )

    @classmethod
    def _parse(cls: type, value: str) -> bool:
        '''
        Parse a string value to extract data.

        In no data is extracted, the value is not a valid instance of this type.

        Args:
            cls: This class
            value: String value to parse

        Returns:
            Values extracted during parsing

        Example:
            >>> TypeStr._parse('foo bar')
            {'data': 'foo bar'}
            >>>
        '''

        parsed = {}
        groups = {}

        if not isinstance(value, str) or not cls._pattern:
            return parsed

        match = re.match \
        (
            pattern = cls._pattern,
            string  = value,
            flags   = re.DOTALL,
        )

        if match:
            groups = match.groupdict()

            data = groups.get('data')

            if data:
                groups['data'] = cls._clean(data)

        return groups

    @classmethod
    def _isinstance(cls: type, value: str) -> bool:
        '''
        Check whether a string value is a valid instance of this type.

        Args:
            cls: This class
            value: String value to check

        Returns:
            Whether the value is a valid instance of this type

        Example:
            >>> class MyStrType(TypeStr):
            	_pattern = '^(?P<data>ABC.{3})$'

            >>>
            >>> MyStrType._isinstance('AAAfoo')
            False
            >>> MyStrType._isinstance('ABCfoo')
            True
            >>>
        '''

        parsed = cls._parse(value)
        data   = parsed.get('data')

        return bool(data) and data == value

    @classmethod
    def _clean(cls: type, value: str) -> str:
        '''
        Clean the data after it has been extracted.

        Args:
            cls: This class
            value: Data value to clean

        Returns:
            Cleaned data value

        Example:
            >>> class MyStrType(TypeStr):
            	_pattern = '^original-prefix(?P<data>.{3})$'

            	@classmethod
            	def _clean(cls, value):
            		return 'new-prefix' + value

            >>>
            >>> my_str = MyStrType('original-prefixABC')
            >>> my_str
            <MyStrType('new-prefixABC')>
            >>>
        '''

        return value