import os
import logging
import traceback
import pyrogram
#from pyrogram import
import ast
import asyncio
from config import Config
from pyrogram import types
from plugins import helper,Fonts,TextHandler,TextDecorator,Database
from pyrogram import Client, filters
from pyrogram.types import CallbackQuery, ChatPermissions, Message
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from pyrogram.errors import InputUserDeactivated, FloodWait, UserIsBlocked

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logging.getLogger("pyrogram").setLevel(logging.WARNING)

@Client.on_callback_query()
async def cb_data(bot, update):
  if update.data == "help":
    await update.message.edit_text(text=helper.HELPTEXT,reply_markup=helper.HELPBTN)
  if update.data == "abtdvlngbot":
    await update.message.edit_text(text=helper.BotAboutText.format(update.message.from_user.mention),reply_markup=helper.DVLGBTN)
  if update.data == "homeTostart":
    await update.message.edit_text(text=helper.STARTText.format(update.message.chat.first_name),reply_markup=helper.HOME_PAGE)
  if update.data == "MoreBots":
    await update.message.edit_text(text=helper.MoreBotsText.format(update.message.from_user.mention),reply_markup=helper.MoreBots_BTN)
  if update.data == "STARTFonting":
    await update.message.edit_text(text="Choose Your Methods",reply_markup=helper.STARTFontingBTN)
  if update.data == "CoolFonts":
    Fotnkeyboard = await Fonts.GenerateButtonForF9ntList(0)
    await update.message.edit_text(text="Choose Your Fonts",reply_markup=Fotnkeyboard)
  if update.data == "DecorateText":
    Designkeyboard = await TextDecorator.GenerateButtonForDecorate()
    await update.message.edit_text(text="Choose Your Design",reply_markup=Designkeyboard)
  if (update.data.startswith("['ChangePage'")):
    page = ast.literal_eval(update.data)[1]
    Fotnkeyboard = await Fonts.GenerateButtonForF9ntList(int(page))
    await update.edit_message_reply_markup(reply_markup=Fotnkeyboard)#inline_message_id = update.message.message_id,text="akhil")#reply_markup=helper.MoreBots_BTN)#InlineKeyboardMarkup([[InlineKeyboardButton("New button", callback_data="new_data")]]))
  if (update.data.startswith("['CF'")):
    Font_Name = ast.literal_eval(update.data)[1]
    Page_No = ast.literal_eval(update.data)[2]
    Fotnkeyboard = await Fonts.GenerateButtonForF9ntList(int(Page_No))
    TextToChange = await TextHandler.GetCurrentTextToStyle(update.message.chat.id)
    if str(TextToChange) =="Nonee":
      await update.message.reply_text("Send Some Text")
      await update.message.delete()
    else:
      TextWithFont = await Fonts.CreateFontFromText(TextToChange,Font_Name)
      await update.message.edit_text(text=f"{TextWithFont}",reply_markup=Fotnkeyboard)
  if (update.data.startswith("['DSGN'")):
    DesignNumber = ast.literal_eval(update.data)[1]
    Designkeyboard = await TextDecorator.GenerateButtonForDecorate()
    TextToChange = await TextHandler.GetCurrentTextToStyle(update.message.chat.id)
    if str(TextToChange) =="Nonee":
      await update.message.reply_text("Send Some Text")
      await update.message.delete()
    else:
      TextWithFont = await TextDecorator.DesignWithText(TextToChange,DesignNumber)
      await update.message.edit_text(text=f"{TextWithFont}",reply_markup=Designkeyboard)
  if update.data == "vrfyusers":
    values_list3,ttlusers = await Database.GetAllUsersList(bot, update)
    i=0
    j=0
    ak = "ak"
    msg = await update.message.reply_text(helper.usrststext.format(ttlusers,i,j))
    #print(values_list3)
    for p in values_list3:
      #print("1")
      try:
        Chtid = int(p)
        #print("2")
        await bot.send_chat_action(chat_id = Chtid, action=pyrogram.enums.ChatAction.TYPING)
        #print("3")
        i+=1
        await msg.edit(helper.usrststext.format(ttlusers,i,j))
      except FloodWait as e:
        await asyncio.sleep(e.x)
        await bot.send_chat_action(chat_id = int(p), action=pyrogram.enums.ChatAction.TYPING)
        i+=1
        await msg.edit(helper.usrststext.format(ttlusers,i,j))
        #return await broadcast_messages(user_id, message)
      except InputUserDeactivated:
        await Database.Clear_Cell(int(p))
        j+=1
        await msg.edit(helper.usrststext.format(ttlusers,i,j))
        #await db.delete_user(int(user_id))
        #logger.info(f"{user_id}-Removed from Database, since deleted account.")
        #return False, "Deleted"
      except UserIsBlocked:
        await Database.Clear_Cell(int(p))
        j+=1
        await msg.edit(helper.usrststext.format(ttlusers,i,j))
        #logger.info(f"{user_id} -Blocked the bot.")
        #return False, "Blocked"
      except Exception as e:
        await Database.Clear_Cell(int(p))
        print(traceback.format_exc())
        j+=1
        await msg.edit(helper.usrststext.format(ttlusers,i,j))
        print(e)
        error = f"{e}".split(":")[0]
        ak+=f"\n{p} {error}"
        #return False, "Error"

      #except Exception as e:
        #j+=1
      #  try:
    #      print(e)
      #  except Exception as ex:
   #       print(ex)
       #   ak+=f"\n{p} {ex}"
        #await msg.edit(helper.usrststext.format(ttlusers,i,j))
      await asyncio.sleep(1)
    try:
      await update.message.reply_text(f"{ak}")
    except:
      await update.message.reply_text(f"All Users are Active")


from data import *

def tambah():
	nama = input("Nama Kontak : ")
	nomor = input("Nomor Kontak : ")
	contact [nama] = nomor

def hapus():
	hapus = input("Nama yang ingin di hapus : ")
	if hapus in contact.keys():
		del contact[hapus]
	else:
		print("Tidak ada kontak yang bernama : ", hapus)

def daftar():
	for nama, nomor in contact.items():
		print(nama,nomor)

def cari():
	cari = input("Nama yang mau di cari : ")
	no= contact.get(cari)
	if cari in contact.keys():
		print(cari , "nomor nya adalah " , no)
	else:
		print("Kontak tidak terdaftar")


import data_jap as data
import danraku2 as model
danraku2 = model
import danraku3
import danraku4
import danraku5 # Juman++
import fake_sector
import utils as U
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import torch as t
import time

testld = data.Loader(data.Test_DS(), 1)
ld = data.Loader(data.Train_DS(), 8)
devld = data.Loader(data.Dev_DS(), 1)
ld2 = data.Loader(data.Train_DS(2), 8)
mini_testld = data.Loader(data.Test_DS_Mini(), 1)
mini_ld = data.Loader(data.Train_DS_Mini(), 8)

def run(m, epoch = 20, path = 'result.png', log_path = 'danraku.log', ld = ld, batch=8):
  U.init_logger(log_path)
  results = []
  losss = []
  recover_batch_size = ld.batch_size
  ld.batch_size = batch
  ld.start = 0
  print(f'Loader window size: {ld.ds.half_window_size}')
  for i in range(epoch):
    loss = U.train_by_data_loader_danraku(m, ld, 1, U.logging.debug)[0]
    losss.append(loss)
    outputs, targets = U.get_test_results(m, testld)
    prec, rec, f1, bacc = U.cal_prec_rec_f1_v2(outputs, targets)
    results.append((prec, rec, f1, bacc))
    print(f'epoch = {i}, loss = {loss}, {prec}, {rec}, {f1}')
  plot_prec_rec_f1_loss(results, losss, epoch, path)
  print('save to result.png')
  ld.batch_size = recover_batch_size
  return m, results, losss

def run_test(m, epoch = 1, path = 'result.png', log_path = 'danraku.log'):
  U.init_logger(log_path)
  results = []
  losss = []
  ld.start = 0
  for i in range(epoch):
    loss = U.train_by_data_loader_danraku(m, mini_ld, 1, U.logging.debug)[0]
    losss.append(loss)
    outputs, targets = U.get_test_results(m, mini_testld)
    prec, rec, f1, bacc = U.cal_prec_rec_f1_v2(outputs, targets)
    results.append((prec, rec, f1, bacc))
    print(f'epoch = {i}, loss = {loss}, {prec}, {rec}, {f1}')
  plot_prec_rec_f1_loss(results, losss, epoch, path)
  print('save to result.png')
  return m, results, losss

def run_plot_loss(epoch = 20):
  m = model.Model_Con()
  U.init_logger('danraku.log')
  train_loss = []
  test_loss = []
  for i in range(epoch):
    train_loss.append(U.train_by_data_loader_danraku(m, ld, 1, U.logging.debug)[0])
    test_loss.append(U.get_avg_loss(m, testld))
  plot_loss(epoch, train_loss, test_loss)
  print('save to loss_epoch.png')
  return m, train_loss, test_loss

def plot_loss(epoch, train_loss, test_loss):
  xs = [x+1 for x in list(range(epoch))]
  plt.clf()
  plt.plot(xs, train_loss, label= 'train loss')
  plt.plot(xs, test_loss, label= 'test loss')
  plt.legend()
  plt.savefig('result.png')

def plot_prec_rec_f1(results, loss, epoch= 20, path = 'result.png'):
  precs = []
  recs = []
  f1s = []
  baccs = []
  for prec, rec, f1, bacc in results:
    precs.append(prec)
    recs.append(rec)
    baccs.append(bacc)
    f1s.append(f1)
  # return precs, recs, f1s
  xs = [x+1 for x in list(range(epoch))]
  plt.plot(xs, precs, label= 'precs')
  plt.plot(xs, recs, label= 'recs')
  plt.plot(xs, f1s, label= 'f1s')
  plt.plot(xs, baccs, label= 'balanced acc')

def plot_prec_rec_f1_loss(results, loss, epoch= 20, path = 'result.png'):
  plt.clf()
  plot_prec_rec_f1(results, loss, epoch, path)
  xs = [x+1 for x in list(range(epoch))]
  plt.plot(xs, loss, label= 'loss')
  plt.legend()
  plt.savefig(path)

# 层级BERT，1 sentence per side
def run_cat_sentence():
  # m = t.load('save/bert_catsentence_epoch3.tch')
  m = danraku3.BERT_Cat_Sentence()
  _, results, losss = run(m, 4, 'bert_catsentence_epoch4_3vs1.png', 'danraku.log')
  t.save(m, 'save/bert_catsentence_1vs3.tch')
  print('run_cat_sentence from 1 -> 4 epoch over')
  return m, results, losss

def run_cross_seg():
  # m = t.load('save/bert_cross_seg_epoch3.tch')
  m = model.Model_Bert_Balanced_CE()
  _, results, losss = run(m, 4, 'bert_cross_seg_epoch4_3vs1.png', 'danraku.log')
  t.save(m, 'save/bert_cross_seg_1vs3.tch')
  print('run_cross_seg from 1 -> 4 epoch over')
  return m, results, losss

# 层级word2vec，1 sentence per side
def run_word2vec():
  m = t.load('save/wiki2vec.tch')
  _, results, losss = run(m, 5, 'wiki2vec_feb10_epoch10.png', 'word2vec.log')
  t.save(m, 'save/wiki2vec.tch')
  print('run_word2vec over')
  return m, results, losss

# 层级word2vec，1 sentence per side
def run_word2vec_2sentence_perside(m = danraku4.Model_Wiki2vec()):
  _, results, losss = run(m, 5, 'wiki2vec_feb7_sentence2_epoch8.png', 'word2vec.log', ld = ld2)
  t.save(m, 'save/wiki2vec_sentence2.tch')
  print('run_word2vec s2 over')
  return m, results, losss

def run_kuro(m = danraku5.BERT_Kuro()):
  _, results, losss = run(m, 3, 'save/kurobert_epoch3.png', 'kurobert.log', ld = ld)
  t.save(m, 'save/kurobert.tch')
  print('kurobert over')
  return m, results, losss

def run_kuro_catss(m = danraku5.Kuro_Catsentence()):
  _, results, losss = run(m, 3, 'save/kurobert_catss_epoch3.png', 'kurobert.log', ld = ld)
  t.save(m, 'save/kurobert_catss.tch')
  print('kurobert catss over')
  return m, results, losss

def run_fake_sector():
  m = fake_sector.FakeSector()
  _, results, losss = run(m, 10, 'save/fake_sector_epoch10.png', 'fake_sector.log', ld = ld)
  t.save(m, 'save/fake_sector.tch')
  print('Fake Sector over')

def run_feb10_night():
  # run_cross_seg()
  # run_cat_sentence()
  # run_kuro()
  run_fake_sector()
  run_kuro_catss()

def test_test(m):
  start = time.time()
  outputs, targets = U.get_test_results(m, testld)
  end = time.time()
  print(f'Time cost: {end - start} seconds')
  return outputs, targets

def test_dev(m):
  start = time.time()
  outputs, targets = U.get_test_results(m, devld)
  end = time.time()
  print(f'Time cost: {end - start} seconds')
  return outputs, targets


from apache_beam.io.gcp.bigquery_tools import table_schema_to_dict
from apache_beam.io.gcp.internal.clients.bigquery import TableFieldSchema

from datetime import datetime, timedelta

from pipe_segment.transform.read_source import ReadSource

from pipe_tools.io.bigquery import parse_table_schema
from pipe_tools.utils.timestamp import as_timestamp

import apache_beam as beam

PAD_PREV_DAYS = 1

def is_first_batch(pipeline_start, first_date):
    return pipeline_start == first_date

def offset_timestamp(dt, **timedelta_args):
    if dt is None:
        return dt
    dt_delta = dt + timedelta(**timedelta_args)
    return dt_delta

class ReadMessagesFromSeveralSources(beam.PTransform):
    def __init__(self, options):
        self.options = options
        datetimeFromStr = lambda x: datetime.strptime(x, '%Y-%m-%d')
        extract_range = lambda s: list(map(datetimeFromStr, s.split(','))) if s is not None else (None, None)
        self.date_range = extract_range(self.options.date_range)
        self.pipeline_start_date_dt = datetimeFromStr(self.options.pipeline_start_date)
        self._message_sources_list = None

    @property
    def message_sources_list(self):
        # message_sources_list is set only once and has ReadSource of each source.
        if not self._message_sources_list:
            first_date_dt, last_date_dt = self.date_range

            #If the first_date_dt is the first day of data, then, don't look back
            if not is_first_batch(self.pipeline_start_date_dt, first_date_dt):
                first_date_dt = offset_timestamp(first_date_dt, days=-PAD_PREV_DAYS)

            if self.options.look_ahead:
                last_date_dt = offset_timestamp(last_date_dt, days=self.options.look_ahead)

            first_date_ts, last_date_ts = list(map(as_timestamp, [first_date_dt, last_date_dt]))
            self._message_sources_list = [ReadSource(src_msg_table, first_date_ts, last_date_ts) for src_msg_table in self.options.source.split(',')]
        return self._message_sources_list

    def read_from_several_sources(self, pcoll):
        compose = lambda x,source: pcoll | f"Source{x}" >> source
        return [compose(idx, source) for idx, source in enumerate(self.message_sources_list)]

    @property
    def message_input_schema(self):
        schema = self.options.source_schema
        if schema is None:
            # no explicit schema provided. Try to find one in the source(s)
            schemas = [s.schema for s in self.message_sources_list if s.schema is not None]
            schema = schemas[0] if schemas else None
        return parse_table_schema(schema)

    @property
    def message_output_schema(self):
        schema = self.message_input_schema

        field = TableFieldSchema()
        field.name = "seg_id"
        field.type = "STRING"
        field.mode="NULLABLE"
        schema.fields.append(field)

        field = TableFieldSchema()
        field.name = "n_shipname"
        field.type = "STRING"
        field.mode="NULLABLE"
        schema.fields.append(field)

        field = TableFieldSchema()
        field.name = "n_callsign"
        field.type = "STRING"
        field.mode="NULLABLE"
        schema.fields.append(field)

        field = TableFieldSchema()
        field.name = "n_imo"
        field.type = "INTEGER"
        field.mode="NULLABLE"
        schema.fields.append(field)

        return table_schema_to_dict(schema)

    def expand(self, pcoll):
        return (
            self.read_from_several_sources(pcoll)
            | "MergeMessages" >> beam.Flatten()
        )


import numpy as np

class LinearRegression(object):
    def __init__(self):
        self.coef_ = None  # 回归系数
        self.intercept_ = None  # 截距

    def fit(self, X, y):
        # 在特征矩阵 X 的第一列添加一列全为1的列，以处理截距
        X = np.insert(X, 0, 1, axis=1)

        # 使用最小二乘法求解回归系数
        X_transpose = np.transpose(X)
        self.coef_ = np.linalg.inv(X_transpose.dot(X)).dot(X_transpose).dot(y)

        # 提取截距和系数
        self.intercept_ = self.coef_[0]
        self.coef_ = self.coef_[1:]

    def predict(self, X):
        # 在特征矩阵 X 的第一列添加一列全为1的列，以处理截距
        X = np.insert(X, 0, 1, axis=1)

        # 使用拟合的系数进行预测
        y_pred = X.dot(np.insert(self.coef_, 0, self.intercept_))
        return y_pred


import sys
import collections
input = sys.stdin.readline #빠른 입력
def search(arr,start,end):
    root=arr[start]
    for i in range(start+1,end+1):

        if arr[i]>root:
            return i

def postorder(arr,start,end):
    if start>=end:
        return
    upperindex=search(arr,start,end)
    #print(upperindex)

    postorder(arr,start+1,upperindex-1)

    postorder(arr,upperindex,end)
    print(arr[start])

arr=[]
while True:
    try:
        num=int(input())
        arr.append(num)
    except:
        break
print('arr:',arr)
postorder(arr,0,len(arr)-1)


import haiku as hk
import jax.numpy as jnp

class Adjust(hk.Module):
    def __init__(self, base, scale_init=1e-4, bias_init=0., name=None):
        super().__init__(name=name)
        self.base = base
        self._scale_init = scale_init
        self._bias_init = bias_init

    def __call__(self, inputs):
        # Parameters
        scale_init = hk.initializers.Constant(self._scale_init)
        scale = hk.get_parameter('scale', (), inputs.dtype, init=scale_init)

        bias_init = hk.initializers.Constant(self._bias_init)
        bias = hk.get_parameter('bias', (), inputs.dtype, init=bias_init)

        if self.base == 1:
            return inputs * scale + bias
        else:
            return inputs * (self.base ** scale) + self.base ** bias - 1


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

"""
    Author Profiling Project:
        src:
            tweet_level_trainer.py
"""

import logging
# ============================ Third Party libs ============================
import os

import numpy as np
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
from pytorch_lightning.loggers import CSVLogger
from sklearn.utils import class_weight
from transformers import T5Tokenizer

# ============================ My packages ============================
from configuration import BaseConfig
from data_loader import read_csv
from dataset import DataModule
from indexer import Indexer
from models.t5_irony import Classifier

logging.basicConfig(level=logging.DEBUG)

if __name__ == "__main__":
    # ---------------------- create config instance -------------------------------
    CONFIG_CLASS = BaseConfig()
    CONFIG = CONFIG_CLASS.get_config()

    # --------------------- create CSVLogger instance ------------------------------
    LOGGER = CSVLogger(save_dir=CONFIG.saved_model_path, name=CONFIG.model_name)

    # ------------------------------ create LM Tokenizer instance-------------------
    TOKENIZER = T5Tokenizer.from_pretrained(CONFIG.language_model_path)

    # ---------------------------------- Loading datasets ---------------------------
    TRAIN_DATA = read_csv(path=os.path.join(CONFIG.processed_data_dir,
                                            CONFIG.train_data),
                          columns=CONFIG.data_headers,
                          names=CONFIG.customized_headers)
    VAL_DATA = read_csv(path=os.path.join(CONFIG.processed_data_dir,
                                          CONFIG.val_data),
                        columns=CONFIG.data_headers,
                        names=CONFIG.customized_headers)
    TEST_DATA = read_csv(path=os.path.join(CONFIG.processed_data_dir,
                                           CONFIG.test_data),
                         columns=CONFIG.data_headers,
                         names=CONFIG.customized_headers)

    logging.debug(f"We have {len(TRAIN_DATA) + len(VAL_DATA) + len(TEST_DATA)} tweets in our data.")

    logging.debug(f"We have {len(TRAIN_DATA)} tweets in train data.")
    logging.debug(f"We have {len(VAL_DATA)} tweets in validation data.")
    logging.debug(f"We have {len(TEST_DATA)} tweets in test data.")

    # ---------------------------------- Indexer ---------------------------------
    TARGET_INDEXER = Indexer(vocabs=list(TRAIN_DATA.targets))
    TARGET_INDEXER.build_vocab2idx()
    TARGET_INDEXER.save(path=CONFIG.assets_dir)

    TRAIN_TARGETS_CONVENTIONAL = [[target] for target in list(TRAIN_DATA.targets)]
    TRAIN_INDEXED_TARGET = TARGET_INDEXER.convert_samples_to_indexes(TRAIN_TARGETS_CONVENTIONAL)

    VAL_TARGETS_CONVENTIONAL = [[target] for target in list(VAL_DATA.targets)]
    VAL_INDEXED_TARGET = TARGET_INDEXER.convert_samples_to_indexes(VAL_TARGETS_CONVENTIONAL)

    TEST_TARGETS_CONVENTIONAL = [[target] for target in list(TEST_DATA.targets)]
    TEST_INDEXED_TARGET = TARGET_INDEXER.convert_samples_to_indexes(TEST_TARGETS_CONVENTIONAL)

    # Calculate class_weights
    class_weights = class_weight.compute_class_weight(
        "balanced",
        classes=np.unique([item for sublist in TRAIN_INDEXED_TARGET for item in sublist]),
        y=np.array([item for sublist in TRAIN_INDEXED_TARGET for item in sublist]))

    # ------------------------------- Make DataLoader Dictionaries --------------------------------
    TRAIN_COLUMNS2DATA = {"texts": list(TRAIN_DATA.texts),
                          "targets": TRAIN_INDEXED_TARGET}
    VAL_COLUMNS2DATA = {"texts": list(VAL_DATA.texts),
                        "targets": VAL_INDEXED_TARGET}
    TEST_COLUMNS2DATA = {"texts": list(TEST_DATA.texts),
                         "targets": TRAIN_INDEXED_TARGET}

    DATA = {"train_data": TRAIN_COLUMNS2DATA,
            "val_data": VAL_COLUMNS2DATA, "test_data": TEST_COLUMNS2DATA}

    # ----------------------------- Create Data Module ------------------------------
    DATA_MODULE = DataModule(data=DATA, config=CONFIG, tokenizer=TOKENIZER)
    DATA_MODULE.setup()
    # ----------------------------- Create Trainer ----------------------------------
    CHECKPOINT_CALLBACK = ModelCheckpoint(monitor="val_loss",
                                          filename="QTag-{epoch:02d}-{val_loss:.2f}",
                                          save_top_k=CONFIG.save_top_k,
                                          mode="min")

    EARLY_STOPPING_CALLBACK = EarlyStopping(monitor="val_loss", patience=5)

    # Instantiate the Model Trainer
    TRAINER = pl.Trainer(max_epochs=CONFIG.n_epochs, gpus=[1],
                         callbacks=[CHECKPOINT_CALLBACK, EARLY_STOPPING_CALLBACK],
                         progress_bar_refresh_rate=60, logger=LOGGER)

    # --------------------------- Create classifier object ---------------------------
    MODEL = Classifier(num_classes=len(TARGET_INDEXER.get_vocab2idx()),
                       lm_path=CONFIG.language_model_path, lr=CONFIG.lr, max_len=CONFIG.max_len,
                       class_weights=class_weights)

    # ---------------------------- Train and Test Model -------------------------------
    TRAINER.fit(MODEL, datamodule=DATA_MODULE)
    TRAINER.test(ckpt_path="best", datamodule=DATA_MODULE)


import re
from proc_tests import KernelProcFileTestBase

class ProcModulesTest(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/modules contains information about loaded kernel modules.'''

    def prepare_test(self, dut):
        api_level = dut.getLaunchApiLevel(strict=False)
        self.require_module = False
        return True

    def parse_contents(self, contents):
        module_present = False
        # MODULE_NAME SIZE REFERENCE_COUNT USER1,USER2, STATE BASE_ADDRESS TAINT_FLAG
        # MODULE_NAME is a string
        # SIZE is an integer
        # REFERENCE_COUNT is an integer or -
        # USER1,USER2, is a list of modules using this module with a trailing comma.
        #   If no modules are using this module or if modules cannot be unloaded then
        #   - will appear. If this mdoule cannot be unloaded then [permanent] will be
        #   the last entry.
        # STATE is either Unloading, Loading, or Live
        # BASE_ADDRESS is a memory address
        # TAINT_FLAG is optional and if present, has characters between ( and )
        test_re = re.compile(r"^\w+ \d+ (\d+|-) (((\w+,)*(\[permanent\],)?)|-) (Unloading|Loading|Live) 0x[0-9a-f]+( \(\w+\))?")
        for line in contents.splitlines():
            if not re.match(test_re, line):
                raise SyntaxError("Malformed entry in /proc/modules: %s" % line)
            else:
                module_present = True
        if self.require_module and not module_present:
            raise SyntaxError("There must be at least one entry in /proc/modules")

        return ''

    def result_correct(self, parse_result):
        return True

    def get_path(self):
        return "/proc/modules"


import tensorflow as tf

def Tokenizer(train_captions):
    top_k = 6000
    tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words = top_k,
                                                    oov_token = "<unk>",
                                                    filters='!"#$%&()*+.,-/:;=?@[\]^_`{|}~ ')

    tokenizer.fit_on_texts(train_captions)
    tokenizer.word_index['<pad>'] = 0
    tokenizer.index_word[0] = '<pad>'
    train_seqs = tokenizer.texts_to_sequences(train_captions)
    cap_vector = tf.keras.preprocessing.sequence.pad_sequences(train_seqs , padding = 'post')

    return tokenizer


import tkinter as tk
from tkinter import ttk
import calendar

class GraphicalCalendar(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Graphical Calendar")
        self.geometry("500x500")
        self.calendar = calendar.TextCalendar()

        self.selected_date = None
        self.create_widgets()

    def create_widgets(self):
        self.cal_frame = ttk.Frame(self)
        self.cal_frame.pack(pady=20)

        self.month_label = ttk.Label(self.cal_frame, text="Select Month:")
        self.month_label.grid(row=0, column=0)

        self.month_var = tk.StringVar(self)
        self.month_combobox = ttk.Combobox(
            self.cal_frame,
            textvariable=self.month_var,
            values=[
                "January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December"
            ],
        )
        self.month_combobox.grid(row=0, column=1)

        self.year_label = ttk.Label(self.cal_frame, text="Select Year:")
        self.year_label.grid(row=0, column=2)

        self.year_var = tk.StringVar(self)
        self.year_combobox = ttk.Combobox(
            self.cal_frame,
            textvariable=self.year_var,
            values=list(range(1900, 2100))
        )
        self.year_combobox.grid(row=0, column=3)

        self.show_button = ttk.Button(
            self.cal_frame,
            text="Show Calendar",
            command=self.show_calendar
        )
        self.show_button.grid(row=0, column=4)

        self.calendar_text = tk.Text(self, wrap=tk.WORD, height=20, width=30)
        self.calendar_text.pack(pady=10)

    def show_calendar(self):
        month = self.month_combobox.current() + 1
        year = int(self.year_var.get())

        self.selected_date = (year, month)

        self.calendar_text.delete(1.0, tk.END)
        self.calendar_text.insert(tk.END, self.calendar.formatmonth(year, month))

if __name__ == "__main__":
    app = GraphicalCalendar()
    app.mainloop()


# Exercise 6-7 of the book "Python Crash Course"

people = []

person = {
	'first_name': 'david',
	'last_name': 'smith',
	'age': 45,
	'city': 'new york',
	}
people.append(person)

person = {
	'first_name': 'vicent',
	'last_name': 'wilhem',
	'age': 30,
	'city': 'chicago',
	}
people.append(person)

person = {
	'first_name': 'john',
	'last_name': 'landrum',
	'age': 50,
	'city': 'san jose',
	}
people.append(person)

for person in people:
	full_name = person['first_name'] + " " + person['last_name']
	age = person['age']
	city = person['city']
	print(f"{full_name.title()} is {age} and living in {city.title()}.")


from trading.trader.order import OrderConstants
from trading.trader.portfolio import Portfolio
from tools.timestamp_util import convert_timestamp_to_datetime

class PrettyPrinter:
    @staticmethod
    def open_order_pretty_printer(order):
        currency, market = order.get_currency_and_market()

        try:
            order_type_name = order.get_order_type().name
        except AttributeError:
            try:
                order_type_name = OrderConstants.TraderOrderTypeClasses[order.get_order_type()].__name__
            except KeyError:
                order_type_name = order.get_order_type().__class__.__name__

        return "{0}: {1} {2} at {3} {4} on {5}: {6} ".format(
            order_type_name,
            PrettyPrinter.get_min_string_from_number(order.get_origin_quantity()),
            currency,
            PrettyPrinter.get_min_string_from_number(order.get_origin_price()),
            market,
            order.get_exchange().get_name(),
            convert_timestamp_to_datetime(order.get_creation_time()))

    @staticmethod
    def trade_pretty_printer(trade):
        currency = trade.get_currency()
        market = trade.get_market()

        try:
            order_type_name = trade.get_order_type().name
        except AttributeError:
            try:
                order_type_name = OrderConstants.TraderOrderTypeClasses[trade.get_order_type()].__name__
            except KeyError:
                order_type_name = trade.get_order_type().__class__.__name__

        return "{0}: {1} {2} at {3} {4} on {5}: {6} ".format(
            order_type_name,
            PrettyPrinter.get_min_string_from_number(trade.get_quantity()),
            currency,
            PrettyPrinter.get_min_string_from_number(trade.get_price()),
            market,
            trade.get_exchange_name(),
            convert_timestamp_to_datetime(trade.get_filled_time()))

    @staticmethod
    def cryptocurrency_alert(crypto_currency, symbol, result, final_eval):
        return "OctoBot ALERT : #{0}\n Symbol : #{1}\n Result : {2}\n Evaluation : {3}".format(
            crypto_currency,
            symbol.replace("/", ""),
            str(result).split(".")[1],
            final_eval)

    @staticmethod
    def global_portfolio_pretty_print(global_portfolio, separator="\n"):
        result = ["{0} ({1}) {2}".format(
            PrettyPrinter.get_min_string_from_number(amounts[Portfolio.TOTAL]),
            PrettyPrinter.get_min_string_from_number(amounts[Portfolio.AVAILABLE]),
            currency)
            for currency, amounts in global_portfolio.items() if amounts[Portfolio.TOTAL] > 0]

        return separator.join(result)

    @staticmethod
    def portfolio_profitability_pretty_print(profitability, profitability_percent, reference):
        difference = "({0}%)".format(PrettyPrinter.get_min_string_from_number(profitability_percent, 5)) \
            if profitability_percent is not None else ""
        return "{0} {1} {2}".format(PrettyPrinter.get_min_string_from_number(profitability, 5),
                                    reference, difference)

    @staticmethod
    def get_min_string_from_number(number, max_digits=8):
        if round(number, max_digits) == 0.0:
            return "0"
        else:
            if number % 1 != 0:
                number_str = "{:.{}f}".format(round(number, max_digits), max_digits)
                if "." in number_str:
                    number_str = number_str.rstrip("0.")
                return number_str
            else:
                return "{:f}".format(number).split(".")[0]


from copy import copy
from unittest import TestCase

from alfred_json.rewriter import IdentifiableDataTypeAggregator, Rewriter, \
    NestedRewriter
from alfred_json.type import IdentifiableDataType
from alfred_rest.tests import RestTestCase

class IdentifiableDataTypeAggregatorTest(TestCase):
    def testRewriteWithoutDataTypes(self):
        sut = IdentifiableDataTypeAggregator()
        original_schema = {
            'id': 'https://example.com/schema',
            'foo': {
                '$ref': 'https://example.com/schema#',
            },
            'bar': {
                'oneOf': [
                    {
                        '$schema': 'https://example.com/schema#'
                    },
                ],
            },
        }
        rewritten_schema = copy(original_schema)
        sut.rewrite(original_schema)
        self.assertEqual(original_schema, rewritten_schema)

    def testRewriteSimpleDataType(self):
        class FooDataType(IdentifiableDataType):
            def __init__(self):
                super().__init__('Foo')

            def get_json_schema(self):
                return {
                    'type': 'float',
                }
        original_schema = {
            'id': 'https://example.com/schema',
            'foo': FooDataType(),
        }
        expected_schema = {
            'id': 'https://example.com/schema',
            'foo': {
                '$ref': '#/definitions/data/Foo',
            },
            'definitions': {
                'data': {
                    'Foo': {
                        'type': 'float',
                    },
                },
            },
        }
        sut = IdentifiableDataTypeAggregator()
        rewritten_schema = sut.rewrite(original_schema)
        self.assertEqual(rewritten_schema, expected_schema)

    def testRewriteNestedDataTypes(self):
        class FooDataType(IdentifiableDataType):
            def __init__(self):
                super().__init__('Foo')

            def get_json_schema(self):
                return {
                    'type': 'float',
                }

        class BazDataType(IdentifiableDataType):
            def __init__(self):
                super().__init__('Baz')

            def get_json_schema(self):
                return {
                    'type': 'string',
                }
        original_schema = {
            'id': 'https://example.com/schema',
            'foo': FooDataType(),
            'bar': {
                'type': 'object',
                'properties': {
                    'baz': BazDataType(),
                },
            },
        }
        expected_schema = {
            'id': 'https://example.com/schema',
            'foo': {
                '$ref': '#/definitions/data/Foo',
            },
            'bar': {
                'type': 'object',
                'properties': {
                    'baz': {
                        '$ref': '#/definitions/data/Baz',
                    },
                },
            },
            'definitions': {
                'data': {
                    'Foo': {
                        'type': 'float',
                    },
                    'Baz': {
                        'type': 'string',
                    },
                },
            },
        }
        sut = IdentifiableDataTypeAggregator()
        rewritten_schema = sut.rewrite(original_schema)
        self.assertEqual(rewritten_schema, expected_schema)

    def testRewriteDuplicateDataTypes(self):
        class FooDataType(IdentifiableDataType):
            def __init__(self):
                super().__init__('Foo')

            def get_json_schema(self):
                return {
                    'type': 'float',
                }
        data_type = FooDataType()
        original_schema = {
            'id': 'https://example.com/schema',
            'foo': data_type,
            'foo2': data_type,
        }
        expected_schema = {
            'id': 'https://example.com/schema',
            'foo': {
                '$ref': '#/definitions/data/Foo',
            },
            'foo2': {
                '$ref': '#/definitions/data/Foo',
            },
            'definitions': {
                'data': {
                    'Foo': {
                        'type': 'float',
                    },
                },
            },
        }
        sut = IdentifiableDataTypeAggregator()
        rewritten_schema = sut.rewrite(original_schema)
        self.assertEqual(rewritten_schema, expected_schema)

    def testRewriteExistingDefinition(self):
        class FooDataType(IdentifiableDataType):
            def __init__(self):
                super().__init__('Foo')

            def get_json_schema(self):
                return {
                    'type': 'float',
                }
        data_type_foo = FooDataType()

        class BarDataType(IdentifiableDataType):
            def __init__(self):
                super().__init__('Bar')

            def get_json_schema(self):
                return {
                    'type': 'array',
                    'items': data_type_foo,
                }
        data_type_bar = BarDataType()

        class BazDataType(IdentifiableDataType):
            def __init__(self):
                super().__init__('Baz')

            def get_json_schema(self):
                return {
                    'type': 'array',
                    'items': data_type_bar,
                }
        data_type_baz = BazDataType()

        original_schema = {
            'id': 'https://example.com/schema',
            'definitions': {
                'data': {
                    'Bar': data_type_bar,
                    'Baz': data_type_baz,
                },
            },
        }
        expected_schema = {
            'id': 'https://example.com/schema',
            'definitions': {
                'data': {
                    'Foo': {
                        'type': 'float',
                    },
                    'Bar': {
                        'type': 'array',
                        'items': {
                            '$ref': '#/definitions/data/Foo',
                        },
                    },
                    'Baz': {
                        'type': 'array',
                        'items': {
                            '$ref': '#/definitions/data/Bar',
                        },
                    },
                },
            },
        }
        sut = IdentifiableDataTypeAggregator()
        rewritten_schema = sut.rewrite(original_schema)
        self.assertEqual(rewritten_schema, expected_schema)

class ExternalReferenceProxyTest(RestTestCase):
    ORIGINAL_EXTERNAL_POINTER = 'http://json-schema.org/draft-04/schema#'
    REWRITTEN_EXTERNAL_POINTER = 'http://alfred.local/about/json/external-schema/aHR0cDovL2pzb24tc2NoZW1hLm9yZy9kcmFmdC0wNC9zY2hlbWE%3D'
    ALFRED_POINTER = 'http://alfred.local/about/json/schema#definitions/data/Foo'
    INTERNAL_POINTER = '#/definitions/data/Bar'

    def testRewritePointerWithNonStringShouldPassThrough(self):
        pointer = {}
        sut = self._app.service('rest', 'external_reference_proxy')
        self.assertEqual(sut.rewrite_pointer(pointer), pointer)

    def testRewritePointerWithAlfredPointerShouldPassThrough(self):
        sut = self._app.service('rest', 'external_reference_proxy')
        self.assertEqual(sut.rewrite_pointer(
            self.ALFRED_POINTER), self.ALFRED_POINTER)

    def testRewritePointerWithInternalPointerShouldPassThrough(self):
        sut = self._app.service('rest', 'external_reference_proxy')
        self.assertEqual(sut.rewrite_pointer(
            self.INTERNAL_POINTER), self.INTERNAL_POINTER)

    def testRewritePointerWithExternalPointerShouldBeRewritten(self):
        sut = self._app.service('rest', 'external_reference_proxy')
        self.assertEqual(sut.rewrite_pointer(
            self.ORIGINAL_EXTERNAL_POINTER), self.REWRITTEN_EXTERNAL_POINTER)

    def testRewrite(self):
        original_schema = {
            'id': self.ORIGINAL_EXTERNAL_POINTER,
            'foo': {
                '$ref': self.ORIGINAL_EXTERNAL_POINTER,
            },
            'bar': {
                '$ref': self.ALFRED_POINTER,
            },
            'baz': {
                '$ref': self.INTERNAL_POINTER,
            },
            'qux': {
                'oneOf': [
                    {
                        '$schema': self.ORIGINAL_EXTERNAL_POINTER
                    },
                ],
            },
        }
        expected_schema = {
            'id': self.REWRITTEN_EXTERNAL_POINTER,
            'foo': {
                '$ref': self.REWRITTEN_EXTERNAL_POINTER,
            },
            'bar': {
                '$ref': self.ALFRED_POINTER,
            },
            'baz': {
                '$ref': self.INTERNAL_POINTER,
            },
            'qux': {
                'oneOf': [
                    {
                        '$schema': self.REWRITTEN_EXTERNAL_POINTER
                    },
                ],
            },
        }
        sut = self._app.service('rest', 'external_reference_proxy')
        rewritten_schema = sut.rewrite(original_schema)
        self.assertEqual(rewritten_schema, expected_schema)

class NestedRewriterTest(RestTestCase):
    class DogRewriter(Rewriter):
        def rewrite(self, schema):
            schema['required'].append('woof')
            return schema

    class CatRewriter(Rewriter):
        def rewrite(self, schema):
            schema['required'].append('meow')
            return schema

    def testRewrite(self):
        sut = NestedRewriter()
        sut.add_rewriter(self.DogRewriter())
        sut.add_rewriter(self.CatRewriter())
        original_schema = {
            'type': 'object',
            'required': [],
        }
        expected_schema = {
            'type': 'object',
            'required': ['woof', 'meow'],
        }
        rewritten_schema = sut.rewrite(original_schema)
        self.assertEqual(rewritten_schema, expected_schema)


import csv

class Phones:
    def __init__(self, path):
        self.path = path

    def list_numbers(self):
        with open(self.path, 'r') as file:
            lines = file.readlines()
            number_list = [i.split(';')[3].strip() for i in lines[1:]]
            return number_list

    @staticmethod
    def remove_prefix(num_list: list):
        return [i.removeprefix('38') for i in num_list]

    @staticmethod
    def del_zero(num_list: list):
        return [i.removeprefix('0') if i.startswith('00') else i for i in num_list]

    @staticmethod
    def del_duplicates(num_list: list):
        return set(num_list)

    @staticmethod
    def invalid_number(num_list: list):
        return [i for i in num_list if len(i) != 10]

    @staticmethod
    def find_intersections(path_first, path_second):
        with open(path_first, 'r') as file1:
            lines = file1.readlines()
            phone_list1 = [i.split(';;;')[1].removesuffix(';;\n') for i in lines]
        phone_list2 = Phones(path_second).list_numbers()
        return list(set(phone_list1) & set(phone_list2))[1:]

    @staticmethod
    def inner_values_rec(path_first, path_second):
        with open(path_first, 'r') as file1:
            lines = file1.readlines()
            phone_list1 = [i.split(';;;')[1].removesuffix(';;\n') for i in lines if
                           i.split(';;;')[1].removesuffix(';;\n').startswith('380')]
        phone_list2 = Phones(path_second).list_numbers()
        values = list(set(phone_list1) - set(phone_list2))[1:]
        with open(f'individual_elements_{path_first}', 'w', encoding='utf-8') as csvfile:
            file_writer = csv.DictWriter(csvfile, delimiter=',', lineterminator="\r", fieldnames=['Phone'])
            file_writer.writeheader()
            for item in values:
                file_writer.writerow({'Phone': item})

    @staticmethod
    def outer_values_rec(path_first, path_second):
        with open(path_first, 'r') as file1:
            lines = file1.readlines()
            phone_list1 = [i.split(';;;')[1].removesuffix(';;\n') for i in lines if
                           i.split(';;;')[1].removesuffix(';;\n').startswith('380')]
        phone_list2 = Phones(path_second).list_numbers()
        values = list(set(phone_list2) - set(phone_list1))[1:]
        with open(f'individual_elements_{path_second}', 'w', encoding='utf-8') as csvfile:
            file_writer = csv.DictWriter(csvfile, delimiter=',', lineterminator="\r", fieldnames=['Phone'])
            file_writer.writeheader()
            for item in values:
                file_writer.writerow({'Phone': item})

    @staticmethod
    def sort_operators(first_phone_list: list, second_phone_list: list):
        combine_list = first_phone_list + second_phone_list

        def add_dash(rand_list, quantity_dash):
            return rand_list + quantity_dash * ['-']

        kyivstar = []
        vodafone = []
        lifecell = []
        other = []
        for item in combine_list:
            if item[:3] in ['067', '068', '096', '097', '098']:
                kyivstar.append(item)
            elif item[:3] in ['050', '066', '095', '099']:
                vodafone.append(item)
            elif item[:3] in ['063', '073', '093']:
                lifecell.append(item)
            else:
                other.append(item)
        with open('Phones_table.md', 'w') as new_file:
            max_len = len(max(kyivstar, vodafone, lifecell, other))
            k = add_dash(kyivstar, max_len - len(kyivstar))
            v = add_dash(vodafone, max_len - len(vodafone))
            l = add_dash(lifecell, max_len - len(lifecell))
            o = add_dash(other, max_len - len(other))
            temp = '## Table of Phones\n'
            temp += '|{:^20}|{:^20}|{:^20}|{:^20}|\n'.format('Kyivstar', 'Vodafone', 'Lifecell', 'Other')
            temp += '|{0:^20}|{0:^20}|{0:^20}|{0:^20}|\n'.format('-' * 20)
            for i in range(max_len):
                temp += '|{:^20}|{:^20}|{:^20}|{:^20}|\n'.format(k[i], v[i], l[i], o[i])
            new_file.write(temp)

    @staticmethod
    def add_to_file(phone_list: list, filename):
        with open(filename, 'a') as file:
            for i in phone_list:
                file.write(i + '\n')

file = Phones('text.txt')
phones = file.list_numbers()
san_phones = file.remove_prefix(phones)
zer_er = file.del_zero(san_phones)
set_numbers = file.del_duplicates(zer_er)
invalid_numbers = file.invalid_number(zer_er)
print(Phones.find_intersections('bot.csv', 'in_zohot.csv'))
Phones.inner_values_rec('bot.csv', 'in_zohot.csv')
Phones.outer_values_rec('bot.csv', 'in_zohot.csv')
p = []
l = []
with open('individual_elements_bot.csv', 'r') as f:
    lines = f.readlines()[1:]
    san_lines = Phones.remove_prefix([i.strip() for i in lines])
    p.extend(san_lines)

with open('individual_elements_in_zohot.csv', 'r') as file:
    lines1 = file.readlines()[1:]
    san_lines1 = Phones.remove_prefix([i.strip() for i in lines1])
    l.extend(san_lines1)

Phones.sort_operators(p, l)

Phones.add_to_file(p, 'example.csv')


import cv2
import numpy as np

# https://docs.opencv.org/3.1.0/df/d9d/tutorial_py_colorspaces.html
# http://colorizer.org/
img = cv2.imread("OpenCV-Python Tutorials/nemo0.jpg")

red = np.uint8([[[0, 0, 255]]])  # red in bgr
hsv_red = cv2.cvtColor(red, cv2.COLOR_BGR2HSV)
print(hsv_red)

# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# define range of blue color in HSV
lower_red = np.array([0, 100, 100])
upper_red = np.array([10, 255, 255])

# Threshold the HSV image to get only red colors
mask = cv2.inRange(hsv, lower_red, upper_red)

# Bitwise-AND mask and original image
res = cv2.bitwise_and(img, img, mask=mask)

cv2.imshow("frame", img)
cv2.imshow("mask", mask)
cv2.imshow("res", res)

cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV-Python Tutorials/1_Image Processing/1_color spaces.py


"""METHODS
Core functions of the app
"""

# # Native # #
import random

# # Installed # #
import yaml

# # Project # #
from ideology_mashup.entities import Ideology, Ideologies, Strings

def parse_ideologies(file_path: str = "ideologies.yaml") -> Ideologies:
    """Parse the ideologies from the given YAML file path.
    :return: list of Ideology objects
    """
    ideologies: Ideologies = list()

    with open(file_path, "r") as file:
        file_content = file.read()

    file_parsed = yaml.safe_load(file_content)
    if file_parsed:
        parsed_ideologies = file_parsed.get("ideologies")

        if parsed_ideologies:
            for parsed_ideology in parsed_ideologies:
                ideologies.append(Ideology(**parsed_ideology))

    return ideologies

def mashup_one(prefix: str, name: str):
    """Join a prefix and a name split by a hyphen
    """
    return prefix.capitalize() + "-" + name.capitalize()

def mashup_ideologies(ideologies: Ideologies, shuffle: bool = False) -> Strings:
    """Mashup a given list of Ideology objectsm, returning all possible combinations.
    For a combination of 2 ideologies to be valid, one must have a name and the other one a prefix.
    :return: list of strings
    """
    mashups: Strings = list()

    for ideology_base in ideologies:
        for ideology_prefix in ideologies:
            name = ideology_base.name
            prefix = ideology_prefix.prefix
            if ideology_base is not ideology_prefix and name and prefix:
                mashups.append(mashup_one(prefix=prefix, name=name))

    if shuffle:
        random.shuffle(mashups)

    return mashups

def write_strings(strings: Strings, file_path: str = "mashed_ideologies.txt"):
    """Write the given list of strings to a file (one string per line)
    """
    with open(file_path, "w") as file:
        for string in strings:
            file.write(string + "\n")


import collections.abc
import textwrap
from typing import Sequence, List, Dict, Iterable, Optional, Tuple, Union

import PIL.Image
import PIL.ImageEnhance
import numpy as np
import torch
import torchvision as tv
from torchvision.transforms.functional import to_pil_image
from matplotlib import pyplot as plt
from torch.utils.data import Subset, RandomSampler
from tqdm import tqdm

from .activations_handle import ActivationDatasetWrapper
from .base import BaseDataset

def to_img(tens: torch.Tensor) -> PIL.Image.Image:
    """Transform a (cuda) tensor to a :py:class:`PIL.Image.Image`."""
    trafo = tv.transforms.ToPILImage()
    return trafo(tens.cpu())

def mean_proportion_pos_px(masks: Sequence[torch.Tensor]):
    """From the given samples, calculate the mean of the proportion of
    positive pixels per mask.
    Assuming mask to be binary.
    """
    prop_per_mask: List[torch.Tensor] = [mask.sum() / mask.numel()
                                         for mask in masks]
    # pylint: disable=no-member
    # pylint: disable=not-callable
    prop: float = float(torch.sum(torch.tensor(prop_per_mask)) / len(masks))
    # pylint: enable=no-member
    # pylint: enable=not-callable
    return prop

def visualize_mask_transforms(
        titled_datasets: Dict[str, ActivationDatasetWrapper],
        save_as: str = None,
        max_num_samples: int = 3) -> None:
    """Visualize difference between original and the transformed masks of
    datasets.
    Visualization results will be shown using
    :py:func:`matplotlib.pyplot.imshow` functionality and are optionally
    saved to a file.

    :param titled_datasets: mapping of ``title`` to ``dataset``
        to be visualized;
        datasets must have the attribute/property ``dataset`` holding the
        original dataset in same indexing order;
        the data contained in each dataset must be a sequence yielding tuples of
        ``(any, transformed mask)``;
        ``data.dataset`` must be a sequence yielding tuples of
        ``(original image, original mask)``;
    :param save_as: file path to save the image under using
        :py:func:`matplotlib.pyplot.savefig`; not saved if ``None``
    :param max_num_samples: the maximum number of samples to show
        for each ``dataset``
    """
    fig = plt.figure(figsize=(3 * max_num_samples * 2,
                              3 * len(titled_datasets)))
    axes = fig.subplots(len(titled_datasets), max_num_samples * 2,
                        sharey='row', squeeze=False)
    fig.tight_layout(h_pad=2)
    for i, (row_title, data) in enumerate(titled_datasets.items()):
        axes[i, 0].set_ylabel(row_title)
        # Collect sample pics
        pics = []
        for ax_idx in range(min(max_num_samples, len(data))):
            _, mask_t = data[ax_idx]
            # Train data is a Subset instance
            assert not isinstance(data, Subset), \
                "'Subset' requires index adaption"
            img_t, orig_mask_t = data.dataset[ax_idx]
            img = to_img(img_t)
            # TODO: support visualization of stacked masks
            if not (isinstance(mask_t, torch.Tensor) and 2 <= len(mask_t.size()) <= 3):
                continue
            mask = to_img(mask_t).resize(img.size, resample=PIL.Image.BOX)
            if isinstance(orig_mask_t, torch.Tensor) and 2 <= len(orig_mask_t.size()) <= 3:
                orig_mask = to_img(orig_mask_t).resize(img.size, resample=PIL.Image.BOX)
                applied_masks = apply_mask(apply_mask(img, mask),
                                        orig_mask, alpha=0.4, color='yellow')
                pics.append(("Masked img {}; green=new, yellow=orig".format(ax_idx),
                             applied_masks))
            pics.append(("Mask {}".format(ax_idx), mask))

        for ax_idx, (title, pic) in enumerate(pics):
            axes[i, ax_idx].set_title('\n'.join(textwrap.wrap(title, 30)))
            axes[i, ax_idx].imshow(pic)

    if save_as is not None:
        plt.savefig(save_as, transparent=True)

def visualize_segmentation_data(dataset: BaseDataset, save_as: str = None,
                                max_num_samples: int = 5,
                                skip_none: bool = True, skip_empty: bool = True,
                                shuffle: bool = False):
    """Visualize a dataset yielding tuples of the form ``(input, target_mask)``.

    Both ``input`` and ``target_mask`` must be images as
    :py:class:`torch.Tensor` of the same width and height.

    :param dataset: the dataset to visualize
    :param save_as: file path to save the image under using
        :py:func:`matplotlib.pyplot.savefig`; not saved if ``None``
    :param max_num_samples: the maximum number of samples to show for each
        ``dataset``
    :param skip_none: whether to ignore when a None value in input
        or target is encountered
    :param skip_empty: whether to ignore samples with all-black target masks
    :param shuffle: whether to randomly select samples from the dataset
    """
    num_samples = min(len(dataset), max_num_samples)
    fig, axes = plt.subplots(2, num_samples,
                             figsize=(4 * num_samples, 4 * 2), dpi=100,
                             sharey='row', squeeze=False)
    fig.tight_layout(h_pad=2)

    # row titles
    axes[0, 0].set_ylabel("input")
    axes[1, 0].set_ylabel("target")

    # plot samples
    sampler: Iterable = RandomSampler(dataset) \
        if shuffle else range(len(dataset))
    num_selected_samples: int = 0
    for i in sampler:
        img_t, mask_t = dataset[i]
        # possibly skip sample
        if skip_none and (img_t is None or mask_t is None):
            continue
        if skip_empty and mask_t.sum() == 0:
            continue

        img: PIL.Image.Image = to_img(img_t)
        inverted_mask: PIL.Image.Image = \
            to_img(1 - mask_t).resize(img.size, resample=PIL.Image.BOX)
        applied_mask: PIL.Image.Image = \
            apply_mask(img, inverted_mask, alpha=1, color='black')

        axes[0][num_selected_samples].imshow(img)
        axes[1][num_selected_samples].imshow(applied_mask)

        num_selected_samples += 1
        if num_selected_samples == num_samples:
            break

    if save_as is not None:
        plt.savefig(save_as, transparent=True)

def visualize_classification_data(dataset: BaseDataset, save_as: str = None,
                                  max_num_samples: int = 5,
                                  skip_none: bool = True,
                                  shuffle: bool = False):
    """Visualize a dataset yielding tuples of the form
    ``(input, target_class_identifier)``.
    The ``input`` must be an image as :py:class:`torch.Tensor`.

    :param dataset: the dataset to visualize
    :param save_as: file path to save the image under using
        :py:func:`matplotlib.pyplot.savefig`; not saved if ``None``
    :param max_num_samples: the maximum number of samples to show
        for each ``dataset``
    :param skip_none: whether to ignore samples where a None value in input
        or target is encountered
    :param shuffle: whether to randomly select samples from the dataset
    """
    num_samples = min(len(dataset), max_num_samples)
    fig, axes = plt.subplots(1, num_samples,
                             figsize=(4 * num_samples, 4), dpi=100,
                             sharey='row')
    fig.tight_layout(h_pad=2)

    # plot samples
    sampler: Iterable = RandomSampler(dataset) \
        if shuffle else range(len(dataset))
    num_selected_samples: int = 0
    for i in sampler:
        img_t, ann = dataset[i]
        # possibly skip sample
        if skip_none and (img_t is None or ann is None):
            continue

        axes[num_selected_samples].imshow(to_img(img_t))
        axes[num_selected_samples].set_title(
            '\n'.join(textwrap.wrap(str(ann), 45)))

        num_selected_samples += 1
        if num_selected_samples == num_samples:
            break

    if save_as is not None:
        plt.savefig(save_as, transparent=True)

def neg_pixel_prop(data, max_num_samples: Optional[int] = 10,
                   show_progress_bar: bool = False) -> float:
    """Collect the mean proportion of negative pixels in the binary
    segmentation mask data.
    The proportion is estimated from the first ``max_num_samples`` samples
    in the dataset. Set the value to ``None`` to take into account all
    samples in the dataset.

    :param data: a :py:class:`typing.Sequence` that yields tuples of
        input (arbitrary) and
        masks (as :py:class:`torch.Tensor`).
    :param max_num_samples: the maximum number of samples to take into
        account for the estimation
    :param show_progress_bar: whether to show a progress bar for loading the
        masks
    :return: the proportion of negative pixels in all (resp. the first
        ``num_samples``) binary segmentation masks contained in the given data
    """
    num_samples: int = len(data) if max_num_samples is None else \
        min(len(data), max_num_samples)
    iterator = range(num_samples)
    if show_progress_bar:
        iterator = tqdm(iterator, desc="Masks loaded")
    masks = []
    for i in iterator:
        masks.append(data[i][1])
    return 1 - mean_proportion_pos_px(masks)

def apply_mask(img: PIL.Image.Image, mask: PIL.Image.Image,
               color: str = 'green', alpha: float = 0.8) -> PIL.Image.Image:
    """Apply monochrome (possibly non-binary) mask to image of same size
    with alpha value.
    The positive parts of the mask are colored in color and added to the image.

    :param img: image
    :param mask: mask (mode ``'L'`` or ``'1'``)
    :param color: color to use for masked regions as argument to Image.new()
    :param alpha: alpha value in ``[0,1]`` to darken mask:

        :0: black,
        :<1: darken,
        :1: original

    :return: ``RGB`` :py:class:`PIL.Image.Image` with mask applied
    """
    if alpha < 0:
        raise ValueError(("Alpha value {} is invalid for darkening"
                          "(must be in [0,1])".format(alpha)))
    if alpha > 1:
        raise ValueError(("Alpha value {} is invalid for darkening (must be in "
                          "[0,1]): Would brighten black areas").format(alpha))

    if mask.size != img.size:
        raise ValueError("Mask size {} and image size {} do not match."
                         .format(mask.size, img.size))

    # The background for mixing
    base_background: PIL.Image.Image = PIL.Image.new(mode='RGB', size=mask.size,
                                                     color=color)

    # Make sure mask has mode 'L':
    mask_l = mask.convert('L')
    # Apply alpha
    enh: PIL.ImageEnhance.Brightness = PIL.ImageEnhance.Brightness(mask_l)
    mask_l = enh.enhance(alpha)

    applied_mask: PIL.Image.Image = PIL.Image.composite(base_background, img,
                                                        mask_l)
    return applied_mask

def apply_masks(img: PIL.Image.Image, masks: Sequence[Union[torch.Tensor, PIL.Image.Image]],
                colors: Sequence[str] = ('blue', 'red', 'yellow', 'cyan'),
                alphas: Union[float, Sequence[float]] = 0.8) -> PIL.Image.Image:
    if not isinstance(alphas, collections.abc.Iterable):
        alphas = [alphas] * len(masks)
    assert len(colors) >= len(masks)
    assert len(alphas) == len(masks)
    masks = _to_pil_masks(*masks)

    for color, alpha, mask in zip(colors, alphas, masks):
        img = apply_mask(img, mask, color=color, alpha=alpha)

    return img

def to_monochrome_img(img_t: torch.Tensor) -> PIL.Image.Image:
    """:py:class:`torch.Tensor` to monochrome :py:class:`PIL.Image.Image` in
    ``'L'`` (=8-bit) mode.

    :param img_t: monochrome image as tensor of size ``[height, width]``
    :return: resized and darkened image of ``mode='L'``
    """
    img_t_denormalized = (img_t * 255).int()
    img = tv.transforms.ToPILImage()(img_t_denormalized).convert('L')
    return img

def compare_masks(*masks: Union[torch.Tensor, PIL.Image.Image],
                  colors: Sequence[str] = ('blue', 'red', 'yellow', 'cyan')) -> PIL.Image.Image:
    """Merge several monochrome masks in different colors into the same image.

    :param masks: monochrome PIL images (model ``'L'`` or ``'1'``),
        ``RGB`` mode PIL images, or torch tensors;
        torch tensors are converted to ``'L'`` mode PIL images
    :return: image with bright part of each mask in corresponding color
    """
    assert len(colors) >= len(masks)
    colors: Iterable[str] = iter(colors)
    pil_masks: Sequence[PIL.Image.Image] = _to_pil_masks(*masks)

    colored_masks: List[PIL.Image.Image] = [
        apply_mask(
            PIL.Image.new(size=mask.size, mode='RGB'),
            mask, alpha=1, color=next(colors))
        if mask.mode != 'RGB' else mask
        for mask in pil_masks]

    # noinspection PyTypeChecker
    mask_comparison = PIL.Image.fromarray(
        np.clip(np.sum([np.array(mask) for mask in colored_masks], axis=0), a_min=0, a_max=255).astype(np.uint8),
        mode='RGB')
    return mask_comparison

def _to_pil_masks(*masks: Union[torch.Tensor, PIL.Image.Image]) -> Tuple[PIL.Image.Image, ...]:
    assert len(masks) > 0
    for mask in [m for m in masks if isinstance(m, torch.Tensor)]:
        assert len(mask.size()) == 2 or (len(mask.size()) == 3 and mask.size()[0] == 1), \
            "Encountered mask tensor with more than one channel of shape {}".format(mask.shape)
    pil_masks: List[PIL.Image.Image] = [
        mask if isinstance(mask, PIL.Image.Image) else to_pil_image(mask, mode='L')
        for mask in masks]
    # region Quick sizes check
    for idx, mask in enumerate(pil_masks):
        if not mask.size == pil_masks[0].size:
            raise ValueError(("Mask at index {} has size {} (w x h), which differs from"
                              " mask at index 0 of size {} (w x h)"
                              ).format(idx, mask.size, masks[0].size))
    # endregion
    return pil_masks


import os
import subprocess
import re
from datetime import datetime
from flask import Blueprint, request, render_template, jsonify
from a2dapp.modals.creds import get_credentials
from a2dapp.routes.auth import login_required
from a2dapp.routes.certs import read_ssl, a2d_self_ssl, a2d_rm_cassl, a2d_ca_ssl, a2d_ca_list

dns_routes = Blueprint('dns', __name__)

# Define the directory paths
NGINX_CONFIG_PATH = '/etc/nginx/sites-available/a2dapp'
SSL_CERT_PATH = '/etc/nginx/ssl/a2d-ssl.crt'
SSL_KEY_PATH = '/etc/nginx/ssl/a2d-ssl.key'

#DNS data to HTML
@dns_routes.route('/local-dns-setting', methods=['GET'])
@login_required
def local_dns_setting():
    creds = get_credentials()  # Need this to display Navbar_full in dns.html

    #SSL status and server name
    current_listen_port = read_nginx_config('listen')
    if current_listen_port == 'ssl': #Strip from nginx conifg file
        current_listen_port = '443'
    current_server_name = read_nginx_config('server_name')
    current_ssl_status = current_listen_port == '443'
    a2d_default_dns = (current_listen_port == '9331' and current_server_name == '_' and not current_ssl_status)

    #Self SSL details
    output = read_ssl(SSL_CERT_PATH)
    stored_selfcommon_name, stored_selforganization_name, _ = output.split(',')
    if stored_selfcommon_name == '':
        stored_selfcommon_name = 'None'
    if stored_selforganization_name == '':
        stored_selforganization_name = 'None'

    #Get cassl list
    cassl_certs_list = list_cassl_certs()

    return render_template(
        'dns.html',
        listen_port=current_listen_port,
        server_name=current_server_name,
        current_ssl_status=current_ssl_status,
        a2d_default_dns=a2d_default_dns,
        stored_selfcommon_name=stored_selfcommon_name,
        stored_selforganization_name=stored_selforganization_name,
        cassl_certs=cassl_certs_list,
        creds=creds
    )

#Set server config
@dns_routes.route('/server-config', methods=['POST'])
@login_required
def server_config():
    set_selfssl_status = 'disable'
    set_cassl_status = 'disable'
    cassl_certs_list = list_cassl_certs()

    listen_port = request.form['listen_port']
    server_name = request.form['server_name']
    enable_self_ssl = request.form.get('enable_ssl') == 'true'
    set_cassl_certs = request.form['set_cassl_certs']
    enable_ca_ssl = request.form.get('enable_cassl') == 'true'
    set_default_dns = request.form.get('set_default_dns') == 'true'

    if enable_self_ssl and not enable_ca_ssl:
        # Check if the self-signed certificate and key files exist
        if (os.path.exists(SSL_CERT_PATH) and os.path.exists(SSL_KEY_PATH)):
            set_selfssl_status = 'enable'
        else:
            return "Create SSL"

    elif enable_ca_ssl and not enable_self_ssl:
        if set_cassl_certs in cassl_certs_list:
            if set_cassl_certs == server_name:
                set_cassl_status = 'enable'
            elif set_cassl_certs == '':
                return "Create caSSL"
            else:
                set_cassl_status = 'enable'
                server_name = set_cassl_certs
        else:
            return "Create caSSL"

    elif listen_port == '443' and not enable_self_ssl and not set_default_dns:
        if not (os.path.exists(SSL_CERT_PATH) and os.path.exists(SSL_KEY_PATH)):
            return "Create SSL"
        else:
            set_selfssl_status = 'enable'

    elif listen_port == '443' and set_default_dns:
        listen_port = '9331'
        set_selfssl_status = 'disable'
        server_name = '_'

    elif set_default_dns:
        listen_port = '9331'
        set_selfssl_status = 'disable'
        server_name = '_'

    else:
        pass

    # Update the Nginx configuration
    update_nginx_config(listen_port, server_name, set_selfssl_status, set_cassl_status)
    reload_nginx()

    if set_selfssl_status == 'enable':
        return "SSL enabled"
    elif set_cassl_status == 'enable':
        return "caSSL enabled"
    elif listen_port == '9331' and set_selfssl_status == 'disable' and server_name == '_':
        return "Set default"
    else:
        return "Server updated"

#Update nginx config
def update_nginx_config(listen_port, server_name, set_selfssl_status, set_cassl_status):
    try:
        with open(NGINX_CONFIG_PATH, 'r') as config_file:
            lines = config_file.readlines()

        if server_name != '_' or '':
            CASSL_CERT_PATH = f'/etc/letsencrypt/live/{server_name}/fullchain.pem'
            CASSL_KEY_PATH = f'/etc/letsencrypt/live/{server_name}/privkey.pem'
        else:
            CASSL_CERT_PATH, CASSL_KEY_PATH = 'None', 'None'

        updated_lines = []

        ssl_line = "listen 443 ssl;"
        self_ssl_line1 = f"ssl_certificate {SSL_CERT_PATH};"
        self_ssl_line2 = f"ssl_certificate_key {SSL_KEY_PATH};"
        ca_ssl_line1 = f"ssl_certificate {CASSL_CERT_PATH};"
        ca_ssl_line2 = f"ssl_certificate_key {CASSL_KEY_PATH};"

        updated_listen = False  # Flag to keep track of whether the "listen" line has been updated

        for line in lines:
            if line.strip().startswith("listen "):
                if not updated_listen:  # Only add one "listen" line
                    if set_selfssl_status == 'enable':
                        updated_lines.append(f"    {ssl_line}\n    {self_ssl_line1}\n    {self_ssl_line2}\n")
                    elif set_cassl_status == 'enable':
                        updated_lines.append(f"    {ssl_line}\n    {ca_ssl_line1}\n    {ca_ssl_line2}\n")
                    else:
                        updated_lines.append(f"    listen {listen_port};\n")
                    updated_listen = True
            elif line.strip().startswith("ssl_certificate"):
                if set_selfssl_status != 'enable' or set_cassl_status != 'enable':
                    # Skip these lines if self-signed SSL is not enabled
                    continue
            elif line.strip().startswith("server_name "):
                updated_lines.append(f"    server_name {server_name};\n")
            else:
                updated_lines.append(line)

        with open(NGINX_CONFIG_PATH, 'w') as config_file:
            config_file.writelines(updated_lines)

    except Exception as e:
        return f"Error updating nginx conf: {e}"

#Reload nginx
def reload_nginx():
    try:
        subprocess.run(["systemctl", "reload", "nginx"], check=True)
    except subprocess.CalledProcessError as e:
        return "Error reloading nginx:", e

#Generate self-signed SSL
@dns_routes.route('/gen-self-signed-ssl', methods=['POST'])
@login_required
def gen_self_signed_ssl():
    common_name = request.form["common_name"]
    validity_days = int(request.form["validity_days"])
    organization_name = request.form["organization_name"]
    self_ssl_result = a2d_self_ssl(common_name, validity_days, organization_name)
    if self_ssl_result == "sSSL generated":
        reload_nginx()
        return "sSSL generated"
    else:
        return "Error generating SSL"

#Generate CN signed SSL
@dns_routes.route('/gen-ca-ssl', methods=['POST'])
@login_required
def gen_ca_ssl():
    ca_common_name = request.form["ca_common_name"]
    email_id = request.form["email_id"]
    cassl_certs_list = list_cassl_certs()

    if ca_common_name not in cassl_certs_list:
        if is_valid_email(email_id):
            ca_ssl = a2d_ca_ssl(ca_common_name, email_id)
            if ca_ssl == "caSSL generated":
                reload_nginx()
                return "caSSL generated"
            else:
                return "Error generating SSL"
        else:
            return "invalid email"
    else:
        return "caSSL exist"

#Email syntax check
def is_valid_email(email):
    pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
    if re.match(pattern, email):
        return True
    else:
        return False

#Remove CN signed SSL
@dns_routes.route('/rm-ca-ssl', methods=['POST'])
@login_required
def rm_ca_ssl():
    rm_cassl_certs = request.form['rm_cassl_certs']
    cassl_certs_list = list_cassl_certs()
    current_server_name = read_nginx_config('server_name')

    if rm_cassl_certs in cassl_certs_list:
        if rm_cassl_certs != current_server_name:
            rm_cassl = a2d_rm_cassl(rm_cassl_certs)
            if rm_cassl == "caSSL removed":
                reload_nginx()
                return "caSSL removed"
            else:
                return "Error removing SSL"
        else:
            return "caSSL in use"
    else:
        return "No caSSL"

#Get the list of CA SSL
def list_cassl_certs():
    result = a2d_ca_list()
    return result

#HTML data display
@dns_routes.route('/for-html', methods=['GET'])
@login_required
def for_html():
    # Read the current DNS configuration values
    current_listen_port = read_nginx_config('listen')
    if current_listen_port == 'ssl':
        current_listen_port = '443'
    current_server_name = read_nginx_config('server_name')
    current_SSL_crt = read_nginx_config('ssl_certificate')

    if current_SSL_crt:
        CASSL_CERT_PATH = f'/etc/letsencrypt/live/{current_server_name}/fullchain.pem'
    else:
        CASSL_CERT_PATH = 'None'

    if current_SSL_crt == SSL_CERT_PATH:
        current_ssl_status = 'Self-Signed'
    elif current_SSL_crt == CASSL_CERT_PATH:
        current_ssl_status = 'CA Validated'
    else:
        current_ssl_status = 'Disabled'

    #Read SSL certificate status
    if current_SSL_crt:
        output = read_ssl(current_SSL_crt)
        common_name, organization_name, expiry_date = output.split(',')

        # Handle empty values by replacing them with None
        if common_name == '':
            common_name = 'None'
        if organization_name == '':
            organization_name = 'None'
        if expiry_date == '':
            expiry_date = 'None'

        current_date = datetime.now()
        current_date =current_date.strftime('%Y-%m-%d %H:%M:%S')

        if expiry_date and expiry_date < current_date:
            expiry_date = 'Expired'
    else:
        common_name, organization_name, expiry_date = 'None', 'None', 'None'

    #Read service status
    nginx_status = get_ctl_status('nginx')
    gunicorn_status = get_ctl_status('a2d')

    response_data = {
        "listen_port": current_listen_port,
        "server_name": current_server_name,
        "current_ssl_status": current_ssl_status,
        "common_name": common_name,
        "organization_name": organization_name,
        "expiry_date": expiry_date,
        "nginx_status": nginx_status,
        "gunicorn_status": gunicorn_status
    }

    return jsonify(response_data)

#Read nginx config
def read_nginx_config(key):
    try:
        with open(NGINX_CONFIG_PATH, 'r') as config_file:
            for line in config_file:
                if line.strip().startswith(f"{key} "):
                    return line.split()[-1].rstrip(';')
    except Exception as e:
        return f"Error reading nginx conf: {e}"

#Get systemctl status
def get_ctl_status(ctl_name):
    try:
        output = subprocess.check_output(['systemctl', 'status', ctl_name], text=True)
        ctl_status = re.search(r'since\s(.*)', output).group(1)
        ctl_status = ctl_status.split(";")[1].strip()
        ctl_status = str(ctl_status)
        return ctl_status

    except subprocess.CalledProcessError as e:
        ctl_status = str(e)
        return ctl_status


from django.shortcuts import render, redirect
from . import models
from django.contrib import messages
from .models import Show

def default(request):
    return redirect('/shows')

def index(request):
    context={
    "showAll":models.index()
    }

    return render (request , "show.html" , context)

def new(request):
    return render(request, "create.html")

def create(request):
    errors = Show.objects.basic_validator(request.POST)
    if len(errors) > 0:
        for key , value in errors.items():
            messages.error(request , value)
        return redirect('/shows/new')
    else :
        models.creat(request)
        return redirect(f'/shows/{models.Show.objects.last().id}' )

def show(request, id):
    context = {
        "show": models.show(id)
    }
    return render(request, 'show specific.html', context)

def show_edit(request,id):
    context = {
        "show": models.show(id)
    }
    return render (request ,"edit.html",context )

def edit(request):
    x= request.POST['edit']
    errors = Show.objects.basic_validator(request.POST)
    if len(errors) > 0:
        for key , value in errors.items():
            messages.error(request , value)
        return redirect(f'/shows/{models.Show.objects.get(id=x).id}/edit')
    else:
        models.edit( request ,x )
        return redirect(f'/shows/{models.Show.objects.get(id=x).id}')

def destroy(request, num):
    models.destroy_show(num)
    return redirect('/shows')

Django/Semi-Restful TV Shows Validated/Semi_Restful_TV_Shows/app_one/views.py


## Builtin
import pathlib
import traceback
## Builtin: gui
import tkinter.filedialog as tkfiledialog, tkinter.messagebox as tkmessagebox
## Custom Module
from alcustoms.tkinter import advancedtkinter, smarttkinter
## This Module
from BRDSolution.inventory import constants, sql, methods as imethods
from BRDSolution.inventory.QuickBooks.costsfromreport import gui,classes,methods

class MainController(advancedtkinter.StackingManager):
    def __init__(self,parentpane, parent=None, eventmanager=None):
        super().__init__(parentpane=parentpane, parent = parent, eventmanager=eventmanager)
        self.eventmanager.bind("<<StackModified>>",self.checkreturn)

    def startup(self):
        con = GetFilesController(parent=self,parentpane= self.parentpane,eventmanager=self.eventmanager)
        self.addqueue(con)
        con.startup()

    def checkreturn(self,*event):
        if not self.stack:
            self.returntoparent()

    def returntoparent(self):
        self.parent.dequeue(self)

class GetFilesController(advancedtkinter.Controller):
    def __init__(self, pane=gui.GetFilesPane, parent=None, parentpane=None, eventmanager=None):
        super().__init__(pane=pane,parent=parent,parentpane=parentpane,eventmanager=eventmanager)

        p = self.pane
        p.databasebutton.configure(command = self.getdatabase)
        p.databaseentry.state(["disabled",])
        p.quickbooksbutton.configure(command = self.getquickbooks)
        p.quickbooksentry.state(["disabled",])

        p.continuebutton.configure(command = self.loadconversion)
        p.cancelbutton.configure(command = self.canceltoparent)

    def canceltoparent(self):
        self.parent.dequeue(self)

    def loadconversion(self):
        p = self.pane
        database,quickbooks = p.databaseentry.get().strip(), p.quickbooksentry.get().strip()
        if any(not file for file in [database,quickbooks,]):
            tkmessagebox.showerror('Missing File',"All fields are required. Please supply any missing files.")
            return
        con = MissingController(dict(database=database,quickbooks=quickbooks),parent=self.parent,parentpane=self.parentpane,eventmanager=self.eventmanager)
        self.parent.addqueue(con)
        con.startup()

    def getdatabase(self):
        file = tkfiledialog.askopenfilename(filetypes = (["Database",(".db",".sqlite3")],))
        if not file: return
        file = imethods.testfileintegrity(file)
        if not isinstance(file,pathlib.Path):
            tkmessagebox.showerror("Invalid File",f"Could not load file: please try loading another file. (error code: {file})")
        else:
            smarttkinter.setentry(self.pane.databaseentry,file)
    def getquickbooks(self):
        file = tkfiledialog.askopenfilename(filetypes = (["Excel File",(".xls",".xlsx")],))
        if not file: return
        file = imethods.testfileintegrity(file)
        if not isinstance(file,pathlib.Path):
            tkmessagebox.showerror("Invalid File",f"Could not load file: please try loading another file. (error code: {file})")
        else:
            smarttkinter.setentry(self.pane.quickbooksentry,file)

class MissingController(advancedtkinter.Controller):
    CLIPBOARDNONE = "<<__NONE__>>"
    def __init__(self,files,pane=gui.MissingPane,parent=None,parentpane=None,eventmanager=None):
        super().__init__(pane=pane,parent=parent,parentpane=parentpane,eventmanager=eventmanager)
        self.files = files
        self.clipboard = self.CLIPBOARDNONE

        p = self.pane
        p.matchitembutton.configure(command = self.openmatchdialog)
        p.searchvar.trace('w',self.filteritems)
        p.treeview.bind("<Double-Button-1>",self.openmatchdialog)
        p.treeview.bind("<Control-c>",self.copydatabaseid)
        p.treeview.bind("<Control-v>",self.pastedatabaseid)
        p.treeview.bind("<Delete>",self.cleardatabaseid)
        p.continuebutton.configure(command = self.loadresults)
        p.cancelbutton.configure(command = self.canceltoparent)

    def cleanup(self):
        self.files = None
        return super().cleanup()

    def startup(self):
        self.database = self.eventmanager.database = sql.InventoryDatabase(file=self.files['database'])
        self.itemstable = self.database.tables['items']
        self.report = classes.Report(self.files['quickbooks'])

        ## Find missing
        for key,item in self.report.items.items():
            itemid = item.itemid
            results = self.itemstable.filter(item={"=":itemid})
            ## If item is unambiguous
            if len(results) == 1:
                item.databaseid = results[0]['itemid']

        self.showallresults()

    def showallresults(self):
        self.pane.treeview.clear()
        self.loaditems(self.report.items)

    def loaditems(self,items):
        for key,item in items.items():
            self.pane.treeview.insert('',"end",key,values=(item.date.strftime("%m/%d/%Y"),item.itemid,item.memo,item.databaseid))

    def loadresults(self):
        databaseitems = [item for item in self.report.items.values() if item.databaseid]
        con = ExportController(databaseitems,parent=self.parent,parentpane = self.parentpane, eventmanager = self.eventmanager)
        self.parent.addqueue(con)
        con.startup()

    def canceltoparent(self):
        self.parent.dequeue(self)

    def openmatchdialog(self,*event):
        sel = self.pane.treeview.selection()
        if not sel:
            tkmessagebox.showerror("No Item Select","You have not selected a Database Item to match.")
            return
        sel = int(sel[0])
        item = self.report.items[sel]
        match = gui.MatchItemPopup(self.pane,sel,item,self.database.tables['items'])
        if not match: return
        return self.setitemmatch(sel,match)

    def setitemmatch(self,itemkey,databasematch):
        item = self.report.items[int(itemkey)]
        item.databaseid = databasematch
        self.pane.treeview.item(itemkey,values = (item.date.strftime("%m/%d/%Y"),item.itemid,item.memo,item.databaseid))

    def filteritems(self,*event):
        p = self.pane
        search = p.searchvar.get().strip().lower()
        if not search: self.showallresults()
        p.treeview.clear()
        results = {key:item for key,item in self.report.items.items() if search in "".join([str(item.itemid).lower(),str(item.memo).lower(),str(item.databaseid).lower()])}
        self.loaditems(results)

    def copydatabaseid(self,*event):
        key = self.pane.treeview.selection(count=1)
        if not key: self.clipboard = self.CLIPBOARDNONE
        else:
            item = self.report.items[int(key)]
            self.clipboard = item.databaseid

    def pastedatabaseid(self,*event):
        if self.clipboard == self.CLIPBOARDNONE: return
        sel = self.pane.treeview.selection()
        if not sel: return
        for key in sel:
            self.setitemmatch(key,self.clipboard)

    def cleardatabaseid(self,*event):
        key = self.pane.treeview.selection(count=1)
        if not key: return
        self.setitemmatch(key,None)

class ExportController(advancedtkinter.Controller):
    def __init__(self,items,pane=gui.ResultPane,parent=None,parentpane=None,eventmanager=None):
        super().__init__(pane=pane,parent=parent,parentpane=parentpane,eventmanager=eventmanager)
        self.items = items

        p = self.pane
        p.continuebutton.configure(command = self.parent.returntoparent)

    def cleanup(self):
        self.items = None
        return super().cleanup()

    def startup(self):
        items = set(self.items)
        costtable = self.eventmanager.database.tables['costs']
        try:
            methods.exportitemstocsv('backup.csv',items)
        except: self.pane.addmessage("CSV Error","Secondary Save Failure",f"Failed to create csv backup of chanages.\n{traceback.format_exc()}")
        try:
            failures = methods.exportitemstodb(costtable,items)
            self.eventmanager.database.commit()
            self.eventmanager.database.close()
        except: self.pane.addmessage("DB Error","Database Backup Failure",f"Failed to commitchanges to database.\n{traceback.format_exc()}")
        else:
            if not failures:
                self.pane.addmessage("DB Result","Database Update Successful",f"Successfully Updated Database with {len(self.items)} Cost Items.")
            else:
                failuremessage = "\n".join(failures)
                self.pane.addmessage("DB Errors","Some Errors during Export",f"{len(items)-len(failures)} Items correctly exported; the rest had the following errors:\n{failuremessage}")


# -*- coding: utf-8 -*-
def int2roman(num):
   roman_mapper = {
       '1':I
   }

def num_islands(mat):
    if not mat:
        return 0
    m,n = len(mat),len(mat[0])
    cnt = 0

    for row in range(m):
        for col in range(n):
            if mat[row][col] == 1:
                check_connection(mat,row,col,m,n)
                cnt += 1
    return cnt

def check_connection(mat,row,col):
    s = [[row,col]]
    while len(s) >0:
        cur = s.pop()
        neibs = [[cur[0]+x,cur[1]+y] for x in [-1,1] for y in [-1,1]]
        for nerb in nerbs:
            if 0<nerb[0]<m and 0<nerb[1]<n:
                if mat[nerb[0]][nerb[1]] == 1:
                    s.append(nerb)
                    mat[nerb[0]][nerb[1]] = "#"


#!/usr/bin/env python
# -*- encoding: utf8 -*-

import codecs
import os.path
from FormProcessor import FormProcessor

class SourceScanner(object):
    '''
    Scan the VB source code to gather
    the useful information
    '''
    def __init__(self, stg, processor, encoding = 'cp932'):
        '''
        class contructor

        arguments:
        stg -- StringTableGenerator
        processor -- LineProcessor
        encoding -- The encoding of target files
        '''
        self.__stg = stg
        self.__processor = processor
        self.__encoding = encoding

    @property
    def stringTableGenerator(self):
        return self.__stg

    @property
    def encoding(self):
        return self.__encoding

    @property
    def processor(self):
        return self.__processor

    def processLine(self, line, writer):
        '''
        process the string line to get the
        processed line, which Japanese strings
        have been replaced by LoadResString
        function

        arguments:
        line -- the input line
        writer -- SourceWriter

        return:
        the processed line
        '''

        infos = self.processor.process(line)
        if len(infos) > 0:
            index = []
            for info in infos:
                index.append(self.stringTableGenerator.putInfo(info))
            line = writer.replace(line, infos, index)
        return line

    def scan(self, fname, writer):
        '''
        scan the file

        open the file named fname, and scan, process
        the contents

        arguments:
        fname -- the name of target file
        writer -- SourceWriter
        '''
        isForm = os.path.splitext(fname)[1].lower() == '.frm'

        if isForm:
            formProc = FormProcessor(self.__stg)

        f = codecs.open(fname, 'r', self.encoding)

        pos = FormProcessor.AFTER_ATTR
        for line in f:
            if isForm:
                pos = formProc.process(line)

            if pos == FormProcessor.AFTER_ATTR:
                line = self.processLine(line, writer)
            writer.addLine(line)

        if isForm:
            formProc.generateControllerInfo()
            writer.addFormInfo(formProc.formLoad)
        f.close()


# -*- coding: utf-8 -*-
"""
Created on Wed Jun 14 12:43:29 2023

@author: anepa
"""

import bilaketakFuntzioak as bf
import csvreader as c
import csv

#fileHerri=open("./herriak/herriakKodigoekin.csv", 'w', newline='')

#writerHerri = csv.writer(fileHerri)

"""
with open("./herriak/herriakSinplifikatuta.csv", 'r') as herri_csv:
	herrireader=csv.reader(herri_csv)
	for row in herrireader:
		emaitza = bf.wikidatanDago(row[0])
		print(row[0] + ': ' + str(emaitza) )
		writerHerri.writerow([f"'{row[0]}' : '{emaitza}'"])
"""
with open("./herriak/herriakKodigoekin.csv", 'r') as herri_csv:
	herrireader=csv.reader(herri_csv)
	hiztegia = {}
	for row in herrireader:
		print(row)
		zatiak = row[0].split(':')
		klabea = zatiak[0]
		balioa = zatiak[1]
		balioa = balioa[1:]
		hiztegia[klabea]= balioa
	for key, value in hiztegia.items():
		print(f"'{key}':'{value}'")

#bf.herriSinplifikatuakLortu()


from discord.ext import commands
import os
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('TOKEN')
client = commands.Bot(command_prefix='.')

class Greeting(commands.Cog):
    def __init__(self, bot):
        self.bot = client

    @commands.Cog.listener()
    @commands.has_permissions(connect=True)
    async def on_member_join(self, member):
        '''
        When a user joins, validate their user id against the one in the database
        '''
        channel = member.guild.system_channel
        if channel is not None:
            await channel.send(f'Welcome {member}!')
client.add_cog(Greeting(client))

client.run(TOKEN)


# 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 kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
        # 가장 작은 것은 맨 왼쪽. 그 다음은 루트. 그 다음은 오른쪽 중에서도 왼쪽

        ans = []
        def dfs(node):
            if node == None:
                return

            dfs(node.left)
            ans.append(node.val)
            dfs(node.right)
        dfs(root)
        return ans[k-1]

Algorithm/LeetCode/230. Kth Smallest Element in a BST.py


from django.shortcuts import render, redirect
from django import forms

from . import util
import markdown
import random
import os

def index(request):
    return render(request, "encyclopedia/index.html", {
        "entries": util.list_entries()
    })

def entry_title(request, entry):
    # this is how we create an entry page, the main page needs to include the submit button
    markdown_file_path = f"entries/{entry}.md"  # path to your Markdown files

    try:
        with open(markdown_file_path, 'r') as file:
            markdown_content = file.read()
            content_entry = markdown.markdown(markdown_content)

        return render(request, "encyclopedia/entry.html", {
            'entry_content': content_entry, 'entry_title': entry
        })
    except FileNotFoundError:
        return render(request, "encyclopedia/entry_error.html")

def search_entries(request):
    entries = util.list_entries()
    search = request.GET.get("q", "")
    search = str(search)
    search_results = []

    for entry in entries:
        if entry.lower() == search.lower():
            return redirect("entry_title", entry)
        elif search.lower() in entry.lower():
            search_results.append(entry)

    return render(request,"encyclopedia/search_results.html", {"search_results": search_results, "search": search})

def random_entry(request):
    entries = util.list_entries()
    entry = random.choice(entries)
    return redirect("entry_title", entry)

def new_page(request):
    return render(request, "encyclopedia/new_entry.html")

def create_new(request):
    content = request.POST.get("entry_content", "")
    title = request.POST.get("entry_title", "")
    file_title = title.replace(" ", "")
    file_path = (f"entries/{file_title}.md")

    if os.path.isfile(file_path):
        return render(request, "encyclopedia/create_entry_error.html")

    else:
        file = open(file_path, 'w')
        file.write(f"# {title}\n{content}")
        return redirect("entry_title", file_title)

def edit_page(request):
    entry_title = request.POST.get("entry_title", "")
    entry_content = util.get_entry(entry_title)
    return render(request, "encyclopedia/edit_entry.html", {"entry_content": entry_content, "entry_title": entry_title})

def edit_entry(request):
    title = request.POST.get("entry_title", "")
    content = request.POST.get("entry_content", "")

    util.save_entry(title, content)
    return redirect("entry_title", title)


# Data Processing
#
# Reads and processes data from an excel worksheet
# Writes median, mean, max, 95% of each 3-min epoch to another excel worksheet
#
# Instructions:
# in the main program at the bottom, fill in names and tracking_num with the desired names.
# An excel worksheet with the name "processed_[tracking_num].xlsx" will appear
# in the current working directory.

import math
import numpy as np
import openpyxl
import matplotlib.pyplot as plt

def read_data(sheet):
    """Read data from excel sheet and return dictionary with data arranged in 3-min epochs"""

    data = {}
    count = 0
    epoch_num = 1
    time, accX, accY, accZ, act = ([] for i in range(5))

    for i in range(2,sheet.max_row):
        # check if the data should go into the next epoch
        if count > 2:
            # put data values into each epoch
            data[epoch_num] = {"time": time, "accX": accX,
                                "accY": accY, "accZ": accZ, "act": act}
            count = 0
            time, accX, accY, accZ, act = ([] for i in range(5))
            epoch_num += 1
        else:
            count += 1  # update line counter

        # read out the x, y, z values
        time.append(sheet.cell(i,1).value)
        accX.append(sheet.cell(i,2).value)
        accY.append(sheet.cell(i,3).value)
        accZ.append(sheet.cell(i,4).value)

        # compose x(t), y(t), z(t) into act(t)
        act.append(math.sqrt(accX[-1]**2 + accY[-1]**2 + accZ[-1]**2))

    data[epoch_num] = {"time": time, "accX": accX,
                         "accY": accY, "accZ": accZ, "act": act}
    return data

def process_data(data):
    """Processes data from read_data to find median, mean, max, and 95% of the data"""

    processed_data = {}

    for i in range(len(data)):
        # find median, mean, max value from each epoch
        median = np.median(data[i+1]["act"])
        mean = np.mean(data[i+1]["act"])
        max_value = max(data[i+1]["act"])
        percent_95 = np.percentile(data[i+1]["act"], 95)

        processed_data[i+1] = {"median":median, "mean":mean, "max":max_value, "95%":percent_95}
    return processed_data

def write_data(sheet, tracking_num):
    # create a new workbook and open worksheet
    new_wb = openpyxl.Workbook()
    new_sheet = new_wb.active

    # headers for data
    new_sheet.cell(1, 1).value = "Tracking_num"
    new_sheet.cell(1, 2).value = "Epoch"
    new_sheet.cell(1, 3).value = "Median"
    new_sheet.cell(1, 4).value = "Mean"
    new_sheet.cell(1, 5).value = "Max"
    new_sheet.cell(1, 6).value = "95%"

    raw_data = read_data(sheet)
    processed_data = process_data(read_data(sheet))

    # write data into the worksheet
    for i in range(2, len(processed_data)+2):
        new_sheet.cell(i, 1).value = tracking_num
        new_sheet.cell(i, 2).value = i-1
        new_sheet.cell(i, 3).value = processed_data[i-1]["median"]
        new_sheet.cell(i, 4).value = processed_data[i-1]["mean"]
        new_sheet.cell(i, 5).value = processed_data[i-1]["max"]
        new_sheet.cell(i, 6).value = processed_data[i-1]["95%"]

    # save worksheet to working directory
    new_wb.save("processed_" + tracking_num + ".xlsx")

if __name__ == "__main__":
    # insert names of the data files
    # person_trial = ["p1n1", "p1n2", "p1n3", "p2n1", "p2n2", "p2n3",
    #                 "p3n1", "p3n2", "p5n1", "p5n3", "p6n1", "p6n2", "p7n1", "p7n2"]
    person_trial = ["p7n1", "p7n2"]

    # wb = openpyxl.load_workbook("p6n2.xlsx")
    # sheet = wb.active

    # data = process_data(read_data(sheet))

    for name in person_trial:
        print("Currently processing p" + name[1] + "n" + name[3] + "...")

        # open workbook with data
        wb = openpyxl.load_workbook(name + ".xlsx")
        sheet = wb.active

        data = process_data(read_data(sheet))

        # read and process data
        write_data(sheet, "p" + name[1] + "n" + name[3])
        print("Finished p" + name[1] + "n" + name[3])


'''This Python code uses the serial library to communicate with the Arduino and the camera module via serial communication. The PIR motion sensor pin and the LED pin are defined as constants at the beginning of the code. The camera commands are also defined as constants.

The send_command() function sends a command to the Arduino or the camera by writing a byte array to the serial port. The get_camera_size() function retrieves the size of the camera image by sending the CAMERA_GET_SIZE command to the Arduino and reading the response. The take_picture() function sends the CAMERA_SNAPSHOT command to the Arduino to take a picture. The flash_led() function sends commands to turn on and off the LED connected to the Arduino.

In the with block, the Python code initializes the camera module by sending the CAMERA_WAKEUP and CAMERA_RESET commands. The camera size is then printed to the console. The code then enters an infinite loop and waits for the Arduino to send a signal indicating that motion has been detected by the PIR motion sensor. When motion is detected, the take_picture() and flash_led() functions are called.'''

import serial
import time

# Define the serial port and baud rate for the Arduino
SERIAL_PORT = '/dev/ttyACM0'
BAUD_RATE = 9600

# Define the digital pin to which the PIR motion sensor is connected
PIR_PIN = 2

# Define the digital pin to which the LED is connected
LED_PIN = 13

# Define the camera commands
CAMERA_WAKEUP = 'AA 0D 00 00 00 00 00 00 00 00 00 00 00 00 00 0D AA'
CAMERA_RESET = 'AA 08 00 00 00 00 00 00 00 00 00 00 00 08 AA'
CAMERA_SNAPSHOT = 'AA 05 00 00 00 00 00 00 00 00 00 00 00 05 AA'
CAMERA_GET_SIZE = 'AA 05 04 00 01 00 00 00 00 00 00 00 00 0A AA'

# Define the serial port and baud rate for the camera
CAMERA_SERIAL_PORT = '/dev/ttyUSB0'
CAMERA_BAUD_RATE = 38400

ser = serial.Serial(SERIAL_PORT, BAUD_RATE)

def send_command(cmd):
    ser.write(bytearray.fromhex(cmd))

def get_camera_size():
    send_command(CAMERA_GET_SIZE)
    size = ser.read(4)
    return f'{size[1]}x{size[2]} {size[3]}bpp'

def take_picture():
    send_command(CAMERA_SNAPSHOT)
    time.sleep(5)

def flash_led():
    send_command('1')
    time.sleep(0.1)
    send_command('0')

with serial.Serial(CAMERA_SERIAL_PORT, CAMERA_BAUD_RATE) as camera:
    camera.write(bytearray.fromhex(CAMERA_WAKEUP))
    time.sleep(1)
    camera.write(bytearray.fromhex(CAMERA_RESET))
    time.sleep(1)
    print(get_camera_size())

    while True:
        if ser.read(1) == b'1':
            take_picture()
            flash_led()