import argparse

import numpy as np
import tables

def main():
    run(**parse_args())

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("filename", type=str)
    parser.add_argument("path", type=str)
    return vars(parser.parse_args())

def run(filename, path):
    with tables.open_file(filename) as store:
        node = store.root
        for name in path.split("/"):
            node = node[name]

        print("Node:")
        print(node)

        print("Data:")
        if isinstance(node, tables.Leaf):
            print("")
            print(node[:])

if __name__ == "__main__":
    main()


import numpy as np

def empirical_distribution(x_array, data):
    count = np.zeros(shape=len(x_array))
    for datum in data:
        count = count + (datum <= x_array)
    return count / len(data)

def one_dim_scatterplot(data, ax, jitter=0, **options):
    if jitter:
        jitter = np.random.uniform(-jitter, jitter, size=data.shape)
    else:
        jitter = np.repeat(0.0, len(data))
    ax.scatter(data, jitter, **options)
    ax.yaxis.set_ticklabels([])
    ax.set_ylim([-1, 1])

def text_in_blank_plot(text, ax):
    _ = ax.text(0.5, 0.5, text,
                horizontalalignment='center',
                verticalalignment='center',
                fontsize=22)
    ax.axis('off')

def superimpose_pdf_of_fit_model(data, model, ax, x_lower=-3, x_upper=3, bins=25):
    x = np.linspace(x_lower, x_upper, num=250)
    _ = ax.hist(data, bins=bins, normed=True, color="black", alpha=0.4)
    ax.plot(x, model.pdf(x), linewidth=3)

def superimpose_cdf_of_fit_model(data, model, ax, x_lower=-3, x_upper=3):
    x = np.linspace(x_lower, x_upper, num=250)
    ax.plot(x, empirical_distribution(x, data), linewidth=2)
    ax.plot(x, model.cdf(x), linewidth=2)


from argparse import ArgumentParser
from tqdm import tqdm
from tensorboardX import SummaryWriter
import shutil
import os
import torch
import torch.nn as nn

from transfer.module.seq2seq import CycleLoss
from transfer.module.vae import (
    VariationalEncoder, VariationalDecoder, GaussianKLLoss)
from transfer.dataset import PttDataset, DcardDataset
from transfer.utils import Voc, transfer, straight_through_estimate
from transfer.constant import pattern, Constants

# cvae
max_length = 50
embedding_dim = 128
latent_dim = w    # content dimension
style_dim = 64      # style dimension
num_layers = 1
dropout = 0.1
bidirectional = True
st_mode = False
hidden_size = 256
# train
epochs = 1
batch_size = 32
chunk_size = 50
# loss coffecient
alpha = 1.          # reconstruct loss
gamma = 1.          # KL div
# gradient clipping
max_grad_norm = 1
# model path
model_dir = './models/%s'
log_dir = './log/%s'

D_threshold = 0.5

def save(E, G, model_name):
    path = os.path.join(model_dir, model_name)
    os.makedirs(path, exist_ok=True)
    torch.save(E.state_dict(), os.path.join(path, 'E.pt'))
    torch.save(G.state_dict(), os.path.join(path, 'G.pt'))

def train(batch_x, batch_y, E, G,
          criterion_cyc, criterion_KL, optimizer_G, num_iter, device):
    real_x, style_x = batch_x['seq'].to(device), batch_x['style'].to(device)
    real_y, style_y = batch_y['seq'].to(device), batch_y['style'].to(device)
    loss = {}

    # encode
    outputs_x, latent_x, mean_x, std_x = E(real_x, style_x, device)
    outputs_y, latent_y, mean_y, std_y = E(real_y, style_y, device)
    # decode
    rect_x = G(latent_x, style_x, outputs_x, max_length, device)
    rect_y = G(latent_y, style_y, outputs_y, max_length, device)
    fake_x = G(latent_y, style_x, outputs_y, max_length, device)
    fake_y = G(latent_x, style_y, outputs_x, max_length, device)
    fake_x = straight_through_estimate(fake_x)
    fake_y = straight_through_estimate(fake_y)

    # ===== G =====
    # reconstruct loss
    G_loss_cyc_x = criterion_cyc(rect_x, real_x)
    G_loss_cyc_y = criterion_cyc(rect_y, real_y)
    G_loss_cyc = (G_loss_cyc_x + G_loss_cyc_y) / 2
    # KL
    G_loss_KL_x = criterion_KL(mean_x, std_x)
    G_loss_KL_y = criterion_KL(mean_y, std_y)
    G_loss_KL = (G_loss_KL_x + G_loss_KL_y) / 2
    # KL loss
    G_loss = alpha * G_loss_cyc + gamma * G_loss_KL
    # optimize G
    optimizer_G.zero_grad()
    G_loss.backward()
    torch.nn.utils.clip_grad_norm_(E.parameters(), max_grad_norm)
    torch.nn.utils.clip_grad_norm_(G.parameters(), max_grad_norm)
    optimizer_G.step()
    # update loss
    loss.update({
        # G loss
        'G/loss_cyc_x': G_loss_cyc_x.item(),
        'G/loss_cyc_y': G_loss_cyc_y.item(),
        'G/loss_KL_x': G_loss_KL_x.item(),
        'G/loss_KL_y': G_loss_KL_y.item(),
        'G/loss_cyc': G_loss_cyc.item(),
        'G/loss_KL': G_loss_KL.item(),
        'G/loss': G_loss.item(),
    })

    return loss

def write_example(E, G, batch_x, batch_y, writer, num_iter, device):
    ptt2ptt = []
    mapping = Constants.idx2example()
    for seq_x in batch_x['seq'].numpy():
        real_x = Voc.decode(seq_x, mapping=mapping)
        rect_x = transfer(E, G, real_x, 'ptt', 'ptt', max_length, device)
        real_x = Voc.decode(seq_x)
        ptt2ptt.append(pattern % (real_x, rect_x))
    writer.add_text('PTT_to_PTT', '\n'.join(ptt2ptt), num_iter)

    dcard2dcard = []
    for seq_y in batch_y['seq'].numpy():
        real_y = Voc.decode(seq_y, mapping=mapping)
        rect_y = transfer(E, G, real_y, 'dcard', 'dcard', max_length, device)
        real_y = Voc.decode(seq_y)
        dcard2dcard.append(pattern % (real_y, rect_y))
    writer.add_text('DCARD_to_DCARD', '\n'.join(dcard2dcard), num_iter)

if __name__ == '__main__':
    parser = ArgumentParser('Train unsupervised style transfer model')
    parser.add_argument(
        '--load_pretrain', help="load pretrained autoencoder",
        action='store_true')
    parser.add_argument(
        '--name', help='the name of model', default="CVAE")
    args = parser.parse_args()
    model_dir = model_dir % args.name
    log_dir = log_dir % args.name
    shutil.rmtree(log_dir, ignore_errors=True)
    writer = SummaryWriter(log_dir=log_dir)

    # load dataset
    x_dataloader = torch.utils.data.DataLoader(
        dataset=PttDataset(chunk_size=chunk_size),
        batch_size=batch_size, shuffle=True, drop_last=True)
    y_dataloader = torch.utils.data.DataLoader(
        dataset=DcardDataset(chunk_size=chunk_size),
        batch_size=batch_size, shuffle=True, drop_last=True)
    voc = Voc()

    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

    embedding = nn.Embedding(Voc.vocab_size, embedding_dim)
    E = VariationalEncoder(
        embedding, latent_dim, hidden_size, num_layers,
        dropout, bidirectional).to(device)
    G = VariationalDecoder(
        embedding, style_dim, latent_dim, hidden_size, num_layers,
        dropout, st_mode).to(device)

    criterion_cyc = CycleLoss()
    criterion_KL = GaussianKLLoss()

    optimizer_G = torch.optim.RMSprop(
        list(E.parameters()) + list(G.parameters()), lr=0.001)

    num_iter = 0
    loss = {'G/loss': -1}
    for epoch in range(epochs):
        batch_num = min(len(x_dataloader), len(y_dataloader))
        with tqdm(total=batch_num) as t:
            t.set_description('Epoch %2d/%2d' % (epoch + 1, epochs))
            for batch_x, batch_y in zip(x_dataloader, y_dataloader):
                loss_new = train(
                    batch_x, batch_y, E, G, criterion_cyc, criterion_KL,
                    optimizer_G, num_iter, device)
                loss.update(loss_new)

                num_iter += 1
                if num_iter % 1000 == 1:
                    write_example(
                        E, G, batch_x, batch_y, writer, num_iter, device)

                for loss_name, value in loss.items():
                    writer.add_scalar(loss_name, value, num_iter)
                t.update(1)
                t.set_postfix(G_loss='%.4f' % loss['G/loss'])
        save(E, G, 'epoch%d' % (epoch + 1))
    write_example(E, G, batch_x, batch_y, writer, num_iter, device)
    writer.close()
    save(E, G, 'final')


# for i in range(int(input())):
#     n = int(input())
#     li = list(map(int, input().split()))
#     if max(li) == 7:
#         if len(li) % 2 != 0:
#             l1 = li[:n // 2]
#             l2 = li[n // 2 + 1:]
#             l2.sort()
#         # print(l1, l2)
#         else:
#             l1 = li[:n // 2 - 1]
#             l2 = li[n // 2:]
#             l2.sort()
#         # print(l1, l2)
#         if l1 == l2:
#             print("yes")
#         else:
#             print("no")
#     else:
#         print("no")

for i in range(int(input())):
    n = int(input())
    li = list(map(int, input().split()))
    s = {1, 2, 3, 4, 5, 6, 7}
    if max(li) == 7 and s == set(li) and li == li[::-1]:
        print("yes")
    else:
        print("no")


from  matplotlib import pyplot as plt
import numpy as np

model_fnames = {
    'crust': 'rho.crust{}.dat',
    'prem': 'rho.prem{}.dat',
}

import argparse
parser=argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--xlength', nargs=2, metavar=('MIN', 'MAX'),type=float)
group.add_argument('--xdepth', nargs=2, metavar=('MIN', 'MAX'),type=float)
parser.add_argument("--yrho",nargs=2,metavar=('MIN','MAX'),type=float)
parser.add_argument("--curve",type=int,required=True)
parser.add_argument("--model", choices=model_fnames.keys())
parser.add_argument("--plot-curves", action='store_true')
parser.add_argument("--plot-crust", action='store_true')
parser.add_argument("--plot-avg", action='store_true')
parser.add_argument("--plot-std", action='store_true')
parser.add_argument("--plot-min", action='store_true')
parser.add_argument("--plot-max", action='store_true')

args=parser.parse_args()
ax=plt.gca()
plt.minorticks_on()
if args.yrho is not None:
    ax.set_ylim(*args.yrho)

if args.xlength:
    min_length, max_length = args.xlength
    min_rho,max_rho=args.yrho
    plt.xlabel('length  (km)')
    plt.ylabel(r'$\rho$  (g/cm$^{3}$)')
    plt.title('Density distribution in Earth matter')

fname=model_fnames[args.model]
lall = []
funcs = []
for i in range(0,args.curve+1):
    rho, ls = np.loadtxt(fname.format(i),float), np.loadtxt('length{}.dat'.format(i),float)
    ls = np.hstack([-ls[ls<=0][0:-1], 2*ls.max()-ls[ls>=0][1:]])
    lall.append(ls)

    funcs.append(lambda l, ls=ls, rho=rho: rho[np.searchsorted(ls, l, side='right')-1])

    kwargs = {}
    if i == 0:
        kwargs['linewidth'] = 2
    if args.plot_curves or i == 0 and args.plot_crust:
        plt.plot(ls, rho, label=('crust' if i == 0 else None))
    ax.set_xlim(min_length, max_length)

lall = np.unique(sorted(np.hstack(lall)))
rhoall = []
for func in funcs:
    rhoall.append(func(lall))
rhoall = np.vstack(rhoall).T
rhoavg = rhoall.mean(axis=1)
if args.plot_avg:

    ax.plot(lall, rhoavg, label='average')
if args.plot_min:
    ax.plot(lall, np.amin(rhoall, axis=1), label='min')
if args.plot_max:
    ax.plot(lall, np.amax(rhoall, axis=1), label='max')
if args.plot_std:
    rhostd = rhoall.std(axis=1)
    ax.fill_between(lall, rhoavg-rhostd, rhoavg+rhostd, alpha=0.3)

plt.legend(loc='lower center')
if args.xdepth:
    rho, d = np.loadtxt("rho.dat"), np.loadtxt("depth.dat")
    min_depth, max_depth = args.xdepth
    min_rho,max_rho=args.yrho
    pmask = np.logical_and(min_depth < d, d <= max_depth)
    prho, pl = rho[pmask], d[pmask]
    mmask =np.logical_and(-max_depth <= d, d < -min_depth)
    mrho, ml = rho[mmask], d[mmask]

    plt.xlabel('depth  (km)')
    plt.ylabel(r'$\rho$  (g/cm$^{3}$)')
    plt.title('Density distribution in Earth matter')
    ax.set_xlim(-(max_depth-min_depth), max_depth-min_depth)
    plt.plot(ml-ml.max(), mrho)
    plt.plot(pl-pl.min(), prho)
    ticks = plt.xticks()[0]
    ticks[ticks<0] = -ticks[ticks<0]
    ticks+=min_depth

ax.grid(which='both')
ax.grid(which='minor', alpha=0.5)
ax.grid(which='major', alpha=0.5)

plt.show()


import os
import requests
import uuid

from . import app, logger
from toutiao.crawler import Crawler
from model import Article
from video.tts import Txt2Mp3
from video.video import img2mp4
from ytb.utils import video_in_path, upload_toutiao
from service.auth import Auth

@app.task(bind=True, queue='queue_toutiao')
def fetch_upload(self, *args, **kwargs):
    article_id = Crawler().fetch_one()
    article = Article.get_by_id(article_id)
    logger.info('fetched article {}'.format(article.title))

    content = article.content
    title = article.title
    image_urls = article.image_urls

    asset_path = '/tmp/{}'.format(uuid.uuid4().hex[:6])
    os.mkdir(asset_path)
    file_path = os.path.join(asset_path, 'asset.txt')
    with open(file_path, 'w') as f:
        f.write(content)

    for i, url in enumerate(image_urls):
        response = requests.get(url)
        if response.status_code == 200:
            img_path = os.path.join(asset_path, '{}.jpg'.format(i))
            with open(img_path, 'wb') as f:
                f.write(response.content)
                logger.info('pic downloaded {}'.format(img_path))

    t2m = Txt2Mp3(asset_path)
    t2m.run()
    img2mp4(asset_path)

    title = title.split('。')[0]
    if '】' in title:
        title = title.split('】')[0] + '】'

    video = video_in_path(asset_path)
    credentials = Auth.get_credentials_by_account_id(2)
    client = Auth.get_client(credentials)
    upload_toutiao(client, video, title)


import json
import requests
import pandas as pd
from datetime import date
import numpy as np

def main():

    ##Antigen
    url = 'https://atlas.jifo.co/api/connectors/425b93dc-c055-477c-b81a-5d4d9a1275f7'
    response = requests.get(url).text
    nested_dict = json.loads(response)

    ag = pd.DataFrame(nested_dict["data"][4])

    new_header = ag.iloc[0]
    ag = ag[1:]
    ag.columns = new_header

    ag["Date"] = pd.to_datetime(ag[""], dayfirst=True)
    del ag[""]

    ag["Positivas"] = ag["Positivas"].str.replace(',','').replace('','0').astype(float)
    ag["Total Px Ag"] = ag["Total Px Ag"].str.replace(',','').replace('','0').astype(float)

    ##PCR
    pcr = pd.read_csv(
        'https://www.datos.gov.co/resource/8835-5baf.csv',
        usecols=['fecha', 'positivas_acumuladas','negativas_acumuladas']
    )

    pcr = pcr[(pcr["fecha"] != "Acumulado Feb") & (pcr.fecha.notnull())]
    pcr["Date"] = pd.to_datetime(pcr["fecha"], dayfirst=True)
    del pcr["fecha"]

    ##Combine
    df = pd.merge(ag, pcr, how='outer').sort_values(by="Date")

    df["Cumulative total"] = (
        df["positivas_acumuladas"].fillna(0)
        .add(df["negativas_acumuladas"].fillna(0))
        .add(df["Total Px Ag"].fillna(0))
    )

    df["Positive total"] = (
        df["positivas_acumuladas"].fillna(0)
        .add(df["Positivas"].fillna(0))
    )

    df = df[df["Cumulative total"] != 0]
    df = df[df["Cumulative total"] > df["Cumulative total"].shift(1)]
    df = df[df["Positive total"] != 0]

    df["Positive rate"] = (
        ((df["Positive total"] - df["Positive total"].shift(1)).rolling(7).mean())
        .div((df["Cumulative total"] - df["Cumulative total"].shift(1)).rolling(7).mean())
        .round(3)
    )

    df = df.drop(columns=[
        "positivas_acumuladas", "negativas_acumuladas", "Positivas", "Total Px Ag", "Positive total"
    ])

    df["Country"] = "Colombia"
    df["Units"] = "tests performed"
    df["Source URL"] = "https://www.ins.gov.co/Noticias/Paginas/coronavirus-pcr.aspx"
    df["Source label"] = "National Institute of Health"
    df["Notes"] = np.nan
    df.reset_index(drop=True, inplace=True)

    df.to_csv("automated_sheets/Colombia.csv", index=False)

if __name__ == '__main__':
    main()


import re
from django.conf import settings
from django.http import Http404, HttpResponseServerError
from django.utils import timezone
from datetime import timedelta, date,datetime
import datetime
import time
from django.utils.timezone import utc

from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template

from xhtml2pdf import pisa

from rest_framework.response import Response

from portalbackend.lendapi import constants
from portalbackend.lendapi.accounting.models import AccountingOauth2, CoA,LoginInfo
from portalbackend.lendapi.accounting.models import FinancialStatementEntryTag, TrialBalance, CoAMap
from portalbackend.lendapi.reporting.models import FinancialStatementEntry
from django.db import transaction
import calendar

class AccountingUtils(object):
    """
    A collection of functions that are used within the Accounting views and functions
    """
    @staticmethod
    def updateAccountingSession(company, accessToken, refreshToken, realmId):
        """
        Updates the Accounting Session Saved inside the database
        :param company:
        :param accessToken:
        :param refreshToken:
        :param realmID:
        :return:
        """
        token = AccountingOauth2.objects.filter(company=company).first()
        if token:
            token.accessToken = accessToken
            token.refreshToken = refreshToken
            if token.realmId != realmId:
                token.realmId = realmId
        else:
            token = AccountingOauth2(company=company, accessToken=accessToken,
                                     refreshToken=refreshToken, realmId=realmId)
        token.save()
        return token

    @staticmethod
    def get_credentials(request):
        """
        Fetches the accounting authentication credentials by the request user (deprecated)
        :param request:
        :return:
        """
        if request.user.company:
            token = AccountingOauth2.objects.filter(company=request.user.company).first()
            if token:
                return token
            raise Http404
        else:
            raise Http404

    @staticmethod
    def get_status_object(company):
        now = datetime.datetime.utcnow().replace(tzinfo=utc)
        time_threshold = now - datetime.timedelta(hours=2)
        try:
            return LoginInfo.objects.filter(company=company, created__range=(time_threshold, now)).first()
        except LoginInfo.DoesNotExist:
            raise Http404

    @staticmethod
    def get_credentials_by_company(company):
        """
        Fetches the accounting authentication credentials by company object
        :param company:
        :return:
        """
        token = AccountingOauth2.objects.filter(company=company).first()
        return token

    @staticmethod
    def create_mapping_dict(queryset):
        """
        Converts a queryset of the default map into a useable dictionary
        :param queryset:
        :return:
        """
        d = {}
        for mapping in queryset:
            mapping_key = re.sub ('[^a-zA-Z0-9]', '', mapping.account_category.lower())
            d[mapping_key] = {"default_map_id": mapping.default_map_id,
                                                   "default_map_name": mapping.default_map_name}
        return d

    @staticmethod
    def parse_statement(request,company, data, statement_type):
        """
        Parses the income statement and balance sheet sheet request, sent in the form of:
         {"StatementType": [{"Period": "2016-01"},...]}, statement type can either be BalanceSheet or IncomeStatement
        :param company:
        :param data:
        :param statement_type: The type of sheet that is being parsed
        :return:
        """
        fixed_fields = ("Period", "CreatedTimestamp", "LastUpdatedTimestamp", "SourceName", "SourceKey")
        entries = []
        error_tags = []
        monthly_report = None
        tags = FinancialStatementEntryTag.objects.all()

        tag_lookup = {}
        sight_tag_lookup = {}
        for tag in tags:
            sight_tag_lookup[tag.all_sight_name] = tag.tag_id
            tag_lookup[str(tag.tag_id)] = {"id": tag.id, "short_label": tag.short_label}

        # All Sight sends back BalanceSheet not Balance Sheet, so we need to translate (same for income statement)
        for month in data[statement_type.replace(" ", "")]:
            y, m = map(int, month["Period"].split('-'))
            num_days = calendar.monthrange(y, m)[1]
            period = datetime.date(y, m, num_days)

            for key, value in month.items():
                if value is None:
                    value = '0'

                if key not in fixed_fields:
                    check_key_exists = sight_tag_lookup.get (key, None)
                    if check_key_exists is None:
                        tag_key = str(key)
                    else:
                        tag_key = sight_tag_lookup[key]

                    try:

                        cur_tag = tag_lookup[str(tag_key)]
                    except KeyError as e:
                        print('### WARNING ### - tag id ', tag_key, ' not found in financial statement entry tag map')
                        if tag_key not in error_tags:
                            error_tags.append(tag_key)

                    else:
                        rows_affected = FinancialStatementEntry.objects.filter(
                                            company=company,
                                            period_ending=period,
                                            statement_type=statement_type,
                                            fse_tag_id=cur_tag['id']).update(value=value)

                        # 0 rows affected == it doesn't exist
                        if rows_affected == 0:
                            # these will be bulk created
                            entry = FinancialStatementEntry(company=company, period_ending=period,
                                                            entry_name=cur_tag['short_label'],
                                                            value=value, currency='CAD',
                                                            fse_tag_id=cur_tag['id'],
                                                            statement_type=statement_type)
                            entries.append(entry)
        return entries,error_tags

    @staticmethod
    def credit_debit_mismatch(company):
        """
        Method compares the Total Assets and Total L&E and returns the period if both the value are not equal
        """
        try:
            total_liabilities_equity = FinancialStatementEntryTag.objects.get(
                tag_category="total_liabilities_equity")
            total_assets = FinancialStatementEntryTag.objects.get(tag_category="total_assets")
            financial_statement_entry_equity = FinancialStatementEntry.objects.filter(
                company=company,
                statement_type=FinancialStatementEntry.BALANCE_SHEET,
                fse_tag_id=total_liabilities_equity)
            dict_credits = {}
            dict_debits = {}
            for financial_statement_entry_object in financial_statement_entry_equity:
                dict_credits[str(financial_statement_entry_object.period_ending)] = int(
                    financial_statement_entry_object.value)
            financial_statement_entry_assets = FinancialStatementEntry.objects.filter(
                company=company,
                statement_type=FinancialStatementEntry.BALANCE_SHEET,
                fse_tag_id=total_assets)
            for financial_statement_entry_object in financial_statement_entry_assets:
                dict_debits[str(financial_statement_entry_object.period_ending)] = int(
                    financial_statement_entry_object.value)
            credit_debit_mismatch = []
            for key in dict_credits:
                if key in dict_debits:
                    val1 = dict_credits[key]
                    val2 = dict_debits[key]
                    if val1 != val2:
                        credit_debit_mismatch.append(key)
            return credit_debit_mismatch
        except Exception as e:
            print(str(e))
            return None

    @staticmethod
    def get_stripped_tags():
        """
        Retrieves stripped down version of Financial Statement Entry tags
        :return:
        """
        d = {}
        # some tags are formatted differently in the post than in the structure
        special_cases = {'TotalLE': 'TotalLiabilityAndEquity',
                         'EquityPortionofLTDebt': 'EquityPositionOfLTDebt',
                         'ShareCapitalandContributedCapital': 'ShareAndContributedCapital',
                         'NetIncome': 'NetIncomeYTD',
                         'SREDITCReceivable': 'SREDReceivable',
                         'PatentsIntangibleAssets': 'PatentsAndIntangibleAssets',
                         'TotalRevenues': 'TotalRevenue',
                         'DepreciationAmortization': 'DepreciationAndAmortization',
                         'SRED': 'SREDAccrual',
                         'RDGrossexcludingSRED': 'RDGrossMinusExcludingSRED',
                         'AccountsReceivables': 'AccountReceivables'
                         }
        tags = FinancialStatementEntryTag.objects.all()
        for tag in tags:
            stripped = tag.short_label
            stripped = stripped.replace(' ', '')
            repl = re.sub('[^a-zA-Z0-9 \n\.]', '', stripped)
            if repl in special_cases:
                repl = special_cases[repl]
            d[repl] = tag
        ld = {k.lower(): v for k, v in d.items()}
        return ld

    @staticmethod
    def build_all_sight_save_request(company):
        data = {"SourceName": "CustomerProfile",
                "SourceKey": company.id,
                "Model": {
                    "Financials": {}
                    }
                }
        trial_balances = TrialBalance.objects.filter(company=company).order_by('period')
        grouped = dict()
        for obj in trial_balances:
            grouped.setdefault(obj.period.strftime("%Y-%m"), []).append(obj)

        data["Model"]["Financials"]["CustomerTrialBalance"] = []
        for key, month_data in grouped.items():
            subdata = {"Period": key, "RecordedTimestamp": timezone.now().strftime("%Y-%m-%d %H:%M:%S.%f"),
                       "Version": "1", "CustomerTrialBalanceItem": []}

            for entry in month_data:
                subdata["CustomerTrialBalanceItem"].append({
                    "AccountId": str(entry.gl_account_id),
                    "AccountName": entry.gl_account_name,
                    "CreditAmount": str(entry.credit),
                    "DebitAmount": str(entry.debit)
                })
            data["Model"]["Financials"]["CustomerTrialBalance"].append(subdata)

        data["Model"]["Financials"]["CustomerAccount"] = []
        coas = CoA.objects.filter(company=company)
        for coa in coas:
            subdata = {
                "AccountId": str(coa.gl_account_id),
                "AccountName": coa.gl_account_name,
                "AccountCategory": coa.gl_account_type
            }
            data["Model"]["Financials"]["CustomerAccount"].append(subdata)

        data["Model"]["Financials"]["CustomerAccountMapping"] = []
        coamaps = CoAMap.objects.filter(company=company)
        for mapping in coamaps:
            subdata = {
                "FromAccountId": str(mapping.cust_account_id),
                "FromAccountName": mapping.cust_account_name,
                "ToAccountId": str(mapping.espresso_account_id),
                "ToAccountName": mapping.espresso_account_name,
            }
            data["Model"]["Financials"]["CustomerAccountMapping"].append(subdata)
        return data

    @staticmethod
    def create_coa_map(request,company, default_mappings, coa, remap):
        mappings = AccountingUtils.create_mapping_dict(default_mappings)
        saved_mappings = []

        for account in coa:
            try:
                # since we're forced to match on strings, we strip out all non-numeric / non-alpha characters
                # and lower all alpha to reduce the chance for mis match between submitted and default tag names
                lookup_key = re.sub('[^a-zA-Z0-9]', '', account.gl_account_type).lower()
                default_map_dict = mappings[lookup_key]
            except KeyError:
                # error = ["%s" % e]
                # return Utils.dispatch_failure(request, 'DATA_PARSING_ISSUE', error)
                # todo: log this to a file, and send a warning to the system admin to check
                print({"error": "no mapping for account type: {} ".format(account.gl_account_type)})
            else:
                entry = CoAMap.objects.filter(company=company, cust_account_id=account.gl_account_id).first()
                # send back anything that's new, so it can be mapped
                if not entry:
                    mapping = CoAMap(company=company,
                                     cust_account_name=account.gl_account_name,
                                     cust_account_id=account.gl_account_id,
                                     espresso_account_id=default_map_dict["default_map_id"],
                                     espresso_account_name=default_map_dict["default_map_name"])
                    mapping.save()
                    saved_mappings.append(mapping)
                else:
                    # and also send back anything that hasn't been verified by the user
                    # verification doesn't occur until the monthly report is signed off, because the
                    # remapping exercise done by the UI may need to take place several times before it can be considered
                    # valid

                    # Below logic for update changes in CoAMap
                    mapping_data = CoAMap.objects.get(id=entry.id)
                    if account.gl_account_name != entry.cust_account_name:
                        mapping_data.cust_account_name = account.gl_account_name
                        mapping_data.save()
                        saved_mappings.append (mapping_data)

                    if not entry.verified_by_user:
                        saved_mappings.append(entry)

        return saved_mappings

    @staticmethod
    def set_coa_map(company, coamap):
        updated_mappings = []
        for account in coamap:
            coamapping = CoAMap.objects.filter(company=company, cust_account_id=account["cust_account_id"]).first()

            tag = FinancialStatementEntryTag.objects.filter(description=account["espresso_account_name"],
                                                            tag_id=account["espresso_account_id"]).first()

            if not tag:
                tag = FinancialStatementEntryTag.objects.filter(tag_id=account["espresso_account_id"]).first()
            coamapping.espresso_account_id = tag.tag_id
            coamapping.espresso_account_name = tag.description
            coamapping.is_mapped = True
            coamapping.save()

            updated_mappings.append(coamapping)

        return updated_mappings

    @staticmethod
    def get_gl_account_id_by_name(company, account_name):
        gl_id = ''

        coa = CoA.objects.filter(company=company, gl_account_name=account_name).first()
        if coa:
            gl_id = coa.gl_account_id

        return gl_id

    @staticmethod
    def render_to_pdf(template_src, context_dict={}):
        template = get_template (template_src)
        html = template.render (context_dict)
        result = BytesIO ()
        pdf = pisa.pisaDocument (BytesIO (html.encode ("UTF-8")), result)
        if not pdf.err:
            return HttpResponse (result.getvalue (), content_type='application/pdf')
        return None


from airflow.utils.decorators import apply_defaults

from presidio.utils.airflow.operators.retention.retention_operator import RetentionOperator

class AlertRetentionOperator(RetentionOperator):
    """
    Runs an alert retention task (a JAR file) using a bash command.
    The c'tor accepts the task arguments that are constant throughout the
    operator runs (e.g. the fixed duration strategy and the schema).
    Other arguments, such as the start date and the end date, are evaluated before every run.
    """

    @apply_defaults
    def __init__(self, command, entity_type, task_id=None, *args, **kwargs):
        """
        C'tor.
        :param command: command
        :type command: string
        :param entity_type: the entity type to process the alert cleanup.
        :type entity_type: string
        :param task_id: The task ID of this operator - If None, the ID is generated automatically
        :type task_id: string
        """
        self.log.debug('alert retention operator init kwargs=%s', str(kwargs))
        self.task_id = task_id or 'alert_retention'
        self.log.debug('agg operator. command=%s', command)
        java_args = {'entity_type': entity_type}

        super(AlertRetentionOperator, self).__init__(
            task_id=self.task_id,
            command=command,
            java_args=java_args,
            *args,
            **kwargs
        )


from django.conf.urls import url

from . import views

user_urls = [
    url(r'^(?P<pk>[0-9]+)/?$', views.UserDetail.as_view(), name='user-detail'),

    url(r'token', views.GetAuthToken.as_view(), name='api-token'),

    url(r'^$', views.UserList.as_view()),
]


"""
Utility functions.
"""

import re
import string
import sys

SYMBOL_REGEX = re.compile(r'[' +
                          re.escape(r'!$%&*+-./:<=>?@[]^_{}~') +
                          string.letters +
                          string.digits +
                          r']+')

def find_delimiters(input, open_d='(', close_d=')', start_idx=0):
    """
    Search INPUT for the first matching set of delimiters OPEN_D and CLOSE_D,
    starting at START_IDX. Return a tuple of indexes marking the beginning &
    end of the delimiter pair (including the delimiters themselves), or None
    if no matches were found.
    """
    while True:
        # Locate the first opening delimiter
        start_idx = input.index(open_d, start_idx)
        if start_idx == -1:
            # Opening delimiter not found.
            return None
        # Start the search for the closing delimiter after allowing for any
        # opening delimiters. This helps us match when the delimiters are
        # equal, and avoids bugs arising from seeing the closing delimiters
        # before the opening.
        end_idx = start_idx + len(open_d)
        while True:
            # Locate the first closing delimiter.
            end_idx = input.index(close_d, end_idx)
            if end_idx == -1:
                # Closing delimiter not found.
                break
            # Extract the portion of our input that is between the delimiters
            # we found.
            between = input[start_idx + len(open_d):end_idx]
            # If there are equal numbers of start/end delimiters remaining
            # between the delimiters we found, we can be sure our delimiters
            # are a matched set.
            if between.count(open_d) == between.count(close_d):
                return start_idx, end_idx + len(close_d)
            # Otherwise, keep looking.
            end_idx += len(close_d)
        start_idx += len(open_d)

def cleanup_whitespace(input):
    """
    Strip leading/trailing whitespace, replace newlines and runs of whitespace
    with single spaces.
    """
    return ' '.join(input.strip().split())

def get_prompt():
    """
    If the default system encoding is utf-8, return a fancy lambda
    prompt. Otherwise return an ascii prompt (the string 'lambda > ').
    """
    return u'\u03bb > ' if sys.getdefaultencoding() == 'utf_8' else 'lambda > '

def enable_utf8():
    """
    If sys.stdin and sys.stdout both have utf-8 encoding, then set the default
    encoding to utf-8, otherwise raise a UnicodeError.
    """
    if sys.stdin.encoding == 'UTF-8' and sys.stdout.encoding == 'UTF-8':
        reload(sys)
        sys.setdefaultencoding('utf_8')
    else:
        raise UnicodeError('UTF-8 encoding not enabled on stdin AND stdout!')

def is_symbol(thing):
    """
    If THING is a symbol, return a regular expression Match object. Otherwise,
    return a boolean false value (False/None).
    """
    try:
        return SYMBOL_REGEX.match(thing)
    except TypeError:
        return False

def stream(thing):
    """
    Given THING (an iterable), yield successive elements of THING. When all
    elements have been yielded, return None to signal the end of the stream.
    """
    for item in thing:
        yield item
    yield None


import packet
import event

class GEL(object):
    def __init__(self):
        self.head = None

    def schedule(self, type, time, packet):
        new_event = event.Event(time, packet, type, None, None)
        self.insert(new_event)

    def insert(self, new_event):
        if self.head == None:
            self.head = new_event
            return None
        iterator = self.head
        if iterator.time > new_event.time:
            self.head = new_event
            new_event.next = iterator
            iterator.prev = new_event
            return new_event
        while (iterator.next != None) and (new_event.time > iterator.next.time):
            iterator = iterator.next
        new_event.prev = iterator
        new_event.next = iterator.next
        iterator.next = new_event
        if new_event.next != None:
            new_event.next.prev = new_event
        return new_event

    def pop(self):
        if self.head == None:
            return None
        first_event = self.head
        if first_event.next != None:
            first_event.next.prev = None
        self.head = first_event.next
        first_event.next = None
        return first_event

    def print_elements(self):
        iterator = self.head
        while iterator != None:
            print(iterator.time)
            iterator = iterator.next


import os
import json
from datetime import datetime
from copy import deepcopy
from dateutil.parser import parse
import jsonschema
from jsonschema.validators import RefResolver

from pystac import (Catalog, Item, Asset, LabelItem, LabelCount, LabelOverview,
                    LabelClasses, Collection, Extent, TemporalExtent,
                    SpatialExtent, STAC_VERSION, STAC_IO, EOItem, MediaType,
                    ItemCollection, SingleFileSTAC)

TEST_LABEL_CATALOG = {
    'country-1': {
        'area-1-1': {
            'dsm': 'area-1-1_dsm.tif',
            'ortho': 'area-1-1_ortho.tif',
            'labels': 'area-1-1_labels.geojson'
        },
        'area-1-2': {
            'dsm': 'area-1-2_dsm.tif',
            'ortho': 'area-1-2_ortho.tif',
            'labels': 'area-1-2_labels.geojson'
        }
    },
    'country-2': {
        'area-2-1': {
            'dsm': 'area-2-1_dsm.tif',
            'ortho': 'area-2-1_ortho.tif',
            'labels': 'area-2-1_labels.geojson'
        },
        'area-2-2': {
            'dsm': 'area-2-2_dsm.tif',
            'ortho': 'area-2-2_ortho.tif',
            'labels': 'area-2-2_labels.geojson'
        }
    }
}

RANDOM_GEOM = {
    "type":
    "Polygon",
    "coordinates": [[[-2.5048828125, 3.8916575492899987],
                     [-1.9610595703125, 3.8916575492899987],
                     [-1.9610595703125, 4.275202171119132],
                     [-2.5048828125, 4.275202171119132],
                     [-2.5048828125, 3.8916575492899987]]]
}

RANDOM_BBOX = [
    RANDOM_GEOM['coordinates'][0][0][0], RANDOM_GEOM['coordinates'][0][0][1],
    RANDOM_GEOM['coordinates'][0][1][0], RANDOM_GEOM['coordinates'][0][1][1]
]

RANDOM_EXTENT = Extent(spatial=SpatialExtent.from_coordinates(
    RANDOM_GEOM['coordinates']),
                       temporal=TemporalExtent.from_now())  # noqa: E126

class TestCases:
    @staticmethod
    def get_path(rel_path):
        return os.path.join(os.path.dirname(__file__), rel_path)

    @staticmethod
    def test_case_1():
        return Catalog.from_file(
            TestCases.get_path('data-files/catalogs/test-case-1/catalog.json'))

    @staticmethod
    def test_case_2():
        return Catalog.from_file(
            TestCases.get_path('data-files/catalogs/test-case-2/catalog.json'))

    @staticmethod
    def test_case_3():
        root_cat = Catalog(id='test3',
                           description='test case 3 catalog',
                           title='test case 3 title')

        image_item = Item(id='imagery-item',
                          geometry=RANDOM_GEOM,
                          bbox=RANDOM_BBOX,
                          datetime=datetime.utcnow(),
                          properties={})

        image_item.add_asset(
            'ortho',
            Asset(href='some/geotiff.tiff', media_type=MediaType.GEOTIFF))

        overviews = [
            LabelOverview('label',
                          counts=[LabelCount('one', 1),
                                  LabelCount('two', 2)])
        ]

        label_item = LabelItem(
            id='label-items',
            geometry=RANDOM_GEOM,
            bbox=RANDOM_BBOX,
            datetime=datetime.utcnow(),
            properties={},
            label_description='ML Labels',
            label_type='vector',
            label_properties=['label'],
            label_classes=[LabelClasses(classes=['one', 'two'], name='label')],
            label_tasks=['classification'],
            label_methods=['manual'],
            label_overviews=overviews)
        label_item.add_source(image_item, assets=['ortho'])

        root_cat.add_item(image_item)
        root_cat.add_item(label_item)

        return root_cat

class SchemaValidator:
    REPO = 'https://raw.githubusercontent.com/radiantearth/stac-spec'
    TAG = 'v{}'.format(STAC_VERSION)
    # SCHEMA_BASE_URI = '{}/{}'.format(REPO, TAG)
    # Temporarily set to point to a fork if stac-spec with updated
    # schemas for Label and EO
    # Pending this issue being resolved: https://github.com/radiantearth/stac-spec/issues/618
    SCHEMA_BASE_URI = (
        'https://raw.githubusercontent.com/simonkassel/stac-spec/'
        'sk/refactor-extension-schemas')

    schemas = {
        Catalog: 'catalog-spec/json-schema/catalog.json',
        Collection: 'collection-spec/json-schema/collection.json',
        Item: 'item-spec/json-schema/item.json',
        ItemCollection: 'item-spec/json-schema/itemcollection.json',
        LabelItem: 'extensions/label/json-schema/label-item.json',
        EOItem: 'extensions/eo/json-schema/eo-item.json',
        SingleFileSTAC:
        'extensions/single-file-stac/json-schema/single-file.json'
    }

    for c in schemas:
        schemas[c] = '{}/{}'.format(SCHEMA_BASE_URI, schemas[c])

    def __init__(self):
        self.schema_cache = {}

    def get_schema(self, obj_type):
        schema_uri = SchemaValidator.schemas.get(obj_type)

        if schema_uri is None:
            raise Exception('No schema for type {}'.format(obj_type))
        schema = self.schema_cache.get(obj_type)
        if schema is None:
            schema = json.loads(STAC_IO.read_text(schema_uri))
            self.schema_cache[obj_type] = schema

        resolver = RefResolver(base_uri=schema_uri, referrer=schema)

        return (schema, resolver)

    def validate_object(self, obj):
        return self.validate_dict(obj.to_dict(), type(obj))

    def validate_dict(self, d, obj_type):
        schema, resolver = self.get_schema(obj_type)

        try:
            return jsonschema.validate(instance=d,
                                       schema=schema,
                                       resolver=resolver)
        except jsonschema.exceptions.ValidationError as e:
            print('Validation error in {}'.format(obj_type))
            raise e

def test_to_from_dict(test_class, stac_object_class, d):
    def _parse_times(a_dict):
        for k, v in a_dict.items():
            if isinstance(v, dict):
                _parse_times(v)
            elif isinstance(v, (tuple, list, set)):
                for vv in v:
                    if isinstance(vv, dict):
                        _parse_times(vv)
            else:
                if k == 'datetime':
                    if not isinstance(v, datetime):
                        a_dict[k] = parse(v)
                        a_dict[k] = a_dict[k].replace(microsecond=0)

    d1 = deepcopy(d)
    d2 = stac_object_class.from_dict(d).to_dict()
    _parse_times(d1)
    _parse_times(d2)
    test_class.assertDictEqual(d1, d2)


'''
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.
'''

class Solution:

    def findMedianSortedArrays(self, A, B):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        n = len(A);
        m = len(B);
        ## n>=m: keep it n<m change it

        if n < m:
            A, B, n, m = B, A, m, n

        j = 0

        for i in range(m):
            while (j < n and A[j] < B[i]):
                j += 1

            if j == n:
                A.append(B[i])
                n += 1
                j -= 1

            if B[i] <= A[j]:
                A.insert(j, B[i])
                n += 1
                j += 1

        if n % 2 == 1:
            return A[n // 2]

        return ((float)(A[n // 2] + A[(n // 2) - 1]) / 2)

test = Solution()
print(test.findMedianSortedArrays([1,3], [2,4]))


#!/usr/bin/env python3

# 03.py
#
# python-training/www.practicepython.org (c) 2017 by Franco Masotti
#                                        <franco.masotti@student.unife.it>
#
# python-training/www.practicepython.org is licensed under a
# Creative Commons Attribution 4.0 International License.
#
# You should have received a copy of the license along with this
# work. If not, see <http://creativecommons.org/licenses/by/4.0/>.

CONST = 5
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b=[]

print("a = " + str(a))
for i in a:
    if i < CONST:
        print ("Element " + str(i) + " in list is less than " + str(CONST))
        b.append(i)

print ("Here in a new purged list with all the previously cited elements")
print ("b = " + str(b))

# Ask the user for a number and return a list that contains only elements from
# the original list a that are smaller than that number given by the user.
discriminator = input("Input another number: ")

for i in a:
    if i < int(discriminator):
        print ("Element " + str(i) + " in list is less than " + str(discriminator))


from django import http
from django.conf import settings
from django.template import RequestContext, loader
from django.contrib.sites.models import Site

def page_not_found(request, template_name='perfect404.html'):
    """
    Perfect 404 handler.

    Templates: `perfect404.html`
    Context:
        request_path
            The path of the requested URL (e.g., '/app/pages/bad_page/')
    """
    referer = request.META.get('HTTP_REFERER', '')

    internal = False
    iam = 'http://%s' % Site.objects.get_current().domain
    if referer[:len(iam)] == iam:
        internal = True

    if settings.ADMINS:
        contact = dict(zip(('name', 'email'), settings.ADMINS[0]))
    else:
        contact = None

    t = loader.get_template(template_name)
    return http.HttpResponseNotFound(t.render(RequestContext(
                    request, {
                        'request_path': request.path,
                        'referer': referer,
                        'internal': internal,
                        'contact': contact,
                    })))


from django.shortcuts import redirect, HttpResponseRedirect
from django.contrib.auth import logout
from django.contrib.auth.models import User
from dashboard.models import Profile, Todo, Note, Event
from django import forms
from .widgets import BootstrapDateTimePickerInput

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('preferred_name', 'zip_code')
        widgets = {
            'preferred_name': forms.TextInput(attrs={'placeholder': 'Preferred name', 'class': 'form-control'}),
            'zip_code': forms.TextInput(attrs={'placeholder': 'ZIP Code', 'class': 'form-control'}),
        }

class TodoForm(forms.ModelForm):
    due = forms.DateTimeField(
        input_formats=['%d/%m/%Y %H:%M', '%m/%d/%Y %I:%M %p'],
        widget=BootstrapDateTimePickerInput(format='%d/%m/%Y %H:%M', attrs={'placeholder': 'Deadline'})
    )
    class Meta:
        model = Todo
        fields = ('task', 'due')
        widgets = {
            'task': forms.TextInput(attrs={'placeholder': 'New task', 'class': 'form-control'}),
        }

class NoteForm(forms.ModelForm):
    class Meta:
        model = Note
        fields = ('title', 'body')
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'New note'}),
            'body': forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Body',
                                        'style': 'height: 6rem'}),
        }

class EventForm(forms.ModelForm):
    start_time = forms.DateTimeField(
        input_formats=['%d/%m/%Y %H:%M', '%m/%d/%Y %I:%M %p'],
        widget=BootstrapDateTimePickerInput(format='%d/%m/%Y %H:%M')
    )
    end_time = forms.DateTimeField(
        input_formats=['%d/%m/%Y %H:%M', '%m/%d/%Y %I:%M %p'],
        widget=BootstrapDateTimePickerInput(format='%d/%m/%Y %H:%M')
    )

    class Meta:
        model = Event
        fields = ('title', 'description', 'start_time', 'end_time')
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            'description': forms.Textarea(attrs={'class': 'form-control'}),
        }

class SearchForm(forms.Form):
    search = forms.CharField(required=False, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Search by keyword'}))


from pypresence import Presence, Activity
import time
import psutil

client_id = '532533523424234'  # Fake ID, put your real one here

RPC = Presence(client_id)  # Initialize the client class
RPC.connect() # Start the handshake loop
ac = Activity(RPC) # Make the activity

ac.start = int(time.time())

print(ac.start)
while True:  # The presence will stay on as long as the program is running
    cpu_per = round(psutil.cpu_percent(),1) # Get CPU Usage
    mem_per = round(psutil.virtual_memory().percent,1) #Get Mem Usage

    ac.details = "RAM: {}%".format(mem_per) # Setting attrs of an activity will auto update the presence
    ac.state = "CPU: {}%".format(cpu_per)

    time.sleep(15) # Can only update rich presence every 15 seconds


import sys, os, csv
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'se_project.settings'
django.setup()
#project_dir = "/home/HOMEGROUP/adelg000/school/CEN4010/BookStore/BookStore/"
#sys.path.append(project_dir)

from user.models import *

count = 0
data = csv.reader(open("sample_data/CART_CONTENT.csv"),delimiter=",")
for row in data:

	if count == 1:
		print("Reading CART_CONTENT model ...")

		cc = CART_CONTENT()

		cc.Cart_contentID=int(row[0])
		print("Cart_contentID = " + str(cc.Cart_contentID))

		# Reading ISBN ...
		bList = BOOK.objects.filter(ISBN=row[1])
		if bList:
			b = bList[0]

			if b:
				cc.ISBN = b;
		else:
			b = BOOK()
			b.ISBN = row[1]
			b.save()
			cc.ISBN = b
		print("ISBN = " + cc.ISBN.ISBN)

		# Reading Cart_ID ...
		cList = CART.objects.filter(Cart_ID=row[2])
		if cList:
			c = cList[0]

			if c:
				cc.Cart_ID = c;
		else:
			c = CART()
			c.Cart_ID = row[2]
			c.save()

		print("Cart_ID = " + cc.Cart_ID.Cart_ID)

		cc.Quantity = int(row[3])
		print("Quantity = " + str(cc.Quantity))

		cc.save()
		print("CART_CONTENT read SUCCESSFUL")

	count = 1


from dependency_injector.wiring import inject, Provide
from fastapi import APIRouter, Depends, Response

from schemas import application_schemas
from services.application_service import ApplicationService
from services.change_history_service import ChangeHistoryService
from containers import Container

router = APIRouter(tags=['applications'])

@router.post("/applications", response_model=application_schemas.ApplicationSchema, status_code=201)
@inject
async def create(
    app: application_schemas.ApplicationCreateSchema,
    app_service: ApplicationService = Depends(Provide[Container.app_service])
) -> Response:
    """Creates an application

    """

    app = await app_service.create(app)

    return app

@router.put("/applications/{app_id}", response_model=application_schemas.ApplicationSchema)
@inject
async def update(
    app_id: int,
    app_data: application_schemas.ApplicationCreateSchema,
    app_service: ApplicationService = Depends(Provide[Container.app_service]),
    change_hostory_service: ChangeHistoryService = Depends(Provide[Container.change_history_service])
) -> Response:
    """Updates an application by id

    """

    await change_hostory_service.make_history(app_id, 'applications', app_data)

    return await app_service.update(app_id, app_data)

@router.delete("/applications/{app_id}", status_code=200)
@inject
async def delete(
    app_id: int,
    app_service: ApplicationService = Depends(Provide[Container.app_service])
) -> Response:
    """Deletes an application by id

    """

    await app_service.delete(app_id=app_id)

    return {'deleted': app_id}

@router.get("/applications", response_model=application_schemas.ApplicationsListSchema)
@inject
async def get_list(
    page: int = 1,
    per_page: int = 10,
    app_service: ApplicationService = Depends(Provide[Container.app_service])
) -> Response:
    """Gets all existing applications

    """

    total_count = await app_service.get_count()
    applications = await app_service.get_list(page, per_page)

    return {"total_count": total_count, "data": applications}


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

import json

from neutron.openstack.common import log as logging
from oslo.config import cfg
import XenAPI

LOG = logging.getLogger(__name__)
CONF = cfg.CONF

agent_opts = [
    cfg.StrOpt("xapi_connection_url"),
    cfg.StrOpt("xapi_connection_username", default="root"),
    cfg.StrOpt("xapi_connection_password")
]

CONF.register_opts(agent_opts, "AGENT")

class VIF(object):
    SEPARATOR = "."

    def __init__(self, device_id, mac_address):
        """Constructs VIF

        `device_id` and `mac_address` should be strings if they will later be
        compared to decoded VIF instances (via from_string).
        """

        self.device_id = device_id
        self.mac_address = mac_address

    def __str__(self):
        return "%s%s%s" % (self.device_id, self.SEPARATOR, self.mac_address)

    @classmethod
    def from_string(cls, s):
        device_id, mac_address = s.split(cls.SEPARATOR)
        return cls(device_id, mac_address)

    def __repr__(self):
        return "VIF(%r, %r)" % (self.device_id, self.mac_address)

    def __eq__(self, other):
        return (self.device_id == other.device_id and
                self.mac_address == other.mac_address)

    def __ne__(self, other):
        return not self.__eq__(other)

    def __hash__(self):
        return hash((self.device_id, self.mac_address))

class XapiClient(object):
    SECURITY_GROUPS_KEY = "failmode"
    SECURITY_GROUPS_VALUE = "secure"

    def __init__(self):
        session = self._session()
        try:
            self._host_ref = session.xenapi.session.get_this_host(
                session.handle)
            self._host_uuid = session.xenapi.host.get_uuid(self._host_ref)
        finally:
            session.xenapi.logout()

    def _session(self):
        LOG.debug("Created new Xapi session")

        session = XenAPI.Session(CONF.AGENT.xapi_connection_url)
        session.login_with_password(CONF.AGENT.xapi_connection_username,
                                    CONF.AGENT.xapi_connection_password)
        return session

    def get_instances(self):
        """Returns a dict of `VM OpaqueRef` (str) -> `device_id` (str)."""
        LOG.debug("Getting instances from Xapi")

        session = self._session()
        try:
            recs = session.xenapi.VM.get_all_records()
        finally:
            session.xenapi.logout()

        # NOTE(asadoughi): Copied from xen-networking-scripts/utils.py
        is_inst = lambda r: (r['power_state'].lower() == 'running' and
                             not r['is_a_template'] and
                             not r['is_control_domain'] and
                             ('nova_uuid' in r['other_config'] or
                              r['name_label'].startswith('instance-')))
        instances = dict()
        for vm_ref, rec in recs.iteritems():
            if not is_inst(rec):
                continue
            instances[vm_ref] = rec["other_config"]["nova_uuid"]
        return instances

    def get_interfaces(self, instances):
        """Returns a set of VIFs from `get_instances` return value."""
        LOG.debug("Getting interfaces from Xapi")

        session = self._session()
        try:
            recs = session.xenapi.VIF.get_all_records()
        finally:
            session.xenapi.logout()

        interfaces = set()
        for rec in recs.values():
            device_id = instances.get(rec["VM"])
            if not device_id:
                continue
            interfaces.add(VIF(device_id, rec["MAC"]))
        return interfaces

    def _process_security_groups(self, session, vm_ref, vif, fn):
        """Process security groups setting on VM's Xenstore for VIF.

        Based on the argument function, VM Opaque Ref, and VIF, we set the
        appropriate VM's Xenstore data to enable security groups or disable
        security groups for the correct VIF.
        """
        vm = session.xenapi.VM.get_record(vm_ref)
        location = ('vm-data/networking/%s' %
                    vif.mac_address.replace(':', ''))
        xsdata = json.loads(vm["xenstore_data"][location])

        # Update Xenstore dict based on fn
        fn(xsdata)

        # Update param Xenstore
        session.xenapi.VM.remove_from_xenstore_data(
            vm_ref, location)
        session.xenapi.VM.add_to_xenstore_data(
            vm_ref, location, json.dumps(xsdata))

        # Update running VM's Xenstore
        args = dict(host_uuid=self._host_uuid,
                    path=location,
                    value=json.dumps(xsdata),
                    dom_id=session.xenapi.VM.get_domid(vm_ref))
        if not args["dom_id"] or args["dom_id"] == -1:
            # If the VM is not running, no need to update the live Xenstore
            return

        session.xenapi.host.call_plugin(
            self._host_ref, "xenstore.py", "write_record", args)

    def _set_security_groups(self, session, vm_refs, interfaces):
        LOG.debug("Setting security groups on %s", interfaces)

        for interface in interfaces:
            vm_ref = vm_refs[interface.device_id]
            self._process_security_groups(
                session,
                vm_ref,
                interface,
                lambda d: d.update({self.SECURITY_GROUPS_KEY:
                                    self.SECURITY_GROUPS_VALUE}))

    def _unset_security_groups(self, session, vm_refs, interfaces):
        LOG.debug("Unsetting security groups on %s", interfaces)

        for interface in interfaces:
            vm_ref = vm_refs[interface.device_id]
            self._process_security_groups(
                session,
                vm_ref,
                interface,
                lambda d: d.pop(self.SECURITY_GROUPS_KEY, None))

    def _refresh_interfaces(self, session, interfaces):
        LOG.debug("Refreshing devices on %s", interfaces)

        device_ids = set([vif.device_id for vif in interfaces])
        for device_id in device_ids:
            args = {"host_uuid": self._host_uuid, "uuid": device_id}
            session.xenapi.host.call_plugin(
                self._host_ref,
                "post_live_migrate",
                "instance_post_live_migration",
                args)

    def update_interfaces(self, instances, added_sg, updated_sg, removed_sg):
        """Handles changes to interfaces' security groups

        Calls refresh_interfaces on argument VIFs. Set security groups on
        added_sg's VIFs. Unsets security groups on removed_sg's VIFs.
        """
        if not (added_sg or updated_sg or removed_sg):
            return

        session = self._session()
        vm_refs = dict((v, k) for k, v in instances.iteritems())
        try:
            self._set_security_groups(session, vm_refs, added_sg)
            self._unset_security_groups(session, vm_refs, removed_sg)
            self._refresh_interfaces(session,
                                     added_sg + updated_sg + removed_sg)
        finally:
            session.xenapi.logout()


# -*- coding: utf-8 -*-
"""
Created on Fri Jan  3 23:25:55 2020

@author: AKASH DIXIT
"""

import http.client, urllib.request, urllib.parse, urllib.error, base64
subscription_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key':subscription_key
}

body = {
    "faceId": "37a6b4dc-bb24-4d68-90f4-0dd3067498f9",
    "personId": "7cc20792-2367-4ad2-b046-2073478bc5d7",
    "personGroupId": "friends"
    }

params = urllib.parse.urlencode({
})

try:
    conn = http.client.HTTPSConnection('centralindia.api.cognitive.microsoft.com')
    conn.request("POST", "/face/v1.0/verify?%s" % params, str(body), headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))


import yaml
from yaml import load, load_all

stream = open('sample.yaml', 'r')
document = load_all(stream, Loader=yaml.FullLoader)

print(type(document))

for doc in document:
	print(type(doc))

	print(doc['people'][1])


"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from registration import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.HomePage, name='home'),
    url(r'^userlogin/$', views.UserLogin.as_view(), name = "user_login"),
    url(r'^companylogin/$', views.CompanyLogin.as_view(), name = "company_login"),
    url(r'^userlogout/$', views.user_logout, name = "user_logout"),
    url(r'^usersignup/$', views.UserSignUp, name = "user_signup"),
    url(r'^companysignup/$', views.CompanySignUp, name = "company_signup"),
    url(r'^job_seeker/',include('job_seeker.urls', namespace = "job_seeker") ),
    url(r'^company/', include('company.urls'), name = "company"),
    url(r'^editor/', include('editor.urls'), name = "editor")
]


#Ejercicio 3 IDS

while True :

    primernum= (int(input("Ingrese un numero entero: ")))
    segundonum=(int(input("Ingrese un numero entero: ")))
    try:
        prueba1 = int(primernum)
        prueba2 = int(segundonum)
        print("El numero es entero")
    except ValueError:
        print("Elnumero no es un entero, ingresarlo de nuevo")
    if primernum>segundonum:
        print(f"El numero mayor es: {primernum}")
    else:
     print(f"El numero mayor es: {segundonum}")


# coding: utf-8

"""
    COVID-19 Local API

    API to get local help information about COVID-19 (hotlines, websites, test sites, health departments)  # noqa: E501

    The version of the OpenAPI document: 0.1.0
    Generated by: https://openapi-generator.tech
"""

import pprint
import re  # noqa: F401

import six

from covid_local.configuration import Configuration

class TestSite(object):
    """NOTE: This class is auto generated by OpenAPI Generator.
    Ref: https://openapi-generator.tech

    Do not edit the class manually.
    """

    """
    Attributes:
      openapi_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    openapi_types = {
        'country_code': 'str',
        'lat': 'float',
        'lon': 'float',
        'name': 'str',
        'street': 'str',
        'zip_code': 'int',
        'city': 'str',
        'address_supplement': 'str',
        'phone': 'str',
        'website': 'str',
        'operating_hours': 'str',
        'appointment_required': 'bool',
        'description': 'str',
        'sources': 'str',
        'distance': 'float'
    }

    attribute_map = {
        'country_code': 'country_code',
        'lat': 'lat',
        'lon': 'lon',
        'name': 'name',
        'street': 'street',
        'zip_code': 'zip_code',
        'city': 'city',
        'address_supplement': 'address_supplement',
        'phone': 'phone',
        'website': 'website',
        'operating_hours': 'operating_hours',
        'appointment_required': 'appointment_required',
        'description': 'description',
        'sources': 'sources',
        'distance': 'distance'
    }

    def __init__(self, country_code=None, lat=None, lon=None, name=None, street=None, zip_code=None, city=None, address_supplement=None, phone=None, website=None, operating_hours=None, appointment_required=None, description=None, sources=None, distance=None, local_vars_configuration=None):  # noqa: E501
        """TestSite - a model defined in OpenAPI"""  # noqa: E501
        if local_vars_configuration is None:
            local_vars_configuration = Configuration()
        self.local_vars_configuration = local_vars_configuration

        self._country_code = None
        self._lat = None
        self._lon = None
        self._name = None
        self._street = None
        self._zip_code = None
        self._city = None
        self._address_supplement = None
        self._phone = None
        self._website = None
        self._operating_hours = None
        self._appointment_required = None
        self._description = None
        self._sources = None
        self._distance = None
        self.discriminator = None

        if country_code is not None:
            self.country_code = country_code
        if lat is not None:
            self.lat = lat
        if lon is not None:
            self.lon = lon
        if name is not None:
            self.name = name
        if street is not None:
            self.street = street
        if zip_code is not None:
            self.zip_code = zip_code
        if city is not None:
            self.city = city
        if address_supplement is not None:
            self.address_supplement = address_supplement
        if phone is not None:
            self.phone = phone
        if website is not None:
            self.website = website
        if operating_hours is not None:
            self.operating_hours = operating_hours
        if appointment_required is not None:
            self.appointment_required = appointment_required
        if description is not None:
            self.description = description
        if sources is not None:
            self.sources = sources
        if distance is not None:
            self.distance = distance

    @property
    def country_code(self):
        """Gets the country_code of this TestSite.  # noqa: E501

        :return: The country_code of this TestSite.  # noqa: E501
        :rtype: str
        """
        return self._country_code

    @country_code.setter
    def country_code(self, country_code):
        """Sets the country_code of this TestSite.

        :param country_code: The country_code of this TestSite.  # noqa: E501
        :type: str
        """

        self._country_code = country_code

    @property
    def lat(self):
        """Gets the lat of this TestSite.  # noqa: E501

        :return: The lat of this TestSite.  # noqa: E501
        :rtype: float
        """
        return self._lat

    @lat.setter
    def lat(self, lat):
        """Sets the lat of this TestSite.

        :param lat: The lat of this TestSite.  # noqa: E501
        :type: float
        """

        self._lat = lat

    @property
    def lon(self):
        """Gets the lon of this TestSite.  # noqa: E501

        :return: The lon of this TestSite.  # noqa: E501
        :rtype: float
        """
        return self._lon

    @lon.setter
    def lon(self, lon):
        """Sets the lon of this TestSite.

        :param lon: The lon of this TestSite.  # noqa: E501
        :type: float
        """

        self._lon = lon

    @property
    def name(self):
        """Gets the name of this TestSite.  # noqa: E501

        :return: The name of this TestSite.  # noqa: E501
        :rtype: str
        """
        return self._name

    @name.setter
    def name(self, name):
        """Sets the name of this TestSite.

        :param name: The name of this TestSite.  # noqa: E501
        :type: str
        """

        self._name = name

    @property
    def street(self):
        """Gets the street of this TestSite.  # noqa: E501

        :return: The street of this TestSite.  # noqa: E501
        :rtype: str
        """
        return self._street

    @street.setter
    def street(self, street):
        """Sets the street of this TestSite.

        :param street: The street of this TestSite.  # noqa: E501
        :type: str
        """

        self._street = street

    @property
    def zip_code(self):
        """Gets the zip_code of this TestSite.  # noqa: E501

        :return: The zip_code of this TestSite.  # noqa: E501
        :rtype: int
        """
        return self._zip_code

    @zip_code.setter
    def zip_code(self, zip_code):
        """Sets the zip_code of this TestSite.

        :param zip_code: The zip_code of this TestSite.  # noqa: E501
        :type: int
        """

        self._zip_code = zip_code

    @property
    def city(self):
        """Gets the city of this TestSite.  # noqa: E501

        :return: The city of this TestSite.  # noqa: E501
        :rtype: str
        """
        return self._city

    @city.setter
    def city(self, city):
        """Sets the city of this TestSite.

        :param city: The city of this TestSite.  # noqa: E501
        :type: str
        """

        self._city = city

    @property
    def address_supplement(self):
        """Gets the address_supplement of this TestSite.  # noqa: E501

        :return: The address_supplement of this TestSite.  # noqa: E501
        :rtype: str
        """
        return self._address_supplement

    @address_supplement.setter
    def address_supplement(self, address_supplement):
        """Sets the address_supplement of this TestSite.

        :param address_supplement: The address_supplement of this TestSite.  # noqa: E501
        :type: str
        """

        self._address_supplement = address_supplement

    @property
    def phone(self):
        """Gets the phone of this TestSite.  # noqa: E501

        :return: The phone of this TestSite.  # noqa: E501
        :rtype: str
        """
        return self._phone

    @phone.setter
    def phone(self, phone):
        """Sets the phone of this TestSite.

        :param phone: The phone of this TestSite.  # noqa: E501
        :type: str
        """

        self._phone = phone

    @property
    def website(self):
        """Gets the website of this TestSite.  # noqa: E501

        :return: The website of this TestSite.  # noqa: E501
        :rtype: str
        """
        return self._website

    @website.setter
    def website(self, website):
        """Sets the website of this TestSite.

        :param website: The website of this TestSite.  # noqa: E501
        :type: str
        """

        self._website = website

    @property
    def operating_hours(self):
        """Gets the operating_hours of this TestSite.  # noqa: E501

        :return: The operating_hours of this TestSite.  # noqa: E501
        :rtype: str
        """
        return self._operating_hours

    @operating_hours.setter
    def operating_hours(self, operating_hours):
        """Sets the operating_hours of this TestSite.

        :param operating_hours: The operating_hours of this TestSite.  # noqa: E501
        :type: str
        """

        self._operating_hours = operating_hours

    @property
    def appointment_required(self):
        """Gets the appointment_required of this TestSite.  # noqa: E501

        :return: The appointment_required of this TestSite.  # noqa: E501
        :rtype: bool
        """
        return self._appointment_required

    @appointment_required.setter
    def appointment_required(self, appointment_required):
        """Sets the appointment_required of this TestSite.

        :param appointment_required: The appointment_required of this TestSite.  # noqa: E501
        :type: bool
        """

        self._appointment_required = appointment_required

    @property
    def description(self):
        """Gets the description of this TestSite.  # noqa: E501

        :return: The description of this TestSite.  # noqa: E501
        :rtype: str
        """
        return self._description

    @description.setter
    def description(self, description):
        """Sets the description of this TestSite.

        :param description: The description of this TestSite.  # noqa: E501
        :type: str
        """

        self._description = description

    @property
    def sources(self):
        """Gets the sources of this TestSite.  # noqa: E501

        :return: The sources of this TestSite.  # noqa: E501
        :rtype: str
        """
        return self._sources

    @sources.setter
    def sources(self, sources):
        """Sets the sources of this TestSite.

        :param sources: The sources of this TestSite.  # noqa: E501
        :type: str
        """

        self._sources = sources

    @property
    def distance(self):
        """Gets the distance of this TestSite.  # noqa: E501

        :return: The distance of this TestSite.  # noqa: E501
        :rtype: float
        """
        return self._distance

    @distance.setter
    def distance(self, distance):
        """Sets the distance of this TestSite.

        :param distance: The distance of this TestSite.  # noqa: E501
        :type: float
        """

        self._distance = distance

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.openapi_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, TestSite):
            return False

        return self.to_dict() == other.to_dict()

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        if not isinstance(other, TestSite):
            return True

        return self.to_dict() != other.to_dict()


from typing import Callable, List, Literal, Optional, Sequence, TypeVar, Union
from pathlib import Path

from requests import HTTPError

import calcbench as cb
import pandas as pd
from tqdm.auto import tqdm

cb.enable_backoff(
    giveup=lambda e: isinstance(e, HTTPError) and (e.response.status_code in [404, 500])
)

T = TypeVar("T")

def iterate_to_dataframe(
    arguments: Sequence[T],
    f: Callable[[T], pd.DataFrame],
) -> pd.DataFrame:
    """Apply arguments to a function that returns a DataFrame append to a dataframe and return.

    Usage::

    >>> %pip install calcbench-api-client[Pandas,Backoff,tqdm]
    >>> from calcbench.downloaders import iterate_to_dataframe
    >>> import calcbench as cb
    >>> tickers = cb.tickers(entire_universe=True)
    >>> d = iterate_and_save_pandas(
    >>>    tickers,
    >>>    lambda ticker: cb.point_in_time(
    >>>        all_face=True,
    >>>        all_footnotes=False,
    >>>        company_identifiers=[ticker],
    >>>        all_history=True,
    >>>        include_preliminary=True,
    >>>        include_xbrl=True,
    >>>    ),
    >>> )
    """
    argument: T
    chunks = []
    for argument in tqdm(list(arguments)):
        try:
            d = f(argument)
            if d.empty:
                continue
        except KeyboardInterrupt:
            raise
        except Exception as e:
            tqdm.write(f"Exception getting {argument} {e}")
        else:
            chunks.append(d)
    if chunks:
        return pd.concat(chunks)
    else:
        return pd.DataFrame()

def iterate_and_save_pandas(
    arguments: Sequence[T],
    f: Callable[[T], pd.DataFrame],
    file_name: Union[str, Path],
    write_index: bool = True,
    columns: Optional[Sequence[str]] = None,
    write_mode: Literal["w", "a"] = "w",
):
    """Apply arguments to a function that returns a DataFrame and save to a .csv file.

    :param arguments: Each item in this sequence will be passed to f
    :param f: Function that generates a pandas dataframe that will be called on arguments
    :param file_name: Name of the file to write
    :param write_index: Write the pandas index to the csv file
    :param columns: which columns to write.  If this is set the index is not written
    :param write_mode: set the initial write mode.  "a" to append, "w" to overwrite.  Useful for resuming downloading.

    Usage::

    >>> %pip install calcbench-api-client[Pandas,Backoff,tqdm]
    >>> from calcbench.downloaders import iterate_and_save_pandas
    >>> import calcbench as cb
    >>> tickers = cb.tickers(entire_universe=True)
    >>> iterate_and_save_pandas(
    >>>    arguments=tickers,
    >>>    f=lambda ticker: cb.standardized(company_identifiers=[ticker], point_in_time=True),
    >>>    file_name="calcbench_standardized_PIT.csv",
    >>> )

    """
    argument: T
    write_headers = write_mode != "a"
    for argument in tqdm(list(arguments)):
        try:
            data = f(argument)
            if data.empty:
                continue
        except KeyboardInterrupt:
            raise
        except Exception as e:
            tqdm.write(f"Exception getting {argument} {e}")
        else:
            if columns:
                try:
                    data = data.reset_index()[columns]
                except Exception as e:
                    tqdm.write(f"Exception getting columns for {argument} {e}")
                    continue
            data.to_csv(
                file_name,
                mode=write_mode,
                index=write_index,
                header=write_headers,
            )
            write_mode = "a"
            write_headers = False

def iterate_and_save_pyarrow_dataset(
    arguments: Sequence[T],
    f: Callable[[T], pd.DataFrame],
    root_path: Union[str, Path],
    partition_cols: Optional[List[str]] = ["ticker"],
):
    """
    Apply the arguments to a function a save to a pyarrow dataset.

    Usage::

    >>> %pip install calcbench-api-client[Pandas,Backoff,tqdm,pyarrow]
    >>> tickers = sorted(cb.tickers(entire_universe=True), key=lambda ticker: hash(ticker)) # randomize the order so the time estimate is better
    >>> iterate_and_save_pyarrow_dataset(
    >>>     arguments=tickers,
    >>>     f=lambda ticker: cb.standardized(company_identifiers=[ticker], point_in_time=True),
    >>>     root_path="~/standardized_PIT_arrow/",
    >>>     partition_cols=["ticker"],
    >>> )
    >>> # Read the dataset
    >>> import pyarrow.parquet as pq
    >>> import pyarrow.compute as pc
    >>> table = pq.read_table(<root_path>)
    >>> expr = pc.field("ticker") == "MSFT"
    >>> msft_data = table.filter(expr).to_pandas()
    >>>
    >>> # Write the data
    >>> pq.write_table(table, "C:/Users/andre/Downloads/standardized_data.parquet")
    >>> from pyarrow import csv
    >>> csv.write_csv(table, "C:/Users/andre/Downloads/entire_universe_standardized_PIT.csv")

    """
    import pyarrow as pa
    import pyarrow.parquet as pq

    for argument in tqdm(list(arguments)):
        try:
            df = f(argument)
            if df.empty:
                continue
        except KeyboardInterrupt:
            raise
        except Exception as e:
            tqdm.write(f"Exception getting {argument} {e}")
        else:
            table = pa.Table.from_pandas(df)
            pq.write_to_dataset(
                table=table,
                root_path=root_path,
                partition_cols=partition_cols,
                **{"allow_truncated_timestamps": True, "coerce_timestamps": "us"},
            )


from game_code import interactions
from game_code.interactions.lib import choices, events, conditions, scene
from game_code.objects import item, entry

library = interactions.room.Room(
    name='Library',
    opening_text='You enter a vast library.\n'
                 'a dozen rows of bookshelves bend under the weight of hundreds of old tomes.\n'
                 'A writing desk is pressed against the wall on the far end of the room.',
    choices=[
        choices.ChoiceInspectRoom(text='Search the writing desk', scene='desk', conditions=[conditions.OnlyOnce()]),
        choices.ChoiceNavigate('Leave room', level='level_2', room='grande_hall'),
    ],
    scenes={
        'desk': interactions.thing.Thing(
            name='Writing Desk',
            opening_text='A few ink pads, vials and a feather.\n'
                         'A few empty scrolls and... a scribble of a clock. The hands point at 3:15. ',
            choices=[
                choices.ChoiceNavigate('Back to the Grande Hall', level='level_2', room='grande_hall'),
            ],
            events=[
                events.UnlockJournal(entry.clock_scribble),
                events.SetGameFlagTrue('clock_scribble'),
            ]
        ),
        # gargoyle fight
        'gargoyle_fight': interactions.thing.Thing(
            name='Library',
            opening_text='You dart into the library and dive behind a book shelf.\n'
                         'The gargoyle roars as it topples all the bookshelves with one great heave.\n'
                         'You barely avoid getting trapped under the collapsing tomes.\n'
                         'And escape back to the Grande Hall',
        )
    }
)


class game:
    def __init__(self):
        print("""
                Press 1 if you would like to play the Fibonacci game.
                Press 2 if you woukd like to play our ...
                Press 3 if you would like to play our ...        """)
        Ans = int(input("\t        Enter your number now\n"))

        if Ans == 1:
            self.fib()

    def fib(self):
        print("You have chosen to play our game based around fibonacci. Fibonacci is a sequence of numbers in which " +
        "the next number of the list is calculated by adding the n-1th term and the n-2th term together" +
        "For example, the first 2 numbers are 0 and 1. So the 3rd number is 0 + 1 which is 1.")
        print("How many values of the fibonacci sequence would you like to see?")
        self.n = int(input())

        self.fibonacci = [0,1]
        if self.n<=0:
            print("Input has to be greater than 0!!!")
        elif self.n == 1:
            return self.fibonacci[0]
        elif self.n == 2:
            return self.fibonacci
        else:
            for i in range(2,self.n):
                tempfib = (self.fibonacci[-1] + self.fibonacci[-2])
                self.fibonacci.append(tempfib)

        print(self.fibonacci)
        self.nextNum = self.fibonacci[-1] + self.fibonacci[-2]

        print("Now its your turn! what is the next number in the fibonacci sequence?")
        self.score = 0
        self.ans = int(input())

        if self.ans == self.nextNum:
            print("Correct, you are getting the hang of this!")
            self.fibonacci.append(self.ans)
            self.score += 1
            print("Your score is: " + str(self.score))
        else:
            print("Wrong, the correct number is {}".format(self.nextNum))

g = game()


import numpy as np
from sklearn.cross_validation import StratifiedShuffleSplit
from numpy.random import random_sample
from collections import Counter

class SNN():
	"""
		A simple one class classifier, implemented for outlier removal.
		To be used with outlier removal function.
	"""

	def __init__(self, error_tolerance=10e-4, learning_rate = .05, outlier_removal_percent=.05):
		self.learning_rate = learning_rate
		self.error_tolerance = error_tolerance
		self.outlier_removal_percent = outlier_removal_percent

	def fit(self, X):

		#converts X to np array
		if (type(X) == list):
			X = np.array(X)

		#randomly initialize weights
		n_samples, n_features = X.shape
		self.w_ = random_sample((1 + n_features, ))
		self.e_ = []
		y = np.ones(n_samples)

		#regularize features from 0 to 1
		for feat in range(n_features):
			min_val = min(X[:, feat])
			max_min_range = max(X[:, feat]) - min_val
			for sample in range(n_samples):
				X[sample, feat] = ((X[sample, feat] - min_val) / max_min_range)

		#iterate untill it converges
		improvement, total_error = 1, None
		while improvement >= self.error_tolerance:
			errors = []
			for xi, target in zip(X, y):
				error = self.predict(xi) - target
				update = self.learning_rate * error
				self.w_[1:] -= update * xi
				self.w_[0] -= update
				errors.append(abs(error))
			self.e_.append(errors)

			#calculates improvement of prediction
			if total_error:
				improvement = (total_error - sum(errors))/total_error
			total_error = sum(errors)

	def predict(self, xi):
		return np.dot(xi, self.w_[1:]) + self.w_[0]

	def id_outliers(self):
		top_X = int(len(self.e_[-1]) * self.outlier_removal_percent)
		error_per_sample = zip(range(len(self.e_[-1])), self.e_[-1])
		return [v[0] for v in sorted(error_per_sample, key=lambda x:-x[1])[:top_X]]

def id_outliers(features, labels, outlier_removal_iterations = 3, outlier_removal_percent=.05, clf_attempts = 5):
	"""
		An outlier removal function.
		Takes 3 adjustable parameters:
			outlier_removal_iterations -> number of iterations it will perform its removal function. In each iteration, the outliers identified are removed before the next iteration is performed
			outlier_removal_percent -> the percentage of outliers that will be removed from X at each iteration
			clf_attempts -> how many times the classifier is run. The sample is only tagged as outlier if in all attempts the sample is flagged as an outlier
			#to fix: issue with indexing. need to return index based on the original list
	"""
	for _ in range(outlier_removal_iterations):
		outliers_counter = Counter()
		for _ in range(clf_attempts):
			clf = SNN(outlier_removal_percent=.05)
			clf.fit(features)
			for outlier in clf.id_outliers():
				outliers_counter[outlier] += 1
		outliers = [k for k,v in outliers_counter.items() if v == clf_attempts]
	return outliers

def remove_outliers(features, labels, outlier_removal_iterations = 3, outlier_removal_percent=.05, clf_attempts = 5):
	outliers = id_outliers(features, labels, outlier_removal_iterations, outlier_removal_percent, clf_attempts)
	features = [feat for idx, feat in enumerate(features) if idx not in outliers ]
	labels = [lab for idx, lab in enumerate(labels) if idx not in outliers ]
	return features, labels, outliers


from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import permission_required
from shop.models import Item, List, UserModel, Store
from shop.forms import AddItemForm
from django.views import generic
from faker import Faker
from django.contrib.auth.forms import PasswordChangeForm

# Create your views here.

class homepage(generic.ListView):
    model = Store
    template_name = "homepage.html"

def store_items(request):
    store = Store.objects.filter(storename__exact="defaultStore")
    context = {"store" : store}
    return render(request, "store_items.html", context=context)

def profile(request):
    #user = UserModel.objects.filter(username__exact="defaultUser")
    #context = {"user": user}
    return render(request, "profilePage.html")

class map(generic.ListView):
    model = Store
    template_name = "map.html"

def signin(request):
    return render(request, "signin.html")

def favorite_cart(request):
    if request.user.is_authenticated:
        userM = UserModel.objects.filter(user__exact=request.user)
        user = request.user
        context = {"userM": userM,
                   "user": user}
        return render(request, "favorite_cart.html", context=context)
    return render(request, "login_error.html")

class StoreListView(generic.ListView):
    model = Store
    template_name = "store_list.html"

class StoreDetailView(generic.DetailView):
    model = Store
    template_name = "items_cart.html"

def favorite_items(request):
    if request.user.is_authenticated:
        userM = UserModel.objects.filter(user__exact=request.user)
        user = request.user
        context = {"userM": userM,
                   "user": user}
        return render(request, "favorite_items.html", context=context)
    return render(request, "login_error.html")

def favorite_items2(request):
    if request.user.is_authenticated:
        userM = UserModel.objects.filter(user__exact=request.user)
        user = request.user
        context = {"userM": userM,
                   "user": user}
        return render(request, "Stats2.html", context=context)
    return render(request, "login_error.html")

def user_preferences(request):
    if request.user.is_authenticated:
        userM = UserModel.objects.filter(user__exact=request.user)
        user = request.user
        context = {"userM": userM,
                   "user": user}

        if request.method == 'POST':
            form = PasswordChangeForm(request.user, request.POST)
            if form.is_valid():
                user = form.save()
                update_session_auth_hash(request, user)  # Important!
                messages.success(request, 'Your password was successfully updated!')
                return redirect('user_preferences')
            else:
                messages.error(request, 'Please correct the error below.')
        else:
            #form = PasswordChangeForm(request.user)
            form = PasswordChangeForm(user)
        return render(request, 'user_preferences.html', {
            'form': form
        })

        #return render(request, "user_preferences.html", context=context)
    return render(request, "login_error.html")

def login_error(request):
    return render(request, "login_error.html")

@permission_required('shop.can_add_items')
def addItem(request):
    fake = Faker()
    if request.user.is_authenticated:
        userM = UserModel.objects.filter(user__exact=request.user)
        user = request.user
        context = {"userM": userM,
                   "user": user}
        if request.method == 'POST':
            form = AddItemForm(request.POST)
            if form.is_valid():
                fakeStore = Store(name=fake.text(50), location=fake.text(10))
                fakeStore.save()
                i_name = form.cleaned_data['new_item']
                i_store = fakeStore
                i_price = fake.random_int(1, 100)
                i_aisle = fake.random_int(1, 26)
                i_short_description = fake.text(150)
                i_item_number = fake.random_int(1, 126)
                item = Item(name=i_name,
                            store=i_store,
                            price=i_price,
                            aisle=i_aisle,
                            short_description=i_short_description,
                            item_number=i_item_number,
                            )
                item.save()
                userM[0].favoriteItems.add(item)
                userM[0].save()
                return HttpResponseRedirect('success/')
        else:
            fakeStore = Store(name=fake.text(50), location=fake.text(10))
            fakeStore.save()
            i_name = fake.text(50)
            i_store = fakeStore
            i_price = fake.random_int(1, 100)
            i_aisle = fake.random_int(1, 26)
            i_short_description = fake.text(150)
            i_item_number = fake.random_int(1, 126)
            def_item = Item(name=i_name,
                        store=i_store,
                        price=i_price,
                        aisle=i_aisle,
                        short_description=i_short_description,
                        item_number=i_item_number,
                        )

            form = AddItemForm(initial={'new_item': def_item,})
        return render(request, "addItem.html", {'form': form})
    return render(request, "login_error.html")

def addItem_success(request):
    return render(request, "success.html")


#!/usr/bin/env python3
"""
Create a standalone telepresence
"""

import sys
from io import BytesIO
from pathlib import Path
from subprocess import check_output
from tempfile import TemporaryDirectory
from zipapp import create_archive
from zipfile import ZIP_DEFLATED, ZipFile

def make_compressed_zipapp(source: Path, output: Path, main_function: str):
    """
    Make an executable compressed zipfile from Python code
    """
    # Build uncompresed zip file
    uncompressed = BytesIO()
    create_archive(source, uncompressed, main=main_function)
    uncompressed.seek(0, 0)

    # Build compressed zip file
    compressed = BytesIO()
    in_zip = ZipFile(uncompressed)
    with ZipFile(compressed, mode="w") as out_zip:
        for info in in_zip.infolist():
            out_zip.writestr(info, in_zip.read(info), ZIP_DEFLATED)
    compressed.seek(0, 0)

    # Build final zip file
    create_archive(compressed, str(output), interpreter="/usr/bin/env python3")
    output.chmod(0o755)  # Seems to default to 744 on CircleCI

def build_telepresence(project: Path, output: Path):
    """
    Build a telepresence executable zipfile
    """
    with TemporaryDirectory() as temp:
        # Run setup.py build to extract source code. Versioneer will replace
        # _version.py with hard-coded version info.
        check_output(["python3", "-Wignore", "setup.py", "build", "-b", temp],
                     cwd=str(project))
        # Build zip app
        make_compressed_zipapp(
            Path(temp) / "lib", output, "telepresence.main:run_telepresence"
        )

    print("Built {}".format(output))

def main():
    """
    Set things up then call the code that builds the executable.
    """
    project = Path(__file__).absolute().resolve().parent.parent

    if len(sys.argv) > 1:
        output = Path(sys.argv[1])
    else:
        version_bytes = check_output(
            ["python3", "-Wignore", "setup.py", "--version"],
            cwd=str(project),
        )
        version = str(version_bytes, "utf-8").strip()

        output = project / "dist" / "telepresence-{}".format(version)

    output.parent.mkdir(parents=True, exist_ok=True)
    if output.exists():
        output.unlink()

    build_telepresence(project, output)

if __name__ == "__main__":
    main()


from django.urls import path

from . import views

urlpatterns = [
    path("<int:id>", views.index, name="index"),
    path("", views.home, name="home"),
    path("home/", views.home, name="home"),
    path("create/", views.create, name="index"),
    path("view/", views.view, name="view"),
    path("assets/", views.assets, name="assets"),
    path("softwares/", views.softwares, name="softwares")
]


import filters
import cv2
import math
import sys
import numpy as np

class Hough(object):

    @staticmethod
    def apply(img, count_alpha, count_rad, threshold):
        img = filters.Filters().diff_image(img, 'laplace')
        cont_prep = Hough._get_countour_preparation(img, threshold)

        rad_max = math.sqrt(math.pow(img.shape[0], 2) + math.pow(img.shape[1], 2))
        delta_alpha = math.pi / (count_alpha - 1)
        delta_rad = 2.0 * rad_max / (count_rad - 1)
        hough_img = np.zeros((count_rad, count_alpha, 3), np.uint8)

        for pixel in cont_prep:
            alpha = -(math.pi / 2)
            for i in xrange(count_alpha):
                R = (pixel[0] * math.cos(alpha) + pixel[1] * math.sin(alpha) + rad_max) / delta_rad
                hough_img[int(R), i] += 1
                alpha += delta_alpha
        return hough_img

    @staticmethod
    def _get_countour_preparation(img, threshold):
        cont_prep = []
        for row in xrange(img.shape[0]):
            for col in xrange(img.shape[1]):
                color_sum = 0
                for channel in xrange(3):
                    color_sum += img[row, col, channel]

                intensity = color_sum / 3
                if intensity >= threshold:
                    cont_prep.append([row, col])
        return cont_prep

if __name__ == '__main__':
    img = cv2.imread(sys.argv[1])

    cv2.imshow('img', img)
    cv2.imshow('hough', Hough.apply(img, 800, 600, 240))
    if cv2.waitKey(0) & 0xFF == ord('s'):
        cv2.imwrite('hough.png', img)
    cv2.destroyAllWindows()


import redis
import time
import random

def add_height_score(db0, db1):
    for player_id in db1.keys():
        height = int(float(db0.lindex(player_id, 5).decode("utf-8")) / 2)
        db1.incr(player_id, height)

def main():
    db0 = redis.Redis(host="localhost", port=6379, db=0)
    db1 = redis.Redis(host="localhost", port=6379, db=1)

    while True:
        try:
            for player_id in db1.keys():
                height = int(float(db0.lindex(player_id, 5).decode("utf-8")))
                db1.incr(player_id, height)

            time.sleep(10)

        # 処理中にプレイヤーが死んだ場合
        except:
            time.sleep(10)

if __name__ == "__main__":
    main()