import requests
from janome.tokenizer import Tokenizer
from bs4 import BeautifulSoup
from wordcloud import WordCloud

# Fetch html
url = "https://www.aozora.gr.jp/cards/000879/files/42_15228.html"
r = requests.get(url, timeout=10)

# Parse
soup = BeautifulSoup(r.content, 'lxml')

# Extract text
text_elm = soup.find('div', attrs={'class': 'main_text'})
# Remove kana element (ruby)
[e.extract() for e in text_elm.select('rt')]

# Get the text kana removed
text = text_elm.text

# Fetch Stop word
stop_word_url = 'http://svn.sourceforge.jp/svnroot/slothlib/CSharp/' \
                'Version1/SlothLib/NLP/Filter/StopWord/word/Japanese.txt'
r_stopword = requests.get(stop_word_url, timeout=10)
stop_words = r_stopword.text.split()

# Split into words
t = Tokenizer()
words = []
for token in t.tokenize(text):
    if token.part_of_speech.split(',')[0] == '名詞' and token.surface not in stop_words:
        words.append(token.surface)

# Generate wordcloud object
font_path = r"C:\Users\agoot\Desktop\LightNovelPOP_FONT\lanobe_pop.otf"
wordcloud = WordCloud(background_color="white", font_path=font_path, regexp=r"\w+").generate(" ".join(words))

# Output graph
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()


n,a,b,c = map(int,input().split())
v = [a,b,c]
u = ["A","B","C"]
nn = a+b+c

if nn==0:
    print("No")
    exit()

ans = []
for _ in range(n):
    s = input()
    x = -1
    if s=="AB":
        x = 0
    if s=="BC":
        x = 1
    if s=="AC":
        x = 2

    y = (x+1)%3

    v[x]+=1
    v[y]-=1
    cnt = 0
    for vi in v:
        if vi==0: cnt+=1
        if vi<0: cnt+=10
    if cnt<2:
        ans.append(u[x])
        continue

    v[x]-=2
    v[y]+=2
    cnt = 0
    for vi in v:
        if vi==0: cnt+=1
        if vi<0: cnt+=10
    if cnt<2:
        ans.append(u[y])
        continue

    break

if len(ans)==n:
    print("Yes")
    for ai in ans:
        print(ai)
else:
    print("No")


import numpy as np
import pandas as pd
import sys
import os

inDir = sys.argv[1]
mappingFile = sys.argv[2]
out = sys.argv[3]

def transformGenExpr(inPath, outPath):
    files = os.listdir(inPath)

    mapOfColumns = {}  # This will contain one list for each file, with the contents of column of expression values
    id = []
    fileCounter = 0

    for inFile in files:
        filename = inFile.split(".")[0]
        columnData = []
        f = open(inPath + inFile, 'r')
        first_line = f.readline()
        for line in f:
            b = line.split('\t')
            if fileCounter == 0:
                id.append(b[0])
            if len(b) >= 3:
                columnData.append(b[3].rstrip())
            else:
                # Row data is too short
                columnData.append('Error')
        f.close()
        mapOfColumns[filename] = columnData
        fileCounter = fileCounter + 1

    outFile = open(outPath, 'w')
    outFile.write("\t" + '\t'.join(id))
    outFile.write("\n")
    for key in mapOfColumns:
        outFile.write(key + "\t")
        outFile.write('\t'.join(mapOfColumns.get(key)))
        outFile.write('\n')
    outFile.close()

def transformMIRNAExpr(inPath, mappingPath, outPath):
    files = os.listdir(inPath)
    mapOfColumns = {}  # This will contain one list for each file, with the contents of column of expression values

    uniqId = getUniqMirna(inPath)
    mapping = getMapping(uniqId, mappingPath)

    for inFile in files:
        filename = inFile.split(".")[0].split("_")[1]
        columnData = {}
        f = open(inPath + inFile, 'r')
        for line in f:
            b = line.split('\t')
            if len(b) >= 2:
                columnData[b[0].rstrip()] = b[1].rstrip()
        f.close()

        result = []
        #get only needed data:
        for key in uniqId:
            if key in columnData.keys():
                result.append(columnData.get(key))
            else:
                result.append(0)
        mapOfColumns[filename] = result

    outFile = open(outPath, 'w')
    outFile.write("\t" + '\t'.join(mapping.get(key) for key in uniqId))
    outFile.write("\n")
    for key in mapOfColumns:
        outFile.write(key + "\t")
        outFile.write('\t'.join(str(x) for x in mapOfColumns.get(key)))
        outFile.write('\n')
    outFile.close()

def getUniqMirna(inPath):
    files = os.listdir(inPath)
    uniqId = {}
    for inFile in files:
        f = open(inPath + inFile, "r")
        for line in f:
            b = line.split("\t")
            if b[0].rstrip() in uniqId.keys():
                uniqId[b[0].rstrip()] = uniqId.get(b[0].rstrip()) + 1
            else:
                uniqId[b[0].rstrip()] = 1
        f.close()

    #filter mirna, that is <20 times in the data found
    return [k for k, v in uniqId.items() if v > 20]

def getMapping(uniqMirna, mappingPath):
    mappingMap = {}
    f = open(mappingPath, "r")
    for line in f:
        b = line.split("\t")
        mimat = b[0].rstrip()

        for id in b[1:-1]:
            mappingMap[id.rstrip()] = mimat

    return mappingMap

    f.close()
########################################################################################################################
#Methodenaufrufe
########################################################################################################################
transformGenExpr(inDir, out)
#transformMIRNAExpr(inDir, mappingFile,out)


import sys
import time
from twisted.python import log
from twisted.internet import defer
from operation.basics import Request, Response

TRACE = 15
VERBOSE = 17

import logging
logging.addLevelName(TRACE, 'TRACE')
logging.addLevelName(VERBOSE, 'VERBOSE')

class Play(Request):

    class Answer(Response):

        def __init__(self, gnubg, opponent, ML, resume):
            Response.__init__(self, '')
            self.gnubg = gnubg
            self.status = {}
            if resume:
                self.expected_answer = "points for %s:" % opponent
                self.complex_answer(self.resume_answer, [opponent,])
            else:
                self.expected_answer = "Starting a new game with %s." % opponent
                self.complex_answer(self.newgame_answer, [opponent, ML])

##        def resume_answer(self, expected, message):
##> turn: hannes.
##match length: 5
##points for helena: 0
##points for hannes: 0
## #board
##> Please move 2 pieces.

        def newgame_answer(self, expected, message):
            opponent = expected[0]
            ML = expected[1]
            try:
                if not message[1].startswith('You rolled'):
                    return False
                rollout = message[1]
                for msg in message[2:]:
                    if msg.startswith('You rolled'):
                        rollout = msg
                        continue
                a, b = rollout.split(',')
                dice_me = a.split(' ')[-1]
                dice_him = b.split(' ')[-1]
                self.status['dice'] = (dice_me, dice_him)
                if dice_me > dice_him:
                    idx = 1
                    for msg in message[2:]:
                        idx += 1
                        if msg.startswith('match_id'):
                            log.msg('ME starts', logLevel=VERBOSE)
                            m,p = msg.split()[1].split(':')
                            self.status['match_id'] = m
                            self.status['position_id'] = p
                            match_id = '%s:%s' % (m, p)
                            log.msg('match_id: %s' % match_id, logLevel=logging.DEBUG)
                            # rausfinden, ob ich dran
                            # deferred Frage abschicken; wenn antwort, sofort senden
                            log.msg('NEED ORACLE 73', logLevel=TRACE)
                            self.oracle = self.gnubg.ask_gnubg('bestMove: %s' % match_id)
                        if msg.startswith('Please move'):
                            log.msg('ME moves', logLevel=VERBOSE)
                            del message[:idx]
                            break
                elif dice_me < dice_him:
                    idx = 1
                    waitfor = '%s makes the first move.' % opponent
                    for msg in message[2:]:
                        idx += 1
                        if msg == waitfor:
                            log.msg('Opponent starts', logLevel=VERBOSE)
                            del message[:idx]
                            break
                # TODO: jetzt mussen folgende zwei zeilen 'umgehangt' werden
##                self.expected_answer = "Starting a new game with %s." % opponent
##                self.complex_answer(self.resume_answer, [opponent,])
            except:
                raise
                return False
            return True

        def resume_answer(self, expected, message):
            opponent = expected[0]
            return True

        def get_oracle(self,):
            return getattr(self, 'oracle', None)

    def __init__(self, dispatch, manage, opponent, ML, resume=False):
        self.gnubg = dispatch.get_gnubg()
        self.opponent = opponent
        self.ML = ML
        self.expected = self.Answer(self.gnubg, opponent, ML, resume)
        self.expected.send_move = self.send_move
        self.label = 'PLAY'
        self.sent_request = time.time()
        Request.__init__(self, dispatch, manage,)

    def send_move(self, move):
        log.msg('got move: %s' % move, logLevel=logging.DEBUG)
        # TODO: class Play needs to be cleaned up!!!!!
        mv = ' '.join(['m',] + [str(25-int(m)) for m in move.strip('()').split(',')])
        #self.send_command(mv)

    def received(self, message):
        log.msg(self.msg_tests % message[0], logLevel=VERBOSE)
        expected_answer = self.expected.test(message)
        status = self.expected.status   # TODO: brauch ich das noch??
        if expected_answer:
            oracle = self.expected.get_oracle()
            log.msg('does oracle work?? %s' % oracle, logLevel=logging.DEBUG)
            if not oracle is None:
                oracle.addCallback(self.send_move)
            log.msg(self.msg_applies + '+'*40, logLevel=VERBOSE)
            time_used = time.time() - self.sent_request
            log.msg(self.msg_waited % time_used, logLevel=logging.INFO)
            self.purge()
            Turn(self.dispatch, self.manage,)
            del message[0]
        else:
            log.msg('PLAY applies NOT '+'-'*36, logLevel=VERBOSE)
        return expected_answer

    def update(self,):
        self.manage[self.expected.expected_answer] = self

class Action:
    """Action objects can take one of six orders and then, if appropriate, ask
    gnubg for the proper action to take in this case.
"""
    def __init__(self, order, parameters, gnubg, callback):
        log.msg('create action with (%s - %s)' % (order, parameters),
                                                        logLevel=logging.DEBUG)
        self.gnubg = gnubg
        self.callback = callback
        self._parameters = parameters
        self._order = {
            'double': self._double,     # state B - double or roll? resign?
            'move': self._move,         # state E - which move? resign?
            'take': self._take,         # state C - take the cube?
            'accept': self._accept,     # state J - accept the resign?
            'rejected': self._rejected, #           handle reject to own resign
            'join': self._true,         # state G - return the neccessary 'join'
            'relax': self._relax,       # state G - be polite and return thanks
            }[order]
        self._order(parameters)

    def redo(self,):
        self._order(self._parameters)

    def _double(self, parameters):
        self.oracle = self.gnubg().ask_gnubg('double: %s resign' % parameters[0])
        log.msg('got DOUBLE oracle: %s' % self.oracle, logLevel=logging.DEBUG)
        if not self.oracle is None:
            self.oracle.addCallback(self.callback)

    def _take(self, parameters):
        self.oracle = self.gnubg().ask_gnubg('take: %s' % parameters[0])
        log.msg('got TAKE oracle: %s' % self.oracle, logLevel=logging.DEBUG)
        if not self.oracle is None:
            self.oracle.addCallback(self.callback)

    def _move(self, parameters):
        # No resign decision here. Gnubg does not either and it does not
        # really make things worse, if we simply move and decide later.
        self.oracle = self.gnubg().ask_gnubg('bestMove: %s %s' % \
                                            (parameters[0], parameters[1]))
        log.msg('got MOVE oracle: %s' % self.oracle, logLevel=logging.DEBUG)
        if not self.oracle is None:
            self.oracle.addCallback(self.callback)

    def _accept(self, parameters):
        self.oracle = self.gnubg().ask_gnubg('accept: %s %s' % \
                                            (parameters[0], parameters[1]))
        log.msg('got ACCEPT oracle: %s' % self.oracle, logLevel=logging.DEBUG)
        if not self.oracle is None:
            self.oracle.addCallback(self.callback)

    def _rejected(self, parameters):
        """Bots own resign has been rejected. This is just returned to Turn()."""
        self.callback()

    def _relax(self, parameters):
        self.oracle = defer.Deferred()
        self.oracle.addCallback(self.callback)
        log.msg('sending relax: %s' % parameters[1], logLevel=logging.DEBUG)
        self.oracle.callback(parameters[1] == 'True')

    def _true(self, parameters):
        self.oracle = defer.Deferred()
        self.oracle.addCallback(self.callback)
        self.oracle.callback(True)

class Turn(Request):
    """A Turn() represents one turn in a game of backgammon. The server will
    notify the bot of a turn to take. That notification is introduced by the
    bot unique id (bot_uid) and comprises a 'command' and parameters.
    An action has to be taken according to the given command.

    # TODO: Beispiel von solchen Messages!
"""
    def __init__(self, dispatch, manage,):
        self.gnubg = dispatch.get_gnubg
        self.expected = dispatch.bot_uid
        self.direction = dispatch.direction
        self._callback = {'double': self.send_double,
                          'move': self.send_move,
                          'take': self.send_take,
                          'accept': self.send_accept,
                          'rejected': self.handle_rejections,
                          'join': self.send_join,
                          'relax': self.send_thanks,
                         }
        self.label = 'TURN'
        self.sent_request = time.time()
        Request.__init__(self, dispatch, manage,)

    def send_double(self, double):
        log.msg('got double decision: %s' % double, logLevel=logging.DEBUG)
        # TODO: the upcoming message self.msg_waited looks wrong in what
        #       lap it measures. It does probably erroneously overwrite
        #       another self.msg_waited!!!!!!

        #self.msg_waited = 'double waited for answer %s seconds' % \
         #                                       time.time() - start_time
        if double.startswith('resign'):
            log.msg('in resign consideration', logLevel=TRACE)
            possible = self.dispatch.resigns.possible(double)
            log.msg('got resign possible: %s' % possible, logLevel=logging.DEBUG)
            if possible:
                self.send_command(possible)
            else:
                self.send_command('roll')
                self.dispatch.resigns.reset()
        else:
            result = double == 'double'
            self.send_command({True: 'double', False: 'roll'}[result])
        self.purge()

    def send_take(self, take):
        log.msg('got take decision: %s' % take, logLevel=logging.DEBUG)
        self.send_command({True: 'accept', False: 'reject'} \
                                                [take in ('take', 'beaver')])
        self.purge()

    def send_move(self, move):
        log.msg('got move: %s' % move, logLevel=logging.DEBUG)
        # No resign decision here. See Action._move() above.
        if self.direction == '+':
            mv = ' '.join(['m',] + \
                        [str(25-int(m)) for m in move.strip('()').split(',')])
        else:
            mv = ' '.join(['m',] + move.strip('()').split(','))
        self.send_command(mv)
        self.purge()

    def send_accept(self, accept):
        log.msg('got accept decision: %s' % accept, logLevel=logging.DEBUG)
        self.send_command(accept)
        self.purge()

    def handle_rejections(self,):
        self.dispatch.resigns.rejected()
        self.purge()

    def send_join(self, join):
        log.msg('got join', logLevel=logging.DEBUG)
        self.send_command('join')
        self.dispatch.resigns.reset()
        self.purge()

    def send_thanks(self, congrats):
        opponent = getattr(self.dispatch, 'opponent', False)
        self.dispatch.relax_hook()
        if not opponent:
            log.msg('cannot send thanks: opponent missing!', logLevel=logging.DEBUG)
            return
        if congrats:
            msg = "tell %s Congratulations and thanks for playing this " \
                                                        "match :)." % opponent
        else:
            msg = "tell %s Thanks for playing this match :)." % opponent
        del self.dispatch.opponent
        log.msg('sending thanks: %s' % msg, logLevel=VERBOSE)
        self.send_command(msg)
        self.purge()

    def received(self, message):
        log.msg(self.msg_tests % message[0], logLevel=VERBOSE)
        expected_reaction = message[0].split()
        ret = len(expected_reaction) > 2
        if ret:
            uid, order = expected_reaction[:2]
            parameters = expected_reaction[2:]
            callback = self._callback[order]
            self.sent_action = time.time()
            self.action = Action(order, parameters, self.gnubg, callback)
            self.dispatch.pending_action = self.action
            log.msg(self.msg_applies + '+'*40, logLevel=VERBOSE)
            time_used = time.time() - self.sent_request
            log.msg(self.msg_waited % time_used, logLevel=logging.INFO)
            Turn(self.dispatch, self.manage,)
            del message[0]
        else:
            log.msg('TURN applies NOT '+'-'*36, logLevel=VERBOSE)
        return ret

    def purge(self,):
        if hasattr(self.dispatch, 'pending_action'):
            del self.dispatch.pending_action
        Request.purge(self)


import os
from pyrogram import Client, filters
from pyrogram.types import Message, User

@Client.on_message(filters.new_chat_members)
async def welcome(bot,message):
	chatid= message.chat.id
	await bot.send_message(text=f"Welcome {message.from_user.mention} to {message.chat.username} ,  Happy to have here ഞാൻ രമണൻ എന്റെ Groupലാണ് നിങ്ങൾ Movie Search ചെയ്യുന്നത് അതുകൊണ്ട് Movieയുടെ Name Google ൽ നോക്കി Groupൽ Search ചെയ്യുക. Movieയുടെ Name ൽ Spelling Mistake ഉണ്ടെങ്കിൽ ഞാൻ നിങ്ങൾക്ക് Movie തരുന്നതല്ല.             എന്ന് നിങ്ങളുടെ രമണൻ",chat_id=chatid)

@Client.on_message(filters.left_chat_member)
async def goodbye(bot,message):
	chatid= message.chat.id
	await bot.send_message(text=f"Bye ,  {message.from_user.mention} , പോയിട്ട് Vada Ramanan നിന്നെ Wait ചെയ്യുന്നുണ്ട്",chat_id=chatid)


def solution(n, t, m, timetable):
    answer = ""

    timetable = [int(time[:2]) * 60 + int(time[3:]) for time in timetable]
    timetable.sort()

    busTime = [9 * 60 + t * i for i in range(n)]

    i = 0  # 버스에 탈 크루의 인덱스
    for bt in busTime:  # 버스 도착 시간을 순회하면서
        cnt = 0  # 버스에 타는 크루 수
        while cnt < m and i < len(timetable) and timetable[i] <= bt:
            i += 1
            cnt += 1
        if cnt < m:  # 버스에 자리 남았으면 버스타임에 내가 타면 됨
            answer = bt
        else:  # 버스에 탈 자리 없으면 맨 마지막 크루보다 1분전에 도착
            answer = timetable[i - 1] - 1
    answer = str(answer // 60).zfill(2) + ":" + str(answer % 60).zfill(2)
    return answer


# The Collatz Sequence

def collatz(number):                 # collatz function
    print(number," ", end= "")
    if number == 1:
        return number
    elif number%2 == 0:
        return collatz(number // 2)
    else:
        return collatz(3 * number + 1)

# main program
print("Enter a Number : ", end="")
num = int(input())
print("\nReturning : ",collatz(num))


from model.contact import Contact
from random import randrange

def test_modify_contact_by_index(app):
    if app.contact.count() == 0:
        app.contact.create(Contact(firstname='fnfnfnfnf', lastname='llnlnlnln'))
    old_contacts = app.contact.get_contact_list()
    index = randrange(len(old_contacts))
    contact = Contact(firstname="New name")
    contact.id = old_contacts[index].id
    contact.lastname = old_contacts[0].lastname
    app.contact.open_contact_to_edit_by_index(contact, index)
    new_contacts = app.contact.get_contact_list()
    assert len(old_contacts) == app.contact.count()
    old_contacts[index] = contact
    assert sorted(old_contacts, key=Contact.id_or_max) == sorted(new_contacts, key=Contact.id_or_max)


import time
import json
import random
import hashlib
from copy import deepcopy
import threading
from redis.exceptions import RedisError
from packageship.application.common import constant
from packageship.libs.log import LOGGER

class BufferCache:
    """
    Data in redis cache,check cache data in redis database

    Attributes:
        _depend: Instances that depend on the query
        _func: The method of being decorated
    """

    _lock = threading.Condition()
    _active_thread = dict()

    def __init__(self, depend):
        self._depend = depend
        self._func = None
        self._args = None
        self._kwargs = None

    def _hash_key(self):
        """
        Description: After sorting the values of the dictionary type
                     calculate the md5 encrypted hash value

        Args:
            encrypt_obj: Dictionaries that need to be computed by hash values
        """
        if not self._args and not self._kwargs:
            return None
        kwargs = deepcopy(self._kwargs)
        kwargs["args"] = ",".join(self._args)

        ret_dict = {}

        def spear_kwargs(old_dict):
            """to spear kwargs
            if key is 'db_priority' and the value is list,
                do not sort list content,and merge to string
                because need to keep the order of the database.

            Other list cases need to be sorted first,then merge to string

            There is no strict order requirement for the remaining keys and values,
                and can be directly converted to strings.

            Args:
                old_dict (dict): the input kwargs

                e.g:
                input kwargs:
                {'depend_type': 'installdep',
                'packagename': ['Judy','Judy1'],
                'parameter': {'db_priority': ['os_version_2', 'os_version_1'], 'level': 0}}

                To:

                {'depend_type': 'installdep',
                'packagename': 'Judy,Judy1',  # be sorted first then merge to string
                'db_priority':'os_version_2,os_version_1'  # do not sorted, can merge to string
                'level':'0'
                }
            """
            for k, v in old_dict.items():
                if isinstance(v, dict):
                    spear_kwargs(v)
                else:
                    if k == "db_priority" and isinstance(v, list):
                        v = ",".join(v)
                    elif isinstance(v, list):
                        v = ",".join(sorted(v))
                    else:
                        v = str(v)
                    ret_dict.update({k: v})

        spear_kwargs(kwargs)
        kw_str = "key"

        for key, val in sorted(ret_dict.items(), key=lambda x: x[0]):
            kw_str += "," + key + ":" + val

        return hashlib.sha256(kw_str.encode("utf8")).hexdigest()

    def _set_cache(self, key):
        """
        Description: Set the cache value for Redis

        Args:
            key: cached key
        """
        self._lock.acquire()
        if key not in self._active_thread:
            self._active_thread[key] = threading.currentThread().ident
        self._lock.release()

        while threading.currentThread().ident != self._active_thread.get(key):
            if constant.REDIS_CONN.exists(key):
                self._set_val(key)
                if key in self._active_thread:
                    del self._active_thread[key]
                return
            time.sleep(round(random.random(), 3))

        self._func(*self._args, **self._kwargs)
        constant.REDIS_CONN.hmset(
            key,
            dict(
                source_dict=json.dumps(self._depend.source_dict),
                binary_dict=json.dumps(self._depend.binary_dict),
                log_msg=self._depend.log_msg,
            ),
        )

    def _set_val(self, key):
        """
        Description: Gets the cached hash value and assigns it to
                     the corresponding dependent instance

        Args:
            key: cached key
        """

        self._depend.source_dict = json.loads(
            constant.REDIS_CONN.hget(key, "source_dict")
        )
        self._depend.binary_dict = json.loads(
            constant.REDIS_CONN.hget(key, "binary_dict")
        )
        self._depend.log_msg = constant.REDIS_CONN.hget(key, "log_msg")

        if not self._depend.log_msg:
            return

        LOGGER.warning(self._depend.log_msg)

    def _cache(self):
        """
        Description: Gets the dependency value in the cache or executes
                     the method to get the dependency data

        """
        key = self._hash_key()
        if not key:
            return
        key = "pkgship_" + key

        try:
            if constant.REDIS_CONN.exists(key):
                self._set_val(key)
                return
            self._set_cache(key)

        except RedisError as error:
            LOGGER.warning(error)
            self._func(*self._args, **self._kwargs)

    def __call__(self, func):
        self._func = func

        def wrapper(*args, **kwargs):
            self._args, self._kwargs = args, kwargs
            self._cache()

        return wrapper

buffer_cache = BufferCache

__all__ = ["buffer_cache"]


from django.db import models

from django.db.models.signals import post_save
from django.dispatch import receiver

# Create your models here.

class Student(models.Model):
    # id = models.AutoField()
    name  = models.CharField(max_length=100)
    age = models.IntegerField()
    email = models.EmailField()
    address = models.TextField( null = True , blank=True)

class Car(models.Model):
    car_name = models.CharField(max_length=500)
    speed = models.IntegerField(default=50)

    def __str__(self) -> str:
        return self.car_name

# CRUD - CREATE , READ , UPDATE, DELETE

# signals :  pre_save , post_save , pre_delete , post_delete

# cars.com/get_speed/?car_name = Nexon
# {
#     car_speed : 120
# }

# @receiver(post_save , Sender = Car)
# def call_car_api(sender , instance , **kwargs):
#     print("Request finished!")


from datetime import datetime, timedelta
import sys
from mt5Connection import mt5
from inputSymbols import getSymbols
from dbOperations import getConnection, getLastDay, insertDatas, dropCol, getUTC
from Share import Share
from logManager import writeLog

client, db = getConnection()
reference = "PETR3"
symbols = getSymbols()
y_date = datetime.today() - timedelta(days=3)
date_zero =  getUTC(datetime(2000, 1, 1, 0, 0))
reference_date = getUTC(datetime(y_date.year, y_date.month, y_date.day, 23, 59)) # Data mais recente
last_day_MT5 = mt5.copy_rates_from(reference, mt5.TIMEFRAME_M5, reference_date, 1)

if (last_day_MT5 is None):
    sys.exit("MT5 - Falha ao receber ultima data.")

date_MT5 = getUTC(datetime.utcfromtimestamp(last_day_MT5[0]['time']))
file = open("logs/log_ErrosUpdate.txt", "a")

def newStock(symbol_db_name, symbol_mt5_name):
    count = 0
    group_rates = []
    rates = mt5.copy_rates_range(symbol_mt5_name, mt5.TIMEFRAME_M5, date_zero, reference_date)
    if rates is None:
        writeLog(file, f'MT5: Falha ao obter dados de {symbol_mt5_name} - (newStock)')
        return

    for rate in rates:
        group_rates.append(
            vars(
                Share(
                    rate['time'], rate["open"], rate["high"],
                    rate["low"], rate["close"], rate["tick_volume"],
                    rate["real_volume"]
                )
            )
        )
        count += 1

        if count == 10000:
            insertDatas(db, group_rates, symbol_db_name)
            group_rates = []
            count = 0

    if len(group_rates) != 0:
        insertDatas(db, group_rates, symbol_db_name)

def updateStock(symbol_db_name, symbol_mt5_name, date_DB):
    count = 0
    group_rates = []
    date_start = date_DB + timedelta(minutes=1)
    rates = mt5.copy_rates_range(symbol_mt5_name, mt5.TIMEFRAME_M5, date_start, reference_date)

    if rates is None:
        writeLog(file, f'MT5: Falha ao obter dados de {symbol_mt5_name} - (updateStock)')
        return

    for rate in rates:
        group_rates.append(
            vars(
                Share(
                    rate['time'], rate["open"], rate["high"],
                    rate["low"], rate["close"], rate["tick_volume"],
                    rate["real_volume"]
                )
            )
        )
        count += 1

        if count == 10000:
            insertDatas(db, group_rates, symbol_db_name)
            group_rates = []
            count = 0

    if len(group_rates) != 0:
        insertDatas(db, group_rates, symbol_db_name)

def updateDB(symbol_db_name, last_day_DB, symbol_mt5_name):
    if(last_day_DB == None):
        newStock(symbol_db_name, symbol_mt5_name)
        return

    date_DB = getUTC(last_day_DB['date'])

    dividend_test = mt5.copy_rates_range(symbol_mt5_name, mt5.TIMEFRAME_M5, date_DB, date_DB)

    if date_DB.hour >= 17 and symbol_db_name != "WIN":
        writeLog(file, f'DB: Ajuste de horário em {symbol_mt5_name}')
        drop_res = dropCol(db, symbol_db_name)
        if 'ns' in drop_res:
            newStock(symbol_db_name, symbol_mt5_name)
        return

    if dividend_test is None or len(dividend_test) == 0:
        writeLog(file, f'MT5: Falha ao obter dados de {symbol_mt5_name} - (dividendTest)')
        return

    if (date_DB == getUTC(datetime.utcfromtimestamp(dividend_test[0]['time'])) and dividend_test[0]['close'] != last_day_DB['close']):
        writeLog(file, f'DB: Ajuste de dividendos em {symbol_mt5_name}')
        drop_res = dropCol(db, symbol_db_name)
        if 'ns' in drop_res:
            newStock(symbol_db_name, symbol_mt5_name)
        return

    if (date_MT5 <= date_DB): return

    updateStock(symbol_db_name, symbol_mt5_name, date_DB)

print('Updating...')
writeLog(file, f'DB: Atualização da base de dados - (Update)')

for symbol_db_name, symbol_mt5_name  in symbols.items():
    updateDB(symbol_db_name, getLastDay(db, symbol_db_name), symbol_mt5_name)

file.close()
client.close()
print("Updated!")


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

question_bank = []
for question in question_data:
    question_text = question["question"]
    question_answer = question["correct_answer"]
    # object of class Question/ Constructor
    new_question = Question(question_text, question_answer)
    question_bank.append(new_question)# list of objects

# print(question_bank)

quiz = QuizBrain(question_bank)
while quiz.still_has_questions():
    quiz.next_question()

print(f"You've completed the quiz with a score of {quiz.score}/{len(question_bank)}")


import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer

nltk.download('stopwords')

def converter_valor(valor_puro, moeda):
    cotacao = 1

    if moeda == "DOLAR":
        cotacao = 4.78
    elif moeda == "EURO":
        cotacao = 5.22

    return valor_puro * cotacao

def unirDemandaComUsuario(demandas, usuarios):
    tipoSolicitante = []
    cargoSolicitante = []
    departamentoSolicitante = []
    nomeSolicitante = []
    setorSolicitante = []

    for index, demanda in demandas.iterrows():
        usuario = usuarios.loc[usuarios['id_usuario'] == demanda.id_usuario]
        index = usuario.index[0]

        tipoSolicitante.append(usuarios.at[index, "dtype"])
        cargoSolicitante.append(usuario.at[index, "cargo"])
        departamentoSolicitante.append(usuario.at[index, "departamento"])
        nomeSolicitante.append(usuario.at[index, "nome_usuario"])
        setorSolicitante.append(usuario.at[index, "setor"])

    demandas["tipo_solicitante"] = tipoSolicitante
    demandas["cargo_solicitante"] = cargoSolicitante
    demandas["departamento_solicitante"] = departamentoSolicitante
    demandas["nome_solicitante"] = nomeSolicitante
    demandas["setor_solicitante"] = setorSolicitante

    demandas.drop(columns=["id_usuario"], inplace=True)

    return demandas

def unirDemandaComBeneficio(demandas, beneficiosDemanda):
    qtd_beneficio_real = []
    qtd_beneficio_potencial = []
    qtd_beneficio_qualitativo = []
    valor_total_beneficios_reais = []
    valor_total_beneficios_potenciais = []
    descricao_beneficios = []

    for index, demanda in demandas.iterrows():
        beneficios = beneficiosDemanda.loc[beneficiosDemanda.id_demanda == demanda.id_demanda]
        qtd_real = 0
        qtd_potencial = 0
        qtd_qualitativo = 0
        valor_total_real = 0.0
        valor_total_potencial = 0.0
        descricao_beneficios_demanda = ""

        for index, beneficio in beneficios.iterrows():
            tipo = beneficio.tipo_beneficio

            if tipo == "QUALITATIVO":
                qtd_qualitativo += 1
            elif tipo == "REAL":
                if np.isnan(beneficio.valor):
                    continue

                qtd_real += 1
                valor_total_real += converter_valor(beneficio.valor, beneficio.moeda)
            elif tipo == "POTENCIAL":
                if np.isnan(beneficio.valor):
                    continue

                qtd_potencial += 1
                valor_total_potencial += converter_valor(beneficio.valor, beneficio.moeda)

            descricao_beneficios_demanda += " " + beneficio.descricao

        qtd_beneficio_real.append(qtd_real)
        qtd_beneficio_potencial.append(qtd_potencial)
        qtd_beneficio_qualitativo.append(qtd_qualitativo)
        valor_total_beneficios_reais.append(valor_total_real)
        valor_total_beneficios_potenciais.append(valor_total_potencial)
        descricao_beneficios.append(descricao_beneficios_demanda)

    demandas["qtd_beneficio_real"] = qtd_beneficio_real
    demandas["qtd_beneficio_potencial"] = qtd_beneficio_potencial
    demandas["qtd_beneficio_qualitativo"] = qtd_beneficio_qualitativo
    demandas["valor_total_beneficios_reais (BRL)"] = valor_total_beneficios_reais
    demandas["valor_total_beneficios_potenciais (BRL)"] = valor_total_beneficios_potenciais
    demandas["descricao_beneficios"] = descricao_beneficios

    return demandas

def unirDemandaComCCs(demandas, CCs, CCsDemanda):
    quantidade_CC_demanda = []
    nomes_CC_demanda = []

    for index, demanda in demandas.iterrows():
        centros_custo_demanda = CCsDemanda[CCsDemanda.id_demanda == demanda.id_demanda]
        quantidade_CCs = 0
        nomes_CC = ""

        for index, centro_custo in centros_custo_demanda.iterrows():
            quantidade_CCs += 1
            nome = CCs.loc[CCs.id_centrocusto == centro_custo.id_centrocusto]
            nomes_CC += " " + nome.at[nome.index[0], "nome_centro_custo"]

        quantidade_CC_demanda.append(quantidade_CCs)
        nomes_CC_demanda.append(nomes_CC)

    demandas["quantidade_CC"] = quantidade_CC_demanda
    demandas["nomes_CC"] = nomes_CC_demanda

    return demandas

def transformarBancoToDataFrame(demandas, usuarios, beneficios, CCs, CCsDemanda):
    df_demandas = pd.DataFrame(demandas)
    df_usuarios = pd.DataFrame(usuarios)
    df_beneficios = pd.DataFrame(beneficios)
    df_CCs = pd.DataFrame(CCs)
    df_CCsDemanda = pd.DataFrame(CCsDemanda)

    df_demandas_transformadas = unirDemandaComUsuario(df_demandas, df_usuarios)
    df_demandas_transformadas = unirDemandaComBeneficio(df_demandas_transformadas, df_beneficios)
    df_demandas_transformadas = unirDemandaComCCs(df_demandas_transformadas, df_CCs, df_CCsDemanda)

    return df_demandas_transformadas

def adicionarNovaColuna(df_alvo, nome_coluna, df_info):
    stemmer = PorterStemmer()
    stop_words = set(stopwords.words('portuguese'))

    palavras = df_info[nome_coluna].values.astype('U').tolist()
    palavras = [palavra.lower() for palavra in palavras]
    palavras = [palavra for palavra in palavras if palavra not in stop_words]
    palavras = [stemmer.stem(palavra) for palavra in palavras]

    cvec = CountVectorizer().fit(palavras)
    df_coluna = pd.DataFrame(cvec.transform(palavras).todense(), columns=cvec.get_feature_names_out())

    return df_coluna

def transformarDataFrameToVetorizada(df_demanda):
    df_preparacao_NLP = pd.DataFrame()
    nome_colunas_desejadas = [
        "frequencia_uso", "objetivo", "situacao_atual","titulo_demanda","tipo_solicitante","cargo_solicitante",
        "departamento_solicitante","nome_solicitante", "setor_solicitante","descricao_beneficios","nomes_CC"
    ]

    for nome_coluna in df_demanda.columns:
        if nome_coluna in nome_colunas_desejadas:
            df_coluna_nova = adicionarNovaColuna(df_preparacao_NLP, nome_coluna, df_demanda)
            df_preparacao_NLP = pd.concat([df_preparacao_NLP, df_coluna_nova], axis=1)

    df_preparacao_nao_NLP = df_demanda.drop(columns=nome_colunas_desejadas, axis=1)

    df_preparacao_nao_NLP_normalizada=(df_preparacao_nao_NLP-df_preparacao_nao_NLP.min())/(df_preparacao_nao_NLP.max()-df_preparacao_nao_NLP.min())
    df_preparacao_nao_NLP_normalizada.drop("id_demanda", axis=1, inplace=True)
    df_preparacao_nao_NLP = pd.concat([df_preparacao_nao_NLP["id_demanda"], df_preparacao_nao_NLP_normalizada], axis=1)

    df_preparacao_NLP.reset_index(inplace=True, drop=True)
    df_preparacao_nao_NLP.reset_index(inplace=True, drop=True)

    df_todos_dados = pd.concat([df_preparacao_NLP, df_preparacao_nao_NLP], axis=1)

    return df_todos_dados

def transformarDicionarioToDataFrame(demanda_dict, usuarios_atualizados, CCs_atualizados):
    # separando as informações específicas da demanda em objetos
    demanda = {
        "id_demanda": -1,
        "frequencia_uso": demanda_dict["frequenciaUso"],
        "objetivo": demanda_dict["objetivo"],
        "situacao_atual": demanda_dict["situacaoAtual"],
        "titulo_demanda": demanda_dict["tituloDemanda"],
    }
    CCsDemanda = demanda_dict["centroCustoDemanda"]
    usuario = demanda_dict["usuario"]

    # adicionando as informações de usuário na demanda
    df_uma_demanda = pd.DataFrame(demanda, index=[0])

    usuario = usuarios_atualizados.loc[usuarios_atualizados['id_usuario'] == usuario["idUsuario"]]
    index = usuario.index[0]
    df_uma_demanda.loc[0,"tipo_solicitante"] = usuarios_atualizados.at[index, "dtype"]
    df_uma_demanda.loc[0,"cargo_solicitante"] = usuarios_atualizados.at[index, "cargo"]
    df_uma_demanda.loc[0,"departamento_solicitante"] = usuarios_atualizados.at[index, "departamento"]
    df_uma_demanda.loc[0,"nome_solicitante"] = usuarios_atualizados.at[index, "nome_usuario"]
    df_uma_demanda.loc[0,"setor_solicitante"] = usuarios_atualizados.at[index, "setor"]

    #adicionando as informacoes dos beneficios na demanda
    qtd_real = 0
    qtd_potencial = 0
    qtd_qualitativo = 0
    valor_total_real = 0.0
    valor_total_potencial = 0.0
    descricao_beneficios_demanda = ""

    if "beneficiosDemanda" in demanda_dict:
        beneficios = demanda_dict["beneficiosDemanda"]
        df_beneficios = pd.DataFrame(beneficios)

        for index, beneficio in df_beneficios.iterrows():
            tipo = beneficio.tipoBeneficio

            if tipo == "QUALITATIVO":
                qtd_qualitativo += 1
            elif tipo == "REAL":
                if np.isnan(beneficio.valor):
                    continue

                qtd_real += 1
                valor_total_real += converter_valor(beneficio.valor, beneficio.moeda)
            elif tipo == "POTENCIAL":
                if np.isnan(beneficio.valor):
                    continue

                qtd_potencial += 1
                valor_total_potencial += converter_valor(beneficio.valor, beneficio.moeda)

            descricao_beneficios_demanda += " " + beneficio.descricao

    df_uma_demanda.loc[0,"qtd_beneficio_real"] = qtd_real
    df_uma_demanda.loc[0,"qtd_beneficio_potencial"] = qtd_potencial
    df_uma_demanda.loc[0,"qtd_beneficio_qualitativo"] = qtd_qualitativo
    df_uma_demanda.loc[0,"valor_total_beneficios_reais (BRL)"] = valor_total_real
    df_uma_demanda.loc[0,"valor_total_beneficios_potenciais (BRL)"] = valor_total_potencial
    df_uma_demanda.loc[0,"descricao_beneficios"] = descricao_beneficios_demanda

    # adicionando as informações de centros de custo na demanda
    centros_custo_demanda = pd.DataFrame(CCsDemanda)
    quantidade_CCs = 0
    nomes_CC = ""

    for index, centro_custo in centros_custo_demanda.iterrows():
        quantidade_CCs += 1
        nome = CCs_atualizados.loc[CCs_atualizados.id_centrocusto == centro_custo.idCentroCusto]
        nomes_CC += " " + nome.at[nome.index[0], "nome_centro_custo"]

    df_uma_demanda.loc[0,"quantidade_CC"] = quantidade_CCs
    df_uma_demanda.loc[0,"nomes_CC"] = nomes_CC

    return df_uma_demanda;

def checarSimilaridade(df_demanda, demanda_nova, usuarios_atualizados, CCs_atualizados):
    df_uma_demanda_nova = transformarDicionarioToDataFrame(demanda_nova, usuarios_atualizados, CCs_atualizados)
    df_todas_demandas = pd.concat([df_demanda, df_uma_demanda_nova])
    df_todas_vetorizadas = transformarDataFrameToVetorizada(df_todas_demandas)

    similarity = cosine_similarity(df_todas_vetorizadas.drop("id_demanda", axis=1))
    indice_similaridade = 0.65
    valores_similaridade_ultima_demanda = similarity[-1]
    id_demandas_similares = []

    print(valores_similaridade_ultima_demanda)

    for i in range(len(valores_similaridade_ultima_demanda)):
        valor = valores_similaridade_ultima_demanda[i]
        if valor > indice_similaridade and i != len(valores_similaridade_ultima_demanda) - 1:
            id_demandas_similares.append(int(df_todas_vetorizadas.iloc[i]["id_demanda"]))

    return id_demandas_similares


from django.db import models
from django.conf import settings

class Wallet(models.Model):
  private_key = models.CharField(
    max_length=2048
  )

  public_key = models.CharField(
    max_length=2048
  )

  def __str__(self):
    return "Кошелек: " + self.public_key

class Person(models.Model):
  user = models.OneToOneField(
    to=settings.AUTH_USER_MODEL,
    on_delete=models.SET_NULL,
    blank=True,
    null=True,
  )

  wallet = models.OneToOneField(
    to=Wallet,
    on_delete=models.SET_NULL,
    blank=True,
    null=True,
    related_name="person"
  )

  def __str__(self):
    if self.user:
      return self.user.first_name + ' ' + self.user.last_name

    return "Не привязано"

class Team(models.Model):
  name = models.CharField(
    max_length=200
  )

  members = models.ManyToManyField(
    to=Person,
    null=True,
    blank=False,
  )

  def __str__(self):
    return self.name

class Challenge(models.Model):
  title = models.CharField(
    max_length=200,
    blank=False,
    null=True,
  )

  description = models.CharField(
    max_length=1024,
    blank=False,
    null=True,
  )

  instruction = models.CharField(
    max_length=1024,
    blank=True,
    null=True,
  )

  responsible = models.OneToOneField(
    to=Person,
    on_delete=models.CASCADE,
    blank=False,
    null=True,
    related_name="responsible"
  )

  users = models.ManyToManyField(
    to=Person,
    blank=False,
    null=True
  )

  def __str__(self):
    return self.title

class Achievement(models.Model):
  title = models.CharField(
    max_length=1024,
    blank=False,
    null=True,
  )

  description = models.CharField(
    max_length=1024,
    blank=False,
    null=True,
  )

  users = models.ManyToManyField(
    to=Person,
    null=True,
    blank=True,
  )

  reward = models.IntegerField()

  def __str__(self):
    if self.title:
      return self.title

    return 'Не указано'


import json
import sqlite3
from sentiment import sentiment, sentiment_lib

JSON_FILE = "../resources/three_minutes_tweets.json"

con = sqlite3.connect(":memory:")

# Obtain a cursor object
curTweet = con.cursor()

# Create a table in the in-memory database
createSDBTable = "CREATE TABLE IF NOT EXISTS Tweet(tweet_id integer primary key, name varchar(200), tweet_text text, country_code varchar(3), display_url text, lang varchar(3), created_at datetime, location varchar(100))"
addColumnTweetSentiment = "ALTER TABLE Tweet ADD COLUMN tweet_sentiment"
curTweet.execute(createSDBTable)
###### LOAD DATA TO SDB ######

with open(JSON_FILE) as f:
    for line in f:
        data = json.loads(line, "utf=8")
        # get data from json
        if 'user' in data:
            user = data.get('user')
            if user:
                name = user.get('name')
                display_url = user.get('url')
                lang = user.get('lang')
                location = user.get('location')
                tweet_text = data.get('text')
                country_code = data.get('country_code')
                created_at = data.get('created_at')
                curTweet.execute("insert into Tweet(name, tweet_text, country_code, display_url, lang, created_at, location) values(?,?,?,?,?,?,?)", (name, tweet_text, country_code, display_url, lang, created_at, location));
    con.commit()

    curTweet.execute(addColumnTweetSentiment)
###### LOAD DATA TO IDB. DATA NORMALIZATION ######
    createIDBUserTable = "CREATE TABLE IF NOT EXISTS User(user_id integer primary key, name varchar(200), display_url text)"
    createIDBTweetDataTable = "CREATE TABLE IF NOT EXISTS TweetData(tweet_id integer primary key, user_id integer, tweet_text text, country_code varchar(3), lang varchar(3), created_at datetime, location varchar(100))"

    curUser = con.cursor()
    curTweetData = con.cursor()

    curUser.execute(createIDBUserTable)
    curTweetData.execute(createIDBTweetDataTable)

    curTweet.execute('SELECT * FROM Tweet')
    for row in curTweet:
        curUser.execute("INSERT INTO User(name, display_url) VALUES(?,?)", (row[1], row[4]))
        curTweetData.execute("INSERT INTO TweetData(user_id, tweet_text, country_code, lang, created_at, location) values(?,?,?,?,?,?)", (curUser.lastrowid, row[2], row[3], row[5], row[6], row[7]))

con.close()


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

import click
import pandas as pd
from Bio import SeqIO

def segment_seqs_over_first_quartile(df_md, seg, threshold='25%'):
    q1 = df_md.loc[seg,'seq_length'].describe()[threshold]
    dfseg = df_md.loc[seg,:]
    dfseg = dfseg[dfseg.seq_length >= q1]
    return {acc:seg for acc in dfseg.accession}

@click.command()
@click.option('-s', '--influenza-sequences',
              required=True, help='NCBI Influenza DB sequences (i.e. influenza.fna from FTP site')
@click.option('-m', '--influenza-metadata',
              required=True,
              help='NCBI Influenza DB metadata (i.e. genomeset.dat from FTP site)')
@click.option('-o', '--outdir',
              default='./',
              help='Output destination for influenza genome segment sequence FASTA files')
@click.option('-N', '--exclude-seqs-sequential-ns',
              type=int,
              default=10,
              help='Exclude sequences with X or more sequential "N" characters (default: 10)')
@click.option('--seq-length-threshold-stat',
              type=click.Choice(['mean', 'min', '25%', '50%', '75%']),
              default='25%',
              help=('Pandas describe method statistic to use as min segment'
                    ' sequence threshold. (default "25%" or sequences must be '
                    'at least as long as the sequence at the 25th percentile '
                    'for each segment)'))
def split(influenza_sequences, influenza_metadata, outdir, exclude_seqs_sequential_ns, seq_length_threshold_stat):
    """Split influenza segment sequences into separate files.

    Exclude sequences that have too many consecutive Ns (default >= 10) and are shorter than the 25th percentile sequence for each segment (by default)
    """
    md_cols = [('accession', str),
               ('host', 'category'),
               ('segment', 'category'),
               ('subtype', 'category'),
               ('country', 'category'),
               ('date', 'category'),
               ('seq_length', 'uint16'),
               ('virus_name', 'category'),
               ('age', 'category'),
               ('gender', 'category'),
               ('group_id', 'category'),]
    df_md = pd.read_csv(influenza_metadata, sep='\t', names=[name for name, _ in md_cols], dtype={name:t for name,t in md_cols})
    df_md.set_index('segment', inplace=True)

    acc_regex = re.compile(r'gi\|\d+\|gb\|(\w+)\|\w+')
    regex_Ns = re.compile(r'N{' + str(exclude_seqs_sequential_ns) + r',}')
    accs_to_write = {}
    for seg in df_md.index.unique():
        accs_to_write.update(segment_seqs_over_first_quartile(df_md, seg))
    seg_to_fh = {seg: open(os.path.join(outdir, f'{seg}.fasta'), 'w') for seg in df_md.index.unique()}

    try:
        count = 0
        for rec in SeqIO.parse(influenza_sequences, 'fasta'):
            count += 1
            if count % 10000 == 0:
                print(f'Parsed {count}')
            m = acc_regex.match(rec.id)
            if m:
                acc = m.group(1)
                seg = accs_to_write.get(acc, None)
                fh = seg_to_fh.get(seg, None)
                if fh:
                    m = regex_Ns.search(str(rec.seq))
                    if m:
                        print(f'Too many Ns in a row in {rec.description}. N count={sum([1 for nt in rec.seq if nt == "N"])}')
                        continue
                    fh.write(f'>{rec.description}\n{rec.seq}\n')
    except Exception as e:
        raise e
    finally:
        for fh in seg_to_fh.values():
            fh.close()
    print('Done!')

if __name__ == '__main__':
    split()


from airflow.hooks.postgres_hook import PostgresHook
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults

class LoadFactOperator(BaseOperator):
    ui_color = '#F98866'

    @apply_defaults
    def __init__(self, redshift_conn_id, select_query, table, delete_load=True, *args, **kwargs):
        super(LoadFactOperator, self).__init__(*args, **kwargs)
        self.redshift_conn_id = redshift_conn_id
        self.select_query = select_query
        self.table = table
        self.delete_load = delete_load

    def execute(self, context):
        self.log.info('Starting LoadFactOperator for {} table'.format(self.table))

        # Create AWS and Redshift hook, and get AWS credential
        redshift_hook = PostgresHook(self.redshift_conn_id)

        # DELETE data from destination Redshift table
        if self.delete_load:
            self.log.info("Clearing data from destination Redshift table")
            delete_query = "DELETE FROM {}".format(self.table)
            redshift_hook.run(delete_query)

        # INSERT records into fact table
        insert_query = "INSERT INTO {} {}".format(self.table, self.select_query)
        redshift_hook.run(insert_query)

        self.log.info('Finish LoadFactOperator for {} table'.format(self.table))


#!/usr/bin/env python

import time, os, sys, csv, re, datetime, ujson as json

def f_conv_date(date):
  try:
    converted = int(time.mktime(datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S').timetuple()))
  except:
    try:
      converted = int(time.mktime(datetime.datetime.strptime(date, '%Y-%m-%d').timetuple())) + (12*60*60)
    except:
      converted = False
      pass
    pass

  return(converted)

f = open('gotowe_dane100.json', 'w')
for file in sorted(os.listdir('/home/dom/Pobrane/zanieczyszczenia/dane_csv_raw')):
  print(file)
  dane = {
    'kod stanowiska': None,
    'czas pomiaru': None,
    'kod stacji': None,
    'nr': None,
    'czas usredniania': None,
    'wskaznik': None,
    'jednostka': None,
    'nazwa_pliku': file,
    'dane': []
  }
  with open('/home/dom/Pobrane/zanieczyszczenia/dane_csv_raw/' + file, 'r') as file:
    reader = csv.reader(file, delimiter = ',')

    for fields in reader:
      out = f_conv_date(date=fields[0])
      if out != False:
        fields[0] = out
        dane['dane'].append(fields)
      elif fields[0] == 'Kod stanowiska':
        dane['kod stanowiska'] = fields
      elif fields[0] == 'Czas pomiaru':
        dane['czas pomiaru'] = fields
      elif fields[0] == 'Kod stacji' or (fields[0] == '' and (fields[1].startswith('Ds') or fields[1].startswith('Mp') or fields[1].startswith('Wm') or fields[1].startswith('Lb') or fields[1].startswith('Kp'))):
        dane['kod stacji'] = fields
      elif fields[0] == 'Nr' or fields[0] == 'Numer' or (fields[0] == '' and fields[1] == '1'):
        dane['nr'] = fields
      elif fields[0] == 'Czas u?redniania' or (fields[0] == '' and fields[1] in ['24g', '1g']):
        dane['czas usredniania'] = fields
      elif fields[0] == 'Wska?nik' or (fields[0] == '' and fields[1] in ['123trimetylobenzen', 'As(PM10)', 'BaA(PM10)', 'BaP(PM10)', 'BbF(PM10)', 'BjF(PM10)', 'BkF(PM10)', 'C6H6', 'Ca2+(PM2.5)', 'Cd(PM10)', 'Cl', 'CO', 'DBahA(PM10)', 'DBah(PM10)', 'EC(PM2.5)', 'formaldehyd', 'Hg(TGM)', 'IP(PM10)', 'Jest', 'K+(PM2.5)', 'Mg2+(PM2.5)', 'Na+(PM2.5)', 'NH4+(PM2.5)', 'Ni(PM10)', 'NO', 'NO2', 'NO3', 'Nox', 'NOx', 'O3', 'OC(PM2.5)', 'Pb(PM10)', 'PM10', 'PM2.5', 'SO2', 'SO42_(PM2.5)']):
        dane['wskaznik'] = fields
      elif fields[0] == 'Jednostka'  or (fields[0] == '' and fields[1] in ['ng/m3', 'ug/m3', 'mg/m3']):
        dane['jednostka'] = fields
      else:
        print(fields)

# sprawdzenie czy dane są odpowiedniej długości
#  dlug = None
#  for k in dane.keys():
#    if k == 'dane':
#      continue
#    if dane[k] != None:
#      if dlug == None:
#        dlug = len(dane[k])
#      else:
#        if dlug != len(dane[k]):
#          print(k)
#          sys.exit(1)
#  for d in dane['dane']:
#    if len(d) != dlug:
#      print(d)
#      sys.exit(1)
#  if dane['kod stacji'] == None:
#    print('kod stacji = none!!!!!!!!!!!!!!')
#    sys.exit(1)

  for stacja in range(len(dane['kod stacji'])):
    if stacja == 0:
      continue
    log_wzor = {
      'kod stanowiska': None,
      'czas pomiaru': None,
      'kod stacji': None,
      'nr': None,
      'czas usredniania': None,
      'wskaznik': None,
      'jednostka': None,
      'data': None,
      'wartosc': None
    }
    for parametr in dane.keys():
      if parametr in ['dane', 'nazwa_pliku']:
        continue
      if dane[parametr] != None:
        log_wzor[parametr] = dane[parametr][stacja]

    for pomiar in dane['dane']:
      if pomiar[stacja] == '':
        continue
      else:
        log_pomiar = log_wzor.copy()
        log_pomiar['data'] = int(pomiar[0])
        log_pomiar['wartosc'] = float(str(pomiar[stacja]).replace(',', '.'))
        log_pomiar['nazwa_pliku'] = dane['nazwa_pliku']
        f.write(json.dumps(log_pomiar))
        f.write('\n')

f.close()


from django.contrib import admin
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
from django.urls.conf import include

urlpatterns = [

    path('', views.home_data, name='home_data'),

    path('data_app/agregar_centro/', views.Agregar_centro.agregar_centro, name='agregar_centro'),
    path('data_app/guardar_centro/', views.Agregar_centro.guardar_centro, name='guardar_centro'),
    path('data_app/editar_centro/', views.Agregar_centro.borrar_centro, name='editar_centro'),

    path('data_app/agregar_producto/', views.Productos_edit.agregar_producto, name='agregar_producto'),
    path('data_app/guardar_producto/', views.Productos_edit.guardar_producto, name='guardar_producto'),
    path('data_app/borrar_producto/', views.Productos_edit.borrar_producto, name='borrar_producto'),
    path('data_app/actualizar_precio/', views.Productos_edit.actualizar_precios, name='actualizar_precio'),
    path('data_app/lista_productos/', views.Productos_edit.lista_productos, name='lista productos'),
    path('data_app/agregar_proveedor/', views.Proveedores_edit.agregar_proveedor, name='proveedor'),
    path('data_app/guardar_proveedor/', views.Proveedores_edit.guardar_proveedor, name='guardar_proveedor'),
    path('data_app/borrar_proveedor/', views.Proveedores_edit.borrar_proveedor, name='borrar_proveedor'),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


END_POINT = {
    "status": "/deliveries/{pk}/status",
    "create": "/deliveries/create",
    "event": "/event",
}

EVENT_ORDER = [
    "CREATE_DELIVERY",
    "INCREASE_BUDGET",
    "START_DELIVERY",
    "PICKUP_PRODUCTS",
    "DELIVER_PRODUCTS",
]

ERR_ID_NOT_FOUND = "Delivery ID not found."
ERR_DELIVERY_STARTED = "Delivery already started."
ERR_NOT_ENOUGH_BUDGET = "Not enough budget for this delivery."
ERR_NOT_ENOUGH_QUANTITY = "Not enough quantity for this delivery."

REDIS_HOST = (
    "redis"  # Docker host, this should be into an env, but this was a quick tutorial
)


import argparse
import time
import numpy as np
import torch
import torch.backends.cudnn as cudnn

import sys
sys.path.insert(0, '../')

from shelfnet.config import cfg
from shelfnet.config import update_config

from shelfnet import models

def parse_args():
    parser = argparse.ArgumentParser(description='Test speed keypoints network')
    # general
    parser.add_argument('--cfg',
                        help='experiment configure file name',
                        required=True,
                        type=str)
    parser.add_argument('opts',
                        help="Modify config options using the command-line",
                        default=None,
                        nargs=argparse.REMAINDER)

    parser.add_argument('--modelDir',
                        help='model directory',
                        type=str,
                        default='')
    parser.add_argument('--logDir',
                        help='log directory',
                        type=str,
                        default='')
    parser.add_argument('--dataDir',
                        help='data directory',
                        type=str,
                        default='')
    parser.add_argument('--prevModelDir',
                        help='prev Model directory',
                        type=str,
                        default='')

    args = parser.parse_args()
    return args

def test_speed():
    args = parse_args()
    update_config(cfg, args)

    print('Compute device: ' + "cuda:%d" % cfg.GPUS[0] if torch.cuda.is_available() else "cpu")
    device = torch.device("cuda:%d" % cfg.GPUS[0] if torch.cuda.is_available() else "cpu")

    # cudnn related setting
    cudnn.benchmark = cfg.CUDNN.BENCHMARK
    torch.backends.cudnn.deterministic = cfg.CUDNN.DETERMINISTIC
    torch.backends.cudnn.enabled = cfg.CUDNN.ENABLED

    model_kwargs = {'is_train': False} \
        if cfg.MODEL.NAME == 'pose_hrnet' else {}
    model = eval('models.' + cfg.MODEL.NAME + '.get_pose_net')(cfg, **model_kwargs)

    # count parameter number
    pytorch_total_params = sum(p.numel() for p in model.parameters())
    print("Total number of parameters: %d" % pytorch_total_params)

    model = model.to(device)
    model.eval()

    run_time = list()
    print("Input shape = " + str(cfg.MODEL.IMAGE_SIZE[::-1]))

    for i in range(0, 10000):
        input = torch.randn(1, 3, *cfg.MODEL.IMAGE_SIZE[::-1]).to(device)
        # ensure that context initialization and normal_() operations
        # finish before you start measuring time
        torch.cuda.synchronize()
        start = time.perf_counter()

        with torch.no_grad():
            _ = model(input)

        torch.cuda.synchronize()  # wait for mm to finish
        end = time.perf_counter()
        run_time.append(end-start)

    run_time.pop(0)

    print('Mean running time is {:.5f}'.format(np.mean(run_time)))
    print('FPS = {:.1f}'.format(1 / np.mean(run_time)))

if __name__ == '__main__':
    test_speed()


#!/usr/bin/python

import os
import sys
import argparse
import random
import numpy as np
import os.path as osp

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader

# custom functions defined by user
from model import DeepCNN
from datasets import EPIDataSet
from trainer import Trainer
from hyperopt import hp
from hyperopt.pyll.stochastic import sample

def randomparams():
    space = {
        'lr': hp.choice('lr', (0.001, 0.0001)),
        'beta1': hp.choice('beta1', (0.9, 0.9)),
        'beta2': hp.choice('beta2', (0.99, 0.999)),
        'weight': hp.choice('weight', (0, 0.0001))
    }
    params = sample(space)

    return params

def get_args():
    """Parse all the arguments.

        Returns:
          A list of parsed arguments.
    """
    parser = argparse.ArgumentParser(description="CNN for predicting CSSBS")

    parser.add_argument("-d", dest="data_dir", type=str, default=None,
                        help="A directory containing the training data.")
    parser.add_argument("-n", dest="name", type=str, default=None,
                        help="The name of a specified data.")

    parser.add_argument("-g", dest="gpu", type=str, default='1',
                        help="choose gpu device.")
    parser.add_argument("-s", dest="seed", type=int, default=5,
                        help="Random seed to have reproducible results.")

    parser.add_argument("-b", dest="batch_size", type=int, default=1,
                        help="Number of sequences sent to the network in one step.")
    parser.add_argument("-e", dest="max_epoch", type=int, default=30,
                        help="Number of training steps.")

    parser.add_argument("-c", dest="checkpoint", type=str, default='./models/',
                        help="Where to save snapshots of the model.")

    return parser.parse_args()

def main():
    """Create the model and start the training."""
    args = get_args()
    random.seed(args.seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(args.seed)
        device = torch.device("cuda:" + args.gpu)
    else:
        device = torch.device("cpu")
        torch.manual_seed(args.seed)
    motifLen = 20
    Data = np.load(osp.join(args.data_dir, '%s_train.npz' % args.name))
    data_tr, label_tr = Data['data'], Data['label']
    Data = np.load(osp.join(args.data_dir, '%s_val.npz' % args.name))
    data_val, label_val = Data['data'], Data['label']
    dim = data_val.shape[1]
    f = open(osp.join(args.checkpoint, 'score.txt'), 'w')
    f.write('auc\tprauc\n')
    # build training data generator
    train_data = EPIDataSet(data_tr, label_tr)
    train_loader = DataLoader(train_data, batch_size=args.batch_size, shuffle=True, drop_last=True, num_workers=0)
    # build test data generator
    val_data = EPIDataSet(data_val, label_val)
    val_loader = DataLoader(val_data, batch_size=1, shuffle=False, num_workers=0)
    # implement
    auc_best = 0
    for _ in range(10):
        params = randomparams()
        model = DeepCNN(in_channels=dim, motiflen=motifLen)
        optimizer = optim.Adam(model.parameters(), lr=params['lr'],
                               betas=(params['beta1'], params['beta2']), weight_decay=params['weight'])
        criterion = nn.BCELoss()
        executor = Trainer(model=model,
                           optimizer=optimizer,
                           criterion=criterion,
                           device=device,
                           checkpoint=args.checkpoint,
                           start_epoch=0,
                           max_epoch=8,
                           train_loader=train_loader,
                           test_loader=val_loader,
                           lr_policy=None)

        auc, prauc, state_dict = executor.train()
        if auc_best < auc:
            print("Store the weights of the model in the current run.\n")
            auc_best = auc
            lr = params['lr']
            betas = (params['beta1'], params['beta2'])
            weight_decay = params['weight']
            checkpoint_file = osp.join(args.checkpoint, 'model_best.pth')
            torch.save({
                'model_state_dict': state_dict
            }, checkpoint_file)
    # loading the best model
    checkpoint_file = osp.join(args.checkpoint, 'model_best.pth')
    checkpoint = torch.load(checkpoint_file)
    state_dict = checkpoint["model_state_dict"]
    model.load_state_dict(state_dict, strict=True)
    optimizer = optim.Adam(model.parameters(), lr=lr, betas=betas, weight_decay=weight_decay)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.9)
    criterion = nn.BCELoss()
    executor = Trainer(model=model,
                       optimizer=optimizer,
                       criterion=criterion,
                       device=device,
                       checkpoint=args.checkpoint,
                       start_epoch=0,
                       max_epoch=args.max_epoch,
                       train_loader=train_loader,
                       test_loader=val_loader,
                       lr_policy=scheduler)
    auc, prauc, state_dict = executor.train()
    torch.save({'model_state_dict': state_dict}, checkpoint_file)
    f.write("{:.3f}\t{:.3f}\n".format(auc, prauc))
    f.close()

if __name__ == "__main__":
    main()


#{
 # Driver Code Starts
#Initial Template for Python 3
H = [0] * 10009
s = -1

def parent(i):
    return (i - 1) // 2

def leftChild(i):
    return 2 * i + 1

def rightChild(i):
    return 2 * i + 2

def shiftUp(i):
    while i > 0 and H[parent(i)] < H[i]:
        H[parent(i)], H[i] = H[i], H[parent(i)]
        i = parent(i)

def shiftDown(i):
    maxIndex = i
    l = leftChild(i)
    if l <= s and H[l] > H[maxIndex]:
        maxIndex = l
    r = rightChild(i)
    if r <= s and H[r] > H[maxIndex]:
        maxIndex = r
    if (i != maxIndex):
        H[i], H[maxIndex] = H[maxIndex], H[i]
        shiftDown(maxIndex)

def insert(p):
    global s
    s += 1
    H[s] = p
    shiftUp(s)

# } Driver Code Ends
#User function Template for python3

# 1. parent(i): Function to return the parent node of node i
# 2. leftChild(i): Function to return index of the left child of node i
# 3. rightChild(i): Function to return index of the right child of node i
# 4. shiftUp(int i): Function to shift up the node in order to maintain the
# heap property
# 5. shiftDown(int i): Function to shift down the node in order to maintain the
# heap property.
# int s=-1, current index value of the array H[].

class Solution:
    def extractMax(self):
        # Code here
        global s
        res = H[0]
        H[0] = H[s]
        s -= 1
        shiftDown(0)
        return res

#{
 # Driver Code Starts.

if __name__ == '__main__':
    t = int(input ())
    for _ in range (t):
        n = int(input())
        a = list(map(int, input().split()))
        for val in a:
            insert(val)
        ob = Solution()
        print('Node with maximum priority :', ob.extractMax())
        print('Priority queue after extracting maximum : ', end = '')
        for i in range(s + 1):
            print(H[i], end = ' ')
        print()
# } Driver Code Ends

GeeksforGeeks/Python3/Medium/Implementation of Priority Queue using Binary Heap/implementation-of-priority-queue-using-binary-heap.py


import numpy as np

class AdaGrad(object):
	"""docstring for AdaGrad"""
	def __init__(self, lr=0.01):
		self.lr = lr
		self.h = None		# h就是在adagrad方法里控制逐渐减小学习率的参数，他是之前所有梯度值的平方和

	def update(self, params, grads):
		if self.h is None:
			self.h = {}
			for key, val in params.items():
				self.h[key] = np.zeros_like(val)

		for key in params.keys():
			self.h[key] += grads[key] * grads[key]		# h存的是之前梯度值的平方和
			params[key] -= self.lr * grads[key] / (np.sqrt(self.h[key]) + le-7)		# W = W - lr * (1/sqrt(h) * dW)
																					# 加一个微小值的目的是防止分母为0


import SepVector
import Hypercube
import genericIO
import WEM
import newExtWEM
import Operator
import json
import numpy as np

import pyOperator as Op
import pyNonLinearSolver as Solver
import pyLinearSolver as LinearSolver
import pyProblem as Prblm
import pyStopper as Stopper
import pyStepper as Stepper
from sys_util import logger

slow2 = genericIO.defaultIO.getVector("Vel/constSlowBg1800_ext.H")
slow = slow2.clone()
alpha = [0.5, 0.8, 0.999, 1]
# alpha_final = [0.1,0.5,0.7,1]
eps = [1e-5,0.01,0.01,0.01]
niter = 20
subiter = 50

for i in range(len(alpha)):
    ######## prepare the input #############
    D = "./"
    with open('Par/geom.json') as f:
        par = json.load(f)
    parObj = genericIO.pythonParams(par).getCpp()
    wave = genericIO.defaultIO.getVector(D+"Wav/constWav.H")
    data = genericIO.defaultIO.getVector(D+"Dat/constData_tap.H")
    ######## prepare the input #############

    ######## prepare the precondioned variable #############
    slow.zero()
    int = Operator.LeakyIntegration(slow,slow2,alpha=alpha[i])
    LinStop  = Stopper.BasicStopper(niter=subiter)
    CGsolver = LinearSolver.LCGsolver(LinStop)
    L2Prob = Prblm.ProblemL2Linear(slow,slow2,int)
    CGsolver.setDefaults(iter_sampling=1,save_obj=False,save_res=False,save_grad=False,
    			flush_memory=True,iter_buffer_size=1,save_model=False )
    CGsolver.run(L2Prob,verbose=True)
    ######## prepare the precondioned variable #############

    ######## prepare the regularization term #############
    der = Operator.Derivative(slow,slow)
    reg = Op.ChainOperator(int,der)
    # derOp = Op.NonLinearOperator(der,der)
    regOp = Op.NonLinearOperator(reg,reg)
    ######## prepare the regularization term #############

    ######## prepare the data fitting term #############
    smoothOp = Operator.Taper(slow,slow,tap=[10],axes=[2],zero=[10])
    bornOp = newExtWEM.extBorn(slow,data,wave,parObj)
    bornOpSm = Op.ChainOperator(smoothOp,bornOp)
    # bornOpSm = Op.ChainOperator(realSmooth,bornOpSm)
    wemOp = newExtWEM.extWEM(slow,data,parObj,wave)
    nlOp = Op.NonLinearOperator(wemOp,bornOpSm,set_background_func=bornOp.setBgSlow)
    intOp = Op.NonLinearOperator(int,int)
    nlOp = Op.CombNonlinearOp(intOp,nlOp)
    ######## prepare the data fitting term ############

    ######## prepare the FWI problem #############
    L2ProbReg = Prblm.ProblemL2NonLinearReg(slow,data,nlOp,eps[i],reg_op=regOp)
    ######## find eps #############
    L2ProbReg.epsilon = eps[i] * L2ProbReg.estimate_epsilon(verbose=True)
    print(L2ProbReg.epsilon)
    ######## find eps #############
    Stop  = Stopper.BasicStopper(niter=niter)
    stepper = Stepper.CvSrchStep(alpha_min=1e-7)
    LBFGSsolver = Solver.LBFGSsolver(Stop,stepper=stepper,m_steps=50,logger=logger("Log/1800_leaky_%s_alpha_%s.txt" % (eps[i],alpha[i])))
    LBFGSsolver.setDefaults(iter_sampling=1,save_obj=True,save_res=True,save_grad=True,
    			flush_memory=True,iter_buffer_size=1,save_model=True,prefix="Inv/1800_leaky_%s_%s" % (eps[i],alpha[i]) )
    LBFGSsolver.run(L2ProbReg,verbose=True,restart=False)

    slow = genericIO.defaultIO.getVector("Inv/1800_leaky_%s_%s_inv_mod.H" % (eps[i],alpha[i]) )
    int.forward(False,slow,slow2)


from __future__ import division # for proper float division

import os
import sys
import math
import time
import types
import functools
import random
import numpy as np
from ddapp import ik
from ddapp import ikconstraints
from ddapp import ikconstraintencoder
import drc as lcmdrc
import json
from ddapp.utime import getUtime
from ddapp import lcmUtils

import ipab

class PlannerPublisher(object):

  def __init__(self, ikPlanner, affordanceMan):
    self.ikPlanner = ikPlanner
    self.affordanceManager = affordanceMan
    self.poses={}

  def setupMessage(self, constraints, endPoseName="", nominalPoseName="", seedPoseName="", additionalTimeSamples=None):
    poses = ikconstraintencoder.getPlanPoses(constraints, self.ikPlanner)
    poses.update(self.poses)
    msg = ipab.exotica_planner_request_t()
    msg.utime = getUtime()
    msg.poses = json.dumps(poses)
    msg.constraints = ikconstraintencoder.encodeConstraints(constraints)
    msg.seed_pose = seedPoseName
    msg.nominal_pose = nominalPoseName
    msg.end_pose = endPoseName
    msg.joint_names = json.dumps(list(self.ikPlanner.jointController.jointNames))
    msg.affordances = self.processAffordances()
    opt=ikplanner.getIkOptions()._properties
    if additionalTimeSamples:
      opt.update({'timeSamples':additionalTimeSamples})
    msg.options = json.dumps(opt)
    return msg

  def processIK(self, constraints, endPoseName="", nominalPoseName="", seedPoseName="", additionalTimeSamples=None):
    listener = self.ikPlanner.getManipIKListener()
    msg = self.setupMessage(constraints, endPoseName, nominalPoseName, seedPoseName, additionalTimeSamples)
    lcmUtils.publish('IK_REQUEST', msg)
    ikplan = listener.waitForResponse(timeout=12000)
    listener.finish()

    endPose = [0] * self.ikPlanner.jointController.numberOfJoints
    if ikplan.num_states>0:
      endPose[len(endPose)-len(ikplan.plan[ikplan.num_states-1].joint_position):] = ikplan.plan[ikplan.num_states-1].joint_position
      info=ikplan.plan_info[ikplan.num_states-1]
    else:
      info = -1
    self.ikPlanner.ikServer.infoFunc(info)
    return endPose, info

  def processTraj(self, constraints, endPoseName="", nominalPoseName="", seedPoseName="", additionalTimeSamples=None):
    # Temporary fix / HACK / TODO (should be done in exotica_json)
    largestTspan = [0, 0]
    for constraintIndex, _ in enumerate(constraints):
      # Get tspan extend to normalise time-span
      if np.isfinite(constraints[constraintIndex].tspan[0]) and np.isfinite(constraints[constraintIndex].tspan[1]):
        largestTspan[0] = constraints[constraintIndex].tspan[0] if (constraints[constraintIndex].tspan[0] < largestTspan[0]) else largestTspan[0]
        largestTspan[1] = constraints[constraintIndex].tspan[1] if (constraints[constraintIndex].tspan[1] > largestTspan[1]) else largestTspan[1]

    # Temporary fix / HACK/ TODO to normalise time spans
    for constraintIndex, _ in enumerate(constraints):
      if np.isfinite(constraints[constraintIndex].tspan[0]) and np.isfinite(constraints[constraintIndex].tspan[1]):
        if largestTspan[1] != 0:
          constraints[constraintIndex].tspan[0] = constraints[constraintIndex].tspan[0] / largestTspan[1]
          constraints[constraintIndex].tspan[1] = constraints[constraintIndex].tspan[1] / largestTspan[1]

    listener = self.ikPlanner.getManipPlanListener()
    msg = self.setupMessage(constraints, endPoseName, nominalPoseName, seedPoseName, additionalTimeSamples)
    lcmUtils.publish('PLANNER_REQUEST', msg)
    lastManipPlan = listener.waitForResponse(timeout=20000)
    listener.finish()
    if lastManipPlan:
      # HACK: need to multiply by original trajectory length again (the one in the plan is planning and not real time), otherwise jumps to real hardware
      for state in lastManipPlan.plan:
        state.utime = state.utime * 50

    self.ikPlanner.ikServer.infoFunc(lastManipPlan.plan_info[0])
    return lastManipPlan, lastManipPlan.plan_info[0]

  def processAddPose(self, pose, poseName):
    self.poses[poseName]=list(pose);

  def processAffordances(self):
      affs = self.affordanceManager.getCollisionAffordances()
      s='['
      first=True
      for aff in affs:
        des=aff.getDescription()
        classname=des['classname'];
        if first:
          s+='{'
        else:
          s+='\n,{'
        first=False
        s+='"classname":"'+classname+'"'
        s+=',"uuid":"'+des['uuid']+'"'
        s+=',"pose": {"position":{"__ndarray__":'+repr(des['pose'][0].tolist())+'},"quaternion":{"__ndarray__":'+repr(des['pose'][1].tolist())+'}}'
        if classname=='MeshAffordanceItem':
          s+=',"filename":"'+aff.getMeshManager().getFilesystemFilename(des['Filename'])+'"'
        if classname=='SphereAffordanceItem':
          s+=',"radius":'+repr(des['Radius'])
        if classname=='CylinderAffordanceItem' or classname=='CapsuleAffordanceItem':
          s+=',"radius":'+repr(des['Radius'])
          s+=',"length":'+repr(des['Length'])
        if classname=='BoxAffordanceItem':
          s+=',"dimensions":'+repr(des['Dimensions'])
        if classname=='CapsuleRingAffordanceItem':
          s+=',"radius":'+repr(des['Radius'])
          s+=',"tube_radius":'+repr(des['Tube Radius'])
          s+=',"segments":'+repr(des['Segments'])
        s+='}'
      s=s+']'
      return s

import ikplanner


import sys
import math

def guardar(x, y):
	if(y == 1):
		f = "CIRCULO"
	if(y == 2):
		f = "CUADRADO"
	if(y == 3):
		f = "RECTANGULO"
	if(y == 4):
		f = "TRIANGULO"

	texto = open("AREAS.txt", "a")
	texto.write("***************" + "\n" "FIGURA: " + f + "\n" + "AREA: " + str(x) + "\n" )
	texto.close()

def circulo(a,b):
	area = ((2*math.pi)*(a*a))
	print("EL AREA ES: " + str(area) + "\n")
	guardar(area,b)

def cuadrado(a,b):
	area = a*a
	print("EL AREA ES: " + str(area) + "\n")
	guardar(area,b)

def rectangulo(a,b,c):
	area = a*b
	print("EL AREA ES: " + str(area) + "\n")
	guardar(area,c)

def triangulo(a,b,c):
	area = (1/2)*a*b
	print("EL AREA ES: " + str(area) + "\n")
	guardar(area,c)

def lectura():
	tex = open("AREAS.txt", "r")
	lectura = tex.read()
	print(lectura)
	tex.close()

def menu(figura):

	if(figura == 1):

		r = float(input("INGRESE EL RADIO DEL CIRCULO: "))
		circulo(r,figura)

	elif(figura == 2):

		lado = float(input("INGRESE EL VALOR DEL LADO: "))
		cuadrado(lado, figura)

	elif(figura == 3):

		lado1 = float(input("INGRESE EL VALOR DEL LADO 1: "))
		lado2 = float(input("INGRESE EL VALOR DEL LADO 2: "))
		rectangulo(lado1, lado2, figura)

	elif(figura == 4):

		base = float(input("INGRESE EL VALOR DEL LA BASE: "))
		altura = float(input("INGRESE EL VALOR DE LA ALTURA: "))
		triangulo(base, altura, figura)

	elif(figura == 5):
		lectura()

	elif(figura == 6):
		print("***** SALIENDO *****")
		sys.exit()
	else:
		print("INGRESE UNA OPCION VALIDA")

while True:
	print(""" \n SELECCIONE LA FIGURA A LA QUE DESEA CALCULAR EL AREA:

	>>>>>>>>>>     (1) CIRCULO
	>>>>>>>>>>     (2) CUADRADO
	>>>>>>>>>>     (3) RECTANGULO
	>>>>>>>>>>     (4) TRIANGULO

	>>>>>>>>>>     (5) HISTORIAL
	>>>>>>>>>>     (6) SALIR

	\n """)

	op = int(input("OPCION: "))
	print("\n")
	menu(op)


#  建立悬臂式掘进机的优化模型
from position import *
import matplotlib.pyplot as plt

'''
各个位姿参数的范围
lh,lv的范围 -0.5 -- 0.5
lsh,lsu的范围 -0.05 -- 0.05
A -0.15 -- 0.15
L -0.02 -- 0.035
H -0.04 -- 0.04
alpha -4 -- 4
beta -4 -- 4
gamma -3 -- 3
#######################例子
A_ = -0.05
L_ = 0.03
alpha_ = 2
########################
H_ = 0
beta_ = 0
gamma_ = 0
A_ = -0.05
L_ = 0.03
alpha_ = 2
'''

compensationStrategy = {'A': ['Delta_lh', 'Delta_lv'], 'L': ['Delta_lh', 'Delta_lv'], 'H': ['Delta_lsh', 'Delta_lsu'],
                        'alpha': ['Delta_lh', 'Delta_lv'], 'beta': ['Delta_lsh', 'Delta_lsu'],
                        'gamma': ['Delta_lsh', 'Delta_lsu']}
positionType = ['A', 'L', 'H', 'alpha', 'beta', 'gamma']
structuralLoop = ['Delta_lsh', 'Delta_lsu', 'Delta_lh', 'Delta_lv']
# 液压缸的调节速度，以m/s为单位
adjustSpeed = {'Delta_lsh': 0.010, 'Delta_lsu': 0.011, 'Delta_lh': 0.09, 'Delta_lv': 0.020}

###################################
# 下面计算的各个参数，均是在PCA分析的基础上
#  调整量
def positionDeviations0(Delta_lsh=0, Delta_lsu=0, Delta_lh=0, Delta_lv=0, position_error=None):
    try:
        # alpha = cal_alpha(Delta_lh, Delta_lv)
        # beta = cal_beta(Delta_lsu, Delta_lsh)
        # gamma = cal_gamma(Delta_lsu, Delta_lsh)
        A = cal_A(Delta_lh, Delta_lv)
        # L = cal_L(Delta_lh, Delta_lv)
        # H = cal_H(Delta_lsu, Delta_lsh)
        # print(A, "........", L, "........", alpha)
        return abs(A - position_error[0])
    except:
        # print('该数据无效')
        return 1000

def positionDeviations1(Delta_lsh=0, Delta_lsu=0, Delta_lh=0, Delta_lv=0, position_error=None):
    try:
        L = cal_L(Delta_lh, Delta_lv)
        return abs(L - position_error[1])
    except:
        # print('该数据无效')
        return 1000

def positionDeviations2(Delta_lsh=0, Delta_lsu=0, Delta_lh=0, Delta_lv=0, position_error=None):
    try:
        H = cal_H(Delta_lsu, Delta_lsh)
        return abs(H - position_error[0])
    except:
        # print('该数据无效')
        return 1000

def positionDeviations3(Delta_lsh=0, Delta_lsu=0, Delta_lh=0, Delta_lv=0, position_error=None):
    try:
        alpha = cal_alpha(Delta_lh, Delta_lv)
        return abs(alpha - position_error[2])
    except:
        # print('该数据无效')
        return 1000

def positionDeviations4(Delta_lsh=0, Delta_lsu=0, Delta_lh=0, Delta_lv=0, position_error=None):
    try:

        beta = cal_beta(Delta_lsu, Delta_lsh)
        return abs(beta - position_error[1])
    except:
        # print('该数据无效')
        return 1000

def positionDeviations5(Delta_lsh=0, Delta_lsu=0, Delta_lh=0, Delta_lv=0, position_error=None):
    try:
        gamma = cal_gamma(Delta_lsu, Delta_lsh)
        return abs(gamma - position_error[2])
    except:
        # print('该数据无效')
        return 1000

#  调整时间，目前不用
def adjustTime(Delta_lsh=0, Delta_lsu=0, Delta_lh=0, Delta_lv=0):
    Time = 0
    Time += abs(Delta_lsh) / adjustSpeed['Delta_lsh']
    Time += abs(Delta_lsu) / adjustSpeed['Delta_lsu']
    Time += abs(Delta_lh) / adjustSpeed['Delta_lh']
    Time += abs(Delta_lv) / adjustSpeed['Delta_lv']
    return Time

###################################
if __name__ == '__main__':
    print(r'$\Delta$')
plt.title('$\Delta$')
plt.show()


"""
Module contains some useful mathematical functions that are used in several other modules.
"""

import numpy as np
import random

def get_weights_for_convex_comb(nb):
    """
    Returns weights for a convex combination of nb elements, of which none is favoured.

    Arguments
    ---------
    nb : int

    Returns
    -------
    weights : numpy.array
    """
    aux = (nb + 1) * [None]
    aux[0] = 0
    aux[nb] = 1
    for i in range(nb - 1):
        aux[i + 1] = random.random()
    aux = np.array(aux)
    aux.sort()

    weights = nb * [None]
    for i in range(nb):
        weights[i] = aux[i + 1] - aux[i]

    return np.array(weights)

def convex_comb(weights, points):
    """
    Computes convex combination of points with given weights and returns the result.

    Arguments
    ---------
    weights : numpy.array
    points : numpy.array

    Returns
    -------
    conv_comb : numpy.array
    """
    points = points.copy()
    conv_comb = np.dot(np.transpose(points), weights)
    return conv_comb


from django import http
from django.db.models import Q
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from . import models

from linebot import LineBotApi, WebhookParser
from linebot.models import (
    Message,
    TextSendMessage,
    TemplateSendMessage,

    MessageEvent,
    PostbackEvent,
)

import requests as rq

line_bot_api = LineBotApi(settings.LINE_CHANNEL_ACCESS_TOKEN)
parser = WebhookParser(settings.LINE_CHANNEL_SECRET)

bot_description = """
WarmCom，全稱為 Warm Company 有溫暖陪伴的意思，同時也諧音 Warm Come 表示溫暖來臨之意。

WarmCom 提供這些服務：
$ 發表文章：以匿名的方式發表文章
$ 給予溫暖：查看別人的文章並溫暖地回應對方
$ 收到溫暖：查看所收到來自他人的溫暖
$ 配對諮商：協助您找到適合的諮商師
$ 成為諮商師：若您願意諮商他人，則可填寫諮商資訊，並開啟配對功能
""".split("\n")[1:]
bot_description = "\n".join(bot_description)

@csrf_exempt
def receive_message(request):
    signature = request.META['HTTP_X_LINE_SIGNATURE']
    body = request.body.decode('utf-8')
    events = parser.parse(body, signature)
    for event in events:
        if isinstance(event, MessageEvent):
            if event.message.text == "這個機器人是做什麼的?":
                msg = TextSendMessage(
                    text = bot_description,
                    emojis = [
                        {
                            "index": 77,
                            "productId": "5ac2213e040ab15980c9b447",
                            "emojiId": "017"
                        },
                        {
                            "index" : 95,
                            "productId" : "5ac2213e040ab15980c9b447",
                            "emojiId" : "018",
                        },
                        {
                            "index" : 118,
                            "productId" : "5ac2213e040ab15980c9b447",
                            "emojiId" : "019",
                        },
                        {
                            "index" : 138,
                            "productId" : "5ac2213e040ab15980c9b447",
                            "emojiId" : "020",
                        },
                        {
                            "index" : 157,
                            "productId" : "5ac2213e040ab15980c9b447",
                            "emojiId" : "021",
                        },
                    ]
                )
                line_bot_api.reply_message(event.reply_token, msg)
        if isinstance(event, PostbackEvent):
            user_id = event.source.user_id
            data = resolve_postback_data(event.postback.data)
            action = data["action"]
            if action == "pair":
                send_message(user_id, "已發送邀請給該諮商師，等待回覆中...")
                rq.post(
                    f"{settings.BASE_DOMAIN}/api/appoint",
                    {
                        "user_id" : user_id,
                        "counselor_id" : data["counselor_id"],
                    }
                )
            if action == "help":
                send_message(user_id, "已接受，將您的 id 傳送給對方")
                rq.post(
                    f"{settings.BASE_DOMAIN}/api/help",
                    {
                        "user_id" : data["target_id"],
                        "counselor_id" : user_id,
                    }
                )

    return http.HttpResponse()

def resolve_postback_data(postback_data: str):
    result = {}
    for tmp in postback_data.split("&"):
        t = tmp.split("=")
        result[t[0]] = t[1]
    return result

def send_to_user(user_id: str, message: Message):
    line_bot_api.push_message(user_id, message)

def send_message(user_id: str, message: str):
    msg = TextSendMessage(text=message)
    send_to_user(user_id, msg)

def generate_counselors_information_template(filter_types: list):
    def generate_counselor_template(counselor: models.Counselor):
        gender = counselor.gender
        age = counselor.age
        job = counselor.job
        is_professional = counselor.is_professional

        title = counselor.user_name
        if len(gender) == 1 or age or is_professional:
            tmp = ""
            if len(gender) == 1:
                tmp += gender
            if age:
                if tmp:
                    tmp += "，"
                tmp += str(age)
            if tmp:
                tmp += "，"
            if is_professional:
                tmp += "專業"
            else:
                tmp += "非專業"
            title += f" ({tmp})"

        text = counselor.description
        if job:
            text = f"職業: {job}\n{text}"
        else:
            text = f"職業: 無\n{text}"
        return {
            "thumbnailImageUrl": counselor.image,
            "imageBackgroundColor": "#FFFFFF",
            "title": title,
            "text": text,
            "actions": [
                {
                    "type": "postback",
                    "label": "配對",
                    "text": f"我要配對 {counselor.user_name}",
                    "data" : f"action=pair&counselor_id={counselor.user_id}"
                },
            ]
        }

    target_type_condition = None
    for idx, target_type in enumerate(filter_types):
        if idx == 0:
            target_type_condition = Q(target=target_type)
        else:
            target_type_condition = target_type_condition | Q(target=target_type)
    counselors = models.Counselor.objects.filter(target_type_condition & Q(can_be_paired=True)).distinct()[0:10]
    if len(counselors) == 0:
        return {}
    return {
        "type": "template",
        "alt_text": "符合條件的諮商師",
        "template": {
            "type": "carousel",
            "columns": [generate_counselor_template(c) for c in counselors],
            "imageAspectRatio": "rectangle",
            "imageSize": "cover"
        }
    }

def send_counselors_information_to_user(user_id: str, filter_types: list):
    counselors_information = generate_counselors_information_template(filter_types)
    if counselors_information:
        send_to_user(user_id, TemplateSendMessage(**counselors_information))
    else:
        send_to_user(user_id, TextSendMessage(text="查不到相關的諮商師"))

def generate_patient_template(user_id, brief):
    user = line_bot_api.get_profile(user_id)
    return {
        "type": "template",
        "alt_text": "有人需要被諮商!",
        "template": {
            "type": "buttons",
            "thumbnailImageUrl": user.picture_url,
            "imageAspectRatio": "rectangle",
            "imageSize": "cover",
            "imageBackgroundColor": "#FFFFFF",
            "title": user.display_name,
            "text": f"{brief}",
            "actions": [
                {
                    "type": "postback",
                    "label": "接受",
                    "text" : "接受",
                    "data" : f"action=help&target_id={user_id}"
                },
            ]
        }
    }

def send_patient_information_to_counselor(counselor_id, user_id, brief):
    template = generate_patient_template(user_id, brief)
    send_to_user(counselor_id, TemplateSendMessage(**template))


#! /usr/bin/env python2

import cvengine
import os
import tempfile
import requests

PLATFORM_IMAGES = {'fedora-atomic': ('https://download.fedoraproject.org'
                                     '/pub/alt/atomic/stable/Fedora-Atomic-'
                                     '27-20171110.1/CloudImages/x86_64/images/'
                                     'Fedora-Atomic-27-20171110.1.x86_64'
                                     '.qcow2')}
PLATFORM_IMAGES['atomic'] = PLATFORM_IMAGES['fedora-atomic']
PLATFORM_FAMILIES = {'atomic': ['atomic', 'fedora-atomic']}

def main():
    assert 'ANSIBLE_INVENTORY' in os.environ
    ansible_inventory = os.environ['ANSIBLE_INVENTORY']

    assert 'CV_TARGET_PLATFORM' in os.environ
    target_image = os.environ['CV_TARGET_PLATFORM']
    assert target_image in PLATFORM_IMAGES
    target_image_url = PLATFORM_IMAGES[target_image]
    target_platform = next((p for p in PLATFORM_FAMILIES if target_image
                            in PLATFORM_FAMILIES[p]),
                           None)
    assert target_platform is not None
    image_file = tempfile.NamedTemporaryFile(prefix='cvengine_image_',
                                             suffix='.qcow2',
                                             delete=False)
    image_data = requests.get(target_image_url)
    with open(image_file.name, 'w') as f:
        f.write(image_data.content)

    artifacts_dir = os.path.join(os.getcwd(), 'logs', 'cvartifacts')

    playbook_command = ('ansible-playbook -v --inventory={inv} '
                        '--extra-vars "subjects={image}" '
                        '/cvengine/ci/run_cvengine.yaml')
    playbook_command = playbook_command.format(inv=ansible_inventory,
                                               image=image_file.name)
    env_vars = {'CV_TARGET_PLATFORM': target_platform,
                'TEST_SUBJECTS': image_file.name,
                'CV_ARTIFACTS_DIRECTORY': artifacts_dir}
    cvengine.util.run.run_cmd(playbook_command,
                              env_vars=env_vars)

if __name__ == '__main__':
    main()


# -*- python -*-
from __future__ import print_function

import sys, os, os.path

Import('root')

srcs = ['${SDK_ROOT}/build/src/rp2_common/boot_stage2/bs2_default_padded_checksummed.S'
  , 'blink.cpp']

env = root.NewEnvironment()
env.Append(LINKFLAGS=['-Wl,-Map=blink.elf.map']
    , CPPFLAGS = '-DPICO_TARGET_NAME=\\\"blink\\\"')

program = env.Program('blink.elf', srcs, LIBS=['stdpico']) #
module = env.Command('blink.uf2', program, '${ELF2UF2} $SOURCE $TARGET')

uf2 = env.Install("${BINROOT}", module)

Return('uf2')


class Heap:
    def __init__(self, size):
        self.customList = (size + 1) * [None]
        self.heapSize = 0
        self.maxSize = size + 1

def Peek(Root):
    if Root is None:
        return
    else:
        return Root.customList[1]

def Sizeofheap(Root):
    if Root is None:
        return
    else:
        return Root.heapSize

newHeap = Heap(5)

def lvlOrderTraversal(Root):
    if Root is None:
        return
    else:
        for i in range(1, Root.heapSize + 1):
            print(Root.customList[i])

def heapifyTreeInsert(rootNode, index, heapType):
    parentIndex = int(index / 2)
    if index <= 1:
        return
    if heapType == "Min":
        if rootNode.customList[index] < rootNode.customList[parentIndex]:
            temp = rootNode.customList[index]
            rootNode.customList[index] = rootNode.customList[parentIndex]
            rootNode.customList[parentIndex] = temp
        heapifyTreeInsert(rootNode, parentIndex, heapType)
    elif heapType == "Max":
        if rootNode.customList[index] > rootNode.customList[parentIndex]:
            temp = rootNode.customList[index]
            rootNode.customList[index] = rootNode.customList[parentIndex]
            rootNode.customList[parentIndex] = temp
        heapifyTreeInsert(rootNode, parentIndex, heapType)

def inserNode(rootNode, nodeValue, heapType):
    if rootNode.heapSize + 1 == rootNode.maxSize:
        return "The Binary Heap is Full"
    rootNode.customList[rootNode.heapSize + 1] = nodeValue
    rootNode.heapSize += 1
    heapifyTreeInsert(rootNode, rootNode.heapSize, heapType)
    return "The value has been successfully inserted"

inserNode(newHeap, 4, "Max")
inserNode(newHeap, 7, "Max")
inserNode(newHeap, 2, "Max")
inserNode(newHeap, 1, "Max")
inserNode(newHeap, 6, "Max")
inserNode(newHeap, 13, "Max")
inserNode(newHeap, 20, "Max")
inserNode(newHeap, 8, "Max")
print(Sizeofheap(newHeap))
lvlOrderTraversal(newHeap)


#! python3

# This program performs the strip() method
# It will remove the characters speficied in the second arguement
# If no other argument is specified, all whitespace characters will be removed

import re

def Strip(text, stripChar=' '):
	stripRegex = re.compile(r'^(' + re.escape(stripChar) + r')+|(' + re.escape(stripChar) + r')+$' )
	stripTest = stripRegex.sub('',text)
	print(stripTest)

text = 'OMITTHISOMITTHISTest123OMITTHIS'
Strip(text, 'OMITTHIS')

Python/Automate the Boring Stuff With Python/RegexStrip.py


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

# In[1]:

import time
import os
import numpy as np
import torch
import torchvision.transforms as tv
from torchvision.utils import make_grid
from PIL import Image
from torchvision.transforms import Compose
import logging
import matplotlib.pyplot as plt
import pandas as pd
import glob

'''Inference workflow
 model = load_model(model_path) ->
 show_img_with_pred(image_path, model, show_results=1) | batch_prediction(images_folder_path, model, save_path='results.csv') '''

mylogs = logging.getLogger()
mylogs.setLevel(logging.INFO)
# Logging to file
file = logging.FileHandler("inference.log", mode='w')
file.setLevel(logging.INFO)
fileformat = logging.Formatter("%(asctime)s:%(levelname)s:%(message)s",datefmt="%H:%M:%S")
file.setFormatter(fileformat)
mylogs.addHandler(file)
# Logging to console
stream = logging.StreamHandler()
stream.setLevel(logging.INFO)
mylogs.addHandler(stream)
mylogs.info("Inference")

device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
mylogs.info(f"Device is: {str(device)}")

def load_model(model_path):
    ''' Loads a model and its state dict. Accepts path to the model, returns the model. Default cnn type is resnet18 '''
    saved_model = torch.load(f'{model_path}')
    model_name  = saved_model['model_name']
    categories = saved_model['сategories']
    mylogs.info("Model: " + model_name)

    import resnet
    if model_name == 'ResNet18':
            model = resnet.ResNet18()
    elif model_name == 'ResNet34':
            model = resnet.ResNet34()
    elif model_name == 'ResNet50':
            model = resnet.ResNet50()
    elif model_name == 'ResNet101':
            model = resnet.ResNet101()
    elif model_name == 'ResNet152':
            model = resnet.ResNet152()
    else:
            model = resnet.ResNet18()
    model.load_state_dict(torch.load(model_path)['model_state_dict'])
    model.eval()

    return model, categories

def transform():
    return tv.Compose([
        tv.ToTensor(),
        tv.Normalize((0, 0, 0),(1, 1, 1))
    ])

def image_loader(image_path):
    '''loads image,transforms it and returns tensor'''
    img = Image.open(image_path)
    img = img.resize((224, 224))
    img = transform()(img)
    #As torch models expects a batch, this image should be turned into a batch with 1 image
    img  = img.unsqueeze(0)
    return img.cpu()

def show_img_with_pred(image_path, model,categories, show_results):
    ''' Predicts one image, returns prediction '''
    img = image_loader(image_path)
    img = img.to('cpu')
    model.to('cpu')
    with torch.no_grad():
        prediction = float(model(img))
    if show_results:
        img = img.to('cpu')
        figure, ax = plt.subplots(figsize=(6,6))
        plt.title(categories[0] if prediction <= 0.5 else categories[1], color= 'black')
        plt.imshow(make_grid(img,1).permute(1,2,0))
        ax.text(3, 8, (round(prediction,3)), style='italic',
        bbox={'facecolor': 'white', 'alpha': 0.5, 'pad': 10})
        plt.show()
    return prediction

def batch_prediction(images_folder_path, model, model_categories, show_results, save_path=None):
    ''' Make an inference on the images in the folder,can save results as a .csv, returns pandas dataframe'''
    imgs_path_list = glob.glob(f'{images_folder_path}\*')
    mylogs.info(f"Inference started. \n {len(imgs_path_list)} images to predict")
    predictions = []
    categories = []
    names = []
    start_time = time.time()
    for image_path in imgs_path_list:
        names.append(os.path.split(image_path)[1])
        prediction = show_img_with_pred(image_path, model,categories,show_results=0)
        category = (model_categories[0] if prediction <= 0.5 else model_categories[1])
        predictions.append(prediction)
        categories.append(category)

    df = pd.DataFrame({'Image' : pd.Series(names), 'Predictions' : pd.Series(predictions), 'Categories' : pd.Series(categories)})
    end_time = time.time()
    total_time = int(end_time - start_time)
    mylogs.info(f'Inference finished. Elapsed time: {total_time }s')
    if save_path:
        df.to_csv(save_path)
        mylogs.info('Results saved')
    if show_results:
        df.hist(column = "Predictions")
        plt.show()
    return df