from flask import Flask, request
from flask_restful import Api, Resource, reqparse,abort, fields, marshal_with
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.app_context().push()

api = Api(app)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)

class VideoModel(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(100), nullable = False)
    views = db.Column(db.Integer, nullable = False)
    likes = db.Column(db.Integer, nullable = False)

    def __repr__(self):
        return f"video(name = {name}, likes = {likes}, views = {views})"

#making a first resource

# class Helloworld(Resource):

#     def get(self):
#         return {"data":"Hello world"}
#     def post(self):
#         return {"data":"postedd world"}

# api.add_resource(Helloworld, "/helloworld")

#passing specific data to the endpoint and supplying parameters

# class Helloworld(Resource):

#     def get(self, name, test):
#         return {"name":name, "test":test}
#     def post(self):
#         return {"data":"postedd world"}

#making some modifications

# names = {
#     "tim":{"age":19, 'gender':"male"},
#     "bill":{"age":70, "gender":"male"}
# }

# class Helloworld(Resource):

#     def get(self, name):
#         return names[name]
#     def post(self):
#         return {"data":"postedd world"}

# api.add_resource(Helloworld, "/helloworld/<string:name>")

#----Creating somehting like a youtube ----

# video_put_args = reqparse.RequestParser() #this validate the fields you want to put in the endpoint and what it should accept
# video_put_args.add_argument("name", type = str, help = 'Name of the video is required', location = 'form', required = True) #adding the rquired true means that the field must not reeturn empty value
# video_put_args.add_argument("views", type = int, help = 'Views of the video is required', location = 'form', required = True)
# video_put_args.add_argument("likes", type = int, help = 'likes of the video is required', location = 'form', required = True)

# videos = {}

# #making sure the program done crash if the video ID doesnt exist

# def abort_if_id_doesnt_exist(video_id):
#     if video_id not in videos:
#         abort(404, message = "video Doesnt not exist based on the ID...")

# #checking if the video already exist
# def abort_if_video_exist(video_id):
#     if video_id  in videos:
#         abort(409, message = "Video already exist")

# class Video(Resource):
#     #this check and gets the available videos
#     def get(self, video_id):
#         abort_if_id_doesnt_exist(video_id) #this comes later when raising a error of deosnt exist
#         return videos[video_id]

#     #this allows the creation of new videos
#     def put(self, video_id):
#         abort_if_video_exist(video_id)
#         args = video_put_args.parse_args()
#         videos[video_id] = args
#         return videos[video_id], 201 #201 stands for create

#     def delete(self, video_id):
#         abort_if_id_doesnt_exist(video_id)
#         del videos[video_id]
#         return '', 204

# api.add_resource(Video, "/video/<int:video_id>")

#--------adding to a database ------------

video_put_args = reqparse.RequestParser() #this validate the fields you want to put in the endpoint and what it should accept
video_put_args.add_argument("name", type = str, help = 'Name of the video is required', location = 'form', required = True) #adding the rquired true means that the field must not reeturn empty value
video_put_args.add_argument("views", type = int, help = 'Views of the video is required', location = 'form', required = True)
video_put_args.add_argument("likes", type = int, help = 'likes of the video is required', location = 'form', required = True)

video_update_args = reqparse.RequestParser()
video_update_args.add_argument("name", type = str, help = 'Name of the video is required', location = 'form') #adding the rquired true means that the field must not reeturn empty value
video_update_args.add_argument("views", type = int, help = 'Views of the video is required', location = 'form')
video_update_args.add_argument("likes", type = int, help = 'likes of the video is required', location = 'form')

#resource field is way to define how an object will be serialized

resource_field = {
    'id':fields.Integer,
    'name':fields.String,
    'views':fields.Integer,
    'likes':fields.Integer
}

class Video(Resource):
    #this check and gets the available videos
    @marshal_with(resource_field) #what this does is when we return take the return value and serialise it with that fields of resource_field
    #reason for doing this is because the result is an object so we need to serialize it
    def get(self, video_id):
        result  = VideoModel.query.filter_by(id = video_id).first()
        if not result:
            abort(404, message = 'could not find video with that ID')
        return result

    #this allows the creation of new videos
    @marshal_with(resource_field)
    def put(self, video_id):
        args = video_put_args.parse_args()
        result  = VideoModel.query.filter_by(id = video_id).first()
        if result:
            abort (409, message= "Video Id taken...")
        video = VideoModel(id = video_id, name =args['name'], views = args['views'], likes = args['likes'])
        db.session.add(video)
        db.session.commit()
        return video, 201 #201 stands for create

    @marshal_with(resource_field)
    def patch(self, video_id):
        args = video_update_args.parse_args()
        result  = VideoModel.query.filter_by(id = video_id).first()
        if not result:
            abort(404, message = "Video doesn't exit....")
        # this check for the field to modify or if the field returns non it wont modify it
        if args['name']:
            result.name = args['name']
        if args['views']:
            result.views = args['views']
        if args['likes']:
            result.likes = args['likes']

        db.session.commit()
        return result

    def delete(self, video_id):
        abort_if_id_doesnt_exist(video_id)
        del videos[video_id]
        return '', 204

api.add_resource(Video, "/video/<int:video_id>")

if __name__ == "__main__":
    with app.app_context():
        db.create_all()
        app.run(debug=True)


from chatterbot import ChatBot
from chatterbot.conversation import Statement

"""
This example shows how to create a chat bot that
will learn responses based on an additional feedback
element from the user.
"""

# Uncomment the following line to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

# Create a new instance of a ChatBot
bot = ChatBot(
    'Feedback Learning Bot',
    storage_adapter='chatterbot.storage.SQLStorageAdapter'
)

def get_feedback():

    text = input()

    if 'yes' in text.lower():
        return True
    elif 'no' in text.lower():
        return False
    else:
        print('Please type either "Yes" or "No"')
        return get_feedback()

print('Type something to begin...')

# The following loop will execute each time the user enters input
while True:
    try:
        input_statement = Statement(text=input())
        response = bot.generate_response(
            input_statement
        )

        print('\n Is "{}" a coherent response to "{}"? \n'.format(
            response.text,
            input_statement.text
        ))
        if get_feedback() is False:
            print('please input the correct one')
            correct_response = Statement(text=input())
            bot.learn_response(correct_response, input_statement)
            print('Responses added to bot!')

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break


from docxtpl import DocxTemplate  # pylint: disable=C0114

def main():
    """Initial main function"""
    doc = DocxTemplate(r".\test.docx")
    context = {'unit_number': "А0000"}
    doc.render(context)
    doc.save("result.docx")

if __name__ == '__main__':
    main()


from typing import Any, Iterable, List, Mapping
from unittest import mock

import pytest
from airbyte_cdk.models import AirbyteStream, SyncMode
from airbyte_cdk.sources.streams import Stream

class StreamStubFullRefresh(Stream):
    """
    Stub full refresh class to assist with testing.
    """

    def read_records(
        self,
        sync_mode: SyncMode,
        cursor_field: List[str] = None,
        stream_slice: Mapping[str, Any] = None,
        stream_state: Mapping[str, Any] = None,
    ) -> Iterable[Mapping[str, Any]]:
        pass

    primary_key = None

def test_as_airbyte_stream_full_refresh(mocker):
    """
    Should return an full refresh AirbyteStream with information matching the
    provided Stream interface.
    """
    test_stream = StreamStubFullRefresh()

    mocker.patch.object(StreamStubFullRefresh, "get_json_schema", return_value={})
    airbyte_stream = test_stream.as_airbyte_stream()

    exp = AirbyteStream(name="stream_stub_full_refresh", json_schema={}, supported_sync_modes=[SyncMode.full_refresh])
    assert exp == airbyte_stream

class StreamStubIncremental(Stream):
    """
    Stub full incremental class to assist with testing.
    """

    def read_records(
        self,
        sync_mode: SyncMode,
        cursor_field: List[str] = None,
        stream_slice: Mapping[str, Any] = None,
        stream_state: Mapping[str, Any] = None,
    ) -> Iterable[Mapping[str, Any]]:
        pass

    cursor_field = "test_cursor"
    primary_key = "primary_key"
    namespace = "test_namespace"

class StreamStubIncrementalEmptyNamespace(Stream):
    """
    Stub full incremental class, with empty namespace, to assist with testing.
    """

    def read_records(
        self,
        sync_mode: SyncMode,
        cursor_field: List[str] = None,
        stream_slice: Mapping[str, Any] = None,
        stream_state: Mapping[str, Any] = None,
    ) -> Iterable[Mapping[str, Any]]:
        pass

    cursor_field = "test_cursor"
    primary_key = "primary_key"
    namespace = ""

def test_as_airbyte_stream_incremental(mocker):
    """
    Should return an incremental refresh AirbyteStream with information matching
    the provided Stream interface.
    """
    test_stream = StreamStubIncremental()

    mocker.patch.object(StreamStubIncremental, "get_json_schema", return_value={})
    airbyte_stream = test_stream.as_airbyte_stream()

    exp = AirbyteStream(
        name="stream_stub_incremental",
        namespace="test_namespace",
        json_schema={},
        supported_sync_modes=[SyncMode.full_refresh, SyncMode.incremental],
        default_cursor_field=["test_cursor"],
        source_defined_cursor=True,
        source_defined_primary_key=[["primary_key"]],
    )
    assert exp == airbyte_stream

def test_supports_incremental_cursor_set():
    """
    Should return true if cursor is set.
    """
    test_stream = StreamStubIncremental()
    test_stream.cursor_field = "test_cursor"

    assert test_stream.supports_incremental

def test_supports_incremental_cursor_not_set():
    """
    Should return false if cursor is not.
    """
    test_stream = StreamStubFullRefresh()

    assert not test_stream.supports_incremental

def test_namespace_set():
    """
    Should allow namespace property to be set.
    """
    test_stream = StreamStubIncremental()

    assert test_stream.namespace == "test_namespace"

def test_namespace_set_to_empty_string(mocker):
    """
    Should not set namespace property if equal to empty string.
    """
    test_stream = StreamStubIncremental()

    mocker.patch.object(StreamStubIncremental, "get_json_schema", return_value={})
    mocker.patch.object(StreamStubIncremental, "namespace", "")

    airbyte_stream = test_stream.as_airbyte_stream()

    exp = AirbyteStream(
        name="stream_stub_incremental",
        json_schema={},
        supported_sync_modes=[SyncMode.full_refresh, SyncMode.incremental],
        default_cursor_field=["test_cursor"],
        source_defined_cursor=True,
        source_defined_primary_key=[["primary_key"]],
        namespace=None,
    )
    assert exp == airbyte_stream

def test_namespace_not_set():
    """
    Should be equal to unset value of None.
    """
    test_stream = StreamStubFullRefresh()

    assert test_stream.namespace is None

@pytest.mark.parametrize(
    "test_input, expected",
    [("key", [["key"]]), (["key1", "key2"], [["key1"], ["key2"]]), ([["key1", "key2"], ["key3"]], [["key1", "key2"], ["key3"]])],
)
def test_wrapped_primary_key_various_argument(test_input, expected):
    """
    Should always wrap primary key into list of lists.
    """

    wrapped = Stream._wrapped_primary_key(test_input)

    assert wrapped == expected

@mock.patch("airbyte_cdk.sources.utils.schema_helpers.ResourceSchemaLoader.get_schema")
def test_get_json_schema_is_cached(mocked_method):
    stream = StreamStubFullRefresh()
    for i in range(5):
        stream.get_json_schema()
    assert mocked_method.call_count == 1


import pytest
import webtest
from webtest.forms import Form
from zipfile import ZipFile
import os

@pytest.fixture
def app(tmpdir):
    from sphinxserver import app
    return app(home=tmpdir.join("root").strpath)

@pytest.fixture
def testapp(app):
    return webtest.TestApp(app)

def simple_zip(tmpdir):
    os.chdir(tmpdir.strpath)
    tmpfile = tmpdir.join("test.txt")
    tmpfile.write("hello world\n")
    zipfile_path = tmpdir.join("test.zip").strpath
    zipfile = ZipFile(zipfile_path, 'w')
    try:
        zipfile.write("test.txt")
    except:
        zipfile.close()
        raise
    zipfile.close()
    return zipfile_path

def test_bad_request(testapp):
    res = testapp.post("/", {':action': 'doc_uploadXXX', 'name': 'test.txt'},
                       upload_files=[("content", "test.zip", b"hello world")], status=400)
    assert "Bad Request" in str(res)

def test_nozip_upload(testapp, tmpdir):
    tmpfile = tmpdir.join("test.txt")
    tmpfile.write("test")
    res = testapp.post("/", {':action': 'doc_upload', 'name': 'test.txt'},
                       upload_files=[("content", tmpfile.strpath)], status=400)
    assert "not a zip file" in str(res)
    assert "test" not in testapp.get("/")

def test_invalid_zip_upload(testapp, tmpdir):
    zipfile = simple_zip(tmpdir)
    stream = open(zipfile, 'rb')
    content = stream.read()
    if type(content) != type(""): #python3
        faulty_char = content[0] + 1
        content = list(content)
        content[0] = faulty_char
        content = bytes(content)
    else: #python2
        faulty_char = chr(ord(content[0])+1)
        content = list(content)
        content[0] = faulty_char
        content = ''.join(content)
    res = testapp.post(
        "/", {':action': 'doc_upload', 'name': 'test'},
        upload_files=[("content", zipfile, content)], status=400)
    assert "not sane" in str(res)
    assert "test" not in testapp.get("/")

def test_too_short_zip_upload(testapp, tmpdir):
    zipfile = simple_zip(tmpdir)
    stream = open(zipfile, 'rb')
    content = stream.read()
    content = content[1:]
    res = testapp.post(
        "/", {':action': 'doc_upload', 'name': 'test'},
        upload_files=[("content", zipfile, content)], status=400)
    assert "Unexpected end" in str(res)
    assert "test" not in testapp.get("/")

def test_zip_upload(testapp, tmpdir):
    zipfile = simple_zip(tmpdir)
    res = testapp.post(
        "/", {':action': 'doc_upload', 'name': 'test'},
        upload_files=[("content", zipfile)])
    assert "test" in testapp.get("/")
    assert "hello world" in testapp.get("/test/test.txt")

def test_app_factory():
    from sphinxserver import app_factory
    app = app_factory(None, home="~/")
    testapp = webtest.TestApp(app)
    testapp.get("/")


# Import the locators file
import sys
sys.path.append(sys.path[0] + "/../..")
from pageobject.locators import locators
from pageobject.locators import *

from pageobject.helpers import helpers
from pageobject.helpers import *

from pyunitsetup import pyunit_setup

test_setup = pyunit_setup()

def scrap_youtube(test_url) -> list:
    meta_data_arr=[]
    test_setup.setUp()
    test_setup.browser.get(test_url)

    # Click on 'Accept All' in case the said window comes up
    # This occurs since cookies are cleared and machines are sanitized each run

    # Stack Overflow - https://stackoverflow.com/questions/66902404/selenium-python-click-agree-to-youtube-cookie
    # Locators were located using the below link
    # https://consent.youtube.com/m?continue=https%3A%2F%2Fwww.youtube.com%2F&gl=FR&m=0&pc=yt&uxe=23983172&hl=en&src=1

    # Below issue is not observed in local machines since there is no clean-up of cookies

    try:
        # Wait until the "Accept all" button is clickable
        accept_all = WebDriverWait(test_setup.browser, 5).until(EC.element_to_be_clickable(
            (By.CSS_SELECTOR, "form:nth-child(3) > div > div > button[aria-label= 'Accept all']")))

        # Click the "Accept all" button
        accept_all.click()

        print("Click on Accept all successful")
    except Exception as e:
        # Even if this exception is raised, we can still continue test execution
        # This means that the button was not found & the YouTube channel link has opened successfully
        print(f"'Accept All' button not present, proceed with the tests: {str(e)}")

    meta_data_arr = helpers.scrap_yt_content(test_setup.browser)

    test_setup.tearDown()
    return meta_data_arr

def main():
    meta_data_arr = scrap_youtube(locators.test_yt_url)
    helpers.print_scrapped_content(meta_data_arr)

if __name__ == '__main__':
    main()


import pytest

from fparser.two import Fortran2003

from psyclone.domain.lfric.kernel import FieldVectorArgMetadata

@pytest.mark.parametrize("datatype, access, function_space", [
    ("GH_REAL", "GH_READ", "W0"), ("gh_real", "gh_read", "w0")])
def test_create(datatype, access, function_space):
    '''Test that an instance of FieldVectorArgMetadata can be created
    successfully. Also test that the arguments are case insensitive.

    '''
    field_vector_arg = FieldVectorArgMetadata(
        datatype, access, function_space, "2")
    assert isinstance(field_vector_arg, FieldVectorArgMetadata)
    assert field_vector_arg.form == "gh_field"
    assert field_vector_arg._datatype == "gh_real"
    assert field_vector_arg._access == "gh_read"
    assert field_vector_arg._function_space == "w0"
    assert field_vector_arg._vector_length == "2"
    assert field_vector_arg._stencil is None

def test_create_stencil():
    '''Test that an instance of FieldVectorArgMetadata can be created
    successfully when stencil metadata is supplied.

    '''
    field_vector_arg = FieldVectorArgMetadata(
        "gh_real", "gh_read", "w0", "2", stencil="xory1d")
    assert isinstance(field_vector_arg, FieldVectorArgMetadata)
    assert field_vector_arg.form == "gh_field"
    assert field_vector_arg._datatype == "gh_real"
    assert field_vector_arg._access == "gh_read"
    assert field_vector_arg._function_space == "w0"
    assert field_vector_arg._vector_length == "2"
    assert field_vector_arg._stencil == "xory1d"

def test_init_invalid_vector_length():
    '''Test that an invalid vector length supplied to the constructor
    raises the expected exception.

    '''
    with pytest.raises(TypeError) as info:
        _ = FieldVectorArgMetadata("GH_REAL", "GH_READ", "W0", 1)
    assert ("The vector size should be a string but found int."
            in str(info.value))

def test_init_invalid_stencil():
    '''Test that an invalid stencil supplied to the constructor
    raises the expected exception.

    '''
    with pytest.raises(ValueError) as info:
        _ = FieldVectorArgMetadata(
            "GH_REAL", "GH_READ", "W0", "2", stencil="invalid")
    assert ("The 'stencil' metadata should be a recognised value (one of "
            "['x1d', 'y1d', 'xory1d', 'cross', 'region', 'cross2d']) but "
            "found 'invalid'." in str(info.value))

def test_get_metadata():
    '''Test that the get_metadata class method works as expected.'''
    fparser2_tree = FieldVectorArgMetadata.create_fparser2(
        "arg_type(GH_FIELD*3, GH_REAL, GH_READ, W0)", Fortran2003.Part_Ref)
    datatype, access, function_space, vector_length, stencil = \
        FieldVectorArgMetadata._get_metadata(fparser2_tree)
    assert datatype == "GH_REAL"
    assert access == "GH_READ"
    assert function_space == "W0"
    assert vector_length == "3"
    assert stencil is None

@pytest.mark.parametrize("fortran_string", [
    "arg_type(GH_FIELD*3, GH_REAL, GH_READ, W0)",
    "arg_type(GH_FIELD*3, GH_REAL, GH_READ, W0, STENCIL(Y1D))"])
def test_fortran_string(fortran_string):
    '''Test that the fortran_string method works as expected.'''

    field_vector_arg = FieldVectorArgMetadata.create_from_fortran_string(
        fortran_string)
    result = field_vector_arg.fortran_string()
    assert result == fortran_string.lower()

def test_vector_length_setter_getter():
    '''Test that the vector length setter and getter work as expected,
    including raising an exception if the value is invalid.

    '''
    field_vector_arg = FieldVectorArgMetadata("GH_REAL", "GH_READ", "W0", "2")

    with pytest.raises(ValueError) as info:
        field_vector_arg.vector_length = "invalid"
    assert ("The vector size should be a string containing an integer, "
            "but found 'invalid'." in str(info.value))

    with pytest.raises(ValueError) as info:
        field_vector_arg.vector_length = "1"
    assert ("The vector size should be an integer greater than 1 but found 1."
            in str(info.value))

    field_vector_arg.vector_length = "3"
    assert field_vector_arg.vector_length == "3"


'''
Created on 13 dic. 2018

@author: sony
'''
import tensorflow as tf
import tensorflow.contrib as tfcontrib
from tensorflow.python.keras import layers
from tensorflow.python.keras import losses
from tensorflow.python.keras import models
from tensorflow.python.keras import backend as K
from matplotlib import axis
from numpy.core.defchararray import center

class ModelMaker(object):
    '''
    classdocs
    '''

    def __init__(self, input_shape,padding_type):
        '''
        Constructor
        '''
        self.padding_type = padding_type

        inputs = layers.Input(shape=input_shape)
        encoder0_pool, encoder0 = self.encoder_block(inputs, 32)
        encoder1_pool, encoder1 = self.encoder_block(encoder0_pool, 64)
        encoder2_pool, encoder2 = self.encoder_block(encoder1_pool, 128)
        encoder3_pool, encoder3 = self.encoder_block(encoder2_pool, 256)
        encoder4_pool, encoder4 = self.encoder_block(encoder3_pool, 512)

        center = self.conv_block(encoder4_pool, 1024)

        decoder4 = self.decoder_block(center, encoder4, 512)
        decoder3 = self.decoder_block(decoder4, encoder3, 256)
        decoder2 = self.decoder_block(decoder3, encoder2, 128)
        decoder1 = self.decoder_block(decoder2, encoder1, 64)
        decoder0 = self.decoder_block(decoder1, encoder0, 32)

        outputs = layers.Conv2D(1, (1,1), activation='sigmoid')(decoder0)

        self.model = models.Model(inputs = [inputs], outputs = [outputs])

    def conv_block(self, input_tensor, num_filters):
        encoder = layers.Conv2D(num_filters, (3,3), padding=self.padding_type)(input_tensor)
        encoder = layers.BatchNormalization()(encoder)
        encoder = layers.Activation('relu')(encoder)
        encoder = layers.Conv2D(num_filters, (3,3), padding=self.padding_type)(encoder)
        encoder = layers.BatchNormalization()(encoder)
        encoder = layers.Activation('relu')(encoder)
        return encoder

    def encoder_block(self, input_tensor, num_filters):
        encoder = self.conv_block(input_tensor, num_filters)
        encoder_pool = layers.MaxPool2D((2,2), strides=(2,2))(encoder)
        return encoder_pool, encoder

    def decoder_block(self, input_tensor, concat_tensor, num_filters):
        decoder = layers.Conv2DTranspose(num_filters, (2,2), strides=(2,2), padding=self.padding_type)(input_tensor)
        decoder = layers.concatenate([concat_tensor, decoder], axis=-1)
        decoder = layers.BatchNormalization()(decoder)
        decoder = layers.Activation('relu')(decoder)
        decoder = layers.Conv2D(num_filters, (3,3), padding=self.padding_type)(decoder)
        decoder = layers.BatchNormalization()(decoder)
        decoder = layers.Activation('relu')(decoder)
        decoder = layers.Conv2D(num_filters, (3,3), padding=self.padding_type)(decoder)
        decoder = layers.BatchNormalization()(decoder)
        decoder = layers.Activation('relu')(decoder)
        return decoder


from datetime import datetime, timedelta
from math import ceil
file = open('dane/pogoda.txt','r').read().split('\n')[1:-1]
file = [ i.split('\t') for i in file]

data = datetime.strptime('31.03.2015', '%d.%m.%Y')
mx = 25000
zbiornik = mx
prev = mx
z1 = True

ans = dict(zip( [i for i in range(4, 10)], [0]*6))

a4 = 0
b4 = 0
c4 = 0

for i in file:
    data += timedelta(days=1)
    T = int(i[0])
    opady = float(i[1].replace(',', '.'))

    #print(zbiornik)

    zbiornik = min(mx, zbiornik + (700*opady))
    if not opady: zbiornik = max(0, zbiornik - ceil(0.0003 * (T**1.5) * prev))

    a4 += T <=15
    c4 += T>15 and opady > 0.6

    if T > 15 and opady <= 0.6:
        b4 += 1
        w = 12000
        if T > 30: w*=2

        if zbiornik -w < 0:
            if z1: z1=False; print(data, mx-zbiornik)
            m = data.month
            ans[m] += (mx-zbiornik)
            zbiornik = mx

        zbiornik = max(0, zbiornik - w)

    prev = zbiornik

print()
for i in ans:
    print(i, ceil(ans[i]/1000) * 11.74)

print()
print(a4, b4, c4)


from bs4 import BeautifulSoup

import requests

def scraping():
    URL_SCRAPING = 'https://usa.avtopmr.com/catalog/'
    resp = requests.get(URL_SCRAPING, timeout=10.0)
    if resp.status_code != 200:
        raise Exception('HTTP error access!')

    data_list = []
    html = resp.text
    soup = BeautifulSoup(html, 'html.parser')
    blocks = soup.select("title")
    for block in blocks:
        print(f'HTML text consists of {len(block.text)} symbols')
        print(50 * '=')
        print(block.text)
        break

if __name__ == '__main__':
    scraping()


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

import os
import asyncio

class nonblocking(object):
    """
    Make fd non blocking.
    copy from pymux.utils
    """
    def __init__(self, fd):
        self.fd = fd

    def __enter__(self):
        import fcntl
        self.orig_fl = fcntl.fcntl(self.fd, fcntl.F_GETFL)
        fcntl.fcntl(self.fd, fcntl.F_SETFL, self.orig_fl | os.O_NONBLOCK)

    def __exit__(self, *args):
        import fcntl
        fcntl.fcntl(self.fd, fcntl.F_SETFL, self.orig_fl)

async def readFromWsToFobj(ws, fobj, debug=False):
    async for msg in ws:
        if debug:
            print(msg, type(msg), len(msg))
        fobj.write(msg)

async def readFromFobjToWs(fobj, ws):
    while True:
        with nonblocking(fobj.fileno()):
            try:
                data = fobj.read(1024)
                if data is None or not data:
                    await asyncio.sleep(0.01)
                    continue
                await ws.send(data)
            except EOFError:
                await asyncio.sleep(0.01)
                continue


from . import submodules as sdl
from datetime import date, timedelta
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors

def buat_pdf_1():
    hd_iv = ["","Tanggal", "Dompet", "Tipe", "pemasukan", "pengeluaran"]
    ls_iv = []

    _, ls_tr = sdl.open_read_csv("sejarah_transaksi.csv")

    # Restriksi 30 hari yg lalu
    hari_ini = date.today()
    hari_30_belakang = (hari_ini - timedelta(days=30)).strftime("%Y/%m/%d")
    ls_tr_new = []
    for ele in ls_tr:
        if ele[0] < hari_30_belakang:
            break
        ls_tr_new.append(ele)

    # Pembuatan baris invoice pertabel
    for id,ele in enumerate(ls_tr_new):
        if ele[1] == "1":
            ls_iv.append([ id+1, ele[0], ele[3], ele[2], f"Rp{int(ele[4]):>10,}", None ])
        else:
            ls_iv.append([ id+1, ele[0], ele[3], ele[2], None, f"Rp{int(ele[4]):>10,}" ])

    # Pembuatan file pdf
    output_file = "report/pdf_1.pdf"
    title_text = "Tabel sejarah transaksi 30 hari terakhir"
    doc = SimpleDocTemplate(output_file, pagesize=letter, orientation='portrait')
    elements = []

    # Penambahan judul pada file
    styles = getSampleStyleSheet()
    title = Paragraph(title_text, styles['Title'])
    elements.append(title)

    # Mengumpulkan data tabel terdiri dari header dan isi
    table_data = [hd_iv] + ls_iv

    # Membuat objek/memori tabel
    table = Table(table_data)

    # Membuat model tabel
    style = TableStyle([
        ('ALIGN', (0, 0), (-1, -1), 'RIGHT'),
        ('TEXTCOLOR', (0, 0), (-1, 0), colors.black),  # Setting font color header
        ('BACKGROUND', (0, 0), (-1, 0), colors.grey),  # Setting backgroung header
        ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),  # Setting font style header
        ('FONTSIZE', (0, 0), (-1, 0), 12),
        ('BOTTOMPADDING', (0, 0), (-1, 0), 8),
        ('TOPPADDING', (0, 0), (-1, 0), 8),
        ('BOTTOMPADDING', (0, 1), (-1, -1), 4),
        ('TOPPADDING', (0, 1), (-1, -1), 4),
        ('GRID', (0, 0), (-1, -1), 1, colors.black),  # Setting cell border
        ('BACKGROUND', (4, 1), (4, -1), colors.lightgreen),  # Setting background hijau kolom ke-4
        ('BACKGROUND', (5, 1), (5, -1), colors.lightcoral),  # Setting backgroung merah kolom ke-5
    ])

    table.setStyle(style)

    # Tambahkan tabel
    elements.append(table)

    # Buat file pdf
    doc.build(elements)


import math
import torch
from torch import nn

class LDLLoss(nn.Module):
    """
    Label Distribution Learning Loss
    """

    def __init__(self, dist_size, delta: float = 1e-6, devise: str = 'cuda'):
        """
        損失関数の初期設定

        Args:
            dist_size (int): 確率分布のサイズ
            delta (float): log(0)を防ぐための微少数
        """
        super(LDLLoss, self).__init__()
        self.dist_size = dist_size
        self.delta = delta
        self.device = devise

    def forward(self, P, y) -> torch.tensor:
        """
        損失の計算

        Args:
            P (torch.tensor(batch_size, self.dist_size)): 予測確率分布
            y (torch.tensor(batch_size)): 正解ラベル
        """
        # 正解クラス y を y を中心とした正規分布 Y に変換
        Y = self.norm_dist(y)
        # Cross Entropy
        loss = -Y * torch.log(P + self.delta)

        return torch.mean(loss)

    def norm_dist(self, y: torch.tensor, sigma: float = 1.0) -> torch.tensor:
        """
        正解ラベルを正規分布に変換する処理

        Args:
            y (torch.tensor(batch_size)): 正規分布の平均
            sigma (float): 正規分布の分散

        Returns:
            torch.tensor(batch_size, self.dist_size): 正規分布
        """
        batch_size = y.size(0)
        X = torch.arange(0, self.dist_size).repeat(batch_size, 1).to(self.device)
        N = torch.exp(-torch.square(X - y.unsqueeze(1)) / (2 * sigma**2))
        d = torch.sqrt(torch.tensor(2 * math.pi * sigma**2)).to(self.device)

        return N / d


import os
from i3d import *
from utils.visualization_util import *
import numpy as np

## USE THIS EXTENSION IF YOU'VE PUT THE VIDEOS YOU WANT TO USE INTO THE INPUT FOLDER

# PATHS: cfg.input_folder, cfg.I3D_path
# AIM: extract from 1 video, n features. Merge n features to end with 32 features and save them in a .txt file

def run_i3d():
    '''
    Starting from videos, extract features using the I3D model pre-trained on the Kinetics dataset and save them as .txt files.
    How?
        Each video is divided into n clips of 16 frames, where n = int(np.round(total_number_of_frames/16)).
        Each clip is given as input to the I3D model, that returns as output an array of 1024 floats. This array is 1 feature.
        (n, 1024) features are then merged to end up with (32, 1024) features per each video.

    1. cfg.input_folder = folder containing all those videos whose features need to be extracted
    2. cfg.I3D_path = folder where these features are saved
    '''
    for filename in os.listdir(cfg.input_folder):
        if filename.endswith('.mp4'):
            video_name = os.path.join(cfg.input_folder, filename)
            name = os.path.basename(video_name).split('.')[0]

            # Read video: create n clips of 16 frames
            video_clips, num_frames = get_video_clips(video_name)
            print("Number of clips in the video : ", len(video_clips))

            # Initialize I3D model
            feature_extractor = i3d_feature_extractor()

            # Extract features
            rgb_features = []
            for i, clip in enumerate(video_clips):
                clip = np.array(clip)
                if len(clip) < params.frame_count:
                    continue

                clip = preprocess_input(clip)
                rgb_feature = feature_extractor.predict(clip)[0]
                rgb_feature = rgb_feature.reshape(1024,)
                rgb_features.append(rgb_feature)
                print("Processed clip : {} / {}".format(i, len(video_clips)))

            # Bag features: from n to 32 features per each video
            rgb_features = np.array(rgb_features)
            rgb_feature_bag = interpolate(rgb_features, params.features_per_bag)

            # Save each bag of features as .txt
            save_path = os.path.join(cfg.I3D_path, name + '.txt')
            np.savetxt(save_path, rgb_feature_bag)

        #print(filename)

if __name__ == '__main__':
   run_i3d()


from http.client import CONTINUE
from TD import TD,S

def indexer(keyword,list):
    idx = []
    for i in range(len(list)):
        if int(list[i]) == int(keyword):
            idx.append(i)
    return idx

def jaccard(list_a, list_b):
    len_intersect = len(set(list_a).intersection(set(list_b)))
    len_union = len(set(list_a).union(set(list_b)))
    return len_intersect/len_union

#File Reader - Population A
input = "Result_VARs.txt"
#input = "Santised_VARs.txt"
f = open(input,"r")
lines = f.readlines()

#Population Creator - Population
pop = []
for i in range(0,len(lines)):
    pop.append(lines[i].split(','))

idx_pop = []
for i in pop:
    idx_pop.append(indexer(1,i))

CONTINUE = False
start = 444 #Input Checkpoint Index Here (In case an out-of-memory crash)

#Create an empty file
if CONTINUE == False:
    list_file = open("jaccard_"+input,"w", encoding='utf-8')
    list_file.write("")
    list_file.close()
    start = 0

for i in range(start,len(idx_pop)):
    sum = 0
    for j in range(len(idx_pop)):
        sum = sum + jaccard(idx_pop[i],idx_pop[j])
    res = sum/len(idx_pop)
    print(str(i)+": "+str(res))
    ##Exporter - Population
    list_file = open("jaccard_"+input,"a", encoding='utf-8')
    list_file.write("\n"+str(i)+": "+str(res))
    list_file.close()

print("Done")

4 - Statistical Analysis/JaccardCalculator.py


import os
import glob
from bs4 import BeautifulSoup
import sys
from PIL import Image
import cv2
import numpy as np
import time
import torch
import torchvision
from torch.utils.data import Dataset
from torchvision import transforms
import os
import random
from matplotlib import pyplot as plt
import wandb
from tqdm import tqdm
import rasterio

# CUDA
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

def image_splitter(img_fp):
    """
    Takes the image filepath returns m x n array of the subimages
    pads with 0
    """
    #     img = gdal.Open(img_fp)
    #     img_array = img.GetRasterBand(1).ReadAsArray()
    # note have to add in rescaled helper fn

    # define splitting helper function
    # got this from https://towardsdatascience.com/efficiently-splitting-an-image-into-tiles-in-python-using-numpy-d1bf0dd7b6f7
    def reshape_split(image, kernel_size):
        img_height, img_width = image.shape
        tile_height, tile_width = kernel_size

        tiled_array = image.reshape(img_height // tile_height,
                                tile_height,
                                img_width // tile_width,
                                tile_width)
        tiled_array = tiled_array.swapaxes(1,2)
        return tiled_array

    # get image name to return
    # splits full fp by \, then gets the YYYY-MM-DD_#.jpg, then cuts off .jpg
    img_name = img_fp.split('\\')[-1][:-4]

    with rasterio.open(img_fp) as src:
        img_array = src.read()[0]
        #print(img_array.shape)

    # have to clip values to take away gray tint from image
    # and have to do this so that inshore offshore classifier works
    img_array = np.clip(img_array, -20, 0)

    img_height, img_width = img_array.shape
    # get next biggest multiple of 800
    # TODO: Make it closest multiple of 800 instead OR PAD WITH BLACK
    new_height = img_height + (800 - img_height % 800)
    new_width = img_width + (800 - img_width % 800)

    # calculate number of pixels to pad
    delta_w = new_width - img_width
    delta_h = new_height - img_height
    top, bottom = delta_h // 2, delta_h - (delta_h // 2)
    left, right = delta_w // 2, delta_w - (delta_w // 2)

    # pad image with 0
    image_pad = np.pad(img_array, ((top, bottom), (left, right)), mode='constant', constant_values=-20)

    # for some reason have to rescale or else when saving image it will be dark
    rescaled = 255*(image_pad-image_pad.min())/(image_pad.max()-image_pad.min())

    # normalize between 0 and 1
    # MAKE A BETTER RESCALING
    rescale_normalized = rescaled / 255

    # split image into subimages
        # will return array [m, n, 800, 800] where there are an m x n number of images with size 800x800
    split = reshape_split(rescale_normalized, kernel_size=(800,800))

    return split, img_name

def plot_large_image(model,image_path,threshold,save_path,device):
    """
    model: pytorch
    image_path: path tif file
    threshold: confidence threshold for model
    save_path: path to save plot (must be jpg), if none will not save
    device: device
    """

    # split image and save size for future
    split_image, _ = image_splitter(image_path)
    split_size = split_image.shape[:2]

    # flatten image and format to work with pytroch device
    flattened = np.reshape(split_image, (-1, split_image.shape[2], split_image.shape[3]))
    torch_split_img = np.array(flattened)
    torch_split_img = torch.tensor(torch_split_img,dtype=torch.float32)
    torch_split_img = torch.unsqueeze(torch_split_img, dim=0)
    torch_split_img = torch_split_img.permute(1,0,2,3)
    torch_split_img = torch_split_img.to(device)

    # predict bounding boxes
    pred = model(torch_split_img)
    # reshape to match image split
    pred = np.reshape(pred,split_size)

    # Create a 3x3 plot with a shared axis
#     figsize=(split_size[0], split_size[1])
    fig, axs = plt.subplots(split_size[0], split_size[1], sharex=True, sharey=True, figsize=(split_size[1] * 2,
                                                                                split_size[0] * 2))

    # Loop over the images and annotations and plot them on the corresponding subplot
    for i in range(split_size[0]):
        for j in range(split_size[1]):
            # Plot the image
            axs[i, j].imshow(split_image[i, j], cmap='gray', aspect='auto')

            # Remove the axis labels and ticks
            axs[i, j].set_xticks([])
            axs[i, j].set_yticks([])
            axs[i, j].axis('off')
            # Loop over the annotations and plot them as bounding boxes
            curPred = pred[i, j]
            for k in range(len(curPred["scores"])):
                # checks if above threshold
                if curPred["scores"][k] > threshold:
                    annotation = curPred['boxes'][k]
                    annotation = annotation.cpu().detach().numpy()
                    left, upper, right, lower = annotation
                    width, height = right - left, lower - upper
                    rect = plt.Rectangle((left, upper), width, height, linewidth=1, edgecolor='r', facecolor='none')
                    axs[i, j].add_patch(rect)

    # Remove the space between the images
    plt.subplots_adjust(wspace=0, hspace=0)
    if save_path:
        plt.savefig(save_path,quality=100,dpi=500)
    # Show the plot
    plt.show()

model = torchvision.models.detection.fasterrcnn_resnet50_fpn()
num_classes = 2  # 1 class (ship) + background
# get number of input features for the classifier
in_features = model.roi_heads.box_predictor.cls_score.in_features
# replace the pre-trained head with a new one
model.roi_heads.box_predictor = torchvision.models.detection.faster_rcnn.FastRCNNPredictor(in_features, num_classes)
model.load_state_dict(torch.load("src/models/faster300ep.pt"))
model.eval()
model.to(device)

feb_fp = "src/visualization/2020-02-04.tif"
april_fp = "src/visualization/2020-04-11.tif"

plot_large_image(model,feb_fp,0.7,"src/visualization/before_covid.jpg",device)

plot_large_image(model,april_fp,0.7,"src/visualization/after_covid.jpg",device)


import csv
import time
import json
from typing import List

class Ecsv():
    def __init__(self,mode='w',fieldnames=None,filename=None):
        if mode == 'w':
            if not fieldnames:
                raise('You need to provide fieldnames')
            self.create(fieldnames,filename)
        elif mode == 'r':
            if not filename:
                raise('You need to provide filename')
            self.read(filename)

    def create(self, fieldnames,filename=None):
        self.fieldnames = fieldnames
        self.timestr = time.strftime("%Y%m%d-%H%M%S")
        if not filename:
            self.filename = './output'+ self.timestr +'.csv'
        else:
            self.filename = filename

        with open(self.filename, 'w', encoding='UTF8', newline='') as f:
            writer = csv.DictWriter(f, fieldnames=self.fieldnames, delimiter=';')
            writer.writeheader()

    def save_row(self, row:dict):
        with open(self.filename, 'a', encoding='UTF8', newline='') as f:
            writer = csv.DictWriter(f, fieldnames=self.fieldnames, delimiter=';')
            writer.writerow(row)

    def save_rows(self, data):
        with open(self.filename, 'a', encoding='UTF8', newline='') as f:
            writer = csv.DictWriter(f, fieldnames=self.fieldnames, delimiter=';')
            writer.writerows(data)

    def read(self,filename):
        with open(filename, mode='r', encoding='UTF8') as csv_file:
            csv_reader = csv.DictReader(csv_file, delimiter=';')
            cnt=0
            self.rows = []
            for row in csv_reader:
                # if cnt == 0:
                #     self.fieldnames = row
                # else:
                #     self.rows.append(row)
                cnt+=1
                self.rows.append(row)

class Ejson():
    def __init__(self,mode='w',filename='test.json'):
        self.filename = filename

    def save_data(self,datadict):
        # extract data to json file
        with open(self.filename, 'w',newline='',encoding='utf-8') as jsonfile:
            json.dump(datadict, jsonfile) # commnad write json data to file

class Esql():
    def __init__(self,mode='w',filename='test.json'):
        self.filename = filename

class Etxt():
    def __init__(self,mode='w',filename=None):
        self.filename = filename
        if mode == 'w':
            self.create(filename)
        elif mode == 'r':
            if not filename:
                raise('You need to provide filename')
            self.read(filename)

    def create(self, filename=None):
        self.timestr = time.strftime("%Y%m%d-%H%M%S")
        if not filename:
            self.filename = './output'+ self.timestr +'.txt'
        else:
            self.filename = filename

        with open(self.filename, 'w', encoding='UTF8', newline='\n') as f:
            pass

    def save_row(self, row:str):
        with open(self.filename, 'a', encoding='UTF8', newline='\n') as f:
            f.write(row + "\n")

    def save_rows(self, data:List[str]):
        with open(self.filename, 'a', encoding='UTF8', newline='\n') as f:
            for string in data:
                f.write(string+ + "\n")

    def read(self,filename):
        with open(filename, mode='r', encoding='UTF8') as f:
            lines = f.readlines()
            cnt=0
            self.rows = []
            for row in lines:
                cnt+=1
                self.rows.append(row)


from __future__ import absolute_import, unicode_literals
import os
import random
from celery import Celery , shared_task
from celery.schedules import crontab

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "multitenantsaas.settings")

app = Celery("multitenantsaas")
app.config_from_object("django.conf:settings", namespace="CELERY")
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

# app.conf.beat_schedule = {
# # Executes every Monday morning at 7:30 a.m.
# # 'send-reminder': {
# #     # run this task every minute
# #     'task': 'apps.node_api.tasks.send_reminder',
# #     'schedule': crontab()
# #   },
# # }

app.conf.beat_schedule = {
    'multiply-task-crontab': {
        'task': 'multiply_two_numbers',
        # 'schedule': crontab(hour=7, minute=30, day_of_week=1),
        'schedule': 45,
        'args': (16, 16),
    },
    'multiply-every-5-seconds': {
        'task': 'multiply_two_numbers',
        'schedule': 60,
        'args': (16, 16)
    },
    # 'add-every-30-seconds': {
    #     # 'task': 'add',
    #     'task': 'apps.finance.tasks.add',
    #     'schedule': 60,
    #     'args': (30, 20)
    # },
}

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

@shared_task(name="sub_two_numbers")
def sub(x, y):
    # Celery recognizes this as the `multiple_two_numbers` task
    total = x - y
    return total


from camisole.models import Lang, Program

class C(Lang):
    source_ext = '.c'
    compiler = Program('gcc',
                       opts=['-std=c11', '-Wall', '-Wextra', '-O2', '-lm'])
    reference_source = r'''
#include <stdio.h>

int main(void)
{
    printf("42\n");
    return 0;
}
'''


from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from task.models import *
from .serializers import *

# GET ALL ROUTES FOR API
@api_view(['GET'])
def getRoutes(request):
    routes = [
        {'GET': '/api/tasks/'},
        {'GET': '/api/task/id/'},
        {'POST': '/api/create-task/'},

        {'POST': '/api/user/token'},
        {'POST': '/api/user/token/refresh'},
        {'GET': '/api/users/'},
        {'GET': '/api/user/id/'},

        {'DELETE': '/api/delete-note/'},
    ]
    return Response(routes)

# GET ALL USERS WITH TASKS & NOTES
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def getUsers(request):
    users = Profile.objects.all()
    serializer = ProfileSerializer(users, many=True)
    return Response(serializer.data)

# GET SINGLE USER WITH TASKS & NOTES
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def getUser(request, pk):
    user = Profile.objects.get(id=pk)
    serializer = ProfileSerializer(user, many=False)
    return Response(serializer.data)

# GET ALL TASKS
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def getTasks(request):
    tasks = Task.objects.all()
    serializer = TaskSerializer(tasks, many=True)
    return Response(serializer.data)

# GET SINGLE TASK WITH ID
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def getTask(request, pk):
    task = Task.objects.get(id=pk)
    serializer = TaskSerializer(task, many=False)
    return Response(serializer.data)

# CREATE NEW TASK
@api_view(['POST'])
@permission_classes([IsAuthenticated])
def createTask(request):
    serializer = TaskSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
    return Response(serializer.data)

# DELETE NOTE_
@api_view(['DELETE'])
@permission_classes([IsAuthenticated])
def deleteNote(request):
    task_id = request.data['task']
    note_id = request.data['note']

    task = Task.objects.get(id=task_id)
    notes = Note.objects.get(id=note_id)

    notes.delete()
    task.getNotesCount
    return Response('Note was removed successfully!')

# # CREATE NEW NOTE_
# @api_view(['POST'])
# @permission_classes([IsAuthenticated])
# def createNote(request, pk):
#     task = Task.objects.get(id=pk)
#     user = request.user.profile
#     data = request.data

#     note, created = Note.objects.get_or_create(
#         user = user,
#         task = task,
#     )
#     note.new_note = data['new_note']
#     note.save()
#     task.getNotesCount
#     serializer = TaskSerializer(task, many=False)
#     return Response(serializer.data)


#!/usr/bin/python
from Utils import *
from random import choice
#from Conf import *

class GemReads:
    def __init__(self, dp, sample_id):
        self.ref = env.GenePath
        self.dp = dp
        self.sample_id = sample_id
        self.num = 1 + (dp * env.GeneLength ) / (env.ReadLength * (2 if env.PairedEnd else 1))
        self.len = env.ReadLength
        self.qual = env.BaseQualOffset
        self.mod = env.GemModel
        self.fsize = env.FragmentLength
        self.fsizeSD = env.FragmentLengthSD
        self.pair = '-p' if env.PairedEnd else ''
        self.ReadsDir = os.path.join(env.ReadsDir, 'DP'+str(dp))
        self.outbase = os.path.join(self.ReadsDir, 'sample'+str(sample_id))
        runCommand('mkdir -p {}'.format(self.ReadsDir))
        self.SNPDir = os.path.join(env.SNPDir, 'DP'+str(dp))
        self.SNP = os.path.join(self.SNPDir, 'sample'+str(sample_id)+'.txt')
        runCommand('mkdir -p {}'.format(self.SNPDir))
        self.Read1 = self.outbase + '_fir.fastq'
        self.Read2 = self.outbase + '_sec.fastq'
        self.exon_region = reduce(lambda x,y: x+y, map(lambda e: range(e.start, e.end + 1), env.gene.exons))

    def generateSNP(self):
        with cd(self.SNPDir):
            with open(env.GenePath, 'r') as gf, open(basename(self.SNP), 'w') as sf:
                sf.write('0.5\n')
                pos = choice(self.exon_region) - env.offset
                name, seq = '', ''
                for line in gf:
                    if line.startswith('>'):
                        name = line.strip()[1:]
                    else:
                        seq += line.strip()
                Ref = seq[pos]
                Alt = choice(env.mut[Ref])
                ##GemReads uses 1-based position
                sf.write('0.5\t{}\t{}\t{}\n'.format(name, pos + 1, Alt))

    def run(self):
        self.generateSNP()
        cmd = ("GemReads.py -r {} -n {} -g {} -l {} -m {} -q {} " + \
              "-o {}").format(env.GeneLink, self.num, self.SNP, env.ReadLength,
                             env.GemModel, env.BaseQualOffset, self.outbase) + \
              " -u {} -s {} -p".format(env.FragmentLength, env.FragmentLengthSD) \
              if env.PairedEnd else ""
        #env.log(cmd)
        runCommand(cmd)

class BWA:
    def __init__(self, reads):
        dp = reads.dp
        sample_id = reads.sample_id
        self.reads = reads
        self.Index = env.BWAindex
        self.Read1 = reads.Read1
        self.Read2 = reads.Read2
        self.SAI1 = '3bam/DP{}/sample{}_fir.sai'.format(dp, sample_id)
        self.SAI2 = '3bam/DP{}/sample{}_sec.sai'.format(dp, sample_id)
        self.SAM = '3bam/DP{}/sample{}.sam'.format(dp, sample_id)
        self.SAMwithRG = '3bam/DP{}/sample{}.RG.sam'.format(dp,sample_id)
        self.BAM = '3bam/DP{}/sample{}.bam'.format(dp, sample_id)
        self.Sorted = '3bam/DP{}/sample{}.sorted'.format(dp, sample_id)
        self.SortedBAM = '3bam/DP{}/sample{}.sorted.bam'.format(dp, sample_id)
        #self.addRG = '\'if (m/^\@PG/) {print "@RG\tID:1\tSM:1\n";} if ($_ !~ m/^\@/) {chomp; print "\tRG:Z:1"}\''
        runCommand('mkdir -p 3bam/DP{}'.format(dp))

    def run(self):
        cmds = []
        cmds.append('bwa aln {} {} > {} 2> /dev/null'.format(self.Index, self.Read1, self.SAI1))
        cmds.append('bwa aln {} {} > {} 2> /dev/null'.format(self.Index, self.Read2, self.SAI2))
        cmds.append('bwa sampe {} {} {} {} {} > {} 2> /dev/null'.format(self.Index, self.SAI1, self.SAI2, self.Read1, self.Read2, self.SAM))
        for cmd in cmds:
            #env.log(cmd)
            os.system(cmd)

class samtools:
    def __init__(self, bwa):
        self.SAM = bwa.SAM
        self.bwa = bwa
        self.SAMwithRG = bwa.SAMwithRG
        self.BAM = bwa.BAM
        self.Sorted = bwa.Sorted
        self.SortedBAM = bwa.SortedBAM
        self.VCFdir = '5vcf/DP{}'.format(bwa.reads.dp)
        self.outbase =  os.path.join(self.VCFdir, 'sample{}'.format(bwa.reads.sample_id))
        self.rawBCF = self.outbase + '.raw.bcf'
        self.fltVCF = self.outbase + '.flt.vcf'
        runCommand('mkdir -p {}'.format(self.VCFdir))

    def addRG(self):
        with open(self.SAM, 'r') as s, open(self.SAMwithRG, 'w') as rg:
            for line in s:
                if line.startswith('@PG'):
                    rg.write('{}@RG\tID:1\tSM:1\n'.format(line))
                elif line.startswith('@'):
                    rg.write(line)
                else:
                    rg.write(line.strip())
                    rg.write('\tRG:Z:1\n')

    def run(self):
        cmds = []
        self.addRG()
        cmds.append('samtools view -Sb {} > {} 2> /dev/null'.format(self.SAMwithRG, self.BAM))
        cmds.append('samtools sort {} {} 2> /dev/null'.format(self.BAM, self.Sorted))
        cmds.append('samtools index {} 2> /dev/null'.format(self.SortedBAM))
        cmds.append('samtools mpileup -uf {} {} 2> /dev/null| bcftools view -bvcg - > {} 2> /dev/null'.format(env.RefFasta, self.SortedBAM, self.rawBCF))
        cmds.append('bcftools view {} 2> /dev/null | vcfutils.pl varFilter -D 1000 > {} 2> /dev/null'.format(self.rawBCF, self.fltVCF))
        for cmd in cmds:
            #env.log(cmd)
            os.system(cmd)

class GATK:
    def __init__(self, bwa):
        self.bwa = bwa
        self.MarkDupJar = os.path.join(env.PICARDPath, 'MarkDuplicates.jar')
        self.GATKJar = os.path.join(env.GATKPath, 'GenomeAnalysisTK.jar')
        self.rawBAM = bwa.SortedBAM
        self.outbase = '4gatk/DP{}/sample{}'.format(bwa.reads.dp, bwa.reads.sample_id)
        self.MarkDupBAM = self.outbase + 'MarkDup.bam'
        self.rawVCF = self.outbase + 'both.raw.vcf'
        self.filteredVCF = self.outbase + 'filtered.vcf'
        self.SNV = self.outbase + 'snv.vcf'
        runCommand('mkdir -p 4gatk/DP{}'.format(bwa.reads.dp))

#        self.MemLimit = env.MemLimit

    def MarkDup(self):
        cmd = 'java -Xmx{} -XX:ParallelGCThreads={} -jar {} '.format(env.MemLimit, env.threads, self.MarkDupJar) + \
              'I={} O={} M={} VALIDATION_STRINGENCY=SILENT ASSUME_SORTED=true'.format(self.rawBAM, self.MarkDupBAM, self.outbase + 'MarkDup.metrics')
        runCommand(cmd)

    def samIndex(self):
        cmd = 'samtools index {}'.format(self.MarkDupBAM)
        runCommand(cmd)

    def UnifiedGenotyper(self, bam):
        cmd = 'java -Xmx{} -XX:ParallelGCThreads={} -jar {} '.format(env.MemLimit, env.threads, self.GATKJar) + \
              '-R {} '.format(env.RefFasta) + \
              '-T UnifiedGenotyper ' + \
              '-nt {} '.format(env.threads) + \
              '-I {} '.format(bam) + \
              '-stand_call_conf 50 ' + \
              '-stand_emit_conf 50 ' + \
              '-mbq 20 ' + \
              '-A QualByDepth ' + \
              '-A HaplotypeScore ' + \
              '-A FisherStrand ' + \
              '-A MappingQualityZero ' + \
              '-A RMSMappingQuality ' + \
              '-A MappingQualityRankSumTest ' +\
              '-A ReadPosRankSumTest ' + \
              '-A BaseQualityRankSumTest ' + \
              '-o {} ' \
              '-glm BOTH'
        runCommand(cmd, show_stderr=True)

    def VariantFiltration(self):
        cmd = 'java -Xmx{} -XX:ParallelGCThreads={} -jar {} '.format(env.MemLimit, env.threads, self.GATKJar) + \
              '-T VariantFiltration ' + \
              '-R {} '.format(env.RefFasta) + \
              '-o {} '.format(self.filteredVCF) + \
              '--variant {} '.format(self.rawVCF) + \
              '--filterExpression "AB < 0.2 || MQ0 > 50" ' + \
              '--filterName "Nov09filters" ' + \
              '--mask mask.vcf ' + \
              '--maskName InDel'
        runCommand(cmd, show_stderr=True)

    def SelectSNV(self):
        cmd = 'java -Xmx{} -XX:ParallelGCThreads={} -jar {} '.format(env.MemLimit, env.threads, self.GATKJar) + \
              '-R {} '.format(env.RefFasta) + \
              '-T SelectVariants ' + \
              '--variant {} '.format(self.filteredVCF) + \
              '-o {} '.format(self.SNV) + \
              '-selectType SNP ' + \
              '-select "DP > 10 && QD > 10.0 && MQ > 50.0 && FS < 13.0 && HaplotypeScore < 13.0 && MQRankSum > -1.96 && ReadPosRankSum > -1.96"'
        runCommand(cmd, show_stderr=True)

    def run(self):
        self.MarkDup()
        self.samIndex()
        self.UnifiedGenotyper(self.MarkDupBAM)
        self.VariantFiltration()
        self.SelectSNV()

def runSample(dp,n1, n2):
    for i in range(n1, n2):
        if (i+1)%10 == 0:
            env.log('{} completed for depth={} ids={}..{}'.format(i-n1+1, dp, n1, n2))
        reads = GemReads(dp, i+1)
        reads.run()
        bwa = BWA(reads)
        bwa.run()
        vcf = samtools(bwa)
        vcf.run()

if __name__ == '__main__':
    dps = 20 #[5, 10, 15, 20, 30, 40, 50, 60, 80, 100]
    #parmap(lambda x: runSample(x, 2), dps, 1)
    n1, n2 = map(int, sys.argv[1:3])
    p = (n2 - n1)/8
    parmap(lambda x: runSample(20, n1 + x*p, n1 + (x+1)*p), range(8), 8)


from copy import deepcopy
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Tuple, Union

import numpy as np
import torch
from agent.dqn import DQNAgent, DQNParams
from env.env import ConnectFour
from typings import STEP_OUTPUT, BoardType, Example
from utils.model_utils import evaluate_quantile_at_action
from utils.replay_buffer import ReplayBuffer
from utils.utils import plot_mean_loss, plot_total_reward

def get_key_from_value(d: Dict[int, DQNAgent], val: DQNAgent) -> int:
    return [k for k, v in d.items() if v == val][0]

def env_step(
    env: ConnectFour, agent: DQNAgent, state: BoardType, player: int
) -> Tuple[int, STEP_OUTPUT]:
    """agentが行動して、envによって、その行動が実現可能かをを判断して、
    可能であれば、一つ先の盤面に進めて次のプレイヤーにバトンパス。不可であれば、再試行する

    Args:
        env (ConnectFour): 環境
        agent (DQNAgent): エージェント
        state (BoardType): 状態
        player (int): プレイヤー

    Returns:
        : STEP_OUTPUT
    """
    is_valid_location = False
    while not is_valid_location:
        action = agent.get_action(state)
        # playerが既に埋まっている列を選択したら(is_valid_location=False)、もう一回試行する
        is_valid_location = env.board.check_is_valid_location(col=action)
    output = env.step(action, player)
    # playerが正しく列を選択したら(is_valid_location=True)、ボードの状態が変わって次のプレイヤーにバトンパス
    return action, output

class Coach:
    def __init__(
        self, env: ConnectFour, agent: DQNAgent, replay_buffer: ReplayBuffer, sync_interval: int
    ) -> None:
        self.env = env
        self.agent = agent
        self.gamma = 0.9
        self.replay_buffer = replay_buffer
        self.sync_interval = sync_interval

    def get_delta(self, example: Example) -> float:
        """予測誤差(TDターゲット - self.agent.qvnet(state))を求める

        Args:
            example (Example): 現在の状態、プレイヤー、行動、次の状態、などを保持

        Returns:
            float: 予測誤差. prioritized_experience_replayで、予測誤差が大きいほど優先されてバッチとして選ばれて学習される
        """
        tensor_examples = self.agent.to_tensor([example])
        with torch.no_grad():
            qs, _ = self.agent.qvnet(tensor_examples.state)

        # Calculate quantile values of current states and actions at taus.
        q = evaluate_quantile_at_action(qs, tensor_examples.action)
        assert q.shape == (self.agent.batch_size, self.agent.num_taus, 1)
        with torch.no_grad():
            t = self.agent.get_target(
                tensor_examples.reward, tensor_examples.next_state, tensor_examples.done
            )
        assert t.shape == (self.agent.batch_size, 1, self.agent.num_taus)
        return float((t - q).detach().abs().sum(dim=1).mean(dim=1, keepdim=True).mean().item())

    def execute_one_episode(self) -> List[Example]:
        state = deepcopy(self.env.reset())
        player = 1

        state_action_list: Dict[int, List[Tuple[BoardType, int]]] = {
            1: [],
            -1: [],
        }
        while True:
            action, output = env_step(self.env, self.agent, state, player)
            if player == 1:
                state_action_list[1].append((state, action))
            else:
                state_action_list[-1].append((state, action))
            if output.done:
                winning_player = player
                examples_by_player: Dict[int, List[Example]] = {
                    1: [],
                    -1: [],
                }
                for player, _list in state_action_list.items():
                    for i, (state, action) in enumerate(_list):
                        if i == len(_list) - 1:  # 最後の状態
                            # next_stateの注意点
                            # done=Trueであれば、TDターゲットの計算に使われないため、勝者の最後の状態のnext_stateはどんな値を入れてもいい
                            # 敗者の最後の状態のnext_stateは勝敗が決着した状態（connect4では、4つのコインが揃った状態）
                            # よって、ここでは最後の状態のnext_stateは勝者でも敗者でも同じ値を入れる
                            next_state = output.next_state
                            # 試合終了フラグ: 勝つ一手を打った瞬間に試合終了
                            done = winning_player == player
                            # reward: 各プレイヤーの最後のプレイに報酬を与える
                            # 勝者の報酬は+1、敗者の報酬は-1、ドローの場合は両プレイヤーともに0
                            reward = output.reward if player == winning_player else -output.reward
                        else:
                            next_state = _list[i + 1][0]  # 一つ目の要素がstate
                            done = False
                            reward = 0
                        examples_by_player[player].append(
                            Example(
                                state=state,
                                action=action,
                                next_state=next_state,
                                player=player,
                                done=done,
                                reward=reward,
                                winning_player=winning_player,
                            )
                        )
                    # get reward and next_state for multi-step learning
                    for i, ex in enumerate(
                        examples_by_player[player][: -self.agent.multi_step_td_target + 1]
                    ):
                        next_i = i + self.agent.multi_step_td_target - 1
                        ex.next_state = examples_by_player[player][next_i].next_state  # n個先の状態
                        total_reward = 0.0  # n個先まで行動したときに実際に得られた収益（割引付き報酬合計）
                        for power in range(self.agent.multi_step_td_target):
                            total_reward += (
                                self.agent.gamma**power
                                * examples_by_player[player][i + power].reward
                            )
                        ex.reward = total_reward
                        ex.delta = self.get_delta(ex)
                return examples_by_player[1] + examples_by_player[-1]

            state = deepcopy(output.next_state)
            player = output.next_player

    def update(self, episode: int) -> Union[float, None]:
        batch_size = self.agent.batch_size
        self.agent.batch_size = 1
        examples_one_episode = self.execute_one_episode()
        self.agent.batch_size = batch_size
        for ex in examples_one_episode:
            self.replay_buffer.add(ex)

        if len(self.replay_buffer) < self.agent.batch_size:
            return None

        examples = self.replay_buffer.get_batch()
        loss = self.agent.update(examples)

        if episode % self.sync_interval == 0:
            self.agent.sync_qvnet()

        return loss

def vs(latest: DQNAgent, past: DQNAgent, num_games: int) -> int:
    """最新のエージェントと過去のエージェントを戦わせて、前者の勝った試合数を返す

    Args:
        latest (DQNAgent): 最新のエージェント
        past (DQNAgent): 過去のエージェント
        num_games (int): 試合数

    Returns:
        int: 最新のエージェントの勝った試合数
    """
    env = ConnectFour()
    num_win = 0
    agent_dict = {
        1: latest,
        -1: past,
    }
    for _ in range(num_games):
        state = deepcopy(env.reset())
        cur_player = 1
        done = False
        while not done:
            cur_agent = agent_dict[cur_player]
            action, output = env_step(env, cur_agent, state, cur_player)
            if output.done:
                num_win += 1 if cur_player == get_key_from_value(agent_dict, latest) else 0

            state = deepcopy(output.next_state)
            cur_player = output.next_player
            done = output.done

    return num_win

def main() -> None:
    mean_win_rates = []
    mean_loss = []
    output_dir = Path(f"outputs/{datetime.now().strftime('%Y%m%d/%H%M%S')}")
    output_dir.mkdir(exist_ok=True, parents=True)
    for i in range(10):
        (output_dir / f"{i}").mkdir(exist_ok=True, parents=True)
        win_rate_list = []
        loss_list = []

        num_episodes = 100000
        sync_interval = 100
        num_games_for_eval = 100
        vs_interval = 1000

        env = ConnectFour()
        agent = DQNAgent(DQNParams())
        replay_buffer = ReplayBuffer(buffer_size=10000, batch_size=agent.batch_size)
        coach = Coach(env, agent, replay_buffer, sync_interval=sync_interval)
        past_agent = deepcopy(coach.agent)

        print(f"=============={i}: {num_episodes} episode==================")
        for episode in range(num_episodes):
            loss = coach.update(episode)

            if episode % vs_interval == 0:
                loss_list.append(loss)
                # current vs before
                win = vs(latest=coach.agent, past=past_agent, num_games=num_games_for_eval)
                win_rate = win / num_games_for_eval
                print(
                    f"episode :{episode}, winning rate : {win_rate:.3f} ({win} / {num_games_for_eval})"
                )
                win_rate_list.append(win_rate)

                torch.save(
                    coach.agent.qvnet.state_dict(),
                    str(output_dir / f"{i}" / f"q_net_{episode}.pth"),
                )

        mean_win_rates.append(win_rate_list)
        mean_loss.append(loss_list)
        plot_total_reward(np.mean(mean_win_rates, axis=0), output_dir)
        plot_mean_loss(np.mean(mean_loss, axis=0), output_dir)

if __name__ == "__main__":
    main()


import os
from flask import jsonify
from app import create_app

app = create_app(os.getenv('FLASK_ENV', 'development'))

@app.errorhandler(404)
def page_not_found(e):
    """
    Esta función devuelve un mensaje JSON con un error de "página no encontrada" y un código
    de estado 404.

    :param e: El parámetro "e" es un objeto de excepción que se pasa a la función cuando no
    se encuentra una página. Contiene información sobre el error que ocurrió
    :return: Un objeto JSON con el mensaje "Página no encontrada" y un código de estado de
    404 (página no encontrada).
    """
    mensaje = {'error': 'Página no encontrada.'}
    return jsonify(mensaje), 404

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


from flask import redirect, render_template, request, send_file, session, url_for
#from typing_extensions import self
from flask import Flask, jsonify,request
from flask_cors import CORS
from os import path
from flask_bcrypt import Bcrypt
from datetime import datetime, timedelta, timezone
from flask_jwt_extended import create_access_token,get_jwt,get_jwt_identity, \
                               unset_jwt_cookies, jwt_required, JWTManager

from flask import send_file
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import MinMaxScaler
import pickle

from io import BytesIO
from reportlab.pdfgen import canvas

app = Flask(__name__)

bcrypt = Bcrypt(app)

cors = CORS(app, resources={r"*": {"origins": "*"}})

app.config['SECRET_KEY'] = 'hjshjhdjah kjshkjdhjs'

app.config["JWT_SECRET_KEY"] = "99999999999999999999"

jwt = JWTManager(app)

#ML
#reasd DATA FILE
fruits = pd.read_table('data.txt')
#manually select the features as targets (my portion that i think they do a great input as data to my model so it learns..the DATA)
feature_names = ['mass', 'width', 'height', 'color_score']
#define X as my actual real data
X = fruits[feature_names]
#define y as the lable to that
y = fruits['fruit_label']
#split the data into train set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
#standerdize the data using min max scalar
scaler = MinMaxScaler()
#apply the scalar to the sets
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

#routes
@app.route('/debug')
def attributes():
    print(fruits.head())
    print("Connected")
    return jsonify("connected!, try routes: /Dtree /knn /logREg")

@app.route('/fr')
def french():
    try:
        return send_file('C:/Users/Lenovo/Desktop/thesis/doc+src/ML/Report.pdf', download_name='Report.pdf')
    except Exception as e:
        return str(e)

@app.route('/Dtree')
def decisionTree():
    clf = DecisionTreeClassifier().fit(X_train, y_train)
    # save the model to disk
    filename = 'finalized_model.sav'
    pickle.dump(clf, open(filename, 'wb'))
    return jsonify('Accuracy of Decision Tree classifier on training set: {:.2f}'.format(clf.score(X_train, y_train)),'Accuracy of Decision Tree classifier on test set: {:.2f}'.format(clf.score(X_test, y_test)))

@app.route('/logReg')
def logisticRegression():
    logreg = LogisticRegression()
    logreg.fit(X_train, y_train)
    return jsonify('Accuracy of Logistic regression classifier on training set: {:.2f}'.format(logreg.score(X_train, y_train)),'Accuracy of Logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))

@app.route('/knn')
def Knearestneigbor():
    knn = KNeighborsClassifier()
    knn.fit(X_train, y_train)
    return jsonify('Accuracy of K-NN classifier on training set: {:.2f}'.format(knn.score(X_train, y_train)),'Accuracy of K-NN classifier on test set: {:.2f}'.format(knn.score(X_test, y_test)))

@app.route('/mymodel')
def final():
    loaded_model = pickle.load(open('finalized_model.sav', 'rb'))
    return jsonify('Accuracy of FINAL classifier on TESTING set: {:.2f}'.format(loaded_model.score(X_test, y_test)))

#dynamic route for user interaction with datasets, solvers selection
# For iris dataset
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.neural_network import MLPClassifier

# Load iris dataset
iris = load_iris()
X_iris, y_iris = iris.data, iris.target
X_iris_train, X_iris_test, y_iris_train, y_iris_test = train_test_split(X_iris, y_iris, test_size=0.2, random_state=42)

# For breast cancer dataset
from sklearn.datasets import load_breast_cancer

# Load breast cancer dataset
breast_cancer = load_breast_cancer()
X_bc, y_bc = breast_cancer.data, breast_cancer.target
X_bc_train, X_bc_test, y_bc_train, y_bc_test = train_test_split(X_bc, y_bc, test_size=0.2, random_state=42)

# For digits dataset
from sklearn.datasets import load_digits

# Load digits dataset
digits = load_digits()
X_digits, y_digits = digits.data, digits.target
X_digits_train, X_digits_test, y_digits_train, y_digits_test = train_test_split(X_digits, y_digits, test_size=0.2, random_state=42)

# For wine dataset
from sklearn.datasets import load_wine

# Load digits dataset
wine = load_wine()
X_wine, y_wine = wine.data, wine.target
X_wine_train, X_wine_test, y_wine_train, y_wine_test = train_test_split(X_wine, y_wine, test_size=0.2, random_state=42)

@app.route('/nn_classification', methods=['POST'])
def nn_classification():

    # Load data from JSON request
    #request_data = request.get_json()
    #dataset = request_data['dataset']
    #solver = request_data['solver']
    #regularization_term = float(request_data['reg_term'])
    dataset = request.form['dataset']
    solver = request.form['solver']
    regularization_term = float(request.form['alpha'])
    # Load dataset
    if dataset == 'iris':
        X_train, X_test, y_train, y_test = X_iris_train, X_iris_test, y_iris_train, y_iris_test
    elif dataset == 'breast_cancer':
        X_train, X_test, y_train, y_test = X_bc_train, X_bc_test, y_bc_train, y_bc_test
    elif dataset == 'digits':
        X_train, X_test, y_train, y_test = X_digits_train, X_digits_test, y_digits_train, y_digits_test
    elif dataset == 'wine':
        X_train, X_test, y_train, y_test = X_wine_train, X_wine_test, y_wine_train, y_wine_test
    else:
        return 'Invalid dataset'

    # Train neural network
    #hidden_layer_sizes = (layer_sizes[i] for i in range(n_layers))
    hidden_layer_sizes=[10,5]
    clf = MLPClassifier(hidden_layer_sizes=hidden_layer_sizes, max_iter=1000, alpha=regularization_term, solver=solver, verbose=10, random_state=42)
    clf.fit(X_train, y_train)

    # Evaluate accuracy
    y_pred = clf.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)

    # Create PDF report
    buffer = BytesIO()
    report = canvas.Canvas(buffer)

    # Write report header
    report.setFont('Helvetica-Bold', 16)
    report.drawString(100, 700, 'Neural Network Classification Report')
    report.line(100, 690, 500, 690)

    # Write selected values
    report.setFont('Helvetica', 12)
    report.drawString(100, 650, f'Dataset: {dataset}')
    report.drawString(100, 630, f'Solver: {solver}')
    report.drawString(100, 610, f'Alpha: {regularization_term:.4f}')

    # Write accuracy
    report.setFont('Helvetica-Bold', 14)
    report.drawString(100, 570, f'Accuracy: {accuracy:.2f}')

    # Save report and return it as a download
    report.save()
    buffer.seek(0)
    return send_file(buffer, download_name='report.pdf')

    # Render form template for GET request

@app.route('/',methods=["GET"])
def neuralNetworkFormHandler():
    return render_template('neuralNetworkForm.html')

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


import cv2
import mediapipe as mp
import math
import numpy

# import for pycaw from github: https://github.com/AndreMiras/pycaw
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume

devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
    IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))

# print(volume.GetMute())
# print(volume.GetMasterVolumeLevel())
# print(volume.GetVolumeRange())
# volume.SetMasterVolumeLevel(-65.25, None)

cap =cv2.VideoCapture(0)

mpDraw = mp.solutions.drawing_utils
mpHands = mp.solutions.hands
hands = mpHands.Hands()

while True:
    success,img = cap.read()
    imgRGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

    #detected palm
    results = hands.process(imgRGB)
    #if palm is detected
    if results.multi_hand_landmarks:
        #loop through all the hands
        for handLms in results.multi_hand_landmarks:
            lmList = []
            for id, lm in enumerate(handLms.landmark):
                h,w,c = img.shape
                cx,cy = int(lm.x*w) , int(lm.y*h)
                lmList.append([id,cx,cy])

                # print(id, lm)
                # mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
            if lmList:
                #co-ordinates of thumb
                x1,y1 = lmList[4][1], lmList[4][2]
                #co-ordinates of fore-finger
                x2,y2 = lmList[8][1], lmList[8][2]
                #circle around thumb tip
                cv2.circle(img,(x1,y1),15,(1,23,123),cv2.FILLED)
                #circle around fore-finger tip
                cv2.circle(img,(x2,y2),15,(1,23,123),cv2.FILLED)
                #line b/w two tips
                cv2.line(img,(x1,y1),(x2,y2),(1,23,123),4)
                z1,z2 = (x1+x2)//2, (y1+y2)//2
                length = math.hypot(x2-x1, y2-y1)
                # print(length)

            # length ranges from 50-300,, 50 signifies 0 or min and 300 signifies 100 or max volume
            # when length is 50---> lib(pycaw) is -65.25 ---> system volume is 0
            # when length is 300---> lib(pycaw) is 0.0 ---> system volume is 100
            volRange = volume.GetVolumeRange()
            minVol = volRange[0]
            maxVol = volRange[1]
            vol = numpy.interp(length, [50,100], [minVol, maxVol])
            volPer = numpy.interp(length, [50,100], [0,100])
            volBar = numpy.interp(length, [50,300], [400,150])

            volume.SetMasterVolumeLevel(vol, None)
            cv2.putText(img, str(int(volPer)), (40,450), cv2.FONT_HERSHEY_COMPLEX, 2, (1,3,5), 3)
            cv2.rectangle(img, (50,150), (85,400) ,(123,213,122), 3)
            cv2.rectangle(img, (50, int(volBar)), (85,400) ,(0,231,23), cv2.FILLED)

    cv2.imshow("Gesture sound control", img)
    cv2.waitKey(1)


# This file is part of ValAsp which is released under the Apache License, Version 2.0.
# See file README.md for full license details.

"""The enum :class:`Fun` and the primitive types are defined in this module.

The primitive types are markers to be used in annotations. They cannot be instantiated, and offer a very limited set of utility function.
"""

from dataclasses import dataclass
from enum import Enum
from typing import List, ClassVar

import clingo
import typing

from valasp.domain.names import PredicateName

class Fun(Enum):
    """Modalities for the :meth:`validate` decorator.

    * **FORWARD_IMPLICIT** means FORWARD for symbols of arity 1, and IMPLICIT otherwise.
    * **FORWARD** can be used with symbols of arity 1 and essentially means to leave the init argument as it is.
    * **IMPLICIT** must be used if the init argument is expected to be a function with the same name of the symbol.
      The arguments of the function are unpacked.
    * **TUPLE** is like IMPLICIT, but the function is expected to have emtpy name.
    """
    FORWARD_IMPLICIT = 0
    FORWARD = 1
    IMPLICIT = 2
    TUPLE = 3

@dataclass(frozen=True)
class Type:
    """Base class for type annotations.

    This class cannot be instantiated, and its subclasses are expected to satisfy this invariant.
    """
    __primitives = None

    def __init__(self):
        raise NotImplemented('this class must be used only as a marker')

    @staticmethod
    def init_code(arg: str) -> List[str]:
        """Return code to validate the type of the given argument name.

        Subclasses are expected to call this method in their init_code() method.

        :param arg: the name of the argument
        :return: validation code
        """
        return [
            f'if not isinstance({arg}, clingo.Symbol):',
            f'    raise TypeError(f"expecting clingo.Symbol, but received type({{{arg}}})")',
        ]

    @classmethod
    def is_primitive(cls, typ: ClassVar) -> bool:
        """Return true if typ is considered a primitive type.

        :param typ: a type
        :return: true if typ is a primitive type
        """
        return typ in cls.__primitives

    @classmethod
    def get_primitive(cls, typ: ClassVar) -> 'Type':
        """Return a subclass of Type associated with typ.

        :param typ: a type for which Type.is_primitive(typ) == True
        :raise: KeyError if Type.is_primitive(typ) != True
        :return: a subclass of Type associated with typ
        """
        return cls.__primitives[typ]

    @classmethod
    def set_primitives(cls, value) -> None:
        """Init the Type class with primitive types.

        This method is called at the end of this file, and cannot be called a second time.

        :param value: a dictionary (see the bottom of this file)
        :raise: PermissionError if called a second time
        """
        if cls.__primitives:
            raise PermissionError(f'attempt to call set_primitives of {cls} from outside its module')
        cls.__primitives = value

    @classmethod
    def parse(cls, value: str) -> str:
        """Return value, or raises an exception if value is not a string.

        :param value: a string to be parsed
        :raise: TypeError if value is not str
        :return: value
        """
        if not isinstance(value, str):
            raise TypeError(f"expecting str, but found {type(value)}")
        return value

@dataclass(frozen=True)
class Integer(Type):
    """Primitive type representing integers.

    This class cannot be instantiated.
    """

    def __init__(self):
        super().__init__()

    @classmethod
    def init_code(cls, arg: str) -> List[str]:
        return super().init_code(arg) + [
            f'if {arg}.type != clingo.SymbolType.Number:',
            f'    raise TypeError(f"expecting clingo.SymbolType.Number, but received {{{arg}}}")',
            f'self.{arg} = {arg}.number',
            f'if not({cls.min()} <= self.{arg} <= {cls.max()}):',
            f'    raise OverflowError(f"argument {arg} will overflow with value {{{arg}}}")',
        ]

    @classmethod
    def max(cls) -> int:
        """Return the greatest integer for the ASP system.

        :return: the greatest integer
        """
        return 2**31 - 1

    @classmethod
    def min(cls) -> int:
        """Return the smallest integer for the ASP system.

        :return: the smallest integer
        """
        return -2**31

    @classmethod
    def parse(cls, value: str) -> int:
        """Return the integer represented in value.

        :param value: a string to be parsed
        :raise: TypeError if value is not an integer, or if its type is not str
        :raise: OverflowError if value does not fit into a 32-bit signed integer
        :return: the integer in value
        """
        res = int(super().parse(value))
        if not(cls.min() <= res <= cls.max()):
            raise OverflowError(f"{value} will overflow")
        return res

@dataclass(frozen=True)
class String(Type):
    """
    Primitive type representing strings.

    This class cannot be instantiated.
    """

    def __init__(self):
        super().__init__()

    @classmethod
    def init_code(cls, arg: str) -> List[str]:
        return super().init_code(arg) + [
            f'if {arg}.type != clingo.SymbolType.String:',
            f'    raise TypeError(f"expecting clingo.SymbolType.String, but received {{{arg}}}")',
            f'self.{arg} = {arg}.string',
        ]

    @classmethod
    def parse(cls, value: str) -> str:
        """Return value, as any string is valid

        :param value: a string to be parsed
        :return: value
        """
        return super().parse(value)

@dataclass(frozen=True)
class Alpha(Type):
    """Primitive type representing named constants.

    This class cannot be instantiated.
    """

    def __init__(self):
        super().__init__()

    @classmethod
    def init_code(cls, arg: str) -> List[str]:
        return super().init_code(arg) + [
            f'if {arg}.type != clingo.SymbolType.Function:',
            f'    raise TypeError(f"expecting clingo.SymbolType.Function, but received {{{arg}}}")',
            f'if {arg}.arguments:',
            f'    raise TypeError(f"expecting function of arity 0, but it is {{{arg}.arguments}}")',
            f'self.{arg} = {arg}.name',
        ]

    @classmethod
    def parse(cls, value: str) -> str:
        """Return value, or raise an exception if value is not valid

        :param value: a string to be parsed
        :raise: ValueError if value is not a valid function name, or TypeError if the type of value is not str
        :return: value
        """
        value = super().parse(value)
        return PredicateName(value).value

@dataclass(frozen=True)
class Any(Type):
    """Primitive type representing wildcards.

        This class cannot be instantiated.
        """

    def __init__(self):
        super().__init__()

    @classmethod
    def init_code(cls, arg: str) -> List[str]:
        return super().init_code(arg) + [
            f'if {arg}.type == clingo.SymbolType.Number:',
            f'    self.{arg} = {arg}.number',
            f'elif {arg}.type == clingo.SymbolType.String:',
            f'    self.{arg} = {arg}.string',
            f'elif {arg}.type == clingo.SymbolType.Function:',
            f'    self.{arg} = {arg}',
            f'else:'
            f'    raise ValueError("expecting Number, String or Function, received {{{arg}.type}}")'
        ]

Type.set_primitives({
    Integer: Integer,
    int: Integer,
    clingo.Number: Integer,

    String: String,
    str: String,
    clingo.String: String,

    Alpha: Alpha,

    Any: Any,
    typing.Any: Any,
})


from itertools import combinations
from collections import Counter

def solution(orders, course):
    answer = []

    for c in course:
        c_list = []
        for o in orders:
            c_list += list(combinations(sorted(o), c))
        counter = Counter(c_list)
        if counter and counter.most_common()[0][1] >= 2:
            for con in counter.most_common():
                if con[1] == counter.most_common()[0][1]:
                    answer.append(''.join(con[0]))

    return sorted(answer)


from itertools import accumulate, starmap
from operator import gt, sub, add

def v(s):
    return 0 if s == ' ' else ord(s) - ord('a') + 1

def vi(u):
    return ' ' if not u else chr(ord('a') + u - 1)

def encrypt(xs):
    xs = map(v, xs)  # step 1
    xs = accumulate(xs)  # step 2
    xs = [x % 27 for x in xs]  # step 3
    return ''.join(map(vi, xs))  # step 4

def decrypt(xs):
    xs = list(map(v, xs))  # step 4 inverse
    ms = accumulate([gt(*t) * 27 for t in zip([0] + xs, xs)])  # step 3 inverse
    xs = list(starmap(add, zip(xs, ms)))
    xs = list(starmap(sub, zip(xs, [0] + xs)))  # step 2 inverse
    return ''.join(map(vi, xs))  # step 1 inverse

n = int(input())
for i in range(n):
    e, ss = input().split(' ', 1)

    print(encrypt(ss) if e == 'e' else decrypt(ss))


import requests
import xml.etree.ElementTree as ET

page = requests.get("https://medium.com/feed/{blogname}")
xml = ET.fromstring(page.content)

my_list = [item for item in xml[0] if item.tag == 'item'][:5]
tlp = ()
for item in my_list:
    dct = {}
    for child in item:
        if 'creator' in child.tag:
            dct['creator'] = child.text
        if 'encoded' in child.tag:
            dct['content'] = child.text
        else:
            dct[child.tag] = child.text
    tlp += (dct,)

for item in tlp:
    print(item['title'])
    print(item['creator'])
    print(item['pubDate'])
    print(item['link'])
    print(item['content'][:500],"...")
    print('\n----------\n')


# Study Module
import validation as v
import random as r
import deckmanager as d
import os
import csv

def getDeck():

    deck = []
    userFileRequest = d.selectDeck()

    # Loops while reading file
    readFile = open(userFileRequest + ".csv", "r")
    for line in readFile:

        #line = readFile.readline()
        # Strips carriage return
        line = line.rstrip("\n")
        line = line.rstrip("\r")

        # Splits line into a list called a card
        card = line.split(",")

        # Appends card to deck
        deck.append(card)

    # Close the file
    readFile.close()

    # Returns the card arrays
    return(deck, userFileRequest)

def study():

    attempts = 0
    deck = []
    card = []
    picker = 0
    currentCard = []
    outfile = ''
    userFileRequest = ""

    # Gets the deck of cards from the getDeck function
    deck, userFileRequest = getDeck()

    # User selects how many cards to study
    userCardNumber = v.getRangedInt("Please enter how many cards you would like to study: ",
                                    "Needs to be a number between one and thirty, please. ", 1, 30)
    # Checks if the deck is valid
    if len(deck) == 0:
        print("No cards in deck.")
    else:
        # Loops as many times as specified by input
        for i in range(userCardNumber):

            # Randomly picks a direction, either giving prompts and recieving answers or vice versa
            currentDirection = r.randint(0, 1)

            # Picks a random number from the cards
            picker = r.randint(0, len(deck) - 1)

            # Sets current card to random card
            currentCard = deck[picker]

            # Repeats as long as you don't have the right answer
            correct = False

            # Resets attempts tracker
            attempts = 0

            # Loops as long as the user hasn't answered correctly
            while correct == False:

                # Checks direction
                if currentDirection == 0:

                    # Gets an answer from user
                    userAnswer = v.getStringByLength(
                        "Prompt: " + str(currentCard[0]) + "\nAnswer: ",
                        "That's not even a word.. \n", 0, 30)

                    # Checks user answer against answer cards
                    if userAnswer == str(currentCard[1]):
                        print("Correct!\n")
                        correct = True
                    else:
                        print("Not quite, try again\n")
                        attempts += 1

                        # Hint displays when attempts is over 3
                        if attempts > 3:
                            print("\nHint: " + currentCard[1][:3])

                        # Answer displays when attempts is over 5
                        if attempts > 5:
                            print("\nThe answer was: " + currentCard[1] + ". Try again next time!")

                elif currentDirection == 1:

                    # Gets an answer from user
                    userAnswer = v.getStringByLength(
                        "Answer: " + str(currentCard[1]) + "\nPrompt: ",
                        "That's not even a word.. \n", 0, 30)

                    # Checks user answer against prompt cards as the direction is reversed
                    if userAnswer == str(currentCard[0]):
                        print("Correct!\n")
                        correct = True
                    else:
                        print("Not quite, try again\n")
                        attempts += 1

                        # Hint displays when attempts is over 3
                        if attempts > 3:
                            print("\nHint: " + currentCard[0][:3])

                        # Answer displays when attempts is over 5
                        if attempts > 5:
                            print("\nThe prompt was: " + currentCard[0] + ". Try again next time!")


import re
import csv
import emoji as e
from emoji.unicode_codes import UNICODE_EMOJI

# Replace "chat.txt" with your chat filename below
whatsapp_chat = 'chat.txt'

def build_line(date, hour, name, message, word_count, emoji_count, message_count):
    return {
        "date": date,
        "hour": hour,
        "name": name,
        "message": message,
        "word_count": word_count,
        "emoji_count": emoji_count,
        "message_count": message_count
    }

def import_file(filename):  # import txt file, cleanup and produce a list of lines that contain all message info
    file = open(filename, encoding="utf8")
    lines = []

    for line in file:
        line = re.sub(r'--image omitted', '', line)
        line = re.sub(r'--video omitted', '', line)
        line = re.sub(r'--audio omitted', '', line)
        line = re.sub(r'\u200e', '', line)
        if re.findall(r"\[(\d{2}/\d{2}/\d{4})", line):
            lines.append(line.strip())
        else:
            last_line = lines.pop()
            line = last_line.strip() + " " + line.strip()
            lines.append(line)
    return lines

def extract_date(line):
    date = re.findall(r"\[(\d{2})/(\d{2})/(\d{4})", line)
    YYYYMMDD = str(date[0][2]) + str(date[0][1]) + str(date[0][0])
    return YYYYMMDD

def extract_hour(line):
    hour = re.findall("\[\d{2}/\d{2}/\d{4},.(\d{2}):\d{2}:\d{2}\]", line)
    return str(hour[0])

def extract_name(line):
    name = re.findall(r".*?:.*?\]\s(.*?):", line)
    return str(name[0])

def extract_message(line):
    message = re.findall(r"\[\d{2}/\d{2}/\d{4}.*?:.*?\]\s.*?:(.*)", line)
    message = str(message[0]).strip()
    return message

def strip_emoji(text):
    no_emoji_text = ''
    for character in text:
        if character not in UNICODE_EMOJI:
            no_emoji_text = no_emoji_text + character
    return no_emoji_text

def export_all_lines_csv(input_txt_file, export_filename):

    file = import_file(input_txt_file)
    results = []
    for line in file:
        full_message = extract_message(line)
        emoji_count = e.emoji_count(full_message)
        emojiless_message = strip_emoji(full_message)
        if len(emojiless_message) > 0:
            word_count = len(emojiless_message.split())
        else:
            word_count = 0
        results.append(build_line(extract_date(line), extract_hour(line), extract_name(line), full_message, word_count, emoji_count, 1))
    fields_to_write_to_csv = ["date", "hour", "name", "message", "word_count", "emoji_count", "message_count"]
    with open(export_filename, mode='w') as csv_file:
        writer = csv.DictWriter(csv_file, fieldnames=fields_to_write_to_csv)

        writer.writeheader()
        for line in results:
            writer.writerow(line)

export_all_lines_csv(whatsapp_chat, 'parsed_chat_output.csv')


answer = []
maxmum = 10000
mm = [i for i in range(maxmum)]
for i in range(2,maxmum):
    if mm[i] == -1: continue
    else:
        answer.append(i)
        for j in range(i*2,maxmum,i):
            mm[j] = -1

arrs = [int(input()) for _ in range(int(input())) ]

for value in arrs:
    storage = []
    index = 0
    for i in answer:
        if( i >= value): break
        for j in answer[index:]:
            if i+j > value: break
            if i+j == value:
               storage.append([i,j])
               break
        index+=1
    print(*storage[-1])

백준/Silver/9020. 골드바흐의 추측/골드바흐의 추측.py