# multiAgents.py
# --------------
# Licensing Information:  You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).

from node import Node
from util import manhattanDistance, Stack
from game import Actions, Directions, GameStateData

import random
import util

from game import Agent
from pacman import GameState

class ReflexAgent(Agent):
    """
    A reflex agent chooses an action at each choice point by examining
    its alternatives via a state evaluation function.

    The code below is provided as a guide.  You are welcome to change
    it in any way you see fit, so long as you don't touch our method
    headers.
    """

    def getAction(self, gameState):
        """
        You do not need to change this method, but you're welcome to.

        getAction chooses among the best options according to the evaluation function.

        Just like in the previous project, getAction takes a GameState and returns
        some Directions.X for some X in the set {NORTH, SOUTH, WEST, EAST, STOP}
        """
        # Collect legal moves and child states
        legalMoves = gameState.getLegalActions()

        # Choose one of the best actions
        scores = [self.evaluationFunction(
            gameState, action) for action in legalMoves]
        bestScore = max(scores)
        bestIndices = [index for index in range(
            len(scores)) if scores[index] == bestScore]
        # Pick randomly among the best
        chosenIndex = random.choice(bestIndices)

        "Add more of your code here if you want to"

        return legalMoves[chosenIndex]

    def evaluationFunction(self, currentGameState, action):
        """
        Design a better evaluation function here.

        The evaluation function takes in the current and proposed child
        GameStates (pacman.py) and returns a number, where higher numbers are better.

        The code below extracts some useful information from the state, like the
        remaining food (newFood) and Pacman position after moving (newPos).
        newScaredTimes holds the number of moves that each ghost will remain
        scared because of Pacman having eaten a power pellet.

        Print out these variables to see what you're getting, then combine them
        to create a masterful evaluation function.
        """
        # Useful information you can extract from a GameState (pacman.py)
        childGameState = currentGameState.getPacmanNextState(action)
        newPos = childGameState.getPacmanPosition()
        newFood = childGameState.getFood()
        newGhostStates = childGameState.getGhostStates()
        newScaredTimes = [
            ghostState.scaredTimer for ghostState in newGhostStates]

        "*** YOUR CODE HERE ***"
        total_score = 0
        old_food = currentGameState.getFood()
        for x in range(old_food.width):
            for y in range(old_food.height):
                if old_food[x][y]:
                    d = manhattanDistance((x, y), newPos)
                    if d == 0:
                        total_score += 100
                    else:
                        total_score += 1 / (d * d)

        for ghost in newGhostStates:
            d = manhattanDistance(ghost.getPosition(), newPos)
            if d <= 1:
                if ghost.scaredTimer != 0:
                    total_score += 2000
                else:
                    total_score -= 200

        return total_score

def scoreEvaluationFunction(currentGameState):
    """
    This default evaluation function just returns the score of the state.
    The score is the same one displayed in the Pacman GUI.

    This evaluation function is meant for use with adversarial search agents
    (not reflex agents).
    """
    return currentGameState.getScore()

class MultiAgentSearchAgent(Agent):
    """
    This class provides some common elements to all of your
    multi-agent searchers.  Any methods defined here will be available
    to the MinimaxPacmanAgent, AlphaBetaPacmanAgent & ExpectimaxPacmanAgent.

    You *do not* need to make any changes here, but you can if you want to
    add functionality to all your adversarial search agents.  Please do not
    remove anything, however.

    Note: this is an abstract class: one that should not be instantiated.  It's
    only partially specified, and designed to be extended.  Agent (game.py)
    is another abstract class.
    """

    def __init__(self, evalFn='scoreEvaluationFunction', depth='2'):
        self.index = 0  # Pacman is always agent index 0
        self.evaluationFunction = util.lookup(evalFn, globals())
        self.depth = int(depth)

    def terminalTest(self, gameState, depth):
        return depth == 0 or gameState.isWin() or gameState.isLose()

class MinimaxAgent(MultiAgentSearchAgent):
    """
    Your minimax agent (question 2)
    """

    def getAction(self, gameState: GameState):
        """
        Returns the minimax action from the current gameState using self.depth
        and self.evaluationFunction.

        Here are some method calls that might be useful when implementing minimax.

        gameState.getLegalActions(agentIndex):
        Returns a list of legal actions for an agent
        agentIndex=0 means Pacman, ghosts are >= 1

        gameState.getNextState(agentIndex, action):
        Returns the child game state after an agent takes an action

        gameState.getNumAgents():
        Returns the total number of agents in the game

        gameState.isWin():
        Returns whether or not the game state is a winning state

        gameState.isLose():
        Returns whether or not the game state is a losing state
        """

        "*** YOUR CODE HERE ***"
        v = float("-inf")
        actions = []
        for a in gameState.getLegalActions(0):
            u = self.min_value(
                gameState=gameState.getNextState(0, a),
                agent=1,
                depth=self.depth,
            )
            if u == v:
                actions.append(a)
            elif u >= v:
                v = u
                actions = [a]

        #return random.choice(actions)
        return actions[0]

    def min_value(self, gameState: GameState, agent, depth):
        if self.terminalTest(gameState, depth):
            return self.evaluationFunction(gameState)

        v = float("inf")
        for a in gameState.getLegalActions(agent):
            succ = gameState.getNextState(agent, a)
            if agent == gameState.getNumAgents() - 1:
                v = min(
                    v, self.max_value(succ, agent=0, depth=depth - 1)
                )
            else:
                v = min(
                    v, self.min_value(succ, agent=agent + 1, depth=depth)
                )
        return v

    def max_value(self, gameState: GameState, agent, depth):
        if self.terminalTest(gameState, depth):
            return self.evaluationFunction(gameState)

        v = float("-inf")
        for a in gameState.getLegalActions(agent):
            v = max(
                v, self.min_value(gameState=gameState.getNextState(agent, a),
                    agent=1, depth=depth)
            )
        return v

class AlphaBetaAgent(MultiAgentSearchAgent):
    """
    Your minimax agent with alpha-beta pruning (question 3)
    """

    def getAction(self, gameState):
        """
        Returns the minimax action using self.depth and self.evaluationFunction
        """
        "*** YOUR CODE HERE ***"

        #return random.choice(actions)
        return self.max_value(
                        gameState=gameState,
                        agent=0,
                        depth=self.depth,
                        root=True)

    def max_value(self, gameState: GameState, agent, depth, alpha=float("-inf"), beta=float("+inf"), root=False):
        acc = []
        if self.terminalTest(gameState, depth):
            return acc if root else self.evaluationFunction(gameState)

        v = float("-inf")
        for a in gameState.getLegalActions(agent):
            v = max(v, self.min_value(gameState=gameState.getNextState(agent, a),
                                      agent=1, depth=depth, alpha=alpha, beta=beta))
            if v > beta:
                return  a if root else v
            if alpha < v:
                alpha = v
                acc = a
        return acc if root else v

    def min_value(self, gameState: GameState, agent, depth, alpha, beta):
        if self.terminalTest(gameState, depth):
            return self.evaluationFunction(gameState)

        v = float("inf")
        for a in gameState.getLegalActions(agent):
            succ = gameState.getNextState(agent, a)
            if agent == gameState.getNumAgents() - 1:
                v = min(v, self.max_value(succ, agent=0, depth=depth - 1, alpha=alpha, beta=beta))
            else:
                v = min(v, self.min_value(succ, agent=agent + 1, depth=depth, alpha=alpha, beta=beta))
            if v < alpha:
                return v
            beta = min(beta, v)
        return v

class ExpectimaxAgent(MultiAgentSearchAgent):
    """
      Your expectimax agent (question 4)
    """

    def getAction(self, gameState):
        """
        Returns the expectimax action using self.depth and self.evaluationFunction

        All ghosts should be modeled as choosing uniformly at random from their
        legal moves.
        """
        "*** YOUR CODE HERE ***"
        return self.max_value(
                    gameState=gameState,
                    agent=0,
                    depth=self.depth,
                    root=True)

    def max_value(self, gameState: GameState, agent, depth, root=False):
        act = []
        if self.terminalTest(gameState, depth):
            return act if root else self.evaluationFunction(gameState)

        v = float("-inf")
        for a in gameState.getLegalActions(agent):
            succ =  gameState.getNextState(agent, a)
            u = self.chance(gameState = succ,
                            agent=1,
                            depth=depth)
            if v < u:
                v = u
                act = a
        return act if root else v

    def chance(self, gameState: GameState, agent, depth):
        if self.terminalTest(gameState, depth):
            return self.evaluationFunction(gameState)
        v=0
        prob_curr = 1 / len(gameState.getLegalActions(agent))

        for a in gameState.getLegalActions(agent):
            succ = gameState.getNextState(agent, a)
            if agent == gameState.getNumAgents() - 1:
                v += prob_curr * self.max_value(succ, agent=0, depth=depth - 1)
            else:
                v += prob_curr * self.chance(succ, agent=agent + 1, depth=depth)
        return v

class MinimaxAgentIter(MultiAgentSearchAgent):
    """
    Your minimax agent with alpha-beta pruning (question 3) iterative
    """

    def getAction(self, gameState):
        """
        Returns the minimax action using self.depth and self.evaluationFunction
        """
        "*** YOUR CODE HERE ***"

        #return random.choice(actions)
        return self.iter(gameState=gameState,
                        agent=0,
                        depth=self.depth)

    def iter(self, gameState: GameState, agent, depth):
        stack = Stack()
        n = Node(parent=None, agent=agent, state=gameState, depth=depth)
        stack.push(n)

        while True:
            x = stack.top()
            if x.isRoot() and x.value is not None:
                return x.action
            if x.value is not None:
                if x.parent.isPacman() and  x.value > x.parent.value:
                    x.parent.value = x.value
                    x.parent.action = x.action
                elif not x.parent.isPacman() and x.value < x.parent.value:
                    x.parent.value = x.value
                stack.pop()
            else:
                if self.terminalTest(x.state, x.depth):
                    x.value = self.evaluationFunction(x.state)
                else:
                    x.value = float("-inf") if x.isPacman() else float("+inf")
                    for a in x.state.getLegalActions(x.agent):
                        if x.isPacman():
                            x_child = Node(parent = x, agent = 1,\
                                            state = x.state.getNextState(x.agent, a),\
                                            depth=x.depth)
                        else:
                            if x.agent == x.state.getNumAgents() - 1:
                                x_child = Node(parent = x, agent = 0,\
                                            state = x.state.getNextState(x.agent, a),\
                                            depth=x.depth - 1)

                            else:
                                x_child = Node(parent = x, agent = x.agent + 1,\
                                            state = x.state.getNextState(x.agent, a),\
                                            depth=x.depth)
                        x_child.action = a
                        stack.push(x_child)

class AlphaBetaAgentIter(MultiAgentSearchAgent):
    """
    Your minimax agent with alpha-beta pruning (question 3) iterative
    """

    def getAction(self, gameState):
        """
        Returns the minimax action using self.depth and self.evaluationFunction
        """
        "*** YOUR CODE HERE ***"

        #return random.choice(actions)
        return self.iter(gameState=gameState,
                        agent=0,
                        depth=self.depth)

    def iter(self, gameState: GameState, agent, depth):
        stack = Stack()
        n = Node(parent=None, agent=agent, state=gameState, depth=depth)
        n.alpha = float("-inf")
        n.beta = float("+inf")
        stack.push(n)

        while True:
            x = stack.top()
            if x.isRoot() and x.value is not None:
                return x.action
            if x.value is not None:
                if x.parent.isPacman():
                    if  x.value > x.parent.value: #Update my father
                        x.parent.value = x.value
                        x.parent.action = x.action
                    if x.value < x.alpha:         #Kill my brothers *evil laughs* (kinda sad... send me an @ if you like it)
                        stack.pop()
                        while x.parent == stack.top().parent:
                            stack.pop()
                        stack.push(x)
                    x.parent.beta = min(x.parent.beta, x.value)

                else:
                    if x.value < x.parent.value:
                        x.parent.value = x.value
                    if x.value > x.parent.beta:
                        stack.pop()
                        while x.parent == stack.top().parent:
                            stack.pop()
                        stack.push(x)
                    x.parent.alpha = max(x.parent.alpha, x.value)
                stack.pop()

            else:
                if self.terminalTest(x.state, x.depth):
                    x.value = self.evaluationFunction(x.state)
                else:
                    x.value = float("-inf") if x.isPacman() else float("+inf")
                    for a in x.state.getLegalActions(x.agent):
                        if x.isPacman():
                            x_child = Node(parent = x, agent = 1,\
                                            state = x.state.getNextState(x.agent, a),\
                                            depth=x.depth)
                        else:
                            if x.agent == x.state.getNumAgents() - 1:
                                x_child = Node(parent = x, agent = 0,\
                                            state = x.state.getNextState(x.agent, a),\
                                            depth=x.depth - 1)

                            else:
                                x_child = Node(parent = x, agent = x.agent + 1,\
                                            state = x.state.getNextState(x.agent, a),\
                                            depth=x.depth)
                        x_child.action = a
                        x_child.alpha = x.alpha
                        x_child.beta = x.beta
                        stack.push(x_child)

def betterEvaluationFunction(currentGameState):
    """
    Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
    evaluation function (question 5).

    DESCRIPTION: Number of food is bad for the game since it is optimal to eat all food. This
                 Then, if Pacman is near food is better than far from it. Also if the distance
                 between the ghost and Pacman is bigger than the distance between Pacman and
                 the capsule, Pacman detects a window for eating ghost when they are scared.
                 Finally, the radius to chase the ghost is 2 cells a head.
    """
    "*** YOUR CODE HERE ***"
    total_score = -currentGameState.getNumFood()*2

    food = currentGameState.getFood()
    pacman = currentGameState.getPacmanPosition()

    for x in range(food.width):
        for y in range(food.height):
            if food[x][y]:
                d = manhattanDistance((x, y), pacman)
                if d == 0:
                    total_score += 100
                else:
                    total_score += 1 / d

    for idx in range(1, currentGameState.getNumAgents()):
        d = manhattanDistance(currentGameState.getGhostPosition(idx), pacman)
        capsule_d = [manhattanDistance(pacman, cap)for cap in currentGameState.getCapsules()]
        if len(capsule_d) > 0:
            if d > capsule_d[0] and d < 3:
                total_score += 2000
        if d < 2 and currentGameState.getGhostState(idx).scaredTimer != 0:
            total_score += 500
        if d >= 1:
            total_score += 50
    return total_score

# Abbreviation
better = betterEvaluationFunction


class PoolTable:
    def __init__(self, table_id, table_status):
        self.table_id = table_id
        self.is_occupied = table_status
        self.start_time_string = None
        self.end_time_string = None
        self.start_time = None
        self.end_time = None
        self.total_time = None

    def __repr__(self):
        return (f"Table ID: {self.table_id} || Table Satus: {self.is_occupied} || Start Time: {self.start_time_string} || End Time: {self.end_time_string}|| Duration:{self.total_time} ")


import streamlit as st
import pandas as pd
from model import load_model, predict
import time

# Load your trained model (make sure the model file 'svm_model_OPT.pkl' is in the same directory)
# status_model_filename = 'model\status\svm_model_OPT.pkl'
status_model = load_model('model/status/DT_model.joblib')

# salary_model_filename = 'model/salary/random_forest_regression_model.joblib'
salary_model = load_model('model/salary/random_forest_regression_model.joblib')

# Create a Streamlit web app
st.title('Campus Recruitment Prediction')
st.write('This app predicts campus recruitment status and salary based on selected features.')
st.markdown("""

""", unsafe_allow_html=True)
st.markdown('
Enter your name
', unsafe_allow_html=True)
name = st.text_input("")
# Define input fields for user input
user_input_container = st.container()

with user_input_container:
    st.markdown('
Please Enter correct details
',unsafe_allow_html=True)

    # Gender
    gender = st.selectbox('Select Gender', ['Male', 'Female'])

    # Group SSC Details
    st.subheader('SSC Details')
    col1, col2 = st.columns(2)
    with col1:
        ssc_p = st.number_input('Enter Percentage', min_value=0.0, max_value=100.0, key='ssc_p')
    with col2:
        ssc_b = st.selectbox('Select Board', ['Central', 'Others'], key='ssc_b')

    # Group HSC Details
    st.subheader('HSC Details')
    col1, col2, col_3 = st.columns(3)
    with col1:
        hsc_p = st.number_input('Enter Percentage', min_value=0.0, max_value=100.0, key='hsc_p')
    with col2:
        hsc_b = st.selectbox('Select Board', ['Central', 'Others'], key='hsc_b')
    with col_3:
        hsc_s = st.selectbox('Select HSC Stream', ['Science', 'Commerce', 'Arts'])

    # Group Degree Details
    st.subheader('Degree Details')
    col1, col2 = st.columns(2)
    with col1:
        degree_p = st.number_input('Enter Percentage', min_value=0.0, max_value=100.0, key='degree_p')
    with col2:
        degree_t = st.selectbox('Select Type', ['Comm&Mgmt', 'Sci&Tech', 'Others'], key='degree_t')

        # Work Experience
    workex = st.selectbox('Work Experience', ['Yes', 'No'])

    # E-test Percentage
    etest_p = st.number_input('Enter E-test Percentage', min_value=0.0, max_value=100.0)

    # Specialization
    specialisation = st.selectbox('Select Specialization', ['Mkt&HR', 'Mkt&Fin'])

    # MBA Percentage
    mba_p = st.number_input('Enter MBA Percentage', min_value=0.0, max_value=100.0)

if degree_t == 'Others':
    degree_t = 'Others1'

# Create a dictionary with user inputs
user_input = {
    'gender': gender,
    'ssc_p': ssc_p,
    'ssc_b': ssc_b,
    'hsc_p': hsc_p,
    'hsc_b': hsc_b,
    'hsc_s': hsc_s,
    'degree_t':degree_t,
    'workex': workex,
    'etest_p': etest_p,
    'specialisation': specialisation,
    'mba_p': mba_p,
    'degree_p': degree_p,

}

# Define a function to preprocess user input
def preprocess_user_input(input_data):
    # Create a dictionary to map user input to label-encoded values for categorical features
    label_encoding_mapping = {
        'Male': 0,
        'Female': 1,
        'Central': 0,
        'Others': 1,
        'Science': 0,
        'Commerce': 1,
        'Arts': 2,
        'Yes': 1,
        'No': 0,
        'Mkt&HR': 0,
        'Mkt&Fin': 1,
        'Comm&Mgmt':0,
        'Sci&Tech':1,
        'Others1':2
    }

    # Label encode categorical features
    for feature in ['gender', 'ssc_b', 'hsc_b', 'hsc_s', 'degree_t','workex', 'specialisation']:
        input_data[feature] = label_encoding_mapping.get(input_data[feature])

    return input_data

# Create a button to make predictions
if st.button("Make Prediction"):
    # Preprocess user input
    preprocessed_input = preprocess_user_input(user_input)

    # Extract the values from the dictionary
    input_values = list(preprocessed_input.values())

    # Call the make_prediction function with the input values
    recruitment_status = predict(status_model, [input_values])
    if recruitment_status==1:
        # Calculate salary only if status is "Placed"
        salary_prediction = predict(salary_model,[input_values])
        rounded_salary = round(salary_prediction / 1000) * 1000
        lower_range = rounded_salary - 1000
        upper_range = rounded_salary + 1000
        st.balloons()
        succes = st.success(f"ðŸŽ‰Congratulations! {name} has high chances of getting PLACED! ðŸŽ‰")
        time.sleep(3)
        succes.empty()
        # Display the prediction
        st.write("Result Predicted: SUCCESSFULL")

        st.write(f"Salary Prediction: {lower_range} to {upper_range}")
    else:
        st.write("Result Predicted: NOT SUCCESSFULL")
        st.write("ðŸ˜” Sorry, you are not placed.")
else:
    # Display a neutral message when the button is not clicked
    st.write("Click the 'Make Prediction' button to check your placement status.")


def convert(s, numRows):
    """
    :type s: str
    :type numRows: int
    :rtype: str
    """
    from collections import defaultdict
    from functools import reduce
    d = defaultdict(list)
    idx = 0
    back = True
    for i in range(len(s)):
        d[idx].append(s[i])
        if idx == numRows - 1 or idx == 0:
            back = not back
        if back:
            idx -= 1
        else:
            idx += 1
    ret = ''
    # for i in d:
    #     ret += ''.join(d[i])
    if d:
        ret = ''.join(reduce(lambda x,y: x+y, d.values()))
    return ret
print(convert("", 3))


from os import path
import glob
import os
import shutil

data_set_path = r'E:\GTA_sequence\fixed_dataset_raycast\set7\training'
example_calib_file = 'E:\\GTA_sequence\\000000.txt'
example_image_file = 'E:\\GTA_sequence\\000000.png'
#Preprocessed DATA OUTPUT DIRECTORY
# output_directory = r''
output_directory = os.path.join(data_set_path)
output_calib_dir = os.path.join(output_directory, "calib")
output_image_dir = os.path.join(output_directory, "image_2")
output_point_cloud_dir = os.path.join(output_directory, "velodyne")

os.makedirs(output_calib_dir, exist_ok=True)
os.makedirs(output_image_dir, exist_ok=True)

if path.exists(output_point_cloud_dir):
    point_cloud_files = sorted(glob.glob(os.path.join(output_point_cloud_dir, "*.bin")))
    if os.path.isfile(example_calib_file):
        print('[INFO] Sample file found. Continuing. . .')
        for file_number in range(len(point_cloud_files)):
            basename = os.path.basename(point_cloud_files[file_number])[:-4]
            output_filename = (output_calib_dir + '\\' +basename + '.txt')
            output_image_filename = (output_image_dir + '\\' +basename + '.png')
            shutil.copy2(example_calib_file, output_filename)
            shutil.copy2(example_image_file, output_image_filename)
    else:
        print('[INFO] No Sample file found. Exiting!')


from typing import NoReturn, Optional, Dict, Any

import pytz

from telegram.utils.deprecate import set_new_attribute_deprecated
from telegram.utils.helpers import DEFAULT_NONE
from telegram.utils.types import ODVInput

class Defaults:
    """Convenience Class to gather all parameters with a (user defined) default value

    Parameters:
        parse_mode (:obj:`str`, optional): Send Markdown or HTML, if you want Telegram apps to show
            bold, italic, fixed-width text or URLs in your bot's message.
        disable_notification (:obj:`bool`, optional): Sends the message silently. Users will
            receive a notification with no sound.
        disable_web_page_preview (:obj:`bool`, optional): Disables link previews for links in this
            message.
        allow_sending_without_reply (:obj:`bool`, optional): Pass :obj:`True`, if the message
            should be sent even if the specified replied-to message is not found.
        timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as the
            read timeout from the server (instead of the one specified during creation of the
            connection pool).

            Note:
                Will *not* be used for :meth:`telegram.Bot.get_updates`!
        quote (:obj:`bool`, optional): If set to :obj:`True`, the reply is sent as an actual reply
            to the message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter will
            be ignored. Default: :obj:`True` in group chats and :obj:`False` in private chats.
        tzinfo (:obj:`tzinfo`, optional): A timezone to be used for all date(time) inputs
            appearing throughout PTB, i.e. if a timezone naive date(time) object is passed
            somewhere, it will be assumed to be in ``tzinfo``. Must be a timezone provided by the
            ``pytz`` module. Defaults to UTC.
        run_async (:obj:`bool`, optional): Default setting for the ``run_async`` parameter of
            handlers and error handlers registered through :meth:`Dispatcher.add_handler` and
            :meth:`Dispatcher.add_error_handler`. Defaults to :obj:`False`.
    """

    __slots__ = (
        '_timeout',
        '_tzinfo',
        '_disable_web_page_preview',
        '_run_async',
        '_quote',
        '_disable_notification',
        '_allow_sending_without_reply',
        '_parse_mode',
        '_api_defaults',
        '__dict__',
    )

    def __init__(
        self,
        parse_mode: str = None,
        disable_notification: bool = None,
        disable_web_page_preview: bool = None,
        # Timeout needs special treatment, since the bot methods have two different
        # default values for timeout (None and 20s)
        timeout: ODVInput[float] = DEFAULT_NONE,
        quote: bool = None,
        tzinfo: pytz.BaseTzInfo = pytz.utc,
        run_async: bool = False,
        allow_sending_without_reply: bool = None,
    ):
        self._parse_mode = parse_mode
        self._disable_notification = disable_notification
        self._disable_web_page_preview = disable_web_page_preview
        self._allow_sending_without_reply = allow_sending_without_reply
        self._timeout = timeout
        self._quote = quote
        self._tzinfo = tzinfo
        self._run_async = run_async

        # Gather all defaults that actually have a default value
        self._api_defaults = {}
        for kwarg in (
            'parse_mode',
            'explanation_parse_mode',
            'disable_notification',
            'disable_web_page_preview',
            'allow_sending_without_reply',
        ):
            value = getattr(self, kwarg)
            if value not in [None, DEFAULT_NONE]:
                self._api_defaults[kwarg] = value
        # Special casing, as None is a valid default value
        if self._timeout != DEFAULT_NONE:
            self._api_defaults['timeout'] = self._timeout

    def __setattr__(self, key: str, value: object) -> None:
        set_new_attribute_deprecated(self, key, value)

    @property
    def api_defaults(self) -> Dict[str, Any]:  # skip-cq: PY-D0003
        return self._api_defaults

    @property
    def parse_mode(self) -> Optional[str]:
        """:obj:`str`: Optional. Send Markdown or HTML, if you want Telegram apps to show
        bold, italic, fixed-width text or URLs in your bot's message.
        """
        return self._parse_mode

    @parse_mode.setter
    def parse_mode(self, value: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to defaults after because it would "
            "not have any effect."
        )

    @property
    def explanation_parse_mode(self) -> Optional[str]:
        """:obj:`str`: Optional. Alias for :attr:`parse_mode`, used for
        the corresponding parameter of :meth:`telegram.Bot.send_poll`.
        """
        return self._parse_mode

    @explanation_parse_mode.setter
    def explanation_parse_mode(self, value: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to defaults after because it would "
            "not have any effect."
        )

    @property
    def disable_notification(self) -> Optional[bool]:
        """:obj:`bool`: Optional. Sends the message silently. Users will
        receive a notification with no sound.
        """
        return self._disable_notification

    @disable_notification.setter
    def disable_notification(self, value: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to defaults after because it would "
            "not have any effect."
        )

    @property
    def disable_web_page_preview(self) -> Optional[bool]:
        """:obj:`bool`: Optional. Disables link previews for links in this
        message.
        """
        return self._disable_web_page_preview

    @disable_web_page_preview.setter
    def disable_web_page_preview(self, value: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to defaults after because it would "
            "not have any effect."
        )

    @property
    def allow_sending_without_reply(self) -> Optional[bool]:
        """:obj:`bool`: Optional. Pass :obj:`True`, if the message
        should be sent even if the specified replied-to message is not found.
        """
        return self._allow_sending_without_reply

    @allow_sending_without_reply.setter
    def allow_sending_without_reply(self, value: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to defaults after because it would "
            "not have any effect."
        )

    @property
    def timeout(self) -> ODVInput[float]:
        """:obj:`int` | :obj:`float`: Optional. If this value is specified, use it as the
        read timeout from the server (instead of the one specified during creation of the
        connection pool).
        """
        return self._timeout

    @timeout.setter
    def timeout(self, value: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to defaults after because it would "
            "not have any effect."
        )

    @property
    def quote(self) -> Optional[bool]:
        """:obj:`bool`: Optional. If set to :obj:`True`, the reply is sent as an actual reply
        to the message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter will
        be ignored. Default: :obj:`True` in group chats and :obj:`False` in private chats.
        """
        return self._quote

    @quote.setter
    def quote(self, value: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to defaults after because it would "
            "not have any effect."
        )

    @property
    def tzinfo(self) -> pytz.BaseTzInfo:
        """:obj:`tzinfo`: A timezone to be used for all date(time) objects appearing
        throughout PTB.
        """
        return self._tzinfo

    @tzinfo.setter
    def tzinfo(self, value: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to defaults after because it would "
            "not have any effect."
        )

    @property
    def run_async(self) -> bool:
        """:obj:`bool`: Optional. Default setting for the ``run_async`` parameter of
        handlers and error handlers registered through :meth:`Dispatcher.add_handler` and
        :meth:`Dispatcher.add_error_handler`.
        """
        return self._run_async

    @run_async.setter
    def run_async(self, value: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to defaults after because it would "
            "not have any effect."
        )

    def __hash__(self) -> int:
        return hash(
            (
                self._parse_mode,
                self._disable_notification,
                self._disable_web_page_preview,
                self._allow_sending_without_reply,
                self._timeout,
                self._quote,
                self._tzinfo,
                self._run_async,
            )
        )

    def __eq__(self, other: object) -> bool:
        if isinstance(other, Defaults):
            return all(getattr(self, attr) == getattr(other, attr) for attr in self.__slots__)
        return False

    def __ne__(self, other: object) -> bool:
        return not self == other


from src.envs.noma_env.utils import compute_distance

def get_ccenv(env_class):
    class CCEnv(env_class):
        def __init__(self, *args, **kwargs):
            env_class.__init__(self, *args, **kwargs)
            self.copo_kind = args[0]['args'].copo_kind  # copoinfonei_r, uav_r, car_r, global_r
            self.have_nei_freq = None  # seem to be deprecated

        def reset(self):
            self.have_nei_freq = {k: 0 for k in self.agent_name_list}  # have_nei_freq
            state = super(CCEnv, self).reset()
            return state

        def step(self, actions):
            '''stepinfonei_rewards, global_rewards, have_nei_freq'''
            o, mask, r, d, info = super(CCEnv, self).step(actions)
            # print(self.nei_matrix)
            def _find_neibours_by_nei_matrix(agent_name):
                '''nei_matrix'''
                u1 = self.agent_name_list.index(agent_name)
                neighbours = []
                for u2, agent_name in enumerate(info.keys()):
                    if self.nei_matrix[u1][u2] == 1:
                        dis = compute_distance(self.obj(u1), self.obj(u2))
                        if self.debug_use_nei_max_distance and dis > self.config["neighbours_distance"]:
                            print('')
                            continue
                        neighbours.append(agent_name)
                return neighbours

            def _find_uav_and_car_neibours_by_nei_matrix(agent_name):
                '''nei_matrix'''
                u1 = self.agent_name_list.index(agent_name)
                neighbours_uav = []
                neighbours_car = []
                for u2, agent_name in enumerate(info.keys()):
                    if self.nei_matrix[u1][u2] == 1:
                        dis = compute_distance(self.obj(u1), self.obj(u2))
                        if self.debug_use_nei_max_distance and dis > self.config["neighbours_distance"]:
                            print('')
                            continue
                        neighbours_uav.append(agent_name) if self.is_uav(u2) else neighbours_car.append(agent_name)
                return neighbours_uav, neighbours_car

            '''infonei_rglobal_r'''
            assert self.copo_kind in (1, 2)
            for agent_name in info.keys():
                # for copo_kind=1
                neighbours = _find_neibours_by_nei_matrix(agent_name)
                nei_rewards = [r[n] for n in neighbours]
                if nei_rewards:
                    info[agent_name]["nei_rewards"] = sum(nei_rewards) / len(nei_rewards)
                    self.have_nei_freq[agent_name] += 1
                else:  # OK =
                    info[agent_name]["nei_rewards"] = r[agent_name]
                # for copo_kind=2
                nei_uav, nei_car = _find_uav_and_car_neibours_by_nei_matrix(agent_name)
                nei_uav_rewards = [r[n] for n in nei_uav]
                nei_car_rewards = [r[n] for n in nei_car]
                if nei_uav_rewards:
                    info[agent_name]["uav_rewards"] = sum(nei_uav_rewards) / len(nei_uav_rewards)
                else:
                    info[agent_name]["uav_rewards"] = r[agent_name]  # OK uav=
                if nei_car_rewards:
                    info[agent_name]["car_rewards"] = sum(nei_car_rewards) / len(nei_car_rewards)
                else:
                    info[agent_name]["car_rewards"] = r[agent_name]  # OK car=
                info[agent_name]["global_rewards"] = sum(r.values()) / len(r.values())
                if d['__all__']:
                    info[agent_name]['have_nei_freq'] = self.have_nei_freq[agent_name] / self.num_timestep
            return o, mask, r, d, info

    name = env_class.__name__
    name = f"CC{name}"
    CCEnv.__name__ = name
    CCEnv.__qualname__ = name
    return CCEnv


import copy
import json
import unittest

from nose.plugins.attrib import attr
from cPickle import loads

from w3af.core.data.parsers.doc.url import URL
from w3af.core.data.kb.info import Info
from w3af.core.data.kb.info_set import InfoSet
from w3af.core.data.misc.cpickle_dumps import cpickle_dumps
from w3af.core.data.kb.tests.test_info import (MockInfo, BLIND_SQLI_REFS,
                                               BLIND_SQLI_TOP10_REFS)

@attr('smoke')
class TestInfoSet(unittest.TestCase):
    def test_not_empty(self):
        self.assertRaises(ValueError, InfoSet, [])

    def test_get_name(self):
        i = MockInfo()
        iset = InfoSet([i])
        self.assertEqual(iset.get_name(), 'TestCase')

    def test_get_desc_no_template(self):
        i = MockInfo()
        iset = InfoSet([i])
        self.assertEqual(iset.get_desc(), MockInfo.LONG_DESC)

    def test_get_desc_template(self):
        i = MockInfo()
        tiset = TemplatedInfoSet([i])
        self.assertEqual(tiset.get_desc(), 'Foos and bars 1')

    def test_get_desc_template_info_attr_access(self):
        value = 'Yuuup!'

        i = MockInfo()
        i['tag'] = value
        iset = InfoSet([i])
        iset.TEMPLATE = '{{ tag }}'

        self.assertEqual(iset.get_desc(), value)

    def test_get_id(self):
        i1 = MockInfo(ids=1)
        i2 = MockInfo(ids=2)
        iset = InfoSet([i2, i1])
        self.assertEqual(iset.get_id(), [1, 2])

    def test_get_plugin_name(self):
        i = MockInfo()
        iset = InfoSet([i])
        self.assertEqual(iset.get_plugin_name(), 'plugin_name')

    def test_add(self):
        i1 = MockInfo(ids=1)
        i2 = MockInfo(ids=2)
        iset = InfoSet([i1])
        added = iset.add(i2)

        self.assertEqual(iset.get_id(), [1, 2])
        self.assertTrue(added)

    def test_add_more_than_max(self):
        i1 = MockInfo(ids=1)
        i2 = MockInfo(ids=2)

        iset = InfoSet([i1])
        iset.MAX_INFO_INSTANCES = 2

        added = iset.add(i1)
        self.assertTrue(added)

        added = iset.add(i2)
        self.assertFalse(added)

    def test_get_uniq_id(self):
        i = MockInfo()
        iset = InfoSet([i])
        self.assertIsNotNone(iset.get_uniq_id())

    def test_eq(self):
        i = MockInfo()
        iset1 = InfoSet([i])

        i = MockInfo()
        iset2 = InfoSet([i])

        self.assertEqual(iset1, iset2)

    def test_pickle(self):
        i = MockInfo()
        iset1 = InfoSet([i])

        pickled_iset1 = cpickle_dumps(iset1)
        iset1_clone = loads(pickled_iset1)

        self.assertEqual(iset1.get_uniq_id(), iset1_clone.get_uniq_id())

    def test_deepcopy(self):
        i = MockInfo()
        iset1 = InfoSet([i])

        iset1_copy = copy.deepcopy(iset1)

        self.assertEqual(iset1.get_uniq_id(), iset1_copy.get_uniq_id())

    def test_to_json(self):
        i = Info('Blind SQL injection vulnerability',
                 MockInfo.LONG_DESC,
                 1,
                 'plugin_name')

        i['test'] = 'foo'
        i.add_to_highlight('abc', 'def')

        iset = InfoSet([i])

        jd = iset.to_json()
        json_string = json.dumps(jd)
        jd = json.loads(json_string)

        self.assertEqual(jd['name'], iset.get_name())
        self.assertEqual(jd['url'], str(iset.get_url()))
        self.assertEqual(jd['var'], iset.get_token_name())
        self.assertEqual(jd['response_ids'], iset.get_id())
        self.assertEqual(jd['vulndb_id'], iset.get_vulndb_id())
        self.assertEqual(jd['desc'], iset.get_desc(with_id=False))
        self.assertEqual(jd['long_description'], iset.get_long_description())
        self.assertEqual(jd['fix_guidance'], iset.get_fix_guidance())
        self.assertEqual(jd['fix_effort'], iset.get_fix_effort())
        self.assertEqual(jd['tags'], iset.get_tags())
        self.assertEqual(jd['wasc_ids'], iset.get_wasc_ids())
        self.assertEqual(jd['wasc_urls'], list(iset.get_wasc_urls()))
        self.assertEqual(jd['cwe_urls'], list(iset.get_cwe_urls()))
        self.assertEqual(jd['references'], BLIND_SQLI_REFS)
        self.assertEqual(jd['owasp_top_10_references'], BLIND_SQLI_TOP10_REFS)
        self.assertEqual(jd['plugin_name'], iset.get_plugin_name())
        self.assertEqual(jd['severity'], iset.get_severity())
        self.assertEqual(jd['attributes'], iset.first_info.copy())
        self.assertEqual(jd['highlight'], list(iset.get_to_highlight()))

    def test_match_different_itag(self):
        """
        https://github.com/andresriancho/w3af/issues/10286
        """
        itag_1 = 'hello'
        i1 = MockInfo(ids=1)
        i1[itag_1] = 1
        iset_1 = InfoSet([i1])
        iset_1.ITAG = itag_1

        itag_2 = 'world'
        i2 = MockInfo(ids=2)
        i2[itag_2] = 2

        self.assertFalse(iset_1.match(i2))

    def test_match_same_itag(self):
        """
        https://github.com/andresriancho/w3af/issues/10286
        """
        itag_1 = 'hello'
        i1 = MockInfo(ids=1)
        i1[itag_1] = 1
        iset_1 = InfoSet([i1])
        iset_1.ITAG = itag_1

        i2 = MockInfo(ids=2)
        i2[itag_1] = 1

        self.assertTrue(iset_1.match(i2))

    def test_get_desc_urls(self):
        i1 = MockInfo()
        i1.set_url(URL('http://w3af.org/1'))

        i2 = MockInfo()
        i2.set_url(URL('http://w3af.org/2'))

        tiset = TemplatedInfoSetPrintUri([i1, i2])
        expected = u' - http://w3af.org/2\n - http://w3af.org/1\n'
        self.assertEqual(tiset.get_desc(), expected)

    def test_get_desc_template_special_chars_unicode(self):
        i1 = MockInfo()
        i1.set_url(URL('http://w3af.org/1'))

        i2 = MockInfo()
        i2.set_url(URL('http://w3af.org/2\xc3\xb6'))

        tiset = TemplatedInfoSetPrintUri([i1, i2])
        expected = u' - http://w3af.org/1\n - http://w3af.org/2ö\n'
        self.assertEqual(tiset.get_desc(), expected)

class TemplatedInfoSet(InfoSet):
    TEMPLATE = '''\
    Foos and bars {{ uris|length }}
    '''

class TemplatedInfoSetPrintUri(InfoSet):
    TEMPLATE = (
        '{% for url in uris[:10] %}'
        ' - {{ url }}\n'
        '{% endfor %}'
    )


# Lists or array
arr = [1, 2, 3, 4, 5, 6, 7, 8]

# Index array
arr2 = arr[0]  # or arr[-1] = [8]
# Memotong array
arr3 = arr[0:3]  # or arr[:4] = [1,2,3,4]

arr4 = [9, 10, 11, 12, 13, 14, 15]

# Merubah isi
arr4[0] = 100

# Menambah isi array (push)
arr5 = arr + arr4

# Copy isi array ( [:] )
arr6 = arr[:]

# Merubah isi array menggunakan metoda slicing
arr[0:2] = [200, 300]

# Menambahkan array didalam array ( Multi dimensi array )
arr7 = [arr, arr4]

# Method array
# Append = untuk menambahkan nilai baru keakhir array
arr.append(1)

# Functions
lenArr = len(arr)
print(lenArr)


#!/usr/bin/env python3

from ..day05.intCode import Processor
from collections import defaultdict, deque, namedtuple
from os import path

Point = namedtuple('Point', ['x', 'y'])

LEFT  = 0
RIGHT = 1

BLACK = 0
WHITE = 1

NORTH = 0
WEST  = 1
SOUTH = 2
EAST  = 3

class Robot:
  def __init__(self, code):
    self.brains = Processor(code, [])
    self.cells = defaultdict(int)
    self.orientation = NORTH

  def start(self):
    unique_cells_painted, x, y, first_run = 0, 0, 0, True
    res = self.brains.process()
    try:
      while True:
        cell_color = self.cells[(x,y)]
        self.brains.add_input(1 if first_run else cell_color)
        paint, rotate = next(res), next(res)
        self.cells[(x,y)] = paint
        self.orientation = (self.orientation + rotate * 2 - 1) % 4
        x += (0, 1, 0, -1)[self.orientation]
        y += (-1, 0, 1, 0)[self.orientation]
    except StopIteration:
      pass
    return len(self.cells.keys())

  def paint(self):
    min_x, min_y, max_x, max_y = 0, 0, 0, 0
    for x, y in self.cells.keys():
      min_x, min_y, max_x, max_y = min(min_x, x), min(min_y, y), max(max_x, x), max(max_y, y)

    width, height = max_x - min_x + 1, max_y - min_y + 1
    grid = [[' ' for i in range(width)] for j in range(height)]

    for x in range(width):
      for y in range(height):
        grid[abs(min_y-y)][abs(min_x-x)] = '#' if self.cells[(x,y)] == 1 else ' '

    for r in grid:
      print(''.join(r))

def get_inputs(file='testinput11.txt'):
  with open(path.join(path.dirname(__file__), file), 'r+') as f:
    return [int(c) for c in f.readline().rstrip('\r').rstrip('\n').split(',') ]

if __name__ == '__main__':
  r = Robot(get_inputs('input11.txt'))
  print(f'Unique cells painted: {r.start()}')
  r.paint()


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

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

def bfs(x,y,k,visited):
    dq=deque([[x, y]])
    visited[y][x]=True
    while dq:
        x, y = dq.popleft()
        for i in range(4):
            nx=x+dx[i]
            ny=y+dy[i]
            if 0<=nx<m and 0<=ny<n and arr[ny][nx]>k and not visited[ny][nx]:
                visited[ny][nx]=True
                dq.append((nx,ny))

n,m=map(int,input().split())
arr=[]
k=0
for _ in range(n):
    tmp=[*map(int,input().split())]
    k=max(k,max(tmp))
    arr.append(tmp)
res=[]
for i in range(1,k+1):
    cnt=0
    visited=[[False]*m for _ in range(n)]
    for y in range(n):
        for x in range(m):
            if arr[y][x]>i and not visited[y][x]:
                cnt+=1
                bfs(x,y,i,visited)
    res.append([i,cnt])
print(*sorted(res,key=lambda x:(-x[1],x[0]))[0])


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

class LinkedList(object):
    """
    Ref: https://stackabuse.com/python-linked-lists/
    """
    def __init__(self, val):
        self.head = Node(val) if not isinstance(Node, val) else val
        self.tail = None
        self.nodes = 1 if self.head else 0

    def insert(self, val):
        val = Node(val) if not isinstance(Node, val) else val
        previous = None

        if not self.head:  # add head node
            self.head = val
            self.tail = val
        else:  # otherwise update the current tail with next pointer
            previous = self.tail
            self.tail.next = val  # first add new next for current tail
            val.previous = previous  # update new node with the old/current tail as prev
            self.tail = val  # add new node to end

        self.nodes += 1

    def remove(self, data_to_remove):
        current = self.head

        while current:
            previous = current.previous
            next = current.next

            if current.data == data_to_remove:
                if previous:
                    previous.next = next
                    if next:
                        next.previous = previous
                else:
                    self.head = next
                    if next:
                        next.previous = None
                return

            current = next

    def size(self):
        return self.nodes

    def traverse(self):
        current = self.head
        while current:
            print(current.data)
            current = current.next


from django.urls import path
from rest_framework.routers import DefaultRouter

from .views import UserProfileViewSet, ImageViewSet, CustomTokenObtainPairView

router = DefaultRouter()
router.register(r'user-profile', UserProfileViewSet)
router.register(r'image', ImageViewSet)

urlpatterns = router.urls

urlpatterns += [
    path('user-login/', CustomTokenObtainPairView.as_view(),)
]


import sys

# Variável para contar a qtdade de linhas
total = 0

for arquivo in sys.argv[1:]: # [1:] para pegar apenas os parâmetros
	# Abre o arquivo
	with open(arquivo, newline='') as arquivo_txt:
		linha = 0
		# Varre linha a linha
		for row in arquivo_txt:
			linha += 1
			print(f"Arquivo '{arquivo}' - Linha {linha}: {row}")

		# Incrementa o contador, depois que as linhas são lidas
		total += linha

print(f"Total lidos: {str(total)}")
print("FIM!")


from flask import Blueprint
from flask import jsonify
from database.jirapoker_db import jirapoker_db
from services.jira_client import jira_client
from models.user import User

user = Blueprint('user', __name__)

@user.route('/api/user/<account_id>/estimated-issue-keys', methods=['GET'])
def get_user_estimated_issues(account_id):
    user = jirapoker_db.user.find_one({'accountId': account_id})
    user_estimated_issues = list(jirapoker_db.estimation_result.find({'userId': user['_id']}, {'_id': False}))
    user_estimated_issue_keys = {}
    for user_estimated_issue in user_estimated_issues:
        user_estimated_issue_keys[user_estimated_issue['issueKey']] = True
    return jsonify(user_estimated_issue_keys)

@user.route('/api/user/<user_name>/avatar-url', methods=['GET'])
def get_user_avatar_url(user_name):
    queried_user = jirapoker_db.user.find_one({'userName': user_name})
    if not queried_user:
        return '', 200
    else:
        return queried_user['avatarUrl'], 200

@user.route('/api/user-profile/<account_id>', methods=['GET'])
def get_user_profile(account_id):
    user = jira_client.get_user(account_id)

    user_profile = User()
    user_profile.accountId = user['accountId']
    user_profile.avatarUrl = user['avatarUrls']['48x48']
    user_profile.userName = user['displayName']
    return jsonify(user_profile.__dict__), 200


from database import dataAccess

class training_app:

    def __init__(self):
        self.da = dataAccess()

    def show_all_projects(self):
        project_list = self.da.call_get_all_project()
        print(project_list)
        for project in project_list:
            print("[%s] %s %s" % (project_list.index(project), project[1], project[3]))
        return project_list

    # save a project, then return the project id
    def save_project(self, project_name, dataset_file):
        self.da.call_add_project(project_name, dataset_file)
        project_list = self.da.call_get_all_project()
        for project in project_list:
            if project[1] == project_name:
                project_id = project[0]
        return project_id

    # save a test, then return the test id
    def save_test(self, test_name, project_id):
        self.da.call_add_test(test_name, project_id)
        test_list = self.da.call_get_all_test()
        test_id = 0
        for test in test_list:
            if test[1] == test_name:
                if test[2] == project_id:
                    test_id = test[0]
        return test_id

    # save the list of label and its right and total prediction in label_test table
    # if label is not yet saved, add one to the label table
    def save_label_test(self, test_id, label_list):
        # check if label already exists
        label_exists = False
        stored_labels = self.da.call_get_all_label()
        for label in label_list:
            for label_name in stored_labels:
                if label[0] == label_name[1]:
                    label_exists = True
            if not label_exists:
                self.da.call_add_label(label[0])
            label_exists = False

        # store all contents of label_list
        for label in label_list:
            self.da.call_add_label_test(test_id, label[0], label[1], label[2])

    # add the list of k and its accuracies to k_test table
    # if k is not saved yet, add one to the k table
    def save_k_test(self, test_id, accuracy_list):
        # check if label already exists
        k_exists = False
        stored_k = self.da.call_get_all_k()
        for k in accuracy_list:
            for k_sample in stored_k:
                if k[0] == k_sample[1]:
                    k_exists = True
            if not k_exists:
                self.da.call_add_k(k[0])
            k_exists = False

        # store all contents of label_list
        for k_test in accuracy_list:
            self.da.call_add_k_test(test_id, k_test[0], k_test[1])

# if __name__ == '__main__':
#     ta = training_app()
#     ta.show_all_projects()
    # ta.save_k_test(9, [[1, 99.243], [3, 99.243], [5, 99.243], [7, 99.243], [9, 99.243]])
    # ta.save_label_test(9, [['A', 5, 5], ['B', 5, 5], ['C', 5, 5], ['D', 5, 5], ['E', 5, 5], ])


import traceback
import multiprocessing as mp
import numpy as np
import time
import random
import calendar
import os
import datetime
import sys
current_dir = os.path.dirname(os.path.abspath(__file__))
from poloninex_api.poloniexRestApi import returnTradeHistory
pkg_dir = os.path.dirname(current_dir)
sys.path.append(pkg_dir)
from utility.error_logger_writer import logger
from influxdb_client.influxdb_client_v1 import InfluxClient

db = InfluxClient()

measurement = 'poloniex_trades'

symbols = ["USDT_SNX","TRX_SNX","BTC_SNX"]

# write trades data to influxdb
def write_trades_data(data,symbol,measurement):
    for d in data:
        fields = {}
        fields.update({"amount":float(d["amount"])})
        fields.update({"total":float(d["total"])})
        fields.update({"price":float(d["rate"])})
        fields.update({"tradeID":int(d["tradeID"])})
        fields.update({"globalTradeID":int(d["globalTradeID"])})
        fields.update({"orderNumber":int(d["orderNumber"])})
        fields.update({"type":d["type"]})
        fields.update({"is_api_return_timestamp": True})
        tags = {}
        tags.update({"symbol":symbol})
        dt_temp = datetime.datetime.strptime(d['date'], "%Y-%m-%d %H:%M:%S")
        uts = time.mktime(dt_temp.timetuple()) * 1000 + random.random()
        dt = datetime.datetime.utcfromtimestamp(uts / 1000)
        dbtime = dt
        db.write_points_to_measurement(measurement,dbtime,tags,fields)

# historical trades data
def write_hist_trades(symbol,measurement):
    data = returnTradeHistory(symbol)
    last_trades_ts = calendar.timegm(datetime.datetime.strptime(data[-1]['date'], "%Y-%m-%d %H:%M:%S").timetuple())
    get_all_data = False
    all_data = []
    while not get_all_data:
        data_temp = returnTradeHistory(symbols[0],int(last_trades_ts)-60*60,int(last_trades_ts))[1:]
        write_trades_data(data_temp, symbol, measurement)
        last_trades_ts = calendar.timegm(datetime.datetime.strptime(data_temp[-1]['date'], "%Y-%m-%d %H:%M:%S").timetuple())
        if data_temp[-1]['tradeID'] == 1:
            get_all_data = True
        else:
            pass

# live trades data
def write_trades_update(symbol,measurement):
    #try:
    #    last_trades_date = db.query_tables(measurement, ["*","order by time desc limit 1"])['date'].tolist()[0]
    #except IndexError:
    #    write_hist_trades(symbol,measurement)
    #    return
    #last_trades_ts = calendar.timegm(datetime.datetime.strptime(last_trades_date, "%Y-%m-%d %H:%M:%S").timetuple())
    current_ts = int(np.ceil(time.time()))
    data = returnTradeHistory(symbol)
    write_trades_data(data, symbol, measurement)

def subscribe_trades(symbol,measurement):
    try:
        write_trades_update(symbol,measurement)
    except Exception:
        error_message = traceback.format_exc()
        #logger(measurement,error_message)
    while True:
        time.sleep(60*10)
        try:
            write_trades_update(symbol,measurement)
        except Exception:
            error_message = traceback.format_exc()
            #logger(measurement,error_message)

if __name__ == "__main__":
    for symb in symbols:
        collecting_symb = mp.Process(target=subscribe_trades, args=(symb,measurement))
        collecting_symb.start()


from datetime import datetime

import pydash
import pandas as pd

from covid19_scrapers.scraper import ScraperBase
from covid19_scrapers.utils import misc
from covid19_scrapers.utils.tableau import TableauParser, find_tableau_request
from covid19_scrapers.webdriver import WebdriverRunner, WebdriverSteps

class Arizona(ScraperBase):
    """Arizona data is extracted from two Tableau dashboards: cases and deaths.

    The way this is extracted is by going to each of the Tableau links.
    When a request is made to the Tableau URL, several requests are made back and forth between the client and server.
    One of the requests made will contain a giant blob that contains 2 pieces of data in json

    The data can then be extracted through a custom parser. From there the needed data can be extracted.
    """
    CASES_URL = 'https://tableau.azdhs.gov/views/COVID19Demographics/EpiData?:embed=y&:showVizHome=y'
    DEATHS_URL = 'https://tableau.azdhs.gov/views/COVID-19Deaths/Deaths?:embed=y&:showVizHome=y'

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def _scrape(self, **kwargs):
        runner = WebdriverRunner()
        cases_results = runner.run(
            WebdriverSteps()
            .go_to_url(self.CASES_URL)
            .find_request(key='cases', find_by=find_tableau_request))

        deaths_results = runner.run(
            WebdriverSteps()
            .go_to_url(self.DEATHS_URL)
            .find_request(key='deaths', find_by=find_tableau_request))

        assert cases_results.requests['cases'], 'No results found for `cases`'
        resp_body = cases_results.requests['cases'].response.body.decode('utf8')
        tableau_parser = TableauParser(resp_body)

        parsed_date = tableau_parser.extract_data_from_key('Date Updated')
        assert 'Date Updated' in parsed_date, 'Unable to parse date'
        assert len(parsed_date['Date Updated']) == 1, 'Unable to parse date'
        date_str = pydash.head(parsed_date['Date Updated'])
        date = datetime.strptime(date_str, '%m/%d/%Y').date()

        parsed_num_cases = tableau_parser.extract_data_from_key('Number of Cases')
        assert 'SUM(Number of Records)' in parsed_num_cases, 'Key not found, unable to parse number of records'
        assert len(parsed_num_cases['SUM(Number of Records)']) == 1, 'Parsing error might have occurred'
        cases = pydash.head(parsed_num_cases['SUM(Number of Records)'])

        parsed_race_eth = tableau_parser.extract_data_from_key('Race/Ethnicity Epi')
        parsed_race_eth_df = pd.DataFrame.from_dict(parsed_race_eth).set_index('Raceeth')
        aa_cases = parsed_race_eth_df.loc['Black, non-Hispanic']['AGG(RecordCount)']
        known_race_cases = cases - parsed_race_eth_df.loc['Unknown']['AGG(RecordCount)']

        assert deaths_results.requests['deaths'], 'No results found for `deaths`'
        resp_body = deaths_results.requests['deaths'].response.body.decode('utf8')
        tableau_parser = TableauParser(resp_body)
        parsed_death_cases = tableau_parser.extract_data_from_key('Number of deaths')
        assert 'SUM(Death count)' in parsed_death_cases, 'Death count not found'
        assert len(parsed_death_cases['SUM(Death count)']) == 1, 'Parsing error might have occurred.'
        deaths = pydash.head(parsed_death_cases['SUM(Death count)'])

        parsed_deaths_by_race = tableau_parser.extract_data_from_key('Death Race/Ethnicity')
        parsed_deaths_by_race_df = pd.DataFrame.from_dict(parsed_deaths_by_race).set_index('Raceeth')
        aa_deaths = parsed_deaths_by_race_df.loc['Black, non-Hispanic']['AGG(RecordCount)']
        known_race_deaths = deaths - parsed_deaths_by_race_df.loc['Unknown']['AGG(RecordCount)']

        pct_aa_cases = misc.to_percentage(aa_cases, known_race_cases)
        pct_aa_deaths = misc.to_percentage(aa_deaths, known_race_deaths)

        return [self._make_series(
            date=date,
            cases=cases,
            deaths=deaths,
            aa_cases=aa_cases,
            aa_deaths=aa_deaths,
            pct_aa_cases=pct_aa_cases,
            pct_aa_deaths=pct_aa_deaths,
            pct_includes_unknown_race=False,
            pct_includes_hispanic_black=False,
            known_race_cases=known_race_cases,
            known_race_deaths=known_race_deaths
        )]


import sys
sys.path.append('../backup_lhcscrub_python/LHCMeasurementTools')
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import argparse

import myfilemanager as mlo
import mystyle as ms
from d000_analysis.LHC_Heatloads import magnet_length
from RcParams import init_pyplot
init_pyplot()

parser = argparse.ArgumentParser(description='Show bunch by bunch power losses from PyECLOUD simulations and measurements.')
parser.add_argument('-q', help='Quadrupole SEY', metavar='Quad SEY', type=float)
parser.add_argument('-d', help='Drift SEY', metavar='Drift SEY', type=float)
parser.add_argument('beam', help='B1 | B2', type=str, metavar='BEAM')
args = parser.parse_args()

if args.beam != 'B1' and args.beam != 'B2':
    raise ValueError('Incorrect specification of beam!')

assumed_quad_sey = args.q
assumed_drift_sey = args.d

T_rev = 88.9e-6
qe = 1.60217657e-19

main_folder = './all_data'
beam_snapshot_folder = './beam_snapshots'

machine_name = 'LHC'
devices = ['Drift', 'ArcDipReal', 'ArcQuadReal']
device_name = 'ArcDipReal'
beam_name = '6500GeV'

coast_strs = ['1.0', '0.5', '0.0']
coast_linestyle_dict = {\
        '1.0': '-',
        '0.5': '-.',
        '0.0': ':'
        }

l_hc = magnet_length['AVG_ARC'][0]
l_dip = 3 * magnet_length['special_HC_D2'][0]
l_quad = magnet_length['special_HC_Q1'][0]
l_drift = l_hc - l_dip - l_quad

#beam_snapshot = 'Fill5219_cut0.920h_450GeV_for_triplets_B1.mat'
beam_snapshot = 'Fill5219_cut1.800h_6500GeV_for_triplets_%s.mat' % args.beam

ob_snapshot = mlo.myloadmat_to_obj(beam_snapshot_folder+'/'+beam_snapshot)
meas_loss_per_meter = ob_snapshot.bunch_power_loss/23./2./8./l_hc
meas_loss_per_hc = meas_loss_per_meter * l_hc

sey_vect = np.arange(1.10,1.501,.05)

if 'B1' in beam_snapshot:
    colorbeam = 'b'
elif 'B2' in beam_snapshot:
    colorbeam = 'r'
else:
    raise ValueError('Beam type (B1 or B2) could not be infered from the name of the snapshot')

N_sims = len(sey_vect)

def get_simulation_ob(device_name, sey, coast_str='0.5'):

    sim_ident = '%s_%s_%s_%s_sey%.2f_coast%s' % (beam_snapshot.split('.mat')[0], machine_name, device_name, beam_name, sey, coast_str)
    ob = mlo.myloadmat_to_obj(main_folder + '/' + sim_ident+'/Pyecltest.mat')
    t_bun = np.arange(0.,np.max(ob.t),  ob.b_spac)
    L_imp_t = np.cumsum(ob.En_imp_eV_time-ob.En_emit_eV_time); #in eV
    L_imp = np.interp(t_bun, ob.t, L_imp_t) #in eV
    Ek = np.interp(t_bun, ob.t, ob.En_kin_eV_time);
    sim_loss_per_meter = (np.diff(L_imp) + np.diff(Ek))*qe/T_rev; #watt/m

    return sim_loss_per_meter, ob, t_bun

quad_contribution_per_meter, quad_ob,t_bun = get_simulation_ob('ArcQuadReal', assumed_quad_sey)
drift_contribution_per_meter, drift_ob,t_bun = get_simulation_ob('Drift', assumed_drift_sey)

plt.close('all')
fig = plt.figure()
title_str = 'Comparison of measured to simulated bunch by bunch power losses.'
fig.canvas.set_window_title(title_str)
title_str += '\nAssumed Quad/Drift SEY: %.2f/%.2f' % (assumed_quad_sey,assumed_drift_sey)
plt.suptitle(title_str,fontsize=25)
sp0 = plt.subplot(3,1,1)
sp1 = plt.subplot(3,1,2, sharex=sp0)

# plot simulation data
for coast_ctr, coast_str in enumerate(coast_strs):
    for sey_ctr, sey in enumerate(sey_vect):
        #print beam_snapshot, device_name, sey

        try:
            dip_contribution_per_meter, ob, t_bun = get_simulation_ob('ArcDipReal', sey, coast_str)
            sim_loss_per_hc = dip_contribution_per_meter * l_dip + quad_contribution_per_meter * l_quad + drift_contribution_per_meter * l_drift

            if sey_ctr == 0:
                label = coast_str + ' e9 coasting'
            else:
                label = None
            color_curr = ms.colorprog(sey_ctr, N_sims)

            sp1.semilogy(ob.t/25e-9, ob.Nel_timep, color=color_curr, label=label, ls=coast_linestyle_dict[coast_str])

            if coast_ctr == 0:
                label = sey
            else:
                label = None

            sp2 = plt.subplot(3,1,3, sharex=sp1)
            ms.sciy()
            sp2.plot(t_bun[:-1]/25e-9, sim_loss_per_hc, '.-', color=color_curr, label=label, ls=coast_linestyle_dict[coast_str])

        except IOError as err:
            print('Got:', err)

sp0.plot(ob_snapshot.ppb_vect, color=colorbeam,label='Measured')
sp0.set_ylabel('Bunch intensity [p$^+$]')
sp0.set_xlim(0,3500)

sp1.set_ylabel('Electrons [1/m]')

sp2.plot(meas_loss_per_hc, '.-', color='k', label='Measured')
sp2.set_ylabel('Power loss [W/hc]')
sp2.set_xlabel('25 ns slot')

sp1.legend(bbox_to_anchor=(1, 1.02),  loc='upper left')
sp2.legend(bbox_to_anchor=(1, 1.02),  loc='upper left')

# Figure for devices only
fig = plt.figure()
title_str = 'Simulated bunch by bunch power losses for different devices.'
fig.canvas.set_window_title(title_str)
plt.suptitle(title_str,fontsize=25)

sp = None
for dev_ctr, device in enumerate(devices):
    sp_ctr = dev_ctr +1
    sp = plt.subplot(3,1,sp_ctr, sharex=sp)
    sp.set_title(device)
    ms.sciy()
    sp.set_ylabel('Power loss [W/m]')
    if sp_ctr == 3:
        sp.set_xlabel('25 ns slot')

    for coast_ctr, coast_str in enumerate(coast_strs):
       for sey_ctr, sey in enumerate(sey_vect):
           try:
               contribution_per_meter, ob, t_bun = get_simulation_ob(device, sey, coast_str)

               color_curr = ms.colorprog(sey_ctr, N_sims)
               if sp_ctr == 1 and coast_ctr == 0:
                   label = sey
               elif sp_ctr == 2 and sey_ctr == 0:
                   label = coast_str + ' e9 coasting'
               else:
                   label = None

               sp.plot(t_bun[:-1]/25e-9, contribution_per_meter, '.-', color=color_curr, label=label, ls=coast_linestyle_dict[coast_str])

           except IOError as err:
                print('Got:', err)

    if sp_ctr == 1 or sp_ctr == 2:
        sp.legend(bbox_to_anchor=(1, 1.02),  loc='upper left')

plt.show()


#!/usr/bin/env python3

# Problem
# A common substring of a collection of strings is a substring of every member of the collection. We say that a common substring is a longest common substring if there does not exist a longer common substring. For example, "CG" is a common substring of "ACGTACGT" and "AACCGTATA", but it is not as long as possible; in this case, "CGTA" is a longest common substring of "ACGTACGT" and "AACCGTATA".
#
# Note that the longest common substring is not necessarily unique; for a simple example, "AA" and "CC" are both longest common substrings of "AACC" and "CCAA".
#
# Given: A collection of k (k≤100) DNA strings of length at most 1 kbp each in FASTA format.
#
# Return: A longest common substring of the collection. (If multiple solutions exist, you may return any single solution.)
#
# Sample Dataset
# >Rosalind_1
# GATTACA
# >Rosalind_2
# TAGACCA
# >Rosalind_3
# ATACA
# Sample Output
# AC

import sys

"""
Input:
a collection of k DNA strings in FASTA format

Output:
Longest common substring of collection
"""

def fasta_file(filename):
    #only need to collect a list of DNA string
    samples=[]
    with open(filename) as file:
        #separate each entries  return list
        individual = file.read().split(">")[1:]
        #separate name and DNA string within entries    return list of DNA string
        for i in individual:
            dna = i.partition("\n")[-1].strip().replace("\n", "")
            samples.append(dna)
    return sorted(samples)

def lcs(filename):
    strings = fasta_file(filename)
    short_string = strings[0]
    other_string = strings[1:]

    l = len(short_string)
    longest_string= ""

    for i in range(0, l):
        for j in range(l, i+len(longest_string), -1):
            s1 = short_string[i:j]

            matched_all = True
            for s2 in other_string:
                if s1 not in s2:
                    matched_all = False
                    break
            if matched_all:
                longest_string=s1
                break
    return longest_string

def main():
    filename=sys.argv[1]
    print(lcs(filename))

if __name__ == "__main__":
    main()


import os
import csv
import vtk
import psutil
import datetime
import numpy as np

INDEX_BASE_X = 9
INDEX_QUATERNION_START_ROW = 34
INDEX_BIAS_X = 38

POINT_DIMENSION = 3
QUATERNION_DIMENSION = 4

INDEX_BASE_X_IN_ARRANGEMENT = 0
INDEX_SHARP_X_IN_ARRANGEMENT = 6

TIP_TO_CENTER_ON_SHARPPROBE= [0, -121, 0]

WRITE_PATH = "E:/tmc/vtk_development/goldFinger/test01/refactor08.csv"
CSV_FOLDER = "E:\\tmc\\vtk_development\\goldFinger\\1"

def readFile(path):
    with open(path, 'r') as csvfile:
        reader = csv.reader(csvfile)
        next(reader)
        column = [row for row in reader]

        basePoint = readSpecificColumnsFromCSV(INDEX_BASE_X, POINT_DIMENSION, column)
        quaternion = readSpecificColumnsFromCSV(INDEX_QUATERNION_START_ROW,QUATERNION_DIMENSION, column)
        bias = readSpecificColumnsFromCSV(INDEX_BIAS_X, POINT_DIMENSION, column)
        arrangementListOfThreeVector = [basePoint,quaternion,bias]
    return arrangementListOfThreeVector

def readSpecificColumnsFromCSV(coordinatesStartColumn, vectorDimension, completeCSVData):
    data_list = []
    for i in range(vectorDimension):
        for index in range(len(completeCSVData)):
            data_list.append(completeCSVData[index][coordinatesStartColumn])
        coordinatesStartColumn = coordinatesStartColumn + 1
    data_list = np.array(data_list)
    data_list = data_list.reshape(vectorDimension, len(completeCSVData))
    data_list = np.transpose(data_list) # 其实不采取转置操作也可行，后面取数据时只需将原来的取列方式改为取行方式即可
    data_list = data_list.astype(float)
    return data_list

def rotateAndBias(quaternion,bias):
    rotatedVector = []
    for i in range(len(quaternion)):
        rotate = vtk.vtkMath()
        ans = [0, 0, 0]
        rotate.RotateVectorByNormalizedQuaternion(TIP_TO_CENTER_ON_SHARPPROBE, quaternion[i], ans)
        rotatedVector.append(ans)
    rotatedVector = np.array(rotatedVector)
    tipOfSharpProbe = []
    for index in range(len(bias)):
        tipOfSharpProbe.append(bias[index] + rotatedVector[index])
    tipOfSharpProbe = np.array(tipOfSharpProbe)
    return tipOfSharpProbe

def arrangeMeanAndVariance(path):
    pointsList = readFile(path)
    testPoint = pointsList[0]
    quaternion = pointsList[1]
    bias = pointsList[2]

    goldFinger = rotateAndBias(quaternion, bias)
    organizedPrintedData = []
    for index in range(3):
        organizedPrintedData.append(np.mean(testPoint[:, index])) # Use numpy instead of statistic library
    for index in range(3):
        organizedPrintedData.append(np.var(testPoint[:, index]))
    for index in range(3):
        organizedPrintedData.append(np.mean(goldFinger[:, index]))
    for index in range(3):
        organizedPrintedData.append(np.var(goldFinger[:, index]))

    return organizedPrintedData

def calcDistance(sharpPoint,basePoint):
    d_x = sharpPoint[0] - basePoint[0]
    d_y = sharpPoint[1] - basePoint[1]
    d_z = sharpPoint[2] - basePoint[2]
    distance = np.sqrt(d_x ** 2 + d_y ** 2 + d_z ** 2)# Remove the reference math library
    return distance

def writePoint2csv(path,writer):
    pointName = os.path.basename(path)[0]# Remove the reference pathlib library
    writeList = arrangeMeanAndVariance(path)
    sharpPoint = writeList[INDEX_SHARP_X_IN_ARRANGEMENT:INDEX_SHARP_X_IN_ARRANGEMENT+3]
    basePoint = writeList[INDEX_BASE_X_IN_ARRANGEMENT:INDEX_BASE_X_IN_ARRANGEMENT+3]
    distance = calcDistance(sharpPoint,basePoint)

    writeList.insert(0, pointName)
    writeList.append(distance)
    writer.writerow(writeList)

def main():
    writePath = WRITE_PATH
    with open(writePath, 'w', encoding='utf-8-sig', newline="") as csvfile:# write instead of append
        csvWriter = csv.writer(csvfile)
        folder = CSV_FOLDER
        fileList = sorted(os.listdir(CSV_FOLDER)) #File names are sorted alphabetically, Optimized the way files are read sequentially
        csvWriter.writerow(
            ["点", "tx均值", "ty均值", "tz均值", "tx方差", "ty方差", "tz方差", "cx均值", "cy均值", "cz均值", "cx方差", "cy方差", "cz方差",
             "两点距离"])
        for filename in fileList:
            path = os.path.join(folder, filename)
            writePoint2csv(path, csvWriter)

if __name__ == '__main__':
    main()
    print(u'Memory usage:   %.4f GB' % (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024))
    print("finishing time:", datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))


"""
员工接口
"""
import requests

import app

class EmpApi(object):
    def __init__(self):
        self.add_emp_api = app.BASE_URL + "/api/sys/user"

    # 添加员工
    def add_emp(self, username, mobile, headers):
        data = {
            "username": username,
            "mobile": mobile,
            "timeOfEntry": "2020-05-05",
            "formOfEmployment": 1,
            "departmentName": "测试部",
            "departmentId": "1063678149528784896",
            "correctionTime": "2020-05-30T16:00:00.000Z"
        }
        return requests.post(self.add_emp_api, json=data, headers=headers)

    # 查询员工
    def query_emp(self, emp_id, headers):
        query_url = self.add_emp_api + "/" + emp_id
        return requests.get(query_url, headers=headers)

    # 修改员工
    def put_emp(self, emp_id, data, headers):
        put_url = self.add_emp_api + "/" + emp_id
        return requests.put(put_url, json=data, headers=headers)

    # 删除员工
    def del_emp(self, emp_id, headers):
        del_url = self.add_emp_api + "/" + emp_id
        return requests.delete(del_url, headers=headers)


#%%
# -*- coding: utf-8 -*-

# Script de Test de psutil
import psutil
import datetime
import time
import os

while True:
    os.system('cls')

    # tasa de uso de cpu
    cup_per = psutil.cpu_percent()

    # Información de la memoria
    memory_info = psutil.virtual_memory()

    # Información del disco duro
    disk_info = psutil.disk_usage("/") # Información del disco del directorio raíz

    # Información de Internet
    net_info = psutil.net_io_counters()

    # Obtener la hora actual del sistema
    current_time = datetime.datetime.now().strftime("%F %T") #% F año mes día% T hora, minuto y segundo

    # Pantalla de mosaico
    log_str = "|---------------------|----------|----------|----------|-----------------------------|\n"
    log_str+= "|---------time--------|---cpu----|--memory--|---disk---|-------------net-------------|\n"
    log_str+= "|                     |  {}core  | {:.2f}G |  {:.2f}  |                             |\n".format(psutil.cpu_count(logical=False),
                                                                                memory_info.total/1024/1024/1024,
                                                                                disk_info.total/1024/1024/1024)
    log_str+= "| {} |   {}%%  |   {}%%  |   {}%%  | in:{} out:{} |\n".format(current_time,
                                                                         cup_per, memory_info.percent,
                                                                         disk_info.percent,
                                                                         net_info.bytes_recv,
                                                                         net_info.bytes_sent)
    print(log_str)

    time.sleep(2)


import pycountry
from text_processing import text_normalizer
from utilities import excel_writer, utils

class ProvincesDistrictsDownloader:

    def __init__(self, path_to_gadm_shp="../tmp/gadm36_shp/gadm36.shp"):
        self.country_codes = self.create_countries_codes_map()
        self.freq_words = text_normalizer.get_all_freq_words()
        with shapefile.Reader(path_to_gadm_shp) as f:
            self.records = f.records()

    def get_country_name(self, key):
        country_name = ""
        try:
            country = pycountry.countries.get(alpha_2=key)
            if country != None:
                country_name = text_normalizer.normalize_country_name(country.name)
        except:
                print("Key " + key + " not found")
        return country_name

    def normalize_country_name(country_name):
        mapping = {"Aland":"Aland Islands",  "Brunei":"Brunei Darussalam",\
             "Czech Republic":"Czechia", "Democratic Republic of the Congo":"Congo",
             "Laos":"Lao People's Democratic Republic",\
             "Republic of Congo":"Congo", "Russia":"Russian Federation",
             "Northern Cyprus":"Cyprus", "Palestina":"Palestine",\
              "Syria":"Syrian Arab Republic", "Vietnam":"Viet Nam"}
        norm_country = text_normalizer.normalize_country_name(country_name)
        if norm_country in mapping:
            return mapping[norm_country]
        return norm_country

    def create_countries_codes_map(self):
        country_codes = {}
        for country in pycountry.countries:
            norm_country = text_normalizer.normalize_country_name(country.name)
            if norm_country != "Korea":
                country_codes[norm_country] = country.alpha_2
        country_codes["North Korea"] = "KP"
        country_codes["South Korea"] = "KR"
        return country_codes

    def derive_provinces_dictionary():
        dict_with_provinces = {}
        for idx, record in enumerate(self.records):
            country = self.normalize_country_name(record[3])
            name = record[6]
            var_names = record[7].split("|")
            district_name = record[18]
            var_district_names = record[19].split("|")
            if name == '':
                continue
            for dist_name in list(filter(None, var_district_names + [district_name, name] + var_names)):
                dist_name = dist_name.split("(")[0].split("[")[0].split(",")[0].strip()
                if dist_name not in dict_with_provinces:
                    dict_with_provinces[dist_name] = {}
                if country not in dict_with_provinces[dist_name]:
                    dict_with_provinces[dist_name][country] = set()
                dict_with_provinces[dist_name][country].add(name)
        keys_to_delete = set()
        for key in dict_with_provinces:
                if len(dict_with_provinces[key]) > 1:
                    keys_to_delete.add(key)
        for key in keys_to_delete:
            del dict_with_provinces[key]
        dict_with_provinces = self.enrich_with_subdivisions_map(dict_with_provinces)
        return dict_with_provinces

    def is_word_to_be_filtered(self, word):
        filtration_words = ["universit", "north","south","east","west",
                            "centr", "middl", "asia", "europ", "union",
                            "enterpris", "cocoa","summit", "savann", "spring",
                            "district", "province", "coast", "upper","lower",
                            "atlantic"]
        if word.lower() in freq_words or word.lower() in text_normalizer.stopwords_all or text_normalizer.is_abbreviation(word):
            return True
        for w in text_normalizer.get_stemmed_words_inverted_index(word):
            is_filtered = False
            for filter_word in filtration_words:
                if filter_word in w.lower():
                    is_filtered = True
                    break
            if not is_filtered and not text_normalizer.is_abbreviation(w):
                return False
        return True

    def enrich_with_subdivisions_map(self, dict_with_provinces):
        subdivisions_map = list(pycountry.subdivisions)
        for subdivision in subdivisions_map:
            sub_name = subdivision.name.split("(")[0].split("[")[0].split("/")[0].split(",")[0].strip()
            country_name = self.get_country_name(subdivision.country_code)
            if sub_name not in dict_with_provinces:
                sub_broad_name = sub_name
                max_sim = 0.0
                for dist in self.dict_with_districts:
                    try:
                        for broad_name in dict_with_provinces[dist][country_name]:
                            max_sim_term = max(
                                utils.normalized_levenshtein_score(broad_name, sub_name),
                                utils.normalized_levenshtein_score(dist, sub_name))
                            if max_sim_term > 0.7 and max_sim_term > max_sim:
                                max_sim = max_sim_term
                                sub_broad_name = broad_name
                    except:
                        pass
                dict_with_provinces[sub_name] = {country_name: set([sub_broad_name])}
                if sub_broad_name not in dict_with_provinces:
                    dict_with_provinces[sub_broad_name] = {country: set([sub_broad_name])}
        return dict_with_provinces

    def derive_districts_dictionary():
        dict_with_districts = {}
        for idx, record in enumerate(self.records):
            country = self.normalize_country_name(record[3])
            name = record[6]
            var_names = record[7].split("|")
            district_name = record[18]
            var_district_names = record[19].split("|")
            if district_name == '':
                continue
            for dist_name in list(filter(None, var_district_names + [district_name])):
                dist_name = dist_name.split("(")[0].split("[")[0].split(",")[0].strip()
                if dist_name not in dict_with_districts:
                    dict_with_districts[dist_name] = {}
                if country not in dict_with_districts[dist_name]:
                    dict_with_districts[dist_name][country] = set()
                dict_with_districts[dist_name][country].add((district_name, name))

        keys_to_delete = set()
        for key in dict_with_districts:
                if len(dict_with_districts[key]) > 1:
                    keys_to_delete.add(key)
        for key in keys_to_delete:
            del dict_with_districts[key]
        return dict_with_districts

    def save_provinces_dictionary(self, filename="provinces.xlsx"):
        self.derive_provinces_dictionary()
        pairs_to_write = []
        for key in dict_with_provinces:
            for country in dict_with_provinces[key]:
                for dist_name in dict_with_provinces[key][country]:
                    if not self.is_word_to_be_filtered(key) and not self.is_word_to_be_filtered(dist_name):
                            if country in country_codes:
                                country_code = country_codes[country]
                            else:
                                continue
                            pairs_to_write.append((key, dist_name, country, country_code))
        excel_writer.ExcelWriter().save_data_in_excel(
            pairs_to_write, ["Keyword", "Province", "Country", "Country code"], filename)

    def save_districts_dictionary(self, filename="districts.xlsx"):
        pairs_to_write = []
        for key in self.dict_with_districts:
            for country in self.dict_with_districts[key]:
                for dist_name, prov_name in self.dict_with_districts[key][country]:
                    if not self.is_word_to_be_filtered(key) and not self.is_word_to_be_filtered(dist_name):
                        if country in country_codes:
                            country_code = country_codes[country]
                        else:
                            continue
                        pairs_to_write.append((key, dist_name, prov_name, country, country_code))
        excel_writer.ExcelWriter().save_data_in_excel(
            pairs_to_write, ["Keyword", "District", "Province", "Country", "Country code"], filename)

    def find_provinces_districts(self, filename_for_districts, filename_for_provinces):
        self.dict_with_districts = self.derive_districts_dictionary()
        self.dict_with_provinces = self.derive_provinces_dictionary()
        self.save_districts_dictionary(filename=filename_for_districts)
        self.save_provinces_dictionary(filename=filename_for_provinces)


import requests, zlib, base64
import random, re, json
from datetime import datetime
from bs4 import BeautifulSoup
from getproxy import  get_proxy

def encode_url(data, isStringify=False):
    if (isStringify == True):
        data_compressed = zlib.compress(data.encode())
        data_base64 = base64.b64encode(data_compressed)
        return data_base64
    else:
        data_json = json.dumps(data).replace(' ', "")
        data_url = encode_url(data_json, True)

def decode_url(data_encoded):
    if isinstance(data_encoded, str):
        data_compressed = base64.b64decode(data_encoded)
        data = zlib.decompress(data_compressed)
        return data

sign = "eJxTSixITE+1NVTLSSyxNTLWMzQyszQ0U8vJS7c1NDTWMzI1AnFBauIz81JSK2wNlQByMw5p"
result1 = decode_url(encode_url(sign))
print(result1)

original_sign = "apage=1&category_type=0&page_index=0&second_category_type=0"
result2 = decode_url(encode_url(original_sign))
print(result2)

# token:
# eJydkNtKw0AQht9lIL1pSDaJOUKQiAmmnmgaUlSkrNttu5hTk20OiO/uFipWLx0G/
# plvhuFnPqCJ1+BpSIQrQ0cb8EBTkGKBDLwVE9NwLMvUHdPQTRnIL2YhZMvw1mTX4L
# 3Ypi5ryNJfjyQR4IyclfqFyONOLFZgx3ntqSpTeswKzJSCMn7ApUKqQi04U8kOlyX
# NLwnmdFs144qPNfXRpKWkKterP1h4/t9BV0OTH0IH7kuhLV2FkhNKoSsFhuRGIEwX
# qTAt9P2k+KT8u78X7xMuWrYtRUVnQ7pgQ5yGU+0hS+5SNsxS4nT7qu/2iyHmaTYP+
# vLmeYrzOnoKHsmyHje5ebuJKlzPgyYJl1nv+/D5BRBXgbM=

# sign:
# eJxTSixITE+1NVRLTixJTc8vqowvqSxItTVQAwnHZ+alpFYAOcWpyfl5KfFoapQArREWVw==
# {"sign":"apage=1&category_type=0&page_index=0&second_category_type=0"}


import os

lang = ['cs','de','en','zh']
for l in lang:
	lst = os.listdir('/Users/ariaduan1.0/Desktop/summer_experiment/Roger/projects/Jon/Selectional_preference/core/split/' + l + '/')
	for i in lst:
		if str(i)[:3] != 'v_a':
			continue
		print(str(i))
		file = open('/Users/ariaduan1.0/Desktop/summer_experiment/Roger/projects/Jon/Selectional_preference/core/split/' + l + '/' + str(i),'rb')
		print(str(i)[9:])
		fileout = open('/Users/ariaduan1.0/Desktop/summer_experiment/Roger/projects/Jon/Selectional_preference/core/split/' + l + '/' + 'v_a_pairs' + str(i)[9:], 'w', encoding = 'utf-8')
		for line in file:
			line = line.decode('utf-8').split()
			for j in line[1:]:
				fileout.write(line[0] + '\t' + j + '\n')
		file.close()
		fileout.close()


class Solution:
    def maxSumOfThreeSubarrays(self, nums: List[int], k: int) -> List[int]:
        prefSum = [0]
        sums = defaultdict(int)

        for i, num in enumerate(nums):
            prefSum.append(prefSum[-1] + num)
            if i >= k:
                sums[i - k] = prefSum[i] - prefSum[i - k]

        sums[len(nums) - k] = prefSum[-1] - prefSum[len(nums) - k]

        @cache
        def dp(i, count):
            if count == 0 or i >= len(sums):
                return (0, 0, 0, 0)

            take = dp(i + k, count - 1)
            first = i if count == 3 else take[1]
            sec = i if count == 2 else take[2]
            third = i if count == 1 else take[3]

            take = (take[0] + sums[i], first, sec, third)
            skip = dp(i + 1, count)

            if take[0] != skip[0]:
                return take if take[0] > skip[0] else skip

            for i in range(1, 4):
                if take[i] != skip[i]:
                    return take if take[i] < skip[i] else skip

        return dp(0, 3)[1:]


from __future__ import print_function

import os
import sys
import re
import xmlrpclib

import cherrypy
from cherrypy import _cptools

try: import simplejson as json
except: import json

TWISTER_PATH = os.getenv('TWISTER_PATH')
if not TWISTER_PATH:
    print('TWISTER_PATH environment variable is not set! Exiting!')
    exit(1)
if TWISTER_PATH not in sys.path:
    sys.path.append(TWISTER_PATH)

try:
    import cleartool
except Exception as e:
    print('Cannot import ClearTool !'.format(e))

#

class CC(_cptools.XMLRPCController):

    @cherrypy.expose
    def hello(self):
        """
        Hello function, for testing connection.
        """
        return True

    @cherrypy.expose
    def cmd(self, args):
        """
        Send cleartool command.
        setview ... endview ... ls ...
        """
        if not isinstance(args, dict):
            return False

        args = {k: v[0] if isinstance(v, list) else v for k, v in args.iteritems()}

        if not args.has_key('command') or not isinstance(args['command'], str):
            return False

        if args['command'] == 'exit':
            print('Clearcase Plugin Srv: Shutting down...')
            cherrypy.engine.exit()
        elif args['command'].startswith('setview'):
            print('Clearcase Plugin Srv: Changing view: `{}`.'.format(args['command']))

        _r = cleartool.cmd(args['command'])

        response = {
            'status': _r[0],
            'data':   _r[1],
            'error':  _r[2],
        }

        return json.dumps(response)

    @cherrypy.expose
    def getTestDescription(self, user, fname):
        """
        Returns the title, description and all tags from a test file.
        """
        try: text = open(fname,'rb').read()
        except: return ''

        li_tags = re.findall('^[ ]*?[#]*?[ ]*?<(?P<tag>\w+)>([ -~\n]+?)</(?P=tag)>', text, re.MULTILINE)
        tags = '
\n'.join(['' + title + ' : ' + descr.replace('<', '<') for title, descr in li_tags])

        data = cleartool.cmd( 'ls {}'.format(fname) )[1]
        if data:
            data = data.split()[0].split('@@')[1]
            extra_info = 'ClearCase Version : {}'.format(data)

        return tags + '
\n' + extra_info

    @cherrypy.expose
    def getTestFile(self, fname):
        """
        Send 1 ClearCase file.
        """
        try: open(fname,'r')
        except: return ''

        with open(fname, 'rb') as handle:
            return xmlrpclib.Binary(handle.read())

#

if __name__ == "__main__":

    serverPort = sys.argv[1:2]

    if not serverPort:
        print('Clearcase Plugin Srv: Must start with parameter PORT number!')
        exit(1)
    else:
        try:
            serverPort = int(serverPort[0])
        except:
            print('Clearcase Plugin Srv: Must start with parameter PORT number!')
            exit(1)

    # Config
    conf = {'global': {
            'server.socket_host': '127.0.0.1',
            'server.socket_port': serverPort,
            'server.thread_pool': 2,
            'engine.autoreload.on': False,
            'log.screen': False
            }
        }

    print('Starting Clearcase Plugin Srv on `{}`.'.format(serverPort))

    # Start !
    cherrypy.quickstart(CC(), '/', config=conf)

    print('Ending Clearcase Plugin Srv from `{}`.'.format(serverPort))


"""
20.1 图像金字塔原理
一般情况下，我们要处理是一副具有固定分辨率的图像。但是有些情况下，我们需要对同一图像的不同分辨率的子图像进行处理。
比如，我们要在一幅图像中查找某个目标，比如脸，我们不知道目标在图像中的尺寸大小。这种情况下，
我们需要创建创建一组图像，这些图像是具有不同分辨率的原始图像。我们把这组图像叫做图像金字塔（简单来说就是同一图像的不同分辨率的子图集
合）。如果我们把最大的图像放在底部，最小的放在顶部，看起来像一座金字塔，故而得名图像金字塔。
有两类图像金字塔：高斯金字塔和拉普拉斯金字塔。
高斯金字塔的顶部是通过将底部图像中的连续的行和列去除得到的。顶部图像中的每个像素值等于下一层图像中5 个像素的高斯加权平均值。
这样操作一次一个MxN 的图像就变成了一个M/2xN/2 的图像。所以这幅图像的面积就变为原来图像面积的四分之一。这被称为Octave。
连续进行这样的操作我们就会得到一个分辨率不断下降的图像金字塔。我们可以使用函数cv2.pyrDown() 和cv2.pyrUp() 构建图像金字塔。

"""
import cv2
import numpy as np

"""
函数cv2.pyrDown() 从一个高分辨率大尺寸的图像向上构建一个金子塔（尺寸变小，分辨率降低）。
pyrDown(InputArray src, OutputArray dst, Stream& stream=Stream::Null())
    src – Source image.
    dst – Destination image. Will have Size((src.cols+1)/2, (src.rows+1)/2) size and the same type as src .
因为一旦使用cv2.pyrDown()，图像的分辨率就会降低，信息就会被丢失。
"""
img = cv2.imread("../../../resource/img/1.jpg",0);
for i in range(4):
    img = cv2.pyrDown(img);
    cv2.imshow("img"+str(i),img);
    cv2.waitKey(1000);
cv2.destroyAllWindows();

"""
函数cv2.pyrUp() 从一个低分辨率小尺寸的图像向下构建一个金子塔（尺寸变大，但分辨率不会增加）。
pyrUp(InputArray src, OutputArray dst, Stream& stream=Stream::Null())
    src – Source image.
    dst – Destination image. Will have Size(src.cols*2, src.rows*2) size and the same type as src .
"""
img = cv2.imread("../../../resource/img/1.jpg",0);
for i in range(5):
    img = cv2.pyrUp(img);
    cv2.imshow("img"+str(i),img);
    cv2.waitKey(1000);
cv2.destroyAllWindows();

#拉普拉斯金字塔可以有高斯金字塔计算得来 Li = Gi - PyrUp(Gi+1)   i+1 为下标
#拉普拉金字塔的图像看起来就像边界图，其中很多像素都是0。他们经常被用在图像压缩中。

20.1 图像金字塔原理.py


# -*- coding: utf-8 -*-
import json

class PaytabsApiResponse(object):
    def __init__(self, status, response_status):
        self.status = status
        self.response_status = response_status.lower()
        self.data = {}

    def add_data(self, key, value):
        self.data.update({key:value})

    def get(self, key):
        return self.data[key] if key in self.data else None

def list_splitter(lst):
    return ' || '.join(lst)

def prepare_invoice(products):
    if isinstance(products, dict) and len(products.keys()) == 3 and \
            'names' in products.keys() and \
            'quantities' in products.keys() and \
            'prices' in products.keys() and \
            isinstance(products['names'], list) and \
            isinstance(products['quantities'], list) and \
            isinstance(products['prices'], list):
        products.update({'names': list_splitter(products['names'])})
        products.update({'prices': list_splitter(products['prices'])})
        products.update({'quantities': list_splitter(products['quantities'])})
        return products
    else:
        raise Exception("Products dictionary isn't in right format")