import sys
import time
import threading
from shutil import get_terminal_size
from warnings import warn
from os import name as os_name

class ProgressIndicator:
    """! Print an animation to show that something is happening.

    Animations are rendered in a new thread, which is set up as deamon so that
    it stops on main thread exit.
    The methods Dots, Spinner and Percentage provide some default animations.
    Start the animation by either using the `start`/`stop` functions or a
    'with as' block, e.g
    ```
    with ProgressIndicator.Spinner():
        do_something...
    ```
    or
    ```
    with ProgressIndicator.Percentage() as indicator:
        for i in range(n):
            do_something...
            indicator.set_progress((i+1)/n)
    ```
    """

    _first_init = True
    _disabled = False

    def __init__(self, message, animator, delay, auto_adjust=False):
        """! Create a ProgressIndicator.

        @param message String. Shown in front of the animation without
            seperator. If it would not fit in a single line with the
            animation, it will be printed once in a new line above the
            animation instead. Must not contain `\\r` or `\\n`.
        @param animator Generator expression. The expression must loop, i.e.
            it should never return StopIteration. `next(animator)` must be a
            string of length < `os.get_terminal_size().columns`. The length
            should be constant, otherwise animations may leave artifacts (this
            can be worked around by prepending `"\\033[K"` to each string).
        @param delay Positive real number. Sets delay in seconds between
            drawing animation frames.
        @param auto_adjust [Default: False] Bool. Specify whether each frame
            of the animation should be forced to fit in a single line. Can be
            usefull for long, line filling animations.
        """
        assert (delay > 0)
        self._message = message
        self._animator = animator
        self._delay = delay
        self._thread = None
        self._thread_is_running = False  # whether _thread is running
        self._auto_adjust = auto_adjust
        self._space = 0  # free space in a line, set by _adjust_to_terminal_size
        self._frame = ""

        self._adjust_to_terminal_size()
        if ProgressIndicator._first_init:
            ProgressIndicator._first_init = False
            ProgressIndicator._console_setup()

    def __enter__(self):
        self.start()
        return self

    def __exit__(self, exception, value, trace):
        self.stop()

    @staticmethod
    def disable_indicators(disable):
        """! Globally prevents new indicators from starting.

        This does not affect currently running indicators.

        @param disable Bool. If True, no new indicators can be started.
            If False, resume default behaviour.
        """
        ProgressIndicator._disabled = disable

    @staticmethod
    def _console_setup():
        if os_name == 'nt':  # os name can be nt, posix, or java
            # Windows uses nt, which does not support carriage returns by
            # default. the following Windows specific module should fix this.
            try:
                from ctypes import windll
                k = windll.kernel32
                # GetStdHandle(-11) is the standart output device, the flag 7
                # is equal to
                #    ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT
                #        | ENABLE_VIRTUAL_TERMINAL_PROCESSING
                # the last flag enables several control sequences, like \r
                k.SetConsoleMode(k.GetStdHandle(-11), 7)
            except (ImportError):
                msg = "Failed to set console mode for 'nt' system (e.g."\
                      " Windows). ProgressIndicator(s) may be displayed"\
                      " incorrectly."
                warn(msg, category=RuntimeWarning, stacklevel=2)

    def _adjust_to_terminal_size(self, reserve=0):
        """! Keep animation in a single line."""
        # use width - 1, since show() prepends a space
        width = get_terminal_size().columns - 1
        self._frame = self._frame[:(width)]
        if len(self._message) + len(self._frame) >= width - reserve:
            sys.stdout.write(self._message + "\n")
            self._message = ""
        self._space = width - len(self._frame) - len(self._message)

    def _advance(self):
        """! Advance the animation to the next frame. """
        self._frame = next(self._animator)
        if self._auto_adjust:
            self._adjust_to_terminal_size()

    def _render(self):
        """! Regularly update the animation. Do not call manually! """
        while self._thread_is_running:
            self.step()
            # wait before writing next frame
            time.sleep(self._delay)

    def set_message(self, message):
        """! Change the message displayed in front of the animation.
        @param message String. Shown in front of the animation without
            seperator. If it would not fit in a single line with the
            animation, it will be printed once in a new line above the
            animation instead. Must not contain `\\r` or `\\n`.
        """
        self._message = message
        self._adjust_to_terminal_size()

    def show(self):
        """! Print the animation without advancing it."""
        # prepend a space to fit the cursor
        sys.stdout.write(f" {self._message}{self._frame}\r")
        sys.stdout.flush()

    def step(self):
        """! Advance and print the animation. """
        self._advance()
        self.show()

    def start(self):
        """! Start the animation in a new thread.

        Must call stop() afterwards.
        """
        if not ProgressIndicator._disabled and not self._thread_is_running:
            self._thread_is_running = True
            # start new threat to render the animator in the background
            self._thread = threading.Thread(target=self._render)
            self._thread.daemon = True  # stops thread on main thread exit
            self._thread.start()

    def stop(self):
        """! Stop the animation and join the thread. """
        if self._thread_is_running:
            self._thread_is_running = False
            if self._thread and self._thread.is_alive():
                self._thread.join()
            sys.stdout.write("\033[K")  # clear line

class Spinner(ProgressIndicator):
    """! Subclass of ProgressIndicator with a predefined animation. """

    def __init__(self, delay=0.1, message=""):
        """! initializes a ProgressIndicator with a rotating line animation.

        This method spawns a new thread to print the animation.
        Start the animation by either using the `start`/`stop` functions or a
        'with' block.

        @param delay [Default: 0.1] positive real number. Sets delay in
            seconds between drawing animation frames.
        @param message [Default: ""] string. Text shown before the indicator.
        """
        def _spin():
            while True:  # loop animation
                yield from "|/-\\"
        super().__init__(message + " ", _spin(), delay)

class Dots(ProgressIndicator):
    """! Subclass of ProgressIndicator with a predefined animation. """

    def __init__(self, delay=1, message="", num_dots=3, dot=".", blank=" "):
        """! initializes ProgressIndicator with a 'dot, dot, dot' animation.

        This method spawns a new thread to print the animation.
        Start the animation by either using the `start`/`stop` functions or a
        'with' block.

        @param delay [Default: 1] positive real number. Sets delay in seconds
            between drawing animation frames.
        @param message [Default: ""] string. Text shown before the indicator.
        @param num_dots [Default: 3] positive integer. Determines maximum
            number of dots drawn before clearing them with blanks.
        @param dot [Default: "."] string. Drawn up to num_dots times.
        @param blank [Default: " "] string. Placeholder for yet to be drawn
            dots. Must have same length as dot.
        """
        assert (len(dot) == len(blank))
        assert (num_dots > 0)

        def _dots():
            while True:  # loop animation
                for n in range(1, num_dots+1):  # iterate animation frames
                    # return single frame
                    yield f"{dot*n}{blank*(num_dots-n)}"
        super().__init__(message + " ", _dots(), delay)

class Percentage(ProgressIndicator):
    """! Manages a ProgressIndicator with a predefined animation. """

    def __init__(self, delay=1, message="", percentage=0, use_bar=True,
                 keep_output=False):
        """! initializes ProgressIndicator displaying a percentage.

        The percentage can be updated using the `set_progress` method.
        By default, this method spawns a new thread to print the animation.
        Start the animation by either using the `start`/`stop` functions or a
        `with` `as` block.

        @param delay [Default: 1] non-negative real number. Sets delay in
            seconds between drawing animation frames. If delay is set to 0, no
            new thread is spawned. The output is then updated in the main
            thread, whenever 'set_progress' is called.
        @param message [Default: ""] string. Text shown before the indicator.
        @param percentage [Default: 0] real number in [0, 1]. Initial
            percentage shown in the animation.
        @param use_bar [Default: True] bool. If True, adds a progress bar
            visualizing the current percentage.
        @param keep_output [Default: False] bool. Whether the last animation
            frame should be kept as a new line  when stopping.
        """
        if delay == 0:
            self._use_thread = False
            delay = 1  # arbitrary, will not be used outside of init
        else:
            self._use_thread = True
        self._keep_output = keep_output
        self._use_bar = use_bar
        self._progress = percentage
        self._disabled = False

        def _perc():
            while True:
                yield f"{100*self._progress:6.2f}%"
        super().__init__(message + " ", _perc(), delay, use_bar)

    def _advance(self):
        """! Advance the animation to the next frame. """
        self._frame = next(self._animator)
        if self._use_bar:
            self._adjust_to_terminal_size(5)
            # prepend bar to frame
            self._frame = self._bar(self._space, self._progress) + self._frame

    def start(self):
        """! Start the animation. Must call stop() afterwards.

        If delay > 0, this method spawns a new thread.
        """
        self._disabled = ProgressIndicator._disabled
        if self._use_thread:
            super().start()

    def stop(self):
        """! Stops the animation. """
        if not self._disabled:
            if self._use_thread:
                self.step()
            if self._keep_output:
                sys.stdout.write("\n")  # newline to 'save' output
            else:
                sys.stdout.write("\033[K")  # clear line
        if self._use_thread:
            super().stop()

    def set_progress(self, percentage):
        """! Updates the percentage shown by the indicator. Steps if delay = 0.

        @param percentage real number. Must be in the interval [0, 1].
        """
        self._progress = percentage
        if not self._disabled and not self._thread_is_running:
            self.step()

    @staticmethod
    def _bar(width, percentage):
        """! Returns a progress bar.

        @param width Total width of the bar. Must be at least 4.
        @param percentage Float in [0,1].
        @return String of length width, visualizing percentage.
        """
        w = width - 3  # 3 == len("[] ")
        n = int(w * percentage)
        return "[" + "#" * n + " " * (w - n) + "] "


import os

os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'  # see issue #152
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# os.environ['XLA_FLAGS'] = '--xla_compile=False'
os.environ['XLA_FLAGS'] = '--xla_gpu_cuda_data_dir=/usr/local/cuda'

import argparse
import json

import tensorflow as tf

from model.transformer import Transformer, default_hparams
from datasets.mimic import get_mimic_dataset

class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
    def __init__(self, d_model, warmup_steps=4000):
        super().__init__()

        self.d_model = d_model
        self.d_model = tf.cast(self.d_model, tf.float32)

        self.warmup_steps = warmup_steps

    def __call__(self, step):
        step = tf.cast(step, dtype=tf.float32)
        arg1 = tf.math.rsqrt(step)
        arg2 = step * (self.warmup_steps ** -1.5)

        return tf.math.rsqrt(self.d_model) * tf.math.minimum(arg1, arg2)

    def get_config(self):
        return {
            'd_model': str(int(self.d_model)),
            'warmup_steps': str(int(self.warmup_steps)),
        }

def masked_loss(label, pred):
    mask = label != 0
    loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
        from_logits=True, reduction='none')
    loss = loss_object(label, pred)

    mask = tf.cast(mask, dtype=loss.dtype)
    loss *= mask

    loss = tf.reduce_sum(loss) / tf.reduce_sum(mask)
    return loss

def masked_accuracy(label, pred):
    pred = tf.argmax(pred, axis=2)
    label = tf.cast(label, pred.dtype)
    match = label == pred

    mask = label != 0

    match = match & mask

    match = tf.cast(match, dtype=tf.float32)
    mask = tf.cast(mask, dtype=tf.float32)
    return tf.reduce_sum(match) / tf.reduce_sum(mask)

def main(args, hparams):

    train_batches, tokenizer = get_mimic_dataset(args.csv_root, args.vocab_root, args.mimic_root,
                                                 batch_size=args.batch_size)

    val_batches, _ = get_mimic_dataset(args.csv_root, args.vocab_root, args.mimic_root,
                                       mode='validate',
                                       batch_size=args.batch_size)

    csv_logger_callback = tf.keras.callbacks.CSVLogger('checkpoints/training.log')
    model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
        filepath=f'checkpoints/{args.model_name}.tf',
        save_weights_only=True,
        monitor='val_masked_accuracy',
        mode='max',
        save_best_only=True)

    # Create a MirroredStrategy.
    strategy = tf.distribute.MirroredStrategy()
    print('Number of devices: {}'.format(strategy.num_replicas_in_sync))

    # Open a strategy scope.
    with strategy.scope():
        learning_rate = args.init_lr if args.init_lr is not None else \
             CustomSchedule(hparams['d_model'])

        optimizer = tf.keras.optimizers.Adam(
            learning_rate, beta_1=0.9, beta_2=0.98, epsilon=1e-9)

        transformer = Transformer(
            num_layers=hparams['num_layers'],
            d_model=hparams['d_model'],
            num_heads=hparams['num_heads'],
            dff=hparams['dff'],
            target_vocab_size=tokenizer.get_vocab_size(),
            dropout_rate=hparams['dropout_rate'],
            input_shape=(224, 224, 1),
            classifier_weights=args.classifier_weights)

        transformer.compile(
            loss=masked_loss,
            optimizer=optimizer,
            metrics=[masked_accuracy],
        )

    transformer.fit(
        train_batches,
        epochs=args.n_epochs,
        validation_data=val_batches,
        callbacks=[model_checkpoint_callback, csv_logger_callback]
    )

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--csv_root', default='preprocessing/mimic')
    parser.add_argument('--vocab_root', default='preprocessing/mimic')
    parser.add_argument('--mimic_root', default='/vol/biodata/data/MIMIC-CXR/mimic-cxr-jpg')
    parser.add_argument('--model_name', default='RATCHET')
    parser.add_argument('--model_params', default='model/hparams.json')
    parser.add_argument('--classifier_weights', default=None)
    parser.add_argument('--n_epochs', default=10)
    parser.add_argument('--init_lr', default=None)
    parser.add_argument('--batch_size', default=32)
    parser.add_argument('--seed', default=42)
    args = parser.parse_args()

    # Load mode default hyperparameters and update from file if exist
    hparams = default_hparams()
    if args.model_params:
        with open(args.model_params) as json_file:
            hparams_from_file = json.load(json_file)
            hparams.update((k, hparams_from_file[k])
                           for k in set(hparams_from_file).intersection(hparams))

    # Set tensorflow random seed
    tf.random.set_seed(args.seed)

    # Run main training sequence
    main(args=args, hparams=hparams)


class Node:
    def __init__(self,data=None):
        self.data = data
        self.next = None

class Stack:
    def __init__(self):
        self.top = Node()
        self.len = 0

    def push(self,data):
        self.top.data = data
        newNode = Node()
        newNode.next = self.top
        self.top = newNode
        self.len+=1

    def pop(self):
        if self.len > 0:
            temp = self.top.next
            temp1 = temp.data

            self.top.next = temp.next
            del temp
            self.len-=1
            return temp1

        return False

    def enpty(self):
        if self.len ==0:
            return True
        return False

    def search(self,data):
        temp = self.top.next
        cou =0
        while temp.next !=None:
            cou+=1
            if temp.data == data:
                return self.len - cou
            temp = temp.next
        return -1

if __name__ =='__main__':
    stk = Stack()
    for i in range(1,25,1):
        stk.push(i)
    for i in range(1,26,1):
        print(stk.pop())

    for i in range(1,25,1):
        stk.push(i)

    print(stk.search(2),end="\n\n\n")
    for i in range(1,26,1):
        print(stk.pop())


import requests
from bs4 import BeautifulSoup

def get_news():
    url = 'https://www.globo.com/'
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'html.parser')

    noticias = soup.find_all('a')
    tgt_class1 = 'post__title'
    tgt_class2 = 'post-multicontent__link--title__text'

    news_dict = {}
    for noticia in noticias:
        if (noticia.h2!= None) and (noticia.h2.get('class')!=None):
            if tgt_class1 in noticia.h2.get('class'):
                news_dict[noticia.h2.text]=noticia.get('href')
            if tgt_class2 in noticia.h2.get('class'):
                news_dict[noticia.h2.text]=noticia.get('href')
    with open('noticias.txt','w') as f:
        for noticia in noticias:
            f.write('{}\n'.format(noticia))
    return news_dict

news = get_news()


from test_framework.p2p import P2PInterface
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, check_node_connections

MAX_OUTBOUND_FULL_RELAY_CONNECTIONS = 16
MAX_BLOCK_RELAY_ONLY_CONNECTIONS = 2
MAX_OUTBOUND_CONNECTIONS = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS + \
    MAX_BLOCK_RELAY_ONLY_CONNECTIONS

class P2PFeelerReceiver(P2PInterface):
    def on_version(self, message):
        # The bitcoind node closes feeler connections as soon as a version
        # message is received from the test framework. Don't send any responses
        # to the node's version message since the connection will already be
        # closed.
        pass

class P2PAddConnections(BitcoinTestFramework):
    def set_test_params(self):
        self.num_nodes = 2

    def setup_network(self):
        self.setup_nodes()
        # Don't connect the nodes

    def run_test(self):
        self.p2p_idx = [0] * self.num_nodes

        def add_outbounds(node, quantity, conn_type, **kwargs):
            for _ in range(quantity):
                self.log.debug(
                    f"Node {node.index}, {conn_type}: {self.p2p_idx[node.index]}")
                node.add_outbound_p2p_connection(
                    P2PInterface(),
                    p2p_idx=self.p2p_idx[node.index],
                    connection_type=conn_type,
                    **kwargs,
                )
                self.p2p_idx[node.index] += 1

        self.log.info(
            f"Add {MAX_OUTBOUND_FULL_RELAY_CONNECTIONS} outbounds to node 0")
        add_outbounds(
            self.nodes[0],
            MAX_OUTBOUND_FULL_RELAY_CONNECTIONS,
            "outbound-full-relay")

        self.log.info(
            f"Add {MAX_BLOCK_RELAY_ONLY_CONNECTIONS} block-relay-only connections to node 0")
        add_outbounds(
            self.nodes[0],
            MAX_BLOCK_RELAY_ONLY_CONNECTIONS,
            "block-relay-only")

        self.log.info(
            f"Add {MAX_BLOCK_RELAY_ONLY_CONNECTIONS} block-relay-only connections to node 1")
        add_outbounds(
            self.nodes[1],
            MAX_BLOCK_RELAY_ONLY_CONNECTIONS,
            "block-relay-only")

        self.log.info("Add 5 inbound connections to node 1")
        for i in range(5):
            self.log.info(f"inbound: {i}")
            self.nodes[1].add_p2p_connection(P2PInterface())

        self.log.info("Add 4 outbounds to node 1")
        add_outbounds(self.nodes[1], 4, "outbound-full-relay")

        self.log.info("Check the connections opened as expected")
        check_node_connections(
            node=self.nodes[0],
            num_in=0,
            num_out=MAX_OUTBOUND_CONNECTIONS)
        check_node_connections(
            node=self.nodes[1],
            num_in=5,
            num_out=4 + MAX_BLOCK_RELAY_ONLY_CONNECTIONS)

        self.log.info("Disconnect p2p connections & try to re-open")
        self.nodes[0].disconnect_p2ps()
        self.p2p_idx[0] = 0
        check_node_connections(node=self.nodes[0], num_in=0, num_out=0)

        self.log.info(
            f"Add {MAX_OUTBOUND_FULL_RELAY_CONNECTIONS} outbounds to node 0")
        add_outbounds(
            self.nodes[0],
            MAX_OUTBOUND_FULL_RELAY_CONNECTIONS,
            "outbound-full-relay")
        check_node_connections(
            node=self.nodes[0],
            num_in=0,
            num_out=MAX_OUTBOUND_FULL_RELAY_CONNECTIONS)

        self.log.info(
            f"Add {MAX_BLOCK_RELAY_ONLY_CONNECTIONS} block-relay-only connections to node 0")
        add_outbounds(
            self.nodes[0],
            MAX_BLOCK_RELAY_ONLY_CONNECTIONS,
            "block-relay-only")
        check_node_connections(
            node=self.nodes[0],
            num_in=0,
            num_out=MAX_OUTBOUND_CONNECTIONS)

        self.log.info("Restart node 0 and try to reconnect to p2ps")
        self.restart_node(0)
        self.p2p_idx[0] = 0

        self.log.info(
            f"Add {MAX_OUTBOUND_FULL_RELAY_CONNECTIONS} outbounds to node 0")
        add_outbounds(
            self.nodes[0],
            MAX_OUTBOUND_FULL_RELAY_CONNECTIONS,
            "outbound-full-relay")
        check_node_connections(
            node=self.nodes[0],
            num_in=0,
            num_out=MAX_OUTBOUND_FULL_RELAY_CONNECTIONS)

        self.log.info(
            f"Add {MAX_BLOCK_RELAY_ONLY_CONNECTIONS} block-relay-only connections to node 0")
        add_outbounds(
            self.nodes[0],
            MAX_BLOCK_RELAY_ONLY_CONNECTIONS,
            "block-relay-only")
        check_node_connections(
            node=self.nodes[0],
            num_in=0,
            num_out=MAX_OUTBOUND_CONNECTIONS)

        check_node_connections(
            node=self.nodes[1],
            num_in=5,
            num_out=4 + MAX_BLOCK_RELAY_ONLY_CONNECTIONS)

        self.log.info("Add 1 feeler connection to node 0")
        feeler_conn = self.nodes[0].add_outbound_p2p_connection(
            P2PFeelerReceiver(), p2p_idx=self.p2p_idx[0], connection_type="feeler")

        # Feeler connection is closed
        assert not feeler_conn.is_connected

        # Verify version message received
        assert_equal(feeler_conn.message_count["version"], 1)
        # Feeler connections do not request tx relay
        assert_equal(feeler_conn.last_message["version"].relay, 0)

if __name__ == '__main__':
    P2PAddConnections().main()


import time
import json
import logging
from pymisp import ExpandedPyMISP, MISPAttribute, MISPEvent, MISPSighting
from resilient_lib import IntegrationError

log = logging.getLogger(__name__)

def get_misp_client(URL, API_KEY, VERIFY_CERT, proxies):
    misp_client = ExpandedPyMISP(URL, API_KEY, ssl=VERIFY_CERT, proxies=proxies)
    return misp_client

def create_misp_event(misp_client, misp_distribution, misp_threat_level, misp_analysis_level, misp_event_name):
    misp_event = MISPEvent()
    misp_event.distribution = misp_distribution
    misp_event.threat_level_id = misp_threat_level
    misp_event.analysis = misp_analysis_level
    misp_event.info = misp_event_name
    event_response = misp_client.add_event(misp_event)
    return event_response

def create_misp_attribute(misp_client, misp_event_id, misp_attribute_type, misp_attribute_value):
    misp_event = MISPEvent()
    misp_event.id = int(misp_event_id)
    misp_event.uuid = get_event_uuid(misp_client, misp_event_id)
    misp_attribute = MISPAttribute()
    misp_attribute.type = misp_attribute_type
    misp_attribute.value = misp_attribute_value
    attribute_response = misp_client.add_attribute(misp_event, misp_attribute)
    return attribute_response

def create_misp_sighting(misp_client, my_misp_sighting):
    misp_sighting = MISPSighting()
    misp_sighting.value = my_misp_sighting
    misp_sighting.timestamp = int(time.time())
    misp_sighting.source = "IBM Resilient SOAR"
    sighting_response = misp_client.add_sighting(misp_sighting)
    return sighting_response

def search_misp_attribute(misp_client, search_attribute):
    search_results = misp_client.search(value=search_attribute)
    if not isinstance(search_results, list):
        raise IntegrationError("Received an unexpected response type from the MISP API. Expected a list but received: {}".format(type(search_results)))
    search_results_len = len(search_results)
    if search_results_len == 0:
        success_status = False
    elif search_results_len > 0:
        success_status = True
    else:
        success_status = False
    search_results_response = {
                                "search_status": success_status,
                                "search_results" : search_results
                            }
    return search_results_response

def get_event_tags(event):
    search_tags = []
    if "Tag" in event["Event"]:
        tags = event["Event"]["Tag"]
        for tag in tags:
            log.info("found tag {}".format(tag["name"]))
            search_tags.append(tag["name"])
    return search_tags

def get_attribute_tags(attribute):
    search_tags = []
    if "Tag" in attribute:
        for tag in attribute["Tag"]:
            log.info("found tag {}".format(tag["name"]))
            search_tags.append(tag["name"])
    return search_tags

def get_misp_attribute_tags(misp_client, search_results):
    search_tags = []
    log.debug(json.dumps(search_results, indent=4))
    for event in search_results:
        # Grab Event Tags
        search_tags += get_event_tags(event)
        # Grab Attribute Tags
        for attribute in event["Event"]["Attribute"]:
            search_tags += get_attribute_tags(attribute)
    search_tags = list(set(search_tags))
    return search_tags

def get_misp_sighting_list(misp_client, misp_event_id):
    misp_event = MISPEvent()
    misp_event.id = int(misp_event_id)
    sighting_result = misp_client.sightings(misp_event)
    return sighting_result

def get_event_uuid(misp_client, misp_event_id):
    # returns list with a single element: an event dict
    result = misp_client.search(eventid=misp_event_id)
    for event in result:
        event_uuid = event['Event']['uuid']
    return event_uuid

def get_attribute_uuid(misp_client, misp_attribute_value, misp_event_id):
    misp_event = MISPEvent()
    misp_event.id = int(misp_event_id)
    event_response = misp_client.get_event(misp_event)
    attribute_uuid = None
    if not event_response['Event']['Attribute']:
        log.error("Could not get a uuid for event = {} and attribute = {}. Does it exist?".format(misp_event_id, misp_attribute_value))
        raise IntegrationError("Failed to find any attributes on event {}".format(misp_event_id))

    else:
        for attribute in event_response['Event']['Attribute']:
            if attribute['value'] == misp_attribute_value:
                attribute_uuid = attribute['uuid']
        if attribute_uuid:
            return attribute_uuid
        else:
            raise IntegrationError("Failed to match attribute value = {} for any attributes associated with event = {}".format(misp_attribute_value, misp_event_id))

def create_tag(misp_client, misp_attribute_value, misp_tag_type, misp_tag_name, misp_event_id):
    if misp_tag_type == "Event":
        object_uuid = get_event_uuid(misp_client, misp_event_id)
    elif misp_tag_type == "Attribute":
        object_uuid = get_attribute_uuid(misp_client, misp_attribute_value, misp_event_id)
    tag_result = misp_client.tag(object_uuid, misp_tag_name)
    return tag_result


from datetime import datetime
from manager_rest import storage_manager, models
import base_test

class StorageManagerTests(base_test.BaseServerTestCase):

    def test_store_load_delete_blueprint(self):
        now = str(datetime.now())
        blueprint = models.BlueprintState(id='blueprint-id',
                                          created_at=now,
                                          updated_at=now,
                                          plan={'name': 'my-bp'},
                                          source='bp-source')
        storage_manager.instance().put_blueprint('blueprint-id', blueprint)
        blueprint_from_list = storage_manager.instance().blueprints_list()[0]
        blueprint_restored = \
            storage_manager.instance().get_blueprint('blueprint-id')
        bp_from_delete = storage_manager.instance().delete_blueprint(
            'blueprint-id')
        self.assertEquals(blueprint.__dict__, blueprint_from_list.__dict__)
        self.assertEquals(blueprint.__dict__, blueprint_restored.__dict__)
        # in bp returned from delete operation only 'id' is guaranteed to
        # return
        self.assertEquals(blueprint.id, bp_from_delete.id)
        self.assertEquals(0,
                          len(storage_manager.instance().blueprints_list()))

    def test_get_blueprint_deployments(self):
        now = str(datetime.now())
        blueprint = models.BlueprintState(id='blueprint-id',
                                          created_at=now,
                                          updated_at=now,
                                          plan={'name': 'my-bp'},
                                          source='bp-source')
        storage_manager.instance().put_blueprint('blueprint-id', blueprint)

        deployment1 = models.Deployment(id='dep-1',
                                        created_at=now,
                                        updated_at=now,
                                        blueprint_id='blueprint-id',
                                        plan={'name': 'my-bp'},
                                        permalink=None,
                                        workflows={},
                                        inputs={},
                                        policy_types={},
                                        policy_triggers={},
                                        groups={},
                                        outputs={})
        storage_manager.instance().put_deployment('dep-1', deployment1)

        deployment2 = models.Deployment(id='dep-2',
                                        created_at=now,
                                        updated_at=now,
                                        blueprint_id='blueprint-id',
                                        plan={'name': 'my-bp'},
                                        permalink=None,
                                        workflows={},
                                        inputs={},
                                        policy_types={},
                                        policy_triggers={},
                                        groups={},
                                        outputs={})
        storage_manager.instance().put_deployment('dep-2', deployment2)

        deployment3 = models.Deployment(id='dep-3',
                                        created_at=now,
                                        updated_at=now,
                                        blueprint_id='another-blueprint-id',
                                        plan={'name': 'my-bp'},
                                        permalink=None,
                                        workflows={},
                                        inputs={},
                                        policy_types={},
                                        policy_triggers={},
                                        groups={},
                                        outputs={})
        storage_manager.instance().put_deployment('dep-3', deployment3)

        blueprint_deployments = storage_manager.instance()\
            .get_blueprint_deployments(
                'blueprint-id')

        self.assertEquals(2, len(blueprint_deployments))
        self.assertEquals(deployment1.__dict__,
                          blueprint_deployments[0].__dict__)
        self.assertEquals(deployment2.__dict__,
                          blueprint_deployments[1].__dict__)

    def test_model_serialization(self):
        dep = models.Deployment(id='dep-id',
                                created_at='some-time1',
                                updated_at='some-time2',
                                blueprint_id='bp-id',
                                plan={'field': 'value'},
                                permalink=None,
                                workflows={},
                                inputs={},
                                policy_types={},
                                policy_triggers={},
                                groups={},
                                outputs={})

        serialized_dep = dep.to_dict()
        self.assertEquals(11, len(serialized_dep))
        self.assertEquals(dep.id, serialized_dep['id'])
        self.assertEquals(dep.created_at, serialized_dep['created_at'])
        self.assertEquals(dep.updated_at, serialized_dep['updated_at'])
        self.assertEquals(dep.blueprint_id, serialized_dep['blueprint_id'])
        self.assertEquals(dep.permalink, serialized_dep['permalink'])

        deserialized_dep = models.Deployment(**serialized_dep)
        self.assertEquals(dep.id, deserialized_dep.id)
        self.assertEquals(dep.created_at, deserialized_dep.created_at)
        self.assertEquals(dep.updated_at, deserialized_dep.updated_at)
        self.assertEquals(dep.blueprint_id, deserialized_dep.blueprint_id)
        self.assertEquals(dep.permalink, deserialized_dep.permalink)

    def test_fields_query(self):
        now = str(datetime.now())
        blueprint = models.BlueprintState(id='blueprint-id',
                                          created_at=now,
                                          updated_at=now,
                                          plan={'name': 'my-bp'},
                                          source='bp-source')
        storage_manager.instance().put_blueprint('blueprint-id', blueprint)

        blueprint_restored = \
            storage_manager.instance().get_blueprint('blueprint-id',
                                                     {'id', 'created_at'})
        self.assertEquals('blueprint-id', blueprint_restored.id)
        self.assertEquals(now, blueprint_restored.created_at)
        self.assertEquals(None, blueprint_restored.updated_at)
        self.assertEquals(None, blueprint_restored.plan)


from mainapp.models import Subject, Teacher, Class, Lesson
import xml.etree.ElementTree as ET
import datetime

daysLookup = {
    'monday': 0,
    'tuesday': 1,
    'wednesday': 2,
    'thursday': 3,
    'friday': 4,
}

classNumberHourLookup = {
    '1': datetime.time(8),
    '2': datetime.time(9),
    '3': datetime.time(10),
    '4': datetime.time(11),
    '5': datetime.time(12),
    '6': datetime.time(13),
    '7': datetime.time(14),
    '8': datetime.time(15)
}

def manage():
    tree = ET.parse('.\mainapp\classSchedule.xml')
    for cls in tree.getroot():
        className = cls.find('classname').text
        for day in cls.find('timetable'):
            dayName = day.get('value')
            for subject in day:
                subjectName = subject.find('name').text
                teacher = subject.find('teacher').text
                lessonTime = subject.find('lesson').text

                clz = Class.objects.get(name=className)
                t = Teacher.objects.get(tempFullName=teacher)
                try:
                    sub = Subject.objects.get(name=subjectName, clazz=clz, teacher=t)
                except:
                    sub = Subject(name=subjectName, clazz=clz, teacher=t)
                    sub.save()

                lessonDateTime = datetime.datetime.combine(
                        next_weekday(datetime.date.today() + datetime.timedelta(days=4), daysLookup[dayName]),
                        classNumberHourLookup[lessonTime])

                try:
                    les = Lesson.objects.get(subject=sub, date=lessonDateTime)
                except:
                    les = Lesson(subject=sub, date=lessonDateTime)
                    les.save()

def next_weekday(d, weekday):
    days_ahead = weekday - d.weekday()
    if days_ahead <= 0:  # Target day already happened this week
        days_ahead += 7
    return d + datetime.timedelta(days_ahead)

def getTeacherTimeTable(name):
    tree = ET.parse('D:\django\mainapp\classSchedule.xml')
    root = tree.getroot()

    parent_map = {c: p for p in tree.iter() for c in p}
    teacherSubjects = [parent_map[thc] for thc in root.findall("class/timetable/day/subject/teacher") if
                       thc.text == name]
    result = {}
    result['monday'] = []
    result['tuesday'] = []
    result['wednesday'] = []
    result['thursday'] = []
    result['friday'] = []

    for subject in teacherSubjects:
        day = parent_map[subject].get('value')
        lessonInfo = [subject.find('name').text, subject.find('lesson').text]
        result[day].append(lessonInfo)

    return result

def getStudentTimeTable(className):
    tree = ET.parse('D:\django\mainapp\classSchedule.xml')
    root = tree.getroot()

    parent_map = {c: p for p in tree.iter() for c in p}
    clazz = [clazz for clazz in root.findall("class") if clazz.find('classname').text == className]
    days = clazz[0].findall('timetable/day')
    result = {}
    for day in days:
        dayValue = day.get('value')
        result[dayValue] = []
        for subject in day:
            lessonInfo = [subject.find('name').text, subject.find('lesson').text]
            result[dayValue].append(lessonInfo)

    return result


import os
import re
import scrapy
import string

class DatpiffSpider(scrapy.Spider):
    name = "datpiff"

    custom_settings = {
        'CONCURRENT_REQUESTS': 1,
        'USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:44.0) Gecko/20100101 Firefox/44.0',
        'TELNETCONSOLE_ENABLED': False,
        'ROBOTSTXT_OBEY': False
        }

    page_count = 0
    base_url = 'http://www.datpiff.com'
    cont_crawling = True

    # change this to where you would like to save all the mixtapes. make sure you have enough room!! it will be a lot.
    save_directory = '../../../../../../../Volumes/UNTITLED/datpiff_saves'

    def start_requests(self):
        while self.cont_crawling:
            yield scrapy.Request(
                    url=self.base_url + '/mixtapes.php?filter=all&p=' + str(self.page_count),
                    callback=self.get_mixtape_page)
            self.page_count += 1

    def get_mixtape_page(self, response):
        mixtapes = response.css('div.contentListing > div.contentItem')
        if len(mixtapes) == 0:
            self.cont_crawling = False
        else:
            for m in mixtapes:
                mixtape_url = m.css('div.contentThumb > a[href$=".html"]::attr(href)').extract()
                if not len(mixtape_url) == 0:
                    yield scrapy.Request(
                        url=self.base_url + mixtape_url[0],
                        callback=self.get_player,
                        dont_filter=True)

    def get_player(self, response):
        html_token = response.css('div[onclick*="openMixtape"]::attr(onclick)').extract()
        if len(html_token) > 0:
            token = re.findall(r'\s*\'(.*)\'', html_token[0])[0]

            yield scrapy.Request(
                url=self.base_url + '/player/' + token,
                callback=self.get_embed,
                dont_filter=True)

    def get_embed(self, response):
        embed_url = response.css('iframe::attr(src)').extract()

        yield scrapy.Request(
            url=embed_url[0],
            callback=self.get_mp3_links,
            dont_filter=True)

    def get_mp3_links(self, response):
        script = response.css('script[src="/js/player.js"]:first-of-type + script').extract()

        url_track_prefix = re.findall(r'\s*var trackPrefix = \'(.*?)\';', script[0])
        track_objs = re.findall(r'\s*playerData\.tracks\.push\((.*?)\);', script[0])
        mixtape_title = re.findall(r'\s*\"title\":\"(.*?)\"', script[0])[0].replace('\'', '')
        artist_name = re.findall(r'\s*\"artist\":\"(.*?)\"', script[0])[0].replace('\'', '')

        for t in track_objs:
            url_track =  re.findall(r'\s*concat\((.*?)\),', t)[0].strip().replace('\'', '')

            yield scrapy.Request(
                url=url_track_prefix[0] + url_track,
                callback=self.download_track,
                dont_filter=True,
                meta={
                    'mixtape_title': mixtape_title,
                    'artist_name': artist_name,
                    'url_track': url_track
                    })

    def download_track(self, response):
        for i in string.punctuation:
            clean_mix_title = response.meta['mixtape_title'].replace(i, '')

        mixtape_dir = self.save_directory + '/' + response.meta['artist_name'] + ' - ' + clean_mix_title
        file_path = mixtape_dir + '/' + response.meta['url_track']

        if not os.path.isdir(mixtape_dir):
            os.mkdir(mixtape_dir)

        with open(file_path, 'wb') as f:
            f.write(response.body)
            f.flush()
            os.fsync(f.fileno())


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

import os
import sys
import argparse
import numpy as np
from easydict import EasyDict as edict
import sklearn
from sklearn.preprocessing import normalize
import time

import signal
import traceback

reload(sys)
sys.setdefaultencoding("utf-8")

if sys.version_info.major == 2:
    import Queue as queue
    from Queue import Queue
else:
    import queue as Queue
import threading

global signal_stop
global read_ths
global infer_ths
global write_ths
global read_done
global write_done
global infer_done

signal_stop = False

def sigint_handler(signum, frame):
    global signal_stop
    global read_ths
    global infer_ths
    global write_ths

    signal_stop = True
    print("main-thread exit")

    # wait for finish
    for t in read_ths:
        t.join()
    for t in infer_ths:
        t.join()
    write_ths.join()

    sys.exit()

signal.signal(signal.SIGINT, sigint_handler)

class writeThread(threading.Thread):
    def __init__(self, out_q, write_func, tasks):
        threading.Thread.__init__(self)
        self.out_q = out_q
        self.write_func = write_func
        self.tasks = tasks

    def run(self):
        global signal_stop
        global write_done
        global infer_done

        done = 0
        while not signal_stop:
            try:
                # embedding, outpaths = self.out_q.get(block=True)
                embedding, outpaths = self.out_q.get(timeout=1)
                if done % 10000 == 0:
                    print(done, self.tasks)
                for idx, outp in enumerate(outpaths):
                    self.write_func(outp, embedding[idx].flatten())
                    done += 1
            except queue.Empty as e:
                if infer_done:
                    break
                pass
        print("write Thread done")
        write_done = True

class readThread(threading.Thread):
    def __init__(self, in_q, out_q, read_func, is_flip, shape):
        threading.Thread.__init__(self)
        self.in_q = in_q
        self.out_q = out_q
        self.read_func = read_func
        self.is_flip = is_flip
        self.shape = shape

    def run(self):
        global signal_stop
        unfinished = True
        while not signal_stop and unfinished:
            try:
                image_path, outpath = self.in_q.get(timeout=1)
                out_img = []
                img = self.read_func(image_path, self.shape[0] == 1)
                if len(img.shape) == 3:
                    img = img[:, :, ::-1]  # to rgb
                    img = np.transpose(img, (2, 0, 1))
                else:
                    img = img[np.newaxis, :]
                attempts = [0, 1] if self.is_flip else [0]
                for flipid in attempts:
                    _img = np.copy(img)
                    if flipid == 1:
                        self.do_flip(_img)
                    out_img.append(_img)

                self.out_q.put((out_img, outpath))

            except queue.Empty as e:
                unfinished = False
                break
        print("read Thread done")

    def do_flip(self, data):
        for idx in range(data.shape[0]):
            data[idx, :, :] = np.fliplr(data[idx, :, :])

class inferThread(threading.Thread):
    def __init__(self, net, fwd_func, in_q, out_q, image_shape, is_flip):
        threading.Thread.__init__(self)
        self.net = net
        self.fwd_func = fwd_func
        self.in_q = in_q
        self.out_q = out_q
        self.batchsz = net.batch_size
        self.image_shape = image_shape
        self.is_flip = is_flip
        self.end = False

    def run(self):
        global signal_stop
        global read_done

        bz = self.batchsz

        if self.is_flip:
            bz = self.batchsz // 2 * 2

        input_blob = np.zeros(
            shape=(bz, self.image_shape[0], self.image_shape[1], self.image_shape[2])
        )

        while not signal_stop and not self.end:
            idx = 0
            outpaths = []
            while idx < bz:
                try:
                    images, outpath = self.in_q.get(timeout=1)
                    input_blob[idx] = images[0]
                    idx += 1
                    if self.is_flip:
                        input_blob[idx] = images[1]
                        idx += 1
                    outpaths.append(outpath)
                except queue.Empty as e:
                    if read_done:
                        self.end = True
                    break

            if idx == 0:
                continue

            embedding = self.fwd_func(self.net, input_blob)

            if self.is_flip:
                embedding1 = embedding[0::2]
                embedding2 = embedding[1::2]
                embedding = embedding1 + embedding2

            # assert len(outpaths) == embedding.shape[0], "%d vs %d"%(len(outpaths), embedding.shape[0])
            if self.is_flip:
                assert len(outpaths) == idx / 2
            else:
                assert len(outpaths) == idx

            embedding = sklearn.preprocessing.normalize(embedding[0 : len(outpaths)])

            self.out_q.put((embedding, outpaths))

        print("infer thread done")

"""
class binThread (threading.Thread):
    def __init__(self, net, in_q, image_shape, is_flip, read_func, write_func):
        threading.Thread.__init__(self)
        self.net = net
        self.in_q = in_q;
        self.batchsz = net.batch_size;
        self.image_shape = image_shape
        self.is_flip = is_flip
        self.read_func = read_func
        self.write_func = write_func
        self.end = False

    def run(self):
        global signal_stop

        bz = self.batchsz

        if (self.is_flip):
            bz = self.batchsz//2 * 2

        input_blob = np.zeros(shape=(bz, self.image_shape[0],
                    self.image_shape[1], self.image_shape[2]))

        while not signal_stop and not self.end:
            # load bin
            inpath, outpath = self.in_q.get(timeout=1)
            data_set = self.read_func(inpath, self.image_shape)

            data_list = data_set[0]
            issame_list = data_set[1]
            embeddings_list = []

            for i in range( len(data_list) ):
                data = data_list[i]
                embeddings = None
                ba = 0
                while ba<data.shape[0]:
                    bb = min(ba+bz, data.shape[0])
                    count = bb-ba
                    _data = nd.slice_axis(data, axis=0, begin=bb-bz, end=bb)
                    db = mx.io.DataBatch(data=(_data,))
                    self.net.model.forward(db, is_train=False)
                    net_out = self.net.model.get_outputs()
                    _embeddings = net_out[0].asnumpy()
                    if embeddings is None:
                        embeddings = np.zeros( (data.shape[0], _embeddings.shape[1]) )
                    embeddings[ba:bb,:] = _embeddings[(bz-count):,:]
                    ba = bb
                embeddings_list.append(embeddings)

            embeddings = embeddings_list[0].copy()
            if self.is_flip:
                embeddings = embeddings_list[0] + embeddings_list[1]
            embeddings = sklearn.preprocessing.normalize(embeddings)

            # save output numpy
            self.write_func(outpath, embeddings)
            self.write_func(outpath+".issame", issame_list)

        print("infer thread done")
"""

class CitrusBaseInfer(object):
    def __init__(self, args, dtype="fp32"):
        self._args = args
        self._dtype = dtype
        self._net = None
        self._ctx = []
        self._image_shape = []
        self._batch_size = 8
        self._net_scale_type = "large"
        self._flops = None
        self._epoch = None
        self._gpu_mem_use = 0
        self._nets = []
        self.fwd_func = None

        self._load_model()

    def _load_model(self):
        raise NotImplementedError()

    #

    def infer_embedding(
        self, imgs=None, read_func=None, write_func=None, is_flip=False
    ):
        global read_ths
        global infer_ths
        global write_ths
        global read_done
        global write_done
        global infer_done

        read_done = False
        write_done = False
        infer_done = False
        assert 0 != len(imgs) and imgs is not None, "images to infer must not be empty!"
        assert write_func is not None
        assert read_func is not None

        try:
            in_q = Queue()
            for items in imgs:
                in_q.put(items)
            tasks = in_q.qsize()

            print("begin thread")

            out_q = Queue()
            img_q = Queue()

            read_ths = []
            infer_ths = []

            read_thread_num = len(self._nets) * 2
            if read_thread_num > 30:
                read_thread_num = 30
            for n in range(read_thread_num):
                read_ths.append(
                    readThread(in_q, img_q, read_func, is_flip, self._image_shape)
                )

            for n in self._nets:
                infer_ths.append(
                    inferThread(
                        n, self.fwd_func, img_q, out_q, self._image_shape, is_flip
                    )
                )

            for t in read_ths:
                t.start()

            for t in infer_ths:
                t.start()

            write_ths = writeThread(out_q, write_func, tasks)
            write_ths.start()

            while not read_done:
                done = True
                for t in read_ths:
                    if t.isAlive():
                        done = False
                if done:
                    read_done = True
                    print("read threads done!\n")
                time.sleep(1)

            while not infer_done:
                done = True
                for t in infer_ths:
                    if t.isAlive():
                        done = False
                if done:
                    infer_done = True
                    print("infer threads done!\n")
                time.sleep(1)

            while not write_done:
                # if use join(), ctrl-c can not work
                time.sleep(1)

            if in_q.qsize() == 0 and img_q.qsize() == 0 and out_q.qsize() == 0:
                return True
            else:
                print(
                    "not done! in_q.qsize()",
                    in_q.qsize(),
                    "img_q.qsize()",
                    img_q.qsize(),
                    "out_q.qsize()",
                    out_q.qsize(),
                )
                return False
        except:
            print("error", traceback.print_exc())
            signal_stop = True
            return False

    #

    """
    def infer_bin(self, imgs=None, read_func = None, write_func = None, is_flip=False):
        global infer_ths
        global infer_done

        infer_done = False
        assert 0 != len(imgs) and imgs is not None, "images to infer must not be empty!"
        assert write_func is not None
        assert read_func is not None

        try:
          in_q = Queue()
          for items in imgs:
            in_q.put(items)

          infer_ths = []

          for n in self._nets:
            infer_ths.append(binThread(n, in_q, self._image_shape, is_flip, read_func, write_func))

          for t in infer_ths:
              t.start()

          while not infer_done:
            done = True
            for t in infer_ths:
              if t.isAlive():
                  done = False
            if done:
                infer_done = True
                print("infer threads done!\n")
            time.sleep(1)

          return True
        except:
          print("error", traceback.print_exc())
          signal_stop = True
          return False
    """
    #


def totalFruit(fruits: list[int]):
    d={}
    for i in fruits:
        if i in d:
            d[i]+=1
        else:
            d[i]=1
    d=dict(sorted(d.items(), key=lambda x:x[1], reverse=True))
    count = 0
    total=0
    print(d)
    for i, j in d.items():
        total+=j
        count+=1
        if count==2:
            break
    return total

# print(totalFruit([1,2,1]))
# print(totalFruit([0,1,2,2]))
# print(totalFruit([1,2,3,2,2]))
print(totalFruit([3,3,3,1,2,1,1,2,3,3,4]))


# Zahir Choudhry
# 9 / 23 / 2021
# Lab 3 - Loops in Python
# CS 131

# Works

def my_sum(my_list):
    sum = 0
    i = 0
    while (i < len(my_list)):
        sum += my_list[i]
        i += 1
    return sum
    """Our version of the sum function.

    https://docs.python.org/dev/library/functions.html#sum

    This function takes a list of numbers and returns the sum of
    those numbers. This function should return 0 if the list is
    empty. We covered this in lecture, but see if you can write the
    function without referring to your notes.

    Arguments:
        my_list (list): A list of integers.

    Returns:
        int: The sum of all elements in my_list.

    Examples:

        >>> print(my_sum([]))
        0

        >>> print(my_sum([1, 2, 3]))
        6

        >>> print(my_sum([1.5, 2.5, 3.5]))
        7.5

    """

# Works

def my_count(my_list, my_element):
    i = 0
    counter = 0
    while (i < len(my_list)):
        if (my_list[i] == my_element):
            counter += 1
        i += 1
    return counter
    """Our version of the count function.

    https://docs.python.org/dev/library/stdtypes.html#common-sequence-operations

    This function takes a list and an element, and returns the number
    of times that element appears in the list.

    Arguments:
        my_list (list): A list of integers.
        my_element (int): The element to be counted.

    Returns:
        int: The number of times my_element appears in my_list.

    Examples:

        >>> print(my_count([], 1))
        0

        >>> print(my_count([1, 2, 3], 2))
        1

        >>> print(my_count([1, 2, 3], 4))
        0

        >>> print(my_count([3, 1, 2, 3, 1], 2))
        1

        >>> print(my_count([3, 1, 2, 3, 1], 3))
        2

    """

def my_max(my_list):
    i = 0
    max = -100
    while(i < len(my_list)):
        if(my_list[i] > max):
            max = my_list[i]
        i += 1
    return max
    """Our version of the max function.

    https://docs.python.org/dev/library/functions.html#max

    This function takes a list of numbers and returns the largest
    of those numbers. You may assume the list is not empty and that
    it will only contain positive numbers.

    Arguments:
        my_list (list): A list of integers.

    Returns:
        int: The largest element in my_list.

    Examples:

        >>> print(my_max([1, 2, 3]))
        3

        >>> print(my_max([1, 1, 2, 3, 1]))
        3

    """

# Works

def my_any(my_list):
    valid = False
    i = 0
    while i < len(my_list):
        if (bool(my_list[i]) == True):
            valid = True
        i += 1
    return valid
    """Our version of the any function.

    https://docs.python.org/dev/library/functions.html#any

    This function takes a list, and returns True at least one
    element of the list is considered True. This function should
    return False if the list is empty.

    Arguments:
        my_list (list): A list of integers and strings.

    Returns:
        bool: True if any element of my_list is considered True.
            False otherwise.

    Examples:

        >>> print(my_any([]))
        False

        >>> print(my_any([1, 'hello', [1, 2, 3]]))
        True

        >>> print(my_any([0, '', []]))
        False

        >>> print(my_any([1, '', []]))
        True

        >>> print(my_any([1, 'hello', []]))
        True

        >>> print(my_any([1, '', [1, 2, 3]]))
        True

    """

# Check W Professor

def my_all(my_list):
    i = 0
    valid = True
    while (i < len(my_list)):
        if (bool(my_list[i]) == False):
            valid = False
        i += 1
    return valid
    """Our version of the all function.

    https://docs.python.org/dev/library/functions.html#all

    This function takes a list, and returns True only if every
    element of the list is considered True. This function should
    return True if the list is empty.

    Arguments:
        my_list (list): A list of integers and strings.

    Returns:
        bool: True if all elements of my_list are considered True.
            False otherwise.

    Examples:

        >>> print(my_all([]))
        True

        >>> print(my_all([1, 'hello', [1, 2, 3]]))
        True

        >>> print(my_all([0, '', []]))
        False

        >>> print(my_all([1, '', []]))
        False

        >>> print(my_all([0, 'hello', []]))
        False

        >>> print(my_all([0, '', [1, 2, 3]]))
        False

    """

#Works

def my_min(my_list):
    i = 0
    min = 100000000
    while i < len(my_list):
        if(my_list[i] <  min):
            min = my_list[i]
        i += 1
    return min
    """Our version of the min function.

    https://docs.python.org/dev/library/functions.html#min

    This function takes a list of numbers and returns the smallest
    of those numbers. You may assume the list is not empty, but you
    may *not* assume that it will only contain positive numbers.

    Arguments:
        my_list (list): A list of integers.

    Returns:
        int: The smallest element in my_list.

    Examples:

        >>> print(my_min([1, 2, 3]))
        1

        >>> print(my_min([1, 1, 2, 3, 1]))
        1

    """

#Works

def my_index_1(my_list, my_element):
    index = None
    i = 0
    while i < len(my_list):
        if(my_list[i] == my_element):
            return i
        i += 1
    return index
    """Our version of the one-argument version of the index function.

    https://docs.python.org/dev/library/stdtypes.html#common-sequence-operations

    This function takes a list and an element, and returns the smallest
    index where the element occurs in the list. This function returns
    None if the element is not in the list.

    Arguments:
        my_list (list): A list of integers.
        my_element (int): The element to be found.

    Returns:
        int: The smallest index at which my_element is found in my_list.

    Examples:

        >>> print(my_index_1([], 2))
        None

        >>> print(my_index_1([1, 2, 3], 2))
        1

        >>> print(my_index_1([3, 1, 2, 3, 1], 2))
        2

        >>> print(my_index_1([3, 1, 2, 3, 1], 3))
        0

    """

# Works

def my_reversed(my_list):
    i = len(my_list)-1
    new_list = []
    while(i >= 0):
        new_list.append(my_list[i])
        i -= 1
    return new_list
    """Our version of the reversed function.

    https://docs.python.org/dev/library/functions.html#reversed

    This function takes a list, and returns a list that contains
    the same elements in reverse.

    Arguments:
        my_list (list): A list of integers and strings.

    Returns:
        list: The elements of my_list, in reverse.

    Examples:

        >>> print(my_reversed([]))
        []

        >>> print(my_reversed([1, 2, 3]))
        [3, 2, 1]

        >>> print(my_reversed([1, 'a', 3, 2, 'z']))
        ['z', 2, 3, 'a', 1]

    """

# Works

def my_enumerate(my_list):
    i = 0
    new_list = []
    while i < len(my_list):
        a_list = [i, my_list[i]]
        new_list.append(a_list)
        i += 1
    return new_list
    """Our version of the enumerate function.

    https://docs.python.org/dev/library/functions.html#enumerate

    This function takes a list, and returns a nested list, where each
    inner list contains the index and the element of the original
    list at that index.

    Arguments:
        my_list (list): A list of integers and strings.

    Returns:
        list: A list of lists, with each inner list containing the index
            and the element of each item in my_list.

    Examples:

        >>> print(my_enumerate([]))
        []

        >>> print(my_enumerate(['a', 'b', 'c']))
        [[0, 'a'], [1, 'b'], [2, 'c']]

    """

#Works

def my_zip_beginner(my_list_1, my_list_2):
    i = 0
    new_list = []
    while i < len(my_list_1): # my_list1 & my_list_2 are same length
        a_list = [my_list_1[i], my_list_2[i]]
        new_list.append(a_list)
        i += 1
    return new_list
    """Our version of a two-argument version of the zip function.

    https://docs.python.org/dev/library/functions.html#zip

    This function takes two lists, and returns a list, with a
    list containing the first elements of both lists as the first
    element, a list containing the second elements of both lists
    as the second element, and so on. You may assume that the two
    lists are of equal length.

    Arguments:
        my_list_1 (list): A list of integers or strings.
        my_list_2 (list): A list of integers or strings.

    Returns:
        list: A list of lists, with each inner list containing the
            corresponding elements from my_list_1 and my_list_2.

    Examples:

        >>> print(my_zip_beginner([], []))
        []

        >>> print(my_zip_beginner([1, 2, 3], ['a', 'b', 'c']))
        [[1, 'a'], [2, 'b'], [3, 'c']]

    """
    pass # FIXME

# print (my_sum([1.5,2.5,3.5]))

# print(my_count([3, 1, 2, 3, 1], 3))

# print(my_max([1, 2, 3]))

# print(my_any([1, 'hello', [1, 2, 3]]))
# print(my_any([0, '', []]))
# print(my_any([1, '', [1, 2, 3]]))

# print(my_all([]))
# print(my_all([1, 'hello', [1, 2, 3]]))
# print(my_all([0, '', []]))
# print(my_all([1, '', []]))
# print(my_all([0, 'hello', []]))
# print(my_all([1, '', True]))

# print(my_min([1, 2, 3]))
# print(my_min([1, 1, 2, 3, 1]))

# print(my_index_1([], 2)) # None
# print(my_index_1([1, 2, 3], 2)) # 1
# print(my_index_1([3, 1, 2, 3, 1], 2)) # 2
# print(my_index_1([3, 1, 2, 3, 1], 3)) # 0

# print(my_reversed([])) # []
# print(my_reversed([1, 2, 3])) # [3, 2, 1]
# print(my_reversed([1, 'a', 3, 2, 'z'])) # ['z', 2, 3, 'a', 1]

# print(my_enumerate([])) # []
# print(my_enumerate(['a', 'b', 'c'])) # [[0, 'a'], [1, 'b'], [2, 'c']]

# print(my_zip_beginner([], [])) # []
# print(my_zip_beginner([1, 2, 3], ['a', 'b', 'c'])) # [[1, 'a'], [2, 'b'], [3, 'c']]

Lab 3 - Loops in Python/My_Functions/my_functions.py


import sys

# open_dir = r"C:\Users\cmh02\OneDrive\바탕 화면\Algorithm\python_algorithm\탐색&시뮬레이션\04_두_리스트_합치기\in1.txt"
# sys.stdin = open(open_dir, "rt")

n = input()
d1 = list(map(int, input().split()))
m = input()
d2 = list(map(int, input().split()))

l = sorted(d1 + d2)

for n in l:
    print(n, end=" ")


import cv2

img = cv2.imread('./imgs/semC.jpg', 0)

# funcao para calcular o valor do pixel
def calcVarA(pixel, pixelEntrada, pixelSaida):
    return (255 / (pixelSaida - pixelEntrada)) * (pixel - pixelEntrada)

line, collunm = img.shape

# Fazendo a stretching linear
for i in range(0, line):
    for j in range(0, collunm):

        pixelVal = calcVarA(img[i][j], 2, 200)

        img[i][j] = pixelVal if pixelVal <= 255 else 255

# salvando a imagem
cv2.imwrite('./imgs/outputImgs/histoImageStretchingLinear.png', img)


from collections import defaultdict

class Solution:
    def first_unique_character(self, arr: str) -> int:
        hash = defaultdict(int)
        for i in range(len(arr)):
            if hash[arr[i]] == 0:
                hash[arr[i]] = i + 1
            elif hash[arr[i]] < len(arr) + 1:
                hash[arr[i]] = len(arr) + 1
        return min(hash.values()) if min(hash.values()) < len(arr) + 1 else -1

so = Solution()
arr = "abcdccacb"
print(so.first_unique_character(arr))


# Получаем целое число от пользователя и сохраняем в переменной number
number = int(input())

# Инициализируем переменную result значением 1
result = 1

# Запускаем цикл от 1 до number + 1
for i in range(1, number + 1):
    # Инициализируем переменную fact значением 1
    fact = 1

    # Запускаем вложенный цикл от 1 до i + 1
    for j in range(1, i + 1):
        # Умножаем fact на j и сохраняем результат в fact
        fact *= j

    # Увеличиваем result на единицу, деленную на значение fact
    result += 1 / fact

# Выводим результат, округленный до пятого знака после запятой
print(round(result, 5))


#!/usr/bin/env python3

import serial
import MySQLdb
import time
# import sys

# box_status="1:0:1:0:1:0:0:0:0:0"

# Open database connection
db = MySQLdb.connect("localhost","admin","vs12345","Vendigo_S1" )

# prepare a cursor object using cursor() method
cursor = db.cursor()
Bstatus = "SELECT BoxStatus FROM BoxStatus"

def process_db():
      try:
         # Execute the SQL command
         cursor.execute(Bstatus)
         global boxstatus
         boxstatus = ""
         #Fetching complete column from the table
         result = cursor.fetchall();
         result = list(result)
         index = 0
         status=['0','0','0','0','0','0','0','0','0','0']
         while index < 10:
            status[index] = list(result[index])
            index += 1
         for i in range (0,10):
            if status[i] == ['0']:
               # print("Box: " + str(i+1) + " Free")
               if i != 9:
                  boxstatus += "0:"
               else:
                  boxstatus += "0"
            else:
               # print("Box: " + str(i+1) + " Occupied")
               if i != 9:
                  boxstatus += "1:"
               else:
                  boxstatus += "1"
         boxstatus.strip()

         print("boxstatus_command = " + boxstatus)
         ser.write(bytes(boxstatus, 'utf-8'))
         # ser.write("1:1:1:0:0:0:1:1:1:1".encode('utf-8'))
         time.sleep(1)
         ser.flushOutput()
         # ser.write(b'1:1:1:0:0:0:1:1:1:1')
         # if(boxstatus == "0:0:0:0:0:0:0:0:0:0") ser.write(bytes(b'1111111111'))

      except Exception:
         exc_type, value, traceback = sys.exc_info()
         print("Failed with exception [%s]" % exc_type.__name__)
         # print("Issue with operation, Kindly Crosscheck Database script!!!!")

if __name__ == '__main__':
   while True:
    try:
      ser = serial.Serial('/dev/ttyUSB1', 115200, timeout=5)
      if ser.isOpen()==True:
         ser.reset_input_buffer()
         break
      ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=5)
      if ser.isOpen()==True:
         ser.reset_input_buffer()
         break
    except serial.SerialException:
      print("Connecting with other port")

   while True:
        process_db()
        time.sleep(1)
        # if ser.in_waiting > 0:
        #     line = ser.readline().decode('utf-8').rstrip()
        #     print(line)
   # disconnect from server
   db.close()


import requests
import re

CONTEST_NAMES_PATTERN = r"(\d+)	([A-Za-z\d\/., -]+)  	([A-Za-z\d. -]+)  	"

def getContestNames() -> dict:

    # Fetch contestcalendar
    resp = requests.get("https://www.contestcalendar.com/cabnames.php")

    if int(resp.status_code / 100) != 2:
        return None

    # Use regex to get all data
    contests = re.findall(CONTEST_NAMES_PATTERN, resp.text, re.M)

    return [
        {
            "id": int(contest[0]),
            "name":contest[1],
            "cabrillo_name":contest[2],
        }
        for contest in contests
    ]


"""
Main
"""
from flask import Flask
from flask import jsonify
from flask import request
from flask import render_template
from werkzeug.exceptions import NotFound

from views.products import products_app

app = Flask(__name__)
app.register_blueprint(products_app, url_prefix="/products")

def print_request():
    """
    print_request
    :return:
    """
    print("request:", request)
    print("headers", request.headers)

@app.get("/")
def get_root():
    """
    get root
    :return:
    """
    # print_request()
    print(request.args)
    # return {
    #     "request.args": request.args,
    #     "request.args['foo']": request.args["foo"],
    #     "request.args.get('foo')": request.args.get("foo"),
    #     "request.args.getlist('foo')": request.args.getlist("foo"),
    #     "request.args.getlist('spam')": request.args.getlist("spam"),
    #     "request.args.to_dict()": request.args.to_dict(),
    #     "request.args.to_dict(flat=False)": request.args.to_dict(flat=False),
    #     "message": "Hello!",
    # }
    return render_template("index.html")

@app.route("/hello/")
@app.route("/hello/<name>/")
def hello_world(name: str = None):
    """
    hello_world
    :param name:
    :return:
    """
    # print_request()
    if name is None:
        name = request.args.get("name", "world")
    return f"
Hello {name}!
"

@app.get("/items/")
def get_items():
    """
    get_items
    :return:
    """
    return jsonify(
        {
            "items": [
                {"id": 1},
                {"id": 2},
            ],
        },
        # spam="eggs",
    )

@app.get("/items/<int:item_id>/")
def get_item(item_id: int):
    """
    get_item
    :param item_id:
    :return:
    """
    return {"item": {"id": item_id}}

@app.get("/items/<item_id>/")
def get_item_as_string(item_id: str):
    """
    get_item_as_string
    :param item_id:
    :return:
    """
    return {"item_id": item_id.upper()}

@app.errorhandler(404)
def handle_404(error):
    """
    handle_404
    :param error:
    :return:
    """
    if isinstance(error, NotFound) and error.description != NotFound.description:
        return error
    return f"
eroror: {error}
"

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


#!/usr/bin/python3
def weight_average(my_list):
    total = 0
    weight_tot = 0
    if len(my_list) == 0:
        return total
    for i in my_list:
        total += i[0] * i[1]
        weight_tot += i[1]
    return total / weight_tot


from Components import *

instance = None  # singleton

class EventSystem(TimeUpdatable):
    def __init__(self):
        TimeUpdatable.__init__(self)

        self.events = {}
        self.event_queue = []
        self.waiting_event_queue = []
        self.elapsedtime = 0.0

        messages = ["P1LostLife", "P2LostLife", "LevelClear", "ResetGame", "AsteroidDestroyed"]

        for msg in messages:
            self.events[msg] = []

    def activate(self):
        TimeUpdatable.activate(self)
        self.gameobject.send_event = self.send_event
        self.gameobject.send_event_after_sec = self.send_event_after_sec
        self.gameobject.trigger_event = self.trigger_event
        self.gameobject.register_event_listener = self.register_event_listener
        self.gameobject.deregister_event_listener = self.deregister_event_listener

    def deactivate(self):
        del self.gameobject.send_event
        del self.gameobject.send_event_after_sec
        del self.gameobject.trigger_event
        del self.gameobject.register_event_listener
        del self.gameobject.deregister_event_listener
        TimeUpdatable.deactivate(self)

    def update(self, delta):
        self.elapsedtime += delta
        self.work_queue()

    def work_queue(self):
        # check if waiting events should be triggered
        waiting_queue = list(self.waiting_event_queue)
        for waitingevent in waiting_queue:
            if self.elapsedtime > waitingevent[1]:
                self.send_event(waitingevent[0])
                self.waiting_event_queue.remove(waitingevent)

        working_queue = list(self.event_queue)
        self.event_queue = []
        for eventtype in working_queue:
            self.trigger_event(eventtype)

    def send_event(self, event):
        self.event_queue.append(event)

    def trigger_event(self, eventtype):
        for method in self.events[eventtype]:
            method()

    def register_event_listener(self, eventtype, listener):
        self.events[eventtype].append(listener)

    def deregister_event_listener(self, eventtype, listener):
        self.events[eventtype].remove(listener)

    def send_event_after_sec(self, eventtype, time):
        self.waiting_event_queue.append([eventtype, self.elapsedtime + time])


"""This module defines class UserLogout."""
from rest_framework_simplejwt.tokens import RefreshToken
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from django.http import JsonResponse
from user_activity.models import UserActivity
from car_app.models import User
from car_app.views.views_helper_functions import decode_token, decode_refresh_token

class UserLogout(APIView):
    """
    This class defines a method that logout a user from
    the application.
    """
    #pylint: disable=no-member

    authentication_classes = [JWTAuthentication]
    permission_classes = [IsAuthenticated]

    def post(self, request):
        """This method blacklists refresh token when a user logs out."""
        if request.content_type != 'application/json':
            return JsonResponse({'error': 'The Content-Type must be json.'}, status=415)

        refresh_token = request.data.get('refresh')
        if not refresh_token:
            return JsonResponse({'error': 'Refresh token is required.'}, status=400)

        result = decode_token(request)
        if isinstance(result, tuple):
            user_id, is_superuser, _ = result

            result = decode_refresh_token(refresh_token)
            if isinstance(result, JsonResponse):
                return result

            refresh_token_user_id = result
            try:
                user_logging_out = User.objects.get(id=refresh_token_user_id)
            except User.DoesNotExist:
                return JsonResponse({'error': 'Provided token has no valid user.'}, status=401)

            manager = user_logging_out.team_manager if hasattr(
                user_logging_out, 'team_manager') else None

            if manager:
                user_manager = True if manager.id == user_id else False
            else:
                user_manager = False

            if refresh_token_user_id != user_id and is_superuser is False and user_manager is False:
                error_message = 'You do not have the permission to perform this action.'
                return JsonResponse({'error': error_message}, status=403)

            try:
                token = RefreshToken(refresh_token)
                token.blacklist()

                UserActivity.objects.create(
                    user = user_logging_out,
                    activity_type = 'Logout',
                )

                return JsonResponse({'message': 'Logged out successfully.'}, status=200)
            except BaseException as error: # pylint: disable=broad-exception-caught
                return JsonResponse({'error': str(error)}, status=400)

        error_message = result
        return error_message


import numpy as np
import os
import scipy.fftpack as pack
from load_audio import load_audio

class PreprocessAudio():

    def __init__(self, file_name, frame_size=0.025, frame_stride=0.01):

        self.sample_rate, self.data = load_audio(file_name)
        self.n = len(self.data)

        self.frame_size = frame_size
        self.frame_stride = frame_stride
        # number of samples per window
        self.window_size = int(self.sample_rate * self.frame_size)
        # number of samples to skip per stride
        self.shift_size = int(self.sample_rate * self.frame_stride)

    def boost(self, alpha=0.95):
        """
        Applying a first order high-pass filter to boost the higher frequencies
        """

        data = np.empty_like(self.data)
        data[0] = self.data[0]
        data[1:] = self.data[1:] - alpha * self.data[:-1]
        return data

    def rolling_window(self):

        shape = self.data.shape[:-1] + (int(self.data.shape[-1] - self.window_size + 1), int(self.window_size))
        strides = self.data.strides + (self.data.strides[-1],)
        return np.lib.stride_tricks.as_strided(self.data, shape=shape, strides=strides)[::self.shift_size]

    def smoothing_filter(self, filter=np.hamming):

        w = filter(self.window_size)
        return self.data * w

    def compute_nfft(self):

        nfft = 1
        while nfft < self.window_size:
            nfft *= 2
        return nfft

    def spectrum(self, nfft):
        """
        returns: given an NxM input matrix, output will be Nx(NFFT/2+1). Each row will be the magnitude spectrum of the corresponding frame.
        """

        fft = np.fft.rfft(self.data, n=nfft, axis=1)
        spectrum = fft * fft / self.window_size
        return spectrum

    def mel_binning(self, spectrum, nfft, lower_freq=300, upper_freq=8000, num_filt=26, norm=True):
        """
        Multiply each filter bank with the power spectrum and add up the coefficents.
        This will give us the amount of 'energy' is each filter bank
        """
        low_mel = PreprocessAudio.hz_to_mel(lower_freq)
        high_mel = PreprocessAudio.hz_to_mel(upper_freq)
        # get equally spaced points in mel scale
        mel_points = np.linspace(low_mel, high_mel, num_filt + 2)
        hz_points = PreprocessAudio.mel_to_hz(mel_points)
        # round hz_points to the nearest fft bin
        bins = np.floor((nfft + 1) * hz_points / self.sample_rate)

        fbank = np.zeros((num_filt, int(np.floor(nfft / 2 + 1))))
        for m in range(1, num_filt + 1):
            f_m_minus = int(bins[m - 1])   # left
            f_m = int(bins[m])             # center
            f_m_plus = int(bins[m + 1])    # right

            for k in range(f_m_minus, f_m):
                fbank[m - 1, k] = (k - bins[m - 1]) / (bins[m] - bins[m - 1])
            for k in range(f_m, f_m_plus):
                fbank[m - 1, k] = (bins[m + 1] - k) / (bins[m + 1] - bins[m])

        filter_banks = np.dot(spectrum, fbank.T)
        filter_banks = np.where(filter_banks == 0, np.finfo(float).eps, filter_banks)  # Numerical Stability

        if norm:
            filter_banks -= (np.mean(filter_banks, axis=0) + 1e-8)

        filter_banks = 20 * np.log10(filter_banks)
        return filter_banks

    def mfcc(self, filter_banks, num_ceps=12, norm=True):

        mfcc = pack.dct(filter_banks, type=2, axis=1, norm='ortho')[:, 1 : (num_ceps + 1)] # Keep 2-13
        if norm:
            mfcc -= (np.mean(mfcc, axis=0) + 1e-8)
        return mfcc

    def energy(self, spectrum):
        """
        E = sum(x[t]^2) --> sum over each frame
        """
        # total energy in each frame
        energy = np.sum(spectrum, 1)
        # if energy is zero, we get problems with log
        energy = np.where(energy == 0, np.finfo(float).eps,energy)
        return energy

    def lifter(self, cepstra, L=22):
        """
        Apply a cepstral lifter the the matrix of cepstra. This has the effect of increasing the
        magnitude of the high frequency DCT coeffs.
        """
        if L > 0:
            nframes, ncoeff = np.shape(cepstra)
            n = np.arange(ncoeff)
            lift = 1 + (L/2.)*np.sin(np.pi*n/L)
            return lift*cepstra
        else:
            # values of L <= 0, do nothing
            return cepstra

    def delta(self, features, N=2):
        """
        Compute delta features from a feature vector sequence.
        """

        num_rows = len(features)
        denominator = 2 * sum([i**2 for i in range(1, N+1)])
        delta_feat = np.empty_like(features)
        padded = np.pad(features, ((N, N), (0, 0)), mode='edge')   # padded version of feat
        for t in range(num_rows):
            delta_feat[t] = np.dot(np.arange(-N, N+1), padded[t : t+2*N+1]) / denominator   # [t : t+2*N+1] == [(N+t)-N : (N+t)+N+1]
        return delta_feat

    def get_filter_banks(self):

        self.data = self.boost()
        self.data = self.rolling_window()
        self.data = self.smoothing_filter()

        nfft = self.compute_nfft()
        spectrum = self.spectrum(nfft)
        filter_banks = self.mel_binning(spectrum, nfft)
        return filter_banks

    """
    def get_features(self):

        energy = self.energy(spectrum).reshape(-1, 1)
        mfcc = self.mfcc(filter_banks)
        delta = self.delta(mfcc)
        delta_delta = self.delta(delta)
        ##### CAN ADD ENERGIES FOR DELTA FEATURES TO GET A TOTAL OF 39
        return np.hstack((mfcc, delta, delta_delta, energy))
    """

    @staticmethod
    def hz_to_mel(hz):

        return 1125 * np.log(1 + hz/700)

    @staticmethod
    def mel_to_hz(mel):

        return 700 * (np.exp(mel / 1125) - 1)


def get_magic_triangle(num):
    triangle = [[1], [1, 1]]
    for i in range(1, num - 1):
        row = []
        current_len = len(triangle[i])
        for _ in range(current_len + 1):
            if _ == 0:
                row.append(triangle[i][_])
            elif _ == current_len:
                row.append(triangle[i][-1])
            else:
                current_sum = triangle[i][_-1] + triangle[i][_]
                row.append(current_sum)
        triangle.append(row)
    return triangle

print(get_magic_triangle(5))


from tkinter import *

import requests
from requests.auth import HTTPBasicAuth

# Creating GUI
root = Tk()

# Giving Title to the program
root.title("Github User Details")

# Icon
root.iconbitmap('icon.ico')

# Frame
frame1 = LabelFrame(root, width=50, height=150, bd=5, bg='grey')
frame1.grid(row=0, column=0, padx=5, pady=5)

# Header
head = Label(frame1, text="GITHUB USER DATA COLLECTOR", font=("Arial", 15), fg="black", bd=2, anchor=W)
head.grid(row=0, columnspan=2, padx=50, pady=30, sticky=W + E)

# Entry columns

leb1 = Label(frame1, text="Enter username - ")
leb1.grid(row=2, columnspan=2, padx=30, pady=5)

e1 = Entry(frame1, width=30, borderwidth=5)
e1.grid(row=3, columnspan=2, padx=30, pady=15)

# Function for the button - show details
def show_details():
    # creating  connection

    authentication = HTTPBasicAuth(e1.get(), "password")
    data = requests.get('https://api.github.com/users/' + e1.get(),
                        auth=authentication)
    # getting data
    user_data = data.json()

    # storing data in a string

    a = "\n\t\tINFO OF  {} ----".format(e1.get()) + "\n\n\n\tName - \t {}".format(
        user_data['name']) + "\n\n\tEmail - \t {}".format(
        user_data['email']) + "\n\n\tId - \t {}".format(
        user_data['id']) + "\n\n\tLocation - \t {}".format(
        user_data['location']) + "\n\n\tFollowers - \t {}".format(
        user_data['followers']) + "\n\n\tFollowing - \t {}".format(
        user_data['following']) + "\n\n\tPublic repos - \t {}".format(
        user_data['public_repos']) + "\n\n\tRepos_url - \t{}".format(
        user_data['repos_url']) + "\n\n\tBio - \t {}".format(
        user_data['bio'])

    # Frame2

    frame2 = LabelFrame(root, width=70, height=150)
    frame2.grid(row=0, column=1, padx=5, pady=5)

    # Text field

    t1 = Text(frame2, width=70, font=("Arial", 10))
    t1.insert(INSERT, a)

    t1.grid(row=2, column=1)
    t1.configure(state='disabled')

    # function for more button

    def more():
        # storing data in a string

        b = "\n\n\tUpdated At - \t{}".format(
            user_data['updated_at']) + "\n\n\tSite Admin - \t{}".format(
            user_data['site_admin']) + "\n\n\tHireable - \t{}".format(
            user_data['hireable']) + "\n\n\tNode Id - \t{}".format(
            user_data['node_id']) + "\n\n\tSubscription Url - \t{}".format(
            user_data['subscriptions_url']) + "\n\n\tType - \t{}".format(
            user_data['type']) + "\n\n\tUrl - \t{}".format(user_data['url'])

        # Frame3

        frame3 = LabelFrame(root, width=75, height=150)
        frame3.grid(row=0, column=2)

        # Text field

        t2 = Text(frame3, font=("Arial", 10))
        t2.insert(INSERT, b)
        t2.grid(row=2, column=1)
        t2.configure(state='disabled')

        # Show another function

        def show_another():
            # destroying frames other than main frame

            frame2.destroy()
            frame3.destroy()

            # deleting the text in the entry box

            e1.delete(0, END)

        # show another button

        show_another_button = Button(frame1, text="Check Another", command=show_another, bg="yellow", anchor=W,
                                     fg="black")
        show_another_button.grid(row=13, column=1)

    # create a button for more data

    more_button = Button(frame2, text="More info", command=more, bg="yellow", fg="black")
    more_button.grid(row=13, column=1)

#  creating button - Show details

show_details_button = Button(frame1, text="Show Details", command=show_details, bg="yellow", fg="black")
show_details_button.grid(row=7, columnspan=2, padx=10, pady=20)

# credits function

def credits_for_project():
    credit_string = "\n\n\t\t\tCREDITS--\n\n\tTHIS BASIC APPLICATION COULD NOT HAVE BEEN DONE WITHOUT\n\n" \
                    "\tTHE HELP OF -\n\n" \
                    "\tGOOGLE , TUTORIALSPOINT, GEEKSFORGEEKS. \n\n" \
                    "\tI was not aware about all the widgets in TKINTER PYTHON and \n\n" \
                    "\tcreating a connection between host site and customer.\n\n" \
                    "\tI took some basic help from these sites.\n\n" \
                    "\tTHANK YOU !\n\n" \
                    "\twww.google.com\n\n" \
                    "\twww.tutorialspoint.com\n\n" \
                    "\twww.geeksforgeeks.org"

    credit_frame = LabelFrame(root, width=75, height=150)
    credit_frame.grid(row=0, column=2)

    t2 = Text(credit_frame, font=("Arial", 10))
    t2.insert(INSERT, credit_string)
    t2.grid(row=2, column=1)
    t2.configure(state='disabled')

# Credit button

credits_button = Button(root, text="Credits", command=credits_for_project, fg="blue")
credits_button.grid(row=15, column=1)

# About this Application Function

def about_this_application():
    about_text = "\t\tLittle About me --\n\nHello I'm currently a 1st year student in\n\n" \
                 "Institute of Technical Education and Research\n\n" \
                 "This is my 1st attempt in any coding competition.\n\n\t\tLittle about Project--\n\n" \
                 "Here I present you a very basic application which can extract data\n\n" \
                 "from GITHUB profile. The modules used are -\n\n" \
                 "TKINTER, PYTHON\n\n" \
                 "REQUESTS/HTTPBasicAUTH, PYTHON\n\n" \
                 "Tkinter is used to create GUI , while requests\n\n" \
                 "HTTPBasicAUTH is used to create a connection between host site and customer.\n" \
                 "After the connection is made we simply request for the data\n\n" \
                 "and store it in a variable, since the data extracted is quite complex\n\n" \
                 "I have used JSON to extract data from the variable.\n\n" \
                 "THANK YOU !"

    about_frame = LabelFrame(root, width=75, height=150)
    about_frame.grid(row=0, column=2)

    t2 = Text(about_frame, font=("Arial", 10))
    t2.insert(INSERT, about_text)
    t2.grid(row=2, column=1)
    t2.configure(state='disabled')

about_button = Button(root, text="About:", command=about_this_application, fg="blue", anchor=W)
about_button.grid(row=10, column=0)

root.mainloop()


import numpy as np
import random
class wcwp_generator:
    def __init__(self,train_lower,train_upper,test_lower,test_upper):
        self.unique_symbols = 2
        self.train_bounds = (train_lower,train_upper)
        self.test_bounds = (test_lower,test_upper)
        return
    def create_input_sequence(self,w_length):
        #create random initial sequence
        sequence = []
        a_count = 0
        string_sentence = ''
        for c in range(w_length):
            index = random.randint(1,self.unique_symbols)
            char = np.zeros([1,self.unique_symbols+1])
            char[0,index] = 1
            sequence += [char]
            if index == 1:
                string_sentence += 'a'
                a_count +=1
            else:
                string_sentence += 'b'
        string_sentence += 'c'
        char = np.zeros([1,self.unique_symbols+1])
        char[0,0] = 1
        sequence +=[char]
        indices = []
        c = 0
        while (len(indices)<a_count):
            index = random.randint(0,w_length-1)
            if not(index in indices):
                indices += [index]
        c = 0
        for c in range(w_length):
            char = np.zeros([1,self.unique_symbols+1])
            if c in indices:
                char[0,1] = 1
                string_sentence += 'a'
            else:
                char[0,2] = 1
                string_sentence += 'b'
            sequence += [char]
        return sequence,string_sentence
    def create_target_sequence(self,sequence):
        w_length =int( (len(sequence) - 1)/2 )
        t_seq = []
        a_count = 0
        for c in range(w_length):
            if sequence[c][0,1] == 1:
                a_count += 1
            t_seq += [np.ones([1,self.unique_symbols+1])]
        #the latter half of the sequence is checking to see if a_count thus far equals to a_count and same for b
        c=0
        na_count = 0
        nb_count = 0
        count = True
        if w_length == 1:
            char = np.zeros([1,3])
            if sequence[0][0,1] == 1:
                char[0,1] = 1
            else:
                char[0,2] = 1
            t_seq+=[char]
        else:
            for c in range(w_length):
                current = sequence[c+w_length]
                char = np.zeros([1,self.unique_symbols+1])
                if count:
                    if current[0,1] == 1:
                        na_count += 1
                    elif current[0,2] == 1:
                        nb_count += 1
                if na_count>=a_count:
                    char[0,2] = 1
                    count = False
                elif nb_count>=(w_length - a_count):
                    char[0,1] = 1
                    count = False
                else:
                    char[0,[1,2]] = 1
                t_seq+=[char]
        t_seq += [np.zeros([1,self.unique_symbols+1])]
        return t_seq
    def dataset_generator(self,sample_num, size, sample_string = []):
        #create n samples
        sample_in = []
        sample_target = []
        while (len(sample_in)<sample_num):
            w_length = random.randint(int((size[0]-1)/2),int((size[1]-1)/2))
            sentence, s_sentence = self.create_input_sequence(w_length)
            if not(s_sentence in sample_string):
                sample_in+=[sentence]
                sample_target+=[self.create_target_sequence(sentence)]
                sample_string+=[s_sentence]
        return sample_in,sample_target,sample_string
    def train_test_generator(self,train_num,test_num):
        train_in,train_out, sample_string = self.dataset_generator(train_num,self.train_bounds)
        test_in,test_out,s = self.dataset_generator(test_num,self.test_bounds,sample_string)
        np.savez("wcwp_training_dataset.npz",input=train_in,target=train_out)
        np.savez("wcwp_testing_dataset.npz",input=test_in,target=test_out)
        return
if __name__ == "__main__":
    wcwpg = wcwp_generator(1,50,50,100)
    seq,string_sentence = (wcwpg.create_input_sequence(5))
    print(string_sentence)
    tseq = wcwpg.create_target_sequence(seq)
    print(tseq)
    wcwpg.train_test_generator(10000,10000)


import tensorflow as tf
import tensorflow.contrib.layers as layers

def _get_inputs(mode: str) -> tf.data.Dataset:
    ds = tf.data.TextLineDataset(
        filenames=['data/californiahousing.csv.gz'],
        compression_type='GZIP')

    ds = ds.skip(1)
    ds.cache()
    if mode == 'TRAIN':
        ds = ds.skip(1024)
        ds = ds.repeat()
    elif mode == 'EVAL':
        ds = ds.take(1024)

    ds = ds.shuffle(4 << 10)

    def _parse_records(records: tf.Tensor) -> (tf.Tensor, tf.Tensor):
        columns = tf.decode_csv(
            records=records,
            record_defaults=[[0]] + ([[0.0]] * 8))

        return tf.stack(columns[1:]), columns[0]

    ds = ds.map(_parse_records)
    ds = ds.batch(512)
    return ds

def _model_fn(features: tf.Tensor, labels: tf.Tensor, mode: str) -> tf.estimator.EstimatorSpec:
    from losses.emd import squared_earth_mover_distance

    is_training = mode == tf.estimator.ModeKeys.TRAIN

    out = features
    for idx, outputs in enumerate([5, 7]):
        out = layers.fully_connected(
            inputs=layers.dropout(out, is_training=is_training, keep_prob=0.9),
            num_outputs=outputs,
            activation_fn=tf.nn.elu,
            biases_initializer=None,
            normalizer_fn=layers.batch_norm,
            normalizer_params={
                'is_training': is_training,
                'center': True,
                'scale': True,
                'renorm': True,
            })

        tf.summary.histogram(f'Activations{idx}', out)

    out = layers.fully_connected(
        inputs=layers.dropout(out, is_training=is_training, keep_prob=0.9),
        num_outputs=7,
        activation_fn=None,
        biases_initializer=tf.initializers.zeros())

    tf.summary.histogram(f'Logits', out)

    emd = squared_earth_mover_distance(
        predictions=out,
        labels=tf.one_hot(labels, depth=7))

    loss = tf.losses.compute_weighted_loss(emd)

    train_op = layers.optimize_loss(
        loss=loss,
        global_step=None,
        learning_rate=1e-2,
        optimizer=tf.train.AdamOptimizer,
        clip_gradients=1.5,
        summaries=layers.OPTIMIZER_SUMMARIES)

    eval_metric_ops = None
    if mode == tf.estimator.ModeKeys.EVAL:
        accuracy = tf.metrics.accuracy(
            labels=labels,
            predictions=tf.argmax(out, axis=-1))
        auc = tf.metrics.auc(tf.one_hot(labels, depth=7), tf.nn.softmax(out))
        eval_metric_ops = {
            'Accuracy': accuracy,
            'AUC': auc,
        }

    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=tf.argmax(out),
        loss=loss,
        train_op=train_op,
        eval_metric_ops=eval_metric_ops)

run_config = tf.estimator.RunConfig()
run_config = run_config.replace(
    save_checkpoints_steps=10000,
    save_summary_steps=100,
)

est = tf.estimator.Estimator(
    model_fn=_model_fn,
    model_dir='/tmp/emd',
    config=run_config)

tf.estimator.train_and_evaluate(
    estimator=est,
    train_spec=tf.estimator.TrainSpec(
        input_fn=lambda: _get_inputs('TRAIN'),
        hooks=[]),
    eval_spec=tf.estimator.EvalSpec(
        input_fn=lambda: _get_inputs('EVAL'),
        steps=1,
        start_delay_secs=5,
        throttle_secs=5,
    ))


from datetime import datetime

from sqlalchemy import Column, Integer, String, DateTime, Date, Boolean, ForeignKey
from sqlalchemy.orm import relationship

from server import db, ma

class Experience(db.Model):
    __tablename__ = "tb_experience"

    id = Column(Integer, primary_key=True)

    institution_name = Column(String)

    id_type_of_experience = Column(Integer, ForeignKey("tb_domain.id"))
    type_of_experience = relationship("Domain", foreign_keys=[id_type_of_experience])

    current_experience = Column(Boolean)

    start_year = Column(Date)

    finish_year = Column(Date)

    details = Column(String)

    created_at = Column(DateTime, default=datetime.now)
    created_by = Column(String)
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    updated_by = Column(String)

class ExperienceSchema(ma.ModelSchema):
    class Meta:
        fields = (
            "id",
            "name",
            "email",
            "description",
            "birth_date",
            "created_at",
            "created_by"
        )