import functools

import click
from click import Context, HelpFormatter
from dbt.cli.options import MultiOption

# define common parameters
project_path = click.option(
    "--project-path",
    type=click.Path(exists=True),
    default=".",
    help="The path to the dbt project to operate on. Defaults to the current directory.",
)

project_paths = click.option(
    "--project-paths",
    cls=MultiOption,
    multiple=True,
    type=tuple,
    default=None,
    help="The paths to the set of dbt projects to connect. Must supply 2+ paths.",
)

projects_dir = click.option(
    "--projects-dir",
    type=click.Path(exists=True),
    default=None,
    help="The path to a directory containing multiple dbt projects. Directory must contain 2+ projects.",
)

exclude_projects = click.option(
    "--exclude-projects",
    "-e",
    cls=MultiOption,
    multiple=True,
    type=tuple,
    default=None,
    help="The set of dbt projects to exclude from the operation when using the --projects-dir option.",
)

create_path = click.option(
    "--create-path",
    type=click.Path(exists=False),
    default=None,
    help="The path to create the new dbt project. Defaults to the name argument supplied.",
)

exclude = click.option(
    "--exclude",
    "-e",
    cls=MultiOption,
    multiple=True,
    type=tuple,
    default=None,
    help="The dbt selection syntax specifying the resources to exclude in the operation",
)

group_yml_path = click.option(
    "--group-yml-path",
    type=click.Path(exists=False),
    help="An optional path to store the new group YAML definition.",
)

select = click.option(
    "--select",
    "-s",
    cls=MultiOption,
    multiple=True,
    type=tuple,
    default=None,
    help="The dbt selection syntax specifying the resources to include in the operation",
)

selector = click.option(
    "--selector",
    cls=MultiOption,
    multiple=True,
    type=tuple,
    default=None,
    help="The name(s) of the YML selector specifying the resources to include in the operation",
)

owner_name = click.option(
    "--owner-name",
    help="The group Owner's name.",
)

owner_email = click.option(
    "--owner-email",
    help="The group Owner's email address.",
)

owner_properties = click.option(
    "--owner-properties",
    help="Additional properties to assign to a group Owner.",
)

read_catalog = click.option(
    "--read-catalog",
    "-r",
    is_flag=True,
    envvar="DBT_MESHIFY_READ_CATALOG",
    help="Skips the dbt docs generate step and reads the local catalog.json file.",
)

latest = click.option(
    "--latest",
    "-l",
    is_flag=True,
    help="Makes the newest version the latest version when incrementing model versions",
)

def owner(func):
    """Add click options and argument validation for creating Owner objects."""

    @functools.wraps(func)
    def wrapper_decorator(*args, **kwargs):
        if kwargs.get('owner_name') is None and kwargs.get('owner_email') is None:
            raise click.UsageError(
                "Groups require an Owner to be defined using --owner-name and/or --owner-email."
            )
        return func(*args, **kwargs)

    return wrapper_decorator

class TupleCompatibleCommand(click.Command):
    """
    A Click Command with a custom formatter that adds metavar options after all arguments.
    This is valuable for commands that use tuple-type options, since type types will eat
    arguments.
    """

    def format_usage(self, ctx: Context, formatter: HelpFormatter) -> None:
        pieces = self.collect_usage_pieces(ctx)
        pieces = pieces[1:] + [pieces[0]]
        formatter.write_usage(ctx.command_path, " ".join(pieces))


import numpy as np
import pickle
import pandas as pd
import streamlit as st

from PIL import Image

pickle_in = open("classifier.pkl","rb")
classifier=pickle.load(pickle_in)

def welcome():
    return "Welcome All"

def predict_impact(opened_by, location, ID_caller, Category_id, ID):
    """Let's find the prediction
    This is using docstrings for specifications.
    ---
    parameters:
      - name: opened_by
        in: query
        type: number
        required: true
      - name: location
        in: query
        type: number
        required: true
      - name: ID_caller
        in: query
        type: number
        required: true
      - name: Category_id
        in: query
        type: number
        required: true
      -name: ID
        in: query
        type: number
        required: true
    responses:
        200:
            description: The output values

    """
    prediction = classifier.predict([[opened_by, location, ID_caller, Category_id, ID]])
    print(prediction)
    return prediction

def main():

    html_temp ="""

Incident Impact Prediction ML App

    """
    st.markdown(html_temp, unsafe_allow_html=True)

    opened_by= st.text_input("Opened_By","Type Here")
    location = st.text_input("Location","Type Here")
    ID_caller = st.text_input("ID_Caller","Type Here")
    Category_id = st.text_input("Category_ID","Type Here")
    ID = st.text_input("ID","Type Here")
    result=""
    if st.button("Predict"):
        result=predict_impact(opened_by,location,ID_caller,Category_id,ID)
    st.success('The output is {}'.format(result))

if __name__ == '__main__':
    main()


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
import xlwt

option = Options()
option.add_argument('--headless')

chrome_driver = 'E:\chromedriver.exe'
driver = webdriver.Chrome(chrome_options=option,executable_path=chrome_driver)

driver.get('https://www.jd.com/?cu=true&utm_source=baidu-pinzhuan&utm_medium=cpc&utm_campaign=t_288551095_baidupinzhuan&utm_term=0f3d30c8dba7459bb52f2eb5eba8ac7d_0_0097a72d863b49999dfb8264d866c91c')

kw = driver.find_element_by_id('key')
print(kw.get_attribute('class'))
kw.send_keys('pythonjava')

kw.send_keys(Keys.RETURN)

time.sleep(1)

sele = driver.find_element_by_xpath('//div[@class="f-sort"]/a[2]')
sele.click()
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet('sheet1',cell_overwrite_ok=True)
c = 0
while 1:

    time.sleep(1)
    driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
    time.sleep(1)

    book = driver.find_elements_by_xpath('//li[@class="gl-item"]')

    time.sleep(1)

    for i in book:

        dic = {}
        name = i.find_element_by_xpath('.//div[@class="p-name"]//em').text
        price = i.find_element_by_xpath('.//div[@class="p-price"]//i').text
        dic[name] = price

        print(dic)

        sheet.write(c,0,name)
        sheet.write(c,1,price)
        workbook.save('book.xls')
        c += 1

    try:
        next_page = driver.find_element_by_xpath('//div[@id="J_bottomPage"]//a[@class="pn-next"]')
        next_page.click()
    except:
        break

driver.quit()


from typing import (
    Optional,
    List
)

from qgis.PyQt.QtCore import (
    QObject,
    pyqtSignal
)

from qgis.core import QgsMapLayer

from kart.utils import setting, setSetting

from ..kartapi import (
    Repository,
    KartException
)

class RepoManager(QObject):
    """
    Manages the local repositories
    """

    _instance: Optional['RepoManager'] = None

    repo_added = pyqtSignal(Repository)
    repo_removed = pyqtSignal(Repository)

    @classmethod
    def instance(cls) -> 'RepoManager':
        """
        Returns the repo manager instance
        """
        if not RepoManager._instance:
            RepoManager._instance = RepoManager()

        return RepoManager._instance

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

        self._repos: List[Repository] = []

        self.read_repos_from_settings()

    def read_repos_from_settings(self):
        """
        Reads repos from user settings
        """
        s = setting("repos")
        if s is None:
            self._repos = []
        else:
            self._repos = []
            paths = s.split("|")
            for path in paths:
                repo = Repository(path)
                if repo.isInitialized():
                    self._repos.append(repo)
                    self.repo_added.emit(repo)

    def save_repos_to_settings(self):
        """
        Saves repos to user settings
        """
        s = "|".join([repo.path for repo in self._repos])
        setSetting("repos", s)

    def add_repo(self, repo: Repository):
        """
        Adds a repository to the manager
        """
        self._repos.append(repo)
        self.save_repos_to_settings()
        self.repo_added.emit(repo)

    def remove_repo(self, repo: Repository):
        """
        Removes a repository from the manager
        """
        for r in self._repos:
            if r.path == repo.path:
                self._repos.remove(r)
                break
        self.save_repos_to_settings()
        self.repo_removed.emit(repo)

    def repos(self) -> List[Repository]:
        """
        Returns the list of known repositories
        """
        return self._repos

    def repo_for_layer(self, layer: QgsMapLayer) -> Optional[Repository]:
        """
        Returns the repo matching a layer, or None if not found
        """
        try:
            for repo in self._repos:
                if repo.layerBelongsToRepo(layer):
                    return repo
        except KartException:
            pass

        return None


from django.core.validators import MaxValueValidator
from django.core.validators import MinValueValidator
from django.db import models
from django.utils import timezone

from phonenumber_field.modelfields import PhoneNumberField

from location.models import Location

class OrderQuerySet(models.QuerySet):
    """Order model custom queryset."""

    def calculate_order_amount(self):
        """Calculates the total amount of the order."""
        return self.annotate(
            order_amount=models.Sum(
                models.F('order_products__cost'),
                output_field=models.DecimalField(),
            ),
        )

    def only_unprocessed(self):
        """Returns only unprocessed orders."""
        return self.calculate_order_amount().exclude(status='processed')

    def with_coords_from_cache(self):
        """Get the coordinates from the location table."""
        location = Location.objects.filter(address=models.OuterRef('address'))
        return self.annotate(
            lat=models.Subquery(
                queryset=location.values('lat'),
                output_field=models.FloatField(),
            ),
            lon=models.Subquery(
                queryset=location.values('lon'),
                output_field=models.FloatField(),
            ),
        )

class Order(models.Model):
    """Order model."""

    PROCESSED = 'processed'
    UNPROCESSED = 'unprocessed'
    ORDER_STATUS_CHOICES = [
        (PROCESSED, 'Обработанный'),
        (UNPROCESSED, 'Необработанный'),
    ]

    CASH = 'cash'
    CARD = 'card'
    NOT_SELECTED = 'not_selected'
    PAYMENT_METHOD_CHOICES = [
        (CASH, 'Наличные'),
        (CARD, 'Карта'),
        (NOT_SELECTED, 'Не выбрано'),
    ]

    firstname = models.CharField('Имя', max_length=20)
    lastname = models.CharField('Фамилия', max_length=20)
    phonenumber = PhoneNumberField('Номер телефона', max_length=15, db_index=True)
    address = models.CharField('Адрес доставки', max_length=200, db_index=True)
    status = models.CharField(
        'Статус',
        choices=ORDER_STATUS_CHOICES,
        default=UNPROCESSED,
        max_length=11,
        db_index=True,
    )
    comment = models.TextField('Комментарий', max_length=200, blank=True)
    registrated_at = models.DateTimeField('Дата регистрации', default=timezone.now, db_index=True)
    called_at = models.DateTimeField('Дата звонка', null=True, blank=True, db_index=True)
    delivered_at = models.DateTimeField('Дата доставки', null=True, blank=True, db_index=True)
    payment_method = models.CharField(
        'Способ оплаты',
        choices=PAYMENT_METHOD_CHOICES,
        default=NOT_SELECTED,
        max_length=15,
        db_index=True,
    )

    restaurant = models.ForeignKey(
        'Restaurant',
        on_delete=models.SET_NULL,
        verbose_name='Забрать в ресторане',
        related_name='orders',
        null=True,
        blank=True,
    )

    objects = OrderQuerySet.as_manager()

    class Meta:
        verbose_name = 'заказ'
        verbose_name_plural = 'заказы'

    def __str__(self):
        return f'{self.firstname} {self.lastname}'

class OrderProduct(models.Model):
    """Order product model."""

    order = models.ForeignKey(
        Order,
        on_delete=models.CASCADE,
        verbose_name='Заказ',
        related_name='order_products',
    )

    product = models.ForeignKey(
        'Product',
        on_delete=models.CASCADE,
        verbose_name='Продукт',
        related_name='order_products',
    )

    quantity = models.PositiveIntegerField('Количество', validators=[MaxValueValidator(50)])
    cost = models.DecimalField(
        'Стоимость',
        max_digits=8,
        decimal_places=2,
        validators=[MinValueValidator(0)],
    )

    class Meta:
        verbose_name = 'товар в заказе'
        verbose_name_plural = 'товары в заказе'

    def __str__(self):
        return f'{self.product.name} {self.quantity}'


import os
from flask import Flask, request, abort, jsonify
import face_recognition
import json
from werkzeug.utils import secure_filename
import numpy as np

app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.debug = True

face_encodes = []
data_dir = "./data"

# You can change this to any folder on your system
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

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

    if "file" not in request.files:
        return json.dumps({"code": "1", "message": "no file"})

    file = request.files["file"]
    if file.filename == '':
        return json.dumps({"code": "2", "message": "no filename"})

    if not file or not allowed_file(file.filename):
        return json.dumps({"code": "3", "message": "no support image"})

    filename = secure_filename(file.filename)
    filename = os.path.join(data_dir, filename)
    file.save(filename)
    encs = get_face_encoding(filename)
    os.remove(filename)
    if len(encs) == 0:
        return json.dumps({"code": "4", "message": "no face"})

    if len(face_encodes) == 0:
        return json.dumps({"code": "5", "message": "no add face"})
    # See how far apart the test image is from the known faces
    face_distances = face_recognition.face_distance(np.array(face_encodes), encs[0])
    for i, face_distance in enumerate(face_distances):
        if face_distance < 0.5:
            return json.dumps({"code": "0", "message": str(face_distance)})

    return json.dumps({"code": "6", "message": "auth failed"})

@app.route('/v1/add-face-encodings', methods=['POST'])
def post_face_encodings():

    if "file" not in request.files:
        return json.dumps({"code": "1", "message": "no file"})

    file = request.files["file"]
    if file.filename == '':
        return json.dumps({"code": "2", "message": "no filename"})

    if not file or not allowed_file(file.filename):
        return json.dumps({"code": "3", "message": "no support image"})

    filename = secure_filename(file.filename)
    filename = os.path.join(data_dir, filename)
    file.save(filename)
    encs = get_face_encoding(filename)
    os.remove(filename)
    if len(encs) == 0:
        return json.dumps({"code": "4", "message": "no face"})
    for enc in encs:
        face_encodes.append(enc)
    return json.dumps({"code": "0", "message": ""})

def get_face_encoding(file):
    image = face_recognition.load_image_file(file)
    encoding = face_recognition.face_encodings(image)
    return encoding

if __name__ == '__main__':
    if not os.path.exists(data_dir):
        os.makedirs(data_dir)
    app.run(debug=True, host='0.0.0.0')


import torch
import math
import numpy as np

#########################
### Design Parameters ###
#########################
n = 100 # amount of antenna elements
m = 6  # dimension of our state space

m1x_0 = torch.ones(m, 1)
m1x_0 = torch.transpose(torch.tensor([[2.5, -9.1, 1.5, 0.01, 0.97, 0]], dtype=torch.float), 0, 1)
P_0 = m1x_0 + torch.transpose(torch.tensor([np.random.normal(0, [0.5**2, 0.5**2, 0.01**2, 0.01**2/10, 0.97**2/10, 0])], dtype=torch.float), 0, 1)
m2x_0 = torch.zeros(m, m)
#P_0 = P_0[None, :]

T = 20  # Amount of timesteps
T_test = 20
lmb = 0.010714285714286  # Wavelength
Dmin = lmb/(2*math.pi)

########################
### Model Parameters ###
########################
tau = 1  # temporal step

A = torch.eye(6)
A[0:3, 3:6] = torch.mul(torch.eye(3), tau)  # State Transition Matrix

B = torch.zeros(3, m)
B[0:3, 0:3] = torch.eye(3)

# Noise Parameters
Qx = torch.tensor([[0.03**2,  0, 0], [0, 0.03**2, 0], [0,  0, 0]])
sigma_r_mod = 0.349065850398866  # noise standard deviation in radians (corresponds to 20°)

# Noise Matrices
Q_aux = torch.block_diag(torch.mul(Qx, pow(tau, 2)/2), torch.mul(Qx, pow(tau, 2)/2))  # auxiliary matrix to define transition covariance matrix
Q_mod = torch.block_diag(torch.mul(Qx, pow(tau, 3)/3), torch.mul(Qx, tau))
Q_mod[3:6, 0:3] = Q_aux[0:3, 0:3]
Q_mod[0:3, 3:6] = Q_aux[0:3, 0:3]  # Finalized transition covariance matrix

R_mod = sigma_r_mod * torch.eye(n)  # Observation covariance matrix

#########################
### Antenna positions ###
#########################

qn = torch.zeros(n, 3)  # Antenna positions
dqn = 0.005357142857143  # distance between individual array elements
sqrtn = math.sqrt(n)
for i in range(n):
    qn[i][2] = dqn * (i % sqrtn)
    qn[i][1] = dqn * math.trunc(i / sqrtn)

Dmax = 2*(19*dqn*math.sqrt(2)/2)**2/lmb  # Maximum Near-Field distance ("sqrtn-1" changed to 19 to generate area of points similar to other paper
q0 = torch.tensor([0, (math.sqrt(n)-1)/2*dqn, (math.sqrt(n)-1)/2*dqn])  # Reference position


import argparse
import os
import re
import subprocess
import sys

import common

# ========= COMMAND LINE ARGUMENTS ========

def parse_arguments():
    parser = argparse.ArgumentParser(description="Create Qt Creator development package.")
    parser.add_argument('--source', '-s', help='path to the Qt Creator sources', required=True,
        metavar='<path>')
    parser.add_argument('--build', '-b', help='path to the Qt Creator build', required=True,
        metavar='<path>')
    parser.add_argument('--verbose', '-v', help='verbose output', action='store_true', default=False)
    parser.add_argument('--7z', help='path to 7z binary',
        default='7z.exe' if common.is_windows_platform() else '7z',
        metavar='<7z_binary>', dest='sevenzip')
    parser.add_argument('--7z_out', '-o', help='output 7z file to create', metavar='<filename>',
        dest='sevenzip_target')
    parser.add_argument('target_directory')
    return parser.parse_args()

# ========= PATTERNS TO INCLUDE ===========

# slash at the end matches directories, no slash at the end matches files

source_include_patterns = [
    # directories
    r"^(?!(share|tests)/.*$)(.*/)?$",                     # look into all directories except under share/ and tests/
    r"^share/(qtcreator/(qml/(qmlpuppet/(.*/)?)?)?)?$", # for shared headers for qt quick designer plugins
    # files
    r"^HACKING$",
    r"^LICENSE.*$",
    r"^README.md$",
    r"^scripts/.*$", # include everything under scripts/
    r"^doc/.*$",     # include everything under doc/
    r"^.*\.pri$",    # .pri files in all directories that are looked into
    r"^.*\.h$",      # .h files in all directories that are looked into
    r"^.*\.hpp$"     # .hpp files in all directories that are looked into
]

build_include_patterns = [
    # directories
    r"^src/$",
    r"^src/app/$",
    # files
    r"^src/app/app_version.h$"
]
if common.is_windows_platform():
    build_include_patterns.extend([
        r"^lib/(.*/)?$", # consider all directories under lib/
        r"^lib/.*\.lib$"
    ])

# =========================================

def copy_regexp(regexp, source_directory, target_directory, verbose):
    def ignore(directory, filenames):
        relative_dir = os.path.relpath(directory, source_directory)
        file_target_dir = os.path.join(target_directory, relative_dir)
        ignored = []
        for filename in filenames:
            relative_file_path = os.path.normpath(os.path.join(relative_dir, filename))
            relative_file_path = relative_file_path.replace(os.sep, '/')
            if os.path.isdir(os.path.join(directory, filename)):
                relative_file_path += '/' # let directories end with slash
            if not regexp.match(relative_file_path):
                ignored.append(filename)
            elif verbose and os.path.isfile(os.path.join(directory, filename)):
                print(os.path.normpath(os.path.join(file_target_dir, filename)))
        return ignored

    common.copytree(source_directory, target_directory, symlinks=True, ignore=ignore)

def sanity_check_arguments(arguments):
    if os.path.exists(arguments.target_directory) and (os.path.isfile(arguments.target_directory)
                                                       or len(os.listdir(arguments.target_directory)) > 0):
        print('error: target directory "{0}" exists and is not empty'.format(arguments.target_directory))
        return False
    return True

def main():
    arguments = parse_arguments()
    if not sanity_check_arguments(arguments):
        sys.exit(1)

    source_include_regexp = re.compile('(' + ')|('.join(source_include_patterns) + ')')
    build_include_regexp = re.compile('(' + ')|('.join(build_include_patterns) + ')')

    copy_regexp(source_include_regexp, arguments.source, arguments.target_directory, arguments.verbose)
    copy_regexp(build_include_regexp, arguments.build, arguments.target_directory, arguments.verbose)

    if arguments.sevenzip_target:
        subprocess.check_call([arguments.sevenzip, 'a', '-mx9', arguments.sevenzip_target,
            os.path.join(arguments.target_directory, '*')])

if __name__ == "__main__":
    main()


from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("natwit", views.natwit, name="natwit"),
    path("yodyiam", views.yodyiam, name="yodyiam"),
    path("<str:name>", views.greet, name="greet")

]


#!/bin/python3
import os
import getopt
import yaml
import math
import sys

JD_ID = 00.00
META = False

def parseArgs():
    global META

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hf:p:m",
                ["help", "file=", "path="])
    except:
        sys.exit(2)

    source_file = "../res/jd.yaml"
    dest_path = "./"

    for opt, arg in opts:
        if opt in ("-h","--help"):
            sys.exit(2)
        elif opt in ("-f", "--file"):
            source_file = arg
        elif opt in ("-p", "--path"):
            dest_path = arg
        elif opt in ("-m", "--meta"):
            META = True
        else:
            assert False, "unknown option"

    return source_file, dest_path

def generateLayerPrefix(jd_id, level):
    if level == 1:
        result = "{:02} - {:02}".format(math.floor(jd_id),math.floor(jd_id + 9))
    elif level == 2:
        result = "{:02}".format(round(jd_id))
    else:
        result = "{:05.2f}".format(jd_id)
    return result

def generateFileSystem(jd_data, dest_path):
    global JD_ID
    global META

    os.chdir(dest_path)

    for upper in jd_data:

        if not META:
            JD_ID += 10

        current_upper_macro = JD_ID
        upper_name = "{} {}".format(generateLayerPrefix(JD_ID,1), upper)

        os.mkdir(upper_name)
        os.chdir(upper_name)

        for middle in jd_data[upper]:

            JD_ID += 1
            current_middle_macro = JD_ID
            middle_name = "{} {}".format(generateLayerPrefix(JD_ID,2), middle)

            os.mkdir(middle_name)
            os.chdir(middle_name)

            for end in jd_data[upper][middle]:
                JD_ID += .01
                end_name = "{} {}".format(generateLayerPrefix(JD_ID,3), end)

                os.mkdir(end_name)

            os.chdir("..")
            JD_ID = current_middle_macro

        os.chdir("..")
        JD_ID = current_upper_macro

        if META:
            JD_ID += 10

if __name__ == '__main__':

    source_file, dest_path = parseArgs()
    print(META)
    source_stream = open(source_file,'r')
    jd_data = yaml.load(source_stream,Loader=yaml.Loader)

    generateFileSystem(jd_data, dest_path)


from rest_framework import serializers
from rest_framework.validators import UniqueValidator

from .models import Account, Address

def validate_address(address_to_check):

    """This function will do a simple validation to check
    if the address is already in the DB."""

    qs = Address.objects.filter(
        building_name_or_number__iexact = address_to_check["building_name_or_number"],
        address_line_1__iexact = address_to_check["address_line_1"],
        address_line_2__iexact = address_to_check["address_line_2"],
        city__iexact = address_to_check["city"],
        county_state__iexact = address_to_check["county_state"],
        post_zip_code__iexact = address_to_check["post_zip_code"],
        country__iexact = address_to_check["country"],
        ).values("address_line_1")

    try:
        qs[0]
    except IndexError:
        return address_to_check
    else:
        raise serializers.ValidationError(f"The address specified already exists")

unique_account_name = UniqueValidator(queryset = Account.objects.all(),
                                      lookup = 'iexact')

def validate_account_present(account_to_check: int):

    try:
        query = Account.objects.get(id = account_to_check)

    except Account.DoesNotExist:
        raise serializers.ValidationError(
            code = "error",
            detail = {"detail": f"The account with id {account_to_check} does not exist"}
            )

    else:
        return query

def validate_address_present(address_to_check: int):

    try:
        query = Address.objects.get(id = address_to_check)

    except Address.DoesNotExist:
        raise serializers.ValidationError(
            code = "error",
            detail = {"detail": f"The address with id {address_to_check} does not exist"}
            )

    else:
        return query


import unittest
import mongomock
import uuid
from datetime import datetime
from database.crud import MongoTransactionCRUD, MongoAccountCRUD
from schemas import Transaction, CurrencyAmount, Account, BankLink, InstitutionInfo

mock_transactions = [
    Transaction(
        id=str(uuid.uuid4()),
        origin="origin",
        amount=CurrencyAmount(currency="EUR", amount=10.0),
        booking_date="2023-09-28T00:00:00",
        type="expense",
        category="unknown",
        account_balance=10.0,
    )
]

mock_account_id = str(uuid.uuid4())
mock_bank_link = BankLink(
    client="Nordigen",
    requisition_id="req",
    institution=InstitutionInfo(name="N26"),
    link="link",
)
mock_account_obj = Account(id=mock_account_id, name="Account")

class TestTransactionCrud(unittest.TestCase):
    def setUp(self):
        self.mongo_client = mongomock.MongoClient().db.collection
        self.transaction_crud = MongoTransactionCRUD(self.mongo_client)
        self.account_crud = MongoAccountCRUD(self.mongo_client)
        self.account_crud.add("user", mock_bank_link, mock_account_obj)

    def test_transaction_add(self):
        self.transaction_crud.add(mock_account_id, mock_transactions)

        transactions = list(self.mongo_client["transactions"].find({}))
        self.assertNotEqual(transactions, [])
        self.assertEqual(transactions[0]["origin"], "origin")

    def test_transaction_update(self):
        updated_transaction = mock_transactions[0].model_copy()
        updated_transaction.category = "groceries"

        self.transaction_crud.add(mock_account_id, mock_transactions)
        self.transaction_crud.update(mock_account_id, updated_transaction)
        transactions = list(self.mongo_client["transactions"].find({}))

        self.assertEqual(transactions[0]["category"], "groceries")

    def test_find_by_account(self):
        self.transaction_crud.add(mock_account_id, mock_transactions)

        account_transactions = self.transaction_crud.find_by_account(mock_account_id)
        self.assertEqual(len(account_transactions), 1)

        account_transactions = self.transaction_crud.find_by_account("non_existing")
        self.assertEqual(len(account_transactions), 0)

if __name__ == "__main__":
    unittest.main()


from django.shortcuts import render, redirect
from django.template.context_processors import csrf
from django.conf import settings
from django.http import HttpResponse
from upload_form.models import FileNameModel
import sys, os
UPLOADE_DIR = os.path.dirname(os.path.abspath(__file__)) + '/vowel_cut/input_file/'
DOWNLOADE_DIR = os.path.dirname(os.path.abspath(__file__)) + '/vowel_cut/wav_output/'

import glob
import subprocess

from io import BytesIO
import zipfile

from upload_form.vowel_cut import vowel_cut_main

def form(request):
    if request.method != 'POST':
        return render(request, 'upload_form/form.html')

    vowel = request.POST.get('vowel')

    # 前回のファイル削除
    file_list = glob.glob(UPLOADE_DIR+'*')
    for file_name in file_list:
        subprocess.call(["rm", file_name])

    file_list = glob.glob(DOWNLOADE_DIR+'*')
    for file_name in file_list:
        subprocess.call(["rm", file_name])

    # アップロードファイルの取得
    files = request.FILES.getlist("file[]")
    for file in files:
        path = os.path.join(UPLOADE_DIR, file.name)
        destination = open(path, 'wb')

        for chunk in file.chunks():
            destination.write(chunk)

        insert_data = FileNameModel(file_name = file.name)
        insert_data.save()

    # 母音抽出及び、ファイルチェック
    common = vowel_cut_main.main(vowel)

    flag = 0
    if common == "No_number_of_files":
        flag = 1
        comment = "拡張子が「.wav」と「.txt」の2種類のみにしてください"
    if common == "No_extention":
        flag = 1
        comment = "拡張子が違います\n拡張子が「.wav」と「.txt」の2種類のみにしてください"
    if common == "No_match_filename":
        flag = 1
        comment = "ファイル名を一致させてください"
    if common == "No_Fs_check":
        flag = 1
        comment = "サンプリング周波数を16000Hzにしてください"

    if flag != 0:
        content = {
            'comment':comment
        }
        return render(request, 'upload_form/form.html',content)

    content = {
        'vowel':vowel,
        'common':common
    }

    return render(request, 'upload_form/complete.html',content)

def download(request):
    vowel = request.POST.get('vowel')
    common = request.POST.get('common')

    memory_file = BytesIO()
    phoneme_zip = zipfile.ZipFile(memory_file, 'w')

    file_list = glob.glob(DOWNLOADE_DIR+'*')

    for file_part in file_list:
        file_name = file_part.replace(DOWNLOADE_DIR,"")
        phoneme_zip.writestr(file_name , open(file_part ,'rb').read())

    phoneme_zip.close()
    response = HttpResponse(memory_file.getvalue(), content_type="application/zip")
    response['Content-Disposition'] = 'attachment; filename={}_cut.zip'.format(common)
    return response


import json
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from django.shortcuts import get_object_or_404, get_list_or_404
from django.http import HttpResponse, Http404, HttpResponseRedirect, JsonResponse
from .models import Game
from .consumers import GamesConsumer, PlayConsumer

# this file is for 'rester' models that return json results
# I could probably try the rester framework, but too many things to learn, and text/json is the same thing :)

@login_required
def create_game(request):
    ''' create a game, return game_id '''
    new_game = Game(player1 = request.user.userplayer)
    new_game.save()
    id = new_game.pk
    response = json.dumps({ 'game_id' : id })
    GamesConsumer.send_available_update()
    return HttpResponse(response, content_type = 'text/json')

@login_required
def join_game(request, pk):
    ''' join a game, return true/false '''
    game = get_object_or_404(Game, pk = pk)
    response = game.join_up(request.user.userplayer)
    if response:
        GamesConsumer.send_available_update()
        GamesConsumer.send_running_update()
        PlayConsumer.send_play_update(game)
    response = json.dumps(response)
    return HttpResponse(response, content_type = 'text/json')

@login_required
def make_move(request, pk, column):
    ''' makes a move for the current player and updates the game '''
    game = get_object_or_404(Game, pk = pk)
    response = game.make_move(request.user.userplayer, column)
    if response:
        PlayConsumer.send_play_update(game)
        if game.status == Game.Status.FINISHED.value:
            GamesConsumer.send_running_update()
            GamesConsumer.send_user_update(game.player1)
            GamesConsumer.send_user_update(game.player2)
    response = json.dumps(response)
    return HttpResponse(response, content_type = 'text/json')


import telebot
from PIL import Image
import pytesseract
import keyboards
from skimage.filters import threshold_local
import numpy as np
import cv2
import imutils
import os

def order_points(pts):
	rect = np.zeros((4, 2), dtype = "float32")

	s = pts.sum(axis = 1)
	rect[0] = pts[np.argmin(s)]
	rect[2] = pts[np.argmax(s)]

	diff = np.diff(pts, axis = 1)
	rect[1] = pts[np.argmin(diff)]
	rect[3] = pts[np.argmax(diff)]

	return rect

def four_point_transform(image, pts):
	rect = order_points(pts)
	(tl, tr, br, bl) = rect

	widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
	widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
	maxWidth = max(int(widthA), int(widthB))

	heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
	heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
	maxHeight = max(int(heightA), int(heightB))

	dst = np.array([
		[0, 0],
		[maxWidth - 1, 0],
		[maxWidth - 1, maxHeight - 1],
		[0, maxHeight - 1]], dtype = "float32")

	M = cv2.getPerspectiveTransform(rect, dst)
	warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))

	return warped

def proccesing(image):

    ratio = image.shape[0] / 500.0
    orig = image.copy()
    image = imutils.resize(image, height=500)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 75, 200)

    print("STEP 1: Edge Detection")

    cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]

    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)

        if len(approx) == 4:
            screenCnt = approx
            break

    print("STEP 2: Find contours of paper")
    cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)

    warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)

    warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
    T = threshold_local(warped, 11, offset=10, method="gaussian")
    warped = (warped > T).astype("uint8") * 255

    scanned = imutils.resize(warped, height=650)

    print("STEP 3: Apply perspective transform")

    filename = "{}.png".format(os.getpid())
    cv2.imwrite(filename, warped)

    text = pytesseract.image_to_string(Image.open(filename))
    #os.remove(filename)
    print(text)

    return text


from itertools import permutations

def operation_sum(path):
	op_sum = 1
	row = 1
	for char in path:
		if char == 'R':
			row_value = row
			op_sum += row_value
		elif char == 'D':
			row += 1
			row_value = row
			op_sum += row_value
	return op_sum

def possible_paths(m,n,sval):
	init_path = 'R'*(m-1) + 'D'*(n-1)
	all_paths = [''.join(char) for char in set(permutations(init_path))]
	for path in all_paths:
		operation_sum(path)
		if (operation_sum(path) == sval):
			print(sval, " ", path)

possible_paths(7,7,50)
#possible_paths(9,9,90)
#possible_paths(9,9,110)


'''
usage.py
'''

import os
from toy_component import ToyComponent
import flask
import dash
import dash_html_components as html

app = dash.Dash('')

app.scripts.config.serve_locally = True

#

#
Demo: toy-component

#     <ToyComponent
#         className="toy"
#         label="This is an example label"
#         value={this.state.value}
#         setProps={newProps => this.setState({value: newProps.value})}
#     />
#

app.layout = html.Div([
    html.H1(
        ['Demo: toy-component']
    ),

    ToyComponent(
        id='toy-input',
        value='',
        label='This is an example label'
    ),

    html.Div(
        [],
        id='output',
        className='output'
    )
], className='container')

@app.callback(
    dash.dependencies.Output('output', 'children'),
    [dash.dependencies.Input('toy-input', 'value')])
def display_output(value):
    '''
    Display the output
    '''
    return 'You have entered: {}'.format(value)

@app.server.route('/demo/<path:path>')
def static_file(path):
    '''
    Serve up local static files using flask
    '''
    static_folder = os.path.join(os.getcwd(), 'demo')
    return flask.send_from_directory(static_folder, path)

app.css.append_css({"external_url": "/demo/style.css"})

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


import json
import gzip
import logging

import requests
from prometheus_client.core import REGISTRY, GaugeMetricFamily
from prometheus_client import make_wsgi_app, start_wsgi_server
from wsgiref.simple_server import make_server

ESI_URL = "https://esi.evetech.net"
DATASOURCE = "tranquility"

logger = logging.getLogger('eve-exporter')
logger.debug('logger initialized')

class CustomCollector(object):
    """Promtheus Custom Collector"""

    def __init__(self) -> None:

        self.session = requests.Session()
        self.system_names = load_systems()

    def collect(self):
        """Creates all metrics for prometheus"""
        yield self.collect_players()
        yield self.get_system_kills()
        yield self.get_system_jumps()

    def collect_players(self):
        """Get the number of players"""
        request = self.call_esi("/v2/status/")
        count = request['players']
        return GaugeMetricFamily('players', 'Number of eve online players', value=count)

    def get_system_kills(self):
        """Get All eveonline system kills"""
        system_kills = GaugeMetricFamily('system_kills', "Number of kills in a system",
                                        labels=['system_id', 'system_name', 'region', 'constellation', 'security', 'type'])

        systems = self.call_esi("/v2/universe/system_kills")

        for system in systems:
            system_info = self.system_names[str(system['system_id'])]
            tags = [
                str(system['system_id']),
                system_info['name'],
                system_info['region'],
                system_info['constellation'],
                str(system_info['security']),
            ]
            system_kills.add_metric(tags + ['npc_kills'], system['npc_kills'])
            system_kills.add_metric(tags + ['pod_kills'], system['pod_kills'])
            system_kills.add_metric(tags + ['ship_kills'], system['ship_kills'])

        return system_kills

    def get_system_jumps(self):
        """Get All eveonline system kills"""
        system_jumps = GaugeMetricFamily('system_jumps', "Number of ship jumps in a system",
                                        labels=['system_id'])
        systems = self.call_esi("/latest/universe/system_jumps")
        for system in systems:
            system_jumps.add_metric(
                [str(system['system_id'])], system['ship_jumps'])

        return system_jumps

    def call_esi(self, endpoint: str):
        """Make a ESI API Call"""
        params = {
            "datasource": DATASOURCE
        }

        try:
            response = self.session.get(
                f"{ESI_URL}{endpoint}", params=params, timeout=30)
            response.raise_for_status()
        except (requests.ConnectionError, requests.ConnectTimeout, requests.ReadTimeout) as ex:
            logger.error("An connection error occured: %s", ex)
            return None
        except requests.HTTPError as ex:
            logger.error("An HTTP error occured: %s", ex)
            return None

        return response.json()

def load_systems():
    """load systems from file"""
    with gzip.open("./data/systems.json.gz", "rb") as stream:
        return json.load(stream)

if __name__ == "__main__":
    REGISTRY.register(CustomCollector())
    app = make_wsgi_app()
    httpd = make_server('', 8000, app)
    httpd.serve_forever()
    start_wsgi_server(8000)


from os.path import normpath
from pprint import pformat

from django.conf import settings
from django.core.signals import request_started
from django.dispatch import Signal
from django.template.context import get_standard_processors
from django.template.loader import render_to_string
from django.test.signals import template_rendered
from debug_toolbar.panels import DebugPanel

# Code taken and adapted from Simon Willison and Django Snippets:
# http://www.djangosnippets.org/snippets/766/

# Monkeypatch instrumented test renderer from django.test.utils - we could use
# django.test.utils.setup_test_environment for this but that would also set up
# e-mail interception, which we don't want
from django.test.utils import instrumented_test_render
from django.template import Template
if Template.render != instrumented_test_render:
    Template.original_render = Template.render
    Template.render = instrumented_test_render
# MONSTER monkey-patch
old_template_init = Template.__init__
def new_template_init(self, template_string, origin=None, name='<Unknown Template>'):
    old_template_init(self, template_string, origin, name)
    self.origin = origin
Template.__init__ = new_template_init

class TemplateDebugPanel(DebugPanel):
    """
    A panel that lists all templates used during processing of a response.
    """
    name = 'Template'
    has_content = True

    def __init__(self):
        self.templates = []
        template_rendered.connect(self._storeTemplateInfo)

    def _storeTemplateInfo(self, sender, **kwargs):
        self.templates.append(kwargs)

    def title(self):
        return 'Templates'

    def url(self):
        return ''

    def process_request(self, request):
        self.request = request

    def content(self):
        context_processors = dict(
            [
                ("%s.%s" % (k.__module__, k.__name__),
                    pformat(k(self.request))) for k in get_standard_processors()
            ]
        )
        template_context = []
        for i, d in enumerate(self.templates):
            info = {}
            # Clean up some info about templates
            t = d.get('template', None)
            # Skip templates that we are generating through the debug toolbar.
            if t.name.startswith('debug_toolbar/'):
                continue
            if t.origin and t.origin.name:
                t.origin_name = t.origin.name
            else:
                t.origin_name = 'No origin'
            info['template'] = t
            # Clean up context for better readability
            c = d.get('context', None)
            info['context'] = '\n'.join([pformat(_d) for _d in c.dicts])
            template_context.append(info)
        context = {
            'templates': template_context,
            'template_dirs': [normpath(x) for x in settings.TEMPLATE_DIRS],
            'context_processors': context_processors,
        }
        return render_to_string('debug_toolbar/panels/templates.html', context)


import os
from abc import abstractmethod
from typing import Callable, Optional, Tuple, Union

import torch
import torch.nn as nn
from loguru import logger
from omegaconf import OmegaConf

from .registry import MODEL_BACKBONE_DICT, MODEL_HEAD_DICT, MODEL_NECK_DICT
from .utils import BackboneOutput, DetectionModelOutput, ModelOutput, load_from_checkpoint

class TaskModel(nn.Module):
    def __init__(self, conf_model, task, backbone_name, head_name, num_classes, model_checkpoint,
                 img_size: Optional[Union[int, Tuple]] = None, freeze_backbone: bool = False) -> None:
        super(TaskModel, self).__init__()
        self.task = task
        self.backbone_name = backbone_name
        self.head_name = head_name

        backbone_fn: Callable[..., nn.Module] = MODEL_BACKBONE_DICT[backbone_name]
        self.backbone: nn.Module = backbone_fn(task=self.task, conf_model_backbone=conf_model.architecture.backbone)

        self.backbone = load_from_checkpoint(self.backbone, model_checkpoint)

        intermediate_features_dim = self.backbone.intermediate_features_dim
        if getattr(conf_model.architecture, 'neck', None):
            neck_name = conf_model.architecture.neck.name
            neck_fn: Callable[..., nn.Module] = MODEL_NECK_DICT[neck_name]
            self.neck = neck_fn(intermediate_features_dim=self.backbone.intermediate_features_dim, conf_model_neck=conf_model.architecture.neck)
            intermediate_features_dim = self.neck.intermediate_features_dim

        head_module = MODEL_HEAD_DICT[self.task][head_name]
        if task == 'classification':
            self.head = head_module(num_classes=num_classes, feature_dim=self.backbone.feature_dim, conf_model_head=conf_model.architecture.head)
        elif task in ['segmentation', 'detection']:
            img_size = img_size if isinstance(img_size, (int, None)) else tuple(img_size)
            self.head = head_module(num_classes=num_classes,
                                    intermediate_features_dim=intermediate_features_dim,
                                    label_size=img_size,
                                    conf_model_head=conf_model.architecture.head)

        if freeze_backbone:
            self._freeze_backbone()
            logger.info(f"Freeze! {self.backbone_name} is now freezed. Now only tuning with {self.head_name}.")

    def _freeze_backbone(self):
        for m in self.backbone.parameters():
            m.requires_grad = False

    @property
    def device(self):
        return next(self.parameters()).device

    def _get_name(self):
        return f"{self.__class__.__name__}[task={self.task}, backbone={self.backbone_name}, head={self.head_name}]"

    @abstractmethod
    def forward(self, x, label_size=None, targets=None):
        raise NotImplementedError

class ClassificationModel(TaskModel):
    def __init__(self, conf_model, task, backbone_name, head_name, num_classes, model_checkpoint,
                 label_size=None, freeze_backbone=False) -> None:
        super().__init__(conf_model, task, backbone_name, head_name, num_classes, model_checkpoint, label_size, freeze_backbone)

    def forward(self, x, label_size=None, targets=None):
        features: BackboneOutput = self.backbone(x)
        out: ModelOutput = self.head(features['last_feature'])
        return out

class SegmentationModel(TaskModel):
    def __init__(self, conf_model, task, backbone_name, head_name, num_classes, model_checkpoint,
                 label_size, freeze_backbone=False) -> None:
        super().__init__(conf_model, task, backbone_name, head_name, num_classes, model_checkpoint, label_size, freeze_backbone)

    def forward(self, x, label_size=None, targets=None):
        features: BackboneOutput = self.backbone(x)
        if hasattr(self, 'neck'):
            features: BackboneOutput = self.neck(features['intermediate_features'])
        out: ModelOutput = self.head(features['intermediate_features'])
        return out

class DetectionModel(TaskModel):
    def __init__(self, conf_model, task, backbone_name, head_name, num_classes, model_checkpoint,
                 label_size, freeze_backbone=False) -> None:
        super().__init__(conf_model, task, backbone_name, head_name, num_classes, model_checkpoint, label_size, freeze_backbone)

    def forward(self, x, label_size=None, targets=None):
        features: BackboneOutput = self.backbone(x)
        if hasattr(self, 'neck'):
            features: BackboneOutput = self.neck(features['intermediate_features'])
        out: DetectionModelOutput = self.head(features['intermediate_features'])
        return out


#!/usr/bin/python
'''
Part of the Qt-Deployment scripts

@package qt-deploy
'''
import os
import sys
import stat
import shutil
import zipfile
import tarfile
import ConfigParser
import argparse
import subprocess
from subprocess import check_call

def copy(src, dst):
    if os.path.islink(src):
        linkto = os.readlink(src)
        os.symlink(linkto, dst)
    else:
        shutil.copy(src, dst)

def copyLib(src, dstDir, version=''):
    srcDir = os.path.dirname(src)
    srcName = os.path.basename(src)
    if version == '':
        found = False
        for f in reversed(os.listdir(srcDir)):
            if srcName in f:
                copy(os.path.join(srcDir, f), os.path.join(dstDir, f))
                found = True
        if not found:
            sys.stdout.write("library " + srcName + " not found\n")
            exit(1)
    else:
        srcNameExtended = srcName + '.' + version
        shutil.copy(os.path.join(srcDir, srcNameExtended),
                    os.path.join(dstDir, srcNameExtended))

class QtDeployment:

    def cleanup(self):
        sys.stdout.write("starting cleanup...")
        sys.stdout.flush()

        if os.path.exists(self.deploymentDir):
            shutil.rmtree(self.deploymentDir)

        if os.path.isfile(self.zipName):
            os.remove(self.zipName)

        if os.path.exists(self.targetOriginal):
            shutil.rmtree(self.targetOriginal)

        sys.stdout.write("done\n")

    def deployMac(self):
        self.cleanup()

        sys.stdout.write("creating disk image...")
        sys.stdout.flush()
        macutil = os.path.join(self.qtBinDir, 'macdeployqt')
        qmlDir = os.path.abspath(self.qmlSourceDir)
        currentDir = os.getcwd()
        os.chdir(self.applicationDir)
        check_call('%s %s -qmldir=%s -dmg -verbose=2' % (macutil,  self.target, qmlDir), shell=True)
        os.chdir(currentDir)
        sys.stdout.write("done\n")

        sys.stdout.write("moving disk image...")
        sys.stdout.flush()
        inPath = os.path.join(self.applicationDir, self.dmgName)
        shutil.move(inPath, self.zipName)
        sys.stdout.write("done\n")

        sys.stdout.write("moving app bundle...")
        sys.stdout.flush()
        inPath = os.path.join(self.applicationDir, self.target)
        shutil.copytree(inPath, self.targetOriginal)
        sys.stdout.write("done\n")

        sys.stdout.write("cleaning app bundle...")
        sys.stdout.flush()
        targetBundle = os.path.join(self.applicationDir, self.target)
        shutil.rmtree(targetBundle)
        sys.stdout.write("done\n")

    def deployWindows(self):
        self.cleanup()

        # create deployment dir
        try:
            os.makedirs(self.deploymentDir)
        except WindowsError:    # ignore error on windows
            pass

        # resolve VS install path
        vsEnvCmd = None
        vsVersions = ['90', '100', '110', '120', '130']
        for version in vsVersions:
            envVar = 'VS%sCOMNTOOLS' % version
            if os.getenv(envVar):
                vsEnvCmd = '"%svsvars32.bat"' % os.getenv(envVar)
                break
        if vsEnvCmd is None:
            sys.stderr.write('Unable to determine Visual Studio version\n')
            sys.exit(1)

        pipe = subprocess.Popen('%s & set VCINSTALLDIR' % vsEnvCmd,
                                stdout=subprocess.PIPE, shell=True)
        vcInstallDir = pipe.communicate()[0].strip().split('=')[1]
        redistDir = os.path.join(vcInstallDir, 'redist', self.arch)

        # copy dependencies using windeployqt
        sys.stdout.write("copying dependencies...")
        sys.stdout.flush()
        winutil = os.path.join(self.qtBinDir, 'windeployqt.exe')
        cmd = '%s & %s %s' % (vsEnvCmd, winutil, self.target)
        cmd += ' --qmldir %s' % os.path.abspath(self.qmlSourceDir)
        cmd += ' --dir %s' % os.path.abspath(self.deploymentDir)
        currentDir = os.getcwd()
        os.chdir(self.applicationDir)
        check_call(cmd, shell=True)
        os.chdir(currentDir)
        sys.stdout.write("done\n")

        # copy target
        inFile = os.path.join(self.applicationDir, self.target)
        targetFile = os.path.join(self.outBinDir, self.target)
        shutil.copyfile(inFile, targetFile)

        # copy additional libraries
        if self.libs[0] != '':
            # create the lib dir in case it does not exists
            sys.stdout.write("copying additional libraries...")
            sys.stdout.flush()
            try:
                os.makedirs(self.outLibDir)
            except WindowsError:    # ignore error on windows
                pass

            for lib in self.libs:
                libName = self.libraryPrefix + lib + self.libraryExtension
                inPath = os.path.join(self.libDir, libName)
                # overwrite if redistributable
                for root, dirs, files in os.walk(redistDir):
                    for f in files:
                        if f == libName:
                            inPath = os.path.join(root, f)
                            break

                copyLib(inPath, self.outLibDir)

            sys.stdout.write("done\n")

        sys.stdout.write("compressing files...")
        sys.stdout.flush()
        # create zip file
        with zipfile.ZipFile(self.zipName, 'w', zipfile.ZIP_DEFLATED) as myzip:
            for root, dirs, files in os.walk(self.deploymentDir):
                for f in files:
                    myzip.write(os.path.join(root, f))
            myzip.close()
        sys.stdout.write("done\n")

    def deployAndroid(self):
        self.cleanup()

        # read storepass from cmd
        sys.stdout.write("reading storepass...")
        proc = subprocess.Popen(self.androidStorepassCmd,
                                stdout=subprocess.PIPE, shell=True)
        storepass = proc.stdout.read().strip()
        sys.stdout.write("done\n")

        # read keypass from cmd
        sys.stdout.write("reading keypass...")
        proc = subprocess.Popen(self.androidKeypassCmd,
                                stdout=subprocess.PIPE, shell=True)
        keypass = proc.stdout.read().strip()
        sys.stdout.write("done\n")

        sys.stdout.write("creating Android package...")
        sys.stdout.flush()
        androidutil = os.path.join(self.qtBinDir, 'androiddeployqt')
        currentDir = os.getcwd()
        os.chdir(self.applicationDir)
        buildSettings = './android-lib%s.so-deployment-settings.json' % self.name
        deployCmd = androidutil
        deployCmd += ' --input %s' % buildSettings
        deployCmd += ' --output %s' % self.androidBuildDir
        deployCmd += ' --deployment bundled'
        deployCmd += ' --android-platform %s' % self.androidPlatform
        deployCmd += ' --sign %s %s' % (self.androidKeystore, self.androidKey)
        deployCmd += ' --storepass %s' % storepass
        deployCmd += ' --keypass %s' % keypass
        check_call(deployCmd, shell=True)
        os.chdir(currentDir)
        sys.stdout.write("done\n")

        sys.stdout.write("moving Android package...")
        sys.stdout.flush()
        inPath = os.path.join(self.androidBuildDir, 'bin', self.apkName)
        shutil.move(inPath, self.zipName)
        sys.stdout.write("done\n")

    def deployLinux(self):
        self.cleanup()

        sys.stdout.write("copying files...")
        sys.stdout.flush()

        # create lib dir
        os.makedirs(self.outLibDir)

        # copy Qt libs
        if self.qtLibs[0] != '':
            for lib in self.qtLibs:
                # if version os specified copy only libs with this version
                version = ''
                libSplit = lib.split(':')
                lib = libSplit[0]
                if len(libSplit) > 1:
                    version = libSplit[1]

                libName = self.libraryPrefix + lib + self.libraryExtension
                inPath = os.path.join(self.qtLibDir, libName)
                copyLib(inPath, self.outLibDir, version)

        # copy additional libraries
        if self.libs[0] != '':
            for lib in self.libs:
                # if version is specified copy only libs with this version
                version = ''
                libSplit = lib.split(':')
                lib = libSplit[0]
                if len(libSplit) > 1:
                    version = libSplit[1]

                libName = lib + self.libraryExtension
                copied = False
                for libDir in self.libDirs:
                    if not os.path.exists(libDir):
                        continue
                    inPath = os.path.join(libDir, libName)
                    found = False
                    if version == '':
                        for f in reversed(os.listdir(libDir)):
                            if libName in f:
                                found = True
                                break
                    else:
                        if os.path.isfile(inPath):
                            found = True
                    if found:
                        copyLib(inPath, self.outLibDir, version)
                        copied = True
                        break

                if not copied:
                    sys.stderr.write('could not find library %s\n' % libName)
                    exit(1)

        # cleanup symlinks, use library in the style *.so.<major_version>
        libs = os.listdir(self.outLibDir)
        libs.sort(key=len)
        doneList = []
        for lib in libs:
            if lib in doneList:  # skip already processed files
                continue
            list = [f for f in libs if lib in f]  # list all occurrences
            target = None
            match = None
            for l in list:
                index = l.find('.so')
                count = l[index:].count('.')
                if count == 2:
                    match = l
                if not os.path.islink(l):
                    target = l
            if target and match:
                if target == match:
                    continue
                target = os.path.join(self.outLibDir, target)
                match = os.path.join(self.outLibDir, match)
                tmp = match + '.tmp'
                shutil.copy(target, tmp)
                for l in list:
                    doneList.append(l)
                    os.remove(os.path.join(self.outLibDir, l))
                shutil.copy(tmp, match)
                os.remove(tmp)

        # remove executable bit from libraries
        for f in os.listdir(self.outLibDir):
            outFile = os.path.join(self.outLibDir, f)
            st = os.stat(outFile)
            os.chmod(outFile, st.st_mode & ~stat.S_IEXEC)

        # create the platforms dir
        os.makedirs(self.outPlatformsDir)

        # copy Qt platform plugins
        for plugin in self.platformPlugins:
            pluginName = self.libraryPrefix + plugin + self.libraryExtension
            inPath = os.path.join(self.platformsDir, pluginName)
            outPath = os.path.join(self.outPlatformsDir, pluginName)
            shutil.copyfile(inPath, outPath)

        # create the bin dir
        os.makedirs(self.outBinDir)

        # copy target and make it executable
        inFile = os.path.join(self.applicationDir, self.target)
        targetFile = os.path.join(self.outBinDir, self.target)
        shutil.copyfile(inFile, targetFile)
        st = os.stat(targetFile)
        os.chmod(targetFile, st.st_mode | stat.S_IEXEC)

        # copy QML plugins
        if self.qmlPlugins[0] != '':
            for qmlplugin in self.qmlPlugins:
                targetPath = os.path.join(self.outQmlDir, qmlplugin)
                if os.path.exists(targetPath):
                    shutil.rmtree(targetPath)
                shutil.copytree(os.path.join(self.qmlDir, qmlplugin), targetPath)

        # copy Qt plugins
        if self.qtPlugins[0] != '':
            for qtplugin in self.qtPlugins:
                targetPath = os.path.join(self.outPluginDir, qtplugin)
                if os.path.exists(targetPath):
                    shutil.rmtree(targetPath)
                shutil.copytree(os.path.join(self.pluginDir, qtplugin), targetPath)

        # remove unnecessary files:
        for root, dirs, files in os.walk(self.outQmlDir):
                for f in files:
                    if (f == 'plugins.qmltypes'):
                        os.remove(os.path.join(root, f))

        sys.stdout.write("done\n")

        sys.stdout.write("compressing files...")
        sys.stdout.flush()
        # strip debug information
        for root, dirs, files in os.walk(self.outLibDir):
            for f in files:
                if self.libraryExtension in f:
                    check_call(['strip', os.path.join(root, f)])
        check_call(['strip', os.path.join(self.outBinDir, self.target)])

        # create run.sh
        runFilePath = os.path.join(self.deploymentDir, self.target)
        runFile = open(runFilePath, 'w')
        if runFile:
            runFile.write('#!/usr/bin/env bash\n')
            runFile.write('if [ -z "$BASH_SOURCE" ]; then\n')
            runFile.write('cd "$(dirname "$(readlink -f "$0")")"\n')
            runFile.write('else\n')
            runFile.write('cd "$(dirname "${BASH_SOURCE[0]}" )"\n')
            runFile.write('fi\n')
            runFile.write('CWD=`pwd`\n')
            runFile.write('export LD_LIBRARY_PATH="$CWD"/lib\n')
            runFile.write('export QML_IMPORT_PATH="$CWD"/qml\n')
            runFile.write('export QML2_IMPORT_PATH="$CWD"/qml\n')
            runFile.write('export QT_QPA_PLATFORM_PLUGIN_PATH="$CWD"/platforms\n')
            runFile.write('export QT_PLUGIN_PATH="$CWD"/plugins\n')
            if (self.platform == 'linux_x86'):
                runFile.write('/lib/ld-linux.so.2 ')
            else:
                runFile.write('/lib64/ld-linux-x86-64.so.2 ')
            runFile.write('"$CWD"/bin/' + self.target + ' $@\n')
            runFile.write('exit $?\n')
            runFile.close()
            st = os.stat(runFilePath)
            os.chmod(runFilePath, st.st_mode | stat.S_IEXEC)
        else:
            sys.stderr.write('error creating %s\n' % runFilePath)
            exit(1)
        # create tar file
        with tarfile.open(self.zipName, 'w:gz') as mytar:
            for root, dirs, files in os.walk(self.deploymentDir):
                for f in files:
                    mytar.add(os.path.join(root, f))
            mytar.close()
        sys.stdout.write("done\n")

    def preparePath(self, path):
        path = os.path.expanduser(path)
        if (len(path) > 0) and (path[0] == '.'):
            path = os.path.abspath(path)
        return path

    def parseConfig(self):
        if self.debug:
            print("parsing config file")

        if self.version:
            defaults = {'version': self.version}
        else:
            defaults = None
        config = ConfigParser.SafeConfigParser(defaults)
        config.read(self.configFile)
        self.name = config.get('DEFAULT', 'name').strip('"')
        try:
            if not self.version:
                self.version = config.get('DEFAULT', 'version').strip('"')
        except:
            self.version = None
        self.platform = config.get('Deployment', 'platform').strip('"')
        self.qtDir = self.preparePath(config.get('Deployment', 'qtDir').strip('"'))
        self.applicationDir = self.preparePath(config.get('Deployment', 'applicationDir').strip('"'))
        self.pkgName = self.preparePath(config.get('Deployment', 'pkgName').strip('"'))
        if self.platform == "mac":
            self.qmlSourceDir = self.preparePath(config.get('Deployment', 'qmlSourceDir').strip('"'))
        elif "windows" in self.platform:
            self.deploymentDir = self.preparePath(config.get('Deployment', 'deploymentDir').strip('"'))
            self.qmlSourceDir = self.preparePath(config.get('Deployment', 'qmlSourceDir').strip('"'))
            self.libs = config.get('Deployment', 'libs').strip('"').split(',')
            self.libDir = self.preparePath(config.get('Deployment', 'libDir').strip('"'))
            self.arch = config.get('DEFAULT', 'arch').strip('"')
        elif "android" in self.platform:
            self.androidPlatform = config.get('Deployment', 'androidPlatform').strip('"')
            self.androidKeystore = self.preparePath(config.get('Deployment', 'androidKeystore').strip('"'))
            self.androidKey = config.get('Deployment', 'androidKey').strip('"')
            self.androidStorepassCmd = self.preparePath(config.get('Deployment', 'androidStorepassCmd').strip('"'))
            self.androidKeypassCmd = self.preparePath(config.get('Deployment', 'androidKeypassCmd').strip('"'))
        else:
            self.deploymentDir = self.preparePath(config.get('Deployment', 'deploymentDir').strip('"'))
            rawLibDirs = config.get('Deployment', 'libDir').strip('"').split(',')
            self.libDirs = []
            for libDir in rawLibDirs:
                self.libDirs.append(self.preparePath(libDir))
            self.qmlPlugins = config.get('Deployment', 'qmlPlugins').strip('"').split(',')
            self.qtPlugins = config.get('Deployment', 'qtPlugins').strip('"').split(',')
            self.platformPlugins = config.get('Deployment', 'platformPlugins').strip('"').split(',')
            self.qtLibs = config.get('Deployment', 'qtLibs').strip('"').split(',')
            self.libs = config.get('Deployment', 'libs').strip('"').split(',')

    def parseArguments(self):
        parser = argparse.ArgumentParser(description='Component for easy deployment of Qt applications')
        parser.add_argument('-v', '--version', help='Version of the application', required=None)
        parser.add_argument('--deploy', help='Deploy the application to the output directory', action='store_true')
        parser.add_argument('--clean', help='Cleanup the created files afterwards', action='store_true')
        parser.add_argument('-d', '--debug', help='Whether debug output should be enabled or not', action='store_true')
        parser.add_argument('config', help='Config file', nargs='?', default=None)
        args = parser.parse_args()

        self.version = args.version
        self.debug = args.debug
        self.deploy = args.deploy
        self.clean = args.clean
        self.configFile = args.config

        if self.debug:
            print("parsed arguments")

        if not self.configFile:
            print("no config file specified")
            exit(1)

    def createVars(self):
        if self.debug:
            print("creating variables")

        if (self.platform == 'windows_x86') or (self.platform == 'windows_x64'):
            self.targetExtension = '.exe'
            self.libraryExtension = '.dll'
            self.libraryPrefix = ''
            self.qtBinDir = os.path.join(self.qtDir, 'bin')
            self.zipName = self.pkgName + '.zip'
            self.outLibDir = self.deploymentDir
            self.outBinDir = self.deploymentDir
        elif (self.platform == 'linux_x86') or (self.platform == 'linux_x64'):
            self.targetExtension = ''
            self.libraryExtension = '.so'
            self.libraryPrefix = 'lib'
            self.zipName = self.pkgName + '.tar.gz'
            self.qtLibDir = os.path.join(self.qtDir, 'lib')
            self.outLibDir = os.path.join(self.deploymentDir, 'lib')
            self.outBinDir = os.path.join(self.deploymentDir, 'bin')
        elif (self.platform == 'mac'):
            self.targetExtension = '.app'
            self.qtBinDir = os.path.join(self.qtDir, 'bin')
            self.zipName = self.pkgName + '.dmg'
            self.dmgName = self.name + '.dmg'
            self.deploymentDir = ''
        elif 'android' in self.platform:
            self.targetExtension = '.apk'
            self.qtBinDir = os.path.join(self.qtDir, 'bin')
            self.zipName = self.pkgName + '.apk'
            self.androidBuildDir = os.path.join(self.applicationDir, 'android-build')
            self.apkName = 'QtApp-release-signed.apk'
            self.deploymentDir = ''
        else:
            self.targetExtension = ''
            self.libraryExtension = ''
            self.qtDir = os.path.join(self.qtDir, 'lib')

        self.target = self.name.lower() + self.targetExtension
        self.targetOriginal = self.pkgName + self.targetExtension
        self.qmlDir = os.path.join(self.qtDir, 'qml')
        self.pluginDir = os.path.join(self.qtDir, 'plugins')
        self.platformsDir = os.path.join(self.qtDir, 'plugins/platforms')
        self.outPluginDir = os.path.join(self.deploymentDir, 'plugins')
        self.outPlatformsDir = os.path.join(self.deploymentDir, 'platforms')
        self.outQmlDir = os.path.join(self.deploymentDir, 'qml')

    def run(self):
        self.parseArguments()
        self.parseConfig()
        self.createVars()
        if self.deploy:
            if self.platform == 'mac':
                self.deployMac()
            elif 'android' in self.platform:
                self.deployAndroid()
            elif 'windows' in self.platform:
                self.deployWindows()
            elif 'linux' in self.platform:
                self.deployLinux()
            else:
                sys.stderr.write('unsupported platform %s\n' % self.platform)
                sys.exit(1)
        if self.clean:
            self.cleanup()

deployment = QtDeployment()
deployment.run()


import torch
import torch.nn.functional as F

def temp_denoise(model, noisyframe, sigma_noise):
	'''Encapsulates call to denoising model and handles padding.
		Expects noisyframe to be normalized in [0., 1.]
	'''
	# make size a multiple of four (we have two scales in the denoiser)
	sh_im = noisyframe.size()
	expanded_h = sh_im[-2]%4
	if expanded_h:
		expanded_h = 4-expanded_h
	expanded_w = sh_im[-1]%4
	if expanded_w:
		expanded_w = 4-expanded_w
	padexp = (0, expanded_w, 0, expanded_h)
	noisyframe = F.pad(input=noisyframe, pad=padexp, mode='reflect')
	sigma_noise = F.pad(input=sigma_noise, pad=padexp, mode='reflect')

	# denoise
	# No noise map here
	# out = torch.clamp(model(noisyframe, sigma_noise), 0., 1.)
	out = torch.clamp(model(noisyframe), 0., 1.)
	if len(out.shape) == 5:
		out = out.view(out.size(0), -1, out.size(3), out.size(4))

	out = out.view(-1, 3, out.size(2), out.size(3))

	if expanded_h:
		out = out[:, :, :-expanded_h, :]
	if expanded_w:
		out = out[:, :, :, :-expanded_w]

	return out

def denoise_seq_ReMoNet(seq, noise_std, temp_psz, temp_osz, model_temporal):
	r"""Denoises a sequence of frames with ReMoNet.

	Args:
		seq: Tensor. [numframes, 1, C, H, W] array containing the noisy input frames
		noise_std: Tensor. Standard deviation of the added noise
		temp_psz: size of the temporal patch
		temp_osz: size of output temporal size
		model_temp: instance of the PyTorch model of the temporal denoiser
	Returns:
		denframes: Tensor, [numframes, C, H, W]
	"""
	# init arrays to handle contiguous frames and related patches
	numframes, C, H, W = seq.shape
	ctrlfr_idx = int((temp_psz-1)//2)
	inframes = list()
	denframes = torch.empty((numframes, C, H, W)).to(seq.device)

	# build noise map from noise std---assuming Gaussian noise
	noise_map = noise_std.expand((1, 1, H, W))

	ctrl_out_fr_idx = temp_osz // 2

	for fridx in range(ctrl_out_fr_idx, numframes, temp_osz):
		print('denoising frame {} - {}'.format(fridx-temp_osz//2, fridx+temp_osz//2))
		# load input frames
		# if not inframes:
		# # if list not yet created, fill it with temp_patchsz frames
		# 	for idx in range(temp_psz):
		# 		# use [2,1,0,1,2] frames to denoise the 0-th frame
		# 		relidx = abs(idx-ctrlfr_idx) # handle border conditions, reflect
		# 		inframes.append(seq[relidx])
		# else:
		# 	# delete left-most frame
		# 	del inframes[0]
		# 	relidx = min(fridx + ctrlfr_idx, -fridx + 2*(numframes-1)-ctrlfr_idx) # handle border conditions
		# 	# add right-most frame
		# 	inframes.append(seq[relidx])
		inframes = []
		for idx in range(temp_psz):
			relidx = abs(fridx - ctrlfr_idx + idx)
			relidx = min(relidx, 2*numframes-1-relidx)
			inframes.append(seq[relidx])

		# stack 5 consecutive frames
		inframes_t = torch.stack(inframes, dim=0).contiguous().view((1, temp_psz*C, H, W)).to(seq.device)

		# append result to output list
		end_idx = min(fridx+ctrl_out_fr_idx+1, denframes.shape[0])
		length = end_idx - (fridx-ctrl_out_fr_idx)
		denframes[fridx-ctrl_out_fr_idx:end_idx] = temp_denoise(model_temporal, inframes_t, noise_map)[:length]

	# free memory up
	del inframes
	del inframes_t
	torch.cuda.empty_cache()

	# convert to appropiate type and return
	return denframes


# -*- coding: utf-8 -*-
"""
Created on Wed Jun 27 00:48:07 2018

@author: Tareq
"""
'''
- two ways of creating dataframe 1. using the dataframe syntax and read_csv. read_excel
- line is the default graph type for the plot method
- tareq.j@metroc.ca
'''

import os
os.chdir(r'C:\Users\mfatemeh\Desktop\python')

import pandas as pd
import numpy as np
from sklearn import preprocessing

#reading excel
file1=pd.read_excel('scimagojr-3.xlsx',sheet_name='Sheet1', index_col=0, na_values=['NA']) # we use col=0 as the index
file1.head()

file1.loc[[1,2,3,4,5],['Country']]
file1.loc[[5],['Country']] # reading an specific row with specif column

#writing to excel
df_out = pd.DataFrame([('string1', 1),('string2', 2),('string3', 3)],
                       columns=['Name', 'Value'])
df_out

df_out.to_excel('tmp.xlsx')

pd.read_excel('tmp.xlsx')

pd.read_excel('tmp.xlsx', dtype={'Name':str, 'Value':float}) # not supported in my lap top

pd.read_excel('tmp.xlsx', converters={'Name':str, 'Value':float}) #since above does not work

pd.read_excel('tmp.xlsx', na_values=['string1', 'string2']) # we specifi any string we want as na_value

#skipping rows with comment
df = pd.DataFrame({'a': ['1', '2', '4'], 'b': ['#2', '#3', '#5']})
df.to_excel('tmp2.xlsx', index=False) #we don't want to have indexes
pd.read_excel('tmp2.xlsx')
pd.read_excel('tmp2.xlsx', comment='#') # we specify that the string after hash is comment even the third column(if there was any)

df = pd.DataFrame({'a': ['1', '2', '4'], 'b': ['#2', '#3', '#5'], 'c': ['1', '2', '4'], 'd': ['#2', '#3', '#5']})
df.to_excel('tmp2.xlsx', index=False) #we don't want to have indexes
pd.read_excel('tmp2.xlsx')
pd.read_excel('tmp2.xlsx', comment='#')

df = pd.DataFrame({'a': ['#1', '#2', '#4'], 'b': ['2', '3', '5']})
df.to_excel('tmp2.xlsx', index=False)
pd.read_excel('tmp2.xlsx', comment='#') #everything after hash key in one row is ignored

#--------------------------------
#Plotting

#Matplotlib is the whole package; matplotlib.pyplot is a module in matplotlib;
#and pylab is a module that gets installed alongside matplotlib.

import matplotlib.pyplot as plt

plt.interactive(False)#interactive mode is off, you have to call plt.show() explicitly to pop up figure window.

x = np.linspace(-2, 2, 100) #creating 100 values between -2 and 2

plt.plot(x, x, label='linear') #first cordinate is the x and the second is y.. we are not saving the plot to any object
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')

plt.xlabel('x label')
plt.ylabel('y label')

plt.title("Simple Plot")

plt.show() # if we do plt.plot all at the samr time and then run
#---------------------------
#Line chart

values = [1, 5, 8, 9, 7, 11, 8, 12, 14, 9]
plt.plot(values)
plt.show() #showing that if we don't specify two arguments, the given value is considered to be  y and the index is used for the x axis

#Multiple line chart
sales1 = [1, 5, 8, 9, 7, 11, 8, 12, 14, 9, 5]
sales2 = [3, 7, 9, 6, 4, 5, 14, 7, 6, 16, 12]
line_chart1 = plt.plot(range(1,12), sales1) #first argument is the x axis and the second argument is the y axis
line_chart2 = plt.plot(range(1,12), sales2)
plt.title('Monthly sales of 2016 and 2017')
plt.ylabel('Sales')
plt.xlabel('Month')
plt.legend(['year 2016', 'year 2017'], loc=1) #loc is the location of the legend and the order is important and the first is for the first object and
#the second argument is for the second object that been run
plt.legeInd(['year 2016', 'year 2017'], loc=4) #loc=4
plt.show()

#Line style
line_chart1 = plt.plot(range(1,12), sales1,'--') # the style is pased
line_chart2 = plt.plot(range(1,12), sales2,':')

#colour
line_chart1 = plt.plot(range(1,12), sales1,'g') # the colour type.. if we are adding more than one parameter, we need to specifiy with the parameter name like colour=
line_chart2 = plt.plot(range(1,12), sales2,'r')
line_chart2 = plt.plot(range(1,12), sales2, color='r', linestyle='--')
#the website to get the style and colour
#https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html
#-----------------------------

#Pie chart
# we have to have values, colour and labe; values are the first parameter an dffthe
values = [60, 80, 90, 55, 10, 30]
colors = ['b', 'g', 'r', 'c', 'm', 'y']
labels = ['US', 'UK', 'India', 'Germany', 'Australia', 'South Korea']
explode = (0.2, 0, 0, 0, 0, 0) #the US  that is the first value that and come out
plt.pie(values, colors=colors, labels= values,explode=explode,
        counterclock=False, shadow=True)
plt.title('Population Density Index')
plt.legend(labels,loc=3)
plt.show()

#Percentage
plt.pie(values, colors=colors, labels=labels,
explode=explode, autopct='%1.2f%%', counterclock=True, shadow=True) # we put the autopct to show the percentage in the slice

#-----------------------------

#Scatter plot chart

weight1=[63.3,57,64.3,63,71,61.8,62.9,65.6,64.8,63.1,68.3,69.7,65.4,66.3,60.7]
height1=[156.3,100.7,114.8,156.3,237.1,123.9,151.8,164.7,105.4,136.1,175.2,137.4,164.2,151,124.3]

plt.scatter(weight1,height1,c='b',marker='o')#marker=’o’ denotes the type of plot  "O' is showing each in circle and
#(. , o v ^ 8 s p D +)
plt.xlabel('weight', fontsize=16)
plt.ylabel('height', fontsize=16)
plt.title('scatter plot - height vs weight',fontsize=20)
plt.show()

#scatter plot for three different groups
weight1=[57,58.2,58.6,59.6,59.8,60.2,60.5,60.6,60.7,61.3,61.3,61.4,61.8,61.9,62.3]
height1=[100.7,195.6,94.3,127.1,111.7,159.7,135,149.9,124.3,112.9,176.7,110.2,123.9,161.9,107.8]

weight2=[62.9,63.1,63.2,63.3,63.4,63.4,63.4,63.5,63.6,63.7,64.1,64.3,64.3,64.7,64.8]
height2=[151.8,156.3,136.1,124.2,156.3,181.2,255.9,163.1,123.1,119.5,179.9,114.8,174.1,108.8,105.4]

weight3=[69.2,69.2,69.4,69.7,70,70.3,70.8,71,71.1,71.7,71.9,72.4,73,73.1,76.2]
height3=[166.8,172.9,193.8,137.4,162.4,137.1,169.1,237.1,189.1,179.3,174.8,213.3,198,191.1,220.6]

weight=np.concatenate((weight1,weight2,weight3)) #45 values
height=np.concatenate((height1,height2,height3))
len(weight)
len(height)

color_array = ['b'] * 15 + ['g'] * 15 + ['r']*15  # we need to cover all 45 values

plt.scatter(weight, height, marker='*', c=color_array)

plt.xlabel('weight', fontsize=16)
plt.ylabel('height', fontsize=16)
plt.title('grouped scatter plot - height vs weight',fontsize=20)
plt.show()
#----------------------------

#Bar chart

city=['Delhi','Beijing','Washington','Tokyo','Moscow']
pos = np.arange(len(city))
Happiness_Index=[60,40,70,65,85]

plt.bar(pos,Happiness_Index,color='blue',edgecolor='black')
plt.xticks(pos, city) #xtick uses the city for the pos.. without the xtick we will see 0,1,2,..
plt.xlabel('City', fontsize=16)
plt.ylabel('Happiness_Index', fontsize=16)
plt.title('Barchart - Happiness index across cities',fontsize=20)
plt.show()

#Horizontal Bar Chart
plt.barh(pos,Happiness_Index,color='blue',edgecolor='black')

#Stacked Bar Chart with legends:
city=['Delhi','Beijing','Washington','Tokyo','Moscow']
Gender=['Male','Female']
pos = np.arange(len(city))
Happiness_Index_Male=[60,40,70,65,85]
Happiness_Index_Female=[30,60,70,55,75]

plt.bar(pos,Happiness_Index_Male,color='blue',edgecolor='black')
plt.bar(pos,Happiness_Index_Female,color='pink',edgecolor='black',bottom=Happiness_Index_Male) #the bottom parameters helping us to avoid the overlap
plt.xticks(pos, city)
plt.xlabel('City', fontsize=16)
plt.ylabel('Happiness_Index', fontsize=16)
plt.title('Stacked Barchart - Happiness index across cities',fontsize=18)
plt.legend(Gender,loc=2)
plt.show()

#Overlapping!!
plt.bar(pos,Happiness_Index_Male,color='blue',edgecolor='black')
plt.bar(pos,Happiness_Index_Female,color='pink',edgecolor='black')

#Grouped Bar Chart with legends:
city=['Delhi','Beijing','Washington','Tokyo','Moscow']
Gender=['Male','Female']
pos = np.arange(len(city))
bar_width = 0.35
Happiness_Index_Male=[60,40,70,65,85]
Happiness_Index_Female=[30,60,70,55,75]

plt.bar(pos,Happiness_Index_Male,bar_width,color='blue',edgecolor='black') #we pass the weigth to get the
plt.bar(pos+bar_width,Happiness_Index_Female,bar_width,color='pink',edgecolor='black')
plt.xticks(pos, city)
plt.xlabel('City', fontsize=16)
plt.ylabel('Happiness_Index', fontsize=16)
plt.title('Group Barchart - Happiness index across cities By Gender',fontsize=18)
plt.legend(Gender,loc=2)
plt.show()

#----------------------------------------------
#Box plot #ploting one numeric coloumns

value1 = [82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21]
value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,52]
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,30]

box_plot_data=[value1,value2,value3,value4]
plt.boxplot(box_plot_data)
plt.show()

#box plot with fills and labels:
bp=plt.boxplot(box_plot_data,patch_artist=True, labels=['course1','course2','course3','course4']) #to fill the box with the default colour
#argument patch_artist fills the boxplot
#bp is the object

for x in bp['boxes']: #the Box inside the bp is the reserved key word for the
    # change outline color
    x.set(color='red', linewidth=2) #this refers to the color of the outliner
    # change fill color
    x.set(facecolor = 'green') #this refers to the color of the box itself

#Horizontal box with different colors:
box=plt.boxplot(box_plot_data,vert=0,patch_artist=True,labels=['course1','course2','course3','course4'],)

colors = ['cyan', 'lightblue', 'lightgreen', 'tan']
for patch, color in zip(box['boxes'], colors): #batch is the loop iterator fot box['Box'] and the color is the temp variable to go in colors
    patch.set_facecolor(color)

plt.show()

#notch format of the box plot
plt.boxplot(box_plot_data,notch='True',patch_artist=True,labels=['course1','course2','course3','course4'])
#----------------------
#Histogram chart
#step type (no fills)
values = [82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
plt.hist(values,bins=5, histtype='step', align='mid', color='g', label='Test Score Data') #5 is the number of bins the number of the steps is bins-1
plt.legend(loc=2)
plt.title('Histogram of score')
plt.show()

#bar type (with fills).
plt.hist(values,5, histtype='bar',
align='mid', color='c', label='Test score Data',edgecolor='black')
plt.legend()
#----------------------
#Create a DataFrame
#Filter/index

d = {
    'Name':['Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine',
            'Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine'],
    'Exam':['Semester 1','Semester 1','Semester 1','Semester 1','Semester 1','Semester 1',
            'Semester 2','Semester 2','Semester 2','Semester 2','Semester 2','Semester 2'],

    'Subject':['Mathematics','Mathematics','Mathematics','Science','Science','Science',
               'Mathematics','Mathematics','Mathematics','Science','Science','Science'],
   'Score':[62,47,55,74,31,77,85,63,42,67,89,81]}

df = pd.DataFrame(d,columns=['Name','Exam','Subject','Score'])
df

df['Name'] # accessing the column

x=df['Score'].mean()
type(x)
x
z=df['Score']
type(z)

y=np.array(x)
type(y)
y
np.mean(y)
#therefore we have optioms of

df[['Name', 'Score']]

df[:2]

df['Score'] > 70

df[df['Score'] > 70] #we get the subset of the data

df[(df['Score'] > 70) & (df['Score'] < 85)]

df.ix[:,'Score'] # is the series

type(df.ix[:,'Score'])
#Indexing with .ix:

df.ix[3,2] #one element 4th row and the 3rd column.. ix is similar to the iloc
df

#Normalisation
d = {
       'Score':[62,-47,-55,74,31,77,85,63,42,67,89,81,56]}

df = pd.DataFrame(d,columns=['Score'], dtype='float')
df

#Create a min max processing object
min_max_scaler = preprocessing.MinMaxScaler()
scaled_array = min_max_scaler.fit_transform(df)

df_normalized = pd.DataFrame(scaled_array)
df_normalized

#---------------------
d = {
    'Name':['Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine',
            'Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine'],
    'Exam':['Semester 1','Semester 1','Semester 1','Semester 1','Semester 1','Semester 1',
            'Semester 2','Semester 2','Semester 2','Semester 2','Semester 2','Semester 2'],

    'Subject':['Mathematics','Mathematics','Mathematics','Science','Science','Science',
               'Mathematics','Mathematics','Mathematics','Science','Science','Science'],
   'Result':['Pass','Pass','Fail','Pass','Fail','Pass','Pass','Fail','Fail','Pass','Pass','Fail']}

df = pd.DataFrame(d,columns=['Name','Exam','Subject','Result'])
df

pd.crosstab(df.Subject, df.Result,margins=False) # the first argument needed to be categortical and the second can be numeric
# it does show the frequency

pd.crosstab([df.Subject, df.Exam],df.Result, margins=True) #margin means to get the total(All) in each margin


from fastapi import FastAPI
from modules import mongodb
import json
from datetime import datetime

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.post("/bookmark")
async def add_bookmark(url: str, label: str = None):
    # get current date and time
    now = datetime.now()
    dt = now.strftime("%d/%m/%Y %H:%M:%S")
    now = datetime.now()
    mydict = {"url": url, "label": label, "date": dt}
    x = mongodb.mycol.insert_one(mydict)
    print(x.inserted_id)
    return {"objId": str(x.inserted_id)}

@app.get("/view-bookmark")
async def send_bookmarks():
    bookmarks = []
    for x in mongodb.mycol.find({}, {"_id": 0}):
        bookmarks.append(x)
        # print(x)
    print(bookmarks)
    return bookmarks


import json
import jsonschema
import os
import re

import reframe as rfm
import reframe.core.exceptions as errors
import reframe.utility.jsonext as jsonext
import reframe.utility.versioning as versioning

DATA_VERSION = '1.3.0'
_SCHEMA = os.path.join(rfm.INSTALL_PREFIX, 'reframe/schemas/runreport.json')

class _RunReport:
    '''A wrapper to the run report providing some additional functionality'''

    def __init__(self, report):
        self._report = report

        # Index all runs by test case; if a test case has run multiple times,
        # only the last time will be indexed
        self._cases_index = {}
        for run in self._report['runs']:
            for tc in run['testcases']:
                c, p, e = tc['name'], tc['system'], tc['environment']
                self._cases_index[c, p, e] = tc

        # Index also the restored cases
        for tc in self._report['restored_cases']:
            c, p, e = tc['name'], tc['system'], tc['environment']
            self._cases_index[c, p, e] = tc

    def __getitem__(self, key):
        return self._report[key]

    def __getattr__(self, name):
        return getattr(self._report, name)

    def slice(self, prop, when=None, unique=False):
        '''Slice the report on property ``prop``.'''

        if unique:
            returned = set()

        for tc in self._report['runs'][-1]['testcases']:
            val = tc[prop]
            if unique and val in returned:
                continue

            if when is None:
                if unique:
                    returned.add(val)

                yield val
            elif tc[when[0]] == when[1]:
                if unique:
                    returned.add(val)

                yield val

    def case(self, check, part, env):
        c, p, e = check.name, part.fullname, env.name
        return self._cases_index.get((c, p, e))

    def restore_dangling(self, graph):
        '''Restore dangling dependencies in graph from the report data.

        Returns the updated graph.
        '''

        restored = []
        for tc, deps in graph.items():
            for d in deps:
                if d not in graph:
                    restored.append(d)
                    self._do_restore(d)

        return graph, restored

    def _do_restore(self, testcase):
        tc = self.case(*testcase)
        if tc is None:
            raise errors.ReframeError(
                f'could not restore testcase {testcase!r}: '
                f'not found in the report file'
            )

        dump_file = os.path.join(tc['stagedir'], '.rfm_testcase.json')
        try:
            with open(dump_file) as fp:
                testcase._check = jsonext.load(fp)
        except (OSError, json.JSONDecodeError) as e:
            raise errors.ReframeError(
                f'could not restore testcase {testcase!r}') from e

def next_report_filename(filepatt, new=True):
    if '{sessionid}' not in filepatt:
        return filepatt

    search_patt = os.path.basename(filepatt).replace('{sessionid}', r'(\d+)')
    new_id = -1
    basedir = os.path.dirname(filepatt) or '.'
    for filename in os.listdir(basedir):
        match = re.match(search_patt, filename)
        if match:
            found_id = int(match.group(1))
            new_id = max(found_id, new_id)

    if new:
        new_id += 1

    return filepatt.format(sessionid=new_id)

def load_report(filename):
    try:
        with open(filename) as fp:
            report = json.load(fp)
    except OSError as e:
        raise errors.ReframeError(
            f'failed to load report file {filename!r}') from e
    except json.JSONDecodeError as e:
        raise errors.ReframeError(
            f'report file {filename!r} is not a valid JSON file') from e

    # Validate the report
    with open(_SCHEMA) as fp:
        schema = json.load(fp)

    try:
        jsonschema.validate(report, schema)
    except jsonschema.ValidationError as e:
        raise errors.ReframeError(f'invalid report {filename!r}') from e

    # Check if the report data is compatible
    found_ver = versioning.parse(
        report['session_info']['data_version']
    )
    required_ver = versioning.parse(DATA_VERSION)
    if found_ver.major != required_ver.major or found_ver < required_ver:
        raise errors.ReframeError(
            f'incompatible report data versions: '
            f'found {found_ver}, required >= {required_ver}'
        )

    return _RunReport(report)


from django.core.paginator import Paginator
from django.http import HttpResponse
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework import permissions as drf_permissions
from rest_framework.decorators import action

from . import models, permissions, serializers

def index(request, page=None):
    objects = models.Route.objects.all()
    paginator = Paginator(objects, 100)
    page = page if page else 1
    return render(request, 'routes_page.html', context={
        'paginator': paginator.page(page)
    })

class RouteViewSet(viewsets.ModelViewSet):
    serializer_class = serializers.RouteSerializer
    queryset = models.Route.objects.all()
    filterset_fields = ['route_type', 'is_active']
    ordering_fields = ['number', 'route_type', 'is_active']
    lookup_field = 'id'

    permission_classes = [drf_permissions.IsAuthenticated]
    permission_classes_by_action = {
        'create': [permissions.AdminPermission],
        'retrieve': [drf_permissions.IsAuthenticated],
        'list': [drf_permissions.IsAuthenticated],
        'update': [permissions.AdminPermission],
        'partial_update': [permissions.AdminPermission],
        'destroy': [permissions.AdminPermission]
    }

    def get_permissions(self):
        try:
            return [permission() for permission in self.permission_classes_by_action[self.action]]
        except KeyError:
            return [permission() for permission in self.permission_classes]

class TransportViewSet(viewsets.ModelViewSet):
    serializer_class = serializers.TransportSerializer
    queryset = models.Transport.objects.all()
    filterset_fields = ['transport_type']
    ordering_fields = ['vehicle_id_number', 'transport_type']
    lookup_field = 'id'

    permission_classes = [drf_permissions.IsAuthenticated]
    permission_classes_by_action = {
        'create': [permissions.AdminPermission],
        'retrieve': [drf_permissions.IsAuthenticated],
        'list': [drf_permissions.IsAuthenticated],
        'update': [permissions.AdminPermission | permissions.DriverPermission],
        'partial_update': [permissions.AdminPermission | permissions.DriverPermission],
        'destroy': [permissions.AdminPermission]
    }

    def get_permissions(self):
        try:
            return [permission() for permission in self.permission_classes_by_action[self.action]]
        except KeyError:
            return [permission() for permission in self.permission_classes]

    @action(methods=('post',), detail=True, permission_classes=[permissions.PassengerPermission])
    def add_review(self, request, id):
        rating = request.data.get('rating', False)
        review_text = request.data.get('review_text', False)
        if not rating and not review_text:
            return HttpResponse('No data available for review')

        review_data = {'rating': rating if rating else None,
                       'review_text': review_text if review_text else None}

        models.PassengersReviews.objects.create(
            user=request.user,
            transport_id=id,
            **review_data
        )
        return HttpResponse('Review added')


"""restore unique naming scheme for gene models if, e.g., sequences were ran through the predictor individually,
instead of as a whole genome, and current gene IDs are non-unique. Also adds species prefix. Assumes all transcript
names etc exactly contain the gene ID; if this isn't true Transcript IDs won't end up unique.
It works for AUGUSTUS output"""

import re
import argparse
from dustdas import gffhelper

def make_id(n, prefix="Cc", depth=6):
    printer = "%0" + str(depth) + 'd'
    idfill = printer % int(n)
    newid = prefix + idfill
    return newid

def dump2gffline(entry):
    return '\t'.join([str(x) for x in [entry.seqid, entry.source, entry.type, entry.start, entry.end, entry.score,
                                       entry.strand, entry.phase, entry.attribute]])

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-g', '--gff', help='input gff with our draft gene models', required=True)
    parser.add_argument('-p', '--prefix', help='prefix species name (default Xx)', default='Xx')
    parser.add_argument('-n', '--increment-from', help='start IDs counting from N', default=0, type=int)
    args = parser.parse_args()
    gffin = args.gff
    prefix = args.prefix
    incrementing_id = args.increment_from

    # read through gff file
    reader = gffhelper.read_gff_file(gffin)

    for entry in reader:
        if "gene" == entry.type:
            geneID = entry.get_ID()
            incrementing_id += 1
        entry.attribute = re.sub(geneID, make_id(incrementing_id, prefix=prefix), entry.attribute)
        print(dump2gffline(entry))

if __name__ == "__main__":
    main()


from openpyxl import load_workbook
import csv

wb = load_workbook(filename = '/home/nic/Dokumenty/web/venv/data.xlsx')
ws = wb['nic']
ws2 = wb['pamet']

indexp = ws2['A9'].value
print(indexp)

odmerp = ws2['A4'].value
if odmerp == "?" :
    newRowLocation = ws.max_row +1

    file = open('/home/nic/Dokumenty/web/venv/data/odmer.csv')
    csvreader = csv.reader(file)

    odmerpp, nic = csvreader
    odmer = " ".join(odmerpp)
    ws.cell(column=1,row=newRowLocation, value=indexp)
    ws.cell(column=2,row=newRowLocation, value=odmer)
    wb.save('/home/nic/Dokumenty/web/venv/data.xlsx')


from itertools import count

def parse(s):
    try:
        return int(s)
    except ValueError:
        return s

def assemble(code):
    for line in code.split('\n'):
        op_name, op1, *op2 = line.strip().split(' ')
        yield [op_name, parse(op1), parse(op2[0]) if op2 else None]

def solve(insts, **init_regs):
    regs = {r:init_regs.get(r, 0) for r in 'abcd'}
    ptr = -len(insts)
    while ptr:
        op, t1, t2 = insts[ptr]
        if op == 'inc':
            regs[t1] += 1
        elif op == 'dec':
            regs[t1] -= 1
        elif op == 'jnz':
            if regs.get(t1, t1):
                ptr += regs.get(t2, t2)
                continue
        elif op == 'cpy':
            if t2 in regs:
                regs[t2] = regs.get(t1, t1)
        elif op == 'out':
            yield regs.get(t1, t1)
        ptr += 1

def find_clock(clock):
    for i in count():
        computer = solve(clock, a=i)
        prev_out = next(computer)
        correct = 0
        for out in computer:
            if out == 1 - prev_out:
                correct += 1
                prev_out = out
                if correct >= 10:
                    return i
            else:
                break

if __name__ == '__main__':
    from aocd.models import Puzzle

    puz = Puzzle(2016, 25)
    insts = list(assemble(puz.input_data))

    puz.answer_a = find_clock(insts)
    print('Part 1:', puz.answer_a)


import torch

import pickle
import gensim
from create_vocab import Vocabulary, create_vocab

model_config = {
    #'gpu': 1,
    'npu': 0,
    '<PAD>': 0,
    '<SOS>': 1,
    '<EOS>': 2,
    '<UNK>': 3,
    'n_layers': 2,
    'dropout': 0.2,
    'output_dim': 6,  # number of classes
    'hidden_dim': 500,
    'n_epochs': 10000, #45000
    'batch_size': 128,  # carefully chosen
    'embedding_dim': 200,  # 50/100/200/300
    'bidirectional': True,
    'learning_rate': 0.0001,
    'model_code': 'bi_lstm_2_layer',
    'max_sequence_length': 20,
    'embeddings_dir': './inputs/data_url_0/t2e/embeddings/'
}

from utils import generate_word_embeddings

def set_dynamic_hparams():
    try:

        with open('./inputs/data_url_0/t2e/vocab.pkl', 'rb') as f:
            vocab = pickle.load(f)
    except FileNotFoundError as e:
        vocab = create_vocab()
        generate_word_embeddings(vocab)

    model_config['vocab_size'] = vocab.size
    model_config['vocab_path'] = 'vocab.pkl'
    return model_config

model_config = set_dynamic_hparams()