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

import collections
import contextlib
import importlib
import inspect
import logging
import os
import pkgutil
import socket
import sys
from datetime import datetime

import aiohttp
import discord
import humanize
import psutil
from discord.ext import commands

from . import commands as inspector

from utils import db
from utils.scheduler import DatabaseScheduler
from utils.transformdict import CaseInsensitiveDict
from utils.time import duration_units

logger = logging.getLogger(__name__)
command_logger = logging.getLogger('commands')

# permission calculations for bot invitations
def _define_permissions(*perms):
    permissions = discord.Permissions.none()
    permissions.update(**dict.fromkeys(perms, True))
    return permissions

_PERMISSIONS = [
    'send_messages',
    'embed_links',
    'add_reactions',
    'attach_files',
    'use_external_emojis',
    'create_instant_invite',
    'manage_messages',
    'read_message_history',
]

# Let's make actual permissions out of that stuff.
_PERMISSIONS = _define_permissions(*_PERMISSIONS)
del _define_permissions

VersionInfo = collections.namedtuple('VersionInfo', 'major minor micro releaselevel serial')

class CodeInspector(commands.AutoShardedBot):
    __version__ = '1.0.0'
    version_info = VersionInfo(1, 0, 0, 'final', 0)

    def __init__(self, config: dict):
        super().__init__(
            command_prefix=commands.when_mentioned_or(*config['prefix']),
            description=config['description'],
            case_insensitive=True,
            fetch_offline_members=False,
        )

        self.app_info = None
        self.creator = None

        self.cogs = CaseInsensitiveDict()
        self.categories = collections.defaultdict(list)
        self.owners = config['owners']

        self.command_counter = collections.Counter()
        self.session = aiohttp.ClientSession(
            connector=aiohttp.TCPConnector(resolver=aiohttp.AsyncResolver(), family=socket.AF_INET),
            loop=self.loop
        )
        self.start_time = datetime.utcnow()
        self.pool = self.loop.run_until_complete(db.create_pool(config['pg_credentials']))
        self.process = psutil.Process(os.getpid())
        self.token = config['token']
        self.webhook_url = config['webhook_url']

        self.db_scheduler = DatabaseScheduler(self.pool, timefunc=datetime.utcnow)
        self.db_scheduler.add_callback(self._dispatch_from_scheduler)

        # add our error handler
        self.load_extension('core.error_handler')

        # load any extensions from the cogs directory
        for name in os.listdir('cogs'):
            if name.startswith('__'):
                continue

            self.load_extension(f'cogs.{name}')

    def _dispatch_from_scheduler(self, entry):
        self.dispatch(entry.event, entry)

    async def is_owner(self, user):
        if self.owners:
            return user.id in self.owners

        return await super().is_owner(user)

    def run(self):
        super().run(self.token, reconnect=True)

    async def ci_stats(self):
        await self.wait_until_ready()

        async with self.pool.acquire() as db:
            with self.process.oneshot():
                memory = self.process.memory_full_info()

                print(
                    '================ CI Stats ================\n'
                    '----------- System information -----------\n'
                    f'Running on {sys.platform} with Python {sys.version}.\n'
                    f'Using {humanize.naturalsize(memory.rss)} physical memory'
                    f' and {humanize.naturalsize(memory.vms)} virtual memory,'
                    f' of which {humanize.naturalsize(memory.uss)} unique to this process.\n'
                    f'PostgreSQL version: {".".join(map(str, db.get_server_version()[:3]))}\n'
                    '------------- Bot statistics -------------\n'
                    f'Version: {self.__version__}\n'
                    f'Online for: {self.uptime}\n'
                    f'Guilds: {len(self.guilds)}\n'
                    f'Users: {len(self.users)}\n'
                    f'Voice channels: {len(self.voice_clients)}\n'
                    f'Shards: {len(self.shards)}\n'
                    f'discord.py version: {discord.__version__}\n'
                    '=========================================='
                )

    async def logout(self):
        await self.session.close()
        self.db_scheduler.close()
        await super().logout()

    @staticmethod
    def search_extensions(name):
        spec = importlib.util.find_spec(name)
        if not spec:
            raise ModuleNotFoundError(f'No module called {name!r}')

        path = spec.submodule_search_locations
        if not path:
            return None

        return (name for info, name, is_pkg in pkgutil.iter_modules(path, spec.name + '.')
                if not is_pkg)

    def _add_extension(self, name):
        ext = importlib.import_module(name)
        if hasattr(ext, 'setup'):
            ext.setup(self)
            self.extensions[name] = ext
        else:
            for _, m in inspect.getmembers(ext):
                if inspect.isclass(m) and isinstance(m, inspector.MetaCog):
                    try:
                        m(self)
                    except Exception as e:
                        logger.error(e)

                    if name not in self.extensions:
                        self.extensions[name] = ext
                else:
                    continue

        if name not in self.extensions:
            del ext
            del sys.modules[name]
            raise discord.ClientException('extension does not have a setup function')

    def load_extension(self, name):
        modules = self.search_extensions(name)
        if not modules:
            return self._add_extension(name)

        for module in modules:
            try:
                self._add_extension(module)
            except discord.ClientException as e:
                if 'extension does not have a setup function' not in str(e):
                    raise

    def unload_extension(self, name):
        super().unload_extension(name)

        for module_name in self.extensions.keys():
            if name == module_name or module_name.startswith(name + '.'):
                self.remove_cog(module_name)
                del self.extensions[module_name]

    @contextlib.contextmanager
    def temp_listener(self, func, name=None):
        """Context manager for temporary listeners."""

        self.add_listener(func, name)
        try:
            yield
        finally:
            self.remove_listener(func)

    async def process_commands(self, message):
        ctx = await self.get_context(message, cls=inspector.Context)

        if ctx.command is None:
            return

        async with ctx.acquire():
            await self.invoke(ctx)

    async def on_ready(self):
        logger.info(f'\n================\nLogged in as:\n{self.user.name}\n{self.user.id}\n================\n')
        self.db_scheduler.run()

        if self.app_info is None:
            self.app_info = await self.application_info()

        if self.creator is None:
            self.creator = await self.get_user_info(self.app_info.owner.id)

        if not hasattr(self, 'start_time'):
            self.start_time = datetime.utcnow()

        await self.change_presence(
            activity=discord.Activity(name='your shitcode', type=discord.ActivityType.watching),
            status=discord.Status.dnd
        )

    async def on_message(self, message):
        if message.author.bot:
            return

        await self.process_commands(message)

    async def on_command(self, ctx):
        self.command_counter['total'] += 1
        if isinstance(ctx.channel, discord.abc.PrivateChannel):
            self.command_counter['in DMs'] += 1
        elif isinstance(ctx.channel, discord.abc.GuildChannel):
            self.command_counter[str(ctx.guild.id)] += 1

        # DM channels don't have a guild attribute
        if not ctx.guild:
            ctx.guild = 'DM channel'
            ctx.guild.id = 'no ID provided'

        fmt = ('Command executed in {0.channel} ({0.channel.id}) from {0.guild} ({0.guild.id})'
               ' by {0.author} ({0.author.id}) with content: {0.message.content}')
        command_logger.info(fmt.format(ctx))

    async def on_command_error(self, ctx, error):
        if not (
                isinstance(error, commands.CheckFailure)
                and not isinstance(error, commands.BotMissingPermissions)
                and await self.is_owner(ctx.author)
        ):
            return

        try:
            await ctx.release()
            async with ctx.acquire():
                await ctx.reinvoke()

        except Exception as e:
            await ctx.command.dispatch_error(ctx, e)

    @property
    def webhook(self):
        webhook_url = self.webhook_url
        if webhook_url:
            return discord.Webhook.from_url(webhook_url, adapter=discord.AsyncWebhookAdapter(self.session))

        return None

    @property
    def source(self):
        return 'https://github.com/itsVale/code-inspector'

    @property
    def uptime(self):
        return duration_units((datetime.utcnow() - self.start_time).total_seconds())

    @discord.utils.cached_property
    def invite_url(self):
        return discord.utils.oauth_url(self.user.id, _PERMISSIONS)


from collections.abc import Mapping, Set, Sequence, Iterator, Iterable

__all__ = ('isa', 'is_mapping', 'is_set', 'is_seq', 'is_list', 'is_tuple',
           'is_seqcoll', 'is_seqcont',
           'iterable', 'is_iter')

def isa(*types):
    """
    Creates a function checking if its argument
    is of any of given types.
    """
    return lambda x: isinstance(x, types)

is_mapping = isa(Mapping)
is_set = isa(Set)
is_seq = isa(Sequence)
is_list = isa(list)
is_tuple = isa(tuple)

is_seqcoll = isa(list, tuple)
is_seqcont = isa(list, tuple, Iterator, range)

iterable = isa(Iterable)
is_iter = isa(Iterator)


import numpy as np
import pandas as pd

DATASET_PATH=r'2000cellsPA20220407 all peaks hepg2.csv'
cell_data_df= pd.read_csv(DATASET_PATH,low_memory=False)
cell_data_df.head()
cell_data_df.columns
#specify the 990 features column names to be modelled
to_model_columns=cell_data_df.columns[1:990]

from sklearn.ensemble import IsolationForest
clf=IsolationForest(n_estimators=100, max_samples='auto', \
                        max_features=1.0, bootstrap=False, n_jobs=-1, random_state=42, verbose=0)
clf.fit(cell_data_df[to_model_columns])

pred = clf.predict(cell_data_df[to_model_columns])
cell_data_df['anomaly']=pred
outliers=cell_data_df.loc[cell_data_df['anomaly']==-1]
outlier_index=list(outliers.index)
#print(outlier_index)
#Find the number of anomalies and normal points here points classified -1 are anomalous
print(cell_data_df['anomaly'].value_counts())
print(outliers)
outliers.to_csv(r'Outliers HepG2 32 in 1000-052311.csv')

#3D PCA
#HepG2 #66c2a5
#MCF7 #fc8d62
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from mpl_toolkits.mplot3d import Axes3D
pca = PCA(n_components=3)  # Reduce to k=3 dimensions
scaler = StandardScaler()
#normalize the cell_data
X = scaler.fit_transform(cell_data_df[to_model_columns])
X_reduce = pca.fit_transform(X)

figsize=(6, 6)
fig = plt.figure(figsize=figsize, dpi=300)
ax = fig.add_subplot(111, projection='3d')
ax.set_zlabel("x_composite_3")

# Plot the compressed data points
ax.scatter(X_reduce[:, 0], X_reduce[:, 1], zs=X_reduce[:, 2], s=8, lw=1, label="Normal",c="#a6d854")

# Plot x's for the ground truth outliers
ax.scatter(X_reduce[outlier_index,0],X_reduce[outlier_index,1], X_reduce[outlier_index,2],
           lw=2, s=33, marker="x", c="red", label="Outliers")
ax.legend()
plt.show()

#2D PCA
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

pca2 = PCA(n_components=2)# Reduce to k=2 dimensions
scaler = StandardScaler()
#normalize the cell_data
XT = scaler.fit_transform(cell_data_df[to_model_columns])
pca2.fit(XT)
res=pd.DataFrame(pca2.transform(XT))

Z = np.array(res)
figsize=(8, 8)
plt.figure(figsize=figsize, dpi=300)
plt.title("Isolation Forest")
#plt.contourf(Z, cmap=plt.cm.Blues_r)

b1 = plt.scatter(res[0], res[1], c='#a6d854', s=33,label="Normal")

b1 = plt.scatter(res.iloc[outlier_index,0],res.iloc[outlier_index,1], c='red',
                 s=33, edgecolor="red",label="Outliers")
plt.legend(loc="upper right")
plt.show()


"""
Compare Game of Life grids represented in the Full-matrix (FM) format.

Every file's first row will indicate the total number of rows and cols of GoL's matrix, followed by a line for each row, wherein 'X' will indicate alive cells, whereas blank spaces dead cells. That matrix will represent the initial state (0-th generation) of GoL's evolution. A delimiter of 100 '*' characters will separate it from the final state matrix (N-th generation).
"""

import argparse
import os

import numpy as np

from concurrent.futures import thread # Parallelize comparisons

DELIMITER = "*" * 100

def parse_args():
    parser = argparse.ArgumentParser(add_help=False,
                            description='Comparing results from Game of Life experiments.')

    # Split required/optional args
    required = parser.add_argument_group('required arguments')
    optional = parser.add_argument_group('optional arguments')

    # Add back help
    optional.add_argument(
        '-h',
        '--help',
        action='help',
        default=argparse.SUPPRESS,
        help='Show this help message and exit'
    )

    required.add_argument('--gt',  type=str, required=True, nargs=None,
                        help="The filename of GoL's ground-truth grid")
    required.add_argument('--cmp', type=str, required=True, nargs='+',
                        help="The list of filenames of GoL's grids to compare. ")

    return parser.parse_args()

if __name__ == "__main__":
    args = parse_args()

    print("[*] Reading GoL's initial ground-truth matrix...")

    # Read the first line of the ground-truth file,
    # which contains GoL matrix's number of rows and columns
    gt_file = open(args.gt, "r")

    first_line_gt = gt_file.readline().split()
    rows, cols = list(map(lambda val: int(val.strip()),
                          first_line_gt))

    ###############################
    # 1. Compare initial matrices #
    ###############################

    # Initialize the ground-truth's NumPy matrix with
    # that number of rows and columns
    gt = np.zeros((rows, cols)).astype(int)

    # Fill the ground-truth matrix
    for i, line in enumerate(gt_file):
        line = line.rstrip("\n")
        if (line == DELIMITER):
            break

        row = list(map(lambda val: 1 if val == 'X' else 0, line))
        gt[i] = np.array(row)

    print("[*] Read GoL's initial ground-truth matrix.\n")

    def compare_initial(filename):
        cmp_file = open(filename, "r")
        first_line_cmp = cmp_file.readline().split()

        rows_cmp, cols_cmp = list(map(lambda val: int(val.strip()),
                                      first_line_cmp))

        if rows_cmp != rows or cols_cmp != cols:
            return [False, cmp_file]
        else:
            cmp = np.zeros((rows, cols)).astype(int)

            for i, line in enumerate(cmp_file):
                line = line.rstrip("\n")
                if (line == DELIMITER):
                    break

                row = list(map(lambda val: 1 if val == 'X' else 0, line))
                cmp[i] = np.array(row)

            return [np.allclose(gt, cmp), cmp_file, cmp]

    print("[*] Reading GoL's initial comparison matrices...")

    with thread.ThreadPoolExecutor(max_workers=16) as exec:
        cmp_results = list(exec.map(compare_initial, args.cmp)) # Store comparison results

    print("[*] Read GoL's initial comparison matrices.\n")

    basename_cmps = []
    basename_gt = os.path.basename(args.gt)

    # Print initial comparison results
    for i, filename in enumerate(args.cmp):
        basename_cmp = os.path.basename(filename)
        basename_cmps.append(basename_cmp)

        # cmp_results[i][0] := Initial matrix comparison result
        # cmp_results[i][1] := Open file pointer or None
        # cmp_results[i][2] := Initial matrix, if it exists

        if cmp_results[i][0]:
            print(basename_gt, "initial matrix is equal to", basename_cmp)
        else:
            print(basename_gt, "initial matrix is different from", basename_cmp)

            # Close file pointers whose initial matrix
            # is different from the ground-truth.
            cmp_results[i][1].close() # Contains the open file pointer
            cmp_results[i][1] = None

    #############################
    # 2. Compare final matrices #
    #############################

    print("\n[*] Reading GoL's final ground-truth matrix...")

    # Fill the ground-truth matrix
    for i, line in enumerate(gt_file):
        line = line.rstrip("\n")
        if (line == DELIMITER):
            break

        row = list(map(lambda val: 1 if val == 'X' else 0, line))
        gt[i] = np.array(row)

    gt_file.close()

    print("[*] Read GoL's final ground-truth matrix.\n")

    def compare_final(data):
        # data[0] := Initial matrix comparison result
        # data[1] := Open file pointer or None
        # data[2] := Initial matrix, if it exists

        if not data[0]:
            return False
        else:
            for i, line in enumerate(data[1]):
                line = line.rstrip("\n")
                if (line == DELIMITER):
                    break

                row = list(map(lambda val: 1 if val == 'X' else 0, line))
                data[2][i] = np.array(row)

            data[1].close() # Close file pointer

            return np.allclose(gt, data[2])

    print("[*] Reading GoL's final comparison matrices...")

    with thread.ThreadPoolExecutor(max_workers=16) as exec:
        cmp_results = list(exec.map(compare_final, cmp_results)) # Store comparison results

    print("[*] Read GoL's final comparison matrices.\n")

    # Print final comparison results
    for i, filename in enumerate(args.cmp):
        basename_cmp = basename_cmps[i]

        if cmp_results[i]:
            print(basename_gt, "final matrix is equal to", basename_cmp)
        else:
            print(basename_gt, "final matrix is different from", basename_cmp)


import os
import json

from .get_com import get_commits

# 监控信息文件
current_dir = os.path.join(os.path.dirname(__file__), 'config.json')

# 监控
async def watch_depo(gid, uid, url):
    with open(current_dir, 'r', encoding="UTF-8") as f:
        file_data = json.load(f)
    group_info = file_data.get(gid, {})
    user_info = group_info.get(uid, {})
    if user_info.get(url, {}): return '你已经监控过该仓库了，无需再次监控'
    data_list = await get_commits(url)
    priv_time = str(data_list[0]['time'])
    user_info[url] = priv_time
    group_info[uid] = user_info
    file_data[gid] = group_info
    with open(current_dir, 'w', encoding="UTF-8") as f:
        json.dump(file_data, f, indent=4, ensure_ascii=False)
    return f'您已成功在群{gid}里监控仓库：{url}'

# 取消监控
async def unwatch_depo(gid, uid, url):
    with open(current_dir, 'r', encoding="UTF-8") as f:
        file_data = json.load(f)
    group_info = file_data.get(gid, {})
    user_info = group_info.get(uid, {})
    if not user_info.get(url, {}): return '你还未监控过该仓库，请先使用命令监控'
    file_data[gid][uid].pop(url)
    with open(current_dir, 'w', encoding="UTF-8") as f:
        json.dump(file_data, f, indent=4, ensure_ascii=False)
    return f'您已成功在群{gid}里取消监控仓库：{url}'

# 监控查询
async def query_depo(gid, uid):
    with open(current_dir, 'r', encoding="UTF-8") as f:
        file_data = json.load(f)
    group_info = file_data.get(gid, {})
    user_info = group_info.get(uid, {})
    return '您暂未监控任何仓库呢！' if not user_info else '您所监控的仓库有：\n' + '\n'.join(list(user_info.keys()))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
from nltk.corpus import stopwords
import spacy
import en_core_web_sm
import pandas as pd
from gensim.models import Word2Vec
from gensim.models.phrases import Phrases, Phraser
from tqdm import tqdm
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

# Call in and clean data
now_articles = pd.read_csv('./now_articles.txt', sep = "\t",
                          names = ['date', 'period', 'outlet', 'outlet_type', 'politics', 'tag', 'text', 'title'],
                          skip_blank_lines = True).dropna()
now_articles['year'] = pd.to_datetime(now_articles.date).apply(lambda x: x.year)
df = now_articles.loc[(now_articles.year.isin([2013, 2014, 2015, 2016, 2017, 2018])) &\
                       (now_articles.outlet.isin(['Daily Mail', 'Guardian']))]
df['mods'] = df.outlet + "_" +  df.year.apply(lambda x: 'Pre' if x < 2016 else 'Post')

# Retokenize entities
class EntityRetokenizeComponent:
  def __init__(self, nlp):
    pass
  def __call__(self, doc):
    for entity in doc.ents:
        if entity.label_ in ["PERSON", "ORG", "GPE", "NORP"]:
            with doc.retokenize() as retokenizer:
                retokenizer.merge(entity)
    return doc

nlp = en_core_web_sm.load()
retokenizer = EntityRetokenizeComponent(nlp)
nlp.add_pipe(retokenizer, name='merge_phrases', last=True)
StopWords = set(stopwords.words('english'))
StopWords.update(['-PRON-'])

for mod in ['Guardian_Pre', 'Guardian_Post', 'Daily Mail_Pre', 'Daily Mail_Post']:
    raw_data = df.loc[df.mods == mod]

    articles = []
    for art in tqdm(nlp.pipe(raw_data.text, n_process=12, batch_size =500)):
        articles.append([tok.lemma_.lower().strip() for tok in art if len(tok.text.strip()) > 1])

    phrases = Phrases(articles, common_terms = StopWords)
    bigram = Phraser(phrases)
    tphrases = Phrases(bigram[articles], common_terms = StopWords)
    trigram = Phraser(tphrases)

    phrased_articles = [trigram[bigram[art]] for art in articles]

    model = Word2Vec(phrased_articles, size=300, window=7, min_count=200, workers=12, sg = 1, iter = 15)
    model.save("./word2vec/{}.model".format(mod))


import json
from flask import request, _request_ctx_stack, abort, Flask
from functools import wraps
from jose import jwt
from urllib.request import urlopen

AUTH0_DOMAIN = 'dev-04zvrt8l.us.auth0.com'
ALGORITHMS = ['RS256']
API_AUDIENCE = 'Casting-Agency'

'''
AuthError Exception
A standardized way to communicate auth failure modes
'''
class AuthError(Exception):
    def __init__(self, error, status_code):
        self.error = error
        self.status_code = status_code

## Auth Header
def get_token_auth_header():
    auth_header = request.headers.get('Authorization', None)
    if auth_header is None:
        print("ZZZZZZZZZZZZZZZZZZZ=11111111")
        raise AuthError({
            'code': 'authorization_header_missing',
            'description': 'Authorization header is expected within the request.'
        }, 401)

    # the token format is "Bearer <Token-Characters>" and token_parts length must be 2
    token_parts = auth_header.split()
    if (token_parts[0].lower() != 'bearer') or len(token_parts) != 2:
        raise AuthError({
            'code': 'invalid_header',
            'description': 'Authorization header must contains "Bearer Token".'
        }, 401)

    token = token_parts[1]
    return token

def check_permissions(permission, payload):
    # the payload is deccoded
    if 'permissions' not in payload:
        raise AuthError({
            'code': 'invalid_claims',
            'description': 'Permissions not found in JWT payload.'
        }, 400)

    if permission not in payload['permissions']:
        raise AuthError({
            'code': 'unauthorized',
            'description': 'Permission not found.'
        }, 403)
    return True

def verify_decode_jwt(token):
    # Get the public key from AUTH0
    jsonurl = urlopen(f'https://{AUTH0_DOMAIN}/.well-known/jwks.json')
    jwks = json.loads(jsonurl.read())

    # Get the data in the header:
    # to unpack the header of the (jwt)token to determine if we have the correct key!
    # cuz the jwt header contains the key id we are looking for
    # to pick the right key used in validation
    unverified_header = jwt.get_unverified_header(token)

    rsa_key = {}
    if 'kid' not in unverified_header:
        print("ZZZZZZZZZZZZZZZZZZZ")
        raise AuthError({
            'code': 'invalid_header',
            'description': 'Authorization malformed.'
        }, 401)
    # Choose our key by Iterate over all keys received
    for key in jwks['keys']:
        if key['kid'] == unverified_header['kid']:
            rsa_key = {
                'kty': key['kty'],
                'kid': key['kid'],
                'use': key['use'],
                'n': key['n'],
                'e': key['e']
            }
    # Finally, verify and decode the payload!!!
    if rsa_key:
        try:
            # Use the Token, Key to validate JWT
            payload = jwt.decode(
                token,
                rsa_key,
                algorithms=ALGORITHMS,
                audience=API_AUDIENCE,
                issuer='https://' + AUTH0_DOMAIN + '/'
            )
            return payload

        # some standard Auth0 exceptions
        except jwt.ExpiredSignatureError:
            raise AuthError({
                'code': 'token_expired',
                'description': 'Token expired.'
            }, 401)

        except jwt.JWTClaimsError:
            raise AuthError({
                'code': 'invalid_claims',
                'description': 'Incorrect claims. Please, check the audience and issuer.'
            }, 401)
        except Exception:
            raise AuthError({
                'code': 'invalid_header',
                'description': 'Unable to parse authentication token.'
            }, 400)
    raise AuthError({
                'code': 'invalid_header',
                'description': 'Unable to find the appropriate key.'
            }, 400)

def requires_auth(permission=''):
    def requires_auth_decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            token = get_token_auth_header()
            try:
                payload = verify_decode_jwt(token)
                check_permissions(permission, payload)
            except:
                abort(403)

            return f(payload, *args, **kwargs)

        return wrapper
    return requires_auth_decorator


import numpy as np
import pandas as pd
from flask import Flask, request, jsonify
import pickle

app = Flask(__name__)

#if it crashes go to root folder "version1"
#then cd classification_and_data
#run "python3 RandomForestClassifier.py"
#copy randomForestModel.pkl to version1/server/
model = pickle.load(open('randomForestModel.pkl','rb'))

@app.route('/api',methods=['POST'])
def predict():
    data = request.get_json(force=True)

    age = data['age']
    budget = data['budget']
    season = 0

    if data['season'] == 'spring':
        season = 0
    elif data['season'] == 'autumn':
        season = 1
    elif data['season'] == 'winter':
        season = 2
    elif data['season'] == 'summer':
        season = 3

    sample = [age, budget, season]
    sampleData = pd.DataFrame([sample], columns=['age', 'budget', 'season'])
    prediction = model.predict(sampleData)
    output = {'city': int(prediction[0])}
    print(prediction)
    return jsonify(output)

if __name__ == '__main__':
    app.run(port=5000, debug=True)


"""Utilities."""

import sys
from typing import Dict, Optional

RGB_RANGE = 0b11111111

def bit_mask(size: int) -> int:
    """Return a bit mask with `size` number of 1-s."""
    if size < 0:
        raise ValueError('{0:d} B TOO SMALL BB'.format(size))
    elif size == 0:
        return 0
    else:
        return int('1' * size, base=2)

def query_user(question: str, default: Optional[bool]=None) -> bool:
    """Ask the user a yes/no question and return the response.

    Seeded from from
    https://stackoverflow.com/questions/3041986/apt-command-line-interface-like-yes-no-input

    Args:
        question: A prompt to provide to the user.
        default: An optional presumed answer, if the user just hits <Enter>.
            It must be "yes" (the default), "no" or None (meaning
            an answer is required of the user).

    Returns:
        True if the user specifies a truthy variant, False if the user specifies
        a falsy variant, or the default value.
    """
    if default is None:
        prompt = '[y/n]'
    elif default is True:
        prompt = '[Y/n]'
    elif default is False:
        prompt = '[y/N]'
    else:
        raise ValueError(f'invalid default answer: "{default}"')

    response_to_bool: Dict[str, bool] = {
        'yes': True,
        'y': True,
        'no': False,
        'n': False
    }
    while True:
        sys.stdout.write(f'{question} {prompt}: ')
        choice = input().lower()
        if default is not None and not choice:
            return default
        elif choice in response_to_bool:
            return response_to_bool[choice]
        else:
            print(
                'Please respond with one of "{0:s}"'.format(
                    '", "'.join(response_to_bool.keys())))


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

img = cv2.imread('flor.jpg')

img_orig = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, otsu_img = cv2.threshold(gray, 127, 255, cv2.THRESH_OTSU)
ret, trunc_img = cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC)
ret, zero_img = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO)

plt.subplot(321),plt.imshow(gray, cmap="gray"),plt.title('Gray')
plt.xticks([]), plt.yticks([])
plt.subplot(322),plt.imshow(otsu_img, cmap="gray"),plt.title('Otsu tresholded')
plt.xticks([]), plt.yticks([])
plt.subplot(323),plt.imshow(trunc_img, cmap="gray"),plt.title('Trunc tresholded')
plt.xticks([]), plt.yticks([])
plt.subplot(324),plt.imshow(zero_img, cmap="gray"),plt.title('To zero tresholded')
plt.xticks([]), plt.yticks([])
plt.subplot(325),plt.imshow(img_orig),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.show()


from django.urls import path
from . import views

urlpatterns = [
    path('description', views.description_list, name="description-list"),
    path('description/<int:id>', views.description_api, name="description-api"),

    path('space_details', views.space_details_list, name="space-details-list"),
    path('space_details/<int:id>', views.space_details_api, name="space-details-api"),

    path('area_details', views.area_details_list, name="area-details-list"),
    path('area_details/<int:id>', views.area_details_api, name="area-details-api"),

    path('charge', views.charge_list, name="charge-list"),
    path('charge/<int:id>', views.charge_api, name="charge-api"),

    path('ratings', views.ratings_list, name="ratings-list"),
    path('ratings/<int:id>', views.ratings_api, name="ratings-api"),

    path('accomodations', views.accomodation_list, name="accomodations-list"),
    path('accomodations/<int:id>', views.accomodation_api, name="accomodations-api"),

    path('accomodation/<int:id>', views.get_accomodation, name="get_accomodation"),

    path('create_reservation', views.create_reservation, name="create_reservation"),
]


from datetime import datetime

def string_to_date(string):
    """
    Convert a string in the format 'YYYY-MM-DD' to a date object.

    Args:
        string (str): A string representing a date in the format 'YYYY-MM-DD'.

    Returns:
        datetime.date or None: A date object if the string is in the correct format,
        None if the string is empty or None.
    """
    date_format = "%Y-%m-%d"
    if string:
        datetime_obj = datetime.strptime(string, date_format)
        return datetime_obj.date()
    else:
        return None


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json

class ScrapyCambadaPipeline(object):

    def open_spider(self, spider):
        self.arquivo = open('frases_historicas.json', 'w')

    def close_spider(self, spider):
        self.arquivo.close()

    def process_item(self, item, spider):
        line = {
            'pk': None,
            'model': 'frases_historicas.FraseHistorica',
            'fields': dict(item)
        }
        line = json.dumps(line)
        self.arquivo.write(line + ',\n')
        return item


"""
DO NOT RUN THIS INSIDE A VIRTUAL ENVIORMENT, THIS WILL BREAK IF YOU DO. I DO NOT KNOW WHY
"""

import time
import os

#For annotation
from typing import List

#For error handling
import traceback

from pynput import keyboard as Keyboard
#Import controllers to simulate keyboard and mouse movements of recorded keys
from pynput.keyboard import Controller as KeyboardController
from pynput.keyboard import Listener as KeyboardListener
from pynput.keyboard import Key
#Import listeners to listen to keyboard/mouse inputs
from pynput.mouse import Button, Controller as MouseController
from pynput.mouse import Listener as MouseListener
from sys import platform

def _get_os_config_directory() -> str:
    #directory where macros and keybins are stored. initialized with illegial characters to stop accidental macros being stored in root...
    set_os_macroconfig_store = "!?[]{]\""

    if platform == "linux" or platform == "linux2":
        set_os_macroconfig_store = os.environ['HOME'] + "/.local/pymacromaker"
        pass
        # linux
    elif platform == "darwin":
        raise Exception("No keybind/macro directory set for this OS X. Fix that then remove this exception")
        pass
        # OS X
    elif platform == "win32":
        raise Exception("No keybind/macro directory set for windows. Fix that then remove this exception.")
        pass
    else:
        raise Exception("Huh, what OS is this? Anyway, no keybind/macro directory set for this OS. ")
        # Windows...
    return set_os_macroconfig_store

place_to_round_to = 2

#print debug stuff if true
debug = False

#Keys which are ignored due to them being used to input other keys(caps lock, shift, etc..), or by merit of them being inconsistent
ignored_keys = ["Key.shift"]

#stirng used to seperate variables in macro storage.
sep_str = " | "

###
#Macro variable name space. Since pointing to in-code variables themselves would cause any pre-existing macro file to break anytime a variable name is changed in new versions, I've instead opted to make a big list of unsorted variables
#that correlate to respective in-code key_clss variables. This way, when the macro is being read from a .txt, the name in code and in the macro file can be indepdendent and or different.

#I.E:
# In code name of mouse position: mouse_pos_xy
# in macro file name of mouse position: mouse_pos

# If I want to change mouse_pos name in code, I can, without breaking macro files made before the variable name change.
###
control_type_variable_name = "control_type"
key_name_variable_name = "key"
hold_duration_variable_name = "hold_duration_s"
press_end_at_epoch_variable_name = "press_end_time_at_epoch"
mouse_pos_variable_name = "mouse_pos"

#For some reason, pynput doesnt have an internal dictionary to convert non alphabetic keys to pynput presses, so I need to make a dictionary to do that..
key_to_object_dict = {
    #non alpha keys
    "Key.enter": Key.enter,
    "Key.ctrl": Key.ctrl,
    "Key.ctrl_r": Key.ctrl_r,
    "Key.space": Key.space,
    "Key.tab": Key.tab,
    "Button.left": Button.left,
    "Button.right": Button.right
}

#Sometimes, when keys are pressed and released, the press event is not caught due to typing too fast/other factors. this is the value the hold duration defaults to if that happens.
default_press_hold_if_press_event_not_triggered = 0.1

#nice website to test keypresses/durations:
#https://www.onlinepianist.com/virtual-piano

class key_presses():
    """ Object keypresses are stored in

    mouse/keyboard use pynput mouse/keyboard control. See: https://www.tutorialspoint.com/how-to-control-your-mouse-and-keyboard-using-the-pynput-library-in-python

    hold_duration_s: how long key is held for in s
    key: the key it self, stored as a string/keyboard/mouse object. Read as a string and do not modify.
    press_end_time_at_epoch: Used for press_stored_key, see there for details.
    """
    def __init__(self):

        self.mouse = MouseController()
        self.keyboard = KeyboardController()
        #how long the key/click was help down for.
        self.hold_duration_s = 0.0

        self.keys= None

        #Mouse pos at time of click/press
        self.mouse_pos_xy = None

        #(If enabled) track when keypress ended at time epoch, so that another key that comes after, it will wait for the duration of time the user waited
        self.press_end_time_at_epoch = 0

        #Control type, weather the key is a mouse key or a keyboard key. determiens how the program should handle simulating inputs
        self._control_type = None

        self.keyboard_listener = KeyboardListener(on_press=self._on_press, on_release=self._on_release)
        self.mouse_listener = MouseListener(on_click=self._on_click)

    def __str__(self):
        #I could probably recycle the variable names here, but then theres an issue of "oh no, I broke everyone's macros because I changed keys to key!"
        return control_type_variable_name + "]" + str(self._control_type) + sep_str + key_name_variable_name + "]" + str(self.keys) + sep_str + hold_duration_variable_name + "]" + str(self.hold_duration_s) + sep_str + press_end_at_epoch_variable_name + "]" + str(self.press_end_time_at_epoch) + sep_str + mouse_pos_variable_name + "]" + str(self.mouse_pos_xy)

    def __repr__(self):
        return control_type_variable_name + "]" + str(self._control_type) + sep_str + key_name_variable_name + "]" + str(self.keys) + sep_str + hold_duration_variable_name + "]" + str(self.hold_duration_s) + sep_str + press_end_at_epoch_variable_name + "]" + str(self.press_end_time_at_epoch) + sep_str + mouse_pos_variable_name + "]" + str(self.mouse_pos_xy)

    def press_stored_key(self, epoch_time_to_wait_before_press=0, hold_duration_override=0):
        """
        press a stored keypress based on how it was pressed when they object that represents its keypressed was stored.

        This method handles the finickyness of pynput keypresses, and also has optional perameters for modifiers on how/when to press keys
         1. epoch_time_to_wait_before_press: Pass a different key's "press_end_time_at_epoch", and the key will wait to press for the duration of time between when the two keys were pressed.
         simulate user input delay
         2. hold_duration_override: override the durration of time the original keypress was held down for, and instead hold it down for the time the override time in seconds. Keep at 0 for no
         override

        NOTE: Currently, "epoch_time_to_wait_before_press" is experimental, and I havent found a way to get the sleep difference between the times to be accurate. there is a small but noticable
        delay, that gets worse the larger you hold the key down for, and im not sure how to fix it.
        """
        if(hold_duration_override != 0):
            self.hold_duration_s = hold_duration_override

        #Prevent accidental pressing of mouse key by releasing the mouse key press
        self.mouse.release(Button.left)
        self.mouse.release(Button.right)

        if(self._control_type == "Mouse"):

            self.mouse.position = self.mouse_pos_xy

            if(epoch_time_to_wait_before_press != 0):
                if(debug == True):
                    print("waiting + " + str(self.press_end_time_at_epoch - epoch_time_to_wait_before_press))
                time.sleep(self.press_end_time_at_epoch - epoch_time_to_wait_before_press)
            #if(debug):
            #    print(type(self.keys))
            self.mouse.press(self.keys)
            time.sleep(self.hold_duration_s)
            self.mouse.release(self.keys)
        elif(self._control_type == "Keyboard"):

            self.mouse.position = self.mouse_pos_xy
            try:
                if(epoch_time_to_wait_before_press != 0):
                    if debug == True:
                        print("waiting + " + str(self.press_end_time_at_epoch - epoch_time_to_wait_before_press))
                        time.sleep(self.press_end_time_at_epoch - epoch_time_to_wait_before_press)

                self.keyboard.press(self.keys)
                time.sleep(self.hold_duration_s)
                self.keyboard.release(self.keys)
            except Exception as e:
                print("ran into error for keyboard press.")
                print("Actual error: " + traceback.format_exc() )
        else:
            raise Exception("Unknown control type: entered control type: " + str(self._control_type))

    #start listening for presses/mouse presses
    def listen_start(self):
        """To start listening for a keypress, call this method.
        Once a keypress/mouse-click is detected, relevant information about when the key was pressed, how long, and where, etc.. is stored inside the Key object used to call this method."""
        self.keyboard_listener.start()
        self.mouse_listener.start()
        self.keyboard_listener.join()
        self.mouse_listener.join()

    #Stop listening for keyboard presses/mouse presses
    def _listen_stop(self):
        self.mouse_listener.stop()
        self.keyboard_listener.stop()

    def _on_press(self, input_key):
        """
        method that is called when a keyboard keypress is detected.
        """
        if(ignored_keys.__contains__(str(input_key))):
            pass
        else:
            #print(str(input_key) + 'key pressed')

            if(str(input_key ) == "Key.shift"):
                print("shifting key")

            if self.hold_duration_s == 0:
                self.hold_duration_s = time.time()

    def _on_release(self, input_key):
        #print(str(input_key) + "key released")

        if(ignored_keys.__contains__(str(input_key))):
            pass
            #print(str(input_key) + " was released, but is classed as a key to ignore. Will not be entered as a part of the macro.")
        else:
            self._control_type, self.keys, self.hold_duration_s, self.press_end_time_at_epoch, self.mouse_pos_xy = "Keyboard", input_key, round(time.time() - self.hold_duration_s, place_to_round_to), time.time(), self.mouse.position

            if(debug):
                print(self._control_type + " pressed, current key is now set to " + str(self))

            #sometimes, if you type keys too fast, the press event for the key is not triggered and its only caught as a release. this sets it to 0.1 seconds by default
            if(self.hold_duration_s > 999999):
                print("press event not triggered, setting keypress duration to " + str(default_press_hold_if_press_event_not_triggered) + " seconds")
                self.hold_duration_s = default_press_hold_if_press_event_not_triggered

            self._listen_stop()

    def _on_click(self, mouse_x, mouse_y, button, click):

        if(click):
            self.captured_time = time.time()
            self.hold_duration_s = self.captured_time

        else:
            self.press_end_time_at_epoch = self.captured_time

            self._control_type, self.keys, self.hold_duration_s, self.press_end_time_at_epoch, self.mouse_pos_xy = "Mouse", button, round(time.time() - self.hold_duration_s, place_to_round_to), time.time(), self.mouse.position
            self._listen_stop()

            if(debug):
                print(self._control_type + " pressed, current key is now set to " + str(self))

def save_macro(name, keys_to_store: List[key_presses]):
    macro_config_directory = _get_os_config_directory() + "/macros/"
    """Save macros in /macros folder in the same directory as input handler."""
    #Store macro same directory as python file is being executed in
    if(os.path.exists(macro_config_directory) == False):
        print("No config folder found, creating a new config folder at: " + macro_config_directory)
        os.makedirs(macro_config_directory)
    with open((macro_config_directory + name + '.txt'), 'w') as filehandle:
        for listitem in keys_to_store:
            #Keycodes encase alphabetical characters in single quotes, I.E, e becomes 'e'. this needs to be removed becasue pynput cant read its own formating.
            #print(listitem)
            listitem = str(listitem).replace("'", "")
            filehandle.write('%s\n' % listitem)

#Construct macro(keypress list) based on saved macro file.
def load_macro(name) -> List[key_presses]:
    macro_config_directory = _get_os_config_directory() + "/macros/"
    """
    Load macros in /macros folder in the same directory as input handler.
    exclude file extension
    """

    #Macro list to construct
    current_macro = []

    #Setup config folder to store macros if no config folder already exists
    if(os.path.exists(macro_config_directory) == False):
        raise Exception("No macro folder found! Create or load some macros into: " + macro_config_directory)
        #print("No config folder found, creating a new config folder at: " + macro_config_directory() + "/macros/")
        #os.makedirs(_get_os_config_directory)
    try:
        with open((macro_config_directory + name + '.txt'), 'r') as filehandle:
            for line in filehandle:
                constructed_key = key_presses()

                #remove \n at begining of readline
                line = line.rstrip('\n')
                unfilt_currentLine = line.split(sep_str)
                #print("unfilt line is " + str(unfilt_currentLine))
                currentLine = [s.split("]") for s in unfilt_currentLine]

                #print("current line is " + str(currentLine))
                #param_name = curr.split("]")[:-1]

                #print(param_name)
                if(debug):
                    print("<<MACRO LOAD>> current read line is: " + str(currentLine))
                #constructed_key.key.key_object = currentLine[1]

                #python doesnt have pointers, so putting things into a big list is variables that cor
                for setting in currentLine:
                    #print("setting is " + str(setting))
                    #check for hcontrol type
                    if(setting[0] == control_type_variable_name):
                        constructed_key._control_type = setting[1]
                        pass
                    #check for key
                    elif(setting[0] == key_name_variable_name):
                        #Correlate non-alphabetical key-codes to their pynput counterpart via dictionary containing them, if that fails, assign to macro list as string(will work for alphabetical characters)
                        try:
                            constructed_key.keys = key_to_object_dict[setting[1]]
                        except:
                            constructed_key.keys = setting[1]

                    #check for hold duration
                    elif(setting[0] == hold_duration_variable_name):
                        constructed_key.hold_duration_s = float(setting[1])

                    #check for time press ended at epoch
                    elif(setting[0] == press_end_at_epoch_variable_name):
                        constructed_key.press_end_time_at_epoch = float(setting[1])

                    #check for mouse_pos
                    elif(setting[0] == mouse_pos_variable_name):
                        #print(setting)
                        #cleanup string to add as mouse pos
                        setting[1] = setting[1].replace("(", "")
                        setting[1] = setting[1].replace(")", "")
                        setting[1] = setting[1].replace(" ", "")

                        setting[1] = setting[1].split(",")
                        #print("mouse setting is " + str(setting[1]))
                        constructed_key.mouse_pos_xy = (int(setting[1][0]), int(setting[1][1]))

                if(debug):
                    print("<<MACRO LOAD>> final constructed key is: " + str(constructed_key))

                current_macro.append(constructed_key)
    except Exception as e:
        print("<<MACRO LOAD>> ran into error constructing macro, investigate.")
        print("<<MACRO LOAD>> Actual error: " + traceback.format_exc())

    #print(current_macro)
    return current_macro


'''

Topic to be covered - Detect Outliers using Elliptic Envelopes

'''

import pandas as pd
import numpy as np

from sklearn.covariance import EllipticEnvelope

dataset = pd.read_csv('Salary.csv')

X = np.array([[100,100],
              [1,1],
              [2,4],
              [5,6],
              [6,8]])

outlier = EllipticEnvelope(contamination=0.1)

outlier.fit(X)
prediction1 = outlier.predict(X)
print(prediction1)

#--------------------------------------#

features = dataset.iloc[:,[1,2]].values

outlier1 = EllipticEnvelope(contamination=0.1)

outlier1.fit(features)
prediction2 = outlier1.predict(features)

print(prediction2)

dataset['outliers'] = prediction2


import csv
import os
from math import ceil

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from sklearn.cluster import KMeans
from tqdm import tqdm

class ColorGenerator:
    def __init__(self, path: str, csv_path: str):
        self.path = path
        self.csv_path = csv_path
        self.headers = [
            'file_name',
            'color_1',
            'color_2',
            'color_3',
            'color_4',
            'color_5',
            'color_6',
        ]

    def _get_kmeans_centers(self, img, nclusters: int):
        return KMeans(n_clusters=nclusters).fit(img).cluster_centers_

    def _get_image(self, path: str):
        with open(path, "rb") as f:
            return np.array(Image.open(f))

    def _preprocess_image(self, image):
        return image.reshape((-1, 3)).astype("float32") / 255

    def _plot_palette(self, centers: np.ndarray) -> None:
        plt.figure(figsize=(14, 6))
        plt.imshow(centers[
            np.concatenate(
                [[i] * 100 for i in range(len(centers))]).reshape((-1, 10)).T
        ])
        plt.grid()
        plt.axis('off')
        plt.show()

    def generate_colors(self, nclusters: int, show_palette: bool) -> None:
        dataset = os.listdir(self.path)
        file = open(self.csv_path, 'w', encoding='UTF8', newline='')
        writer = csv.writer(file)
        writer.writerow(self.headers)

        for image in tqdm(dataset):
            entry = [image]
            img = self._get_image(os.path.join(self.path, image))
            rgb_pixels = self._preprocess_image(img)
            centers = self._get_kmeans_centers(rgb_pixels, nclusters)
            entry.extend(
                [
                    ';'.join(map(lambda a: str(ceil(a * 255)), i))
                    for i in centers
                ]
            )
            if show_palette:
                self._plot_palette(centers)

            writer.writerow(entry)

gen = ColorGenerator(path='dataset', csv_path='data_test.csv')
gen.generate_colors(6, False)


from django.shortcuts import render, redirect
from fish_bowl_monitor.models import TankTemp
from rest_framework.views import APIView
from rest_framework.response import Response

def fish_home_chart(request):
    check_db = TankTemp.objects.latest('date_time_stamp')
    oldest = TankTemp.objects.earliest('date_time_stamp')
    return render(request, 'chart.html', {
        'current_tank_temp': check_db.temperature_data,
        'earliest_tank_temp': oldest.temperature_data,
        'earliest_date': oldest.date_time_stamp,
    })

def fish_home(request):
    check_db = TankTemp.objects.latest('date_time_stamp')
    oldest = TankTemp.objects.earliest('date_time_stamp')
    return render(request, 'home.html', {
        'current_tank_temp': check_db.temperature_data,
        'earliest_tank_temp': oldest.temperature_data,
        'earliest_date': oldest.date_time_stamp,
        }
    )

def add_records():
    for idx in range(0, 2000):
        print("hello?!?!")
        TankTemp.objects.create(temperature_data=0, weather_data_temp=0)

def set_up_db(request):
    add_records()
    # return redirect(f'/')

class ChartData(APIView):
    authentication_classes = []
    permission_classes = []

    def get(self, reqest):
        # Petty sure this can get cleaned up, but I need to get some reps in before it's clear how
        total = TankTemp.objects.all().count()
        qset = TankTemp.objects.filter().order_by('date_time_stamp').values_list('date_time_stamp', 'temperature_data', 'weather_data_temp')[total - 1440:total:60]
        labels = []
        default_data = []
        outside_temp = []
        for stuff in qset:
            default_data.append(stuff[1])
            dtg = stuff[0].strftime("%b %d %Y %H:%m")
            labels.append(dtg)
            outside_temp.append(stuff[2])

        data = {
            'labels': labels,
            'default_data': default_data,
            'outside_temp': outside_temp,
        }
        return Response(data)


from evaluation.metrics import evaluate
from models.predictor import predict
from utils.argcheck import check_float_positive, check_int_positive
from utils.io import load_numpy
from utils.modelnames import models
from utils.progress import WorkSplitter, inhour

import argparse
import numpy as np
import time

def main(args):
    # Progress bar
    progress = WorkSplitter()

    # Show hyper parameter settings
    progress.section("Parameter Setting")
    print("Data Path: {}".format(args.path))
    print("Train File Name: {}".format(args.train))
    if args.validation:
        print("Valid File Name: {}".format(args.valid))
    print("Algorithm: {}".format(args.model))
    print("Rank: {}".format(args.rank))
    print("Lambda: {}".format(args.lamb))
    print("Trainig Epoch: {}".format(args.epoch))
    print("Evaluation Ranking Topk: {}".format(args.topk))

    # Load Data
    progress.section("Loading Data")
    start_time = time.time()
    R_train = load_numpy(path=args.path, name=args.train)
    print("Elapsed: {}".format(inhour(time.time() - start_time)))
    print("Train U-I Dimensions: {}".format(R_train.shape))

    progress.section("Train")
    RQ, Yt, Bias = models[args.model](R_train, epoch=args.epoch, lamb=args.lamb,
                                      rank=args.rank, corruption=args.corruption)
    Y = Yt.T

    progress.section("Predict")
    prediction = predict(matrix_U=RQ,
                         matrix_V=Y,
                         bias=Bias,
                         topK=args.topk,
                         matrix_Train=R_train,
                         gpu=args.gpu)

    if args.validation:
        progress.section("Create Metrics")
        start_time = time.time()

        metric_names = ['R-Precision', 'NDCG', 'Clicks', 'Recall', 'Precision', 'MAP']

        R_valid = load_numpy(path=args.path, name=args.valid)
        result = evaluate(prediction, R_valid, metric_names, [args.topk])
        print("-")
        for metric in result.keys():
            print("{}:{}".format(metric, result[metric]))
        print("Elapsed: {}".format(inhour(time.time() - start_time)))

if __name__ == "__main__":
    # Commandline arguments
    parser = argparse.ArgumentParser(description="Main")

    parser.add_argument('--corruption', dest='corruption', type=check_float_positive, default=0.5)
    parser.add_argument('--disable_validation', dest='validation', action='store_false')
    parser.add_argument('--epoch', dest='epoch', type=check_int_positive, default=1)
    parser.add_argument('--gpu', dest='gpu', action='store_true')
    parser.add_argument('--lamb', dest='lamb', type=check_float_positive, default=100)
    parser.add_argument('--model', dest='model', default="VAE-CF")
    parser.add_argument('--path', dest='path', default="data/yelp/")
    parser.add_argument('--rank', dest='rank', type=check_int_positive, default=100)
    parser.add_argument('--topk', dest='topk', type=check_int_positive, default=50)
    parser.add_argument('--train', dest='train', default='Rtrain.npz')
    parser.add_argument('--valid', dest='valid', default='Rvalid.npz')
    args = parser.parse_args()

    main(args)


# Given the roots of two binary trees p and q, write a function to check if they are the same or not.
# Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

import math
from typing import Optional

class TreeNode:
    def __init__(self, val:int=None, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

# signigicant Runtime and Memory performance
class Solution:
    def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
        if not p and not q:
            return True
        if not p or not q:
            return False
        if p.val == q.val:
            return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
        return False

# class Solution:
#     def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
#         if not p and not q:
#             return True
#         if not p or not q:
#             return False

#         lf, ln = [p], []
#         rt, rn = [q], []
#         while lf and rt:
#             if len(lf)!=len(rt):
#                 return False

#             for i in range(len(lf)):
#                 if lf[i].val != rt[i].val:
#                     return False

#                 if lf[i].left and rt[i].left:
#                     ln.append(lf[i].left)
#                     rn.append(rt[i].left)
#                 elif not lf[i].left and not rt[i].left:
#                     pass
#                 else:
#                     return False

#                 if lf[i].right and rt[i].right:
#                     ln.append(lf[i].right)
#                     rn.append(rt[i].right)
#                 elif not lf[i].right and not rt[i].right:
#                     pass
#                 else:
#                     return False
#             lf, ln = ln, []
#             rt, rn = rn, []

#         if lf or rt:
#             return False
#         else:
#             return True

s = Solution()
print(s.isSameTree(
    TreeNode(1, TreeNode(2), TreeNode(3)),
    TreeNode(1, TreeNode(2), TreeNode(3))
    ))
print(s.isSameTree(
    TreeNode(1, TreeNode(2)),
    TreeNode(1, None, TreeNode(2))
    ))
print(s.isSameTree(
    TreeNode(1, TreeNode(2), TreeNode(1)),
    TreeNode(1, TreeNode(1), TreeNode(2))
    ))
print(s.isSameTree(TreeNode(), TreeNode()))


""" PyXLL extension loader.

Loads all the extensions contributing to the pyxll.modules namespace. This
module is supposed to be added to you pyxll.cfg file to automatically load all
the installed packages that contributes to PyXLL through setuptools.

"""
import errno
import logging
import os

from stevedore import extension

from pyxll import get_config
from .ribbon_synthesizer import RibbonSynthesizer

logger = logging.getLogger(__name__)

# Keyword args to pass to plugin initializers
invoke_kwds = {}

config = dict(get_config().items('PYXLL'))
default_ribbon_path = config.get('default_ribbon', '')
ribbon_path = config.get('ribbon', '')

should_make_ribbon = (default_ribbon_path and ribbon_path and
                      default_ribbon_path != ribbon_path)
ribbon_synthesizer = RibbonSynthesizer.from_file(default_ribbon_path)

if should_make_ribbon:
    invoke_kwds['submit_ribbon_tab'] = ribbon_synthesizer.submit_ribbon_tab
else:
    logger.info("The ribbon will not be modified because your config does"
                " not define both PYXLL::ribbon and PYXLL::default_ribbon")

extension_manager = extension.ExtensionManager(
    namespace='pyxll.extensions',
    invoke_on_load=True,
    invoke_kwds=invoke_kwds,
)

extension_names = extension_manager.names()

if len(extension_names) > 0:
    logger.info(
        'Extensions loaded:\n{}'.format('\n'.join(extension_names))
    )
else:
    logger.info('No extension loaded')

if should_make_ribbon:
    target_dir = os.path.dirname(ribbon_path)
    try:
        # If the ribbon dirs don't exist, make them.
        os.makedirs(target_dir)
        logger.info("Created ribbon directory: {}".format(target_dir))
    except Exception as e:
        if not e.errno == errno.EEXIST:
            raise
    with open(ribbon_path, 'wb') as f:
        f.write(ribbon_synthesizer.to_bytes())
        logger.info("Wrote extended ribbon to {}".format(ribbon_path))


import requests
from bs4 import BeautifulSoup
import json

def get_citations_needed_count(url):
    """
    Retrieves a Wikipedia page and counts the number of citations needed.

    Args:
        url (str): The URL of the Wikipedia page.

    Returns:
        int: The number of citations needed.

    """
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')
    citation_needed_tags = soup.find_all('sup' , class_='noprint Inline-Template Template-Fact')
    return len(citation_needed_tags)

def get_citations_needed_report(url):
    """
    Retrieves a Wikipedia page and generates a report of passages that need citations.

    Args:
        url (str): The URL of the Wikipedia page.

    Returns:
        list: A list of text statements for each citation.

    """
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')
    citation_needed_tags = soup.find_all('sup' , class_='noprint Inline-Template Template-Fact')
    statements = " "

    for citation in citation_needed_tags:
        statement = citation .find_previous('p')
        # statements.append(statement.text)
        statements += statement.text

    return statements

url = 'https://en.wikipedia.org/wiki/History_of_Mexico'
count = get_citations_needed_count(url)
statements = get_citations_needed_report(url)

print(f"Number of citations needed: {count}")
print("Citations needed report:")
print(statements)

# report = {
#     "number_of_citations_needed": count,
#     "citations": statements
# }

# with open('report.json', 'w') as file:
#     json.dump(report, file)

# print(json.dumps(report, indent=4))


import numpy as np
import pygame
from OpenGL.GL import *
from pygame.locals import *

from lab0809.utils import project_with_angle

class MyOpenGL:
    def __init__(self, width, height):
        self._width = width
        self._height = height

        self._clock = pygame.time.Clock()

        self._init()

        self.objects = []

    def _init(self):
        pygame.init()
        pygame.display.set_mode((self._width, self._height), DOUBLEBUF | OPENGL)

        glOrtho(0, self._width, 0, self._height, 2000, -2000)
        glTranslatef(self._width / 2, self._height / 2, 0)

        glEnable(GL_DEPTH_TEST)
        glEnable(GL_LIGHTING)
        glEnable(GL_BLEND)
        glEnable(GL_NORMALIZE)
        glShadeModel(GL_SMOOTH)

        glEnable(GL_COLOR_MATERIAL)
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)

        glEnable(GL_LIGHT0)
        glLightfv(GL_LIGHT0, GL_AMBIENT, [0.2, 0.2, 0.2, 1])
        glLightfv(GL_LIGHT0, GL_DIFFUSE, [4, 4, 4, 1])
        glLightfv(GL_LIGHT0, GL_POSITION, [0, 1, 0, 0])

    @staticmethod
    def draw(objects):
        rotations = [0 for _ in objects]
        # light_angle = [0, 0, 1]
        glRotatef(-40, 1, .5, 0)

        while True:
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)

            shadow_casters = []
            draw_bottom_plane()

            # light_angle = project_with_angle(light_angle, 0.1, 0.05, 0.025)
            #
            # glLightfv(GL_LIGHT0, GL_POSITION, light_angle + [0])

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

            for tor_i, tor in enumerate(objects):
                glPushMatrix()
                rotations[tor_i] += (tor_i + 1) * 1.5

                glRotatef(rotations[tor_i], 0, 1, 0)
                glRotatef(rotations[tor_i], 0, 0, 1)
                for face in tor.draw_gl():
                    rotation = np.radians(rotations[tor_i])
                    shadow_casters.append(
                        [project_with_angle(v, theta_x=0, theta_y=-rotation, theta_z=-rotation) for v in face]
                    # [rotate(v, theta_x=rotation) for v in face]
                    # [rotate(v, theta_y=-rotation) for v in face]
                    # [rotate(v, theta_z=-rotation) for v in face]
                    )

                glPopMatrix()

            glBegin(GL_TRIANGLES)
            for shadow_caster in shadow_casters:
                glColor3fv((0, 0, 0))
                for (x, y, z) in shadow_caster:
                    glVertex3fv((x, -349.5, z))
            glEnd()

            pygame.display.flip()
            pygame.time.wait(10)

def draw_bottom_plane():
    bottom_y = -350
    size = 1200

    glColor3fv((.9, .3, .1))
    v1, v2, v3, v4 = \
        (-size / 2, bottom_y, -size / 2), \
        (-size / 2, bottom_y, size / 2), \
        (size / 2, bottom_y, size / 2), \
        (size / 2, bottom_y, -size / 2)

    glBegin(GL_QUADS)
    for v in (v1, v2, v3, v4):
        glVertex3fv(v)
    glEnd()

    return [0, 0, 0, bottom_y]


import subprocess
import os
from zipfile import ZipFile, error
from colorama import Fore
from tqdm import tqdm

def load_data():

    filepath = 'inputs/cell-images-for-detecting-malaria.zip'
    output = 'inputs/'
    if 'cell-images-for-detecting-malaria.zip' not in os.listdir('inputs'):
        print(f'{Fore.YELLOW}Downloading the dataset from Kaggle...')
        subprocess.run(f'kaggle datasets download -d iarunava/cell-images-for-detecting-malaria -p {output}',
                       stdout=subprocess.PIPE,
                       shell=True)
        print(f'{Fore.YELLOW}Download done.')

    with ZipFile(filepath, 'r') as zipObj:
        try:
            for i in tqdm(zipObj.infolist(), desc=f'{Fore.YELLOW}Extracting:'):
                zipObj.extract(i, output)
            print(f'{Fore.GREEN}Files successfully extracted.')
            return True
        except error as e:
            print(f'{Fore.RED}Cannot extract the files:\n{e}')


from PIL import Image, ImageFont, ImageDraw
text = u
font = ImageFont.truetype("msyh.ttf",18)
lines = []
line =''
for word in text.split():
    print(word)
    if font.getsize(line+word)[0] >= 300:
        lines.append(line)
        line = u''
        line += word
        print('size=',font.getsize(line+word)[0])
    else:
        line = line + word
line_height = font.getsize(text)[1]
img_height = line_height*(len(lines)+1)
print('len=',len(lines))
print('lines=',lines)


"""Simple built-in debugger. Runs pdb on reception of SIGUSR1 signal."""
import signal

__all__ = ()

def handle_sigusr1(sig, frame):
    print("\nSIGUSR1 signal received. Starting interactive debugger...", flush=True)
    import pdb

    pdb.Pdb().set_trace(frame)

signal.signal(signal.SIGUSR1, handle_sigusr1)


import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    maxDiff = None
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_Sample_Input_1(self):
        input = """AtCoder"""
        output = """Yes"""
        self.assertIO(input, output)

    def test_Sample_Input_2(self):
        input = """Aa"""
        output = """Yes"""
        self.assertIO(input, output)

    def test_Sample_Input_3(self):
        input = """atcoder"""
        output = """No"""
        self.assertIO(input, output)

    def test_Sample_Input_4(self):
        input = """Perfect"""
        output = """No"""
        self.assertIO(input, output)

def resolve():
  alpha2num = lambda c: ord(c) - ord('a')

  from collections import defaultdict

  inf = 10**18+1
  S = list(input())
  N = len(S)
  big = 0
  small = 0
  count = defaultdict(int)
  for i in range(N):
    count[S[i]] += 1
    if count[S[i]] > 1:
      print("No")
      return
    if S[i] <= "Z":
      big += 1
    else:
      small += 1

  print("Yes" if big >= 1 and small >= 1 else "No")

import sys
if sys.argv[-1] == './Main.py':
  resolve()

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


import argparse
import os
import unittest

from infra.tools.new_tool import new_tool
import infra_libs

DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')

class TestArgParseOptions(unittest.TestCase):
  def test_add_argparse_options_smoke(self):
    parser = argparse.ArgumentParser()
    new_tool.add_argparse_options(parser)

class TestGenerateToolFiles(unittest.TestCase):
  def test_generate(self):
    with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir:
      toolname = 'test_tool'
      new_tool.generate_tool_files('test_tool', tempdir)
      self.assertTrue(os.path.isdir(os.path.join(tempdir, toolname)))
      self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname,
                                                  '__init__.py')))
      self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname,
                                                  '__main__.py')))
      self.assertTrue(os.path.isdir(os.path.join(tempdir, toolname, 'test')))
      self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname, 'test',
                                                  '__init__.py')))

  def test_no_overwrite_tool(self):
    with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir:
      self.assertFalse(new_tool.generate_tool_files('test_tool', tempdir))
      self.assertTrue(new_tool.generate_tool_files('test_tool', tempdir))

  def test_missing_destination_dir(self):
    with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir:
      # When destination directory does not exist, just do nothing and return
      # a non-zero value.
      self.assertTrue(new_tool.generate_tool_files(
        'test_tool', os.path.join(tempdir, 'missing-directory')))

class TestGeneratePythonFile(unittest.TestCase):
  def test_generate_empty_file(self):
    with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir:
      filename = new_tool.generate_python_file(tempdir, 'test.py', None)
      self.assertTrue(os.path.isfile(filename))
      # Read the content and do some basic check
      with open(filename, 'r') as f:
        content = f.read()
      self.assertTrue(content.startswith(new_tool.COPYRIGHT_NOTICE))

      # Now make sure the file is not overwritten
      new_content = 'other content'
      with open(filename, 'w') as f:
        f.write(new_content)
      filename2 = new_tool.generate_python_file(tempdir, 'test.py', None)
      self.assertEqual(filename, filename2)
      with open(filename, 'r') as f:
        content = f.read()
      self.assertEqual(content, new_content)

  def test_generate_file_from_template(self):
    with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir:
      filename = new_tool.generate_python_file(
        tempdir, 'test.py', 'test_template', template_dir=DATA_DIR,
        value='Passed string.')
      self.assertTrue(os.path.isfile(filename))

      # Read the content
      with open(filename, 'r') as f:
        content = f.read()

      expected_content = (new_tool.COPYRIGHT_NOTICE
                          + 'This is a template used by the test suite.\n'
                          'Passed string.\n')
      self.assertEqual(content, expected_content)


""" Module containing DocumentPublisher class """
from ..common.common import SectionHandler

# pylint: disable=too-few-public-methods
class DocumentPublisher(SectionHandler):
    """ Responsible for converting the DocumentPublisher section:
      - /cvrf:cvrfdoc/cvrf:DocumentPublisher
     """
    type_category_mapping = {
        'Vendor': 'vendor',
        'Coordinator': 'coordinator',
        'User': 'user',
        'Discoverer': 'discoverer',
        'Other': 'other',
    }

    def __init__(self, config):
        super().__init__()
        self.name = config.get('publisher_name')
        self.namespace = config.get('publisher_namespace')

    def _process_mandatory_elements(self, root_element):
        self.csaf['name'] = self.name
        self.csaf['namespace'] = self.namespace
        self.csaf['category'] = self.type_category_mapping[root_element.attrib['Type']]

    def _process_optional_elements(self, root_element):
        # optional values
        if hasattr(root_element, 'ContactDetails'):
            self.csaf['contact_details'] = root_element.ContactDetails.text
        if hasattr(root_element, 'IssuingAuthority'):
            self.csaf['issuing_authority'] = root_element.IssuingAuthority.text


from __future__ import annotations

from typing import Any, Dict, List, Union

import attr

from binarylane.types import UNSET, Unset

@attr.s(auto_attribs=True)
class Size:
    slug: str
    size_type: str
    available: bool
    regions: List[str]
    price_monthly: float
    price_hourly: float
    disk: int
    memory: int
    transfer: float
    excess_transfer_cost_per_gigabyte: float
    vcpus: int
    vcpu_units: str
    options: Dict[str, str]
    description: Union[Unset, None, str] = UNSET
    cpu_description: Union[Unset, None, str] = UNSET
    storage_description: Union[Unset, None, str] = UNSET

    def to_dict(self) -> Dict[str, Any]:
        slug = self.slug
        size_type = self.size_type

        available = self.available
        regions = self.regions

        price_monthly = self.price_monthly
        price_hourly = self.price_hourly
        disk = self.disk
        memory = self.memory
        transfer = self.transfer
        excess_transfer_cost_per_gigabyte = self.excess_transfer_cost_per_gigabyte
        vcpus = self.vcpus
        vcpu_units = self.vcpu_units
        options = self.options

        description = self.description
        cpu_description = self.cpu_description
        storage_description = self.storage_description

        field_dict: Dict[str, Any] = {}
        field_dict.update(
            {
                "slug": slug,
                "size_type": size_type,
                "available": available,
                "regions": regions,
                "price_monthly": price_monthly,
                "price_hourly": price_hourly,
                "disk": disk,
                "memory": memory,
                "transfer": transfer,
                "excess_transfer_cost_per_gigabyte": excess_transfer_cost_per_gigabyte,
                "vcpus": vcpus,
                "vcpu_units": vcpu_units,
                "options": options,
            }
        )
        if description is not UNSET:
            field_dict["description"] = description
        if cpu_description is not UNSET:
            field_dict["cpu_description"] = cpu_description
        if storage_description is not UNSET:
            field_dict["storage_description"] = storage_description

        return field_dict


import requests

import csv
import datetime
import json
import os

class SlackAgentClient(object):
    """
    Class to post messages to slack
    """
    def webhook_post(self, district, message):
        url = os.getenv('SLACK_WEBHOOK_URL')
        text = f"*[Vaccine Slot Alert][{district}]*\n```{message}```"
        payload = {
            "text": text
        }
        requests.request("POST", url, data=json.dumps(payload), headers={}, params={})

class VaccineSlotFinder:

    AGE_LIMIT = 18
    DATE_FORMAT = '%d-%m-%Y'

    PIN_WHITELIST = ['245304']

    DISTRICT_MAP = json.loads(os.getenv('DISTRICT_MAP'))

    DAY_COUNT = 90

    def __init__(self):
        self.base_url = "https://cdn-api.co-vin.in/api/v2"
        self.found = False

    def _process_response(self, response, string):
        center_found = False
        for center in response["centers"]:
            sessions = center["sessions"]
            for session in sessions:
                if session["min_age_limit"] == self.AGE_LIMIT and session["available_capacity"] > 0:
                    center_found = True
                    self.found = True

                    timings = ", ".join(session["slots"])
                    message = f"""
                    Date: {session["date"]}
                    No. of Slots: {session["available_capacity"]}
                    Timings: {timings}
                    Vaccine: {session["vaccine"]}
                    Center Name:    {center["name"]}
                    Block:  {center["block_name"]}
                    District: {center["district_name"]}
                    Charges: {center["fee_type"]}
                    Pincode: {center["pincode"]}
                    """
                    SlackAgentClient().webhook_post(center["district_name"], message)

        if not center_found:
            print("Not found")
            # SlackAgentClient().webhook_post(f"""Not found for {string}""")

    def controller(self):
        for district in self.DISTRICT_MAP.keys():
            date = None
            for week in range(int(self.DAY_COUNT/7)):
                date = self._get_next_date(date)
                print(f"Checking for district {self.DISTRICT_MAP[district]} on date {date}")
                self.find_by_district(district, date)

            # if not self.found:
            #     SlackAgentClient().webhook_post(self.DISTRICT_MAP[district], "Slots not available")

    def find_by_pincode(self, pincode, date):
        path = "/appointment/sessions/public/calendarByPin"
        url = self.base_url + path
        querystring = {"pincode": pincode, "date": date}

        payload = ""

        response = requests.request("GET", url, data=payload, headers={}, params=querystring)

        response = response.json()

        self._process_response(response, f"{pincode} on {date}")

    def find_by_district(self, district, date):
        path = "/appointment/sessions/public/calendarByDistrict"
        url = self.base_url + path
        querystring = {"district_id": district, "date": date}

        payload = ""

        response = requests.request("GET", url, data=payload, headers={}, params=querystring)

        response = response.json()

        self._process_response(response, f"{self.DISTRICT_MAP[district]} on {date}")

    def print_states_csv(self):
        path= "/admin/location/states"
        url = self.base_url + path

        payload = ""
        response = requests.request("GET", url, data=payload, headers={}, params={})

        response = response.json()
        with open('districts.csv', 'w', newline='') as file:
            writer = csv.writer(file)
            writer.writerow(["State Name", "State ID", "District Name", "District ID"])

        for res in response["states"]:
            # print(f"""{res["state_name"]},{res["state_id"]}""")
            self._print_districts_csv(res["state_id"], res["state_name"])

    def _print_districts_csv(self, state_id, state_name):
        path = f"/admin/location/districts/{state_id}"
        url = self.base_url + path

        payload = ""
        response = requests.request("GET", url, data=payload, headers={}, params={})

        response = response.json()
        with open('districts.csv', 'a', newline='') as file:
            writer = csv.writer(file)
            for res in response["districts"]:
                # print(f"""{state_name},{state_id},{res["district_name"]},{res["district_id"]}""")
                writer.writerow([state_name, state_id, res["district_name"], res["district_id"]])

    def _get_next_date(self, cur_date=None):
        if cur_date:
            date = datetime.datetime.strptime(cur_date, self.DATE_FORMAT)
            next = date + datetime.timedelta(days=7)
            return next.strftime(self.DATE_FORMAT)

        # Returns default date as today in IST
        return (datetime.datetime.now()+ datetime.timedelta(hours=5,minutes=30)).strftime(self.DATE_FORMAT)

# AWS Lambda invoker
def lambda_handler(event, context):
    VaccineSlotFinder().controller()

# For local testing
if __name__ == "__main__":
    print("here")
    VaccineSlotFinder().controller()


import psycopg2
from psycopg2.extras import DictCursor

import utils

def add_user(steamid:str, name: str):
    try:
        dbConn = psycopg2.connect(utils.utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        cursor.execute(f"INSERT INTO profiles (steamid, name) SELECT '{steamid}', '{name}' WHERE NOT EXISTS(SELECT * FROM profiles WHERE steamid=('{steamid}'));")
        print(f"added user {name} with steamid {steamid}")
    finally:
        dbConn.commit()
        cursor.close()
        dbConn.close()

def get_users():
    try:
        dbConn = psycopg2.connect(utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        cursor.execute("SELECT * FROM profiles;")
        return cursor.fetchall()
    finally:
        cursor.close()
        dbConn.close()

def add_item(name: str, type: str):
    try:
        dbConn = psycopg2.connect(utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        cursor.execute(f"""INSERT INTO items (name, type) SELECT '{name}', '{type}' WHERE NOT EXISTS(SELECT * FROM items WHERE name=('{name}'));""")
        print(f"added item {name} with type {type}")
    finally:
        dbConn.commit()
        cursor.close()
        dbConn.close()

def add_item_price(item_name: str, price: int, date: str):
    try:
        dbConn = psycopg2.connect(utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        cursor.execute(f"INSERT INTO item_prices (item, price, date) SELECT '{item_name}', '{price}', '{date}';")
        print(f"added price {price} to item {item_name}")
    finally:
        dbConn.commit()
        cursor.close()
        dbConn.close()

def add_to_inventory(steamid: str, item_name: str, quantity: int):
    try:
        dbConn = psycopg2.connect(utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        cursor.execute(f"INSERT INTO profile_items (profile, item, quantity) VALUES ('{steamid}', '{item_name}','{quantity}');")
        print(f"added item {item_name} to inventory of {steamid}")
    finally:
        dbConn.commit()
        cursor.close()
        dbConn.close()

def set_inventory(steamid: str, inventory: dict):
    try:
        dbConn = psycopg2.connect(utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        clear_inventory(steamid)
        for item_name in inventory:
            add_item(item_name, inventory[item_name]["type"])
            add_to_inventory(steamid, item_name, inventory[item_name]["quantity"])
    finally:
        dbConn.commit()
        cursor.close()
        dbConn.close()

def clear_inventory(steamid: str):
    try:
        dbConn = psycopg2.connect(utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        cursor.execute(f"DELETE FROM profile_items WHERE profile = '{steamid}';")
        print(f"cleared inventory of {steamid}")
    finally:
        dbConn.commit()
        cursor.close()
        dbConn.close()

def get_inventory(steamid: str):
    '''[profile, item_name, quantity, price, type, date]'''
    try:
        dbConn = psycopg2.connect(utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        querry = f"""
            SELECT ip.item, quantity, price
            FROM item_prices ip
            JOIN (
            SELECT item, MAX(date) AS max_date
            FROM item_prices
            GROUP BY item
            ) latest ON ip.item = latest.item AND ip.date = latest.max_date
            JOIN profile_items ON profile_items.item = ip.item
            WHERE profile = '{steamid}';
            """
        # cursor.execute(f"SELECT profile, profile_items.item, quantity, price, type, date FROM profile_items JOIN items on item = items.name join item_price on profile_items.item=item_price.item WHERE profile = '{steamid}' order by date;")
        cursor.execute(querry)
        return cursor.fetchall()
    finally:
        cursor.close()
        dbConn.close()

def get_item_list():
    try:
        dbConn = psycopg2.connect(utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        cursor.execute(f"""
        SELECT items.name, sum(profile_items.quantity) as qnts FROM items
        JOIN profile_items ON profile_items.item = items.name
        GROUP BY items.name
        order by qnts desc
        ;
        """)
        return cursor.fetchall()
    finally:
        cursor.close()
        dbConn.close()

def get_latest_prices():
    try:
        dbConn = psycopg2.connect(utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        cursor.execute(f"""
            SELECT price, sum(profile_items.quantity) AS qnts, this.item FROM profile_items
            JOIN (
                SELECT ip.item, ip.date, ip.price
                FROM item_prices ip
                JOIN (
                SELECT item, MAX(date) AS max_date
                FROM item_prices
                GROUP BY item
                ) latest ON ip.item = latest.item AND ip.date = latest.max_date
                ORDER BY price DESC
            ) this ON this.item = profile_items.item
            GROUP BY this.item, price
            ORDER BY qnts DESC
            ;
            """)
        return cursor.fetchall()
    finally:
        cursor.close()
        dbConn.close()

def display():
    try:
        dbConn = psycopg2.connect(utils.DB_CONNECTION_STRING)
        cursor = dbConn.cursor(cursor_factory=DictCursor)
        cursor.execute("""
            SELECT profiles.name, profile_items.item, profile_items.quantity, item_prices.date, item_prices.price FROM profiles
            JOIN profile_items ON profiles.steamid = profile_items.profile
            JOIN item_prices ON profile_items.item = item_prices.item
            ORDER BY item_prices.date DESC
        """)
        return cursor.fetchall()
    finally:
        cursor.close()
        dbConn.close()


from fastapi import APIRouter
from tasks import start_img_preprocess
from celery.result import AsyncResult
from .model import ImageBase64

router = APIRouter(
    prefix="/api/v1/img_prep",
    tags=["img_prep"],
)

@router.get("/status/{task_id}")
async def get_status(task_id):
    task_result = AsyncResult(task_id)
    return { "task_status": task_result.status }

@router.get("/img/{task_id}")
async def get_img(task_id):
    task_result = AsyncResult(task_id)
    return { "img": task_result.result }

@router.post("/img")
async def create_item(img: ImageBase64):
    task = start_img_preprocess.delay(img.img_body_base64)
    return { "task_id": task.id }


import sqlite3

class BotDB:
    def __init__(self, db_file: str):
        self.conn = sqlite3.connect(db_file)
        self.cursor = self.conn.cursor()

        self.cursor.execute('''CREATE TABLE IF NOT EXISTS "categories" (
	"id"	INTEGER NOT NULL UNIQUE,
	"name"	TEXT NOT NULL,
	PRIMARY KEY("id" AUTOINCREMENT)
);''')
        self.cursor.execute('''CREATE TABLE IF NOT EXISTS "products" (
	"id"	INTEGER NOT NULL UNIQUE,
	"name"	TEXT NOT NULL,
	"category_id"	TEXT NOT NULL,
	"description"	TEXT,
	"price"	REAL NOT NULL,
	"image_path"	TEXT,
	PRIMARY KEY("id" AUTOINCREMENT),
	FOREIGN KEY("category_id") REFERENCES "categories"("id")
);''')
        self.cursor.execute('''CREATE TABLE IF NOT EXISTS "orders" (
	"id"	INTEGER NOT NULL UNIQUE,
	"user_id"	INTEGER NOT NULL,
	"product_id"	INTEGER NOT NULL,
	"order_date"	TEXT NOT NULL,
	"delivery_date"	TEXT NOT NULL,
	PRIMARY KEY("id" AUTOINCREMENT),
	FOREIGN KEY("user_id") REFERENCES "users"("id"),
	FOREIGN KEY("product_id") REFERENCES "products"("id")
);''')
        self.cursor.execute('''CREATE TABLE IF NOT EXISTS "users" (
	"id"	INTEGER NOT NULL UNIQUE,
	"user_telegram_id"	INTEGER NOT NULL,
	"nickname"	TEXT NOT NULL,
	"phone_number"	INTEGER NOT NULL,
	PRIMARY KEY("id" AUTOINCREMENT)
);''')

    def user_exists(self, user_id: int):
        result = self.cursor.execute("SELECT id FROM users WHERE user_telegram_id = ?", (user_id,))
        return bool(len(result.fetchall()))

    def add_user(self, user_id: int, nickname: str, phone_number: int):
        self.cursor.execute("INSERT INTO users (user_telegram_id, nickname, phone_number) VALUES(?, ?, ?)", (user_id, nickname, phone_number))
        return self.conn.commit()

    def get_categories(self):
        result = self.cursor.execute("SELECT name FROM categories").fetchall()
        lst = []
        for i in result:
            lst.append(i[0])
        return lst

    def get_category_id(self, category: str):
        result = self.cursor.execute("SELECT id FROM categories WHERE name = ?", (category,)).fetchall()
        return result[0]

    def get_products_in_category(self, category_id: int):
        result = self.cursor.execute("SELECT name FROM products WHERE category_id = ?", (category_id)).fetchall()
        lst = []
        for i in result:
            lst.append(i[0])
        return lst

    def get_product_id(self, product_name: str):
        return self.cursor.execute("SELECT id FROM products WHERE name = ?", (product_name,)).fetchall()[0]

    def get_product(self, product_id: int):
        return {
            'id': product_id[0],
            'name': self.cursor.execute("SELECT name FROM products WHERE id = ?", (product_id)).fetchall()[0][0],
            'category_id': self.cursor.execute("SELECT category_id FROM products WHERE id = ?", (product_id)).fetchall()[0][0],
            'description': self.cursor.execute("SELECT description FROM products WHERE id = ?", (product_id)).fetchall()[0][0],
            'price': self.cursor.execute("SELECT price FROM products WHERE id = ?", (product_id)).fetchall()[0][0],
            'image_path': self.cursor.execute("SELECT image_path FROM products WHERE id = ?", (product_id)).fetchall()[0][0]
        }

    def add_order(self, user_id: int, product_id: int, order_date: str, delivery_date: str):
        self.cursor.execute("INSERT INTO orders (user_id, product_id, order_date, delivery_date) VALUES(?, ?, ?, ?)", (user_id, product_id, order_date, delivery_date))
        return self.conn.commit()

    def close(self):
        self.conn.close()


import torch
from torch import nn
from torch.autograd import Function
from torch.nn.modules.utils import _pair

from ..utils import ext_loader

ext_module = ext_loader.load_ext(
    '_ext', ['dynamic_voxelize_forward', 'hard_voxelize_forward'])

class _Voxelization(Function):

    @staticmethod
    def forward(ctx,
                points,
                voxel_size,
                coors_range,
                max_points=35,
                max_voxels=20000):
        """Convert kitti points(N, >=3) to voxels.

        Args:
            points (torch.Tensor): [N, ndim]. Points[:, :3] contain xyz points
                and points[:, 3:] contain other information like reflectivity.
            voxel_size (tuple or float): The size of voxel with the shape of
                [3].
            coors_range (tuple or float): The coordinate range of voxel with
                the shape of [6].
            max_points (int, optional): maximum points contained in a voxel. if
                max_points=-1, it means using dynamic_voxelize. Default: 35.
            max_voxels (int, optional): maximum voxels this function create.
                for second, 20000 is a good choice. Users should shuffle points
                before call this function because max_voxels may drop points.
                Default: 20000.

        Returns:
            voxels_out (torch.Tensor): Output voxels with the shape of [M,
                max_points, ndim]. Only contain points and returned when
                max_points != -1.
            coors_out (torch.Tensor): Output coordinates with the shape of
                [M, 3].
            num_points_per_voxel_out (torch.Tensor): Num points per voxel with
                the shape of [M]. Only returned when max_points != -1.
        """
        if max_points == -1 or max_voxels == -1:
            coors = points.new_zeros(size=(points.size(0), 3), dtype=torch.int)
            ext_module.dynamic_voxelize_forward(points, coors, voxel_size,
                                                coors_range, 3)
            return coors
        else:
            voxels = points.new_zeros(
                size=(max_voxels, max_points, points.size(1)))
            coors = points.new_zeros(size=(max_voxels, 3), dtype=torch.int)
            num_points_per_voxel = points.new_zeros(
                size=(max_voxels, ), dtype=torch.int)
            voxel_num = ext_module.hard_voxelize_forward(
                points, voxels, coors, num_points_per_voxel, voxel_size,
                coors_range, max_points, max_voxels, 3)
            # select the valid voxels
            voxels_out = voxels[:voxel_num]
            coors_out = coors[:voxel_num]
            num_points_per_voxel_out = num_points_per_voxel[:voxel_num]
            return voxels_out, coors_out, num_points_per_voxel_out

voxelization = _Voxelization.apply

class Voxelization(nn.Module):
    """Convert kitti points(N, >=3) to voxels.

    Please refer to `PVCNN <https://arxiv.org/abs/1907.03739>`_ for more
    details.

    Args:
        voxel_size (tuple or float): The size of voxel with the shape of [3].
        point_cloud_range (tuple or float): The coordinate range of voxel with
            the shape of [6].
        max_num_points (int): maximum points contained in a voxel. if
            max_points=-1, it means using dynamic_voxelize.
        max_voxels (int, optional): maximum voxels this function create.
            for second, 20000 is a good choice. Users should shuffle points
            before call this function because max_voxels may drop points.
            Default: 20000.
    """

    def __init__(self,
                 voxel_size,
                 point_cloud_range,
                 max_num_points,
                 max_voxels=20000):
        super().__init__()

        self.voxel_size = voxel_size
        self.point_cloud_range = point_cloud_range
        self.max_num_points = max_num_points
        if isinstance(max_voxels, tuple):
            self.max_voxels = max_voxels
        else:
            self.max_voxels = _pair(max_voxels)

        point_cloud_range = torch.tensor(
            point_cloud_range, dtype=torch.float32)
        voxel_size = torch.tensor(voxel_size, dtype=torch.float32)
        grid_size = (point_cloud_range[3:] -
                     point_cloud_range[:3]) / voxel_size
        grid_size = torch.round(grid_size).long()
        input_feat_shape = grid_size[:2]
        self.grid_size = grid_size
        # the origin shape is as [x-len, y-len, z-len]
        # [w, h, d] -> [d, h, w]
        self.pcd_shape = [*input_feat_shape, 1][::-1]

    def forward(self, input):
        if self.training:
            max_voxels = self.max_voxels[0]
        else:
            max_voxels = self.max_voxels[1]

        return voxelization(input, self.voxel_size, self.point_cloud_range,
                            self.max_num_points, max_voxels)

    def __repr__(self):
        s = self.__class__.__name__ + '('
        s += 'voxel_size=' + str(self.voxel_size)
        s += ', point_cloud_range=' + str(self.point_cloud_range)
        s += ', max_num_points=' + str(self.max_num_points)
        s += ', max_voxels=' + str(self.max_voxels)
        s += ')'
        return s


import spacy
import flask
from flask import jsonify,request

app = flask.Flask(__name__)

@app.route('/getproducts',methods=["POST"])
def get_all_item():
    nlp = spacy.load("output/model-last/")
    sentence = request.json['sentence']
    doc =nlp(sentence)
    output = []
    for ent in doc.ents:
        output.append(ent.text)

    output = jsonify(output)
    return output

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


#!/usr/bin/env python
import sys
import copy
import rospy
import moveit_commander
# import geometry_msgs.msg
import aruco_msgs.msg
# from math import pi
from math import fabs
# from std_msgs.msg import String
# from moveit_commander.conversions import pose_to_list
import control_msgs.msg
import numpy as np
import quaternion

moveit_commander.roscpp_initialize(sys.argv)
robot = moveit_commander.RobotCommander()
scene = moveit_commander.PlanningSceneInterface()
group_name = "manipulator"
move_group = moveit_commander.MoveGroupCommander(group_name)
rel_pos = aruco_msgs.msg.MarkerArray()

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

def pose_callback(data):

    global rel_pos
    if data.markers:
        rel_pos = data.markers[0].pose.pose

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

def status_callback (data):

    if not any(data.desired.velocities):

        global rel_pos
        curr_pos = move_group.get_current_pose().pose
        new_pos = curr_pos
        rel_or = rel_pos.orientation
        rel_or = np.quaternion(rel_or.w,rel_or.x,rel_or.y,rel_or.z)
        transform = quaternion.as_rotation_matrix(rel_or)

        rel_pos.position.z -= 0.30

        change_in_coords = np.matmul(np.linalg.inv(transform), np.array([[rel_pos.position.x,rel_pos.position.y,rel_pos.position.z]]).T)
        change_in_coords = change_in_coords * 0.01

        # import pdb; pdb.set_trace()
        print(rel_pos.position)
        checkx = fabs(rel_pos.position.x)>0.01
        checky = fabs(rel_pos.position.y)>0.01
        checkz = rel_pos.position.z>0.30 or rel_pos.position.z<0.29

        if checky or checkx or checkz:
                if checkz:
                    new_pos.position.x -= change_in_coords[0][0]
                if checky:
                    new_pos.position.y += change_in_coords[1][0]
                if checkx:
                    new_pos.position.z += change_in_coords[2][0]

                # import pdb; pdb.set_trace()

                # move_group.set_pose_target(new_pos)
                # plan = move_group.go(wait=True)
                waypoints = []
                waypoints.append(copy.deepcopy(new_pos))

                (plan, fraction) = move_group.compute_cartesian_path(
                                       waypoints,   # waypoints to follow
                                       0.01,        # eef_step
                                       0.0)
                move_group.execute(plan)

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

def listener():
    rospy.init_node('aruco_pose_listener', anonymous=True)

    rospy.Subscriber("/aruco_marker_publisher/markers", aruco_msgs.msg.MarkerArray, pose_callback)
    rospy.Subscriber("/arm_controller/state", control_msgs.msg.JointTrajectoryControllerState, status_callback)
    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    listener()