#!/usr/bin/env python
#-*- coding:utf-8 -*-
##
## mbrun.py
##
##  Created on: Jan 31, 2021
##      Author: Alexey Ignatiev
##      E-mail: alexey.ignatiev@monash.edu
##

# imported modules
#==============================================================================
import getopt
import monoboost as mb
import numpy as np
import os
import pickle
import sys

#
#==============================================================================
def parse_options():
    """
        Parses command-line option
    """

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hm:', ['help', 'model'])
    except getopt.GetoptError as err:
        sys.stderr.write(str(err).capitalize())
        usage()
        sys.exit(1)

    # target model
    model = None

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit(0)
        elif opt in ('-m', '--model'):
            model = str(arg)
        else:
            assert False, 'Unhandled option: {0} {1}'.format(opt, arg)

    return model, [float(v) for v in args[0].strip().split(',')]

#
#==============================================================================
def usage():
    """
        Prints usage message.
        """

    print('Usage:', os.path.basename(sys.argv[0]), '[options] comma-separated-values')
    print('Options:')
    print('        -m, --model=<string>    Path to monoboost model')
    print('        -h, --help              Show this message')

# main
#==============================================================================
if __name__ == '__main__':
    model, inst = parse_options()

    # loading the pickle object and extracing the model
    with open(model, 'rb') as fobj:
        mb_clf = pickle.load(fobj)

    # predicting and reporting the class
    # (note that the class is rounded as monoboost
    # does regression rather than classification)
    print(int(round(mb_clf.predict(np.array([inst]))[0])))


import numpy as np
from scipy.optimize import leastsq
from core import *
from fitting import *
from calibration import *
from kpo import *
from kpi import *
import sys
from numpy.random import rand, randn
from random import choice, shuffle

'''----------------------------------------------------------------------
Grid-fitting tools by Martinache & Pope for chi2 maps.

Use these if nested sampling or MCMC are absolutely out of the question -
they are very slow!
----------------------------------------------------------------------'''

# =========================================================================
# =========================================================================
def chi2map_sep_con(kpo, pa=0, reduced=False, cmap=None,
                    srng=[10.0, 200.0, 100], crng=[10.0, 50.0, 100]):

    ''' Draws a 2D chi2 map of the binary parameter space for a given PA
    ---------------------------------------
    PA: position angle in degrees.
    other options:
    - reduced: (boolean) reduced chi2 or not
    - cmap: (matplotlib color map) "jet" or "prism" are good here
    - srng: range of separations [min, max, # steps]
    - crng: range of contrasts   [min, max, # steps]
    --------------------------------------- '''
    nullvar = np.var(kpo.kpd)
    seps = srng[0] + (srng[1]-srng[0]) * np.arange(srng[2])/float(srng[2])
    cons = crng[0] + (crng[1]-crng[0]) * np.arange(crng[2])/float(crng[2])

    chi2map = np.zeros((srng[2], crng[2]))

    for i,sep in enumerate(seps):
        for j,con in enumerate(cons):
            chi2map[i,j] = np.sum(
                binary_KPD_fit_residuals([sep, pa, con], kpo)**2)
        sys.stdout.write("\r(%3d/%d): sep = %.2f mas" % (i+1, srng[2], sep))
        sys.stdout.flush()
    if reduced:
        chi2map /= kpo.kpd.size
        nullvar /= kpo.kpd.size
    plt.clf()

    asp = 1./np.abs((srng[1]-srng[0])/(crng[1]-crng[0]))
    plt.imshow(chi2map, aspect=asp, cmap=cmap,
               extent=[crng[0], crng[1], srng[0], srng[1]])
    plt.ylabel("ang. sep. (mas)")
    plt.xlabel("contrast prim/sec")

    print("\n----------------------------------------")
    print("Best "+reduced*"reduced "+"chi2 = %.3f" % (chi2map.min(),))
    print(reduced*"reduced "+"chi2 for null hypothesis: %.3f" % (nullvar,))
    arg = chi2map.argmin()
    print("Obtained for sep = %.1f mas" % (seps[arg // chi2map.shape[1]]))
    print("Obtained for con = %.1f"     % (cons[arg  % chi2map.shape[1]]))
    print("----------------------------------------")
    return chi2map

# =========================================================================
# =========================================================================
def chi2map_sep_pa(kpo, con=10., reduced=False, cmap=None,
                    srng=[10.0, 200.0, 100], arng=[0.0, 360.0, 60]):

    ''' Draws a 2D chi2 map of the binary parameter space for a given contrast
    ---------------------------------------
    con: contrast (primary/secondary)
    other options:
    - reduced: (boolean) reduced chi2 or not
    - cmap: (matplotlib color map) "jet" or "prism" are good here
    - srng: range of separations     [min, max, # steps]
    - arng: range of position angles [min, max, # steps]
    --------------------------------------- '''

    nullvar = np.var(kpo.kpd)
    seps = srng[0] + (srng[1]-srng[0]) * np.arange(srng[2])/float(srng[2])
    angs = arng[0] + (arng[1]-arng[0]) * np.arange(arng[2])/float(arng[2])
    nkphi = kpo.kpi.nkphi

    chi2map = np.zeros((srng[2], arng[2]))

    for i,sep in enumerate(seps):
        for j,th in enumerate(angs):
            chi2map[i,j] = np.sum(
                binary_KPD_fit_residuals([sep, th, con], kpo)**2)
        sys.stdout.write("\r(%3d/%d): sep = %.2f mas" % (i+1, srng[2], sep))
        sys.stdout.flush()

    if reduced:
        chi2map /= kpo.kpd.size
        nullvar /= kpo.kpd.size
    plt.clf()

    asp = 1./np.abs((srng[1]-srng[0])/(arng[1]-arng[0]))

    plt.imshow((chi2map), aspect=asp, cmap=cmap,
               extent=[arng[0], arng[1], srng[0], srng[1]])
    plt.xlabel("Position Angle (deg)")
    plt.ylabel("Separation (mas)")

    print("\n----------------------------------------")
    print("Best "+reduced*"reduced "+"chi2 = %.3f" % (chi2map.min()))
    print(reduced*"reduced "+"chi2 for null hypothesis: %.3f" % (nullvar,))
    arg = chi2map.argmin()
    print("Obtained for sep = %.1f mas" % (seps[arg // chi2map.shape[1]]))
    print("Obtained for ang = %.1f deg" % (angs[arg  % chi2map.shape[1]]))
    print("----------------------------------------")
    return chi2map

# =========================================================================
# =========================================================================
def chi2_volume(src, cal=None, regul="None"):
    ''' Proceed to an exhaustive search of the parameter space
    '''

    ns, s0, s1 = 50, 20.0, 200.0
    nc, c0, c1 = 50,  2.0, 100.0
    na, a0, a1 = 60,  0.0, 360.0

    seps = s0 + np.arange(ns) * (s1-s0) / ns
    angs = a0 + np.arange(na) * (a1-a0) / na
    cons = c0 + np.arange(nc) * (c1-c0) / nc

    chi2vol = np.zeros((ns,na,nc))

    sig = src.kpd
    if cal != None: sig -= cal.kpd

    for i,sep in enumerate(seps):
        for j, ang in enumerate(angs):
            for k, con in enumerate(cons):
                res = binary_multi_KPD_fit_residuals([sep, ang, con], src)
                chi2vol[i,j,k] = ((res)**2).sum()

    return chi2vol

# =========================================================================
# =========================================================================
def correlation_plot(kpo, params=[250., 0., 5.],b=None, plot_error=True):
    '''Correlation plot between KP object and a KP binary model

    Parameters are:
    --------------
    - kpo: one instance of kernel-phase object
    - params: a 3-component array describing the binary (sep, PA and contrast)

    Option:
    - plot_error: boolean, errorbar or regular plot
    --------------------------------------------------------------------------
    '''
    if b == None:

        mm = np.round(np.max(np.abs(kpo.kpd)), -1)

        f1 = plt.figure()
        sp0 = f1.add_subplot(111)
        if plot_error:
            sp0.errorbar(binary_KPD_model(kpo, params), kpo.kpd,
                         yerr=kpo.kpe, linestyle='None')
        else:
            sp0.plot(binary_KPD_model(kpo, params), kpo.kpd, 'bo')
        sp0.plot([-mm,mm],[-mm,mm], 'g')
        sp0.axis([-mm,mm,-mm,mm])

        rms = np.std(binary_KPD_fit_residuals(params, kpo))
        msg  = "Model:\n sep = %6.2f mas" % (params[0],)
        msg += "\n   PA = %6.2f deg" % (params[1],)
        msg += "\n  con = %6.2f" % (params[2],)
        msg += "\n(rms = %.2f deg)" % (rms,)

        plt.text(0.0*mm, -0.75*mm, msg,
                 bbox=dict(facecolor='white'), fontsize=14)

        msg = "Target: %s\nTelescope: %s\nWavelength = %.2f um" % (
            kpo.kpi.name, kpo.hdr['tel'], kpo.hdr['filter']*1e6)

        plt.text(-0.75*mm, 0.5*mm, msg,
                  bbox=dict(facecolor='white'), fontsize=14)

        plt.xlabel('Data kernel-phase signal (deg)')
        plt.ylabel('Kernel-phase binary model (deg)')
        plt.draw()
        return None

    else:
        plt.clf()
        mm = np.round(np.max(np.abs(kpo.kpd)), -1)
        plt.errorbar(b,kpo.kpd,yerr=kpo.kpe,fmt='b.')
        plt.plot([-mm,mm],[-mm,mm], 'g')
        plt.axis('tight',fontsize='large')
        plt.xlabel('Model Kernel Phases',fontsize='large')
        plt.ylabel('Kernel Phase Signal', fontsize='large')
        plt.title('Kernel Phase Correlation Diagram',
                  fontsize='large')
        plt.draw()
        plt.show()


# coding=utf-8
from gensim import corpora

# 产生对应格式的texts
def to_texts(file):
    datas = open(file, 'r')
    texts = [[word for word in data.split(',')]
             for data in datas]
    return texts

# 统计维度，并生成对应的字典（去除停用词以及仅出现一次词的id）
def tokens(texts):
    dictionary = corpora.Dictionary(texts)
    dictionary.save('temp/tokens.dict')

    # 停用词
    # stop_ids = [dictionary.token2id[stopword] for stopword in stoplist
    #             if stopword in dictionary.token2id]

    # 出现一次的低频词
    once_ids = [tokenid for tokenid, docfreq in dictionary.dfs.iteritems()
                if docfreq == 1]

    # 删除停用词和低频词，并去除不连续的缺口
    dictionary.filter_tokens(once_ids)
    dictionary.compactify()

    # for index,data in dictionary.token2id.items():
    #     print index,data

    return dictionary

# 产生稀疏文档向量
def to_vectors(dictionary, texts):
    corpus = [dictionary.doc2bow(text) for text in texts]

    # 存入硬盘备用
    corpora.MmCorpus.serialize('temp/vectors.mm', corpus)

    # for data in corpus:
    #     print data

    return corpus

# 不同的语料库格式
def corpus_formate(corpus):
    #存储为不同的语料
    corpora.SvmLightCorpus.serialize('data/corpus.svmlight', corpus)
    corpora.BleiCorpus.serialize('data/corpus.lda-c', corpus)
    corpora.LowCorpus.serialize('data/corpus.low', corpus)

    #载入语料库
    corpus = corpora.MmCorpus('temp/vectors.mm')

if __name__ == '__main__':
    import logging
    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

    file = 'data/result.txt'
    texts= to_texts(file)
    dictionary = tokens(texts)
    corpus = to_vectors(dictionary, texts)
    corpus_formate(corpus)


"""
wikigruth

A Python tool for creating coreference resolution ground truths from Wikipedia.

"""

from wikigrouth.corpus import Corpus

__author__ = 'Bernhard Haslhofer (bernhard.haslhofer@ait.ac.at)'
__copyright__ = 'Copyright 2015, Bernhard Haslhofer'
__license__ = "MIT"
__version__ = '0.1.2'

__all__ = ['aggregate_corpus']

def aggregate_corpus(seedfile, outputpath=None, override=False):
    corpus = Corpus(seedfile, outputpath, override)
    corpus.aggregate(override)


from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _

from cms.toolbar.items import Button, ButtonList

from djangocms_versioning.cms_toolbars import VersioningToolbar

from djangocms_version_locking.helpers import (
    content_is_unlocked_for_user,
    get_lock_for_content,
)

class ButtonWithAttributes(Button):
    template = "djangocms_version_locking/toolbar/items/button_with_attributes.html"

    def __init__(self, name, url, active=False, disabled=False,
                 extra_classes=None, html_attributes=None):
        super().__init__(name, url, active, disabled, extra_classes)

        self.html_attributes = html_attributes

    def get_context(self):
        context = super().get_context()
        context['html_attributes'] = self.html_attributes
        return context

def new_edit_button(func):
    def inner(self, **kwargs):
        # Only run this if model is versioned and in content mode.
        if not self._is_versioned() or not self.toolbar.content_mode_active:
            return

        # Check whether current toolbar object has a version lock.
        if content_is_unlocked_for_user(self.toolbar.obj, self.request.user):
            # No version lock. Call original func to render edit button.
            func(self, **kwargs)
            return

        # Populate a title with the locked author details
        html_attributes = {}
        version_lock = get_lock_for_content(self.toolbar.obj)
        if version_lock:
            # If the users name is available use it, otherwise use their username
            html_attributes['title'] = _("Locked with {name}").format(
                name=version_lock.created_by.get_full_name() or version_lock.created_by.username,
            )

        # There is a version lock for the current object.
        # Add a disabled edit button.
        item_list = ButtonList(side=self.toolbar.RIGHT)
        button = ButtonWithAttributes(
            format_html(
                '{name}',
                name=_('Edit'),
            ),
            url='javascript:void(0)',
            disabled=True,
            extra_classes=['cms-btn-action', 'cms-version-locking-btn-icon'],
            html_attributes=html_attributes,
        )
        item_list.buttons.append(button)
        self.toolbar.add_item(item_list)
    return inner

VersioningToolbar._add_edit_button = new_edit_button(VersioningToolbar._add_edit_button)


import time
import requests
import sys
import json
from datetime import datetime
from colorama import Fore, Back, Style, init

coinNames = ["BTC-EUR", "ETH-EUR", "LTC-EUR", "BCH-EUR", "LINK-USD"]

USDTOEUR = 0.89

MAX_COINS = len(coinNames)

def loadCurrencies():
    with open("currencies.txt") as file:
        line = file.readline().strip()
        currencies = list(map(float, line.split(" ")))
        file.close()
        return currencies

    return [0, 0, 0, 0]

def parseFloat(num):
    return "{0:.4f}".format(num)

def parsePct(num):
    return "{0:.3f}".format(((num-1)*100))

def getPriceFromGDAX(coin):
    r = requests.get("https://api.gdax.com/products/"+coin+"/book?level=1", headers={"user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36"})
    json = r.json()
    value = (float(json["bids"][0][0]) + float(json["asks"][0][0])) / 2.0
    if coin.split("-")[1] == "USD":
        value *= USDTOEUR
    return value

def getStatsFromGDAX(coin):
    r = requests.get("https://api.gdax.com/products/"+coin+"/stats", headers={"user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36"})
    json = r.json()
    return json

def fillCoins(coins):
    for i in range(MAX_COINS):
        coins.append(float(getPriceFromGDAX(coinNames[i])))

def fillStats(stats):
    for i in range(MAX_COINS):
        stats.append(getStatsFromGDAX(coinNames[i]))

def fillPcts(coins, stats, pct):
    for i in range(MAX_COINS):
        pct.append(coins[i] / float(stats[i]["open"]))

def printStyle(pct):
    temp = ""
    if pct > 0.0:
        temp += Fore.GREEN + parseFloat(pct)
    elif pct < 0.0:
        temp += Fore.RED + parseFloat(pct)
    else:
        temp += parseFloat(pct)

    temp += "%" + Fore.WHITE
    return temp

min = []
max = []
for _ in enumerate(coinNames):
    min.append(99999999)
    max.append(0)

currencies = loadCurrencies()
init()
firstIter = True
while True:
    try:
        isMin = False
        isMax = False
        minIdx = 0
        maxIdx = 0
        file = open("data.txt", "a")
        coins = []
        fillCoins(coins)

        stats = []
        fillStats(stats)

        pct = []
        fillPcts(coins, stats, pct)

        for i in range(MAX_COINS):
            if coins[i] < min[i]:
                min[i] = coins[i]
                isMin = True
                minIdx |= 1 << i

            if coins[i] > max[i]:
                max[i] = coins[i]
                isMax = True
                maxIdx |= 1 << i

        line = ""
        total = 0.0
        for i in range(MAX_COINS):
            total += coins[i] * currencies[i]

        line += Fore.WHITE + datetime.now().strftime('%d-%m-%Y %H:%M:%S') + "\nTotal: " + parseFloat(total) + "€\n "
        for i in range(MAX_COINS):
            if currencies[i] == 0.0:
                continue

            line += coinNames[i] + ": " + parseFloat(coins[i]) + " => " + parseFloat(coins[i] * currencies[i]) + "€ (" + printStyle(pct[i] * 100 - 100) + ")"

            if firstIter == False:
                if maxIdx & i+1:
                    line += " MAX"
                if minIdx & i+1:
                    line += " MIN"

            line += "\n "
        firstIter = False
        #line = "BTC: " + parseFloat(coins[0]) + " => " + parseFloat(coins[0] * currencies[0]) + " (" + parsePct(pct[0])  + "%), ETH: " + parseFloat(coins[1])  + " (" + parsePct(pct[1])  + "%), LTC: " + parseFloat(coins[2]) + " (" + parsePct(pct[2])  + "%), BCH: " + parseFloat(coins[3]) + "(" + parsePct(pct[3])  + "%)"
        extra = ""
        print(line + Fore.WHITE, end="")
        #if isMax:
        #    extra += " MAX " + str(maxIdx)
        #if isMin:
        #    extra += " MIN " + str(minIdx)
        #print(Fore.WHITE + extra)
        outputLine = ""
        for idx, name in enumerate(coinNames):
            outputLine += str(parseFloat(coins[idx]))
            if idx != len(coinNames) - 1:
                outputLine += " "
        outputLine += "\n"
        file.write(outputLine)
        file.close()
        time.sleep(30)
    except json.JSONDecodeError as error:
        file = open("logs.txt", "a")
        file.write("JSONDecodeError error: " + error.msg + "\n" + error.doc + " AT POS: " + error.pos + ":" + ":" + "\n")
        file.close()
    except:
        file = open("logs.txt", "a")
        file.write("Unexpected error: " + str(sys.exc_info()[0]) + "\n")
        file.close()


"""
**Notes:**

A little less accurate than [ordinary least squares with MFC](3.html) but hard to tell if this is statistically significant.

Have a look at  [ridge regression](10.html) for some interesting developments.
"""
from sklearn import linear_model
import numpy as np
import matplotlib.pyplot as plt
import random
import utils
from bunch import Bunch
from math import floor

def train(training_data):
    # 70% of sqwaks for training, 30% for testing
    training_data_cutoff = int(floor(len(training_data) * .7))
    random.shuffle(training_data)

    x_data = []
    y_data = []

    sample_rate = 44100

    for i, sample in enumerate(training_data):
        # Preprocessing with mel-frequency cepstrum
        mfc_data = utils.mfc(np.asarray(sample["amplitudes"]), sample_rate)
        x_data.append(mfc_data)
        y_data.append(sample["rating"])

    reg = linear_model.BayesianRidge()
    reg.fit(x_data[:training_data_cutoff], y_data[:training_data_cutoff])

    # predict on the remaining 30%
    predicted = reg.predict(x_data[training_data_cutoff:])

    actual = y_data[training_data_cutoff:]

    return Bunch({
        "predicted": predicted,
        "actual": actual,
        "x_data_test": x_data[training_data_cutoff:],
        "y_data_test": y_data[training_data_cutoff:],
        "reg": reg
    })

def report():
    results = utils.load_data()
    trained_data = train(results)
    utils.generate_report(
        trained_data,
        original_data=results,
        title='Bayesian Regression MFC',
        experiment_id="12",
        experiment_type="regression",
        description=__doc__,
        train=train,
        processing_method="MFC",
        learning_alg="Bayesian Regression",
        num_iterations=10
    )


import json

def format_mapping_dicts(nodes):
    """
    Convert text nodes to integer mappings

    :param nodes:
    :return:
    """

    json_key = 'content'
    output_nodes = load_json("./training_info/nodes.json")[json_key]
    output_keywords = load_json("./training_info/keywords.json")[json_key]
    output_urls = load_json("./training_info/urls.json")[json_key]
    output_full_node_info = dict()

    for node in nodes:
        url = nodes[node]['url'].replace(" ", "")

        if node not in output_nodes:
            output_nodes.append(node)

        for keyword in nodes[node]['keywords']:
            k = keyword.lower().replace(" ", "")
            if k not in output_keywords:
                output_keywords.append(k)

        if url not in output_urls:
            output_urls.append(url)

        node_information_dict = {
            output_nodes.index(node): {
                    "url": output_urls.index(url),
                    "keywords": [output_keywords.index(keyword.lower().replace(" ", ""))
                                 for keyword in nodes[node]['keywords']]}
        }

        output_full_node_info.update(node_information_dict)

    save_json(path="./training_info/nodes.json", j={json_key: output_nodes})
    save_json(path="./training_info/keywords.json", j={json_key: output_keywords})
    save_json(path="./training_info/urls.json", j={json_key: output_urls})

    return output_full_node_info

def load_json(path):
    """

    :param path:
    :return:
    """

    with open(path, 'r') as F:
        j = json.load(F)
    return j

def save_json(path, j):
    with open(path, 'w') as F:
        F.write(json.dumps(j))

def run():
    nodes = load_json("./training_info/info.json")
    short_nodes = format_mapping_dicts(nodes=nodes)
    print(short_nodes)

if __name__ == '__main__':
    run()


from keras import Sequential
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.datasets import fashion_mnist
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import classification_report, confusion_matrix

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
x_train = x_train / 255
x_test = x_test / 255
print(x_train.shape, '\n')
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print(x_train.shape)
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), input_shape=x_train[0].shape))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(filters=64, kernel_size=(3, 3)))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(units=757, activation="relu"))
model.add(Dense(units=10, activation="softmax"))
print(model.summary())
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
trained_model = model.fit(x_train, y_train, verbose=1, epochs=3)
print(trained_model.history.keys())
plt.style.use("seaborn")
fig, ax = plt.subplots(figsize=(15, 9))
ax.plot(trained_model.history["loss"])
ax.plot(trained_model.history["accuracy"])
ax.legend(["Loss", "Accuracy"])
ax.set(title="LOSS VS ACCURACY GRAPH", xlabel="EPOCHS", ylabel="LOSS & ACCURACY")
plt.show()
y_pred = model.predict_classes(x_test)
cf = classification_report(y_test, y_pred)
print(cf)
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(15, 9))
sns.heatmap(cm, annot=True)
plt.show()
acs = model.evaluate(x_test, y_test)[1]
acs = acs * 100
print(acs)
plt.imshow(x_train[5].reshape(x_train[5].shape[0], x_train[5].shape[1]))
plt.title("Actual = {} Prediction = {}".format(y_test[5], y_pred[5]))
plt.show()


import enum

from tensorflow.python.autograph.pyct import anno
from tensorflow.python.autograph.pyct import ast_util
from tensorflow.python.autograph.pyct import parser
from tensorflow.python.autograph.pyct import templates
from tensorflow.python.autograph.pyct import transformer
from tensorflow.python.util.tf_export import tf_export

# TODO(mdan): These contexts can be refactored into first class objects.
# For example, we could define Program and Entity abstractions that hold on
# to the actual entity and have conversion methods.

# TODO(mdan): Add a test specific to this converter.

@tf_export('autograph.experimental.Feature')
class Feature(enum.Enum):
  """This enumeration represents optional conversion options.

  These conversion options are experimental. They are subject to change without
  notice and offer no guarantees.

  _Example Usage_

  ```python
  optionals= tf.autograph.experimental.Feature.EQUALITY_OPERATORS
  @tf.function(experimental_autograph_options=optionals)
  def f(i):
    if i == 0:  # EQUALITY_OPERATORS allows the use of == here.
      tf.print('i is zero')
  ```

  Attributes:
    ALL: Enable all features.
    AUTO_CONTROL_DEPS: Insert of control dependencies in the generated code.
    ASSERT_STATEMENTS: Convert Tensor-dependent assert statements to tf.Assert.
    BUILTIN_FUNCTIONS: Convert builtin functions applied to Tensors to
      their TF counterparts.
    EQUALITY_OPERATORS: Whether to convert the equality operator ('==') to
      tf.math.equal.
    LISTS: Convert list idioms, like initializers, slices, append, etc.
    NAME_SCOPES: Insert name scopes that name ops according to context, like the
      function they were defined in.
  """

  ALL = 'ALL'

  AUTO_CONTROL_DEPS = 'AUTO_CONTROL_DEPS'
  ASSERT_STATEMENTS = 'ASSERT_STATEMENTS'
  BUILTIN_FUNCTIONS = 'BUILTIN_FUNCTIONS'
  EQUALITY_OPERATORS = 'EQUALITY_OPERATORS'
  LISTS = 'LISTS'
  NAME_SCOPES = 'NAME_SCOPES'

  @classmethod
  def all(cls):
    """Returns a tuple that enables all options."""
    return tuple(cls.__members__.values())

  @classmethod
  def all_but(cls, exclude):
    """Returns a tuple that enables all but the excluded options."""
    if not isinstance(exclude, (list, tuple, set)):
      exclude = (exclude,)
    return tuple(set(cls.all()) - set(exclude) - {cls.ALL})

STANDARD_OPTIONS = None  # Forward definition.

class ConversionOptions(object):
  """Immutable container for global conversion flags.

  Attributes:
    recursive: bool, whether to recursively convert any user functions or
      classes that the converted function may use.
    user_requested: bool, whether the conversion was explicitly requested by
      the user, as opposed to being performed as a result of other logic. This
      value always auto-resets to False in child conversions.
    optional_features: Union[Feature, Set[Feature]], controls the use of
      optional features in the conversion process. See Feature for available
      options.
  """

  def __init__(self,
               recursive=False,
               user_requested=False,
               internal_convert_user_code=True,
               optional_features=Feature.ALL):
    self.recursive = recursive
    self.user_requested = user_requested
    # TODO(mdan): Rename to conversion_recursion_depth?
    self.internal_convert_user_code = internal_convert_user_code

    if optional_features is None:
      optional_features = ()
    elif isinstance(optional_features, Feature):
      optional_features = (optional_features,)
    optional_features = frozenset(optional_features)
    self.optional_features = optional_features

  def as_tuple(self):
    return (self.recursive, self.user_requested,
            self.internal_convert_user_code, self.optional_features)

  def __hash__(self):
    return hash(self.as_tuple())

  def __eq__(self, other):
    assert isinstance(other, ConversionOptions)
    return self.as_tuple() == other.as_tuple()

  def __str__(self):
    return 'ConversionOptions[{}]'

  def uses(self, feature):
    return (Feature.ALL in self.optional_features or
            feature in self.optional_features)

  def call_options(self):
    """Returns the corresponding options to be used for recursive conversion."""
    return ConversionOptions(
        recursive=self.recursive,
        user_requested=False,
        internal_convert_user_code=self.recursive,
        optional_features=self.optional_features)

  def to_ast(self):
    """Returns a representation of this object as an AST node.

    The AST node encodes a constructor that would create an object with the
    same contents.

    Returns:
      ast.Node
    """
    if self == STANDARD_OPTIONS:
      return parser.parse_expression('ag__.STD')

    template = """
      ag__.ConversionOptions(
          recursive=recursive_val,
          user_requested=user_requested_val,
          optional_features=optional_features_val,
          internal_convert_user_code=internal_convert_user_code_val)
    """

    def list_of_features(values):
      return parser.parse_expression('({})'.format(', '.join(
          'ag__.{}'.format(str(v)) for v in values)))

    expr_ast = templates.replace(
        template,
        recursive_val=parser.parse_expression(str(self.recursive)),
        user_requested_val=parser.parse_expression(str(self.user_requested)),
        internal_convert_user_code_val=parser.parse_expression(
            str(self.internal_convert_user_code)),
        optional_features_val=list_of_features(self.optional_features))
    return expr_ast[0].value

STANDARD_OPTIONS = ConversionOptions(
    recursive=True,
    user_requested=False,
    internal_convert_user_code=True,
    optional_features=None)

class ProgramContext(object):
  """ProgramContext keeps track of converting function hierarchies.

  Attributes:
    options: ConversionOptions
    autograph_module: Deprecated. Do not use.
  """

  def __init__(self, options, autograph_module=None):
    self.options = options
    self.autograph_module = autograph_module

class Base(transformer.Base):
  """All converters should inherit from this class.

  Attributes:
    ctx: EntityContext
  """

  def __init__(self, ctx):
    super(Base, self).__init__(ctx)

    self._used = False
    self._ast_depth = 0

  def get_definition_directive(self, node, directive, arg, default):
    """Returns the unique directive argument for a symbol.

    See lang/directives.py for details on directives.

    Example:
       # Given a directive in the code:
       ag.foo_directive(bar, baz=1)

       # One can write for an AST node Name(id='bar'):
       get_definition_directive(node, ag.foo_directive, 'baz')

    Args:
      node: ast.AST, the node representing the symbol for which the directive
        argument is needed.
      directive: Callable[..., Any], the directive to search.
      arg: str, the directive argument to return.
      default: Any

    Raises:
      ValueError: if conflicting annotations have been found
    """
    defs = anno.getanno(node, anno.Static.ORIG_DEFINITIONS, ())
    if not defs:
      return default

    arg_values_found = []
    for def_ in defs:
      if (directive in def_.directives and arg in def_.directives[directive]):
        arg_values_found.append(def_.directives[directive][arg])

    if not arg_values_found:
      return default

    if len(arg_values_found) == 1:
      return arg_values_found[0]

    # If multiple annotations reach the symbol, they must all match. If they do,
    # return any of them.
    first_value = arg_values_found[0]
    for other_value in arg_values_found[1:]:
      if not ast_util.matches(first_value, other_value):
        qn = anno.getanno(node, anno.Basic.QN)
        raise ValueError(
            '%s has ambiguous annotations for %s(%s): %s, %s' %
            (qn, directive.__name__, arg, parser.unparse(other_value).strip(),
             parser.unparse(first_value).strip()))
    return first_value

  def visit(self, node):
    if not self._ast_depth:
      if self._used:
        raise ValueError('converter objects cannot be reused')
      self._used = True

    self._ast_depth += 1
    try:
      return super(Base, self).visit(node)
    finally:
      self._ast_depth -= 1


#usage: python3 dnslook.py <domain or ip address>
#python3.x
import sys
import dns.resolver
import dns.reversename
import argparse
import ipaddress

parser = argparse.ArgumentParser()
parser.add_argument("destination")
args = parser.parse_args()
dest = args.destination
dnsrecords = ["A", "AAAA", "MX", "NS", "SOA", "TXT"]

#function to check if user supplied a name or an IP
def check(dest):
    try:
        if ipaddress.ip_address(dest):
            revlook(dest)
        else:
         dnslook(dest)
    except:
        dnslook(dest)

#function to perform reverse lookup (if we think an IP was supplied)
def revlook(dest):
    revdest = dns.reversename.from_address(dest)
    try:
        result = dns.resolver.resolve(revdest, "PTR")
        for val in result:
            print("PTR:", val.to_text())
    except dns.resolver.NoAnswer:
        print("PTR: No record found")
    except dns.resolver.NXDOMAIN:
        print("PTR: NXDOMAIN response")

#function to perform a forward lookup (if we think a name was supplied)
def dnslook(dest):
        for record in dnsrecords:
                try:
                        result = dns.resolver.resolve(dest, record)
                        for val in result:
                                print(record,":", val.to_text())
                except dns.resolver.NoAnswer:
                        print(record,": No record found")
                except dns.resolver.NXDOMAIN:
                        print(record,": NXDOMAIN response received")
                except dns.exception.Timeout:
                        print(record,": The request timed out!")

check(dest)


from collections import namedtuple
from inflection import camelize, singularize, pluralize, underscore

from rest_framework.relations import PrimaryKeyRelatedField, ManyRelatedField
from rest_framework.serializers import ListSerializer
from rest_framework.utils.serializer_helpers import BoundField

from ember_drf.serializers import SideloadListSerializer, SideloadSerializer

def convert_to_ember_json(data):
    """Convert all dictionary keys to camel case."""
    if isinstance(data, dict):
        camel_dict = {}
        for key, value in data.items():
            camel_dict[camelize(key, False)] = convert_to_ember_json(value)
        return camel_dict
    if isinstance(data, (list, tuple)):
        return [convert_to_ember_json(i) for i in data]
    return data

def convert_from_ember_json(data):
    """Convert all dictionary keys to snake_case."""
    if isinstance(data, dict):
        snake_dict = {}
        for key, value in data.items():
            snake_dict[underscore(key)] = convert_from_ember_json(value)
        return snake_dict
    if isinstance(data, (list, tuple)):
        return [convert_from_ember_json(i) for i in data]
    return data

RelatedFieldRename = namedtuple('RelatedFieldRename', ['index', 'new_name'])

def find_related_fields_to_rename(fields, prefix=[]):
    """
    Find all related fields on a Serializer or NestedBoundField.
    Args:
        fields: an object whose `.__iter__()` method returns instances of
            `BoundField` (e.g. a `Serializer` or `NestedBoundField`)
        prefix: a list of parent keys that map to the current list of
            fields.  (e.g. `{'one': {'two': {'three': 'value'}}}` will have
                `prefix=['one', 'two'] when `{'three': 'value'})` is passed
                into the function.
    Returns:
        list: list of RelatedFieldRename instances.
    """
    ret = []
    if isinstance(fields, ListSerializer):
        fields = fields.child.__class__()

    # this is selfish hack to support something I am doing
    # hopefully I can factor this out soon
    if isinstance(fields, BoundField):
        fields = fields._field

    # iterate over each field to determine if it is related
    for field in fields:
        assert isinstance(field, BoundField), (
            'Fields must be an iterator that returns `BoundField` instances.'
        )
        key = prefix + field.name.split('.')
        # iterate over any nested lists
        if field._proxy_class is ListSerializer:
            ret.extend(
                find_related_fields_to_rename(field.child, prefix=key)
            )
        # any nested dicts should be iterated over
        # this could check if `field` is an instance of `NestedBoundField`
        # but that would break my hack above
        elif hasattr(field, 'fields'):
            ret.extend(find_related_fields_to_rename(field, prefix=key))
        # the field is a list of pks and should be renamed
        elif field._proxy_class is ManyRelatedField:
            new_name = singularize(key[-1]) + '_ids'
            ret.append(RelatedFieldRename(key, new_name))
        # the field is a single pk and should be renamed
        elif field._proxy_class is PrimaryKeyRelatedField:
            new_name = key[-1] + '_id'
            ret.append(RelatedFieldRename(key, new_name))
    return ret

def rename_related_fields(data, fields):
    """
    Rename related fields to ActiveModel json style.

    Args:
        data (dict or list): object to be traversed and renamed.
        fields (list): list of `RelatedFieldRename` instances.
    Returns:
        dict or list: with all related keys in `fields` renamed as specified.
    """

    root_fields = [(f.index[0], f.new_name) for f in fields if len(f.index) == 1]
    nested_fields = [f for f in fields if len(f.index) > 1]

    if isinstance(data, dict):
        new_dict = {}
        for key, value in data.items():
            found_match = False

            # check if any immediate children fields are related fields
            for field, new_name in root_fields:
                if key == field:
                    new_dict[new_name] = value
                    found_match = True
                    break
            if found_match:
                continue

            # check if any nested fields contain related fields
            for field in nested_fields:
                if key == field.index[0]:
                    new_fields = [
                        RelatedFieldRename(field.index[1:], field.new_name)
                        for field in nested_fields
                    ]
                    new_dict[key] = rename_related_fields(value, new_fields)
                    found_match = True
                    break

            # copy the existing key/value pair for non related fields
            if not found_match:
                new_dict[key] = value
        return new_dict
    elif isinstance(data, list):
        return [rename_related_fields(i, fields) for i in data]

def convert_to_active_model_json(data):
    try:
        serializer = data.serializer
    except AttributeError:
        # do nothing if we are not given a ReturnDict with `.serializer` set.
        return data
    related_fields = []
    if isinstance(serializer, (SideloadSerializer, SideloadListSerializer)):
        for key, value in data.items():
            assert hasattr(value, 'serializer'), (
                'Each root key must nest a `ReturnDict` or `ReturnList` with '
                '`.serializer` set.'
            )
            related_fields.extend(
                find_related_fields_to_rename(value.serializer, prefix=[key])
            )
    return rename_related_fields(data, related_fields)

def remove_id_suffixes(string):
    if string[-3:] == '_id':
        return string[:-3]
    elif string[-4:] == '_ids':
        return pluralize(string[:-4])
    return string

def convert_from_active_model_json(data):
    if isinstance(data, list):
        return [convert_from_active_model_json(item) for item in data]
    elif isinstance(data, dict):
        new_dict = {}
        for key, value in data.items():
            new_dict[remove_id_suffixes(key)] = \
                convert_from_active_model_json(value)
        return new_dict
    return data


#User function Template for python3

class Solution:
    def addMinChar (self, str1):
        # code here
        n = len(str1)
        i,j,ans = 0,n-1,n-1

        while i<=j:
            if str1[i]==str1[j]:
                i+=1
                j-=1
            else:
                ans-=1
                i=0
                j = ans

        return n-ans-1

#{
 # Driver Code Starts
#Initial Template for Python 3

if __name__ == '__main__':
    t = int (input ())
    for _ in range (t):
        str1 = input()

        ob = Solution()
        print(ob.addMinChar(str1))

# } Driver Code Ends

Add Minimum Characters - GFG/add-minimum-characters.py


#CECI est un thread qui va tourner de manière indéfénie

import serial

import time

def count_words(a):
    ser = serial.Serial('/dev/ttyUSB0')
    while(True):
        print('TWSS')
        time.sleep(5)

        u=ser.readline().decode()
        print(u)

        time.sleep(2)

    return u


from core.registry.factory import create_model #
from core.registry.register import register_model

create_retrieval = create_model
register_retrieval = register_model

def _cfg(url='', drive='', out_dim=1024, **kwargs):
    return {
        'url': url,
        'drive': drive,
        'reduction': False,
        'input_size': (3, 1024, 1024),
        'out_dim': out_dim,
        **kwargs
    }


import discord
import json
import sys

class rolepinger():

    async def pinger(self, message):
        # Checks for Admin
        with open('settings.json') as json_file:
            settingsFile = json.load(json_file)
        if settingsFile["adminRoleId"] in str(message.author.roles):
            # Makes the Pings role mentionable
            await (discord.utils.get(message.guild.roles, name='Pings')).edit(mentionable=True)
            # Sends ping
            await message.channel.send(f'<@&{settingsFile["notificationsRole"]}>')
            await message.channel.send(f'*To unsubscribe from pings, type >pings in <#{settingsFile["botChannel"]}>*')
            # Makes the Pings role unmentionable
            await (discord.utils.get(message.guild.roles, name='Pings')).edit(mentionable=False)
        else:
            await message.channel.send("You must have the `Administrator` role to do this...")


#!/usr/bin/env python
# Karan Nayan
# Initial Date: July 11, 2013
# Updated:      July 11, 2013
# Updated:      Oct  28, 2016  Shoban
#
# These files have been made available online through a Creative Commons Attribution-ShareAlike 3.0  license.
# (http://creativecommons.org/licenses/by-sa/3.0/)
#
# This is an example of controlling the rotation of motors using encoders
# Connect the LEGO Motors to Motor ports MA and MB.
#
# You can learn more about BrickPi here:  http://www.dexterindustries.com/BrickPi
# Have a question about this example?  Ask on the forums here:  http://forum.dexterindustries.com/c/brickpi

#For the code to work - sudo pip install -U future

from __future__ import print_function
from __future__ import division
from builtins import input

# the above lines are meant for Python3 compatibility.
# they force the use of Python3 functionality for print(),
# the integer division and input()
# mind your parentheses!

from BrickPi import *   #import BrickPi.py file to use BrickPi operations

BrickPiSetup()  # setup the serial port for sudo su communication
BrickPiSetupSensors()       #Send the properties of sensors to BrickPi
BrickPiUpdateValues()

"""
Pass the arguments in a list.
If a single motor has to be controlled then the arguments should be passed like
elements of an array,e.g, motorRotateDegree([255],[360],[PORT_A]) where power=255 and
angle=360 for the motor connected at Port A
"""
power=[255]
deg=[360]
port=[PORT_A]
motorRotateDegree(power,deg,port)		#This read the encoder values every 100 ms (default). Not that accurate but not very processor intensive
#motorRotateDegree(power,deg,port,.05)	#This read the encoder values every 50 ms. A little more accurate
#motorRotateDegree(power,deg,port,0)	#This read the encoder values without any delay. Most accurate but take a lot of processing power
#motorRotateDegree(power,deg,port,0,.04)	#This read the encoder values without any delay. The time to rotate the motors in the opposite direction when stopping is specified in the last argument. Useful when free running the motors

"""
If multiple motors have to be controlled then the parameters for running each motor must be passed
as the elements of an array,e.g, motorRotateDegree([255,100],[360,30],[PORT_A,PORT_B]) where
power=255 and angle=30 are for motor at PORT_A and power=100 and angle=30 are for motor at PORT_B.
It can be used similarly for any number of motors.
"""
time.sleep(1)
power=[255,30]
deg=[360,-180]
port=[PORT_A,PORT_B]
motorRotateDegree(power,deg,port)


#!/usr/bin/env python
#
# Author: Harrison Chapman
# This file will hold parent classes of types of models

from __future__ import with_statement

# GAE Imports
from google.appengine.api import memcache
from google.appengine.ext import db, ndb
from google.appengine.ext import blobstore
from google.appengine.api import files

# Local module imports
from passwd_crypto import hash_password, check_password

# Global python imports
import logging
import datetime
import logging
import itertools
from functools import wraps

import re
from unicodedata import normalize

_punct_re = re.compile(r'[!"#$%&\'()*/<=>?@\[\\\]^`{|},.]+')
_space_re = re.compile(r'[\t \-_]')

def slugify(text, delim=u'-'):
  """Generates an slightly worse ASCII-only slug."""
  result = []
  text = unicode(text)
  for word in _space_re.split(_punct_re.sub("", text.lower())):
    word = normalize('NFKD', word).encode('ascii', 'ignore')
    if word:
      result.append(word)
  return unicode(delim.join(result))

class ModelError(Exception):
  pass

class QueryError(ModelError):
  pass

class NoSuchEntry(QueryError):
  pass

def quantummethod(f):
  '''
  Class method decorator specific to the instance.

  It uses a descriptor to delay the definition of the
  method wrapper.
  '''
  class descript(object):
    def __init__(self, f):
      self.f = f

    def __get__(self, instance, klass):
      if instance is None:
        # Class method was requested
        return self.make_unbound(klass)
      return self.make_bound(instance)

    def make_unbound(self, klass):
      @wraps(self.f)
      def wrapper(*args, **kwargs):
        pass
      return wrapper

    def make_bound(self, instance):
      @wraps(self.f)
      def wrapper(*args, **kwargs):
        return self.f(instance, *args, **kwargs)
      # This instance does not need the descriptor anymore,
      # let it find the wrapper directly next time:
      #setattr(instance, self.f.__name__, wrapper)
      return wrapper

  return descript(f)

def is_key(obj):
  return (isinstance(obj, ndb.Key) or
          isinstance(obj, str) or
          isinstance(obj, unicode))

def as_key(obj):
  if isinstance(obj, str) or isinstance(obj, unicode):
    return ndb.Key(urlsafe=obj)
  if isinstance(obj, ndb.Key):
    return obj
  if isinstance(obj, ndb.Model):
    return obj.key
  if isinstance(obj, CachedModel):
    return obj.key
  return None

def as_keys(key_list):
  return filter(None, [as_key(key) for key in key_list])

class CacheItem(object):
  def __init__(self, cachekey):
    self._key = cachekey

  @classmethod
  def fetch(self, cachekey):
    pass
  def save(self, data):
    CachedModel.cache_set(data, self._key)

class CountTableCache(CacheItem):
  ''' Represents a table of keys/values in cache '''
  def __init__(self, cachekey, table=dict()):
    super(CountTableCache, self).__init__(cachekey)

class QueryCache(CacheItem):
  ''' An object representing a list of keys in the datastore
  pertaining to a query called commonly and worthy of caching.

  _dblen represents the most recent guesstimate of the (minimum)
  number of keys that an actual query would return'''
  def __init__(self, cachekey, data=None, cursor=None, more=True, keylen=0):
    if data is None: data = []
    super(QueryCache, self).__init__(cachekey)
    self.set(data=data, cursor=cursor, more=more, keylen=keylen)

  @classmethod
  def fetch(cls, cachekey):
    result = CachedModel.cache_get(cachekey)

    if result is None:
      return cls(cachekey)
    return cls(cachekey, **result)

  def save(self):
    logging.error(self._data)
    logging.error(self._cursor)
    logging.error(self._more)
    super(QueryCache, self).save({'data': self._data,
                                  'cursor': self._cursor,
                                  'more': self._more})

  def insert(self, idx, key):
    self._data.insert(idx, key)

  def append(self, key):
    self._data.append(key)

  def extend(self, keys):
    self._data.extend(keys)

  def prepend(self, key):
    self.insert(0, key)

  def __len__(self):
    return len(self._data)

  def set(self, data, cursor=None, more=True, keylen=0):
    ''' Set the data for this QueryCache with real results from
    a datastore query'''
    self._data = data
    self._cursor = cursor
    self._more = more

  def extend_by(self, data, cursor=None, more=True, keylen=0):
    ''' Extend the data for this QueryCache with real results from
    a datastore query'''
    self._data.extend(data)
    self._cursor = cursor
    self._more = more

  def need_fetch(self, num):
    ''' Determines if we need a new fetch from the datastore
    We need a new fetch if:
      a. We have fewer keys than the desired number AND
      b. We don't have maximal cache'''
    return (num > len(self) and self._more) or len(self) <= 0

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

  @property
  def results(self):
    return self._data

  @property
  def cursor(self):
    return self._cursor
  @cursor.setter
  def cursor(self, cursor):
    self._cursor = cursor

  @property
  def more(self):
    return self._more
  @more.setter
  def more(self, more):
    self._more = more

class SortedQueryCache(QueryCache):
  def __init__(self, cachekey, data=None, cursor=None, more=True, keylen=0,
               sort_fn=None):
    if data is None: data = []
    super(SortedQueryCache, self).__init__(cachekey, data=data, cursor=cursor,
                                           more=more, keylen=keylen)

  def ordered_unique_insert(self, new_key, new_val):
    """Run through data until we find where to add the new key. Don't
    add the key if no spot is found"""
    for i, (key, val) in enumerate(self._data):
      if new_val >= val:
        if key != new_key:
          self._data.insert(i, (new_key, new_val))
        break

  @property
  def results(self):
    if self._data:
      return zip(*self._data)[0]
    return []

class CachedModel(object):
  _RAW = None
  ''' A CachedModel is a database model which tries its best to keep
  its information synced in the memcache so that fewer database hits
  are used.
  '''

  UNCACHED_READ = "@@@noncached_count"

  LOG_SET_DEBUG = "Memcache set (%s, %s) required"
  LOG_SET_FAIL = "Memcache set(%s, %s) failed"
  LOG_DEL_ERROR = "Memcache delete(%s) failed with error code %s"

  @property
  def key(self):
    if self._dbentry:
      return self._dbentry.key
    elif self._dbkey:
      return self._dbkey
    else:
      return None

  @property
  def raw(self):
    if self._dbentry is None and self._dbkey is not None:
      self._dbentry = self._dbkey.get()
    return self._dbentry

  @classmethod
  def as_object(cls, thing):
    """ Tries really, really hard to make thing into an instance of cls. """
    if isinstance(thing, cls): return thing
    if isinstance(thing, cls._RAW): return cls(raw=thing)
    if isinstance(thing, str) or isinstance(thing, unicode):
      thing = ndb.Key(urlsafe=thing)
    if isinstance(thing, ndb.Key): return cls.get(thing)
    return None

  def __init__(self, raw=None, raw_key=None, parent=None, cached=True,
               **kwargs):
    self._cached = cached
    self._dbentry = None

    if raw is not None:
      if not isinstance(raw, self._RAW):
        raise Exception(
          "Passed raw object must be of type %s or a child. "
          "Raw object was of kind %s."%
          (self._RAWKIND, raw.__class__.__name__))
      self._dbentry = raw

    elif raw_key is not None:
      if not isinstance(raw_key, ndb.Key):
        raise Exception("raw_key must be of type ndb.Key")
      if raw_key.kind() != self._RAWKIND:
        raise Exception(
          "raw_key must point to an entry of type %s"%self._RAWKIND)
      self._dbkey = raw_key
      self._dbentry = None

    else:
      self._dbentry = self._RAW(parent=parent, **kwargs)

  @classmethod
  def cache_set(cls, value, cache_key, *args):
    '''
    Classmethod to access memcache.set

    Returns the cached value, for chaining purposes.
    '''
    logging.debug(cls.LOG_SET_DEBUG %(cache_key %args, value))
    if not memcache.set(cache_key %args, value):
      logging.error(cls.LOG_SET_FAIL % (cache_key %args, value))
    return value

  @classmethod
  def cache_delete(cls, cache_key, *args):
    '''
    Classmethod to access memcache.delete
    '''
    response = memcache.delete(cache_key %args)
    if response < 2:
      logging.error(cls.LOG_DEL_ERROR %(cache_key %args, response))

  @classmethod
  def cache_get(cls, cache_key, *args):
    '''
    Classmethod to access memcache.get
    '''
    result = memcache.get(cache_key %args)
    return result

  def add_to_cache(self):
    '''
    Populate the memcache with self. The base only stores the object
    associated to its key, but children may wish to also cache queries
    e.g. username.

    For chaining purposes, returns self
    '''
    return self

  def purge_from_cache(self):
    '''
    Remove self from the memcache where applicable. The opposite of
    "add_to_cache".
    '''
    return self

  @classmethod
  def get(cls, keys, use_datastore=True, one_key=False):
    '''
    Get the data for (a) model(s) by its key(s) using the cache first,
    and then possibly datastore if necessary
    '''
    # TODO: Don't idiot-proof this, make rest of code more observant
    if keys is None:
      return None
    if isinstance(keys, cls):
      return keys
    elif cls._RAW is not None and isinstance(keys, cls._RAW):
      return cls(raw=keys)

    if is_key(keys) or one_key:
      logging.error(keys.get())
      return cls(raw=keys.get())

    raw_objs = filter(None, ndb.get_multi(keys))
    rslt = [cls(raw=raw) for raw in raw_objs]
    return rslt

  @classmethod
  def get_by_index(cls, index, *args, **kwargs):
    keys_only = kwargs.get("keys_only") or False
    cached = cls.cache_get(index, *args)

    if cached is not None:
      if keys_only:
        return cached
      objs = cls.get(cached)
      if objs is not None:
        return objs

    return None

  @classmethod
  def get_cached_query(cls, index, *args, **kwargs):
    return QueryCache.fetch(index % args)

  def put(self):
    '''
    Update datastore with self, and then update memcache for self.
    '''
    if self._dbentry:
      ret_val = self._dbentry.put()
      if ret_val:
        self.add_to_cache()
      return ret_val

    raise Exception("Cannot put() nonexistant raw database model")

  def delete(self):
    '''
    Remove self from memcache, then from datastore.
    '''
    self.purge_from_cache()
    self.key.delete()

  @classmethod
  def delete_key(cls, key):
    cls(raw_key=key).delete()

class ApiModel(CachedModel):
  def to_json():
    pass


class Order:
    def __init__(self, order_id, user, items, promotion):
        self.order_id = order_id
        self.user = user
        self.items = items
        self.promotion = promotion
        if promotion.limit is not None:
            self.promotion.limit.check(self)

        self._calc_item_total()
        self._calc_adjustment_total()
        self.payment_total = self.item_total - self.discount_total

    def _calc_item_total(self):
        self.item_total = 0
        for p in self.items:
            self.item_total += p.price

    def _calc_adjustment_total(self):
        self.discount_total = 0
        if self.promotion.rule.is_eligible(self) and (
                self.promotion.limit is None or self.promotion.limit.is_eligible(self)):
            self.computed_discount = self.promotion.action.perform(self)
            if self.promotion.limit is not None:
                self.promotion.limit.perform(self)


import subprocess
from pathlib import Path
from abc import ABC, abstractmethod

from colorama import Style, Fore

import settings
from common.media import AudioFile, VideoFile

class VoiceOver(AudioFile):
    _preprocessed: bool

    def __init__(self, path: Path, autopreprocess=True):
        super(VoiceOver, self).__init__(path)
        self._preprocessed = False
        if autopreprocess:
            self.preprocess()

    @property
    def _preprocessed_filename(self) -> str:
        return self.name if self.is_preprocessed else f'{self.name}_preprocessed{self.EXTENSION_M4A}'

    @property
    def _preprocessed_path(self) -> Path:
        return self.path if self.is_preprocessed else settings.TEMP_PATH / self._preprocessed_filename

    @property
    def is_preprocessed(self):
        return self._preprocessed

    def preprocess(self):
        if not self.is_preprocessed:
            print('\nПодготавливаем закадровый перевод...')
            subprocess.call(['ffmpeg',
                             '-i', self.path,
                             '-ar', '44100',
                             '-ac', '2',
                             '-ab', settings.TRANSLATOR_OUTPUT_AUDIO_BITRATE_IN_VIDEOFILE,
                             '-af', 'volume=' + str(settings.TRANSLATOR_VOLUME_BOOST),
                             self._preprocessed_path],
                            stdout=subprocess.DEVNULL,
                            stderr=subprocess.STDOUT)
            self._replace_path_and_remove_old_file(self._preprocessed_path)
            self._preprocessed = True
            print(f'{Style.DIM}{Fore.LIGHTGREEN_EX}ГОТОВО{Style.RESET_ALL}')
        else:
            print('\nЗакадровый перевод уже подготовлен, дополнительная обработка не требуется')

class TranslatableMediaFile(ABC):
    """Media file to which you can add voiceover"""

    @abstractmethod
    def add_voiceover(self, voiceover: VoiceOver) -> bool:
        """Adds voice-over translation"""

class TranslatableAudioFile(AudioFile, TranslatableMediaFile):
    """Audio file to which you can add voiceover"""
    _preprocessed: bool

    def __init__(self, path: Path, autopreprocess: bool = True):
        super(TranslatableAudioFile, self).__init__(path)
        self._preprocessed = False
        if not autopreprocess:
            self.preprocess()

    @property
    def is_preprocessed(self):
        return self._preprocessed

    @property
    def _preprocessed_filename(self) -> str:
        return self.name if self.is_preprocessed else f'{self.name}_preprocessed{self.EXTENSION_M4A}'

    @property
    def _preprocessed_path(self) -> Path:
        return self.path if self.is_preprocessed else settings.TEMP_PATH / self._preprocessed_filename

    def preprocess(self):
        if not self.is_preprocessed:
            print('Подготавливаем аудио...')
            subprocess.call(['ffmpeg',
                             '-i', self.path,
                             '-af', 'volume=' + str(settings.TRANSLATOR_VOLUME_ORIGINAL_AUDIO),
                             self._preprocessed_path],
                            stdout=subprocess.DEVNULL,
                            stderr=subprocess.STDOUT)
            self._replace_path_and_remove_old_file(self._preprocessed_path)
            self._preprocessed = True
            print(f'{Style.DIM}{Fore.LIGHTGREEN_EX}ГОТОВО{Style.RESET_ALL}')

        else:
            print('\nАудио уже по подготовлено к переводу, дополнительная обработка не требуется')

    def add_voiceover(self, voiceover: VoiceOver) -> bool:
        print('\nНакладываем закадровый перевод...')
        output_file_path = self._output_file_path
        subprocess.call(['ffmpeg',
                         '-i', voiceover.path,
                         '-i', self.path,
                         '-filter_complex', 'amix=inputs=2:duration=first',
                         '-ab', settings.TRANSLATOR_OUTPUT_AUDIO_BITRATE_IN_AUDIOFILE,
                         output_file_path],
                        stdout=subprocess.DEVNULL,
                        stderr=subprocess.STDOUT
                        )
        self.remove()
        output_file_path.rename(self.path)
        print(f'{Style.DIM}{Fore.LIGHTGREEN_EX}ГОТОВО{Style.RESET_ALL}')
        return True

class TranslatableAudioTrackForVideoFile(TranslatableAudioFile):
    """Audio track file for video to which you can add voiceover"""

    def add_voiceover(self, voiceover: VoiceOver) -> bool:
        print('\nНакладываем закадровый перевод...')
        output_file_path = self._output_file_path
        subprocess.call(['ffmpeg',
                         '-i', voiceover.path,
                         '-i', self.path,
                         '-filter_complex', 'amix=inputs=2:duration=first',
                         '-ab', settings.TRANSLATOR_OUTPUT_AUDIO_BITRATE_IN_VIDEOFILE,
                         output_file_path],
                        stdout=subprocess.DEVNULL,
                        stderr=subprocess.STDOUT
                        )
        self.remove()
        output_file_path.rename(self.path)
        print(f'{Style.DIM}{Fore.LIGHTGREEN_EX}ГОТОВО{Style.RESET_ALL}')
        return True

class TranslatableVideoFile(VideoFile, TranslatableMediaFile):
    """Video file to which you can add voiceover"""

    def extract_audio(self) -> TranslatableAudioTrackForVideoFile:
        return TranslatableAudioTrackForVideoFile(super(TranslatableVideoFile, self).extract_audio().path)

    def add_voiceover(self, voiceover: VoiceOver) -> bool:
        audio = self.extract_audio()
        audio.add_voiceover(voiceover)
        self.replace_audio(audio)
        audio.remove()
        voiceover.remove()
        return True

def convert_to_translatable_mediafile(mediafile: VideoFile | AudioFile) -> TranslatableAudioFile | TranslatableVideoFile | None:
    """Converts a media object to translatable"""
    file_types = {
        # audio
        TranslatableAudioFile.EXTENSION_MP3: TranslatableAudioFile,
        TranslatableAudioFile.EXTENSION_AAC: TranslatableAudioFile,
        TranslatableAudioFile.EXTENSION_M4A: TranslatableAudioFile,
        TranslatableAudioFile.EXTENSION_OGG: TranslatableAudioFile,
        TranslatableAudioFile.EXTENSION_WAV: TranslatableAudioFile,
        # video
        TranslatableVideoFile.EXTENSION_MP4: TranslatableVideoFile,
        TranslatableVideoFile.EXTENSION_MKV: TranslatableVideoFile,
        TranslatableVideoFile.EXTENSION_AVI: TranslatableVideoFile,
        TranslatableVideoFile.EXTENSION_FLV: TranslatableVideoFile,
        TranslatableVideoFile.EXTENSION_WEBM: TranslatableVideoFile,
    }

    file_type = file_types.get(mediafile.extension)
    return file_type(mediafile.path) if file_type else None


import requests
from datetime import datetime

class Conversor:
    url = "http://economia.awesomeap.com.br/json/last/USD-BRL"

    def __init__(self,valor) -> None:
        self.__valor = valor
        self.__data = datetime.now()

    @staticmethod
    def verificar_disponibilidade():
        try:
            cotacao = requests.get(Conversor.url).status_code
            if cotacao == 200:
                return True
            return False
        except:
            print("Ocorreu um erro ao acessar")
            return False

    def obter_valor_dolar(self) -> None:
        cotacao = requests.get(self.__url).json()
        dolar  = float(cotacao['USDBRL']['bid'])
        print(f"Valor atual do dólar R$ {dolar:.2f}")
        print("Data atual: ", self.__data)

print(Conversor.verificar_disponibilidade())


import cv2
import picamera
import picamera.array
import time

with picamera.PiCamera() as camera:
    with picamera.array.PiRGBArray(camera) as stream:
        while True:
            camera.capture(stream, format='bgr')
            image = stream.array
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            cv2.imshow('frame', gray)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            # reset the stream before the next capture
            stream.seek(0)
            stream.truncate()
        cv2.destroyAllWindows()


# CTI-110
# P2T1 - Sales Prediction
# Jaya Manderson
# 02/18/2018
#

projectedTotalSales = float( input( "Please input projected amount" + \
                                    " of total sales: ") )

profit = 0.23 * projectedTotalSales

print( "The profit is $" + format( profit , ",.2f" ) )


# -*- coding: utf-8 -*-
"""
Created on Sun Apr  4 23:16:23 2021

@author: ErikB
"""
# board_size = 5

def board_columns():
    temp_column = []
    for column in range(board_size):
        list_temp=[]
        for i, num in enumerate(range(1, board_size_sq+1, board_size)):
            list_temp.append(game_dict[num+column])
        temp_column.append(list_temp)
    return temp_column

def board_rows():
    temp_row = []
    for row in range(board_size):
        board_mod = row * board_size
        list_temp=[]
        for i, num in enumerate(range(1, board_size+1)):
            list_temp.append(game_dict[num+board_mod])
        temp_row.append(list_temp)
    return temp_row

def board_diagonals():
    temp_diagonals = []
    numbuffer = 0
    temp_diag_one = []
    temp_diag_two = []
    while len(temp_diagonals) == 0:
        while len(temp_diag_one) != board_size:
            if len(temp_diag_one) == 0:
                numbuffer = 1
                temp_diag_one.append(game_dict[1])
            else:
                numbuffer += (board_size + 1)
                temp_diag_one.append(game_dict[numbuffer])
        temp_diagonals.append(temp_diag_one)
    while len(temp_diagonals) == 1:
        while len(temp_diag_two) != board_size:
            if len(temp_diag_two) == 0:
                numbuffer = board_size
                temp_diag_two.append(game_dict[board_size])
            else:
                numbuffer += (board_size - 1)
                temp_diag_two.append(game_dict[numbuffer])
        temp_diagonals.append(temp_diag_two)
    return temp_diagonals

def check_status():
    for row in rows:
        if len(set(row)) == 1:
            return True
    for column in columns:
        if len(set(column)) == 1:
            return True
    for diagonal in diagonals:
        if len(set(diagonal)) == 1:
            return True

def update_rcd():
    global rows, columns, diagonals
    rows = board_rows()
    columns = board_columns()
    diagonals = board_diagonals()

def print_board():
    current_board = list(game_dict.values())
    seq = 0
    #the padding value that we will check against and apply to our printed numbers
    print('\n')
    for i in range(board_size):
        row = '|'
        for e in range(board_size):
            temp_string = str(current_board[i+e+seq])
            pass_mod = 1
            #modulo function that will loosely center the text, flipping whether the space goes before or after based on iterations
            while len(temp_string) != pv:
                if pass_mod % 2 != 0:
                    temp_string = ' '+temp_string
                    pass_mod += 1
                else:
                    temp_string = temp_string+' '
                    pass_mod += 1
            row += temp_string + '|'
        seq += board_size - 1
        print (row)
    print('\n')

def start_game():
    print('Start game, please enter board size:')
    global board_size
    global board_size_sq
    global game_dict
    global X_win
    global O_win
    global pv

    board_size = int(input())
    board_size_sq = board_size ** 2
    game_dict = {}
    for i in range(board_size_sq):
        game_dict.update({i+1:i+1})
    X_win = board_size * 'X'
    O_win = board_size * 'O'
    pv = len(str(list(game_dict.values())[-1]))

    update_rcd()

    game_state = True
    turn_tracker = 1
    while game_state == True:
        if turn_tracker % 2 == 0:
            marker = 'X'
        else:
            marker = 'O'
        print(f"It's {marker}'s turn!")
        play_round(marker)
        update_rcd()
        status = check_status()
        if status == True:
            print_board()
            print(f"{marker} WINS!")
            game_state = False
        else:
            turn_tracker += 1

def play_round(marker):
    print_board()
    print('\n')
    _ = 0
    while _ == 0:
        move = int(input("Your move:"))
        while move in range(1,board_size_sq+1):
            if game_dict.get(move) == move:
                game_dict[move] = marker
                return None
            else:
                move = 'error'
                print('That square was alreaedy picked, or your input was invalid, please try again.')

start_game()


import os
import pandas as pd
import warnings
from datetime import datetime
from typing import Union, Tuple

from constants import Models
from utils import read_dataframe_input, filter_df_between

warnings.simplefilter("ignore")

class DataBuilder:
    """Class to interact with data model for budget

    Attributes
    ----------
        min_date : datetime
            Start date for the budget
        max_date: datetime
            End date for the budget

    Methods
    -------
        build_data_model():
            Acquires data from multiple sources, consolidates
        get_df():
            Returns df filtered for model

    """

    def __init__(self, min_date: datetime, max_date: datetime):
        """Initializes DataBuilder class

        Parameters
        ----------
            min_date : datetime
                Start date for the budget
            max_date: datetime
                End date for the budget
        """
        self.min_date = min_date
        self.max_date = max_date

    def _get_dates(self):
        """Reads table of dates from Inputs file

        Reads table from excel file into pd.DataFrame format. Sets self.dates
        """
        dates = read_dataframe_input(**Models.BudgetDate)
        dates = filter_df_between(
            dates, "date", (self.min_date, self.max_date), Models.BudgetDate.IndexColumn
        )
        self.dates = dates

    def _get_date_list(self):
        """One-line description of function

        Multi-line expanded description of function
        """
        self.date_list = [
            f"{x['year']}-{x['month_number']}-{x['day_number']}"
            for _, x in self.dates.iterrows()
        ]

    def _get_items(self):
        """Reads table of items from Inputs file

        Reads table from excel file into pd.DataFrame format. Sets self.items
        """
        self.items = read_dataframe_input(**Models.BudgetItem)

    def _get_date_items(self):
        """Creates cartesian product of dates and items dataframes

        Combines two existing dataframes as a cross join (or cartesian product).
        Sorts by budget_item, then date ascending. Adds a 0-value field for budget amt,
        which we fill in next.
        """
        df = self.dates.merge(self.items, how="cross")
        df = df.sort_values(
            by=["budget_item_id", "date_id"], ascending=[True, True]
        ).reset_index(drop=True)
        df["budget_item_amount"] = 0.00
        self.date_items = df

    def _is_even_week(self, week_number: int) -> bool:
        """Boolean check if week_number is even

        Simple math function to check whether week is even or not
        This is used for frequencies such as 'bi-weekly' so we can understand
            when to budget for something correctly

        Parameters
        ----------
            week_number : int

        Returns
        -------
            bool
                is_even_week
        """
        return (week_number % 2) == 0

    def _validate_date(self, year: int, month: int, day: int) -> bool:
        """Boolean check of date validity

        Create date based on year, month, day params.
        Validate based on the date_list created by earlier dates data

        Parameters
        ----------
            year : int
                year of date
            month : int
                month of date
            day : int
                day in month

        Returns
        -------
            bool
                exists_in_list
        """
        return f"{year}-{month}-{day}" in self.date_list

    def _get_max_day_in_yearmonth(self, year: int, month: int) -> int:
        """Get max day_number for given month & year

        Uses existing date_items DF, filters to given year & month, returns maximum
            day_number based on those params.

        Parameters
        ----------
            year : int
                given_year
            month : int
                given_month

        Returns
        -------
            int
                last_day_in_yearmonth
        """
        return (
            self.date_items.groupby(["year", "month_number"])["day_number"]
            .max()
            .reset_index()
            .query(f"year == {year} & month_number == {month}")["day_number"]
        )

    def _get_date_attribs(self, date: datetime) -> Tuple[int, bool]:
        """Get additional date attributes

        Gets additional date attributes based on passed datetime.
        Start day of week (day # in week based on datetime passed) and
        whether the week was an even week. This is used in bi-weekly freq's
        to understand what cadence to budget on

        Parameters
        ----------
            date : datetime
                start_date provided to base cadence upon

        Returns
        -------
            Tuple[int, bool]
                day_of_week, is_even_week
        """
        iter_date = self.dates[self.dates["date"] == date].iloc[0]
        start_day_of_week = iter_date["day_of_week"]
        is_even_week = self._is_even_week(iter_date["week_number"])
        return (start_day_of_week, is_even_week)

    def _audit_date_frequencies(self):
        """Check validity of dates in date_items

        Frequency defines day of budget in some cases (i.e. always due on 30th of month)
        If month does not have 30 days, we need to account for this and move to
            the max existing day in month.
        Iterates over rows in date_items, skips rows w/o frequency_day
        If row has an invalid date, set to max_day_in_yearmonth
        """
        for index, row in self.date_items.iterrows():
            if pd.isnull(row["frequency_day"]):
                continue
            if self._validate_date(
                row["year"], row["month_number"], int(row["frequency_day"])
            ):
                continue
            else:
                new_day = self._get_max_day_in_yearmonth(
                    row["year"], row["month_number"]
                )
                self.date_items.at[index, "frequency_day"] = new_day

    def _calc_multiplier(self, row: dict) -> float:
        """Calculate multiplier for budget_amount

        Calculates multiplier applied to input budget value
        If income, keep number positive else negative (expense)
        If seasonality applied, add seasonality_multiplier on top

        Parameters
        ----------
            row : dict
                row: dict (pd.DataFrame row)

        Returns
        -------
            float
                multiplier
        """
        return (1.00 if row["item_type"] == "Income" else -1.00) * (
            row["seasonality_multiplier"] if row["is_seasonality"] else 1.00
        )

    def _validate_record(self, row: dict) -> bool:
        """Validate basic requirements for date_item

        A few criterae to check before running through freq_types
            1 - if inactive record, return false
            2 - if end_date exists and date is past end, return false
            3 - if start_date exists and date is before start, return false

        Parameters
        ----------
            row : dict
                row: dict (pd.DataFrame row)

        Returns
        -------
            bool
                is valid record
        """
        if not row["is_active"]:
            return False

        if not pd.isnull(row["end_date"]) and row["date"] > row["end_date"]:
            ## print(f"Skipping end date.. {row['endDate']}, {row['date']}")
            return False

        if not pd.isnull(row["start_date"]) and row["date"] < row["start_date"]:
            ## print(f"Skipping start date.. {row['startDate']}, {row['date']}")
            return False

        return True

    def _calculate_budget_amount(self, row: dict) -> float:
        """Calculate budget amount for record in date_items

        Performs validation_check on record
        Checks by frequency_type for matching criteria
        Returns item_amount * calc_multiplier for records we want to budget for
            else 0.00

        Parameters
        ----------
            row : dict
                row of pd.Dataframe

        Returns
        -------
            float
                budget_amount
        """
        if not self._validate_record(row):
            return 0.00

        ## Daily -> Return Budgeted Amount
        if row["frequency_type"] == "Daily":
            return row["item_amount"] * self._calc_multiplier(row)

        ## Weekly ->
        elif row["frequency_type"] == "Weekly":
            ## If Limited to a Day, Ignore other Days
            if (
                not pd.isnull(row["frequency_day"])
                and row["day_of_week"] != row["frequency_day"]
            ):
                return 0.00
            ## If same day as limited, or no limited day (and day_number == 1)
            elif (row["day_of_week"] == row["frequency_day"]) or (
                pd.isnull(row["frequency_day"]) and row["day_of_week"] == 1
            ):
                return row["item_amount"] * self._calc_multiplier(row)
            else:
                return 0.00

        ##Bi-Weekly ->
        elif row["frequency_type"] == "Bi-Weekly":
            ##If no start-date, raise exception
            if pd.isnull(row["start_date"]):
                raise ValueError(
                    "Need startDate for bi-weekly expense to determine alternating weeks."
                )
            ##If start date, validate start day of week & even/odd week increments
            else:
                start_day_of_week, is_even_week = self._get_date_attribs(
                    row["start_date"]
                )
                if (
                    self._is_even_week(row["week_number"]) == is_even_week
                    and row["day_of_week"] == start_day_of_week
                ):
                    return row["item_amount"] * self._calc_multiplier(row)
                else:
                    return 0.00

        ##Monthly ->
        elif row["frequency_type"] == "Monthly":
            ##If limited to day of month
            if not pd.isnull(row["frequency_day"]):
                ##If not same day, return 0
                if row["day_number"] != row["frequency_day"]:
                    return 0.00
                ##Otherwise, calc
                else:
                    return row["item_amount"] * self._calc_multiplier(row)

            ##If not limited to day of month & is first of month, return
            elif pd.isnull(row["frequency_day"]) and row["frequency_day"] == 1:
                return row["item_amount"] * self._calc_multiplier(row)
            ##Otherwise 0
            else:
                return 0.00

        ##Annual ->
        elif row["frequency_type"] == "Annual":
            if pd.isnull(row["frequency_date"]):
                raise ValueError("Issue with frequency_date")
            elif (
                not pd.isnull(row["frequency_date"])
                and row["date"] == row["frequency_date"]
            ):
                return row["item_amount"] * self._calc_multiplier(row)
            else:
                return 0.00

        ##One-Time ->
        elif row["frequency_type"] == "One-Time":
            if pd.isnull(row["frequency_date"]):
                raise ValueError("Issue with frequency_date")
            elif row["date"] == row["frequency_date"]:
                return row["item_amount"] * self._calc_multiplier(row)
            else:
                return 0.00

        print(row)
        print("Issue -- did not make it through an IF")
        return 0.00

    def _calc_budget_amounts(self):
        """Applies function to each row in date_items

        Calculates budget_amount field using above function
        """
        self.date_items["budget_item_amount"] = self.date_items.apply(
            lambda x: self._calculate_budget_amount(x), axis=1
        )

    def build_data_model(self):
        """Runs individual steps to create data model"""
        self._get_dates()
        self._get_date_list()
        self._get_items()
        self._get_date_items()
        self._audit_date_frequencies()
        self._calc_budget_amounts()

    def get_df(self) -> pd.DataFrame:
        """Get DF limited to fields for tool

        Returns
        -------
            pd.DataFrame
                data model
        """
        df = self.date_items.copy()
        df = df[Models.ExportData.Columns]
        return df


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import os
# read_csv without arguments

nf = pd.read_csv('/kaggle/input/netflix-shows/netflix_titles.csv')

nf.head()
# Parse dates with "parse_dates" argument

nf = pd.read_csv('/kaggle/input/netflix-shows/netflix_titles.csv', parse_dates=['date_added'])

nf.head()
# Read selected columns using "usecols" argument

list_cols = ['show_id', 'type', 'title', 'country', 'date_added', 'rating', 'duration']

nf = pd.read_csv('/kaggle/input/netflix-shows/netflix_titles.csv', parse_dates=['date_added'], usecols = list_cols)

nf.head()
# Set a column as index by "index_col" argument - useful for making time series

nf = pd.read_csv('/kaggle/input/netflix-shows/netflix_titles.csv', parse_dates=['date_added'],

                 usecols = list_cols, index_col = 'show_id')

nf.head()
data_dir = '/kaggle/input/flowers-recognition/flowers/flowers'

# Dictionary of all images paths

flowers = {'sunflower':[], 'tulip':[], 'daisy':[], 'rose': [], 'dandelion': []}
# Populate the dictionary with image paths

for flower in flowers.keys():

    for dirname, _, filenames in os.walk(os.path.join(data_dir, flower)):

        for filename in filenames:

            flowers[flower].append((os.path.join(

                os.path.join(data_dir, flower), filename)))
# Showing 2 random images from each of the categories

from tensorflow.keras.preprocessing import image

for flower in list(flowers.keys()):

    plt.figure(figsize=(8, 5))

    flower_choice = np.random.choice(len(flowers[flower]),2) # Choose two images by random

    plt.subplot(1, 2, 1)

    img_path1 = flowers[flower][flower_choice[0]]

    img = image.load_img(img_path1)

    plt.imshow(img)

    plt.title(flower)

    plt.subplot(1, 2, 2)

    img_path2 = flowers[flower][flower_choice[1]]

    img = image.load_img(img_path2)

    plt.imshow(img)

    plt.title(flower)

plt.show()
# The dataset contains a metadata table

path = '../input/dickens/dickens'

meta = pd.read_csv(os.path.join(path, 'metadata.tsv'), delimiter='\t')

meta.head()
# Read the first part of one txt file

file1 = open(os.path.join(path, '924-0.txt'),'r')

_ = file1.read(500)

print(_)
files = {} # Create a dictionary of file names and contents

for f in os.listdir('/kaggle/input/dickens/dickens'):

    if f.endswith('.txt'):

        with open(os.path.join(path, f), "r") as file:

            files[f] = file.read()
# Convert to dataframe

_ = pd.DataFrame.from_dict(files, orient='index', columns=['content']).reset_index()

dickens = meta.merge(_, left_on='Path', right_on='index')

dickens.head()
from google.cloud import bigquery

client = bigquery.Client()

# Get dataset

hacker_ref = client.dataset('hacker_news', project='bigquery-public-data')

hacker = client.get_dataset(hacker_ref)

# List all table names

tables = list(client.list_tables(hacker))

for table in tables:

    print(table.table_id)
# Get a table

table_ref = hacker.table('full')

table = client.get_table(table_ref)
# Before loading the table, we can get some attributes

print ('No. of rows: ' + str(table.num_rows))

print ('Size (MB): ' + str(int(table.num_bytes / 1024768)))

print ('Columns:')

print (list(c.name for c in table.schema))
# Get first 100 rows

table_df = client.list_rows(table, max_results=100).to_dataframe()

table_df.head()
# Write SQL query to get data

# Get top 10 authors by number of articles

query = """

SELECT author, count(id) as stories

FROM `bigquery-public-data.hacker_news.stories`

GROUP BY author

ORDER BY count(id) DESC

"""

query_job = client.query(query)

iterator = query_job.result()

rows = list(iterator)

# Transform the rows into a nice pandas dataframe

top_authors = pd.DataFrame(data=[list(x.values()) for x in rows], columns=list(rows[0].keys()))

# Look at the first 10 headlines

top_authors.head(10)
img = pd.read_parquet('../input/bengaliai-cv19/train_image_data_0.parquet')

img.shape
img.head()
img2 = img.iloc[:,1:].values.reshape((-1,137,236,1))

row=3; col=4;

plt.figure(figsize=(20,(row/col)*12))

for x in range(row*col):

    plt.subplot(row,col,x+1)

    plt.imshow(img2[x,:,:,0])

plt.show()
import cv2

DIM = 64

img3 = np.zeros((img2.shape[0],DIM,DIM,1),dtype='float32')

for j in range(img2.shape[0]):

    img3[j,:,:,0] = cv2.resize(img2[j,],(DIM,DIM),interpolation = cv2.INTER_AREA)

row=3; col=4;

plt.figure(figsize=(20,(row/col)*12))

for x in range(row*col):

    plt.subplot(row,col,x+1)

    plt.imshow(img3[x,:,:,0])

plt.show()
# Free up memory

del img
import json

data  = []

with open("/kaggle/input/arxiv/arxiv-metadata-oai-snapshot.json", 'r') as f:

    for line in f:

        data.append(json.loads(line))

print("No. of records: {}".format(len(data)))
# See first item

data[0]
# Convert to dataframe - due to memory issue we only load the first 1000 items

df = pd.DataFrame(data[:1000])

df.head()
# Free up some memory

del data
from xml.etree import ElementTree

path = '../input/covid19-clinical-trials-dataset/COVID-19 CLinical trials studies/'

files = os.listdir(path)

print('Total Researches going on: ',len(files))
# Inspect the first element

file_path = os.path.join(path, files[0])

tree = ElementTree.parse(file_path)

root = tree.getroot()

print (root.tag, root.attrib)
# Look at all the children nodes

for child in root:

    print (child.tag, child.attrib)
df_temp = pd.DataFrame()

df = pd.DataFrame()

i = 0

for file in files[:10]: # Get the first 10 studies

    file_path = os.path.join(path, file)

    tree = ElementTree.parse(file_path)

    root = tree.getroot()

    trial = {} # Initialize dictionary

    # read tags using root.find() method

    trial['nct_id'] = root.find('id_info').find('nct_id').text

    trial['brief_title'] = root.find('brief_title').text

    trial['overall_status'] = root.find('overall_status').text

    df_temp  = pd.DataFrame(trial,index=[i])

    i=i+1

    df = pd.concat([df, df_temp])
df


#User function Template for python3

class Solution:
    def isSorted(self,arr,n):

        for i in range(1, n-1):
            if(arr[i-1] <=arr[i]<=arr[i+1] or arr[i-1] >=arr[i]>=arr[i+1]):
                continue
            else:
                return 0
        return 1

#{
#  Driver Code Starts
#Initial Template for Python 3

#contributed by RavinderSinghPB

if __name__ =='__main__':
    tcs=int(input())

    for _ in range(tcs):
        n=int(input())
        arr=[int(x) for x in input().split()]

        print(Solution().isSorted(arr,n))
# } Driver Code Ends


from tqdm import tqdm

import time
import numpy as np

import torch
import torch.nn as nn

from utils.accuracy_score_compute import Evaluate
#https://blog.csdn.net/u012370185/article/details/94409933

def evaluate(model, val_dataloader, log, args):
    """Runs official COCO evaluation.

    limit: if not 0, it's the number of images to use for evaluation

    Args:
        model : The model.
        val_dataloader : A DataLoader object with valiadtion data
        cocoGT : coco GroundTruth
        eval_type : "bbox" or "segm" for bounding box or segmentation evaluation. Defaults to "segm".
    """

    model.eval()

    t_start = time.time()
    Iou = []
    evaluate = Evaluate(args)
    for n_batch, sample in enumerate(tqdm(val_dataloader, desc='Evaluate:')):
        with torch.no_grad():
            image = sample[0]["image"].cuda()
            target = sample[0]['label'].cuda()
            origin_size = sample[1]
            image_var = torch.autograd.Variable(image).float()
            target_var = torch.autograd.Variable(target).long()

            prediction = model(image_var)
            prediction = nn.functional.interpolate(
            prediction, size=target_var.size()[1:], mode="bilinear", align_corners=False)
            prediction = prediction.cpu().numpy()
            target_var = target_var.cpu().numpy()
            prediction = prediction.argmax(axis=1).astype(np.uint8)
            evaluate.hist_martrix(prediction,target_var)
    acc, acc_cls, iou, fwavacc = evaluate.label_accuracy_score()
    Iou.append(iou)

    MIOU = sum(Iou)/len(Iou)
    total_time = time.time() - t_start
    log.logger.info("MIoU is: {}. ".format(MIOU))
    log.logger.info("Total time:{}".format(float(total_time)))


from datetime import date
import functools
import gzip
import json

from miracle.stream.frontend_s3 import (
    main,
    validate,
    validate_user,
)
from miracle.stream.tests.conftest import make_record

def _payload(crypto, data):
    data = json.dumps(data).encode('utf-8')
    return crypto.encrypt(data).encode('ascii')

def test_validate_user():
    assert not validate_user(None)
    assert not validate_user(b'')
    assert not validate_user(b'abcdefgh')
    assert not validate_user('')
    assert not validate_user('ab')
    assert not validate_user('a' * 40)
    assert not validate_user('abcd?')
    assert not validate_user('abcd\xfe')
    assert validate_user('foo')
    assert validate_user('fae006df902d4809aaddb176b6bdf8dd')
    assert validate_user('fae006df-902d-4809-aadd-b176b6bdf8dd')

def test_validate():
    invalid_inputs = [
        {'other_key': []},
        {'user': 123},
        {'user': 123, 'other': []},
        {'user': b'ab', 'other': []},
    ]
    for invalid in invalid_inputs:
        assert not validate(invalid)

    valid_inputs = [
        {'user': 'foo'},
        {'user': 'foo', 'other': [{}]},
        {'user': 'foo', 'other': [], 'another': 'abc'},
    ]
    for valid in valid_inputs:
        assert validate(valid) == valid

def test_main(bucket, crypto, processor):
    _p = functools.partial(_payload, crypto)
    today = date.today()
    user = 'foo'
    prefix = 'v2/sessions/%s/%s/%s/' % (today.year, today.month, user)

    records = [
        make_record('2', _p({'user': user, 'extra': 1})),
    ]
    assert main(processor, records) == ('2', 0)

    objs = list(bucket.filter(Prefix=prefix))
    assert len(objs) == 1
    assert objs[0].key.endswith('.json.gz')

    obj = bucket.get(objs[0].key)
    assert obj['ContentEncoding'] == 'gzip'
    assert obj['ContentType'] == 'application/json'
    body = obj['Body'].read()
    obj['Body'].close()

    body = json.loads(gzip.decompress(body).decode('utf-8'))
    assert body == {'user': user, 'extra': 1}

    # Upload a second time
    records = [
        make_record('3', _p({'user': user, 'extra': 2})),
    ]
    assert main(processor, records) == ('3', 0)

    objs = list(bucket.filter(Prefix=prefix))
    assert len(objs) == 2

def test_main_fail(crypto, processor, stats):
    payload = b'not encrypted'
    records = [make_record('1', payload)]
    assert main(processor, records) != ('1', 0)

    payload = crypto.encrypt(b'no json').encode('ascii')
    records = [make_record('2', payload)]
    assert main(processor, records) != ('2', 0)

    payload = crypto.encrypt(json.dumps({'no': 'user'})).encode('ascii')
    records = [make_record('3', payload)]
    assert main(processor, records) != ('3', 0)

    stats.check(counter=[
        ('stream.process.error', 1, ['reason:encryption']),
        ('stream.process.error', 1, ['reason:json']),
        ('stream.process.error', 1, ['reason:validation']),
    ])