#!/usr/bin/env python
import rospy
import time
# Used to change the turtle's position.
from geometry_msgs.msg import Twist
# Used to get the turtle's position.
from turtlesim.msg import Pose

# This function is used to print the position of the turtle.
def callback(msg):
    # Condition used to print only when starting for the first time and stopping.
    if msg.linear_velocity == 0 and msg.angular_velocity == 0:
        # Setting the format of the printed message.
        # Print the x and y positions with 2 numbers after the decimal point.
        rospy.loginfo("(X: %.2f, Y: %.2f)" % (msg.x, msg.y))

# This function moves the turtle forward.
def move_forward():
    # The speed in the x-axis.
    msg.linear.x = 0.5
    # The current time.
    s_time = rospy.Time.now().to_sec()
    # The starting distance.
    current_distance = 0
    # While we have not passed 1 meter, keep moving.
    while current_distance <= 1:
        # Update the position by sending it throught the publisher.
        pub.publish(msg)
        # Get current time.
        f_time = rospy.Time.now().to_sec()
        # Calculate new position (x = v*t).
        current_distance = 0.5 * (f_time - s_time)
    # After the loop, stops the turtle.
    msg.linear.x = 0

# This function turns the turtle.
def change_angle():
    # Define pi.
    pi = 3.14
    # Consider the starting angel as 0.
    current_angle = 0
    # The current time.
    s_time = rospy.Time.now().to_sec()
    # The speed we will change angle at.
    turning_speed = 15 * 2 * pi / 360
    wanted_angle = 45 * 2 * pi / 360
    msg.angular.z = abs(turning_speed)
    # While we have not reached the wanted angle, keep turning.
    while current_angle < wanted_angle:
        # Update the angle by sending it through the publisher.
        pub.publish(msg)
        # Get current time.
        f_time = rospy.Time.now().to_sec()
        # Calculate new angle.
        current_angle = turning_speed * (f_time - s_time)
    # After the loop, stops the turtle.
    msg.angular.z = 0

# The main function.
if __name__ == "__main__":
    # Name the node to start so rospy can start communicating.
    rospy.init_node("move_turtle")
    # To make the turtle move in ROS we need to publish Twist messages to the topic cmd_vel.
    # Create a publisher.
    pub = rospy.Publisher("turtle1/cmd_vel", Twist, queue_size=10)
    # In order to print the turtle's pose we need to subscribe to the topic turtle1/pose.
    # When a new message is received , the callback function is invoked with the message as an argument.
    sub = rospy.Subscriber("turtle1/pose", Pose, callback)
    # This message has a linear component for the x,y,z velocities and an angular component for the
    # angular rate about the x,y,z axes.
    msg = Twist()
    # Setting the rate of the of the node, approximately 10 times per second.
    rate = rospy.Rate(10)
    # Start moving.
    move_forward()
    # When done moving, turn at a 45 degree angle.
    change_angle()
    # Let the final position be printed before finishing.
    time.sleep(2)


def count(n):
    global cnt
    if n:
        cnt += 1
        count(left[n])
        count(right[n])

for tc in range(1, int(input())+1):
    E, n = map(int, input().split())
    left = [0] * (E+2)
    right = [0] * (E+2)
    cnt = 0
    infos = list(map(int, input().split()))
    for i in range(E):
        idx, num = infos[2*i], infos[2*i+1]
        if not left[idx]:
            left[idx] = num
        else:
            right[idx] = num

    count(n)
    print(f'#{tc} {cnt}')


# For every good kata idea there seem to be quite a few bad ones!

# In this kata you need to check the provided array (x) for good ideas 'good' and bad ideas 'bad'.
# If there are one or two good ideas, return 'Publish!', if there are more than 2 return 'I smell a series!'.
# If there are no good ideas, as is often the case, return 'Fail!'.

def well(x):
    good = 0
    for i in range(len(x)):
        if x[i] == 'good':
            good+=1

    if good >0 and good <=2:
        return 'Publish!'
    elif good >2:
        return 'I smell a series!'
    else:
        return 'Fail!'


import os
from argparse import ArgumentParser
import argparse

import numpy as np
import torch

import src
from src.utils import MetricDict

def str2bool(v):
    if isinstance(v, bool):
        return v
    if v.lower() in ("yes", "true", "t", "y", "1"):
        return True
    elif v.lower() in ("no", "false", "f", "n", "0"):
        return False
    else:
        raise argparse.ArgumentTypeError("Boolean value expected.")

def parse_args(notebook=False):
    parser = ArgumentParser()

    # ----------------------------- Data options ---------------------------- #
    parser.add_argument(
        "--root",
        type=str,
        default="/Users/quentin/Documents/DLAV/translating-images-into-maps-main/nuscenes_data",
        help="root directory of the dataset",
    )
    parser.add_argument(
        "--nusc-version", type=str, default="v1.0-mini", help="nuscenes version",
    )
    parser.add_argument(
        "--occ-gt",
        type=str,
        default="200down100up",
        help="occluded (occ) or unoccluded(unocc) ground truth maps",
    )
    parser.add_argument(
        "--gt-version",
        type=str,
        default="semantic_maps_new_200x200",
        help="ground truth name",
    )
    parser.add_argument(
        "--train-split", type=str, default="train_mini", help="ground truth name",
    )
    parser.add_argument(
        "--val-split", type=str, default="val_mini", help="ground truth name",
    )
    parser.add_argument(
        "--data-size",
        type=float,
        default=0.2,
        help="percentage of dataset to train on",
    )
    parser.add_argument(
        "--load-classes-nusc",
        type=str,
        nargs=14,
        default=[
            "drivable_area",
            "ped_crossing",
            "walkway",
            "carpark_area",
            "road_segment",
            "lane",
            "bus",
            "bicycle",
            "car",
            "construction_vehicle",
            "motorcycle",
            "trailer",
            "truck",
            "pedestrian",
            "trafficcone",
            # "barrier",
        ],
        help="Classes to load for NuScenes",
    )
    parser.add_argument(
        "--pred-classes-nusc",
        type=str,
        nargs=12,
        default=[
            "drivable_area",
            "ped_crossing",
            "walkway",
            "carpark_area",
            "bus",
            "bicycle",
            "car",
            "construction_vehicle",
            "motorcycle",
            "trailer",
            "truck",
            "pedestrian",
            "trafficcone",
            # "barrier",
        ],
        help="Classes to predict for NuScenes",
    )
    parser.add_argument(
        "--lidar-ray-mask",
        type=str,
        default="dense",
        help="sparse or dense lidar ray visibility mask",
    )
    parser.add_argument(
        "--grid-size",
        type=float,
        nargs=2,
        default=(50.0, 50.0),
        help="width and depth of validation grid, in meters",
    )
    parser.add_argument(
        "--z-intervals",
        type=float,
        nargs="+",
        default=[1.0, 9.0, 21.0, 39.0, 51.0],
        help="depths at which to predict BEV maps",
    )
    parser.add_argument(
        "--grid-jitter",
        type=float,
        nargs=3,
        default=[0.0, 0.0, 0.0],
        help="magn. of random noise applied to grid coords",
    )
    parser.add_argument(
        "--aug-image-size",
        type=int,
        nargs="+",
        default=[1280, 720],
        help="size of random image crops during training",
    )
    parser.add_argument(
        "--desired-image-size",
        type=int,
        nargs="+",
        default=[1600, 900],
        help="size images are padded to before passing to network",
    )
    parser.add_argument(
        "--yoffset",
        type=float,
        default=1.74,
        help="vertical offset of the grid from the camera axis",
    )

    # -------------------------- Model options -------------------------- #
    parser.add_argument(
        "--model-name",
        type=str,
        default="PyrOccTranDetr_S_0904_old_rep100x100_out100x100",
        help="Model to train",
    )
    parser.add_argument(
        "-r",
        "--grid-res",
        type=float,
        default=0.5,
        help="size of grid cells, in meters",
    )
    parser.add_argument(
        "--frontend",
        type=str,
        default="resnet50",
        choices=["resnet18", "resnet34", "resnet50"],
        help="name of frontend ResNet architecture",
    )
    parser.add_argument(
        "--pretrained",
        type=bool,
        default=True,
        help="choose pretrained frontend ResNet",
    )
    parser.add_argument(
        "--pretrained-bem",
        type=bool,
        default=True,
        help="choose pretrained BEV estimation model",
    )
    parser.add_argument(
        "--pretrained-model",
        type=str,
        default="27_04_23_11_08",
        help="name of pretrained model to load",
    )
    parser.add_argument(
        "--load-ckpt",
        type=str,
        default="checkpoint-epfl-epoch-0016-mini-False-iou-1.pth.gz",
        help="name of checkpoint to load",
    )
    parser.add_argument(
        "--ignore", type=str, default=["nothing"], help="pretrained modules to ignore",
    )
    parser.add_argument(
        "--ignore-reload",
        type=str,
        default=["nothing"],
        help="pretrained modules to ignore",
    )
    parser.add_argument(
        "--focal-length", type=float, default=1266.417, help="focal length",
    )
    parser.add_argument(
        "--scales",
        type=float,
        nargs=4,
        default=[8.0, 16.0, 32.0, 64.0],
        help="resnet frontend scale factor",
    )
    parser.add_argument(
        "--cropped-height",
        type=float,
        nargs=4,
        default=[20.0, 20.0, 20.0, 20.0],
        help="resnet feature maps cropped height",
    )
    parser.add_argument(
        "--y-crop",
        type=float,
        nargs=4,
        default=[15, 15.0, 15.0, 15.0],
        help="Max y-dimension in world space for all depth intervals",
    )
    parser.add_argument(
        "--dla-norm",
        type=str,
        default="GroupNorm",
        help="Normalisation for inputs to topdown network",
    )
    parser.add_argument(
        "--bevt-linear-additions",
        type=str2bool,
        default=False,
        help="BatchNorm, ReLU and Dropout addition to linear layer in BEVT",
    )
    parser.add_argument(
        "--bevt-conv-additions",
        type=str2bool,
        default=False,
        help="BatchNorm, ReLU and Dropout addition to conv layer in BEVT",
    )
    parser.add_argument(
        "--dla-l1-nchannels",
        type=int,
        default=64,
        help="vertical offset of the grid from the camera axis",
    )
    parser.add_argument(
        "--n-enc-layers",
        type=int,
        default=2,
        help="number of transfomer encoder layers",
    )
    parser.add_argument(
        "--n-dec-layers",
        type=int,
        default=2,
        help="number of transformer decoder layers",
    )

    # ---------------------------- Loss options ---------------------------- #
    parser.add_argument(
        "--loss", type=str, default="dice_loss_mean", help="Loss function",
    )
    parser.add_argument(
        "--exp-cf",
        type=float,
        default=0.0,
        help="Exponential for class frequency in weighted dice loss",
    )
    parser.add_argument(
        "--exp-os",
        type=float,
        default=0.2,
        help="Exponential for object size in weighted dice loss",
    )

    # ------------------------ Optimization options ----------------------- #
    parser.add_argument("--optimizer", type=str, default="adam", help="optimizer")
    parser.add_argument("-l", "--lr", type=float, default=5e-5, help="learning rate")
    parser.add_argument("--momentum", type=float, default=0.9, help="momentum for SGD")
    parser.add_argument("--weight-decay", type=float, default=1e-4, help="weight decay")
    parser.add_argument(
        "--lr-decay",
        type=float,
        default=0.99,
        help="factor to decay learning rate by every epoch",
    )

    # ------------------------- Training options ------------------------- #
    parser.add_argument(
        "-e", "--epochs", type=int, default=40, help="number of epochs to train for"
    )
    parser.add_argument(
        "-b", "--batch-size", type=int, default=8, help="mini-batch size for training"
    )
    parser.add_argument(
        "--accumulation-steps",
        type=int,
        default=5,
        help="Gradient accumulation over number of batches",
    )

    # ------------------------ Experiment options ----------------------- #
    parser.add_argument(
        "--name", type=str,
        default="27_04_23_11_08",
        help="name of experiment",
    )
    parser.add_argument(
        "-s",
        "--savedir",
        type=str,
        default="pretrained_models",
        help="directory to save experiments to",
    )
    parser.add_argument(
        "-g",
        "--gpu",
        type=int,
        nargs="*",
        default=[0],
        help="ids of gpus to train on. Leave empty to use cpu",
    )
    parser.add_argument(
        "--num-gpu", type=int, default=1, help="number of gpus",
    )
    parser.add_argument(
        "-w",
        "--workers",
        type=int,
        default=0,
        help="number of worker threads to use for data loading",
    )
    parser.add_argument(
        "--val-interval",
        type=int,
        default=1,
        help="number of epochs between validation runs",
    )
    parser.add_argument(
        "--print-iter",
        type=int,
        default=5,
        help="print loss summary every N iterations",
    )
    parser.add_argument(
        "--vis-iter",
        type=int,
        default=20,
        help="display visualizations every N iterations",
    )
    parser.add_argument(
        "--cuda-available",
        type=int,
        default=0,
        help="defines cuda or cpu environment",
    )
    parser.add_argument(
        "--iou",
        type=int,
        default=1,
        help="defines iou metric to use (0 for iou, 1 for diou)",
    )
    parser.add_argument(
        "--video-root",
        type=str,
        default="/Users/quentin/Downloads",
        help="root for the video files",
    )
    parser.add_argument(
        "--video-name",
        type=str,
        default="test1",
        help="name of the video file within the video root and without extension",
    )
    if notebook:
        return parser.parse_args(args=[])
    else:
        return parser.parse_args()

def init(args):
    args.savedir = os.path.join(os.getcwd(), args.savedir)

    # Build depth intervals along Z axis and reverse
    z_range = args.z_intervals
    args.grid_size = (z_range[-1] - z_range[0], z_range[-1] - z_range[0])

    # Calculate cropped heights of feature maps
    h_cropped = src.utils.calc_cropped_heights(
        args.focal_length, np.array(args.y_crop), z_range, args.scales
    )
    args.cropped_height = [h for h in h_cropped]

    num_gpus = torch.cuda.device_count()
    args.num_gpu = num_gpus


from turtle import Screen #importa pantalla
from player import Player
from invader import Invader
import time
from scoreboard import Scoreboard
screen = Screen()
screen.setup(600,600)#tamaño pantalla
#screen.bgcolor("black")#cambia el color del fondo
screen.tracer(0)

player = Player()
invader = Invader()
scoreboard = Scoreboard()

screen.listen()
screen.onkey(player.turn_right, "Right")#aqui se pueden las teclas fisicas
screen.onkey(player.turn_left, "Left")
screen.onkey(player.create_missile, "space")

game_is_on = True

while game_is_on:
    time.sleep(0.1)
    player.fire()
    invader.create_invader()
    invader.move()
    screen.update()

    for i in invader.enemies:
        for m in player.missiles:
            if m.distance(i)<20:
                player.missiles.remove(m)
                invader.enemies.remove(i)
                m.hideturtle()
                i.hideturtle()
                scoreboard.increase_score()

            if m.distance(player)>600:
                player.missiles.remove(m)
                m.hideturtle()

        if i.distance(player)<10:
            scoreboard.game_over()
            game_is_on = False

screen.exitonclick()


from typing import Dict, Any
import json
from json import JSONDecodeError
from models.models import DeckPreview

def handler(event: Dict[str, Any], _: Any) -> Dict[str, Any]:
    try:
        params = event["queryStringParameters"]
        deck_id = params.get("deckId", None)
        user_id = params.get("userId", None)

        if deck_id:
            deck_preview = DeckPreview.get(user_id, deck_id)
            deck_p = {
                "deckName": deck_preview.deckName,
                "unmarked": deck_preview.unmarked,
                "easy": deck_preview.easy,
                "medium": deck_preview.medium,
                "hard": deck_preview.hard,
                "deckId": deck_preview.deckId,
                "userId": deck_preview.userId
            }
            return {
                "statusCode": 200,
                "body": json.dumps(deck_p)
            }
        else:
            previews = [dict(prev.attribute_values) for prev in DeckPreview.query(user_id)]
            return {
                "statusCode": 200,
                "body": json.dumps(previews)
            }

    except DeckPreview.DoesNotExist as e:
        return {
            "statusCode": 400,
            "message": str(e)
        }
    except (KeyError, ValueError) as e:
        return {
            "statusCode": 400,
            "message": str(e)
        }


#! python3
# multi_download_xkcd.py - Downloads comics from XKCD using multiple threads

import bs4
import os
import requests
import threading

xkcd_url = "https://xkcd.com/"

os.makedirs("xkcd", exist_ok=True)  # Make the folder xkcd - it's ok if the folder already exists.

def download_xkcd(start_comic, end_comic):
    for url_number in range(start_comic, end_comic):  # Loop through the url numbers start -> stop.

        # Download the page
        page_url = "{}{}".format(xkcd_url, url_number)  # Build the URL of the specific page
        print("Downloading page{}".format(page_url))  # Tell user what age is being downloaded.

        res = requests.get(page_url)  # Grab the URL

        res.raise_for_status()  # Check it's ok - exit code if URL bad.

        soup = bs4.BeautifulSoup(res.text, "html.parser")  # Parse the text as a beautiful soup object.

        # Find the URL of the comic image

        comic_element = soup.select("#comic.img")

        if not comic_element:
            print("No comic element found!")
        else:
            comic_url = comic_element[0].get("src")  # Get the VALUE of SRC, which is the URL of the comic.

            # Download the image
            print("downloading image {}...".format(comic_url))
            res = requests.get(comic_url)
            res.raise_for_status()

            # Save the image to ./xkcd
            file_path = os.path.join("xkcd", os.path.basename(comic_url))

            with open(file_path, "wb") as image_file:
                for chunk in res.iter_content(100000):
                    image_file.write(chunk)

# Create and start the thread objects

download_threads = []  # A list of the thread objects

for i in range(0, 1400, 100):  # Loops 14 times, making 14 threads
    download_thread = threading.Thread(target=download_xkcd, args=(i, i + 99))  # Call the download ftn.
    download_threads.append(download_thread)  # Add the output of the function to a list?
    download_thread.start()  # Start the thread.

# Wait for all the threads to end.

for download_thread in download_threads:
    """Each of the running threads is in the "download_threads list.
    Here we loop though each of the running threads, calling JOIN on each one
    This will no run the next line of code until the individual thread in question is complete
    So by looping over all of them, we halt the main thread until ALL of the other download threads are done."""
    download_thread.join()

print ("Done")

Automate the boring stuff/Chapter 16 - Time, Tasks & Launching Programs/02) multi_download_xkcd.py


"""
6. Самый большой прямоугольник.
Дано: список высот всех столбцов в гистограмме (список целых чисел)
Задание: У вас есть гистограмма. Попробуйте найти размер самого большого прямоугольника, который можно построить из столбцов гистограммы. Пример.
Примеры:
    largest_histogram([5]) == 5
    largest_histogram([5, 3]) == 6
    largest_histogram([1, 1, 4, 1]) == 4
    largest_histogram([1, 1, 3, 1]) == 4
    largest_histogram([2, 1, 4, 5, 1, 3, 3]) == 8
"""

def square(int_num):  # К сожалению, не обошлось без большой вложенности в данной функции
    int_num.append(0)
    result_n = []
    len_num = len(int_num)
    for i in range(len_num):
        size = 1
        for g in range(i + 1, len_num, 1):
            if int_num[i] <= int_num[g]:
                size += 1
            else:
                if int_num[i] < int_num[i - 1]:
                    size += 1
                result_next = int_num[i] * size
                result_n.append(result_next)
                break
    max_sq = max(result_n)
    return max_sq

num = input("Введите высоту гистограммы ").split()
int_num = list(map(int, num))
max_square = square(int_num)
print(f' {num} max square: {max_square} ')


"""
여행가 A는 N x N 크기의 정사각형 공간에 서있다. 이 공간은 1*1 코기의 정사각형으로 나뉘어짐
가장 왼쪽 위 좌표는 1,1이고, 가장 오른쪽 아래 좌표는 N,N이다.
여행가는 상,하,좌,우 방향으로 이동 가능하며 시작 좌표는 항상 1,1이다. 여행가의 이동계획이 적힌 계획서가 놓여있다.
- L : 왼쪽으로 한 칸 이동
- R : 오른쪽으로 한 칸 이동
- U : 위쪽으로 한 칸 이동
- D : 아래로 한 칸 이동

NxN 크기 공간을 벗어나는 움직임은 무시된다. 예를 들어 1,1위치에서 L,U의 이동은 무시됨
"""
# 1,1 이 시작점으로 주어지면 0행,0열을 사용하지 않거나, 1,1을 소스코드에서 0,0으로 처리하는 것처럼 두가지 방법 존재

"""
요구사항대로 충실히 구현하면 되는 문제
일련의 명령에 따라 개체를 차례로 이동시킨다는 점에서 시뮬레이션 유형으로 분류되며, 구현이 중요한 문제 유형.
"""

n = int(input())
x, y = 1, 1
plans = input().split()

# L,R,U,D에 따른 이동
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
move_types = ["L", "R", "U", "D"]

# 이동 계획을 하나씩 확인
for plan in plans:
    # 이동 후 좌표 구하기
    for i in range(len(move_types)):
        if plan == move_types[i]:
            nx = x + dx[i]
            ny = y + dy[i]
    # 공간을 벗어나는 경우 무시
    if nx < 1 or ny < 1 or nx > n or ny > n:
        continue
    # 이동 수행
    x, y = nx, ny

print(x, y)


import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from json import dumps, loads
from privates import CLIENT_ID,CLIENT_SECRET

# EMPTYING the json file every time the program is run
with open("sp_song.json", "w") as file:
    file.write("")

# INITIALIZING the spotify client
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(
    client_id=CLIENT_ID, client_secret=CLIENT_SECRET))

list_of_uri = []

async def search_for_song(song: str) -> list[str]:
    song_match = []

    # SEARCH for the song in spotofy
    response = sp.search(
        q=song, market="IN", type="track",limit=1)

    # CONVERTING response to json so that it can be written into the json file
    response_js = dumps(response)

    # WRITING the response into json so that it can be retrived aferward
    with open("sp_song.json", "w") as file:
        file.write(str(response_js))

    # READING the json that we have just written
    with open("sp_song.json", "r") as file:
      read_js = file.read()
    #READING the response json we have recieved
    read = loads(str(response_js))

    # PRINTING the names of the song
    for  i in read["tracks"]["items"]:
        song_match.append(i["name"])
    list_of_uri.append(read["tracks"]["items"][0]["uri"])
    # print(list_of_uri)
    return song_match

def get_uris():
    """Return List of the spotify song URI's"""
    return list_of_uri


import time

from utils import keyboard

FRAME_FIRST = 0

FRAME_FINAL = 9999

FRAME_SHIFT_KEY = 'right' if FRAME_FIRST <= FRAME_FINAL else 'left'

def main():
    for countdown in range(3, -1, -1):
        print(f'Waiting: {countdown}')
        time.sleep(1)

    frame_range = range(min(FRAME_FIRST, FRAME_FINAL), max(FRAME_FIRST, FRAME_FINAL) + 1)

    total = len(frame_range)

    if FRAME_SHIFT_KEY == 'left':
        frame_range = reversed(frame_range)

    for i, frame in enumerate(frame_range):
        print(f'Frame: {frame} ({i + 1}/{total})')

        # MMD does not respond to an instant press(), so we hold() instead
        keyboard.hold('enter')

        # go to next frame
        keyboard.hold(FRAME_SHIFT_KEY)

        time.sleep(0.2)

if __name__ == '__main__':
    main()


# Pizza_0=["onion", "pepper","olive"]
# Pizza_1=["mushroom", "tomato", "basil","salt","peper"]
# Pizza_2=["chicken", "mushroom", "pepper"]
# Pizza_3=["tomato", "mushroom"]
# Pizza_4=["chicken","basil"]
# Types_of_pizzas = [Pizza_0,Pizza_1,Pizza_2,Pizza_3,Pizza_4]
# #print(Types_of_pizzas)
# max = 0

# for i in range(5):
#     for j in range(5):

#         test_set = (set().union(Types_of_pizzas[i], Types_of_pizzas[j]))
#         max_ing = len(test_set)
#         if(max < max_ing):
#             max=max_ing
#             wanted_set = test_set

# print(max)
# print(wanted_set)
# y- no of types of pizzas
# n- no of pizzas to be suplied
# l- no of pizzas to be delivered to per team
# b
# Pizza_0=["onion", "pepper","olive"]
# Pizza_1=["mushroom", "tomato", "basil"]
# Pizza_2=["chicken", "mushroom", "pepper"]
# Pizza_3=["tomato", "mushroom"]
# Pizza_4=["chicken","basil"]

from itertools import combinations
import itertools
def findcomb(y, n, l):
    #remove integer
    print("\n")
    for i in range(n):
        a=[]
        for j in range(1, int(y[i][0])+1):
            a.append(y[i][j])
        b.append(a)
    for i in b:
        print(i)
    #combinations
    c= list(combinations(b, l))
    for i in c:
        i=list(i)
        print(i)
    print("\n")
    #combine probablities into one
    p=[]
    for i in range(len(c)):
        q=[]
        for j in range(l):
            q=q+c[i][j]
        q = list(dict.fromkeys(q))
        p.append(q)
    for i in p:
        i=list(i)
        print(i)
    m=0
    pos=-1
    for i in range(len(p)):
        if m<len(p[i]):
            m=len(p[i])
            pos=i
    print("\n")
    print(m)
    print(pos)
    last=[]
    print(b+list(c[pos]))
    print(last)
    for i in b:
        if i not in c[pos]:
            last.append(i)
    print(last)
n=int(input())
y=[]
b=[]
#input
for i in range(n):
    x = list(map(str, input().split(' ')))
    y.append(x)

l=int(input())
findcomb(y, n, l)


import numpy as np
import scipy.optimize as optimize
import pylab

"""
H: joint histogram, first dim is 'x', second dim is 'y'
F : number of features x dimX x dimY

H and m MUST be normalized

x is the variable to be predicted based on the observation y.
"""

def ssd(x,j):
    return (x-j)**2

def m1x(x,j):
    return x

def m1y(x,y):
    return y

def m2xy(x,y):
    return x*y

def m2xx(x,y):
    return x**2

def m2yy(x,y):
    return y**2

_NITER = 10
_METHOD = 'powell'
_TOL = 1e-5

class maxCEnt:

    def __init__(self, F, Fmean):
        self.dimX = H.shape[0]
        self.dimY = H.shape[1]
        self.H = H/H.sum()
        self.nfeatures = len(F)
        aux = np.rollaxis(np.asarray(F), 0, 3) - np.asarray(Fmean)
        aux = aux / np.abs(aux).max() # normalize between -1 and 1
        self.F = np.rollaxis(aux, 2, 0)
        self._lda = None
        self._mY = None

    """
    Returns a list: pdf values, partition function
      p(x,y) = (1/Z) m(y) exp[ -lda t(x,y) ]
      Z(lda,m) = \int m(y) exp[ -lda t(x,y) ] dx dy
    """
    def _ratio(self, lda):
        F = self.F
        dimX = self.dimX
        dimY = self.dimY
        U = np.dot(lda, F.reshape(F.shape[0], dimX*dimY)).reshape(dimX, dimY)
        return np.exp(-U)

    def _pdf(self, lda, mY):
        ratio = self._ratio(lda)
        p = mY*ratio
        Z = p.sum()
        p = p/Z
        return p, Z

    def pdf(self, lda, mY):
        return self._pdf(lda, mY)[0]

    def Z(self, lda, mY):
        return self._pdf(lda, mY)[1]

    def z(self, lda):
        return self._ratio(lda).sum(0)

    """
    Variational dual function
    """
    def vdual(self, lda, mY):
        return -np.log(self.Z(lda, mY))

    def adjust_lda(self, mY, lda0=None, method=_METHOD, tol=_TOL):
        if lda0 == None:
            lda0 = np.zeros(self.nfeatures)
        def cost(lda):
            return -self.vdual(lda, mY)
        if method == 'bfgs':
            lda = optimize.fmin_bfgs(cost, lda0, gtol=tol)
        elif method == 'cg':
            lda = optimize.fmin_cg(cost, lda0, gtol=tol)
        elif method == 'powell':
            lda = optimize.fmin_powell(cost, lda0, ftol=tol)
        else:
            print('Unknown optimization method.')
            lda = lda0
        self._lda = lda
        return lda

    def adjust_mY(self, lda, mY):
        mY = self.pdf(lda, mY).sum(0)
        self._mY = mY
        return mY

    """
    Blahut Arimoto algorithm
    """
    def fit(self, lda=None, mY=None, niter=_NITER, method=_METHOD):
        if lda == None:
            lda = np.zeros(self.nfeatures)
        if mY == None:
            mY = np.ones(self.dimY)/self.dimY
        for i in range(niter):
            print ('Iteration %d' % i)
            print('B-step...')
            lda = self.adjust_lda(mY, lda0=lda, method=method)
            print('A-step...')
            mY = self.adjust_mY(lda, mY)
        return self.pdf(lda, mY)

    """
    Fixed lambda Blahut Arimoto algorithm
    """
    def dual(self, lda, mY=None, niter=_NITER, method=_METHOD):
        if mY == None:
            mY = np.ones(self.dimY)/self.dimY
        for i in range(niter):
            print ('Iteration %d' % i)
            mY = self.adjust_mY(lda, mY)
        return self.vdual(lda, mY)

class feature:

    def __init__(self, dim):
        self.dimX = dim[0]
        self.dimY = dim[1]
        self.features = []
        self.nfeatures = 0

    def add(self, fun):
        f = np.zeros([self.dimX, self.dimY])
        for x in range(self.dimX):
            for y in range(self.dimY):
                f[x,y] = fun(x, y)
        self.features.append(f)
        self.nfeatures = self.nfeatures + 1

    def mean(self, H):
        Fmean = []
        for f in self.features:
            Fmean.append(np.sum(f*H))
        return Fmean

def pdfshow(p, gamma=.1, invert=False):
    stamp_x = 'Unknown x'
    stamp_y = 'Observation y'
    if invert==True:
        pylab.imshow(p**gamma)
        pylab.xlabel(stamp_y)
        pylab.ylabel(stamp_x)
    else:
        pylab.imshow((p.T)**gamma)
        pylab.xlabel(stamp_x)
        pylab.ylabel(stamp_y)
    myaxes = pylab.axes()
    ymin, ymax = myaxes.get_ylim()
    myaxes.set_ylim(ymax, ymin)
    pylab.show()

##H = np.ones([12,12])
##H = np.random.rand(12, 12)

H = np.load('ammon_anubis.npz')['H']

F = feature(H.shape)

if True:
    F.add(ssd)
else:
    F.add(m1x)
    F.add(m1y)
    F.add(m2xx)
    F.add(m2yy)
    F.add(m2xy)

fmean = F.mean(H)
M = maxCEnt(F.features, fmean)

"""
mY0 = np.ones(M.dimY)/M.dimY
lda = M.adjust_lda(mY0)
p, Z, U = M._pdf(lda, mY0)
mY = M.adjust_mY(lda, mY0)
"""

p = M.fit()
pdfshow(p)

pylab.plot(M._mY)
pylab.show()

"""
dual = M.dual(M._lda, niter=1000)
pylab.plot(M._mY)
pylab.show()
"""

"""
ldas = (np.arange(100)-50)/5. + M._lda
duals = np.zeros(len(ldas))
for i in range(len(ldas)):
    duals[i] = M.dual(ldas[i])

pylab.plot(ldas, duals)
pylab.show()
"""


import pandas as pd

df = pd.read_csv(
    'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/'
    + 'csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_US.csv')
df = df[df['Province_State'] == 'Pennsylvania']
df.drop(columns=['UID', 'iso2', 'iso3', 'code3', 'Country_Region', 'Lat', 'Long_', 'Combined_Key'], inplace=True)

df = df.melt(id_vars=['FIPS', 'Admin2', 'Province_State'],
             var_name="Date",
             value_name="Confirmed"
             )

df['Date'] = pd.to_datetime(df['Date'])
df = df.sort_values(by=['Province_State', 'Admin2', 'Date'])

df['Daily Confirmed'] = df.groupby(['Province_State', 'Admin2'])['Confirmed'].diff().fillna(0)

df.loc[df['Daily Confirmed'] < 0, 'Daily Confirmed'] = 0

Rolling_average = df.groupby(['Admin2'])['Daily Confirmed'].rolling(7).mean()
Rolling_average = Rolling_average.fillna(0)
Avg_df = pd.DataFrame(Rolling_average)
Avg_df = Avg_df.rename(columns={'Daily Confirmed': '7_day_avg'})
Avg_df = Avg_df.reset_index()
Avg_df = Avg_df.set_index('level_1')

average = Avg_df['7_day_avg']
df = df.join(average)

dff = pd.read_csv(
    'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/'
    + 'csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_US.csv')
dff = dff[dff['Province_State'] == 'Pennsylvania']
dff.drop(columns=['UID', 'iso2', 'iso3', 'code3', 'Country_Region', 'Lat', 'Long_', 'Combined_Key'], inplace=True)

dff = dff.melt(id_vars=['FIPS', 'Admin2', 'Province_State', 'Population'],
               var_name="Date",
               value_name="Deaths"
               )

dff['Date'] = pd.to_datetime(dff['Date'])
dff = dff.sort_values(by=['Province_State', 'Admin2', 'Date'])

dff['Daily Deaths'] = dff.groupby(['Province_State', 'Admin2'])['Deaths'].diff().fillna(0)

dff.loc[dff['Daily Deaths'] < 0, 'Daily Deaths'] = 0
deaths = dff['Deaths']
ddeaths = dff['Daily Deaths']

df = df.join(deaths)
df = df.join(ddeaths)
df.to_csv('PA_time_series_data.csv')


import argparse
import os
import json
import fitz
from fitz.utils import getColor
from tqdm import tqdm

current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "..", "..", "data")

def get_color_phrases():
    """
    color_phrases is Dict like
    {
        "color1": ["phrase1", "phrase2"],
        "color2": ["phrase3"],
    }

    Colors list is in `scripts/`
    """
    color_phrases_path = os.path.join(
        current_dir, "..", "..", "color_phrases.json"
    )
    with open(color_phrases_path, "r") as file:
        color_phrases = json.load(file)

    return color_phrases

def extract_locs_by_phrase(page, phrase):
    # phrase: "On the contrary"
    # words: ["On", "the", "contrary"]
    words = phrase.split(" ")

    word_locs = page.get_text("words")

    # Format of a word_loc: (x0, y0, x1, y1, "word", block_no, line_no, word_no)
    # ref. https://pymupdf.readthedocs.io/en/latest/textpage.html#TextPage.extractWORDS
    word_idx = 4
    extracted_locs = []
    for start in range(len(word_locs) - len(words) + 1):
        for i in range(len(words)):
            idx = start + i
            if word_locs[idx][word_idx] != words[i]:
                break
        else:
            # when not break
            for fidx in range(start, start + len(words)):
                extracted_locs.append(word_locs[fidx][:word_idx])

    return extracted_locs

def highlight_in_color(page, locs, color: str):
    highlight = page.add_highlight_annot(locs)
    rgb = getColor(color.upper())
    highlight.set_colors(stroke=rgb)
    highlight.update()

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("input_pdf", help="the path of input pdf")
    args = parser.parse_args()
    input_pdf_path = args.input_pdf
    input_pdf_filename = os.path.basename(input_pdf_path)
    doc = fitz.open(input_pdf_path)

    color_phrases = get_color_phrases()

    for color, phrases in tqdm(color_phrases.items()):
        for phrase in tqdm(phrases):
            for page in doc:
                extracted_locs = extract_locs_by_phrase(page, phrase)
                highlight_in_color(page, extracted_locs, color)

    output_dir = os.path.join(data_dir, "output")
    os.makedirs(output_dir, exist_ok=True)

    output_pdf_filename = input_pdf_filename
    output_path = os.path.join(output_dir, output_pdf_filename)
    doc.save(output_path, garbage=4, deflate=True)

if __name__ == "__main__":
    main()


'''
ex15_exam_grade_if.py
Colin Anderson
J27C76 Software Design and Development
'''

import os

Min_score = 0
Max_score = 100
score = 0

GRADE_A = 90
GRADE_A_STAR = 100
GRADE_B = 80
GRADE_C = 70
GRADE_D = 60
GRADE_E = 50
GRADE_F = 40

def main():
    #ask user for score
    score = int(input(f'please enter a number between {Min_score} and {Max_score}: '))

# check if score is valid - written range

    if score < Min_score or score > Max_score:
        print(f'{score} is out of range')
        print (f'The number must be within the range {Min_score} and {Max_score}: ')
        os.system('cls')

        main()

    grade = 'F'
    if score >= GRADE_A_STAR:
        grade = 'A*'
    elif score >= GRADE_A:
        grade = 'A'
    elif score>=GRADE_B:
        grade='B'
    elif score>=GRADE_C:
        grade='C'
    elif score>=GRADE_D:
        grade='D'
    elif score>=GRADE_E:
        grade='E'
    else:
        grade='F'

    print(f'GRADE:{grade}')
    print (f'SCORE: {score}')

if __name__=='__main__':
    main()


import json
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from lib.setting import apikey
from lib.cli_output import console
from lib.bcolors import Bcolors

class awvs():
    def __init__(self):
        self.task = []
        self.server = 'https://127.0.0.1:13443/'
        self.header = {'X-Auth': apikey, 'content-type': 'application/json'}
        # 前6个为系统默认扫描策略，后面为自定义
        self.scan_rule = {
            "FS": "11111111-1111-1111-1111-111111111111",
            "HR": "11111111-1111-1111-1111-111111111112",
            "XSS": "11111111-1111-1111-1111-111111111116",
            "SQL": "11111111-1111-1111-1111-111111111113",
            "WP": "11111111-1111-1111-1111-111111111115",
            "CO": "11111111-1111-1111-1111-111111111117"
        }

        requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    def request(self, path):
        try:
            return requests.get(url=self.server + path, timeout=10, verify=False, headers=self.header)
        except Exception as e:
            print(e)

    def scan_type(self):
        # 扫描策略选择
        try:
            l = '[0] Full Scan\n'
            l += '[1] High Risk Vulnerabilities\n'
            l += '[2] Cross-site Scripting Vulnerabilities\n'
            l += '[3] SQL Injection Vulnerabilities\n'
            l += '[4] Weak Passwords\n'
            l += '[5] Crawl Only'
            print(Bcolors.BLUE + l + Bcolors.ENDC)
            rule = input(Bcolors.RED + '[JR/AWVS_Check/Set_Rule]>> ' + Bcolors.ENDC)
            if rule == '0':
                return self.scan_rule['FS']
            elif rule == '1':
                return self.scan_rule['HR']
            elif rule == '2':
                return self.scan_rule['XSS']
            elif rule == '3':
                return self.scan_rule['SQL']
            elif rule == '4':
                return self.scan_rule['WP']
            elif rule == '5':
                return self.scan_rule['CO']
            else:
                #console('JR/AWVS_Check','策略扫描','输入有误')
                print(Bcolors.WARNING + '[-] Ops, 输入有误...' + Bcolors.ENDC)
        except Exception as e:
            print(e)

    def check_id(self):
        # 扫描任务ID选择
        try:
            r = self.request('api/v1/scans')
            response = json.loads(r.text)
            text = response['scans']
            if len(text) > 0:
                for i in range(len(text)):
                    self.task.append(text[i]['scan_id'])
                    print(Bcolors.GREEN + '[' + str(i) + ']', text[i]['target']['address'] + Bcolors.ENDC)
                task_id = input(Bcolors.RED + '[JR/AWVS_Check/Set_Task_Id]>> ' + Bcolors.ENDC)
                return self.task[int(task_id)]
            else:
                #console('JR/AWVS_Check', '任务清单', '当前无扫描任务')
                print(Bcolors.GREEN + '[-] Ops, 当前无扫描任务...' + Bcolors.ENDC)
                return
        except Exception as e:
            #console('JR/AWVS_Check', '任务清单', '输入有误')
            print(Bcolors.WARNING + '[-] Ops, 输入有误...' + Bcolors.ENDC)
        finally:
            # 清空获取的任务列表
            del self.task[:]

    def add(self):
        # 添加扫描对象
        try:
            target = input(Bcolors.RED + '[JR/AWVS_Check/Set_Target]>> ' + Bcolors.ENDC)
            data = {'address': target, 'description': target, 'criticality': '10'}
            r = requests.post(url=self.server + 'api/v1/targets', timeout=10,
                              verify=False, headers=self.header, data=json.dumps(data))
            # print(r.status_code)
            if r.status_code == 201:
                # print('添加成功')
                return json.loads(r.text)['target_id']
        except Exception as e:
            print(e)

    def scan(self):
        # 启动扫描任务
        data = {'target_id': self.add(), 'profile_id': self.scan_type(),
                'schedule': {'disable': False, 'start_date': None, 'time_sensitive': False}}
        try:
            r = requests.post(url=self.server + 'api/v1/scans', timeout=10, verify=False, headers=self.header,
                              data=json.dumps(data))
            if r.status_code == 201:
                print(Bcolors.GREEN + '[-] OK, 扫描任务已经启动...' + Bcolors.ENDC)
        except Exception as e:
            print(e)

    def stop(self):
        # 停止扫描任务
        try:
            r = requests.post(url=self.server + 'api/v1/scans/' + self.check_id() + '/abort',
                              timeout=10, verify=False, headers=self.header)
            if r.status_code == 204:
                print(Bcolors.GREEN + '[-] OK, 扫描已经停止...' + Bcolors.ENDC)
        except Exception as e:
            pass

    def view(self):
        # 查看任务状态
        try:
            r = self.request('api/v1/scans/' + self.check_id())
            response = json.loads(r.text)
            addr = response['target']['address']
            high = response['current_session']['severity_counts']['high']
            medium = response['current_session']['severity_counts']['medium']
            low = response['current_session']['severity_counts']['low']
            status = response['current_session']['status']
            print(u'[-] 扫描目标: {}'.format(addr))
            print(u'[-] 扫描状态: {}'.format(status))
            print(u'[-] 高危漏洞: {}'.format(high))
            print(u'[-] 中危漏洞: {}'.format(medium))
            print(u'[-] 低危漏洞: {}'.format(low))
        except Exception as e:
            pass

    def delete(self):
        # 删除扫描任务
        try:
            r = requests.delete(url=self.server + 'api/v1/scans/' + self.check_id(),timeout=10, verify=False, headers=self.header)
            if r.status_code == 204:
                print(Bcolors.GREEN + '[-] OK, 已经删除任务...' + Bcolors.ENDC)
        except Exception as e:
            pass

    def usage(self):
        s = '帮助:\n'
        s += '    scan        任务创建\n'
        s += '    stop        任务停止\n'
        s += '    delete      任务删除\n'
        s += '    view        任务进度\n'
        s += '    back        返回列表\n'
        print(Bcolors.BLUE + s + Bcolors.ENDC)

if __name__ == '__main__':
    mywvs = awvs()
    mywvs.usage()


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

"""
This is the Project Class, which is the base of our document.

To start with pylekture, you just need to import 2 functions

Here is an example to create a project.
    from pylekture.project import new_project, projects
    my_project = new_project()

Then, you can create a scenario
    my_scenario = my_project.new_scenario()

You can here create a project, or make a list of projects available.
"""

import os
import threading
import simplejson as json

import datetime
from pylekture import __version__
from pylekture.scenario import Scenario
from pylekture.constants import debug, _projects
from pylekture.functions import prop_dict
from pylekture.ramp import Ramp
from pylekture.rand import Random
from pylekture.errors import LektureTypeError

def new_project(*args, **kwargs):
    """
    Create a new project
    :
    """
    try:
        size = len(_projects)
        _projects.append(Project(*args, **kwargs))
        return _projects[size]
    except Exception as problem:
        print('ERROR 22' + str(problem))
        return False

def projects():
    """
    List of all projects available
    """
    return _projects

class Project(object):
    """
    A project handles everything you need.
    Scenarios and Events are all project-relative
    """
    def __init__(self, *args, **kwargs):
        super(Project, self).__init__()
        self.name = 'Untitled Project'
        self.description = "I'm a project"
        self._version = __version__
        self._path = None
        self.service = self.__class__.__name__
        self._lastopened = None
        self._created = str(datetime.datetime.now())
        self._scenarios = []
        self._autoplay = None
        self._events = []
        for key, value in kwargs.items():
            setattr(self, key, value)

    def __repr__(self):
        s = "Project (name={name}, path={path}, description={description}, autoplay={autoplay}, " \
            "scenarios={scenarios}, events={events})"
        return s.format(name=str(self.name),
                        path=self.path,
                        description=self.description,
                        autoplay=self.autoplay,
                        scenarios=len(self.scenarios),
                        events=len(self.events))

    @property
    def autoplay(self):
        """
        The autplay attribute. If true, the project plays when finish loading from hard drive
            :arg: Boolean
        """
        return self._autoplay
    @autoplay.setter
    def autoplay(self, autoplay):
        self._autoplay = autoplay

    @property
    def lastopened(self):
        """
        Datetime of the last opened date of this project. Default is None

        :getter: datetime object
        :type getter: string
        """
        return self._lastopened

    @property
    def created(self):
        """
        Datetime of the creation of the project

        :getter: datetime object
        :type getter: string
        """
        return self._created

    @property
    def version(self):
        """
        The version of pylekture used to create this project
        Read-Only

        :return: version (tag + number of commits since tag + sha of commit)
        :rtype: string
        """
        return self._version

    @property
    def path(self):
        """
        This is the filepath of the project.
        It's initialised at None when created, and can be set to any valid path.

        :param path: valid filepath. Return True if valid, False otherwise.
        :type path: string
        """
        return self._path
    @path.setter
    def path(self, value):
        self._path = value

    def reset(self):
        """reset a project by deleting project.attributes, scenarios and events related"""
        # reset project attributes
        self._version = None
        self._path = None
        # reset scenarios
        self._scenarios = []
        # reset  events
        self._events = []

    def read(self, path):
        """
        Read a lekture-project file from hard drive. Must be valid.
        if valid it will be loaded and return True, otherwise, it will return False

            :param path: Filepath to read from.
            :type path: string
            :returns: Boolean
            :rtype: True if the project has been correctly loaded, False otherwise
        """
        path = os.path.abspath(path)
        if not os.path.exists(path):
            print("ERROR 901 - THIS PATH IS NOT VALID " + path)
            return False
        else:
            print("loading project in " + path)
            if self.load(path):
                self._path = path
                self.write(path)
                if self._autoplay:
                    self.play()
                return True
            else:
                return False

    def load(self, path):
        """
        Load a lekture-project from a file from hard drive
        It will play the file after loading, according to autoplay attribute value

            :arg: file to load. Filepath must be valid when provided, it must be checked before.

            :rtype:True if the project has been correctly loaded, False otherwise
        """
        flag = False
        try:
            with open(path) as in_file:
                # clear the project
                loaded = json.load(in_file)
                flag = True
        # catch error if file is not valid or if file is not a lekture project
        except (IOError, ValueError):
            print("ERROR 906 - project not loaded, this is not a lekture-project file")
            return False
        if flag:
            # create objects from loaded file
            flag = self.fillin(loaded)
        return flag

    def fillin(self, loaded):
        """
        Creates Scenario and Events obects
        First, dump attributes, then scenario and finish with events.

        :returns: True if file formatting is correct, False otherwise
        :rtype: boolean
        """
        try:
            # reset project
            self.reset()
            # dump attributes
            attributes = loaded.pop('attributes')
            for attribute, value in attributes.items():
                if attribute == "created":
                    self._created = value
                elif attribute == "version":
                    self._version = value
                elif attribute == "autoplay":
                    self.autoplay = value
                elif attribute == "loop":
                    self.loop = value
                elif attribute == "name":
                    self.name= value
            self._lastopened = str(datetime.datetime.now())
            # dump scenarios
            scenarios = loaded.pop('scenarios')
            # dump events before scenario, because a scenario contains events
            events = loaded.pop("events")
            for event in events:
                # remove the service name. We are in the event dict, so we are sure that it is an event
                service = event.pop('service')
                self.new_event(service, **event)
            # dump scenario
            for scenario in scenarios:
                service = scenario.pop('service')
                self.new_scenario(**scenario)
            if loaded == {}:
                # project has been loaded, lastopened date changed
                # we have a path because we loaded a file from somewhere
                print("project loaded")
                return True
            else:
                print('ERROIR 906 - loaded file has not been totally loaded', loaded)
                return False
        # catch error if file is not valid or if file is not a lekture project
        except (IOError, ValueError) as Error:
            if debug:
                print(Error, "ERROR 907 - project not loaded, this is not a lekture-project file")
            return False

    def write(self, path=None):
        """
        Write a project on the hard drive.
        """
        if path:
            savepath = path
        else:
            savepath = self._path
        if savepath:
            if savepath.endswith("/"):
                savepath = savepath + self.name
            # make sure we will write a file with json extension
            # TODO : deal with a constant in the constants file
            if not savepath.endswith(".lekture"):
                savepath = savepath + ".lekture"
            try:
                # create / open the file
                out_file = open((savepath), "wb")
            except IOError:
                # path does not exists
                # TODO : make rules and list for all errors
                print("ERROR 909 - path is not valid, could not save project - " + savepath)
                return False
            project = self.export()
            try:
                the_dump = json.dumps(project, sort_keys=True, indent=4,\
                                      ensure_ascii=False).encode("utf8")
            except TypeError as Error:
                print('ERROR 98 ' + str(Error))
                return False
            try:
                out_file.write(the_dump)
                print("file has been written in " + savepath)
                out_file.close()
                self.path = os.path.realpath(savepath)
                return True
            except TypeError as Error:
                print('ERROR 99 ' + str(Error))
                return False
        else:
            print('no filepath. Where do you want I save the project?')
            return False

    def export(self):
        """
        export a project into a dict/json string
        """
        export = {}
        export.setdefault('attributes', {})
        for key, value in prop_dict(self).items():
            if key == 'events':
                events = []
                for event in value:
                    events.append(event.export())
                export.setdefault('events', events)
            elif key == 'scenarios':
                scenarios = []
                for scenario in value:
                    scenarios.append(scenario.export())
                export.setdefault('scenarios', scenarios)
            else:
                export['attributes'].setdefault(key, value)
        return export

    def play(self, index=0):
        """
        Play the whole project from the beginning.
        If you provide index argument, you can specify where to start the project playing.
        project.play(index=4) will start from the forth scenario of the project.
        If if does not exist, it will return False.

        :param index: Optional
        :type index: integer
        """
        if self.scenarios:
            player = self.Play(self)
            player.join()
            # all scenario have been played
        else:
            if debug:
                print("This project is empty")

    class Play(threading.Thread):
        """
        Instanciate a thread for Playing a whole project
        Allow to start twice or more each projects at the same time
        """
        def __init__(self, project):
            self.project = project
            threading.Thread.__init__(self)
            if debug >= 3:
                dbg = "project-play: {name} in {thread} - it is {time}"
                print(dbg.format(name=self.project.name, thread=str(threading.current_thread().name), time=str(datetime.datetime.now())))
            self.start()

        def run(self):
            for scenario in self.project.scenarios:
                # compute time in seconds (getduration is in milliseconds)
                wait = scenario.getduration() / 1000
                # add wait and post_wait to duration
                wait = wait + scenario.wait + scenario.post_wait
                # play each scenario
                player = scenario.play()
                if player:
                    player.join()
            if debug >= 3:
                dbg = "project-ends: {name} in {thread} - it is {time}"
                print(dbg.format(name=self.project.name, thread=str(threading.current_thread().name), time=str(datetime.datetime.now())))

    def scenarios_set(self, old, new):
        """Change order of a scenario in the scenario list of the project"""
        s_temp = self._scenarios[old]
        self._scenarios.pop(old)
        self._scenarios.insert(new, s_temp)

    @property
    def scenarios(self):
        """
        Report existing scenarios

        :return: All Scenario of this project
        :rtype: list
        """
        return self._scenarios

    def new_scenario(self, **kwargs):
        """
        Create a new scenario for this Project
            :args: Optional args are every attributes of the scenario, associated with a keyword
            :rtype: Scenario object
        """
        size = len(self._scenarios)
        scenario = Scenario(parent=self)
        self._scenarios.append(scenario)
        for key, value in kwargs.items():
            if key == 'events':
                for event in value:
                    scenario.add_event(self.events[event])
            else:
                setattr(self._scenarios[size], key, value)
        return scenario

    def del_scenario(self, scenario):
        """
        delete a scenario of this project
        This function won't delete events of the scenario
        """
        if scenario in self.scenarios:
            # delete the scenario
            self._scenarios.remove(scenario)
        else:
            if debug:
                print("ERROR - trying to delete a scenario which not exists \
                      in self._scenarios", scenario)
    @property
    def events(self):
        """
        All the events of this scenario
        """
        return self._events

    def new_event(self, event_type, **kwargs):
        """
        create an event
        """
        size = len(self.events)
        if event_type == 'ramp':
            the_event = Ramp(kwargs)
        elif event_type == 'random':
            the_event = Random(kwargs)

        else:
            the_event = None
        if the_event:
            self.events.append(the_event)
            return self.events[size]
        else:
            return None

    def del_event(self, event):
        """
        delete an event, by index or with object instance
        """
        used = False
        for scenario in self.scenarios:
            if event in scenario.events:
                print('WARNING - this event is still referenced in scenario ' + scenario.name)
                used = True
        if not used:
            index = self.events.index(event)
            self.events.pop(index)
            return True
        else:
            return False


f = open("C:\\Python\\AI\\Zaj1\\baza-wiedzy.txt")
#Podaje całą ścieżkę, ponieważ inaczej wyskakują błędy
read = f.readlines()
fakty = read[len(read)-1].rstrip('\n').split(',')
warunki = []
wnioski = []

for i in read:
    #Warunki
    zformatowane = i.rstrip('\n').split()
    if i == 'fakty\n':
        break
    temp = zformatowane[1].split(',')
    warunki.append(temp)
    #Wnioski
    temp = zformatowane[-1]
    wnioski.append(temp)

czyZaszlaZmiana = True
#Sprawdzenie
while czyZaszlaZmiana:
    czyZaszlaZmiana = False
    print(fakty)
    for i in range(len(warunki)):
        if all(j in fakty for j in warunki[i]):
            if wnioski[i] not in fakty:
                czyZaszlaZmiana = True
                fakty.append(wnioski[i])

f.close()


import json
import ntpath
import magic
import os

def extract_dict_from_raw_mode_data(raw):
    """extract json to dictionay

    :param raw: jsondata
    :return: :extracted dict
    """
    try:
        return json.loads(raw)
    except json.decoder.JSONDecodeError:
        return {}

def exctact_dict_from_files(data):
    """extract files from dict data.

    :param data: [{"key":"filename", "src":"relative/absolute path to file"}]
    :return: :tuple of file metadata for requests library
    """
    if not os.path.isfile(data['src']):
        raise Exception(
            'File '+data['src']+' does not exists')
    mime = magic.Magic(mime=True)
    file_mime = mime.from_file(data['src'])
    file_name = ntpath.basename(data['src'])
    return (file_name, open(data['src'], 'rb'), file_mime, {
        'Content-Disposition': 'form-data; name="'+data['key']+'"; filename="' + file_name + '"',
        'Content-Type': file_mime})

def extract_dict_from_formdata_mode_data(formdata):
    data = {}
    files = {}
    try:
        for row in formdata:
            if row['type'] == "text":
                data[row['key']] = row['value']
            if row['type'] == "file":
                files[row['key']] = exctact_dict_from_files(row)
        return data, files
    except Exception:
        print("extact from formdata_mode_data error occurred: ")
        return data, files

def extract_dict_from_raw_headers(raw):
    d = {}
    for header in raw.split('\n'):
        try:
            key, value = header.split(': ')
            d[key] = value
        except ValueError:
            continue

    return d

def extract_dict_from_headers(data):
    d = {}
    for header in data:
        try:
            if 'disabled' in header and header['disabled'] == True:
                continue
            d[header['key']] = header['value']
        except ValueError:
            continue

    return d

def format_object(o, key_values):
    if isinstance(o, str):
        try:
            return o.replace('{{', '{').replace('}}', '}').format(**key_values)
        except KeyError as e:
            raise KeyError(
                "Except value %s in PostPython environment variables.\n Environment variables are %s" % (e, key_values))
    elif isinstance(o, dict):
        return format_dict(o, key_values)
    elif isinstance(o, list):
        return [format_object(oo, key_values) for oo in o]
    elif isinstance(o, object):
        return o

def format_dict(d, key_values):
    kwargs = {}
    for k, v in d.items():
        kwargs[k] = format_object(v, key_values)
    return kwargs


import torch
from transformers import StoppingCriteria
from typing import Dict, Sequence
from valley import conversation as conversation_lib
import transformers
from valley.util.config import *
import copy
from torchvision import transforms
from valley.data import video_transform
import decord
import os
import numpy as np
from pathlib import Path
from PIL import Image

def collate_wrapper(batch):
    image_list = [b[0] for b in batch]
    prompt_list = [b[2] for b in batch]
    # input_ids = pad_sequence(prompt_list, padding_value = 0, batch_first = True)
    conv_list = [b[3] for b in batch]
    label_list = [b[1] for b in batch]
    return prompt_list, image_list, conv_list, label_list

def collate_process_image_text(batch, tokenizer, image_processor):
    batch_prompt, batch_image, conv_list, label_list = batch
    batch_prompt = tokenizer(batch_prompt, padding=True)
    input_ids, attention_mask = batch_prompt.input_ids, batch_prompt.attention_mask
    input_ids = torch.as_tensor(input_ids)
    attention_mask = torch.as_tensor(attention_mask)
    videos = []
    for this_batch_images in batch_image:
        video = image_processor.preprocess(
            this_batch_images, return_tensors='pt')['pixel_values']
        videos.append(video)
    return input_ids, attention_mask, videos, conv_list, label_list

class KeywordsStoppingCriteria(StoppingCriteria):
    def __init__(self, keywords, tokenizer, input_ids):
        self.keywords = keywords
        self.tokenizer = tokenizer
        self.start_len = None
        self.input_ids = input_ids

    def __call__(self, output_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
        if self.start_len is None:
            self.start_len = self.input_ids.shape[1]
        else:
            outputs = self.tokenizer.batch_decode(
                output_ids[:, self.start_len:], skip_special_tokens=True)[0]
            for keyword in self.keywords:
                if keyword in outputs:
                    return True
        return False

# for finetune

def safe_save_model_for_hf_trainer(trainer: transformers.Trainer,
                                   output_dir: str):
    """Collects the state dict and dump to disk."""

    if trainer.args.lora:
        if trainer.args.should_save:
            trainer.model.save_pretrained(output_dir)

    else:
        if trainer.deepspeed:
            print('saving deepspeed model...')
            torch.cuda.synchronize()
            trainer.save_model(output_dir)
            return

        state_dict = trainer.model.state_dict()
        if trainer.args.should_save:
            cpu_state_dict = {
                key: value.cpu()
                for key, value in state_dict.items()
            }
            del state_dict
            trainer._save(output_dir, state_dict=cpu_state_dict)  # noqa

def smart_tokenizer_and_embedding_resize(
    special_tokens_dict: Dict,
    tokenizer: transformers.PreTrainedTokenizer,
    model: transformers.PreTrainedModel,
):
    """Resize tokenizer and embedding.

    Note: This is the unoptimized version that may make your embedding size not be divisible by 64.
    """
    num_new_tokens = tokenizer.add_special_tokens(special_tokens_dict)
    model.resize_token_embeddings(len(tokenizer))

    if num_new_tokens > 0:
        input_embeddings = model.get_input_embeddings().weight.data
        output_embeddings = model.get_output_embeddings().weight.data

        input_embeddings_avg = input_embeddings[:-num_new_tokens].mean(
            dim=0, keepdim=True)
        output_embeddings_avg = output_embeddings[:-num_new_tokens].mean(
            dim=0, keepdim=True)

        input_embeddings[-num_new_tokens:] = input_embeddings_avg
        output_embeddings[-num_new_tokens:] = output_embeddings_avg

def _tokenize_fn(strings: Sequence[str],
                 tokenizer: transformers.PreTrainedTokenizer) -> Dict:
    """Tokenize a list of strings."""
    tokenized_list = [
        tokenizer(
            text,
            return_tensors="pt",
            padding="longest",
            max_length=tokenizer.model_max_length,
            truncation=True,
        ) for text in strings
    ]
    input_ids = labels = [
        tokenized.input_ids[0] for tokenized in tokenized_list
    ]
    input_ids_lens = labels_lens = [
        tokenized.input_ids.ne(tokenizer.pad_token_id).sum().item()
        for tokenized in tokenized_list
    ]
    return dict(
        input_ids=input_ids,
        labels=labels,
        input_ids_lens=input_ids_lens,
        labels_lens=labels_lens,
    )

def _mask_targets(target, tokenized_lens, speakers, only_mask_system):
    # cur_idx = 0
    cur_idx = tokenized_lens[0]
    tokenized_lens = tokenized_lens[1:]
    target[:cur_idx] = IGNORE_INDEX
    if not only_mask_system:
        for tokenized_len, speaker in zip(tokenized_lens, speakers):
            if speaker == "human":
                target[cur_idx+2:cur_idx + tokenized_len] = IGNORE_INDEX
            cur_idx += tokenized_len

def _add_speaker_and_signal(header, source, get_conversation=True):
    """Add speaker and start/end signal on each round."""
    BEGIN_SIGNAL = "### "
    END_SIGNAL = "\n"
    conversation = header
    for sentence in source:
        from_str = sentence["from"]
        if from_str.lower() == "human":
            from_str = conversation_lib.default_conversation.roles[0]
        elif from_str.lower() == "gpt":
            from_str = conversation_lib.default_conversation.roles[1]
        else:
            from_str = 'unknown'
        sentence["value"] = (BEGIN_SIGNAL + from_str + ": " +
                             sentence["value"] + END_SIGNAL)
        if get_conversation:
            conversation += sentence["value"]
    conversation += BEGIN_SIGNAL
    return conversation

def preprocess_multimodal(
    sources: Sequence[str],
    multimodal_cfg: dict,
    cur_token_len: int,
) -> Dict:
    is_multimodal = multimodal_cfg['is_multimodal']
    # image_token_len = multimodal_cfg['image_token_len']
    image_token_len = cur_token_len
    if not is_multimodal:
        return sources

    for source in sources:
        for sentence in source:
            replace_token = DEFAULT_IMAGE_PATCH_TOKEN * image_token_len
            if multimodal_cfg['use_im_start_end']:
                replace_token = DEFAULT_IM_START_TOKEN + replace_token + DEFAULT_IM_END_TOKEN
            sentence["value"] = sentence["value"].replace(
                DEFAULT_IMAGE_TOKEN, replace_token)

    return sources

def preprocess_multimodal_multiimage(
    sources: Sequence[str],
    multimodal_cfg: dict,
    cur_token_len: int,
    num_image: int
) -> Dict:
    is_multimodal = multimodal_cfg['is_multimodal']
    # image_token_len = multimodal_cfg['image_token_len']
    image_token_len = cur_token_len
    if not is_multimodal:
        return sources

    for source in sources:
        for sentence in source:
            if multimodal_cfg['use_im_start_end']:
                replace_token = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_PATCH_TOKEN * \
                    image_token_len + DEFAULT_IM_END_TOKEN
                replace_token = replace_token + DEFAULT_VI_START_TOKEN + \
                    DEFAULT_VIDEO_FRAME_TOKEN * num_image + DEFAULT_VI_END_TOKEN
            sentence["value"] = sentence["value"].replace(
                DEFAULT_IMAGE_TOKEN, replace_token)
            sentence["value"] = sentence["value"].replace(
                DEFAULT_VIDEO_TOKEN, replace_token)
    return sources

def preprocess(
    sources: Sequence[str],
    tokenizer: transformers.PreTrainedTokenizer, conv_mode, only_mask_system = False
) -> Dict:
    """
    Given a list of sources, each is a conversation list. This transform:
    1. Add signal '### ' at the beginning each sentence, with end signal '\n';
    2. Concatenate conversations together;
    3. Tokenize the concatenated conversation;
    4. Make a deepcopy as the target. Mask human words with IGNORE_INDEX.
    """
    # add end signal and concatenate together
    conversations = []
    for source in sources:
        header = f"{conversation_lib.conv_templates[conv_mode].system}\n\n"
        conversation = _add_speaker_and_signal(header, source)
        conversations.append(conversation)
    # tokenize conversations
    conversations_tokenized = _tokenize_fn(conversations, tokenizer)
    input_ids = conversations_tokenized["input_ids"]
    targets = copy.deepcopy(input_ids)
    for target, source in zip(targets, sources):
        tokenized_lens = _tokenize_fn([header] + [s["value"] for s in source],
                                      tokenizer)["input_ids_lens"]
        speakers = [sentence["from"] for sentence in source]
        _mask_targets(target, tokenized_lens, speakers, only_mask_system)

    return dict(input_ids=input_ids, labels=targets)

def load_video(
        path,
        image_processer = None,
        frame_mode='fixed',
        fixed_frame_number=8,
        fps_number=0.5,
        frame_process_method='centercrop',
):
    if os.path.isfile(path):
        video_reader = decord.VideoReader(
            path, num_threads=1, ctx=decord.cpu(0))
        decord.bridge.set_bridge('torch')
        video_len = len(video_reader)

        if frame_mode == 'fixed':
            video = video_reader.get_batch(np.linspace(
                0, video_len - 1, fixed_frame_number).astype(np.int_)).byte()  # 8, height,width,3
            video = video.permute(3, 0, 1, 2)  # 3 x 8 x height x width
        elif frame_mode == 'fps':
            fps_offset = int(round(video_reader.get_avg_fps())/fps_number)
            video = video_reader.get_batch(
                range(0, video_len, fps_offset)).byte()
            video = video.permute(3, 0, 1, 2)  # 3 x 8 x height x width
        input_mean = [0.48145466, 0.4578275, 0.40821073] # Consistent with clilp preprocessing
        input_std = [0.26862954, 0.26130258, 0.27577711] #Consistent with clilp preprocessing
        crop_size, scale_size = 224, 256
        trans = transforms.Compose([
            video_transform.TensorToNumpy(),
            video_transform.Resize(scale_size),
            video_transform.CenterCrop(crop_size),
            video_transform.ClipToTensor(channel_nb=3),
            video_transform.Normalize(mean=input_mean, std=input_std)
        ])
        video = trans(video)
    else:
        video_frames = list(Path(path).rglob('*'))
        if frame_mode == 'fixed':
            video_frames = [video_frames[i] for i in np.linspace(
                0, len(video_frames) - 1, fixed_frame_number).astype(np.int_)]
        elif frame_mode == 'fps':
            raise ValueError('Input folder is not support this frame mode')
        else:
            raise ValueError('Frame mode is only support "fps" or "fixed"')
        video_frames = [Image.open(str(path)) for path in video_frames]

        if frame_process_method == 'resize':
            min_length = min(video_frames[0].size)
            resize = transforms.Resize([min_length, min_length])
            video_frames = [resize(frame) for frame in video_frames]
            # test_frame = video_frames[0]

        video = image_processer.preprocess(
            video_frames, return_tensors='pt')['pixel_values']
        video = video.permute(1, 0, 2, 3)
    return video


#problem 82. Remove Duplicates from Sorted List II
#link: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
## Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
import copy
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        data = head
        newone = None
        while data:
              holder = data.val
              if data.next and holder == data.next.val:
                    data = data.next.next
                    while data and holder == data.val:
                            data = data.next
              else:

                if(not newone):
                    newone = copy.copy(data)
                    newone.next = None

                else:
                    temp = newone
                    while temp.next:
                          temp = temp.next
                    temp.next = copy.copy(data)
                    temp.next.next = None
                data = data.next
        return newone


import json

class Car:

    def __init__(self, brand, model, license_plate,
                 hour_rent, day_rent, week_rent, consumption):
        self._brand = brand
        self._model = model
        self._consumption = consumption
        self._license_plate = license_plate
        self._hour_rent = hour_rent
        self._day_rent = day_rent
        self._week_rent = week_rent
        self._is_rented = False

    @property
    def license_plate(self):
        return self._license_plate

    @license_plate.setter
    def license_plate(self):
        print("You are not allowed to change the license plate from here.")

    @property
    def is_rented(self):
        return self._is_rented

    @is_rented.setter
    def is_rented(self, new_is_rented_flag):

        # Using a setter for encapsulation & safety measures,
        # because the _is_rented flag is going to be used from outer modules.
        if new_is_rented_flag == True or new_is_rented_flag == False:
            self._is_rented = new_is_rented_flag

    def rent(self, type_="hour"):

        if not self._is_rented:
            price = self.calculate_rent(type_)

        else:
            print("This car is currently rented")

    def calculate_rent(self, type_):
        rent_cost = getattr(self, f"_{type_}_rent")
        self._is_rented = True

        return rent_cost

    def __str__(self):
        """Dunder method for easier printing of the car as a string.
        E.g. if the __str__() method is implemented, we can print
        a Car object as str(car_object), with a format that we've chosen.

        Returns:
            string: The chosen format of a descriptive object string.
        """
        border = '-' * 30
        header = f"{self._brand.capitalize()} - {self._model}"
        info = f"Renting Prices:\nHour: ${self._hour_rent}, Day: ${self._day_rent}, Week: ${self._week_rent}"
        license_plate_info = f"License plate: {self.license_plate}"

        return f"{border}\n{header}\n{info}\n{license_plate_info}\n{border}"


######################################
######################################
#Program to plot meteorological date
#of the OUT_STAT files
#of SAM with python and  Netcdf library.
#
#Create by: Jhonatan Aguirre
#Date:23/03/1010
#working
######################################
######################################

import  source.data_own          as down

import  source.two_dimensional   as cf

from    files_direction import *

from    Parameters import *

import  matplotlib.pyplot as plt

import tkinter

matplotlib.use('TkAgg')

#Defined the name of the experiments defined in Parameters files
#separate with colon
exp         =   [ LASSO_teste4, ]

#figures name
explabel    =   ['LASSO_teste4',]

# label in the figures
explabel2   =   ['LASSO_teste4',]

# Data to plot
exp_days    =   [ (2015,6,6,9,2015,6,6,20), ]

hours	  =   [ (8,20)]

alt	  =   [  4.5,]

color	  =   [ 'navy',
              ]

cor1      =   [ 'navy',
              ]

cor2      =   [ 'red',
              ]

explabel2 =   [' CF ' ]
####bar ,y axis, top_lfc_pbl,cm a mais do grafico
axis_on   =[(True,True,True,0.4,0.0)]
contour   =[(0.005,15,21)]
fn1       =   cf.plot2d_im_ctn(exp,exp_days,alt,explabel,explabel2,hours,contour,axis_on,show=False)

explabel2 =   [' uMF ']
contour=[(0.001,0.12,21)]
axis_on=[(True,True,True,0.4,0.0)]
fn2    =   cf.plot2d_im_mass_flux(exp,exp_days,alt,explabel,explabel2,hours,contour,axis_on,show=False)

explabel2 =   ['W$\mathrm{_{ls}}$']
axis_on   =   [(True,True,True,0.4,0.0)]
contour   =   [
                (-2.80,1.5,21),
              ]

fn3       =   cf.plot2d_im_wobs(exp,exp_days,alt,explabel,explabel2,hours,contour,axis_on,show=False)
contour   =   [
                (10.0,100,21),
              ]
explabel2 =   [' RH' ]
axis_on   =   [(True,True,True,0.4,0.0)]
fn4       =   cf.plot2d_im_relh(exp,exp_days,alt,explabel,explabel2,hours,contour,axis_on,show=False)

explabel2 =   [' CLW ']
contour   =   [(0.0005,0.03,21)]
axis_on   =   [(True,True,True,0.4,0.0)]
fn5       =   cf.plot2d_im_ql(exp,exp_days,alt,explabel,explabel2,hours,contour,axis_on,show=False)

explabel2 =   [' Wc']
axis_on   =   [(True,True,True,0.4,0.0)]
contour   =   [(0.01,2.0,21)]
fn6       =   cf.plot2d_im_wsup(exp,exp_days,alt,explabel,explabel2,hours,contour,axis_on,show=False)

explabel2 =   ['Wd']
axis_on   =   [(True,True,True,0.4,0.0)]
contour   =   [(-2,-0.01,21)]
fn7       =   cf.plot2d_im_wsup_down(exp,exp_days,alt,explabel,explabel2,hours,contour,axis_on,show=False)

plt.show()


import ntpath
import re
from lxml import etree
from cybertop.attacks import Attack
from cybertop.util import getLandscapeXSDFile
from cybertop.util import getLandscapeNamespace
from cybertop.log import LOG
import os.path

class Parser(object):
    """
    The file parser.
    """

    def __init__(self, configParser, pluginManager):
        """
        Constructor.
        @param configParser: The configuration parser.
        @param pluginManager: The plug-in manager.
        """
        self.configParser = configParser
        self.pluginManager = pluginManager

    def getAttackFromFile(self, fileName):
        """
        Creates an attack object by parsing a CSV file.
        @param fileName: the file name of the CSV file to parse.
        @return: the attack object.
        @raise IOError: if the file has an invalid format or if no suitable parser plug-in is available.
        """

        # First: checks if the file is a regular file.
        if not ntpath.isfile(fileName):
            LOG.critical("The file '%s' is not a regular file.", fileName)
            raise IOError("The file '%s' is not a regular file." % fileName)

        # Second: checks the file name format.
        match = re.match("^(Very Low|Very low|very low|Low|low|High|high|Very High|Very high|high)-(.+)?-(\d+)\.csv$", os.path.basename(fileName))
        if match:
            severity = match.group(1).lower()
            if severity == "very low":
                severity = 1
            elif severity == "low":
                severity = 2
            elif severity == "high":
                severity = 3
            else:
                severity = 4
            attackType = match.group(2)
            identifier = int(match.group(3))
        else:
            severity = 4
            attackType = os.path.splitext(ntpath.basename(fileName))[0]
            identifier = None

        anomaly_name = attackType

        # Finds a suitable parser.
        plugin = None
        for i in self.pluginManager.getPluginsOfCategory("Parser"):
            pluginFileName = i.details.get("Core", "FileName")
            if re.match(pluginFileName, attackType):
                plugin = i
                break
        if plugin is None:
            LOG.critical("No suitable attack event parser found.")
            raise IOError("No suitable attack event parser found")

        # Creates an attack object.
        attackType = plugin.details.get("Core", "Attack")
        attack = Attack(severity, attackType, identifier, anomaly_name)

        # Opens the file and read the events.
        count = 0
        with open(fileName, "rt") as csv:
            for line in csv:
                count += 1
                event = plugin.plugin_object.parse(fileName, count, line)
                if event is not None:
                    attack.events.append(event)

        # Third: checks if there are some events.
        if count <= 1:
            LOG.critical("The file '%s' is empty.", fileName)
            raise IOError("The file '%s' is empty." % fileName)

        LOG.info("Parsed an attack of type '%s' with severity %d and containing %d events.", attack.type, attack.severity, len(attack.events))
        return attack

    def getAttackFromList(self, identifier, severity, attackType, attackList, anomaly_name):
        """
        Creates an attack object by parsing a CSV list.
        @param identifier: the attack id.
        @param severity: the attack severity.
        @param attackType: the attack type.
        @param attackList: the list to parse.
        @return: the attack object.
        @raise IOError: if the file has an invalid format or if no suitable parser plug-in is available.
        """

        # Finds a suitable parser.
        plugin = None
        for i in self.pluginManager.getPluginsOfCategory("Parser"):
            pluginFileName = i.details.get("Core", "FileName")
            if re.match(pluginFileName, attackType):
                plugin = i
                break
        if plugin is None:
            LOG.critical("No suitable attack event parser found.")
            raise IOError("No suitable attack event parser found")

        # Creates an attack object.
        attackType = plugin.details.get("Core", "Attack")
        attack = Attack(severity, attackType, identifier, anomaly_name)

        # Opens the file and read the events.
        count = 0
        for line in attackList:
            count += 1
            event = plugin.plugin_object.parse(None, count, line)
            print(">>>>>>", event)
            if event is not None:
                attack.events.append(event)

        # Third: checks if there are some events.
        if count == 0:
            LOG.critical("The list is empty")
            raise IOError("The list is empty")

        LOG.info("Parsed an attack of type '%s' with severity %d and containing %d events.", attack.type, attack.severity, len(attack.events))
        return attack

    def getLandscape(self, fileName):
        """
        Creates a landscape map by parsing an XML file.
        @param fileName: the file name of the XML file to parse.
        @return: the landscape map.
        @raise IOError: if the file has an invalid format.
        """
        schema = etree.XMLSchema(etree.parse(getLandscapeXSDFile()))
        parser = etree.XMLParser(schema = schema)

        if not os.path.exists(fileName):
            LOG.critical("The file '%s' does not exist", fileName)
            raise IOError("The file '%s' does not exist", fileName)

        root = etree.parse(fileName, parser).getroot()
        landscape = {}
        for i in root:
            identifier = i.attrib["id"]
            capabilities = set()
            for j in i.findall("{%s}capability" % getLandscapeNamespace()):
                capabilities.add(j.text)
            landscape[identifier] = capabilities

        LOG.info("Landscape with %d IT resources read.", len(landscape))
        return landscape


import json
import nltk
import csv
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
from Trie import Trie
import re
from utils import construct_trie,construct_re,get_matches,get_matches_overlap
'''********************
getSentences function
PARAMETERS:  KEYWORD -- word to search for; TEXT -- string made of sentence(s)
RETURN: a list of strings, each is a sentence containing the keyword
************************'''

def getSentences(keyword, text):
    keywordSentences = []
    candSentences = text.split('. ')
    for sentence in candSentences:
        if keyword.lower() in sentence.lower():
            sentence = sentence.replace('\n',' ')
            keywordSentences.append(sentence)
    return keywordSentences

'''
eval_sentence function
PARAMETER: a list of strings, each is a sentence containing target keyword
            a list of stopwords in nltk
RETURN: a list of sentences which satisfy the criterias
'''
def eval_sentence(extractedSentences,stop_words):
    scoreSentenceTuple = []
    for sentence in extractedSentences:
        word_tokens = word_tokenize(sentence)
        filtered_sentence = [w for w in word_tokens if not w.lower() in stop_words]
        score = len(filtered_sentence)/len(word_tokens)
        scoreSentenceTuple.append((sentence,score))
    return scoreSentenceTuple

def generate_wordcloud(SWords,wordcloudlist):
    comment_words = ''
    # for sentence in wordcloudlist:
    #     tokens = sentence.split()
    for i in range(len(wordcloudlist)):
        wordcloudlist[i] = wordcloudlist[i].lower()
    comment_words += " ".join(wordcloudlist)+" "
    #print("CCCC:",comment_words)
    #comment_words = "apple apple banana apple pear pear"
    wordcloud = WordCloud(width = 800, height = 800,
                    background_color ='white',
                    stopwords = SWords,
                    min_font_size = 10).generate(comment_words)

    plt.figure(figsize = (8, 8), facecolor = None)
    plt.imshow(wordcloud)
    plt.axis("off")
    plt.tight_layout(pad = 0)
    plt.show()