import os
import tweepy

def get():
    with open(os.path.join(os.path.dirname(__file__), "template.html")) as f:
        return f.read()

def post(screen_name=""):
    CK = os.getenv("TW_CK")
    CS = os.getenv("TW_CS")
    AT = os.getenv("TW_AT")
    AS = os.getenv("TW_AS")
    auth = tweepy.OAuthHandler(CK, CS)
    auth.set_access_token(AT, AS)
    try:
        api = tweepy.API(auth)
        user = api.get_user(screen_name=screen_name)
        output = "ID: {}".format(user.id)
    except Exception as e:
        print(e)
        if screen_name == "":
            output = ""
        else:
            output = "何かがおかしい。<br>" + str(e)
    return {"output": output}

if __name__ == "__main__":
    print(post("laddge"))


from typing import Any, Dict

from app.app import db

Model: Any = db.Model

class Camera(Model):
    __tablename__ = 'cameras'

    Name = db.Column(db.String(10), unique=True)
    images = db.relationship('Image', backref='camera', lazy=True)

    @classmethod
    def from_dict(cls, camera_dict: Dict[str, Any]) -> 'Camera':
        return cls(
            Name=camera_dict['Name']
        )

    def to_dict(self) -> Dict[str, Any]:
        to_dict = super().to_dict()
        to_dict.update(
            {
                'Name': self.Name
            }
        )
        return to_dict

    def update_from_dict(self, camera_dict: Dict[str, Any]) -> None:
        super().update_from_dict(camera_dict)
        if 'Name' in camera_dict:
            self.Name = camera_dict['Name']

class ProductType(Model):
    __tablename__ = 'product_types'

    Name = db.Column(db.String(4), unique=True)
    images = db.relationship('Image', backref='product_type', lazy=True)

    @classmethod
    def from_dict(cls, type_dict: Dict[str, Any]) -> 'ProductType':
        return cls(
            Name=type_dict['Name']
        )

    def to_dict(self) -> Dict[str, Any]:
        to_dict = super().to_dict()
        to_dict.update(
            {
                'Name': self.Name
            }
        )
        return to_dict

    def update_from_dict(self, type_dict: Dict[str, Any]) -> None:
        super().update_from_dict(type_dict)
        if 'Name' in type_dict:
            self.Name = type_dict['Name']

class Image(Model):
    __tablename__ = 'images'

    Name = db.Column(db.String(31), unique=True)
    URL = db.Column(db.Text)
    Sol = db.Column(db.Integer, nullable=False)
    DetatchedLabel = db.Column(db.Boolean)
    CameraID = db.Column(
        db.Integer,
        db.ForeignKey('cameras.ID'),
        nullable=False,
    )
    ProductTypeID = db.Column(
        db.Integer,
        db.ForeignKey('product_types.ID'),
        nullable=False,
    )

    @classmethod
    def from_dict(cls, image_dict: Dict[str, Any]) -> 'Image':
        return cls(
            Name=str(image_dict['Name']),
            URL=str(image_dict['URL']),
            Sol=int(image_dict['Sol']),
            DetatchedLabel=bool(image_dict['DetatchedLabel']),
            CameraID=int(image_dict['CameraID']),
            ProductTypeID=int(image_dict['ProductTypeID']),
        )

    def to_dict(self) -> Dict[str, Any]:
        to_dict = super().to_dict()
        to_dict.update(
            {
                'Name': self.Name,
                'URL': self.URL,
                'Sol': self.Sol,
                'DetatchedLabel': self.DetatchedLabel,
                'CameraID': self.CameraID,
                'camera': self.camera.to_dict(),
                'ProductTypeID': self.ProductTypeID,
                'product_type': self.product_type.to_dict(),
            }
        )
        return to_dict

    def update_from_dict(self, image_dict: Dict[str, Any]) -> None:
        super().update_from_dict(image_dict)
        if 'Name' in image_dict:
            self.Name = image_dict['Name']
        if 'URL' in image_dict:
            self.URL = image_dict['URL']
        if 'Sol' in image_dict:
            self.Sol = image_dict['Sol']
        if 'DetatchedLabel' in image_dict:
            self.DetatchedLabel = image_dict['DetatchedLabel']
        if 'CameraID' in image_dict:
            self.CameraID = image_dict['CameraID']
        if 'ProductTypeID' in image_dict:
            self.ProductTypeID = image_dict['ProductTypeID']


import numpy as np
import cv2
from matplotlib import pyplot as plt
import random
from PIL import Image

img = Image.open('original.jpg')

img1 = cv2.imread('original.jpg')
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)

h, w, _ = img1.shape
print('width: ', w)
print('height:', h)

count = 0
w1 = w2 = int(w/20)
h1 = h2 = int(h/10)
w2 = w3 = w2 - 5
h2 = h3 = h2 - 5
countpixel = 0

for y in range(0, 10):
    h2 = h2 + (h1*y)
    for x in range(0, 20):
        img1[h2,w2] = (255, 0, 0)
        countpixel = countpixel + 1
        w2 = w2 + w1
        if x==19:
            h2 = h3
            w2 = w3

img1 = Image.fromarray(img1) #conversion

print(countpixel)

plt.figure(num='DEADPIXEL Failure - Configuration 3')
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img1),plt.title('DEADPIXEL - Configuration 3')
plt.xticks([]), plt.yticks([])
plt.show()


from flask import jsonify, request
from app.models import Pharmaceutical_info, Token
from app import db

def updatePharmaceuticalInfo():
    '''update pharmaceutical info record'''
    data = request.get_json()
    token = request.headers['TOKEN']
    id=int(data['id'])

    t=Token.query.filter_by(token=token).first()

    is_expired=t.status

    if data:
        if id in data and id is not None:
            if token and is_expired == 'active':

                if Pharmaceutical_info.query.filter_by(id=id).first() is not None:
                    pharmaceutical_info = Pharmaceutical_info(
                        list_of_excipients=data['list_of_excipients'],
                        storage_conditions=data['storage_conditions'],
                        nature_content_ofContainer=data['treatment'],
                        instruction_for_handling=data['instruction_for_handling'],
                        instruction_for_disposal=data['instruction_for_disposal'],
                        text_revision_date=data['text_revision_date'],
                        drug_id=data['drug_id']
                            )
                    db.session.add(pharmaceutical_info)
                    db.session.commit()

                else:
                    return jsonify('Drug pharmaceutical info record specified does not exist'),500
            else:
                return jsonify('no token provided or token has expired'),500
        else:
            return jsonify('No pharmaceutical info id provided'),500
    return jsonify('Provide new pharmaceutical info records for update'),500


import datetime

name = str(input('Name: '))
c = int(input('Age: '))

e = int(input("Number: "))

def years(age):
    d = datetime.date.today().year
    a = d+(100-c)

    for i in range(e):
        print("You're gonna be 100 yrs old the year:", a)
    return(a)

def main():
    return

if __name__ == '__main__':
    main()

years(c)

"""
name = input("Enter you name: ")
age = int(input("Enter your age: "))

x = 100 - age # user will be 100 year old in x years
y = 2019 + x # user will be 100 years old in this year

print(name + " You will turn 100 in " + str(y))
"""


from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
        QApplication, QWidget,
        QHBoxLayout, QVBoxLayout,
        QGroupBox, QButtonGroup, QRadioButton,
        QPushButton, QLabel)
from random import shuffle, randint

class Question():
    def __init__(self, question, right_answer, wrong1, wrong2, wrong3):
        self.question = question
        self.right_answer = right_answer
        self.wrong1 = wrong1
        self.wrong2 = wrong2
        self.wrong3 = wrong3

question_list = []
question_list.append(Question('Государственный язык Бразилии', 'Португальский', 'Бразильский', 'Испанский', 'Итальянски'))
question_list.append(Question('Без какой составляющей компьютер сможет функционировать?', 'Видеокарта', 'Процессор', 'Оперативная память', 'Блок питания'))
question_list.append(Question('Какая видеокарта мощнее?', 'RTX 3090', 'RTX 3060', 'RTX 1050', 'RTX 750 TI'))
question_list.append(Question('Какой страны не существует?', 'Грекистан', 'Бразилия', 'Китай', 'Россия'))
question_list.append(Question('Какой самый сильный злодей в игре Star Wars?', 'Дарт Молл', 'Дарт Вейдер', 'Граф Дуку', 'Дарт Сидиус'))
question_list.append(Question('Что такое H2O?', 'Вода', 'Кислород', 'Водород', 'Углекислый газ'))
question_list.append(Question('Кто из богатырей сказал:"А ловко ты это придумал"?', 'Добрыня Никитич', 'Алеша Попович', 'Илья Муромец', 'Никита Кожемяк'))
question_list.append(Question('Какое оружие в Cs Go убивает с одной пули в тело?', 'AWP', 'Desert Eagle', 'AK-47', 'Glock-18'))
question_list.append(Question('Первый Президент России', 'Ельцин', 'Путин', 'Медведев', 'Горбачев'))
question_list.append(Question('Человек известный как Маэстро', 'Панасенков', 'Величайший', 'Легендарный', 'Залетный'))
question_list.append(Question('Любимый Президент Сан Саныча','Обэмэ', 'Путин', 'Лукашонок', 'Зеленка'))
question_list.append(Question('Из какой игры популярный мем АМОГУС?', 'Among Us', 'Clash Royale', 'Brawl Stars', 'Subway Surfers'))
question_list.append(Question('Какая была разборка?', 'Питерская', 'Минская', 'Московская', 'Краснодарская'))
question_list.append(Question('Как зовут Водилу у которого бочек потик?', 'Ярик', 'Саша', 'Славик', 'Димон'))
question_list.append(Question('Какого камня нет в море?', 'Сухого', 'Мокрого', 'Черного', 'Разноцветного'))
question_list.append(Question('Самый дорогой скин на Glock-18', 'Градиент', 'Глазки', 'Дух', 'Ядерный сад'))
question_list.append(Question('Кто идет домой?', 'Русские', 'Французы', 'Немцы', 'Англичане'))

#Добавить 15-20 вопросов
app = QApplication([])

btn_OK = QPushButton('Ответить')
lb_Question = QLabel('Самый сложный вопрос в мире!')

RadioGroupBox = QGroupBox("Варианты ответов")

rbtn_1 = QRadioButton('Вариант 1')
rbtn_2 = QRadioButton('Вариант 2')
rbtn_3 = QRadioButton('Вариант 3')
rbtn_4 = QRadioButton('Вариант 4')

RadioGroup = QButtonGroup()
RadioGroup.addButton(rbtn_1)
RadioGroup.addButton(rbtn_2)
RadioGroup.addButton(rbtn_3)
RadioGroup.addButton(rbtn_4)

layout_ans1 = QHBoxLayout()
layout_ans2 = QVBoxLayout()
layout_ans3 = QVBoxLayout()
layout_ans2.addWidget(rbtn_1)
layout_ans2.addWidget(rbtn_2)
layout_ans3.addWidget(rbtn_3)
layout_ans3.addWidget(rbtn_4)

layout_ans1.addLayout(layout_ans2)
layout_ans1.addLayout(layout_ans3)
RadioGroupBox.setLayout(layout_ans1)

AnsGroupBox = QGroupBox("Результат теста")
lb_Result = QLabel('прав ты или нет?')
lb_Correct = QLabel('ответ будет тут!')
layout_res = QVBoxLayout()
layout_res.addWidget(lb_Result, alignment=(Qt.AlignLeft | Qt.AlignTop))
layout_res.addWidget(lb_Correct, alignment=Qt.AlignHCenter, stretch=2)
AnsGroupBox.setLayout(layout_res)

layout_line1 = QHBoxLayout()
layout_line2 = QHBoxLayout()
layout_line3 = QHBoxLayout()

layout_line1.addWidget(lb_Question, alignment=(Qt.AlignHCenter | Qt.AlignVCenter))
layout_line2.addWidget(RadioGroupBox)
layout_line2.addWidget(AnsGroupBox)
AnsGroupBox.hide()

layout_line3.addStretch(1)
layout_line3.addWidget(btn_OK, stretch=2) # кнопка должна быть большой
layout_line3.addStretch(1)
layout_card = QVBoxLayout()

layout_card.addLayout(layout_line1, stretch=2)
layout_card.addLayout(layout_line2, stretch=8)
layout_card.addStretch(1)
layout_card.addLayout(layout_line3, stretch=1)
layout_card.addStretch(1)
layout_card.setSpacing(5) # пробелы между содержимым

# ----------------------------------------------------------
# Виджеты и макеты созданы, далее - функции:
# ----------------------------------------------------------

def show_result():
    ''' показать панель ответов '''
    RadioGroupBox.hide()
    AnsGroupBox.show()
    btn_OK.setText('Следующий вопрос')

def show_question():
    ''' показать панель вопросов '''
    RadioGroupBox.show()
    AnsGroupBox.hide()
    btn_OK.setText('Ответить')
    RadioGroup.setExclusive(False) # сняли ограничения, чтобы можно было сбросить выбор радиокнопки
    rbtn_1.setChecked(False)
    rbtn_2.setChecked(False)
    rbtn_3.setChecked(False)
    rbtn_4.setChecked(False)
    RadioGroup.setExclusive(True)

answers = [rbtn_1, rbtn_2, rbtn_3, rbtn_4]

def ask(q: Question):
    shuffle(answers)
    answers[0].setText(q.right_answer)
    answers[1].setText(q.wrong1)
    answers[2].setText(q.wrong2)
    answers[3].setText(q.wrong3)
    lb_Question.setText(q.question)
    lb_Correct.setText(q.right_answer)
    show_question()
def show_correct(res):
    lb_Result.setText(res)
    show_result()

def check_answer():
    if answers[0].isChecked():
        show_correct('Правильно!')
        window.score += 1
        print('Статистика\n-Всего вопросов:', window.total, '\nПравильных ответов:', window.score)
        print('Рейтинг:', (window.score/window.total*100), '%')
    else:
        if answers[1].isChecked() or answers[2].isChecked() or answers[3].isChecked():
            show_correct('Неверно!')
            print('Рейтинг:', (window.score/window.total*100), '%')

def next_question():
    window.total += 1
    print('Статистика\n-Всего вопросов:', window.total, '\nПравильных ответов:', window.score)
    cur_question = randint(0, len(question_list) - 1)
    q = question_list[cur_question]
    ask(q)

def click_ok():
    if btn_OK.text() == 'Ответить':
        check_answer()
    else:
        next_question()

window = QWidget()
window.setLayout(layout_card)
window.setWindowTitle('PradaCard') #изменить  название прораммы
btn_OK.clicked.connect(click_ok)
window.score = 0
window.total = 0
next_question()
window.show()
app.exec()


# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import absolute_import
import os

from salt.utils import parsers
from salt.utils.verify import verify_env, verify_files
from salt.config import _expand_glob_path
import salt.cli.caller

class SaltCall(parsers.SaltCallOptionParser):
    '''
    Used to locally execute a salt command
    '''

    def run(self):
        '''
        Execute the salt call!
        '''
        self.parse_args()

        if self.config['verify_env']:
            verify_env([
                    self.config['pki_dir'],
                    self.config['cachedir'],
                ],
                self.config['user'],
                permissive=self.config['permissive_pki_access'],
                pki_dir=self.config['pki_dir'],
            )
            if not self.config['log_file'].startswith(('tcp://',
                                                       'udp://',
                                                       'file://')):
                # Logfile is not using Syslog, verify
                verify_files(
                    [self.config['log_file']],
                    self.config['user']
                )

        if self.options.file_root:
            # check if the argument is pointing to a file on disk
            file_root = os.path.abspath(self.options.file_root)
            self.config['file_roots'] = {'base': _expand_glob_path([file_root])}

        if self.options.pillar_root:
            # check if the argument is pointing to a file on disk
            pillar_root = os.path.abspath(self.options.pillar_root)
            self.config['pillar_roots'] = {'base': _expand_glob_path([pillar_root])}

        if self.options.local:
            self.config['file_client'] = 'local'
        if self.options.master:
            self.config['master'] = self.options.master

        # Setup file logging!
        self.setup_logfile_logger()

        caller = salt.cli.caller.Caller.factory(self.config)

        if self.options.doc:
            caller.print_docs()
            self.exit(os.EX_OK)

        if self.options.grains_run:
            caller.print_grains()
            self.exit(os.EX_OK)

        caller.run()


#!/usr/bin/env python3

from __future__ import print_function
from bcc import BPF, USDT
import argparse
import ctypes as ct
import time
import os
import io
import ipaddress
import socket
from collections import defaultdict

# globals
SYSCALLS = ["socket", "socketpair", "bind", "listen", "accept", "accept4",
            "connect", "getsockname", "getpeername", "sendto", "recvfrom",
            "setsockopt", "getsockopt", "shutdown", "sendmsg", "sendmmsg",
            "recvmsg", "recvmmsg", "read", "write", "open", "openat", "creat",
            "close", "sendfile64"]

SYSCALL = 1
DISK = 3
NET = 4
PADDING = "  "
BLUE = '\033[95m'
UNDERLINE = '\033[4m'
ENDC = '\033[0m'

# C result class

class CallEvent(ct.Structure):
    _fields_ = [
        ("depth", ct.c_ulonglong),
        ("pid", ct.c_ulonglong),
        ("lat", ct.c_ulonglong),
        ("type", ct.c_ulonglong),
        ("fd_type", ct.c_ulonglong),
        ("fdw", ct.c_ulonglong),
        ("fdr", ct.c_ulonglong),
        ("fd_ret", ct.c_ulonglong),
        ("bytes_write", ct.c_ulonglong),
        ("bytes_read", ct.c_ulonglong),
        ("addr", ct.c_ulonglong),
        ("clazz", ct.c_char * 80),
        ("method", ct.c_char * 80),
        ("file", ct.c_char * 80),
        ]

###############################################################################
# TEMPLATES
###############################################################################

# program template
PROGRAM = """
#include <uapi/linux/ip.h>
#include <uapi/linux/in.h>

struct call_t {
    u64     depth;                  // first bit is direction (0 entry, 1 return)
    u64     pid;                    // (tgid << 32) + pid from bpf_get_current...
    u64     lat;                    // time latency
    u64     type;                   // syscall or php function
    u64     fd_type;                // disk or net filedescriptor
    u64     fdw;                    // filedescriptor write
    u64     fdr;                    // filedescriptor read
    u64     fd_ret;                 // returned filedescriptor
    u64     bytes_write;            // number of write bytes
    u64     bytes_read;             // number of read bytes
    u64     addr;                   // addr to connect
    char    clazz[80];              // class name
    char    method[80];             // method name
    char    file[80];               // php file name
};

#define SYS     1
#define FUNC    2
#define DISK    3
#define NET     4

BPF_PERF_OUTPUT(calls);
BPF_HASH(entry, u64, u64);
BPF_HASH(start, u64, u64);
BPF_HASH(start_func, u64, u64);
BPF_HASH(fd, u64, u64);
BPF_HASH(addr, u64, u64);
BPF_HASH(filedescriptors, u64, u64);
"""

# php probes template
PHP_TRACE_TEMPLATE = """
int {name}(struct pt_regs *ctx) {{
    u64 *depth, zero = 0, clazz = 0, method = 0, file = 0;
    struct call_t data = {{}};
    u64 pid = bpf_get_current_pid_tgid();

    {read_class}
    {read_method}
    {read_file}
    bpf_probe_read(&data.clazz, sizeof(data.clazz), (void *)clazz);
    bpf_probe_read(&data.method, sizeof(data.method), (void *)method);
    bpf_probe_read(&data.file, sizeof(data.file), (void *)file);
    u64 id = clazz + method + file;

    data.type = FUNC;
    data.pid = pid;
    depth = entry.lookup_or_init(&data.pid, &zero);
    data.depth = {depth};
    {update_func}

    if (!(data.depth & (1ULL << 63))) {{
        u64 time = bpf_ktime_get_ns();
        start_func.update(&id, &time);
    }} else {{
        u64 *start_ns = start_func.lookup(&id);
        if (!start_ns) {{
            calls.perf_submit(ctx, &data, sizeof(data));
            start_func.delete(&id);
            return 0;
        }}
        data.lat = bpf_ktime_get_ns() - *start_ns;
        start_func.delete(&method);
    }}

    calls.perf_submit(ctx, &data, sizeof(data));
    return 0;
}}
"""

# syscall tracepoint template
SYS_TRACE_TEMPLATE = """
TRACEPOINT_PROBE(syscalls, sys_enter_{syscall_name}) {{
    u64 pid = bpf_get_current_pid_tgid();
    if ({pid_condition}) {{
        return 0;
    }}
    u64 time = bpf_ktime_get_ns();
    start.update(&pid, &time);
    {syscall_enter_logic}
    return 0;
}}

TRACEPOINT_PROBE(syscalls, sys_exit_{syscall_name}) {{
    u64 pid = bpf_get_current_pid_tgid();
    if ({pid_condition}) {{
        return 0;
    }}

    u64 *depth, zero = 0, clazz = 0, method = 0;

    struct call_t data = {{}};
    data.type = SYS;
    data.pid = pid;
    depth = entry.lookup_or_init(&data.pid, &zero);
    data.depth = *depth;
    char method_str[80] = "{syscall_name}";
    bpf_probe_read(&data.method, sizeof(data.method), method_str);

    u64 *start_ns = start.lookup(&pid);
    if (!start_ns) {{
        calls.perf_submit(args, &data, sizeof(data));
        return 0;
    }}

    data.lat = bpf_ktime_get_ns() - *start_ns;
    {syscall_exit_logic}
    calls.perf_submit(args, &data, sizeof(data));
    return 0;
}}
"""

class SyscallEvents:
    e = defaultdict(list)

    def event(self, syscalls, enter, exit):
        for syscall in syscalls:
            self.e[syscall].append((enter, exit))

    def plugin(self, syscall):
        return ("".join(a[0] for a in self.e[syscall]),
                "".join(a[1] for a in self.e[syscall]))

    def pid_condition(self, pids):
        return " && ".join("pid >> 32 != %s" % str(pid) for pid in pids)

    def syscall(self, pids, syscall):
        enter, exit = self.plugin(syscall)
        return SYS_TRACE_TEMPLATE.format(syscall_name=syscall,
                                         pid_condition=self.pid_condition(pids),
                                         syscall_enter_logic=enter,
                                         syscall_exit_logic=exit
                                         )

    def generate(self, pids, syscalls=None):
        if syscalls is None:
            global SYSCALLS
            syscalls = SYSCALLS
        return "".join(self.syscall(pids, syscall) for syscall in syscalls)

def print_event(pid, lat, message, depth):
    return ("%-6d %-10s %-40s" %
            (pid, str(lat), (PADDING * (depth - 1)) + message))

def syscall_message(event):
    message = io.StringIO()
    message.write("sys.")
    message.write(BLUE)
    message.write(event.method.decode("utf-8", "replace"))
    message.write(ENDC)
    if event.fdw > 0:
        message.write(" write on fd: %s" % event.fdw)
    if event.fdr > 0:
        message.write(" read fd: %s" % event.fdr)
    if event.fd_ret > 0:
        message.write(" return fd: %s" % event.fd_ret)

    if event.addr > 0:
        addr = str(ipaddress.ip_address(event.addr))
        rev = addr.split('.')[::-1]
        addr = '.'.join(rev)
        message.write(" connect to: %s" % addr)
        try:
            host = socket.gethostbyaddr(addr)
            message.write(" -> %s" % host[0])
        except socket.herror or socket.gaierror:
            pass
    return message.getvalue()

class Process:
    total_lat = 0
    total_net_time = 0
    total_disk_time = 0
    net_write_volume = 0
    disk_write_volume = 0
    net_read_volume = 0
    disk_read_volume = 0

    def __init__(self):
        self.data_buffer = io.StringIO()

    def reset(self):
        self.total_lat = 0
        self.total_net_lat = 0
        self.total_disk_lat = 0
        self.net_write_volume = 0
        self.disk_write_volume = 0
        self.net_read_volume = 0
        self.disk_read_volume = 0

    def add_in_buffer(self, data):
        self.data_buffer.write(data + '\n')

    def get_buffer(self):
        return self.data_buffer.getvalue()

class Callback:
    process_dict = defaultdict(Process)

    def __init__(self, args):
        self.args = args

    def __call__(self, cpu, data, size):
        event = ct.cast(data, ct.POINTER(CallEvent)).contents
        depth = event.depth & (~(1 << 63))
        if depth == 0:
            return
        process = self.process_dict[str(event.pid)]
        if event.type == SYSCALL:
            process.total_lat += event.lat

            if event.fd_type == NET:
                process.total_net_time += event.lat

                if event.bytes_write > 0:
                    process.net_write_volume += event.bytes_write
                elif event.bytes_read > 0:
                    process.net_read_volume += event.bytes_read

            elif event.fd_type == DISK:
                process.total_disk_time += event.lat

                if event.bytes_write > 0:
                    process.disk_write_volume += event.bytes_write
                elif event.bytes_read > 0:
                    process.disk_read_volume += event.bytes_read

            if not self.args.syscalls:
                return

            process.add_in_buffer(print_event(
                event.pid >> 32,
                event.lat,
                syscall_message(event),
                depth
            ))

        else:
            # Return function case
            if event.depth & (1 << 63):
                direction = "<- "

                if SYSCALLS:
                    if process.total_lat > 0:
                        process.add_in_buffer(print_event(
                                event.pid >> 32,
                                process.total_lat,
                                BLUE + "traced syscalls total latence" + ENDC,
                                depth
                        ))

                    if process.total_net_time > 0:
                        process.add_in_buffer(
                            print_event(
                                event.pid >> 32, process.total_net_time, BLUE + (
                                    "sys time spent on the network |-> %d bytes written, %d bytes read" %
                                    (process.net_write_volume, process.net_read_volume)) + ENDC, depth))

                    if process.total_disk_time > 0:
                        process.add_in_buffer(
                            print_event(
                                event.pid >> 32, process.total_disk_time, BLUE + (
                                    "sys time spent on the disk |-> %d bytes written, %d bytes read" %
                                    (process.disk_write_volume, process.disk_read_volume)) + ENDC, depth))

                    # reset counters
                    process.reset()
            # Entry function case
            else:
                direction = "-> "

            process.add_in_buffer(
                print_event(
                    event.pid >> 32,
                    str(
                        event.lat) if event.lat > 0 else "-",
                    "".join(
                        (direction,
                         event.clazz.decode(
                             'utf-8',
                             'replace'),
                            ".",
                            event.method.decode(
                             'utf-8',
                             'replace'),
                            " ",
                            UNDERLINE,
                            "from ",
                            event.file.decode(
                            'utf-8',
                            'replace'),
                            ENDC)),
                    depth))
            # Quit the program on the last main return
            if event.depth & (
                    1 << 63) and event.method.decode(
                    'utf-8',
                    'replace') == "main" and depth == 1:
                print(process.get_buffer())
                del self.process_dict[str(event.pid)]
                if not self.process_dict:
                    exit()

class PHPEvents:
    usdt_tab = []
    txt = []
    probes = []

    def probe(self, pids, probe_name, func_name, read_class, read_method,
              read_file, is_return=False):
        "Generate the c for php probes"
        depth = "*depth + 1" if not is_return else "*depth | (1ULL << 63)"
        update = "++(*depth);" if not is_return else "if (*depth) --(*depth);"
        values = {
            'name': func_name,
            'read_class': read_class,
            'read_method': read_method,
            'read_file': read_file,
            'depth': depth,
            'update_func': update
        }
        self.txt.append(PHP_TRACE_TEMPLATE.format(**values))
        self.probes.append((probe_name, func_name))

    def generate(self, pids):
        for probe_name, func_name in self.probes:
            for pid in pids:
                usdt = USDT(pid=pid)
                usdt.enable_probe_or_bail(probe_name, func_name)
                self.usdt_tab.append(usdt)
        return "".join(self.txt)

def c_program(pids):
    "Generate the C program"
    program = io.StringIO()
    program.write(PROGRAM)
    php = PHPEvents()

    php.probe(pids,
              "function__entry",
              "php_entry",
              "bpf_usdt_readarg(4, ctx, &clazz);",
              "bpf_usdt_readarg(1, ctx, &method);",
              "bpf_usdt_readarg(2, ctx, &file);",
              is_return=False)
    php.probe(pids,
              "function__return",
              "php_return",
              "bpf_usdt_readarg(4, ctx, &clazz);",
              "bpf_usdt_readarg(1, ctx, &method);",
              "bpf_usdt_readarg(2, ctx, &file);",
              is_return=True)

    program.write(php.generate(pids))
    # trace syscalls

    s = SyscallEvents()
    # intercept when an open filedescriptor is read. get the fd for printing
    # and get the type for sort the latence in NET or DISK
    s.event(("read",), """
                u64 fdarg = args->fd;
                fd.update(&pid, &fdarg);

                """,
            """
                data.bytes_read = args->ret;
                u64 *fdarg = fd.lookup(&pid);
                if (fdarg) {
                    data.fdr = *fdarg;
                    fd.delete(&pid);
                    u64 *fdt = filedescriptors.lookup(fdarg);
                    if (fdt) {
                        data.fd_type = *fdt;
                    }
                }

                """)

    # intercept when write on an open filedescriptor. get the fd for printing
    # and get the type for sort the latence in NET or DISK
    s.event(("write", "sendto", "sendmsg"), """
                u64 fdarg = args->fd;
                fd.update(&pid, &fdarg);

                """,
            """
                data.bytes_write = args->ret;
                u64 *fdarg = fd.lookup(&pid);
                if (fdarg) {
                    data.fdw = *fdarg;
                    fd.delete(&pid);
                    u64 *fdt = filedescriptors.lookup(fdarg);
                    if (fdt) {
                        data.fd_type = *fdt;
                    }
                }

                """)

    # store in a map the filedescriptors when open or socket open it.
    # and store the type: NET or DISK
    s.event(("open", "openat", "creat"), "", """
                u64 ret = args->ret;
                u64 flag = DISK;
                filedescriptors.update(&ret, &flag);
                data.fd_ret = ret;

                """)

    s.event(("socket",), "", """
                u64 ret = args->ret;
                u64 flag = NET;
                filedescriptors.update(&ret, &flag);
                data.fd_ret = ret;

                """)

    # decorator for trace the address in the connect arg
    s.event(("connect",), """
                struct sockaddr_in *useraddr = ((struct sockaddr_in *)(args->uservaddr));
                u64 a = useraddr->sin_addr.s_addr;
                addr.update(&pid, &a);
                u64 fdarg = args->fd;
                fd.update(&pid, &fdarg);

                """,
            """
                u64 *a = addr.lookup(&pid);
                if (a) {
                    data.addr = *a;
                    addr.delete(&pid);
                }
                u64 *fdarg = fd.lookup(&pid);
                if (fdarg) {
                    data.fdw = *fdarg;
                    fd.delete(&pid);
                }

                """)

    s.event(("bind",), """
                struct sockaddr_in *useraddr = ((struct sockaddr_in *)(args->umyaddr));
                u64 a = useraddr->sin_addr.s_addr;
                addr.update(&pid, &a);
                u64 fdarg = args->fd;
                fd.update(&pid, &fdarg);

                """,
            """
                u64 *a = addr.lookup(&pid);
                if (a) {
                    data.addr = *a;
                    addr.delete(&pid);
                }
                u64 *fdarg = fd.lookup(&pid);
                if (fdarg) {
                    data.fdw = *fdarg;
                    fd.delete(&pid);
                }

                """)

    program.write(s.generate(pids))
    return program.getvalue(), php.usdt_tab

def main():
    # cli arguments
    parser = argparse.ArgumentParser(
        description="php_tool",
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        "pid",
        type=int,
        nargs="+",
        help="process id to attach to")
    parser.add_argument(
        "--debug", action="store_true",
        help="debug mode: print the generated BPF program")
    parser.add_argument(
        "--check", action="store_true",
        help="print the generated BPF program and quit")
    parser.add_argument(
        "-S", "--syscalls", action="store_true",
        help="print the syscalls details inside each function")
    args = parser.parse_args()

    program, usdt_tab = c_program(args.pid)

    # debug options
    if args.check or args.debug:
        print(program)
        if args.check:
            exit()

    # inject the C program generated in eBPF
    bpf = BPF(text=program, usdt_contexts=usdt_tab)

    print("php super tool, pid = %s... Ctrl-C to quit." % (args.pid))
    print("%-6s %-10s %s" % ("PID", "LAT", "METHOD"))

    # don't forget the page_cnt option for increase the ring buffer size
    bpf["calls"].open_perf_buffer(Callback(args), page_cnt=8192)
    while True:
        try:
            bpf.perf_buffer_poll()
        except KeyboardInterrupt:
            exit()

if __name__ == "__main__":
    main()


import json

with open("pokemons.json") as file:
    pokemons = json.load(file)["results"]

# ACESSAR UMA CHAVE
# print(pokemons[0]["evolution"])

grass_type_pokemons = [
    pokemon for pokemon in pokemons if "Grass" in pokemon["type"]
]

with open("grass_pokemons.json", "w") as file:
    # json.dumps joga numa variavel
    # json_to_write = json.dumps(grass_type_pokemons) #converte de python pra json
    # file.write(json_to_write)

    # json.dump joga direto no arquivo
    json.dump(grass_type_pokemons, file)


import c_a_parameters_bible as pb
import sqlite3
from sqlite3 import Error

conn = sqlite3.connect(pb.BIBLE_ID + '/f_c_db_'+pb.BIBLE_ID+'.sqlite')
cur = conn.cursor()
cur.execute(''' SELECT book,token, count(*) as 'count' FROM wordtokens GROUP BY book,token ORDER by 1,3 desc; ''')

old_book = ""
token_count = 0
line = ""
for row in cur:
	book = row[0]
	token = row[1]
	count = row[2]

	if(old_book != book):
		token_count = 0
		old_book = book
		print(line)
		line = book + "\t"
	else:
		token_count += 1
		if(token_count<4):
			line = line + token + "(" + str(count) + ")"  + "\t\t"
conn.commit()
conn.close()


from soc import debug_messages
from playing import get_all_messages
from collections import Counter
from edtext import clean
from more_itertools import unique_everseen
import json

def main():
    messages_json = get_all_messages()
    start = 3 * 1500
    finish = start + 1500
    messages_chunk = messages_json[start:finish]
    messages_text = [el['message'] for el in messages_json]
    # debug_messages(messages_text)
    counted_all = Counter()
    edited_messages = list(unique_everseen([clean(' '.join(el)) for el in messages_text]))
    for message in edited_messages:
        counted_all += Counter(message.split())
    with open('result.json', 'w', encoding='utf-8') as file:
        json.dump(counted_all, file)
    # interesting = 'должна'
    # last = '''М, 23, Питер. Ищу с кем сходить на Твое имя в эти выходные. А так же с кем до смотреть Апрельскую ложь. Живу на севере Питера  Vk.com/s.slip'''

if __name__ == '__main__':
    main()


from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.http import Http404
from django.shortcuts import render
from .models import Movie

# data = {
#     'movies': [
#         {
#             'id': 5,
#             'title': 'Jaws',
#             'year': 1669,
#         },
#         {
#             'id': 6,
#             'title': 'Sharknado',
#             'year': 1660,
#         },
#         {
#             'id': 7,
#             'title': 'The Meg',
#             'year': 1670,
#         }
#     ]
# }

def movies(req):
    data = Movie.objects.all()
    return render(req, 'movies/movies.html', {'movies': data})

def home(req):
    return HttpResponse("Home Page")

def detail(req, id):
    try:
        data = Movie.objects.get(pk=id)
    except:
        raise Http404('Movie does not exist')

    return render(req, 'movies/detail.html', {'movie': data})

def add(req):
    title = req.POST.get('title')
    year = req.POST.get('year')

    if title and year:
        movie = Movie(title=title, year=year)
        movie.save()
        return HttpResponseRedirect('/movies')
    return render(req, 'movies/add.html')

def delete(req, id):
    try:
        movie = Movie.objects.get(pk=id)
    except:
        raise Http404('Movie does not exist')

    movie.delete()
    return HttpResponseRedirect('/movies')


# -*- coding: utf-8 -*-
import scrapy
import time
from scrapy.http import Request
from loguru import logger
from urllib.parse import urljoin

from SafetyInformation.items import SafeInfoItem
from SafetyInformation.settings import SLEEP_TIME, TOTAL_PAGES

class Myhack58Spider(scrapy.Spider):
    name = 'myhack58'
    allowed_domains = ['myhack58.com']
    start_urls = ['http://www.myhack58.com/Article/html/1/4/Article_004_1.htm']
    page = 1
    headers = {
        'Referer': 'http://www.myhack58.com/',
        'Host': 'www.myhack58.com',
    }
    source = 'http://www.myhack58.com/'

    def parse(self, response):
        logger.info("==========当前正在抓取第{}页==========".format(self.page))
        item = SafeInfoItem()
        info_list = response.xpath('//div[@class="ArticleListBox"]/ul')
        for info in info_list:
            title = info.xpath('./li[@class="Noptitle"]/a/text()').extract_first('')
            link = info.xpath('./li[@class="Noptitle"]/a/@href').extract_first('')
            link = urljoin(self.source, link)
            intro = info.xpath('./li[@class="Noptdes"]/text()').extract_first('')
            n_info = info.xpath('./li[@class="Noptinfo"]/text()').extract_first('').split(' ')
            nopt_info = [n for n in n_info if n.strip()]
            tags = nopt_info[0].split('：')[-1]
            date = nopt_info[1].split('：')[-1]
            info_type = nopt_info[2].split('：')[-1] if nopt_info[2].split('：')[-1] else 'news'
            source = self.source
            author = ''
            item['title'] = title
            item['link'] = link
            item['intro'] = intro
            item['date'] = date
            item['source'] = source
            item['type'] = info_type
            item['author'] = author
            logger.info(item)
            yield item

        time.sleep(SLEEP_TIME)
        self.page += 1
        next_url = 'http://www.myhack58.com/Article/html/1/4/Article_004_{}.htm'.format(self.page)
        if self.page <= TOTAL_PAGES:
            yield Request(url=next_url, headers=self.headers, callback=self.parse)


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

import os
import time
import magic
import datetime
import logging
from pastefile import utils
from jsondb import JsonDB
from flask import send_from_directory, abort
from werkzeug import secure_filename

LOG = logging.getLogger(__name__)

def get_infos_file_from_md5(md5, dbfile):
    # Open JsonDB for read only
    db = JsonDB(dbfile=dbfile)
    db.load()
    return db.read(md5)

def remove_file(db, file_id):
    "Remove a file on disk and in db"
    try:
        os.remove(db.db[file_id]['storage_full_filename'])
    except OSError:
        LOG.error('Error while trying to remove %s'
                  % file_id)
    if not os.path.isfile(db.db[file_id]['storage_full_filename']):
        db.delete(file_id)
        return True
    return False

def clean_files(dbfile, expire=86400):
    with JsonDB(dbfile=dbfile) as db:
        if db.lock_error:
            LOG.warning('Cant clean files')
            return False
        for k, v in list(db.db.iteritems()):
            if int(db.db[k]['timestamp']) < int(time.time() - int(expire)):
                remove_file(db=db, file_id=k)

def get_file_info(id_file, config, env):
    infos = get_infos_file_from_md5(md5=id_file, dbfile=config['FILE_LIST'])
    if not infos:
        return False
    try:
        size = utils.human_readable(os.stat(infos['storage_full_filename']).st_size)
        expire = datetime.datetime.fromtimestamp(
                      int(infos['timestamp']) +
                      int(config['EXPIRE'])).strftime('%d-%m-%Y %H:%M:%S'),
        file_infos = {
            'name': infos['real_name'],
            'md5': id_file,
            'burn_after_read': infos['burn_after_read'],
            'timestamp': infos['timestamp'],
            'expire': expire,
            'mime_type': infos['mime_type'],
            'type': infos['type'],
            'size': size,
            'url': "%s/%s" % (utils.build_base_url(env=env), id_file)
        }
        return file_infos
    except:
        LOG.error('Unable to gather infos for file %s' % id_file)
        return False

def add_new_file(filename, source, dest, db, mime_type, type, md5, burn_after_read):

    # IMPROVE : possible "bug" If a file is already uploaded, the burn_after_read
    #           Will not bu updated
    # File already exist, return True and remove ths source
    if md5 in db.db:
        try:
            os.remove(source)
        except OSError as e:
            LOG.error("Can't remove tmp file: %s" % e)
        return True

    # If no lock, return false
    if db.lock_error:
        return False

    try:
        os.rename(source, dest)
    except OSError as e:
        LOG.error("Can't move processing file to storage directory: %s" % e)
        return False

    db.write(md5, {
        'real_name': filename,
        'type': type,
        'mime_type': mime_type,
        'storage_full_filename': dest,
        'timestamp': int(time.time()),
        'burn_after_read': str(burn_after_read),
    })
    return True

def upload_file(request, config):
    value_burn_after_read = request.form.getlist('burn')
    if value_burn_after_read:
        burn_after_read = True
    else:
        burn_after_read = False

    # Write tmp file on disk
    try:
        file_md5, tmp_full_filename = utils.write_tmpfile_to_disk(file=request.files['file'],
                                                                  dest_dir=config['TMP_FOLDER'])
    except IOError:
        return 'Server error, contact administrator\n'

    secure_name = secure_filename(request.files['file'].filename)

    with JsonDB(dbfile=config['FILE_LIST']) as db:

        # Just inform for debug purpose
        if db.lock_error:
            LOG.error("Unable to get lock during file upload %s" % file_md5)

        # Try to write file on disk and db. Return false if file is not writed
        storage_full_filename = os.path.join(config['UPLOAD_FOLDER'], file_md5)
        mime_type = magic.from_file(tmp_full_filename, mime=True)
        _type = magic.from_file(tmp_full_filename)
        succed_add_file = add_new_file(filename=secure_name,
                                       source=tmp_full_filename,
                                       dest=storage_full_filename,
                                       mime_type=mime_type,
                                       type=_type,
                                       db=db,
                                       md5=file_md5,
                                       burn_after_read=burn_after_read)

    if not succed_add_file:
        # In the case the file is not in db, we have 2 reason :
        #  * We was not able to have the lock and write the file in the db.
        #  * Or an error occure during the file processing
        # In any case just tell the user to try later
        try:
            os.remove(tmp_full_filename)
        except OSError as e:
            LOG.error("Can't remove tmp file: %s" % e)

        LOG.info('Unable lock the db and find the file %s in db during upload' % file_md5)
        return 'Unable to upload the file, try again later ...\n'

    LOG.info("[POST] Client %s has successfully uploaded: %s (%s)"
             % (request.remote_addr, storage_full_filename, file_md5))
    return "%s/%s\n" % (utils.build_base_url(env=request.environ),
                        file_md5)

def delete_file(request, id_file, dbfile):
    with JsonDB(dbfile=dbfile) as db:
        if db.lock_error:
            return "Lock timed out\n"
        if id_file not in db.db:
            return abort(404)

        if not remove_file(db=db, file_id=id_file):
            return 'Unable to delete file %s\n' % id_file

        LOG.info("[DELETE] Client %s has deleted: %s"
                 % (request.remote_addr, id_file))
        return "File %s deleted\n" % id_file

def get_file(request, id_file, config):
    # First, open JsonDB for read-only
    db = JsonDB(dbfile=config['FILE_LIST'])
    db.load()
    if id_file not in db.db or db.db[id_file]['burn_after_read'] == 'Burned':
        return abort(404)
    if db.db[id_file]['burn_after_read'] == 'True':
        # Now, try to lock the db
        if not db._lock():
            return "Can't lock db for burning file"
        db.db[id_file]['burn_after_read'] = 'Burned'
        db.save()
        db._release()

    filename = os.path.basename(db.db[id_file]['storage_full_filename'])
    LOG.info("[GET] Client %s has requested: %s (%s)"
             % (request.remote_addr, db.db[id_file]['real_name'], id_file))

    if not os.path.isabs(config['UPLOAD_FOLDER']):
        path = "%s/%s" % (os.path.dirname(config['instance_path']),
                          config['UPLOAD_FOLDER'])
    else:
        path = config['UPLOAD_FOLDER']

    # If the user agent is in the display list, format headers to direct display feature
    if request.user_agent.browser in config['DISPLAY_FOR']:
        return send_from_directory(path,
                                   filename,
                                   mimetype=db.db[id_file]['mime_type'],
                                   attachment_filename=db.db[id_file]['real_name'])

    # Else keep the regular send file
    return send_from_directory(path,
                               filename,
                               mimetype=db.db[id_file]['mime_type'],
                               attachment_filename=db.db[id_file]['real_name'],
                               as_attachment=True)

def get_all_files(request, config):
    # Open JsonDB for read only
    db = JsonDB(dbfile=config['FILE_LIST'], logger=config['LOGGER_NAME'])
    db.load()
    files_list_infos = {}
    for k, v in db.db.iteritems():
        _infos = get_file_info(id_file=k,
                               config=config,
                               env=request.environ)
        if not _infos:
            continue
        files_list_infos[k] = _infos
    return files_list_infos

def db_purge(dbfile):
    """If a file is not present but present in db
    this function will clean it from the db"""
    with JsonDB(dbfile=dbfile) as db:
        for k, v in db.db.items():
            elt = db.db[k]['storage_full_filename']
            if not os.path.exists(elt):
                LOG.info("%s present in db but doesn't exist" % elt)
                db.delete(k)


from wall import views
from django.urls import path

urlpatterns = [
	path('deer/',views.deerList.as_view(), name='deerlist'),
	path('realwreath/',views.RealWreathView.as_view(), name ="realwreathview"),
	path('ornament/',views.OrnamentView.as_view(), name ='ornament_list'),
	path('solvequestion/',views.SolveQuestion.as_view(), name="solvequestion"),
	#path('present/',PresentView.as_view(), name = "presentview"),
	path('socks/',views.SocksView.as_view(), name="socks")

]


# 실버 1
# 14716. 현수막

import sys
from collections import deque
input = sys.stdin.readline

n, m = map(int, input().split())
mtx = [list(map(int, input().split())) for _ in range(n)]

q = deque()

dx = [-1, 0, 1, 0, -1, 1, -1, 1]
dy = [0, -1, 0, 1, -1, 1, 1, -1]

def bfs(i, j):
    q.append([i, j])
    mtx[i][j] = 0

    while q:
        x, y = q.popleft()

        for k in range(8):
            nx = x + dx[k]
            ny = y + dy[k]

            if 0 <= nx < n and 0 <= ny < m and mtx[nx][ny] == 1:
                q.append([nx, ny])
                mtx[nx][ny] = 0

count = 0

for i in range(n):
    for j in range(m):
        if mtx[i][j] == 1:
            bfs(i, j)
            count += 1

print(count)


import asyncio
import dbus_next
from dbus_next.aio import MessageBus
import socket
import enum
import logging
import common

class BtConnectionRole(enum.Enum):
    Master = 1
    Slave = 2
    NotConnected = 3

class BtClient(object):
    BT_CONTROL_PORT = 17  # Service port - control port specified in the bluetooth HID specification
    BT_INTERRUPT_PORT = 19  # Service port - interrupt port specified in the bluetooth HID specification

    @staticmethod
    async def create_via_address(address):
        client = BtClient(address)
        await client._connect_to_dbus_service()
        return client

    @staticmethod
    async def create_via_device_object_path(device_object_path):
        address = BtClient.get_mac_address_from_devie_object_path(device_object_path)
        client = BtClient(address)
        await client._connect_to_dbus_service()
        return client

    @staticmethod
    def get_mac_address_from_devie_object_path(device_object_path):
        return device_object_path[-17:].replace("_",":")

    @staticmethod
    def get_device_object_path_from_mac_address(device_object_path):
        return "/org/bluez/hci0/dev_" + device_object_path.replace(":","_")

    def __init__(self, address):
        self._address = address
        self._device_object_path = BtClient.get_device_object_path_from_mac_address(self._address)
        self._is_alive = True
        self._stop_event = False
        self._is_connected = False
        self._is_bluez_connected = True
        self._control_socket = None
        self._interrupt_socket = None
        self._loop = asyncio.get_event_loop()
        self._message_queue = asyncio.Queue()
        self._task = None
        self._bt_master_task = None
        self._bt_check_alive_task = None

    @property
    def address(self):
        return self._address

    @property
    def object_path(self):
        return self._device_object_path

    @property
    def name(self):
        return self._name

    @property
    def is_alive(self):
        if self._task:
            return not self._task.done()
        else:
            return False

    @property
    def is_connected(self):
        return self._is_connected

    @property
    def info(self):
        info_dict = {}
        info_dict["name"] = self.name
        info_dict["address"] = self.address
        info_dict["isConnected"] = self.is_connected
        return info_dict

    def connect(self):
        if not self._task or self._task.done():
            self._stop_event = False
            self._task = asyncio.create_task(self._run())

    def accept_connection(self, control_socket, interrupt_socket):
        if not self._task or self._task.done():
            self._control_socket = control_socket
            self._interrupt_socket = interrupt_socket
            self._stop_event = False
            self._task = asyncio.create_task(self._run())

    async def _establish_socket_connection(self):
        if self._control_socket and self._interrupt_socket:
            logging.debug(f"{self.name}: Incoming socket connection from {self.name} ({self.address})")
            self._handle_state_at_successfull_connection()
        else:
            logging.debug(f"{self.name}: Establish socket connection to {self.name} ({self.address})")
            try:
                self._control_socket = socket.socket(
                    socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP)
                await self._loop.run_in_executor(None, lambda: self._control_socket.connect((self.address, self.BT_CONTROL_PORT)))
                self._control_socket.setblocking(False)
                self._interrupt_socket = socket.socket(
                    socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP)
                await self._loop.run_in_executor(None, lambda: self._interrupt_socket.connect((self.address, self.BT_INTERRUPT_PORT)))
                self._interrupt_socket.setblocking(False)
                self._handle_state_at_successfull_connection()
            except Exception as e:
                logging.error(f"{self.name}: Exception during connect: {e}")
                self._disconnect()
        if self._is_connected:
            self._enshure_bt_master()
            self._checking_connection_state_periodically()

    def _handle_state_at_successfull_connection(self):
        self._message_queue = asyncio.Queue()
        self._is_connected = True

    def _disconnect(self):
        if self._control_socket:
            self._control_socket.close()
            self._control_socket = None
        if self._interrupt_socket:
            self._interrupt_socket.close()
            self._interrupt_socket = None
        self._is_connected = False

    def stop(self):
        self._stop_event = True

    async def join(self):
        await self._task

    async def _connect_to_dbus_service(self):
        self._bluez_obj = None
        while not self._bluez_obj:
            try:
                self._dbus = await MessageBus(bus_type=dbus_next.BusType.SYSTEM).connect()
                introspection = await self._dbus.introspect(
                    "org.bluez", self._device_object_path)
                self._bluez_obj = self._dbus.get_proxy_object(
                    "org.bluez", self._device_object_path, introspection)
                self._bluez_itf = self._bluez_obj.get_interface("org.bluez.Device1")
                self._bluez_props = self._bluez_obj.get_interface("org.freedesktop.DBus.Properties")
                logging.debug(f"{self._address}: D-Bus bluez object connected")
                self._name = await self._bluez_itf.get_name()
                logging.debug(f"{self._address}: Name resolves to {self._name}")
                self._bluez_props.on_properties_changed(self._on_properties_changed)
            except dbus_next.DBusError:
                logging.warning(f"{self._address}: D-Bus bluez object available - reconnecting...")
                await asyncio.sleep(5)

    def _on_properties_changed(self, interface_name, changed_properties, invalidated_properties):
        for changed, variant in changed_properties.items():
            logging.debug(f'{self._name}: D-Bus bluez property changed: {changed} - {variant.value}')
            if changed == "Connected":
                self._is_bluez_connected = variant.value

    async def _run(self):
        if self._stop_event: return
        await self._establish_socket_connection()
        if self._is_connected:
            logging.info(f"\033[0;32m{self.name}: Connection established ({self.address})\033[0m")
        while self._is_connected and not self._stop_event:
            message = ""
            try:
                message = await asyncio.wait_for(self._message_queue.get(), timeout=2)
                await self._loop.sock_sendall(self._interrupt_socket, bytes(message))
                self._message_queue.task_done()
            except asyncio.QueueEmpty:
                pass
            except asyncio.TimeoutError:
                pass
            except socket.error:
                logging.error(f"{self._name}: Socket error during send")
                self._disconnect()
            except:
                logging.error(f"{self._name}: Message could not be sed: {message}")
                raise

        logging.info(f"\033[0;31m{self.name}: Connection terminated - client closed ({self.address})\033[0m")
        self._disconnect()

    def _enshure_bt_master(self):
        if not self._bt_master_task or self._bt_master_task.done():
            self._bt_master_task = asyncio.create_task(self._switch_to_master())

    async def _switch_to_master(self):
        logging.info(f"{self._name}: Enshure connection role {BtConnectionRole.Master.name}")
        connection_role = await self._get_connection_role()
        while (connection_role == BtConnectionRole.Slave) and not self._stop_event:
            try:
                await common.System.exec_cmd(f"hcitool sr {self._address} MASTER")
            except Exception as exc:
                logging.debug(f"{self._name}: Connection role switch exception: {exc}")
            await asyncio.sleep(2)
            connection_role = await self._get_connection_role()
        logging.info(f"{self._name}: Connection role is {connection_role.name}")

    def _checking_connection_state_periodically(self):
        if not self._bt_check_alive_task or self._bt_check_alive_task.done():
            self._bt_check_alive_task = asyncio.create_task(self._send_periodic_alive_messages())

    async def _send_periodic_alive_messages(self):
        logging.debug(f"{self._name}: Sending periodic messages to check connection state started")
        while self._is_connected and not self._stop_event:
            await asyncio.sleep(4)
            # sending an empty message to check if socket connection is still alive
            self.send(bytes([0, 0, 0, 0]))
        logging.debug(f"{self._name}: Sending periodic messages to check connection state stopped")

    async def _get_connection_role(self):
        returncode, stdout, stderr = await common.System.exec_cmd("hcitool con")
        for line in stdout.decode("utf-8").splitlines():
            if line.find(self._address) >= 0:
                if line.find("SLAVE") >= 0:
                    return BtConnectionRole.Slave
                if line.find("MASTER") >= 0:
                    return BtConnectionRole.Master
        return BtConnectionRole.NotConnected

    def send(self, message):
        if self._is_connected:
            self._message_queue.put_nowait(message)


from nltk.corpus import stopwords
from tensorflow.keras.preprocessing.text import Tokenizer
import gensim
import pandas as pd

from preprocess import process_text_data
from utils import load_epub
from word2vec import Word2Vec

def remove_stopword(book):
    corpus = list()
    stopword = stopwords.words('english')
    for _, sentences in book.items():
        for sentence in sentences:
            stopword_removed = list()
            for word in sentence.split():
                if word in stopword:
                    continue
                stopword_removed.append(word)
            if len(stopword_removed) > 1:
                corpus.append(' '.join(stopword_removed))
    return corpus

def tokenize_corpus(corpus):
    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(corpus)

    sequences = tokenizer.texts_to_sequences(corpus)

    return tokenizer, sequences

if __name__ == '__main__':
    epub_path = \
        './ebook/J. D. Salinger - The Catcher in the Rye '\
        '(1951, Penguin Books Ltd).epub'
    book = load_epub(epub_path)
    book = process_text_data(book)
    book[0] = [' '.join(book[0])]

    corpus = remove_stopword(book)
    tokenizer, sequences = tokenize_corpus(corpus)

    word2idx = tokenizer.word_index

    vocab_size = len(word2idx) + 1
    window_size = 10
    embedding_size = 20
    word2vec = Word2Vec(vocab_size, window_size, embedding_size)

    word2vec.train(sequences, epochs=15)
    embedding = word2vec.get_embedding()
    with open('./output/embedding/word_embedding.txt', 'w') as f:
        f.write(f'{vocab_size-1} {embedding_size}\n')
        for word, idx in word2idx.items():
            word_vec = ' '.join(map(str, list(embedding[idx, :])))
            f.write(f'{word} {word_vec}\n')

    w2v = gensim.models.KeyedVectors.load_word2vec_format(
        fname='./output/embedding/word_embedding.txt'
    )

    targets = [
        'holden', 'phoebe', 'ackley', 'stradlater', 'jane', 'janine', 'rye'
    ]

    for target in targets:
        print(f'What Words are most Similarit to {target}')

        similar_words = w2v.most_similar(positive=[target])
        similar_df = pd.DataFrame(
            similar_words, columns=['word', 'similarity']
        )
        similar_df.to_csv(
            f'./output/dataframes/q3-similar_with_{target}.csv', index=False
        )


import pygame
import random

WIDTH = 480
HEIGHT = 600
FPS = 60

# Задаем цвета
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# Создаем игру и окно
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Star Defender")
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 40))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH / 2
        self.rect.bottom = HEIGHT - 10
        self.speed_x = 0

    def update(self):
        self.speed_x = 0
        key_state = pygame.key.get_pressed()

        if key_state[pygame.K_LEFT]:
            self.speed_x = -8
        if key_state[pygame.K_RIGHT]:
            self.speed_x = 8
        self.rect.x += self.speed_x

        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.left < 0:
            self.rect.left = 0

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 40))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100, -40)
        self.speed_x = random.randrange(-3, 3)
        self.speed_y = random.randrange(1, 8)

    def update(self):
        self.rect.x += self.speed_x
        self.rect.y += self.speed_y

        if self.rect.top > HEIGHT + 10:
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(-100, -40)
            self.speed_x = random.randrange(-3, 3)
            self.speed_y = random.randrange(1, 8)

        if self.rect.left < -20 or self.rect.right > WIDTH + 20:
            self.speed_x = -self.speed_x

all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
    m = Mob()
    all_sprites.add(m)
    mobs.add(m)

# Цикл игры
running = True
while running:
    # Держим цикл на правильной скорости
    clock.tick(FPS)

    # Ввод процесса (события)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False

    # Обновление
    all_sprites.update()

    # Рендеринг
    screen.fill(BLACK)
    all_sprites.draw(screen)

    # После отрисовки всего, переворачиваем экран
    pygame.display.flip()

pygame.quit()


import pathlib
import sys
import xml.etree.ElementTree as ET
from typing import Final

NAMESPACES: Final[dict[str, str]] = {
    "": "http://www.w3.org/2000/svg",
    "xlink": "http://www.w3.org/1999/xlink",
}

for prefix, uri in NAMESPACES.items():
    ET.register_namespace(prefix, uri)

def urlize(s: str) -> str:
    return s.lower().replace(" ", "-")

def link_schemata(path: pathlib.Path) -> None:
    """Link analysis bracket texts to corresponding schemata."""
    schemata = [
        "Do-Re-Mi",
        "Fenaroli",
        "Prinner",
        "Quiescenza",
        "Romanesca",
    ]

    tree = ET.parse(path)
    root = tree.getroot()

    for schema in schemata:
        url = f"../../schemata/{urlize(schema)}/"
        for text in [schema, f"({schema})"]:
            for g in root.findall(f".//tspan[.={text!r}]/../..", NAMESPACES):
                subelements = list(g)
                for subelement in subelements:
                    g.remove(subelement)
                a = ET.SubElement(g, "a", {ET.QName(NAMESPACES["xlink"], "href"): url})
                a.extend(subelements)

    tree.write(path)

def main() -> None:
    path = pathlib.Path(sys.argv[1])

    link_schemata(path)

if __name__ == "__main__":
    main()


import json
from asyncio import sleep
from contextlib import asynccontextmanager
from logging import getLogger
from os import remove
from pathlib import Path

import aiofiles
import aiohttp
from aiogram import Bot
from aiogram.types import Downloadable

from ..settings import get_settings

logger = getLogger("tools.file_manager")

settings = get_settings()

async def _get_bot_data() -> dict:
    path = Path(__file__).parent.parent
    async with aiofiles.open(str(path) + "/bot.json", encoding="utf-8") as f:
        return json.loads(await f.read())

async def _update_bot_data(data: dict) -> dict:
    path = Path(__file__).parent.parent
    async with aiofiles.open(str(path) + "/bot.json", mode="w", encoding="utf-8") as f:
        await f.write(json.dumps(data, indent=4))

async def read_chat(chat_name: str) -> dict | None:
    try:
        bot = await _get_bot_data()
        chat = bot["chats"][chat_name]
        return chat
    except KeyError:
        logger.error(f"KeyError: Chat - {chat_name} not in whitelist!")

async def update_bot_chat(chat_name: str, fields: dict) -> dict | None:
    try:
        bot = await _get_bot_data()
        for key in fields.keys():
            bot["chats"][chat_name][key] = fields[key]
        await _update_bot_data(bot)
        return bot
    except KeyError:
        logger.error(f"KeyError: Chat - {chat_name} not in whitelist!")

async def download(url: str, destination: str | Path):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            if resp.status == 200:
                async with aiofiles.open(destination, mode="wb") as f:
                    await f.write(await resp.read())
            else:
                logger.error(f"{resp.status} status code during downloading file")

@asynccontextmanager
async def remote_open(bot: Bot, object: Downloadable):
    try:
        await sleep(0.03)
        file = await bot.get_file(object.file_id)
        file_path = settings.TMP_DIR / (
            str(file.file_unique_id) + file.file_path[file.file_path.rindex(".") :]
        )
        url = f"https://api.telegram.org/file/bot{settings.TOKEN}/{file.file_path}"
        await download(url, file_path)
        yield file_path
    except Exception as ex:
        logger.warning(str(ex))
    finally:
        remove(file_path)


######################################################################
# File: retrieval_model.py
# Author: Vishal Dey
# Created on: 11 Dec 2019
#######################################################################
'''
 Synopsis: Create w2v for corresponding string description of each problem
	Reads in pretrianed Word2vec vectors and add each word vector to obtain
	phrase vectors.
	Either compute TF-IDF / w2v based cosine similarity
'''

import os
import json
import sys

import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel, cosine_similarity
from copy import deepcopy

# for reproducibility
np.random.choice(123)

# read json file
def read_json(fname):
	with open(os.path.join('./data/', fname), 'r') as fp:
		return json.load(fp)

# compute TF-iDF based most similar problem
def find_similar_tfidf(tfidf_matrix, tfidf_vector):
	cosine_similarities = linear_kernel(tfidf_vector, tfidf_matrix).flatten()
	related_docs_indices = [i for i in cosine_similarities.argsort()[::-1]]
	#print(related_docs_indices[0], cosine_similarities[related_docs_indices[0]])
	return related_docs_indices[0]

# compute w2v cosine based most similar problem
def find_similar_w2v(w2vmatrix, w2vemb, query, EMB_DIM, minv=0, maxv=1):
	query_emb = []
	for word in query.split():
		if word in w2vemb:
			query_emb.append(np.array(list(map(float, w2vemb[word]))))
		else:
			query_emb.append(np.random.uniform(minv, maxv, EMB_DIM))

	cosine_similarities = linear_kernel(query_emb, w2vmatrix).flatten()
	related_docs_indices = [i for i in cosine_similarities.argsort()[::-1]]
	print(related_docs_indices)
	return related_docs_indices[0]

# load w2v problem
def load_w2v(w2v_file):
	EMB_DIM = 0
	w2v_emb = {}
	minv = sys.float_info.max
	maxv = sys.float_info.min

	with open(os.path.join('./w2v', w2v_file), 'r') as fp:
		EMB_DIM = int(fp.readline().split()[1])
		for line in fp.readlines():
			tmp = line.split()
			tmp = list(map(float, tmp[1:]))
			minv = min(minv, min(tmp))
			maxv = max(maxv, max(tmp))

	print(EMB_DIM, minv, maxv)
	return w2v_emb, EMB_DIM, minv, maxv

# compute w2v matrix (problem x EMB_DIM)
def compute_w2vmatrix(docs, w2v_emb, EMB_DIM, minv=0, maxv=1):
	w2vmatrix = []
	for doc in docs:
		emb = [0]*EMB_DIM
		for word in doc.split():
			if word in w2v_emb:
				emb += np.array(list(map(float, w2v_emb[word])))
			else:
				emb += np.random.uniform(minv, maxv, EMB_DIM)
		emb /= len(doc.split())

		w2vmatrix.append(emb)
	return w2vmatrix

# post fix equation solver
def post_solver(post_equ):
	stack = []
	op_list = ['+', '-', '/', '*', '^']

#	if len(post_equ)-2*len([i for i in post_equ if (any(c.isdigit() for c in i))]) >= 0:
#		print(post_equ)
#		return 0
	for elem in post_equ:
		if elem not in op_list:
			op_v = elem
			if '%' in op_v:
				op_v = float(op_v[:-1])/100.0
			if op_v == 'PI':
				op_v = np.pi
			stack.append(str(op_v))
		elif elem in op_list:
			op_v_1 = stack.pop()
			op_v_1 = float(op_v_1)
			op_v_2 = stack.pop()
			op_v_2 = float(op_v_2)
			if elem == '+':
				stack.append(str(op_v_2+op_v_1))
			elif elem == '-':
				stack.append(str(op_v_2-op_v_1))
			elif elem == '*':
				stack.append(str(op_v_2*op_v_1))
			elif elem == '/':
				if op_v_1 == 0:
					return np.nan
				stack.append(str(op_v_2/op_v_1))
			else:
				stack.append(str(op_v_2**op_v_1))
	return stack.pop()

# get post fix equation from the template
def get_equation(template, num_list):
	equation = []
	for x in template:
		if 'temp' in x:
			equation.append(str(num_list[int(x.split('_')[1])]))
		else:
			equation.append(x)
	return equation

def main():
	math23k = read_json('math23k_final.json')
	#math23k = read_json('final_dolphin_data.json')

	# read in test IDs and answer -  gold truth
	test_ids = []
	with open('./data/id_ans_test', 'r') as fp:
		for line in fp.readlines():
			test_ids.append(line.split('\t')[0])
#	del math23k['1682']

	test_length = len(test_ids)

	train = {}
	test = {}
	corpus = []

	# make training corpus text
	for k, v in math23k.items():
		if k not in test_ids:
			train[k] = v
			corpus.append(v['template_text'])
		else:
			test[k] = v

	corpus = list(corpus)
	indices = list(train.keys())

	print(len(corpus), test_length)

	# tfidf vectorizer
	vectorizer = TfidfVectorizer()
	corpus_tfidf = vectorizer.fit_transform(corpus)

	# load w2v
	#w2v_emb, EMB_DIM, minv, maxv = load_w2v('crawl-300d-2M.vec')
	#w2vmatrix = compute_w2vmatrix(corpus, w2v_emb, EMB_DIM, minv, maxv)

	#print(len(w2vmatrix))
	similar_indices = {}

	for k, v in test.items():
		query_tfidf = vectorizer.transform([v['template_text']])
		i = find_similar_tfidf(corpus_tfidf, query_tfidf)
		#i = find_similar_w2v(w2vmatrix, w2v_emb, v['template_text'], EMB_DIM, minv, maxv)
		#i = jaccard_similarity(corpus, v['template_text'])
		#print(i)
		similar_indices[k] = indices[i]

	num_correct = 0
	# outputs the prediction
	fout = open('pred.txt', 'w')

	for k, v in similar_indices.items():
		template = math23k[v]['post_template']
		num_list = math23k[k]['num_list']
		ans = math23k[k]['ans']

		if (max([int(s.split('temp_')[1]) for s in set(template) if 'temp' in s]) < len(num_list)):
			pred_ans = post_solver(get_equation(template, num_list))
			print(math23k[k]['index'], pred_ans, ans, file=fout)
			if (np.isclose(float(pred_ans), float(ans))):
				num_correct += 1
		else:
			print('wrong template prediction: mismatch with len of num list')

	print('Accuracy = ', str(float(num_correct)/test_length))
	fout.close()

main()


from astropy.io import fits
import numpy as np
import astropy.units as u
from agpy import cubes
from FITS_tools import cube_regrid
import agpy

#f2 = fits.open('H2CO_22_speccube.fits')
dpath = '/Volumes/128gbdisk/w51/'
#f2 = fits.open('Darray_H2CO_22_speccube_uniform_contsub_justspw19.image.fits')
f2 = fits.open(dpath+'W51Ku_BD_spw19.small_uniform_contsub19.cvel.clean.image.fits')

#cont2 = (f2[0].data[0,60:350,:,:].sum(axis=0) + f2[0].data[0,600:950,:,:].sum(axis=0))/(950-600+350-60)
cont2 = fits.getdata(dpath+'W51Ku_BD_spw20.small_uniform_continuum.clean.image.fits').squeeze()
ch = fits.getheader(dpath+'W51Ku_BD_spw20.small_uniform_continuum.clean.image.fits')
noisefloor = cont2[100:200,300:500].std()
#cont2 = fits.getdata(dpath+'W51Ku_BD_spw20.small_uniform_continuum.clean.image.fits').squeeze()
#ch = fits.getheader(dpath+'W51Ku_BD_spw20.small_uniform_continuum.clean.image.fits')

beam = (ch['BMAJ']*ch['BMIN'] * np.pi * u.deg**2)
cont_offset = (2.73*u.K).to(u.Jy,u.brightness_temperature(beam,14.488*u.GHz))

extra_cont_offset = 0.05

contlev = cont_offset.value + extra_cont_offset + noisefloor

#spec2 = f2[0].data[0,350:600,:,:] + 0.03
spec2 = f2[0].data[190:293,:,:] + contlev + cont2
# try to "subtract out" the negative continuum...
#spec2[:,cont2<0] -= cont2[cont2<0]
dosmooth = False
if dosmooth:
    spec2s = cube_regrid.spatial_smooth_cube(spec2,kernelwidth=1.5)

    cont2s = agpy.smooth(cont2+contlev,kernelwidth=1.5)
else:
    spec2s = spec2
    cont2s = cont2
spec2s[:,cont2s<0] -= cont2s[cont2s<0]
#cont2s[cont2s<contlev]=contlev

#cont2 += contlev
#cont2[cont2<contlev+noisefloor]=contlev+noisefloor

# "noise floor" (not really)
#spec2[spec2<0.01] = 0.01

#tau2 = -np.log((spec2/cont2))
tau2 = -np.log((spec2s/cont2s))
tau2[np.isnan(tau2)] = 5

f2[0].header['CRPIX3'] -= 190
f2[0].data = tau2
f2.writeto('H2CO_22_Ku_BD_small_taucube_sm.fits',clobber=True)

tau2ds = (tau2[11:83:3,:,:]+tau2[12:83:3,:,:]+tau2[13:83:3,:,:])/3.
h = f2[0].header
f2[0].header['CRVAL3'] = (10-h['CRPIX3']+1)*h['CDELT3']+h['CRVAL3']
f2[0].header['CRPIX3'] = 1
f2[0].header['CDELT3'] = h['CDELT3']*3
f2[0].data = tau2ds
f2.writeto('H2CO_22_Ku_BD_small_taucube_sm_ds.fits',clobber=True)

yy,xx = np.indices(tau2.shape[1:])
rr = ((xx-255.)**2+(yy-255.)**2)**0.5
mask = rr < 200
nanmask = np.zeros_like(mask,dtype='float')
nanmask[True-mask] = np.nan

integ1 = tau2[10:47,:,:].sum(axis=0)
f2[0].data = integ1 + nanmask
f2[0].writeto('H2CO_22_Ku_BD_small_tausummed_48to61.fits',clobber=True)

integ2 = tau2[41:69,:,:].sum(axis=0)
f2[0].data = integ2 + nanmask
f2[0].writeto('H2CO_22_Ku_BD_small_tausummed_59to68.fits',clobber=True)

integ3 = tau2[69:80,:,:].sum(axis=0)
f2[0].data = integ3 + nanmask
f2[0].writeto('H2CO_22_Ku_BD_small_tausummed_68to72.fits',clobber=True)

integ3 = tau2[10:80,:,:].sum(axis=0)
f2[0].data = integ3 + nanmask
f2[0].writeto('H2CO_22_Ku_BD_small_tausummed_48to72.fits',clobber=True)


from flask import Flask, render_template, request, redirect
from models import db, EmployeeModel

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)

@app.before_first_request
def create_table():
    db.create_all()

@app.route('/')
def index():
    return render_template("index.html", title="Flask CRUD")

@app.route("/data/create", methods=["GET", "POST"])
def create():
    if request.method == "GET":
        return render_template("createpage.html", titel="Create data")

    if request.method == "POST":
        employee_id = request.form["employee_id"]
        name = request.form["name"]
        age = request.form["age"]
        position = request.form["position"]
        employee = EmployeeModel(
            employee_id=employee_id, name=name, age=age, position=position)
        db.session.add(employee)
        db.session.commit()
        return redirect("/data")

@app.route("/data")
def RetrieveList():
    employees = EmployeeModel.query.all()
    return render_template("datalist.html", employees=employees, title="Datalist")

@app.route("/data/<int:id>", methods=["GET", "POST"])
def RetrieveEmployee(id):
    if request.method == "POST":
        employee = EmployeeModel.query.filter_by(employee_id=id).first()
        if employee:
            return render_template("data.html", employee=employee, title="Data")
        return f"Employee with id ={id} Doenst exist"

@app.route("/data/<int:id>/update", methods=["GET", "POST"])
def update(id):
    employee = EmployeeModel.query.filter_by(employee_id=id).first()
    if request.method == "POST":
        if employee:
            db.session.delete(employee)
            db.session.commit()
            name = request.form["name"]
            age = request.form["age"]
            position = request.form["position"]
            employee = EmployeeModel(
                employee_id=id, name=name, age=age, position=position)
            db.session.add(employee)
            db.session.commit()
            return redirect(f"/data")

    return render_template("update.html", employee=employee, title="Update")

@app.route("/data/<int:id>/delete", methods=["GET", "POST"])
def delete(id):
    employee = EmployeeModel.query.filter_by(employee_id=id).first()
    if request.method == "POST":
        if employee:
            db.session.delete(employee)
            db.session.commit()
            return redirect("/data")
        abort(404)

    return render_template("delete.html", title="Delete")


import json
import time
import psycopg
from datetime import date, timedelta
from dateutil.relativedelta import relativedelta
from pgvector.psycopg import register_vector
from chatgpt import chatgpt_api
from ny_times import times_api

connection_string = ''
start_date = date(2020, 5, 27)
end_date = date(2000, 1, 1)

def get_connection_string():
    with open('pg_connection_string.txt', 'r') as f:
        return f.read()

def get_openai_api_key():
    with open('openai_api_key.txt', 'r') as f:
        return f.read()

def get_times_api_key():
    with open('times_api_key.txt', 'r') as f:
        return f.read()

def insert_times_embedding(content,
                           headline,
                           breadcrumbs,
                           url,
                           channel,
                           subchannel,
                           intro,
                           abstract,
                           snippet,
                           embedding_vector,
                           article_id):
    with psycopg.connect(connection_string) as conn:
        register_vector(conn)
        with conn.cursor() as cur:
            cur.execute(
                "INSERT INTO " + 'times_embeddings' +
                " (content, headline, breadcrumbs, url, channel, subchannel, intro, abstract, snippet, embedding, article_id)" +
                " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                (content,
                 headline,
                 breadcrumbs,
                 url,
                 channel,
                 subchannel,
                 intro,
                 abstract,
                 snippet,
                 embedding_vector,
                 article_id
                 ))
            conn.commit()

def generate_times_embeddings(year, month):
    '''Fetches time articles and creates embeddings from them'''
    print("Doing " + str(month) + "/" + str(year))
    api = times_api(get_times_api_key())
    embedder = chatgpt_api(get_openai_api_key())
    articles = []
    try:
        articles = api.get_articles_of_date(year, month)
        print("Found: " + str(len(articles)) + " articles")
    except Exception as ex:
        print("Error while fetching articles, maybe a timeout. Retrying soon")
        print(ex)
        time.sleep(120)
        generate_times_embeddings(year, month)

    for article in articles:
        # The full text is not provided within the api. I could scrape it, but
        # chances are there will be html, other tags or missing words in it.
        # For now: lets test with just the headline, abstract and first para
        text = article['abstract'] + ' ' + article['lead_paragraph']
        if(text == ''):
            continue
        embeddings = []
        try:
            embeddings = embedder.embed(text)
        except Exception as ex:
            print("Error while fetching embedding text:")
            print(ex)
            print("Waiting a few seconds, this is probably due to timeout.")
            time.sleep(300)
            start_date -= relativedelta(months=+1)
            generate_times_embeddings(start_date.year, start_date.month)

        # Now store the relevant data in the database
        try:
            breadcrumbs = list(map(lambda k: k['value'], article['keywords']))
            insert_times_embedding(text,
                                   article['headline']['main'],
                                   breadcrumbs,
                                   article['web_url'],
                                   article['keywords'][0]['value'],
                                   article['keywords'][1]['value'],
                                   article['lead_paragraph'],
                                   article['abstract'],
                                   article['snippet'],
                                   embeddings,
                                   article['_id'])
        except Exception as ex:
            # Dont print it. There are too many.
            # print("Failed to insert one article, prolly missing channels. Skipping it.")
            # print(ex)
            pass

if __name__ == "__main__":
    connection_string = get_connection_string()
    while start_date >= end_date:
        try:
            generate_times_embeddings(start_date.year, start_date.month)
            start_date -= relativedelta(months=+1)
        except Exception as ex:
            print("Unknown error. Restarting in a bit.")
            time.sleep(60)


import json
import logging
import os
from typing import Dict

import lib.configs
from lib.activelearning import Last
from lib.infers.deepgrow_pipeline import InferDeepgrowPipeline
from lib.infers.vertebra_pipeline import InferVertebraPipeline

import monailabel
from monailabel.interfaces.app import MONAILabelApp
from monailabel.interfaces.config import TaskConfig
from monailabel.interfaces.datastore import Datastore
from monailabel.interfaces.tasks.infer_v2 import InferTask
from monailabel.interfaces.tasks.scoring import ScoringMethod
from monailabel.interfaces.tasks.strategy import Strategy
from monailabel.interfaces.tasks.train import TrainTask
from monailabel.scribbles.infer import GMMBasedGraphCut, HistogramBasedGraphCut
from monailabel.tasks.activelearning.first import First
from monailabel.tasks.activelearning.random import Random

# bundle
from monailabel.tasks.infer.bundle import BundleInferTask
from monailabel.tasks.train.bundle import BundleTrainTask
from monailabel.utils.others.class_utils import get_class_names
from monailabel.utils.others.generic import get_bundle_models, strtobool
from monailabel.utils.others.planner import HeuristicPlanner

logger = logging.getLogger(__name__)

class MyApp(MONAILabelApp):
    def __init__(self, app_dir, studies, conf):
        self.model_dir = os.path.join(app_dir, "model")

        configs = {}
        for c in get_class_names(lib.configs, "TaskConfig"):
            name = c.split(".")[-2].lower()
            configs[name] = c

        configs = {k: v for k, v in sorted(configs.items())}

        # Load models from app model implementation, e.g., --conf models <segmentation_spleen>
        models = conf.get("models")
        if not models:
            print("")
            print("---------------------------------------------------------------------------------------")
            print("Provide --conf models <name>")
            print("Following are the available models.  You can pass comma (,) seperated names to pass multiple")
            print(f"    all, {', '.join(configs.keys())}")
            print("---------------------------------------------------------------------------------------")
            print("")
            exit(-1)

        models = models.split(",")
        models = [m.strip() for m in models]
        invalid = [m for m in models if m != "all" and not configs.get(m)]
        if invalid:
            print("")
            print("---------------------------------------------------------------------------------------")
            print(f"Invalid Model(s) are provided: {invalid}")
            print("Following are the available models.  You can pass comma (,) seperated names to pass multiple")
            print(f"    all, {', '.join(configs.keys())}")
            print("---------------------------------------------------------------------------------------")
            print("")
            exit(-1)

        # Use Heuristic Planner to determine target spacing and spatial size based on dataset+gpu
        spatial_size = json.loads(conf.get("spatial_size", "[48, 48, 32]"))
        target_spacing = json.loads(conf.get("target_spacing", "[1.0, 1.0, 1.0]"))
        self.heuristic_planner = strtobool(conf.get("heuristic_planner", "false"))
        self.planner = HeuristicPlanner(spatial_size=spatial_size, target_spacing=target_spacing)

        # app models
        self.models: Dict[str, TaskConfig] = {}
        for n in models:
            for k, v in configs.items():
                if self.models.get(k):
                    continue
                if n == k or n == "all":
                    logger.info(f"+++ Adding Model: {k} => {v}")
                    self.models[k] = eval(f"{v}()")
                    self.models[k].init(k, self.model_dir, conf, self.planner)
        logger.info(f"+++ Using Models: {list(self.models.keys())}")

        # Load models from bundle config files, local or released in Model-Zoo, e.g., --conf bundles <spleen_ct_segmentation>
        self.bundles = get_bundle_models(app_dir, conf, conf_key="bundles") if conf.get("bundles") else None

        super().__init__(
            app_dir=app_dir,
            studies=studies,
            conf=conf,
            name=f"MONAILabel - Radiology ({monailabel.__version__})",
            description="DeepLearning models for radiology",
            version=monailabel.__version__,
        )

    def init_datastore(self) -> Datastore:
        datastore = super().init_datastore()
        if self.heuristic_planner:
            self.planner.run(datastore)
        return datastore

    def init_infers(self) -> Dict[str, InferTask]:
        infers: Dict[str, InferTask] = {}

        #################################################
        # Models
        #################################################
        for n, task_config in self.models.items():
            c = task_config.infer()
            c = c if isinstance(c, dict) else {n: c}
            for k, v in c.items():
                logger.info(f"+++ Adding Inferer:: {k} => {v}")
                infers[k] = v

        #################################################
        # Bundle Models
        #################################################
        if self.bundles:
            for n, b in self.bundles.items():
                i = BundleInferTask(b, self.conf)
                logger.info(f"+++ Adding Bundle Inferer:: {n} => {i}")
                infers[n] = i

        #################################################
        # Scribbles
        #################################################
        infers.update(
            {
                "Histogram+GraphCut": HistogramBasedGraphCut(
                    intensity_range=(-300, 200, 0.0, 1.0, True),
                    pix_dim=(2.5, 2.5, 5.0),
                    lamda=1.0,
                    sigma=0.1,
                    num_bins=64,
                    labels=task_config.labels,
                ),
                "GMM+GraphCut": GMMBasedGraphCut(
                    intensity_range=(-300, 200, 0.0, 1.0, True),
                    pix_dim=(2.5, 2.5, 5.0),
                    lamda=5.0,
                    sigma=0.5,
                    num_mixtures=20,
                    labels=task_config.labels,
                ),
            }
        )

        #################################################
        # Pipeline based on existing infers
        #################################################
        if infers.get("deepgrow_2d") and infers.get("deepgrow_3d"):
            infers["deepgrow_pipeline"] = InferDeepgrowPipeline(
                path=self.models["deepgrow_2d"].path,
                network=self.models["deepgrow_2d"].network,
                model_3d=infers["deepgrow_3d"],
                description="Combines Clara Deepgrow 2D and 3D models",
            )

        #################################################
        # Pipeline based on existing infers for vertebra segmentation
        # Stages:
        # 1/ localization spine
        # 2/ localization vertebra
        # 3/ segmentation vertebra
        #################################################
        if (
            infers.get("localization_spine")
            and infers.get("localization_vertebra")
            and infers.get("segmentation_vertebra")
        ):
            infers["vertebra_pipeline"] = InferVertebraPipeline(
                task_loc_spine=infers["localization_spine"],  # first stage
                task_loc_vertebra=infers["localization_vertebra"],  # second stage
                task_seg_vertebra=infers["segmentation_vertebra"],  # third stage
                description="Combines three stage for vertebra segmentation",
            )
        logger.info(infers)
        return infers

    def init_trainers(self) -> Dict[str, TrainTask]:
        trainers: Dict[str, TrainTask] = {}
        if strtobool(self.conf.get("skip_trainers", "false")):
            return trainers
        #################################################
        # Models
        #################################################
        for n, task_config in self.models.items():
            t = task_config.trainer()
            if not t:
                continue

            logger.info(f"+++ Adding Trainer:: {n} => {t}")
            trainers[n] = t

        #################################################
        # Bundle Models
        #################################################
        if self.bundles:
            for n, b in self.bundles.items():
                t = BundleTrainTask(b, self.conf)
                if not t or not t.is_valid():
                    continue

                logger.info(f"+++ Adding Bundle Trainer:: {n} => {t}")
                trainers[n] = t

        return trainers

    def init_strategies(self) -> Dict[str, Strategy]:
        strategies: Dict[str, Strategy] = {
            "random": Random(),
            "first": First(),
            "last": Last(),
        }

        if strtobool(self.conf.get("skip_strategies", "true")):
            return strategies

        for n, task_config in self.models.items():
            s = task_config.strategy()
            if not s:
                continue
            s = s if isinstance(s, dict) else {n: s}
            for k, v in s.items():
                logger.info(f"+++ Adding Strategy:: {k} => {v}")
                strategies[k] = v

        logger.info(f"Active Learning Strategies:: {list(strategies.keys())}")
        return strategies

    def init_scoring_methods(self) -> Dict[str, ScoringMethod]:
        methods: Dict[str, ScoringMethod] = {}
        if strtobool(self.conf.get("skip_scoring", "true")):
            return methods

        for n, task_config in self.models.items():
            s = task_config.scoring_method()
            if not s:
                continue
            s = s if isinstance(s, dict) else {n: s}
            for k, v in s.items():
                logger.info(f"+++ Adding Scoring Method:: {k} => {v}")
                methods[k] = v

        logger.info(f"Active Learning Scoring Methods:: {list(methods.keys())}")
        return methods

"""
Example to run train/infer/batch infer/scoring task(s) locally without actually running MONAI Label Server

More about the available app methods, please check the interface monailabel/interfaces/app.py

"""

def main():
    import argparse
    import shutil
    from pathlib import Path

    from monailabel.utils.others.generic import device_list, file_ext

    os.putenv("MASTER_ADDR", "127.0.0.1")
    os.putenv("MASTER_PORT", "1234")

    logging.basicConfig(
        level=logging.INFO,
        format="[%(asctime)s] [%(process)s] [%(threadName)s] [%(levelname)s] (%(name)s:%(lineno)d) - %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
        force=True,
    )

    home = str(Path.home())
    studies = f"{home}/Dataset/Radiology"

    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--studies", default=studies)
    parser.add_argument("-m", "--model", default="segmentation")
    parser.add_argument("-t", "--test", default="batch_infer", choices=("train", "infer", "batch_infer"))
    args = parser.parse_args()

    app_dir = os.path.dirname(__file__)
    studies = args.studies
    conf = {
        "models": args.model,
        "preload": "false",
    }

    app = MyApp(app_dir, studies, conf)

    # Infer
    if args.test == "infer":
        sample = app.next_sample(request={"strategy": "first"})
        image_id = sample["id"]
        image_path = sample["path"]

        # Run on all devices
        for device in device_list():
            res = app.infer(request={"model": args.model, "image": image_id, "device": device})
            # res = app.infer(
            #     request={"model": "vertebra_pipeline", "image": image_id, "device": device, "slicer": False}
            # )
            label = res["file"]
            label_json = res["params"]
            test_dir = os.path.join(args.studies, "test_labels")
            os.makedirs(test_dir, exist_ok=True)

            label_file = os.path.join(test_dir, image_id + file_ext(image_path))
            shutil.move(label, label_file)

            print(label_json)
            print(f"++++ Image File: {image_path}")
            print(f"++++ Label File: {label_file}")
            break
        return

    # Batch Infer
    if args.test == "batch_infer":
        app.batch_infer(
            request={
                "model": args.model,
                "multi_gpu": False,
                "save_label": True,
                "label_tag": "original",
                "max_workers": 1,
                "max_batch_size": 0,
            }
        )

        # app.batch_infer(
        #     request={
        #         "model": "pipeline",
        #         "multi_gpu": False,
        #         "save_label": True,
        #         "label_tag": "original",
        #         "max_workers": 1,
        #         "max_batch_size": 0,
        #     }
        # )

        return

    # Train
    app.train(
        request={
            "model": args.model,
            "max_epochs": 10,
            "dataset": "Dataset",  # PersistentDataset, CacheDataset
            "train_batch_size": 1,
            "val_batch_size": 1,
            "multi_gpu": False,
            "val_split": 0.1,
        },
    )

if __name__ == "__main__":
    # export PYTHONPATH=~/Projects/MONAILabel:`pwd`
    # python main.py
    main()