from flask import Flask, jsonify, render_template, request
#https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/
from flask_sqlalchemy import SQLAlchemy
#https://flask-marshmallow.readthedocs.io/en/latest/
from flask_marshmallow import Marshmallow
import random

app = Flask(__name__)

##Connect to Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///cafes.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)

##Cafe TABLE Configuration
class Cafe(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(250), unique=True, nullable=False)
    map_url = db.Column(db.String(500), nullable=False)
    img_url = db.Column(db.String(500), nullable=False)
    location = db.Column(db.String(250), nullable=False)
    seats = db.Column(db.String(250), nullable=False)
    has_toilet = db.Column(db.Boolean, nullable=False)
    has_wifi = db.Column(db.Boolean, nullable=False)
    has_sockets = db.Column(db.Boolean, nullable=False)
    can_take_calls = db.Column(db.Boolean, nullable=False)
    coffee_price = db.Column(db.String(250), nullable=True)

#https://flask-marshmallow.readthedocs.io/en/latest/#flask_marshmallow.sqla.SQLAlchemyAutoSchema
class CafeSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Cafe

cafe_schema = CafeSchema()

@app.route("/")
def home():
    return render_template("index.html")

## HTTP GET - Read Record
@app.route("/random", methods=["GET"])
def get_random_cafe():
    cafes = db.session.query(Cafe).all()
    random_cafe = random.choice(cafes)
    cafe = cafe_schema.dump(random_cafe)
    return jsonify({"cafe": cafe})

@app.route("/all", methods=["GET"])
def get_all_cafes():
    cafes = db.session.query(Cafe).all()
    all_cafes = []
    for cafe in cafes:
        each_cafe = cafe_schema.dump(cafe)
        all_cafes.append(each_cafe)
    return jsonify({"cafes": all_cafes})

@app.route("/search", methods=["GET"])
def find_cafes():
    query_location = request.args.get("loc").title()
    cafes = db.session.query(Cafe).filter_by(location=query_location).all()
    filtered_cafes = []
    for cafe in cafes:
        each_cafe = cafe_schema.dump(cafe)
        filtered_cafes.append(each_cafe)

    if filtered_cafes:
        return jsonify({"cafes_in_the_area": filtered_cafes})
    return jsonify(error={"Not Found": "Sorry, we don't have a cafe at that location."})

## HTTP POST - Create Record
@app.route("/add", methods=["POST"])
def add_new_cafe():
    api_key = request.args.get("api_key")
    if api_key == "TopSecretAPIKey":
        new_cafe = Cafe(
            name=request.form.get("name"),
            map_url=request.form.get("map_url"),
            img_url=request.form.get("img_url"),
            location=request.form.get("loc"),
            seats=request.form.get("seats"),
            has_toilet=bool(request.form.get("toilet")),
            has_wifi=bool(request.form.get("wifi")),
            has_sockets=bool(request.form.get("sockets")),
            can_take_calls=bool(request.form.get("calls")),
            coffee_price=request.form.get("coffee_price"),
        )
        db.session.add(new_cafe)
        db.session.commit()
        return jsonify(response={"success:": "Successfully added the new cafe."})
    return jsonify(error={"Forbidden": "Sorry, that's not allowed. Make sure you have the correct api_key."})

# HTTP PUT/PATCH - Update Record
@app.route("/update_price/<int:cafe_id>", methods=["PATCH"])
def patch_new_price(cafe_id):
    cafe = db.session.query(Cafe).get(cafe_id)
    if cafe:
        new_price = request.args.get("new_price")
        cafe.coffee_price = new_price
        db.session.commit()
        updated_cafe = cafe_schema.dump(cafe)
        return jsonify(response={"updated_cafe": updated_cafe}), 200
    else:
        return jsonify(error={"Not Found": "Sorry a cafe with that id was not found in the database."}), 404

# HTTP DELETE - Delete Record
@app.route("/report-closed/<cafe_id>", methods=["DELETE"])
def delete_cafe(cafe_id):
    cafe = db.session.query(Cafe).get(cafe_id)
    if cafe:
        api_key = request.args.get("api_key")
        if api_key == "TopSecretAPIKey":
            db.session.delete(cafe)
            db.session.commit()
            return jsonify({"success": "Cafe successfully deleted."})
        else:
            return jsonify(error={"Forbidden": "Sorry, that's not allowed. Make sure you have the correct api_key."})
    return jsonify(error={"Not Found": "Sorry a cafe with that id was not found in the database."})

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


import xml.etree.ElementTree as ET
import subprocess
import sys

# Function to parse Nmap XML report and extract IPs and open ports
def parse_nmap_report(xml_file):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    targets = []

    for host in root.findall('host'):
        ip = host.find('address').get('addr')
        ports = []
        for port in host.findall('ports/port'):
            if port.find('state').get('state') == 'open':
                ports.append(port.get('portid'))
        if ports:
            targets.append((ip, ports))

    return targets

# Function to generate and execute Nmap commands for targeted scans
def scan_target_ports(targets, nmap_command):
    for ip, ports in targets:
        port_list = ','.join(ports)
        command = f'{nmap_command} -p {port_list} {ip} -oN {ip}.txt'
        subprocess.run(command, shell=True)

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python script.py <xml_file_path> <nmap_command>")
        sys.exit(1)

    xml_file_path = sys.argv[1]
    nmap_command = sys.argv[2]

    # Parse the Nmap XML report and get IPs and open ports
    targets = parse_nmap_report(xml_file_path)

    # Scan the open ports for each IP address
    scan_target_ports(targets, nmap_command)


# Training configuration
MODEL = ""

BATCH_SIZE = 128

LRATE = 1e-4

NUM_EPOCHS = 10

WEIGHT_DECAY = 1e-4

TRAIN_SIZE = 33600

VAL_SIZE = 8400

# General configuration
TRAIN_FILE = "../data/train.csv"

TEST_FILE = "../data/test.csv"

SUBMISSION_FILE = "result/submission.csv"

EVAL_FILE = "../eval.xlsx"

MODEL_STATE_DICT_FILE = "result/model.pt"


#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""A pytest plugin that runs under the covers with gabbi.

This is a backwards compatible improvement to the way gabbi can work with
pytest. Tests are loaded (and yielded in the same way) but they are filtered
more correctly, and fixture start and stops are done more correctly.

This allows the test count to be accurate, which is nice.
"""

import pytest

# Globals storing the test-like functions to be used when starting
# and stopping a suite.
STARTS = {}
STOPS = {}

def get_cleanname(item):
    """Extract a test name from a pytest Function item."""
    if '[' in item.name:
        cleanname = item.name.split('[', 1)[1]
        cleanname = cleanname.split(']', 1)[0]
        return cleanname
    return item.name

def get_suitename(name):
    """Extract a test suite from a clean name.

    This is fragile. It assumes there are no underscores in
    suite names, which is not always true.
    """
    prefix, rest = name.split(':', 1)
    if prefix.startswith('start_') or prefix.startswith('stop_'):
        prefix = prefix.split('_', 1)[1]
    suite = rest.split('_', 1)[0]
    return prefix + ':' + suite

def c_pytest_collection_modifyitems(items, config):
    """Set the starters and stoppers for a limited collection of tests."""
    latest_suite = None
    latest_item = None
    for item in items:
        cleanname = get_cleanname(item)
        if ':' not in cleanname:
            continue
        prefix, testname = cleanname.split('_', 1)
        suitename = get_suitename(cleanname)
        if prefix == 'start' or prefix == 'stop':
            continue
        if latest_suite != suitename:
            item.starter = STARTS[suitename]
            if latest_item:
                latest_item.stopper = STOPS[
                    get_suitename(get_cleanname(latest_item))]
        latest_suite = suitename
        latest_item = item
    # Set the last stopper in the list
    if latest_item:
        latest_item.stopper = STOPS[get_suitename(get_cleanname(latest_item))]

def a_pytest_collection_modifyitems(items, config):
    """Traverse collected tests to save START and STOPS.

    Remove those START and STOPS from the tests to run.
    """
    remaining = []
    deselected = []
    for item in items:
        cleanname = get_cleanname(item)
        if ':' not in cleanname:
            remaining.append(item)
            continue
        suitename = get_suitename(cleanname)
        if cleanname.startswith('start_'):
            test = item.callspec.params['test']
            result = item.callspec.params['result']
            # TODO(cdent): Consider a named tuple here
            STARTS[suitename] = (test, result, [])
            deselected.append(item)
        elif cleanname.startswith('stop_'):
            test = item.callspec.params['test']
            STOPS[suitename] = test
            deselected.append(item)
        else:
            remaining.append(item)
            # Add each kept test to the start fixture
            # in case we need to skip all the tests.
            STARTS[suitename][2].append(item)

    if deselected:
        items[:] = remaining

@pytest.hookimpl(hookwrapper=True)
def pytest_collection_modifyitems(items, config):
    """Hook for processing collected tests.

    Discover start and stops, then use the default hook
    for filter for keywords and markers, then attach
    starter and stopper to the remaining tests.
    """
    a_pytest_collection_modifyitems(items, config)
    yield
    c_pytest_collection_modifyitems(items, config)

def pytest_runtest_setup(item):
    """Run a starter if a test has one.

    This is done before run, so it means that a single test will
    run its priors after running this.
    """
    if hasattr(item, 'starter'):
        test, result, tests = item.starter
        test(result, tests)

def pytest_runtest_teardown(item, nextitem):
    """Run a stopper if a test has one."""
    if hasattr(item, 'stopper'):
        item.stopper()


#!/usr/bin/env python3

"""
This script runs continuous face detection on the Vision Bonnet and sends face parameters to Arduino robot car via
GPIO expansion pins of Vision Bonnet.

PIN_A is used to send to Arduino the coded angle between the direction the robot faces and direction to the detected face
PIN_B is used to send to Arduino the coded distance in inches to the detected face (coded to the interval between 0.2 and 0.9)
      If value of 0.1 is sent it means no face is detected on the frame
PIN_C is used to send to Arduino the coded frame number used to check if face detector is running
PIN_D monitors if Arduino is powered up and is used for safe shutdown of Google Visin AIY kit

"""
import argparse
import os
import RPi.GPIO as GPIO

from aiy.vision.inference import CameraInference
from aiy.vision.models import face_detection
from picamera import PiCamera

from time import sleep
from math import atan2
from gpiozero import PWMLED
from gpiozero import LED
from gpiozero import Button

from aiy.leds import Leds
from aiy.pins import PIN_A
from aiy.pins import PIN_B
from aiy.pins import PIN_C
from aiy.pins import PIN_D

# Colors for LED
RED = (0xFF, 0x00, 0x00)
GREEN = (0x00, 0xFF, 0x00)
BLUE = (0x00, 0x00, 0xFF)
YELLOW = (0xFF, 0xFF, 0x00)
PURPLE = (0xFF, 0x00, 0xFF)
CYAN = (0x00, 0xFF, 0xFF)
WHITE = (0xFF, 0xFF, 0xFF)

# Initialize the GPIO pins A,B,C,D
pin_A = PWMLED(PIN_A)
pin_B = PWMLED(PIN_B)
pin_C = PWMLED(PIN_C)
pin_D = Button(PIN_D)

# Initialize LED (in the button on the top of AIY Google Vision box)
leds = Leds()

# Blink LED (color, period in seconds, n_blinks)
def blink_led(color=RED,period=1,n_blinks=3):
    for blink in range(n_blinks):
        leds.update(Leds.rgb_off())
        sleep(period/2)
        leds.update(Leds.rgb_on(color))
        sleep(period/2)
    leds.update(Leds.rgb_off())

def shut_aiy_kit_down():
    print('Terinating session...')
    blink_led(color=RED,period=0.5,n_blinks=10)
    sleep(0.5)
    GPIO.cleanup()
    sleep(3)
    os.system("sudo shutdown -h now")

# Select the face with the largest width (others will be ignored)
def select_face(faces):
    if len(faces) > 0:
        max_width = 0
        for face in faces:
            width = face.bounding_box[2]
            if width > max_width:
                max_width = width
                face_selected = face
        return face_selected
    else:
        return None

def main():
    """Face detection camera inference example."""

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--num_frames',
        '-n',
        type=int,
        dest='num_frames',
        default=-1,
        help='Sets the number of frames to run for, otherwise runs forever.')
    args = parser.parse_args()

    blink_led(color=BLUE,period=0.5,n_blinks=10)

    # Initialize variables for automatic shutdown
    shuttdown_flag = False
    min_frames_w_arduino_power = 10
    max_frames_w_no_arduino_power = 10
    counter_frames_w_power = 0
    counter_frames_w_no_power = 0

    focal_length = 1320                             # focal length in pixels for 1640 x 1232 resolution - found by calibration
    camera_resolution = (1640, 1232)
    x_center = int(camera_resolution[0] / 2)
    real_face_width_inch = 11                       # width/height of bounding box of human face in inches
    min_angle = atan2(-x_center,focal_length)       # min angle where face can be detected (leftmost area) in radians
    max_angle = atan2(x_center,focal_length)        # max angle where face can be detected (rightmost area) in radians
    min_distance = 20                               # min distance to detected face in inches
    max_distance = 200                              # max distance to detected face in inches
    face_detected_on_prev_frame = False             # Flag indicated if face was detected on the previous frame
    a = (0.9 - 0.2) / (max_distance - min_distance) # Coefficient a (slope) for coding distance value from range [min_distance, max_distance] to [0.2, 0.9]
    b = 0.9 - a * max_distance                      # Coefficient b (intercept) for coding distance value from range [min_distance, max_distance] to [0.2, 0.9]

    with PiCamera() as camera:
        # Forced sensor mode, 1640x1232, full FoV. See:
        # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes
        # This is the resolution inference run on.
        camera.sensor_mode = 4

        # Scaled and cropped resolution. If different from sensor mode implied
        # resolution, inference results must be adjusted accordingly. This is
        # true in particular when camera.start_recording is used to record an
        # encoded h264 video stream as the Pi encoder can't encode all native
        # sensor resolutions, or a standard one like 1080p may be desired.
        camera.resolution = camera_resolution

        # Start the camera stream.
        camera.framerate = 30
        #camera.start_preview()

        # Calculate face data (x_mean, y_mean, width, height).
        def face_data(face):
            x, y, width, height = face.bounding_box
            x_mean = int(x + width/2)
            angle = atan2(x_mean - x_center,focal_length)
            distance = 0
            if width > 0:
                distance = focal_length * real_face_width_inch / width
            return angle, distance

        with CameraInference(face_detection.model()) as inference:
            for i, result in enumerate(inference.run()):
                if i == args.num_frames:
                    break
                faces = face_detection.get_faces(result)
                face = select_face(faces)
                if face:
                    if face_detected_on_prev_frame:
                        angle, distance = face_data(face)
                        if angle < min_angle:
                            angle = min_angle
                        if angle > max_angle:
                            angle = max_angle
                        angle_to_send = (angle - min_angle)/(max_angle - min_angle)
                        pin_A.value = 0.8 * angle_to_send + 0.1
                        if distance < min_distance:
                            distance = min_distance
                        if distance > max_distance:
                            distance = max_distance
                        pin_B.value = a * distance + b
                        leds.update(Leds.rgb_on(GREEN))
                    face_detected_on_prev_frame = True
                else:
                    if not face_detected_on_prev_frame:
                        pin_A.value = 0.5
                        pin_B.value = 0.1
                        leds.update(Leds.rgb_off())
                    face_detected_on_prev_frame = False
                clock = i % 80 + 11
                pin_C.value = clock/100
                print('Iteration #', str(i), ' A=', str(pin_A.value), ' B=', str(pin_B.value), ' C=', str(pin_C.value), ' D=', str(pin_D.value))
                print('face_detected_on_prev_frame = ',face_detected_on_prev_frame)

                if pin_D.is_pressed:
                    counter_frames_w_no_power += 1
                else:
                    counter_frames_w_power += 1
                print('released, shuttdown_flag = ',shuttdown_flag,' frames with power=',str(counter_frames_w_power))
                print('pressed,  shuttdown_flag = ',shuttdown_flag,' frames with no power=',str(counter_frames_w_no_power))
                if counter_frames_w_power >= min_frames_w_arduino_power and not shuttdown_flag:
                    shuttdown_flag = True
                    counter_frames_w_no_power = 0
                if counter_frames_w_no_power >= max_frames_w_no_arduino_power and shuttdown_flag:
                    shut_aiy_kit_down()
                sleep(0.1)

        #camera.stop_preview()

if __name__ == '__main__':
    main()


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

class Solution:
    def averageOfLevels(self, root: TreeNode) -> List[float]:
        res = []
        if not root:
            return res
        que = []
        que.append(root)
        while que!= []:
            tmp = 0
            sz = len(que)
            for _ in range(sz):
                cur = que.pop(0)
                tmp += cur.val
                if cur.left:
                    que.append(cur.left)
                if cur.right:
                    que.append(cur.right)
            res.append(tmp/sz)
        return res


# sample code to start ceph service
# lolyu@redhat.com
from __future__ import print_function
import docker
import sys
import os
import socket
import re
import subprocess
import shlex
import time

DEVNULL = open(os.devnull, 'rw')

image_name = "ceph/daemon"
tag_name = "latest-jewel"

client = docker.from_env()
image = client.images.pull(image_name, tag=tag_name)

dir1 = "/etc/ceph"
dir2 = "/var/lib/ceph/"
os.mkdir(dir1)
os.mkdir(dir2)
volumes_dict = {dir1: {"bind": dir1, "mode": "rw"},
                dir2: {"bind": dir2, "mode": "rw"}}

# MaximumRetryCount is not needed if restart=always
restart_policy_dict = {"Name": "always"}

# get CIDR of host
ip_addr = socket.gethostbyname(socket.gethostname())
fd = os.popen("ip address")
for line in fd.read().splitlines():
    if ip_addr in line:
        cidr = re.findall(r'[0-9]+(?:\.[0-9]+){3}/[0-9]+', line)[0]
        break

env_dict = {"MON_IP": ip_addr, "CEPH_PUBLIC_NETWORK": cidr}
mon = client.containers.run(image.tags[0], "mon",
                            name="mon", detach=True,
                            volumes=volumes_dict, network_mode='host',
                            restart_policy=restart_policy_dict,
                            environment=env_dict)
mon.stop()
os.system('echo "osd pool default size = 1" >> /etc/ceph/ceph.conf')
os.system('echo "osd pool default min_size = 1" >> /etc/ceph/ceph.conf')
mon.start()

# disk size in Mb
size = 10000
cmd = shlex.split('dd if=/dev/urandom of=/home/disk.raw bs=1M count=%d' % size)
try:
    subprocess.call(cmd, stdout=DEVNULL, stderr=subprocess.STDOUT)
except Exception:
    print("dd failed")
    sys.exit()

for i in range(0, 10):
    cmd = shlex.split("losetup /dev/loop%d /home/disk.raw" % i)
    try:
        status = subprocess.call(cmd, stdout=DEVNULL, stderr=subprocess.STDOUT)
    except Exception:
        continue
    else:
        break

cmd = shlex.split('lsblk')
out = subprocess.check_output(cmd)
dev = None
for line in out.splitlines():
    if 'loop' in line:
        dev = line.split()[0]
        break
if not dev:
    print("set loop device failed")
    sys.exit()

volumes_dict['/dev/'] = {"bind": '/dev/', "mode": "rw"}
env_dict = {"OSD_DEVICE": "/dev/%s" % dev, "OSD_FORCE_ZAP": "1"}
myosd1 = client.containers.run(image.tags[0], "osd_ceph_disk",
                               name="myosd1", detach=True,
                               volumes=volumes_dict, pid_mode="host",
                               restart_policy=restart_policy_dict,
                               environment=env_dict, network_mode="host",
                               privileged=True)

cmd_list = ['ceph osd lspools',
            'ceph osd pool set rbd size 1',
            'ceph osd pool set rbd min_size 1']
for cmd in cmd_list:
    status, out = mon.exec_run(cmd)
    print("cmd: %s" % cmd)
    print(out)
    if status != 0:
        print("Something wrong with %s" % cmd)
        print("exiting...")
        sys.exit()

# wait till mon and osd finish connection
# for larger disk, may need more time to reach HEALTH_OK
print("### check ceph status after 40 seconds...")
time.sleep(40)
status, out = mon.exec_run('ceph -s')
print(out)

#def is_HEALTH_OK(container):
#    try:
#        status, out = container.exec_run('ceph -s')
#    except docker.errors.APIError:
#        return False
#    finally:
#        return True if 'HEALTH_OK' in out else False
#
#
#def wait_until(timeout, period=0.5):
#    def __wait_until(predicate):
#        def __decorator(container):
#            end = time.time() + timeout
#            while time.time() < end:
#                if predicate(container):
#                    return True
#                time.sleep(period)
#            return False
#        return __decorator
#    return __wait_until

#@wait_until(60)
#def is_HEALTH_OK(container):
#    try:
#        status, out = container.exec_run('ceph -s')
#    except docker.errors.APIError:
#        return False
#    finally:
#        return True if 'HEALTH_OK' in out else False
#
#if is_HEALTH_OK(mon):
#    print("ceph cluster is HEALTH_OK")


from src.DataPoint import DataPoint
from src.Logger import log

class ScanData:

    rawData = False
    data = False

    def __init__(self, rawData):
        self.rawData = rawData

        freqData = self.processFreqData(self.rawData[0])
        sData = self.processSData(self.rawData[1])

        if len(freqData) == len(sData):
            self.data = self.generateDataPoints(freqData, sData)
        else:
            log.error(f"Length of Frequency data ({len(freqData)}) and S data ({len(sData)}) do not mach")
            self.data = []

    def processFreqData(self, freqData):
        freqArray = []

        if " " in freqData or "." in freqData:
            log.error("Unexpected Freqency data: spaces or dots found")
            return []

        for line in freqData.split('\n'):
            if line:
                try: f = int(line)
                except Exception as e:
                    log.error("Error appending Frequency: " + str(e))
                    return []

                freqArray.append(int(line))

        return freqArray

    def processSData(self, sData):
        line = False
        outData = []

        for line in sData.split('\n'):
            if line:
                d = line.strip().split(' ')

                try: sR = float(d[0])
                except Exception as e:
                    log.error("Error appending sData (Real): " + str(e))
                    return []

                try: sI = float(d[1])
                except Exception as e:
                    log.error("Error appending sData (Imaginary): " + str(e))
                    return []

                if sR > 100 or sR < -100:
                    log.error("Unexpectedly low/high sData (Real): " + str(sR))
                    return []

                outData.append((sR, sI))

        return outData

    def generateDataPoints(self, freqs, sVals):

        dataPointArray = []

        for i in range(len(freqs)):

            dataPointArray.append(DataPoint(freqs[i], sVals[i][0], sVals[i][1]))

        return dataPointArray

    def get(self):
        return self.data

    def getFirst(self):
        if(len(self.data) == 0): return False
        return self.data[0]

    def getLast(self):
        if(len(self.data) == 0): return False
        return self.data[-1]

    def length(self):
        return len(self.data)


import copy
import unittest

import torch
from parameterized import parameterized
from torch import nn
from transformers.pytorch_utils import Conv1D

from peft import LoraConfig, get_peft_model

from .testing_common import PeftCommonTester

# MLP is a vanilla FF network with only linear layers
# EmbConv1D has an embedding and a Conv1D layer
# Conv2D has a Conv2D layer
TEST_CASES = [
    ("Vanilla MLP 1", "MLP", LoraConfig, {"target_modules": "lin0"}),
    ("Vanilla MLP 2", "MLP", LoraConfig, {"target_modules": ["lin0"]}),
    ("Vanilla MLP 3", "MLP", LoraConfig, {"target_modules": ["lin1"]}),
    ("Vanilla MLP 4", "MLP", LoraConfig, {"target_modules": ["lin0", "lin1"]}),
    ("Embedding + transformers Conv1D 1", "EmbConv1D", LoraConfig, {"target_modules": ["conv1d"]}),
    ("Embedding + transformers Conv1D 2", "EmbConv1D", LoraConfig, {"target_modules": ["emb"]}),
    ("Embedding + transformers Conv1D 3", "EmbConv1D", LoraConfig, {"target_modules": ["emb", "conv1d"]}),
    ("Conv2d 1", "Conv2d", LoraConfig, {"target_modules": ["conv2d"]}),
    ("Conv2d 2", "Conv2d", LoraConfig, {"target_modules": ["conv2d", "lin0"]}),
]

class MLP(nn.Module):
    def __init__(self):
        super().__init__()
        self.lin0 = nn.Linear(10, 20)
        self.relu = nn.ReLU()
        self.drop = nn.Dropout(0.5)
        self.lin1 = nn.Linear(20, 2)
        self.sm = nn.LogSoftmax(dim=-1)

    def forward(self, X):
        X = X.float()
        X = self.lin0(X)
        X = self.relu(X)
        X = self.drop(X)
        X = self.lin1(X)
        X = self.sm(X)
        return X

class ModelEmbConv1D(nn.Module):
    def __init__(self):
        super().__init__()
        self.emb = nn.Embedding(100, 5)
        self.conv1d = Conv1D(1, 5)
        self.relu = nn.ReLU()
        self.flat = nn.Flatten()
        self.lin0 = nn.Linear(10, 2)

    def forward(self, X):
        X = self.emb(X)
        X = self.conv1d(X)
        X = self.relu(X)
        X = self.flat(X)
        X = self.lin0(X)
        return X

class ModelConv2D(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv2d = nn.Conv2d(5, 10, 3)
        self.relu = nn.ReLU()
        self.flat = nn.Flatten()
        self.lin0 = nn.Linear(10, 2)

    def forward(self, X):
        X = X.float().reshape(2, 5, 3, 3)
        X = self.conv2d(X)
        X = self.relu(X)
        X = self.flat(X)
        X = self.lin0(X)
        return X

class MockTransformerWrapper:
    """Mock class to behave like a transformers model.

    This is needed because the tests initialize the model by calling transformers_class.from_pretrained.

    """

    @classmethod
    def from_pretrained(cls, model_id):
        # set the seed so that from_pretrained always returns the same model
        torch.manual_seed(0)

        if model_id == "MLP":
            return MLP()

        if model_id == "EmbConv1D":
            return ModelEmbConv1D()

        if model_id == "Conv2d":
            return ModelConv2D()

        raise ValueError(f"model_id {model_id} not implemented")

class PeftCustomModelTester(unittest.TestCase, PeftCommonTester):
    """TODO"""

    transformers_class = MockTransformerWrapper

    def prepare_inputs_for_testing(self):
        X = torch.arange(90).view(9, 10).to(self.torch_device)
        return {"X": X}

    @parameterized.expand(TEST_CASES)
    def test_attributes_parametrized(self, test_name, model_id, config_cls, config_kwargs):
        self._test_model_attr(model_id, config_cls, config_kwargs)

    @parameterized.expand(TEST_CASES)
    def test_adapter_name(self, test_name, model_id, config_cls, config_kwargs):
        self._test_adapter_name(model_id, config_cls, config_kwargs)

    @parameterized.expand(TEST_CASES)
    def test_prepare_for_training_parametrized(self, test_name, model_id, config_cls, config_kwargs):
        # This test does not work with custom models because it assumes that
        # there is always a method get_input_embeddings that returns a layer
        # which does not need updates. Instead, a new test is added below that
        # checks that LoRA works as expected.
        pass

    @parameterized.expand(TEST_CASES)
    def test_save_pretrained(self, test_name, model_id, config_cls, config_kwargs):
        self._test_save_pretrained(model_id, config_cls, config_kwargs)

    @parameterized.expand(TEST_CASES)
    def test_from_pretrained_config_construction(self, test_name, model_id, config_cls, config_kwargs):
        self._test_from_pretrained_config_construction(model_id, config_cls, config_kwargs)

    @parameterized.expand(TEST_CASES)
    def test_merge_layers(self, test_name, model_id, config_cls, config_kwargs):
        # for embeddings, even with init_lora_weights=False, the LoRA embeddings weights are still initialized to
        # perform the identity transform, thus the test would fail.
        if config_kwargs["target_modules"] == ["emb"]:
            return

        config_kwargs = config_kwargs.copy()
        config_kwargs["init_lora_weights"] = False
        self._test_merge_layers(model_id, config_cls, config_kwargs)

    @parameterized.expand(TEST_CASES)
    def test_generate(self, test_name, model_id, config_cls, config_kwargs):
        # Custom models do not (necessarily) have a generate method, so this test is not performed
        pass

    @parameterized.expand(TEST_CASES)
    def test_generate_half_prec(self, test_name, model_id, config_cls, config_kwargs):
        # Custom models do not (necessarily) have a generate method, so this test is not performed
        pass

    @parameterized.expand(TEST_CASES)
    def test_training_customs(self, test_name, model_id, config_cls, config_kwargs):
        self._test_training(model_id, config_cls, config_kwargs)

    @parameterized.expand(TEST_CASES)
    def test_training_customs_layer_indexing(self, test_name, model_id, config_cls, config_kwargs):
        # At the moment, layer indexing only works when layer names conform to a specific pattern, which is not
        # guaranteed here. Therefore, this test is not performed.
        pass

    @parameterized.expand(TEST_CASES)
    def test_training_customs_gradient_checkpointing(self, test_name, model_id, config_cls, config_kwargs):
        self._test_training_gradient_checkpointing(model_id, config_cls, config_kwargs)

    @parameterized.expand(TEST_CASES)
    def test_inference_safetensors(self, test_name, model_id, config_cls, config_kwargs):
        self._test_inference_safetensors(model_id, config_cls, config_kwargs)

    @parameterized.expand(TEST_CASES)
    def test_peft_model_device_map(self, test_name, model_id, config_cls, config_kwargs):
        self._test_peft_model_device_map(model_id, config_cls, config_kwargs)

    @parameterized.expand(TEST_CASES)
    def test_only_params_are_updated(self, test_name, model_id, config_cls, config_kwargs):
        # An explicit test that when using LoRA on a custom model, only the LoRA parameters are updated during training
        X = self.prepare_inputs_for_testing()
        model = self.transformers_class.from_pretrained(model_id).to(self.torch_device)
        config = config_cls(
            base_model_name_or_path=model_id,
            **config_kwargs,
        )
        model = get_peft_model(model, config)
        model_before = copy.deepcopy(model)

        model.train()
        optimizer = torch.optim.SGD(model.parameters(), lr=0.5)

        # train at least 3 steps for all parameters to be updated (probably this is required because of symmetry
        # breaking of some LoRA layers that are initialized with constants)
        for _ in range(3):
            optimizer.zero_grad()
            y_pred = model(**X)
            loss = y_pred.sum()
            loss.backward()
            optimizer.step()

        tol = 1e-4
        params_before = dict(model_before.named_parameters())
        params_after = dict(model.named_parameters())
        self.assertEqual(params_before.keys(), params_after.keys())
        for name, param_before in params_before.items():
            param_after = params_after[name]
            if "lora_" in name:
                self.assertFalse(torch.allclose(param_before, param_after, atol=tol, rtol=tol))
            else:
                self.assertTrue(torch.allclose(param_before, param_after, atol=tol, rtol=tol))

    @parameterized.expand(TEST_CASES)
    def test_disable_adapters(self, test_name, model_id, config_cls, config_kwargs):
        X = self.prepare_inputs_for_testing()
        model = self.transformers_class.from_pretrained(model_id).to(self.torch_device)
        config = config_cls(
            base_model_name_or_path=model_id,
            **config_kwargs,
        )
        model = get_peft_model(model, config)
        model.eval()
        outputs_before = model(**X)

        model.train()
        optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

        # train at least 3 steps for all parameters to be updated (probably this is required because of symmetry
        # breaking of some LoRA layers that are initialized with constants)
        for _ in range(3):
            optimizer.zero_grad()
            y_pred = model(**X)
            loss = y_pred.sum()
            loss.backward()
            optimizer.step()

        model.eval()
        outputs_after = model(**X)

        with model.disable_adapter():
            outputs_disabled = model(**X)

        self.assertFalse(torch.allclose(outputs_before, outputs_after))
        self.assertTrue(torch.allclose(outputs_before, outputs_disabled))


import sys,os
loader_path = '../../classifier/loader/'
sys.path.append(loader_path)
from loader import load_all
model_dict=load_all()
print('model_dict',model_dict)
#手头不方便
columns = ['IDClassifier', 'IfKnowDebtor', 'ConfirmLoan', 'WillingToPay', 'CutDebt','Installment']

# def demo(cls_name):

while True:
    input_cls_name=input('请输入分类起名称：')
    flag=True
    while True:
        if not flag:
            break
        else:
            input_answer=input('请输入答案，退出该分类起请按ENTER：\n')
            ans=model_dict[input_cls_name].classify(input_answer)
            print(ans)
            flag=input_answer


#!/usr/bin/env python

import re
from subprocess import check_output

import click

def last_commit(path, git):
    """
    Find the hash of the last commit that touched a file.
    """
    cmd = [git, "log", "-n", "1", "--pretty=format:%H", "--", path]
    try:
        return check_output(cmd)
    except Exception as exc:
        raise exc

def diff(reference_commit_hash, translation_commit_hash, reference_path, git):
    """
    Returns the diff between two hashes on a specific file
    """
    cmd = [git, "diff",
           "%s...%s" % (translation_commit_hash, reference_commit_hash),
           "--",
           reference_path]
    try:
        return check_output(cmd)
    except Exception as exc:
        raise exc

def find_full_path(path, git):
    cmd = [git, "ls-tree",
           "--name-only", "--full-name", "HEAD",
           path]
    try:
        return check_output(cmd).strip()
    except Exception as exc:
        raise exc

def find_reference(path, git):
    abs_path = find_full_path(path, git=git)
    return re.sub('content/(\w{2})/', 'content/en/', abs_path)

@click.command()
@click.argument("path")
@click.option("--reference", "reference",
              help="Specify the reference version of the file. Default to the English one.",
              default=None)
@click.option("--git-path",
              "git",
              help="Specify git path",
              default="git")
def main(path, reference, git):
    """
    Find what changes occurred between two versions

    ex:
    ./upstream_changes.py content/fr/_index.html
    """
    if reference is None:
        reference = find_reference(path, git=git)
    reference_commit_hash = last_commit(path=reference, git=git)
    translation_commit_hash = last_commit(path=path, git=git)

    print(diff(
        reference_commit_hash=reference_commit_hash,
        translation_commit_hash=translation_commit_hash,
        reference_path=reference,
        git=git
    ))

if __name__ == '__main__':
    main()


import projectiveTransformation
import transformMatrix4Data

def cp2TransformMatrix(cpData, referenceFrame):
    x1 = cpData.getPointValues(0, 'x')
    y1 = cpData.getPointValues(0, 'y')
    x2 = cpData.getPointValues(1, 'x')
    y2 = cpData.getPointValues(1, 'y')
    x3 = cpData.getPointValues(2, 'x')
    y3 = cpData.getPointValues(2, 'y')
    x4 = cpData.getPointValues(3, 'x')
    y4 = cpData.getPointValues(3, 'y')

    referenceIndex = getFrameIndex(x1, referenceFrame)

    transformMatrix = transformMatrix4Data.TransformMatrix4()
    if len({len(x1), len(x2), len(x3), len(x4), len(y1), len(y2), len(y3), len(y4)}) > 1:
        raise Exception('all corners must have the same amount of keyframes')

    for i in range(0, len(x1)):
        frameNumber = x1[i][0]
        # check times of all 4 entries are equal
        if len({x1[i][0], x2[i][0], x3[i][0], x4[i][0], y1[i][0], y2[i][0], y3[i][0], y4[i][0]}) > 1:
            raise Exception('times for keyframes of all four corners must be identical')

        matrix = projectiveTransformation.computeProjectionMatrix4FromCornerPin(
            x1[referenceIndex][1], y1[referenceIndex][1], x1[i][1], y1[i][1],
            x2[referenceIndex][1], y2[referenceIndex][1], x2[i][1], y2[i][1],
            x3[referenceIndex][1], y3[referenceIndex][1], x3[i][1], y3[i][1],
            x4[referenceIndex][1], y4[referenceIndex][1], x4[i][1], y4[i][1])

        transformMatrix.setKey((frameNumber, matrix))

    return transformMatrix

def getFrameIndex(pointValues, frameIndex):
    for i in range(0, len(pointValues)):
        if pointValues[i][0] == frameIndex:
            return i
    raise Exception("tracking data doesn't contain tracking data for frame " + str(
        frameIndex) + ".\nPlease choose another reference frame.")


import os
import sys
import math
import time
import string
import pprint
import itertools
import traceback
import optparse

parser = optparse.OptionParser()
parser.add_option("-v", action="count", dest="verbose", default=0)
parser.add_option("-e", action="count", dest="example", default=0)
parser.add_option("-2", action="store_true", dest="part2", default=False)
parser.add_option("--step", action="store_true", dest="step", default=False)
parser.add_option("--display", action="store_true", dest="display", default=False)
(opts, args) = parser.parse_args()

in_file_name = "d25_input.txt"
if opts.example > 0:
    in_file_name = "d25_examples%s.txt" % opts.example

with open(in_file_name, "r") as f:
    data = [x.strip() for x in f.readlines()]

print(in_file_name, len(data))

subject_number = 7
magic_date = 20201227
card_pub = int(data[0])
door_pub = int(data[1])
print('Card public = {0}\nDoor public = {1}'.format(card_pub, door_pub))

loop_size = 0
encryption_key = None
new_val, card_enc_key, door_enc_key = 1, 1, 1
while encryption_key is None:
    loop_size += 1
    new_val = new_val * subject_number % magic_date
    card_enc_key = card_enc_key * door_pub % magic_date
    door_enc_key = door_enc_key * card_pub % magic_date
    if new_val == card_pub:
        if opts.verbose:
            print('')
        print('Found card loop size = {0}'.format(loop_size))
        encryption_key = card_enc_key
    if new_val == door_pub:
        if opts.verbose:
            print('')
        print('Found door loop size = {0}'.format(loop_size))
        encryption_key = door_enc_key
    if opts.verbose and loop_size % 1000 == 0:
        print('Checking loop_size {0} = {1}'.format(loop_size, new_val), end='\r', flush=True)

print('PART1: Encyption key = {0}'.format(encryption_key))


# coding: utf-8

from time import time
from typing import Union

from crappy.blocks.block import Block

class PID(Block):
   """A PID corrector.

   A PID will continuously adjust its output based on the target value and the
   actual measured value, to try to actually reach the target.
   """

   def __init__(self,
                      kp: float,
                      ki: float = 0,
                      kd: float = 0,
                      freq: float = 500,
                      out_max: float = float('inf'),
                      out_min: float = -float('inf'),
                      target_label: str = 'cmd',
                      input_label: str = 'V',
                      time_label: str = 't(s)',
                      labels: list = None,
                      reverse: bool = False,
                      i_limit: Union[float, tuple] = 1,
                      send_terms: bool = False) -> None:
      """Sets the args and initializes the parent class.

      Args:
         kp (:obj:`float`): `P` gain.
         ki (:obj:`float`): `I` gain.
         kd (:obj:`float`): `D` gain.
         freq (:obj:`float`, optional): The block will loop at this frequency.
         out_max (:obj:`float`, optional): A value the output can never be
            superior to.
         out_min (:obj:`float`, optional): A value the output can never be
            inferior to.
         target_label (:obj:`str`, optional): The label of the setpoint.
         input_label (:obj:`str`, optional): The reading of the actual value to be
            compared with the setpoint.
         time_label (:obj:`str`, optional): The label of the time.
         labels (:obj:`list`, optional): The labels of the output of the block. It
            must contain two :obj:`str` : the time label and the actual output.
         reverse (:obj:`bool`, optional): To reverse the retro-action.
         i_limit (:obj:`tuple`, optional): To avoid over-integration. If given as
            a :obj:`tuple` of two values, they will be the boundaries for the `I`
            term. If given as a single :obj:`float` the boundaries will be:
            ::

               i_limit * out_min, i_limit * out_max

         send_terms (:obj:`bool`, optional): To get the weight of each term in the
            output value. It will add ``['p_term', 'i_term', 'd_term']`` to the
            labels. This is particularly useful to tweak the gains.
      """

      Block.__init__(self)
      self.niceness = -10
      self.freq = freq
      self.out_max = out_max
      self.out_min = out_min
      self.target_label = target_label
      self.input_label = input_label
      self.time_label = time_label
      self.labels = ['t(s)', 'pid'] if labels is None else labels
      self.reverse = reverse
      self.i_limit = i_limit
      self.send_terms = send_terms

      self.set_k(kp, ki, kd)
      self.i_term = 0
      self.last_val = 0
      if self.send_terms:
         self.labels.extend(['p_term', 'i_term', 'd_term'])
      if not isinstance(self.i_limit, tuple):
         i_min = self.i_limit * self.out_min if self.out_min is not None else None
         i_max = self.i_limit * self.out_max if self.out_max is not None else None
         self.i_limit = (i_min, i_max)
      assert len(self.i_limit) == 2, "Invalid i_limit arg!"
      self.current_cycle = 0

   def begin(self) -> None:
      self.last_t = self.t0
      data = [inp.recv_last(True) for inp in self.inputs]
      for i, r in enumerate(data):
         if self.target_label in r:
            self.target_link_id = i
         if self.input_label in r and self.time_label in r:
            self.feedback_link_id = i
      assert hasattr(self, "target_link_id"), "[PID] Error: no link containing"\
            " target label {}".format(self.target_label)
      assert hasattr(self, "feedback_link_id"), \
            "[PID] Error: no link containing input label {} " \
            "and time label {}".format(self.input_label, self.time_label)
      assert set(range(len(self.inputs))) == {self.target_link_id,
                                                                  self.feedback_link_id}, \
            "[PID] Error: useless link(s)! Make sure PID block does not " \
            "have extra inputs"
      self.last_target = data[self.target_link_id][self.target_label]
      self.last_t = data[self.feedback_link_id][self.time_label]
      # For the classical D approach:
      # self.last_val = self.last_target -\
      #      data[self.feedback_link_id][self.input_label]
      # When ignore setpoint mode
      self.last_val = data[self.feedback_link_id][self.input_label]

      if self.send_terms:
         self.send([self.last_t, 0, 0, 0, 0])
      else:
         self.send([self.last_t, 0])

   def clamp(self, v: float, limits: tuple = None) -> float:
      if limits is None:
         mini, maxi = self.out_min, self.out_max
      else:
         mini, maxi = limits
      return max(v if maxi is None else min(v, maxi), mini)

   def set_k(self, kp: float, ki: float = 0, kd: float = 0) -> None:
      s = -1 if self.reverse else 1
      self.kp = s * kp
      self.ki = s * kp * ki
      self.kd = s * kp * kd

   def loop(self) -> None:
      data = self.inputs[self.feedback_link_id].recv_last(True)
      t = data[self.time_label]
      dt = t - self.last_t
      if dt <= 0:
         return
      if data["cycle"] != self.current_cycle :        # modified
         self.i_term = 0                              # modified
         self.current_cycle = data["cycle"]           # modified
      feedback = data[self.input_label]
      if self.feedback_link_id == self.target_link_id:
         target = data[self.target_label]
      else:
         data = self.inputs[self.target_link_id].recv_last()
         if data is None:
            target = self.last_target
         else:
            target = data[self.target_label]
            self.last_target = target
      diff = target-feedback

      p_term = self.kp * diff
      self.last_t = t

      # Classical approach
      # d_term = self.kd * (diff - self.last_val)
      # self.last_val = diff
      # Alternative: ignore setpoint to avoid derivative kick
      d_term = -self.kd * (feedback - self.last_val) / dt
      self.last_val = feedback

      self.i_term += self.ki * diff * dt
      out = p_term + self.i_term + d_term
      if not self.out_min < out < self.out_max:
         out = self.clamp(out)
      self.i_term = self.clamp(self.i_term, self.i_limit)
      if self.send_terms:
         self.send([time() - self.t0, out, p_term, self.i_term, d_term])
      else:
         self.send([time() - self.t0, out])


import time
from adafruit_bus_device.spi_device import SPIDevice  # type: ignore[import]

class RF24:
    def __init__(self, spi, csn, ce_pin, spi_frequency=10000000):
        self._spi = SPIDevice(
            spi, chip_select=csn, baudrate=spi_frequency, extra_clocks=8
        )
        self._status = 0
        self._reg_write(0, 0x0E)
        if self._reg_read(0) & 3 != 2:
            raise RuntimeError("nRF24L01 Hardware not responding")
        self._ce_pin = ce_pin
        self._ce_pin.switch_to_output(value=False)
        self._reg_write(3, 3)
        self._reg_write(6, 7)
        self._reg_write(2, 0)
        self._reg_write(0x1C, 0x3F)
        self._reg_write(1, 0x3F)
        self._reg_write(0x1D, 5)
        self._reg_write(4, 0x5F)
        self._pipe0_read_addr = None
        self.channel = 76
        self.payload_length = 32
        self.flush_rx()
        self.flush_tx()
        self.clear_status_flags()

    # pylint: disable=no-member
    def _reg_read(self, reg):
        in_buf = bytearray([0, 0])
        with self._spi as spi:
            spi.write_readinto(bytes([reg, 0]), in_buf)
        self._status = in_buf[0]
        return in_buf[1]

    def _reg_read_bytes(self, reg, buf_len=5):
        in_buf = bytearray(buf_len + 1)
        with self._spi as spi:
            spi.write_readinto(bytes([reg] + [0] * buf_len), in_buf)
        self._status = in_buf[0]
        return in_buf[1:]

    def _reg_write_bytes(self, reg, out_buf):
        in_buf = bytearray(len(out_buf) + 1)
        with self._spi as spi:
            spi.write_readinto(bytes([0x20 | reg]) + out_buf, in_buf)
        self._status = in_buf[0]

    def _reg_write(self, reg, value=None):
        out_buf = bytes([reg])
        if value is not None:
            out_buf = bytes([(0x20 if reg != 0x50 else 0) | reg, value])
        in_buf = bytearray(len(out_buf))
        with self._spi as spi:
            spi.write_readinto(out_buf, in_buf)
        self._status = in_buf[0]

    # pylint: enable=no-member

    @property
    def ce_pin(self):
        return self._ce_pin.value

    @ce_pin.setter
    def ce_pin(self, val):
        self._ce_pin.value = val

    @property
    def address_length(self):
        return self._reg_read(0x03) + 2

    @address_length.setter
    def address_length(self, length):
        self._reg_write(0x03, (length - 2) if 3 <= length <= 5 else 0)

    def open_tx_pipe(self, addr):
        self._reg_write_bytes(0x0A, addr)
        self._reg_write_bytes(0x10, addr)

    def close_rx_pipe(self, pipe_num):
        if pipe_num < 0 or pipe_num > 5:
            raise ValueError("pipe_number must be in range [0, 5]")
        open_pipes = self._reg_read(2)
        if open_pipes & (1 << pipe_num):
            self._reg_write(2, open_pipes & ~(1 << pipe_num))

    def open_rx_pipe(self, pipe_num, addr):
        if not 0 <= pipe_num <= 5:
            raise ValueError("pipe_number must be in range [0, 5]")
        if not addr:
            raise ValueError("address length cannot be 0")
        if pipe_num < 2:
            if not pipe_num:
                self._pipe0_read_addr = addr
            self._reg_write_bytes(0x0A + pipe_num, addr)
        else:
            self._reg_write(0x0A + pipe_num, addr[0])
        self._reg_write(2, self._reg_read(2) | (1 << pipe_num))

    @property
    def listen(self):
        return self._reg_read(0) & 3 == 3

    @listen.setter
    def listen(self, is_rx):
        self.ce_pin = 0
        self._reg_write(0, (self._reg_read(0) & 0xFC) | (2 + bool(is_rx)))
        if is_rx:
            self.ce_pin = 1
            if self._pipe0_read_addr is not None:
                self._reg_write_bytes(0x0A, self._pipe0_read_addr)
            else:
                self.close_rx_pipe(0)
        else:
            if self._reg_read(0x1D) & 6 == 6:
                self.flush_tx()
            self._reg_write(2, self._reg_read(2) | 1)
        time.sleep(0.0001)

    def available(self):
        return self.update() and self._status >> 1 & 7 < 6

    def any(self):
        if self._reg_read(0x1D) & 4 and self._status >> 1 & 7 < 6:
            return self._reg_read(0x60)
        if self._status >> 1 & 7 < 6:
            return self._reg_read(0x11 + (self._status >> 1 & 7))
        return 0

    def read(self, length=None):
        ret_size = length if length is not None else self.any()
        if not ret_size:
            return None
        result = self._reg_read_bytes(0x61, ret_size)
        self.clear_status_flags(True, False, False)
        return result

    def send(self, buf, ask_no_ack=False, force_retry=0, send_only=False):
        self.ce_pin = 0
        if isinstance(buf, (list, tuple)):
            result = []
            for byte in buf:
                result.append(self.send(byte, ask_no_ack, force_retry, send_only))
            return result
        if self._status & 0x10 or self._status & 1:
            self.flush_tx()
        if not send_only and self._status >> 1 & 7 < 6:
            self.flush_rx()
        self.write(buf, ask_no_ack)
        while not self._status & 0x30:
            self.update()
        result = bool(self._status & 0x20)
        while force_retry and not result:
            result = self.resend(send_only)
            force_retry -= 1
        if self._status & 0x60 == 0x60 and not send_only:
            result = self.read()
        return result

    @property
    def tx_full(self):
        return bool(self._status & 1)

    @property
    def pipe(self):
        result = self._status >> 1 & 7
        if result < 6:
            return result
        return None

    @property
    def irq_dr(self):
        return bool(self._status & 0x40)

    @property
    def irq_ds(self):
        return bool(self._status & 0x20)

    @property
    def irq_df(self):
        return bool(self._status & 0x10)

    def update(self):
        self._reg_write(0xFF)
        return True

    def clear_status_flags(self, data_recv=True, data_sent=True, data_fail=True):
        config = bool(data_recv) << 6 | bool(data_sent) << 5 | bool(data_fail) << 4
        self._reg_write(7, config)

    def interrupt_config(self, data_recv=True, data_sent=True, data_fail=True):
        config = (not data_recv) << 6 | (not data_fail) << 4 | (not data_sent) << 5
        self._reg_write(0, (self._reg_read(0) & 0x0F) | config)

    @property
    def dynamic_payloads(self):
        return self._reg_read(0x1D) & 4 == 4

    @dynamic_payloads.setter
    def dynamic_payloads(self, enable):
        self._reg_write(0x1D, (self._reg_read(0x1D) & 3) | bool(enable) << 2)
        self._reg_write(0x1C, 0x3F if enable else 0)

    @property
    def payload_length(self):
        return self._reg_read(0x11)

    @payload_length.setter
    def payload_length(self, length):
        for i in range(6):
            self._reg_write(0x11 + i, max(1, min(32, length)))

    @property
    def arc(self):
        return self._reg_read(4) & 0x0F

    @arc.setter
    def arc(self, cnt):
        self._reg_write(4, (self._reg_read(4) & 0xF0) | max(0, min(int(cnt), 15)))

    @property
    def ard(self):
        return ((self._reg_read(4) & 0xF0) >> 4) * 250 + 250

    @ard.setter
    def ard(self, delta):
        delta = max(250, min(delta, 4000))
        self._reg_write(4, (self._reg_read(4) & 0x0F) | int((delta - 250) / 250) << 4)

    @property
    def ack(self):
        return self._reg_read(0x1D) & 6 == 6 and bool(self._reg_read(0x1C))

    @ack.setter
    def ack(self, enable):
        features = self._reg_read(0x1D) & 5
        if enable:
            self._reg_write(0x1C, 0x3F)
            features = features | 4
        features |= 2 if enable else 0
        self._reg_write(0x1D, features)

    def load_ack(self, buf, pipe_num):
        if 0 <= pipe_num <= 5 and (not buf or (len(buf) < 32)):
            if not self._reg_read(0x1D) & 2:
                self.ack = True
            if not self.tx_full:
                self._reg_write_bytes(0xA8 | pipe_num, buf)
                return True
        return False

    @property
    def data_rate(self):
        rf_setup = self._reg_read(6) & 0x28
        return (2 if rf_setup == 8 else 250) if rf_setup else 1

    @data_rate.setter
    def data_rate(self, speed):
        speed = 0 if speed == 1 else (0x20 if speed != 2 else 8)
        self._reg_write(6, (self._reg_read(6) & 0xD7) | speed)

    @property
    def channel(self):
        return self._reg_read(5)

    @channel.setter
    def channel(self, chnl):
        if not 0 <= int(chnl) <= 125:
            raise ValueError("channel must be in range [0, 125]")
        self._reg_write(5, int(chnl))

    @property
    def power(self):
        return bool(self._reg_read(0) & 2)

    @power.setter
    def power(self, is_on):
        self._reg_write(0, self._reg_read(0) & 0x7D | bool(is_on) << 1)
        time.sleep(0.00015)

    @property
    def pa_level(self):
        return (3 - ((self._reg_read(6) & 6) >> 1)) * -6

    @pa_level.setter
    def pa_level(self, pwr):
        if pwr not in (-18, -12, -6, 0):
            raise ValueError("pa_level must be -18, -12, -6, or 0")
        self._reg_write(6, self._reg_read(6) & 0xF8 | (3 - int(pwr / -6)) * 2 | 1)

    def resend(self, send_only=False):
        if self.fifo(True, True):
            return False
        self.ce_pin = 0
        if not send_only and self._status >> 1 & 7 < 6:
            self.flush_rx()
        self.clear_status_flags()
        self.ce_pin = 1
        while not self._status & 0x30:
            self.update()
        result = bool(self._status & 0x20)
        if self._status & 0x60 == 0x60 and not send_only:
            result = self.read()
        return result

    def write(self, buf, ask_no_ack=False, write_only=False):
        if not buf or len(buf) > 32:
            raise ValueError("buffer length must be in range [1, 32]")
        self.clear_status_flags()
        if self._status & 1:
            return False
        config = self._reg_read(0)
        if config & 3 != 2:
            self._reg_write(0, (config & 0x7C) | 2)
            time.sleep(0.00015)
        if not self.dynamic_payloads:
            pl_width = self.payload_length
            if len(buf) < pl_width:
                buf += b"\0" * (pl_width - len(buf))
            elif len(buf) > pl_width:
                buf = buf[:pl_width]
        self._reg_write_bytes(0xA0 | (bool(ask_no_ack) << 4), buf)
        if not write_only:
            self.ce_pin = 1
        return self._status & 0x10 == 0

    def flush_rx(self):
        self._reg_write(0xE2)

    def flush_tx(self):
        self._reg_write(0xE1)

    def fifo(self, about_tx=False, check_empty=None):
        _fifo, about_tx = (self._reg_read(0x17), bool(about_tx))
        if check_empty is None:
            return (_fifo & (0x30 if about_tx else 0x03)) >> (4 * about_tx)
        return bool(_fifo & ((2 - bool(check_empty)) << (4 * about_tx)))

    @property
    def rpd(self):
        return bool(self._reg_read(0x09))


import logging
import json
import azure.functions as func
from ocrlayout.bboxhelper import BBoxHelper

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    bboxresponse={}
    try:
        width = req.params.get('width')
        if not width:
            return func.HttpResponse(body=f'Missign width parameter',status_code=400)

        height = req.params.get('height')
        if not height:
            return func.HttpResponse(body=f'Missign height parameter',status_code=400)

        req_body = req.get_json()
        if len(req_body)==0:
            return func.HttpResponse(body=f'Empty request body',status_code=400)

        bboxresponse=BBoxHelper(verbose=False).processAWSOCRResponse(req_body,width=int(width),height=int(height))

    except Exception as ex:
        bboxresponse["statuscode"]=500
        bboxresponse["exception"]=ex.__repr__()

    if hasattr(bboxresponse,'__dict__'):
        return func.HttpResponse(json.dumps(bboxresponse.__dict__, default = lambda o: o.__dict__))
    else:
        return func.HttpResponse(json.dumps(bboxresponse))


from pathlib import Path
from urllib.request import urlretrieve

def fetch_from_ftp(url: str, file: Path) -> None:
    if not file.parent.exists():
        file.parent.mkdir(parents=True, exist_ok=True)

    if not file.exists():
        urlretrieve(url, file)


from labo2.ImageProcessing.datasetloading import load_ensemble

from sklearn import svm, metrics
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import time

# Load dataset from Ensemble B directory
image_dataset = load_ensemble("EnsembleB/")
dataset_size = image_dataset.target_size

time_train_arr = []
time_pred_arr = []
pred_arr = []
scalability_arr = [dataset_size,
                    dataset_size*0.8,
                    dataset_size*0.6,
                    dataset_size*0.4,
                    dataset_size*0.2]

# Run svm validation for each scalable dataset
for size in scalability_arr:
    X = image_dataset.data
    Y = image_dataset.target

    print("\nTest de scalabilité à " + str(size) + " %")
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=int(size), random_state=42)

    svc = svm.SVC(dataset_size, 'linear')

    # Train machine
    init_train_time = time.time()
    svc.fit(X_train, y_train)
    end_train_time = time.time()
    time_train_arr.append(end_train_time - init_train_time)

    # Obtain predictions
    init_pred_time = time.time()
    pred_arr.append(svc.score(X_test, y_test))
    y_pred = svc.predict(X_test)
    end_pred_time = time.time()
    time_pred_arr.append(end_pred_time - init_pred_time)

    print("Matrice de classification - \n{}".format(
        metrics.classification_report(y_test, y_pred)
    ))

    print("Matrice de confusion - \n{}".format(
        metrics.confusion_matrix(y_test, y_pred)
    ))

plt.title("Graphe du temps d'apprentissage")
plt.plot(scalability_arr, time_train_arr)
plt.show()

plt.title("Graphe du temps de prédictions")
plt.plot(scalability_arr, time_pred_arr)
plt.show()


import sdapp
from sdexception import SDException
import sdconst
import sdprint
import sdpipelineprocessing

def run(metadata,filter_name,filter_value,mode):
    if mode=='keep':
        po=sdpipelineprocessing.ProcessingObject(keep_matching_files,filter_name,filter_value)
        metadata=sdpipelineprocessing.run_pipeline(metadata,po)
    elif mode=='remove':
        po=sdpipelineprocessing.ProcessingObject(remove_matching_files,filter_name,filter_value)
        metadata=sdpipelineprocessing.run_pipeline(metadata,po)
    else:
        raise SDException("SDSIMPLF-002","Incorrect mode (%s)"%mode)

    return metadata

def keep_matching_files(files,filter_name,filter_value):
    """Keeps files with a match.

    Note:
        - If filter name not found in file attributes, raises exception.
    """
    new_files=[]
    for f in files:

        if filter_name not in f:
            raise SDException("SDSIMPLF-001","Filter name not found in file attributes (filter_name=%s)"%(filter_name,))

        if f[filter_name]==filter_value:
            new_files.append(f)

    files=new_files

    return files

def remove_matching_files(files,filter_name,filter_value):
    """Remove files with a match.

    Note:
        If filter name not found in file attributes, raises exception.
    """
    new_files=[]
    for f in files:

        if filter_name not in f:
            raise SDException("SDSIMPLF-003","Filter name not found in file attributes (filter_name=%s)"%(filter_name,))

        if f[filter_name]!=filter_value:
            new_files.append(f)

    files=new_files

    return files

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-1','--print_only_one_item',action='store_true')
    parser.add_argument('-F','--format',choices=sdprint.formats,default='raw')
    parser.add_argument('-m','--mode',required=True)
    parser.add_argument('-s','--status',choices=sdconst.TRANSFER_STATUSES_ALL,required=True)
    args = parser.parse_args()

    files=json.load( sys.stdin )
    metadata=Metadata(files=files)
    metadata=run(metadata,'status',args.status,args.mode)
    sdprint.print_format(metadata.get_files(),args.format,args.print_only_one_item) # warning: load list in memory


from itertools import groupby
from math import sqrt
from re import match

import numpy

class Orientation:
    def __init__(self, flipped, rotation):
        self.flipped = flipped
        self.rotation = rotation

Unmatched = -1
Orientations = [Orientation(False, 0), Orientation(False, 1), Orientation(False, 2), Orientation(False, 3),
                Orientation(True, 0), Orientation(True, 1), Orientation(True, 2), Orientation(True, 3)]

class Box:
    def __init__(self, tiles):
        self.tiles = set(tiles)
        self.size = int(sqrt(len(self.tiles)))
        self.tile_size = tiles[0].size()

    def contents(self):
        return self.tiles

    def is_unmatched(self, edge):
        matches = set()
        for t in self.tiles:
            for f in t.faces():
                if (edge == f) or (edge == f[::-1]):
                    matches.add(t.tile_id)
        return len(matches) <= 1

    def take_matching_piece(self, required_edges):
        for tile in self.tiles:
            for o in Orientations:
                matching_edges = 0
                for i, edge in enumerate(tile.faces(o)):
                    if required_edges[i] == Unmatched:
                        if self.is_unmatched(edge):
                            matching_edges += 1
                    elif required_edges[i] is not None:
                        if required_edges[i] == edge:
                            matching_edges += 1
                    else:
                        matching_edges += 1
                if matching_edges == 4:
                    self.tiles.remove(tile)
                    return PlacedTile(tile, o)

class Tile:
    def __init__(self, tile_id, data):
        self.tile_id = tile_id
        self.raw_data = numpy.array(data)

    def size(self):
        return len(self.raw_data)

    def faces(self, orientation=Orientation(False, 0)):
        a = self.raw_data.copy()
        if orientation.flipped:
            a = numpy.fliplr(a)
        a = numpy.rot90(a, orientation.rotation)
        top = a[0].tolist()
        bottom = a[-1].tolist()[::-1]
        left = a[:, 0].tolist()[::-1]
        right = a[:, -1].tolist()
        return [top, left, bottom, right]

    def data(self, orientation):
        a = self.raw_data.copy()
        if orientation.flipped:
            a = numpy.fliplr(a)
        a = numpy.rot90(a, orientation.rotation)
        return a

    @staticmethod
    def from_lines(lines):
        tile = Tile(int(match('Tile (\\d+):', lines[0]).group(1)),
                    [*map(lambda line: list(map(lambda c: c == '#', line)), lines[1:])])
        return tile

class Table:
    def __init__(self, box):
        self.box = box
        self.size = box.size
        self.tiles = [[None for _ in range(self.size)] for _ in range(self.size)]
        self.spaces = self.size * self.size

    def place_tile(self, placed_tile, x, y):
        print(
            f"Placing {placed_tile.tile.tile_id} at {x},{y} rotated by {placed_tile.orientation.rotation} flipped={placed_tile.orientation.flipped}")
        self.tiles[y][x] = placed_tile
        self.spaces -= 1

    def is_complete(self):
        return self.spaces == 0

    def tile_at(self, x, y):
        return self.tiles[y][x]

    def get_required_edge_for(self, x, y, direction):
        offsets = [[0, -1], [-1, 0], [0, 1], [1, 0]]
        required_edge = None
        if (direction == 0 and y == 0) or (direction == 1 and x == 0) or (direction == 2 and y == self.size - 1) or (
                direction == 3 and x == self.size - 1):
            required_edge = Unmatched
        else:
            adjacent_tile = self.tile_at(x + offsets[direction][0], y + offsets[direction][1])
            if adjacent_tile is not None:
                left_face = adjacent_tile.tile.faces(adjacent_tile.orientation)[(direction + 2) % 4]
                required_edge = left_face[::-1]
        return required_edge

    def to_picture(self):
        tile_size = self.box.tile_size
        picture = numpy.full([self.size * (tile_size - 2), self.size * (tile_size - 2)], False)
        for y in range(len(self.tiles)):
            for x in range(len(self.tiles[y])):
                px = x * (tile_size - 2)
                py = y * (tile_size - 2)
                picture[py:py + tile_size - 2, px:px + tile_size - 2] = self.tiles[y][x].data()[1:tile_size - 1,
                                                                        1:tile_size - 1]

        return picture

    def __str__(self):
        sa = [[None for _ in range(self.size)] for _ in range(self.size)]
        for y in range(self.size):
            for x in range(self.size):
                if self.tiles[y][x] is not None:
                    sa[y][x] = self.tiles[y][x].tile.tile_id
        return sa.__str__()

class PlacedTile:
    def __init__(self, tile, orientation):
        self.tile = tile
        self.orientation = orientation

    def data(self):
        return self.tile.data(self.orientation)

def get_box():
    lines = open('input/actual.txt').read().splitlines()
    grouped_lines = [list(y) for x, y in groupby(lines, lambda line: len(line) == 0) if not x]
    return Box([*map(Tile.from_lines, grouped_lines)])

def find_tile_matching(face, tiles):
    matches = set()
    for t in tiles:
        for f in t.faces():
            if (face == f) or (face == f[::-1]):
                matches.add(t.tile_id)
    return matches

def do_puzzle(box):
    table = Table(box)

    x = 0
    y = 0
    while not table.is_complete():
        print(f"Looking for tile for {x},{y}")
        top_required = table.get_required_edge_for(x, y, 0)
        left_required = table.get_required_edge_for(x, y, 1)
        bottom_required = table.get_required_edge_for(x, y, 2)
        right_required = table.get_required_edge_for(x, y, 3)

        tile = box.take_matching_piece([top_required, left_required, bottom_required, right_required])
        table.place_tile(tile, x, y)

        if x < table.size - 1 and table.tile_at(x + 1, y) is None:
            x += 1
        elif y < table.size - 1 and table.tile_at(x, y + 1) is None:
            y += 1
        elif x > 0 and table.tile_at(x - 1, y) is None:
            x -= 1
        elif y > 0 and table.tile_at(x, y - 1) is None:
            y -= 1

    return table

def remove_sea_monsters(picture):
    masked_picture = picture.copy()
    sea_monster = numpy.array([*map(lambda line: [*map(lambda c: c == '#', line.ljust(20))],
                                    open('input/sea-monster.txt').read().splitlines())])
    not_sea_monster = numpy.logical_not(sea_monster)
    for o in Orientations:
        sea = numpy.rot90(picture, o.rotation)
        if o.flipped:
            sea = numpy.fliplr(sea)
        found = False
        masked_picture = sea.copy()
        for x in range(0, sea.shape[0] - sea_monster.shape[0]):
            for y in range(0, sea.shape[1] - sea_monster.shape[1]):
                masked = numpy.logical_and(sea_monster, sea[x:x + sea_monster.shape[0], y:y + sea_monster.shape[1]])
                if (masked == sea_monster).all():
                    found = True
                    masked_picture[x:x + sea_monster.shape[0], y:y + sea_monster.shape[1]] = numpy.logical_and(
                        not_sea_monster, sea[x:x + sea_monster.shape[0], y:y + sea_monster.shape[1]])
        if found:
            break
    return masked_picture

def main():
    box = get_box()
    table = do_puzzle(box)
    corner_product = table.tile_at(0, 0).tile.tile_id * table.tile_at(table.size - 1, 0).tile.tile_id * table.tile_at(
        table.size - 1, table.size - 1).tile.tile_id * table.tile_at(0, table.size - 1).tile.tile_id
    print(corner_product)

    picture = table.to_picture()
    for y in range(len(picture)):
        print(''.join(list(map(lambda c: '#' if c else '.', picture[y]))))

    monsterless_picture = remove_sea_monsters(picture)

    print(numpy.count_nonzero(monsterless_picture))

if __name__ == "__main__": main()


import os
import re
import sys
import requests
import operator
import time
from requests.api import request

from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity

from flask import request

factory = StemmerFactory()
stemmer = factory.create_stemmer()

def load_stopWords():
	url = "https://raw.githubusercontent.com/Wayan123/Sentiment-Analysis/main/stopwordlist.txt"
	ina_stopword = requests.get(url).content
	return ina_stopword.split()

stopwords = load_stopWords()

def cleanData(sentence):
	ret = []
	sentence = stemmer.stem(sentence)
	for word in sentence.split():
		if not word in stopwords:
			ret.append(word)
	return " ".join(ret)

def getVectorSpace(cleanSet):
	vocab = {}
	for data in cleanSet:
		for word in data.split():
			vocab[data] = 0
	return vocab.keys()

def calculateSimilarity(sentence, doc):
	if doc == []:
		return 0
	vocab = {}
	for word in sentence:
		vocab[word] = 0

	docInOneSentence = ''
	for t in doc:
		docInOneSentence += (t + ' ')
		for word in t.split():
			vocab[word]=0

	cv = CountVectorizer(vocabulary=vocab.keys())

	docVector = cv.fit_transform([docInOneSentence])
	sentenceVector = cv.fit_transform([sentence])
	return cosine_similarity(docVector, sentenceVector)[0][0]

# with open('sum.txt') as f:
#   texts = [line.rstrip('\n') for line in f]

def process():
    if request.method == 'POST':
        texts = [request.form['text']]

        sentences = []
        clean = []
        originalSentenceOf = {}

        #Data cleansing
        for line in texts:
            cl = cleanData(line)
            sentences.append(line)
            clean.append(cl)
            originalSentenceOf[cl] = line

        setClean = set(clean)

        scores = {}
        for data in clean:
            temp_doc = setClean - set([data])
            score = calculateSimilarity(data, list(temp_doc))
            scores[data] = score

        n = 20 * len(sentences) / 100

        alpha = 0.5
        summarySet = []
        while n > 0:
            mmr = {}

            for sentence in scores.keys():
                if not sentence in summarySet:
                    mmr[sentence] = alpha * scores[sentence] - (1-alpha) * calculateSimilarity(sentence, summarySet)
            selected = max(mmr.items(), key=operator.itemgetter(1))[0]
            summarySet.append(selected)
            n -= 1

        return {
            'results': sentence
        }


import os, fnmatch
from os.path import join, basename

# Options
AddOption('--boost-mt', dest='boostmt', nargs=0, help='Using boost libraries with -mt suffix')
AddOption('--dbg', dest='dbg', nargs=0, help='Build library with debug options')

def globrec(rootdir, wildcard):
    matches = []
    for root, dirnames, filenames in os.walk(rootdir):
        for filename in fnmatch.filter(filenames, wildcard):
            matches.append(join(root, filename))
    return matches

def unionArgs(*args):
    res = args[0].copy()
    for f in args[1:] :
        for k,v in f.iteritems():
            prev = res.get(k)
            res[k] = prev and prev + v or v  # for old pythons
    return res

def libboost(name):
    suffix = '' if GetOption('boostmt') == None else '-mt'
    return 'boost_' + name + suffix

# Environment
boostRoot = os.getenv('BOOST_ROOT')

# Arguments
commonArgs = {
    'ENV'     : os.environ,
    'CPPPATH' : ['include'] + [join(p, 'include') for p in [boostRoot]],
    'CXXFLAGS': ['-Wall', '-Werror=return-type', '-Wno-unused-local-typedefs',
                 '-D_FILE_OFFSET_BITS=64', '-DLIBED2K_USE_BOOST_DATE_TIME'],
    'LIBPATH' : [join(p, 'lib') for p in [boostRoot]],
    'LIBS'    : [libboost('system'), libboost('thread'), 'pthread']
    }

debugArgs = {
    'CXXFLAGS': ['-g', '-O0', '-rdynamic', '-DLIBED2K_DEBUG']
    }

releaseArgs = {
    'CXXFLAGS': ['-O2']
    }

args = unionArgs(commonArgs, releaseArgs if GetOption('dbg') == None else debugArgs)

env = Environment(**args)
sources = globrec('src', '*.cpp') + globrec('src', '*.c')
lib = env.StaticLibrary(join('lib', 'ed2k'), sources)

binenv = Environment(**unionArgs(args, {'LIBS' : ['ed2k'], 'LIBPATH' : ['lib']}))
conn = binenv.Program(join('bin', 'conn'), [join('test', 'conn', 'conn.cpp'), lib])

uenv = Environment(**unionArgs(args,
                               {'CXXFLAGS': ['-Wno-sign-compare'],
                                'LIBS': [libboost('unit_test_framework')]}))
utests = globrec('unit', '*.cpp')
uenv.Program(join('unit', 'run_tests'), utests + [lib])


import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

titanic = sns.load_dataset("titanic")

titanic['age'] = titanic['age'].fillna(titanic['age'].median())
titanic['embarked'] = titanic['embarked'].fillna('S')
titanic['embark_town'] = titanic['embark_town'].fillna('Southampton')
titanic['deck'] = titanic['deck'].fillna('C')

print(titanic['alone'].value_counts())

def category_age(x):
    if x < 10:
        return 0
    elif x < 20:
        return 1
    elif x < 30:
        return 2
    elif x < 40:
        return 3
    elif x < 50:
        return 4
    elif x < 60:
        return 5
    elif x < 70:
        return 6
    else:
        return 7

titanic['age2'] = titanic['age'].apply(category_age)
titanic['sex'] = titanic['sex'].map({'male': 1, 'female': 0})
titanic['family'] = titanic['sibsp'] + titanic['parch'] + 1

heatmap_data = titanic[['survived', 'sex', 'age2', 'family', 'pclass', 'fare']]
sns.heatmap(heatmap_data.astype(float).corr(), cmap='RdBu', annot = True)

plt.show()


import socket
import time
import multiprocessing

def sockbounce(s,target, host, port):
    print("relay started", host, port)
    s.settimeout(10)
    try:
        while True:
            print("awaiting data")
            data1, addr1 = s.recvfrom(1420)
            print("r ", addr1, " : ", data1)
            target.sendto(data1, (host, port))
    except socket.timeout:
        print("contact gone silent. killing socket")
        s.close()

def awaitturns(s1,s2):
    print("awaitturns started")
    data1, addr1 = s1.recvfrom(1420)
    data2, addr2 = s2.recvfrom(1420)
    print("received %s from s1", data1)
    print("received %s from s2", data2)
    s1.sendto(data2, addr1)
    s2.sendto(data1, addr2)
    # activate bouncemode
    bounceleft  = multiprocessing.Process(target=sockbounce, args=(s1, s2, addr2[0], addr2[1]))
    bounceright = multiprocessing.Process(target=sockbounce, args=(s2, s1, addr1[0], addr1[1]))
    bounceleft.start()
    bounceright.start()

# management socket
sockx = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockx.bind(("0.0.0.0", 51337))
magic = bytes([0x54, 0x55, 0x52, 0x4e, 0x4d, 0x45]) # TURNME

def managementsocket():
    print("TURN started")
    while True:
        print("awaiting datax")
        datax, addrx = sockx.recvfrom(1420)
        print(datax)
        print(magic)
        if (datax == magic):
            print("turnme request, generating portpair...")
            s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s1.bind(("0.0.0.0", 0))

            s2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s2.bind(("0.0.0.0", 0))

            pairtosend = str(s1.getsockname()[1]) + "::" + str(s2.getsockname()[1])

            sockx.sendto(str.encode(pairtosend), addrx)
            turnproc = multiprocessing.Process(target=awaitturns, args=(s1,s2))
            turnproc.start()

psx = multiprocessing.Process(target=managementsocket)
psx.start()

exit
manager = multiprocessing.Manager()
maddr1 = manager.Value("maddr1", "0")
maddr2 = manager.Value("maddr2", "0")
mport1 = manager.Value("mport1", 0)
mport2 = manager.Value("mport2", 0)

sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock1.bind(("0.0.0.0", 31337))

sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock2.bind(("0.0.0.0", 31338))

data1, addr1 = sock1.recvfrom(1420)
data2, addr2 = sock2.recvfrom(1420)
print("received %s from sock1", data1)
print("received %s from sock2", data2)
sock1.sendto(data2, addr1)
sock2.sendto(data1, addr2)

maddr1.value=addr1[0]
mport1.value=addr1[1]

maddr2.value=addr2[0]
mport2.value=addr2[1]

def sock1reader():
    print("relay1 started")
    while True:
        print("awaiting data1")
        data1, addr1 = sock1.recvfrom(1420)
        if (mport1.value != addr1[1]):
            print("changed1")
            mport1.value=addr1[1]
        print("r1 ", addr1)
        sock2.sendto(data1, (maddr2.value, mport2.value))

def sock2reader():
    print("relay2 started")
    while True:
        print("awaiting data2")
        data2, addr2 = sock2.recvfrom(1420)
        if (mport2.value != addr2[1]):
            print("changed2")
            mport2.value=addr2[1]
        print("r2 ", addr2)
        sock1.sendto(data2, (maddr1.value, mport1.value))

print("WARNING: underpass is not finished yet, do not use for production")
ps1 = multiprocessing.Process(target=sock1reader)
ps2 = multiprocessing.Process(target=sock2reader)
ps1.start()
ps2.start()


#!/usr/bin/env python3

"""
INFO0085-1 - Compilers
University of Liege
Academic year 2019-2020

Authors :
    - Maxime Meurisse
    - Valentin Vermeylen
"""

###########
# Imports #
###########

import os
import sys
import argparse

from lexer.lexer import Lexer, LexerExt
from parser.parser import Parser, ParserExt
from semantic.semantic import Semantic, SemanticExt
from llvm.llvm import LLVM, LLVMExt

########
# Main #
########

if __name__ == '__main__':
    #########################
    # Initialize arg parser #
    #########################

    # We instantiate the parser (for executable arguments)
    arg_parser = argparse.ArgumentParser(description='VSOPC is a compiler for the object-oriented language VSOP. By default, VSOPC generate a native executable.')

    # We add executable arguments
    arg_parser.add_argument('-ext', help='treat input source file as an extended VSOP file', action='store_true')

    group = arg_parser.add_mutually_exclusive_group()
    group.add_argument('-lex', help='dump tokens on stdout and stop', action='store_true')
    group.add_argument('-parse', help='dump parsed AST on stdout and stop', action='store_true')
    group.add_argument('-check', help='dump annotated AST on stdout and stop', action='store_true')
    group.add_argument('-llvm', help='dump LLVM IR on stdout and stop', action='store_true')

    arg_parser.add_argument('source', help='path to the VSOP source file', type=str)

    # We parse arguments
    args = arg_parser.parse_args()

    #######################
    # Argument management #
    #######################

    if args.source:
        # We get the VSOP source file path
        source = args.source

        # We check the extension of the file
        valid_ext = ['.vsop', '.vsopext']
        source_ext = os.path.splitext(source)[1]

        if source_ext not in valid_ext:
            print('main.py: error: extension of the input file must be "{}" or "{}"'.format(valid_ext[0], valid_ext[1]), file=sys.stderr)
            sys.exit(1)

        # We check if the VSOP file exist
        if not os.path.isfile(source):
            print('main.py: error: "{}" does not exist'.format(source), file=sys.stderr)
            sys.exit(1)

        # We tokenize the source file (remark : we do not save
        # the token list because lexing is done implicitly in
        # the parser after)
        if args.ext:
            vsop_lexer = LexerExt(source)
        else:
            vsop_lexer = Lexer(source)

        # If there is the '-lex' arg
        if args.lex:
            vsop_lexer.lex(dump=True)
            sys.exit(0)
        else:
            vsop_lexer.lex()

        # If we get there, we parse the VSOP file (remark : lexing
        # is done implicitly in the parsing so we reset the lexer
        # in order to not have conflict with previous information
        # of the last lexing)
        vsop_lexer.reset()

        if args.ext:
            vsop_parser = ParserExt(source, vsop_lexer)
        else:
            vsop_parser = Parser(source, vsop_lexer)

        ast = vsop_parser.parse()

        # If there is the '-parse' arg
        if args.parse:
            print(ast)
            sys.exit(0)

        # If we get there, we annotate the AST
        if args.ext:
            vsop_semantic = SemanticExt(source, ast)
        else:
            vsop_semantic = Semantic(source, ast)

        a_ast = vsop_semantic.annotate()

        # If there is the '-check' arg
        if args.check:
            print(a_ast)
            sys.exit(0)

        # If we get there, we generate the LLVM IR code
        if args.ext:
            vsop_llvm = LLVMExt(source, a_ast)
        else:
            vsop_llvm = LLVM(source, a_ast)

        llvm_ir = vsop_llvm.generate_ir()

        # If there is the '-llvm' arg
        if args.llvm:
            print(llvm_ir)
            sys.exit(0)

        # If we get there (no arg), we generate a native executable
        vsop_llvm.generate_exec(llvm_ir)


# -*- coding: utf-8 -*-
"""
Created on Fri Apr 10 01:49:15 2020

@author: alexi
"""

#https://codeforces.com/problemset/problem/352/A --- Alexis Galvan

def jeff_digits():

    total = int(input())
    cards = list(map(int, input().split()))

    table = {5:0,0:0}

    for i in range(len(cards)):
        table[cards[i]] += 1

    if table[5] < 9 and table[0] > 0:
        return 0
    elif table[5] < 9 and table[0] == 0:
        return -1
    elif table[0] == 0:
        return -1

    times = int(table[5]/9)

    temp = ('5'*9)*times

    temp_cero = ('0'*table[0])

    return int(temp+temp_cero)

A = jeff_digits()
print(A)


import picamera
import glob
import os
import time

def capture_animated_gif():
    cwd = os.getcwd()
    if not os.path.exists(cwd+"/images"):
        os.makedirs(cwd+"/images")

    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.start_preview()
        start = time.time()
        # let the camera warm up.
        time.sleep(2)
        # capture 10 frames.
        camera.capture_sequence((
            'images/image%03d.png' % i
            for i in range(10)
            ), use_video_port=True, format='png')
        print('Captured 10 images at %.2ffps' % (10 / (time.time() - start)))
        camera.stop_preview()

    # generate animated gif from 10 captured frames.
    L = glob.glob(cwd+"/images/*.png")
    L.sort()
    #series_to_animated_gif(L, cwd+"/images/animated.gif")
    #series_to_animated_gif(L, cwd+"/images/animated.gif").delay()


# coding: utf-8

# In[1]:

from fenics import *
from mshr import *

import numpy as np
import matplotlib.pyplot as plt

get_ipython().run_line_magic('matplotlib', 'inline')

# In[2]:

# Equation parameters
T = 2.0
num_steps = 10
dt = T / num_steps

alpha = 3
beta = 1.2

# In[3]:

# Create mesh and define function space
nx = ny = 8
mesh = UnitSquareMesh(nx, ny)
V = FunctionSpace(mesh, 'P', 1)

# In[4]:

# Boundary condition
def boundary(x, on_boundary):
    return on_boundary

u_D = Expression('1 + x[0]*x[0] + alpha*x[1]*x[1] + beta*t', degree=2, alpha=alpha, beta=beta, t=0)
bc = DirichletBC(V, u_D, boundary)

# In[5]:

#Define initial value at time = 0
u_n = project(u_D, V)

# In[6]:

#Define problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(beta - 2 - 2 * alpha)

F = u*v*dx + dt*dot(grad(u), grad(v))*dx - (u_n + dt*f)*v*dx
a, L = lhs(F), rhs(F)

# In[9]:

# Time-stepping solution
u = Function(V)
t = 0

vtkfile = File('heat_gaussian_solution/solution.pvd')

for n in range(num_steps):
    t += dt
    u_D.t = t

    solve(a == L, u, bc)

    vtkfile << (u, t)
    plot(u)

    vertex_values_u = u.compute_vertex_values(mesh)
    u_e = u_D.compute_vertex_values(mesh)
    error = np.abs(vertex_values_u - u_e).max()

    print('t = %.2f, error = %.2f' % (t, error))

    # Update previous solution
    u_n.assign(u)


# fmt: off
import logging
from pathlib import Path

from farm.data_handler.processor import InferenceProcessor
from farm.infer import Inferencer
from farm.modeling.adaptive_model import AdaptiveModel
from farm.modeling.language_model import LanguageModel
from farm.modeling.tokenization import Tokenizer
from farm.utils import set_all_seeds, initialize_device_settings

def embedding_extraction():
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s -   %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        level=logging.INFO)

    ##########################
    ########## Settings
    ##########################
    set_all_seeds(seed=42)
    # load from a local path:
    #lang_model = Path("../saved_models/glove-german-uncased")
    # or through s3
    lang_model = "glove-german-uncased" #only glove or word2vec or converted fasttext (fixed vocab) embeddings supported
    do_lower_case = True
    use_amp = None
    device, n_gpu = initialize_device_settings(use_cuda=True, use_amp=use_amp)

    # Create a InferenceProcessor
    tokenizer = Tokenizer.load(pretrained_model_name_or_path=lang_model, do_lower_case=do_lower_case)
    processor = InferenceProcessor(tokenizer=tokenizer, max_seq_len=128)

    # Create an AdaptiveModel
    language_model = LanguageModel.load(lang_model)
    model = AdaptiveModel(
        language_model=language_model,
        prediction_heads=[],
        embeds_dropout_prob=0.1,
        lm_output_types=["per_sequence"],
        device=device)

    # Create Inferencer for embedding extraction
    inferencer = Inferencer(
        model=model,
        processor=processor,
        task_type="embeddings"
    )

    # Extract vectors
    basic_texts = [
        {"text": "Schartau sagte dem Tagesspiegel, dass Fischer ein Idiot sei"},
        {"text": "Martin Müller spielt Handball in Berlin"},
    ]

    result = inferencer.extract_vectors(
        dicts=basic_texts,
        extraction_strategy="cls_token",
        extraction_layer=-1
    )
    print(result)
    inferencer.close_multiprocessing_pool()

if __name__ == "__main__":
    embedding_extraction()

# fmt: on


def jumps(array):
    start = 0
    cur = array[0] + 1
    if array[0] >= n:
        return 1
    times = 0
    dp = [-1] * n
    for j in range(cur):
        dp[j] = 1
    for i in range(cur, n):
        for j in range(0,i):
            if dp[j] > 0 and j + array[j] >= i:
                dp[i] = min(dp[j] + 1,dp[i]) if dp[i] > 0 else dp[j] + 1
    return dp[-1]

n = int(input())
array = [int(x) for x in input().split()]

print(jumps(array))

# jumps([3,2,1,0,0]) = 0


"""
#! 判断一个数是否是质数, 如果一个数只能被1和它自身整除, 那么这个数是质数, 最小的质数是2
#! 时间复杂度O(logn)
"""

def judge_prime(n: int)->bool:
    """判断一个数是不是质数

    Args:
        n (int): 输入的数

    Returns:
        bool: 如果是质数返回True, 否则返回False
    """
    i = 2
    while(i*i <= n):
        if(n % i == 0):
            return False
        i+=1
    return n>=2


# -*- coding: utf-8 -*-
from numpy import append, arange, array, exp, floor, nan, tensordot
from numpy.version import version as npVersion
from pandas_ta._typing import DictLike, Int, IntFloat
from pandas import Series
from pandas_ta.utils import strided_window, v_offset, v_pos_default, v_series

def alma(
    close: Series, length: Int = None,
    sigma: IntFloat = None, dist_offset: IntFloat = None,
    offset: Int = None, **kwargs: DictLike
) -> Series:
    """Arnaud Legoux Moving Average (ALMA)

    The ALMA moving average uses the curve of the Normal (Gauss) distribution,
    which can be shifted from 0 to 1. This allows regulating the smoothness
    and high sensitivity of the indicator. Sigma is another parameter that is
    responsible for the shape of the curve coefficients. This moving average
    reduces lag of the data in conjunction with smoothing to reduce noise.

    Sources:
        https://www.sierrachart.com/index.php?page=doc/StudiesReference.php&ID=475&Name=Moving_Average_-_Arnaud_Legoux
        https://www.prorealcode.com/prorealtime-indicators/alma-arnaud-legoux-moving-average/

    Args:
        close (pd.Series): Series of 'close's
        length (int): It's period, window size. Default: 9
        sigma (float): Smoothing value. Default 6.0
        dist_offset (float): Value to offset the distribution where
            min 0 (smoother), max 1 (more responsive). Default 0.85
        offset (int): How many periods to offset the result. Default: 0

    Kwargs:
        fillna (value, optional): pd.DataFrame.fillna(value)
        fill_method (value, optional): Type of fill method

    Returns:
        pd.Series: New feature generated.
    """
    # Validate
    length = v_pos_default(length, 9)
    close = v_series(close, length)

    if close is None:
        return

    sigma = v_pos_default(sigma, 6.0)
    if isinstance(dist_offset, float) and 0 <= dist_offset <= 1:
        offset_ = float(dist_offset)
    else:
        offset_ = 0.85

    offset = v_offset(offset)

    # Calculate
    np_close = close.values
    x = arange(length)
    k = floor(offset_ * (length - 1))
    weights = exp(-0.5 * ((sigma / length) * (x - k)) ** 2)
    weights /= weights.sum()

    if npVersion >= "1.20.0":
        from numpy.lib.stride_tricks import sliding_window_view
        window = sliding_window_view(np_close, length)
    else:
        window = strided_window(np_close, length)
    result = append(array([nan] * (length - 1)),
                    tensordot(window, weights, axes=1))
    alma = Series(result, index=close.index)

    # Offset
    if offset != 0:
        alma = alma.shift(offset)

    # Fill
    if "fillna" in kwargs:
        alma.fillna(kwargs["fillna"], inplace=True)
    if "fill_method" in kwargs:
        alma.fillna(method=kwargs["fill_method"], inplace=True)

    # Name and Category
    alma.name = f"ALMA_{length}_{sigma}_{offset_}"
    alma.category = "overlap"

    return alma