import math

import torch
import torch.nn as nn
import torch.nn.functional as F

class Bottleneck(nn.Module):
    def __init__(self, nChannels, growthRate, dropout_rate):
        super(Bottleneck, self).__init__()
        self.dropout_rate = dropout_rate
        interChannels = 4 * growthRate
        self.bn1 = nn.BatchNorm2d(nChannels)
        self.conv1 = nn.Conv2d(nChannels, interChannels, kernel_size=1,
                               bias=False)
        self.bn2 = nn.BatchNorm2d(interChannels)
        self.conv2 = nn.Conv2d(interChannels, growthRate, kernel_size=3,
                               padding=1, bias=False)

    def forward(self, x):
        out = self.conv1(F.relu(self.bn1(x)))
        out = self.conv2(F.relu(self.bn2(out)))
        if self.dropout_rate > 0:
            out = F.dropout(out, p=self.dropout_rate, training=self.training)
        out = torch.cat((x, out), 1)
        return out

class SingleLayer(nn.Module):
    def __init__(self, nChannels, growthRate, dropout_rate):
        super(SingleLayer, self).__init__()
        self.dropout_rate = dropout_rate
        self.bn1 = nn.BatchNorm2d(nChannels)
        self.conv1 = nn.Conv2d(nChannels, growthRate, kernel_size=3,
                               padding=1, bias=False)

    def forward(self, x):
        out = self.conv1(F.relu(self.bn1(x)))
        if self.dropout_rate > 0:
            out = F.dropout(out, p=self.dropout_rate, training=self.training)
        out = torch.cat((x, out), 1)
        return out

class Transition(nn.Module):
    def __init__(self, nChannels, nOutChannels):
        super(Transition, self).__init__()
        self.bn1 = nn.BatchNorm2d(nChannels)
        self.conv1 = nn.Conv2d(nChannels, nOutChannels, kernel_size=1,
                               bias=False)

    def forward(self, x):
        out = self.conv1(F.relu(self.bn1(x)))
        out = F.avg_pool2d(out, 2)
        return out

class Model(nn.Module):
    def __init__(self, nClasses=256, growthRate=12, depth=40, bottleneck=False,
                 dropout_rate=0.0, n_layers=3, **kwargs):
        super().__init__()

        self.n_layers = n_layers
        print("n_layers", n_layers)

        # dense blocks per layer
        nDenseBlocks = (depth - 4) // n_layers
        if bottleneck:
            nDenseBlocks //= 2

        if bottleneck:
            reduction = 0.5
        else:
            reduction = 1.0

        # initial convolution
        nChannels = 2 * growthRate
        self.conv1 = nn.Conv2d(3, nChannels, kernel_size=3, padding=1,
                               bias=False)

        for layer_n in range(1, n_layers + 1):
            dense_layer = self._make_dense(
                nChannels, growthRate, nDenseBlocks, bottleneck, dropout_rate)
            setattr(self, f'dense{layer_n}', dense_layer)
            nChannels += nDenseBlocks * growthRate
            if layer_n < n_layers:
                nOutChannels = int(math.floor(nChannels * reduction))
                trainsition_layer = Transition(nChannels, nOutChannels)
                setattr(self, f'trans{layer_n}', trainsition_layer)
                nChannels = nOutChannels

        self.bn1 = nn.BatchNorm2d(nChannels)
        self.fc = nn.Linear(nChannels, nClasses)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
            elif isinstance(m, nn.Linear):
                m.bias.data.zero_()

    def _make_dense(self, nChannels, growthRate, nDenseBlocks, bottleneck, dropout_rate):
        layers = []
        for i in range(int(nDenseBlocks)):
            if bottleneck:
                layers.append(Bottleneck(nChannels, growthRate, dropout_rate))
            else:
                layers.append(SingleLayer(nChannels, growthRate, dropout_rate))
            nChannels += growthRate
        return nn.Sequential(*layers)

    def forward(self, x):
        out = self.conv1(x)
        for i in range(1, self.n_layers):
            dense_layer = getattr(self, f'dense{i}')
            trans_layer = getattr(self, f'trans{i}')
            out = trans_layer(dense_layer(out))
        last_dense_layer = getattr(self, f'dense{self.n_layers}')
        out = last_dense_layer(out)

        out = F.avg_pool2d(F.relu(self.bn1(out)), out.size()[-1])
        out = torch.squeeze(torch.squeeze(out, 2), 2)
        out = F.log_softmax(self.fc(out))
        return out

    def save(self, path):
        torch.save(self.state_dict(), path)

    def load(self, path):
        state_dict = torch.load(path)
        self.load_state_dict(state_dict)


# django imports
from django import template

import itertools
import datetime
import pytz
import dateutil

register = template.Library()

@register.filter
def group_by_date(dates, timezone):
    tz = pytz.timezone(timezone)
    dates_parser = []
    for day in dates:
        try:
            new_date = pytz.utc.localize(dateutil.parser.parse(day))
        except ValueError:
            new_date = dateutil.parser.parse(day)
        dates_parser.append(new_date)
    days = [
        tz.normalize(day.replace(tzinfo=pytz.utc)) for day in dates_parser
    ]
    days2 = [
        list(group) for k, group in itertools.groupby(
            days, key=datetime.datetime.toordinal,
        )
    ]

    return [(day[0].date, day) for day in days2]


# 에라토스테네스의 체
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
nums = [i for i in range(2, n+1)]
t = 0

while True:
    m = nums[0]
    for n in nums:
        if n%m == 0:
            ans = n
            nums.remove(n)
            t += 1
        if t == k:
            break
    if t == k:
        break

print(ans)


from flask import Flask, jsonify, request, redirect, Response, render_template
import requests
from config import api_key, cam_names, rover_det

app = Flask(__name__)

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

@app.route('/rover', methods = ['POST'])
def rover():
	rov_name = request.form['optrover']
	return redirect(f'/{rov_name}.html?emsg=OK')

@app.route('/<rov_name>.html')
def pic_criteria(rov_name):
	err_msg = request.args.get('emsg')
	if err_msg == None:
		err_msg = ""

	rov_det = rover_det[rov_name]
	rov_pic = rov_det["rov_pic"]
	st_date = rov_det["landing_date"]
	end_date = rov_det["max_date"]
	cameras = rov_det["cameras"]

	cam_list = {}
	for cam in cameras:
		cam_list.update({cam:cam_names[cam]})

	return render_template('pic_criteria.html', rname=rov_name, rpic=rov_pic, sdat=st_date, edat=end_date, clist=cam_list, emsg=err_msg)

@app.route('/img_criteria', methods = ['POST'])
def imgcrit():
	rov_name = request.args.get('rov_name')
	form_date = request.form['date']
	try:
		form_cam = request.form['optcam']
	except:
		form_cam = ""
	return redirect(f'/list.html?rov_name={rov_name}&img_date={form_date}&sel_cam={form_cam}')

@app.route('/list.html')
def img_list():
	opts=""
	rov_name = request.args.get('rov_name')
	img_date = request.args.get('img_date')
	sel_cam = request.args.get('sel_cam')
	opts = "earth_date=" + img_date
	if sel_cam != "":
		opts += "&camera=" + sel_cam
	opts += "&api_key=" + api_key
	emsg = ""
	api_list = requests.get(f'https://api.nasa.gov/mars-photos/api/v1/rovers/{rov_name}/photos?{opts}')
	if api_list.text == "":
		emsg = 'No images for that camera and date.  Please try again.'
		return redirect(f'/{rov_name}.html?emsg="No images for that camera and date.  Please try again."')
	else:
		api_list = eval(api_list.text)
		img_list = api_list["photos"]
		max_rows = len(img_list)

		rend_list = {}
		for i in range(1, max_rows+1):
			row_cam = img_list[i-1]["camera"]["full_name"]
			row_url = img_list[i-1]["img_src"]
			row_date = img_list[i-1]["earth_date"]
			dictval = {
				"camera":row_cam,
				"img_url":row_url,
				"earth_date":row_date,
				}
			rend_list.update({i:dictval})

		return render_template('list.html', rname=rov_name, ilist=rend_list, alist=api_list)

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


"""Change column distance_bin to distance_cat

Revision ID: 2524785502b4
Revises: c137e7385dd7
Create Date: 2020-03-20 16:47:15.648707

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2524785502b4'
down_revision = 'c137e7385dd7'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('race', sa.Column('distance_cat', sa.String(), nullable=True))
    op.create_foreign_key(None, 'race', 'runner_contact', ['runner_contact_id'], ['id'])
    op.drop_column('race', 'distance_bin')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('race', sa.Column('distance_bin', sa.VARCHAR(), autoincrement=False, nullable=True))
    op.drop_constraint(None, 'race', type_='foreignkey')
    op.drop_column('race', 'distance_cat')
    # ### end Alembic commands ###


# A critical point in a linked list is defined as either a local maxima or a local minima.

# A node is a local maxima if the current node has a value strictly greater than the previous node and the next node.

# A node is a local minima if the current node has a value strictly smaller than the previous node and the next node.

# Note that a node can only be a local maxima/minima if there exists both a previous node and a next node.

# Given a linked list head, return an array of length 2 containing [minDistance, maxDistance] where minDistance is the minimum distance between any two distinct critical points and maxDistance is the maximum distance between any two distinct critical points. If there are fewer than two critical points, return [-1, -1].

# Example 1:

# Input: head = [3,1]
# Output: [-1,-1]
# Explanation: There are no critical points in [3,1].
# Example 2:

# Input: head = [5,3,1,2,5,1,2]
# Output: [1,3]
# Explanation: There are three critical points:
# - [5,3,1,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2.
# - [5,3,1,2,5,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1.
# - [5,3,1,2,5,1,2]: The sixth node is a local minima because 1 is less than 5 and 2.
# The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1.
# The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3.
# Example 3:

# Input: head = [1,3,2,2,3,2,2,2,7]
# Output: [3,3]
# Explanation: There are two critical points:
# - [1,3,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2.
# - [1,3,2,2,3,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2.
# Both the minimum and maximum distances are between the second and the fifth node.
# Thus, minDistance and maxDistance is 5 - 2 = 3.
# Note that the last node is not considered a local maxima because it does not have a next node.

# Constraints:

# The number of nodes in the list is in the range [2, 105].
# 1 <= Node.val <= 105
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
        prev = None
        min_dis = float("inf")
        max_dis = float("-inf")
        first_critical = None
        last_critical = None
        cur = head
        i = 0
        while cur:
            if prev and cur.next:
                if (cur.next.val > cur.val < prev.val) or (prev.val< cur.val > cur.next.val ):
                    if last_critical is not None:
                        min_dis = min(min_dis,i-last_critical)
                        last_critical = i
                    else:
                        last_critical = i
                    if first_critical is not None:
                        max_dis = max(max_dis,i-first_critical)
                    else:
                        first_critical = i
            prev,cur = cur,cur.next
            i+=1
        return [-1 if min_dis == float("inf") else min_dis, -1 if max_dis==float("-inf") else max_dis ]

2058 Find the Minimum and Maximum Number of Nodes Between Critical Points.py


user_input = input("vorodi: ").split(",")
print(user_input)
file_obj = open("my_file.txt","w+")
for item in range(len(user_input)):
    shomarandeh = "shomare:__"+str(item+1) +" |"
    name = "name:"+user_input[item]
    char_count = "|" +"char count "+ str(len(user_input[item]))
    to_wirte = "{0:<6s}{1:^24s}{02:<24s}\n".format(shomarandeh,name,char_count)
    data = file_obj.write(to_wirte)
file_obj.close()


from fastapi import FastAPI, Request, HTTPException, status, Depends ,File, UploadFile
from fastapi.templating import Jinja2Templates
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi.staticfiles import StaticFiles

from starlette.responses import HTMLResponse

from tortoise.contrib.fastapi import register_tortoise
from tortoise.signals import post_save
from tortoise import BaseDBAsyncClient

from typing import Optional, Type, List

from PIL import Image

import secrets
from datetime import datetime

from emails import *
from db_models import User, Business, Product
from pydentic_models import (
    user_pydenticIn,
    user_pydentic,
    business_pydentic,
    business_pydenticIn,
    product_pydenticIn,
    product_pydentic
)
from authentication import get_hashed_password, verify_token, token_generator

app = FastAPI()

templates = Jinja2Templates(directory='templates')

oauth2_scheme = OAuth2PasswordBearer(tokenUrl='token')

app.mount('/static', StaticFiles(directory='static'), name='static')

async def get_current_user(token: str=Depends(oauth2_scheme)):
    try:
        payload = jwt.decode(token, config_credentials['SECRET'], algorithms=['HS256'])
        user = await User.get(id=payload.get('id'))
    except:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail='Invalid token',
            headers={'WWW-Authenticate': 'Bearer'}
        )

    return await user

@app.post('/products')
async def get_products(product: product_pydenticIn, user: user_pydentic=Depends(get_current_user)):
    product = product.dict(exclude_unset=True)

    if product['original_price'] > 0:
        product['percentage_discount'] = ((product['original_price'] - product['new_price']) / product['original_price']) * 100

        product_obj = await Product.create(**product, business=user)
        product_obj = await product_pydenticIn.from_tortoise_orm(product_obj)

        return {
            'status': 'OK',
            'data': product_obj
        }
    else:
        return {
            'status': 'ERROR'
        }

@app.get('/products')
async def get_product():
    response = await product_pydentic.from_queryset(Product.all())
    return {
            'status': 'OK',
            'data': response
        }

@app.get('/products/{id}')
async def get_product(id: int):
    product = await Product.get(id=id)
    business = await product.business
    owner = await business.owner

    response = await product_pydentic.from_queryset_single(Product.get(id=id))

    return {
            'status': 'OK',
            'data': {
                'product_details': product,
                'business_details': {
                    'name': business.name,
                    'city': business.city,
                    'region': business.region,
                    'description': business.description,
                    'logo': business.logo,
                    'owner_id': owner.id,
                    'email': owner.email,
                    'join_date': owner.join_date.strtime('%b %d %Y'),
                },
            }
        }

@app.delete('/products/{id}')
async def delete_product(id: int, user: user_pydentic=Depends(get_current_user)):
    product = await Product.get(id=id)
    business = await product.business
    owner = await business.owner

    if user == owner:
        product.delete()
        return {
            'status': 'OK'
        }

    return HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail='Invalid token',
        headers={'WWW-Authenticate': 'Bearer'}
    )

@app.post('/user/me')
async def user_login(user: user_pydenticIn=Depends(get_current_user)):
    business = await Business.get(owner=user)
    logo = business.logo
    logo_path = 'localhost:8000/static/images/' + logo

    return {
        'status': 'OK',
        'data': {
            'username': user.username,
            'email': user.email,
            'verified': user.is_verified,
            'join_date': user.join_date.strtime('%b %d %Y'),
            'logo': logo_path
        }
    }

@app.post('/token')
async def generate_token(request_form: OAuth2PasswordRequestForm = Depends()):
    token = await token_generator(request_form.username, request_form.password)
    return {'access_token': token,
            'token_type': 'bearer'}

@app.get('verification/', response_class=HTMLResponse)
async def email_verification(request: Request, token: str):
    user = await verify_token(token)

    if user and not user.is_verified:
        user.is_verified = True
        await user.save()
        return templates.TemplateResponse(
            'verification.html',
            {'request': request,
             'username': user.username},
        )
    raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail='Invalid token',
            headers={'WWW-Authenticate': 'Bearer'}
    )

@post_save(User)
async def create_business(sender: 'Type[User]', instance: User, created:bool,
    using_db: 'Optional[BaseDBAsyncClient]', updated_fields: List[str]) -> None :
    if created:
        business_obj = await Business.create(
            name=instance.username,
            owner = instance,
        )

        await business_pydentic.from_tortoise_orm(business_obj)
        await send_email([instance.email], instance)

@app.post('/registration')
async def user_registration(user: user_pydenticIn):
    user_info = user.dict(exclude_unset=True)
    user_info['password'] = get_hashed_password(user_info['password'])
    user_obj = await User.create(**user_info)
    new_user = await user_pydentic.from_tortoise_orm(user_obj)
    return {
        'status': 'OK',
        'data': f'Hello, {new_user.username}, thanks for your registration,  check your email'
    }

@app.get('/')
def index():
    return {'Message': 'Hello World!'}

@app.post('/uploadfile/profile')
async def create_upload_file(file: UploadFile=File(...), user: user_pydentic=Depends(get_current_user)):
    FILEPATH = './static/images/'
    filename = file.filename
    extension = filename.split('.')[1]

    if extension not in ['jpg', 'png']:
        return {
            'status': 'ERROR',
            'detail': 'File extension not allowed'
        }

    token_name = f'{secrets.token_hex(10)}.{extension}'
    generated_name = FILEPATH + token_name
    file_content = await file.read()

    with open(generated_name, 'wb') as file:
        file.write(file_content)

    img = Image.open(generated_name)
    img = img.resize(size=(200, 200))
    img.save(generated_name)

    file.close()

    business = await Business.get(owner=user)
    owner = await business.owner

    if owner == user:
        business.logo = token_name
        await business.save()

    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail='Not Authenticated to perform this action',
            headers={'WWW-Authenticate': 'Bearer'}
        )

    file_url = 'localhost:8000' + generated_name[1:]

    return {
        'status': 'OK',
        'filename': file_url
    }

@app.post('/uploadfile/product/{id}')
async def create_upload_file(id: int, file: UploadFile=File(...),
                                user: user_pydentic=Depends(get_current_user)):
    FILEPATH = './static/images/'
    filename = file.filename
    extension = filename.split('.')[1]

    if extension not in ['jpg', 'png']:
        return {
            'status': 'ERROR',
            'detail': 'File extension not allowed'
        }

    token_name = f'{secrets.token_hex(10)}.{extension}'
    generated_name = FILEPATH + token_name
    file_content = await file.read()

    with open(generated_name, 'wb') as file:
        file.write(file_content)

    img = Image.open(generated_name)
    img = img.resize(size=(200, 200))
    img.save(generated_name)

    file.close()

    product = await Product.get(id=id)
    business = await Product.business
    owner = await business.owner

    if owner == user:
        product.product_image = token_name
        await product.save()

    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail='Not Authenticated to perform this action',
            headers={'WWW-Authenticate': 'Bearer'}
        )

    file_url = 'localhost:8000' + generated_name[1:]

    return {
        'status': 'OK',
        'filename': file_url
    }

@app.put('/product/{id}')
async def update_product(id: int, update_info: product_pydenticIn, user: user_pydentic=Depends(get_current_user)):
    product = await Product.get(id=id)
    business = await product.business
    owner = await business.owner

    update_info = update_info.dict(exclude_unset=True)
    update_info['date_published'] = datetime.utcnow()

    if owner == user and update_info['original_price'] >= 0:
        update_info['percentage_discount'] = ((update_info['original_price'] - update_info['new_price']) / update_info['original_price']) * 100

        product = await product.update_from_dict(update_info)
        await product.save()
        response = await product_pydentic.from_tortoise_orm(product)

        return {
            'status': 'OK',
            'data': response
        }

    raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail='Not Authenticated to perform this action',
            headers={'WWW-Authenticate': 'Bearer'}
        )

@app.put('/business/{id}')
async def update_business(id: int, update_business: business_pydenticIn, user: user_pydentic=Depends(get_current_user)):
    update_business = update_business.dict()
    business = await Business.get(id=id)
    owner = await business.owner

    if user == owner:
        await business.update_from_dict(update_business)
        await business.save()
        response = await business_pydentic.from_tortoise_orm(business)

        return {
            'status': 'OK',
            'data': response
        }

    raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail='Not Authenticated to perform this action',
            headers={'WWW-Authenticate': 'Bearer'}
        )

register_tortoise(
    app,
    db_url='sqlite://database.sqlite3',
    modules={'models': ['db_models']},
    generate_schemas=True,
    add_exception_handlers=True,
)


#!/usr/bin/env python
"""A script to normalized interview transcripts.
It outputs a single text file with cleaned lines
one sentence per line"""

import argparse
import re
import string
import spacy

fillers = [
    "eh",
    "m",
    "mm",
    "mmm",
    "ah",
    "ahm",
    "ehm",
    "yy",
    "y",
    "aha",
    "a-ha",
    "aa",
    "e",
    "ee",
    "łyy",
    "ym",
    "yym",
    "ymm",
    "yyym",
    "oh",
    "am",
    "oo",
    "hm",
    "em",
    "emm",
    "eem",
    "yyo",
    "ii",
    "nnn",
    "nn",
    "no",
    "mhm",
    "am",
    "amm",
    "aam",
    "eey",
    "eeyy",
    "mmyy",
    "yhm",
    "ymhm",
    "mmy",
    "yynn",
    "li",
    "cc",
]

nlp = spacy.load("pl_core_news_lg")
punctuation = string.punctuation + '…' +'–' + '’' + "‘"

def get_data(path: str) -> list:
    """reads .txt file into a list of strings"""
    list_of_lines = []
    with open(path, "r") as source:
        for line in source:
            if line == False:
                continue
            else:
                line = line.lstrip().rstrip()
                list_of_lines.append(line)
    return list_of_lines

def write_data(data: list, path: str):
    """writes data line by line into a file"""
    with open(path, "w") as sink:
        for sentence in data:
            print(sentence, file=sink)

def remove_fillers(line: str) -> str:
    """removes filler expresisons"""
    tokens = line.split()
    for word in tokens:
        if word in fillers:
            tokens.remove(word)
    no_fillers = " ".join(word for word in tokens)
    return no_fillers

def pre_tokenization(data: list) -> list:
    """data normalization to be performed before sentence tokenization"""
    cleaned = []
    for line in data:
        # replace ';' and '%' with 'ł'
        add_l = re.sub(r"[;%]", "ł", line)
        # replace the elipses with whitespaces to account for certain types of stutters
        no_elipses = re.sub(r"[…]", " ", add_l)
        # replace two period elipses with whitespace
        two_periods = re.sub(r"\.\.", " ", no_elipses)
        # remove hyphenated stutters
        no_stutters = re.sub(r"\b[a-zA-ZżźćńółęąśŻŹĆĄŚĘŁÓŃ]+-+\W", "", two_periods)
        # remove digits and numbers
        no_numbers = re.sub(r"(?:[+-]|\()?\$?\d+(?:,\d+)*(?:\.\d+)?\)?", "", no_stutters)
        # remove bracketed content
        no_brackets = re.sub(r"\[.*?\]", "", no_numbers)
        # remove content in parentheses
        no_parens = re.sub(r"\(.*?\)", "", no_brackets)
        # remove all duplicate words
        # retain only the first word
        no_duplicates = re.sub(r"\b(\w+)(?:\W+\1\b)+", r"\1", no_parens)
        # append only non-empty strings
        if no_duplicates:
            cleaned.append(no_duplicates)
    return cleaned

def make_continuous(data: list) -> string:
    """joins a list of strings into one long string"""
    one_line = " ".join(data)
    return one_line

def post_tokenization(data: list) -> list:
    """data normaization to be performed after sentence tokenization"""
    cleaned = []
    for sentence in data:
        # casefold and strip
        casefolded = sentence.lower().lstrip().rstrip()
        # standardize quotation marks
        standard_quotes = re.sub(r"[„”“]", '"', casefolded)
        # remove punctuation
        no_polish_punctuation = standard_quotes.translate(str.maketrans("", "", punctuation))
        # remove the hyphens
        no_hyphens = re.sub(r"-", " ", no_polish_punctuation)
        # remove the fillers
        no_fillers = remove_fillers(no_hyphens)
        # remove duplicates left over after the fillers were removed
        # leave only the first word
        no_duplicates = re.sub(r"\b(\w+)(?:\W+\1\b)+", r"\1", no_fillers)
         # remove multiple white spaces
        single_spaces = re.sub(" +", ' ', no_duplicates)
        if single_spaces:
            cleaned.append(single_spaces)
    return cleaned

def main(args: argparse.Namespace) -> None:
    lines = get_data(args.input)
    cleaned = pre_tokenization(lines)
    one_line = make_continuous(cleaned)
    to_tokenize = nlp(one_line)
    sent_tokenized = [sentence.text for sentence in to_tokenize.sents]
    cleaned_again = post_tokenization(sent_tokenized)
    write_data(cleaned_again, args.output)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--input", help="path to source file")
    parser.add_argument("--output", help="path to output")
    main(parser.parse_args())


#important topics
#inheritance and incapsulation

#here varbale will be default public

#if you want to make variable private use __variable
class Person:
    __age=89
    def __init__(self):
        self.first_name="Ram"
        self.last_name="Thapa"
        self.age=23

p=Person()
print(p.first_name)
print(Person.__age);#it will give error because it is private

#getter and setter are used to access the  private variable
class Person:
    __age=89
    def __init__(self):
        self.first_name="Ram"
        self.last_name="Thapa"
        self.age=23
    #getter is used to get the value of the private variable
    def get_age(self):
        return self.__age

    #setter is used to set the value of the private variable
    def set_age(self,age):
        if age>0:
            self.__age=age
        else:
            print("age should be greater than 0")

print(Person.get_age())

#getter has one parameter which is self
#setter have two parameter one is self and other is new value


#Import random
import random
#Create the function below:
inte=random.randint(1,5)
entero=inte
row=[]
matrix=[]
def matrixBuilder(entero):
    for i in range(entero):
       row.append(1)
    for i in range(entero):
        global matrix
        matrix.append(row)
    return matrix
print(matrixBuilder(entero))


import  os

etl_repo_home="/opt/pentaho/repositories/grip-pentaho-di-reports/ETL/reports"
#Creating Base directory
mifid2_base_dir="mifid"

mifid2tr_rg_dirs=['mifid2-tr\mifid2-tr-reports-trade',
                  'mifid2-tr\mifid2-tr-reports-correction',
                  'mifid2-tr\mifid2-tr-reports-automation']

mifid2cpr_rg_dirs=['mifid2-cpr\mifid2-cpr-reports-position',
                   'mifid2-cpr\mifid2-cpr-reports-correction',
                   'mifid2-cpr\Mifid2-cpr-reports-automation']

try:
    os.chdir(etl_repo_home)
except:
    print("Not able to Change the directory to ETL repository Home")
    exit()

if os.path.isdir(mifid2_base_dir):
    os.chdir(mifid2_base_dir)
else:
    try:
        os.makedirs(mifid2_base_dir)
        os.chdir(mifid2_base_dir)
    except:
        print("Not able to Change the directory to ETL repository Home")
        exit()

for dirlist in mifid2tr_rg_dirs,mifid2cpr_rg_dirs:
    for dir in dirlist:
        if os.path.isdir(dir):
            print('Directory Already Exist : ',dir)
        else:
            os.makedirs(dir)


import os
from zipfile import ZipFile, ZIP_DEFLATED

class Zip():
    """Zip up all the contents of a directory into the output file."""
    def __init__(self, input_directory, output_file):
      self.input_directory = input_directory
      self.output_file = output_file

    def zip(self):
      try:
        zip_file = ZipFile(self.output_file, 'w', ZIP_DEFLATED)
        for root, dirs, files in os.walk(self.input_directory, topdown=True):
          dir_prefix = root[len(os.path.commonprefix([self.input_directory, root]))+1:]
          if len(dirs) is 0 and len(files) is 0:
            zip_file.write(root, dir_prefix[:-1])
          else:
            for name in files:
              zip_file.write(os.path.join(root, name), os.path.join(dir_prefix, name))

      finally:
        zip_file.close()


import copy

import matplotlib.colors as colors
import matplotlib.pyplot as plt
import nibabel as nib
import numpy as np
from matplotlib import cm

from util.util import info, crop_center, error, print_timestamped

different_colors = ["#FF0000", "#008000", "#0000FF", "#FFD700",  # Red, green, blue, gold
                    "#00BFFF", "#DDA0DD", "#808080", "#800000",  # Light blue, magenta, gray, maroon
                    "#808000", "#00FF00", "#FFFF00", "#800080",  # Olive, lime, yellow, purple
                    "#008080", "#000080"]  # Teal, navy

class PlotHandler:

    def __init__(self, args, options, complementary=False):
        self.output_data_folder = options.output_data_folder
        self.phase = options.phase  # 0 search, 1 train, 2 test
        self.prefix = options.prefix
        self.mapping_source = args.mapping_source
        self.mapping_target = args.mapping_target
        self.plot_names_dict = {}
        self.plot_names_dict['source'] = self.mapping_source
        self.plot_names_dict['target'] = self.mapping_target
        self.sliced = args.sliced
        self.chosen_slice = args.chosen_slice
        self.plot_only_results = args.plot_only_results
        self.nifti_image_extension = ".nii.gz"
        self.target_shape = (210, 180)
        if self.sliced:
            self.image_extension = ".png"
        else:
            self.image_extension = self.nifti_image_extension

        # Set plot folder
        base = self.output_data_folder / args.experiment_name
        if complementary:
            specific_name = "_complementary"
        else:
            specific_name = "_" + args.method + "_main" + str(args.main_clusters) + "_sub" + str(args.sub_clusters)

        if self.phase != 1:
            specific_name += "_" + str(args.postprocess)

        if self.phase == 0 or (self.phase == 2 and args.model_phase == "search"):
            self.plot_folder = base / (args.test_set + "_search" + specific_name)
        elif self.phase == 1:
            self.plot_folder = base / (self.prefix + specific_name)
        else:
            self.plot_folder = base / (args.test_set + specific_name)

        if self.phase != 1 or not self.plot_only_results:
            self.plot_folder.mkdir(parents=True, exist_ok=True)

        self.train_folder = None
        self.labels_folder = None

        info("The plots for the current run will be saved in " + str(base) + ".")
        if not args.plot_only_results and self.phase != 2:
            self.train_folder = self.plot_folder / "train"
            self.labels_folder = self.plot_folder / "labels"
            self.train_folder.mkdir(parents=True, exist_ok=True)
            self.labels_folder.mkdir(parents=True, exist_ok=True)

            print(
                "The plots of the training images for the current run will be saved in " + str(self.train_folder) + ".")
            print("The plots of the labels for the current run will be saved in " + str(self.labels_folder) + ".")

    def plot_reference(self, reference_mri, model_folder, mris_shape, affine, method, main, sub):
        plot_nifti(reference_mri,
                   model_folder / (
                           "lab_" + method + "_main" + str(main) + "_sub" + str(sub) + self.nifti_image_extension),
                   mris_shape,
                   affine)

    def plot_train(self, visuals, patient_name, mris_shape, affine):
        if not self.plot_only_results:
            for label, image in visuals.items():
                filename = self.train_folder / (patient_name + "_" + self.plot_names_dict[label] + self.image_extension)
                if self.sliced:
                    reshaped = image.reshape(mris_shape)
                    cropped = crop_center(reshaped, self.target_shape)
                    plot_image(cropped,
                               filename,
                               colormap=cm.get_cmap('gray'),
                               mris_shape=mris_shape,
                               plotbar=False)
                else:
                    reshaped = image.reshape(mris_shape)
                    cropped = crop_center(reshaped[:, :, self.chosen_slice], self.target_shape)
                    plot_image(cropped,
                               str(filename).split(".")[0] + ".png",
                               colormap=cm.get_cmap('gray'),
                               plotbar=False)
                    plot_nifti(image,
                               filename,
                               mris_shape,
                               affine=affine)

    def plot_results(self, visuals, patient_name, smoothing, mris_shape, affine):
        self.plot_names_dict['learned_target'] = self.mapping_target + "_learned"
        self.plot_names_dict['learned_target_smoothed'] = self.mapping_target + "_learned_" + smoothing
        for label, image in visuals.items():
            if 'truth' not in label:
                folder = self.plot_folder / patient_name
                folder.mkdir(parents=True, exist_ok=True)
                filename = folder / (patient_name + "_" + self.plot_names_dict[label] + self.image_extension)
                if self.sliced:
                    reshaped = image.reshape(mris_shape)
                    cropped = crop_center(reshaped, self.target_shape)
                    plot_image(cropped,
                               filename,
                               colormap=cm.get_cmap('gray'),
                               mris_shape=mris_shape,
                               plotbar=False)
                else:
                    reshaped = image.reshape(mris_shape)
                    cropped = crop_center(reshaped[:, :, self.chosen_slice], self.target_shape)
                    plot_image(cropped,
                               str(filename).split(".")[0] + ".png",
                               colormap=cm.get_cmap('gray'),
                               plotbar=False)
                    plot_nifti(image,
                               filename,
                               mris_shape,
                               affine=affine)

    def plot_shaded_labels(self, patient_name, labels1, labels2, method, main_clusters, mris_shape, affine):
        m1_filename = self.labels_folder / (
                patient_name + "_" + self.mapping_source + "_labels_" + method + self.image_extension)
        m2_filename = self.labels_folder / (
                patient_name + "_" + self.mapping_target + "_labels_" + method + self.image_extension)
        if self.sliced:
            reshaped1 = labels1.reshape(mris_shape)
            cropped1 = crop_center(reshaped1, self.target_shape)
            plot_image(cropped1,
                       m1_filename,
                       shaded_labels=1.0,
                       colormap=colormap_fusion(main_clusters),
                       mris_shape=mris_shape,
                       plotbar=False)
            reshaped2 = labels2.reshape(mris_shape)
            cropped2 = crop_center(reshaped2, self.target_shape)
            plot_image(cropped2,
                       m2_filename,
                       shaded_labels=1.0,
                       colormap=colormap_fusion(main_clusters),
                       mris_shape=mris_shape,
                       plotbar=False)
        else:
            reshaped1 = labels1.reshape(mris_shape)
            cropped1 = crop_center(reshaped1[:, :, self.chosen_slice], self.target_shape)
            plot_image(cropped1,
                       str(m1_filename).split(".")[0] + ".png",
                       shaded_labels=1.0,
                       colormap=colormap_fusion(main_clusters),
                       plotbar=False)
            reshaped2 = labels2.reshape(mris_shape)
            cropped2 = crop_center(reshaped2[:, :, self.chosen_slice], self.target_shape)
            plot_image(cropped2,
                       str(m2_filename).split(".")[0] + ".png",
                       shaded_labels=1.0,
                       colormap=colormap_fusion(main_clusters),
                       plotbar=False)
            plot_nifti(labels1,
                       m1_filename,
                       mris_shape,
                       affine=affine)
            plot_nifti(labels2,
                       m2_filename,
                       mris_shape,
                       affine=affine)

    def print_tumour(self, tumour, patient_name, mris_shape, affine):
        if not self.plot_only_results:
            folder = self.plot_folder / patient_name
            folder.mkdir(parents=True, exist_ok=True)
            filename = folder / (patient_name + "_truth_tumour" + self.image_extension)
            if self.sliced:
                plot_image(tumour,
                           filename,
                           mris_shape=mris_shape,
                           plotbar=False)
            else:
                reshaped = tumour.reshape(mris_shape)
                cropped = crop_center(reshaped[:, :, self.chosen_slice], self.target_shape)
                plot_image(cropped,
                           str(filename).split(".")[0] + ".png",
                           plotbar=False)
                plot_nifti(tumour,
                           filename,
                           mris_shape,
                           affine=affine)

def plot_image(image,
               filename,
               colormap=copy.copy(cm.get_cmap('viridis')),
               mris_shape=None,
               shaded_labels=None,
               one_int_bounds=False,
               plotbar=True,
               white_bg=False,
               verbose=True):
    if plotbar:
        res_size1 = 6
        res_size2 = 5
    else:
        res_size1 = res_size2 = 5
    fig = plt.figure(figsize=(res_size1, res_size2), dpi=300)
    # ax = plt.gca()
    if len(image.shape) == 1:
        if mris_shape is not None:
            image = image.reshape(mris_shape)
        else:
            error("The image cannot be reshaped and showed with imshow")
    elif len(image.shape) > 2:
        error("The image has a shape greater than 2. You might have forgotten to slice it.")
    image = np.rot90(image, k=-1)
    image = np.flip(image, axis=1)
    if shaded_labels is None:
        max_lin = np.max(image)
        bounds = None
    else:
        n_clusters = int(colormap.N / 256)
        max_lin = shaded_labels
        bounds = np.linspace(0, max_lin, n_clusters + 1)
    if one_int_bounds and max_lin < 15:
        bounds = range(int(max_lin) + 1)
    min_val = np.min(image) + 0.1e-10
    if min_val > max_lin:
        min_val = max_lin
    if colormap != cm.get_cmap('gray'):
        colormap.set_under('w')
    plt.axis('off')
    plt.xticks([])
    plt.yticks([])
    sc = plt.imshow(image,
                    cmap=colormap,
                    vmin=min_val,
                    vmax=max_lin)
    if plotbar:
        # divider = make_axes_locatable(ax)
        # cax = divider.append_axes("right", size="5%", pad=0.05)
        cax = None
        plt.colorbar(sc, cax=cax, ticks=bounds)

    plt.savefig(filename, bbox_inches='tight', transparent=not white_bg)
    if verbose:
        print_timestamped("Saved in " + str(filename))
    plt.close(fig)

def plot_nifti(image, filename, mris_shape=None, affine=None, verbose=True):
    if affine is None:
        affine = np.array([[-1., 0., 0., -0.],
                           [0., -1., 0., 239.],
                           [0., 0., 1., 0.],
                           [0., 0., 0., 1.]])
    if len(image.shape) == 1:
        if mris_shape is not None:
            image = image.reshape(mris_shape)
        else:
            error("The image cannot be reshaped, please add the shape.")
    new_nifti = nib.Nifti1Image(image, affine=affine)
    nib.save(new_nifti, filename)
    if verbose:
        print_timestamped("Saved in " + str(filename))

def colormap_fusion(n_clusters):
    if n_clusters > len(different_colors):
        error("The number of clusters is greater than the available size of colours.")
    stacked_colors = []
    for i in range(n_clusters):
        colormap = shaded_color_map(different_colors[i])
        linspace_colormap = colormap(np.linspace(0.20, 1, 256))
        stacked_colors.append(linspace_colormap)

    newcolors = np.vstack(stacked_colors)
    return colors.ListedColormap(newcolors)

def full_colormap_fusion(n_clusters):
    if n_clusters > len(different_colors):
        error("The number of clusters is greater than the available size of colours.")
    return colors.ListedColormap(different_colors[:n_clusters])

def shaded_color_map(rgb_color):
    return colors.LinearSegmentedColormap.from_list("", ["white", rgb_color])


import numpy as np
import scipy.stats as stats

def gauss(x,mz,sigma,A):
    return A*np.exp(-(x-mz)**2/2/sigma**2)

def sig(respwr, mz):
        return mz/(2.355*respwr)

def peaklist(filename):
    arr = []
    pknums = []
    f = open(filename)
    for line in f:
        if 'Num peaks:' in line:
            x = line.split(' ')
            pknums.append(int(x.pop(-1)))
        if ('Name:' not in line and 'MW:' not in line and 'Comment:' not in line
         and 'Num' not in line and line != '\n'):
            arr1 = line.split('\t')
            arr2 = arr1.pop(-1)
            arr.append(arr1)
    arr = np.array(arr).astype(np.float)
    f.close()
    return arr

# specsim.genMS(mz, win, pkvar = 0.2, noiselevel = 5e-3, bsline = False,
# baselevel = 0.05, rp = 4000, points = 30000)
# Generate mass spectrum with dict input mz
# Inputs:
# mz - type: dict, format {'mz1':[m/z, int],.....} where m/z is the peak center
#                      and int is the signal height on a scale [0,1]
# win - spectral window on which to create spectrum, m/z 100 - m/z 1000 recomm.
#
# pkvar - scale of random samples used to scale positive peak heights, 0.2
# typical
#
# noiselevel - height of electronics noise
#
# bsline - if True, raised exponential baseline is added at beginning of
#           spectrum, default is False.
# baselevel - coefficient to determine prominence of raised baseline
#             realistic values ~ 0.05 - 0.2
# misalign - option to add slight random shift to m/z access, simulates ppm
#            error for peak center values
# rp - resolving power of the spectrum, recommended value = 4,000
#
# points - number of data points to use, recommended = 30,000
# RETURNS:
# sigsim - vector of simulated intensity
# x - vector of m/z values over which sigsim is calculated

def genMS(mz, win, pkvar = 0.2, noiselevel = 5e-3, bsline = False,
baselevel = 0.05, misalign = False, rp = 4000, points = 30000):

    mz0 = win[0]
    mzf = win[1]

    x = np.linspace(mz0,mzf, points)
    sigsim = np.zeros(points)

    # Add random shift to m/z axis
    if misalign == True:
        # Generate spectrum with Gaussian peaks and small random peak shift
        for key in mz:
            sigsim += gauss(x, mz[key][0], sig(rp,mz[key][0]+
                        np.random.normal(0,0.005)), mz[key][1])
    else:
        # Generate spectrum with Gaussian peaks
        for key in mz:
            sigsim += gauss(x, mz[key][0], sig(rp,mz[key][0]), mz[key][1])

    # Add baseline rise at low mass end
    if bsline == True:
        baseline = baselevel*np.exp(-1*np.linspace(0,5,points))
        sigsim += baseline

    # Add peak variance
    for i in range(points):
        if sigsim[i] > 0:
            sigsim[i] *= abs(1 + np.random.normal(0,pkvar))

    # Add electronic baseline noise
    noise = np.random.normal(0,1,points)*noiselevel
    sigsim += noise

    return x, sigsim

def file_sz(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    k = l.split(' ')
    k.pop(-1)
    return i + 1, len(k)

def importgendat(fileloc):
    points, n = file_sz(fileloc)
    arr = np.empty((points, n))
    f = open(fileloc)
    i = 0
    for line in f:
        arr1 = line.split(' ')
        b = arr1.pop(-1)
        arr2 = np.array(arr1).astype(np.float)
        arr[i,:] = arr2
        i += 1
    mz = arr[:,0]
    signal = arr[:,1:]
    return mz, signal


# This program adds two numbers

num1 = 1
num2 = 2

# Add two numbers
sum = num1 + num2

# Display the sum
print('The sum is:', sum)

# finding the Average of 2 numbers given by Valar Mam

A = 1
B = 2

# avg

avg = (A + B) / 2
print("average of A & B =", avg)

# finding the apple task given by Valar Mam

my_list = ["apple", "banana", "cherry"]
# if element present then return
# exist otherwise not exist
# Checking if apple exists in list
#print("for apple in test_list:")
if "apple" in my_list:
    print("apple Exists")
else:
    print("apple dose not exist")

#  change value of first item to "mango" in the given list by valar mam

my_list[0]="mango"
print("after changing value",my_list)

#how will you insert new element 'watermelon' to then given list

my_list.append("watermelon")
print("after adding new element",my_list)

# create a tuple with only one item and will tuple allow  duplicate values explain with examples

names=('valr','valr','karthi', 'balaji','valr')

print('names',names)

animals=('Valar',)
print('list of animals',animals)

# create dictionary and get value of first key?

useless = {1:'Amma',2:'Amma1', 'valar':'very useless'}
print("list of useless things",useless)
useless[1]
print('first element',useless[1])

#will dictionary allows duplicate values with same key ? explain with example?

useless = {1:'Amma',1:'Amma', 'valar':'very useless'}
print('check for duplicate',useless)

#Create set and delete the last element from set by valar mam.

a={1,2,3,4,5}
a.remove(5)
print("removing the last element",a)

#A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7, 8} are two set elements .how will you get the common elements from in both the sets?

a = {1, 2, 3, 4, 5}
b = {4, 5, 6, 7, 8}
print("intersection",a.intersection(b))


#!/usr/bin/python3
#encoding: UTF-8

import lxml.etree as ET
import markdown as MD
import lib as LIB

#-------------------------------------------------------------------------------
def xpath_list(from_node, xpath):
  """
     Return all nodes matching xpath from from_node as dom node list.
  """
  if isinstance(from_node, ET._Element):
    ret = from_node.xpath(xpath)
  else:
    ret=[]

  return ret

#-------------------------------------------------------------------------------
def xpath_num_sorted(from_node, xpath, xp_key):
  """
     Return all nodes matching xpath from from_node as dom node list num sorted
     on xp_key xpath (relative to each item).
  """
  all = xpath_list(from_node, xpath)
  all_sorted = sorted(all, key=lambda itm: xpath_int(itm, xp_key))
  return all_sorted

#-------------------------------------------------------------------------------
def xpath_alpha_sorted(from_node, xpath, xp_key):
  """
     Return all nodes matching xpath from from_node as dom node list alpha sorted
     on xp_key xpath (relative to each item).
  """
  all = xpath_list(from_node, xpath)
  all_sorted = sorted(all, key=lambda itm: xpath_plain(itm, xp_key))
  return all_sorted

#-------------------------------------------------------------------------------
def xpath_node(from_node, xpath):
  """
     Return first node matching xpath from from_node as dom node.
  """
  return LIB.first(xpath_list(from_node, xpath))

#-------------------------------------------------------------------------------
def xpath_plain(from_node, xpath):
  """
     Return first node matching xpath from from_node as plain text.
  """
  return LIB.first_str(xpath_list(from_node, xpath))

#-------------------------------------------------------------------------------
def xpath_int(from_node, xpath):
  """
     Return first node matching xpath from from_node as integer.
  """
  return int(LIB.first_str(xpath_list(from_node, xpath)))

#-------------------------------------------------------------------------------
def xpath_md(from_node, xpath):
  """
     Return first node matching xpath from from_node as markdown translated to HTML.
     /!\ just for simple paragraphs (no images, no extern refs...)
  """
  return MD.markdown(LIB.first_str(xpath_list(from_node, xpath)))

#-------------------------------------------------------------------------------
def add_error(meta, fct, errlevel, errno, errmsg, line, text):
  """
     Add new error to meta node of an XML document.
  """
  if (meta is not None):
    err_node = ET.SubElement(meta, "error", {"fct":fct, "errlevel":errlevel, "errno":errno, "errmsg":errmsg, "line":str(line)})
    err_node.text = str(text)
    ret = err_node

  else:
    ret = None

  return ret

#-------------------------------------------------------------------------------
def add_external_ref(meta, ext_ref, from_ref):
  """
     Add new external_ref to meta node of an XML document.
  """
  if (meta is not None):
    ext_node = ET.SubElement(meta, "external", {"to_ref":ext_ref, "from_ref":from_ref})
    ret = ext_node

  else:
    ret = None

  return ret

#-------------------------------------------------------------------------------
def add_attachment(meta, to_path, ext, file):
  """
     Add new attachment to meta node of an XML document.
  """
  ret=None
  if (meta is not None):
    if len(xpath_list(meta,"attachment[@to_path='"+to_path+"']"))==0:
      att_node = ET.SubElement(meta, "attachment", {"to_path":to_path, "ext":ext, "file":file})
      ret = att_node

  return ret

#-------------------------------------------------------------------------------
def extend_to_connected(all_docs, ref_set):
  """
     Collect all connected references at any level.
  """
  new_ref_set = set() | ref_set
  for ref in ref_set:
    to_refs = xpath_list(all_docs, ".//external[@from_ref='"+ref+"']/@to_ref")
    new_ref_set |= set(to_refs)
    #from_refs = xpath_list(all_docs, ".//_meta/external[@to_ref='"+ref+"']/@from_ref")
    #new_ref_set |= set(from_refs)

  if (len(new_ref_set) != len(ref_set)):
    new_ref_set = extend_to_connected(all_docs, new_ref_set)

  return new_ref_set

#-------------------------------------------------------------------------------
def add_dict_as_xml(parent, a_dict):
  """
     Add to parent all nodes corresponding to tree structure contained in dict.
  """
  ret=parent
  if (parent is not None):
    for name0, value in a_dict.items():
      name = name0.lower()
      if isinstance(value, str) :     #simple string
        new = ET.SubElement(parent, name).text=value

      elif isinstance(value, list) :  #array
        for v in value:
          new = ET.SubElement(parent, name).text=v

      elif isinstance(value, dict) :  #dictionnary
        new = ET.SubElement(parent, name)
        add_dict_as_xml(new, value)

      else :
        new = ET.SubElement(parent, name)

  return ret


from unittest.mock import patch

from family_foto import Role, add_user

def mock_user(test_case, user_name, role_name, active=None) -> None:
    """
    Mocks a user on current_user
    :param test_case: test case class, where the user should be mocked
    :param user_name: name of the mocked user
    :param role_name: string of the role name
    :param active: if the user is already approved
    """
    test_case.patcher = patch('flask_login.utils._get_user')
    test_case.mock_current_user = test_case.patcher.start()
    user_role = Role.query.filter_by(name=role_name).first()
    user = add_user(user_name, '1234', [user_role])
    test_case.mock_current_user.return_value = user
    test_case.mock_current_user.id = user.id
    test_case.mock_current_user.active = user.active if active is None else active


from sqlite3 import *
from typing import Union

class DB:
    def __init__(self):
        self.db = connect("app.db")
        self.cr = self.db.cursor()
        self.cr.execute("create table if not exists `users`(user_id INTEGER, username TEXT, chat_name TEXT, "
                        "chat_username TEXT, chat_id INTEGER)")

    def __del__(self):
        self.db.close()

    async def get_user(
            self, user_id=None, username=None
    ) -> list:

        self.cr.execute("select * from `users` where user_id = ?", (user_id,)) if user_id else self.cr.execute(
            "select * from `users` where username = ?", (username,))
        data = self.cr.fetchall()
        return data

    async def get_all(
            self
    ) -> list:
        self.cr.execute("select * from users")
        res = self.cr.fetchall()
        return res

    async def insert_user(
            self,
            user_id: Union[str, int],
            username: str,
            chat_id: Union[str, int],
            chat_name: str,
            chat_username: str
    ):
        self.cr.execute("select * from `users` where user_id = ? and chat_id = ?", (user_id, chat_id))
        results = self.cr.fetchall()
        if results:
            return
        self.cr.execute(
            "insert into `users`(user_id, username, chat_id, chat_name, chat_username) values(?, ?, ?, ?, ?)",
            (user_id, username, chat_id, chat_name, chat_username)
        )
        self.db.commit()


import os
# redis db
REDIS_URL = os.environ.get('REDIS_URL') or 'http://redis:6379/1'
REDIS_SET_NAME = 'cookiejars'
COOKIES_POOL_SIZE = 10
# 查询结果缓存时间
RESULT_EXPIRE = 24*60*60

# for selenium
CHROME_DRIVER_PATH = '/Users/apple/phantomjs-2.1.1-macosx/bin/chromedriver'
CHROME_BASE_URL = "https://www.sogou.com/"

# for server
IP = '0.0.0.0'
PORT = 6789
WORKER = 4

# download
TIMEOUT = 10
RETRY = 3


'''
a = 2
b = 4
if a > b:
    print ("Hola")
elif b > a:
    print ("Hola2")

# Una tupla no se incrementa, es estatico, un arreglo si.
#  (1,2,3) TUPLA
#  [1,2,3...] ARREGLO/LISTA
c = (2,3,4)
print(type(c))
d = [3,6,4]
print(type(d))
'''

# ------------------------------------- EJERCICIO -------------------------------------

'''
Averiguar si un ano es bisiesto o no. Si el ano no es divisible entre 4, es un anio comun. Si el anio no es divisible por 100, es un ano bisiesto
o si no, si el numero no es divisible entre 400, es un ano comun, si no, es un ano bisiesto.

Tip, usa != y %.
'''
'''
ano = int (input("Dame el ano "))
if ano < 1582:
    print("No gregoriano")
elif ano % 4 != 0:
    print("Common year")
elif ano % 100 != 0:
    print ("Leap year")
elif ano % 400 != 0:
    print ("Common year")
else:
    print ("Leap year")

# Las asignaciones se hacen en paralelo, no en serie.
# No do while.

'''
'''
a = 1
i = 1
while a > 0:
    i = i+1
    print(i)
'''

even_numbers = 0
odd_numbers = 0
number = int(input("Enter a number"))
while number != 0:
    number = int(input("Enter a number"))
    if number % 2 == 0:
        even_numbers += 1
    else:
        odd_numbers += 1
print ("Odd numbers:", odd_numbers)
print ("Even numbers:", even_numbers)

counter = 3
while counter: #Sin condicionantes, mientras el numero sea diferente de 0, TRUE, igual a 0, FALSE
    print (counter)
    counter -= 1
print ("outside the loop")


from django.shortcuts import render, redirect, get_object_or_404
from users.models import Profile
from .models import *
from addproject.models import *
from datetime import datetime
from django.shortcuts import render, redirect
from addproject.models import *
import json
import datetime
from django.http import JsonResponse
from addproject.models import *
from users import *
import sys, os
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
from addproject import models
from django.core.paginator import Paginator

def showmain(request):
    calendar = Calendar.objects.filter(writer=request.user, endday__contains=datetime.date.today(
    )).order_by('endday')  # 글을 작성한 유저의 캘린더 정보만 가져오겠다. 가까운 날짜 순으로 정렬
    projects = Project.objects.filter(followers=request.user)
    profile = Profile.objects.all()
    posts = Post.objects.all().order_by('-day')
    return render(request, 'mateapp/mainpage.html', {'calendar': calendar, 'projects':projects,'posts':posts, })

def showevent(request):
    if request.method == 'POST':
        date_num = json.loads(request.body)
        year = datetime.date.today().year
        month = datetime.date.today().month
        calendar = Calendar.objects.filter(writer=request.user, endday__contains=datetime.date(
            year, month, int(date_num))).order_by('endday')

        if calendar.count() == 1:
            c0_title = calendar[0].title
            c0_startday = calendar[0].startday
            c0_endday = calendar[0].endday
            c0_starttime = calendar[0].starttime
            c0_endtime = calendar[0].endtime
            c0_place = calendar[0].place
            c0_body = calendar[0].body
            c0_color = calendar[0].color
            c1_title = None
            c1_startday = None
            c1_endday = None
            c1_starttime = None
            c1_endtime = None
            c1_place = None
            c1_body = None
            c1_color = None
            context = {
            "status": "exist1",
            "title1": c0_title,
            "startday1": c0_startday,
            "endday1": c0_endday,
            "starttime1": c0_starttime,
            "endtime1": c0_endtime,
            "place1": c0_place,
            "body1" : c0_body,
            "color1" : c0_color,
            "title2": c1_title,
            "startday2": c1_startday,
            "endday2": c1_endday,
            "starttime2": c1_starttime,
            "endtime2": c1_endtime,
            "place2": c1_place,
            "body2" : c1_body,
            "color2" : c1_color,
        }
        elif calendar.count() >= 2:
            c0_title = calendar[0].title
            c0_startday = calendar[0].startday
            c0_endday = calendar[0].endday
            c0_starttime = calendar[0].starttime
            c0_endtime = calendar[0].endtime
            c0_place = calendar[0].place
            c0_body = calendar[0].body
            c0_color = calendar[0].color
            c1_title = calendar[1].title
            c1_startday = calendar[1].startday
            c1_endday = calendar[1].endday
            c1_starttime = calendar[1].starttime
            c1_endtime = calendar[1].endtime
            c1_place = calendar[1].place
            c1_body = calendar[1].body
            c1_color = calendar[1].color
            context = {
            "status": "exist2",
            "title1": c0_title,
            "startday1": c0_startday,
            "endday1": c0_endday,
            "starttime1": c0_starttime,
            "endtime1": c0_endtime,
            "place1": c0_place,
            "body1" : c0_body,
            "color1" : c0_color,
            "title2": c1_title,
            "startday2": c1_startday,
            "endday2": c1_endday,
            "starttime2": c1_starttime,
            "endtime2": c1_endtime,
            "place2": c1_place,
            "body2" : c1_body,
            "color2" : c1_color,
        }

        else:
            context = {"status": "null"}
        return JsonResponse(context)

def login(request):
    if request.user.is_authenticated:
        projects = Project.objects.all()
        return render(request, 'mateapp/mainpage.html', {'projects':projects})

    else:
        return render(request, 'account/login.html')

def create_schedule(request):
    projecttitles = Project.objects.filter(writer=request.user)
    if request.method == 'POST':
        new_schedule = Calendar()
        new_schedule.title = request.POST['title']
        new_schedule.writer = request.user
        new_schedule.body = request.POST['body']
        new_schedule.startday = request.POST.get('startday')
        new_schedule.endday = request.POST.get('endday')
        new_schedule.starttime = request.POST.get('starttime')
        new_schedule.endtime = request.POST.get('endtime')
        new_schedule.place = request.POST['place']
        new_schedule.save()
        return redirect('mateapp:calendar')
    else :
        new_schedule = Calendar.objects.all()
        return render(request, 'mateapp/create_schedule.html',{'new_schedule':new_schedule, 'projecttitles':projecttitles})

def calendar(request):
    calendar = Calendar.objects.filter(writer=request.user)  # 글을 작성한 유저의 캘린더 정보만 가져오겠다. 가까운 날짜 순으로 정렬
    calendars = Calendar.objects.filter(writer=request.user)
    schedules_list = []
    schedules = Calendar.objects.filter(writer=request.user)
    schedules_list.append(schedules)
    # 간트차트

    projects = Project.objects.all() # 모델을 전부 불러옴
    todos_list = [] # 빈리스트를 만듬 , 담아서 렌더링하는 경우가 많음
    todos = Calendar.objects.filter(writer=request.user)
    todos_list.append(todos) # 그 프로젝트의 등록된 투두를 불러와서 그걸 넣은거임
        # 보내고 싶은거 리스트로 보내서 장고나 뭐든 저런식으로 할 일이 많음
    #     # 알아두기
    return render(request, 'mateapp/calendar.html', {'projects':projects, 'todos_list':todos_list,'calendar':calendar, 'schedules_list':schedules_list, 'calendars':calendars})
    # 리스트 자체를 렌더링함

def timetable(request):
    if request.method == "POST":
        # Profile에서 요청받은 user의 정보만 불러옴
        profile = Profile.objects.get(user=request.user)
        profile.timetable = request.FILES.get('timetable')
        profile.save(update_fields=['timetable'])
    return redirect('mateapp:showmain')  # render 보단 redirect 가 낫다.

def project_detail(request, project_id):
    projects = Project.objects.filter(followers=request.user)
    project = Project.objects.get(pk=project_id)
    posts = Post.objects.all()
    post = Post.objects.filter(project=project)
    comment = Comment.objects.filter()
    page = int(request.GET.get('p',1))
    paginator = Paginator(post,4)
    boards = paginator.get_page(page)
    return render(request, 'mateapp/project.html', {'boards':boards, 'projects':projects,'project':project,'posts':posts, 'post':post})

# 포스트가 갖고 있는 숫자가 가장 높은걸 필터로 찾아서 오늘 날짜와 비교해서 출력함

# 게시물 CRUD

def create_post(request, project_id):
    projects = Project.objects.all()
    project = Project.objects.get(pk=project_id)
    posts = Post.objects.all()
    day = datetime.date.today()
    # post = Post.objects.get(project=project)
    if request.method == "POST":
        # project = Project.objects.get(title=project_title)
        post_title = request.POST['title']
        post_body = request.POST['body']
        Post.objects.create(title=post_title, user=request.user, project=project, body=post_body) # post는 세가지 필드가 있는데,
        # 어떤 모델이든간에 pk가 있어야함 Foreign key는 생략이 될 수가 없음, 일대다 관계일때 쓴다는건데
        #
    return redirect('mateapp:project_detail', project_id)
    # return render(request, 'mateapp/project.html', {'posts':posts,'projects':projects})

def create_comment(request, project_id, post_id):
    project = Project.objects.get(pk=project_id)
    post = Post.objects.get(pk=post_id)
    if request.method == "POST":
        post = get_object_or_404(Post,pk=post_id) #Post로 등록된 값이 잇으면 불러오고 없으면 404 출력시킴
        content = request.POST['content']
        file = request.FILES.get('file')
        Comment.objects.create(content=content, post=post, user=request.user) # 모델=뷰
    return redirect('mateapp:post_detail', project_id, post_id)
    # id는 식별값이기 때문에 무조건 존재하는 필드임

def post_detail(request, project_id, post_id):
    project = Project.objects.get(pk = project_id)
    post = Post.objects.get(pk = post_id)
    comments = Comment.objects.filter(post = post)
    page = int(request.GET.get('p',1))
    paginator = Paginator(comments,4)
    boards = paginator.get_page(page)
    return render(request, 'mateapp/project_post.html', {'boards':boards,'project':project, 'post':post, 'comments':comments})


import sys
import os

sys.path.append(os.path.join(sys.path[0], '../../bindings/python/'))
sys.path.append(os.path.join(sys.path[0], '../../bin/'))

import pySmartIdEngine

def output_recognition_result(result):
    print('Document type: %s' % result.GetDocumentType())
    print('Match results:')
    match_results = result.GetMatchResults()
    for i in range(len(match_results)):
        match_result_quad = match_results[i].GetQuadrangle()
        print('    Template Type = %s' % match_results[i].GetTemplateType())
        print('    Zone = { (%f, %f), (%f, %f), (%f, %f), (%f, %f) }' % (
            match_result_quad.GetPoint(0).x, match_result_quad.GetPoint(0).y,
            match_result_quad.GetPoint(1).x, match_result_quad.GetPoint(1).y,
            match_result_quad.GetPoint(2).x, match_result_quad.GetPoint(2).y,
            match_result_quad.GetPoint(3).x, match_result_quad.GetPoint(3).y))

    print('Segmentation results:')
    segmentation_results = result.GetSegmentationResults()
    for i in range(len(segmentation_results)):
        raw_fields_names = segmentation_results[i].GetRawFieldsNames()
        for j in range(len(raw_fields_names)):
            print('    Raw field %s' % raw_fields_names[j])

    print('String fields:')
    string_field_names = result.GetStringFieldNames()
    for i in range(len(string_field_names)):
        field = result.GetStringField(string_field_names[i])
        is_accepted = ' [+] ' if field.IsAccepted() else ' [-] '
        print('    %-20s %s %s' % (field.GetName(), is_accepted, field.GetUtf8Value()))

    print('Image fields:')
    image_field_names = result.GetImageFieldNames()
    for i in range(len(image_field_names)):
        field = result.GetImageField(image_field_names[i])
        is_accepted = ' [+] ' if field.IsAccepted() else ' [-] '
        print('    %-20s %s W: %d H: %d' % (field.GetName(), is_accepted,
                                            field.GetValue().GetWidth(), field.GetValue().GetHeight()))

    print('Result terminal:         %s' % (' [+] ' if result.IsTerminal() else ' [-] '))

def main():
    if len(sys.argv) != 4:
        print('Usage: %s <path-to-image-file> <path-to-config-bundle> <document-type>' % sys.argv[0])
        print('Example: %s ../testdata/image.png ../data-zip/bundle_mrz.zip mrz.*' % sys.argv[0])
        sys.exit(-1)

    image_path, config_path, document_types = sys.argv[1:]

    print('RecognitionEngine Version: %s' % pySmartIdEngine.RecognitionEngine.GetVersion())
    print('Creating RecognitionEngine from %s' % config_path)

    engine = pySmartIdEngine.RecognitionEngine(config_path)

    session_settings = engine.CreateSessionSettings()
    set_enabled_document_types = pySmartIdEngine.StringVector()
    set_enabled_document_types.append(document_types)
    session_settings.SetEnabledDocumentTypes(set_enabled_document_types)

# load image

    testImage = pySmartIdEngine.Image()

  # This works only in trial and full versions

    # testImage = pySmartIdEngine.Image(image_path)

# get file as binary buffer

    binaryRGBfile = open(image_path, "rb")
    size = os.path.getsize(image_path)
    binaryRGBfileBuffer = bytearray(size)
    binaryRGBfile.readinto(binaryRGBfileBuffer)

# get raw RGB data from file

  # You can take image buffer directly from Image object, works in trial and full versions

    # bufferSizeForRGB = testImage.GetHeight()*testImage.GetStride()*testImage.GetChannels()
    # bufferForRGB = bytearray(bufferSizeForRGB)
    # testImage.CopyToBuffer(bufferForRGB, bufferSizeForRGB)

# get file as Base64 string

    f = open(os.path.join(sys.path[0], "base64.txt"), 'r')
    base64BufferString = f.readline()
    f.close()

  # You can take base64 buffer directly from Image object

    # base64BufferSize = testImage.GetRequiredBase64BufferLength()
    # base64Buffer = bytearray(base64BufferSize)
    # testImage.CopyBase64ToBuffer(base64Buffer, base64BufferSize)
    # base64BufferString = base64Buffer.decode()
    # base64BufferString = str(base64BufferString) #explicit conversion need for Python 2

  # or you can take base64 string directly by

    # base64BufferString = testImage.GetBase64String()

# start recognition session

    session = engine.SpawnSession(session_settings)

    resultFromImage = session.ProcessImage(testImage)
    session.Reset()

    resultFromImageFile = session.ProcessImageFile(image_path)
    session.Reset()

    resultFromImageData = session.ProcessImageData(binaryRGBfileBuffer, size)
    session.Reset()

    resultFromBase64 = session.ProcessImageDataBase64(base64BufferString)
    session.Reset()

    # resultFromRawBuffer = session.ProcessSnapshot(bufferForRGB, bufferSizeForRGB, testImage.GetWidth(), testImage.GetHeight(), testImage.GetStride(), testImage.GetChannels())

# print result

    output_recognition_result(resultFromImage)
    output_recognition_result(resultFromImageFile)
    output_recognition_result(resultFromImageData)
    output_recognition_result(resultFromBase64)
    # output_recognition_result(resultFromRawBuffer)

if __name__ == '__main__':
    main()


from vtk import *

# input data, every row is for a different item
positions = [[0, 0, 0],[1.5, 0, 0]]

orientations = [[1.0, 0.0, 0.0],[0.0, 1.0, 1.0]]

colors = [[255, 0, 0],
          [0, 255, 255]]

heights = [1,
           2]

# rendering of those two defined cylinders
points = vtkPoints()
points.InsertNextPoint(*positions[0])
points.InsertNextPoint(*positions[1])
polydata = vtkPolyData()
polydata.SetPoints(points)

color_def = vtkUnsignedCharArray()
color_def.SetNumberOfComponents(3)
color_def.SetNumberOfTuples(polydata.GetNumberOfPoints())
color_def.InsertTuple3(0, *colors[0])
color_def.InsertTuple3(1, *colors[1])
polydata.GetPointData().SetScalars(color_def)

pointNormalsArray = vtkDoubleArray()
pointNormalsArray.SetNumberOfComponents(3)
pointNormalsArray.SetNumberOfTuples(polydata.GetNumberOfPoints())
pointNormalsArray.SetTuple(0, orientations[0])
pointNormalsArray.SetTuple(1, orientations[1])
polydata.GetPointData().SetNormals(pointNormalsArray)

cyl_source = vtkCylinderSource()
cyl_source.SetResolution(10)
cyl_source.SetHeight(0.8)
cyl_source.SetRadius(0.1)
cyl_source.Update()

glyph = vtkGlyph3D()
glyph.SetInputData(polydata)
glyph.SetSourceConnection(cyl_source.GetOutputPort())
glyph.SetColorModeToColorByScalar()
glyph.SetVectorModeToUseNormal()
glyph.ScalingOff()

mapper = vtkPolyDataMapper()
mapper.SetInputConnection(glyph.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
ren = vtkRenderer()
ren.AddActor(actor)

renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)
renwin.Render()
iren.Initialize()
iren.Start()


import os
import picamera
import numpy as np
from picamera.array import PiMotionAnalysis

# A simple demo of sub-classing PiMotionAnalysis to construct a motion detector

MOTION_MAGNITUDE = 60   # the magnitude of vectors required for motion
MOTION_VECTORS = 10     # the number of vectors required to detect motion

class MyMotionDetector(PiMotionAnalysis):
    def analyse(self, a):
        # Calculate the magnitude of all vectors with pythagoras' theorem
        a = np.sqrt(
            np.square(a['x'].astype(np.float)) +
            np.square(a['y'].astype(np.float))
            ).clip(0, 255).astype(np.uint8)
        # Count the number of vectors with a magnitude greater than our
        # threshold
        vector_count = (a > MOTION_MAGNITUDE).sum()
        if vector_count > MOTION_VECTORS:
            print('Detected motion!')

with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    camera.framerate = 24
    with MyMotionDetector(camera) as motion_detector:
        camera.start_recording(
            os.devnull, format='h264', motion_output=motion_detector)
        try:
            while True:
                camera.wait_recording(1)
        finally:
            camera.stop_recording()


from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

fcff_df = pd.read_excel('FCFF_analysis_filtered.xlsx', index_col=[0])
sgx_df = pd.read_csv('myData.csv', index_col=[1])

class StonksApp:
    def __init__(self, master):
        self.master = master
        master.title("StonkApp")

        # Initialise app variables
        self.idx = 0
        self.current_stock = fcff_df.index[self.idx]

        # Set up frame with charts and stats
        self.update_main_frame(self.generate_info_dict())

        # Set up frame for app buttons
        self.update_buttons_frame()

    def plot_chart(self, row: int, column: int, *args: pd.DataFrame, columnspan: int=2, title: str="", xlabel: str="", ylabel: str=""):
        """ Function to plot graphs on same chart from dataframes passed into the function as arguments

        :param row, column, and columnspan: variables for Tkinter grid styling
        :param title, xlablel, ylabel: variables for matplotlib chart
        :param *args: dataframes to be plotted onto chart
        """

        # Setting up of chart
        figure = plt.Figure(figsize=(6,5), dpi=70)
        ax = figure.add_subplot(111)
        line_graph = FigureCanvasTkAgg(figure, self.main_frame)
        line_graph.get_tk_widget().grid(row=row, column=column, columnspan=columnspan)

        # Plotting graphs
        for df in args:
            df.plot(kind='line', legend=True, ax=ax)

        # Chart styling
        ax.set_title(title)
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)

    def generate_info_dict(self) -> dict:
        """ Function to generate a dictionary of info name and info value pairs to be displayed in app

        :return: info_dict
        """

        info_dict = {}

        # Show name of stock
        trading_code = self.current_stock.replace(".SI", "")
        self.trading_name = sgx_df.loc[trading_code, "Trading Name"]
        info_dict["Name"] = self.trading_name

        # Show sector of stock
        self.sector = sgx_df.loc[trading_code, "Sector"]
        info_dict["Sector"] = self.sector

        # Show wacc of stock
        self.wacc = fcff_df.loc[self.current_stock, "WACC"]
        info_dict["WACC"] = self.wacc

        # Show fcf of stock
        self.fcff = fcff_df.loc[self.current_stock, "FCFF"]
        self.shares_out = fcff_df.loc[self.current_stock, "Shares outstanding"]
        self.fcf = self.fcff/self.shares_out
        info_dict["FCF"] = self.fcf

        # Show fair value stat
        self.fair_value = fcff_df.loc[self.current_stock, "Fair value"]
        info_dict["Fair value"] = self.fair_value

        # Show percentage undervalued stat
        self.percentage_undervalued = fcff_df.loc[self.current_stock, "Percentage undervalued"]
        info_dict["Percentage undervalued"] = self.percentage_undervalued

        return info_dict

    def update_main_frame(self, info_dict: dict):
        """ Function to populate main frame """

        self.main_frame = Frame(self.master)
        self.main_frame.grid(row=0, column=0)

        # Update variables
        self.IS_df = pd.read_csv(f"Database/{self.current_stock}/IS.csv", index_col=[0])
        self.BS_df = pd.read_csv(f"Database/{self.current_stock}/BS.csv", index_col=[0])
        self.CF_df = pd.read_csv(f"Database/{self.current_stock}/CF.csv", index_col=[0])

        # Graphs to be plotted
        self.revenue_df = self.IS_df.loc["Revenue"]
        self.revenue_df = self.revenue_df.astype(float)

        self.operating_income_df = self.IS_df.loc["Operating Income"]
        self.operating_income_df=self.operating_income_df.astype(float)

        # Plot graph of revenue and operating income
        self.plot_chart(0, 0, self.revenue_df, self.operating_income_df, title="", xlabel="Year", ylabel="")

        # Display useful information
        for i, key in enumerate(info_dict):
            Label(self.main_frame, text= f"{key}: \n{info_dict[key]}", font='Helvetica 10').grid(row=(i//2)+1, column=i%2)

    def update_buttons_frame(self):
        """ Function to populate button frame with back, next, like, and watchlist buttons """
        """ Arranges layout of buttons """
        self.button_frame = Frame(self.master)

        # Back button
        self.back_button = Button(self.button_frame, text="Back", command=lambda: self.next(self.idx - 1))
        self.back_button.grid(row=0, column=0, pady="10", padx="10")

        # Next button
        self.next_button = Button(self.button_frame, text="Next", command=lambda: self.next(self.idx + 1))
        self.next_button.grid(row=0, column=1)

        # Like button
        self.like_button = Button(self.button_frame, text="Like", command=self.like)
        self.like_button.grid(row=1, column=0, pady="5", padx="10")

        # Toggle like button if stock is in watchlist
        self.toggle_like_button()

        # Watchlist button
        self.watchlist_button = Button(self.button_frame, text="Watchlist", command=self.watchlist)
        self.watchlist_button.grid(row=1, column=1, pady="5", padx="10")

        # Frame palcement
        self.button_frame.grid(row=1, column=0)

    def toggle_like_button(self):
        """ Toggle like button based on whether self.current_stock is in watchlist """

        with open("Cache/watchlist.txt", "r") as watchlist:
            lines = watchlist.readlines()
        if str(self.current_stock + '\n') in lines:
            self.like_button.config(relief="sunken")
        else:
            self.like_button.config(relief="raised")

    """ Functions to make buttons interactable """

    def next(self, idx):
        """ Function for next button to show next or previous stock """
        # Update variables
        self.idx = idx
        self.current_stock = fcff_df.index[self.idx]

        self.update_main_frame(self.generate_info_dict())

        # Toggle like button based on whether stock is in watchlist
        self.toggle_like_button()

    def like(self):
        """ Function for like button to add stock to watchlist """

        if self.like_button.config('relief')[-1] == 'sunken':
            self.like_button.config(relief="raised")
            with open("Cache/watchlist.txt", "r") as f:
                lines = f.readlines()
            with open("Cache/watchlist.txt", "w") as f:
                for line in lines:
                    if line.strip("\n") != self.current_stock:
                        f.write(line)
        else:
            with open("Cache/watchlist.txt", "a") as myfile:
                myfile.write(f"{self.current_stock}\n")
            self.like_button.config(relief="sunken")

    def watchlist(self):
        """ Function to see stocks in watchlist """

        def view_watchlist_stock(stock):
            """ Function for view button to look at selected stock """
            watchlist_window.destroy()
            self.master.deiconify()
            self.update_main_frame(self.generate_info_dict())
            self.current_stock = stock
            self.update_buttons_frame()

            #update self.idx to that of stock
            self.idx = list(fcff_df.index).index(stock)

        def delete_watchlist_stock(stock):
            """ Function for delete button to delete selected stock """

            with open("Cache/watchlist.txt", "r") as f:
                lines = f.readlines()
            with open("Cache/watchlist.txt", "w") as f:
                for line in lines:
                    if line.strip("\n") != stock:
                        f.write(line)

            idx = Lines.index(stock+'\n')
            labels[idx].destroy()
            view_buttons[idx].destroy()
            delete_buttons[idx].destroy()

            if len(lines) == 1:
                Label(second_frame, text='Watchlist is currently empty', font='Helvetica 10').grid(column=0)

            #untoggle like button on main window if stock on that window is removed from watchlist
            if stock == self.current_stock:
                self.update_buttons_frame()

        def search():
            """ Function for search button to search for a specified stock by its full ticker """
            search_ticker = search_entry.get()
            if search_ticker in fcff_df.index:
                view_watchlist_stock(search_ticker)

            else:
                messagebox.showerror("Error","Sorry the ticker you entered was not found within this spreadsheet")
                return

        def on_closing():
            """ Function to make main window reappear on closing of watchlist window """
            watchlist_window.destroy()
            self.master.deiconify()

        def back_to_main_button_command():
            """ Function to get back to main app when button is clicked"""
            watchlist_window.destroy()
            self.master.deiconify()

        # Create new window over current window
        self.master.withdraw()  # hide main window
        watchlist_window = Toplevel(self.master)
        watchlist_window.protocol("WM_DELETE_WINDOW", on_closing)  # make main window reappear on closing
        watchlist_window.title("Watchlist")
        watchlist_window.geometry("400x500")

        # Create search bar
        search_frame = Frame(watchlist_window)
        search_frame.pack()
        search_entry = Entry(search_frame)
        search_entry.pack(side=LEFT)
        search_button = Button(search_frame, text='Search', command=search)
        search_button.pack(side=LEFT)

        # Create a button to get back to main app
        back_to_main_button = Button(watchlist_window, text="Back to main app", command=back_to_main_button_command)
        back_to_main_button.pack(pady=5)

        ##### scroll button #####
        # Create A Main Frame
        main_frame = Frame(watchlist_window)
        main_frame.pack(fill=BOTH, expand=1)

        # Create A Canvas
        my_canvas = Canvas(main_frame)
        my_canvas.pack(side=LEFT, fill=BOTH, expand=1)

        # Add A Scrollbar To The Canvas
        my_scrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview)
        my_scrollbar.pack(side=RIGHT, fill=Y)

        # Configure The Canvas
        my_canvas.configure(yscrollcommand=my_scrollbar.set)
        my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox("all")))

        def _on_mouse_wheel(event):
            my_canvas.yview_scroll(-1 * int((event.delta / 120)), "units")

        my_canvas.bind_all("<MouseWheel>", _on_mouse_wheel)

        # Create ANOTHER Frame INSIDE the Canvas
        second_frame = Frame(my_canvas)

        # Add that New frame To a Window In The Canvas
        my_canvas.create_window((0,0), window=second_frame, anchor="nw")

        ##### end of scroll bar #####

        # Get list of stocks in watchlist
        file1 = open('Cache/watchlist.txt', 'r')
        Lines = file1.readlines()

        if len(Lines) == 0:
            Label(second_frame, text='Watchlist is currently empty', font='Helvetica 10').grid(column=0)

        labels = []     # Create empty lists to reference which ones to delete later on
        view_buttons = []
        delete_buttons = []

        # Display stocks in watchlist, with buttons to view or delete stock
        for i in range(len(Lines)):
            watchlist_stock_label = Label(second_frame, text=Lines[i], font='Helvetica 10')
            watchlist_stock_label.grid(row=i, column=0)
            watchlist_stock_button = Button(second_frame, text='View', command=lambda i=i: view_watchlist_stock(Lines[i].strip()))
            watchlist_stock_button.grid(row=i, column=1)
            delete_watchlist_stock_button = Button(second_frame, text='Remove', command=lambda i=i:delete_watchlist_stock(Lines[i].strip()))
            delete_watchlist_stock_button.grid(row=i, column=2)

            labels.append(watchlist_stock_label)
            view_buttons.append(watchlist_stock_button)
            delete_buttons.append(delete_watchlist_stock_button)

    def settings(self):
        pass

if __name__ == "__main__":
    root = Tk()
    StonksApp(root)
    root.mainloop()


"""General purpose tools get fenced code blocks from Markdown."""
from dataclasses import dataclass
from operator import attrgetter
from pathlib import Path
from typing import List, Optional

import phmutest.direct
import phmutest.reader
import phmutest.select
from phmutest.direct import Marker

class FCBChooser:
    """Choose Markdown FCBs matching criteria."""

    def __init__(self, markdown_filename: str):
        """Gather all the Markdown fenced code blocks in the file.

        Args:
            markdown_filename:
                Path to the Markdown file as a string.
        """
        self.all_blocks = phmutest.select.configure_block_roles(
            skips=[], markdown_file=Path(markdown_filename)
        )

    def select(
        self, *, label: str = "", info_string: Optional[str] = None, contains: str = ""
    ) -> List[str]:
        """Return list of contents of each FCB that matches all criteria.

        Args:
            label
                FCB has phmutest label directive 'label'. Empty string means
                select all FCBs (default).

            info_string
                FCB info string matches 'info_string'. Empty string means match
                FCBs with no info string. None means select all FCBs (default).

            contains
                FCB contents have substring 'contains'. Empty string means
                select all FCBs (default).

        Returns:
            List of strings, in file order, of the contents of selected FCBs.
            Empty list if no matches are found.
            Fenced code block strings typically end with a newline.
        """
        label_blocks = self.all_blocks
        info_blocks = self.all_blocks
        contains_blocks = self.all_blocks
        if label:
            label_blocks = []
            for block in self.all_blocks:
                for directive in block.directives:
                    if directive.type == Marker.LABEL and directive.value == label:
                        label_blocks.append(block)

        if info_string is not None:
            info_blocks = [b for b in self.all_blocks if info_string == b.info_string]

        if contains:
            contains_blocks = [b for b in self.all_blocks if contains in b.contents]

        satisfies_all = set(label_blocks) & set(info_blocks) & set(contains_blocks)
        selected = list(satisfies_all)
        selected.sort(key=attrgetter("line"))
        return [b.contents for b in selected]

    def contents(self, label: str = "") -> str:
        """Return contents of the labeled fenced code block with label.

        This works the same as phmdoctest.tool.FCBChooser.contents().

        Args:
            label
                FCB has phmutest label directive 'label'.

        Returns:
            Contents of the labeled fenced code block as a string
            or empty string if the label is not found. Fenced code block
            strings typically end with a newline.
        """
        blocks = self.select(label=label)
        return blocks[0] if blocks else ""

@dataclass
class LabeledFCB:
    label: str  # the label directive's value
    line: str  # Markdown file line number of block contents
    contents: str  # fenced code block contents

"""Information about a fenced code block that has a label directive."""

def labeled_fenced_code_blocks(markdown_filename: str) -> List[LabeledFCB]:
    """Return Markdown fenced code blocks that have label directives.

    Label directives are placed immediately before a fenced code block
    in the Markdown source file. The directive can be placed before any
    fenced code block.
    The label directive is the HTML comment `<!--phmutest-label VALUE-->`
    where VALUE is a string with no embedded whitespace.
    The space before VALUE must be present.
    If there is more than one label directive on the block, the
    label value that occurs earliest in the file is used.

    Args:
        markdown_filename
            Path to the Markdown file.

    Returns:
        List of LabeledFCB objects.

        LabeledFCB is has these fields:

        - label is the value of a label directive
          placed in a HTML comment before the fenced code block.
        - line is the line number in the Markdown file where the block
          starts.
        - contents is the fenced code block contents as a string.
    """
    fcbnodes = phmutest.reader.fcb_nodes(markdown_filename)
    labeled_blocks = []
    for node in fcbnodes:
        directives = phmutest.direct.get_directives(node)
        for directive in directives:
            if directive.type == Marker.LABEL:
                block = LabeledFCB(
                    label=directive.value,
                    line=str(directive.line),
                    contents=node.payload,
                )
                labeled_blocks.append(block)
                break
    return labeled_blocks

def fenced_code_blocks(markdown_filename: str) -> List[str]:
    """Return Markdown fenced code block contents as a list of strings.

    Args:
        markdown_filename
            Path to the Markdown file.

    Returns:
        List of strings, one for the contents of each Markdown
        fenced code block.
    """
    fcbnodes = phmutest.reader.fcb_nodes(markdown_filename)
    return [node.payload for node in fcbnodes]


"""
Filename:     calc_volcello.py
Author:       Damien Irving, irving.damien@gmail.com
Description:  Calculate the CMIP5 volcello variable

"""

# Import general Python modules

import sys, os, pdb
import argparse
import numpy
import iris

# Import my modules

cwd = os.getcwd()
repo_dir = '/'
for directory in cwd.split('/')[1:]:
    repo_dir = os.path.join(repo_dir, directory)
    if directory == 'ocean-analysis':
        break

modules_dir = os.path.join(repo_dir, 'modules')
sys.path.append(modules_dir)

try:
    import general_io as gio
    import convenient_universal as uconv
    import spatial_weights
except ImportError:
    raise ImportError('Must run this script from anywhere within the ocean-analysis git repo')

# Define functions

def construct_volume_cube(volume_data, data_cube, global_atts):
    """Construct the new volume cube """

    volume_cube = data_cube.copy()
    volume_cube.data = volume_data
    volume_cube.standard_name = 'ocean_volume'
    volume_cube.long_name = 'Ocean Grid-Cell Volume'
    volume_cube.var_name = 'volcello'
    volume_cube.units = 'm3'
    volume_cube.cell_methods = ()
    if global_atts:
        volume_cube.attributes = global_atts

    return volume_cube

def main(inargs):
    """Run the program."""

    # Depth data
    data_cube = iris.load_cube(inargs.dummy_file, inargs.dummy_var)
    dim_coord_names = [coord.name() for coord in data_cube.dim_coords]
    aux_coord_names = [coord.name() for coord in data_cube.aux_coords]
    assert dim_coord_names[0] == 'time'
    depth_name = dim_coord_names[1]
    data_cube = data_cube[0, ::]
    data_cube.remove_coord('time')
    depth_data = spatial_weights.get_depth_array(data_cube, depth_name)
    # Area data
    if inargs.area_file:
        area_cube = iris.load_cube(inargs.area_file, 'cell_area')
        gio.check_global_ocean_area(area_cube.data.sum())
        area_data = uconv.broadcast_array(area_cube.data, [1, 2], depth_data.shape)
    else:
        area_data = spatial_weights.area_array(data_cube)

    volume_data = depth_data * area_data

    if inargs.sftof_file:
        sftof_cube = iris.load_cube(inargs.sftof_file)
        assert sftof_cube.data.max() == 100
        sftof_data = uconv.broadcast_array(sftof_cube.data, [1, 2], depth_data.shape)
        volume_data = volume_data * (sftof_data / 100.0)

    volume_data = numpy.ma.asarray(volume_data)
    data = numpy.ma.masked_invalid(data_cube.data)
    volume_data.mask = data.mask
    global_atts = area_cube.attributes if inargs.area_file else None
    volume_cube = construct_volume_cube(volume_data, data_cube, global_atts)
    volume_cube.attributes['history'] = gio.write_metadata()

    gio.check_global_ocean_volume(volume_cube.data.sum())

    iris.save(volume_cube, inargs.outfile)

if __name__ == '__main__':

    extra_info ="""
author:
  Damien Irving, irving.damien@gmail.com

"""

    description='Calculate the CMIP volcello variable'
    parser = argparse.ArgumentParser(description=description,
                                     epilog=extra_info,
                                     argument_default=argparse.SUPPRESS,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument("dummy_file", type=str, help="Dummy file (for depth information)")
    parser.add_argument("dummy_var", type=str, help="Dummy variable")
    parser.add_argument("outfile", type=str, help="Output file name")

    parser.add_argument("--sftof_file", type=str, default=None,
                        help="Sea area fraction file name")
    parser.add_argument("--area_file", type=str, default=None,
                        help="Area file name (required for curvilinear grids, optional otherwise)")

    args = parser.parse_args()
    main(args)


from obspy import read
from numpy import r_,ones,zeros

path=u'/Users/dmelgar/Slip_inv/Chiapas_hernandez_new/data/waveforms/before_delta_t/'
outpath='/Users/dmelgar/Slip_inv/Chiapas_hernandez_new/data/waveforms/'

def delay_st(st,delta):
    d=st[0].data
    npts=int(abs(delta)/st[0].stats.delta)
    if delta<0:
        d=r_[d[npts:-1],d[-1]*ones(npts+1)]
    else:
        d=r_[ones(npts)*d[0],d[0:-npts]]
    return d

sta='43413'
delta=-3*60.
e=read(path+sta+'.sac')
e[0].data=delay_st(e,delta)
e.write(outpath+sta+'.sac',format='SAC')

sta='huat'
delta=-2*60.
e=read(path+sta+'.sac')
e[0].data=delay_st(e,delta)
e.write(outpath+sta+'.sac',format='SAC')

sta='ptan'
delta=-2*60.
e=read(path+sta+'.sac')
e[0].data=delay_st(e,delta)
e.write(outpath+sta+'.sac',format='SAC')


#! /usr/bin/env python
from ppclass import pp

u = pp()
u.file = "/home/aymeric/Big_Data/DATAPLOT/diagfired.nc"
u.var = "u"
u.t = "0.5,0.8"
u.z = "10,20"
u.getdefineplot(extraplot=2) # prepare 2 extraplots (do not show)
u.p[0].proj = "ortho"
u.p[0].title = "$u$"
u.makeplot()

v = pp()
v << u  # NB: initialize v object with u object's attributes
v.var = "v"
v.get()
v.plotin = u # plotin must be defined before .defineplot()
v.defineplot()
v.p[1].proj = "ortho"
v.p[1].title = "$v$"
v.makeplot() # plot within the previous one (do not show)

wind = u**2 + v**2
wind = wind**0.5
wind.plotin = v
wind.filename = "windspeed"
wind.defineplot()
wind.p[2].proj = "ortho"
wind.p[2].title = "$\sqrt{u^2+v^2}$"
wind.makeplot() # plot within the previous one (show because complete)

## in this case it is not possible to make another plot afterwards...