import pandas as pd
import numpy as np
from pytorch_transformers import *
from keras.preprocessing.sequence import pad_sequences
from sklearn.utils import resample

class MyBertForFeatureExtraction(BertPreTrainedModel):
    def __init__(self, config):
        super(MyBertForFeatureExtraction, self).__init__(config)
        self.num_labels = config.num_labels

        self.bert = BertModel(config)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.classifier = nn.Linear(config.hidden_size, self.config.num_labels)

        self.init_weights()

    def forward(self, input_ids, attention_mask=None, token_type_ids=None,
            position_ids=None, head_mask=None, labels=None):

        outputs = self.bert(input_ids,
                            attention_masks=attention_mask,
                            token_type_ids=token_type_ids,
                            position_ids=position_ids,
                            head_masks=head_mask)

        pooled_output = outputs[1]

        return pooled_output

def processDataFrame(df, is_training):
   df = df.dropna()
   df.label = df.label.astype(int)

   df_class1, df_class2 = df[df.label == 0], df[df.label == 1]

   (df_majority, df_minority) = (df_class1, df_class2) if len(df_class1) > len(df_class2) else (df_class2, df_class1)

   if not is_training:
      print ('test dataset [%d]: %d, [%d]: %d'%(df_majority.label.values[0], len(df_majority), df_minority.label.values[0], len(df_minority)))
   else:
      print ('train dataset [%d]: %d, [%d]: %d'%(df_majority.label.values[0], len(df_majority), df_minority.label.values[0], len(df_minority)))
      length_minority = 20000 if len(df_minority) > 20000 else len(df_minority)
      #length_minority = len(df_minority)

      df_majority_downsampled = resample(df_majority, replace=False, n_samples=length_minority, random_state=123)
      df_minority_downsampled = resample(df_minority, replace=False, n_samples=length_minority, random_state=123)

      df_downsampled = pd.concat([df_majority_downsampled, df_minority_downsampled])
      df = df_downsampled

      print ('train dataset [%d]: %d, [%d]: %d'%(df_majority_downsampled.label.values[0], len(df_majority_downsampled), df_minority_downsampled.label.values[0], len(df_minority_downsampled)))

   return df

def makeBertElements(df, MAX_LEN):
    # Create sentence and label lists
    sentences = df.sentence.values

    # We need to add special tokens at the beginning and end of each sentence for BERT to work properly
    sentences = ["[CLS] " + str(sentence) + " [SEP]" for sentence in sentences]
    labels = df.label.values

    tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)
    tokenized_texts = [tokenizer.tokenize(sent) for sent in sentences]

    # Set the maximum sequence length. The longest sequence in our training set is 47, but we'll leave room on the end anyway.
    # In the original paper, the authors used a length of 512.
    # MAX_LEN = 128

    # Pad our input tokens
    #input_ids = pad_sequences([tokenizer.convert_tokens_to_ids(txt) for txt in tokenized_texts],
    #                          maxlen=MAX_LEN, dtype="long", truncating="post", padding="post")

    # Use the BERT tokenizer to convert the tokens to their index numbers in the BERT vocabulary
    input_ids = [tokenizer.convert_tokens_to_ids(x) for x in tokenized_texts]
    input_ids = pad_sequences(input_ids, maxlen=MAX_LEN, dtype="long", truncating="post", padding="post")

    # Create attention masks
    attention_masks = []

    # Create a mask of 1s for each token followed by 0s for padding
    for seq in input_ids:
        seq_mask = [float(i>0) for i in seq]
        attention_masks.append(seq_mask)

    return input_ids, attention_masks, labels

def makeBertElement(sentence, tokenizer, MAX_LEN):
    sentence = "[CLS] " + sentence + " [SEP]"
    tokenized_texts = tokenizer.tokenize(sentence)
    input_id = tokenizer.convert_tokens_to_ids(tokenized_texts)
    input_ids = pad_sequences([input_id], maxlen=MAX_LEN, dtype="long", truncating="post", padding="post")
    attention_masks = []
    for seq in input_ids:
        seq_mask = [float(i>0) for i in seq]
        attention_masks.append(seq_mask)

    return input_ids, attention_masks

def makeBertFeatures(tuples, MAX_LEN):
    model = MyBertForFeatureExtraction.from_pretrained("bert-base-uncased", num_labels=2)
    if torch.cuda.is_available():
        device = 'cuda'
        model.cuda()
    else:
        device = 'cpu'

    d_bertfeatures = {}
    elements = [element for (element, sentence) in tuples]
    sentences = ["[CLS] " + str(sentence) + " [SEP]" for (element, sentence) in tuples]

    tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)
    tokenized_texts = [tokenizer.tokenize(sent) for sent in sentences]

    input_ids = [tokenizer.convert_tokens_to_ids(x) for x in tokenized_texts]
    input_ids = pad_sequences(input_ids, maxlen=MAX_LEN, dtype="long", truncating="post", padding="post")

    attention_masks = []

    for seq in input_ids:
        seq_mask = [float(i>0) for i in seq]
        attention_masks.append(seq_mask)

    input_ids = torch.tensor(input_ids).to(device)
    attention_masks = torch.tensor(attention_masks).to(device)
    with torch.no_grad():
        outputs = model(input_ids, token_type_ids=None, attention_mask=attention_masks)
    outputs = outputs.to('cpu').tolist()

    if len(elements) != len(outputs):
        print ('the size is different!')
        return None

    for element, output in zip(elements, outputs):
        d_bertfeatures[element] = output

    return d_bertfeatures

# Function to calculate the accuracy of our predictions vs labels
def flat_accuracy(preds, labels):
    pred_flat = np.argmax(preds, axis=1).flatten()
    labels_flat = labels.flatten()
    return np.sum(pred_flat == labels_flat) / len(labels_flat)


import time
import datetime

from jinja2 import Environment, FileSystemLoader

from data import params
from bot import logic
from server import fs

def start():
    parameters = params.Params()
    isOk = parameters.new()

    if isOk == True:
        parameters.account = parameters.exchange_client.getAccount()

    if parameters.account is None:
        isOk = False

    seconds_in_delay = 3600
    delay = seconds_in_delay

    report_send = False

    environment = Environment(loader=FileSystemLoader("templates/"))

    while isOk == True:
        subject = "New asset update - " + time.strftime("%d/%m/%Y")
        message = ""
        body = ""

        parameters.account.total = parameters.account.available

        reports = []
        if parameters.exchange_type != "HISTORY":
            reports = logic.getHistory(parameters)

        actives = parameters.exchange_client.getAllActiveAssets(parameters)

        alerts = logic.monitor(parameters, actives)

        if alerts != "":
            message += "############# ALERTS #############\n\n"
            message += alerts + "\n"
            body += environment.get_template("alerts.html.j2").render(text=alerts)

        parameters.exchange_client.actualizeAccount(parameters.account)

        profitables = parameters.exchange_client.findProfitable(parameters)

        sleep = 0
        if parameters.exchange_type != "HISTORY":

            actives_html = environment.get_template("actives.html.j2").render(list=actives)
            html_file = fs.File('output', 'index.html')

            if html_file.create() is None:
                print("Unable to create 'output/index.html' file. Please check permissions on filesystem")
                isOk = False

            html_file.putInFile(actives_html)

            profitables_html = environment.get_template("profitables.html.j2").render(list=profitables)
            html_file = fs.File('output', 'profitables.html')

            if html_file.create() is None:
                print("Unable to create 'output/profitables.html' file. Please check permissions on filesystem")
                isOk = False

            html_file.putInFile(profitables_html)

            for asset in actives:
                parameters.account.total += asset.current

            account_html = environment.get_template("account.html.j2").render(
                account=parameters.account,
                history=reports[0].list,
            )
            html_file = fs.File('output', 'account.html')

            if html_file.create() is None:
                print("Unable to create 'output/account.html' file. Please check permissions on filesystem")
                isOk = False

            html_file.putInFile(account_html)

            sleep = parameters.refresh_time * 60

        else:
            isOk = parameters.exchange_client.isOk

        if report_send == True and (datetime.time(00,00) <= datetime.datetime.now().time() <= datetime.time(00,59) or isOk == False):
            reports = logic.getHistory(parameters)
            report_message = logic.report(parameters, reports)
            message += "############# REPORT #############\n"
            message += report_message
            body += environment.get_template("reports.html.j2").render(text=report_message)
            delay = seconds_in_delay

        update_message = ""
        if parameters.latest_bot_release is not None and message != "":
            update_message = logic.checkUpdate(parameters.latest_bot_release)

        if update_message != "":
            message += "############# UPDATE #############\n\n"
            message += update_message
            body += environment.get_template("version.html.j2").render(text=update_message)

        if parameters.smtp is not None and message != "":
            html = environment.get_template("base.html.j2").render(body=body)
            parameters.smtp.send(subject=subject, plain=message, html=html)

        if message != "":
            print("\n" + subject)
            print("\n" + message)

        if (delay > 0):
            delay -= parameters.refresh_time * 60
            report_send = False

        else:
            report_send = True

        if parameters.make_order == True and isOk:
            logic.buy(parameters, profitables)

        time.sleep(sleep)

        parameters.actualize()

        if parameters.exchange_type == "HISTORY" and not isOk:
            parameters.database.client.drop_collection("actives")
            parameters.database.client.drop_collection("history")

if __name__ == '__main__':
    start()


import tensorflow as tf
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import style

X = pd.read_csv('data/feature_martix.csv')
Y = pd.read_csv('data/label_matrix.csv')

# data-preprocessing
main_data = pd.concat([X, Y], axis=1)
main_data = main_data.reindex(np.random.permutation(X.index))
dfs = np.split(main_data, [312], axis=1)
X = dfs[0]
Y = dfs[1]
a = Y.values.reshape(11787)
neww = tf.one_hot(a,200)
sess = tf.Session()
Y = neww.eval(session=sess) # returns num-py array

# hyperparameters
batch_size = 100
lr = 0.0002
num_iters = 3300
K = 250
#L = 250
n = 11787
pkeep = tf.placeholder(tf.float32)

# model creation
x = tf.placeholder(tf.float32, shape=[None, 312])
Y_ = tf.placeholder(tf.float32, shape=[None, 200])

w1 = tf.Variable(tf.truncated_normal([312, K], stddev=0.1))
b1 = tf.Variable(tf.ones([K]))

w2 = tf.Variable(tf.truncated_normal([K, 200], stddev=0.1))
b2 = tf.Variable(tf.ones([200]))

#w3 = tf.Variable(tf.truncated_normal([L, 200], stddev=0.1))
#b3 = tf.Variable(tf.zeros([200]))

# layers
y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
do1 = tf.nn.dropout(y1, pkeep)

#y2 = tf.nn.relu(tf.matmul(do1, w2) + b2)
#do2 = tf.nn.dropout(y2, pkeep)

y = tf.nn.softmax(tf.matmul(do1, w2) + b2)

# loss function
cross_entropy = -tf.reduce_sum(Y_ * tf.log(y))

is_correct = tf.equal(tf.argmax(Y_, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))

# training algo.
train_step = tf.train.AdamOptimizer(learning_rate=lr).minimize(cross_entropy)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

aaa = []
mmm = []
ccc = []
att = []
testccc = []

# training
for i in range(num_iters):
    train_data = {x: X[0:8520], Y_: Y[0:8520], pkeep:0.75}

    sess.run(train_step, feed_dict=train_data)

    atr, c = sess.run([accuracy, cross_entropy], feed_dict=train_data)

    test_data = {x: X[7520:], Y_: Y[7520:], pkeep:1}
    at, ac = sess.run([accuracy, cross_entropy], feed_dict=test_data)

    aaa.append(atr)
    mmm.append(i)
    ccc.append(c)
    att.append(at)
    testccc.append(ac)

    if(i%100 == 0):
      print(i, atr, c)

print('Accuracy on training set is ', atr)

print('Accuracy on test set is', at)

style.use('ggplot')
plt.plot(mmm,aaa,color='b',label='training')
plt.plot(mmm,att,color='r',label='testing')
plt.xlabel('Num Iters')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
plt.plot(mmm,ccc,color='b',label='training')
plt.plot(mmm, testccc,color='r', label='testing')
plt.xlabel('Num Iters')
plt.ylabel('Cross-Entropy')
plt.legend()
plt.show()

'''
Output -
0 0.0049295775 57336.72
100 0.06173709 40583.14
200 0.19941315 32039.98
300 0.32347417 26043.324
400 0.4019953 21903.246
500 0.46232393 19005.758
...
2700 0.86596245 4253.572
2800 0.8653756 4143.8467
2900 0.87805164 3876.802
3000 0.8849765 3692.5117
3100 0.8847418 3564.2808
3200 0.89248824 3450.6558
Accuracy on training set is  0.900939
Accuracy on test set is 0.6212796
'''


import ctypes as ct
import ctypes.wintypes as wt

EVENT_FILTER_TYPE_NONE = 0x00000000
EVENT_FILTER_TYPE_SCHEMATIZED = 0x80000000
EVENT_FILTER_TYPE_SYSTEM_FLAGS = 0x80000001
VENT_FILTER_TYPE_TRACEHANDLE = 0x80000002
EVENT_FILTER_TYPE_PID = 0x80000004
EVENT_FILTER_TYPE_EXECUTABLE_NAME = 0x80000008
EVENT_FILTER_TYPE_PACKAGE_ID = 0x80000010
EVENT_FILTER_TYPE_PACKAGE_APP_ID = 0x80000020
EVENT_FILTER_TYPE_PAYLOAD = 0x80000100
EVENT_FILTER_TYPE_EVENT_ID = 0x80000200
EVENT_FILTER_TYPE_STACKWALK = 0x80001000

MAX_EVENT_FILTER_EVENT_ID_COUNT = 64
MAX_EVENT_FILTER_DATA_SIZE = 1024

class EVENT_FILTER_DESCRIPTOR(ct.Structure):
    _fields_ = [('Ptr', ct.c_ulonglong),
                ('Size', ct.c_ulong),
                ('Type', ct.c_ulong)]

class EVENT_FILTER_HEADER(ct.Structure):
    _fields_ = [('Id', wt.USHORT),
                ('Version', wt.CHAR),
                ('Reserved', wt.CHAR * 5),
                ('InstanceId',  ct.c_ulonglong),
                ('Size', wt.ULONG),
                ('NextOffset', wt.ULONG)]

class EVENT_FILTER_EVENT_ID(ct.Structure):
    _fields_ = [('FilterIn', wt.BOOLEAN),
                ('Reserved', wt.CHAR),
                ('Count', wt.USHORT),
                ('Events', wt.USHORT * 0)]

    def __init__(self, filter_in, events):
        struct_size = len(events) * ct.sizeof(wt.USHORT) + ct.sizeof(EVENT_FILTER_EVENT_ID)
        self._buf = (ct.c_char * struct_size)()
        self._props = ct.cast(ct.pointer(self._buf), ct.POINTER(EVENT_FILTER_EVENT_ID))
        self._props.contents.FilterIn = filter_in
        self._props.contents.Reserved = 0
        self._props.contents.Count = len(events)

        for i in range(len(events)):
            ct.memmove(ct.cast(ct.addressof(self._buf) + ct.sizeof(EVENT_FILTER_EVENT_ID) + (ct.sizeof(wt.WCHAR) * i),
                               ct.c_void_p),
                       ct.byref(wt.USHORT(events[i])),
                       ct.sizeof(wt.WCHAR))

    def get(self):
            return self._props

class EVENT_FILTER_LEVEL_KW(ct.Structure):
    _fields_ = [('MatchAnyKeyword', ct.c_ulonglong),
                ('MatchAllKeyword', ct.c_ulonglong),
                ('Level', wt.CHAR),
                ('FilterIn', wt.BOOLEAN)]

class EVENT_FILTER_EVENT_NAME(ct.Structure):
    _fields_ = [('MatchAnyKeyword', ct.c_ulonglong),
                ('MatchAllKeyword', ct.c_ulonglong),
                ('Level', wt.CHAR),
                ('FilterIn', wt.BOOLEAN),
                ('NameCount', wt.USHORT),
                ('Names', wt.CHAR * 0)]

    def __init__(self, match_any, match_all, level, filter_in, names):
        struct_size = ((sum([len(name) for name in names]) * ct.sizeof(wt.CHAR)) + (ct.sizeof(wt.CHAR) * len(names))) +\
                      ct.sizeof(EVENT_FILTER_EVENT_NAME)
        self._buf = (ct.c_char * struct_size)()
        self._props = ct.cast(ct.pointer(self._buf), ct.POINTER(EVENT_FILTER_EVENT_NAME))
        self._props.contents.MatchAnyKeyword = match_any
        self._props.contents.MatchAllKeyword = match_all
        self._props.contents.Level = level
        self._props.contents.FilterIn = filter_in
        self._props.contents.NameCount = len(names)

        str_off = 0
        for i in range(len(names)):
            ct.memmove(ct.cast(ct.addressof(self._buf) + ct.sizeof(EVENT_FILTER_EVENT_NAME) + str_off,
                               ct.c_void_p),
                       names[i],
                       len(names[i]))
            str_off += len(names[i]) + ct.sizeof(wt.CHAR)

    def get(self):
            return self._props

class EVENT_DESCRIPTOR(ct.Structure):
    _fields_ = [('Id', ct.c_ushort),
                ('Version', ct.c_ubyte),
                ('Channel', ct.c_ubyte),
                ('Level', ct.c_ubyte),
                ('Opcode', ct.c_ubyte),
                ('Task', ct.c_ushort),
                ('Keyword', ct.c_ulonglong)]


# Calcular el volumen y el área superficial al ingresar r1, r2 y h
import math
def VolumenFigura(r1:float, r2:float, h:float) -> float:
    VolEsfera = (4/3) * math.pi * (r1**3)
    VolCono = (1/3) * (h) * math.pi * (r2**2)
    return VolEsfera + VolCono

def AreaFigura(r1:float, r2:float, h:float) -> float:
    ArSupEsfera = 4 * math.pi * (r1**2)
    Hipot = (h**2 + r2**2)**0.5  # formula de la hipotenusa para hallar área superficial
    ArSupCono = (math.pi * r2 * Hipot) + (math.pi * (r2**2))
    return ArSupEsfera + ArSupCono

if __name__ == "__main__":
    r1 = float(input("ingrese el radio de la esfera: ")) # radio en cm
    r2 = float(input("ingrese el radio del cono: ")) # radio en cm
    h = float(input("ingrese la altura del cono: ")) # altura en cm
    Volumen = VolumenFigura(r1, r2, h)
    print("el volumen de la figura es: " + str(Volumen))
    AreaSuperficial= AreaFigura(r1, r2, h)
    print("el área superficial de la figura es: " + str(AreaSuperficial))


import angr
import claripy
from pwn import *

BINARY = './crackme'
INPUT_LENGTH = 80

BASE = 0x400000
START_ADDR = BASE + 0xc6c

FIND_ADDR = BASE + 0xe99
AVOID_ADDR = BASE + 0xe8e

log.info('loading binary')
p = angr.Project(BINARY, load_options={'auto_load_libs': False})

log.info('setting up state')
state = p.factory.blank_state(addr=START_ADDR)

magic = claripy.BVS('magic', INPUT_LENGTH * 8)
state.memory.store(state.regs.rsp - 0x100, magic)
state.regs.rdi = state.regs.rsp - 0x100

log.info('exploring')
path = p.factory.path(state)
path_group = p.factory.path_group(path)

path_group.explore(find=FIND_ADDR, avoid=AVOID_ADDR)

if path_group.found:
    result = path_group.found[0].state.se.any_str(magic)#, INPUT_LENGTH)
    log.success(result)
else:
    log.error('No path found')

"""Submit flag
eWVzIGFuZCBoaXMgaGFuZHMgc2hvb2sgd2l0aCBleAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
"""


import pytest
from utils.utils import get_local_spark_session, read_json_from_file
from unittest.mock import patch
from pyspark.sql import SparkSession

@pytest.fixture
def spark_session() -> SparkSession:
    return SparkSession.builder.getOrCreate()

def test_get_local_spark_session(spark_session: SparkSession) -> None:
    spark = get_local_spark_session()
    assert isinstance(spark, SparkSession)

def test_read_json_from_file() -> None:
    file_path = "test.json"
    expected_data = [{"key": "value"}]
    with patch("builtins.open") as mock_open:
        mock_file = mock_open.return_value.__enter__.return_value
        mock_file.read.return_value = '[{"key": "value"}]'
        data = read_json_from_file(file_path)
    mock_open.assert_called_once_with(file_path, 'r')
    assert data == expected_data


"""
有一个整数数组 nums ，和一个查询数组 requests ，其中 requests[i] = [starti, endi] 。
第 i 个查询求 nums[starti] + nums[starti + 1] + ... + nums[endi - 1] + nums[endi] 的结果 ，starti 和 endi 数组索引都是 从 0 开始 的。

你可以任意排列 nums 中的数字，请你返回所有查询结果之和的最大值。

由于答案可能会很大，请你将它对 109 + 7 取余 后返回。

示例 1：

输入：nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
输出：19
解释：一个可行的 nums 排列为 [2,1,3,4,5]，并有如下结果：
requests[0] -> nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
requests[1] -> nums[0] + nums[1] = 2 + 1 = 3
总和为：8 + 3 = 11。
一个总和更大的排列为 [3,5,4,2,1]，并有如下结果：
requests[0] -> nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11
requests[1] -> nums[0] + nums[1] = 3 + 5  = 8
总和为： 11 + 8 = 19，这个方案是所有排列中查询之和最大的结果。
示例 2：

输入：nums = [1,2,3,4,5,6], requests = [[0,1]]
输出：11
解释：一个总和最大的排列为 [6,5,4,3,2,1] ，查询和为 [11]。
示例 3：

输入：nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]]
输出：47
解释：一个和最大的排列为 [4,10,5,3,2,1] ，查询结果分别为 [19,18,10]。

提示：

n == nums.length
1 <= n <= 105
0 <= nums[i] <= 105
1 <= requests.length <= 105
requests[i].length == 2
0 <= starti <= endi < n

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/maximum-sum-obtained-of-any-permutation
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
"""
from typing import List

class Solution:
    def maxSumRangeQuery(self, nums: List[int], r: List[List[int]]) -> int:
        """
        思路：差分+前缀和
        1. 通过差分思想计算连续的数字出现的次数
        2. 起点位置+1，终点前一个位置-1
        3. 通过前缀和可计算出每个位置数字出现的次数
        @param nums:
        @param r:
        @return:
        """
        mod = 10 ** 9 + 7
        res = 0
        n = len(nums)
        nums.sort()
        arr = [0] * n
        for l, h in r:
            arr[l] += 1
            if h < n - 1:
                arr[h + 1] -= 1
        for i in range(1, n):
            arr[i] += arr[i - 1]
        arr.sort()
        for i in range(n):
            res += nums[i] * arr[i]
        return res % mod

if __name__ == '__main__':
    nums = [1, 2, 3, 4, 5]
    requests = [[1, 3], [0, 1]]
    print(Solution().maxSumRangeQuery(nums, requests))

1589. 所有排列中的最大和.py


# client.py

import asyncio
import importlib
import os
import sys
import time
import traceback
from typing import Optional, Union
import inspect
import glob

from pyrogram import Client
from pyrogram.errors import AuthKeyDuplicated, BotResponseTimeout
from pyrogram.raw.types.messages import BotResults

from init import ChangeInitMessage
from venom import Config, logging, SecureConfig
from venom.helpers import time_format, get_import_paths
from .database import _close_db, get_collection
from .methods import Methods
from .ext import pool
from ..plugins import all_plugins, ROOT

_LOG = logging.getLogger(__name__)
_LOG_STR = "### %s ###"

_START = time.time()
TOGGLES = get_collection("TOGGLES")

async def _init_tasks():
    init_list_ = []
    for task in all_plugins():
        imported = importlib.import_module(f"venom.plugins.{task}")
        if not hasattr(imported, "_init"):
            continue
        init_func = getattr(imported, "_init")
        init_list_.append(init_func())
    try:
        await asyncio.gather(*init_list_)
    except ConnectionError:
        print(f"Connection error.\n{traceback.format_exc()}")
    except BaseException as e:
        print(f"Error in init functions: {e}\n{inspect.currentframe().f_back.f_globals.get('__name__')}")
    init_list_.clear()

class CustomVenom(Methods, Client):
    """ testing """

    # def __init__(self, **kwargs):
    #     """ testing """
    # plugins = all_plugins()
    # for plugin in plugins:
    #     importlib.import_module(f"venom.plugins.{plugin}")
    # super().__init__(**kwargs)

    _root = ROOT

    @classmethod
    def parse(cls, client: Union['Venom', 'VenomBot'], **kwargs):
        pass

    def import_all(self):
        for one in get_import_paths(self._root, "/**/"):
            test = importlib.import_module(f"venom.plugins.{one}")
            name_ = one.split(".")[1]
            test.__init__(name_)

class VenomBot(CustomVenom):

    def __init__(self, bot: Optional[Union['VenomBot', 'Venom']] = None, *args, **kwargs) -> None:
        self.bot = bot
        super().__init__(in_memory=True, *args, **kwargs)

    @classmethod
    def parse(cls, client: Union['Venom', 'VenomBot'], **kwargs):
        return cls()

    # async def start(self):
    #     await super(VenomBot, self).start()
    #     try:
    #         await _init_tasks()
    #     except ConnectionError:
    #         _LOG.error("### Some initial tasks didn't complete because of client error. ###")

class Venom(CustomVenom):

    logging.info(_LOG_STR, "Processing: Venom client")
    failed_imports = ""

    def __init__(self):
        sc = SecureConfig()
        kwargs = {
            'name': 'VenomX',
            'api_id': sc.API_ID,
            'api_hash': sc.API_HASH
        }
        self.DUAL_MODE = False
        if sc.BOT_TOKEN:
            kwargs['bot_token'] = sc.BOT_TOKEN
        if sc.STRING_SESSION and sc.BOT_TOKEN:
            self.DUAL_MODE = True
            self.bot = VenomBot(bot=self, **kwargs)
        if sc.STRING_SESSION:
            kwargs['session_string'] = sc.STRING_SESSION
        else:
            self.bot = VenomBot(bot=self, **kwargs)
        super().__init__(**kwargs)
        # self.executor.shutdown()
        # self.executor = pool._get()

    @classmethod
    def parse(cls, client: Union['Venom', 'VenomBot', Client], **kwargs):
        """ testing """
        return cls()

    @property
    def both(self):
        if Config.USER_MODE:
            return self
        else:
            return self.bot

    @property
    def uptime(self):
        time_ = (time.time() - _START)
        formatted_ = time_format(time_)
        return formatted_

    @property
    def hasbot(self):
        if SecureConfig().BOT_TOKEN:
            return True
        return False

    @property
    def isuser(self):
        if SecureConfig().STRING_SESSION and Config.USER_MODE:
            return True
        return False

    @property
    def isbot(self):
        if SecureConfig().BOT_TOKEN and not Config.USER_MODE:
            return True
        return False

    def import_plugins(self):
        modules_ = glob.glob("venom/**/*.py", recursive=True)
        for module in modules_:
            try:
                import_path = "".join(module[:-3]).replace("/", ".")
                importlib.import_module(import_path)
            except ImportError as i_e:
                self.failed_imports += f"[{i_e.name}] - {i_e.msg}\n"
            except BaseException as e:
                print(e)
        _LOG.info(self.failed_imports)

    async def start(self):
        try:
            await super().start()
            Config.VALID_STRING_SESSION = True
            if hasattr(self, 'bot') and self.bot is not None:
                _LOG.info(_LOG_STR, "Starting bot")
                await self.bot.start()
            self.import_plugins()
        except AuthKeyDuplicated:
            _LOG.info(_LOG_STR, "AuthKeyDuplicated !!!\nStarting bot mode as main interface...")
            Config.USER_MODE = False
            await TOGGLES.update_one(
                {'_id': 'USER_MODE'},
                {'$set': {'switch': False}},
                upsert=True
            )
            SecureConfig().STRING_SESSION = ""
            Config.USER_MODE = False
            await self.bot.start()
        ChangeInitMessage().second_line()
        await _init_tasks()
        ChangeInitMessage().third_line()
        end_ = time.time()
        print(end_ - _START)

    async def stop(self: 'Venom', block: bool = True):
        try:
            if self.bot:
                _LOG.info(_LOG_STR, "Stopping bot")
                await self.bot.stop(block)
            _close_db()
            await super().stop(block=block)
        except ConnectionError:
            await self.bot.stop(block)

    async def restart(self, hard: bool = False):
        _LOG.info(_LOG_STR, "Restarting VenomX")
        await self.stop(block=False)
        if not hard:
            os.execl(sys.executable, sys.executable, "-m", "venom")
        else:
            # _LOG.info(_LOG_STR, "Installing requirements")
            os.execl("bash", "run")


import urllib2
import json
from broadcast import Broadcast
from dab import DABSender

class Radio(object):
    def __init__(self, url):
        self.url = url

    def get_schedule(self, date):
        response = urllib2.urlopen(self.build_url(date)).read()
        return json.loads(response)

    def build_url(self, date):
        if date:
            return '{0}/{1}.json'.format(self.url, date)
        else:
            return '{0}.json'.format(self.url)

class RadioScanner(object):
    DEFAULT_RADIOS = [
        Radio('http://www.bbc.co.uk/radio2/programmes/schedules'),
        Radio('http://www.bbc.co.uk/5livesportsextra/programmes/schedules'),
        Radio('http://www.bbc.co.uk/worldserviceradio/programmes/schedules')
    ]

    def __init__(self, radios=None, sender=None, date=None):
        self.radios = radios if radios else self.DEFAULT_RADIOS
        self.sender = sender if sender else DABSender()
        self.date = date

    def scan(self):
        for radio in self.radios:
            broadcasts = radio.get_schedule(self.date)['schedule']['day']['broadcasts']
            broadcast_objects = [Broadcast.from_dictionary(b) for b in broadcasts]
            self.sender.send(broadcast_objects)


from custom_exceptions.duplicate_account_number_exception import DuplicateAccountNumberException
from custom_exceptions.negative_Deposit_Amount_Exception import NegativeDepositAmountException
from custom_exceptions.withdrawl_exception import WithdrawlAccountException
from Entities.Accounts import Account
from data_access_layer.implementation_classes.account_postgres_dao import AccountPostgresDAO
from service_layer.implementation_services.account_service_imp import AccountServiceImp

account_dao=AccountPostgresDAO()
account_service=AccountServiceImp(account_dao)

duplicate_account_number=Account(0,5239,200,21)

def test_catch_duplicate_account_number():
    try:
        account_service.service_create_account_id(duplicate_account_number)
        assert False
    except DuplicateAccountNumberException as e:
        assert str(e) == "Sorry,That Account Number is Already Taken!!! Please Choose A New Account Number"

def test_catch_negative_deposit():
    try:
        account_service.service_deposit_into_account(20,-20)
        assert False
    except NegativeDepositAmountException as e:
        assert str(e) == "Unsuccessful Deposit: No Negative deposit amounts allowed"

def test_catch_withdrawl_exceeds():
    try:
        account_service.service_withdraw_from_account(20,-500)
        assert False
    except WithdrawlAccountException as e:
        assert str(e) == "Unsuccessful Withdrawl: Please Make Sure You Enough Money In Your Account or You Entered A Positive Withdrawl Amount"

def test_catch_transfer_exceeds():
    try:
        account_service.service_transfer_between_accounts(6,23,200)
        assert False
    except WithdrawlAccountException as e:
        assert str(e) == "You Don't Have Enough Money For a Transfer!!"


from os import rename
import tkinter as tk
import pathlib
import logging
import re
import helper
import datetime

class ItemList(object):
    def __init__(self, path_object):
        self.path_to_run = path_object
        self.check_button_list = []
        self.lbl_renamed_list = []
        self.path_object_list = []
        self.selected_options = []
        # if path object in this dict, means it shall be renamed
        self.path_object_to_renamed_path_object = {}
        self.search = ""
        self.replace = ""
        self.show_checked_only = False
        self.show_renamed_only = False

        # reference to objects from other classes assigned at main.py
        self.canvas_frame = None
        self.scroll_bar_frame = None
        self.bottom_frame = None

    def update_rename_column(self):
        """
        Update the list of items that are showing at the renamed column based on the selected options, the searched value and replace with value
        """
        enumerate_index = 1
        self.path_object_to_renamed_path_object.clear()

        for (path_object, level), check_button, label in zip(
            self.path_object_list, self.check_button_list, self.lbl_renamed_list
        ):
            skip = False
            match = True
            item_name = check_button["text"]
            new_name = item_name

            # user did not select this file/dir to rename
            if not check_button.val.get():
                skip = True

            # path_object match one of the following statements so should not be renamed
            if "Exclude Folders" in self.selected_options and path_object.is_dir():
                skip = True
            elif "Exclude Files" in self.selected_options and path_object.is_file():
                skip = True
            elif "Exclude Subfolder Items" in self.selected_options and level >= 1:
                skip = True

            if skip:
                logging.debug(f"file name: {item_name} is skipped")
                label.config(text="")
                continue

            # the scope of the name that shall be renamed
            if "Item Name Only" in self.selected_options:
                new_name = path_object.stem
            elif "Item Extension Only" in self.selected_options:
                new_name = path_object.suffix

            if "Case Sensitive" in self.selected_options:
                if self.search != "" and self.search in new_name:
                    match = True
                    if "Match All Occurences" in self.selected_options:
                        new_name = new_name.replace(self.search, self.replace)
                    else:
                        new_name = new_name.replace(self.search, self.replace, 1)
            else:
                # since regex is not enabled, escape all special characters
                search_name = re.escape(self.search)
                if search_name != "" and re.search(
                    search_name, new_name, flags=re.IGNORECASE
                ):
                    match = True
                    if "Match All Occurences" in self.selected_options:
                        new_name = re.sub(
                            search_name, self.replace, new_name, flags=re.IGNORECASE
                        )
                    else:
                        new_name = re.sub(
                            search_name, self.replace, new_name, 1, flags=re.IGNORECASE
                        )

            if not match:
                logging.debug("file name: {item_name} has no match for search input")

            # Note, even if there are no matches, if any one of the case change options match, we will still rename the original name
            if "Make Uppercase" in self.selected_options:
                if new_name != new_name.upper():
                    new_name = new_name.upper()
            elif "Make Lowercase" in self.selected_options:
                if new_name != new_name.lower():
                    new_name = new_name.lower()
            elif "Make Titlecase" in self.selected_options:
                old_path = path_object
                # titlecase should have no effect on item extension
                if old_path.stem != new_name.title():
                    new_name = new_name.title()

            # if one of the following options are valid, means we actually edited either the name (stem) or the extension path,
            # so we need to add back the name (stem) or the extension
            if "Item Name Only" in self.selected_options:
                new_name = new_name + path_object.suffix
            elif "Item Extension Only" in self.selected_options:
                new_name = path_object.stem + new_name

            if "Enumerate Items" in self.selected_options:
                # only apply to name that will be renamed
                if new_name != item_name:
                    # making the new_name as Path object just to get the stem and suffix more easily
                    new_path = pathlib.Path(new_name)

                    # special consideration for name start with "." like ".gitignore", path_instance.stem() return ".gitignore" instead of empty
                    if new_path.stem[0] == ".":
                        new_name = f"({enumerate_index}){new_name}"
                    else:
                        new_name = (
                            f"{new_path.stem} ({enumerate_index}){new_path.suffix}"
                        )
                    enumerate_index += 1

            if new_name != item_name:
                logging.debug(
                    f"old name: {item_name} will be renamed to new name: {new_name}"
                )
                new_name = helper.replace(path_object, new_name)
                renamed_path_object = path_object.parent / new_name
                self.path_object_to_renamed_path_object[
                    path_object
                ] = renamed_path_object
                logging.debug(renamed_path_object.name)

                label.config(text=renamed_path_object.name)
            else:
                label.config(text="")

    def update_search(self, value):
        """
        Update the search keyword. As search keyword is updated, the previously items that are eligible to be renamed might not longer be valid.
        So we need the update the appropriate components.
        """
        self.search = value
        self.update_rename_column()
        self.update_display_widgets()
        self.update_renaming_count()

    def update_replace(self, value):
        """
        Update the replace value. Since no other components will be affected, we just need to update the renamed column.
        """
        self.replace = value
        self.update_rename_column()

    def update_selected_count(self):
        """
        Update the selected count UI at bottom left corner
        """
        total = 0
        for check_button in self.check_button_list:
            if check_button.val.get():
                total += 1

        self.bottom_frame.lbl_items_selected.config(text=f"Items Selected: {total}")
        logging.debug(f"Selected count: {total}")

    def update_renaming_count(self):
        """
        Update the renaming count UI at bottom left corner
        """
        total = 0
        for label in self.lbl_renamed_list:
            if label["text"] != "":
                total += 1

        self.bottom_frame.lbl_items_renaming.config(text=f"Item Renaming: {total}")
        self.bottom_frame.update_btn_rename_state(total)
        logging.debug(f"Renaming count: {total}")

    def check_button_callback(self, index):
        """Callback for each check button in the original column """

        check_button = self.check_button_list[index]
        logging.debug(f"button, {check_button['text']} clicked")

        self.update_rename_column()
        self.update_display_widgets()
        self.update_selected_count()
        self.update_renaming_count()

    def update_show_renamed_only(self):
        """
        Trigger to show all selected items or show only items that will be renamed
        """
        logging.debug("called")
        # only one of them can be True at any time
        self.show_renamed_only = not self.show_renamed_only
        self.show_checked_only = False
        self.update_display_widgets()
        self.update_renaming_count()

    def update_show_checked_only(self):
        """
        Trigger to show all items or show only checked items
        """
        logging.debug("called")
        # only one of them can be True at any time
        self.show_checked_only = not self.show_checked_only
        self.show_renamed_only = False
        self.update_display_widgets()

    def update_display_widgets(self):
        """Display the items in both original and renamed columns"""
        logging.debug("called")

        # clear everything on the grid
        for _, (check_button, lbl_renamed, path_object) in enumerate(
            zip(self.check_button_list, self.lbl_renamed_list, self.path_object_list)
        ):
            check_button.grid_forget()
            lbl_renamed.grid_forget()

        # populate the grid
        line = 0
        for check_button, lbl_renamed, path_object in zip(
            self.check_button_list, self.lbl_renamed_list, self.path_object_list
        ):
            # show checked rows only
            if self.show_checked_only and not check_button.val.get():
                continue
            # show rows that will be renamed only
            elif self.show_renamed_only and not lbl_renamed["text"]:
                continue

            _, level = path_object

            check_button.grid(
                # +1 to exclude the first row which contain the columns name
                row=line + 1,
                column=0,
                # left padding for file in inner directories
                ipadx=(20 * level,),
                pady=1,
                sticky="w",
            )
            lbl_renamed.grid(
                row=line + 1,
                column=1,
                sticky="w",
            )
            line += 1

    def rename(self):
        for (
            path_object,
            renamed_path_object,
        ) in self.path_object_to_renamed_path_object.items():
            try:
                path_object.rename(renamed_path_object)
                logging.info(f"success: {path_object} renamed to {renamed_path_object}")
            except:
                logging.info(
                    f"failed: {path_object} not able to rename to {renamed_path_object}"
                )

    def create_widgets(self):
        """Create all widgets from the populated path objects list that we have gotten"""

        for index, (path_object, _) in enumerate(self.path_object_list):
            # used to represent value of checkbox, 1 or 0, set default to 1
            int_var = tk.IntVar(value=1)
            check_button = tk.Checkbutton(
                self.canvas_frame,
                text=path_object.name,
                variable=int_var,
                bg="white",
                command=lambda index=index: self.check_button_callback(index),
            )
            check_button.val = int_var

            lbl_renamed = tk.Label(self.canvas_frame, text="", bg="white")

            self.check_button_list.append(check_button)
            self.lbl_renamed_list.append(lbl_renamed)

    def update_options(self, options):
        """
        Update the options that are selected
        """
        logging.debug("called")
        self.selected_options = options
        self.update_rename_column()
        self.update_display_widgets()
        self.update_renaming_count()

    def get_items(self):
        """
        Populate the UI with initial state of the program, i.e. all file/folder names in the directory of where this program was runned
        """
        self._get_items(
            pathlib.Path(self.path_to_run),
            0,
        )
        self.create_widgets()
        self.update_display_widgets()
        self.update_selected_count()
        self.update_renaming_count()
        # ensure the UI is updated
        self.canvas_frame.update_idletasks()
        self.scroll_bar_frame.on_canvas_configure(None)

    def _get_items(self, path, level):
        """
        Starting from root, get_items go into sub folders and retrieve the pathlib object that represent each of the folders and files.
        By default, process the folders first before files

        Keyword arguments:
        path - path object of a folder
        level - level of this folder starting from the directory of where this program was runned
        """
        dir_path_objects = [x for x in path.iterdir() if x.is_dir()]
        for dir_path_object in dir_path_objects:
            self.path_object_list.append((dir_path_object, level))
            new_path = pathlib.Path(f"{dir_path_object.resolve()}")
            self._get_items(new_path, level + 1)

        file_path_objects = [x for x in path.iterdir() if x.is_file()]
        for file_path_object in file_path_objects:
            self.path_object_list.append((file_path_object, level))


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

# In[ ]:

# D:\GitHub\Flower_Classifier_Project\flower_data\valid\23\image_03416.jpg
# D:\GitHub\Flower_Classifier_Project

# In[23]:

import torch
import os
from torchvision import transforms
from matplotlib import pyplot as plt
from torch import nn, optim
import numpy as np
import pickle
import json
import pandas as pd
from IPython.display import display
import numpy as np
import string
from PIL import Image
import argparse
import torchvision
from matplotlib import cm

# In[51]:

parser = argparse.ArgumentParser()

parser.add_argument('image_path', metavar='image_path', type=str, nargs='?',
                    help='The path of the image to be loaded to the prediction model.')

parser.add_argument('checkpoint_path', metavar='checkpoint_path', type=str, nargs='?',
                    help='The path of the  checkpoint.pth that contains the model information.')

parser.add_argument('--cat', action='store',
                    dest='cat_to_name',
                    help='Store the name of the json file with category names of the flowers.')

parser.add_argument('--topk', action='store', type=int,
                    dest='topk',
                    help='Store the top k predictions wanted in the output.')

parser.add_argument('--gpu', action="store_true", default=False,
                   help='Set the model to predicut using the GPU')

'''parser.add_argument('--device', action='store',
                    dest='device',
                    help='Store the device in which the prediction will run.')'''

results = parser.parse_args()

if results.gpu:
    results.device = 'cuda'
else:
    results.device = 'cpu'

print('\nCommand line selections:')
print('image_path = {!r}'.format(results.image_path))
print('checkpoint_name = {!r}'.format(results.checkpoint_path))
print('cat_to_name = {!r}'.format(results.cat_to_name))
print('topk = {!r}'.format(results.topk))
print('device = {!r}'.format(results.device))
print('-')

print('Command line warnings:')
if results.image_path == None: print('No image parsed. You will have the chance to select an image as an input.')
if results.checkpoint_path == None: print('No checkpoint path given. The root cehckpoint.pth will be used. ')
if results.cat_to_name == None: print('No --cat parsed. You will have the chance to input categories later.')
if results.topk == None: print('No --topk parsed. You will have the chance to input the top k later.')
'''if results.device == None: print('No --device parsed. You will have the chance to input the device later.')'''
print('-')

# command line checks
'''if results.device not in ('cuda', 'cpu'):
    results.device = None
    print('Wrong device input. Please use the command input to select desired device.')'''
if results.topk:
    if results.topk >= 34 or results.topk <=2:
        results.topk = None
        print('Wrong topk input. Please use the command input to select desired topk from 2 to 33.')

# # Helper Functions

# In[26]:

def pretty_flower_frame(cat_to_name):
    """
    A clean way to see the flower mappings to be able to validate the model easier

    Parameters
    ----------
    cat_to_name: a dictionary mapping the integer encoded categories to the actual names of the flowers

    display dataframe
    returns None
    """
    is_tensor = [True for value in cat_to_name.values() if value == 0]
    is_tensor = len(is_tensor) > 0

    # setting key names
    if is_tensor:
        key_name = 'folder_number'
        value_name = 'tensor_id'
    else:
        key_name = 'index'
        value_name = 'flower_name'

    # creating dict
    flower_dict = {key_name: [], value_name: []}
    for key, value in cat_to_name.items():
        flower_dict[key_name].append(key)
        flower_dict[value_name].append(value)

    # creating pandas dataframe
    pd.set_option('display.max_rows', 500)
    df = pd.DataFrame(flower_dict)
    df.set_index(key_name, inplace=True)
    df = (df.sort_values(value_name))
    df.reset_index(key_name, inplace=True)

    # combining all frames
    df1 = df.iloc[:26, :]
    df2 = df.iloc[26:52].reset_index(drop=True)
    df3 = df.iloc[52:78, :].reset_index(drop=True)
    df4 = df.iloc[78:102, :].reset_index(drop=True)

    display(pd.concat([df1, df2, df3, df4], axis=1).fillna(''))

# In[27]:

def select_image(cat_id):
    """
    Parameters
    ----------
    cat_id: the flower id given by the respective folder number given in the flower_data folder

    return selected image path with image file name

    Note: use this to load the image path in the predict function
    """
    image_path = os.getcwd() + '\\flower_data\\valid\\{}\\'.format(cat_id)
    images = list((os.listdir(image_path)))
    try:
        images.remove('.ipynb_checkpoints')
    except:
        pass
    i = (np.random.randint(0, len(images)))
    image_name = images[i]
    selected_image = image_path + image_name
    return selected_image

# In[28]:

def predict(image_path, model, topk=5, show=True):
    """
    Parameters
    ----------
    image_path: the selected picture path with image file name
        use select_image function
    model: the trained model or loaded trained model
    topk: the top k prediction by the greatest probability -default 5
    show: to decide if just the probs and to_classes should be returned without
        printing or showing images -default True

    shows picture of flower
    prints actual flower name
    returns list of probabilities and top classes by index and name

    Note: the index returned in top_classes can be used in the select_image function
        to return path with picture file name of the respective flower
    """
    image, cat_id = process_image(image_path)

    # prep the images to show
    actual_flower = image[:]

    # getting root of the image file
    image_path = ('\\'.join(image_path.split('\\')[:-2]))

    #  continue processing and returning outputs
    image = image.unsqueeze_(0)
    image = image.to(device)
    model.to(device)
    model.eval()
    with torch.no_grad():
        outputs = torch.exp(model.forward(image))

    probs, classes = outputs.topk(topk)

    probs = probs.to('cpu').numpy().reshape(-1)
    tensoridx = classes.to('cpu').numpy().reshape(-1,)

    # brings a dictionary of tensor index to cat id
    tensoridx_to_catid = {val: key for key, val in model.class_to_idx.items()}

    # brings category name from category id mapping to tensor index
    top_classes = [tensoridx_to_catid[idx] + ' - ' + str(cat_to_name[tensoridx_to_catid[idx]]) for idx in tensoridx]

    # showing pictures
    flower_ids = [tensoridx_to_catid[idx] for idx in tensoridx]
    first_image_path = os.path.join(image_path, flower_ids[0])
    second_image_path = os.path.join(image_path, flower_ids[1])

    # first image validation
    first_flower_filename = os.listdir(first_image_path)[0]
    if first_flower_filename == '.ipynb_checkpoints':
        first_flower_filename = os.listdir(first_image_path)[1]

    # second image validation
    second_flower_filename = os.listdir(second_image_path)[0]
    if second_flower_filename == '.ipynb_checkpoints':
        second_flower_filename = os.listdir(second_image_path)[1]

    first_image_path = os.path.join(first_image_path, first_flower_filename)
    second_image_path = os.path.join(second_image_path, second_flower_filename)

    first_image = process_image(first_image_path)[0]
    second_image = process_image(second_image_path)[0]

    accurate = ' - Correct'
    color = 'black'
    if str(cat_id) != flower_ids[0]:
        accurate = ' - Incorrect'
        color = 'red'

    if show:
        title_enhancer('Actual: ' + cat_to_name[cat_id] + accurate, size=20, color=color)
        title_enhancer('1st: Actual | 2nd: First Prediction | 3rd: Second Prediction', 14)
    if show: imshow(actual_flower, first_image, second_image)

    if show: plt_bar_chart(probs, top_classes)

    return list(probs), top_classes

# In[29]:

def tensor_normalizer(tensor_img):

    tensor_norm = tensor_img

    # extracting channels
    img_a = tensor_norm[0]
    img_b = tensor_norm[1]
    img_c = tensor_norm[2]

    # normalizing
    img_a = (img_a - 0.485)/(0.229)
    img_b = (img_b - 0.456)/(0.224)
    img_c = (img_c - 0.406)/(0.225)

    # assigning to numpy matrixes
    tensor_norm[0] = img_a
    tensor_norm[1] = img_b
    tensor_norm[2] = img_c

    return tensor_norm

# In[30]:

def process_image(image_path):
    """
    Scales, crops, and normalizes a PIL image for a PyTorch model

    Parameters
    ----------
    image_path: the path of the image with the image file name

    returns transformed image and category id
    """
    cat_id = (str(image_path.split('\\')[-2]))

    # TODO: Process a PIL image for use in a PyTorch model
    img = Image.open(image_path)

    # transformer to tensor
    data_transforms = transforms.Compose([transforms.ToTensor()])

    # resize
    size = 256, 256
    img_tran = img.resize(size, Image.ANTIALIAS)

    # center crop
    width, height = img_tran.size
    top, down = [(height-224)/2]*2
    left, right = [(width-224)/2]*2
    img_tran = img_tran.crop((left, top, width-right, height-down))

    tensor_img = data_transforms(img_tran)

    '''    # to show normalized image
    data_transformsPIL = transforms.Compose([
                                  transforms.ToPILImage()
                                  ])

    data_transformsPIL(tensor_img).show()

    data_transformsPIL(tensor_normalizer(tensor_img)).show()'''

    return tensor_img, cat_id

# In[31]:

def process_image_shortcut(image_path):
    """
    Scales, crops, and normalizes a PIL image for a PyTorch model

    Parameters
    ----------
    image_path: the path of the image with the image file name

    returns transformed image and category id
    """
    cat_id = (str(image_path.split('\\')[-2]))

    # TODO: Process a PIL image for use in a PyTorch model
    im = Image.open(image_path)

    # TODO: Process a PIL image for use in a PyTorch model
    data_transforms = transforms.Compose([transforms.Resize(256),
                                      transforms.CenterCrop(224),
                                      transforms.ToTensor(),
                                      transforms.Normalize(mean=[0.485, 0.456,0.406], std=[0.229, 0.224, 0.225])])

    '''    data_transformsPIL = transforms.Compose([
                                  transforms.ToPILImage()
                                  ])
    data_transformsPIL(data_transforms(im)).show()'''

    return data_transforms(im), cat_id

# In[32]:

def pretty_output(output):
    df = pd.DataFrame({'Flowers': output[1], 'Probabilities': output[0]})
    df.Probabilities = df.Probabilities.apply(lambda x: '{:.2%}'.format(x))
    df.index = df.index + 1
    display(df)

# # Re-building Model

# In[ ]:

def model_rebuilder(checkpoint_name):

    def run_load(checkpoint_dict):
        model_load = getattr(torchvision.models, checkpoint_dict['model'])(pretrained=True)
        model_load.classifier = checkpoint_dict['classifier']
        model_load.load_state_dict(checkpoint_dict['state_dict'])
        model_load.class_to_idx = checkpoint_dict['class_to_idx']
        return model_load
        print('Checkpoint loaded :)')
        print('-')

    try:
        return run_load(torch.load(checkpoint_name))
    except:
        try:
            print('The default checkpoint.pth was loaded successfully.')
            print('-')
            return run_load(torch.load('checkpoint.pth'))
        except:
            print('No checkpoint found. ')

model_load = model_rebuilder(results.checkpoint_path + '\\checkpoint.pth')

# # Looking for Category to Name File

# In[35]:

def category_to_name_validation(filename='cat_to_name.json'):

    if results.cat_to_name != None:
        filename=results.cat_to_name
        with open(filename, 'r') as handle:
            cat_to_name = json.load(handle)
            return cat_to_name
        print('The argument -cat {} was used.         Names will be shown instead of category ids'.format(results.cat_to_name))
    else:
        try:
            with open(filename, 'r') as handle:
                cat_to_name = json.load(handle)
                print('A json file named cat_to_name.json was found. Names will be shown instead of category ids.')
                return cat_to_name
        except:
            print('No {} file found. Try again'.format(filename))
            cat_to_name_filenam = input(' Do you have a category-to-flower-name-mapping json file?            \n enter filename.json or no ->')
            if cat_to_name_filenam.lower() not in ('', 'no'):
                try:
                    return category_to_name_validation(cat_to_name_filenam)
                except:
                    pass
            else:
                print('Warning! You have opted to not show flower names. Instead, the flower category ids will be shown. n\These are the same as the folders ids in which the flower images are located.')
                return model_load.class_to_idx

cat_to_name = category_to_name_validation()
print('-')

# # Questionnaire

# In[37]:

def device_validation():

    if results.device == None:
        if torch.cuda.is_available():
            device = input('In what device do you want to run the prediction, cuda or cpu? ->')
            if device.lower() in ['cpu', 'cuda']:
                print('Thanks! You selected to run the prediction using the {}.'.format(device))
                return device
            else:
                print('Warning! Wrong input. Choose cuda or cpu.')
                return device_validation()
        else:
            device = 'cpu'
            print('Sorry! But your device does not support GPU. Note that the prediction will run faster with a GPU.             Please, consider changing to a device with a GPU.')
    else:
        return results.device
device = device_validation()
print('-')

# In[38]:

def topk_validation():
    if results.topk == None:
        try:
            topk = int(input('How many top outputs do you want to see?             \nenter an integer -->'))
            if topk >=2 and topk <= 33:
                print('Thanks! Your output will show {} of the top predictions.'.format(topk))
                return topk
            else:
                print('Try again! Please enter integer from 2 to 33.')
                return topk_validation()
        except:
            print('Please try again! Enter an integer.')
            return topk_validation()
    else:
        return results.topk
topk = topk_validation()

# # Predicting

# In[39]:

print('These is the List of Flower Images:')
pretty_flower_frame(cat_to_name)

# In[52]:

def show_outputs():

    if results.image_path == None:

        try:
            flower_index = int(input('From the flowers in the validation set above, which one do you want to predict?            \n enter the index number or non numeric character to exit ->'))

            if flower_index > 102:
                print('\nWarning! The flower index is out of bound. Please enter an index between 1 and 102.')
                show_outputs()

            selected_image = select_image(flower_index)

            print()
            print('\nPredicting: ' + str(flower_index) + ' - ' +
                  string.capwords(str(cat_to_name[str(flower_index)])))

            output = predict(selected_image, model_load, topk, show=False)

            pretty_output(output)

            continue_pred = input('\nDo you want to predict another flower name? yes or no ->')

            def continue_prediction(continue_pred):
                if continue_pred.lower() == 'yes':
                    show_outputs()
                elif continue_pred.lower() == 'no':
                    print('Ok! The application will now close.')
                else:
                    print('Try again:')
                    continue_pred = input('Do you want to predict another flower name? yes or no ->')
                    continue_prediction(continue_pred)

            continue_prediction(continue_pred)

        except Exception as e:
            print('Good bye!')
            pass

    else:
        try:
            output = predict(results.image_path, model_load, topk, show=False)
            pretty_output(output)
        except:
            print('Sorry! The image path provided is not valid. But you can choose to predict an image from the list above.')
            results.image_path = None
            show_outputs()

show_outputs()

# In[ ]:


import numpy as np
import torch
from torch.utils.data import DataLoader

from backbone.inc_net import IncrementalNet
from methods.base import BaseLearner
from utils.toolkit import accuracy, mean_class_recall, count_parameters, cal_ece, tensor2numpy

EPSILON = 1e-8

'''
新方法命名规则:
python文件(方法名小写)
类名(方法名中词语字母大写)
'''

# base is finetune with or without memory_bank
class Finetune_normal(BaseLearner):

    def prepare_task_data(self, data_manager):
        # self._cur_task += 1
        # self._cur_classes = data_manager.get_task_size(self._cur_task)
        self._cur_task = data_manager.nb_tasks - 1
        self._cur_classes = data_manager.get_task_size(0)

        self._total_classes = self._known_classes + self._cur_classes

        train_dataset = data_manager.get_dataset(source='train', mode='train', indices=np.arange(self._known_classes, self._total_classes))
        test_dataset = data_manager.get_dataset(source='test', mode='test', indices=np.arange(self._known_classes, self._total_classes))

        self._logger.info('Train dataset size: {}'.format(len(train_dataset)))
        self._logger.info('Test dataset size: {}'.format(len(test_dataset)))

        self._train_loader = DataLoader(train_dataset, batch_size=self._batch_size, shuffle=True, num_workers=self._num_workers)
        self._test_loader = DataLoader(test_dataset, batch_size=self._batch_size, shuffle=False, num_workers=self._num_workers)

        if self._use_valid:
            valid_dataset = data_manager.get_dataset(source='valid', mode='test', indices=np.arange(self._known_classes, self._total_classes))
            self._valid_loader = DataLoader(valid_dataset, batch_size=self._batch_size, shuffle=False, num_workers=self._num_workers)
            self._logger.info('Valid dataset size: {}'.format(len(valid_dataset)))

        self._criterion = self._get_criterion(self._criterion_name)

    def prepare_model(self, checkpoint=None):
        if self._network == None:
            self._network = IncrementalNet(self._logger, self._config.backbone, self._config.pretrained, self._config.pretrain_path)
        self._network.update_fc(self._total_classes)
        if checkpoint is not None:
            self._network.load_state_dict(checkpoint['state_dict'])
            self._logger.info("Loaded checkpoint model's state_dict !")
        if self._config.freeze_fe:
            self._network.freeze_FE()
        self._logger.info('All params: {}'.format(count_parameters(self._network)))
        self._logger.info('Trainable params: {}'.format(count_parameters(self._network, True)))
        self._network = self._network.cuda()

    def _epoch_train(self, model, train_loader, optimizer, scheduler):
        losses = 0.
        correct, total = 0, 0
        model.train()
        for _, inputs, targets in train_loader:
            inputs, targets = inputs.cuda(), targets.cuda()

            logits, feature_outputs = model(inputs)
            loss = self._criterion(logits, targets)
            preds = torch.max(logits, dim=1)[1]

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            losses += loss.item()

            correct += preds.eq(targets).cpu().sum()
            total += len(targets)

        if scheduler != None:
            scheduler.step()
        train_acc = np.around(tensor2numpy(correct)*100 / total, decimals=2)
        train_loss = ['Loss', losses/len(train_loader)]
        return model, train_acc, train_loss

    def _epoch_test(self, model, test_loader, ret_pred_target=False):
        cnn_correct, total = 0, 0
        cnn_pred_all, target_all, features_all = [], [], []
        cnn_max_scores_all = []
        model.eval()
        for _, inputs, targets in test_loader:
            inputs, targets = inputs.cuda(), targets.cuda()
            outputs, feature_outputs = model(inputs)
            cnn_max_scores, cnn_preds = torch.max(torch.softmax(outputs, dim=-1), dim=-1)

            if ret_pred_target:
                cnn_pred_all.append(tensor2numpy(cnn_preds))
                target_all.append(tensor2numpy(targets))
                features_all.append(tensor2numpy(feature_outputs['features']))
                cnn_max_scores_all.append(tensor2numpy(cnn_max_scores))
            else:
                cnn_correct += cnn_preds.eq(targets).cpu().sum()
                total += len(targets)

        if ret_pred_target:
            cnn_pred_all = np.concatenate(cnn_pred_all)
            target_all = np.concatenate(target_all)
            features_all = np.concatenate(features_all)
            cnn_max_scores_all = np.concatenate(cnn_max_scores_all)
            return cnn_pred_all, cnn_max_scores_all, target_all, features_all
        else:
            test_acc = np.around(tensor2numpy(cnn_correct)*100 / total, decimals=2)
            return test_acc

    def eval_task(self):
        self._logger.info(50*"-")
        self._logger.info("log {} of the task".format(self._eval_metric))
        self._logger.info(50*"-")
        cnn_pred, cnn_pred_score, y_true, features = self._epoch_test(self._network, self._test_loader, True)

        if self._eval_metric == 'acc':
            cnn_total, cnn_task = accuracy(cnn_pred.T, y_true, self._total_classes, self._increment_steps)
        elif self._eval_metric == 'mcr':
            cnn_total, cnn_task = mean_class_recall(cnn_pred.T, y_true, self._total_classes, self._increment_steps)
        else:
            raise ValueError('Unknown evaluate metric: {}'.format(self._eval_metric))
        self._logger.info("Final Test Acc: {:.2f}".format(cnn_total))
        self._logger.info("The Expected Calibration Error (ECE) of the model: {:.4f}".format(cal_ece(cnn_pred, cnn_pred_score, y_true)))
        self._logger.info(' ')

        self._known_classes = self._total_classes


import subprocess
import json
import urllib.request
from subprocess import Popen, PIPE
wifioutput = subprocess.check_output(['netsh', 'wlan', 'show', 'interfaces']).decode('UTF-8').splitlines()
wificonnected = '    Network type           : Infrastructure'

status= ('lightgreen','yellow','red')
props = ('Signal','Radio','BSSID:','SSID:','Transmit', 'Receive','Channel')
networkArr = ('IP', 'Subnet', 'Default')
data = []
ndata = []

def format_data(line):
	for prop in props:
		if (prop in str(line)):
			signal = line,
			tuplesignal = [tuple(i.split(': ')) for i in signal]
			signaldict = dict((x, y) for x, y in tuplesignal)
			signaldict = {k.strip():  v for k,v in signaldict.items()}
			# print(signaldict) # for debug use to see values with there prop
			return signaldict[prop.replace(":","")]

if wificonnected in wifioutput:
	for line in wifioutput:
		pLine = format_data(line)
		if pLine is not None:
			data.append(pLine)
		else:
			pass

		print ("Quality:", data[0], "|", "Band:", data[1], "|", "SSID:", data[3], "|", "BSSID:", data[2], "|", "Tx/Rx Rate:", data[4], "/", data[5], "Mbps", "|", "Channel:", data[6])
else:
	print("Disconnected")

def network_func(line):
	for n in networkArr:
		if n in str(line):
			tupleIP = [tuple(i.split(': ')) for i in line]
			ipdict = dict((x, y) for x, y in tupleIP)
			ipdict = {k.strip(): v for k,v in ipdict.items()}
			# print(ipdict) # for debug use to see values with there prop
			return ipdict[n.replace(":", "")]

ipoutput = subprocess.check_output(['netsh', 'interface', 'ipv4', 'show', 'addresses', 'name=Wi-Fi']).decode('UTF-8').splitlines()
dhcpconnected = '    DHCP enabled:                         Yes'

if wificonnected in wifioutput:
	if dhcpconnected in ipoutput:
		for line in ipoutput:
			nLine = network_func(line)
			if nLine is not None:
				ndata.append(nLine.lstrip(' '))
			else:
				pass
		print("IP Address:", ndata[0], "|", "Subnet:", ndata[1], "|", "Gateway:", ndata[2])
	else:
		print("Static or Disconnected")
else:
	print("No IP Address")

########################## Meraki AP Detection and Info #######################################################
try:
		apresp = urllib.request.urlopen('http://ap.meraki.com/index.json').read()
		merakiapjson = json.loads(apresp)
		if 'error' not in merakiapjson:
				SignaldB = merakiapjson['client']['rssi']
				MerakiNetwork = merakiapjson['config']['network_name']
				MerakiAP = merakiapjson['config']['node_name']
				MerakiModel = merakiapjson['config']['product_model']
				MerakiAPIP = merakiapjson['connection_state']['wired_ip']
				print ("SNR at AP:", SignaldB,"dB","|","AP Name:", MerakiAP,"|","AP IP:", MerakiAPIP, "|", "AP Model:", MerakiModel, "|", "Meraki Network:", MerakiNetwork)
		else:
				print("Not Connected to an MR")
except:
		print ("Not Connected to an MR")
################################ Meraki Switch Detection and Info ###################################################
try:
		switchresp = urllib.request.urlopen('http://switch.meraki.com/index.json').read()
		merakiswitchjson = json.loads(switchresp)
		if 'error' not in merakiswitchjson:
				SwitchName = merakiswitchjson['config']['node_name']
				SwitchModel = merakiswitchjson['config']['product_model']
				SwitchIP = merakiswitchjson['connection_state']['wired_ip']
				SwitchVLAN = merakiswitchjson['client']['vlan']
				SwitchPort = merakiswitchjson['client']['port']
				print("Port:", SwitchPort, "|", "VLAN:", SwitchVLAN, "|", "Switch Name:", SwitchName, "|", "Switch IP:", SwitchIP, "|", "Switch Model:", SwitchModel)
		else:
				print("Not Connected to an MS")
except:
		print ("Not Connected to an MS")
############################# Meraki Firewall Detection and Info ################################################################
try:
		firewallresp = urllib.request.urlopen('http://wired.meraki.com/index.json').read()
		merakifirewalljson = json.loads(firewallresp)
		if 'error' not in merakifirewalljson:
				FirewallName = merakifirewalljson['config']['node_name']
				FirewallModel = merakifirewalljson['config']['product_model']
				FirewallIP = merakifirewalljson['connection_state']['wired_ip']
				print("Firewall Name:", FirewallName, "|", "Firewall IP:", FirewallIP, "|", "Model:", FirewallModel)
		else:
				print("Not Connected to an MX")
except:
		print ("Not Connected to an MX")


from utils import isprime

num = 600851475143
divs = []

for i in range(2 , int(num ** 0.5 + 0.5) + 5) :
    if num % i == 0 :
        divs.append(i)
        divs.append(num // i)

divs.sort(reverse = True)

for i in divs :
    if isprime(i) :
        print(i)
        break


class Solution:
    def permuteUnique(self, nums):
        res, n = [], len(nums)
        nums.sort()
        self.recursion(nums, n, [], res)
        return res
    def recursion(self, arr, length, cur, ans):
        if len(cur) == length:
            ans.append(cur)
            return
        for i in range(len(arr)):
            if i > 0 and arr[i] == arr[i-1]:
                continue
            pivot = arr[i]
            remaining = arr[:i] + arr[i+1:]
            self.recursion(remaining, length, [pivot]+cur, ans)


from django.conf.urls import url
from django.contrib import admin
from volt import views
from tech import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$',views.index,name='index'),
    url(r'^index',views.index,name='index'),
    url(r'^studentlogin/',views.student_login,name='studentlogin'),
    url(r'^student_signup/',views.student_signup,name='studentsignup'),
    url(r'^login/',views.employees,name='employees'),
    url(r'^adminlogin/',views.admin,name='admin'),
    url(r'^createnewaccount/',views.jobseeker,name='jobseeker'),
    url(r'^forgetpassword/',views.forgetpassword,name='forgetpassword'),
    url(r'^exammainpage/',views.exammainpage,name='exammainpage'),
    url(r'^javaexam/',views.javaexam,name='javaexam'),
    url(r'^pythonexam/',views.pythonexam,name='pythonexam'),
    url(r'^phpexam/',views.phpexam,name='phpexam'),
    url(r'^signup/',views.signup,name='signup'),
    url(r'^applynow/',views.applynow,name='applynow'),
    url(r'^freshersignup/',views.fresher_signup,name='freshersignup'),
    url(r'^phpbasictest/',views.phpbasictest,name='phpbasictest'),
    url(r'^phpmocktest/',views.phpmocktest,name='phpmocktest'),
    url(r'^pythonbasictest/',views.pythonbasictest,name='pythonbasictest'),
    url(r'^pythonmocktest/',views.pythonmocktest,name='pythonmocktest'),
    url(r'^javabasictest/',views.java_basictest),
    url(r'^javamocktest/',views.javamocktest,name='javamocktest'),
    url(r'^hrmanagerlogin/',views.hrmanagerlogin,name='hrmanagerlogin'),
    url(r'^technicalmanagerlogin/',views.technicalmanagerlogin,name='technicalmanagerlogin'),
    url(r'^hrpage/',views.hrpage,name='hrpage'),
    url(r'^teamleaderpage/',views.teamleaderpage,name='teamleaderpage'),
    url(r'^technicalmanagerpage/',views.technicalmanagerpage,name='technicalmanagerpage'),
    url(r'^teamleaderlogin/',views.teamleaderlogin,name='teamleaderlogin'),
    url(r'^experiencesignup/',views.experiencesignup,name='experiencesignup'),
    url(r'^fresherlogin/',views.fresherlogin,name='fresherlogin'),
    url(r'^experiencelogin/',views.experiencelogin,name='experiencelogin'),
    url(r'^admin1/',views.admin1,name='admin1'),
    url(r'^addemployee/',views.ademp,name='addemployee'),
    url(r'^viewemployee/',views.viewemp,name='viewemployee'),
    url(r'^addstudent/',views.addstu,name='addstudent'),
    url(r'^viewstudent/',views.viewstu,name='viewstudent'),

]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

# if settings.DEBUG:
#     urlpatterns += patterns('',
#     	url(r'\^media/(?P<path>.\*)\$', 'django.views.static.serve',
#     		{'document_root': settings.MEDIA_ROOT, }),
# )


# python3
import asyncio, socket
import os, stat, sys
from typing import Optional, List, Union, Callable, Tuple, IO

class Connection:
    async def send(self, buf:bytes) -> None:
        pass
    async def close(self) -> None:
        pass

class DgramUnixConnection(Connection):
    proto: asyncio.DatagramProtocol
    trans: asyncio.DatagramTransport

    async def connect(self, path:Optional[str] = None, sock:Optional[socket.socket] = None) -> None:
        self.proto = asyncio.DatagramProtocol()
        self.trans, _ = await asyncio.get_event_loop().create_datagram_endpoint( # type: ignore
            lambda: self.proto,
            remote_addr = path, # type: ignore
            sock = sock,
            family = socket.AF_UNIX if sock is None else 0
        )

    async def send(self, buf:bytes) -> None:
        self.trans.sendto(buf) #no async, Just write and forget.

    async def close(self) -> None:
        self.trans.close()

class StreamUnixConnection(Connection):

    async def connect(self, path:Optional[str] = None, sock:Optional[socket.socket] = None) -> None:
        self.reader, self.writer = await asyncio.open_unix_connection(
            path = path, sock = sock
        )

    async def send(self, buf:bytes) -> None:
        self.writer.write(buf) #no async, Just write and forget.

    async def close(self) -> None:
        self.writer.close()

class BlockingFileConnection(Connection):
    async def connect(self, fd:IO[bytes]) -> None:
        sys.stderr.write('sdnotify Warning: using blocking file connection\n')
        sys.stderr.flush()
        self.fd = fd

    async def send(self, buf:bytes) -> None:
        self.fd.write(buf)
        self.fd.flush()

    async def close(self) -> None:
        self.fd.close()

class SystemdNotifier:
    conn: Optional[Connection] = None
    debug = False

    def __init__(self, path:Optional[str] = None, unset:bool = True) -> None:
        if path is None:
            self.path = os.getenv('NOTIFY_SOCKET')
        else:
            self.path = path
        if unset:
            os.unsetenv('NOTIFY_SOCKET')

    async def connect(self, path:Optional[str] = None) -> Connection:
        if path is None:
            path = self.path
        if path is None:
            raise Exception("Not running in systemd? no path passed to SystemdNotifier.connect()")
        if path[:1]=='@':
            path = '\0' + path[1:]
        elif path.isdecimal():
            try:
                sock = socket.socket(int(path))
            except Exception:
                pass
            else: # No exception
                if sock.type == socket.SOCK_DGRAM:
                    duc = DgramUnixConnection()
                    await duc.connect(sock = sock)
                    return duc
                if sock.type == socket.SOCK_STREAM:
                    suc = StreamUnixConnection()
                    await suc.connect(sock = sock)
                    return suc
            bfc = BlockingFileConnection()
            await bfc.connect(os.fdopen(int(path), 'wb'))
            return bfc
        elif not stat.S_ISSOCK(os.stat(path).st_mode):
            bfc = BlockingFileConnection()
            await bfc.connect(open(path, 'ab'))
            return bfc
        duc = DgramUnixConnection()
        await duc.connect(path = path)
        return duc

    async def notify(self, state:Union[None, bytes, str] = None, catch:bool = True, **statekw:Union[bytes,str,int]) -> bool:
        send:List[bytes] = []
        for k, v in statekw.items():
            send.append(k.upper().encode('ascii'))
            send.append(b'=')
            if not isinstance(v, bytes):
                v = str(v).encode('utf-8')
            v = v.rstrip(b'\n').replace(b'\n', b'  ')
            send.append(v)
            send.append(b'\n')
        if state:
            if not isinstance(state, bytes):
                state = str(state).encode('utf-8')
            if len(statekw) or b'=' not in state:
                send.append(b'STATUS=')
            if b'\n' in state[:-1]:
                state = state.rstrip(b'\n').replace(b'\n',b'  ')
            send.append(state)
            if state[-1:] != b'\n':
                send.append(b'\n')

        try:
            c = self.conn
            if c is None:
                c = await self.connect()
                self.conn = c
            await c.send(b''.join(send))
        except Exception as e:
            if self.conn:
                try:
                    await self.conn.close()
                except Exception:
                    pass
                finally:
                    self.conn = None
            if not catch:
                raise
            return False
        return True

if __name__ == '__main__':
    import time

    class Test:
        async def test_notify(self, s:SystemdNotifier)->None:
            await s.notify('s:test', catch=False)
            await s.notify(b'b:test', catch=False)
            await asyncio.sleep(.5)
            await s.notify(ready=1,status='s:test',xfoo=b'b:bar', catch=False)
            await asyncio.sleep(.5)

        def run(self) -> None:
            child_pid = -1
            try:
                self.setup()
                sd = SystemdNotifier()
                sd.debug = True
                child_pid = os.fork()
                if child_pid == 0:
                    self.cleanup_fork()
                    asyncio.run(self.test_notify(sd))
                    sys.exit(0)
                loop = True
                while loop:
                    rv = os.waitid(os.P_PID, child_pid, os.WEXITED | os.WNOHANG)
                    if rv is not None:
                        assert rv.si_pid == child_pid
                        if rv.si_code != os.CLD_EXITED:
                            raise Exception(f"Abnormal subprocess termination {rv}")
                        if rv.si_status != 0:
                            raise Exception(f"Subprocess terminated with nonzero exit code {rv.si_status}")
                        loop = False
                    try:
                        data = self.recv()
                        if data is not None:
                            print(f'Received: {data!r}')
                    except BlockingIOError:
                        pass
                    time.sleep(0.1)
            finally:
                if child_pid != 0:
                    self.cleanup()

        def setup(self) -> None: pass
        def cleanup_fork(self) -> None: pass
        def recv(self) -> Optional[bytes]: return None
        def cleanup(self) -> None: pass

    class Test_sock(Test):
        def setup(self) -> None:
            sockname = f'TEST-SOCKET-{os.getpid()}-{time.time()}'
            os.environ['NOTIFY_SOCKET']='@' + sockname
            sockname = '\x00' + sockname
            l = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
            l.bind(sockname)
            l.setblocking(False)
            self.sock = l

        def cleanup_fork(self) -> None:
            self.sock.close()

        def recv(self) -> Optional[bytes]:
            return self.sock.recv(65536)

        def cleanup(self) -> None:
            self.sock.close()

    class Test_fd(Test):
        r:int
        w:int
        def setup(self) -> None:
            self.r, self.w = os.pipe()
            os.environ['NOTIFY_SOCKET']=f'{self.w}'
            import fcntl
            fcntl.fcntl(self.r, fcntl.F_SETFL, fcntl.fcntl(self.r, fcntl.F_GETFL) | os.O_NONBLOCK)

        def cleanup_fork(self) -> None:
            os.close(self.r)

        def recv(self) -> Optional[bytes]:
            return os.read(self.r, 65536)

        def cleanup(self) -> None:
            os.close(self.r)
            os.close(self.w)

    class Test_pipe(Test):
        def setup(self) -> None:
            self.pipename = f'/tmp/test-pipe-{os.getpid()}-{time.time()}'
            os.mkfifo(self.pipename)
            os.environ['NOTIFY_SOCKET']=self.pipename

        def recv(self) -> Optional[bytes]:
            if not hasattr(self, 'r'):
                self.r = open(self.pipename, 'rb')
                import fcntl
                fcntl.fcntl(self.r, fcntl.F_SETFL, fcntl.fcntl(self.r, fcntl.F_GETFL) | os.O_NONBLOCK)
            b = self.r.read()
            return None if b == b'' else b

        def cleanup(self) -> None:
            os.unlink(self.pipename)
            self.r.close()

    if len(sys.argv) > 2:
        arg = 'invalid'
    elif len(sys.argv) == 2:
        arg = sys.argv[1]
    else:
        arg = 'sock'

    from typing import cast, Dict, Type
    t = cast(Dict[str,Type[Test]], globals()).get('Test_' + arg, None)
    if isinstance(t, cast(Type[Test],type)) and issubclass(t, Test):
        t().run()
    else:
        print("Usage: python3 aio_sdnotify.py [sock|fd|pipe]")


import time
import sys

# Vacuum and gripper uses same config
from ..gripper import config_gripper
from .communication_vacuum import VacuumSerial

class Vacuum:
    def __init__(self, usb_port=None):
        # Checking python version
        self.python_2 = (sys.version_info.major == 2)

        # Getting gripper connection class
        self.vacuum_serial = VacuumSerial(usb_port)

        # The vacuum does not need to be activated

    def activate(self, wait=False):
        self.vacuum_serial.activate()
        if wait:
            self.wait()

    def deactivate(self, wait=False):
        self.vacuum_serial.deactivate()
        if wait:
            self.wait()

    def read(self):
        response = self.vacuum_serial.read()
        split = response.split(';')
        register_07D0 = split[0]
        register_07D1 = split[1]
        register_07D2 = split[2]
        return register_07D0, register_07D1, register_07D2

    '''
    # Saved for reference
    # Seems to give presure or something like that
    # No suck = 100
    # Suck ~= 50
    def get_position(self):
        _, _, register_07D2 = self.read()
        return int(register_07D2[0:2], 16)
    '''

    def get_status(self):
        register_07D0, _, _ = self.read()
        status = bin(int(register_07D0[0:2], 16))[2:].zfill(8)
        gOBJ = int(status[0:2], 2)
        gSTA = int(status[2:4], 2)
        gGTO = int(status[4], 2)
        gACT = int(status[7], 2)
        return gOBJ, gSTA, gGTO, gACT

    def wait(self):
        while True:
            gOBJ, _, _, _ = self.get_status()
            if gOBJ != 0:
                break

    def shutdown(self):
        self.gripper_serial.shutdown()
        time.sleep(0.5)


import os
import numpy, json

def getDirList(aDirname, aType):
	filelist = []
	filenames = os.listdir(aDirname)
	for filename in filenames:
		full_filename = os.path.join(aDirname, filename)
		ext = os.path.splitext(full_filename)[-1]
		if ext == aType:
			filelist.append(full_filename)

	return filelist

def readJSON(aPath):
	f = open(aPath, 'r')
	json_text = ""
	while True:
		line = f.readline()
		if not line: break
		json_text += line
	f.close()
	return json_text

def readIntegerArray(aPath):
	f = open(aPath, 'r')
	Arr = f.readline().split(" ")
	return Arr


from sys import path
import os.path
path.insert(1,os.path.abspath('./stacksAndQueues'))
from queue import Queue
from graph import Graph

def bfs_helper(g: Graph,source:int,visited:list):
    result = ''
    q = Queue()
    q.enqueue(source)
    visited[source]= True
    while not q.is_empty():
        vertex = q.dequeue()
        result += str(vertex)
        node = g.array[vertex].get_head()
        while node:
            if not visited[node.value]:
                q.enqueue(node.value)
                visited[node.value] = True
            node = node.next
    return result,visited

def bfs(g:Graph,source:int):
    visited = [False]*g.vertices
    result = ''
    result,visited = bfs_helper(g,source,visited)
    for i in range(g.vertices):
        if not visited[i]:
            result_new,visited = bfs_helper(g,i,visited)
            result += result_new
    return result

if __name__ == "__main__":
    g = Graph(4)
    g.add_edge(0,1)
    g.add_edge(0,2)
    g.add_edge(1,3)
    g.add_edge(2,3)
    print(bfs(g,0))


# Problem: https://leetcode.com/problems/decode-ways/

# General approach: Iteratively process string from right to left
# Time complexity: O(n)
# Space complexity: O(n) (can be made smaller by throwing away old values)

from typing import List, Set

class Solution:
    def numDecodings(self, s: str) -> int:
        sLen = len(s)
        # Corner cases
        if sLen == 0 or s.startswith('0'):
            return 0
        if sLen == 1:
            return 1

        charEncodings: Set[str] = {'1', '2', '3', '4', '5', '6', '7', '8',
                                   '9', '10', '11', '12', '13', '14', '15',
                                   '16', '17', '18', '19', '20', '21', '22',
                                   '23', '24', '25', '26',
                                   }

        tracker: List[int] = [0] * sLen

        # Process counts for last and second last character substrings
        if s[sLen-1] in charEncodings:
            tracker[sLen-1] = 1
        if s[sLen-2] in charEncodings:
            tracker[sLen-2] += tracker[sLen-1]
        if s[sLen-2:] in charEncodings:
            tracker[sLen-2] += 1

        for i in range(sLen-3, -1, -1):
            count: int = 0
            if s[i] in charEncodings:
                count += tracker[i+1]
            if s[i: i+2] in charEncodings:
                count += tracker[i+2]

            tracker[i] = count

        return tracker[0]

def main():
    s: Solution = Solution()

    print(s.numDecodings('12'))  # Expected: 2
    print(s.numDecodings('226'))  # Expected: 3
    print(s.numDecodings('06'))  # Expected: 0

if __name__ == '__main__':
    main()


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

import tushare as ts
import pandas as pd
import numpy as np

from saiutil import *
from saidb   import *
from saisql  import *
from saicalc import *
from sailog  import *
from saimail import *
from sairef  import *

#######################################################################
# ST摘帽
# stXX --> XXYY
#######################################################################

"""
2016-12-25
insert into tbl_name (
stock_id, stock_old_name, stock_new_name,
inst_date, inst_time)
values ('000504', '*ST生物', '南华生物', '2017-05-29', '09:41:00');
"""
def name_record_to_db(_stock_id, _old_name, _new_name, _work_date, _db):
    tm = get_time()
    sql = "insert into tbl_name (  \
stock_id, stock_old_name, stock_new_name, \
inst_date, inst_time)  \
values ('%s', '%s', '%s', '%s', '%s')" % (_stock_id, _old_name, _new_name, _work_date, tm)

    log_debug("sql: \n%s", sql)

    rv = sql_to_db_nolog(sql, _db)
    if rv != 0:
        log_error("error: record fupai: %s", sql)

    return rv

def work_one(_stock_id, _name, _work_date, _db):

    log_info("work_one [%s, %s] begin", _stock_id, _name)

    begin = get_micro_second()

    # new name
    name = get_name(_stock_id)

    if name.find("ST") != -1:
        log_info("new name still keeps bad1")
        return None

    if name.find("退") != -1:
        log_info("new name still keeps bad2")
        return None

    if name != _name:
        log_info("nice: %s [%s => %s]", _stock_id, _name, name)
        name_record_to_db(_stock_id, _name, name, _work_date, _db)
    else:
        log_info("old name")
        name = None

    log_debug("it costs %d us", get_micro_second() - begin)

    return name

def name_check_to_notice(_work_date, _db):

    sql = "select * from tbl_name where inst_date = '%s'" %(_work_date)
    log_info("sql: %s", sql)

    df = pd.read_sql_query(sql, _db);
    if df is None:
        log_info("'%s' not found in db", _stock_id)
        return -1
    else:
        log_info("got data: %d lines", len(df))

    content = ""
    for row_index, row in df.iterrows():
        stock_id = row['stock_id']
        old_name = row['stock_old_name']
        new_name = row['stock_new_name']
        one  = "%s 摘帽 [%s] => [%s] (%s)\n" % (stock_id, old_name, new_name, _work_date)
        info = get_basic_info_all(stock_id, _db)
        one += info
        one += "++++++++++++++++++++++++++++++++++++++++\n"
        log_debug("\n%s", one)
        content += one

    if len(content) > 0:
        subject = "rename: %s" % (_work_date)
        log_debug("mail: %s", subject)
        log_debug("\n%s", content)
        if sai_is_product_mode():
            saimail(subject, content)
        else:
            pass
            # saimail(subject, content)

def xxx(_db):

    work_date = get_today()

    basic = get_stock_list_df_tu()
    if basic is None:
        log_debug("error: get_stock_list_df_tu failure")
        return -1

    content = ""
    for row_index, row in basic.iterrows():
        stock_id = row_index
        name = row['name']
        if name.find("ST") != -1:
            log_info("special treatment: [%s, %s]", stock_id, name)
            new = work_one(stock_id, name, work_date, _db)
        else:
            pass

    name_check_to_notice(work_date, _db)

    return 0

def work():
    db = db_init()

    xxx(db)

    db_end(db)

#######################################################################

def main():
    sailog_set("name.log")

    log_info("let's begin here!")

    if sai_is_product_mode():
        # check holiday
        if today_is_weekend():
            log_info("today is weekend, exit")
        else:
            log_info("today is workday, come on")
            work()
    else:
        log_debug("test mode")
        work()

    log_info("main ends, bye!")
    return

main()
exit()

#######################################################################

# name.py


# If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.

# Not all numbers produce palindromes so quickly. For example,

# 349 + 943 = 1292
# 1292 + 2921 = 4213
# 4213 + 3124 = 7337

# That is, 349 took three iterations to arrive at a palindrome.

# Although no one has proved it yet, it is thought that some numbers, like 196 never produce a palindrome.
# A number that never forms a palindrome through the reverse and add process is called a Lychrel number.
# Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise.
# In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one,
# with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty
# iterations before producing a palindrome:
# 4668731596684224866951378664 (53 iterations, 28-digits).

# Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994

# How many Lychrel numbers are there below ten-thousand?

# NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.

from common.helpers import is_palindrome

lychrel_numbers = 0
for i in range(1,10000):
    # Add i to its reverse
    res = i + int(str(i)[::-1])
    # Check if it's a palindrome
    # If it is, break
    # If it isn't, add it to its reverse and repeat
    if is_palindrome(res):
        # print(f"{res} is a palindrome")
        continue
    else:
        # print(f"{res} is not a palindrome")
        b = res
        is_p = False
        for x in range(1,50):
            b += int(str(b)[::-1])
            if is_palindrome(b):
                # print(f"{b} is a palindrome")
                is_p = True
                break
            # else:
            #     print(f"{i} is not a palindrome")
            x += 1
        if is_p:
            continue

    # If it's not a palindrome after 50 iterations, it's a Lychrel number
    # So add 1 to the Lychrel number counter
    print(f"{i} is a Lychrel number")
    lychrel_numbers += 1

print(f"There are {lychrel_numbers} Lychrel numbers below 10,000")


# exceptions are the key method of handling error in Python
# "try" something, then catch an "except"
# you can always raise your own exceptions with "raise"

def main():
    try:
        fh = open('xlines.txt')
    #   if typo isn't there, it'll run as normal
        for line in fh:
            print(line.strip())
    #     the strip method strips the string of any trailing new lines
    except IOError as e:
        # this as e encapsulated the error and directory in a variable
        print("Couldn't open the file,", e)
    #     except, by itself, will catch any error
    # if you further specify except with an IOError, it'll give you an error message when you have that particular error

    # else:

if __name__ == "__main__": main()


# It is a six-digit number.
# The value is within the range given in your puzzle input.
# Two adjacent digits are the same (like 22 in 122345).
# Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).

# Make sure to add 1 to your end for range function
start = 146810
end = 612565
total = 0
def split(number):
  return list(number)

def isGreater(num):
  if num[0] <= num[1] and num[1] <= num[2] and num[2] <= num[3] and num[3] <= num[4] and num[4] <= num[5]:
    return True

# Iterate through our given range
for num in range(start, end):

  num = str(num)
  num = split(num)

  if num[0] == num[1] and num[1] != num[2]:
    if isGreater(num):
      total += 1

  elif num[1] == num[2] and num[2] != num[3] and num[1] != num[0]:
    if isGreater(num):
      total += 1

  elif num[2] == num[3] and num[3] != num[4] and num[2] != num[1]:
      if isGreater(num):
        total += 1

  elif num[3] == num[4] and num[4] != num[5] and num[3] != num[2]:
      if isGreater(num):
        total += 1

  elif num[4] == num[5] and num[4] != num[3]:
      if isGreater(num):
        total += 1

print(total)


"""
Very simple script that simply iterates over all files data/pdf/f.pdf
and create a file data/txt/f.pdf.txt that contains the raw text, extracted
using the "pdftotext" command. If a pdf cannot be converted, this
script will not produce the output file.
"""

import os
import sys
import time
import shutil

from utils import Config

# make sure pdftotext is installed
if not shutil.which('pdftotext'): # needs Python 3.3+
    print('ERROR: you don\'t have pdftotext installed. Install it first before calling this script')
    sys.exit()

files = []
if os.path.exists(Config.pdf_dir):
    conf_dirs = [f for f in os.listdir(Config.pdf_dir)
                 if os.path.isdir(os.path.join(Config.pdf_dir, f))]
    for cdir in conf_dirs:
        conf_sub_dirs = [
            f for f in os.listdir(os.path.join(Config.pdf_dir, cdir))
            if os.path.isdir(os.path.join(Config.pdf_dir, cdir, f))]
        for sdir in conf_sub_dirs:
            file_ids = os.listdir(os.path.join(Config.pdf_dir, cdir, sdir))
            file_ids = [os.path.join(cdir, sdir, f) for f in file_ids]
            files.extend(file_ids)
print('Have {:d} PDFs in our data folder'.format(len(files)))

have = []
if os.path.exists(Config.txt_dir):
    conf_dirs = [f for f in os.listdir(Config.txt_dir)
                 if os.path.isdir(os.path.join(Config.txt_dir, f))]
    for cdir in conf_dirs:
        conf_sub_dirs = [
            f for f in os.listdir(os.path.join(Config.txt_dir, cdir))
            if os.path.isdir(os.path.join(Config.txt_dir, cdir, f))]
        for sdir in conf_sub_dirs:
            file_ids = os.listdir(os.path.join(Config.txt_dir, cdir, sdir))
            file_ids = [os.path.join(cdir, sdir, f) for f in file_ids]
            have.extend(file_ids)
print('Have {:d} TXTs in our data folder'.format(len(have)))

for i, f in enumerate(files): # there was a ,start=1 here that I removed, can't remember why it would be there. shouldn't be, i think.
    os.makedirs(os.path.join(Config.txt_dir, os.path.split(f)[0]), exist_ok=True)
    txt_basename = f + '.txt'
    if txt_basename in have:
        print('%d/%d skipping %s, already exists.' % (i+1, len(files), txt_basename, ))
        continue

    pdf_path = os.path.join(Config.pdf_dir, f)
    txt_path = os.path.join(Config.txt_dir, txt_basename)
    cmd = "pdftotext %s %s" % (pdf_path, txt_path)
    os.system(cmd)

    print('%d/%d %s' % (i+1, len(files), cmd))

    # check output was made
    if not os.path.isfile(txt_path):
        # there was an error with converting the pdf
        print('there was a problem with parsing %s to text, creating an empty text file.' % (pdf_path, ))
        os.system('touch ' + txt_path) # create empty file, but it's a record of having tried to convert

    time.sleep(0.01) # silly way for allowing for ctrl+c termination


# 폰켓몬
# https://programmers.co.kr/learn/courses/30/lessons/1845
# 해결 / 15분 소요 / 21.12.19
'''
최대한 많은 종류의 폰켓몬 갯수 구하기

1. 조합을 구하기
2. 조합 요소들의 중복을 제거 후
3. 종류를 제일 많이 가지고 있는 요소 찾은 후 최댓값 반환

- 시간 초과
def solution(nums):
    lst = list(combinations(nums, len(nums) // 2))
    max = -999
    for l in lst:
        if max < len(set(l)):
            max = len(set(l))
    return max
'''
# 2번째 접근
def solution(nums):
    r = len(nums) // 2
    lst = list(set(nums))

    # 선택의 갯수 보다 작거나 같은 경우
    if len(lst) <= r:
        return len(lst)
    return r