from collections import defaultdict
from random import randint
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app)

db = {}
client_db = {}
policies = defaultdict(list)
devices = []

test_policies = [
    {
        "id": "0",
        "name": "Temperature Sensor",
        "authorized_devices": ["CO2 Sensor", "Air Quality Sensor"],
        "authorized_users": ["ron@test.com", "banana@gmail.com"],
    },
    {
        "id": "1",
        "name": "Humidity Sensor",
        "authorized_devices": ["Air Quality Sensor"],
        "authorized_users": ["test@gmail.com"],
    },
]

# @app.after_request
# def add_headers(response):
#     response.headers.add('Content-Type', 'application/json')
#     response.headers.add('Access-Control-Allow-Origin', '*')
#     response.headers.add('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS, PATCH')
#     response.headers.add('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
#     response.headers.add('Access-Control-Expose-Headers', 'Content-Type,Content-Length,Authorization,X-Pagination')
#     return response

def option_todo(id):
    return "", 204

app.add_url_rule(
    "/", view_func=option_todo, provide_automatic_options=False, methods=["OPTIONS"]
)
app.add_url_rule(
    r"/<path:path>",
    view_func=option_todo,
    provide_automatic_options=False,
    methods=["OPTIONS"],
)

@app.after_request
def after_request(response):
    response.headers["Access-Control-Allow-Methods"] = "*"
    response.headers["Access-Control-Allow-Origin"] = "*"
    response.headers["Vary"] = "Origin"
    return response

@app.route("/users", methods=["POST"])
def create_user():
    request_data = request.json
    print(f"Creating a new user with requst data: {request_data}")

    db[request_data["email"]] = request_data["password"]

    return jsonify()

@app.route("/clients", methods=["POST"])
def create_client():
    request_data = request.json
    print(f"Creating a new client with requst data: {request_data}")

    client_db[request_data["email"]] = request_data["password"]

    print(f"Client db: {client_db}")

    return jsonify({"token": "fake-token-for-client"})

@app.route("/users/signin", methods=["PUT"])
def signin_user():
    request_data = request.json
    print(f"Signing in user with request data: {request_data}")

    if (
        request_data["email"] in db
        and db[request_data["email"]] == request_data["password"]
    ):
        return jsonify({"token": "faker-token"})
    else:
        return jsonify({"error": "Invalid credentials"}), 401

@app.route("/devices", methods=["GET"])
@cross_origin(origin="*")
def get_devices():
    print(f"Get devices Request headers are {request.headers}")
    res = jsonify(
        {
            "devices": [
                {
                    "id": "0",
                    "name": "CO2 Sensor",
                    "region": "US West",
                    "ipfs": "lkasdjf9325kadjsfkj",
                    "updated_at": "2020-01-01",
                },
                {
                    "id": "1",
                    "name": "Air Quality Sensor",
                    "region": "US East",
                    "ipfs": "dsafadsf4tadsfads42",
                    "updated_at": "2020-04-19",
                },
                {
                    "id": "2",
                    "name": "Temperature Sensor",
                    "region": "US Central",
                    "ipfs": "kadsjfki45889tioeqw",
                    "updated_at": "2020-05-09",
                },
            ]
        }
    )
    res.headers.add("Access-Control-Allow-Origin", "*")
    return res

@app.route("/devices", methods=["POST"])
def register_device():
    request_data = request.json
    print(f"Registering a new device with request data: {request_data}")

    if "id" not in request_data:
        return jsonify({"error": "Device ID is required"}), 400

    devices.append(request_data)

    return jsonify({"deviceID": request_data["id"], "apiKey": "fake-api-key"})

@app.route("/policies", methods=["GET"])
@cross_origin(origin="*")
def get_policies():
    print(f"Get policies Request headers are {request.headers}")
    res = jsonify(
        {
            "policies": test_policies,
        }
    )
    res.headers.add("Access-Control-Allow-Origin", "*")
    return res

@app.route("/policies", methods=["POST"])
def create_policy():
    print(f"Request headers are {request.headers}")
    request_data = request.json
    print(f"Creating a new policy with request data: {request_data}")
    policies[request_data["device_id"]].append(request_data["accessing_device_id"])
    policies[request_data["device_id"]].append(request_data["accessing_user_id"])

    print(f"Policy db: {policies}")

    test_policies.append(
        {
            "id": str(randint(0, 100000)),
            "name": "Test Addition",
            "authorized_devices": ["Test Sensor"],
            "authorized_users": ["test@test.com"],
        },
    )

    return jsonify()

@app.route("/ipfs-hash/<device_id>", methods=["PUT"])
def get_ipfs_hash(device_id):
    if device_id == "":
        return jsonify({"error": "No device id provided"}), 400

    print(f"Getting ipfs hash for device id: {device_id} with body {request.json}")

    return jsonify({"ipfsHash": "QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX"})

@app.route("/ipfs-hash", methods=["GET"])
def get_ipfs_hashs():
    return jsonify({"ipfsHash": "QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX"})

# Routes for IoT device
@app.route("/iot/ipfs-hash", methods=["PUT"])
def update_ipfs_hash():
    print(f"/iot/ipfs-hash called with body {request.json}")
    request_data = request.json
    if "device_id" not in request_data:
        return jsonify({"error": "No device id provided"}), 400

    if "ipfs_hash" not in request_data:
        return jsonify({"error": "No ipfs hash provided"}), 400

    if "device_key" not in request_data:
        return jsonify({"error": "No device key provided"}), 400

    return jsonify(), 200

@app.route("/iot/data-access", methods=["PUT"])
def data_access():
    print(f"/iot/data-access called with body {request.json}")
    request_data = request.json
    if "device_id" not in request_data:
        return jsonify({"error": "No device id provided"}), 400

    if "device_key" not in request_data:
        return jsonify({"error": "No device key provided"}), 400

    return jsonify({"ipfs-hash": "<IPFS HASH>"}), 200

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


import pygame
from pygame.sprite import Sprite

# When you use sprites, you can group related elements in
# your game and act on all the grouped elements at once.
class Bullet(Sprite):
    #  """Create a bullet object at the ship's current position."""
    def __init__(self, ai_game):
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = self.settings.bullet_color

         # Create a bullet rect at (0, 0) and then set correct position.
        # The bullet isn’t based on an image, so we have to build a rect from scratch using the pygame.Rect() class.
        # This class requires the x- and y-coordinates of the top-left corner of the rect, and the width and height of the rect
        self.rect = pygame.Rect(0, 0, self.settings.bullet_width, self.settings.bullet_height)
        # we set the bullet’s midtop attribute to match the ship’s midtop attribute. This will make the bullet emerge from the top of the ship, making it
        # look like the bullet is fired from the ship.
        self.rect.midtop = ai_game.ship.rect.midtop

        # Store the bullet's position as a decimal value.
        self.y = float(self.rect.y)

        # Store the bullet's position as a decimal value.
        self.y = float(self.rect.y)

    def update(self):
        # """Move the bullet up the screen."""
        # Update the decimal position of the bullet
        self.y -= self.settings.bullet_speed
        self.rect.y = self.y

    def draw_bullet(self):
        #  """Draw the bullet to the screen."""
        pygame.draw.rect(self.screen, self.color, self.rect)


from math import isclose
from typing import Tuple

import astropy.units as u
import hypothesis.strategies as st
import numpy as np
import pandas as pd
import pytest
from astropy.table import setdiff
from astropy.table.operations import join
from astropy.units.core import UnitConversionError
from hypothesis import assume
from hypothesis.core import given

from kanon.models.models import (
    long_of_the_tropical_mean_sun,
    mercury_equ_proportional_minutes,
)
from kanon.tables.htable import HTable, join_multiple
from kanon.tables.symmetries import Symmetry

sample = {"a": [1, 2, 3, 4, 5], "b": [5, 9, 12, 25, 26]}

def make_sample_table(index="a"):
    return HTable(sample, index=index)

def test_init():
    table = HTable(sample)
    assert table[2]["b"] == 12

    with pytest.raises(IndexError):
        table.get(3)

    table.add_index("b")
    assert table.get(5) == 1

gen_table_strategy = st.builds(
    HTable,
    st.lists(
        st.tuples(
            st.integers(min_value=int(-1e15), max_value=int(1e15)),
            st.floats(allow_nan=False, allow_infinity=False, width=16),
        ),
        min_size=2,
        unique_by=(lambda x: x[0]),
    ).map(lambda x: list(zip(*sorted(x)))),
    names=st.just(("A", "B")),
    index=st.just("A"),
)

@given(gen_table_strategy)
def test_symmetry(tab: HTable):
    assume(len(tab) > 1)
    tab.symmetry = [Symmetry("mirror", sign=-1)]
    df = tab.to_pandas()
    assert df["B"].iloc[0] == -df["B"].iloc[-1]
    tab.symmetry = [Symmetry("periodic")]
    df = tab.to_pandas()
    assert df["B"].iloc[0] == df["B"].iloc[len(tab)]

@given(
    gen_table_strategy.flatmap(
        lambda x: st.tuples(
            st.floats(
                min_value=float(min(x["A"])),
                max_value=float(max(x["A"])),
                allow_nan=False,
            ),
            st.just(x),
        )
    )
)
def test_interpolation(hypo: Tuple[float, HTable]):
    key, tab = hypo
    fres = np.interp(
        key, [float(x) for x in tab.columns[0]], [float(x) for x in tab.columns[1]]
    )
    sres = tab.get(key)
    assert isclose(fres, float(sres), abs_tol=1e9)

def test_apply():
    tab = make_sample_table()

    assert np.array_equal(
        tab.apply("b", lambda x: x + 2)["b"],
        np.add(tab["b"], np.array([2] * len(tab))),
    )

    tab_float = tab.apply("b", lambda x: x + 0.3)
    assert tab_float["b"].dtype == np.dtype("float64")
    tab_int = tab_float.apply("b", round, "rounded")
    assert tab_int["rounded"].dtype == np.dtype("int64")

    def tostr(x):
        return f"{x}a"

    tab_str = tab.apply("b", tostr)
    assert tab_str["b"].dtype.char == "U"

def test_index():
    tab = make_sample_table()

    assert tab.loc[1]["b"] == 5
    with pytest.raises(KeyError):
        tab.loc[6]

    new_tab = tab.copy(set_index="b")
    assert new_tab.loc[5]["a"] == 1
    with pytest.raises(KeyError):
        new_tab.loc[1]

def test_populate():
    tab = make_sample_table(index="b")

    pop_array = [i for i in range(5, 15)]

    populated = tab.populate(pop_array, method="interpolate")

    assert len(populated) == len(set(populated["b"]).union(pop_array))

    assert populated.loc[6]["a"] == tab.get(6)

def test_diff():
    tab = make_sample_table()

    assert list(tab.diff()["b"]) == [0, 4, 3, 13, 1]
    assert list(tab.diff(prepend=[3])["b"]) == [2, 4, 3, 13, 1]
    assert list(tab.diff(append=[24])["b"]) == [4, 3, 13, 1, -2]

    with pytest.raises(ValueError):
        tab.diff(prepend=[1, 1])

def test_fill():
    tab = make_sample_table()
    tab["b"] = tab["b"].astype("float64")

    tab = tab.populate([i / 4 for i in range(4, 16)])

    tab_filled = tab.fill("distributed_convex", (2, 4))
    assert list(tab_filled.loc[3:4]["b"]) == [12, 15, 18, 21, 25]

    tab_filled = tab.fill("distributed_concave", (2, 4))
    assert list(tab_filled.loc[3:4]["b"]) == [12, 16, 19, 22, 25]

    tab_unmasked = tab.filled(50)

    assert (
        len(setdiff(tab_unmasked, tab.fill("distributed_convex", (4, 5)).filled(50)))
        == 0
    )
    assert (
        len(
            setdiff(tab_unmasked, tab.fill("distributed_convex", (3.5, 3.5)).filled(50))
        )
        == 0
    )

    def fill_50(df: pd.DataFrame):
        return df.fillna(50)

    assert len(setdiff(tab_unmasked, tab.fill(fill_50))) == 0

    with pytest.raises(ValueError) as err:
        tab.fill("distributed_convex", (2.5, 4))
    assert str(err.value) == "First and last rows must not be masked"

    with pytest.raises(ValueError) as err:
        tab.fill("not a method", (2, 4))
    assert str(err.value) == "Incorrect fill method"

def test_join_multiple():
    data = [HTable({"a": x, "b": x}) for x in [[1, 2, 3], [8, 10, 13], [435, 13, 3.5]]]
    multiple_joined = join_multiple(*data, join_type="outer")
    single_joined = join(
        join(data[0], data[1], join_type="outer"), data[2], join_type="outer"
    )

    assert len(setdiff(multiple_joined, single_joined)) == 0

def test_plot():
    tab = make_sample_table()
    plot = tab.plot2d(".")
    assert len(plot) == 1
    assert plot[0].get_marker() == "."

def test_get_with_quantity():
    tab = make_sample_table()

    tab["a"].unit = u.degree

    assert tab.get(3) == 12
    assert tab.get(3 * u.degree) == 12

    assert tab.get(180 * u.arcmin) == 12

    with pytest.raises(UnitConversionError):
        assert tab.get(3 * u.deg_C)

def test_freeze():
    tab = make_sample_table()
    tab.freeze()
    assert tab.loc[3]["b"] == 12
    assert tab.get(3) == 12
    tab.loc[3]["b"] = 4
    assert tab.loc[3]["b"] == 4
    assert tab.get(3) == 12
    tab.unfreeze()
    assert tab.loc[3]["b"] == 4
    assert tab.get(3) == 4

def test_from_model():
    args = list(range(5))
    tab = HTable.from_model(long_of_the_tropical_mean_sun, args, (2,))
    assert tab.get(0) == long_of_the_tropical_mean_sun(0, 2)
    assert tab.get(1) == long_of_the_tropical_mean_sun(1, 2)
    assert len(tab) == 5

    tab_double = HTable.from_model(
        mercury_equ_proportional_minutes, args, (1, 2), arguments2=args
    )
    assert tab_double.get(1).get(1) == mercury_equ_proportional_minutes(1, 1, 1, 2)
    assert tab_double.get(2).get(2) == mercury_equ_proportional_minutes(2, 2, 1, 2)

    assert len(tab_double) == 5
    assert len(tab_double.get(0)) == 5

    assert tab_double.table_type == mercury_equ_proportional_minutes.table_type

def test_shift():
    tab = make_sample_table()

    assert tab.displace("b", -1)["b"][0] == 4

    shifted = tab.shift("a", 2)

    assert tab["a"][0] == shifted["a"][2]
    assert tab["a"][-1] == shifted["a"][1]

def test_double():
    args = list(range(5))

    tab = HTable.from_model(
        mercury_equ_proportional_minutes, args, (1, 2), arguments2=args + [6, 7]
    )

    df = tab.to_pandas()

    assert len(df) == 5
    assert len(df.loc[0]) == 7


from collections import deque

def method1():
    file1 = open("input files/day06.txt")
    s = file1.readline()
    arr = s.split(",")
    fishcount = deque([0 for i in range(9)])
    for fish in arr:
        fishcount[int(fish)] += 1
    for x in range(256):
        fishcount.rotate(-1)
        fishcount[6] += fishcount[8]
    print(sum(fishcount))

method1()


import re, os

"""
Simple script to create emails_and_names.txt from the section lists
provided on Piazza. Download your section list to the same directory
as this script and run it to create the emails and names text file
for the short problem grading script.
"""

def main():
    currdir = os.listdir(".")
    r = re.compile("Section.*csv")
    csv_name = list(filter(r.match, currdir))
    if len(csv_name) == 0:
        print("Could not find Section_1x.csv!")
        return
    elif len(csv_name) > 1:
        print("Found multiple Section_1x.csv files, using " + csv_name[0])
    csv_file = open(csv_name[0])
    csv_lines = csv_file.readlines()[1:]
    names_file = open("emails_and_names.txt", "w")
    for i in range(len(csv_lines)):
        csv_lines[i] = csv_lines[i].strip("#\n").split(",")
        names_file.write("{}\t{}, {}\n".format(csv_lines[i][0].lower(), csv_lines[i][1], csv_lines[i][2]))
main()


import sys
from argparse import ArgumentParser, Namespace
from pathlib import Path
from typing import Optional, Sequence, Text

from ezno_convert.common import VERSION, DATE_FORMAT
from ezno_convert.convert import WORDConverter, PPTConverter, XLConverter
from ezno_convert.enums import WORD, PPT, XL

class CommandLineInterface(ArgumentParser):
    def __init__(self):
        super().__init__(description=f'Native Office Converter {VERSION}')
        self.add_argument('PATH', nargs='+', type=Path, help='''
        Path(s) of files or folders to convert.
        If a folder is specified, include all files in it (to filter types, see folder options below)
        ''')
        self.add_argument('-o', '--output', type=Path, metavar='PATH', required=False, help='''
        Where to save output files.
        If input PATH is a folder (or multiple paths) this output PATH must also be a folder.
        Default is to save output files in the same folder as input files.
        If the output PATH isn't a folder (input is a file) and the proper extension is omitted,
        it will be added automatically.
        ''')
        self.add_argument('-c', '--converter', metavar='TYPE', default='PDF', help='''
        Type of conversion to preform. For available types see --list_types (Default: %(default)s)
        ''')
        self.add_argument('-t', '--no_timestamp', action='store_true', help='''
        Do not add timestamp to output filenames. Warning: this will overwrite existing output files.
        ''')
        self.add_argument('-d', '--dateformat', default=DATE_FORMAT, help='''
        Specify datetime format to add to filenames. Ignored if --no-timestamp specified. Default: %(default)s
        ''')
        # self.add_argument('-s', '--simulate', action='store_true',
        #                   help='List files and simulate conversions without actually converting anything')
        self.add_argument('-l', '--list_types', action='store_true', help='Print available conversion types and exit')
        self.add_argument('-v', '--version', action='version', version=f'%(prog)s {VERSION}')

        folder = self.add_argument_group(
            title='Folder Options',
            description='These options apply only to input paths that are folders, they are ignored otherwise'
        )
        folder.add_argument('-r', '--recursive', action='store_true', help='Search in sub-folders recursively')
        folder.add_argument('-w', '--word', action='store_true',
                            help=f'Convert all Word Documents {WORD.extensions.value}')
        folder.add_argument('-p', '--powerpoint', action='store_true',
                            help=f'Convert all PowerPoint Presentations {PPT.extensions.value}')
        folder.add_argument('-x', '--excel', action='store_true',
                            help=f'Convert all Excel Spreadsheets {XL.extensions.value}')
        folder.add_argument('-a', '--all', action='store_true', help=f'''
        Convert all possible files {WORD.extensions.value + PPT.extensions.value + XL.extensions.value}.
        Possible conversions depend on conversion type.
        Example 1: "-a -c PDF" and "-a -c XPS" will apply to all file types
        Example 2: "-a -c AnimatedGIF" will only apply to PowerPoint Presentations even without "-p"
        ''')

        xl = self.add_argument_group(
            title='Excel only options',
            description=f'These options apply only to excel files {XL.extensions.value}, they are ignored otherwise'
        )
        xl.add_argument('--split', action='store_true', help='Convert each worksheet separately')
        xl.add_argument('--sheet', nargs='+', help='''
        Specify names or indexes of specific sheets to convert, instead of converting the entire file. Implies --split
        ''')

    def parse_args(self, args: Optional[Sequence[Text]] = None) -> Namespace:
        args = super().parse_args(args)
        dirs = [p for p in args.PATH if p.is_dir()]

        for p in args.PATH:
            if not p.is_file() and not p.is_dir():
                print(f'Path not valid: {p}', file=sys.stderr)
        args.PATH = [p for p in args.PATH if p.is_file() or p.is_dir()]
        if not args.PATH:
            self.error('No paths were valid')

        if len(args.PATH) == 1 and args.PATH[0].is_file():
            valid_output = args.output is None or args.output.is_dir() or args.output.parent.is_dir()
        elif len(args.PATH) > 1 or dirs:
            valid_output = args.output is None or args.output.is_dir()
        else:
            valid_output = False

        if not valid_output:
            self.error(f'Output path invalid ({args.output})')

        if not any((args.word, args.powerpoint, args.excel)):
            args.all = True

        if args.no_timestamp:
            args.dateformat = None

        word = [p for p in args.PATH if p.suffix in WORD.extensions.value]
        args.word = word + dirs if (args.word or args.all) and hasattr(WORD, args.converter) else []
        excel = [p for p in args.PATH if p.suffix in XL.extensions.value]
        args.excel = excel + dirs if (args.excel or args.all) and hasattr(XL, args.converter) else []
        powerpoint = [p for p in args.PATH if p.suffix in PPT.extensions.value]
        args.powerpoint = powerpoint + dirs if (args.powerpoint or args.all) and hasattr(PPT, args.converter) else []

        if args.sheet:
            args.sheet = [int(sh) if sh.isdigit() else sh for sh in args.sheet]
        elif args.split:
            args.sheet = True

        return args

    def run_converters(self, args: Optional[Sequence[Text]] = None):
        opt = self.parse_args(args)
        kwargs = dict(dst=opt.output, recursive=opt.recursive, date_fmt=opt.dateformat)

        if opt.word:
            word_gen = WORDConverter(src=opt.word, target=getattr(WORD, opt.converter, None), **kwargs)
            word_gen.execute_all(True)
        if opt.powerpoint:
            pp_gen = PPTConverter(src=opt.powerpoint, target=getattr(PPT, opt.converter, None), **kwargs)
            pp_gen.execute_all(True)
        if opt.excel:
            xl_gen = XLConverter(src=opt.excel, target=getattr(XL, opt.converter, None), sheets=opt.sheet, **kwargs)
            xl_gen.execute_all(True)

def main():
    CommandLineInterface().run_converters()

if __name__ == '__main__':
    main()


#Day 8 part 1

#easy
#     "1" : 2
#     "4" : 4
#     "7" : 3
#     "8" : 7

with open("DataDay8.txt", "r") as f:
    lines = f.readlines()

def difficulty(lst):
    hard = []
    easy = []
    for line in lst:
        inp, outp = line.split("|")
        inp, outp = inp.split(), outp.split()
        for out in outp:
            if len(out) in [2,4,3,7]:
                easy.append(out)
            else:
                hard.append(out)
    return easy, hard

print(len(difficulty(lines)[0]))


V = int(input())
P1 = int(input())
P2 = int(input())
H = float(input())

if 1 <= V <= 10000 or 1 <= P1 <= 5000 or 1 <= P2 <= 5000 or 1 <= H <= 24:
    P1_liters = (P1 * H)
    P2_liters = (P2 * H)
    total_V = P1_liters + P2_liters
    if total_V <= V:

        print(f'The pool is {(total_V / V) * 100: .2f}% full. Pipe 1: {(P1_liters / total_V) * 100: .2f}%. Pipe 2: {(P2_liters / total_V) * 100: .2f}%.')
    else:
        print(f'For {H} hours the pool overflows with {total_V - V: .2f} liters.')
else:
    print('Error')

soft_uni_basic/Conditional Statements/more_exercise/01. Pipes In Pool.py


import logging
import re

from jira.exceptions import JIRAError

from bert_e import exceptions
from bert_e.lib import jira as jira_api
from .utils import bypass_jira_check

LOG = logging.getLogger(__name__)

def jira_checks(job):
    """Performs consistency checks against associated Jira issue."""
    if bypass_jira_check(job):
        return

    if job.git.src_branch.prefix in job.settings.bypass_prefixes:
        LOG.debug("Bypassing JIRA checks due to branch prefix '%s'",
                  job.git.src_branch.prefix)
        return

    if not all([job.settings.jira_keys,
                job.settings.jira_email,
                job.settings.jira_account_url]):
        return

    if not check_issue_reference(job):
        return

    issue = get_jira_issue(job)
    check_project(job, issue)
    check_issue_type(job, issue)

    if not job.settings.disable_version_checks:
        check_fix_versions(job, issue)

def get_jira_issue(job):
    """Return the jira issue associated with a pull request.

    Raises:
        JiraIssueNotFound: if the issue doesn't exist.

    """
    try:
        return jira_api.JiraIssue(
            account_url=job.settings.jira_account_url,
            issue_id=job.git.src_branch.jira_issue_key,
            email=job.settings.jira_email,
            token=job.settings.jira_token
        )
    except JIRAError as err:
        if err.status_code == 404:
            raise exceptions.JiraIssueNotFound(
                issue=job.git.src_branch.jira_issue_key,
                active_options=job.active_options
            ) from err
        raise

def check_issue_reference(job) -> bool:
    """Check that the name of the pull request's source branch references
    a jira through an issue id.

    Returns:
        True if the source branch references a jira issue. False otherwise.

    Raises:
        MissingJiraId: if there is no issue id and any of the destination
                       branches doesn't allow ticketless pull requests.

    """

    src_branch = job.git.src_branch
    if not src_branch.jira_issue_key:
        for dst_branch in job.git.cascade.dst_branches:
            if not dst_branch.allow_ticketless_pr:
                raise exceptions.MissingJiraId(
                    source_branch=src_branch.name, dest_branch=dst_branch.name,
                    active_options=job.active_options
                )
        return False
    return True

def check_project(job, issue):
    """Check that the jira project referenced by the pull request is one
    we authorize.

    Raises:
        IncorrectJiraProject: if the project is not handled by this instance
                              of BertE.

    """
    if job.git.src_branch.jira_project not in job.settings.jira_keys:
        raise exceptions.IncorrectJiraProject(
            issue=issue, expected_project=', '.join(job.settings.jira_keys),
            active_options=job.active_options
        )

def check_issue_type(job, issue):
    """Check if the Jira issue is of a supported type.

    Raises:
        IssueTypeNotSupported if the issue is not of a type explicitely
        supported in the configuration.

    """
    issuetype = issue.fields.issuetype.name
    prefixes = job.settings.prefixes
    if not prefixes:
        return

    if issuetype not in prefixes:
        raise exceptions.IssueTypeNotSupported(
            issue=issue, pairs=prefixes, active_options=job.active_options
        )

def check_fix_versions(job, issue):
    """Check that the fixVersions field of the Jira issue correctly documents
    the versions this pull request is actually targetting.

    Raises:
        IncorrectFixVersion: if versions are inconsistent.

    """
    issue_versions = set(version.name for version in issue.fields.fixVersions)
    expected_versions = set(job.git.cascade.target_versions)

    # Ignore suffixed versions such as "5.1.9_hf7" in that check
    # Do not ignore X.Y.Z.0 version
    vfilter = re.compile(r'^\d+\.\d+\.\d+(\.0|)$')
    checked_versions = set(v for v in issue_versions if vfilter.match(v))

    # If the job is targetting a hotfix branch, feed hf_target with the version
    hf_target = None
    if len(expected_versions) == 1:
        target_version = list(expected_versions)[0]
        hf_filter = re.compile(r'^\d+\.\d+\.\d+\.\d+$')
        if hf_filter.match(target_version):
            hf_target = target_version

    if hf_target:
        if hf_target not in issue_versions:
            raise exceptions.IncorrectFixVersion(
                issue=issue,
                issue_versions=sorted(issue_versions),
                expect_versions=sorted(expected_versions),
                active_options=job.active_options
            )
    elif checked_versions != expected_versions:
        raise exceptions.IncorrectFixVersion(
            issue=issue,
            issue_versions=sorted(issue_versions),
            expect_versions=sorted(expected_versions),
            active_options=job.active_options
        )


#Import and create a Spark session
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from pyspark.sql.functions import col, skewness, kurtosis
from pyspark.mllib.stat import Statistics
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
spark = SparkSession.builder.appName("Python Spark create RDD")\
		.config("spark.some.config.option", "some-value")\
		.getOrCreate()

#Set path
path = "E:\\ML\\Santander\\"

#Import data
df = spark.read.format('com.databricks.spark.csv')\
					.options(header="true", inferschema ='true')\
					.load(path+"train.csv",header=True)

df.show(2)
df.printSchema()
df.columns

#Defintion
def describe_pd(df_in, columns, deciles = False):
	if deciles:
		percentiles = np.array(range(0,110,10))
	else:
		percentiles = [25,50,75]
	percs = np.transpose([np.percentile(df_in.select(x).collect(),percentiles)\
					   for x in columns])
	percs = pd.DataFrame(percs, columns = columns)
	percs['summary'] = [str(p) + '%' for p in percentiles]
	spark_describe = df_in.describe().toPandas()
	new_df = pd.concat([spark_describe, percs],ignore_index = True)
	new_df = new_df.round(2)
	return new_df[['summary']+columns]

#Describe
selected = [s for s in df.columns if 'var_' in s]
print(selected)
df.select(selected).describe().show()
selected = ['var_0','var_1','var_2','var_3','var_4', 'var_5']
describe_pd(df,selected)
describe_pd(df,selected,deciles=True)

#Skewness and kurtosis
df.select(skewness('var_0'),kurtosis('var_0')).show()

#Plot histogram
var = [ 'var_108']
bins = np.arange(0,105,5.0)
df[var].describe().show()
plt.figure(figsize=(10,8))
plt.hist(df_new[var].astype(float),alpha=0.8,histtype='bar',ec='black')

df.dtypes
df[df.var_100 <14.0]

#Correlation matrix
selected = ['target','var_0','var_1','var_2','var_3','var_4', 'var_5']
features = df.select(selected).rdd.map(lambda row: row[0:])
corr_mat = Statistics.corr(features,method="pearson")
corr_df = pd.DataFrame(corr_mat)
corr_df.index, corr_df.columns = selected, selected
print(corr_df.to_string())

#Pair plot
sns.pairplot(df.select(selected).toPandas())
plt.show()

df.select("target").describe().show()


import pygame
import random
import numpy as np
import matplotlib.pyplot as plt

pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True

num_episodes = 1
total_reward = 0

full_height = 80
duck_height = 40

player_height = full_height
player_width = 40

player_x = 400
player_y = 400

jump_target = 360  # Target y-coordinate for jumping
jump_speed = 0  # Initial jump speed
jump_acceleration = -0.5  # Acceleration for the jump
gravity = 0.5  # Gravity applied to the player

obstacle_width = 40
obstacle_height = 40
obstacle_x = 1240
obstacle_y = 440
obstacle_speed = 10  # Speed at which the obstacle moves to the left

on_ground = True
ascending = False  # Flag to track if the player is ascending or descending

game_over = False

score = 0

game_font = pygame.font.Font(None, 36)  # Font for displaying the score

add_score = True

replay_button = pygame.Rect(550, 300, 180, 60)

def spawn_obstacle():
    global obstacle_x, obstacle_y
    obstacle_x = 1240
    obstacle_y = 440

spawn_obstacle()

# Q-learning parameters
alpha = 0.7  # Learning rate
gamma = 0.9  # Discount factor
epsilon = 0.1  # Exploration rate

# Q-table
num_states = 2  # Number of states (actions: jump or duck)
num_distances = 1280  # Number of possible distances (based on the screen width)
num_actions = 2  # Number of actions (0: do nothing, 1: jump, 2: duck)
q_table = np.zeros((num_states, num_distances, num_actions))

episode_numbers = []
total_rewards = []

def choose_action(state, distance):
    if random.uniform(0, 1) < epsilon:
        # Explore: choose a random action
        return random.randint(0, num_actions - 1)
    else:
        # Exploit: choose the action with the highest Q-value for the current state
        return np.argmax(q_table[state, distance])

def get_distance():
    return obstacle_x - player_x

def update_q_table(state, action, reward, distance):
    global game_over

    if game_over:
        # Apply penalty for dying
        reward = -100

    # Update Q-value for the previous state-action pair using the Q-learning formula
    q_table[state, distance, action] = (1 - alpha) * q_table[state, distance, action] + alpha * (
            reward + gamma * np.max(q_table[state, distance]))

def perform_action(action):
    global on_ground, ascending, jump_speed, player_height, player_y

    if action == 1 and on_ground:
        on_ground = False
        ascending = True
        jump_speed = -10
    elif action == 2 and on_ground:
        player_height = duck_height
        player_y = 440
    elif on_ground:
        player_height = full_height
        player_y = 400

def update_game_state():
    global on_ground, player_y, jump_speed, ascending, player_height, game_over

    if not on_ground:
        if ascending:
            player_y += jump_speed
            jump_speed += jump_acceleration

            if player_y <= jump_target:
                ascending = False

        else:
            player_y += jump_speed
            jump_speed += gravity

        if player_y >= 400 and not on_ground:
            player_y = 400
            on_ground = True
            jump_speed = 0

def check_collision():
    global game_over

    player = pygame.Rect(player_x, player_y, player_width, player_height)
    obstacle = pygame.Rect(obstacle_x, obstacle_y, obstacle_width, obstacle_height)

    if player.colliderect(obstacle):
        game_over = True
        restart_game()

def update_score():
    global score, add_score, total_reward

    if obstacle_x < player_x and add_score:
        update_q_table(state, action, 50, next_state)
        total_reward += 1
        score += 50
        add_score = False

def display_episode_results(episode_number, total_reward):
    # Append the episode number and total reward to the lists
    episode_numbers.append(episode_number)
    total_rewards.append(total_reward)

    # Clear the previous plot and create a new one
    plt.figure()

    # Plot the data
    plt.plot(episode_numbers, total_rewards, label='Episode Results')

    # Fit a polynomial regression line
    trend = np.polyfit(episode_numbers, total_rewards, deg=1)
    trend_line = np.poly1d(trend)
    plt.plot(episode_numbers, trend_line(episode_numbers), color='red', label='Trend Line')

    plt.xlabel('Number of Episodes')
    plt.ylabel('Total Reward')
    plt.title('Episode Results')
    plt.legend()

    # Display the plot
    plt.show()

def restart_game():
    global on_ground, ascending, jump_speed, player_height, player_y, game_over, score, add_score, num_episodes, total_reward
    display_episode_results(num_episodes, total_reward)
    on_ground = True
    ascending = False
    jump_speed = 0
    player_height = full_height
    player_y = 400
    game_over = False
    score = 0
    add_score = True
    num_episodes += 1
    total_reward = 0
    spawn_obstacle()

def update_obstacle():
    global obstacle_x, add_score

    obstacle_x -= obstacle_speed

    if obstacle_x <= 0:
        add_score = True
        spawn_obstacle()

learning_status_font = pygame.font.Font(None, 24)  # Font for displaying the learning status

def display_statistics():
    statistics_text = [f"Episodes: {num_episodes}", f"Total Reward: {total_reward}", f"Distance: {get_distance()}"]
    # Add more statistics as needed

    x = 10
    y = 40
    line_height = 24

    for stat in statistics_text:
        stat_text = learning_status_font.render(stat, True, (0, 0, 0))
        screen.blit(stat_text, (x, y))
        y += line_height

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill("#f0efef")

    if not game_over:
        # Get the current state based on the player's position and whether they are on the ground or not
        state = int(on_ground)

        # Choose an action based on the current state
        action = choose_action(state, get_distance())

        update_obstacle()

        # Perform the chosen action
        perform_action(action)

        # Update the game state
        update_game_state()

        # Check for collision with obstacle
        check_collision()

        # Update the score
        update_score()

        player = pygame.Rect(player_x, player_y, player_width, player_height)
        obstacle = pygame.Rect(obstacle_x, obstacle_y, obstacle_width, obstacle_height)

        pygame.draw.rect(screen, "black", player)
        pygame.draw.rect(screen, "black", obstacle)

        # ... (rest of the code)

        # Update Q-table based on the current state, action, reward, and next state
        next_state = int(on_ground)
        reward = 1  # Placeholder for the reward
        update_q_table(state, action, reward, get_distance())

    display_statistics()

    pygame.display.flip()
    clock.tick(60)

pygame.quit()


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import requests
import sys
import time
import unittest

from flask import make_response

from base import TestPluginBaseClass, test_app

@test_app.route('/alive')
def alive_alive():
    res = make_response("
HELLO WORLD!
")
    return res

@test_app.route('/timeout')
def alive_timeout():
    res = make_response('
 TIMEOUT
')
    time.sleep(7)

class TestAlivePlugin(TestPluginBaseClass):
    __test__ = True
    @classmethod
    def setUpClass(cls):
        super(TestAlivePlugin, cls).setUpClass()
        cls.pname = 'AlivePlugin'

    def validate_alive(self, runner_resp, request_resp, expected=None, expectation=True):
        if expectation is True:
            self.assertEqual('FINISHED', runner_resp[2]['data']['state'])
            self.assertEqual('Site is reachable', runner_resp[1]['data']['Summary'])
            self.assertEqual(True, 'The server has responded with 200' in runner_resp[1]['data']['Description'])
            self.assertEqual(200, request_resp.status_code)
        elif expectation == '404':
            self.assertEqual('ABORTED', runner_resp[2]['data']['state'])
            self.assertEqual('Fatal', runner_resp[1]['data']['Severity'])
            self.assertEqual(404, request_resp.status_code)
            self.assertEqual('Site could not be reached', runner_resp[1]['data']['Summary'])
            self.assertEqual(True, 'The server has responded with 404' in runner_resp[1]['data']['Description'])
        elif expectation in (False, 'TIMEOUT'):
            self.assertEqual('ABORTED', runner_resp[2]['data']['state'])
            self.assertEqual('Fatal', runner_resp[1]['data']['Severity'])
            self.assertEqual('Site could not be reached', runner_resp[1]['data']['Summary'])
            self.assertEqual(expected, runner_resp[1]['data']['URLs'][0]['URL'])
            if expectation is False:
                # requests will throw exeception if not connectable
                self.assertEqual(requests.exceptions.ConnectionError, type(request_resp))

    def test_alive_200(self):
        api_name = '/alive'
        self.validate_plugin(api_name, self.validate_alive, expectation=True)
    def test_alive_404(self):
        api_name = '/not-alive'
        self.validate_plugin(api_name, self.validate_alive, expectation='404')
    def test_alive_timeout(self):
        api_name = '/timeout'
        self.validate_plugin(api_name, self.validate_alive, \
            expected='http://localhost:1234/timeout', expectation='TIMEOUT')


import sys
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
import nltk
nltk.download('stopwords')
nltk.download('wordnet') # download for lemmatization
nltk.download("averaged_perceptron_tagger")
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem.wordnet import WordNetLemmatizer
import re
import matplotlib.pyplot as plt
import pickle

from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer

from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.multioutput import MultiOutputClassifier
from sklearn.metrics import classification_report, make_scorer, f1_score

def load_data(database_filepath = 'sqlite:///data/disaster.db'):
    '''
    Reads data into dataframes from database.

    Returns:
        Dataframes containing dependent and independent variables, as well column names for independent variables.
    '''
    engine = create_engine(database_filepath)
    sql = "select * from model_data4"
    df = pd.read_sql(sql, con=engine)

    # We remove 'child_alone' column as it has no values.
    df = df.drop(['child_alone', 'offer', 'tools'], axis = 1)

    X = df[["message", "genre"]]
    Y = df.iloc[:,5:]

    column_names = list(Y.columns.values)

    return X, Y, column_names

def tokenize(text):
    '''
    Extracts tokens from string by means of cleaning, tokenising and lemmatising input.

    Input:
        Text string to be tokenised.

    Output:
        List of cleaned tokens.
    '''

    url_regex = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
    # detect all URL present in the messages
    detected_urls = re.findall(url_regex, text)
    # replace URL with "urlplaceholder"
    for url in detected_urls:
        text = text.replace(url, "urlplaceholder")

    tokens = word_tokenize(text)
    lemmatizer = WordNetLemmatizer()

    clean_tokens = []
    for tok in tokens:
        clean_tok = lemmatizer.lemmatize(tok).lower().strip()
        clean_tokens.append(clean_tok)

    return clean_tokens

# Engineer new features.
class StartingVerbExtractor(BaseEstimator, TransformerMixin):
    '''
    Function to extract starting verb from each sentence.

    Input:
        Base estimator and
        Transformer classes used within ML pipelines.
    Return:
        Transform method returns a dataframe containing transformed values.
    '''

    def starting_verb(self, text):
        sentence_list = nltk.sent_tokenize(text)
        for sentence in sentence_list:
            pos_tags = nltk.pos_tag(tokenize(sentence))
            first_word, first_tag = pos_tags[0]
            if first_tag in ['VB', 'VBP'] or first_word == 'RT':
                return True
        return False

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        X_tagged = pd.Series(X).apply(self.starting_verb)
        return pd.DataFrame(X_tagged)

def pipeline2(clf  = AdaBoostClassifier(random_state = 1)):
     pipeline = Pipeline([
         ('features', FeatureUnion([
            ('text_pipeline', Pipeline([
                ('vect', CountVectorizer(tokenizer=tokenize)),
                ('tfidf', TfidfTransformer())
            ])),
            ('starting_verb', StartingVerbExtractor())
         ])),
         ('clf', MultiOutputClassifier(clf))
     ])
     return pipeline

def build_model():
    '''
    Builds the model to be fitted to the data.

    Input:
        None.

    Output:
        Returns the model object which could be used to fit and predict on data.
    '''
    parameters_ada = {
    'clf__estimator__learning_rate': [0.1, 0.3],
    'clf__estimator__n_estimators': [100, 200]
    }

    pipeline_2 = pipeline2()
    pipeline_cv = GridSearchCV(estimator=pipeline_2, param_grid=parameters_ada, cv=3, verbose=10, n_jobs=-1)

    #pipeline_cv = GridSearchCV(estimator=pipeline_2, param_grid=parameters_ada, cv=3, verbose=10, n_jobs=-1)

    return pipeline_cv

def evaluate_model(model, X_test, Y_test, category_names):
    '''
    Function that predicts values based on model selected, and generates an accuracy report.

    Input:
        Model:      Model to be used for predictions.
        X_test:     Test data containing independent variables to be used for predictions.
        Y_test:     Dependent variable to be used for accuracy evaluation.

    Returns:
        Nothing.
    '''
    y_pred = model.predict(X_test)
    print(classification_report(Y_test, y_pred, target_names = category_names))

def save_model(model, model_filepath):
    '''
    Saves model to pickle file.

    Input:
        model:          Model to be saved.
        model_path:     Path to where model should be saved.

    Returns:
        Nothing.
    '''
    pickle.dump(model, open(model_filepath, 'wb'))

def main():
    if len(sys.argv) == 3:
        database_filepath, model_filepath = sys.argv[1:]
        print('Loading data...\n    DATABASE: {}'.format(database_filepath))
        X, Y, category_names = load_data("sqlite:///" + database_filepath)
        #X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)
        X_load = X["message"].values
        Y_load = Y.values
        X_train, X_test, Y_train, Y_test = train_test_split(X_load, Y_load, random_state = 1)
        print("Xtrain size: {}".format(X_train.shape[0]))
        print("Ytrain size: {}".format(Y_train.shape[0]))

        print('Building model...')
        model = build_model()

        print('Training model...')
        np.random.seed(333)

        model.fit(X_train, Y_train)

        print('Evaluating model...')
        evaluate_model(model, X_test, Y_test, category_names)

        print('Saving model...\n    MODEL: {}'.format(model_filepath))
        save_model(model, model_filepath)

        print('Trained model saved!')

    else:
        print('Please provide the filepath of the disaster messages database '\
              'as the first argument and the filepath of the pickle file to '\
              'save the model to as the second argument. \n\nExample: python '\
              'train_classifier.py ../data/DisasterResponse.db classifier.pkl')

if __name__ == '__main__':
    main()
#%%


import requests, json
from pprint import pprint
r = requests.get("http://api.open-notify.org/iss-now.json")
t = r.text
j = json.loads(t)

lat = j["iss_position"]["latitude"]
lon = j["iss_position"]["longitude"]

key = "  "

r_map = requests.get("https://api.opencagedata.com/geocode/v1/json?q={}+{}&key={}}".format(lat,lon,key))
plot = r_map.text
plot_p = json.loads(plot)

p_country = plot_p["results"][0]["formatted"]

print("The ISS is currently above: {} ".format(p_country))


import torch
import torch.nn as nn
from loss.ssim import SSIM

class SegmentationLosses(object):
    def __init__(self, weight=None,  batch_average=True, cuda=False):
        self.weight = weight
        self.batch_average = batch_average
        self.cuda = cuda

    def build_loss(self, mode='ce'):
        """Choices: ['ce' or 'focal']"""
        if mode == 'ce':
            return self.CrossEntropyLoss
        elif mode == 'focal':
            return self.FocalLoss
        elif mode == 'ssim':
            return self.SSIMLoss
        else:
            raise NotImplementedError

    def CrossEntropyLoss(self, logit, target):
        n, c, h, w = logit.size()
        criterion = nn.CrossEntropyLoss(weight=self.weight,
                                        reduction='mean')
        if self.cuda:
            criterion = criterion.cuda()

        loss = criterion(logit, target.long())

        if self.batch_average:
            loss /= n

        return loss

    def FocalLoss(self, logit, target, gamma=2, alpha=0.5): #0.5
        n, c, h, w = logit.size()
        criterion = nn.CrossEntropyLoss(weight=self.weight,
                                        reduction='mean')
        if self.cuda:
            criterion = criterion.cuda()

        logpt = -criterion(logit, target.long())
        pt = torch.exp(logpt)
        if alpha is not None:
            logpt *= alpha
        loss = -((1 - pt) ** gamma) * logpt

        if self.batch_average:
            loss /= n

        return loss

    def SSIMLoss(self, logit, target):
        n, c, h, w = logit.size()
        criterion = SSIM(window_size=11)
        if self.cuda:
            criterion = criterion.cuda()

        loss = criterion(logit, target.long())

        if self.batch_average:
            loss /= n

        return 1-loss


import requests
from lxml import html
import csv
import re
import click

def add_quote(a):
    return '"{0}"'.format(a)

def write_to_csv(filename, blog_list):

    headers = ['Title', 'Url', 'Author', 'Date', 'Category', 'Likes']
    with open(filename, 'w', encoding='utf-8', newline="") as f:
        writer = csv.DictWriter(f, headers)
        writer.writeheader()
        for row in blog_list:
            writer.writerow(row)

blog_list_info = []

# @click.command()
# @click.option('--url', default='https://blogs.sap.com/', help='Please provide sap blogs url only')
# @click.option('--category', default='Technical Articles', help='Please provide a blog post category')
# @click.option('--like', default='0', help='Please provide your choice')
def scrape(url):
    resp = requests.get(
        url=url,
        headers={
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"}
    )

    tree = html.fromstring(html=resp.text)

    blog_list = tree.xpath('//ul[contains(@class,"contentList")]/li')

    for item in blog_list:
        blog_category = item.xpath(
            './/div[contains(@class,"-user")]//child::span[contains(@class,"__category")]/text()')[0]

        blog_likes = item.xpath(
            './/div[contains(@class,"__right")]/child::div/div[position()=2]/div[contains(@class,"Number")]/text()')[0]

        if blog_category != 'Technical Articles':
            continue
        elif blog_category == 'Technical Articles' and int(blog_likes) < 10:
            continue

        blog_url = item.xpath(
            './/div[contains(@class, "__leftContent")]//child::div[contains(@class, "__title")]/a/@href')[0]
        blog_title = item.xpath(
            './/div/div[contains(@class,"_body")]/div[2]/a/text()')[0]
        blog_author = item.xpath(
            './/div[contains(@class,"-user")]//child::a/text()')[0]

        blog_date = item.xpath(
            './/div[contains(@class,"-user")]//child::span[contains(@class,"__date")]/text()')[1]

        blog_info = {
            'Title': blog_title.strip(),
            'Url': blog_url.strip(),
            'Author': blog_author.strip(),
            'Date': blog_date.strip(),
            'Category': blog_category.strip(),
            'Likes': blog_likes.strip()
        }
        blog_list_info.append(blog_info)

    older_post_url = item.xpath(
        '//ul[contains(@class,"pagination-list")]/li/a/span[contains(text(),"Older")]/ancestor::a/@href')[0]

    page_no = re.compile(r"\d+").findall(older_post_url)[0]

    if page_no != '50':
        scrape(url=older_post_url)

scrape(url="https://blogs.sap.com")
write_to_csv('blog.csv', blog_list_info)


# 5. Создать (программно) текстовый файл, записать в него программно набор чисел, разделенных пробелами.
# Программа должна подсчитывать сумму чисел в файле и выводить ее на экран.

import random

with open('file5.txt', 'w+') as file:
    random_numbers = [random.randint(1, 100) for i in range(10)]
    file.write(' '.join(map(str, random_numbers)))
    file.seek(0)
    sum_numbers = sum(list(map(int, file.read().split())))
    print(sum_numbers)


from PIL import Image
from matplotlib import image
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

weights_save_path = './checkpoint/mnist.ckpt'

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.load_weights(weights_save_path)

preNum = int(input('Input the number of test pictures: '))

for i in range(preNum):
    image_path = input('The path of the test picture: ')
    img = Image.open(image_path)
    image = plt.imread(image_path)
    plt.set_cmap('gray')
    plt.imshow(image)

    img = img.resize((28, 28), Image.ANTIALIAS)
    img_arr = np.array(img.convert('L'))
    img_arr = 255 - img_arr
    img_arr = img_arr/255.0
    x_predict = img_arr[tf.newaxis, ...]

    result = model.predict(x_predict)
    # 求取向量中值最大的一维的索引
    pred = tf.argmax(result, axis=1)

    print('\n')
    print(int(pred))
    plt.pause(5)
    plt.close()


from pathlib import Path
from random import choice
import shutil
from PySide6.QtCore import QUrl
from PySide6.QtGui import QDesktopServices
from PySide6.QtWidgets import QFileDialog, QMessageBox, QPushButton, QWidget
from pamet.desktop_app.config import pamet_data_folder_path
from pamet import desktop_app
from pamet.desktop_app import get_user_settings

from pamet.model.script_note import ScriptNote
from pamet.views.note.card.props_edit.widget import FixedTextEdit
from .name_seed_data import adjectives, fruits, animals

from .ui_props_widget import Ui_ScriptNotePropsWidget

FILE_SCHEMA = 'file://'

def random_name():
    return f'{choice(adjectives)}-{choice(fruits)}-{choice(animals)}'

class ScriptNotePropsWidget(QWidget):

    def __init__(self, parent):
        QWidget.__init__(self, parent)

        self.ui = Ui_ScriptNotePropsWidget()
        self.ui.setupUi(self)

        self.edit_widget = parent

        # Locals
        self._unsaved_script_changes = False
        config = get_user_settings()
        self._scripts_folder = Path(config.scripts_folder)
        self._scripts_folder.mkdir(parents=True, exist_ok=True)

        # Add the note text edit
        self.text_edit = FixedTextEdit(self)
        self.ui.noteTextVerticalLayout.addWidget(self.text_edit)
        self.text_edit.textChanged.connect(self._handle_text_change)

        # Generate the create script buttons
        self._templates_folder = Path(config.script_templates_folder)
        for template in self._templates_folder.iterdir():
            button = QPushButton(template.stem)
            content = template.read_text()
            extension = template.suffix

            button.clicked.connect(lambda cnt=content, ext=extension: self.
                                   create_script_file(cnt, ext))
            self.ui.createScriptWidget.layout().addWidget(button)

        # Setup the UI according to the note props (is a script path set up)
        script_path = self.edit_widget.edited_note.script_path
        self.ui.scriptPathLineEdit.setText(str(script_path))
        if script_path is None:
            script_path = ''
        self.handle_script_path_change(str(script_path))

        self.ui.argumentsLineEdit.setText(self.edited_note.command_args)
        self.text_edit.setText(self.edited_note.text)

        # Connect the UI with the handlers
        self.ui.scriptPathLineEdit.textChanged.connect(
            self.handle_script_path_change)
        self.ui.scriptNameLineEdit.textChanged.connect(
            self.handle_script_name_change)
        self.ui.renamePushButton.clicked.connect(
            self.handle_rename_button_click)
        self.ui.openInEditorPushButton.clicked.connect(
            self._handle_open_in_editor_button_click)
        self.ui.openContainingFolderPushButton.clicked.connect(
            self._handle_open_containing_folder_button_click)
        self.ui.deletePushButton.clicked.connect(
            self._handle_delete_button_click)
        self.ui.chooseFilePushButton.clicked.connect(self._open_file_chooser)
        self.ui.argumentsLineEdit.textChanged.connect(
            self._handle_args_changed)
        self.ui.setDefaultScriptsFolder.clicked.connect(
            self._set_default_folder)
        self.ui.configureTemplatesButton.clicked.connect(
            self._configure_templates)
        self.ui.runPushButton.clicked.connect(
            lambda: desktop_app.script_runner.run(self.edited_note))
        self.ui.runInTerminalCheckbox.toggled.connect(
            self._handle_run_in_terminal_toggled)

    @property
    def edited_note(self) -> ScriptNote:
        return self.edit_widget.edited_note

    def create_script_file(self, content: str, extension: str):
        script_path = self._scripts_folder
        while script_path.exists():
            script_path = self._scripts_folder / random_name()
            script_path = script_path.with_suffix(extension)

        try:
            script_path.write_text(content)
            # script_path.chmod(script_path.stat().st_mode | stat.S_IEXEC)
        except Exception as e:
            QMessageBox(self, 'Could not write to file',
                        f'Could not create the script file. Exception: {e}')
            return

        self.ui.scriptPathLineEdit.setText(str(script_path))

    def _save_script_changes(self):
        script_path: Path = self.edited_note.script_path
        if not script_path:
            raise Exception

        script_path.write_text(self.edited_note.script_te)

    def _handle_text_change(self):
        self.edited_note.text = self.text_edit.toPlainText()

    def handle_script_path_change(self, new_path: str):
        old_path = self.edited_note.script_path

        if new_path.startswith(FILE_SCHEMA):
            new_path = new_path[len(FILE_SCHEMA):]
            self.ui.scriptPathLineEdit.setText(new_path)
            return

        new_path: Path = Path(new_path)
        self.edited_note.script_path = new_path

        if new_path.exists() and new_path.is_file():
            self.ui.pathErrorLabel.setText('Exists')
            self.ui.deletePushButton.setEnabled(True)
            self.ui.scriptNameLineEdit.setEnabled(True)
            self.ui.scriptNameLineEdit.setText(new_path.name)
            self.ui.openInEditorPushButton.setEnabled(True)
        else:
            if shutil.which(new_path):
                self.ui.pathErrorLabel.setText('Executable')
            else:
                self.ui.pathErrorLabel.setText('Missing')
            self.ui.deletePushButton.setEnabled(False)
            self.ui.scriptNameLineEdit.setEnabled(True)
            self.ui.scriptNameLineEdit.setText('')
            self.ui.openInEditorPushButton.setEnabled(False)

        note_text = self.edited_note.text
        # Keep the text matching the file name except if the user hasn't
        # made an edit (and they differ).
        if not note_text or (old_path and note_text == old_path.name):
            self.text_edit.setText(new_path.name)

    def handle_script_name_change(self, new_name: str):
        if new_name and new_name != self.edited_note.script_path.name:
            self.ui.renamePushButton.setEnabled(True)
        else:
            self.ui.renamePushButton.setEnabled(False)

    def handle_rename_button_click(self):
        script_path: Path = self.edited_note.script_path
        if not (script_path.exists() and script_path.is_file()):
            raise Exception(f'Cannot rename {script_path}. File missing.')

        new_name = self.ui.scriptNameLineEdit.text()
        new_path = script_path.parent / new_name
        try:
            # Make it possible to create a subdir on the spot
            new_path = new_path.resolve()
            new_path.parent.mkdir(parents=True, exist_ok=True)

            script_path.rename(new_path)
        except Exception as e:
            QMessageBox.warning(
                self, 'Error during rename',
                ('Could not rename the file {script_path} to {new_path}.'
                 f' Error: {e}'))
            return

        self.ui.scriptPathLineEdit.setText(str(new_path))

    def _handle_open_in_editor_button_click(self):
        QDesktopServices.openUrl(QUrl.fromLocalFile(str(self.edited_note.script_path)))

    def _handle_open_containing_folder_button_click(self):
        QDesktopServices.openUrl(QUrl.fromLocalFile(str(
            self.edited_note.script_path.parent)))

    def _handle_delete_button_click(self):
        path = self.edited_note.script_path
        try:
            path.unlink()
        except Exception as e:
            QMessageBox.warning(
                self, 'Error deleting file',
                f'Could not delete file {path}. Exception: {e}')
        self.ui.scriptPathLineEdit.setText('')

    def _open_file_chooser(self):
        path, _ = QFileDialog.getOpenFileName(self, 'Choose a script file',
                                              str(self._scripts_folder))
        if not path:
            return

        self.ui.scriptPathLineEdit.setText(path)

    def _handle_args_changed(self, new_args_text: str):
        self.edited_note.command_args = new_args_text

    def _set_default_folder(self):
        instructions_text = '''
        The config.json will be opened. You can change the scripts folder there.

        If you don't know what you're doing - it would be best to not touch it.
        '''
        QMessageBox.information(self, 'How to set the folder',
                                instructions_text)
        QDesktopServices.openUrl(QUrl.fromLocalFile(str(pamet_data_folder_path)))
        self.edit_widget._handle_esc_shortcut()

    def _configure_templates(self):
        instructions_text = '''
        You can add templates to the templates folder, which will be opened
        after you close this prompt.
        To restore the original templates - just delete the files and restart
        the app.
        '''
        QMessageBox.information(self, 'How to set the folder',
                                instructions_text)
        QDesktopServices.openUrl(QUrl.fromLocalFile(str(self._templates_folder)))
        self.edit_widget._handle_esc_shortcut()

    def _handle_run_in_terminal_toggled(self, checked: bool):
        self.edited_note.run_in_terminal = checked


from universal_hash import *

class PerfectHashTableKeyError(KeyError):
    pass

class SimpleHashTable:
    def __init__(self, size):
        self.get_second_hash = UnivHash(size)
        self.__size = size
        self.data = [None] * size

    def __setitem__(self, key, value):
        second_hash = self.get_second_hash(key)
        assert self.data[second_hash][0] == key
        self.data[second_hash] = (key, value)

    def __getitem__(self, key):
        second_hash = self.get_second_hash(key)
        assert self.data[second_hash][0] == key
        return self.data[second_hash][1]

    def __delitem__(self, key):
        second_hash = self.get_second_hash(key)
        assert self.data[second_hash][0] == key
        self.data[second_hash] = (key, None)

class PerfectHashTable:
    def __init__(self, keys):
        self.__keys_count = len(keys)
        self.__data = [[] for _ in range(self.__keys_count)]
        self.__size = 0
        self.__keys = keys
        self.get_first_hash = UnivHash(self.__keys_count)
        self.build()

    def __setitem__(self, key, value):
        self.__size += 1
        first_hash = self.get_first_hash(key)
        if type(self.__data[first_hash]) is SimpleHashTable:
            second_hash = self.__data[first_hash].get_second_hash(key)
            assert self.__data[first_hash].data[second_hash][0] == key
            self.__data[first_hash].data[second_hash] = (key, value)
        else:
            assert self.__data[first_hash][0] == key
            self.__data[first_hash] = (key, value)

    def __getitem__(self, key):
        first_hash = self.get_first_hash(key)
        data = self.__data[first_hash]
        if data is None:
            raise PerfectHashTableKeyError
        if type(data) is SimpleHashTable:
            return data[key]
        else:
            return data[1]

    def __delitem__(self, key):
        first_hash = self.get_first_hash(key)
        data = self.__data[first_hash]
        if data is None:
            raise PerfectHashTableKeyError
        if type(data) is SimpleHashTable:
            del self.__data[first_hash][key]
        else:
            self.__data[first_hash] = (key, None)

    def build(self):
        for key in self.__keys:
            first_hash = self.get_first_hash(key)
            self.__data[first_hash].append(key)
        for i in range(self.__keys_count):
            size = len(self.__data[i])
            if size == 0:
                self.__data[i] = None
            elif size == 1:
                self.__data[i] = (self.__data[i][0], None)
            else:
                keys = self.__data[i]
                size = size * size
                while True:
                    flag = True
                    self.__data[i] = SimpleHashTable(size)
                    for key in keys:
                        second_hash = self.__data[i].get_second_hash(key)
                        if self.__data[i].data[second_hash] is None:
                            self.__data[i].data[second_hash] = (key, None)
                        else:
                            flag = False
                            break
                    if flag:
                        break

if __name__ == '__main__':
    pht = PerfectHashTable(range(0, 100))
    for i in range(31):
        pht[i] = i + 1
    for i in range(10):
        del pht[i]
    for i in range(30):
        print(pht[i])
    print(pht[101])


import os
import unittest
import json
import random

from flask_sqlalchemy import SQLAlchemy

from flaskr import create_app
from models import setup_db, Question, Category

class TriviaTestCase(unittest.TestCase):
    """This class represents the trivia test case"""

    def setUp(self):
        """Define test variables and initialize app."""
        self.app = create_app()
        self.client = self.app.test_client
        self.database_name = "trivia_test"
        self.database_path = "postgresql://{}:{}@{}/{}".\
            format('postgres', '0000', 'localhost:5432', self.database_name)
        setup_db(self.app, self.database_path)

        # binds the app to the current context
        with self.app.app_context():
            self.db = SQLAlchemy()
            self.db.init_app(self.app)
            # create all tables
            self.db.create_all()

        self.new_question = {
            "question": "What actor did author Anne Rice first denounce, then praise in the role of her beloved Lestat?",
            "answer": "Tom Cruise",
            "difficulty": 4,
            "category": 5}

    def tearDown(self):
        """Executed after reach test"""
        pass

    """
    TODO
    Write at least one test for each test for successful operation and for expected errors.
    """

    def test_get_pagenated_questions(self):
        """TEST PAENATE QUESTION"""
        res = self.client().get('/questions')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(len(data['questions']), 10)
        self.assertTrue(data['total_questions'])

    def test_404_sent_requesting_invalid_page(self):
        res = self.client().get('/questions?page=1000')
        # print('response:', res)
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 404)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], 'content not found')

    def test_delete_question(self):
        """TEST DELETE QUESTION"""
        res = self.client().delete('/questions/22')
        data = json.loads(res.data)
        qestion = Question.query.filter_by(id=22).one_or_none()

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertEqual(data['deleted'], 22)
        self.assertTrue(data['questions'])
        self.assertTrue(data['total_questions'])
        self.assertEqual(qestion, None)

    def test_422_delete_invalid_question_id(self):
        """TEST FAILUIR DELETE QUESTION"""
        res = self.client().delete('/questions/500')
        # print('response:', res)
        data = json.loads(res.data)
        # print('data:', data)

        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], 'unprocessable')

    def test_create_new_question(self):
        """TEST CREATE NEW QUESTION"""
        res = self.client().post('/questions', json=self.new_question)
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertTrue(data['created'])
        self.assertTrue(data['total_questions'])

    def test_405_create_new_question_not_allowed(self):
        res = self.client().post('/questions/30', json=self.new_question)
        # print('response: ',res)
        # data = json.loads(res.data)
        # print('data: ', data)

        self.assertEqual(res.status_code, 405)
        # self.assertEqual(data['success'], False)
        # self.assertEqual(data['message'], "method not allowed")

    def test_search_questions(self):
        res = self.client().post('/questions', json={"searchTerm": "title"})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertTrue(data['total_questions'])
        self.assertEqual(len(data['questions']), 2)

    def test_invalid_search_questions(self):
        res = self.client().post('/questions', json={"searchTerm": "sunlight"})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['total_questions'], 0)
        self.assertEqual(len(data['questions']), 0)

    def test_get_questions_by_category(self):
        res = self.client().get('/categories/5/questions')
        data = json.loads(res.data)
        current_category = Category.query.filter_by(id=5).first_or_404()
        questions = Question.query.filter_by(category=5).all()

        self.assertEqual(res.status_code, 200)
        self.assertTrue(data['current_category'])
        self.assertTrue(data['questions'])
        self.assertTrue(len(data['questions']))

    def test_404_get_questions_beyond_category_ids(self):
        res = self.client().get('/categories/15/questions')
        # print('response:',res)
        data = json.loads(res.data)
        current_category = Category.query.filter_by(id=15).one_or_none()

        self.assertEqual(res.status_code, 404)
        self.assertEqual(current_category, None)
        self.assertEqual(data['message'], "content not found")
        self.assertEqual(data['success'], False)

    def test_play(self):
        res = self.client().post(
            '/quizzes',
            json={
                "previous_questions": [],
                "quiz_category": {
                    "id": 5,
                    "type": "Entertainment"}})

        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertTrue(data['question'])

# Make the tests conveniently executable
if __name__ == "__main__":
    unittest.main()


import logging

from django.core.mail import send_mail
from django.conf import settings
from django.shortcuts import render, redirect
from django.template.loader import render_to_string
from django.http import HttpResponse
from .forms import ContactForm

from utils.logging import setup_logger

logger, log_level = setup_logger(__name__)

def main(request):
    if log_level <= logging.INFO:
        logger.info("START")

    if log_level <= logging.INFO:
        logger.info("END")
    return render(request, 'index.html')

def about(request):
    return render(request, 'about.html')

def contact(request):
    if log_level <= logging.INFO:
        logger.info("START")

    if log_level <= logging.DEBUG:
        logger.debug(f"request.method={request.method}")

    if request.method == 'POST':
        form = ContactForm(request.POST)

        if form.is_valid():
            name = form.cleaned_data['name']
            subject = form.cleaned_data['subject']
            email = form.cleaned_data['email']
            phone = form.cleaned_data['phone']
            message = form.cleaned_data['message']

            html = render_to_string('emails/contact_form.html', {
                'name': name,
                'subject': subject,
                'email': email,
                'phone': phone,
                'message': message,
            })

            send_mail(subject, "", settings.EMAIL_HOST_USER,
                      ["steve213nsw@gmail.com"], html_message=html)

            return redirect('/contact')
    else:
        form = ContactForm()

    if log_level <= logging.INFO:
        logger.info("END")

    return render(request, 'contact.html', {'form': form})


import numpy as np
import pandas as pd
import random
import shutil
import subprocess
from subprocess import PIPE

# プログラムの根幹部分
def main():
    # お掃除
    subprocess.run(['rm velocity* final.csv'], shell=True)
    # 速度構造設定ファイル
    input = "hogehoge.csv"
    # 子孫数
    gene = 200
    # 世代数
    generation = 10
    # 選択・交叉・突然変異の確率[%]
    rate_sentaku = 5
    rate_kousa = 65
    rate_henni = 30
    # 拡大係数
    rate_min = 0.8
    rate_max = 1.5
    # 選択する際の抽出数[1/]
    proportion = 10
    # 層の数を求める
    layer_number = layer_count(input)
    # 層の範囲の読み込み
    thick_min = first_gene00(input,layer_number)
    thick_max = first_gene01(input,layer_number)
    vp_min = first_gene02(input,layer_number)
    vp_max = first_gene03(input,layer_number)
    vs_min = first_gene04(input,layer_number)
    vs_max = first_gene05(input,layer_number)
    # 初期値作成
    create_gene(layer_number, thick_min, thick_max, vp_min, vp_max, vs_min, vs_max, gene)
    # 1回目の適応度計算(震源決定)
    subprocess.run(["bash GA.sh"], shell=True)
    # 結果を読み込む
    infile = "out_HYPST_2020m2.dat"
    inversion_result = np.loadtxt(infile)
    gene2 = inversion_result.shape[0]
    # NaNは削除
    inversion_result = inversion_result[~np.isnan(inversion_result).any(axis=1), :]
    # 結果を書き出す
    minrow = np.argmin(inversion_result[:,layer_number*3])
    temp = np.delete(inversion_result, minrow, 0)
    minrow2 = np.argmin(temp[:,layer_number*3]) + 1
    final_result = inversion_result[minrow]
    final_result.reshape(1, layer_number*3 + 1)
    np.savetxt("final.csv", final_result, fmt='%5.5f', delimiter=",")
    # ここから世代分回す
    for m in range (0, generation) :
        print(str(m + 2) + "世代目開始…。")
        j = 0
        while True :
            # 選択・交叉・突然変異の判定
            next = judge(rate_sentaku, rate_kousa, rate_henni)
            if next == "sentaku" :
                append = selection(gene2, inversion_result, proportion, layer_number)
            elif next == "kousa" :
                append = kousa(gene2, inversion_result, layer_number, minrow, minrow2)
            elif next == "henni" :
                append = henni(gene2, inversion_result, layer_number, rate_min, rate_max)
            if j == 0 :
                next_gene = inversion_result[minrow]
                # for check2 in range(0, layer_number) :
                #     vp = layer_number+check2
                #     vs = layer_number+layer_number+check2
                #     vp_s = append[vp]
                #     vs_s = append[vs]
                #     vpvs = vp_s / vs_s
                #     if vpvs < 1.5 and vpvs > 1.9 :
                #         guard = "ng"
                #         break
                #     else :
                #         guard = "ok"
                #         pass
                # if guard == "ng" :
                #     continue
                # else :
                #     pass
                #     next_gene = append
            elif j == 1 :
                next_gene = np.vstack([next_gene, inversion_result[minrow2]])
            else :
                vp_s = []
                vs_s = []
                for check2 in range(0, layer_number) :
                    vp = layer_number + check2
                    vs = layer_number + layer_number + check2
                    vp_s = append[vp]
                    vs_s = append[vs]
                    vpvs = vp_s / vs_s
                    if vpvs < 1.5 and vpvs > 1.9 :
                        check = "ng"
                        break
                        # append = inversion_result[minrow]
                        # next_gene = np.vstack([next_gene, append])
                        # next_gene = np.unique(next_gene, axis=0)
                    else :
                        # next_gene = np.vstack([next_gene, append])
                        check = "ok"
                if check == "ok" :
                    next_gene = np.vstack([next_gene, append])
                    # next_gene = np.unique(next_gene, axis=0)
                else :
                    pass
            # ループ判定
            if next_gene.shape[0] > gene + 2 :
                break
            else :
                pass
            j += 1
        # 結果列はもういらないので削除
        next_gene = np.delete(next_gene, layer_number*3, 1)
        np.savetxt("test2.dat", next_gene, fmt="%5.3f")
        # 列を並び替える
        for k in range(0, len(next_gene)) :
            for l in range(0, layer_number) :
                layer_number0 = l
                layer_number1 = l + layer_number
                layer_number2 = layer_number1 + layer_number
                if l == 0 :
                    temp_gene = next_gene[k]
                    new_gene = temp_gene[[layer_number0, layer_number1, layer_number2]]
                else :
                    temp_gene = next_gene[k]
                    temp_gene = temp_gene[[layer_number0, layer_number1, layer_number2]]
                    new_gene = np.vstack([new_gene, temp_gene])
            np.savetxt("velocity" + str(k), new_gene, fmt='%5.3f')
            with open("velocity" + str(k), mode='a') as f:
                f.write(" ")
        # 適応度計算
        subprocess.run(["bash GA.sh"], shell=True)
        inversion_result = np.loadtxt("out_HYPST_2020m2.dat")
        # NaNは削除
        inversion_result = inversion_result[~np.isnan(inversion_result).any(axis=1), :]
        # 結果を書き出す
        minrow = np.argmin(inversion_result[:,layer_number*3])
        temp = np.delete(inversion_result, minrow, 0)
        minrow2 = np.argmin(temp[:,layer_number*3]) + 1
        final_result = np.vstack([final_result,inversion_result[minrow]])
        np.savetxt("final.csv", final_result, fmt='%5.3f', delimiter=",")
        print(str(m + 2 ) + "世代目終了…。\n 最小は" + str(minrow) + "番目。")
    np.savetxt("final.csv", final_result, fmt='%5.5f', delimiter=",")
    return

# 層の数を求める
def layer_count(input) :
    df = pd.read_csv(input, header=None)
    return len(df)

# ----------------- ここから ------------------------
# ファイルから層厚・P波速度・S波速度の範囲を読み込む。
# ファイルはカンマ区切り。(hogehoge.csvを参照のこと)
def first_gene00(input, layer_number) :
    layer_thickmin = []
    df = np.loadtxt(input, delimiter=",")
    for i in range(0, layer_number):
        layer_thickmin.append(df[i, 1])
    return layer_thickmin

def first_gene01(input, layer_number) :
    layer_thickmax = []
    df = np.loadtxt(input, delimiter=",")
    for i in range(0, layer_number):
        layer_thickmax.append(df[i, 2])
    return layer_thickmax

def first_gene02(input, layer_number) :
    layer_velpmin = []
    df = np.loadtxt(input, delimiter=",")
    for i in range(0, layer_number):
        layer_velpmin.append(df[i, 3])
    return layer_velpmin

def first_gene03(input, layer_number) :
    layer_velpmax = []
    df = np.loadtxt(input, delimiter=",")
    for i in range(0, layer_number):
        layer_velpmax.append(df[i, 4])
    return layer_velpmax

def first_gene04(input, layer_number) :
    layer_velsmin = []
    df = np.loadtxt(input, delimiter=",")
    for i in range(0, layer_number):
        layer_velsmin.append(df[i, 5])
    return layer_velsmin

def first_gene05(input, layer_number) :
    layer_velsmax = []
    df = np.loadtxt(input, delimiter=",")
    for i in range(0, layer_number):
        layer_velsmax.append(df[i, 6])
    return layer_velsmax
# ----------------- ここまで ------------------------

# 初期遺伝子作成
def create_gene(layer_number, thick_min, thick_max, vp_min, vp_max, vs_min, vs_max, gene_number):
    i = 0
    while True :
        depth = []
        vp = []
        vs = []
        for j in range(0, layer_number):
            dep = round(random.uniform(thick_min[j], thick_max[j]), 2)
            while True :
                vpvp = round(random.uniform(vp_min[j], vp_max[j]), 2)
                vsvs = round(random.uniform(vs_min[j], vs_max[j]), 2)
                vpvs = vpvp /vsvs
                if vpvs < 1.83 and vpvs > 1.5 :
                    break
                else :
                    pass
            depth.append(dep)
            vp.append(vpvp)
            vs.append(vsvs)
        depth = np.array(depth)
        vp = np.array(vp)
        vs = np.array(vs)
        # print(depth)
        origin = np.c_[depth, vp, vs]
        np.savetxt("velocity" + str(i), origin, fmt='%5.3f')
        with open("velocity" + str(i), mode='a') as f:
            f.write(" ")
        origin2 = np.hstack([depth,vp,vs])
        # print(origin2)
        if i == 0 :
            log = origin2
        else :
            loglog = origin2
            new_log = np.vstack([log,loglog])
            # 重複行は削除
            log = np.unique(new_log, axis=0)
        # ループ判定
        if log.shape[0] > gene_number :
            break
        else :
            pass
        i +=1
    np.savetxt("log.dat", log, fmt="%5.3f")
    return

# 速度構造ログと誤差を合体させて結果ファイルを作成する
def result(infile, infile2, gene) :
    sig = np.loadtxt(infile)
    sig2 = np.loadtxt(infile2)
    sig = np.hstack([sig,sig2.reshape(gene,1)])
    sig = sig[~np.isnan(sig).any(axis=1), :]
    return sig

# 選択パート
# 世代数の1/Nを抽出して最も誤差が小さい物を探す。(トーナメント選択)
def selection(gene, sig, proportion, layer_count) :
    for i in range(0, int(round(gene/proportion,0))) :
        if i == 0 :
            a = random.randint(0, len(sig) - 1)
            select_matrix = sig[a]
            sig = np.delete(sig, a, 0)
        else :
            a = random.randint(0, len(sig) - 1)
            select_matrix = np.vstack([select_matrix,sig[a]])
            sig = np.delete(sig, a, 0)
    minrow = np.argmin(select_matrix[:,layer_count*3])
    generated = select_matrix[minrow]
    np.savetxt("next_select.csv", generated, fmt="%5.3f")
    return generated

# 交叉パート
def kousa(gene, sig, layer_count, minrow, minrow2):
    # a = random.randint(0,1)
    a = 0
    if a == 0 :
        # 新たに生成する
        temp1 = sig[minrow]
        temp2 = sig[minrow2]
        new_ab = temp1
        for i in range(0,len(temp1)) :
            new_ab[i] = round(random.uniform(temp1[i], temp2[i]), 2)
    else :
        # ランダムに2つの列を選択する
        a = 1
        b = 1
        while a == b :
            a = random.randint(0, len(sig) - 1)
            b = random.randint(0, len(sig) - 1)
        change_start = 1
        change_goal =  1
        # 変更する始点・終点を決める
        while change_start > change_goal:
            change_start = random.randint(0,layer_count*3)
            change_goal = random.randint(0,layer_count*3)
        # 1点交叉
        c = sig[a].reshape((1,layer_count*3 + 1))
        d = sig[b].reshape((1,layer_count*3 + 1))
        new_a = c
        new_b = d
        new_a[change_start:change_goal] = d[change_start:change_goal]
        new_b[change_start:change_goal] = c[change_start:change_goal]
        judge = random.randint(0,1)
        if judge == 0 :
            new_ab = new_a
        else :
            new_ab = new_b
        np.savetxt("next_kousa.csv", new_ab, fmt="%5.3f")
    return new_ab

# 突然変異パート
def henni(gene, sig, layer_count, rate_min, rate_max):
    # 拡大係数を積算したものに交換する。
    # ランダムに1つの列を選択する。
    a = random.randint(0, len(sig) - 1)
    # 任意の地震波速度構造に変異させる。
    new_henni = sig[a]
    change_henni = 0
    # 1層目をいじってはいけない
    # while change_henni == 0 or change_henni == 6 or change_henni == 12 or change_henni == 1 or change_henni == 7 or change_henni == 13 or change_henni == 5 or change_henni == 11 or change_henni == 17 :
    while change_henni == 0 or change_henni == 6 or change_henni == 12 or change_henni == 5 or change_henni == 11 or change_henni == 17 :
        change_henni = random.randint(0,layer_count*3)
    rate = random.uniform(rate_min, rate_max)
    new_henni[change_henni] = new_henni[change_henni] * round(rate, 2)
    np.savetxt("next_henni.csv", new_henni, fmt="%5.3f")
    return np.round(new_henni, decimals=2)

# 選択・交叉・突然変異を決定する
# 確率は「突然変異」 < 「交叉」「選択」 の順にする。
def judge(rate_sentaku, rate_kousa, rate_henni):
    a = random.randint(0,100)
    if a < rate_henni :
        choice = "henni"
    elif a < rate_henni + rate_kousa :
        choice = "kousa"
    else :
        choice = "sentaku"
    return choice

if __name__ == "__main__":
    main()


import sys
sys.path.append('../')
import database
import pprint
from queryconstructor import QueryConstructor
from plotconstructor import *

filename_prefix = "ping_over_signal_att_"

filename=filename_prefix

qc = QueryConstructor()
qc.setGroupOrderSelectBy("network","cast(signalstrength as int)")
qc.addSelectMedian("ping","avg")
qc.addSelectPercentile("ping","avg",25)
qc.addSelectPercentile("ping","avg",75)
qc.addSelectField("count(*)")
qc.addWhereEqualsString("network","cellid","62095")
#qc.addWhereEqualsString("network","cellid","62095")
qc.addWhereEqualsString("ping","dstip","www.google.com")
qc.applyMobileClauses()
qc.applyLatencyClauses("www.google.com")

result = database.query(qc.toString())

pprint.pprint(result)

plot = SimpleBarPlot()
plot.addMapping(MappingWithBounds(result,0,1,2,3,"AT&T"))
plot.setYLabel("ms")
plot.setXLabel("raw signalstrength")
#plot.setTitle("RTT latency over signal, for AT&T users in US ("+str(networktype)+")")
plot.setLegend("ms")

plot.construct()

#plot.save(filename)
plot.show()


'''
Author: littleherozzzx zhou.xin2022.code@outlook.com
Date: 2023-01-12 13:27:24
LastEditTime: 2023-02-01 11:19:38
Software: VSCode
'''
import os
import requests
import sys
import os
from urllib.parse import unquote
from prettytable import PrettyTable
from time import sleep
import datetime as dt
import hashlib
import base64
import json
sys.path.append(os.getcwd())

from config.config import ConfigParser

class Killer:
    def __init__(self):
        pass

    def init(self, configFile):
        self.loadConfig(configFile)
        self.__initSession()

    def loadConfig(self, configFile):
        self.configParser = ConfigParser(configFile)
        if not os.path.exists(configFile):
            self.configParser.createConfig()
        self.cfg = self.configParser.parseConfig()
        self.sessionCfg = self.cfg['session']
        self.urls = self.cfg['urls']
        self.seat_list = self.cfg["seat_list"]
        self.data = self.cfg["data"]
        self.settings = self.cfg["settings"]
        self.userInfo = self.cfg["user_info"]
        self.plans = self.cfg["plans"]
        self.plans = self.cfg["plans"]

    def saveConfig(self):
        self.cfg['seat_list'] = self.seat_list
        self.cfg['user_info'] = self.userInfo
        self.cfg['plans'] = self.plans
        self.cfg['plans'] = self.plans
        self.configParser.saveConfig(self.cfg)

    def __initSession(self):
        import urllib3
        urllib3.disable_warnings()
        self.session = requests.Session()
        self.session.headers.clear()
        self.session.headers = self.sessionCfg['headers']
        self.session.trust_env = self.sessionCfg['trust_env']
        self.session.verify = self.sessionCfg['verify']
        self.session.params = self.sessionCfg['params']

    def login(self):
        url = self.urls["login"]
        loginRes = self.session.post(url=url, data=self.userInfo).json()
        if loginRes["CODE"] == "ok":
            self.uid = loginRes["DATA"]["uid"]
            self.name = loginRes["DATA"]["user_info"]["name"]
        return loginRes["CODE"] == "ok"

    def __queryRooms(self):
        # 查询所有可用的房间类型，返回一个字典，键为房间名，值为房间对应的请求参数
        url = self.urls["query_rooms"]
        queryRoomsRes = self.session.get(url=url).json()
        rawRooms = queryRoomsRes["content"]["children"][1]["defaultItems"]
        rooms = {x["name"]: unquote(x["link"]["url"]).split('?')[1] for x in rawRooms}
        for room in rooms.keys():
            rooms[room] = self.session.get(url=self.urls["query_seats"] + "?" + rooms[room]).json()["data"]
            sleep(2)
        return rooms

    def __querySeats(self):
        #  查询每个房间的作为信息
        time = dt.datetime.now()
        if time.hour >= 22:
            time = time + dt.timedelta(days=1)
            time = time.replace(hour=11, minute=0, second=0)
        for room in self.rooms.keys():
            data = {
                "beginTime": time.timestamp(),
                "duration": 3600,
                "num": 1,
                "space_category[category_id]": self.rooms[room]["space_category"]["category_id"],
                "space_category[content_id]": self.rooms[room]["space_category"]["content_id"],
            }
            resp = self.session.post(url=self.urls["query_seats"], data=data).json()
            self.rooms[room]["floors"] = {x["roomName"]:x for x in resp["allContent"]["children"][2]["children"]["children"]}
            for floor in self.rooms[room]["floors"].keys():
                self.rooms[room]["floors"][floor]["seats"] = self.rooms[room]["floors"][floor]["seatMap"]["POIs"]
            sleep(2)
    def updateRooms(self):
        self.rooms = self.__queryRooms()
        self.__querySeats()
        return list(self.rooms.keys())

    def getFloorNamesByRoom(self, roomName):
        floors = self.rooms[roomName]["floors"]
        return list(floors.keys())

    def getSeatsByRoomAndFloor(self, roomName, floorName):
        seats = self.rooms[roomName]["floors"][floorName]["seats"]
        return seats

    def addPlan(self, roomName, beginTime, duration, seatsInfo, seatBookers):
        self.plans.append({
            "roomName": roomName,
            "beginTime": beginTime,
            "duration": duration,
            "seatsInfo": list(seatsInfo),
            "seatBookers": list(seatBookers),
        })

    def plan2data(self, plan):
        data = {}
        data["beginTime"] = int(plan["beginTime"].timestamp())
        data["duration"] = plan["duration"]*3600
        for i in range(len(plan["seatsInfo"])):
            data[f"seats[{i}]"] = plan["seatsInfo"][i]["seatId"]
        data["is_recommend"] = 0
        data["api_time"] = int(dt.datetime.now().timestamp())
        for i in range(len(plan["seatsInfo"])):
            data[f"seatBookers[{i}]"] = plan["seatBookers"][i]
        apiToken = f"post&/Seat/Index/bookSeats?LAB_JSON=1&api_time{data['api_time']}&beginTime{data['beginTime']}&duration{data['duration']}&is_recommend0&seatBookers[0]{data['seatBookers[0]']}&seats[0]{data['seats[0]']}"
        md5 = hashlib.md5(apiToken.encode("utf-8")).hexdigest()
        apiToken = base64.b64encode(md5.encode("utf-8"))
        return data, apiToken

    def showPlan(self):
        print(f"当前共有{len(self.plans)}个预约方案")
        if len(self.plans) == 0:
            return
        table = PrettyTable(["序号", "房间名", "楼层名", "座位号", "开始时间", "持续时间", "预约人"])
        for i, plan in enumerate(self.plans):
            seat = plan["seatsInfo"][0]
            table.add_row([f"{i+1}", seat['roomName'], seat['floorName'], ",".join([x["seatNum"] for x in plan["seatsInfo"]]), plan['beginTime'], str(plan['duration'])+"小时", ",".join([x["bookerName"] for x in plan["seatsInfo"]])])
        print(table)

    def deletePlan(self, index):
        index = set(index)
        self.plans = [x for i, x in enumerate(self.plans) if i not in index]
    def run(self, plan):
            data, Api_Token = self.plan2data(plan)
            url = self.urls["book_seat"]
            self.session.headers["Api-Token"] = Api_Token.decode()
            self.session.headers["Content-Length"] = "114"
            res = self.session.post(url=url, data=data).json()
            return res

    def changeTime(self, index, beginTime, duration):
        for i in index:
            self.plans[i]["beginTime"] = beginTime
            self.plans[i]["duration"] = duration

if __name__ == "__main__":

    killer = Killer()
    killer.init("./config/config.yaml")
    print(killer.login())
    killer.run(killer.plans[0])