import unittest
import tempfile
import random
import errno
import stat
import os

from pyverbs.qp import QPCap, QPInitAttrEx, QPInitAttr, QPAttr, QP
from pyverbs.pyverbs_error import PyverbsRDMAError
from pyverbs.addr import AHAttr, GlobalRoute
from pyverbs.xrcd import XRCD, XRCDInitAttr
from pyverbs.srq import SRQ, SrqInitAttrEx
from pyverbs.cmid import CMID, AddrInfo
from pyverbs.device import Context
import pyverbs.cm_enums as ce
import pyverbs.device as d
import pyverbs.enums as e
from pyverbs.pd import PD
from pyverbs.cq import CQ
from pyverbs.mr import MR

PATH_MTU = e.IBV_MTU_1024
MAX_DEST_RD_ATOMIC = 1
MAX_RD_ATOMIC = 1
MIN_RNR_TIMER =12
RETRY_CNT = 7
RNR_RETRY = 7
TIMEOUT = 14

class PyverbsAPITestCase(unittest.TestCase):
    def setUp(self):
        """
        Opens the devices and queries them
        """
        lst = d.get_device_list()
        self.devices = []
        if len(lst) == 0:
            raise unittest.SkipTest('No IB devices found')
        for dev in lst:
            c = d.Context(name=dev.name.decode())
            attr = c.query_device()
            attr_ex = c.query_device_ex()
            self.devices.append((c, attr, attr_ex))

    def tearDown(self):
        for tup in self.devices:
            tup[0].close()

class RDMATestCase(unittest.TestCase):
    """
    A base class for test cases which provides the option for user parameters.
    These can be provided by manually adding the test case to the runner:
    suite = unittest.TestSuite()
    ... # Regular auto-detection of test cases, no parameters used.
    # Now follows your manual addition of test cases e.g:
    suite.addTest(RDMATestCase.parametrize(<TestCaseName>, dev_name='..',
                                           ib_port=1, gid_index=3,
                                           pkey_index=42))
    """
    ZERO_GID = '0000:0000:0000:0000'

    def __init__(self, methodName='runTest', dev_name=None, ib_port=None,
                 gid_index=None, pkey_index=None):
        super(RDMATestCase, self).__init__(methodName)
        self.dev_name = dev_name
        self.ib_port = ib_port
        self.gid_index = gid_index
        self.pkey_index = pkey_index

    @staticmethod
    def parametrize(testcase_klass, dev_name=None, ib_port=None, gid_index=None,
                    pkey_index=None):
        """
        Create a test suite containing all the tests from the given subclass
        with the given dev_name, port, gid index and pkey_index.
        """
        loader = unittest.TestLoader()
        names = loader.getTestCaseNames(testcase_klass)
        suite = unittest.TestSuite()
        for n in names:
            suite.addTest(testcase_klass(n, dev_name=dev_name, ib_port=ib_port,
                                         gid_index=gid_index,
                                         pkey_index=pkey_index))
        return suite

    def setUp(self):
        """
        Verify that the test case has dev_name, ib_port, gid_index and pkey index.
        If not provided by the user, a random valid combination will be used.
        """
        if self.pkey_index is None:
            # To avoid iterating the entire pkeys table, if a pkey index wasn't
            # provided, use index 0 which is always valid
            self.pkey_index = 0

        self.args = []
        if self.dev_name is not None:
            ctx = d.Context(name=self.dev_name)
            if self.ib_port is not None:
                if self.gid_index is not None:
                    # We have all we need, return
                    return
                else:
                    # Add avaiable GIDs of the given dev_name + port
                    self._add_gids_per_port(ctx, self.dev_name, self.ib_port)
            else:
                # Add available GIDs for each port of the given dev_name
                self._add_gids_per_device(ctx, self.dev_name)
        else:
            # Iterate available devices, add available GIDs for each of
            # their ports
            lst = d.get_device_list()
            for dev in lst:
                dev_name = dev.name.decode()
                ctx = d.Context(name=dev_name)
                self._add_gids_per_device(ctx, dev_name)

        if not self.args:
            raise unittest.SkipTest('No port is up, can\'t run traffic')
        # Choose one combination and use it
        args = random.choice(self.args)
        self.dev_name = args[0]
        self.ib_port = args[1]
        self.gid_index = args[2]

    def _add_gids_per_port(self, ctx, dev, port):
        # Don't add ports which are not active
        if ctx.query_port(port).state != e.IBV_PORT_ACTIVE:
            return
        idx = 0
        while True:
            gid = ctx.query_gid(port, idx)
            if gid.gid[-19:] == self.ZERO_GID:
                # No point iterating on
                break
            else:
                self.args.append([dev, port, idx])
                idx += 1

    def _add_gids_per_device(self, ctx, dev):
        port_count = ctx.query_device().phys_port_cnt
        for port in range(port_count):
            self._add_gids_per_port(ctx, dev, port+1)

class CMResources:
    """
    CMResources class is a base aggregator object which contains basic
    resources for RDMA CM communication.
    """
    def __init__(self, **kwargs):
        """
        :param kwargs: Arguments:
            * *src* (str)
               Local address to bind to (for passive side)
            * *dst* (str)
               Destination address to connect (for active side)
            * *port* (str)
                Port number of the address
        """
        src = kwargs.get('src')
        dst = kwargs.get('dst')
        self.is_server = True if dst is None else False
        self.qp_init_attr = None
        self.msg_size = 1024
        self.num_msgs = 100
        self.port = kwargs.get('port') if kwargs.get('port') else '7471'
        self.mr = None
        if self.is_server:
            self.ai = AddrInfo(src, self.port, ce.RDMA_PS_TCP, ce.RAI_PASSIVE)
        else:
            self.ai = AddrInfo(dst, self.port, ce.RDMA_PS_TCP)
        self.create_qp_init_attr()
        self.cmid = CMID(creator=self.ai, qp_init_attr=self.qp_init_attr)

    def create_mr(self, cmid):
        self.mr = cmid.reg_msgs(self.msg_size)

    def create_qp_init_attr(self):
        self.qp_init_attr = QPInitAttr(cap=QPCap(max_recv_wr=1))

    def pre_run(self):
        if self.is_server:
            self.cmid.listen()
        else:
            self.cmid.connect()

class BaseResources(object):
    """
    BaseResources class is a base aggregator object which contains basic
    resources like Context and PD. It opens a context over the given device
    and port and allocates a PD.
    """
    def __init__(self, dev_name, ib_port, gid_index):
        """
        Initializes a BaseResources object.
        :param dev_name: Device name to be used (default: 'ibp0s8f0')
        :param ib_port: IB port of the device to use (default: 1)
        :param gid_index: Which GID index to use (default: 0)
        """
        self.ctx = Context(name=dev_name)
        self.gid_index = gid_index
        self.pd = PD(self.ctx)
        self.ib_port = ib_port

class TrafficResources(BaseResources):
    """
    Basic traffic class. It provides the basic RDMA resources and operations
    needed for traffic.
    """
    def __init__(self, dev_name, ib_port, gid_index):
        """
        Initializes a TrafficResources object with the given values and creates
        basic RDMA resources.
        :param dev_name: Device name to be used
        :param ib_port: IB port of the device to use
        :param gid_index: Which GID index to use
        """
        super(TrafficResources, self).__init__(dev_name=dev_name,
                                               ib_port=ib_port,
                                               gid_index=gid_index)
        self.psn = random.getrandbits(24)
        self.msg_size = 1024
        self.num_msgs = 1000
        self.port_attr = None
        self.mr = None
        self.cq = None
        self.qp = None
        self.rqpn = 0
        self.rpsn = 0
        self.init_resources()

    @property
    def qpn(self):
        return self.qp.qp_num

    def init_resources(self):
        """
        Initializes a CQ, MR and an RC QP.
        :return: None
        """
        self.port_attr = self.ctx.query_port(self.ib_port)
        self.create_cq()
        self.create_mr()
        self.create_qp()

    def create_cq(self):
        """
        Initializes self.cq with a CQ of depth <num_msgs> - defined by each
        test.
        :return: None
        """
        self.cq = CQ(self.ctx, self.num_msgs, None, None, 0)

    def create_mr(self):
        """
        Initializes self.mr with an MR of length <msg_size> - defined by each
        test.
        :return: None
        """
        self.mr = MR(self.pd, self.msg_size, e.IBV_ACCESS_LOCAL_WRITE)

    def create_qp(self):
        """
        Initializes self.qp with an RC QP.
        :return: None
        """
        qp_caps = QPCap(max_recv_wr=self.num_msgs)
        qp_init_attr = QPInitAttr(qp_type=e.IBV_QPT_RC, scq=self.cq,
                                  rcq=self.cq, cap=qp_caps)
        qp_attr = QPAttr(port_num=self.ib_port)
        self.qp = QP(self.pd, qp_init_attr, qp_attr)

    def pre_run(self, rpsn, rqpn):
        """
        Modify the QP's state to RTS and fill receive queue with <num_msgs> work
        requests.
        This method is not implemented in this class.
        :param rpsn: Remote PSN
        :param rqpn: Remote QPN
        :return: None
        """
        raise NotImplementedError()

class RCResources(TrafficResources):

    def to_rts(self):
        """
        Set the QP attributes' values to arbitrary values (same values used in
        ibv_rc_pingpong).
        :return: None
        """
        attr = QPAttr(port_num=self.ib_port)
        attr.dest_qp_num = self.rqpn
        attr.path_mtu = PATH_MTU
        attr.max_dest_rd_atomic = MAX_DEST_RD_ATOMIC
        attr.min_rnr_timer = MIN_RNR_TIMER
        attr.rq_psn = self.psn
        attr.sq_psn = self.rpsn
        attr.timeout = TIMEOUT
        attr.retry_cnt = RETRY_CNT
        attr.rnr_retry = RNR_RETRY
        attr.max_rd_atomic = MAX_RD_ATOMIC
        gr = GlobalRoute(dgid=self.ctx.query_gid(self.ib_port, self.gid_index),
                         sgid_index=self.gid_index)
        ah_attr = AHAttr(port_num=self.ib_port, is_global=1, gr=gr,
                         dlid=self.port_attr.lid)
        attr.ah_attr = ah_attr
        self.qp.to_rts(attr)

    def pre_run(self, rpsn, rqpn):
        """
        Configure Resources before running traffic
        :param rpsn: Remote PSN (packet serial number)
        :param rqpn: Remote QP number
        :return: None
        """
        self.rqpn = rqpn
        self.rpsn = rpsn
        self.to_rts()

class UDResources(TrafficResources):
    UD_QKEY = 0x11111111
    UD_PKEY_INDEX = 0
    GRH_SIZE = 40

    def create_mr(self):
        self.mr = MR(self.pd, self.msg_size + self.GRH_SIZE,
                     e.IBV_ACCESS_LOCAL_WRITE)

    def create_qp(self):
        qp_caps = QPCap(max_recv_wr=self.num_msgs)
        qp_init_attr = QPInitAttr(qp_type=e.IBV_QPT_UD, cap=qp_caps,
                                  scq=self.cq, rcq=self.cq)
        qp_attr = QPAttr(port_num=self.ib_port)
        qp_attr.qkey = self.UD_QKEY
        qp_attr.pkey_index = self.UD_PKEY_INDEX
        self.qp = QP(self.pd, qp_init_attr, qp_attr)

    def pre_run(self, rpsn, rqpn):
        self.rqpn = rqpn
        self.rpsn = rpsn

class XRCResources(TrafficResources):
    def __init__(self, dev_name, ib_port, gid_index, qp_count=2):
        self.temp_file = None
        self.xrcd_fd = -1
        self.xrcd = None
        self.srq = None
        self.qp_count = qp_count
        self.sqp_lst = []
        self.rqp_lst = []
        self.qps_num = []
        self.psns = []
        self.rqps_num = None
        self.rpsns = None
        super(XRCResources, self).__init__(dev_name, ib_port, gid_index)

    def close(self):
        os.close(self.xrcd_fd)
        self.temp_file.close()

    def create_qp(self):
        """
        Initializes self.qp with an XRC SEND/RECV QP.
        :return: None
        """
        qp_attr = QPAttr(port_num=self.ib_port)
        qp_attr.pkey_index = 0

        for _ in range(self.qp_count):
            attr_ex = QPInitAttrEx(qp_type=e.IBV_QPT_XRC_RECV,
                                   comp_mask=e.IBV_QP_INIT_ATTR_XRCD,
                                   xrcd=self.xrcd)
            qp_attr.qp_access_flags = e.IBV_ACCESS_REMOTE_WRITE | \
                                      e.IBV_ACCESS_REMOTE_READ
            recv_qp = QP(self.ctx, attr_ex, qp_attr)
            self.rqp_lst.append(recv_qp)

            qp_caps = QPCap(max_send_wr=self.num_msgs, max_recv_sge=0,
                            max_recv_wr=0)
            attr_ex = QPInitAttrEx(qp_type=e.IBV_QPT_XRC_SEND, sq_sig_all=1,
                                   comp_mask=e.IBV_QP_INIT_ATTR_PD,
                                   pd=self.pd, scq=self.cq, cap=qp_caps)
            qp_attr.qp_access_flags = 0
            send_qp =QP(self.ctx, attr_ex, qp_attr)
            self.sqp_lst.append(send_qp)
            self.qps_num.append((recv_qp.qp_num, send_qp.qp_num))
            self.psns.append(random.getrandbits(24))

    def create_xrcd(self):
        """
        Initializes self.xrcd with an XRC Domain object.
        :return: None
        """
        self.temp_file = tempfile.NamedTemporaryFile()
        self.xrcd_fd = os.open(self.temp_file.name, os.O_RDONLY | os.O_CREAT,
                               stat.S_IRUSR | stat.S_IRGRP)
        init = XRCDInitAttr(
            e.IBV_XRCD_INIT_ATTR_FD | e.IBV_XRCD_INIT_ATTR_OFLAGS,
            os.O_CREAT, self.xrcd_fd)
        try:
            self.xrcd = XRCD(self.ctx, init)
        except PyverbsRDMAError as ex:
            if ex.error_code == errno.EOPNOTSUPP:
                raise unittest.SkipTest('Create XRCD is not supported')
            raise ex

    def create_srq(self):
        """
        Initializes self.srq with a Shared Receive QP object.
        :return: None
        """
        srq_attr = SrqInitAttrEx(max_wr=self.qp_count*self.num_msgs)
        srq_attr.srq_type = e.IBV_SRQT_XRC
        srq_attr.pd = self.pd
        srq_attr.xrcd = self.xrcd
        srq_attr.cq = self.cq
        srq_attr.comp_mask = e.IBV_SRQ_INIT_ATTR_TYPE | e.IBV_SRQ_INIT_ATTR_PD | \
                             e.IBV_SRQ_INIT_ATTR_CQ | e.IBV_SRQ_INIT_ATTR_XRCD
        self.srq = SRQ(self.ctx, srq_attr)

    def to_rts(self):
        gid = self.ctx.query_gid(self.ib_port, self.gid_index)
        gr = GlobalRoute(dgid=gid, sgid_index=self.gid_index)
        ah_attr = AHAttr(port_num=self.ib_port, is_global=True,
                         gr=gr, dlid=self.port_attr.lid)
        qp_attr = QPAttr()
        qp_attr.path_mtu = PATH_MTU
        qp_attr.timeout = TIMEOUT
        qp_attr.retry_cnt = RETRY_CNT
        qp_attr.rnr_retry = RNR_RETRY
        qp_attr.min_rnr_timer = MIN_RNR_TIMER
        qp_attr.ah_attr = ah_attr
        for i in range(self.qp_count):
            qp_attr.dest_qp_num = self.rqps_num[i][1]
            qp_attr.rq_psn = self.psns[i]
            qp_attr.sq_psn = self.rpsns[i]
            self.rqp_lst[i].to_rts(qp_attr)
            qp_attr.dest_qp_num = self.rqps_num[i][0]
            self.sqp_lst[i].to_rts(qp_attr)

    def init_resources(self):
        self.create_xrcd()
        super(XRCResources, self).init_resources()
        self.create_srq()

    def pre_run(self, rpsns, rqps_num):
        self.rqps_num = rqps_num
        self.rpsns = rpsns
        self.to_rts()


import urllib.request as urllib2
from bs4 import BeautifulSoup
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
from string import punctuation
from nltk.probability import FreqDist
from heapq import nlargest
from collections import defaultdict, namedtuple

homePageUrl = "https://www.glanos.de/blog/"
articles = []
UrlTupel = namedtuple('UrlTupel', 'title link')
ArticleTupel = namedtuple('ArticleTupel', 'title text')

# Erstellen einer Liste von URLs zu allen Blogeinträgen, die auf einer Seite verlinkt sind, inkl. Titel:
def getListOfUrl(url):
    homePage = urllib2.urlopen(url).read().decode('utf8', 'ignore')
    homeSoup = BeautifulSoup(homePage, 'lxml')
    res = list()
    for link in homeSoup.find_all("a"):
        title = link.get("title")
        if title:
            resLink = UrlTupel(title, link.get("href"))
            res.append(resLink)

    urlList = list(set(res))
    return urlList

# Extraktion der Blogartikel auf der jeweiligen Seite als Liste von Tupel (Titel, Text):
def getTextFromUrl(urlList):
    articleList = list()
    for elem in urlList:
        fullArticle = []
        page = urllib2.urlopen(elem.link).read().decode('utf8', 'ignore')
        soup = BeautifulSoup(page, 'lxml')
        paragraphs = soup.find_all('p')
        for para in paragraphs:
            fullArticle.append(para.text)
        article = "".join(fullArticle)
        articleWithTitle = ArticleTupel(elem.title, article)
        articleList.append(articleWithTitle)
    return articleList

urlList = getListOfUrl(homePageUrl)
articleList = getTextFromUrl(urlList)

# Weiterverarbeitung der extrahierten Blogseiten

ownStopWords = ["„", "“", "–", "”"]  # Eigene Liste mit Satzzeichen, die in importierter Liste fehlten
stopwords = set(stopwords.words('german') + stopwords.words('english') + list(punctuation) + ownStopWords)  # Liste mit Stopwords auf Deutsch und Englisch und Satzzeichen

# Zählen der Häufigkeit der Wörter in jedem Artikel
for article in articleList:
    sentences = sent_tokenize(article.text)
    words = word_tokenize(article.text)
    words = [word for word in words if word.lower() not in [stopword.lower() for stopword in stopwords]]  # Stopwords und Satzzeichen sollen in Häufigkeitsliste ignoriert werden
    freq = FreqDist(words)

    # Ranking der Sätze durch Häufigkeit der darin enthaltenen Wörter
    ranking = defaultdict(int)
    for i, sent in enumerate(sentences):
        for w in word_tokenize(sent.lower()):
            if w in freq:
                ranking[i] += freq[w]

    # Erstellen einer Zusammenfassung der Artikel aus den drei wichtigsten Sätzen
    sentencesNumber = nlargest(3, ranking, key=ranking.get)
    sentencesSorted = [sentences[i] for i in sorted(sentencesNumber)]
    summary = "".join(sentencesSorted)
    print(article.title + ":")
    print(summary)


import json
import os
import sys
from typing import Optional
from git import Repo

from _utils.console import Console
from _utils.exit import Exit

from _core.exception import GitError, VersionNotFound, VersionAlreadyExist

class TagCreator(object):
    def __init__(self, verbose: bool = False):
        self.verbose = verbose
        self.console = Console(verbose)

    def create(self, base_dir: str, input_version: Optional[str]) -> dict:
        repo: Repo = Repo(base_dir)
        commit = repo.commit(repo.head)
        version: str = self.project_version(base_dir)

        if version is None and input_version is not None:
            version = input_version

        if version is None:
            raise VersionNotFound(base_dir)

        version_formatted: str = self.format_version(version)

        if (len(repo.tags)) == 0:
            latest_version = ''
        else:
            latest_version = repo.tags[len(repo.tags) - 1]

        if latest_version != '' and version_formatted == str(latest_version):
            raise VersionAlreadyExist(latest_version)

        repo.create_tag(version_formatted, commit, 'Version %s' % version_formatted)
        repo.remote().push(version_formatted)

        return dict(
            version=version_formatted,
            commit_checksum=commit.hexsha,
            commit_author=commit.author,
            commit_message=commit.message
        )

    def format_version(self, version: str) -> str:
        if not version.startswith('v'):
            return "v{}".format(version)

        return version

    def project_version(self, base_dir: str) -> Optional[str]:
        if os.path.isfile(base_dir + '/composer.json'):
            return self.php_project_version(base_dir)
        else:
            return None

    def php_project_version(self, base_dir: str) -> Optional[str]:
        with open(base_dir + '/composer.json', 'r') as composer_file:
            composer_data: dict = json.load(composer_file)

            if 'version' in composer_data:
                return composer_data['version']
            else:
                return None

if __name__ == "__main__":
    sys.exit(1)


from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import nltk

# --------------------------------------------------Preprocessing-------------------------------------------------------

def preprocess(text):
    # convert to lowercase, tokenize text, remove everything that's not a word
    text = text.lower()
    text = nltk.word_tokenize(text)
    text = [word for word in text if any(letter.isalpha() for letter in word)]

    # tag words with part of speech tags
    text = nltk.pos_tag(text)

    # change tags to wordnet tags
    text = [(word, get_wordnet_pos(pos)) for (word, pos) in text]

    # remove stopwords
    wo_stop = [(word, pos) for (word, pos) in text if word not in stopwords.words('english')]

    # remove all untagged words
    wo_stop = [(word, pos) for (word, pos) in wo_stop if pos != '']

    # remove every token < 3 characters
    wo_stop = [(word, pos) for (word, pos) in wo_stop if len(word) > 2]

    # lemmatize the text with word net
    wordnet_lemmatizer = WordNetLemmatizer()
    lemma = [wordnet_lemmatizer.lemmatize(word, pos=pos) for (word, pos) in wo_stop]

    return lemma

# ---------------------------------------change nltk POS Tags to Wordnet tags-------------------------------------------

def get_wordnet_pos(pos_tag):
    noun = ['CD', 'FW', 'NN', 'NNS', 'NNP', 'NNPS']
    adjective = ['JJ', 'JJR', 'JJS']
    adverb = ['PDT', 'IN', 'DT', 'MD', 'RB', 'RBR', 'RBS', 'RP']
    verb = ['VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ']

    if pos_tag in noun:
        return 'n'

    elif pos_tag in adjective:
        return 'a'

    elif pos_tag in adverb:
        return 'r'

    elif pos_tag in verb:
        return 'v'

    else:
        return ''

# --------------------------------------check if word is synonym of another---------------------------------------------

def check_wordnet_synonym(word_one, word_two):
    word_synonyms = []

    # find all synonyms for word one
    for synset in wordnet.synsets(word_one):

        # don't write the word itself in the list
        for lemma in synset.lemma_names():
            if lemma != word_one:
                word_synonyms.append(lemma)

    # check if second word is in the synonym list of the first one
    if word_two in word_synonyms:
        return True
    else:
        return False

# --------------------------------------check if word is a hypernym of another------------------------------------------

def check_wordnet_hypernym(word_one, word_two):
    word_hypernyms = []

    if len(wordnet.synsets(word_one)) > 0:

        # find all hypernyms of the first word
        for words in wordnet.synsets(word_one)[0].hypernyms():

            # don't write the word itself in the list
            for lemma in words.lemma_names():
                if lemma != word_one:
                    word_hypernyms.append(lemma)

        # check if second word is in the hypernym list of the first one
        if word_two in word_hypernyms:
            return True
        else:
            return False

    else:
        return False

# --------------------------------------check if word is a hyponym of another-------------------------------------------

def check_wordnet_hyponym(word_one, word_two):
    word_hyponyms = []

    if len(wordnet.synsets(word_one)) > 0:

        # find all hyponyms of the first word
        for words in wordnet.synsets(word_one)[0].hyponyms():

            # don't write the word itself in the list
            for lemma in words.lemma_names():
                if lemma != word_one:
                    word_hyponyms.append(lemma)

        # check if second word is in the hyponym list of the first one
        if word_two in word_hyponyms:
            return True
        else:
            return False

    else:
        return False

# ----------------------------approach one: check if words of one list are in other list--------------------------------

def simple_differentiation(list_one, list_two):
    # remove words that are in both lists
    result = [word for word in list_one if word not in list_two]

    # return the differences
    return result

# ---------approach two: check if words of one list are synonym, hypernom or hyponym of words in other list-------------

def semantic_differentiation(list_one, list_two):
    result = []
    same = False

    # remove all words that are synonyms, hypernyms or hyponyms to one another
    for word in list_one:
        for word2 in list_two:
            if check_wordnet_synonym(word, word2) or check_wordnet_hypernym(word, word2) \
                    or check_wordnet_hyponym(word, word2):
                same = True
        if not same:
            result.append(word)
        same = False

    # return the differences
    return result


import torch
from torch_scatter import scatter_add

class KGATNorm(object):
    r"""Row-normalizes Relational Adjacency Matrix"""

    def __init__(self, adj_type='si'):
        self.adj_type = adj_type

    def __call__(self, data):

        m = data.n_nodes_ckg
        edge_weights = torch.empty(data.edge_type.size())

        if self.adj_type == 'si':
            for relation in range(data.n_relations_ckg):
                adj_t = data.edge_index[:, data.edge_type == relation]
                edge_weight = torch.ones(adj_t.size(1))

                deg = scatter_add(edge_weight, adj_t[0])
                deg_inv_sqrt = deg.pow_(-1)
                deg_inv_sqrt.masked_fill_(deg_inv_sqrt == float('inf'), 0)

                edge_weights[data.edge_type == relation] = deg_inv_sqrt[adj_t[0]] * edge_weight

        data.edge_weights = edge_weights

        return data

    def __repr__(self):
        return '{}()'.format(self.__class__.__name__)


from openerp.osv import osv, fields

class account_invoice(osv.Model):
    _inherit = 'account.invoice'
    _columns = {
        'x_serino': fields.char(
            'Fatura No',
            size=64,
            required=False,
            select=1),
        'x_teslimat': fields.char(
            'Teslimat Kisaltmasi',
            size=64,
            required=False),
        'address_contact_id': fields.many2one(
            'res.partner',
            'Shipping Address'
            ),

    }
account_invoice()


import pyautogui
import time
import keyboard
import urllib
import os
import webbrowser
import requests

x=((os.path.abspath(__file__).replace(os.path.basename(__file__),"")) + "Python") #x = path-ul fisierului "Python"
if not os.path.exists(x):
    os.mkdir(x) #Daca nu exista acest folder, il creem.
thisdir = os.getcwd()

path1 = x + r"\like1.png" #Setam path-ul de instalare al pozelor ce urmeaza cautate.
path2 = x + r"\like2.png" #

def Find_Browser(): #Cautam un browser din calculator, incercand sa deschidem browserul utilizat de persoana.
    force = False #Force este o variabila creeata pentru a forta scrierea linkului prin pyautogui.
    try:
        os.startfile('decentr.exe')
        force = True
    except: #Daca nu se gaseste browserul anterior mentionat trece prin exceptie la cautarea urmatorului browser.
        force = False
        try:
            os.startfile('brave.exe')
            force = True
        except:
            force = False
            try:
                os.startfile('opera.exe')
                force = True
            except:
                force = False
                try:
                    os.startfile('chrome.exe')
                    force = True
                except:
                    force = False
                    try:
                        os.startfile('msedge.exe')
                        force = True
                    except: #Daca niciun browser din lista nu este gasit, atunci se va deschide browserul predefinit din calculator.
                        force = False #Setam force in fals deoarece comanda de mai jos poate deschide in mod direct un link.
                        webbrowser.open("https://www.facebook.com/tnl.bacau")

    if(force == True): #Daca este gasit un browser din lista, se forteaza o deschidere a site ului prestabilit.
        time.sleep(1)
        pyautogui.hotkey("alt", "d")
        time.sleep(0.2)
        pyautogui.typewrite("https://www.facebook.com/tnl.bacau")
        pyautogui.press('enter')

def Download(): #Instalam pozele referinta de pe tumblr, adica de unde le-am uploadat.
    img_url = 'https://64.media.tumblr.com/63e1fe5bbda4c2144fd61b79b3f332e8/58f50439d66a2fda-c7/s250x400/ad30e8bb92d6df907248ab36a4228308ff009ab3.pnj'
    urllib.request.urlretrieve(img_url, path1) #Preia poza de pe internet si o instaleaza in path ul setat mai sus, sub numele "like1.png".
    img_url = 'https://64.media.tumblr.com/95f669f82b6ce4935d62ae41f5bd8cb1/58f50439d66a2fda-4b/s250x400/6ee64789a9cbd347e61d10718a0a29046922cd81.pnj'
    urllib.request.urlretrieve(img_url, path2) # -,,- "like2.png".

def Like(): #Cat timp nu se apasa butonul "Escape", cautam progresiv pe ecran cele 2 imagini de referinta.
    while not keyboard.is_pressed("Esc"):
        try:
            pyautogui.scroll(-300)
        except:
            pass
        if pyautogui.locateOnScreen(path1,confidence=0.7) != None:
            like = pyautogui.locateOnScreen(path1,confidence=0.7)
            pyautogui.click(like)
        elif pyautogui.locateOnScreen(path2,confidence=0.7) != None:
            like = pyautogui.locateOnScreen(path2,confidence=0.7)
            pyautogui.click(like)

#1
Find_Browser()
#2
Download()
#3
Like()


from pathlib import Path
import sys
import unittest

PROJECT_DIRECTORY = Path(__file__).parent.parent
sys.path.append(str(PROJECT_DIRECTORY))

from week1.lesson_1_2 import pattern_count
from week1.lesson_1_3 import frequency_map, frequent_words
from week1.lesson_1_4 import complement, reverse, reverse_complement, pattern_matching

import common

class TestLesson_1_2(unittest.TestCase):

    def test_pattern_count(self):

        file_path = PROJECT_DIRECTORY / "data" / "test_data" / "PatternCount.txt"
        test_data = common.retrieve_test_data(file_path)
        for (string, pattern), expected in test_data:
            result = pattern_count(string, pattern)
            self.assertEqual(result, int(expected))

        with open(Path(__file__).parent.parent / "data" / "v_cholerae_oric.txt", 'r') as stream:
            data = stream.read()

        result = pattern_count(data, "TGATCA")
        expected = 8
        self.assertEqual(result, expected)

class TestLesson_1_3(unittest.TestCase):

    def test_frequency_map(self):

        string = "CGATATATCCATAG"
        length = 3
        result = frequency_map(string, length)
        expected = {'CGA': 1, 'GAT': 1, 'ATA': 3, 'TAT': 2, 'ATC': 1, 'TCC': 1, 'CCA': 1, 'CAT': 1, 'TAG': 1}

        self.assertEqual(result, expected)

    def test_frequent_words(self):

        file_path = PROJECT_DIRECTORY / "data" / "test_data" / "FrequentWordsNoDuplicates.txt"
        test_data = common.retrieve_test_data(file_path)
        for (string, count), expected in test_data:
            result = frequent_words(string, int(count))
            expected = expected.split()
            self.assertEqual(result, expected)

        vibrio_cholerae = (
            "CAATGATCAACGTAAGCTTCTAAGCATGATCAAGGTGCTCACACAGTTTATCCACAACCTGAGTGGATGACATCAAGATAGGTCGTTGTATCTCCTTCCTCTCGTA"
            "CTCTCATGACCACGGAAAGATGATCAAGAGAGGATGATTTCTTGGCCATATCGCAATGAATACTTGTGACTTGTGCTTCCAATTGACATCTTCAGCGCCATATTGC"
            "GCTGGCCAAGGTGACGGAGCGGGATTACGAAAGCATGATCATGGCTGTTGTTCTGTTTATCTTGTTTTGACTGAGACTTGTTAGGATAGACGGTTTTTCATCACTG"
            "ACTAGCCAAAGCCTTACTCTGCCTGACATCGACCGTAAATTGATAATGAATTTACATGCTTCCGCGACGATTTACCTCTTGATCATCGATCCGATTGAAGATCTTC"
            "AATTGTTAATTCTCTTGCCTCGACTCATAGCCATGATGAGCTCTTGATCATGTTTCCTTAACCCTCTATTTTTTACGGAAGAATGATCAAGCTGCTGCTCTTGATC"
            "ATCGTTTC"
            )
        length = 10
        result = frequent_words(vibrio_cholerae, length)
        expected = ["CTCTTGATCA", "TCTTGATCAT"]

        self.assertEqual(result, expected)

class TestLesson_1_4(unittest.TestCase):

    def test_reverse(self):

        pattern = "AAAACCCGGT"
        result = reverse(pattern)
        expected = "TGGCCCAAAA"

        self.assertEqual(result, expected)

    def test_complement(self):

        pattern = "AAAACCCGGT"
        result = complement(pattern)
        expected = "TTTTGGGCCA"

        self.assertEqual(result, expected)

    def test_reverse_complement(self):

        file_path = PROJECT_DIRECTORY / "data" / "test_data" / "ReverseCompliment.txt"
        test_data = common.retrieve_test_data(file_path)
        for pattern_, expected in test_data:
            pattern = pattern_[0]  # Returns a list by implementation, take the only and first element.
            result = reverse_complement(pattern)
            self.assertEqual(result, expected)

    def test_pattern_matching(self):

        file_path = PROJECT_DIRECTORY / "data" / "test_data" / "PatternMatching.txt"
        test_data = common.retrieve_test_data(file_path)
        for (pattern, string), expected_ in test_data:
            expected = [int(item) for item in expected_.split()]
            result = pattern_matching(pattern, string)
            self.assertEqual(result, expected)

        with open(Path(__file__).parent.parent / "data" / "Vibrio_cholerae.txt", 'r') as stream:
            vibrio_cholerae = stream.read()

        pattern = "CTTGATCAT"
        result = pattern_matching(pattern, vibrio_cholerae)
        expected = [60039, 98409, 129189, 152283, 152354, 152411, 163207, 197028, 200160, 357976, 376771, 392723,
                    532935, 600085, 622755, 1065555]
        self.assertEqual(result, expected)

class TestLesson_1_5(unittest.TestCase):

    def test_thermotoga_petrophila(self):

        with open(Path(__file__).parent.parent / "data" / "t_petrophila_oriC.txt", 'r') as stream:
            thermotoga_petrophila = stream.read()

        pattern_a = "ATGATCAAG"
        pattern_b = "CTTGATCAT"
        length = len(pattern_a)

        count_a = frequency_map(thermotoga_petrophila, length).get(pattern_a, 0)
        count_b = frequency_map(thermotoga_petrophila, length).get(pattern_b, 0)

        self.assertEqual(count_a, 0)
        self.assertEqual(count_b, 0)

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


"""
pip install pysqlite3
"""

import sqlite3
conn = sqlite3.connect('user1.db')
conn.execute("create tabel if not exists data(name text not null);")

getUser = input("Fullname: ")
conn.execute("INSERT INTO data VALUES(?)",)

conn.commit()
conn.close()
print('Successfully...')


import torch
import torchvision.transforms as tf
import cv2
import os
import model

def predict(CNN_model, LSTM_model, folder_path, device):
    frames = get_frames_from_folder(folder_path)
    frames = frames.to(device)
    outputs = LSTM_model(CNN_model(frames))
    prediction = outputs.max(1, keepdim=True)[1]
    return prediction

def get_frames_from_folder(folder_path):
    transform = tf.Compose([tf.ToTensor(), tf.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])
    frames_names = os.listdir(folder_path)
    frames = []

    for frame_name in frames_names:
        frame_path = os.path.join(folder_path, frame_name)
        frame = cv2.imread(frame_path, cv2.IMREAD_COLOR)
        frame = cv2.resize(frame, (256, 256))
        frame = transform(frame)
        frames.append(frame)

    frames = torch.stack(frames, dim=0).view(1, 60, 3, 256, 256)
    return frames

def get_answer(folder_path):
    return torch.tensor(int(folder_path[-1])).to('cuda')

def print_inference_acc(cnn_model, lstm_model, folder_path, device):
    cnn_model = cnn_model
    lstm_model = lstm_model
    prediction = []
    name_folder = []
    correct = 0
    num_folders = len(os.listdir(folder_path))
    for folder in os.listdir(folder_path):
        video_path = os.path.join(folder_path, folder)
        name_folder.append(folder)
        pred = predict(cnn_model, lstm_model, video_path, device).to('cuda')
        answer = get_answer(folder)
        correct += pred.eq(answer.view_as(pred)).sum().item()
        prediction.append(pred.item())
    accuracy = correct / num_folders
    predict_dict = dict(zip(name_folder, prediction))
    predict_dict = dict(sorted(predict_dict.items(), key=lambda k: int(k[0][:-2])))
    print('Accuracy = {}%'.format(round(accuracy * 100, 2)))
    print(predict_dict)

if __name__=="__main__":
    folder_path = r'./data_ext/inference/'
    cnn_path = r'./weights/CNN_ext.pth'
    lstm_path = r'./weights/LSTM_ext.pth'

    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else 'cpu')

    CNN_model = model.CNN(5).to(device)
    LSTM_model = model.LSTM(5, 256, 2, 2).to(device)
    CNN_model.load_state_dict(torch.load(cnn_path), strict=False)
    LSTM_model.load_state_dict(torch.load(lstm_path), strict=False)
    CNN_model.eval()
    LSTM_model.eval()

    print_inference_acc(CNN_model, LSTM_model, folder_path, device)

    # prediction = []
    # name_folder = []
    # for folder in os.listdir(folder_path):
    #     video_path = os.path.join(folder_path, folder)
    #     name_folder.append(folder)
    #     local_predict = predict(CNN_model, LSTM_model, video_path, device)
    #     prediction.append(local_predict)
    # predict_dict = dict(zip(name_folder, prediction))
    # print(predict_dict)


#!python
"""advent of code 2022 day 14 part 1"""
lines = tuple(line.split(" -> ") for line in open("input.txt").read().splitlines())

min_x = min(int(rock.split(',')[0]) for rocks in lines for rock in rocks)
max_x = max(int(rock.split(',')[0]) for rocks in lines for rock in rocks)
min_y = min(int(rock.split(',')[1]) for rocks in lines for rock in rocks)
max_y = max(int(rock.split(',')[1]) for rocks in lines for rock in rocks)

grid = [[' ' for x in range(max_x+2)] for y in range(max_y+2)]

def pprint():
    print('\n'.join('║'+''.join(_[min_x-1:max_x+1])+'║' for _ in grid[min_y-4:max_y+4]))

for rocks in lines:
    for r1, r2 in zip(rocks, rocks[1:]):
        r1 = tuple(map(int, r1.split(',')))
        r2 = tuple(map(int, r2.split(',')))
        if r1[0]==r2[0]:
            for y in range(min(r1[1],r2[1]), max(r1[1],r2[1])+1):
                grid[y][r1[0]] = '█'
        if r1[1]==r2[1]:
            for x in range(min(r1[0],r2[0]), max(r1[0],r2[0])+1):
                grid[r1[1]][x] = '█'

def drop(x, y):
    if y > max_y:
        return False
    if grid[y+1][x-1]!=' ' and grid[y+1][x]!=' ' and grid[y+1][x+1]!=' ':
        grid[y][x] = 'o'
        return True
    if grid[y+1][x+1]==' ' and grid[y+1][x-1]!=' ' and grid[y+1][x]!=' ':
        return drop(x+1, y+1)
    if grid[y+1][x-1]==' ' and grid[y+1][x]!=' ':
        return drop(x-1, y+1)
    if grid[y+1][x]==' ':
        return drop(x, y+1)

answer = 0
while drop(500, 0):
    answer += 1

pprint()

print("part 1:", answer)


"""####################################################################
    Title: Solving the source localization problem of spinal cord
    potentials for electric dipoles in the cervical spinal cord
    Subtitle: Select a 3D model
    Author: Markus E. Oberndorfer
####################################################################"""

# ----- IMPORT PACKAGES -----
from ngsolve import Mesh

# ----- IMPORT CUSTOM FUNCTIONS -----
from .mesh_functions import CreateParamsGeom, CreateMesh

# ----- FUNCTIONS -----
def ModelSelection(measurments_geom, main_path, mesh_folder):
    """
    This function either load an existing mesh, or created a new one.

    Input:
        - user_ID: Is a short ID that points to the folder structure of
        interest. [str]
        - main_path: Contains the path of the main file [pathlib object]

    Output:
        - mesh: Is an object that stores the model geometry and its
        properties. [NGSolve mesh object]
        - params_geom: Software intern structure. See ReadMe.txt file
          for more information. [-]
        - loc_C7: Location of the seventh cervical vertebral body. [float]
        - maxh_: Is the maximum size of one mesh element. [float]
    """

    # create functions for measurment check and read_data
    params_geom = CreateParamsGeom(measurments_geom)

    path_vol_file = mesh_folder / 'mesh_file.vol'
    path_spine_params = mesh_folder / 'spine_params.txt'

    if path_vol_file.exists():
        mesh = Mesh(str(path_vol_file))
        mesh.Curve(3)

        spine_params_ = open(path_spine_params, 'r')
        spine_params = spine_params_.read()
        spine_params_.close()

        spine_params = spine_params.rsplit(';')
        loc_C7 = float(spine_params[0])
        maxh_ = float(spine_params[1])

        print('All mesh files found and loaded.')
    else:
        print('Mesh of the newest model does not exist. \
              Creating new Mesh now ...')
        save_file = [path_vol_file, path_spine_params]
        mesh, loc_C7, maxh_ = CreateMesh(measurments_geom, save_file)
        print('Mesh sucessfully generated!')

    return mesh, params_geom, loc_C7, maxh_

if __name__ == '__main__':
    ...


#######################
# Fix import issues   #
import sys            #
import traceback      #
sys.path.append(".")  #
sys.path.append("..") #
#######################

import numpy as np
import matplotlib.pyplot as plt
import src.dataloader as dataloader
import src.icp as icp
import src.visualization as visualization

print("Loading the data")
odometry, lidar_points, images = dataloader.parse_lcm_log("./data/EECS_3", load_images=True, image_stop=np.inf, n_jobs=-1)

pc1 = lidar_points[56]
pc1 = np.hstack((pc1, np.ones((len(pc1),1)))).T
pc2 = lidar_points[182]
pc2 = np.hstack((pc2, np.ones((len(pc2),1)))).T

transforms = [np.eye(3)]

fig, ax = plt.subplots()
ax.set_aspect("equal")
ax.scatter(pc1[0,:], pc1[1,:], color="red")
ax.scatter(pc2[0,:], pc2[1,:], color="blue")
plt.savefig("results/iter%02d.png" % 0)
for i in range(30):
	next_transform, correspondences, error = icp.icp_iteration(pc1.T, pc2.T, transforms[-1])
	ax.cla()
	print(next_transform)
	temp = next_transform @ pc1
	transforms.append(next_transform)
	visualization.draw_icp_iteration(ax, temp.T, pc2.T, correspondences)
	plt.savefig("results/iter%02d.png" % (i+1))
	print(error)
	if error < 0.01:
		break


from import_export import resources
from .models import Contact

class ContactResource(resources.ModelResource):

    class Meta:
        model = Contact
        fields = ('email', 'date')
        export_order = ('email', 'date')
        widgets = {
            'date': {'format': '%d.%m.%Y'},
        }


from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
import os

app = Flask(__name__)

dir_name = os.path.abspath(os.path.dirname(__file__))
db_uri = "sqlite:///" + os.path.join(dir_name, "flasksql_1.db")

app.config.update(
    DEBUG=True,
    SECRET_KEY="something-secret",
    WTF_CSRF_ENABLED=False,
    SQLALCHEMY_DATABASE_URI=db_uri,
    SQLALCHEMY_TRACK_MODIFICATIONS=False,
)

db = SQLAlchemy(app)

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(80), nullable=False)
    age = db.Column(db.Integer, nullable=False)
    job = db.Column(db.String(100), nullable=True)

    def __repr__(self):
        return f"<Person {id}"

    def to_dict(self):
        return {
            "name": self.name,
            "age": self.age,
            "job": self.job,
        }

@app.route("/")
def index():
    people = Person.query.all()
    by_name = Person.query.filter_by(name="Semen").first()
    by_age = Person.query.filter(Person.age > 30).all()
    by_job = Person.query.filter(Person.job == "HR")

    sub = db.session.query(db.func.min(Person.age).label("min_age")).subquery()

    youngest = Person.query.join(sub, sub.c.min_age == Person.age).first()

    return jsonify(
        {
            "people": [p.to_dict() for p in people],
            "by_name": by_name.to_dict(),
            "by_age": [p.to_dict() for p in by_age],
            "by_job": [p.to_dict() for p in by_job],
        }
    )

if __name__ == "__main__":
    # creating tables in the database
    db.create_all()

    # delete all the records in Person
    Person.query.delete()

    # creating new ones
    ivan = Person(name="Ivan", age=3)
    sveta = Person(name="Sveta", age=32, job="HR")
    semen = Person(name="Semen", age=30, job="IT")
    kolya = Person(name="Kolya", age=23, job="HR")

    db.session.add(ivan)
    db.session.add(sveta)
    db.session.add(semen)
    db.session.add(kolya)

    db.session.commit()

    app.run()


# -*- coding: utf-8 -*-
"""Django page CMS test suite module for page links"""

from pages import settings
from pages.tests.testcase import TestCase
from pages.models import Content

class LinkTestCase(TestCase):
    """Django page CMS link test suite class"""

    def test_01_set_body_pagelink(self):
        """Test the get_body_pagelink_ids and set_body_pagelink functions."""
        setattr(settings, "PAGE_LINK_FILTER", True)
        page1 = self.create_new_page()
        page2 = self.create_new_page()
        # page2 has a link on page1
        content_string = 'test hello'
        content = Content(
            page=page2,
            language='en-us',
            type='body',
            body=content_string % ('#', page1.id)
        )
        content.save()
        self.assertEqual(
            Content.objects.get_content(page2, 'en-us', 'body'),
            content_string % (page1.get_url_path(), page1.id)
        )
        self.assertFalse(page2.has_broken_link())
        page1.delete()
        self.assertEqual(
            Content.objects.get_content(page2, 'en-us', 'body'),
            'test hello'
        )
        self.assertTrue(page2.has_broken_link())


"""
Modulo Collections - Counter (contador)

Collections -> High-performance Container Datetypes

Counter -> Rece um iteravel como parametro e cria um objeto do tipo collections counter
que eh parecido com um dicionario, contendo como chave um elemento da lista passada por paramentro
e como valor a quantidade de ocorrencias desse eleemento

lista = [1, 1, 1, 2, 2, 3, 3, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 5, 3, 45, 45, 66, 66, 43, 34]

#Utilizando o Counter
res = Counter(lista)

print(type(res))

print(res)

#Para cada elemento da lista, o Counter criou uma chave e colocou como valor a quantidade de ocorrencias

#Exemplo 2
print(Counter('Eduardo'))
"""

#Utilizando o counter

from collections import Counter

#Exemplo 3

texto = """
Aprenda Python 3.8.5 com Expressões Lambdas,
Iteradores, Geradores, Orientação a Objetos e muito mais!
"""

palavras = texto.split()

res = Counter(palavras)

print(res)

#Encontrando as 5 palavras com mais ocorrencias no texto
print(res.most_common(5))


import numpy as np
import cv2 as cv2
from skimage import data, io
import matplotlib.pyplot as plt
import matplotlib
from sklearn import preprocessing
from skimage.transform import resize
import random

im = data.page()
if len(im.shape) == 3:  # converting to gray scale if image is RGB
    im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)  # h 400 * w 600

fig = plt.figure(figsize=(30, 30))
rows = 2
columns = 2
fig.add_subplot(rows, columns, 1)
plt.imshow(im, cmap="gray")

out_thresholded = cv2.adaptiveThreshold(
    src=im,
    maxValue=255,
    adaptiveMethod=cv2.ADAPTIVE_THRESH_MEAN_C,
    thresholdType=cv2.THRESH_BINARY,
    blockSize=11,
    C=2,
)
fig.add_subplot(rows, columns, 2)
plt.imshow(out_thresholded, cmap="gray")
plt.show()

2020-03-19 Pooling, Edge Detection, Thresholding/4_Otsu Thresholding/adaptive_thresholding.py


# coding: utf-8
from django.utils.translation import ugettext as _
from django.http import Http404, HttpResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from empor.shortcuts import JsonResponse
from cart.utils import archive_cart
from cart.models import CartItem, ArchivedCartItem
from cart.views import get_cart
from discount.models import Voucher
from order.models import OrderItem, Order
from order.forms import OrderForm
from django.conf import settings

@login_required
def index(request):
    cart = get_cart(request)
    items = CartItem.objects.filter(cart=cart)
    if request.method == 'POST':
        voucher_code = request.POST.get('voucher_code', None)
        form = OrderForm(request.POST)
        if form.is_valid():
            order = form.save(commit=False)
            order.discount_total = cart.discount_total
            order.gross_total = cart.gross_total
            order.net_total = cart.net_total
            order.user = request.user
            order.voucher_code = voucher_code
            order.order_id = ''
            order.billing_post_code = request.POST['billing_zip']
            order.billing_county = request.POST['billing_county']
            order.billing_district = request.POST['billing_district']
            order.shipping_post_code = request.POST['shipping_zip']
            order.shipping_county = request.POST['shipping_county']
            order.shipping_district = request.POST['shipping_district']
            order.save()
            request.session.save()
            request.session['order'] = order

            if order.payment_method == 0:
                return redirect('order-paypal')
            else:
                return redirect('order-success')
    else:
        profile = request.user.profile
        form = OrderForm(initial={
                'billing_recipient': request.user.get_name(),
                'billing_phone' : profile.phone,
                'billing_address': profile.address,
                'billing_country': profile.country,
                'payment_method': 0,
                'reciept_type': 0,
                'dispatch_time': 0,
            }
        )
    try:
        voucher = request.session['voucher']
    except KeyError:
        voucher = None

    return render(request, 'order/index.html', {'cart': cart, 'form': form, 'items': items, 'voucher': voucher})

@login_required
def orders(request):
    orders = Order.objects.filter(user=request.user)

    return render(request, 'order/orders.html', {'orders': orders})

@login_required
def info(request, order_id):
    order = get_object_or_404(Order, id=order_id)
    items = OrderItem.objects.filter(order=order)
    try:
        voucher = Voucher.objects.get(code=order.voucher_code)
    except Voucher.DoesNotExist:
        voucher = None

    if order.user != request.user:
        raise Http404

    return render(request, 'order/order.html', {'order': order, 'items': items, 'voucher': voucher})

@login_required
def paypal(request):
    try:
        order = request.session['order']
    except KeyError:
        raise Http404
    if order.user != request.user:
        raise Http404
    cart = get_cart(request)
    items = CartItem.objects.filter(cart=cart)

    return render(request, 'order/paypal.html', {'order': order, 'items': items, 'debug': settings.DEBUG})

@login_required
def success(request):
    from order.utils import generate_order_pdf
    from django.template.loader import render_to_string
    from django.core import mail
    from django.core.mail import EmailMultiAlternatives, EmailMessage
    from django.contrib.auth.models import Group
    try:
        order = request.session['order']
    except KeyError:
        raise Http404
    if order.user != request.user:
        raise Http404

    cart = get_cart(request)
    cart = archive_cart(cart)
    order.cart = cart.id
    order.save()
    del request.session['cart']
    voucher = request.session['voucher'] if 'voucher' in request.session else None

    items = ArchivedCartItem.objects.filter(archived_cart=cart)
    for item in items:
        order_item = OrderItem()
        order_item.order = order
        order_item.product = item.product
        order_item.quantity = item.quantity
        order_item.discount_total = item.discount_total
        order_item.gross_total = item.gross_total
        order_item.net_total = item.net_total
        order_item.save()
        #Update sold figure
        item.product.sold += item.quantity
        item.product.save()

    order.status = 1
    order.save()

    pdf = generate_order_pdf(request.get_host(), order, voucher)

    subject = _('EMPOR Order Confirmation')
    html_content = render_to_string('order/email.html', {
        'order': order,
        'items': items,
        'voucher': voucher,
        'STATIC_URL': settings.STATIC_URL,
        'host': request.get_host()
    })
    text_content = render_to_string('order/email.txt', {
        'order': order,
        'voucher': voucher,
        'items': items,
        'host': request.get_host()
    })

    connection = mail.get_connection()
    connection.open()

    message = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [order.user.email])
    message.attach_alternative(html_content, 'text/html')
    filename = order.order_id
    message.attach(filename.encode('utf-8'), pdf, 'application/pdf')

    group = Group.objects.get(name='Service').user_set.only('email')
    subject = 'EMPOR - 新訂單 %s' % order.order_id
    group_email = [ user.email for user in group ]
    notification = EmailMessage(subject, html_content, settings.DEFAULT_FROM_EMAIL, group_email)
    notification.content_subtype = 'html'

    connection.send_messages([message, notification])
    connection.close()

    if 'voucher' in request.session:
        del request.session['voucher']
    return redirect('order-thankyou')

@login_required
def thankyou(request):
    if 'order' in request.session:
        order = request.session['order']
        if 'cart' in request.session:
            del request.session['cart']
    else:
        order = None
    return render(request, 'order/thankyou.html', {'order': order})

@login_required
def voucher_check(request):
    try:
        request.session['voucher']
        return JsonResponse({'success': False})
    except KeyError:
        cart = get_cart(request)
        code = request.POST.get('voucher_code', None)

        if code:
            voucher = Voucher.objects.get(code=code)
            value = voucher.get_value()
            if voucher.active:
                if voucher.percentage:
                    margin = int(cart.net_total * value)
                    cart.discount_total += margin
                    cart.net_total -= margin
                else:
                    cart.net_total -= value
                    cart.discount_total += value
            cart.save()
            request.session['cart'] = cart
            request.session['voucher'] = voucher
            return JsonResponse({
                'success': True,
                'gross': cart.gross_total,
                'discount': cart.discount_total,
                'net': cart.net_total,
                'voucher_name': voucher.name,
                'voucher_value': voucher.get_display_value(),
            })
        return JsonResponse({'success': False})

@login_required
def voucher_reset(request):
    try:
        cart = get_cart(request)
        voucher = request.session['voucher']
        value = voucher.get_value()
        if voucher.percentage:
            total = int(cart.net_total / float(1 - value))
            cart.discount_total -= int(total * float(value))
            cart.net_total = total
        else:
            cart.net_total += value
            cart.discount_total -= value

        cart.save()
        request.session['cart'] = cart
        del request.session['voucher']

        return JsonResponse({
            'success': True,
            'gross': cart.gross_total,
            'discount': cart.discount_total,
            'net': cart.net_total,
        })
    except KeyError:
        return JsonResponse({'success': False })

@login_required
def get_shipping(request, country_id):
    country_id = int(country_id)
    if country_id > 0:
        if request.is_ajax():
            return JsonResponse({'success': True, 'shipping': settings.SHIPPING_OVERSEAS_COST})
        return settings.SHIPPING_OVERSEAS_COST
    else:
        cart = get_cart(request)
        if settings.SHIPPING_FREE_ITEM_COUNT:
            if cart.items.count() > settings.SHIPPING_FREE_ITEM_COUNT:
                if request.is_ajax():
                    return JsonResponse({'success': True, 'shipping': 0})
                return 0
        if settings.SHIPPING_FREE_MINIMUM_PURCHASE:
            if cart.net_total > settings.SHIPPING_FREE_MINIMUM_PURCHASE:
                if request.is_ajax():
                    return JsonResponse({'success': True, 'shipping': 0})
                return 0
        if request.is_ajax():
            return JsonResponse({'success': True, 'shipping': settings.SHIPPING_DEFAULT_COST})
        return settings.SHIPPING_DEFAULT_COST


from scipy import optimize
import numpy as np

def func(x, a0, a1):
    "Coordinate vector `x` should be an array of size two."
    return a0 * x[0]**2 + a1*x[1]**2

x = np.ones(2)
print("Wektor wspolrzednych: ", x)

a0, a1 = (1, 200)
eps = np.sqrt(np.finfo(float).eps)
epsilon = [eps, np.sqrt(200) * eps]
print("Epsilon: ", epsilon)
print("""               f(xk[i] + epsilon[i]) - f(xk[i])
wzor: f'[i] = ---------------------------------
                         epsilon[i]""")
print("Wynik: ", optimize.approx_fprime(x, func, epsilon, a0, a1))


class Solution:
    def countPoints(self, rings: str) -> int:
        mp = collections.defaultdict(set)
        n = len(rings)

        for i in range(0, n, 2):
            mp[rings[i + 1]].add(rings[i])

        res = 0

        for k, v in mp.items():
            if len(v) == 3:
                res += 1

        return res


import matplotlib.cm as cm
import numpy as np
import tensorflow as tf

try:
    from tensorflow.keras.utils import array_to_img
    from tensorflow.keras.utils import img_to_array
except ImportError:
    from tensorflow.keras.preprocessing.image import array_to_img
    from tensorflow.keras.preprocessing.image import img_to_array

def process_image(img, size=(224, 224)):
    img_array = tf.keras.applications.imagenet_utils.preprocess_input(
        img, mode="torch"
    )
    img_array = tf.image.resize(img_array, size,)[
        None,
    ]
    return img_array

def get_gradcam_model(model):
    inp = tf.keras.Input(shape=(224, 224, 3))
    feats = model.forward_features(inp)
    preds = model.forward_head(feats)
    return tf.keras.models.Model(inp, [preds, feats])

def get_gradcam_prediction(
    img,
    grad_model,
    process=True,
    decode=True,
    pred_index=None,
    cmap="jet",
    alpha=0.4,
):
    """Grad-CAM for a single image

    Args:
        img (np.ndarray): raw image without batch_shape, e.g. (224, 224, 3)
        grad_model (tf.keras.Model): model with feature map and prediction
        process (bool, optional): imagenet pre-processing. Defaults to True.
        pred_index (int, optional): for particular calss. Defaults to None.
        cmap (str, optional): colormap. Defaults to 'jet'.
        alpha (float, optional): opacity. Defaults to 0.4.

    Returns:
        preds_decode: top5 predictions
        heatmap: gradcam heatmap
    """
    # process image for inference
    if process:
        img_array = process_image(img)
    else:
        img_array = tf.convert_to_tensor(img)[
            None,
        ]
        if img.min() != img.max():
            img = (img - img.min()) / (img.max() - img.min())
            img = np.uint8(img * 255.0)
    # get prediction
    with tf.GradientTape(persistent=True) as tape:
        preds, feats = grad_model(img_array)
        if pred_index is None:
            pred_index = tf.argmax(preds[0])
        class_channel = preds[:, pred_index]
    # compute heatmap
    grads = tape.gradient(class_channel, feats)
    pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
    feats = feats[0]
    heatmap = feats @ pooled_grads[..., tf.newaxis]
    heatmap = tf.squeeze(heatmap)
    heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
    heatmap = heatmap.numpy()
    heatmap = np.uint8(255 * heatmap)
    # colorize heatmap
    cmap = cm.get_cmap(cmap)
    colors = cmap(np.arange(256))[:, :3]
    heatmap = colors[heatmap]
    heatmap = array_to_img(heatmap)
    heatmap = heatmap.resize((img.shape[1], img.shape[0]))
    heatmap = img_to_array(heatmap)
    overlay = img + heatmap * alpha
    overlay = array_to_img(overlay)
    # decode prediction
    preds_decode = (
        tf.keras.applications.imagenet_utils.decode_predictions(preds.numpy())[
            0
        ]
        if decode
        else preds
    )
    return preds_decode, overlay


from torch.nn import Module
import torch
from os import makedirs
from os.path import exists, join
from torch.nn import Conv2d, Linear, LSTM
from typing import Union
from shutil import rmtree
import numpy as np
from time import time
import pickle
import pandas as pd

class LatentRepresentationCollector:

    def __init__(self, model: Module, savepath: str, save_instantly: bool = True, downsampling: int = None):
        self.savepath = savepath
        self.downsampling = downsampling
        if exists(savepath):
            print('Found previous extraction in folder, removing it...')
            rmtree(savepath)
        makedirs(self.savepath)
        self.layers = self.get_layers_recursive(model)
        for name, layer in self.layers.items():
            if isinstance(layer, Conv2d) or isinstance(layer, Linear) \
                                  or isinstance(layer, LSTM):
                self._register_hooks(layer=layer,
                                     layer_name=name,
                                     interval=1)
        self.save_instantly = save_instantly

        self.logs = {
            'train': {},
            'eval': {}
        }
        self.record = True

    def _record_stat(self, activations_batch: torch.Tensor, layer: Module, training_state: str):
        if activations_batch.dim() == 4:  # conv layer (B x C x H x W)
            if self.downsampling is not None:
                activations_batch = torch.nn.functional.interpolate(activations_batch, self.downsampling)
            activations_batch = activations_batch.view(activations_batch.size(0), -1)
        batch = activations_batch.cpu().numpy()
        if not self.save_instantly:
            if layer.name not in self.logs[training_state]:
                self.logs[training_state][layer.name] = batch
            else:
                self.logs[training_state][layer.name] = np.vstack((self.logs[training_state][layer.name], batch))
        else:
            savepath = self.savepath+'/'+training_state+'-'+layer.name+'.p'
            if not exists(savepath):
                self.logs[training_state][layer.name] = open(savepath, 'wb')
            pickle.dump(batch, file=self.logs[training_state][layer.name])
            #df.to_csv(savepath, mode='a' if not exists(savepath) else 'w+', header=not exists(savepath))
            #if exists(savepath+'.npy'):
            #    data = np.load(savepath+'.npy')
            #    new_data = np.vstack((data, batch))
            #else:
            #    print('Did not find prexisting file, moving on...')
            #    new_data = batch
            #np.save(savepath, new_data)

    def _register_hooks(self, layer: Module, layer_name: str, interval: int) -> None:
        layer.name = layer_name
        def record_layer_history(layer: torch.nn.Module, input, output):
            """Hook to register in `layer` module."""

            if not self.record:
                return

            # Increment step counter
            layer.forward_iter += 1

            training_state = 'train' if layer.training else 'eval'
            activations_batch = output.data
            self._record_stat(activations_batch, layer, training_state)

        layer.register_forward_hook(record_layer_history)

    def get_layer_from_submodule(self, submodule: torch.nn.Module,
                                 layers: dict, name_prefix: str = ''):
        if len(submodule._modules) > 0:
            for idx, (name, subsubmodule) in \
                              enumerate(submodule._modules.items()):
                new_prefix = name if name_prefix == '' else name_prefix + \
                                                            '-' + name
                self.get_layer_from_submodule(subsubmodule, layers, new_prefix)
            return layers
        else:
            layer_name = name_prefix
            layer_type = layer_name
            if not isinstance(submodule, Conv2d) and not \
                   isinstance(submodule, Linear) and not \
                   isinstance(submodule, LSTM):
                print(f"Skipping {layer_type}")
                return layers
            layers[layer_name] = submodule
            print('added layer {}'.format(layer_name))
            return layers

    def get_layers_recursive(self, modules: Union[list, torch.nn.Module]):
        layers = {}
        if not isinstance(modules, list) and not hasattr(
                modules, 'out_features'):
            # is a model with layers
            # check if submodule
            submodules = modules._modules  # OrderedDict
            layers = self.get_layer_from_submodule(modules, layers, '')
        else:
            for module in modules:
                layers = self.get_layer_from_submodule(module, layers, '')
        return layers

    def save(self):
        if not exists(self.savepath):
            makedirs(self.savepath)
        for mode, logs in self.logs.items():
            for layer_name, data in self.logs[mode].items():
                if isinstance(data, np.ndarray):
                    with open(self.savepath+'/'+mode+'-'+layer_name+'.p', 'wb') as p:
                        pickle.dump(data, p)
                else:
                    data.close()

def extract_from_dataset(logger: LatentRepresentationCollector, train: bool, model: Module, dataset, device: str):
    mode = 'train' if train else 'eval'
    correct, total = 0, 0
    old_time = time()
    with torch.no_grad():
        for batch, data in enumerate(dataset):
            if batch % 1 == 0 and batch != 0:
                print(batch, 'of', len(dataset), 'processing time', time() - old_time,' acc:', correct / total)
                old_time = time()
            inputs, labels = data
            inputs, labels = inputs.to(device), labels.to(device)
            outputs = model(inputs)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels.long()).sum().item()
            if 'labels' not in logger.logs[mode]:
                logger.logs[mode]['labels'] = labels.cpu().numpy()
            else:
                logger.logs[mode]['labels'] = np.hstack((logger.logs[mode]['labels'], labels.cpu().numpy()))
    print('accuracy:', correct/total)


import sys

def per(start, m, s, li):
    if start >= m:
        print(" ".join(list(s)))
        return
    for i in range(len(li)):
        if int(s[start-1]) <= li[i]:
            per(start + 1, m, s + str(li[i]), li)

def main():
    n, m = map(int, sys.stdin.readline().split())
    li = list(range(1, n + 1))
    for i in range(len(li)):
        per(1, m, str(li[i]), li)

main()


from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient, APITestCase

class PingTests(APITestCase):
    def setUp(self):
        super().setUp()
        self.client = APIClient()

    def test_ping_pong(self):
        request_url = reverse('server:ping')
        response = self.client.get(request_url)
        self.assertEqual(response.data, 'pong')
        self.assertEqual(response.status_code, status.HTTP_200_OK)


# this program just prints colors with hex code and color numbers

from dict import NUMBER_RGB, NUMBER_HEX, NUMBER_NAME

# color reset
reset = "\033[0m"

# print all colors
for i in range(0,256):
    R,G,B = NUMBER_RGB[i]
    background = f"\033[48;2;{R};{G};{B}m"
    hexcode = NUMBER_HEX[i]
    print(background+hexcode+reset, end="")
    if (i+1) % 16 == 0:
        print()

print()

# print 16 system colors
for i in range(2):
    for j in range(0,16):
        R,G,B = NUMBER_RGB[j]
        fgcolor = f"\033[38;2;{R};{G};{B}m"
        bgcolor = f"\033[48;2;{R};{G};{B}m"
        if not i:
            print(str(j).center(5)+reset,end="")
        else:
            print(bgcolor+"     "+reset,end="")
    print()
print()

# print color cubes
start = 16
max_chunk = 3
line_of_start = start+6
for i in range(2):
    count = 0
    for j in range(6*6*3):
        R,G,B = NUMBER_RGB[start+count]
        bgcolor = f"\033[48;2;{R};{G};{B}m"
        #if start+count >= 226:
        #    bgcolor = bgcolor+f"\033[38;2;150;150;150m"
        print(bgcolor+str(start+count).center(5)+reset, end="")
        count += 1
        if count == 6:
            print("   ",end="")
            start = start + 6*6
            count = 0
            max_chunk -= 1

        if max_chunk == 0:
            print()
            max_chunk = 3
            start = line_of_start
            line_of_start += 6
    start = 124
    line_of_start = start+6
    print()

# print 24 shades of gray
for i in range(2):
    for j in range(232,256):
        R,G,B = NUMBER_RGB[j]
        fgcolor = f"\033[38;2;{R};{G};{B}m"
        bgcolor = f"\033[48;2;{R};{G};{B}m"
        if not i:
            print(str(j).center(5)+reset,end="")
        else:
            print(bgcolor+"     "+reset,end="")
    print()


import os
import yaml
import functools
from collections import namedtuple
from dotenv import load_dotenv, find_dotenv
from jinja2 import Template
from munch import Munch

APP_ROOT = os.path.abspath(os.getcwd())
CONFIG_PATH = os.path.join(APP_ROOT, 'config')

def get_env_value(key, default_value=''):
    return os.environ.get(key, default_value)

def singleton(cls):
    """ Use class as singleton. """

    cls.__new_original__ = cls.__new__

    @functools.wraps(cls.__new__)
    def singleton_new(cls, *args, **kw):
        it = cls.__dict__.get('__it__')
        if it is not None:
            return it

        cls.__it__ = it = cls.__new_original__(cls, *args, **kw)
        it.__init_original__(*args, **kw)
        return it

    cls.__new__ = singleton_new
    cls.__init_original__ = cls.__init__
    cls.__init__ = object.__init__

    return cls

@singleton
class Config(object):
    _config = None

    def __new__(cls):
        if cls._config is None:
            cls._config = Config.load()
        return object.__new__(cls)

    @staticmethod
    def load():
        load_dotenv(find_dotenv())
        env = os.getenv('ENV') or 'development'
        _config = Munch({
            'app_root': APP_ROOT,
            'env': env,
            'DEBUG': True if env is 'development' else False
        })
        _append = {}
        default_file = os.path.join(CONFIG_PATH, 'default.yaml')
        if os.path.isfile(default_file):
            with open(default_file, 'r') as stream:
                template = stream.read()
                template = Template(template)
                content = template.render(home_path=APP_ROOT)
                default = yaml.load(content)
                _append.update(default)

        env_file = os.path.join(CONFIG_PATH, env + '.yaml')
        if os.path.isfile(env_file):
            with open(env_file, 'r') as stream:
                template = stream.read()
                template = Template(template)
                content = template.render(home_path=APP_ROOT)
                _append.update(yaml.load(content))

        _config.update(_append)
        return _config

    def export(self):
        return self._config

config = Config().export()


import math

def create_buckets(base):
    buckets = []
    for i in range(base):
        buckets.append([])

        return buckets

def sort(items, base):
    tmp = -1
    j = 0
    m = int(math.log(items[0], base)) + 1
    while j < m:
        buckets = create_buckets(base)
        for number in items:
            tmp = number / math.pow(base, j)
            digit = int(tmp % base)
            buckets[digit].append(number)
        a = 0
        #print (buckets)
        for bucket in buckets:
            for i in bucket:
                items[a] = i
                a += 1
        print(items)

        j = j + 1

A = [223,456,123,789,113]

def radix10(items):
    sort(items,10)

print(A)
radix10(A)


from dcore._dispatcher import mpi
from .worker_base import SumkDFTWorkerBase, setup_sk

class SumkDFTWorkerDOS(SumkDFTWorkerBase):
    """For computing DOS"""
    def __init__(self, model_hdf5_file, input_file, output_file) -> None:
        super().__init__(model_hdf5_file, input_file, output_file)

    def run(self):
        # Compute dos
        from dcore.sumkdft_post import SumkDFTDCorePost
        sk = SumkDFTDCorePost(hdf_file=self.model_hdf5_file, use_dft_blocks=False, h_field=0.0)
        setup_sk(sk, 'w', self.params)
        results = {}
        results['dos'], results['dosproj'], results['dosproj_orb'] = \
            sk.dos_wannier_basis(broadening=self.params['broadening'],
                             mesh=self.params['mesh'],
                             with_Sigma=True, with_dc=self.params['with_dc'], save_to_file=False)

        if mpi.is_master_node():
            self.save_result(results)


import argparse
import time

import funcx
from funcx.sdk.client import FuncXClient
from funcx.serialize import FuncXSerializer

fxs = FuncXSerializer()

# funcx.set_stream_logger()

def double(x):
    return x * 2

def test(fxc, ep_id, task_count=10):

    fn_uuid = fxc.register_function(double, description="Yadu double")
    print("FN_UUID : ", fn_uuid)

    start = time.time()
    task_ids = fxc.map_run(
        list(range(task_count)), endpoint_id=ep_id, function_id=fn_uuid
    )
    delta = time.time() - start
    print(f"Time to launch {task_count} tasks: {delta:8.3f} s")
    print(f"Got {len(task_ids)} tasks_ids ")

    for _i in range(3):
        x = fxc.get_batch_result(task_ids)
        complete_count = sum(
            [1 for t in task_ids if t in x and x[t].get("pending", False)]
        )
        print(f"Batch status : {complete_count}/{len(task_ids)} complete")
        if complete_count == len(task_ids):
            break
        time.sleep(2)

    delta = time.time() - start
    print(f"Time to complete {task_count} tasks: {delta:8.3f} s")
    print(f"Throughput : {task_count / delta:8.3f} Tasks/s")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-e", "--endpoint", required=True)
    parser.add_argument("-c", "--count", default="10")
    args = parser.parse_args()

    print("FuncX version : ", funcx.__version__)
    fxc = FuncXClient(funcx_service_address="https://dev.funcx.org/api/v1")
    test(fxc, args.endpoint, task_count=int(args.count))


#!/usr/bin/env python3

import connexion

from swagger_server import encoder
from swagger_server.controllers import product_controller

def main():
    app = connexion.App(__name__, specification_dir='./swagger/')
    app.app.json_encoder = encoder.JSONEncoder
    app.add_api('swagger.yaml', arguments={'title': 'Data Lake Stream Data Service API'})
    product_controller.init_stream_data()
    app.run(port=8087)

if __name__ == '__main__':
    main()


# Algo:
# 1. Sort list from largest to smallest
# 2. Sum all digits together
# 3. Find the sum's remainder with 3
# 4. If there's a remainder, see if a digit can be taken out to make the digits divisible by 3 (pass to helper func)
# 5. If no remainder, pass the current set of digits to be made into a large number
# 6. If there's no way to make it divisible by 3, then return 0
#
# Algo to make digits divisible by 3:
# 1. Create a list of digits with remainder of 1 with 3 [1, 4, 7]
# 2. Create a list of digits with remainder of 2 with 3 [2, 5, 8]
# 3. With remainder, look through appropriate list and try to remove 1 digit
# 4. If not in first list, look through second list and try to remove 2 digits
# 5. Return resulting list

def solution(l):
    l_copy = l[:]
    l_copy.sort(reverse=True)
    l_sum = sum(l_copy)
    remainder = l_sum % 3
    if remainder:
        l_copy = make_divisible(l_copy, remainder)
    if not l_copy:
        return 0
    large_num_str = str(l_copy).strip("[]").replace(", ", "")
    large_num = int(large_num_str)
    return large_num

def make_divisible(l, remainder):
    rem_1 = [1, 4, 7]
    rem_2 = [2, 5, 8]

    first_list = rem_1
    second_list = rem_2

    if remainder == 2:
        first_list = rem_2
        second_list = rem_1

    for num in first_list:
        if num in l:
            l.remove(num)
            return l

    to_remove = []
    for num in second_list:
        if num in l:
            num_count = l.count(num)
            for _ in range(num_count):
                to_remove.append(num)
        if len(to_remove) >= 2:
            for remove_num in to_remove[:2]:
                l.remove(remove_num)
            return l
    return False


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

class Solution:
    def largestValue(self, root):
        if not root:
            return []
        res = []
        queue = [root]
        while queue:
            child = []
            maxValue = float("-inf")
            for node in queue:
                if maxValue < node.val:
                    maxValue = node.val
                if node.left: child.append(node.left)
                if node.right: child.append(node.right)
            res.append(maxValue)
            queue = child
        return res

515.在每个树行中找最大值.py


from Memcache import Memcache
from Parser import Parser
import Utils as utils
import json
import time
from random import randint

class Fetcher:
    memcache = Memcache()
    parser_online = Parser()
    parser_highscores = Parser()

    def cache_online_players(self, world):
        op = self.parser_online.get_online_players(str(world))
        v = json.dumps(op)
        # cache.put(cache="online_players", key=world, value=v)
        self.memcache.cache.set(world, v)
        print('Cached online players for ' + str(world))

    def cache_highscores(self, world, profession):
        hs = self.parser_highscores.get_highscores(world, profession)
        v = json.dumps(hs)
        # cache.put(cache="highscores", key=world + '_' + profession, value=v)
        self.memcache.cache.set('highscores_' + world + '_' + profession, v)
        print('Cached higscores for  ' + str(world) + '_' + profession)

    def fetch_online_players(self, interval):
        while True:
            for world in utils.worlds:
                self.cache_online_players(world)
            time.sleep(interval)

    def fetch_highscores(self, interval):
        worlds_arr = utils.worlds
        professions_arr = utils.professions
        if randint(0, 1) == 1:
            worlds_arr = utils.worlds[::-1]
            professions_arr = utils.professions[::-1]

        while True:
            for world in worlds_arr:
                for profession in professions_arr:
                    time.sleep(2)
                    self.cache_highscores(world, profession)
            time.sleep(interval)


import numpy as np

from datetime import datetime, timedelta
from typing import Union
from utils.log_maker import write_log

def get_meas_info(meas):
    print("measurement_dict")
    for key, values_df in meas.measurement_dict.items():
        print(key)
        min_ts = values_df.timestamp_ms.min()
        max_ts = values_df.timestamp_ms.max()
        print("min ts: {}, {}".format(to_str_timestamp(min_ts), min_ts))
        print("max ts: {}, {}".format(to_str_timestamp(max_ts), max_ts))
        print("length: {}\n".format(len(values_df)))

    print("diff_dict")
    for key in meas.diff_dict.keys():
        print(key)
        meas.calculate_diff(key)
        values = meas.diff_dict[key]
        print("length: {}\n".format(len(values)))

def to_int_timestamp(timestamp_str: str) -> int:
    """ timestamp in microseconds
        first case: 2023-04-28T14:47:00Z
        second case: 2023-04-28T14:47:12.123Z
    """
    if timestamp_str.find(".") == -1:
        return int(datetime.strptime(timestamp_str, '%Y-%m-%dT%H:%M:%SZ').timestamp() * 1000)
    else:
        return int(datetime.strptime(timestamp_str, '%Y-%m-%dT%H:%M:%S.%fZ').timestamp() * 1000)

def to_str_timestamp(timestamp: Union[int, datetime]):
    if isinstance(timestamp, (int, np.int64)):
        dt_object = datetime.fromtimestamp(timestamp / 1000)
    else:
        dt_object = timestamp
    return dt_object.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"

def from_int_to_datetime(timestamp: Union[int, None]) -> Union[datetime, None]:
    if timestamp is None:
        return None
    else:
        return datetime.fromtimestamp(timestamp / 1000)

def hour_to_millisec(hour: Union[int, float]) -> int:
    return int(hour * 60 * 60 * 1000)

def min_to_millisec(minute: Union[int, float]) -> int:
    return int(minute * 60 * 1000)

def get_length_from_timestamps(start_ts: int, end_ts: int, full_format: bool = False) -> timedelta:
    start = datetime.fromtimestamp(start_ts / 1000)
    end = datetime.fromtimestamp(end_ts / 1000)
    length = end - start

    if not full_format:
        mm, ss = divmod(length.total_seconds(), 60)
        hh, mm = divmod(mm, 60)
        length = "{}:{}".format(int(hh), int(mm))
    return length

def get_data_info(data_dict: dict, prefix: str = ""):
    """ columns: limb, side, timestamp, type, x, y, z, timestamp_ms, keys_tuple"""
    measurement_ids = sorted(data_dict.keys())
    # print(colored("\n{} measurement ids: {}".format(prefix, measurement_ids), color="blue"))
    write_log("main_loop.txt", "{} measurement ids: {}".format(prefix, measurement_ids),
              title="GetDataInfo", print_out=True, color="blue", add_date=True)

    for measurement_id in measurement_ids:
        # print("\nmeasurement {}".format(measurement_id))
        write_log("main_loop.txt", "measurement {}".format(measurement_id),
                  title="GetDataInfo", print_out=True, color="blue", add_date=True)
        meas_df = data_dict[measurement_id]

        keys_tuples = meas_df.keys_tuple.unique()
        print("keys_tuples:")
        for keys_tuple in keys_tuples:
            key_df = meas_df[meas_df.keys_tuple == keys_tuple]

            timestamp_ms_min = key_df.timestamp_ms.min()
            timestamp_ms_max = key_df.timestamp_ms.max()
            first_timestamp = datetime.fromtimestamp(timestamp_ms_min / 1000)  # from micro sec to milli sec
            last_timestamp = datetime.fromtimestamp(timestamp_ms_max / 1000)  # from micro sec to milli sec
            delta = last_timestamp - first_timestamp
            # print("{}: from {} ({}) to {} ({}), delta (min): {:.2f}".format(keys_tuple, key_df.timestamp.min(),
            #                                                                 timestamp_ms_min,
            #                                                                 key_df.timestamp.max(),
            #                                                                 timestamp_ms_max,
            #                                                                 delta.total_seconds() / 60))
            write_log("main_loop.txt", "{}: from {} ({}) to {} ({}), delta (min): {:.2f}".format(keys_tuple, key_df.timestamp.min(),
                                                                                timestamp_ms_min,
                                                                                key_df.timestamp.max(),
                                                                                timestamp_ms_max,
                                                                                delta.total_seconds() / 60),
                      title="GetDataInfo", print_out=True, color="blue", add_date=True)


import json
import boto3
import os

sns = boto3.client('sns')
# queue_url = 'https://sqs.ap-southeast-1.amazonaws.com/830103335377/OrderQueue'
# topic_arn = 'arn:aws:sns:ap-southeast-1:830103335377:OrderTopic'
topic_arn = os.environ['ORDER_TOPIC_ARN'] # This is the environment variable we set in the Lambda function
queue_url = os.environ['ORDER_QUEUE_URL'] # This is the environment variable we set in the Lambda function
# queue = boto3.client('sqs').get_queue_url(QueueName=queue_url)['QueueUrl']
sqs = boto3.resource('sqs').Queue(queue_url)

def lambda_handler(event, context):
    for message in sqs.receive_messages():
        print(message.body)

        message_body = json.loads(message.body)
        subject = message_body['subject']
        message = message_body['message']

        response = sns.publish(
            TopicArn=topic_arn,
            Subject=subject,
            Message=message
        )

        message.delete()

    return {
        'statusCode': 200,
        'body': json.dumps({
            'message': 'Orders processed'
        })
    }


'''
lexicon = [chair, table, process, television]

Some random sentence to process

[0     0      0       0    1]

Our lexicon will be consisting out of every word in the entire dataset,
that way we can build an array of 0s & 1s like above to have uniform datapoints
and we dont need to worry about difference in sentence length etc.
'''

import nltk
from nltk.tokenize import word_tokenize
#Information on natural language processing on sentdex website
from nltk.stem import WordNetLemmatizer
import numpy as np
import random
import pickle
from collections import Counter

lemmatizer = WordNetLemmatizer
hm_lines = 1000000

#Read data and stuff into lexicon
def create_lexicon(pos, neg):
    lexicon = []
    with open(pos, 'r') as f:
        contents = f.readlines()
        for l in contents[:hm_lines]:
            all_words = word_tokenize(l.lower())
            lexicon += list(all_words)

    with open(neg, 'r') as f:
        contents = f.readlines()
        for l in contents[:hm_lines]:
            all_words = word_tokenize(l.lower())
            lexicon += list(all_words)

    #lexicon = [lemmatizer.lemmatize(i,pos[0].lower()) for i,pos in nltk.pos_tag(lexicon)]
    #lexicon = [lemmatizer.lemmatize(i,'v') for i in lexicon]
    w_counts = Counter(lexicon)
    #w_counts will be like {'the: '3453, 'and': 20344}
    l2 = []
    #Eliminate words that appear between 50 and 1000 times (etc. 'and', 'or', ...)
    for w in w_counts:
        if 1000 > w_counts[w] > 50:
            l2.append(w)
    print(len(l2))
    return l2

def sample_handling(sample, lexicon, classification):
    featureset = []
    '''
    featureset = [
    [[indexes of features],[pos neg]],
    [[ 0 1 1 1 0 0  0],[0 1]],
    ...
    ]
    '''
    with open(sample, 'r') as f:
        contents = f.readlines()
        for l in contents[:hm_lines]:
            current_words = word_tokenize(l.lower())
            #current_words = [lemmatizer.lemmatize(i,pos[0].lower()) for i,pos in  nltk.pos_tag(current_words)]
            #current_words = [lemmatizer.lemmatize(i,'v') for i in current_words]
            #Init features on zero
            features = np.zeros(len(lexicon))
            for word in current_words:
                if word.lower() in lexicon:
                    indexd_value = lexicon.index(word.lower())
                    features[indexd_value] += 1
            features = list(features)
            featureset.append([features, classification])
    return featureset

def create_feature_sets_and_labels(pos, neg, test_size=0.1):
    lexicon = create_lexicon(pos, neg)
    features = []
    features += sample_handling('pos.txt', lexicon,[1,0])
    features += sample_handling('neg.txt', lexicon,[0,1])
    #Shuffling really important, else the neural network might really havy bias
    random.shuffle(features)

    features = np.array(features)

    testing_size = int(test_size*len(features))
    #:,0 --> all 0th elements in a list of lists1
    #Because first element in features is list of dfeatures we get the list of features
    train_x = list(features[:,0][:-testing_size])
    train_y = list(features[:,1][:-testing_size])

    test_x = list(features[:,0][-testing_size:])
    test_y = list(features[:,1][-testing_size:])

    return train_x, train_y, test_x, test_y

if __name__ == '__main__':
    train_x,train_y,test_x,test_y = create_feature_sets_and_labels('pos.txt', 'neg.txt')
    with open('sentiment_set.pickle','wb') as f:
        pickle.dump([train_x,train_y,test_x,test_y], f)

    #


import requests
import config
import pandas as pd
import re

from logging import getLogger
from pyquery import PyQuery as pq
from datetime import datetime, timedelta
import matplotlib.pyplot as plt

logger = getLogger('main')

def get_html_eps(ticker: str) -> str:
    try:
        if isinstance(ticker, str):
            data = requests.get(f'{config.EPS_URL}{ticker.upper()}/eps', headers=config.HEADER, timeout=config.TIMEOUT)
            if data.status_code == 200:
                return data.text
            elif data.status_code == 404:
                logger.warning('No such ticker')
            else:
                logger.warning('Something wrong')
    except Exception as e:
        logger.warning(e)

def get_eps_data(content: str) -> dict:
    html = pq(content)
    table = html('table.histDataTable')
    td_date = table('td.col1')
    td_value = table('td.col2')
    dates = [str(pd.to_datetime(td.text).date()) for td in td_date]
    values = [float(td.text) for td in td_value]
    eps_data = dict(zip(dates, values))
    return eps_data

def get_quarter_eps_values(eps_data: dict) -> tuple:
    last_quarter = list(eps_data.keys())[0]
    last_quarter_date = datetime.strptime(last_quarter, '%Y-%m-%d').date()
    years_ago_quarter_date = last_quarter_date - timedelta(days=365)
    last_quarter_value = eps_data[last_quarter]
    years_ago_quarter_value = eps_data[str(years_ago_quarter_date)]
    return last_quarter_value, years_ago_quarter_value

def get_percantage_ratio(*args) -> str:
    val1, val2 = args
    values = list(args)
    max_val = max(values)
    values.remove(max_val)
    if val1 > val2 > 0:
        ratio = ((max_val - values[0]) / val2) * 100
    elif max_val < 0:
        ratio = ((max_val - values[0]) / val2) * 100
    else:
        ratio = - ((max_val - values[0]) / val2) * 100
    # ratio = ((val1 - val2) / val2) * 100
    return f'{ratio:.1f}'

def form_chart_quarter_eps(ticker: str, eps_data: dict):
    """Forming chart from last year quarter eps values"""
    last_year = list(eps_data.keys())[0].split('-')[0]
    data_frame = {key: value for (key, value) in eps_data.items() if last_year in key}
    fig1, ax1 = plt.subplots()
    plt.xlabel('Quarter')
    plt.ylabel('EPS')
    plt.title(f'Quarterly EPS indicator - {ticker.upper()}')
    ax1.bar(*zip(*data_frame.items()))
    fig1.savefig(f'{config.IMG_DIR}/{ticker}_q_eps')
    plt.close(fig1)

def get_annual_eps_value(eps_data: dict):
    """calculate annual EPS values"""
    years = list(set([int(k.split('-')[0]) for (k, v) in eps_data.items()]))
    max_year = max(years)
    three_years_ago = max_year - 3
    filter_years = sorted([str(x) for x in years if max_year >= x >= three_years_ago], reverse=True)
    values = list()
    for year in filter_years:
        values.append(round(sum([v for (k, v) in eps_data.items() if year in k]), 2))
    return dict(zip(filter_years, values))

def get_annual_percentage_ratio(**kwargs):
    keys = list(kwargs.keys())[0:-1]
    values = list(kwargs.values())
    pair_values = tuple(zip(values[::1], values[1::1]))
    percentage_ratio = [f'{get_percantage_ratio(*v)}%' for v in pair_values]
    return dict(zip(keys, percentage_ratio))

def form_chart_annual_eps(ticker: str, eps_data_annual: dict):
    """Forming chart sum of annual eps values"""
    fig2, ax2 = plt.subplots()
    plt.xlabel('Years')
    plt.ylabel('EPS')
    plt.title(f'Annually EPS indicator - {ticker.upper()}')
    ax2.bar(*zip(*eps_data_annual.items()))
    fig2.savefig(f'{config.IMG_DIR}/{ticker}_a_eps')
    plt.close(fig2)

def check_ticker_nyse(ticker: str) -> dict:
    try:
        json = {"normalizedTicker": ticker.upper()}
        request = requests.post(config.NYSE_TICKER_URL, json=json, timeout=config.TIMEOUT)
        if request.status_code == 200:
            return request.json()
    except Exception as e:
        logger.warning(e)

def check_ticker_nasdaq(ticker: str) -> dict:
    try:
        request = requests.get(f'{config.NASDAQ_TICKER_URL}{ticker.upper()}/info?assetclass=stocks',
                               timeout=config.TIMEOUT)
        if request.status_code == 200 and request.json().get('status').get('rCode') == 200:
            return request.json()
    except Exception as e:
        logger.warning(e)

def get_url_param_for_roe(ticker: str) -> str:
    try:
        request = requests.get(config.ROE_TICKER_URL, headers=config.HEADER, timeout=config.TIMEOUT)
        if request.status_code == 200:
            param = re.search(r'{0}\\/\w+'.format(ticker), request.text).group(0)
            return re.sub(r'\\', '', param)
    except Exception as e:
        logger.warning(e)

def get_html_roe(param: str):
    try:
        request = requests.get(f'{config.ROE_URL}{param}/roe')
        if request.status_code == 200:
            return request.text
    except Exception as e:
        logger.warning(e)

def get_roe_data(content: str):
    html = pq(content)
    table = html('table[class^="table"]').eq(0)
    tbody= table('tbody')
    td_date = tbody('td:nth-of-type(1)')
    td_value = tbody('td:nth-of-type(4)')
    dates = [td.text for td in td_date]
    values = [td.text for td in td_value]
    return dict(zip(dates, values))

def get_roe_three_year_data(roe_data: dict):
    last_quarter = list(roe_data.keys())[0]
    last_quarter_date = datetime.strptime(last_quarter, '%Y-%m-%d').date()
    dates = [str(last_quarter_date)]
    for i in range(3):
        years_ago_quarter_date = last_quarter_date - timedelta(days=365)
        last_quarter_date = years_ago_quarter_date
        dates.append(str(years_ago_quarter_date))
    data = {k: v for (k, v) in roe_data.items() if k in dates}
    data = {k.split('-')[0]: float(re.sub(r'%', '', v)) for (k, v) in data.items()}
    return data

def form_chart_annual_roe(ticker: str, roe_data: dict):
    """Forming chart annually roe percentage data"""
    fig3, ax3 = plt.subplots()
    plt.xlabel('Years')
    plt.ylabel('ROE percentage (%)')
    plt.title('Annually ROE indicator')
    ax3.bar(*zip(*roe_data.items()))
    fig3.savefig(f'{config.IMG_DIR}/{ticker}_a_roe')
    plt.close(fig3)

if __name__ == '__main__':
    ticker = "ATVI"
    info = check_ticker_nasdaq(ticker)
    print(info)
    # html = get_html_eps(ticker)
    # eps = get_eps_data(html)
    # eps_annual = get_annual_eps_value(eps)
    # per = get_annual_percentage_ratio(**eps_annual)
    # form_chart_annual_eps(ticker, eps_annual)
    # form_chart_quarter_eps(ticker, eps)
    # print(eps_annual)
    # print(per)
    # form_chart_annual_eps(ticker, eps_annual)
    # form_chart_quarter_eps(ticker, eps)

    # param = get_url_param_for_roe(ticker)
    # html = get_html_roe(param)
    # roe = get_roe_data(html)
    # data = get_roe_three_year_data(roe)
    # form_chart_annual_roe(ticker, data)


import sys

def read(filename):
    coords = []
    max_x = max_y = None

    with open(filename) as f:
        for line in f:
            comma = line.find(',')
            x, y = int(line[:comma]), int(line[comma+1:])
            max_x = x if (not max_x or x > max_x) else max_x
            max_y = y if (not max_y or y > max_y) else max_y
            coords.append(((len(coords) + 1), x, y))

    return (coords, max_x + 1, max_y + 1)

def manhattan_distance(p, q):
    return abs(p[0] - q[0]) + abs(p[1] - q[1])

def manhattan_distance_within(curr_coord, fixed_coords, distance):
    sum_d = 0

    for (_, fixed_x, fixed_y) in fixed_coords:
        d = manhattan_distance(curr_coord, (fixed_x, fixed_y))
        sum_d += d

    if (sum_d < distance):
        return True

    return False

def run_algo(coords, max_x, max_y, distance):
    region = 0

    y = 0
    while y < max_y:
        x = 0
        while x < max_x:
            if True:
                within = manhattan_distance_within((x, y), coords, distance)
                if within:
                    region += 1
            x += 1
        y += 1

    print(region)

if __name__ == '__main__':
    res = read(sys.argv[1])
    run_algo(res[0], res[1], res[2], int(sys.argv[2]))