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

# In[54]:

get_ipython().system('pip install nltk')
get_ipython().system('pip install gensim')

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import nltk
nltk.download('stopwords')
nltk.download('punkt')
from nltk.corpus import stopwords
from nltk.util import ngrams
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from collections import defaultdict
from collections import Counter
plt.style.use('ggplot')
stop_words = set(stopwords.words('english'))
import re
from nltk.tokenize import word_tokenize
import gensim
import string
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
from tqdm import tqdm
from keras.models import Sequential
from keras.preprocessing.text import Tokenizer
from keras.layers import Embedding, LSTM, Dense, SpatialDropout1D
from keras.initializers import Constant
from keras.optimizers import Adam
from keras.layers import Bidirectional
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense, SpatialDropout1D
from tensorflow.keras.optimizers import Adam

# In[29]:

get_ipython().system('wget https://github.com/stanfordnlp/GloVe/blob/master/glove.twitter.27B.25d.txt')
embedding_dict = {}
with open("glove.twitter.27B.25d.txt", 'r', encoding='utf-8') as file:
    for line in file:
        values = line.split()
        word = values[0]
        vector = np.asarray(values[1:], dtype='float32')
        embedding_dict[word] = vector

# In[32]:

train_data = pd.read_csv('train.csv')
test_data = pd.read_csv('test.csv')

train_data.head(3)
print('There are {} rows and {} columns in the training data'.format(train_data.shape[0], train_data.shape[1]))
print('There are {} rows and {} columns in the test data'.format(test_data.shape[0], test_data.shape[1]))

# In[33]:

x = train_data.target.value_counts()
sns.barplot(x=x.index, y=x)
plt.gca().set_ylabel('samples')

# In[34]:

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
tweet_len = train_data[train_data['target'] == 1]['text'].str.split().map(lambda x: len(x))
ax1.hist(tweet_len, color='red')
ax1.set_title('Disaster tweets')
tweet_len = train_data[train_data['target'] == 0]['text'].str.split().map(lambda x: len(x))
ax2.hist(tweet_len, color='blue')
ax2.set_title('Non-disaster tweets')
fig.suptitle('Word count in tweets')
plt.show()

# In[35]:

plt.style.use('ggplot')
stop = set(stopwords.words('english'))

# In[39]:

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
tweet_len = train_data[train_data['target'] == 1]['text'].str.split().map(lambda x: len(x))
sns.histplot(tweet_len, ax=ax1, color='red')
ax1.set_title('disaster tweets')
tweet_len = train_data[train_data['target'] == 0]['text'].str.split().map(lambda x: len(x))
sns.histplot(tweet_len, ax=ax2, color='blue')
ax2.set_title('Not disaster tweets')
fig.suptitle('Words in a tweet')
plt.show()

# In[41]:

def create_corpus(target_data, target):
    corpus = []
    for x in target_data[target_data['target'] == target]['text'].str.split():
        for i in x:
            corpus.append(i)
    return corpus

corpus = create_corpus(train_data, target=1)

counter = Counter(corpus)
most = counter.most_common()
x, y = [], []
for word, count in most[:40]:
    if word not in stop:
        x.append(word)
        y.append(count)
sns.barplot(x=y, y=x)
plt.title('Most common words in disaster tweets')
plt.show()

# In[42]:

def get_top_tweet_bigrams(corpus, n=None):
    vec = CountVectorizer(ngram_range=(2, 2)).fit(corpus)
    bag_of_words = vec.transform(corpus)
    sum_words = bag_of_words.sum(axis=0)
    words_freq = [(word, sum_words[0, idx]) for word, idx in vec.vocabulary_.items()]
    words_freq = sorted(words_freq, key=lambda x: x[1], reverse=True)
    return words_freq[:n]

# In[43]:

df = pd.concat([train_data, test_data])
df.shape

# In[44]:

def remove_url(text):
    url_pattern = re.compile(r'https?://\S+|www\.\S+')
    return url_pattern.sub(r'', text)

# In[45]:

example = "Click on this link: https://www.example.com"
print(remove_url(example))
df['text'] = df['text'].apply(lambda x: remove_url(x))

# In[46]:

def remove_html_tags(text):
    html_pattern = re.compile(r'<.*?>')
    return html_pattern.sub(r'', text)

# In[47]:

example = """

Real or Fake

Kaggle

getting started

"""

print(remove_html_tags(example))
df['text'] = df['text'].apply(lambda x: remove_html_tags(x))

# In[48]:

def remove_emojis(text):
    emoji_pattern = re.compile("["
                               u"\U0001F600-\U0001F64F"  # emoticons
                               u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                               u"\U0001F680-\U0001F6FF"  # transport & map symbols
                               u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                               u"\U00002702-\U000027B0"
                               u"\U000024C2-\U0001F251"
                               "]+", flags=re.UNICODE)
    return emoji_pattern.sub(r'', text)

# In[49]:

example = remove_emojis("second Earthquake ðŸ˜”ðŸ˜”")
print(remove_emojis(example))
df['text'] = df['text'].apply(lambda x: remove_emojis(x))

# In[50]:

def remove_punctuation(text):
    table = str.maketrans('', '', string.punctuation)
    return text.translate(table)

# In[51]:

example = "I @am the #best"
print(remove_punctuation(example))
df['text'] = df['text'].apply(lambda x: remove_punctuation(x))

# In[52]:

def remove_stopwords(text):
    words = [word.lower() for word in word_tokenize(text) if word.lower() not in stop_words]
    return " ".join(words)

# In[55]:

example = "I am a very good boy, but I don't like to study"
print(remove_stopwords(example))
df['text'] = df['text'].apply(lambda x: remove_stopwords(x))

# In[56]:

corpus = df['text'].apply(lambda x: word_tokenize(x))
num_words = len(corpus)
embedding_dict = gensim.models.Word2Vec(corpus, vector_size=100, window=5, min_count=2)
max_seq_len = 100
embed_num_dims = 25

# In[57]:

X = train_data['text']
y = train_data['target']

tokenizer = Tokenizer(num_words=10000)
tokenizer.fit_on_texts(X)
X = tokenizer.texts_to_sequences(X)
X = pad_sequences(X, maxlen=max_seq_len)

# In[58]:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=42)
lstm_out = 128
batch_size = 32
num_epochs = 1

# In[59]:

model = Sequential()
model.add(Embedding(10000, embed_num_dims, input_length=max_seq_len))
model.add(Bidirectional(LSTM(lstm_out, dropout=0.2, recurrent_dropout=0.2)))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_train, y_train, batch_size=batch_size, epochs=num_epochs, validation_data=(X_test, y_test))

# In[60]:

embed_num_dims = 100
max_seq_len = X.shape[1]
word_index = tokenizer.word_index
num_words = min(10000, len(word_index) + 1)

# In[61]:

def create_embedding_matrix(word_index, embedding_dict, embed_num_dims):
    embedding_matrix = np.zeros((len(word_index) + 1, embed_num_dims))
    for word, index in word_index.items():
        if word in embedding_dict.wv.key_to_index:
            embedding_matrix[index] = embedding_dict.wv[word]

        return embedding_matrix

# In[62]:

embedding_matrix = create_embedding_matrix(word_index, embedding_dict, embed_num_dims)

# In[63]:

model = Sequential()
model.add(Embedding(len(word_index) + 1,
                    embed_num_dims,
                    embeddings_initializer=Constant(embedding_matrix),
                    input_length=max_seq_len,
                    trainable=False))
model.add(SpatialDropout1D(0.2))
model.add(LSTM(64, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))

optimizer = Adam(learning_rate=1e-5)

model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])

# In[64]:

y = train_data['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15)

history = model.fit(X_train, y_train, batch_size=4, epochs=5, validation_data=(X_test, y_test))

# In[76]:

epochs = [i for i in range(5)]

plt.figure(figsize=(12, 6))
plt.plot(epochs, history.history['accuracy'], 'go-', label='Training Accuracy')
plt.plot(epochs, history.history['val_accuracy'], 'ro-', label='Validation Accuracy')
plt.title('Training & Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()

# In[66]:

loss, accuracy = model.evaluate(X_test, y_test)

# In[68]:

sample_sub=pd.read_csv('train.csv')
y_pre=model.predict(X_test)
y_pre=np.round(y_pre).astype(int).reshape(1, -1)[0]
sub = pd.concat([sample_sub['id'], pd.Series(y_pre, name='target')], axis=1)
sub.to_csv('submission.csv', index=False)
sub.head()

# In[70]:

dataset = pd.read_csv('train.csv')
embedding_dim=25
embedding_dict = {}
with open('glove.twitter.27B.25d.txt', 'r', encoding='utf-8') as f:
    for line in f:
        values = line.split()
        word = values[0]
        coefs = np.asarray(values[1:], dtype='float32')
        embedding_dict[word] = coefs

# In[71]:

word_index = tokenizer.word_index
num_words = min(10000, len(word_index) + 1)
embedding_matrix = np.zeros((num_words, embedding_dim))
for word, i in word_index.items():
    if i >= num_words:
        continue
    embedding_vector = embedding_dict.get(word)
    if embedding_vector is not None:
        embedding_matrix[i] = embedding_vector

# In[72]:

model = Sequential()
model.add(Embedding(num_words, embedding_dim, input_length=max_seq_len, embeddings_initializer=Constant(embedding_matrix), trainable=False))
model.add(SpatialDropout1D(0.2))
model.add(LSTM(64, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))

optimizer = Adam(learning_rate=1e-5)
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])

epochs = 10
batch_size = 32
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(X_test, y_test))

# In[73]:

new_data = ['your text data goes here', 'more text data']
new_sequences = tokenizer.texts_to_sequences(new_data)
new_padded_sequences = pad_sequences(new_sequences, maxlen=max_seq_len)
predictions = model.predict(new_padded_sequences)

# In[74]:

y_pre=model.predict(X_test)
y_pre=np.round(y_pre).astype(int).reshape(1, -1)[0]
sub = pd.concat([sample_sub['id'], pd.Series(y_pre, name='target')], axis=1)
sub.to_csv('submission.csv', index=False)
sub.head()

# In[75]:

loss, accuracy = model.evaluate(X_test, y_test)

# In[ ]:


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

import unittest
import sys
import datetime
import sqlite3

sys.path.append("../fixtures/")
sys.path.append("../waxtablet/")

import sqlitemgr
import rdbrecord

class ReadEntryHeadTest(unittest.TestCase):
	"""
		Test the head() method of the ReadEntry class. Delete a couple of the objects from the entry_list of the ReadEntry object after
		the first method call, then call the method again.
	"""

	def setUp(self):

		table = "entries"
		db = "../fixtures/test_journal"

		sg = sqlitemgr.SQLiteMgr(db)
		re = rdbrecord.ReadEntry(sg.make_conn(), table, db)

		self.first_head_comp = [(1, '2017-02-11'), (2, '2017-02-27'), (3, '2017-03-09'), (4, '2017-03-12'), (5, '2017-03-04'), \
								(6, '2017-03-15'), (7, '2017-04-04'), (8, '2017-04-12')]

		self.second_head_comp = [(1, '2017-02-11'), (2, '2017-02-27'), (4, '2017-03-12'), (5, '2017-03-04'), (7, '2017-04-04')]

		re.head(8)
		self.first_head = [(i.entry_number, str(i.entry_date)) for i in re.entry_list]
		self.first_head_result = re.result

		del re.entry_list[2]
		del re.entry_list[4]

		re.head()		# just use the default value for the argument to this method
		self.second_head = [(i.entry_number, str(i.entry_date)) for i in re.entry_list]
		self.second_head_result = re.result

	def test_head(self):
		self.assertEqual(self.first_head, self.first_head_comp)
		self.assertEqual(self.second_head, self.second_head_comp)

	def test_result(self):
		self.assertEqual(self.first_head_result, "OK")
		self.assertEqual(self.second_head_result, "OK")

if __name__ == '__main__':
	unittest.main()


'''
create_time: 2023/5/5 10:25
author: yss
version: 1.0
'''

import argparse
parser = argparse.ArgumentParser(description="hello")

#print(type(parser))

parser.add_argument('-a',action="store_true",default=False)
parser.add_argument('-b',action="store",dest='b')

args = parser.parse_args(['-a','-bhaha'])
print(args)

print("{} {}".format(args.a,args.b))


import sqlite3
import requests
from requests.exceptions import ConnectionError
from bs4 import BeautifulSoup
import re
import pandas as pd

# Find every country
def get_country(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')
    country = [country.get_text() for country in soup.find(class_="box-aside__section__in").find_all('a') if country != None]
    for row in country:
        cursor.execute("INSERT INTO COUNTRY_SL (COUNTRY) SELECT ? WHERE NOT EXISTS (SELECT 1 FROM COUNTRY_SL WHERE COUNTRY = ?)",(row,row))
        connection.commit()

# Find links for any country
def links_country(url):
    cursor.execute("SELECT ID, COUNTRY FROM COUNTRY_SL")
    countries = cursor.fetchall()
    for id, country in countries:
        cursor.execute("SELECT MAX(ID_COUNTRY)-1 FROM LINKS")
        max = cursor.fetchone()
        print(max)
        if id > int(max[0]):
            response = requests.get(f"{url}{country.lower().replace('& ','').replace(' ','-')}")
            soup = BeautifulSoup(response.content, 'html.parser')
            for link in soup.find(class_="box-overflow").find_all('a'):
                cursor.execute("INSERT INTO LINKS (ID_COUNTRY, HREF) SELECT ?, ? WHERE NOT EXISTS (SELECT 1 FROM LINKS WHERE HREF = ?)", (country[0], link['href'], link['href']))
                connection.commit()

# Links for main_stage etc. if it exist in each season
def new_links(url):
    cursor.execute("SELECT ID, ID_COUNTRY, HREF FROM LINKS WHERE FLAG = 0")
    links = cursor.fetchall()
    for id, id_country, link in links:
        link = f"https://www.betexplorer.com{link.lower()}results"
        response = requests.get(link)
        print(link)
        soup = BeautifulSoup(response.content, 'html.parser')
        try:
            page = soup.find_all("div", class_ = 'box-overflow__in')[1].find_all('a')
            for href in page:
                new_link = f"{link}/{str(href['href'])}"
                if 'stage' in new_link:
                    cursor.execute("INSERT INTO SUB_LINKS (ID_COUNTRY,ID_LINK, HREF) SELECT ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM SUB_LINKS WHERE HREF = ?)",(id_country, id, new_link, new_link))
                    cursor.execute("UPDATE LINKS SET FLAG = 1 WHERE ID = ?",(id,))
                    connection.commit()
                else:
                    cursor.execute("UPDATE LINKS SET FLAG = 2 WHERE ID = ?", (id,))
                    connection.commit()
        except:
            cursor.execute("UPDATE LINKS SET FLAG = 2 WHERE ID = ?",(id,))
            connection.commit()
            continue

# Go to every link
def seasons_scrap():
        cursor.execute("SELECT ID, ID_COUNTRY, NULL, HREF, 1 AS SOURCE FROM LINKS WHERE FLAG = 2 UNION SELECT ID, ID_COUNTRY, ID_LINK, HREF, 2 AS SOURCE FROM SUB_LINKS WHERE FLAG = 0")
        rows = cursor.fetchall()
        for id, id_country, id_link, href, source in rows:
            if source == 1:
                href = 'https://www.betexplorer.com'+href+'results'
            response = requests.get(href)
            print(href)
            soup = BeautifulSoup(response.content, 'html.parser')
            league = href.split('/')[5]
            matches = soup.find_all('tr')
            if not matches:
                if source == 1:
                    cursor.execute("UPDATE LINKS SET FLAG = 4 WHERE ID = ?", (id,))
                elif source == 2:
                    cursor.execute("UPDATE SUB_LINKS SET FLAG = 4 WHERE ID = ?", (id,))
            connection.commit()
            for match in matches:
                if match.find('td', class_ = 'h-text-left'):
                    teams = match.find('td',class_='h-text-left').get_text()
                    score = match.find('td',class_='h-text-center').get_text()
                    numbers = re.findall('\d{1,2}\.\d{1,2}(?:\.\d{4})?\.?',str(match))
                    home, draw, away = ['NaN']*3
                    try:
                        home, draw, away, date = numbers
                    except:
                        date = numbers[0]
                    if date.endswith("."):
                        date += '2023'
                    if source == 1:
                        cursor.execute("UPDATE LINKS SET FLAG = 3 WHERE ID = ?",(id,))
                    elif source == 2:
                        cursor.execute("UPDATE SUB_LINKS SET FLAG = 3 WHERE ID = ?", (id,))
                    cursor.execute("INSERT INTO MATCHES (ID_COUNTRY,ID_SOURCE,ID_LINK, COMPETITION, TEAMS, RESULT, HOME, DRAW, AWAY, DATE) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ",
                    (id_country, source, id, league, teams, score, home, draw, away, date))
                    print(f'{id};{source}\n')
                    connection.commit()

# Update counts matches
def update_info():
    cursor.execute("UPDATE LINKS SET COUNT =(SELECT COUNT(b.ID_LINK) FROM MATCHES b WHERE LINKS.ID = b.ID_LINK and b.ID_SOURCE = 1 GROUP BY b.ID_LINK)")
    cursor.execute("UPDATE SUB_LINKS SET COUNT =(SELECT COUNT(b.ID_LINK) FROM MATCHES b WHERE SUB_LINKS.ID = b.ID_LINK and b.ID_SOURCE = 2 GROUP BY b.ID_LINK)")
    cursor.execute("UPDATE LINKS SET COUNT = 0 WHERE COUNT IS NULL")
    cursor.execute("UPDATE SUB_LINKS SET COUNT = 0 WHERE COUNT IS NULL")

try:
    connection = sqlite3.connect('/Users/admin/database/handball.db')
    cursor = connection.cursor()
    url = 'https://www.betexplorer.com/handball/'
    get_country(url)
    links_country(url)
    new_links(url)
    seasons_scrap()
    update_info()
    connection.commit()
    query = 'SELECT c.COUNTRY, CASE WHEN ID_SOURCE = 1 THEN l.HREF WHEN ID_SOURCE = 2 THEN sl.HREF END as LINK, ' \
            'm.COMPETITION, m.TEAMS, m.RESULT, m.HOME, m.DRAW, m.AWAY, m.DATE ' \
            'FROM MATCHES m INNER JOIN COUNTRY_SL c ON m.ID_COUNTRY = c.ID ' \
            'LEFT JOIN LINKS l ON m.ID_LINK = l.ID ' \
            'LEFT JOIN SUB_LINKS sl ON m.ID_LINK = sl.ID'
    df=pd.read_sql_query(query,connection)
    df.to_csv('/Users/admin/database/data.csv')
    connection.close()

except ConnectionError:
    print('Error with connection')


from flask import Flask, request, render_template, make_response, url_for, redirect, Markup
import requests
import configparser

# Read the Cloudflare account information from the config file
config = configparser.ConfigParser()
config.read('credentials.cfg')
pi_hostname = config.get('piholeApi', 'pi_hostname')
pi_local_domain = config.get('piholeApi', 'pi_local_domain')
token = config.get('piholeApi', 'token')

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route('/hook/block-enable', methods=['GET'])
def block_enable():

    url = f'http://{pi_hostname}.{pi_local_domain}/admin/api.php?enable&auth={token}'
    r = requests.request("GET", url)
    r.raise_for_status()

    if r.status_code == 200:
        jsn = {"is_active": "true"}
        return(jsn)

    else:
        jsn = {"Error": "Not possible to enable Adblocking"}
        return(jsn)

@app.route('/hook/block-disable', methods=['GET'])
def block_disable():

    url = f'http://{pi_hostname}.{pi_local_domain}/admin/api.php?disable&auth={token}'
    r = requests.request("GET", url)
    r.raise_for_status()

    if r.status_code == 200:
        jsn = {"is_active": "false"}
        return(jsn)

    else:
        jsn = {"Error": "Not possible to disable Adblocking"}
        return(jsn)

if __name__ == "__main__":
    app.run(debug=True)


from datetime import datetime, timedelta, timezone
# from aws_xray_sdk.core import xray_recorder

class UserActivities:
  def run(user_handle):
    model = {
      'errors': None,
      'data': None
    }

    now = datetime.now(timezone.utc).astimezone()

    # # x-ray subsegment
    # subsegment = xray_recorder.begin_subsegment('sub_user_activities')

    # data = {
    # "about": "this is a user_activities subsegment",
    # "time":now.isoformat(),
    # "model_data":model
    # }

    # subsegment.put_metadata('data', data)
    # xray_recorder.end_subsegment()

    if user_handle == None or len(user_handle) < 1:
      model['errors'] = ['blank_user_handle']
    else:
      now = datetime.now()
      results = [{
        'uuid': '248959df-3079-4947-b847-9e0892d1bab4',
        'handle':  'Andrew Brown',
        'message': 'Cloud is fun!',
        'created_at': (now - timedelta(days=1)).isoformat(),
        'expires_at': (now + timedelta(days=31)).isoformat()
      }]
      model['data'] = results
    return model


from os import cpu_count
import numpy as np
import pyopencl as cl
from pyopencl.tools import get_test_platforms_and_devices
import sys
from time import time
from pprint import pprint

KERNELS_FILE: str
context: cl.Context
queue: cl.CommandQueue
program: None
tile_size = 64

def initOpenCL():
    global context, queue, program
    platforms = cl.get_platforms()
    devices = platforms[0].get_devices(cl.device_type.GPU)
    device = devices[0]
    global_mem = device.get_info(cl.device_info.GLOBAL_MEM_SIZE)
    local_mem = device.get_info(cl.device_info.LOCAL_MEM_SIZE)
    max_CU = device.get_info(cl.device_info.MAX_COMPUTE_UNITS)
    max_WGS = device.get_info(cl.device_info.MAX_WORK_GROUP_SIZE)
    info = """Device: {}:
Global mem size: {} Bytes
Local mem size: {} Bytes
Max CU: {}
Max work groub size: {} WIs\n""".format( device.name, global_mem, local_mem, max_CU, max_WGS)
    print(info)
    context = cl.Context(devices)#cl.create_some_context()
    queue = cl.CommandQueue(context,
        properties=cl.command_queue_properties.PROFILING_ENABLE)

    #print("Prop: {}".format(queue.set_property(cl.command_queue_properties.PROFILING_ENABLE, True)))

    with open(KERNELS_FILE, 'r') as kernel_file:
        program = cl.Program(context, kernel_file.read() % {'tile_size': tile_size}).build()
        kernel_file.close()

def host_mul(A, B):
    sum = np.int32(0)

    start = time()
    C = A @ B
    cpu_time = time() - start

    return C, cpu_time

def global_normilize(G, L):
    while not G % L == 0:
        G += 1
    return G

def normilize_sizes(M, N):
    local_size_M = 1
    i = 0
    barrier = 32
    while(local_size_M <= M and local_size_M <= barrier and 2 ** (i + 1) <= barrier and M > 2 ** (i + 1)):
        # print(1, local_size_M <= M, local_size_M <= barrier, M > 2 ** (i + 1))
        i += 1
        local_size_M = 2 ** i

    # print(local_size_M)

    M = global_normilize(M, local_size_M)
    # print(M)

    local_size_N = 1
    barrier = 1024 // local_size_M
    # print(barrier)
    i = 0

    while(local_size_N <= N and local_size_N <= barrier and 2 ** (i + 1) <= barrier and N > 2 ** (i + 1)):
        # print(2, local_size_N <= M, local_size_N <= barrier, N > 2 ** (i + 1))
        i += 1
        local_size_N = 2 ** i

    # print(local_size_N)
    N = global_normilize(N, local_size_N)
    # print(N)

    return M, N, local_size_M, local_size_N

def lab1(M, K, N, check_results, print_results):
    global context, queue, program, tile_size

    print(f"LAB 1\n[{M}x{K}] X [{K}x{N}]\n")

    # kernels = program.all_kernels()
    low = 100
    high = 1000

    A = np.random.randint(low, high, M * K).astype(np.int32).reshape(M, K)
    B = np.random.randint(low, high, K * N).astype(np.int32).reshape(K, N)

    if print_results:
        print(f"Matr A: \n {A}\n")
        print(f"Matr B: \n {B}\n")
    # print(matr.nbytes)
    host_result_matr, device_result_matr = None, np.empty((M * N)).astype(np.int32)
    cpu_time = 0

    if check_results:
        host_result_matr, cpu_time = host_mul(A, B)
        if print_results:
            print(f"Host result matr: \n {host_result_matr}\n")

    buffer_in_A = cl.Buffer(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=A)
    buffer_in_B = cl.Buffer(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=B)
    buffer_out_C = cl.Buffer(context, cl.mem_flags.WRITE_ONLY, device_result_matr.nbytes)

    # kernel_name = 'global_matr_mul'
    kernel = program.global_matr_mul

    kernel.set_args(buffer_in_A, buffer_in_B, buffer_out_C, M, K, N)

    # check_amount = 50
    gpu_time = 0

    m, n, l_m, l_n = normilize_sizes(M, N)
    global_size = (n, m)
    local_size = (l_n, l_m)

    print("Global size: {}\nLocal size: {}\n".format(global_size, local_size))
    # for j in range(check_amount):

    event = cl.enqueue_nd_range_kernel(queue, kernel, global_size, local_size)
    event.wait()
    gpu_time += (event.profile.end  - event.profile.start)
    # gpu_time /= check_amount

    cl.enqueue_copy(queue, device_result_matr, buffer_out_C)
    device_result_matr = device_result_matr.reshape(M, N)

    if print_results:
        print(f"Device result matr: \n {device_result_matr}\n")
    if check_results:
        comparison = host_result_matr == device_result_matr
        is_equal = comparison.all()
        print(f'Compare with host matr: {is_equal}')
        print('CPU_TIME: {} ms'.format(cpu_time * 1e3))
        # data.update({"CPU_TIME (s)": cpu_time, 'Compare with host': flag})

    # data.update({"GPU_TIME (ms)": gpu_time/1e6})
    print('GPU_TIME: {} ms\n'.format(gpu_time * 1e-6))

def main():
    global KERNELS_FILE

    argv = sys.argv

    check_results = False
    print_results = False

    if len(argv) == 1:
        print("Enter more arguments")
        return
    elif len(argv) == 5:
        pass
    elif len(argv) == 6:
        check_results = True if argv[5] == 'check' else False
    elif len(argv) == 7:
        check_results = True if argv[5] == 'check' else False
        print_results = True if argv[6] == 'print' else False
    else:
        print("Enter less arguments")
        return
    KERNELS_FILE = argv[1]
    M = np.int32(argv[2])
    K = np.int32(argv[3])
    N = np.int32(argv[4])

    # if (not (M % 32 == 0 and N % 32 == 0 ))

    initOpenCL()
    start = time()
    lab1(M, K, N, check_results, print_results)
    print("Program work time:{}".format(time() - start))

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Mon Feb 10 12:36:30 2020

@author: JULI

Grupo: 6
Nombre: Juliana Ruiz Sánchez
Documento:1001370876
enunciado: 3.2.	Escriba un programa que le pida al usuario una palabra o frase y una letra. El programa deberá imprimir la misma frase
o palabra ingresada, pero ocultando la letra que ingresó el usuario con un asterisco.
Análisis:
-Entrada:
        pa=palabra o frase
        le=letra
-Salida: nf=nueva frase
"""
pa=input('Ingrese una palabra o frase ')
le=input('Ingrese letra a ocultar ')
nf=''
i=0
ta=len(pa)
while i<ta:
    if pa[i]==le:
        nf=nf+'*'
    else:
        nf=nf+pa[i]

    i=i+1
print(nf)


import typing
from typing import Dict, Optional

from monitoring.monitorlib.clients.scd_automated_testing import QueryError
from monitoring.monitorlib.scd_automated_testing.scd_injection_api import InjectFlightResponse
from monitoring.uss_qualifier.common_data_definitions import Severity
from monitoring.uss_qualifier.scd.configuration import SCDQualifierTestConfiguration
from monitoring.uss_qualifier.scd.data_interfaces import AutomatedTest, TestStep, FlightInjectionAttempt
from monitoring.uss_qualifier.scd.executor.errors import TestRunnerError
from monitoring.uss_qualifier.scd.executor.report_recorder import ReportRecorder
from monitoring.uss_qualifier.scd.executor.target import TestTarget
from monitoring.uss_qualifier.scd.executor.test_steps import inject_flight, delete_flight
from monitoring.uss_qualifier.scd.reports import Report, Issue, AutomatedTestContext, TestStepReference, TestPhase

# TODO: Replace print by logging
class TestRunner:
    """A class to run automated test steps for a specific combination of targets per uss role"""

    def __init__(self, context: AutomatedTestContext, automated_test: AutomatedTest, targets: Dict[str, TestTarget], dss_target: Optional[TestTarget], report: Report):
        self.context = context
        self.automated_test = automated_test
        self.targets = targets
        self.dss_target = dss_target
        self.report_recorder = ReportRecorder(report, self.context)

    def get_scd_configuration(self) -> SCDQualifierTestConfiguration:
        return SCDQualifierTestConfiguration(injection_targets=list(map(lambda t: t.config, self.targets.values())))

    def run_automated_test(self):
        for i, step in enumerate(self.automated_test.steps):
            print('[SCD]   Running step {}: {}'.format(i, step.name))
            self.execute_step(step, i)

    def teardown(self):
        """Delete resources created by this test runner."""
        print("[SCD]   Teardown {}".format(self.automated_test.name))

        cleanup_test_step = 0
        for role, target in self.targets.items():
            flight_names = target.managed_flights()
            for flight_name in flight_names:
                print("[SCD]    - Deleting {} flights for target {}.".format(len(flight_names), target.name))

                step_ref = TestStepReference(
                    name="Clean up flight {} in {}".format(flight_name, target.name),
                    index=cleanup_test_step,
                    phase=TestPhase.Cleanup
                )

                try:
                    resp, query = target.delete_flight(flight_name)
                    self.report_recorder.capture_interaction(step_ref, query, 'Remove flight during test cleanup')
                except QueryError as e:
                    interaction_id = self.report_recorder.capture_interaction(step_ref, e.query, 'Remove flight during test cleanup')
                    self.report_recorder.capture_deletion_unknown_issue(
                                    interaction_id=interaction_id,
                                    summary="Deletion request for flight {} was unsuccessful".format(flight_name),
                                    details="Deletion attempt failed with status {}.".format(e.query.status_code),
                                    flight_name=flight_name,
                                    target_name=target.name,
                                    uss_role=role
                            )
                    print("[SCD] Error: Unable to delete flight {} during teardown".format(flight_name))
                cleanup_test_step = cleanup_test_step + 1

    def execute_step(self, step: TestStep, step_index: int):
        target = self.get_target(step)
        if target is None:
            self.print_targets_state()
            raise RuntimeError("[SCD] Error: Unable to identify the target managing flight {}".format(
                step.inject_flight.name if 'inject_flight' in step else step.delete_flight.flight_name
            ))

        step_ref = TestStepReference(
            name=step.name,
            index=step_index,
            phase=TestPhase.Test
        )

        if 'inject_flight' in step:
            inject_flight.execute(self, step, step_ref, target)
        elif 'delete_flight' in step:
            delete_flight.execute(self, step, step_ref, target)
        else:
            raise RuntimeError("[SCD] Error: Unable to identify the action to execute for step {}".format(step.name))

        print("[SCD]     Step {} COMPLETED".format(step.name))

    def get_managing_target(self, flight_name: str) -> typing.Optional[TestTarget]:
        """Returns the managing target which created a flight"""
        for role, target in self.targets.items():
            if target.is_managing_flight(flight_name):
                return target
        return None

    def get_target(self, step: TestStep) -> typing.Optional[TestTarget]:
        """Returns the target which should be called in the TestStep"""
        if 'inject_flight' in step:
            return self.targets[step.inject_flight.injection_target.uss_role]
        elif 'delete_flight' in step:
            return self.get_managing_target(step.delete_flight.flight_name)
        else:
            raise NotImplementedError("Unsupported step. A Test Step shall contain either a inject_flight or a delete_flight object.")

    def get_target_role(self, target_name):
        results = list(filter(lambda x: x[1].name == target_name, self.targets.items()))
        return results[0] if len(results) > 0 else None

    def evaluate_inject_flight_response(self, interaction_id: str, target: TestTarget, attempt: FlightInjectionAttempt, resp: InjectFlightResponse) -> typing.Optional[Issue]:
        if resp.result not in attempt.known_responses.acceptable_results:
            print("[SCD]     Result: ERROR. Received {}, expected one of {}. Reason: {}".format(
                resp.result,
                attempt.known_responses.acceptable_results,
                resp.get('notes', None))
            )
            known_issue = attempt.known_responses.incorrect_result_details.get(resp.result, None)
            if known_issue:
                issue = self.report_recorder.capture_injection_issue(interaction_id=interaction_id, target_name=target.name, attempt=attempt, known_issue=known_issue)
                if known_issue.severity != Severity.Low:
                    raise TestRunnerError("Failed attempt to inject flight {}: {}".format(attempt.name, known_issue.summary), issue)
            else:
                issue = self.report_recorder.capture_injection_unknown_issue(
                    interaction_id=interaction_id,
                    summary="Injection request was unsuccessful",
                    details="Injection attempt failed with unknown response {}".format(resp.result),
                    target_name=target.name,
                    attempt=attempt
                )
                raise TestRunnerError("Unsuccessful attempt to inject flight {}".format(attempt.name), issue)
        return None

    def print_targets_state(self):
        print("[SCD] Targets States:")
        for name, target in self.targets.items():
            print(f"[SCD]   - {name}: {target.created_flight_ids}")


#!/usr/bin/env python
# -- coding: utf-8 --
__time__ = '2017/4/1 00:09'
__author__ = 'zhour udong'

from .models import EmailVerifyRecord, Banner, UserProfile

from xadmin.plugins.auth import UserAdmin
import xadmin
from xadmin import views

class UserProfileAdmin(UserAdmin):
    model_icon = 'fa fa-user'
    def get_form_layout(self):
        if self.org_obj:
            self.form_layout = (
                Main(
                    Fieldset('',
                             'username', 'password',
                             css_class='unsort no_title'
                             ),
                    Fieldset(_('Personal info'),
                             Row('first_name', 'last_name'),
                             'email'
                             ),
                    Fieldset(_('Permissions'),
                             'groups', 'user_permissions'
                             ),
                    Fieldset(_('Important dates'),
                             'last_login', 'date_joined'
                             ),
                ),
                Side(
                    Fieldset(_('Status'),
                             'is_active', 'is_staff', 'is_superuser',
                             ),
                )
            )
        return super(UserAdmin, self).get_form_layout()

class BaseSetting(object):
    enable_themes = True
    use_bootswatch = True

class GlobalSettings(object):
    site_title = "幕学在线管理系统"
    site_footer = '幕学在线公司'
    menu_style = "accordion"
    # 导航栏收起

class EmailVerifyRecordAdmin(object):
    list_display = ('code', 'email', 'send_type', 'send_time',)
    search_fields = ['code', 'email', 'send_type',]
    list_filter = ('code', 'email', 'send_type', 'send_time',)
    # 自定义图标文件
    # 更新版本位置 extra_app/xadmin/static/xadmin/vendor/font-awesome
    # http://fontawesome.io/  替换 css文件夹 和 fonts文件家 其他两个不需要荻花吧
    # 替换该目录下的版本即可更新
    model_icon = 'fa fa-address-book-o'

class BannerAdmin(object):
    list_display = ('title', 'image', 'url', 'index','add_time')
    search_fields = ['title', 'image', 'url', 'index']
    list_filter = ('title', 'image', 'url', 'index','add_time')

# 卸载django admin 自动注册的userprofile
# from django.contrib.auth.models import User
# xadmin.site.unregister(User)

xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin)
xadmin.site.register(Banner, BannerAdmin)
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)
# xadmin.site.register(UserProfile, UserProfileAdmin)


class Node:
    def __init__(self, data=None):
        self.value = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
        self.tail = None
        self.count = 0

    def printLL(self):
        current = self.head
        print(current.value, end="")
        current = current.next
        while current is not None:
            print("->", end="")
            print(current.value, end="")
            current = current.next

    def append(self, data=None):
        newNode = Node(data)
        if self.count == 0:
            self.head = newNode
        else:
            self.tail.next = newNode
        self.tail = newNode
        self.count += 1

    def prepend(self, data=None):
        newNode = Node(data)
        temp = self.head
        newNode.next = temp
        self.head = newNode
        self.count += 1
        if self.count == 1:
            self.tail = self.head

    def removeHead(self):
        if self.count > 0:
            self.head = self.head.next
            self.count -= 1
            if self.count == 0:
                self.tail = None

    def removeTail(self):
        if self.count > 0:
            if self.count == 1:
                self.head = self.tail = None
            else:
                current = self.head
                while current.next != self.tail:
                    current = current.next
                current.next = None
                self.tail = current
                self.count -= 1

LL = LinkedList()
LL.append(20)
LL.append(21)
LL.prepend(31)
LL.printLL()
LL.removeHead()
print()
LL.printLL()
LL.removeTail()
print()
LL.printLL()
LL.removeTail()
print()
LL.printLL()


import requests
from bs4 import BeautifulSoup as bsp
import webbrowser
def printAllDeatils(urls , dataa):
    url=urls
    r=requests.get(url)
    htmlv=r.content
    soup=bsp(htmlv,"html.parser")
    value=soup.find("ol", class_="row collapsible classResource")
    allLi=value.find_all("li")
    dicMain={}

    for i in allLi:

        x=i.find("div",class_="collapsible-header bold")

        if x:
            dataa.write("\n")
            dataa.write(x.text.strip().replace("\n","").replace("\r","").strip())
            dataa.write("\n")
            print()
            print("                  ",x.text.strip().replace("\n","").replace("\r","").strip())
            print()

        y=i.find_all("div",class_="collapsible-body")
        dic={}
        count=1
        for j in y:

            listValue=j.find_all("li",class_="collection-item")

            for mainValue in listValue:
                dataa.write(str(count))
                dataa.write("  " + mainValue.text.strip())
                print(count,end=" ")
                print("   ",mainValue.text.strip())

                ans=mainValue.text.strip().replace(' ','-')
                dic[count]=f"{url}/{ans}"
                count+=1

            print()
        if x:
            dicMain[x.text.strip().replace("\n","").replace("\r","").strip()]=dic
    print("\n\n")

    value=input("Enter Value =")
    value1=int(input(f"In {value} where you want to go :-"))
    print("\n\n")
    url=dicMain[value][value1]
    r=requests.get(url)
    htmlv=r.content
    soup=bsp(htmlv,"html.parser")
    value=soup.find("ul", class_="collection")
    value2=value.find_all("li")
    count=1
    dict1={}
    print("Question In",value1," are\n")
    for i in value2:
        dataa.write(str(count))
        dataa.write(i.find("span",class_="name").text.strip())
        print(count,end=" ")
        x=i.find("a").get("href")

        print("  ",i.find("span",class_="name").text.strip())
        dict1[count]=x
        count+=1
    print("\n\n\n")
    choice=int(input("Which Question You Want To Do \n"))
    url3="https://pepcoding.com"+dict1[choice]
    print(url3)
    webbrowser.open_new_tab(url3)

# This is Main Function
url="https://pepcoding.com/resources/"
r=requests.get(url)
htmlv=r.content
soup=bsp(htmlv,"html.parser")
values=soup.find_all("div", class_="card free-resource-card show-on-hover border-radius")
count=1
dict={}
print("\n")
dataa = open("C:\\Users\\pc\\OneDrive\\Desktop\\data science\\web scraping\\pep\\PepCoding-scrapper-\DATA\\pepData.txt" , 'a')

for value in values:

    print(count,end="  ")
    dataa.write(value.find("h2").text.strip() + "\n")
    print(value.find("h2").text.strip())
    x=value.find("a").get("href")
    dict[count]=(f"https://pepcoding.com{x}")
    count+=1

print("\n\n\n")

choice=int(input("Enter Choice = "))
printAllDeatils(str(dict[choice]) , dataa)
dataa.close()

ML-AI/Web Scraper/Pep Coding Scraping/Python Code/pepcodingwebscrapping.py


from flask import Flask, jsonify, request
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))  # append package folder to PYTHONPATH

import auth_service
import json

app = Flask(__name__)

def authenticate(**kwargs):
    """Validate user login

    Args:
    - **kwargs: holds login credentials/payload from API request

    Returns:
    - `list[str]`: return list of scopes
    """

    print("[DEBUG]", json.dumps(kwargs, indent=2))
    kwargs["username"]
    kwargs["password"]
    if "scope_demo" in kwargs:
        return kwargs["scope_demo"].split()
    return []

# initialize auth rules
auth_service.setup(app, authenticate, {
    "api": [
        r"^/api/.*$"
    ],
    "dev": [
        r"^/devpage$"
    ],
    "admin": [
        r"^/admin$"
    ],
})

@app.route("/test")
def hello():
    return jsonify({
        "message": "anyone can access this"
    })

@app.route("/devpage")
def developer():
    return jsonify({
        "message": f"accessible for 'dev' scope, your scopes: {auth_service.get_token_payload()['scope']}"
    })

@app.route("/api/test")
def api_test():
    return jsonify({
        "message": f"accessible for 'api' scope, your scopes: {auth_service.get_token_payload()['scope']}"
    })

@app.route("/admin")
def admin():
    return jsonify({
        "message": f"accessible for 'admin' scope, your scopes: {auth_service.get_token_payload()['scope']}"
    })

if __name__ == "__main__":
    app.run(
        host="0.0.0.0",
        port=8080,
        debug=True
    )


import aiohttp_jinja2
from aiohttp_session import get_session
from aiohttp import web
from aiohttp.typedefs import Handler
from aiohttp.web_middlewares import _Middleware
from manga_sales.containers import DatabaseContainer

async def handle_404(request: web.Request) -> web.Response:
    return aiohttp_jinja2.render_template("404.html", request, {}, status=404)

async def handle_500(request: web.Request) -> web.Response:
    return aiohttp_jinja2.render_template("500.html", request, {}, status=500)

def create_error_middleware(overrides: dict[int, Handler]) -> _Middleware:
    @web.middleware
    async def error_middleware(
        request: web.Request, handler: Handler
    ) -> web.StreamResponse:
        try:
            return await handler(request)
        except web.HTTPException as ex:
            override = overrides.get(ex.status)
            if override:
                return await override(request)

            raise

    return error_middleware

@web.middleware
async def session_middleware(
    request: web.Request, handler: Handler
) -> web.StreamResponse:
    request.session = await get_session(request)
    return await handler(request)

@web.middleware
async def db_middleware(request: web.Request, handler: Handler) -> web.StreamResponse:
    container = DatabaseContainer()
    response = await handler(request)
    await container.shutdown_resources()  # type: ignore # pylint: disable=no-member
    return response

def setup_middlewares(app: web.Application) -> None:
    error_middleware = create_error_middleware({404: handle_404, 500: handle_500})
    app.middlewares.append(error_middleware)
    app.middlewares.append(db_middleware)
    # app.middlewares.append(session_middleware)


import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import ode
import time as time
from multiprocessing import Pool
from scipy.integrate import trapz
import functools
from multiprocessing import cpu_count
from scipy.interpolate import interp1d
from numba import jit, prange
from decimal import Decimal
import math as mt
import scipy.interpolate
import _pickle as cPickle
import matplotlib.animation as Animation
import pylab as pylab
import scipy.fftpack as fft
import os as os
from shutil import copyfile
import matplotlib.cm as cm
from matplotlib import gridspec
import scipy.integrate as integrate

# from numdifftools import Derivative
flatten = np.ndarray.flatten
# from scipy.interpolate import UnivariateSpline
# from scipy import interpolate
from statsmodels.nonparametric.smoothers_lowess import lowess

colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple'
    , 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan']

plt.rcParams['axes.labelsize'] = 25
plt.rcParams['legend.fontsize'] = 20
plt.rcParams['xtick.labelsize'] = 15
plt.rcParams['ytick.labelsize'] = 15

pi = np.pi;
sin = np.sin;
cos = np.cos;
exp = np.exp;
log = np.log;
abs = np.abs;
absolute = np.absolute;
sqrt = np.sqrt
acos = mt.acos
asin = mt.sin
sinh = np.sinh
cosh = np.cosh

def timer(note, start):
    t = time.time()
    print(note, ':', t-start)
    return

def make_dir(Master_path):
    if os.path.exists(Master_path):
        print('Data Directory exists')
    else:
        os.mkdir(Master_path)
        print('Data directory does not exist')

def save(data, name, directory, Master_path):
    path = '%s/%s/%s.npy' % (Master_path, directory, name)

    file = open(path, 'wb')
    np.save(file, data)
    file.close()
    return

def load(name, directory, Master_path):
    path = '%s/%s/%s.npy' % (Master_path, directory, name)
    file = open(path, 'rb')
    stack = np.load(file)
    file.close()
    return stack

def load_or_exec(filename, directory, function, Master_path, execute_arguments):
    command = input('Rewrite %s in %s (T/F):'%(filename, directory))

    sub_path = '%s/%s' % (Master_path, directory)
    file_path = '%s/%s/%s.npy' % (Master_path, directory, filename)

    if os.path.exists(sub_path):
        print('Sub Directory exists')
    else:
        os.mkdir(sub_path)
        print('Sub directory does not exist')
        print('Builing new Subdirectory %s'%directory)

    if os.path.exists(file_path) and command == 'F':

        print('File exists')
        file = load(filename, directory, Master_path)

    else:

        print('File does not exist or computing fresh set')
        file = function(*execute_arguments)
        save(file, filename, directory, Master_path)

    return np.array(file)

def V_PN(phi,p):

    V = (1-1/(1+((phi)**2))**p)
    V_norm = 0.5*(1 - 1 / (1 + ((phi) ** 2) / p) ** p)

    V_mon = phi**p + 0.1*cos(phi)

    return V_norm

def load_or_exec_multi_file(Master_path, directory, function, execute_arguments):

    path_data = '%s/%s' % (Master_path, directory)

    Rewrite_command = input('Overwrite existing data in %s?(T or F):'%directory)

    if os.path.exists(path_data) == True and Rewrite_command == 'F':

        print('Using Existing Data')

    else:
        if os.path.exists(path_data):
            print('Sub Directory exists')
            print('Overwriting data in %s'%(directory))
            function(*execute_arguments)
        else:
            os.mkdir(path_data)
            print('Sub directory does not exist')
            print('Builing new Subdirectory %s' % directory)
            print('Computing new data')
            function(*execute_arguments)

    return

H_inf = 1e-8
T_R = (90 / (100 * pi ** 2)) ** (1 / 2) * (H_inf) ** (1 / 2)
T_0 = 23.5 * 1e-14
Mpl = 2.435 * 1e18  ##Mpl in GeV
GeV2 = 6.8 * 1e20  ##Gev^2 in Gauss
a_R = T_0 / (T_R * Mpl)

def mapper(var, var_arr , target_arr):

    diff_arr = abs(var_arr-var)
    min_idx = np.argmin(diff_arr)

    out = target_arr[min_idx]

    return out

def Lambda_0(Lambda_k_f, B_k_f):
    Lambda_0 = 3.3e5 * a_R * (Lambda_k_f) ** (1 / 3) * (B_k_f) ** (2 / 3);
    return Lambda_0

def B_0(Lambda_0):
    B_0 = 1e-8 * Lambda_0
    return B_0

def rho_plot(p, alpha, k, x_len, a_stack, Master_path, f, trigger_matrix, kill_matrix, Phi_Stack, asymp_stack):
    h = [-1, 1]

    targ_f = [0.01, 0.005]
    f_idxs = [np.argmin(abs(targ - f)) for targ in targ_f]

    p_idxs = [0]

    targ_alpha = [0.5, 0.6, 0.7, 0.8]
    alpha_idxs = [np.argmin(abs(targ - alpha)) for targ in targ_alpha]

    for cnt_f, idx_f in enumerate(f_idxs):
        item_f = f[idx_f]
        for cnt_p, idx_p, in enumerate(p_idxs):
            item_p = p[idx_p]
            sol = Phi_Stack[idx_f][idx_p]
            t = sol[0];
            phi_a = np.array([mapper(x, a_stack[idx_f][idx_p][0], a_stack[idx_f][idx_p][1]) for x in t]) \
                    / mapper(t[0],a_stack[idx_f][idx_p][0],a_stack[idx_f][idx_p][1])
            phi = sol[1]
            dphi = sol[2]

            for cnt_alpha, idx_alpha in enumerate(alpha_idxs):
                item_alpha = alpha[idx_alpha]

                name_1 = 'alpha:%.5f_h:%s_p:%.3f_f_%.5f' % (item_alpha, -1, item_p, item_f)
                name_2 = 'alpha:%.5f_h:%s_p:%.3f_f_%.5f' % (item_alpha, 1, item_p, item_f)
                name_0_1 = 'alpha:%.5f_h:%s_p:%.3f_f_%.5f' % (0., -1, item_p, item_f)
                name_0_2 = 'alpha:%.5f_h:%s_p:%.3f_f_%.5f' % (0., 1, item_p, item_f)

                stack_B_1, stack_B_2 = np.array(load(name_1, 'Rho_B', Master_path)), np.array(
                    load(name_2, 'Rho_B', Master_path))
                stack_B_0_1, stack_B_0_2 = np.array(load(name_0_1, 'Rho_B', Master_path)), np.array(
                    load(name_0_2, 'Rho_B', Master_path))

                x_B = stack_B_1[:, 0]

                x_B_0_1, rho_B_0_1 = stack_B_0_1[:, 0], stack_B_0_1[:, 1]
                x_B_0_2, rho_B_0_2 = stack_B_0_2[:, 0], stack_B_0_2[:, 1]
                rho_B = stack_B_1[:, 1] / rho_B_0_1 + stack_B_2[:, 1] / rho_B_0_2  # *m**2/H_map**2/(a_i**4)

                stack_E_1 = np.array(load(name_1, 'Rho_E', Master_path));
                stack_E_0_1 = np.array(load(name_0_1, 'Rho_E', Master_path))
                stack_E_2 = np.array(load(name_2, 'Rho_E', Master_path));
                stack_E_0_2 = np.array(load(name_0_2, 'Rho_E', Master_path))
                x_E_0_1, rho_E_0_1 = stack_E_0_1[:, 0], stack_E_0_1[:, 1]
                x_E_0_2, rho_E_0_2 = stack_E_0_2[:, 0], stack_E_0_2[:, 1]
                rho_E = stack_E_1[:, 1] / rho_E_0_1 + stack_E_2[:, 1] / rho_E_0_2  # *m**2/H_map**2/(a_i**2)

                m = H_inf / item_f * (3 * (0.5 * absolute(dphi[0]) ** 2 + V_PN(phi[0], item_p)) ** (-1)) ** (1 / 2)

                H_map = sqrt(item_f ** 2 * (0.5 * dphi ** 2 + np.array([V_PN(z, item_p) for z in phi]))) / sqrt(
                    (0.5 * dphi[0] ** 2 + V_PN(phi[0], item_p)));
                H_map = np.array([mapper(x, t, H_map) for x in x_B])
                t_i = trigger_matrix[idx_f][idx_p]
                a_i = mapper(t_i, a_stack[idx_f][idx_p][0], a_stack[idx_f][idx_p][1])
                a_map = np.array([mapper(x, a_stack[idx_f][idx_p][0], a_stack[idx_f][idx_p][1]) for x in x_B]) / a_i;
                a_map = 1. / a_i  # print(np.shape(rho_B),np.shape(rho_E),np.shape(H_map))#a_map = 1/a_i
                cross_idx = np.argwhere(
                    np.diff(np.sign(np.absolute(rho_E + rho_B) - 3 * H_map ** 2 / m ** 2))).flatten()

                t_i = trigger_matrix[idx_f][idx_p]
                t_f = kill_matrix[idx_f][idx_p];
                a_f = mapper(t_f, a_stack[idx_f][idx_p][0], a_stack[idx_f][idx_p][1])

                A_0 = [absolute(asymp_stack[idx_f, idx_h, idx_p, 0, :, 1]) for idx_h in [0, 1]]
                A = [absolute(asymp_stack[idx_f, idx_h, idx_p, idx_alpha, :, 1]) - absolute(A_0[idx_h]) for idx_h in
                     [0, 1]]

                max_idx = np.argmax(A[0])
                max_k, max_A = [k[idx_f][idx_p][max_idx],
                                sqrt(absolute(A[0][max_idx]) ** 2 + absolute(A[1][max_idx]) ** 2)]

                max_A0 = sqrt(absolute(A_0[0][max_idx]) ** 2 + absolute(A_0[1][max_idx]) ** 2)

                rho_B_1 = (absolute(stack_B_1[:, 1]) - absolute(rho_B_0_1)) * m ** 4 * Mpl ** 4;
                rho_E_1 = (absolute(stack_E_1[:, 1]) - absolute(rho_E_0_1)) * m ** 4 * Mpl ** 4  # *m**2/H_map**2
                rho_B_2 = (absolute(stack_B_2[:, 1]) - absolute(rho_B_0_2)) * m ** 4 * Mpl ** 4;
                rho_E_2 = (absolute(stack_E_2[:, 1]) - absolute(rho_E_0_2)) * m ** 4 * Mpl ** 4  # *m**2/H_map**2
                ratio = \
                (((rho_B_1 + rho_B_2) / a_map ** 4 + (rho_E_1 + rho_E_2) / a_map ** 2) * m ** 2 / (3 * H_map ** 2))[-1]
                print(r'$\alpha:$', item_alpha, 'f:', item_f, 'Ratio:%.5f' % ratio)

                Lambda_f = 2 / (m * Mpl);
                Lambda_f = (a_f / (max_k * m * Mpl))

                if ratio < 2:
                    print(r'$B_{gen}:%.4e$' % (sqrt(rho_B_1 + rho_B_2) * m ** 2 * Mpl ** 2 * a_map ** -2)[-1])
                    print(r'$\Lambda_{gen}:%.4e \quad \Lambda_{gen, peak}:%.4e$' % (
                    2 / (m * Mpl), (a_f / (max_k * m * Mpl))))
                    B_k_f = (m ** 2 * (max_k / (a_f / a_i)) ** 2 * (max_A / max_A0)) * Mpl ** 2;
                    print(r'$B_{gen, peak}:%.4e$' % (B_k_f));

                    Lambda_0 = Lambda_0(Lambda_f, B_k_f);
                    B_0 = B_0(Lambda_0)
                    print(r'$\Lambda_{m,0}:%.4e$' % (Lambda_0))

                    print(r'$B_{0, peak}:%.4e$' % (B_0))
                    # a_R = 1e-29 ##Subhramanian entropy conservation
                    cross_idx_1 = np.argwhere(np.diff(np.sign(np.absolute(
                        rho_E_1 / a_map ** 2 + rho_B_1 / a_map ** 4) - 3 * H_map ** 2 * m ** 2 * Mpl ** 4))).flatten()
                    cross_idx_2 = np.argwhere(np.diff(np.sign(np.absolute(
                        rho_E_2 / a_map ** 2 + rho_B_2 / a_map ** 4) - 3 * H_map ** 2 * m ** 2 * Mpl ** 4))).flatten()

                    B_k_f_1 = sqrt((rho_B_1 / a_map ** 4)[cross_idx_1]) * (m * Mpl) ** 2

    return


from multiprocessing import Queue

import frida

from src.content_provider_fuzzing.execution_units.frida.frida_instrumentation_exec_unit import \
    FridaInstrumentationRequest, FridaInstrumentationExecUnit
from tests.sample_provider_tests.frida_tests.frida_test_setup import FridaTest

class TestFridaSystemServerInstrumentation(FridaTest):
    def test_frida_instrumentation(self):
        self.feedback_queue = Queue()

        instrumentation_request = FridaInstrumentationRequest(
            frida_device=frida.get_usb_device(),
            target_process_name='system_server',
            frida_js_script="""
Java.perform(function () {
    send('AAA');
});
""",
            on_message_function=self.__on_message
        )
        self.instrumentation_exec_unit = FridaInstrumentationExecUnit(instrumentation_request)
        self.instrumentation_exec_unit.setup()
        self.instrumentation_exec_unit.run()
        self.instrumentation_exec_unit.cleanup()

        assert self.feedback_queue.get(timeout=10) == {'type': 'send', 'payload': 'AAA'}

    def __on_message(self, message, data):
        self.feedback_queue.put(message)


from django.http import HttpResponse
from django.shortcuts import render
from multiprocessing import context
from .models import Post
from . import forms

def index(request):
    db = Post.objects.all()
    context ={
        'title':'Blog',
        'heading':'Blog',
        'subheading': 'postingan',
        'post' : db,
    }
    # context = {
    #     'Judul' : 'AppBlog',
    #     'h1' : 'Django',
    #     'menu' : [['/', 'Home'], ['/blog/recent', 'Recent'], ['/blog/post', 'Post']]
    # }
    return render(request,'blog/index.html', context)

def recent(request):
    return HttpResponse("
RECENT BLOG
")

def post(request):
    return HttpResponse("
INI ISINYA POST
")

def about(request):
    return HttpResponse("
ABOUT BLOG
")

def form(request):
    classform = forms.namaclass()
    context = {
        'heading':'Home',
        'classform' : classform
    }

    # if request.method == 'POST':
    #     print("ini adalah method post")
    #     context['nama'] == request.POST['nama']
    #     context['alamat'] == request.POST['alamat']
    #     context['namac'] == request.POST['namac']
    #     context['alamat'] == request.POST['alamat']
    # else:
    if request.POST.get('nama') and request.POST.get('alamat') and request.POST.get('tgl_lahir') and request.POST.get('email'):
            post = Post()
            post.nama = request.POST.get('nama')
            post.alamat = request.POST.get('alamat')
            post.tgl_lahir = request.POST.get('tgl_lahir')
            post.email = request.POST.get('email')
            post.save()

            return render(request, 'form/index.html')
    else:
        print("ini adalah method get")
    return render(request, 'form.html', context)

Praktikum Modul 7 dan 8/Dev/tutor/blog/views.py


from flask import *
from flask_login import *
from mysql_db import MySQL
import mysql.connector
from math import ceil
PERMITED_PARAMS = ['title', 'description', 'year', 'publisher', 'author', 'page_count', 'cover_id']
EDIT_PARAMS = ['last_name', 'first_name', 'middle_name', 'role_id']
PER_PAGE = 3

app = Flask(__name__)
application = app

app.config.from_pyfile('config.py')

db = MySQL(app)

from auth import bp as auth_bp
from auth import init_login_manager, permission_check

from visits import bp as visits_bp

app.register_blueprint(auth_bp)
init_login_manager(app)

app.register_blueprint(visits_bp)

@app.before_request
def loger():
    if request.endpoint == 'static':
        return
    path = request.path
    user_id = getattr(current_user, 'id', None)
    query = 'INSERT INTO visit_logs(user_id, path) VALUES (%s, %s);'
    try:
        with db.connection().cursor(named_tuple=True) as cursor:
            cursor.execute(query, (user_id, path))
            db.connection().commit()
    except mysql.connector.errors.DatabaseError:
        db.connection().rollback()

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/books')
def books():
    page = request.args.get('page', 1, type = int)
    query = ('SELECT books.title, books.description, books.year, books.publisher, books.author, books.page_count, covers.filename FROM books JOIN covers ON books.cover_id = covers.id LIMIT %s OFFSET %s')
    with db.connection().cursor(named_tuple=True) as cursor:
        cursor.execute(query,(PER_PAGE, (page-1)*PER_PAGE))
        books_list = cursor.fetchall()

    query = 'SELECT COUNT(*) AS count FROM (SELECT books.title, books.description, books.year, books.publisher, books.author, books.page_count, covers.filename FROM books JOIN covers ON books.cover_id = covers.id) as result'
    with db.connection().cursor(named_tuple=True) as cursor:
        cursor.execute(query)
        count = cursor.fetchone().count

    last_page = ceil(count/PER_PAGE)

    return render_template('books.html', books_list=books_list, last_page = last_page, current_page = page)

@app.route('/books/new')
@login_required
@permission_check('create')
def users_new():
    roles_list = load_roles()
    return render_template('books_new.html', roles_list=roles_list, book={})

def load_roles():
    query = 'SELECT * FROM roles;'
    cursor = db.connection().cursor(named_tuple=True)
    cursor.execute(query)
    roles = cursor.fetchall()
    cursor.close()
    return roles

def extract_params(params_list):
    params_dict = {}
    for param in params_list:
        params_dict[param] = request.form[param] or None
    return params_dict

@app.route('/books/create', methods=['POST'])
@login_required
@permission_check('create')
def create_user():
    params = extract_params(PERMITED_PARAMS)
    query = 'INSERT INTO books (title, description, year, publisher, author, page_count, cover_id) VALUES (%(title)s, %(description)s, %(year)s, %(publisher)s, %(author)s, %(page_count)s, %(cover_id)s'
    try:
        with db.connection().cursor(named_tuple=True) as cursor:
            cursor.execute(query, params)
            db.connection().commit()
            flash('Успешно!', 'success')
    except mysql.connector.errors.DatabaseError:
        db.connection().rollback()
        flash('При сохранении данных возникла ошибка.', 'danger')
        return render_template('books_new.html', book = params, roles_list = load_roles())

    return redirect(url_for('books'))

# create_user = login_required(create_user)

@app.route('/users/<int:book_id>/update', methods=['POST'])
@login_required
@permission_check('edit')
def update_book(book_id):
    params = extract_params(EDIT_PARAMS)
    params['id'] = book_id
    if current_user.can('change_role'):
        query = ('UPDATE users SET last_name=%(last_name)s, first_name=%(first_name)s, '
                'middle_name=%(middle_name)s, role_id=%(role_id)s WHERE id=%(id)s;')
    else:
        del params['role_id']
        query = ('UPDATE users SET last_name=%(last_name)s, first_name=%(first_name)s, '
                'middle_name=%(middle_name)s WHERE id=%(id)s;')
    try:
        with db.connection().cursor(named_tuple=True) as cursor:
            cursor.execute(query, params)
            db.connection().commit()
            flash('Успешно!', 'success')
    except mysql.connector.errors.DatabaseError:
        db.connection().rollback()
        flash('При сохранении данных возникла ошибка.', 'danger')
        return render_template('users_edit.html', user = params, roles_list = load_roles())

    return redirect(url_for('users'))

@app.route('/users/<int:user_id>/edit')
@login_required
@permission_check('edit')
def edit_user(user_id):
    query = 'SELECT * FROM users WHERE users.id = %s;'
    cursor = db.connection().cursor(named_tuple=True)
    cursor.execute(query, (user_id,))
    user = cursor.fetchone()
    cursor.close()
    return render_template('users_edit.html', user=user, roles_list = load_roles())

@app.route('/users/<int:user_id>/delete', methods=['POST'])
@login_required
@permission_check('delete')
def delete_user(user_id):
    query = 'DELETE FROM users WHERE users.id=%s;'
    try:
        cursor = db.connection().cursor(named_tuple=True)
        cursor.execute(query, (user_id,))
        db.connection().commit()
        cursor.close()
        flash('Пользователь успешно удален', 'success')
    except mysql.connector.errors.DatabaseError:
        db.connection().rollback()
        flash('При удалении пользователя возникла ошибка.', 'danger')
    return redirect(url_for('users'))

@app.route('/user/<int:user_id>')
@login_required
@permission_check('show')
def show_user(user_id):
    query = 'SELECT * FROM users WHERE users.id = %s;'
    cursor = db.connection().cursor(named_tuple=True)
    cursor.execute(query, (user_id,))
    user = cursor.fetchone()
    cursor.close()

    if user is not None:
        return render_template('users_show.html', user=user)
    else:
        flash('Недостаточно прав', 'warning')
        return redirect(url_for('users'))


from django.conf.urls import url, include
from rest_framework import routers
from base import views
from django.contrib import admin
from rest_framework_jwt import views as jwt_views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
router.register(r'gafs', views.GAFViewSet)
router.register(r'challenges', views.ChallengeViewSet)
router.register(r'assessments', views.AssessmentViewSet)
router.register(r'papers', views.PaperViewSet)
router.register(r'genes', views.GeneViewSet)
router.register(r'organisms', views.OrganismViewSet)
router.register(r'refseq', views.RefSeqViewSet)

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api-token-auth/', jwt_views.obtain_jwt_token),
    url(r'^api-token-auth-api/', views.ObtainAuthToken.as_view()),
    url(r'^api-token-auth-whoami/', views.WhoAmI.as_view()),
    url(r'^api-token-refresh/', jwt_views.refresh_jwt_token),
    url(r'^api-token-verify/', jwt_views.verify_jwt_token),
    url(r'^api/', include('stored_messages.urls')),
    url(r'^mark_all_read/$', views.mark_all_read, name='mark_all_read'),
]


from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.prompts import (
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
    ChatPromptTemplate,
    MessagesPlaceholder
)
import streamlit as st
from streamlit_chat import message
from utils import *
import os
import pyttsx3
import speech_recognition as sr
import threading
import time
from gtts import gTTS
import playsound
from streamlit_card import card
import base64
from streamlit_option_menu import option_menu
import requests
from PIL import Image
from io import BytesIO

from streamlit_card import card

import nltk
from nltk.stem.lancaster import LancasterStemmer
import numpy as np
import tflearn
import tensorflow as tf
import random

# restore all of our data structures
import pickle
import requests
from tensorflow.keras.models import load_model

api_url="http://127.0.0.1:8000/fetchanswer/"
stemmer = LancasterStemmer()

data = pickle.load( open( "training_data", "rb" ) )
words = data['words']
classes = data['classes']
train_x = data['train_x']
train_y = data['train_y']

# import our chat-bot intents file
import json
with open('IntentBotData1.json') as json_data:
    intents = json.load(json_data)

# load our saved model
model = load_model("Intent_Model.h5")

def clean_up_sentence(sentence):
    # tokenize the pattern
    sentence_words = nltk.word_tokenize(sentence)
    # stem each word
    sentence_words = [stemmer.stem(word.lower()) for word in sentence_words]
    return sentence_words

# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
def bow(sentence, words, show_details=False):
    # tokenize the pattern
    sentence_words = clean_up_sentence(sentence)
    # bag of words
    bag = [0]*len(words)
    for s in sentence_words:
        for i,w in enumerate(words):
            if w == s:
                bag[i] = 1
                if show_details:
                    print ("found in bag: %s" % w)

    return(np.array(bag))

ERROR_THRESHOLD = 0.55
def classify(sentence):
    # generate probabilities from the model
    results = model.predict(np.array([bow(sentence, words)]))[0]
    print(results)
    print(len(results))
    # filter out predictions below a threshold
    results = [[i, r] for i, r in enumerate(results) if r > ERROR_THRESHOLD]
    print(results)
    # sort by strength of probability
    results.sort(key=lambda x: x[1], reverse=True)
    print(results)
    return_list = []
    for r in results:
        return_list.append((classes[r[0]], r[1]))
    # return tuple of intent and probability
    print(return_list)
    return return_list

def response(sentence, userID='123', show_details=False):
    results = classify(sentence)
    # if we have a classification then find the matching intent tag
    if results:
        # loop as long as there are matches to process
        while results:
            for i in intents['intents']:
                # find a tag matching the first result
                if i['tag'] == results[0][0]:
                    # a random response from the intent
                    return random.choice(i['responses'])
            results.pop(0)
    else:
        data = None  # Initialize data with None

        try:
            params = {"question": sentence}
            response = requests.get(api_url, params=params)
            print(response.text)
            data = response.json()
        except Exception as e:
            print(e)

        # Now you can check if data is not None before trying to access it
        if data is not None:
            return data['question']
        else:
            return "An error occurred"

PAGE_TITLE: str = "AI Talks"
PAGE_ICON: str = "ðŸ¤–"
LANG_EN: str = "En"

location_map=['washroom']

st.set_page_config(page_title=PAGE_TITLE, page_icon=PAGE_ICON)
st.title('ðŸ¤– Parklink System Chatbot')

selected_lang = option_menu(
    menu_title="",
    options=[LANG_EN],
    icons=["globe2", "translate"],
    menu_icon="cast",
    default_index=0,
    orientation="horizontal",
)
# Store the image URL in session state
if 'image_url' not in st.session_state:
    st.session_state['image_url'] = "url"

if 'responses' not in st.session_state:
    st.session_state['responses'] = ["How can I assist you?"]

if 'requests' not in st.session_state:
    st.session_state['requests'] = []

# Create a pyttsx3 object for text to speech
engine = pyttsx3.init()

# Create a speech recognition object for speech to text
r = sr.Recognizer()

# Define a function to listen to the user's voice and return the text
def listen():
    # Use the default microphone as the audio source
    with sr.Microphone() as source:
        # Listen for the user's voice
        print("Listening...")
        audio = r.listen(source)
    try:
        # Recognize the user's voice using Google Speech Recognition
        text = r.recognize_google(audio)
        # Print the user's input
        print(f"You said: {text}")
        # Return the text
        return text
    except Exception as e:
        # If there is an error, return None
        print(str(e))
        print("Sorry, I could not understand you.")
        return None

# Define a function to speak the chatbot's response
def speak(text):
    try:
        print(f"AI said: {text}")
        tts = gTTS(text=text, lang='en',tld='ca')
        tts.save("good.mp3")
        playsound.playsound('good.mp3')
        os.remove('good.mp3')
    except Exception as e:
        print("Exception occured:",e)

# container for chat history
response_container = st.container()
# container for text box
textcontainer = st.container()

if st.button("Voice-Search ðŸŽ¤"):
# Listen to the user's input and store it in a variable
    query = listen()
    if query:
        with st.spinner("processing..."):
            result=response(query)
            print("Response------>",result)
            # conversation_string = get_conversation_string()
            # # st.code(conversation_string)
            # refined_query = query_refiner(conversation_string, query)
            # st.subheader("Refined Query:")
            # st.write(result)
            # context = find_match(refined_query)
            # print(context)
            # response = conversation.predict(input=f"Context:\n {context} \n\n Query:\n{query}")
            # if "content/images" in response and "jpg" in response:
            #     speak("Here is the map for the desired location:")
            # else:
            response=result
            speak(result)
            # print("hello")

        st.session_state.requests.append(query)
        st.session_state.responses.append(response)

with textcontainer:
    with st.form("Query Form", clear_on_submit=True):
        query = st.text_input("Query: ", key="input")
        submit_button = st.form_submit_button("Submit")

        if submit_button and query:
            with st.spinner("typing..."):
                result = response(query)
                print("Response------>", result)
                response = result
                st.session_state.requests.append(query)
                st.session_state.responses.append(response)

with response_container:
    print(st.session_state['responses'])
    print(response_container)
    if st.session_state['responses']:
        for i in range(len(st.session_state['responses'])):
            if "images/" in st.session_state['responses'][i] and st.session_state['image_url']!="url":
                st.image(st.session_state['responses'][i], caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
            message(st.session_state['responses'][i],key=str(i))
            if i < len(st.session_state['requests']):
                message(st.session_state["requests"][i], is_user=True,key=str(i)+ '_user')

        try:
            print(st.session_state['responses'])
            last_element=st.session_state['responses'][-1]
            if 'images/' in last_element and '.jpeg' in last_element and st.session_state['image_url']=="url":
                print("True")
                with st.chat_message("Response:"):
                    contentIndex=st.session_state['responses'][-1].index('images')
                    jpgIndex=st.session_state['responses'][-1].index('.jpeg')
                    url=st.session_state['responses'][-1][contentIndex:jpgIndex+5]
                    print(st.session_state['responses'][-1][contentIndex:jpgIndex+5])
                    print(st.session_state['image_url'])
                    st.session_state['image_url'] = url
                    print(st.session_state['image_url'])
                    st.image(st.session_state['image_url'], caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")

        except Exception as e:

            print("Exception Occured :", e)


from __future__ import absolute_import
from __future__ import division
# Standard __future__ imports
from __future__ import print_function

import apache_beam as beam
from tensorflow_model_analysis.evaluators.query_metrics import query_types
from tensorflow_model_analysis.post_export_metrics import metric_keys

from typing import Any, Dict, List, NamedTuple, Text

_State = NamedTuple('_State', [('total_queries', int), ('total_documents', int),
                               ('min_documents', int), ('max_documents', int)])

class QueryStatisticsCombineFn(beam.CombineFn):
  """Computes simple statistics about queries."""

  LARGE_INT = 1000000000

  def create_accumulator(self):
    return _State(
        total_queries=0,
        total_documents=0,
        min_documents=self.LARGE_INT,
        max_documents=0)

  def add_input(self, accumulator: _State,
                query_fpl: query_types.QueryFPL) -> _State:
    return _State(
        total_queries=accumulator.total_queries + 1,
        total_documents=accumulator.total_documents + len(query_fpl.fpls),
        min_documents=min(accumulator.min_documents, len(query_fpl.fpls)),
        max_documents=max(accumulator.max_documents, len(query_fpl.fpls)))

  def merge_accumulators(self, accumulators: List[_State]) -> _State:
    total_queries = 0
    total_documents = 0
    min_documents = self.LARGE_INT
    max_documents = 0
    for acc in accumulators:
      total_queries += acc.total_queries
      total_documents += acc.total_documents
      min_documents = min(min_documents, acc.min_documents)
      max_documents = max(max_documents, acc.max_documents)
    return _State(
        total_queries=total_queries,
        total_documents=total_documents,
        min_documents=min_documents,
        max_documents=max_documents)

  def extract_output(self, accumulator: _State) -> Dict[Text, Any]:
    return {
        metric_keys.base_key('total_queries'): accumulator.total_queries,
        metric_keys.base_key('total_documents'): accumulator.total_documents,
        metric_keys.base_key('min_documents'): accumulator.min_documents,
        metric_keys.base_key('max_documents'): accumulator.max_documents
    }


__author__ = 'aditichaudhary'
import codecs
import math
from collections import defaultdict
import numpy as np
np.random.seed(1)
import os
from sklearn.metrics import pairwise_distances_argmin_min
from copy import deepcopy
import pickle

def getTokenIndexInfo(data_loader, gold_dict, model, output_dict, sentence_index, sentence_index_sent,
                      sentence_index_tokens, token_index):
    sent_index = model.token_index_sent_index_map[token_index]
    sentence_index.append(sent_index)
    sent = model.sent_index_sent_map[sent_index]
    sent = [data_loader.id_to_word[int(token)] for token in sent]
    gold_path = gold_dict[" ".join(sent)]
    sentence_index_sent[sent_index] = (sent, gold_path, output_dict[" ".join(sent)])
    sentence_index_tokens[sent_index].append(model.token_index_relative_idx_map[token_index])
    return sent, model.token_index_relative_idx_map[token_index]

def createAnnotationOutput_mimic_oracle(args, model, data_loader, gold_dict, output_dict):
    sentence_index = []
    sentence_index_sent = {}
    sentence_index_tokens = defaultdict(list)

    typeTag, typeTagIndex, tokenIndex_map, sentIndex_map, _ = readTrain(args.test_path)
    data = {}
    with codecs.open(args.to_annotate, "w", encoding='utf-8') as fout, codecs.open(args.debug, "w", encoding='utf-8') as fdebug:
        sorted_type = sorted(model.approxTypeErrors.items(), key=lambda kv: kv[1], reverse=True )[:args.k]
        fdebug.write("TOKEN\tTYPE\tGOLD\tPRED\tPREDPROB\tERRORS\n")
        for (type, error_percent) in sorted_type:
            token_pred_error = model.predTypeErrors[type]
            token_tag_error = model.approxTokenClassErrors[type]
            sorted_token_tag_error = sorted(token_tag_error.items(),key= lambda kv: kv[1], reverse=True)
            errors = []
            maxTag = sorted_token_tag_error[0][0]
            for (tagId, error) in sorted_token_tag_error:
                tag = data_loader.id2tags["POS"][tagId]
                errors.append(tag + "=" + str(error))

            predErrors = []
            sorted_tag_error = sorted(token_pred_error.items(),key= lambda kv: kv[1], reverse=True)
            for (tagId, error) in sorted_tag_error:
                tag = data_loader.id2tags["POS"][tagId]
                predErrors.append(tag + "=" + str(error))

            token_indices = list(model.type_tokenIndices[type])
            required_embeddings, gold_token_tags, pred_token_tags = [], [],[]
            for token_index in token_indices:
                embedding = model.token_embeddings[token_index]
                gammaVal= model.token_gamma_key[token_index][maxTag]
                prob = np.exp(gammaVal)
                required_embeddings.append(embedding * prob)
                (token_, tag_, sent_index_, relative_index_) = tokenIndex_map[token_index]
                one_sent_ = sentIndex_map[sent_index_]
                pred_path_ = output_dict[" ".join(one_sent_)]
                gold_path_ = gold_dict[" ".join(one_sent_)]
                pred_token_tags.append(pred_path_[relative_index_])
                gold_token_tags.append(gold_path_[relative_index_])

            cluster_center = centeroidnp(np.array(required_embeddings))
            closest, _ = pairwise_distances_argmin_min(np.array([cluster_center]), required_embeddings)
            centroid = token_indices[closest[0]]

            (token, tag, sent_index, relative_index) = tokenIndex_map[centroid]

            one_sent = sentIndex_map[sent_index]

            sentence_index.append(sent_index)
            pred_path = output_dict[" ".join(one_sent)]
            gold_path = gold_dict[" ".join(one_sent)]
            sentence_index_sent[sent_index] = (one_sent, gold_path, pred_path)
            sentence_index_tokens[sent_index].append(relative_index)
            data[token] = {"tokenindices":token_indices, "weighted":required_embeddings, "centroid_center":cluster_center, "pred":pred_token_tags,  "gold":gold_token_tags}
            fdebug.write( str(centroid) + "\t" + data_loader.id_to_word[type] + "\t" + gold_path[relative_index] + "\t" + pred_path[relative_index] + "\t" + "@".join(predErrors) +"\t" + "@".join(errors) + "\n")

        covered = set()
        count = 0

        with open("./" + args.model_name + "approx.pkl", "wb") as f:
            pickle.dump(data, f)

        with codecs.open(args.to_annotate, "w", encoding='utf-8') as fout:

            for sent_index in sentence_index:
                if sent_index not in covered:
                    covered.add(sent_index)
                    (sent, gold_path, pred_path) = sentence_index_sent[sent_index]
                    path = deepcopy(pred_path)
                    for token_index in sentence_index_tokens[sent_index]:
                        path[token_index] = "UNK"

                    for token, tag_label, gold_tag in zip(sent, path, gold_path):
                        fout.write(token + "\t" + tag_label + "\t" + gold_tag + "\n")
                        if tag_label == "UNK":
                            count += 1

                    fout.write("\n")

def centeroidnp(arr):
    length, dim = arr.shape
    return np.array([np.sum(arr[:, i])/length for i in range(dim)])

def readTrain(input):
    typeTag = {}
    typeTagIndex = {}
    tokenIndex_map = {}
    sentIndex_map = {}
    one_sent = []
    output_dict = {}
    gold_path = []
    with codecs.open(input, "r", encoding='utf-8') as fin:
        token_index = 0
        sent_index = 0
        relative_index = 0
        for line in fin:
            if line.startswith("#"):
                continue
            if line == "" or line == "\n":
                sentIndex_map[sent_index] = one_sent
                sent_index += 1
                relative_index = 0
                output_dict[" ".join(one_sent)] = gold_path
                gold_path = []
                one_sent = []

            else:
                line = line.strip().split("\t")
                token, tag = line[1], line[5]
                one_sent.append(token)
                gold_path.append(tag)
                if token not in typeTag:
                    typeTag[token] = defaultdict(lambda:0)
                    typeTagIndex[token] = defaultdict(list)
                typeTag[token][tag] += 1
                typeTagIndex[token][tag].append(token_index)
                tokenIndex_map[token_index] = (token, tag, sent_index, relative_index)
                relative_index += 1
                token_index += 1

        return typeTag, typeTagIndex, tokenIndex_map, sentIndex_map, output_dict

def createAnnotationOutput_SPAN_wise(args, model, data_loader, gold_dict, output_dict, score_sentence):
    sentence_index = []
    reverse = True

    # Order the sentences by entropy of the spans
    fout = codecs.open(args.to_annotate, "w", encoding='utf-8')
    sorted_spans = sorted(model.most_uncertain_entropy_spans, key=lambda k: model.most_uncertain_entropy_spans[k],
                          reverse=reverse)
    print("Total unique spans: {0}".format(len(sorted_spans)))
    count_span = args.k
    count_tokens = args.k

    # DEBUG Print Span Entropy
    fdebug = codecs.open(args.debug, "w", encoding='utf-8')

    # Accumulate tokens by sentence
    sentence_index_sent = {}
    sentence_index_tokens = defaultdict(list)
    spans_index = 0
    for sorted_span in sorted_spans:
        # Debug
        spans_index += 1
        span_words = []
        span_in_text = data_loader.id_to_word[int(sorted_span)]

        if count_tokens <= 0:
            break

        (span_entropy, sentence_key, start, end, best_path, instance_entropy, sent_index, token_index) = \
        model.most_uncertain_entropy_spans[sorted_span]
        sent = sentence_key.split()
        sent = [data_loader.id_to_word[int(token)] for token in sent]
        gold_path = gold_dict[" ".join(sent)]

        if args.normdata and span_in_text.lower() in data_loader.word_to_id:
            sorted_span = data_loader.word_to_id[span_in_text.lower()]

        tokenEntropy = model.typeHeap[str(sorted_span)]
        tokenInfo = model.heapInfo[str(sorted_span)]

        sorted_tokenEntropy, sorted_tokenInfo = zip(*(sorted(zip(tokenEntropy, tokenInfo), reverse=True)))
        (t_sentence_key, t_start, t_sent_index) = sorted_tokenInfo[0]
        t_sent = [data_loader.id_to_word[int(token)] for token in t_sentence_key.split()]
        t_gold_path = gold_dict[" ".join(t_sent)]
        sentence_index.append(t_sent_index)
        sentence_index_sent[t_sent_index] = (t_sent, t_gold_path, output_dict[" ".join(t_sent)])
        sentence_index_tokens[t_sent_index].append(t_start)
        span_words.append(t_sent[t_start])
        pred_path = output_dict[" ".join(t_sent)]
        fdebug.write(
            str(t_sent[t_start]) + "\t" + " ".join(span_words) + "\t" + str(span_entropy) + "\t" + pred_path[
                t_start] + "\t" + t_gold_path[t_start] + "\n")
        count_tokens -= 1

    # print remaining cluster stats
    remaining_spans = sorted_spans[spans_index:]
    for span in remaining_spans:

        (span_entropy, sentence_key, start, end, best_path, instance_entropy, sent_index, token_index) = \
        model.most_uncertain_entropy_spans[span]
        sent = sentence_key.split()
        sent = [data_loader.id_to_word[int(token)] for token in sent]
        gold_path = gold_dict[" ".join(sent)]
        span_words = []
        span_in_text = data_loader.id_to_word[int(span)]
        sentence_index_sent[sent_index] = (sent, gold_path, output_dict[" ".join(sent)])
        fdebug.write(str(token_index) + "\t" + " ".join([sent[start]]) + "\t" + str(span_entropy) + "\n")

    covered = set()
    count = 0
    for sent_index in sentence_index:
        if sent_index not in covered:
            covered.add(sent_index)
            (sent, gold_path, path) = sentence_index_sent[sent_index]
            for token_index in sentence_index_tokens[sent_index]:
                path[token_index] = "UNK"

            for token, tag_label, gold_tag in zip(sent, path, gold_path):
                fout.write(token + "\t" + tag_label + "\t" + gold_tag + "\n")
                if tag_label == "UNK":
                    count += 1

            fout.write("\n")

    print("Total unique spans for exercise: {0}".format(args.k))
    print("Total spans for exercise: {0}".format(count))


# You are given a list of sentences as input. Your task is to create a program
# that counts the frequency of each word in the sentences and returns a dictionary
# with the word as the key and its frequency as the value. You should ignore case
# sensitivity and punctuation while counting the words.

import re
from collections import Counter

def count_word_frequency(sentences):
    word_frequency = Counter()
    for sentence in sentences:
        sentence = re.sub(r'[^\w\s]', '', sentence.lower())
        words = sentence.split()
        word_frequency.update(words)

    return dict(word_frequency)

# list of sentences
sentences = [
    "I went to college today morning with my Friend.",
    "Even though the food was very spicy, my friend managed to eat it somehow!",
    "I went to temple also!"
]

frequency = count_word_frequency(sentences)
print(frequency)


from entities.board import board
from logic.spaceCreator import spaceCreator
import os
import time
from pynput import keyboard

class gameLogic:
    def __init__(self):
        self.board = None
        self.availableDificulties = [
            "easy",
            "medium",
            "hard"
        ]

        self.boardSizes = {
            "easy": (8,8,10),
            "medium": (10,10,20),
            "hard": (20,20,100)
        }

        self.neighbour_map = [(0,-1),(0,1),(-1,-1),(-1,0),(-1,1),(1,-1),(1,0),(1,1)]

        self.gameover = False
        self.won = False
        self.flaggedMines = []
        self.uncovered_spaces = 0
        self.x = 0
        self.y = 0
        self.clear = lambda: os.system('cls' if os.name=='nt' else 'clear')

    def startGame(self):
        print("Minesweeper")
        print("Choose game difficulty:")
        for i in range(len(self.availableDificulties)):
            print(str(i+1)+"."+self.availableDificulties[i])
        if self.selectedDifficulty(input())==False:
            self.startGame()
        else:
            listener = keyboard.Listener(on_press=self.__keyboard_listener,suppress=True)
            listener.start()
            while(self.gameover==False):
                self.clear()
                self.__print_board(self.board.fakeArray)
                self.__print_controls()
                time.sleep(.500)
                self.clear()
                self.__print_board(self.board.fakeArray,True)
                self.__print_controls()
                time.sleep(.500)
            listener.stop()
            if(self.gameover and self.won == False):
                self.clear()
                self.__print_board(self.board.fakeArray)
                print("Game over")
            elif(self.gameover and self.won):
                self.clear()
                self.__print_board(self.board.fakeArray)
                print("You won")
            input()

    def selectedDifficulty(self,selectedOption)->bool:
        self.clear()
        selectedOption = int(selectedOption)-1
        if selectedOption  not in range(len(self.availableDificulties)):
            return False
        else:
            selecteDificulty = self.availableDificulties[selectedOption]
            (width,height,mines)=self.boardSizes[selecteDificulty]
            print("You choosed: "+self.availableDificulties[selectedOption])
            print("This option, will create a board of {0} by {1} , with {2} mines".format(width,height,mines))
            print("Are you sure?")
            confirm = input("Yes/No:\n")
            if confirm.lower() == "yes":
                self.board = board(width,height,mines)
                space = spaceCreator(self.board)
            else:
                return False

    def __print_board(self,board, cursorBlink = False):
        for i in range(len(board)):
            row = ""
            for j in range(len(board)):
                if self.y == i and self.x == j and cursorBlink:
                    row += "\u2588"+","
                elif board[i][j] == None:
                    row += "?,"
                else:
                    row += board[i][j]+","
            print(row[0:len(row)-1])

    def __print_controls(self):
        print("Mines: "+str(self.board.mines)+"  Flags: "+str(len(self.flaggedMines)))
        print("Move with arrow keys")
        print("Press F to flag mines")
        print("Press SPACE key to uncover square")
        print("Press ESC to close game")

    def __keyboard_listener(self,key):
        if key == keyboard.Key.esc:
            self.gameover = True
        elif key == keyboard.Key.down and self.y < len(self.board.boardArray)-1:
            self.y += 1
        elif key == keyboard.Key.up and self.y > 0:
            self.y -= 1
        elif key == keyboard.Key.left and self.x > 0:
            self.x -= 1
        elif key == keyboard.Key.right and self.x < len(self.board.boardArray[self.y])-1:
            self.x += 1
        elif key == keyboard.Key.space:
            self.__uncover_spaces()
        else:
            try:
                if key.char == 'F' or key.char == 'f':
                    self.__flag_space()
                print(key)
            except AttributeError:
                print("special key {0} ".format(key))

    def __uncover_spaces(self):
        if(self.board.boardArray[self.y][self.x]=="*" and self.board.fakeArray[self.y][self.x]=="?"):
            self.board.fakeArray[self.y][self.x] = "*"
            self.gameover = True
        elif(self.board.boardArray[self.y][self.x]==" "):
            self.board.fakeArray[self.y][self.x] = self.board.boardArray[self.y][self.x]
            self.uncovered_spaces += 1
            self.__run_uncover_task()
        elif(self.board.boardArray[self.y][self.x] in ['1','2','3','4','5','6','7','8']):
            self.board.fakeArray[self.y][self.x] = self.board.boardArray[self.y][self.x]
            self.uncovered_spaces += 1
        self.__is_winner()

    def __run_uncover_task(self,space=None):
        for x,y in self.neighbour_map:
            new_x = (self.x if space == None else space["x"])+x
            new_y = (self.y if space == None else space["y"])+y
            if(new_x<0 or new_y<0 or new_x>self.board.width-1 or new_y>self.board.height-1):
                continue
            if(self.board.fakeArray[new_y][new_x]=="?" and self.board.boardArray[new_y][new_x]=="*"):
                continue
            elif(self.board.fakeArray[new_y][new_x]=="?" and self.board.boardArray[new_y][new_x]==" "):
                self.board.fakeArray[new_y][new_x]=" "
                self.uncovered_spaces+=1
                self.__run_uncover_task({"x":new_x,"y":new_y})
            elif(self.board.fakeArray[new_y][new_x]=="?" and self.board.boardArray[new_y][new_x] in ['1','2','3','4','5','6','7','8']):
                self.board.fakeArray[new_y][new_x]=self.board.boardArray[new_y][new_x]
                self.uncovered_spaces+=1

    def __flag_space(self):
        if(self.board.fakeArray[self.y][self.x]=="?") and len(self.flaggedMines)<self.board.mines:
            self.board.fakeArray[self.y][self.x] = "F"
            self.flaggedMines.append((self.y,self.x))
        elif(self.board.fakeArray[self.y][self.x]=="F"):
            self.board.fakeArray[self.y][self.x] = "?"
            self.flaggedMines.remove((self.y,self.x))
        if(self.uncovered_spaces+len(self.flaggedMines) == self.board.width*self.board.height):
            self.won = True
            self.gameover = True
        self.__is_winner()

    def __is_winner(self):
        if(self.uncovered_spaces+len(self.flaggedMines) == self.board.width*self.board.height):
            self.won = True
            self.gameover = True


# 5 Exercise
# FUNCTIONS ADVANCED

# You will receive a sequence of numbers (integers), separated by a single space. Separate the negative numbers
# from the positive. Find the total sum of the negatives and positives, replace the negative number with its absolute
# value and print the following:
#
#  On the first line print the sum of the negatives
#  On the second line print the sum of the positives
#  On the third line:
# o If the absolute negative number is bigger than the positive number:
# "The negatives are stronger than the positives"
# o If the positive number is bigger than the absolute negative number:
# "The positives are stronger than the negatives"

data = [int(v) for v in input().split()]
negative = [v for v in data if v < 0]
absolute = [abs(v) for v in data if v < 0]
positive = [v for v in data if v >= 0]
print(sum(negative))
print(sum(positive))
if sum(absolute) > sum(positive):
    print("The negatives are stronger than the positives")
elif sum(positive) > sum(absolute):
    print("The positives are stronger than the negatives")

5 Functions Advanced/5EX4 Negative vs Positive.py


# https://leetcode.com/problems/convert-a-number-to-hexadecimal/
import os, sys, shutil, glob, re
import time, calendar
from datetime import datetime, timezone
import hashlib, zipfile, zlib
from math import *
from operator import itemgetter
from functools import wraps, cmp_to_key, reduce
from itertools import count, combinations, permutations
from collections import namedtuple, defaultdict, Counter
from queue import Queue
from bisect import bisect_left, bisect_right

def refreshGlobals():
  pass

refreshGlobals()

class Solution:
  def toHex(self, num):  # -> str
    chMap = list('0123456789abcdef')
    if num == 0:
      return "0"
    if num < 0:
      num = num + (1 << 32)
    print('bin(num):', bin(num))
    ans = ''
    while num:
      print('in loop num:', num)
      tailNum = ((1 << 4) - 1) & num
      ans = chMap[tailNum] + ans
      num >>= 4

    print('ans:', ans)
    return ans

if __name__ == '__main__' and ('SJDEAK' in os.environ):
  from utils.tree import TreeNode, array2TreeNode

  def test(*args):
    print('输入数据: ', *args)
    print('结果: ', Solution().toHex(*args), end='\n-----\n')

  test(26)
  test(33)
  test(1)
  test(0)
  test(-1)
else:
  print = lambda *args, **kwargs: None


import os
import subprocess

def handler(event, context):

    print('=== start func ===\n')

    if os.name=='nt':
        # windows
        cmd = 'main.exe'
    else:
        # not windows (expect posix)
        cmd = './main'

    subprocess.call(cmd)
    print('\n=== end func ====')

    ret = 'end'
    return ret

if __name__ == '__main__':
	handler(0, 0)


__author__ = 'Raredemon'

## Python modules
import re
## NOC modules
from noc.sa.script import Script as NOCScript
from noc.sa.interfaces import IGetChassisID

class Script(NOCScript):
    name = "BDCOM.xPON.get_chassis_id"
    implements = [IGetChassisID]
    cache = True

    rx_mac = re.compile(r"^Base ethernet MAC Address:\s+(?P<mac>\S+)$", re.MULTILINE)

    def execute(self):
        # Try SNMP first
        if self.snmp and self.access_profile.snmp_ro:
            try:
                macs = []
                for v in self.snmp.get_tables(
                    ["1.3.6.1.2.1.2.2.1.6.289"], bulk=True):
                        macs += [v[1]]
                return {
                    "first_chassis_mac": min(macs),
                    "last_chassis_mac": max(macs)
                }
            except self.snmp.TimeOutError:
                pass

        # Fallback to CLI
        match = self.rx_mac.search(self.cli("show version", cached=True))
        mac = match.group("mac")
        return {
            "first_chassis_mac": mac,
            "last_chassis_mac": mac
        }