#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2021/9/9 11:05
# @Author :
# @File : modelTest.py
# @Function:

import warnings
warnings.filterwarnings("ignore")

import argparse
import random
import numpy as np

import torch
from torch import cuda
from transformers import AutoModel, AutoTokenizer
from torch.utils.data import DataLoader
import os

import sys
project_dir = '/data/0shared/*/attentionRegularization'
sys.path.append(project_dir)
import utils
import mednli_dataset
import alignment
from common import mednli_train_with_test
from models import mednli_model
import pandas as pd

#判断设备
device ='cuda:9' if cuda.is_available() else 'cpu'
#参数定义
PARSER = argparse.ArgumentParser(description='The code for attention visualization')
PARSER.add_argument('-device', '--device', default=device)
PARSER.add_argument('-max_len', '--max_len', default=256, type=int)
# bert-base-uncased
# bionlp/bluebert_pubmed_mimic_uncased_L-12_H-768_A-12(抛弃)
# ttumyche/bluebert
# dmis-lab/biobert-v1.1
# emilyalsentzer/Bio_ClinicalBERT
# nfliu/scibert_basevocab_uncased
# albert-base-v2
# roberta-base
PARSER.add_argument('-model_name', '--model_name', default='dmis-lab/biobert-v1.1', type=str)
PARSER.add_argument('-project_dir', '--project_dir', default=project_dir, type=str)
PARSER.add_argument('-test_file', '--test_file', default=os.path.join(project_dir, 'data/medNLI/mli_test_v1_kg.csv'), type=str)
PARSER.add_argument('-epochs', '--epochs', default=2, type=int)
PARSER.add_argument('-batch_size', '--batch_size', default=1, type=int)
PARSER.add_argument('-learning_rate', '--learning_rate', default=1e-05, type=float)
PARSER.add_argument('-class_nums', '--class_nums', default=1, type=int)  # 0-1 两个类别 是个二分类问题
PARSER.add_argument('-save_path', '--save_path', default=os.path.join(project_dir, 'save_model/medNLI'), type=str)
PARSER.add_argument('-loss_type', '--loss_type', type=str, default='task', choices=('task+pdg', 'task+adg','bce+both'))

# 绘图时使用
PARSER.add_argument('-attention_pca', '--attention_pca', type=bool, default=True)

args = PARSER.parse_args()

if __name__ == '__main__':
    # 固定随机种子（据说只有放在main里才能起作用）
    seed = 2021
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True

    args.label2id = {'entailment': 0, 'contradiction': 1, 'neutral': 2}
    # step-1:加载预训练模型
    tokenizer = AutoTokenizer.from_pretrained(args.model_name, truncation=True, do_lower_case=True, reteurn_dict=True, local_files_only=False)
    bert_model = AutoModel.from_pretrained(args.model_name, output_attentions=True, output_hidden_states=True) #, output_attentions=True
    bert_total_parameters = sum(p.numel() for p in bert_model.parameters() if p.requires_grad)
    bert_trainable_parameters = sum(p.numel() for p in bert_model.parameters())
    print('params number of bert:', bert_total_parameters)
    print('trainable params number of bert:', bert_trainable_parameters)

    # 加载已经训练好的保存下来的模型
    # step-2:构建新的模型

    test_medNlIModel = mednli_model.MedNLIClassifier(tokenizer, bert_model, args)

    # step-3:加载保存下来的参数
    if '/' in args.model_name:
        model_name = args.model_name.replace('/', '-')
    else:
        model_name = args.model_name
    print('current test model:', 'model_%s_%s.pt' % (model_name, args.loss_type))
    test_medNlIModel.load_state_dict(torch.load(os.path.join(args.save_path, 'model_%s_%s.pt' % (model_name, args.loss_type)), map_location=device))

    test_medNlIModel = test_medNlIModel.to(args.device)

    # step-4: 准备好测试数据集
    test_df = pd.read_csv(args.test_file)
    medNLI_test_dataset = mednli_dataset.MedNLIDataset(test_df, tokenizer, args, flag='test')
    test_dataloader = DataLoader(medNLI_test_dataset, shuffle=False, batch_size=args.batch_size)

    # step-5: 模型推理
    test_score_list = mednli_train_with_test.test(test_medNlIModel, test_dataloader, args)


import dataclasses

#/usr/bin/env python
# -*- coding: utf-8 -*-
import os, pathlib, sys,click,logging,string,csv,json,tempfile,re

sys.path.append(os.getcwd())

parent_path = pathlib.Path(__file__).parent.absolute()
sys.path.append(str(parent_path))

master_path = parent_path.parent
sys.path.append(str(master_path))

project_path = master_path.parent
sys.path.append(str(project_path))

import yaml2pyclass

class Config(yaml2pyclass.CodeGenerator):
    @dataclasses.dataclass
    class ExternalApiClass:
        sec_firm: str
        pdf: str

    @dataclasses.dataclass
    class ApiClass:
        @dataclasses.dataclass
        class RouteClass:
            main: str

        port: int
        host: str
        route: RouteClass

    @dataclasses.dataclass
    class NerModelsClass:
        @dataclasses.dataclass
        class EngClass:
            model: str
            sent_vocab: str
            tag_vocab: str
            max_len: int

        eng: EngClass

    external_API: ExternalApiClass
    API: ApiClass
    ner_models: NerModelsClass
    log_file_path: str
    search_word_file: str


#!/bin/env python
#coding: utf8
from flask import Flask, render_template, jsonify, request, make_response, send_file
from flask_digest import Stomach

from service import *

CONTENT_PATH='/ryg' #为了保持通过nginx之后uri一致使得Digest鉴权机制成功,需要设置上下文路径,

def create_app():
  app = Flask(__name__, static_url_path=CONTENT_PATH)
  app.config['JSON_AS_ASCII'] = False
  stomach = Stomach(__name__)
  return (app, stomach)

app, stomach = create_app()

db=dict()
@stomach.register
def add_user(username, password):
    db[username] = password

@stomach.access
def get_user(username):
    return db.get(username, None)

@app.route(CONTENT_PATH+"/getSummary/")
@app.route(CONTENT_PATH+"/getSummary/<string:hn>")
@stomach.protect
def getSystemSummary(hn='all'):
    detailFlag = False
    if hn=='all' and request.args.get("detail", '0') in ['1', 'true']:
        detailFlag =True
    return jsonify(getSummary(hn, detailFlag))

@app.route(CONTENT_PATH+"/getObjHistory/<string:objId>/<int:count>")
@stomach.protect
def getStatHistory(objId, count):
    return jsonify(getObjHistory(objId, count))

@app.route(CONTENT_PATH+"/")
@app.route(CONTENT_PATH+"/index.html")
@stomach.protect
def index():
    return app.send_static_file("index.html")

if __name__=="__main__":
    import os
    #env sample : export RYG_AUTH=paco,ericsson:admin:123456
    auth_str = os.environ.get('RYG_AUTH')
    if auth_str is None:
        add_user('admin', '123456') #not good practice, just for test
    else:
        map(lambda x:add_user(*x),
            map(lambda x:x.split(','),
                auth_str.split(':')))
    app.run(host="0.0.0.0", port=38888, debug=False)


from typing import Iterator, NamedTuple
from datetime import datetime

from .base import Event

import logging

log = logging.getLogger(__name__)

class PushEventCommit(NamedTuple):
    event_id: int
    event_created_at: datetime

    repo_name: str
    org_name: str
    actor_login: str

    sha: str
    author_name: str
    author_email: str

class PushEvent(Event):

    def get_pure_commits(self) -> Iterator[dict]:
        commits = self.payload.get('commits')
        if commits:
            for commit in commits:
                author = commit.get('author')
                lined_commit = commit.copy()
                if author:
                    lined_commit['author_name'] = author.get('name')
                    lined_commit['author_email'] = author.get('email')
                yield lined_commit

    def get_commits(self) -> Iterator[PushEventCommit]:
        for commit in self.get_pure_commits():
            yield PushEventCommit(event_id=self.id,
                                  event_created_at=self.created_at,
                                  repo_name=self.repository.name,
                                  org_name=self.organization.login,
                                  actor_login=self.actor.login,
                                  sha=commit.get('sha'),
                                  author_name=commit.get('author_name'),
                                  author_email=commit.get('author_email'))


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

"""
CS224N 2019-20: Homework 5
model_embeddings.py: Embeddings for the NMT model
Pencheng Yin <pcyin@cs.cmu.edu>
Sahil Chopra <schopra8@stanford.edu>
Anand Dhoot <anandd@stanford.edu>
Michael Hahn <mhahn2@stanford.edu>
"""

import torch.nn as nn

# Do not change these imports; your module names should be
#   `CNN` in the file `cnn.py`
#   `Highway` in the file `highway.py`
# Uncomment the following two imports once you're ready to run part 1(j)

from cnn import CNN
from highway import Highway

# End "do not change"

class ModelEmbeddings(nn.Module):
    """
    Class that converts input words to their CNN-based embeddings.
    """

    def __init__(self, word_embed_size, vocab):
        """
        Init the Embedding layer for one language
        @param word_embed_size (int): Embedding size (dimensionality) for the output word
        aka e_word

        @param vocab (VocabEntry): VocabEntry object. See vocab.py for documentation.

        Hints: - You may find len(self.vocab.char2id) useful when create the embedding
        """
        super(ModelEmbeddings, self).__init__()
        self.word_embed_size = word_embed_size
        self.vocab = vocab
        self.e_char = 50
        self.char_emb = nn.Embedding(len(vocab.char2id),self.e_char,padding_idx=vocab.char_pad)
        self.highway = Highway(self.word_embed_size)
        self.cnn = CNN(self.e_char,self.word_embed_size)
        self.dropout = nn.Dropout(p=0.3)

    def forward(self, x_padded):
        """
        Looks up character-based CNN embeddings for the words in a batch of sentences.
        @param x_padded: Tensor of integers of shape (sentence_length, batch_size, max_word_length) where
            each integer is an index into the character vocabulary
        @param x_word_emb: Tensor of shape (sentence_length, batch_size, word_embed_size), containing the
            CNN-based embeddings for each word of the sentences in the batch
        """

#         raw_input x_padded: (max_sentence_length,bs,max_word_length aka m)
#             - each integer is an index into the character vocabulary
#             - this should be output of to_input_tensor_char()

#         --char_emb()-->
#         x_emb: (max_sentence_length,bs,max_word_length,e_char)
#             - with e_char is size of character embedding.
        x_emb = self.char_emb(x_padded)

#         --reshape()-->
#         x_reshaped: (max_sentence_length,bs,e_char,max_word_length)
        x_reshaped = x_emb.permute(0,1,3,2)

#         --cnn()-->
#         x_conv: (max_sentence_length,bs,e_word,max_word_length-k+1)
#             - with k is kernel size,e_word is the desired word embedding size
#             - do a loop for each sentence
#         --relu_and_globalmaxpool()-->
#         x_conv_out: (max_sentence_length,bs,e_word)
        x_conv_out = self.cnn(x_reshaped)

#         --high_way()-->
#         x_highway: (max_sentence_length,bs,e_word)
        x_highway = self.highway(x_conv_out)
#         --dropout()-->
#         x_word_emb: (max_sentence_length,bs,e_word)
        x_word_emb = self.dropout(x_highway)
        return x_word_emb


import logging
import os
from datetime import datetime

dir_path = './sum_dist/logs'
filename = f'{datetime.now().strftime("%Y%m%d-%H%M%S")}.log'

def get_logger(log_name, log_level="INFO"):
    if not os.path.exists(f'{dir_path}'):
        os.makedirs(f'{dir_path}')

    logger = logging.getLogger(log_name)
    logger.setLevel(log_level)

    formatter = logging.Formatter(
        '\n%(asctime)s %(name)-5s === %(levelname)-5s === %(message)s\n')

    file_handler = logging.FileHandler(
        filename=f'{dir_path}/{filename}',
        mode='w',
        encoding='utf-8')
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.INFO)
    logger.addHandler(file_handler)

    console_handler = logging.StreamHandler()
    console_handler.setLevel(log_level)
    console_handler.setFormatter(formatter)
    logger.addHandler(console_handler)

    return logger


# -*- coding: utf-8 -*-
from openerp import tools
from openerp.osv import fields, osv
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
from openerp.tools.translate import _

class give_k(osv.osv):
    _name = 'give.k'
    _description = 'Tang K'

    _columns = {
        'name': fields.char('Name'),
        'date_start': fields.datetime('Start Date'),
        'date_end': fields.datetime('End Date'),
        'number_k': fields.integer('Number K Give'),
        'is_active': fields.boolean('Active'),
        'log_ids': fields.one2many('give.k.history', 'ref_id', 'Logs Changed'),
                }

    _defaults = {
        'name': u'Tặng K',
        'is_active': False,
#         'number_k': 0
                 }

    def get_active_id(self, cr, uid, context=None):
        ids = self.search(cr, uid, [], order='create_date desc', limit=1, context=context)
        return {
                "name": _("Give K"),
                "type": "ir.actions.act_window",
                "view_type": "form",
                "view_mode": "form",
                "res_model": self._name,
                "res_id": ids and ids[0] or None,
            }

    def onchange_number_k(self, cr, uid, ids, number_k, context=None):
        if int(number_k) < 0:
            return {'warning': {
                'title': _('Input number K'),
                'message': _("Number K must be > 0 !"),
            }}
        return {}

    def onchange_date(self, cr, uid, ids, date_start, date_end, context=None):
        date_start = date_start and datetime.strptime(date_start, DEFAULT_SERVER_DATETIME_FORMAT) or False
        date_end = date_end and datetime.strptime(date_end, DEFAULT_SERVER_DATETIME_FORMAT) or False
        if date_end and date_start and date_start > date_end:
            return {'warning': {
                'title': _('Select date'),
                'message': _("Start Date can't larger than End Date !"),
            }}
        return {}

    def create(self, cr, uid, vals, context=None):
        date_start = date_end = False
        number_k = 0
        if 'number_k' in vals and vals.get('number_k'):
            number_k = vals.get('number_k')
        if 'date_start' in vals and vals.get('date_start'):
            date_start = datetime.strptime(vals.get('date_start'), DEFAULT_SERVER_DATETIME_FORMAT)
        if 'date_end' in vals and vals.get('date_end'):
            date_end = datetime.strptime(vals.get('date_end'), DEFAULT_SERVER_DATETIME_FORMAT)
        if number_k <= 0:
            raise osv.except_osv(_('Error'), _('Number K must be > 0 !'))
        if not (date_start and date_end and number_k):
            raise osv.except_osv(_('Error'), _('Must have Start Date and End Date and Number K !'))
        if date_start > date_end:
            raise osv.except_osv(_('Error'), _("Start Date can't larger than End Date !"))
        give_id = super(give_k, self).create(cr, uid, vals, context=context)
        vals_tmp = vals.copy()
        vals_tmp.update({ 'ref_id': give_id })
        self.pool.get('give.k.history').create(cr, uid, vals_tmp, context=context)
        return give_id

    def write(self, cr, uid, ids, vals, context=None):
        give_logs = self.pool.get('give.k.history')
        for givek in self.browse(cr, uid, ids, context=context):
            number_k = givek.number_k
            is_active = givek.is_active
            if 'is_active' in vals:
                is_active = vals.get('is_active',False)
            if 'number_k' in vals:
                number_k = vals.get('number_k', 0)
            if number_k <= 0:
                raise osv.except_osv(_('Error'), _('Number K must be > 0 !'))
            date_start = 'date_start' in vals and vals.get('date_start') or givek.date_start
            date_end = 'date_end' in vals and vals.get('date_end') or givek.date_end
            date_start = date_start and datetime.strptime(date_start, DEFAULT_SERVER_DATETIME_FORMAT) or False
            date_end = date_end and datetime.strptime(date_end, DEFAULT_SERVER_DATETIME_FORMAT) or False
            if not (date_start and date_end and number_k):
                raise osv.except_osv(_('Error'), _('Must have Start Date and End Date and Number K !'))
            if date_start > date_end:
                raise osv.except_osv(_('Error'), _("From Date can't larger than To Date !"))
            vals_tmp = {
                'ref_id': givek.id,
                'name': 'name' in vals and vals.get('name',False) or givek.name,
                'date_start': 'date_start' in vals and vals.get('date_start',False) or givek.date_start,
                'date_end': 'date_end' in vals and vals.get('date_end',False) or givek.date_end,
                'number_k': number_k,
                'is_active': is_active,
                        }
            give_logs.create(cr, uid, vals_tmp, context=context)
        return super(give_k, self).write(cr, uid, ids, vals, context=None)

class give_k_history(osv.osv):
    _name = 'give.k.history'
    _description = 'Lich Su Thay Doi Tang K'
    _order = 'id desc'

    _columns = {
        'name': fields.char('Name'),
        'ref_id': fields.many2one('give.k', 'Give K Reference'),
        'date_start': fields.datetime('Start Date'),
        'date_end': fields.datetime('End Date'),
        'number_k': fields.integer('Number K Give'),
        'is_active': fields.boolean('Active'),
                }


import numpy as np
import shapely
import vsketch

import pysometric as pyso

class Example01Sketch(vsketch.SketchClass):
    """
    Creates a grid of boxes with fill and hatch texturing.
    """

    unit_size = vsketch.Param(1.0, unit="cm")

    def create_boxes(self):
        boxes = []
        top = {"textures": [pyso.HatchTexture(4)]}
        right = {"textures": [pyso.FillTexture()]}
        for i in np.arange(-5, 5, 2):
            for j in np.arange(-5, 5, 2):
                boxes.append(pyso.Box((i, j, 0), 1, 1, 1, top, {}, right))

        return boxes

    def draw(self, vsk: vsketch.Vsketch) -> None:
        vsk.size("a4", landscape=False, center=False)

        frame = shapely.box(0, 0, vsk.width, vsk.height)
        scene = pyso.Scene(frame, self.unit_size, self.create_boxes())
        scene.render(vsk)

    def finalize(self, vsk: vsketch.Vsketch) -> None:
        vsk.vpype("linemerge linesimplify reloop linesort")

if __name__ == "__main__":
    Example01Sketch.display()


import torch
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import torchvision
from torch.nn.functional import conv2d
import cv2
import os
from pathlib import Path

PATH = r"C:\Users\daniel\Desktop\Filtri\gatto.jpg"

def load_image(infilename):
    """This function loads an image into memory when you give it
       the path of the image
    """
    img = Image.open(infilename)
    img.load()
    trasformer = torchvision.transforms.Grayscale(1)
    img = trasformer(img)
    data = np.asarray(img, dtype="float32")
    return data

def linear_stretch(image: torch.tensor, a: torch.float32, b: torch.float32):
    return torch.clamp(torch.round((image.to(torch.float32) * a) + b), min=0, max=255).type(torch.uint8)

def otsu_treshold(image: torch.tensor, device: torch.device):
    hist = torch.zeros((256, ))
    for i in range(256):
        hist[i] = torch.sum(image.type(torch.float32) == i)

    tresh = -1
    maxvar = -1
    dat = torch.arange(256).type(torch.float32)

    for i in range(256):
        w1 = hist[:i].sum()
        w2 = hist[i:].sum()

        if w1 == 0 or w2 == 0:
            continue
        m1 = torch.sum(hist[:i]* dat[:i])/w1
        m2 = torch.sum(hist[i:]* dat[i:])/w2
        var = w1*w2*((m1-m2)**2)
        if var > maxvar:
            maxvar = var
            tresh = i
    return tresh - 1

def tresh_filter(image:torch.tensor, treshold: torch.uint8):
    print("Using treshold: {}".format(treshold))
    return ((image > treshold) * 255).type(torch.uint8)

def im_show(image: torch.tensor):
    plt.imshow(image, cmap="gray")
    plt.show()

def run_prev_filters():
    im = load_image(PATH)
    im = torch.from_numpy(im).type(torch.uint8)

    # linear stretch
    filtered = linear_stretch(im, 0.8, 5)
    im_show(filtered)

    # casual treshold filter
    treshold = 70
    filtered = tresh_filter(im, treshold)
    im_show(filtered)

    # OTSU treshold filter
    treshold = otsu_treshold(im, device)
    filtered = tresh_filter(im, treshold)
    im_show(filtered)

def sobel(image:torch.Tensor, device: torch.device):
    im = image.to(torch.float32).to(device)
    Sx = torch.tensor([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=torch.float32).reshape((1,1,3,3)).to(device)
    Sy = torch.tensor([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], dtype=torch.float32).reshape((1,1,3,3)).to(device)

    gx = conv2d(im.reshape((1, 1, im.shape[0], im.shape[1])), Sx).to(device)
    gy = conv2d(im.reshape((1, 1, im.shape[0], im.shape[1])), Sy).to(device)

    m = ((gx**2 + gy**2)**(0.5)) / 1140 *255
    d = (torch.atan2(gy, gx)*90/np.pi) + 90
    s = torch.ones(m.shape).to(torch.float32) * 100

    image = torch.cat((d,s,m), dim=0).squeeze(1)
    image = cv2.cvtColor(image.numpy().swapaxes(0, 2).swapaxes(0, 1).astype(np.uint8), cv2.COLOR_HSV2RGB)
    return image

def live_filtering(filter, params: tuple):
    cam = cv2.VideoCapture(0)
    while True:
        ret, frame = cam.read()
        frame = np.asarray(frame.mean(axis=2), dtype="float32")

        frame = torch.from_numpy(frame).type(torch.uint8)
        #result = filter(frame, params[0], params[1]).numpy()   # Linear Stretch
        #result = filter(frame, params[0]).numpy()              # Casual treshold

        #t = otsu_treshold(frame, params[0])
        #result = filter(frame, t).numpy()                      # OTSU

        result = filter(frame, params[0])                       # SOBEL

        cv2.imshow("Live Filter", result)

        k = cv2.waitKey(1)
        if k%256 == 27:
            # ESC pressed
            print("ESC pressed, closing...")
            break

    cam.release()
    cv2.destroyAllWindows()

def image_stitching():
    im_a = cv2.imread("gallery_0.jpg")
    im_a = np.swapaxes(np.swapaxes(im_a, 0, 2), 1, 2)
    im_a = im_a[::-1, :, :]  # from BGR to RGB

    im_b = cv2.imread("gallery_1.jpg")
    im_b = np.swapaxes(np.swapaxes(im_b, 0, 2), 1, 2)
    im_b = im_b[::-1, :, :]  # from BGR to RGB

    points_0 = np.float32([[193, 33], [316, 95], [181, 238], [310, 210]])
    points_1 = np.float32([[137, 50], [339, 58], [132, 191], [335, 199]])

    Tmat = cv2.getPerspectiveTransform(points_1, points_0)

    warp_dst = cv2.warpPerspective(np.swapaxes(np.swapaxes(im_b, 0, 2), 1, 0), Tmat, (int(im_b.shape[2]*0.8), int(im_b.shape[1]*1.8)))

    warp_dst = np.swapaxes(np.swapaxes(warp_dst, 0, 2), 1, 2)

    dim1 = max(warp_dst.shape[1], im_a.shape[1])
    dim2 = max(warp_dst.shape[2], im_a.shape[2])

    out = np.zeros((3, dim1, dim2),dtype=np.uint8)
    out[:, :im_a.shape[1], :] = im_a
    out[:, im_a.shape[1]: warp_dst.shape[1], :warp_dst.shape[2]] = warp_dst[:, im_a.shape[1]: , :]

    plt.imshow(np.swapaxes(np.swapaxes(out, 0, 2), 1, 0))
    plt.show()

if __name__ == "__main__":
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print("Running on {}".format(device))
    if torch.cuda.is_available():
        print("Actual GPU in use: {}".format(torch.cuda.get_device_name(device)))

    print(Path(__file__).parent.absolute())
    os.chdir(Path(__file__).parent.absolute())
    # run_prev_filters()

    # live_filtering(linear_stretch, (0.8, 10, ))                   # LINEAR TRESHOLD LIVE
    # live_filtering(tresh_filter, (30, ))                          # CASUAL TRESHOLD LIVE
    # live_filtering(tresh_filter, (device, ))                      # OTSU LIVE
    # live_filtering(sobel, (torch.device("cpu"), ))                # SOBEL LIVE

    image_stitching()


# import colorgram
#
# rgb_colors = []
# colors = colorgram.extract('./res/hrist.jpg', 6)
# for color in colors:
#     r = color.rgb.r
#     g = color.rgb.g
#     b = color.rgb.b
#
#     rgb_colors.append((r, g, b))
#
# print(rgb_colors)
import turtle as t
import random
from turtle import Turtle, Screen

color_list = [(229, 228, 226), (225, 223, 224), (199, 175, 117), (124, 36, 24), (210, 221, 213), (168, 106, 57)]
t.colormode(255)

turtle = Turtle()
turtle.hideturtle()

def draw_spot_painting(size, dots_size):
    turtle.penup()
    for row in range(size):
        if row % 2 == 0 or row == 0:
            turtle.setheading(180)
        else:
            turtle.setheading(0)

        for col in range(size):
            turtle.dot(dots_size, random.choice(color_list))

            if col < size - 1:
                turtle.forward(dots_size * 2)

        turtle.setheading(90)
        turtle.forward(dots_size * 2)

draw_spot_painting(10, 20)

screen = Screen()
screen.exitonclick()


#Letter-number Cipher

#Define Our Variables
LN_Plaintext = input("Input your Plaintext for the Letter-Number Cipher: ")

def LN_Encryption(LN_Plaintext):
    LN_Ciphertext = [] #Create a List that will store the Ciphertext data, which appear as numbers converted from the letters of our plaintext strings

    for i in LN_Plaintext.lower(): #For loop to iterate though the plaintext. Lower() ensures that a capital letter and lowercase appear as the same ASCII value in the ciphertext.
        i = ord(i) - 96 #Use the ord() fucntion to return an integer representing the Unicode Value and then subtract 96 to get the ASCII value. Blank spaces appear as -64 in the ciphertext.
        LN_Ciphertext.append(i) #Append the looped values from the plaintext into the Ciphertext list.

    return(LN_Ciphertext)

print(LN_Encryption(LN_Plaintext))


# -*- coding: utf-8 -*-
"""
Created on Fri Jun  2 13:30:55 2023

@author: jksls
"""

import pandas as pd
import numpy as np

import matplotlib.pyplot as mpl
mpl.style.use('fivethirtyeight')

df = pd.read_csv('dataset/new_cars_prices.csv')
df.dropna(axis=0 , how='any' , inplace = True)

p_old    = np.array(df['Old Price'], dtype=str)
p_new    = np.array(df['New Price'], dtype=str)
p_old    = np.array([k.replace('\n EGP', '').replace(',', '').replace('\nEGP', '') for k in p_old])
p_new    = np.array([k.replace('\n EGP', '').replace(',', '').replace('\nEGP', '') for k in p_new])
p_old    = p_old.astype(int)
p_new    = p_new.astype(int)
name     = np.array(df['Car Model'])
date     = np.array(df['date_range'])
diff     = np.array([(p_new[i] - p_old[i]) for i in range(len(p_old))])
diff_abs = np.array([abs(diff[i]) for i in range(len(diff))])
uniq_nam = np.unique(name)

# Max of old and new prices and the name of the car
print(max(p_old),max(p_new))
print(name[p_old == max(p_old)] , name[p_new == max(p_new)])

# Min of old and new prices and the name of the car
print(min(p_old) , min(p_new))
print(name[p_old == min(p_old)] , name[p_new == min(p_new)])

p_avg       = []
dif_avg     = []
dif_avg_abs = []

for i in range(len(uniq_nam)):
    merged_p = np.unique(np.concatenate([p_old[name == uniq_nam[i]],p_new[name == uniq_nam[i]]]))
    p_avg.append(merged_p.mean())
    dif_avg.append(diff[name == uniq_nam[i]].mean())
    dif_avg_abs.append(diff_abs.mean())

print(max(p_avg) , max(dif_avg) , max(dif_avg_abs))
print(min(p_avg) , min(dif_avg) , min(dif_avg_abs))

print()


from tensorflow.examples.tutorials.mnist import input_data
import numpy as np

def proc(path_dataset, one_hot=True):

    mnist = input_data.read_data_sets(path_dataset, one_hot=one_hot)

    train_x = mnist.train.images
    train_y = mnist.train.labels

    train_x = train_x.reshape(len(train_x), 28, 28, 1)

    return train_x, train_y

if __name__ == '__main__':
    root = 'src'
    x, y = proc(root)

    print('shape of x:', x.shape, 'range:', np.min(x), '~', np.max(x))
    print('shape of y:', y.shape)

    np.save('x.npy', x)
    np.save('y.npy', y)


import tensorflow as tf
from tensorflow import keras
from pixel_based_module import Pixel_Anchor

class BasicBlock(keras.layers.Layer):
    def __init__(self, filters, strides=1):
        super(BasicBlock, self).__init__()
        self.conv1 = keras.layers.Conv2D(filters=filters, kernel_size=(3, 3), strides=strides, padding='same')
        self.bn1 = keras.layers.BatchNormalization()
        self.relu = keras.layers.ReLU()

        self.conv2 = keras.layers.Conv2D(filters=filters, kernel_size=(3, 3), strides=1, padding='same')
        self.bn2 = keras.layers.BatchNormalization()

        self.identity = keras.layers.Conv2D(filters=filters, kernel_size=(1, 1), strides=strides, padding='same')

        # if strides > 1:
        #     self.identity = keras.Sequential()
        #     self.identity.add(keras.layers.Conv2D(filters, (1, 1), strides=strides))
        # else:
        #     self.identity = lambda x: x

    def call(self, inputs, training=None):
        x = self.conv1(inputs)
        x = self.bn1(x)
        x = self.relu(x)

        x = self.conv2(x)
        x = self.bn2(x)
        identity = self.identity(inputs)

        x = keras.layers.add([x, identity])
        x = self.relu(x)

        return x

class ResNET50(keras.layers.Layer):
    def __init__(self):
        super(ResNET50, self).__init__()

        self.preprocess = keras.Sequential([
            keras.layers.Conv2D(filters=64, kernel_size=(3, 3), strides=1, padding='same'),
            keras.layers.BatchNormalization(),
            keras.layers.ReLU(),
            keras.layers.MaxPool2D(pool_size=(2, 2), strides=1, padding='same')
        ])

        self.resblock_1 = self.__build_resblock(64, 2)
        self.resblock_2 = self.__build_resblock(64, 2, strides=2)
        self.resblock_3 = self.__build_resblock(128, 2, strides=1)
        self.resblock_4 = self.__build_resblock(128, 2, strides=2)
        self.resblock_5 = self.__build_resblock(256, 2, strides=1)
        self.resblock_6 = self.__build_resblock(256, 2, strides=2)
        self.resblock_7 = self.__build_resblock(512, 2, strides=1)
        self.resblock_8 = self.__build_resblock(512, 2, strides=1)
        self.resblock_9 = self.__build_resblock(512, 2, strides=2)
        self.resblock_10 = self.__build_resblock(1024, 2, strides=1)
        self.resblock_11 = self.__build_resblock(1024, 2, strides=1)
        self.resblock_12 = self.__build_resblock(1024, 2, strides=2)

    def call(self, inputs, training=None):
        x = self.preprocess(inputs)
        x = self.resblock_1(x)
        x = self.resblock_2(x)
        x = self.resblock_3(x)
        x = self.resblock_4(x)
        x = self.resblock_5(x)
        x = self.resblock_6(x)
        x = self.resblock_7(x)
        x = self.resblock_8(x)
        x = self.resblock_9(x)
        x = self.resblock_10(x)
        x = self.resblock_11(x)
        x = self.resblock_12(x)

        return x

    def __build_resblock(self, filters, basic_block_num, strides=1):
        resblocks = keras.Sequential()

        resblocks.add(BasicBlock(filters, strides))

        for _ in range(basic_block_num):
            resblocks.add(BasicBlock(filters, strides=1))

        return resblocks

class ResNET50_Pixel_Anchor(keras.layers.Layer):
    def __init__(self):
        super(ResNET50_Pixel_Anchor, self).__init__()

        self.preprocess = keras.Sequential([
            keras.layers.Conv2D(filters=64, kernel_size=(3, 3), strides=1, padding='same'),
            keras.layers.BatchNormalization(),
            keras.layers.ReLU(),
            keras.layers.MaxPool2D(pool_size=(2, 2), strides=1, padding='same')
        ])

        self.resblock_1 = self.__build_resblock(64, 2)
        self.resblock_2 = self.__build_resblock(64, 2, strides=2)
        self.resblock_3 = self.__build_resblock(128, 2, strides=2)
        self.resblock_4 = self.__build_resblock(128, 2, strides=1)
        self.resblock_5 = self.__build_resblock(256, 2, strides=2)
        self.resblock_6 = self.__build_resblock(256, 2, strides=1)
        self.resblock_7 = self.__build_resblock(512, 2, strides=2)
        self.resblock_8 = self.__build_resblock(512, 2, strides=1)
        self.resblock_9 = self.__build_resblock(512, 2, strides=1)
        self.resblock_10 = self.__build_resblock(1024, 2, strides=2)
        self.resblock_11 = self.__build_resblock(1024, 2, strides=1)
        self.resblock_12 = self.__build_resblock(1024, 2, strides=1)

    def call(self, inputs, training=None):
        x = self.preprocess(inputs)
        x = self.resblock_1(x)      # [None, 1 times, 1 times, 64]
        x = self.resblock_2(x)      # [None, 1/2 times, 1/2 times, 64]
        x = self.resblock_3(x)      # [None, 1/4 times, 1/4 times, 128]
        x = self.resblock_4(x)      # [None, 1/4 times, 1/4 times, 128]
        out_1_4 = x
        x = self.resblock_5(x)      # [None, 1/8 times, 1/8 times, 256]
        x = self.resblock_6(x)      # [None, 1/8 times, 1/8 times, 256]
        out_1_8 = x
        x = self.resblock_7(x)      # [None, 1/16 times, 1/16 times, 512]
        x = self.resblock_8(x)      # [None, 1/16 times, 1/16 times, 512]
        x = self.resblock_9(x)      # [None, 1/16 times, 1/16 times, 512]
        out_1_16 = x
        # x = self.resblock_10(x)     # [None, 1/32 times, 1/32 times, 1024]
        # x = self.resblock_11(x)     # [None, 1/32 times, 1/32 times, 1024]
        # x = self.resblock_12(x)     # [None, 1/32 times, 1/32 times, 1024]

        return out_1_4, out_1_8, out_1_16

    def __build_resblock(self, filters, basic_block_num, strides=1):
        resblocks = keras.Sequential()

        resblocks.add(BasicBlock(filters, strides))

        for _ in range(basic_block_num):
            resblocks.add(BasicBlock(filters, strides=1))

        return resblocks

if __name__ == "__main__":
    import cv2
    import numpy as np
    r = ResNET50_Pixel_Anchor()
    im_data =  cv2.imread('D:\\github_projects\\pixel_text\\images\\test1.jpg')
    im_data = cv2.resize(im_data, (224, 224))
    im_data = np.expand_dims(im_data, 0)
    im_data = tf.dtypes.cast(im_data, dtype=tf.float32)

    x1, x2, x3 = r(im_data)

    p = Pixel_Anchor()
    x = p([x1, x2, x3])

    cv2.imshow('123', im_data)
    cv2.waitKey(0)
    a = 10


from pathlib import Path
import torch
from torch import nn
from PIL import Image
from torchvision import transforms
import argparse
from csv import writer
from tqdm import tqdm

def create_pretrain_model():
    """
    Create a pretrained model for ELO image classification. Base on the squeezenet architecture.
    :return: torchvision.models.squeezenet.SqueezeNet with weights for the classification.
    """
    # Create model
    model = torch.hub.load("pytorch/vision:v0.10.0", "squeezenet1_0", pretrained=False)
    model.classifier[1] = nn.Conv2d(512, 3, kernel_size=(1, 1), stride=(1, 1))
    model.num_classes = 3
    # Load pre trained weights
    model.load_state_dict(
        torch.load("SqueezeNet_pretrain_epoch-38.pt", map_location=torch.device("cpu"))
    )
    model.eval()

    return model

def batch_classify(images_path, output_path):
    """
    Classify a list of images, dataloader are not use to avoid memory issues with big list of files.
    :param images_path: list of path of images
    :param output_path If not none, the prediction will be added to the targeted csv
    :return:
    """
    # Create the torch model
    model = create_pretrain_model()
    # Classify the image
    classification_list = []
    for image_path in tqdm(images_path):
        classification_list.append(classify_an_image(model, image_path, output_path))

    return classification_list

def classify_an_image(model, image_path, output_path):
    """
    Load ONE image an classify it.
    :param model: the pretrained model
    :param image_path: path to one image
    :param output_path If not none, the prediction will be added to the targeted csv
    :return: dictionary with the image name and the classification probability.
    """
    image_path = Path(image_path)

    # Load image
    input_size = 224
    input_image = Image.open(image_path)
    preprocess = transforms.Compose(
        [
            transforms.Resize(input_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
        ]
    )
    input_tensor = preprocess(input_image)
    input_batch = input_tensor.unsqueeze(
        0
    )  # create a mini-batch as expected by the model

    # Make a prediction
    with torch.no_grad():
        output = model(input_batch)
    # Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes
    print(output[0])
    # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
    probabilities = torch.nn.functional.softmax(output[0], dim=0)
    print(probabilities)

    probabilities = probabilities.numpy()
    proba_to_text = {
        "image_path": str(image_path),
        "good": round(probabilities[0], 4),
        "porous": round(probabilities[1], 4),
        "bulging": round(probabilities[2], 4),
    }
    print(proba_to_text)

    # If an ouput path is specified:
    if not output_path is None:
        output_path = Path(output_path)
        # if the file output is not existing, create an empty one with the header
        if not output_path.is_file():
            with open(output_path, "w", newline="") as write_csv:
                csv_writer = writer(write_csv)
                csv_writer.writerow(["image_path", "good", "porous", "bulging"])

        with open(output_path, "a+", newline="") as write_csv:
            csv_writer = writer(write_csv)
            csv_writer.writerow(
                [
                    proba_to_text["image_path"],
                    proba_to_text["good"],
                    proba_to_text["porous"],
                    proba_to_text["bulging"],
                ]
            )

    return proba_to_text

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Will classify one image.")
    parser.add_argument(
        "-i", "--input_img", type=Path, help="Path to the image to load"
    )
    parser.add_argument(
        "-o",
        "--output",
        type=str,
        help="If not none, the prediction will be added to the targeted csv",
    )
    args = parser.parse_args()

    # Create the torch model
    model = create_pretrain_model()

    # Classify the image
    classify_an_image(model, args.input_img, args.output)


l=[]
intnum=int(input("enter integar number:"))

   #l1=[intnum]

n=intnum

for i in range(intnum) :
    d=n//2
    by=str(n%2)
    l.append(by)

             #l1.append(d)

    n=n//2

             #  if n==0:
              #     break

l.reverse()
           #print(l1)
           #print(l)

print("converts a decimal number into its equivalent binary number:","".join(l))


import json
import ruamel.yaml as yaml
import logging
from lmanage.utils import logger_creation as log_color

class Yaml:
    def __init__(self, yaml_path):
        self.yaml_path = yaml_path
        with open(self.yaml_path, 'r') as file:
            self.parsed_yaml = yaml.load(file, Loader=yaml.BaseLoader)

    def get_metadata(self, type):
        if type == 'folder_metadata':
            metadata = self.__get_folder_metadata()
        elif type == 'permission_set_metadata':
            metadata = self.__get_permission_metadata()
        elif type == 'model_set_metadata':
            metadata = self.__get_model_set_metadata()
        elif type == 'role_metadata':
            metadata = self.__get_role_metadata()
        elif type == 'user_attribute_metadata':
            metadata = self.__get_user_attribute_metadata()
        elif type == 'look_metadata':
            metadata = self.__get_look_metadata()
        elif type == 'dashboard_metadata':
            metadata = self.__get_dashboard_metadata()
        elif type == 'board_metadata':
            metadata = self.__get_board_metadata()

        if not metadata:
            print('')
            # log f'No {type} specified. Please check your yaml file at {self.yaml_path}.')

        return metadata

    def __get_permission_metadata(self):
        response = []
        for objects in self.parsed_yaml:
            if 'permissions' in list(objects.keys()):
                response.append(objects)
        response = [r for r in response if r.get('name') != 'Admin']
        return response

    def __get_model_set_metadata(self):
        response = []
        for objects in self.parsed_yaml:
            if 'models' in list(objects.keys()):
                response.append(objects)
        return response

    def __get_role_metadata(self):
        response = []
        for objects in self.parsed_yaml:
            if 'permission_set' and 'model_set' in list(objects.keys()):
                response.append(objects)
        response = [r for r in response if r.get('name') != 'Admin']
        return response

    def __get_folder_metadata(self):
        response = []
        for objects in self.parsed_yaml:
            if 'content_metadata_id' in list(objects.keys()):
                response.append(objects)
        return response

    def __get_user_attribute_metadata(self):
        response = []
        for objects in self.parsed_yaml:
            if 'hidden_value' in list(objects.keys()):
                response.append(objects)
        return response

    def __get_look_metadata(self):
        response = []
        for objects in self.parsed_yaml:
            if 'query_obj' in list(objects.keys()):
                response.append(objects)
        return response

    def __get_dashboard_metadata(self):
        response = []
        for objects in self.parsed_yaml:
            if 'lookml' in list(objects.keys()):
                response.append(objects)
        return response

    def __get_board_metadata(self):
        response = []
        for objects in self.parsed_yaml:
            if 'board_sections' in list(objects.keys()):
                response.append(objects)
        return response


#!/usr/bin/env python3s

import time
import datetime
from plyer import notification

first_Time = True

if __name__ == '__main__':
    while(True):
        if first_Time:
            print('\007')
            notification.notify(
                title = 'Hello Dear, have a nice day!',
                message = 'Break at ' + (datetime.datetime.now() + datetime.timedelta(minutes=30)).strftime('%H:%M:%S'),
                #displaying time
                timeout = 2
            )
            first_Time = False
        else:
            print('\007')
            notification.notify(
                title = 'GET BACK TO WORK',
                message = 'Next break at ' + (datetime.datetime.now() + datetime.timedelta(minutes=30)).strftime('%H:%M:%S'),
                #displaying time
                timeout = 2
            )
        # sleep for 30 minutes
        time.sleep(60 * 30)

        # \007 -> beep sound
        print('\007')
        notification.notify(
            title = 'TAKE A BREAK',
            message = 'Be back at ' + (datetime.datetime.now() + datetime.timedelta(minutes=5)).strftime('%H:%M:%S'),
            #displaying time
            timeout = 2
        )
        # sleep for 5 minutes
        time.sleep(60 * 5)


import threading
from datetime import timedelta

from django.utils import timezone
from django.core.management.base import BaseCommand, CommandError
from django.db.utils import ProgrammingError

from buckets.utils import BucketFileManagement, delete_table_for_model_class
from buckets.models import Archive
from utils.oss import HarborObject

class Command(BaseCommand):
    """
    清理bucket命令，清理满足彻底删除条件的对象和目录
    """
    pool_sem = threading.Semaphore(1000)  # 定义最多同时启用多少个线程

    help = 'Really delete objects and directories that have been deleted from a bucket'
    _clear_datetime = None

    def add_arguments(self, parser):
        parser.add_argument(
            '--bucket-name', default=None, dest='bucket-name',
            help='Name of bucket have been deleted will be clearing,',
        )

        parser.add_argument(
            '--days-ago', default='30', dest='days_ago', type=int,
            help='Clear objects and directories that have been deleted more than days ago.',
        )
        parser.add_argument(
            # 当命令行有此参数时取值const, 否则取值default
            '--all-deleted', default=None, nargs='?', dest='all_deleted', const=True,
            help='All buckets that have been deleted will be clearing.',
        )

        parser.add_argument(
            '--multithreading', default=None, nargs='?', dest='multithreading', const=True,
            help='use multithreading mode.',
        )

    def handle(self, *args, **options):
        multithreading = options.get('multithreading')
        if multithreading:
            self.stdout.write(self.style.NOTICE('work mode in multithreading'))
        else:
            self.stdout.write(self.style.NOTICE('work mode in one thread'))

        days_ago = options.get('days_ago', 30)
        try:
            days_ago = int(days_ago)
            if days_ago < 0:
                days_ago = 0
        except Exception as e:
            raise CommandError(f"Clearing buckets cancelled. invalid 'days-ago', {str(e)}")

        self._clear_datetime = timezone.now() - timedelta(days=days_ago)

        buckets = self.get_buckets(**options)

        if input('Are you sure you want to do this?\n\n' + "Type 'yes' to continue, or 'no' to cancel: ") != 'yes':
            raise CommandError("Clearing buckets cancelled.")

        multithreading = options.get('multithreading')
        if multithreading:
            self.clear_buckets_multithreading(buckets)
        else:
            self.clear_buckets(buckets)

    def get_buckets(self, **options):
        """
        获取给定的bucket或所有bucket
        :param options:
        :return:
        """
        bucket_name = options['bucket-name']
        all_deleted = options['all_deleted']

        # 指定名字的桶
        if bucket_name:
            self.stdout.write(self.style.NOTICE('Will clear all buckets named {0}'.format(bucket_name)))
            return Archive.objects.filter(name=bucket_name, type=Archive.TYPE_COMMON,
                                          archive_time__lt=self._clear_datetime).all()

        # 全部已删除归档的桶
        if all_deleted:
            self.stdout.write(self.style.NOTICE('Will clear all buckets that have been softly deleted '))
            return Archive.objects.filter(type=Archive.TYPE_COMMON, archive_time__lt=self._clear_datetime).all()

        # 未给出参数
        if not bucket_name:
            bucket_name = input('Please input a bucket name:')

        self.stdout.write(self.style.NOTICE('Will clear all buckets named {0}'.format(bucket_name)))
        return Archive.objects.filter(name=bucket_name, type=Archive.TYPE_COMMON).all()

    def get_objs_and_dirs(self, model_class, num=100):
        """
        获取对象,默认最多返回1000条

        :param model_class: 对象和目录的模型类
        :param num: 获取数量
        :return:
        """
        try:
            objs = model_class.objects.filter(fod=True).all()[:num]
        except Exception as e:
            self.stdout.write(self.style.ERROR('Error when clearing bucket table named {0},'.format(
                model_class.Meta.db_table) + str(e)))
            return None

        return objs

    def is_meet_delete_time(self, bucket):
        """
        归档的桶是否满足删除时间要求，即是否可以清理

        :param bucket: Archive()
        :return:
            True    # 满足
            False   # 不满足
        """
        archive_time = bucket.archive_time.replace(tzinfo=None)

        if timezone.is_aware(self._clear_datetime):
            if not timezone.is_aware(archive_time):
                archive_time = timezone.make_aware(archive_time)
        else:
            if not timezone.is_naive(archive_time):
                archive_time = timezone.make_naive(archive_time)

        if archive_time < self._clear_datetime:
            return True

        return False

    def clear_one_bucket(self, bucket, in_thread: bool = True):
        """
        清除一个bucket中满足删除条件的对象和目录

        :param bucket: Archive()
        :param in_thread: 是否是线程
        :return:
        """
        self.stdout.write('Now clearing bucket named {0}'.format(bucket.name))
        table_name = bucket.get_bucket_table_name()
        model_class = BucketFileManagement(collection_name=table_name).get_obj_model_class()

        # 已删除归档的桶不满足删除时间条件，直接返回不清理
        if not self.is_meet_delete_time(bucket):
            return

        pool_name = bucket.get_pool_name()
        try:
            while True:
                ho = HarborObject(pool_name=pool_name, obj_id='')
                objs = self.get_objs_and_dirs(model_class=model_class)
                if objs is None or len(objs) <= 0:
                    break

                for obj in objs:
                    if obj.is_file():
                        obj_key = obj.get_obj_key(bucket.id)
                        ho.reset_obj_id_and_size(obj_id=obj_key, obj_size=obj.si)
                        ok, err = ho.delete(obj_size=obj.si)
                        if ok:
                            obj.delete()
                        else:
                            self.stdout.write(self.style.WARNING(
                                f"Failed to deleted a object from ceph:" + err))
                    else:
                        obj.delete()

                self.stdout.write(self.style.WARNING(
                    f"Success deleted {objs.count()} objects from bucket {bucket.name}."))

            # 如果bucket对应表没有对象了，删除bucket和表
            if model_class.objects.filter(fod=True).count() == 0:
                if delete_table_for_model_class(model_class):
                    bucket.delete()
                    self.stdout.write(self.style.WARNING(f"deleted bucket and it's table:{bucket.name}"))
                else:
                    self.stdout.write(self.style.ERROR(f'deleted bucket table error:{bucket.name}'))

            self.stdout.write(self.style.SUCCESS('Clearing bucket named {0} is completed'.format(bucket.name)))
        except (ProgrammingError, Exception) as e:
            if e.args[0] == 1146:   # table not exists
                bucket.delete()
                self.stdout.write(self.style.WARNING(f"only deleted bucket({bucket.name}),{e}"))
            else:
                self.stdout.write(self.style.ERROR(f'deleted bucket({bucket.name}) table error: {e}'))

        if in_thread:
            self.pool_sem.release()     # 可用线程数+1

    def clear_buckets_multithreading(self, buckets):
        """
        多线程清理bucket
        :param buckets:
        :return: None
        """
        for bucket in buckets:
            if self.pool_sem.acquire():     # 可用线程数-1，控制线程数量，当前正在运行线程数量达到上限会阻塞等待
                worker = threading.Thread(target=self.clear_one_bucket, kwargs={'bucket': bucket})
                worker.start()

        # 等待所有线程结束
        while True:
            c = threading.active_count()
            if c <= 1:
                break

        self.stdout.write(self.style.SUCCESS('Successfully clear {0} buckets'.format(buckets.count())))

    def clear_buckets(self, buckets):
        for bucket in buckets:
            try:
                self.clear_one_bucket(bucket, in_thread=False)
            except Exception as e:
                self.stdout.write(self.style.ERROR(f'deleted bucket({bucket.name}) table error: {e}'))

        self.stdout.write(self.style.SUCCESS('Successfully clear {0} buckets'.format(buckets.count())))


from ..data_utils import cal_knn
from ..data_utils import sparse_voxel
import numpy as np

class KnnQuery(object):
    def __init__(self, points, knn_module, set_k, GPU_id=None):
        knn_module = getattr(cal_knn, knn_module)  # importlib.import_module('.'.join(['cal_knn', self.knn_module]))
        self.knn_searcher = knn_module(set_k=set_k, GPU_id=GPU_id)
        self.knn_searcher.train(points)

    def search(self, point, k):
        if len(point.shape) < 2:
            point = np.expand_dims(point, axis=0)
        return self.knn_searcher.search(point, k)

class BlockQuery(object):
    def __init__(self, points, voxel_size, block_size, ignore_bounds=False):
        self.sparse_voxel = sparse_voxel.SparseVoxel(points=points,
                                                     voxel_size=voxel_size,)
        self.sparse_voxel.initialize()
        self.block_size = np.array(block_size)
        self.ignored_fine_bound = ignore_bounds
        self.search_dim_x, self.search_dim_y = self.get_block_search_dim()
        #logger.info('Getting search dictionary')

    def get_block_search_dim(self):
        search_dim = (self.block_size - self.sparse_voxel.voxel_size[:2]) / \
                     self.sparse_voxel.voxel_size[:2] / 2.0
        new_search_dim = np.ceil(search_dim).astype(np.int)
        if np.sum(new_search_dim != search_dim) == 0:
            self.ignored_fine_bound = True
        return [-1 * new_search_dim[0], new_search_dim[0] + 1], \
               [-1 * new_search_dim[1], new_search_dim[1] + 1]

    def search_candidates(self, center_pt):
        center_vox = self.sparse_voxel.voxelize(center_pt)
        s1 = slice(center_vox[0] + self.search_dim_x[0], center_vox[0] + self.search_dim_x[1])
        s2 = slice(center_vox[1] + self.search_dim_y[0], center_vox[1] + self.search_dim_y[1])

        return self.sparse_voxel[s1, s2].toindex()

    def search(self, center, points):
        candidate_index = self.search_candidates(center)

        if not self.ignored_fine_bound:
            candidate_points = points[candidate_index]
            mask_x = np.logical_and(candidate_points[:, 0] <= center[0] + self.block_size[0] / 2,
                                    candidate_points[:, 0] >= center[0] - self.block_size[0] / 2)
            mask_y = np.logical_and(candidate_points[:, 1] <= center[1] + self.block_size[1] / 2,
                                    candidate_points[:, 1] >= center[1] - self.block_size[1] / 2)
            return candidate_index[np.logical_and(mask_x, mask_y)]
        else:
            return candidate_index

if __name__ == "__main__":
    import open3d
    import sys
    import time
    pnum = 1000000
    s = np.array([1,1,1])
    knn_module = 'SkNN'
    #pts = gen_point_cloud(high=1000, low=1, center_num=20, size=pnum, scale=0.3)
    pts = np.asarray(open3d.io.read_point_cloud("/home/lixk/data/dataset/nus3d_pcd/FOE.pcd").points)
    sp_i = np.random.randint(1, pts.shape[0], 1000)
    knn = KnnQuery(pts, knn_module, 2048)
    tic = time.time()
    for i in range(100):
        _ = knn.search(pts[sp_i[i]], 2048)
    toc = time.time()
    print((toc - tic) / 100)


import sqlite3

class Data:
    def __init__(self, name):
        self.conn = sqlite3.connect('name.db')
        self.cursor = self.conn.cursor()
        self.cursor.execute('''CREATE TABLE IF NOT EXISTS tab_1 (id INTEGER PRIMARY KEY AUTOINCREMENT, col_1 INTEGER)''')

    def process_arguments(self, *args):
        if len(args) == 1:
            first_arg = args[0]
            value = 3
            self.cursor.execute('''INSERT INTO tab_1 (col_1) VALUES (?)''', (value, ))
            self.conn.commit()
        elif len(args) == 2:
            second_arg = args[1]
            if isinstance(second_arg, int):
                self.cursor.execute('''DELETE FROM tab_1 WHERE id = 1''') # удаляет 1 строку, limit выводит указанное кол-во строк из таблицы
                self.conn.commit()
        elif len(args) == 3:
            third_arg = args[2]
            if isinstance(third_arg, int):
                self.cursor.execute('''UPDATE tab_1 SET col_1 = (?) WHERE id = 3''', (77,))
                self.conn.commit()

    def output(self, *args):
        self.cursor.execute(''' SELECT col_1 FROM tab_1''')
        oo = self.cursor.fetchall()
        return oo

data = Data("name.db")

for i in range(3):
    data.process_arguments(1) # добавляет три записи  в базу данных
print(data.output())

data.process_arguments(1, 2, "string")   # Не обновит запись, так как третий аргумент не является числом
print(data.output())

data.process_arguments(1, "string", 3) # перезапишет третью запись (3 -> 77)
print(data.output())

data.process_arguments(1, 2)      # Удалит первую запись
print(data.output())


#!/usr/bin/env python3

import os
import signal
from threading import Thread
from datetime import datetime
from time import sleep
from subprocess import Popen
import shlex

from bottle import run, get, request, HTTPResponse

def fetch_unix_time():
    return int(datetime.now().timestamp())

def run_cmd(cmd, start_time, duration):
    unix_time = fetch_unix_time()
    sleep(start_time - unix_time)

    print(cmd, flush=True)
    cmd = "timeout {} {}".format(duration, cmd)

    p = Popen(cmd, shell=True)
    p.wait()

@get('/trigger')
def trigger():
    group_id = request.query.group_id
    start_time = request.query.start_time
    duration = request.query.duration
    hashtag = request.query.hashtag
    hashtag = hashtag.replace('#', '\#')

    start_time = fetch_unix_time()
    print(start_time, flush=True)

    cmd = 'php filter-oauth.php {} {}'.format(group_id, hashtag)
    Thread(target=run_cmd, args=(cmd, start_time, duration)).start()

    res = HTTPResponse(200)
    res.body = {}
    res.body['start_time'] = start_time

    return res

if __name__ == '__main__':
    run(host='0.0.0.0', port=80, debug=True, reloader=False)


from __future__ import annotations
from abc import ABC, abstractmethod
from typing import List, Tuple
from argparse import ArgumentParser
from rstcloth import RstCloth
import os
from io import StringIO

from ..east import EnrichedAST
from ..astline import ASTLine

class Component(ABC):
    """
    Abstract class representing a component of the documentation.

    :param builder: The builder object that is creating the documentation.
    :param parameters: A dictionary of parameters for the component.
    """

    parse_group_description = "DEFAULT PARSER GROUP DESCRIPTION"
    name = "DEFAULT_NAME"

    def __init__(self, builder:'Builder', parameters) -> None:
        self.builder = builder
        self._sio = StringIO()
        self.document = RstCloth(self._sio)

        self.parameters = parameters
        self.check_parameters()

    @classmethod
    def cmdline_documentation(cls, parser: ArgumentParser) -> ArgumentParser:
        """
        Add command line arguments for this component to the given ArgumentParser.

        :param parser: The ArgumentParser to add the arguments to.
        :return: The created argument group.
        """
        parser_group = parser.add_argument_group(
            cls.parse_group_description
        )

        parser_group.add_argument(
            f'--{cls.name}.exclude',
            action="store_true",
            help='(flag) component will be excluded from the documentation'
        )

        return parser_group

    def check_parameters(self):
        """
        Check if the given parameters are valid and set default values if necessary.
        """
        if not self.name in self.parameters:
            self.parameters[self.name] = {}

        if not 'exclude' in self.parameters[self.name]:
            self.parameters[self.name]['exclude'] = False

    @abstractmethod
    def build_rst_file(self) -> None:
        """
        Build the reStructuredText file for this component.
        """
        pass

    def write_rst_file(self):
        """
        Write the reStructuredText file to disk.
        """
        if self._sio.getvalue():  # Things have been written
            with open(os.path.join(self.parameters['doc_dir'], self.name + ".rst"), 'w') as file:
                file.write(self._sio.getvalue())

    def _get_name(self, astline: ASTLine):
        """
        Get the name of the given AST line, taking into account the presence of multiple EASTs.

        :param astline: The AST line to get the name for.
        :return: The name of the AST line.
        """
        if len(self.builder.easts) > 1:
            return astline.prefix + astline.identifier

        return astline.identifier

    def _get_location(self, astline: ASTLine):
        """
        Get the location of the given AST line in the source code.

        :param astline: The AST line to get the location for.
        :return: The location of the AST line.
        """
        if astline.location.begin.line != astline.location.end.line:
            return f"{astline.location.begin.line}-{astline.location.end.line}"
        else:
            return f"{astline.location.begin.line}"

    def _include_code(self, astline: ASTLine):
        """
        Include the code of the given AST line in the rst file.

        :param astline: The AST line to include the code for.
        """
        self.document.directive('literalinclude',
                                arg=f'/{astline.location.begin.filename}',
                                fields=[('language','prolog'),
                                        ('lines' , f'{self._get_location(astline)}')])

        self.document.newline()

    def _include_comments(self, astline: ASTLine):
        """
        Helper function for including comments of an ASTLine in the documentation.

        :param astline: The ASTLine object whose comments are to be included in the documentation.
        """
        if astline.comments:
            for comment in astline.comments:
                self.document.directive('note',content=f"↳ {comment}")

            self.document.newline()

    def _include_source(self, east: EnrichedAST):
        """
        Include the source code for the current file being processed in the output documentation.

        :param east: An instance of the EnrichedAST class.
        """
        self.document.directive('literalinclude',
                                arg=f'/{east.filename}',
                                fields=[('language','prolog'),
                                        ('linenos' , '')])
        self.document.newline()

class Index(Component):
    name = 'index'

    @classmethod
    def cmdline_documentation(cls, parser: ArgumentParser) -> ArgumentParser:
        return

    def __init__(self, builder, parameters) -> None:
        super().__init__(builder, parameters)

    def check_parameters(self):
        super().check_parameters()

    def _generate_toctree(self):
        self.document._add('.. toctree::')
        self.document._add(' :maxdepth: 2')
        self.document.newline()

        for c in self.builder.components:
            if c.name != self.name:
                self.document._add(" " + c.name)

    def build_rst_file(self) -> None:
        self.document.title(self.parameters['project_name'])
        self.document.newline()
        self.document.content(self.parameters['description'])
        self.document.newline(3)
        self.document.h1("Content")
        self.document.newline()
        self._generate_toctree()

class Source(Component):
    name = "source"
    parse_group_description = "Source files parameters"

    @classmethod
    def cmdline_documentation(cls, parser: ArgumentParser) -> ArgumentParser:
        parser_group = super().cmdline_documentation(parser)
        parser_group.add_argument(
            f'--{cls.name}.exclude-file',
            action="store",
            nargs= '*',
            help='source file(s) that will be excluded from source documentation section'
        )

    def __init__(self, builder, parameters) -> None:
        super().__init__(builder, parameters)

    def check_parameters(self):
        super().check_parameters()
        if not 'exclude_file' in self.parameters[self.name]:
            self.parameters[self.name]['exclude_file']= False

    def build_rst_file(self) -> None:
        self.document.title('Source Code')
        self.document.newline()

        for east in self.builder.easts:
            self.document.h2(
                east.filename[self.parameters['src_dir'].rindex('/')+1:])
            self.document.newline()
            self._include_source(east)
            self.document.newline()


from django.db import models
from django.utils.translation import ugettext as _
from Apps.Asset.Property_asset.models import *
from Apps.Hrm.Master_General.models import Department
from Apps.Asset.Master.models import *
from Apps.Asset.Request.models import *
from datetime import datetime
from tinymce.models import HTMLField

class Header_request_asset(models.Model):
	no_reg = models.CharField(verbose_name='No. Reg Pengadaan', max_length=25, editable=False)
	department = models.ForeignKey(Department, verbose_name=_('Nama Department '))
	ra_add_date = models.DateField(verbose_name=_('Tanggal '),auto_now_add=True)
	ra_lock = models.BooleanField(verbose_name=_('Persetujuan ?'), default=False)
	ra_lock_date = models.DateField(verbose_name=_('Batas Persetujuan '))
	asset_staff_review = models.TextField(verbose_name=_('Asset Staff Review '), blank=True, help_text='Masukkan Kriteria Pengadaan disini.')

	class Meta:
		verbose_name_plural="Header Request Asset"
		verbose_name="Header_request_asset"

	def incstring(self):
		try:
			data = Header_request_asset.objects.all()
			jml = data.count()
		except:
			jml=0
		pass
		no = 0
		if jml == 0:
			no = 0
		else:
			for d in data:
				split = str(d.no_reg).split('/')
				no = int(split[3])
		num = no + 1
		cstring = str(num)
		return cstring

	def inclen(self):
		leng = len(self.incstring())
		return leng

	def no_rek(self):
		date = datetime.now()
		now = date.strftime("%m")
		nowyear = date.strftime("%Y")
		intnow = int(now)
		intyear = int(nowyear)
		strnow = str(intnow)
		if len(strnow) < 2 :
			strnow = '0%(strnow)s' % {'strnow' : strnow}
		nol = 5 - self.inclen()
		if nol == 1: num = "0"
		elif nol == 2: num = "00"
		elif nol == 3: num = "000"
		elif nol == 4: num = "0000"
		number = num + self.incstring()
		return 'PA/%(year)s/%(month)s/%(unik)s' % {'year': intyear, 'month': strnow, 'unik': number}

	def save(self, force_insert=True, force_update=True, using=None, update_fields=None):
		if self.no_reg =='':
			self.no_reg = self.no_rek()
		else:
			self.no_reg = self.no_reg
		super(Header_request_asset, self).save()

	def __unicode__(self):
		return u'%s' % self.no_reg

class Data_request_asset(models.Model):
	header_request = models.ForeignKey(Header_request_asset, verbose_name=_('Header Request Asset  '))
#	no_item = models.IntegerField(verbose_name=_('No.Item  '))
	request = models.OneToOneField(Data_user_request, related_name=_('Data Request '), blank=True, null=True, help_text='Pilih Request Pengadaan dari Permintaan Pengadaan')
	#request_change = models.OneToOneField(Data_user_request, related_name=_('Data Request '), blank=True, null=True ,help_text='Pilih Request Pengadaan dari Permintaan Pengadaan')
	unit_of_measure = models.ForeignKey(Unit_of_measure_asset, verbose_name=_('Satuan '), blank=True, null=True)
	ra_used = models.DateField(verbose_name=_('Waktu Penggunaan '), blank=True, null=True)
	ra_amount = models.IntegerField(verbose_name=_('Jumlah '), blank=True, null=True)
	description = HTMLField(verbose_name=_('Deskripsi'), blank=True, help_text='Deskripsi Pengadaan ')
#	currency = models.ForeignKey(Asset_currency, verbose_name=_('Mata Uang '))
#	ra_unit_price = models.DecimalField(verbose_name=_('Harga Per Unit '),decimal_places=2, max_digits=10)
#	asset = models.ForeignKey(Ms_asset, verbose_name=_('Nama Asset'))

	class Meta:
		verbose_name_plural="Data Request Asset"
		verbose_name="Data_request_asset"

#	def desc(self):
#		des = Data_user_request.objects.get(id=self.request.id)
#		return des.description
#	desc.short_description='Deskripsi'

	def descriptionx(self):
		return '%s' % self.description
	descriptionx.allow_tags = True
	descriptionx.short_description = 'Asset Staff Review'

	def __unicode__(self):
		return  u'%s' % self.header_request


#!/usr/bin/python

fib = 1,2
max = 4e6

sum = 0

while True:
    if fib[0] > max:
        break

    if (fib[0] % 2) == 0:
        sum += fib[0]

    fib = fib[1], fib[0]+fib[1]

print(sum)


import pandas as pd
import matplotlib.pyplot as plt
from DataReader import data
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from targetEncoding import numD, cat

#traemos las variables categoricas con los nuevos valores numericos
numData = pd.DataFrame(numD)
for i in cat:
    data[i] = numData[i]
features = ['ID_USER','genero', 'monto', 'fecha', 'hora', 'dispositivo',
       'establecimiento', 'ciudad', 'tipo_tc', 'linea_tc', 'interes_tc',
       'status_txn', 'is_prime', 'dcto', 'cashback']
x = data.loc[:, features].values
y = data.loc[:,['fraude']].values
x = StandardScaler().fit_transform(x)

pca = PCA(n_components=2)
principalComponents = pca.fit_transform(x)
principalDf = pd.DataFrame(data = principalComponents, columns = ['principal component 1', 'principal component 2'])
finalDf = pd.concat([principalDf, data[['fraude']]], axis = 1)

fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('Componente  principal 1', fontsize = 15)
ax.set_ylabel('Componente principal 2', fontsize = 15)
ax.set_title(' Visualización de la data usando 2 component PCA ', fontsize = 20)
targets = [1, 0]
colors = ['black','y']
for target, color in zip(targets, colors):
    indicesToKeep = finalDf['fraude'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
               , finalDf.loc[indicesToKeep, 'principal component 2']
               , c = color
               , s = 50)
ax.legend(targets)
ax.grid()
plt.show()


#!/usr/bin/env python3
"""Parse raw CESAR output for one query, get codon table."""
import argparse
import sys
from copy import deepcopy
from version import __version__

try:  # for robustness
    from modules.common import eprint
    from modules.common import die
except ImportError:
    from common import eprint
    from common import die

# constants
STOPS = {"TAG", "TGA", "TAA"}
# nucleotide %ID and blosum thresholds
# for exons classification
HQ_PID = 75
HQ_BLOSUM = 65

AB_INCL_PID = 25
AB_INCL_BLOSUM = 25

C_INCL_PID = 65
C_INCL_BLOSUM = 50

A_T_PID = 65
A_T_BLOSUM = 40

LO_T_PID = 45
LO_T_BLOSUM = 25

def parse_cesar_out(target, query, v=False):
    """Convert raw CESAR output into a codon table."""
    # CESAR output structure notes:
    # >reference
    #       ATGGCAa            aaGTC>>>CTGGGGAtt          cCCC...
    # >query
    # atcagcATGGGAAagtacgtagcgtAAGTCCCCCTACCGATAaggatcgtgtCCCC...
    # This is a pairwise alignment between the reference and the query
    # CESAR aligns reference exon on the query sequence
    # In reference:
    #    space means -> no exon aligns, either an intron or intergenic region
    #    uppercase letter -> complete codon
    #    lowercase letter -> codon split between two exons
    #    > sign -> marks intron deletion
    #    - -> insertion in query
    # In query:
    #    uppercase letter -> coding sequence (where reference exons align)
    #    lowercase letter -> non-coding sequence (introns, UTR, intergenic regions)
    #    - -> deletion in query
    # CESAR accepts intact coding reference exons as input

    # CESAR output parsing assumes that target sequence starts with a space
    # but in 0.1% cases it is not true, so for those cases I'll add 1 space:
    target = " " + target
    query = "-" + query
    # get codons number
    letters_num = len([c for c in target if c.isalpha()])
    codons_num = letters_num // 3
    eprint(f"Expecting {codons_num} target codons") if v else None
    codon_data = []
    # this "struct" contains codon information
    next_elem_box = {
        "ref_codon": "",  # keep reference codon; str
        "que_codon": "",  # keep query codon; str
        "q_exon_num": 0,  # exon number of codon, in query; int
        "t_exon_num": 0,  # exon num in target; int
        "split_": 0,  # 0 - not split
        "que_coords": [],  # relative coordinates in query
    }
    for _ in range(codons_num):
        # fill codon table with struct-like objects
        codon_data.append(deepcopy(next_elem_box))

    # initiate some values before parsing
    was_space = False  # previous char in reference is space
    intr_del_switch = False  # previous char in reference was >
    exon_num = -1  # (in query) to start with 0 for consistence
    t_exon_num = -1  # exon num in reference, initial value
    codon_num = 0  # codon num, start with 0
    codon_counter = 0  # counter for codons
    is_split_now = False  # flag means that we are reading a split codon
    q_coord = -1  # coordinate in query (relative)

    for t, q in zip(target, query):
        # read a pair of characters, one from ref, another from query
        # intron deletion-related conditions
        if t == ">":
            # going through an intron deletion
            if not intr_del_switch:
                # first char in this deletion
                # exon number in reference changes, in query -> does not
                t_exon_num += 1
            # flag ON -> not to increase ref exon number in this >>>> run
            intr_del_switch = True
            continue
        else:
            # not the intron deletion: put intron_del flag off
            intr_del_switch = False

        if q != "-":
            # not a gap in the query: next letter -> inc coordinate
            q_coord += 1

        if t == " " and not was_space:
            # if space -> intron
            # new intron starts
            was_space = True
            exon_num += 1  # exon just ended in both ref and query
            t_exon_num += 1  # so increment the numbers
            # fill the codons
            curr_codon = codon_data[codon_num]
            curr_codon_letters = [c for c in curr_codon["ref_codon"] if c.isalpha()]
            curr_codon_gaps = [c for c in curr_codon["ref_codon"] if c == "-"]

            if len(curr_codon_gaps) > 0 and len(curr_codon_letters) == 0:
                # empty codon terminates here
                codon_counter = 0
                codons_num += 1
                codon_num += 1
                codon_data.append(deepcopy(next_elem_box))

            if is_split_now:
                # this is a split codon: compute split_ value
                codon_data[codon_num]["split_"] = len(
                    codon_data[codon_num]["ref_codon"]
                )
            continue

        elif t == " ":
            # space continues (was_space == True)
            continue
        else:  # not a space, intron flag off
            was_space = False

        # t is not a space if we are here
        # add codon letters per codon
        codon_data[codon_num]["ref_codon"] += t
        codon_data[codon_num]["que_codon"] += q
        # update exon numbers
        codon_data[codon_num]["q_exon_num"] = exon_num
        codon_data[codon_num]["t_exon_num"] = t_exon_num

        if q != "-":  # it not gap -> a letter -> a valid query coordinate
            codon_data[codon_num]["que_coords"].append(q_coord)

        # split/ non split flags
        if t.isupper():
            # increase number of items in codon
            is_split_now = False
        elif t.islower():
            # we are going through a split codon
            is_split_now = True
        elif t == "-":
            # change nothing
            continue
        else:  # should never happen, unexpected character in the reference seq
            eprint(f"Broken codon:{str(codon_data[codon_num])}") if v else None
            die(f"CESAR output is corrupted - char {t} in query seq", 1)
        # not a gap -> switch codon counters
        codon_counter += 1
        if codon_counter == 3:
            # we got 3 letters in the reference codon: goto next one
            codon_counter = 0
            codon_num += 1
        if codon_num == codons_num:
            # must be no codons anymore
            break

    for elem in codon_data:
        # show codon table if required
        eprint(str(elem)) if v else None

    # check and finalize
    for i in range(codons_num):
        i_codon = codon_data[i]
        is_first = i == 0
        is_last = i == codons_num - 1
        t_codon_seq = i_codon["ref_codon"].replace("-", "")
        # sanity checks, write error messages if something is wrong with input
        if t_codon_seq.upper() != "ATG" and is_first:  # starts with ATG?
            eprint("Error! CESAR output is corrupted, target must start with ATG!")
        elif t_codon_seq.upper() not in STOPS and is_last:  # ends with STOP?
            eprint(
                "Error! CESAR output is corrupted, target must end with a stop codon!"
            )
        elif t_codon_seq.upper() in STOPS and not is_last:  # Stop in frame?
            eprint(
                "Error! CESAR output is corrupted, found in-frame STOP codon in reference!"
            )
        # all ref letters in codon must be either lower or uppercase, not a mixture
        all_hi = all(x.isupper() for x in t_codon_seq)
        all_lo = all(x.islower() for x in t_codon_seq)
        if not all_hi and not all_lo:
            eprint(f"Broken codon:{str(i_codon)}")
            eprint("Error! CESAR output is corrupted, wrong split codon mapping!")
        # make all uppercase
        codon_data[i]["ref_codon"] = codon_data[i]["ref_codon"].upper()
    return codon_data

def parse_args():
    """Parse args."""
    app = argparse.ArgumentParser()
    app.add_argument("cesar_out", help="CESAR output file, this script input")
    if len(sys.argv) < 2:
        app.print_help()
        sys.exit(0)
    args = app.parse_args()
    return args

def classify_exon(ex_class, incl, pid, blosum, v=False):
    """Decide what do we do with this exon."""
    # ex_class: how exon aligns
    # A - chain aligns exon perfectly
    # B - an exon flank (left or right) is not aligned
    # C - chain blocks don't intersect the exon
    # M - exon located outside the chain borders
    # we look at:
    # 1) exon class
    # 2) nucleotide %ID and BLOSUM scores
    # 3) was exon detected in the expected region?
    # and then classify it or say it's deleted/missing

    # A / B class branch
    if ex_class == "A" or ex_class == "B" or ex_class == "A+":
        if incl:
            # CHECK FOR A+ CLASS
            if pid > A_T_PID and blosum > A_T_BLOSUM:
                return True, "HQ"
                # ok, if class A, True, HQ -> check for A+
            else:
                return True, "AQ"
        else:
            # as well as C included
            if pid > A_T_PID and blosum > A_T_BLOSUM:
                return True, "AQ"
            elif pid > LO_T_PID and blosum > LO_T_BLOSUM:
                return True, "LQ"
            else:
                return False, "NA"
    # class C branch
    elif ex_class == "C":
        if incl:
            # the same with A/B class excluded
            if pid > A_T_PID and blosum > A_T_BLOSUM:
                return True, "AQ"
            elif pid > LO_T_PID and blosum > LO_T_BLOSUM:
                return True, "LQ"
            else:
                return False, "NA"
        else:
            # class C excluded -> we do not annotate this a priori
            return False, "NA"
    # class M branch
    else:
        # class M -> no classification actually
        return False, "NA"

def main():
    """Entry point."""
    args = parse_args()
    f = open(args.cesar_out, "r")
    lines = f.readlines()
    f.close()
    ref_seq = lines[1]
    que_seq = lines[3]
    codons_data = parse_cesar_out(ref_seq, que_seq)
    for elem in codons_data:
        print(elem)

if __name__ == "__main__":
    main()


import sys
import math
import torch
from torch import nn
from torch.nn import functional as F
from torch.nn.parameter import Parameter
from torchvision import models
#from efficientnet_pytorch import EfficientNet

import sys
sys.path.append("../input/pretrained-models/pretrained-models/pretrained-models.pytorch-master/")
from pretrainedmodels import se_resnext101_32x4d, senet154
from pretrainedmodels import inceptionresnetv2
from pretrainedmodels import pnasnet5large
from pretrainedmodels import xception

class ArcMarginProduct(nn.Module):
    r"""Implement of large margin arc distance: :
        Args:
            in_features: size of each input sample
            out_features: size of each output sample
            s: norm of input feature
            m: margin
            cos(theta + m)
        """

    def __init__(self, in_features, out_features=5004):
        super(ArcMarginProduct, self).__init__()
        self.weight = nn.Parameter(torch.FloatTensor(out_features, in_features))
        self.reset_parameters()
        # nn.init.kaiming_uniform_(self.weight)

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data.uniform_(-stdv, stdv)

    def forward(self, features):
        # x=self.head(features)
        # print(self.weight.shape)
        # self.fc1.weight=nn.Parameter(F.normalize(self.fc1.weight)).cuda()
        cosine = F.linear(F.normalize(features), F.normalize(self.weight.cuda()))
        # cosine = cosine.clamp(-1, 1)
        # self.fc1(F.normalize(x))
        # F.linear(F.normalize(x), F.normalize(self.weight.cuda()))
        return cosine

class AvgPool(nn.Module):
    def forward(self, x):
        return F.avg_pool2d(x, x.shape[2:])

class MaxPool(nn.Module):
    def forward(self, x):
        return F.max_pool2d(x, x.shape[2:])

class AdaptiveConcatPool2d(nn.Module):
    def __init__(self, sz=None):
        super().__init__()
        sz = sz or (1,1)
        self.ap = nn.AdaptiveAvgPool2d(sz)
        self.mp = nn.AdaptiveMaxPool2d(sz)
    def forward(self, x):
        return torch.cat([self.mp(x), self.ap(x)], 1)

def gem(x, p=3, eps=1e-6):
    return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1./p)

class GeM(nn.Module):
    def __init__(self, p=3, eps=1e-6):
        super(GeM,self).__init__()
        self.p = Parameter(torch.ones(1)*p)
        self.eps = eps

    def forward(self, x):
        return gem(x, p=self.p, eps=self.eps)

    def __repr__(self):
        return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist()[0]) + ', ' + 'eps=' + str(self.eps) + ')'

class CSE(nn.Module):
    def __init__(self, in_ch, r):
        super(CSE, self).__init__()

        self.linear_1 = nn.Linear(in_ch, in_ch//r)
        self.linear_2 = nn.Linear(in_ch//r, in_ch)

    def forward(self, x):
        input_x = x

        x = x.view(*(x.shape[:-2]),-1).mean(-1)
        x = F.relu(self.linear_1(x), inplace=True)
        x = self.linear_2(x)
        x = x.unsqueeze(-1).unsqueeze(-1)
        x = F.sigmoid(x)

        x = input_x * x

        return x

class SSE(nn.Module):
    def __init__(self, in_ch):
        super(SSE, self).__init__()

        self.conv = nn.Conv2d(in_ch, in_ch, kernel_size=1, stride=1)

    def forward(self, x):
        input_x = x

        x = self.conv(x)
        x = F.sigmoid(x)

        x = input_x * x

        return x

class SCSE(nn.Module):
    def __init__(self, in_ch, r=8):
        super(SCSE, self).__init__()

        self.cSE = CSE(in_ch, r)
        self.sSE = SSE(in_ch)

    def forward(self, x):
        cSE = self.cSE(x)
        sSE = self.sSE(x)

        x = cSE + sSE

        return x

class SEBlock(nn.Module):
    def __init__(self, in_ch, r=8):
        super(SEBlock, self).__init__()

        self.linear_1 = nn.Linear(in_ch, in_ch//r)
        self.linear_2 = nn.Linear(in_ch//r, in_ch)

    def forward(self, x):
        input_x = x

        x = F.relu(self.linear_1(x), inplace=True)
        x = self.linear_2(x)
        x = F.sigmoid(x)

        x = input_x * x

        return x

class Flatten(nn.Module):
    def forward(self, input):
        return input.view(input.size(0), -1)

def create_net(net_cls, pretrained: bool):
    #net = net_cls()
    #if pretrained is not None:
    #    net.load_state_dict(torch.load(pretrained))
    net = net_cls(pretrained=pretrained)
    return net

class ResNet(nn.Module):
    def __init__(self, num_classes,
                 pretrained=False, net_cls=models.resnet50):
        super().__init__()
        self.net = create_net(net_cls, pretrained=pretrained)
        self.net.avgpool = AdaptiveConcatPool2d()
        self.net.fc = nn.Sequential(
            Flatten(),
            #SEBlock(2048*2),
            nn.Dropout(),
            nn.Linear(2048*2, num_classes)
        )

    def fresh_params(self):
        return self.net.fc.parameters()

    def forward(self, x):
        return self.net(x)

class DenseNet(nn.Module):
    def __init__(self, num_classes,
                 pretrained=False, net_cls=models.densenet121):
        super().__init__()
        self.net = net_cls(pretrained=pretrained)
        self.avg_pool = AdaptiveConcatPool2d()
        self.net.classifier = nn.Sequential(
            Flatten(),
            SEBlock(1024*2),
            nn.Dropout(),
            nn.Linear(1024*2, num_classes)
        )

    def fresh_params(self):
        return self.net.classifier.parameters()

    def forward(self, x):
        out = self.net.features(x)
        out = F.relu(out, inplace=True)
        out = self.avg_pool(out)
        out = self.net.classifier(out)
        return out

class SEResNext(nn.Module):
    def __init__(self, num_classes, pretrained="imagenet", pool_type="concat"):
        super().__init__()
        self.net = se_resnext101_32x4d(pretrained=pretrained)
        if pool_type == "concat":
            self.net.avg_pool = AdaptiveConcatPool2d()
            last_channel = 2048*2
        elif pool_type == "gem":
            self.net.avg_pool = GeM()
            last_channel = 2048
        self.net.last_linear = nn.Sequential(
            Flatten(),
            SEBlock(last_channel),
            nn.Dropout(),
            nn.Linear(last_channel, num_classes)
        )

    def fresh_params(self):
        return self.net.last_linear.parameters()

    def forward(self, x):
        return self.net(x)

class Efficient(nn.Module):
    def __init__(self, num_classes, encoder='efficientnet-b7'):
        super().__init__()
        n_channels_dict = {'efficientnet-b0': 1280, 'efficientnet-b1': 1280, 'efficientnet-b2': 1408,
                           'efficientnet-b3': 1536, 'efficientnet-b4': 1792, 'efficientnet-b5': 2048,
                           'efficientnet-b6': 2304, 'efficientnet-b7': 2560}
        self.net = EfficientNet.from_pretrained(encoder)
        self.avg_pool = AdaptiveConcatPool2d()
        self.classifier = nn.Sequential(
            Flatten(),
            SEBlock(n_channels_dict[encoder] * 2),
            nn.Dropout(),
            nn.Linear(n_channels_dict[encoder] * 2, num_classes)
        )

    def forward(self, x):
        x = self.net.extract_features(x)
        x = self.avg_pool(x)
        out = self.classifier(x)

        return out

class InceptionResNetV2(nn.Module):
    def __init__(self, num_classes, pretrained="imagenet"):
        super().__init__()
        self.net = inceptionresnetv2(pretrained=pretrained)
        self.net.avgpool_1a = AdaptiveConcatPool2d()
        self.net.last_linear = nn.Sequential(
            Flatten(),
            SEBlock(1536*2),
            nn.Linear(1536*2, num_classes)
        )

    def fresh_params(self):
        return self.net.last_linear.parameters()

    def forward(self, x):
        return self.net(x)

class SENet(nn.Module):
    def __init__(self, num_classes, pretrained="senet154-c7b49a05.pth", dropout=False, arcface=False):
        super().__init__()
        self.net = senet154(pretrained=pretrained)
        self.net.last_linear = nn.Linear(2048, num_classes)

    def fresh_params(self):
        return self.net.last_linear.parameters()

    def forward(self, x):
        return self.net(x)

class SENetV2(nn.Module):
    def __init__(self, num_classes, pretrained="senet154-c7b49a05.pth", dropout=False, arcface=False):
        super().__init__()
        self.net = senet154(pretrained=pretrained)
        self.net.avg_pool = AvgPool()
        self.net.last_linear = nn.Linear(2048, num_classes)

    def fresh_params(self):
        return self.net.last_linear.parameters()

    def forward(self, x):
        return self.net(x)

class SENetV3(nn.Module):
    def __init__(self, num_classes, pretrained="senet154-c7b49a05.pth", dropout=False, arcface=False):
        super().__init__()
        self.net = senet154(pretrained=pretrained)
        self.net.avg_pool = AvgPool()
        self.net.last_linear = nn.Linear(2048, num_classes)

    def fresh_params(self):
        return self.net.last_linear.parameters()

    def forward(self, x):
        return self.net(x)

class SENetV4(nn.Module):
    def __init__(self, num_classes, pretrained="senet154-c7b49a05.pth", dropout=False, arcface=False):
        super().__init__()
        self.net = senet154(pretrained=pretrained)
        self.net.avg_pool = AdaptiveConcatPool2d()
        self.net.last_linear = nn.Sequential(
            Flatten(),
            SEBlock(2048*2),
            nn.Linear(2048*2, num_classes)
        )

    def fresh_params(self):
        return self.net.last_linear.parameters()

    def forward(self, x):
        return self.net(x)

class PNASNet5Large(nn.Module):
    def __init__(self, num_classes, pretrained="pnasnet5large-bf079911.pth", dropout=False, arcface=False):
        super().__init__()
        self.net = pnasnet5large(pretrained=pretrained)
        self.net.avg_pool = AdaptiveConcatPool2d()
        self.net.last_linear = nn.Sequential(
            Flatten(),
            SEBlock(4320 * 2),
            nn.Linear(4320 * 2, num_classes)
        )

    def fresh_params(self):
        return self.net.last_linear.parameters()

    def forward(self, x):
        return self.net(x)

class Xception(nn.Module):
    def __init__(self, num_classes, pretrained="xception-43020ad28.pth"):
        super().__init__()
        self.net = xception(pretrained=pretrained)
        self.avg_pool = AdaptiveConcatPool2d()
        self.net.last_linear = nn.Sequential(
            Flatten(),
            SEBlock(2048 * 2),
            nn.Linear(2048 * 2, num_classes)
        )

    def fresh_params(self):
        return self.net.last_linear.parameters()

    def forward(self, x):
        out = self.net.features(x)
        out = F.relu(out, inplace=True)
        out = self.avg_pool(out)
        out = self.net.last_linear(out)
        return out


from abc import abstractmethod

from django.conf import settings
from django.db import models
from django.urls import reverse
from mdeditor.fields import MDTextField
from uuslug import slugify

# Create your models here.
class BaseModel(models.Model):
    id = models.AutoField(primary_key=True)
    created_time = models.DateTimeField('创建时间', auto_now_add=True)
    last_mod_time = models.DateTimeField('修改时间', auto_now=True)

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        # is_update_views = isinstance(
        #     self,
        #     Article) and 'update_fields' in kwargs and kwargs['update_fields'] == ['views']
        # if is_update_views:
        #     Article.objects.filter(pk=self.pk).update(views=self.views)
        # else:
        if 'slug' in self.__dict__:
            slug = getattr(
                self, 'title') if 'title' in self.__dict__ else getattr(
                self, 'name')
            setattr(self, 'slug', slugify(slug))
        super().save(*args, **kwargs)

    def get_full_url(self):
        # site = get_current_site().domain
        site = settings.SITE
        url = "https://{site}{path}".format(site=site,
                                            path=self.get_absolute_url())
        return url

    @abstractmethod
    def get_absolute_url(self):
        pass

class Article(BaseModel):
    """文章"""
    STATUS_CHOICES = (
        ('d', '草稿'),
        ('p', '发表'),
    )
    COMMENT_STATUS = (
        ('o', '打开'),
        ('c', '关闭'),
    )
    TYPE = (
        ('a', '文章'),
        ('p', '页面'),
    )
    title = models.CharField('标题', max_length=200, unique=True)
    body = MDTextField('正文')
    pub_time = models.DateTimeField(
        '发布时间', blank=False, null=False, auto_now_add=True)
    status = models.CharField(
        '文章状态',
        max_length=1,
        choices=STATUS_CHOICES,
        default='p')
    comment_status = models.CharField(
        '评论状态',
        max_length=1,
        choices=COMMENT_STATUS,
        default='o')
    type = models.CharField('类型', max_length=1, choices=TYPE, default='a')
    views = models.PositiveIntegerField('浏览量', default=0)
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        verbose_name='作者',
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    article_order = models.IntegerField(
        '排序,数字越大越靠前', blank=False, null=False, default=0)
    category = models.ForeignKey(
        'Category',
        verbose_name='分类',
        on_delete=models.CASCADE,
        blank=False,
        null=False)
    tags = models.ManyToManyField('Tag', verbose_name='标签集合', blank=True)

    def get_body(self):
        return f"{self.title} {self.author.username} {self.body}"

    def body_to_string(self):
        return self.body

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['-article_order', '-pub_time']
        verbose_name = "文章"
        verbose_name_plural = verbose_name
        get_latest_by = 'id'

    def viewed(self):
        self.views += 1
        self.save()

    def get_absolute_url(self):
        return reverse('blog:detailbyid', kwargs={
            'article_id': self.id,
            'year': self.created_time.year,
            'month': self.created_time.month,
            'day': self.created_time.day
        })

    def get_tags_str(self):
        tag_name = []
        print(self.tags)
        print(self.tags.name)
        if self.tags.name:
            for tag in self.tags.name:
                print(tag)
                tag_name.append(tag)
        return "，".join(tag_name)

class Category(BaseModel):
    """文章分类"""
    name = models.CharField('分类名', max_length=30, unique=True)
    parent_category = models.ForeignKey(
        'self',
        verbose_name="父级分类",
        blank=True,
        null=True,
        on_delete=models.CASCADE)
    slug = models.SlugField(default='no-slug', max_length=60, blank=True)

    class Meta:
        ordering = ['name']
        verbose_name = "分类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('blog:categorybyslug', kwargs={
            'category_name': self.slug
        })

    def get_category_tree(self):
        """
        递归获得分类目录的父级
        :return:
        """
        categorys = []

        def parse(category):
            categorys.append(category)
            if category.parent_category:
                parse(category.parent_category)

        parse(self)
        return categorys

    def get_sub_categorys(self):
        """
        获得当前分类目录所有子集
        :return:
        """
        categorys = []
        all_categorys = Category.objects.all()

        def parse(category):
            if category not in categorys:
                categorys.append(category)
            childs = all_categorys.filter(parent_category=category)
            for child in childs:
                if category not in categorys:
                    categorys.append(child)
                parse(child)

        parse(self)
        return categorys

class Tag(BaseModel):
    """文章标签"""
    name = models.CharField('标签名', max_length=30, unique=True)
    slug = models.SlugField(default='no-slug', max_length=60, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['name']
        verbose_name = "标签"
        verbose_name_plural = verbose_name

    def get_absolute_url(self):
        return reverse('blog:tagbyslug', kwargs={'tag_name': self.slug})

class Comment(models.Model):
    body = models.TextField('正文', max_length=300)
    created_time = models.DateTimeField('创建时间', auto_now_add=True)
    last_mod_time = models.DateTimeField('修改时间', auto_now=True)
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        verbose_name='作者',
        on_delete=models.CASCADE)
    article = models.ForeignKey(
        Article,
        verbose_name='文章',
        on_delete=models.CASCADE)
    parent_comment = models.ForeignKey(
        'self',
        verbose_name="上级评论",
        blank=True,
        null=True,
        on_delete=models.CASCADE)
    is_enable = models.BooleanField(
        '是否显示', default=True, blank=False, null=False)

    class Meta:
        ordering = ['id']
        verbose_name = "评论"
        verbose_name_plural = verbose_name
        get_latest_by = 'id'

    def __str__(self):
        return self.body

    def get_sub_categorys(self):
        """
        获得当前分类目录所有子集
        :return:
        """
        comments = []
        all_comments = Comment.objects.all()

        def parse(comment):
            if comment not in comments:
                comments.append(comment)
            childs = all_comments.filter(parent_comment=comment)
            for child in childs:
                if comment not in comments:
                    comments.append(child)
                parse(child)

        parse(self)
        return comments


import PyPDF2
import re

pdfFile = "sdge_bill.pdf"
pdfRead = PyPDF2.PdfFileReader(pdfFile)
page = pdfRead.getPage(2)
pageContent = page.extractText()
# print(pageContent[0:1000])

for i in range(0, pdfRead.getNumPages()):
	if "Electric" in pdfRead.getPage(i).extractText()[:1000]:
		print("works")
		break
	else:
		print("NEXT")

# print(pageContent)

#  print(re.findall(r"Meter Number: [0-9]{8}", pageContent)[0][-8:])
# print(re.findall(r"Total Taxes & Fees on Electric Charges  \n(-\s*\$[0-9]*.?[0-9]{2})", pageContent)[0])

Meter_number = re.findall(r"Meter Number: [0-9]{8}", pageContent)[0][-8:]
taxes = re.findall(r"Total Taxes & Fees on Electric Charges  \n(-?\s*\$[0-9]*.?[0-9]{2})", pageContent)[0]
print("Meter number:", Meter_number.replace(" ", ""))
print("Electric Tax total amount:", taxes.replace(" ", ""))