import pandas as pd
import seaborn as sns
import plotly.express as px
import matplotlib.pyplot as plt

# # The DataFrames

# +
import os

print(os.listdir("../data"))
PATH = "../data/{}.csv"
# -

games = pd.read_csv(PATH.format("games"))
details = pd.read_csv(PATH.format("games_details"))
players = pd.read_csv(PATH.format("players"))
ranking = pd.read_csv(PATH.format("ranking"))
teams = pd.read_csv(PATH.format("teams"))

dfs = [[games, "games"], [details, "details"], [players, "players"], [ranking, "ranking"], [teams, "teams"]]

def show(df, name):
    print(f"> DataFrame {name} : ")
    display(df.head(5))

[show(df, name) for df, name in dfs];

# ### 1 - Games
#
#
# We start by study the `games` dataset. Each row here corresponds to a game.

# +
## quick look at the different columns
display(games.columns)

## we set them in lower case because caps lock is lame
games.columns = [name.lower() for name in games.columns]
# -

## let's use datetime
games["date"] = pd.to_datetime(games.game_date_est)

f = plt.figure(figsize=(15, 6))
games["SEASON"] = pd.to_datetime(games.season.astype(str))
g = sns.histplot(data=games, x="SEASON", palette="viridis", binwidth=365)
g.set_title("distribution of the number of game per seasons")
g.set_ylabel("number of games")
g.set_xlabel("season");

g = sns.countplot(data=games, x="home_team_wins")
g.set_title("distribution of games won by hosting team")
g.set_xlabel("host team won")
g.set_ylabel("number of games");

## for home team
f, a = plt.subplots(figsize=(15, 6))
sns.histplot(data=games, x="pts_away", color="blue", label="visitor team", kde=True)
sns.histplot(data=games, x="pts_home", color="red", label="home team", kde=True)
plt.legend()
a.set_title("distribution of the number of points scored in a game")
a.set_ylabel("number of game")
a.set_xlabel("number of points");

# We can hypothesize right away that there is a significant difference in the number of points scored when **hosting** or visiting a game. Let's assess this with a **t test**:

# +
from scipy import stats

stats.ttest_ind(games.pts_away.dropna(), games.pts_home.dropna())
# -

# The p-value is $< 0.01$ so we can reject the null-hypothesis saying that these means are the same with a confidence of $99\%$. This statistic can potentially be explained using other features. We might study this later full project.
#
#
# Here we assess the precision in the 3 points shoots between visitors and host teams :

# +
## 3 pts percentage

f = plt.figure(figsize=(15, 6))
sns.histplot(data=games, x="fg3_pct_away", color="blue", label="visitor team", kde=True)
sns.histplot(data=games, x="fg3_pct_home", color="red", label="home team", kde=True)
plt.legend()
# -

# Here, the same for 2 points shots

# +
## 2 pts percentage

f = plt.figure(figsize=(15, 6))
sns.histplot(data=games, x="fg_pct_away", color="blue", label="visitor team", kde=True)
sns.histplot(data=games, x="fg_pct_home", color="red", label="home team", kde=True)
plt.legend()

# -

# The precision is more different for normal shoots than for 3 points shoots.
#
# The dataset's feature `HOME_TEAM_WINS` is not very praticle if we need to compute it multiple times so we precompute the `winner_id`.

# +
def finds_winner(entry):
    if(entry["home_team_wins"]):
        return entry["home_team_id"]
    else :
        return entry["visitor_team_id"]

games["winner_id"] = games.apply(finds_winner, axis=1)
# -

## little check that it worked as intended
games[["home_team_id", "visitor_team_id", "home_team_wins", "winner_id"]].head()

## unecessary features
games = games.drop(["team_id_home", "game_status_text", "game_date_est"], axis=1)

# While we are preprocessing we are going to invistigate the `NaN` entries :

## entries with at least one NaN value
games[games.isna().sum(axis=1) != 0]

# These are the match that may have been won on "default wins". We decide to get rid of them.

games = games.dropna(how="any")

# We will study the relation between different features :

uninteresting_cols = ["home_team_id", "visitor_team_id", "season", "winner_id", "game_id", "date"]
sns.pairplot(data=games.drop(uninteresting_cols, axis=1), corner=True)

# We store the cleaned dataset in the `data/preprocessed` directory.

games.to_csv("../data/preprocessed/games.csv")

# ### 2 - Details
#
# Now let's study the `details` dataset. This one contains one row per player involved in one game. This gives us information about the actions of each player on the field during a given match.

details.columns = [name.lower() for name in details.columns]
details

details.describe()

# +
## obvious stat : the players have a lower precision on 3 points shoots than in the others

f, a = plt.subplots(figsize=(10, 4))
sns.histplot(details.groupby("player_id").mean(), x="fg3_pct", color="red", label="3 pts shoots", kde=True)
sns.histplot(details.groupby("player_id").mean(), x="fg_pct", label="shoots", kde=True)
a.set_title("precision percentage of shoots per player")
a.set_ylabel("number of players")
a.set_xlabel("precisions ration")
plt.legend();
# -

g = sns.histplot(details.groupby("player_name").pts.mean().reset_index()\
            .sort_values("pts", ascending=False).dropna())
g.set_title("Distribution of the mean number of points per game per player")
g.set_ylabel("number of players")
g.set_xlabel("mean number of points per game");

# +
f = plt.figure(figsize=(15, 7))
team_points = details.groupby('team_id').pts.mean().reset_index()\
            .sort_values("pts").dropna()\
            .merge(teams[["team_id", "name"]], on="team_id")

g = sns.barplot(data=team_points, x='name', y="pts")
g.set_xticklabels(g.get_xticklabels(), rotation=45)
g.set_title("mean number of points for every player in a team")
g.set_xlabel("name of the team")
g.set_ylabel("average number of points for every player in a team");

# +
f = plt.figure(figsize=(15, 5))
shoot = details.groupby("player_name").pts.mean().reset_index()\
            .sort_values("pts", ascending=False).head(20)

g = sns.barplot(data=shoot, x="player_name", y="pts")
g.set_xticklabels(g.get_xticklabels(), rotation=45)
g.set_title("mean points per match for the 20 bests players")
g.set_xlabel("mean number of points per match")
g.set_ylabel("player");
# -

details["min"] = details["min"].astype(str).apply(lambda x : float(x.split(":")[0]))

fig = px.scatter(details.groupby("player_name")[["pts", "min"]].mean().reset_index(),\
                 x="pts", y="min", hover_data=['player_name'],\
                 labels={
                        "pts" : "mean points per match",
                        "min" : "average number of minutes played"
                }, \
                title="Relation between the average of time played and the number of points scored")
fig.update_layout(width=800, height=800)
fig.show()

# +
f = plt.figure(figsize=(15, 5))
shoot = details.groupby("player_name").fg3m.mean().reset_index()\
            .sort_values("fg3m", ascending=False).head(20)

g = sns.barplot(data=shoot, x="player_name", y="fg3m")
g.set_xticklabels(g.get_xticklabels(), rotation=45)
g.set_title("mean points per match for the 20 bests players")
g.set_xlabel("mean number of points 3 points marked per match")
g.set_ylabel("player");
# -

fig = px.scatter(details.groupby("player_name")[["fg3m", "fgm"]].sum().reset_index(),\
                 x="fg3m", y="fgm", hover_data=['player_name'],\
                 labels={
                        "fg3m" : "3 points shoot marked",
                        "fgm" : "2 points shoot marked"
                }, \
                title="Relation between the number of 3 points and 2 points shots marked")
fig.update_layout(width=800, height=800)
fig.show()

# This graph shows us that Lebron James has absolutely 0 ounce of chill.

g = sns.histplot(data=details, x="min", binwidth=3)
g.set_title("distribution of the number of ")
g.set_xlabel("minutes played")
g.set_ylabel("number of players");

# During investigation we discovered that a player played 96 minutes in a game. We did our investigation on the precise game
# and realised this never happened. So we get rid of the row.

details = details.drop(details[details["min"] == 96.0].index[0])

details.to_csv("../data/preprocessed/details.csv")

# ### 3 - Rankings

# +
ranking.columns = [name.lower() for name in ranking.columns]
ranking["standingsdate"] = pd.to_datetime(ranking.standingsdate)

## useless cols
ranking = ranking.drop(["league_id", "returntoplay"], axis=1)
ranking
# -

g = sns.countplot(data=ranking[ranking.standingsdate == "2020-12-21"], x="conference")
g.set_title("number of teams of each conference of the 12/21/2020");

f, a = plt.subplots(figsize=(16, 4))
plt.plot(ranking.groupby("standingsdate").count().team_id)

lebron_at_cleveland_years = list(range(2003, 2011)) + list(range(2014, 2019))
wins_cleveland = ranking[ranking.team == "Cleveland"][["standingsdate", "w"]].drop_duplicates()
wins_cleveland["Lebron_was_here"] = wins_cleveland.standingsdate.apply(lambda date : date.year in lebron_at_cleveland_years)
wins_cleveland = wins_cleveland.drop_duplicates().sort_values("standingsdate", ascending=True)
px.line(wins_cleveland, x="standingsdate", y="w", color="Lebron_was_here", labels={"w" : "wins for Cleveland", "standingsdate" : "date in the season", "Lebron_was_here" : "was Lebron playing at Cleveland ?"}, title="Impact of Lebron on Cleveland wins during a season")

# We have one ranking per day. We need to order the team for each of them to extrac the real ranking for each of them.

# +
def find_ranking(daily_scores):
    sub_sorted = daily_scores.sort_values("w", ascending=False).reset_index()
    sub_sorted["rank"] = sub_sorted.index + 1
    sub_sorted = sub_sorted.set_index("index")
    return sub_sorted

from senpy import notify_me

ranking = ranking.groupby("standingsdate").apply(lambda subdf : find_ranking(subdf))
notify_me("ranking computation done")
# -

ranking.to_csv("../data/preprocessed/rankings.csv")

# ### 4 - Teams
#
#
# This can helps add some data about the team later but isn't so useful by itself.

teams

## do smthg
display(teams[teams.TEAM_ID == 1610612744])
display(teams[teams.TEAM_ID == 1610612757])
teams.columns = [col.lower() for col in teams.columns]
teams["name"] = teams["city"] + " " + teams["nickname"]

teams.name.drop_duplicates()

sns.histplot(data=teams, x="YEARFOUNDED", binwidth=2)

# ### 5 - Players
#
#
# This dataset doesn't contain any new info so we won't use it.

players

# # Lebron Jenkins, out


# -*- coding: utf-8 -*-
"""
########## Term Project ############
#                                  #
# owner: @author Sterling Engle    #
#        @author Jacob Sunia       #
#        @author Larry Delgado     #
#                                  #
# Due Jun 24, 2021 at 11:59 PM PDT #
# Finished: TBD at TBD             #
#----------------------------------#
# CSULB CECS 343 Intro to S/W Engr #
# Professor Phuong Nguyen          #
####################################
"""

# import tabulate

class ExpenseRecords:
    # Initialize expenses with empty dictionary and updates as needed
    def __init__(self, expenseRecord):

        self.__expenses = {}
        self.__updatedList = expenseRecord
        if len(expenseRecord) > 0 and type(expenseRecord[0]) is dict:
            self.__updatedList = []
            for entry in expenseRecord:
                self.__updatedList.append(Tenant(entry.get("aptNumber"),
                                             entry.get("name")))
        else:
            self.__updatedList = expenseRecord

    def insertExp(self, month, day, category, payee, amount):
        self.__expenses['Month'] = month
        self.__expenses['Day'] = day
        self.__expenses['Category'] = category
        self.__expenses['Payee'] = payee
        self.__expenses['Amount'] = amount
        self.__updatedList.append(self.__expenses)

    # If key is 'Amount' add the value of it to eTotal
    def return_total_expenses(self):
        eTotal = 0
        for key in self.__updatedList:
            if key == 'Amount':
                eTotal += self.__updatedList[key]
        return eTotal

    # Print Expense Records
    def displaySummary(self):
        print("==== Expense Summary ====")
        print(self)
        # Tabulate
        '''
        headers = self.__updatedList[0].keys()
        rows = [x.values() for x in self.__updatedList]
        print(tabulate.tabulate(rows, headers, tablefmt='rst'))
        '''
        # Print Expense Total Last
        print("\nExpense Total: " + str(self.return_total_expenses()))

Term_Project/ARS_Multiuser/sterlings versions so we can pull/ExpenseRecords.py


import socket,os

from queue import Queue
from youtube.video2img import Video
from youtube.video2img import ThreadPool

# Function to be executed in a thread
def videoNclip(*args):
    id = args[0]
    img_pool = args[1]

    vd = Video()
    if not os.path.isfile(os.path.join("videos", id)):
        url = "http://youtube.com/watch?v=" + id
        vd.setUrl(url)
        print("  비디오 다운로드 >>", vd.stream.default_filename)
        vd.download("videos")

    if not os.path.isdir(os.path.join("images", id)):
        vd.video_path = os.path.join("videos", id)
        vd.image_foldername = id

        print("  비디오 분할 >>", vd.stream.default_filename)
        vd.clip("images")
        img_pool.add_task(imgStarter, id)

    #########################
    img_pool.add_task(imgStarter, id)

def imgStarter(id):
    cs = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    cs.connect("./_im2txt")
    cs.send(id.encode('utf-8'))
    cs.close()

print("VIDEO2IMAGE SERVER READY")
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
    os.remove("./_video2img")
except OSError:
    pass
s.bind("./_video2img")
s.listen(1)

# Pool count = 3
video_pool = ThreadPool(3)

# processed Video ID
image_pool = ThreadPool(1)

while 1:
    conn, addr = s.accept()
    data = conn.recv(1024)

    id = data.decode()
    video_pool.add_task(videoNclip, id, image_pool)

conn.close()


from functools import partial
from operator import itemgetter

import numpy as np
from django.core.management import BaseCommand

from web.models import NotionDocument
from web.services.bert_service.read import get_bert_client
from web.services.notion_service.read import get_notion_client
from web.services.notion_service.read import to_plaintext
from web.utils import cosine_distance
from web.utils import get_text_chunks

class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('notion_url')

    def handle(self, *args, **kwargs):
        url = kwargs['notion_url']
        notion_client = get_notion_client()
        print("Getting doc")
        block = notion_client.get_block(url)
        print("SIMILAR TO:", block.title)
        text = to_plaintext(block)
        text_chunks = get_text_chunks(text)
        bert_client = get_bert_client()
        print("Embedding")
        embeddings = bert_client.encode(text_chunks)
        document_embedding = np.average(embeddings, axis=0, weights=[len(x) for x in text_chunks])  # weighted avg by chunk length
        compute_similarity = partial(cosine_distance, document_embedding)

        print("Finding similar")
        relevant_texts = NotionDocument.objects\
            .filter(embedding__isnull=False)\
            .exclude(parent_notion_document__title="People")\
            .values('id', 'embedding')
        embeddings = {x['id']: x['embedding'] for x in relevant_texts}
        similarities = {text_id: compute_similarity(embedding) for text_id, embedding in embeddings.items()}
        top_similar_ids = sorted(similarities.items(), key=itemgetter(1), reverse=True)

        ids, similarities = zip(*top_similar_ids)
        similarities = np.ravel(similarities)
        print(f'similarity range: {max(similarities):.3f} - {min(similarities):.3f}')

        # starting_index = len(ids) - 5
        starting_index = 0
        most_similar_text = NotionDocument.objects\
            .filter(id__in=ids[:5])\
            .values('title', 'parent_notion_document__title')

        for i, similar_text in enumerate(most_similar_text):
            print('-------------------')
            print(f'similarity: {similarities[(starting_index + i)]:.3f}')
            print()
            parent_title = similar_text['parent_notion_document__title']
            source_title = similar_text['title']
            title = f"{parent_title} > {source_title}" if parent_title else source_title
            print('#', title)
            # print(similar_text['text'])


from __future__ import annotations

import time
from typing import TYPE_CHECKING, Iterable
from uuid import uuid4

from .broker import get_broker
from .rate_limits import Barrier
from .results import ResultMissing

if TYPE_CHECKING:
    from .message import Message

class pipeline:
    """Chain actors together, passing the result of one actor to the
    next one in line.

    Parameters:
      children(Iterator[Message|pipeline]): A sequence of messages or
        pipelines.  Child pipelines are flattened into the resulting
        pipeline.
      broker(Broker): The broker to run the pipeline on.  Defaults to
        the current global broker.
    """
    messages: list[Message]

    def __init__(self, children: Iterable[Message | pipeline], *, broker=None):
        self.broker = broker or get_broker()
        messages: list[Message]
        self.messages = messages = []

        for child in children:
            if isinstance(child, pipeline):
                messages.extend(message.copy() for message in child.messages)
            else:
                messages.append(child.copy())

        for message, next_message in zip(messages, messages[1:]):
            message.options["pipe_target"] = next_message.asdict()

    def __len__(self):
        """Returns the length of the pipeline.
        """
        return len(self.messages)

    def __or__(self, other):
        """Returns a new pipeline with "other" added to the end.
        """
        return type(self)(self.messages + [other])

    def __str__(self):  # pragma: no cover
        return "pipeline([%s])" % ", ".join(str(m) for m in self.messages)

    @property
    def completed(self):
        """Returns True when all the jobs in the pipeline have been
        completed.  This will always return False if the last actor in
        the pipeline doesn't store results.

       Raises:
          RuntimeError: If your broker doesn't have a result backend
            set up.
        """
        try:
            self.messages[-1].get_result()

            return True
        except ResultMissing:
            return False

    @property
    def completed_count(self):
        """Returns the total number of jobs that have been completed.
        Actors that don't store results are not counted, meaning this
        may be inaccurate if all or some of your actors don't store
        results.

        Raises:
          RuntimeError: If your broker doesn't have a result backend
            set up.

        Returns:
          int: The total number of results.
        """
        for count, message in enumerate(self.messages, start=1):
            try:
                message.get_result()
            except ResultMissing:
                return count - 1

        return count

    def run(self, *, delay=None):
        """Run this pipeline.

        Parameters:
          delay(int): The minimum amount of time, in milliseconds, the
            pipeline should be delayed by. If both pipeline's delay and
            first message's delay are provided, the bigger value will be
            used.

        Returns:
          pipeline: Itself.
        """
        delay = max(delay or 0, self.messages[0].options.get("delay") or 0) or None
        self.broker.enqueue(self.messages[0], delay=delay)
        return self

    def get_result(self, *, block=False, timeout=None):
        """Get the result of this pipeline.

        Pipeline results are represented by the result of the last
        message in the chain.

        Parameters:
          block(bool): Whether or not to block until a result is set.
          timeout(int): The maximum amount of time, in ms, to wait for
            a result when block is True.  Defaults to 10 seconds.

        Raises:
          ResultMissing: When block is False and the result isn't set.
          ResultTimeout: When waiting for a result times out.

        Returns:
          object: The result.
        """
        last_message = self.messages[-1]

        if isinstance(last_message, (group, pipeline)):
            return last_message.get_result(block=block, timeout=timeout)

        backend = self.broker.get_results_backend()
        return last_message.get_result(backend=backend, block=block, timeout=timeout)

    def get_results(self, *, block=False, timeout=None):
        """Get the results of each job in the pipeline.

        Parameters:
          block(bool): Whether or not to block until a result is set.
          timeout(int): The maximum amount of time, in ms, to wait for
            a result when block is True.  Defaults to 10 seconds.

        Raises:
          ResultMissing: When block is False and the result isn't set.
          ResultTimeout: When waiting for a result times out.

        Returns:
          A result generator.
        """
        deadline = None
        if timeout:
            deadline = time.monotonic() + timeout / 1000

        for message in self.messages:
            if deadline:
                timeout = max(0, int((deadline - time.monotonic()) * 1000))

            if isinstance(message, (group, pipeline)):
                yield message.get_result(block=block, timeout=timeout)

            backend = self.broker.get_results_backend()
            yield message.get_result(backend=backend, block=block, timeout=timeout)

class group:
    """Run a group of actors in parallel.

    Parameters:
      children(Iterator[Message|group|pipeline]): A sequence of
        messages, groups or pipelines.
      broker(Broker): The broker to run the group on.  Defaults to the
        current global broker.
    """

    def __init__(self, children, *, broker=None):
        self.children = list(children)
        self.broker = broker or get_broker()
        self.completion_callbacks = []

    def __len__(self):
        """Returns the size of the group.
        """
        return len(self.children)

    def __str__(self):  # pragma: no cover
        return "group([%s])" % ", ".join(str(c) for c in self.children)

    def add_completion_callback(self, message):
        """Adds a completion callback to run once every job in this
        group has completed.  Each group may have multiple completion
        callbacks.

        Warning:
          This functionality is dependent upon the GroupCallbacks
          middleware.  If that's not set up correctly, then calling
          run after adding a callback will raise a RuntimeError.

        Parameters:
          message(Message)
        """
        self.completion_callbacks.append(message.asdict())

    @property
    def completed(self):
        """Returns True when all the jobs in the group have been
        completed.  Actors that don't store results are not counted,
        meaning this may be inaccurate if all or some of your actors
        don't store results.

        Raises:
          RuntimeError: If your broker doesn't have a result backend
            set up.
        """
        return self.completed_count == len(self)

    @property
    def completed_count(self):
        """Returns the total number of jobs that have been completed.
        Actors that don't store results are not counted, meaning this
        may be inaccurate if all or some of your actors don't store
        results.

        Raises:
          RuntimeError: If your broker doesn't have a result backend
            set up.

        Returns:
          int: The total number of results.
        """
        for count, child in enumerate(self.children, start=1):
            try:
                if isinstance(child, group):
                    child.get_results()
                else:
                    child.get_result()
            except ResultMissing:
                return count - 1

        return count

    def run(self, *, delay=None):
        """Run the actors in this group.

        Parameters:
          delay(int): The minimum amount of time, in milliseconds,
            each message in the group should be delayed by.

        Returns:
          group: This same group.
        """
        if self.completion_callbacks:
            from .middleware.group_callbacks import GROUP_CALLBACK_BARRIER_TTL, GroupCallbacks

            rate_limiter_backend = None
            for middleware in self.broker.middleware:
                if isinstance(middleware, GroupCallbacks):
                    rate_limiter_backend = middleware.rate_limiter_backend
                    break
            else:
                raise RuntimeError(
                    "GroupCallbacks middleware not found! Did you forget "
                    "to set it up? It is required if you want to use "
                    "group callbacks."
                )

            # Generate a new completion uuid on every run so that if a
            # group is re-run, the barriers are all separate.
            # Re-using a barrier's name is an unsafe operation.
            completion_uuid = str(uuid4())
            completion_barrier = Barrier(rate_limiter_backend, completion_uuid, ttl=GROUP_CALLBACK_BARRIER_TTL)
            completion_barrier.create(len(self.children))

            children = []
            for child in self.children:
                if isinstance(child, group):
                    raise NotImplementedError

                elif isinstance(child, pipeline):
                    pipeline_children = child.messages[:]
                    pipeline_children[-1] = pipeline_children[-1].copy(options={
                        "group_completion_uuid": completion_uuid,
                        "group_completion_callbacks": self.completion_callbacks,
                    })

                    children.append(pipeline(pipeline_children, broker=child.broker))

                else:
                    children.append(child.copy(options={
                        "group_completion_uuid": completion_uuid,
                        "group_completion_callbacks": self.completion_callbacks,
                    }))
        else:
            children = self.children

        for child in children:
            if isinstance(child, (group, pipeline)):
                child.run(delay=delay)
            else:
                self.broker.enqueue(child, delay=delay)

        return self

    def get_results(self, *, block=False, timeout=None):
        """Get the results of each job in the group.

        Parameters:
          block(bool): Whether or not to block until the results are stored.
          timeout(int): The maximum amount of time, in milliseconds,
            to wait for results when block is True.  Defaults to 10
            seconds.

        Raises:
          ResultMissing: When block is False and the results aren't set.
          ResultTimeout: When waiting for results times out.

        Returns:
          A result generator.
        """
        deadline = None
        if timeout:
            deadline = time.monotonic() + timeout / 1000

        for child in self.children:
            if deadline:
                timeout = max(0, int((deadline - time.monotonic()) * 1000))

            if isinstance(child, group):
                yield list(child.get_results(block=block, timeout=timeout))
            elif isinstance(child, pipeline):
                yield child.get_result(block=block, timeout=timeout)
            else:
                backend = self.broker.get_results_backend()
                yield child.get_result(backend=backend, block=block, timeout=timeout)

    def wait(self, *, timeout=None):
        """Block until all the jobs in the group have finished or
        until the timeout expires.

        Parameters:
          timeout(int): The maximum amount of time, in ms, to wait.
            Defaults to 10 seconds.

        Raises:
          ResultTimeout: When waiting times out.
        """
        for _ in self.get_results(block=True, timeout=timeout):  # pragma: no cover
            pass


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

import json
from game.read_write import *
from game.deck import *

def test_load_deck_data():
    data = load_deck_data()

    with open('../docs/deck.json', 'r') as json_file:
        test_deck = json.load(json_file)

    assert test_deck['deck'] == data

def test_shuffle_deck():
    test_data = load_deck_data()
    original_data = test_data.copy()
    shuffle_deck(test_data)
    assert test_data != original_data

def test_hand_player_cards():
    test_data = load_deck_data()
    shuffle_deck(test_data)
    test_deck = get_cards_in_deck(test_data)
    test_player_one_hand = [card for card in hand_player_cards(test_deck)]
    test_player_two_hand = [card for card in hand_player_cards(test_deck)]

    count = 0
    assert len(test_player_one_hand) == 9
    assert len(test_player_two_hand) == 9
    for card in test_deck:
        count += 1
        assert card not in test_player_one_hand
        assert card not in test_player_two_hand
    assert count == 34

def test_get_next_card_in_deck():
    test_data = load_deck_data()
    shuffle_deck(test_data)
    test_deck = get_cards_in_deck(test_data)

    test_player_one_hand = [card for card in hand_player_cards(test_deck)]
    test_player_two_hand = [card for card in hand_player_cards(test_deck)]

    test_card = get_next_card_in_deck(test_deck)
    count = 0
    for card in test_deck:
        assert card is not test_deck
        count += 1
    assert count == 33

def test_split_hand():
    test_data = load_deck_data()
    shuffle_deck(test_data)
    test_deck = get_cards_in_deck(test_data)

    test_player_one_hand = [card for card in hand_player_cards(test_deck)]

    test_dict = split_hand(test_player_one_hand)
    assert len(test_dict['user_hand']) == 3
    assert len(test_dict['hidden']) == 3
    assert len(test_dict['visible']) == 3


import os, sys
parentddir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
sys.path.append(parentddir)

import torch

from datasets.CIFAR10Dataset import CIFAR10Dataset, CIFAR10_CLASSES
from datasets.NSFWDataset import NSFW_SSDDateset, NSFW_LABELS
from datasets.PascalVOCDataset import PascalVOCDataset, PASCAL_CLASSES
from datasets.PascalVOCDataset_v2 import PascalVOCDataset_v2, PASCAL_CLASSES

def get_cifar10(data_dir, transforms):
    dataset = CIFAR10Dataset(
        data_dir=data_dir,
        transform=transforms
    )

    return dataset, CIFAR10_CLASSES

def get_pascalvoc(data_folder, split, transforms=None):
    dataset = PascalVOCDataset(
        data_folder,
        split,
        keep_difficult=True
    )

    return dataset, PASCAL_CLASSES

def get_pascalvoc_v2(data_folder, split, transforms=None):
    dataset = PascalVOCDataset_v2(
        data_folder,
        split,
        True,
        transforms
    )

    return dataset, PASCAL_CLASSES

def get_nsfw(anno_txt_path, split):
    dataset = NSFW_SSDDateset(
        anno_txt_path,
        split
    )

    return dataset, NSFW_LABELS

def load_dataset(dataset_name, data_dir, split, options, transforms, batch_size, shuffle=False):
    num_workers = options["num_workers"]

    if dataset_name == "cifar10":
        dataset, class_list = get_cifar10(data_dir=data_dir, transforms=transforms)
    elif dataset_name == "pascalvoc":
        dataset, class_list = get_pascalvoc(data_folder=data_dir, split=split)
    elif dataset_name == "pascalvoc_v2":
        dataset, class_list = get_pascalvoc_v2(data_folder=data_dir, split=split, transforms=transforms)
    elif dataset_name == "nsfw":
        dataset, class_list = get_nsfw(anno_txt_path=data_dir, split=split)
    else:
        raise Exception("Wrong Dataset Name... Check config.json")

    dataloader = torch.utils.data.DataLoader(
        dataset,
        batch_size=batch_size,
        shuffle=shuffle,
        collate_fn=dataset.collate_fn,
        num_workers=num_workers,
        pin_memory=True,
        drop_last=False
    )

    return dataloader, class_list


import tensorflow as tf
import numpy as np

def get_model_fn(hparams):
    def model_fn(features, labels, mode):
        # copy input
        mu_sigma_in = tf.reshape(features['mu_sigma'], [-1, hparams['latent_size'] * 2])
        mu_sigma_in = tf.cast(mu_sigma_in, tf.float32)
        mu_in, sigma_in = tf.split(mu_sigma_in, 2, axis=1)
        # get parameters from data
        batch_size = tf.shape(mu_in)[0]
        dropout_bool = hparams['dropout_rate'] is not None and mode != tf.estimator.ModeKeys.PREDICT

        # sample from distribution
        if mode == tf.estimator.ModeKeys.TRAIN:
            z_in = mu_in + sigma_in * tf.random_normal([batch_size, hparams['latent_size']])
        else:
            z_in = mu_in

        # layers of dialogue network
        x = tf.layers.dense(z_in, hparams['hidden_size'], use_bias=False, name='initial_projection')
        for i_layer in range(hparams['num_layers']):
            x = x + tf.layers.dense(x, hparams['hidden_size'], activation=tf.nn.relu, name='layer_%i' % i_layer)
            x = x / np.sqrt(2)
            if dropout_bool:
                x = tf.nn.dropout(x, keep_prob=1 - hparams['dropout_rate'])
        x = tf.layers.dense(x, 2 * hparams['latent_size'], name='final_projection')
        mu, logsigma = tf.split(x, 2, axis=1)
        sigma = tf.exp(logsigma)
        mu_sigma = tf.concat([mu, sigma], axis=1)

        # form predictions list
        if mode == tf.estimator.ModeKeys.PREDICT:
            predictions = {
                'mu_sigma': mu_sigma,
            }
            return tf.estimator.EstimatorSpec(mode=mode,
                    predictions=predictions,
                    export_outputs={'output':tf.estimator.export.PredictOutput(mu_sigma)})

        # Calc answer cross-entropy
        mu_sigma_label = tf.reshape(labels, [-1, hparams['latent_size'] * 2])
        mu_sigma_label = tf.cast(mu_sigma_label, tf.float32)
        mu_label, sigma_label = tf.split(mu_sigma_label, 2, axis=1)
        loss_ce = (
                    tf.reduce_mean(tf.square(sigma_label / (sigma + 1e-8))) +
                    tf.reduce_mean(tf.square((mu - mu_label) / (sigma + 1e-8))) +
                    2 * tf.reduce_mean(logsigma)
            )
        loss_kl = (
                    tf.reduce_mean(tf.square(sigma / (sigma_label + 1e-8))) +
                    tf.reduce_mean(tf.square((mu - mu_label) / (sigma_label + 1e-8))) -
                    2 * tf.reduce_mean(logsigma)
            )
        # Calc summary loss
        loss = loss_kl
        loss = tf.identity(loss, name='loss')

        # Configure the Training Op (for TRAIN mode)
        if mode == tf.estimator.ModeKeys.TRAIN:
            if hparams['adam']:
                optimizer = tf.train.AdamOptimizer(learning_rate=hparams['learning_rate'])
            else:
                optimizer = tf.train.GradientDescentOptimizer(learning_rate=hparams['learning_rate'])
            train_op = optimizer.minimize(
                    loss=loss,
                    global_step=tf.train.get_global_step())
            return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

        # Add evaluation metrics (for EVAL mode)
        eval_metric_ops = {}
        eval_metric_ops["accuracy_in_1_sigma"] = tf.metrics.accuracy(
                        labels=tf.ones([batch_size], dtype=tf.int32),
                        predictions=tf.cast(tf.reduce_prod(
                                (tf.sign(1 - tf.square((mu - mu_label) / sigma_label)) + 1) / 2,
                                                    axis=1), tf.int32)
        )
        return tf.estimator.EstimatorSpec(
                mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
    return model_fn


import numpy as np
import matplotlib.pyplot as plt

'''
def Int(dX):
    n = len(dX)
    X = [0 for i in range(n+1)]
    for i in range(n+1):
        X[i+1] = X[i] + dX[i]
    return X
'''
# constates de "resolución"
h = 0.001
r = 10

#  constantes "conocidas"
E = 0.0002
B = 0.3
C = 10.0
mu = 3
F = 0

f = lambda t_1, lbd: E*t_1*lbd**6 - B*(lbd-lbd**(-5))
g = lambda t_1: t_1**(-1/2)
#print(f(4.,5.))
#print(g(1/4))

D = [0 for j in range(r)]
for j in range(2,r):
    lbd_1 = np.power(j, 1/3)
    print(lbd_1)

    # Euler to find $T_1(\lambda)$
    LBD = np.arange(lbd_1, 1, -h)
    n = len(LBD)
    LBD = np.append(LBD, [1.0])
    #print(LBD)
    dLBD = [LBD[i+1]-LBD[i] for i in range(n)]
    dT_1 = [0 for i in range(n)]
    T_1 = [0 for i in range(n+1)]
    T_1[0] = 0
    for i in range(n):
        dT_1[i] = f(T_1[i], LBD[i]) * dLBD[i]
        T_1[i+1] = T_1[i] + dT_1[i]
    #dT_1[n-1] = f(T_1[n-1], LBD[n-1])*dLBD[n-1]
    '''
    plt.plot(LBD, T_1, label="$T_1(\lambda)$")
    plt.xlabel("$\lambda$")
    plt.ylabel("$T_1$")
    plt.title("First plot")
    plt.legend()
    plt.savefig("T_1-lbd.png")
    plt.show()
    '''

    # Euler to find $t(\lambda)$
    T = [0 for i in range(n+1)]
    dT = [0 for i in range(n)]
    dT[0] = h
    T[1] = T[0] + dT[0]
    for i in range(1,n):
        dT[i] = -g(T_1[i])*dLBD[i]
        T[i+1] = T[i] + dT[i]
    #dT[n-1] = g(T_1[n-1])*dLBD[n-1]

    plt.plot(T, LBD, label="$\lambda(t)$")
    plt.xlabel("$t$")
    plt.ylabel("$\lambda$")
    plt.title("Desinflado de un globo para $\lambda_1=$" + str(lbd_1))
    plt.legend()
    plt.savefig("lbd-t.png")
    plt.show()

    # Integrate to find $v(t)$
    A = [C * LBD[i]**4 * T_1[i] for i in range(n)]
    dV = [0 for i in range(n)]
    V = [0 for i in range(n+1)]
    for i in range(n):
        dV[i] = (A[i] - mu*V[i])*dT[i]
        V[i+1] = V[i] + dV[i]
    #T.append(T[n-1] + dT[n-1])
    i = n
    F = mu*V[n]
    while V[i]>0:
        #print(V[i])
        dT.append(h)
        T.append(T[i] + dT[i])
        dV.append(-(mu*V[i]+F)*dT[i]/2)
        V.append(V[i] + dV[i])
        i += 1
    dT.append(h)
    '''
    plt.plot(T, V, label="$v(t)$")
    plt.xlabel("$t$")
    plt.ylabel("$v$")
    plt.title("Third plot")
    plt.legend()
    plt.savefig("v-t.png")
    plt.show()
    '''

    # Integrate to find $x(t)$
    m = len(T)
    dX = [0 for i in range(m)]
    X = [0 for i in range(m+1)]
    #print(m, len(dT))
    for i in range(m):
        dX[i] = V[i]*dT[i]
        X[i+1] = X[i] + dX[i]
    T.append(T[m-1] + dT[m-1])
    '''
    plt.plot(T, X, label="$x(t)$")
    plt.xlabel("$t$")
    plt.ylabel("$x$")
    plt.title("Gráfica del movimiento teóirico del coche")
    plt.legend()
    plt.savefig("x-t.png")
    plt.show()
    '''

    D[j] = X[m]
    print(D[j])

plt.plot(D[1:], label="$d(V)$")
plt.xlabel("$n$")
plt.ylabel("$d$")
plt.title("Predicción teórica de los resultados experimentales")
plt.legend()
plt.savefig("d-n.png")
plt.show()


import numpy as np
from copy import copy
import datetime
from pprint import pprint

class WordLetter:
    def __init__(self, symbol):
        self.symbol = symbol
        self.next_letter: WordLetter = None

class Word:
    def __init__(self, initial_sequence: str, transformation_rules: dict):
        self.letters = WordLetter(initial_sequence[0])
        self.transformation_rules = transformation_rules
        working_letter = self.letters
        for _ in initial_sequence[1:]:
            working_letter.next_letter = WordLetter(_)
            working_letter = working_letter.next_letter
        self.initial_letters = copy(self.letters)

    def perform_transformation(self):
        working_letter = self.letters
        while working_letter.next_letter:
            letter_pair = working_letter.symbol + working_letter.next_letter.symbol
            in_letter = self.transformation_rules.get(letter_pair)
            if in_letter:
                insertion = WordLetter(in_letter)
                insertion.next_letter = working_letter.next_letter
                working_letter.next_letter = insertion
                working_letter = insertion.next_letter
            else:
                working_letter = working_letter.next_letter

    def do_transformation_series(self, n):
        for i in range(n):
            self.perform_transformation()
            print(i, self.get_statistics())

    def get_statistics(self):
        statistics_dict = {}
        working_letter = self.letters
        while True:
            if working_letter.symbol in statistics_dict:
                statistics_dict[working_letter.symbol] += 1
            else:
                statistics_dict[working_letter.symbol] = 1
            working_letter = working_letter.next_letter
            if working_letter is None:
                break
        return statistics_dict

def day_runner(is_test: bool = False):
    # ====== preparing input ======
    file_name = 'testinput' if is_test else 'inputtxt'
    with open(file_name) as file:
        lines = file.read().splitlines()

    initial_sequence = lines[0]
    transformation_rules = dict(k_v.split(" -> ") for k_v in lines[2:] if k_v)

    task_1(initial_sequence, transformation_rules, is_test)
    task_2(initial_sequence, transformation_rules, is_test)

def task_1(initial_sequence, transformation_rules, is_test):
    test_output = 1588
    word = Word(initial_sequence=initial_sequence, transformation_rules=transformation_rules)
    word.do_transformation_series(10)
    statistics = word.get_statistics()
    min_val = min(statistics.values())
    max_val = max(statistics.values())

    task_output = max_val - min_val
    if is_test:
        is_passed = task_output == test_output
        print(f'Task_1: {"GC! ^_^:" if is_passed else "Sorry :(" } the result {task_output} {"=" if is_passed else "!="} {test_output} (expected)')
    else:
        print(f'Task_1: The result is {task_output}')

def task_2(initial_sequence, transformation_rules, is_test):
    test_output = 2188189693529
    word = Word(initial_sequence=initial_sequence, transformation_rules=transformation_rules)
    word.do_transformation_series(40)
    statistics = word.get_statistics()
    min_val = min(statistics.values())
    max_val = max(statistics.values())

    task_output = max_val - min_val
    if is_test:
        is_passed = task_output == test_output
        print(f'Task_1: {"GC! ^_^:" if is_passed else "Sorry :(" } the result {task_output} {"=" if is_passed else "!="} {test_output} (expected)')
    else:
        print(f'Task_1: The result is {task_output}')

def main():
    day_runner(is_test=True)
    day_runner()

if __name__ == '__main__':
    main()


import numpy as np

def checkContinous(df, duration=5):
    discontinousFrameIdx = []
    previousFrame = 0
    frameList = df['frame'].tolist()
    groupIdx = []
    startGroupIdx = -1

    for i in range(len(df)):
        currentFrame = frameList[i]
        if currentFrame == 0:
            previousFrame = 0
            startGroupIdx += 1
            groupIdx.append(startGroupIdx)
            continue
        elif i < (len(df) - 1) and currentFrame != (previousFrame + duration) and currentFrame != (
                frameList[i + 1] - duration):
            discontinousFrameIdx.append(i)
            previousFrame = frameList[i + 1]
            startGroupIdx += 1
            continue
        elif i == (len(df) - 1) and currentFrame != (previousFrame + duration):
            discontinousFrameIdx.append(i)
            continue

        previousFrame = currentFrame
        groupIdx.append(startGroupIdx)

    df = df.drop(discontinousFrameIdx)

    df["groupIdx"] = groupIdx
    startFrame = df.groupby("groupIdx")["frame"].min()
    df["startFrame"] = 0
    groupIdx = np.array(groupIdx)
    for i in df["groupIdx"].unique().tolist():
        df.at[groupIdx == i, "startFrame"] = startFrame[i]

    return df


'''from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()'''
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import tweepy
from textblob import TextBlob
ck='yO3xKe4YCkGv2Tu3W8rmjJWOX'
cs='JWuDVy8A1f7TITyK5gZoguYNvAS7xJgJ4xQUFclAAp2E6Q28O1'

at='979590528483258368-heb1Vj5r7sjOW1rMGPXrEVEtK7I8Frk'
ats='2XCyn0gOFMxxINO9IDrr6PDgznqUEN8AGCIlxHSZKhOtt'

auth=tweepy.OAuthHandler(ck,cs)
auth.set_access_token(at,ats)

api=tweepy.API(auth)

public_tweets=api.search('IPL')
p=[]
s=[]
t=[]
for tweet in public_tweets:
    t.append(pd.to_datetime(tweet._json['created_at']))
    analysis=TextBlob(tweet.text)
    #print(type(analysis.sentiment))
    p.append(analysis.sentiment[0])
    s.append(analysis.sentiment[1])
d={'polarity':p,'subjectivity':s,'time':t}
df=pd.DataFrame(data=d)
df = df.sort_values('time', ascending=True)
ax = df[['subjectivity','polarity']].plot(kind='bar',title='IPL:Twitter Sentimental Analysis')
ax.set_xlabel('Tweets', fontsize=12)
ax.set_ylabel('polarity/subjectivity', fontsize=12)
plt.show()
plt.plot(df['time'], df['polarity'])
plt.xlabel('date')
plt.ylabel('polarity')
plt.title('IPL:Twitter Sentimental Analysis')
plt.show()


import os
from Utility.Utility import Utility

class KeyService(object):
    @staticmethod
    def find_start(keyset):
        l = os.listdir(keyset.get_key_path())
        l.sort()
        if len(l) > 0:
            start = int(Utility.find_num(l[-1])) + 1
        else:
            start = 1
        keyset.__start = start


import paho.mqtt.client as paho
import time
import math
mqttc = paho.Client()

host = "localhost"
topic = "BBCAR_LOG"

LOG = []
tmp_log = ""
log_num = 0
def on_connect(self, mosq, obj, rc):
      print("Connected rc: " + str(rc))
def on_message(mosq, obj, msg):
    global tmp_log,log_num,LOG
    print("[Received] Topicddd: " + msg.topic + ", Message: " + str(msg.payload,encoding = "utf-8") + "\n")
    tmp_log = str(msg.payload,encoding = "utf-8")
    if tmp_log=="end":   #ending to disconnect
        print("disconnect")
        mqttc.disconnect()
    else:
        LOG.append(tmp_log)
        log_num = log_num+1

def on_subscribe(mosq, obj, mid, granted_qos):
      print("Subscribed OK")

def on_unsubscribe(mosq, obj, mid, granted_qos):
      print("Unsubscribed OK")

mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_unsubscribe = on_unsubscribe

print("Connecting to " + host + "/" + topic)
mqttc.connect(host, port=1883, keepalive=60)
mqttc.subscribe(topic, 0)

mqttc.loop_forever()

for i in range (0,log_num):  #PRINT TIME AND VELOCITY
    print(LOG[i])

filename = "BBCAR_LOG"+"_"+str(time.strftime("%Y%m%d%H%M%S"))+".txt"
with open(filename, "w",errors = 'ignore') as f:
    for line in LOG:
        f.write(line+"\n")
print("Save file in", filename)


def mergeSort(arr):
    if len(arr) < 2:
        return arr

    mid = len(arr) // 2
    front_arr = mergeSort(arr[mid:])
    back_arr = mergeSort(arr[:mid])

    merged_arr = []
    h, l = 0, 0
    while l < len(front_arr) and h < len(back_arr):
        if front_arr[l] < back_arr[h]:
            merged_arr.append(front_arr[l])
            l += 1
        else:
            merged_arr.append(back_arr[h])
            h += 1
    merged_arr += front_arr[l:]
    merged_arr += back_arr[h:]
    return merged_arr

print(mergeSort([6,1,2,3,9,4,1]))


import argparse,json,os,time,random,shutil,torch,pytorch_lightning as pl,torch.nn as nn,torch.nn.functional as F
from torch.utils.data import DataLoader
from tqdm import tqdm
from pytorch_lightning import seed_everything
from transformers import AdamW, get_linear_schedule_with_warmup
from transformers import BertModel,BertTokenizerFast
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
os.environ["TOKENIZERS_PARALLELISM"] = "true"

from util.data import SADataset,collate_fn,label2id,id2label
from util.inference import getQuadruple,from_token_span_idx_to_word_char_idx
from util.model import *
from util.evaluate_single_dataset import evaluate_single_dataset

def init_args():
    parser = argparse.ArgumentParser()
    # bert-base-multilingual-uncased
    parser.add_argument('--model',default='bert-base-multilingual-uncased',type=str)
    # CLN+BiAtt / CLN+CNN / CLN+CNN2 / CLN / Init: SELU + meanpooling / NOP: Init without meanpooling
    parser.add_argument('--model_type',default='CLN+BiAtt',type=str)
    # mpqa ds ca eu norec
    parser.add_argument('--dataset',default='eu',type=str)
    parser.add_argument('--dataset_path',default='../Dataset/v1',type=str)
    parser.add_argument("--train_epochs", default=40, type=int)
    parser.add_argument('--batch_size',default=4,type=int)
    parser.add_argument('--gpu',default='0,',type=str)
    parser.add_argument('--use_fp16',action='store_true', default=True)

    parser.add_argument('--seed', default=33, type=int)
    parser.add_argument("--learning_rate", default=2e-3, type=float)
    parser.add_argument("--do_train", action='store_true', default=True)
    parser.add_argument("--do_eval", action='store_true', default=False)
    parser.add_argument("--do_statistics", action='store_true', default=False)

    parser.add_argument("--is_biaffine", default=1, type=int)

    # eval
    parser.add_argument('--eval_model_dir', default='',type=str)
    parser.add_argument('--trainer_logger_version',default=5,type=int)

    parser.add_argument('--eval_batch_size',default=20,type=int)
    parser.add_argument('--test_batch_size',default=8,type=int)
    parser.add_argument("--max_len", default=150, type=int)
    parser.add_argument('--cache',default='/home/hyc/res/cache',type=str)
    parser.add_argument('--gradient_accumulation_steps', type=int, default=2)
    parser.add_argument("--weight_decay", default=0.0, type=float)
    parser.add_argument("--warmup", default=0.1, type=float)
    parser.add_argument("--adam_epsilon", default=1e-8, type=float)

    # model
    parser.add_argument("--lstm_layer_num", default=4, type=int)
    parser.add_argument("--r1_hid_size", default=768, type=int)
    parser.add_argument("--r2_hid_size", default=0, type=int)
    parser.add_argument("--label_num", default=11, type=int)
    parser.add_argument("--dist_emb_size", default=70, type=int)
    parser.add_argument("--reg_emb_size", default=30, type=int)
    parser.add_argument("--bert_hid_size", default=768, type=int)
    parser.add_argument("--conv_hid_size", default=128, type=int)
    parser.add_argument("--biaffine_size", default=512, type=int)
    parser.add_argument("--ffnn_hid_size", default=384, type=int)

    parser.add_argument("--emb_dropout", default=0.5, type=float)
    parser.add_argument("--conv_dropout", default=0.5, type=float)
    parser.add_argument("--out_dropout", default=0.3, type=float)

     # for BiAxialAttention
    parser.add_argument("--num_head", default=2, type=int)

    # for boundary
    parser.add_argument("--D", default=1, type=int)
    parser.add_argument("--eps", default=0.2, type=float)

    # for cnn2
    parser.add_argument("--cnn_kernel_size", default=3, type=int)
    parser.add_argument("--cnn_depth", default=3, type=int)
    parser.add_argument("--cnn_groups", default=1, type=int) # 1:1 or 2:2 or 0:input_dim

    # for prodictor
    parser.add_argument("--td", default=0.7, type=float)

    args = parser.parse_args()

    args_model = args.model.replace('\\','-') if '\\' in args.model else args.model
    args.output_dir = f'./outputs/{args.dataset}/{args.model_type}-{args.is_biaffine}'
    os.makedirs(args.output_dir,exist_ok=True)

    args.is_multi_gpu = True if args.gpu.count(',')>1 else False
    args.is_multi_language = True if 'm' in args_model else False

    args.dilation = [1,2,3]
    args.weight = [1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4]
    return args

args = init_args()

def get_dataset(tokenizer,type,args):
    data_dir = f'{args.dataset_path}/{args.dataset}/{type}.json'
    return SADataset(tokenizer,data_dir,type,args)

class TableModel(pl.LightningModule):
    def __init__(self,args):
        super().__init__()
        self.args = args
        self.save_hyperparameters()

        self.bert = BertModel.from_pretrained(args.model, cache_dir=args.cache)
        self.tokenizer = BertTokenizerFast.from_pretrained(args.model, cache_dir=args.cache)

        self.dis_embs = nn.Embedding(20, args.dist_emb_size)
        self.reg_embs = nn.Embedding(3, args.reg_emb_size)

        self.lstm = nn.LSTM(args.bert_hid_size, args.bert_hid_size // 2, num_layers=args.lstm_layer_num, batch_first=True, bidirectional=True)

        conv_input_size = args.bert_hid_size + args.dist_emb_size + args.reg_emb_size

        if 'CNN' in args.model_type:
            self.convLayer = ConvolutionLayer(conv_input_size, args.conv_hid_size, args.dilation, args.conv_dropout)
            if args.cnn_groups == 0:
                self.convLayer2 = ConvolutionLayer2(conv_input_size, conv_input_size, args.cnn_kernel_size, args.cnn_depth, conv_input_size)
            else:
                self.convLayer2 = ConvolutionLayer2(conv_input_size, conv_input_size, args.cnn_kernel_size, args.cnn_depth, args.cnn_groups)

        self.dropout = nn.Dropout(args.emb_dropout)
        self.predictor = CoPredictor(args.label_num, args.bert_hid_size, args.biaffine_size,
                                     args.conv_hid_size * len(args.dilation), args.ffnn_hid_size,
                                     args.out_dropout)

        self.biaxialAtt = BiAxialAttention(args.num_head, args.bert_hid_size)

        self.cln = LayerNorm(args.bert_hid_size, args.bert_hid_size, conditional=True)

        self.rep1 = Rep_SELU(args.bert_hid_size,args.r1_hid_size)

        if args.model_type == 'Init':
            predict_hid_size = 5 * args.r1_hid_size + args.r2_hid_size + args.dist_emb_size + args.reg_emb_size
        elif args.model_type == 'CLN+CNN':
            predict_hid_size = args.conv_hid_size * len(args.dilation)
        elif args.model_type == 'CLN+BiAtt':
            predict_hid_size = 3 * args.bert_hid_size + args.dist_emb_size + args.reg_emb_size
        else :
            predict_hid_size =  args.r1_hid_size + args.r2_hid_size + args.dist_emb_size + args.reg_emb_size

        self.predictor2 = Predictor(args.label_num,
                                    args.bert_hid_size, args.ffnn_hid_size,
                                    predict_hid_size, args.biaffine_size)

        self.layernorm1 = nn.LayerNorm(args.r1_hid_size)

    def average_reps(self, d, bert_reps):
        final_reps = []
        for idx,(sent, bert_rep) in enumerate(zip(d['sentence'], bert_reps)):
            len_subtokens = max(d['sentence_len']) - d['sentence_len'][idx] + d['token_len'][idx]
            sent_reps = []
            sub_reps = []
            i = 0
            k = 0
            tokens = sent.split(' ')
            for j, rep in enumerate(bert_rep[:len_subtokens]):
                sub_reps.append(rep)

                len_subsubtokens = 0
                if i<len(d['token_range'][idx]):
                    len_subsubtokens = d['token_range'][idx][i][1]-d['token_range'][idx][i][0] + 1
                else:
                    len_subsubtokens = 1

                if k == len_subsubtokens - 1:
                    ave_rep = torch.stack(sub_reps, dim=0).mean(0)
                    sent_reps.append(ave_rep)
                    sub_reps = []
                    i += 1
                    k = 0
                else:
                    k += 1
            sent_reps = torch.stack(sent_reps, dim=0)
            final_reps.append(sent_reps)

        final_reps = torch.stack(final_reps, dim=0)

        return final_reps

    def forward(self, d):

        word_reps = self.bert(input_ids=d['source_ids'], attention_mask=d['source_mask']).last_hidden_state
        word_reps = word_reps[:,1:-1,:] # remove CLS/SEQ rep
        word_reps = self.average_reps(d,word_reps)
        packed_embs = pack_padded_sequence(word_reps, d['sentence_len'], batch_first=True, enforce_sorted=False)
        packed_outs, (hidden, _) = self.lstm(packed_embs)
        word_reps, _ = pad_packed_sequence(packed_outs, batch_first=True, total_length=max(d['sentence_len']))

        if args.model_type == 'Init' or  args.model_type == 'NOP':
            rep1 = self.rep1(word_reps)
            rep1 = self.layernorm1(rep1)
        elif 'CLN' in args.model_type:
            rep1 = self.cln(word_reps.unsqueeze(2), word_reps)

        if args.model_type == 'Init':
            rep_row_1 = torch.mean(rep1,dim=2,keepdim=True).expand_as(rep1)
            rep_col_1 = torch.mean(rep1,dim=1,keepdim=True).expand_as(rep1)

            rep2 = rep1.permute((0,2,1,3))
            rep_row_2 = torch.mean(rep2,dim=2,keepdim=True).expand_as(rep1)
            rep_col_2 = torch.mean(rep2,dim=1,keepdim=True).expand_as(rep1)

        dis_emb = self.dis_embs(d['dis_inputs'])
        reg_emb = self.reg_embs(d['reg_inputs'])

        if args.model_type == 'Init':
            inputs = torch.cat([rep1, dis_emb, reg_emb, rep_row_1, rep_row_2, rep_col_1, rep_col_2], dim=-1)
        elif args.model_type == 'CLN+BiAtt':
            inputs = rep1
        else:
            inputs = torch.cat([rep1, dis_emb, reg_emb], dim=-1)

        if args.model_type == 'CLN+CNN':
            conv_inputs = torch.masked_fill(inputs, d['reg_inputs'].eq(0).unsqueeze(-1), 0.0)
            conv_outputs = self.convLayer(conv_inputs)
            inputs = torch.masked_fill(conv_outputs, d['reg_inputs'].eq(0).unsqueeze(-1), 0.0)
        elif args.model_type == 'CLN+CNN2':
            conv_inputs = torch.masked_fill(inputs, d['reg_inputs'].eq(0).unsqueeze(-1), 0.0)
            conv_outputs = self.convLayer2(conv_inputs)
            inputs = torch.masked_fill(conv_outputs, d['reg_inputs'].eq(0).unsqueeze(-1), 0.0)
        elif args.model_type == 'CLN+BiAtt':
            inputs = torch.masked_fill(inputs, d['reg_inputs'].eq(0).unsqueeze(-1), 0.0)
            inputs = self.biaxialAtt(inputs)
            inputs = torch.masked_fill(inputs, d['reg_inputs'].eq(0).unsqueeze(-1), 0.0)
            inputs = torch.cat([inputs, dis_emb, reg_emb], dim=-1)

        outputs = self.predictor2(word_reps,inputs,is_biaffine = (args.is_biaffine==1), td=args.td)

        return outputs

    def _step(self, batch):

        outputs = self(batch)

        labels = batch["label"]

        weight = torch.tensor(args.weight).float().to(outputs.device)

        loss = F.cross_entropy(outputs.reshape([-1,outputs.shape[-1]]),labels.reshape([-1]),weight=weight)

        return loss

    def on_fit_start(self):
        seed_everything(args.seed,workers=True)

    def training_step(self, batch, batch_idx):
        loss = self._step(batch)
        self.log("train_loss", loss, on_step=True, on_epoch=True, prog_bar=True,
                 sync_dist = self.args.is_multi_gpu, batch_size=args.batch_size)
        return {"loss": loss}

    def training_epoch_end(self, outputs):
        avg_train_loss = torch.stack([x["loss"] for x in outputs]).mean()
        self.log("avg_train_loss", avg_train_loss,
                 sync_dist = self.args.is_multi_gpu, batch_size=args.batch_size)

    def validation_step(self, batch, batch_idx):
        return 0

    def validation_epoch_end(self, outputs):

        dev_res = test(self,args.logger_version,'dev')
        dev_f1 = dev_res['sentiment_tuple/f1']
        self.log("dev_f1", dev_f1,
                  sync_dist = args.is_multi_gpu, batch_size=args.batch_size)

        with open(f"{args.output_dir}/lightning_logs/version_{args.logger_version}/result.txt",'a+') as f:
            f.write(f'{self.trainer.current_epoch}-dev_res:{dev_res} \n')

    def configure_optimizers(self):

        bert_params = set(self.bert.parameters())
        other_params = list(set(self.parameters()) - bert_params)

        for param in self.bert.parameters(): # frozen BERT params
            param.requires_grad = False

        params = [
            {'params': other_params,
             'lr': args.learning_rate,
             'weight_decay': args.weight_decay},
        ]

        optimizer = AdamW(params, lr=self.args.learning_rate, eps=self.args.adam_epsilon)

        t_total = ((len(self.train_dataset) // (self.args.batch_size))
                   // self.args.gradient_accumulation_steps
                   * float(self.args.train_epochs))
        scheduler = get_linear_schedule_with_warmup(
            optimizer,
            num_warmup_steps = self.args.warmup * t_total,
            num_training_steps=t_total,
        )
        scheduler = {"scheduler": scheduler, "interval": "step", "frequency": 1}
        return [optimizer],[scheduler]

    def get_tqdm_dict(self):
        tqdm_dict = {"loss": "{:.4f}".format(self.trainer.avg_loss), "lr": self.lr_scheduler.get_last_lr()[-1]}
        return tqdm_dict

    def setup(self, stage=None):
        self.train_dataset = get_dataset(self.tokenizer, "train",self.args)
        self.val_dataset = get_dataset(self.tokenizer,"dev",self.args)
        self.test_dataset = get_dataset(self.tokenizer,"test",self.args)

    def train_dataloader(self):
        return DataLoader(self.train_dataset, batch_size=self.args.batch_size, collate_fn=collate_fn,
                          drop_last=False, shuffle=True, num_workers=4, pin_memory=True)

    def val_dataloader(self):
        return DataLoader(self.val_dataset, batch_size=self.args.eval_batch_size, collate_fn=collate_fn,
                          drop_last=False, shuffle=False, num_workers=4, pin_memory=True)

def check_available(d):
    if len(d['Polar_expression'][0])>0 and len(d['Source'][0])==0 and len(d['Target'][0])==0 and args.dataset not in ['eu','ca','norec']:
        return False
    return True

def test(module,trainer_logger_version,type,load_model_dir=None):
    device = f"cuda:{args.gpu.split(',')[0]}"

    if load_model_dir!=None:
        module = module.load_from_checkpoint(load_model_dir,args=args)
        module.to(device)

    module.eval()

    tokenizer = module.tokenizer
    test_dataset = get_dataset(tokenizer,type,args)
    data_loader = DataLoader(test_dataset, batch_size=args.test_batch_size, num_workers=4,collate_fn=collate_fn, pin_memory=True)

    text_list,result_json=[],[]

    for i,batch in enumerate(data_loader):
        for k in batch.keys():
            if torch.is_tensor(batch[k]):
                batch[k] = batch[k].to(device)

        with torch.no_grad():
            outs = module(batch)

        preds = torch.max(outs,dim=-1)[1] # b*n*n
        for j in range(preds.shape[0]):

            token_len = batch['sentence_len'][j]
            preds_j = preds[j,:token_len,:token_len]
            e_res = getQuadruple(preds_j,args)

            result_ = {}
            result_['sent_id'] = batch['text'][j][0]
            result_['text'] = batch['text'][j][1]
            result_['opinions'] = []

            d = {}
            for ei in e_res:
                d['Source'],d['Target'],d['Polar_expression'],d['Polarity']=[[],[]],[[],[]],[[],[]],'Neutral'
                for h_span in ei['h']:
                    idx_l,idx_r = h_span
                    l,r = batch['token_offset'][j][idx_l][0], batch['token_offset'][j][idx_r][1]
                    # l,r = from_token_span_idx_to_word_char_idx(batch['token_range'][j],batch['token_offset'][j],h_span)
                    d['Source'][0].append(batch['text'][j][1][l:r])
                    d['Source'][1].append(f'{l}:{r}')
                for t_span in ei['t']:
                    idx_l,idx_r = t_span
                    l,r = batch['token_offset'][j][idx_l][0], batch['token_offset'][j][idx_r][1]
                    # l,r = from_token_span_idx_to_word_char_idx(batch['token_range'][j],batch['token_offset'][j],t_span)
                    d['Target'][0].append(batch['text'][j][1][l:r])
                    d['Target'][1].append(f'{l}:{r}')
                for e_span in ei['e']:
                    idx_l,idx_r = e_span
                    l,r = batch['token_offset'][j][idx_l][0], batch['token_offset'][j][idx_r][1]
                    # l,r = from_token_span_idx_to_word_char_idx(batch['token_range'][j],batch['token_offset'][j],e_span)
                    d['Polar_expression'][0].append(batch['text'][j][1][l:r])
                    d['Polar_expression'][1].append(f'{l}:{r}')

                if 'POS' in ei['p']:
                    d['Polarity'] = 'Positive'
                elif 'NEG' in ei['p']:
                    d['Polarity'] = 'Negative'

                if check_available(d):
                    result_['opinions'].append(d)

                d = {}

            result_json.append(result_)

        text_list.extend(batch['text'])

    time_now_str = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time()))
    with open(f"{args.output_dir}/lightning_logs/version_{trainer_logger_version}/predict-{time_now_str}-{type}.json","w") as f:
        json.dump(result_json,f)

    result = evaluate_single_dataset(
        gold_file = f"{args.dataset_path}/{args.dataset}/{type}.json",
        pred_file = f"{args.output_dir}/lightning_logs/version_{trainer_logger_version}/predict-{time_now_str}-{type}.json"
    )
    f1 = result['sentiment_tuple/f1']

    try:
        os.rename(f"{args.output_dir}/lightning_logs/version_{trainer_logger_version}/predict-{time_now_str}-{type}.json",f"{args.output_dir}/lightning_logs/version_{trainer_logger_version}/{module.trainer.current_epoch}-predict-{type}-{f1:.5f}.json")
    except:
        os.rename(f"{args.output_dir}/lightning_logs/version_{trainer_logger_version}/predict-{time_now_str}-{type}.json",f"{args.output_dir}/lightning_logs/version_{trainer_logger_version}/predict-{time_now_str}-{type}-{f1:.5f}.json")

    return result

if __name__ == '__main__':

    if args.do_train:
        print("\n****** Conduct Training ******")

        checkpoint_callback = pl.callbacks.ModelCheckpoint(
            monitor='dev_f1', mode='max', save_top_k=5,
            filename='{epoch}-{test_f1:.5f}',
        )

        train_params = dict(
            default_root_dir=args.output_dir,
            accumulate_grad_batches=args.gradient_accumulation_steps,
            accelerator='gpu',
            devices=args.gpu,
            gradient_clip_val=1.0,
            max_epochs=args.train_epochs,
            callbacks=[checkpoint_callback],
            strategy = "ddp" if args.is_multi_gpu else None,
            precision=16 if args.use_fp16 else 32,
            num_sanity_val_steps=0,
            deterministic=True
            # logger=[], # mulit gpu need
        )
        trainer = pl.Trainer(**train_params)

        args.logger_version = trainer.logger.version
        print(args)
        model = TableModel(args)

        seed_everything(args.seed,workers=True)

        trainer.fit(model)

    if args.do_eval:
        print("\n****** Conduct Testing ******")
        trainer_logger_version = args.trainer_logger_version
        if args.do_train:
            trainer_logger_version = trainer.logger.version
            del trainer
            del model
            g = os.walk(f'{args.output_dir}/lightning_logs/version_{trainer_logger_version}/checkpoints')
            dif,min_key = {},1e5
            for path, dir_list, file_list in g:
                for file_name in file_list:
                    if '.ckpt' in file_name:
                        key_loss = float(file_name[-10:-5])
                        dif[key_loss] = file_name
                        min_key = min(min_key,key_loss)
            load_model_dir = f'{args.output_dir}/lightning_logs/version_{trainer_logger_version}/checkpoints/{dif[min_key]}'
        else:
            load_model_dir = args.eval_model_dir
        print(f'load_model_dir:{load_model_dir}')

        module = TableModel(args)
        result = test(module,trainer_logger_version,'test',load_model_dir)

        if args.do_train:
            load_model_dir = f'{args.output_dir}/lightning_logs/version_{trainer_logger_version}/epoch_last.ckpt'
            test(module,trainer_logger_version,'test',load_model_dir)


#Map function

from functools import reduce

numbers = [1,2,3,4,5]
# def square(x):
#     return x**2

# numbers_squared = map(square,numbers)
# print(list(numbers_squared))

# numbers_squared2 = map(lambda x : x **2,numbers)
# print(list(numbers_squared2))

numbers_str = ["1","2","3","4","5"]
# numbers_int = map(int,numbers_str)
# print(list(numbers_int))

names = ["Cengiz Han","Zeynep","Pervin"]

# def change_to_upper(name):
#     return name.upper()

# names_upper_cased=map(change_to_upper,names)
# print(list(names_upper_cased))

# names_upper_cased2 = map(lambda name: name.upper(),names)
# print(list(names_upper_cased2))

#filter function

def is_even (num):
    if num %2==0:
        return True
    return False

even_numbers = filter(is_even,numbers)
print(list(even_numbers))

def is_odd(num):
    if num %2 != 0:
        return True
    return False

odd_numbers = filter(is_odd,numbers)
print(list(odd_numbers))

def is_name_long(name):
    if len(name)>7:
        return True
    return False

long_names = filter(is_name_long,names)
print(list(long_names))

#reduce function

def add_two_numbers(x,y):
    return int(x) + int(y)

total = reduce(add_two_numbers,numbers_str)
print(total)


from error_handling import *
from Inputing_Data import dictStudent, dictSubject, dictPrograms
from admin_menu import adminMenu
from student_menu import studentMenu

if __name__ == "__main__":

	while True:
		print("-----------------------------------------")
		print("Welcome to the J.I.K enrolment system!")
		print("1 - Student Login") # student and system seemed too similar so i changed one to admin, feel free to change it again tho - keely
		print("2 - Admin Login ")
		print("3 - Exit ")
		main_opt = input("Press the respective key to navigate:")

		if not checkValidOptionNumb(main_opt, 3):  # if input not valid, reprint menu
			continue

		if main_opt == '3': # exit
			print("-----------------------------------------")
			print("Are you sure you want to exit? ")
			exit_inpt = input("(key 'y' for yes/ any other key for no):")
			if exit_inpt == "y": # if anything else, will naturally just continue while loop (reprint menu)
				print("-----------------------------------------")
				print('Goodbye.')
				break

		if main_opt == '1': # student menu
			studentMenu()

		if main_opt == '2': # admin menu
			adminMenu()


'''
Module to communicate with Tektronix TDS3014b oscilloscope using ethernet.

See example_tektronix.py for example usage.

Modified from module developed by Joe Gregorio, (C) 2006 MIT:
ftp://sprite.ssl.berkeley.edu/pub/sharris/MAVEN_LPW_Preamp/109_TDS3014B_control/tds3014b.py

@author: Jami L Johnson
Created May 27, 2014
'''
from __future__ import print_function, absolute_import

from . import httplib2
import urllib
import os
import struct
import datetime
from obspy import read, Trace, UTCDateTime
from obspy.core.trace import Stats

try:
    import numpy as np

except ImportError:
    pass

class Error(Exception): pass
class connection_error(Error): pass

class TDS3014b:
    def __init__(self, ip_addr):
        self.ip_addr = ip_addr
        self.base_url = "http://%s" % (ip_addr)
        self.h = httplib2.Http()

    def gpibCMD(self, cmd):
        """
        Send a command to the scope and gets the response, if any.
        """
        if(cmd.startswith('*')):
            prefix = ''
        else:
            prefix = ':'

        cmd_url = "?COMMAND=%s%s" % (prefix, urllib.quote_plus(cmd))
        url = '/'.join([self.base_url, cmd_url])
        f = urllib.urlopen(url)
        content = f.read()
        f.close()
        return content

    def getScreen(self):
        """
        :returns: a screen shot in PNG format.
        """
        url = '/'.join([self.base_url, 'Image.png'])
        try:
            f = urllib.urlopen(url)
        except IOError as e:
            raise connection_error(e)
        content = f.read()
        f.close()

        return content

    def forceTrigger(self):
        """
        Forces scope to trigger
        """
        self.gpibCMD('FPANEL:PRESS FORCETRIG')

    def parseISF(self, isf_data, header_only=None, convert=None):
        """
        Determine starting point of data.
        Header has something like 'CURVE #520000', where ascii '5' is the
        length of the length field '20000'.

        ::

            'CURVE #520000xxxx...'
              ^      ^^    ^
              |      ||    +---- data_loc:    location of first valid byte of data
              |      |+--------- len_loc:     location of first byte of data length field
              |      +---------- len_len_loc: location of length of length
              +----------------- tag_loc:     location of start tag
        """
        start_tag = 'CURVE #'
        tag_loc = int(isf_data.find(start_tag))
        len_len_loc = tag_loc + len(start_tag)
        len_loc = len_len_loc + 1
        len_len = int(isf_data[len_len_loc]) # e.g. 5
        data_loc = len_loc + len_len
        data_len = int(isf_data[len_loc:len_loc+len_len]) # e.g. 20000

        # Extract and parse header
        header = isf_data[:tag_loc]
        # Reformat the header into a dictionary
        header_dict = {}
        items = header.replace(':WFMPRE:','').replace(':','').split(';') # list of "key value" pairs
        for item in items:
            if(item):
                key, value = item.split(' ', 1) # maxsplit 1 to ignore subsequent spaces in value
                value = value.replace('"', '')
                header_dict[key] = value

        if(header_only):
            return header_dict

        # Extract data and convert from string to integer value
        data = isf_data[data_loc:]

        stats = Stats()
        stats.npts = int(header_dict['NR_PT'])
        stats.calib = float(header_dict['YMULT'])
        byte_order = header_dict['BYT_OR'] # 'MSB' or 'LSB'

        if(byte_order == 'MSB'):
            byte_order = '>'
        else:
            byte_order = '<'
        points = []
        for i in range(0, stats.npts*2, 2):
            value = data[i:i+2] # as string
            converted = struct.unpack('%sh' % byte_order, value)[0]
            points.append(converted)

        # Optionally convert points to engineering units
        if(convert):
            try:
                points = np.array(points) * stats.calib  # requires numpy
            except NameError:
                # If numpy not available, use list instead.
                p = []
                for point in points:
                    p.append(point * stats.calib)
                points = p
        stats.time_offset = float(header_dict['XZERO'])
        stats.calib_unit = header_dict['YUNIT']
        stats.delta = float(header_dict['XINCR'])
        stats.amp_offset = float(header_dict['YOFF'])
        stats.comments = header_dict['WFID']
        stats.channel = header_dict['WFID'][0:3]

        return stats, points

    def getWaveform(self, channel=1, format=None):
        """
        Returns the data points that make up the currently displayed
        waveform. There will be 10,000 points spanning the X-axis time,
        and 7 bits spanning the Y-axis voltage. The time and voltage
        range depend on how the scope is currently configured, and this
        configuration is returned as metadata depending on the value of
        format.

        Arguments:

        :param channel: Integer, 1-4
        :param format: String (see below).
        :returns: Data representing the currently displayed waveform
                  from the oscilloscope in the format described above.

        format argument

        Optional argument specifying what format the returned data
        should be in. Default value is 'eng'. Possible values and
        their effect on the return value are:

        **raw**
            return value will contain only the raw ISF-formatted
            data from the scope. See notes on ISF below.

        **csv**
            return value is a (header, data) tuple, where header is a
            python dict of terms describing the data. Data is a string
            of the 10k data points from the scope in ASCII/CSV format.

            Note that this takes about 20 seconds as opposed to <1
            second for ISF.

        **counts**
            return value is a (header, data) tuple where header is a
            python dictionary of terms describing data, which is a numpy
            array of data points in ADC counts. (If numpy is installed,
            otherwise data is a list.) The header information can be
            used to convert counts to volts.

        **eng**
            return value is a (header, data) tuple as in the 'array'
            case above, only the header will be examined in order to
            return the data points in volts (as floating point numbers).

        Exceptions:

        E.g.:

        getWaveform(channel=1)

        Notes:

        For 'eng' and 'array' formats, the TDS3014B generates 9-bit
        samples, which are packed into a two-byte signed integer. For
        example, 0xF300 = -3328 -3328 * YMULT of 1.5625e-4 = -0.52 volts

        See also:

        http://www.photonics.umd.edu/software/isfread.m
            Example ISF decoder using matlab.

        http://www2.tek.com/cmswpt/swdetails.lotr?ct=SW&cs=sut&ci=5355&lc=EN
            ISF to CSV Conversion utility from Tektronix (DOS)

        http://www.tek.com/forum/viewtopic.php?f=5&t=50
            Notes on ISF header format.
        """

        if(format == None):
            format = 'eng' # Default format

        # get data in ISF format first. If the user requested CSV, return the
        # header from the ISF request along with the CSV data from a second
        # request. This keeps the return value format similar for both cases and
        # ensures that the user doesn't just get CSV data with no metadata.
        content = self.fetchData(channel, format='internal')

        if(format == 'raw'):
            return content
        elif(format == 'csv'):
            header = self.parseISF(content, header_only=True)
            csv_data = self.fetchData(channel, format='spreadsheet')
            return header, csv_data
        elif(format == 'counts'):
            header, points = self.parseISF(content, convert=False)
            return header, points
        elif(format == 'eng'):
            header, points = self.parseISF(content, convert=True)

            return header, points

    def fetchData(self, channel, format):
        """
        Get readout of current data.
        Communicate with the scope and request a readout of the current
        data. Format can be either 'internal' (for ISF data) or
        'spreadsheet' (for CSV).
        """
        data = urllib.urlencode([('command', 'select:ch%d on' % (channel)),
                                 ('command', 'save:waveform:fileformat %s' % (format)),
                                 ('wfmsend', 'Get')])
        url = '/'.join([self.base_url, "getwfm.isf"])
        response, content = self.h.request(url, "POST", data)
        content = content[:-3] # remove '\n\r\n' from content
        return content

    def getIDN(self):
        return self.gpibCMD('*IDN?')

    def getMeasurement(self, meas_num, numeric=False):
        """
        Queries the scope for the measurement number meas_num.  The
        scope can have up to four measurements which are user
        configured.

        e.g.:
        >>> result = t.get_measurement(1) # Get measurement for MEAS1
        >>> print result
        '1.3312E-8'

        The units for the measurement can be determined by using
        get_measurement_params() (below).

        """
        if(1 <= meas_num <= 4):
            retval = self.gpibCMD('measurement:MEAS%d:data?' % meas_num).strip().split(',')[0]
            if(numeric):
                retval = float(retval)
            return retval
        else:
            msg = "ERROR: meas_num must be between 1 and 4 inclusive."
            print(msg)
            raise Error(msg)

    def getMeasurementParams(self, meas_num):
        """
        Queries the scope for the parameters for measurement number
        meas_num.  Returns a string.

        e.g.:
        >>> result = t.getMeasurementParams(3) # get parameters for MEAS3
        >>> print result
        'FREQ;"Hz";CH1;CH2;FORW;RIS;RIS;1'

        """
        if(1 <= meas_num <= 4):
            return self.gpibCMD('measurement:MEAS%d?' % meas_num).strip()
        else:
            msg = "ERROR: meas_num must be between 1 and 4 inclusive."
            print(msg)
            raise Error(msg)

    def recallSavedConfig(self, config_num):
        """
        Recalls scope saved configuration number config_num.
        """
        self.gpibCMD('recall:setup %d' % (config_num))

    def setVscale(self, chan_num, vdiv):
        """
        Sets the vertical scale for the specified channel to the
        specified scale.  Scale is in volts per division.

        e.g.:
        >>> t.setVscale(2, .5) # set ch. 2 to 500 mV/div
        """

        self.gpibCMD('ch%d:scale %s' % (chan_num, vdiv))

    def setHscale(self, secdiv=None, freq=None):
        """
        Sets the scope horizontal timebase.  The scale can either be
        specified by supplying the secdiv parameter, in which case the
        scale is set to the given seconds per division.  Or it can be
        set by supplying a frequency.  The seconds per division value
        will be calculated such that 10 cycles of the given frequency
        will be shown on screen.

        e.g.:
        >>> t.setHscale(secdiv=1) # 1 second per division
        >>> t.setHscale(freq=100) # 10 ms/div
        """
        if(secdiv != None):
            self.gpibCMD('horizontal:main:scale %s' % (str(secdiv)))
        elif(freq != None):
            secdiv = 1.0/freq
            self.gpibCMD('horizontal:main:scale %s' % (str(secdiv)))
        else:
            msg = "ERROR: One of secdiv or freq must be specified."
            print(msg)
            raise Error(msg)

def test():
    scope = TDS3014B('localhost:31338')
    return scope


# сумма всех умерших
import pandas as pd
from multiprocessing import Pool
from time import monotonic
import matplotlib.pyplot as plt

if __name__ == '__main__':
    # считать таблицу
    df = pd.read_csv("data.csv", sep=',', encoding='windows-1251')
    # вывести в консоль число столбцов и их названия, типы данных, число строк
    print("Кол-во столбцов:", len(df.columns))
    print(df.dtypes, end='\n\n')
    print("Кол-во строк:", len(df['Страна']), end='\n\n')
    # изучить таблицу
    print(df.isnull().sum(), end='\n\n')
    # избавиться от нежелательных данных
    df.fillna(-1)

    deadsum = 0

    fig = plt.figure(figsize=(10, 10))
    plt.title("Время выполнения")
    plt.ylabel("Время, сек")
    plt.xlabel("Количество процессов")
    xvals = []
    yvals = []
    for i in range(1, 25):
        t_s = monotonic()
        with Pool(i) as pl:
            deadsum = pl.apply(sum, [df['Умерли']])
        print(i, "time:", round(monotonic()-t_s, 3))
        xvals.append(i)
        yvals.append(round(monotonic()-t_s, 3))
    plt.plot(xvals, yvals, marker='.', markersize=10)
    plt.savefig("graph.png")
    print("Сумма умерших:", deadsum)


#std imports
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

#local imports
import currimage
from pbtspot import globalconstants

class Rectangle:
    """
    defines a rectangle in Haar feature
    @param: x_val,y_val,width,height:integer>0
    @param: weight: positive or negative integer
    """
    def __init__(self,x_val,y_val,width,height,weight):
        self.check_values(x_val,y_val,width,height,weight);

        #also check whether all four points within image limits, make a function
        #if you want weights to be +-1, check that too.
        self.x_val=x_val;
        self.y_val=y_val;
        self.width=width;
        self.height=height;
        self.weight=weight;

    def __str__(self):
        return("Rectange.X:%d,Y:%d,W:%d,H:%d"%(self.x_val,self.y_val,self.width,self.height))

    def __repr__(self):
        return("Rectange.X:%d,Y:%d,W:%d,H:%d","Wt:%d"%(self.x_val,self.y_val,self.width,self.height,self.weight))

    def check_values(self,x_val,y_val,width,height,weight):

        #Todo: this class must be able to access image size variables
        if(width<0):
            raise ValueError("Width value must be within limit:%d",width);

        if(height<0):
            raise ValueError("Height value must be within limit:%d",height);

        if(x_val<0):
            raise ValueError("X value must be within limit:%d",x_val);

        if(y_val<0):
            raise ValueError("Y value must be within limit:%d",y_val);
    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.__dict__ == other.__dict__
        else:
            return False

    def __ne__(self, other):
        return not self.__eq__(other)

class BaseFeature:
    """
    defines a BaseFeature. Different Feature types derive from it.
    defines virtual functions.
    """
    #We define features simply as a list of rectangles
    def __init__(self):
        glob_const=globalconstants.GlobalConstants();
        self.global_window_width=glob_const.global_window_width;
        self.global_window_height=glob_const.global_window_height;

        self.rectlist=None; #defined by subclass

        #Other variables, to be defined during optimization
        #should we initialize them here?
        self.threshold=None;
        self.toggle=None;
        self.error=None
        self.margin=None;
        self.response_vector=None;

    #Abstract classes, to be defined later
    def generate_feature(self,x_val,y_val,width,height):
        raise NotImplementedError('Must implement a "generate_feature" method.')

    def __repr__(self):
        raise NotImplementedError('Subclass must implement a "__repr__" method.')

    #Implemented Classes
    #this class now takes in int_image, not image
    #taking in image was the main cause of slowing down the program
    # for each feature it kept on calculating the same int_image
    def convolve_int_image(self,int_image):

        #int_image=image;
        #currimg=currimage.CurrImage(image);
        #currimg.calc_integral_image();
        #int_image=currimg.int_image;

        if self.rectlist is None:
            raise AttributeError("Rectlist not defined by sublass yet.")

        sum_px=0;
        #can optimize by caching some values
        for rect in self.rectlist:
            sum_px+=rect.weight*self.sum_int_image(int_image,rect);
        return sum_px;

    def sum_int_image(self,int_image,rect):
        top_left=self.int_image_val(int_image,rect.y_val-1,rect.x_val-1);
        bottom_left=self.int_image_val(int_image,rect.y_val+rect.height-1,rect.x_val-1);
        top_right=self.int_image_val(int_image,rect.y_val-1,rect.x_val+rect.width-1);
        bottom_right=self.int_image_val(int_image,rect.y_val+rect.height-1,rect.x_val+rect.width-1);

        #print rect
        #print (bottom_right+top_left-bottom_left-top_right)
        #From Figure 3 of Viola-Jones Paper
        return (bottom_right+top_left-bottom_left-top_right);

    def sum_rotated_int_image(self,rotated_int_image,rect):
        first_term=self.rotated_int_image_val(rotated_int_image,rect.y_val+rect.width,rect.x_val+rect.width);
        second_term=self.rotated_int_image_val(rotated_int_image,rect.x_val-rect.height,rect.y_val+rect.height);
        third_term=self.rotated_int_image_val(rotated_int_image,rect.x_val,rect.y_val);
        fourth_term=self.rotated_int_image_val(rotated_int_image,rect.x_val+rect.width-rect.height,\
                                               rect.y_val+rect.width+rect.height);

    def int_image_val(self,int_image, y_val,x_val):
        """
        this function returns the integral image coordinates
        and handles the special case when values are sought at the
        margin of the matrix
        """
        if(x_val==-1 or y_val==-1):
            return 0;
        else:
            return int_image[y_val,x_val];

    def show_feature(self):
        """
        plots a feature in an image, for testing purposes
        """
        if self.rectlist==None:
            raise ValueError("Rectlist not defined yet");

        img_toshow=50*np.ones((self.global_window_height,self.global_window_width),dtype=int);#Grey
        for rect in self.rectlist:
            if(rect.weight>0):
                img_toshow[rect.y_val:rect.y_val+rect.height,\
                           rect.x_val:rect.x_val+rect.width]=100;#white
            else:
                img_toshow[rect.y_val:rect.y_val+rect.height,\
                           rect.x_val:rect.x_val+rect.width]=0;#black
        fig=plt.figure();
        ax=fig.add_subplot(111);
        ax.imshow(img_toshow,cmap=cm.Greys_r,interpolation='None');
        plt.colorbar
        plt.grid(True);
        plt.show()
        return

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.__dict__ == other.__dict__
        else:
            return False

    def __ne__(self, other):
        return not self.__eq__(other)


# Scrapy settings for mycrawler project
#
# For simplicity, this file contains only the most important settings by
# default. All the other settings are documented here:
#
#     http://doc.scrapy.org/en/latest/topics/settings.html
#

BOT_NAME = 'mycrawler'

SPIDER_MODULES = ['mycrawler.spiders']
NEWSPIDER_MODULE = 'mycrawler.spiders'
ITEM_PIPELINES = ['mycrawler.pipelines.mycrawlerSqlPipeline']

# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'mycrawler (+http://www.yourdomain.com)'
USER_AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0'
#LOG_LEVEL = 'INFO'
REDIRECT_ENABLED = False

MYSQLDB_CONNARGS = (
    "ec2-54-255-51-21.ap-southeast-1.compute.amazonaws.com",
    "root",
    "proptiger",
    "crawling"
)


from distutils.core import setup
ver = '1.0.24'
setup(
    name='gus',
    packages=['gus'],  # this must be the same as the name above
    version=ver,
    description='a python2 git utils',
    author='chang',
    author_email='zhchang@gmail.com',
    url='https://github.com/zhchang/gutils',  # use the URL to the github repo
    download_url='https://github.com/zhchang/gutils/tarball/' + ver,
    keywords=['python', 'git', 'util'],  # arbitrary keywords
    scripts=['scripts/gus'],
    install_requires=[
          'cliapp>=1.0.9',
      ],
    classifiers=[],
)


class math_1:
    def __init__(self):
        self.mathematics = []

    def multiplication(self):
        return [i*3 for i in self.mathematics]

    def subtraction(self):
        return [i - 5 for i in self.mathematics]
    def division(self):
        user_input = input("Insert the number->")
        if user_input == 0:
            raise ZeroDivisionError
        else:
            return [i/int(user_input) for i in self.mathematics]

result = math_1()
result.mathematics.append(40.)
result.mathematics.append(45.)
result.mathematics.append(20.)
print(result.multiplication())
print(result.subtraction())
print(result.division())


#더하기 / 함수 작성

#1. 함수 정의 (매개변수 a,b / 즉 2개의 값을 받아온 후 연산처리하여 결과값 반환)
def add(a,b):
    c= a+b
    return c

#2. 정의한 함수 인자값 전달후 , 함수측 에서 반환값이 있다면 sum변수에 저장
sum = add(1,2)

#3. sum변수 출력 / 3의 반환
print(sum)

#뺄셈
def minus (a,b):
    c = a-b
    return c
sum = minus(2,1)
print(sum)

#곱셈
def mul(a,b):
    c = a*b
    return c
sum = mul(2,2)
print(sum)

#나눗셈 (몫)
def divide(a,b):
    c = a/b
    return c
sum=divide(2,4)
print(sum)

#나눗셈 (나머지)
def divide(a,b):
    c = a%b
    return c
sum=divide(2,4)
print(sum)


import requests
import time
import logging
import json
import datetime
import config
import csv
import pytz
import os

def getDateTimeNow():
    return datetime.datetime.now(pytz.utc).strftime('%d/%m/%Y, %H:%M:%S')

def getUsers():
    try:
        results = []
        with open(config.csvFilePath["users"], newline='') as inputfile:
            for row in csv.reader(inputfile):
                results.append(row[0])
    except Exception as e:
        logging.warning('{0} : failed to read user list from file. error: {1}'.format(getDateTimeNow(),e))
    logging.info('{0} : Scanned users -{1}'.format(getDateTimeNow(),results))
    return results

def getGist(user):
    url = 'https://api.github.com/users/{0}/gists'.format(user)
    resp = requests.get(url=url)
    data = resp.json()
    gists = []
    currentDateTime = datetime.datetime.now(pytz.utc)
    date = currentDateTime - datetime.timedelta(days=config.period['days'],hours=config.period['hours'], seconds= config.period['seconds'])
    try:
        for i in range(len(data)):
            dateTimeStr = data[i]['updated_at']
            updatedAt = datetime.datetime.strptime(dateTimeStr, '%Y-%m-%dT%H:%M:%SZ')
            updatedAt_local = pytz.utc.localize(updatedAt)
            if updatedAt_local >= date:
                gists.append(data[i])
    except Exception as e:
        logging.warning('{0} : Github-API: Failed to get gist through github-API'.format(getDateTimeNow()))
        if e:
            logging.warning('{0} : Error message: {1}'.format(getDateTimeNow(), e))
        if len(data['message']) != 0:
            logging.warning('{0} : Response message: {1}'.format(getDateTimeNow(),data['message']))

    return gists
def formatGist(gists):
    formatted_gists = []
    for i in range(len(gists)):
        gistDict = {
            "user": gists[i]['owner']['login'],
        "html_url": gists[i]['html_url'],
        "update_date": gists[i]['updated_at']
        }
        formatted_gists.append(gistDict)
    return formatted_gists

def writeGist(formatted_gists):
    try:
        out_file = open("gists.txt", "a")
        out_file.write(json.dumps(formatted_gists, indent=4) + "\n")
        out_file.close()
    except Exception as e:
        logging.warning('{0} : failed to write gist to file. error: {1}'.format(getDateTimeNow(),e))

def createActivity(gists):
    try:
        activityId = []
        for i in range(len(gists)):
            dateTimeStr = gists[i]['updated_at']
            updatedAt = datetime.datetime.strptime(dateTimeStr, '%Y-%m-%dT%H:%M:%SZ')
            tz = pytz.timezone(config.period['timezone'])
            updatedAt_local = tz.localize(updatedAt)
            due_date = datetime.datetime.now(pytz.utc).strftime('%Y-%m-%d')
            due_time = datetime.datetime.now(pytz.utc).strftime('%I:%M %p')
            url = "https://{0}.pipedrive.com/api/v1/activities?api_token={1}".format(config.pipedrive['domain'],config.pipedrive['api-key'])
            data = {
                "subject": "Gist created - User: {0} at {1}".format(gists[i]['owner']['login'],str(updatedAt_local)),
                "type": "Task",
                "note":  "gist url: " + gists[i]['html_url'],
                "due_date": due_date,
                "due_time": due_time
            }
            resp = requests.post(url, json=data)
            response = resp.json()
            if response['data']['id']:
                activityId.append(response['data']['id'])
            else:
                logging.warning('{0} : Pipedrive-API: failed to add activity to pipedrive. Response: {1}'.format(getDateTimeNow(),resp))
    except Exception as e:
        logging.warning('{0} : Pipedrive-API: failed to add activity to pipedrive. Error: {1}'.format(getDateTimeNow(),e))
    logging.info('{0} : Pipedrive-API: Created activities successfully for id : {1}'.format(getDateTimeNow(), str(activityId)))

def scan():
    #logging.basicConfig(filename='system.log', level=logging.DEBUG)
    logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
    users = getUsers()
    open('gists.txt', 'w').close()
    logging.info('{0} : Start Scanning'.format(getDateTimeNow()))
    for i in range(len(users)):
        gists = getGist(users[i])
        formatted_gists = formatGist(gists)
        if len(gists)>0:
            logging.info('{0} : {1} new gists created by user {2} were found.'.format(getDateTimeNow(),str(len(gists)),str(users[i])))
            writeGist(formatted_gists)
            createActivity(gists)
        else:
            logging.info('{0} : NO new gist created by user {1} was found.'.format(getDateTimeNow(),str(users[i])))
    logging.info('{0} : Completed Scanning'.format(getDateTimeNow()))


# Given an integer n, find and print the sum of numbers from 1 to n.
# Note : Use while loop only.

# Input Format :
# Integer n

# Output Format :
# Sum

# Read input as specified in the question
# Print output as specified in the question
n = int(input())
i = 1
Sum = 0
while i <= n:
    Sum = Sum + i
    i += 1

print(Sum)

Coding Ninjas/Conditionals & Loops/Q2_Sum_of_n_numbers.py