import os

from django.shortcuts import render
from sample_app import support_functions
from sample_app.models import Country, Currency, Rates, Stock, Company, Exchange, AccountHolder, Portfolio, UploadedFiles
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect
from .forms import UploadFileForm

# Create your views here.
def home(request):
    data = dict()
    user =request.user
    if user.is_superuser:
        return render(request,"maintenance.html",context=data)
    import datetime
    date = datetime.datetime.now()
    data['now'] = date
    data['city'] = "New York"
    return render(request, "home.html",context=data)
def show3divs(request):
    data = dict()
    return render(request,"div_test.html",context=data)
def showform(request):
    data=dict()
    return render(request,"form_test.html",context=data)

def maintenance(request):
    data = dict()
    try:
        form_submitted = request.GET['form_submitted']
        choice = request.GET['selection']
        if choice == "currencies":
            support_functions.add_countries_and_currencies(support_functions.get_currency_list())
    except:
        pass
    return render(request,"maintenance.html",context=data)
#Make sure you've imported Country from models.py!
def currency_selection(request):
    data = dict()
    countries = Country.objects.all()
    data['countries'] = countries
    return render(request,"country_selector1.html",context=data)
def exch_rate(request):
    data=dict()
    try:
        country1 = request.GET['country_from']
        country2 = request.GET['country_to']
        data['country1'] = Country.objects.get(id=country1)
        data['country2'] = Country.objects.get(id=country2)
        currency1 = Country.objects.get(id=country1).currency
        currency2 = Country.objects.get(id=country2).currency
        support_functions.update_xrates(currency1)
        data['currency1'] = currency1
        data['currency2'] = currency2
        try:
            rate = currency1.rates_set.get(x_currency=currency2.symbol).rate
            data['rate'] = rate
        except:
            pass
    except:
        pass
    return render(request,"exchange_detail.html",context=data)

def company_selection(request):
    data= dict()
    try:
        support_functions.read_function()
    except:
        print("error in company selection!")
    companies= Stock.objects.all().values('name')
    id=Stock.objects.all().values('id')
    url= Stock.objects.all().values('url')
    ticker= Stock.objects.all().values('ticker')
    data['companies']= companies
    data['urls']= url
    data['tickers']= ticker
    data['id']= id
    return render(request, "company_selector.html", context=data)
def register_new_user(request):
    context = dict()
    form = UserCreationForm(request.POST)
    if form.is_valid():
        new_user = form.save()
        dob = request.POST["dob"]
        acct_holder = AccountHolder(user=new_user,date_of_birth=dob)
        acct_holder.save()
        return render(request,"entry.html",context=dict())
    else:
        form = UserCreationForm()
        context['form'] = form
        return render(request, "registration/register.html", context)
def entry(request):
    data = dict()
    return render(request, "entry.html", context=data)
def ticker_sel(request):
    data=dict()
    try:
        ticker1 = request.GET['c_name']
        data['url']=Stock.objects.get(name=ticker1).url
        data['ticker']=Stock.objects.get(name=ticker1).ticker
        data['name']=Stock.objects.get(name=ticker1).name
        stock_details = support_functions.get_stock_details(data['url'])
        data['price_chance'] = stock_details[0]
        data['change_percent'] = stock_details[1]
        data['stock_price'] = stock_details[2]
        data['market_cap'] = stock_details[3]
        data['shares_in_issue'] = stock_details[4]
        data['revenue'] = stock_details[5]
        data['profit_loss'] = stock_details[6]
        data['EPS'] = stock_details[7]
        data['PE_ratio'] = stock_details[8]
    except:
        ticker="AHC"
        data['ticker']=ticker
        print("error")
    return render(request,"company_details.html",data)

def form_results2(request):
    data=dict()
    username = request.GET['name']
    stock = request.GET['stock']
    amount = float(request.GET['dollars'])
    print("data test:", username, stock, amount)
    commission = amount*0.20
    returned_amount = amount-commission
    data['person'] = username
    data['selected_stock'] = stock
    data['amount'] = returned_amount
    return render(request,"form_results.html",context=data)

def upload_file(request):
    data = {}
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
        file = request.FILES['myfile']
        handle_uploaded_file(request, file)
    else:
        print(form.errors)
    account_holder = AccountHolder.objects.get(user=request.user)
    data["UploadedFiles"] = UploadedFiles.objects.filter(user_account=account_holder)
    return render(request, 'view_file_list.html', context=data)

def handle_uploaded_file(request, f):
    user = request.user
    account_holder = AccountHolder.objects.get(user=user)
    try:
        f1=UploadedFiles.objects.create(user_account=account_holder, user_uploaded_file=f)
        f1.save()
        print("Working")
    except Exception as error:
       xprint(error)

def form_results(request):
    data = dict()
    user = request.user
    account_holder = AccountHolder.objects.get(user=user)
    ticker = request.GET['ticker']
    quantity = request.GET['quantity']
    try:
        p1 = Portfolio.objects.get(user_account=account_holder, user_stock_ticker=ticker)
        p1.user_stock_quantity = quantity
        p1.save()
    except:
        p1 = Portfolio(user_account=account_holder, user_stock_ticker=ticker, user_stock_quantity=quantity)
        p1.save()
    p_list= list(Portfolio.objects.filter(user_account=account_holder).values())
    q_list=list()
    t_list=list()
    for l in p_list:
        q_list.append(l.get('user_stock_quantity'))
        t_list.append(l.get('user_stock_ticker'))

    data["Portfolio"] = Portfolio.objects.filter(user_account=account_holder)
    result_list = Portfolio.objects.filter(user_account=account_holder)
    price_list = list()
    beta_list = list()
    for entry in result_list:
        t = entry.user_stock_ticker
        price_list.append(support_functions.get_latest_price(t))
        beta_list.append(
            support_functions.get_stock_beta("https://finance.yahoo.com/quote/" + t + "?p=" + t + "&.tsrc=fin-srch"))
    data["price_list"] = price_list
    value=list()
    for i in range(0, len(price_list)):
        value.append(float(price_list[i])*float(q_list[i]))
    data["quantity_list"]=q_list
    data["values"]=value
    beta_w=0
    summ=0
    print("we have value and q")
    for i in range(0, len(value)):
        if beta_list[i]=="N/A":
            pass
        else:
            beta_w=beta_w + (float(value[i])*float(beta_list[i]))
            summ=summ+value[i]
    beta_w=beta_w/summ
    beta_w_l=list()
    beta_w_l.append(beta_w)
    print("we have value beta_w_l")

    for i in range(1, len(value)):
        beta_w_l.append(beta_w)
    print(beta_w_l)
    zipped_list = zip(t_list,q_list,price_list,value,beta_list,beta_w_l)
    data = {'zipped_list': zipped_list}
    print("we have value zipped_list",zipped_list)
    return render(request, "form_results.html", context=data)


import pytest

@pytest.fixture
def bssn_dirtree(make_builder):
    builder = make_builder()
    builder.set_root('bssn')
    builder.set_ccodesdir('codes')
    builder.set_outdir('output')
    return builder.build()

@pytest.fixture
def bssn_dirtree_build_prefix(make_builder):
    builder = make_builder(prefix_path='build')
    builder.set_root('bssn')
    builder.set_ccodesdir('codes')
    builder.set_outdir('output')
    return builder.build()

@pytest.fixture
def bssn_dirtree_build_prefix_records():
    return {
        'root': 'build/bssn',
        'ccodesdir': 'build/bssn/codes',
        'outdir': 'build/bssn/codes/output'
        }


from __future__ import print_function
import sys
import os
from argparse import ArgumentParser, SUPPRESS
import numpy as np
import cv2
import logging as log
from time import time
from openvino.inference_engine import IENetwork, IECore
from PIL import Image

def generate_upsample_file(original_image_path, scale_factor, upsampled_file_name):
    image = cv2.imread(str(original_image_path))
    upsampled_image = cv2.resize(image, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_CUBIC)
    cv2.imwrite(str(original_image_path.parent / upsampled_file_name), upsampled_image)

def build_argparser():
    parser = ArgumentParser(add_help=False)
    args = parser.add_argument_group('Options')
    args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
    args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model",
                      required=True, type=str)
    args.add_argument("-i", "--input", help="Required. Path to a folder with images or path to an image files",
                      required=True, type=str, nargs="+")
    args.add_argument("-l", "--cpu_extension",
                      help="Optional. Required for CPU custom layers. "
                           "Absolute MKLDNN (CPU)-targeted custom layers. Absolute path to a shared library with the "
                           "kernels implementations", type=str, default=None)
    args.add_argument("-d", "--device",
                      help="Optional. Specify the target device to infer on; CPU, GPU, FPGA, HDDL or MYRIAD is "
                           "acceptable. Sample will look for a suitable plugin for device specified. Default value is CPU",
                      default="CPU", type=str)
    args.add_argument("-nt", "--number_top", help="Optional. Number of top results", default=10, type=int)
    return parser

def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    log.info("Creating Inference Engine")
    ie = IECore()
    if args.cpu_extension and 'CPU' in args.device:
        ie.add_extension(args.cpu_extension, "CPU")
    # Read IR
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)

    if "CPU" in args.device:
        supported_layers = ie.query_network(net, "CPU")
        not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) != 0:
            log.error("Following layers are not supported by the plugin for specified device {}:\n {}".
                      format(args.device, ', '.join(not_supported_layers)))
            log.error("Please try to specify cpu extensions library path in sample's command line parameters using -l "
                      "or --cpu_extension command line argument")
            sys.exit(1)
    assert len(net.inputs.keys()) == 2, "Sample supports only single input topologies"
    assert len(net.outputs) == 1, "Sample supports only single output topologies"

    log.info("Preparing input blobs")
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    net.batch_size = len(args.input)

    # Read and pre-process input images
    n, c, h, w = net.inputs[input_blob].shape
    log.info(net.inputs)
    log.info(net.outputs[out_blob].shape)

    original_image = cv2.imread(args.input[0])
    log.info("shape of original image: {}".format(original_image.shape))
    #CHANGE THIS TO REMOVE HARDCODING
    original_image = cv2.resize(original_image, (480, 270))

    transposed_image = np.transpose(original_image, (2, 0, 1))

    upsampled_file_name = "upsampled.jpg"

    upsampled_dsize = (1920, 1080)
    upsampled_image = cv2.resize(original_image, upsampled_dsize, interpolation=cv2.INTER_CUBIC)
    transposed_upsampled_image = np.transpose(upsampled_image, (2, 0, 1))
    log.info("shape of upsampled image: {}".format(upsampled_image.shape))

    log.info("Batch size is {}".format(n))

    # Loading model to the plugin
    log.info("Loading model to the plugin")
    exec_net = ie.load_network(network=net, device_name=args.device)

    # Start sync inference
    log.info("Starting inference")
    images = {'0': np.expand_dims(transposed_image, axis=0),
              '1': np.expand_dims(transposed_upsampled_image, axis=0)}
    res = exec_net.infer(inputs=images)

    # Processing output blob
    log.info("Processing output blob")
    res = res[out_blob][0]
    res *= 255
    res = np.clip(res, 0., 255.)
    res = res.transpose((1, 2, 0)).astype(np.uint8)

    out_img = os.path.join(os.path.dirname(__file__), "output.jpg")
    cv2.imwrite(out_img, res)

    log.info("Result image was saved to {}".format(out_img))

if __name__ == '__main__':
    sys.exit(main() or 0)


import os
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import logging
import sys

import optuna
from optuna.trial import TrialState

from npyx.feat import filter_df
from tqdm.auto import tqdm
from sklearn.metrics import confusion_matrix, f1_score
from sklearn.model_selection import StratifiedKFold
from sklearn.ensemble import RandomForestClassifier
from imblearn.over_sampling import RandomOverSampler

from sklearn.metrics import confusion_matrix

import xgboost as xgb

seed = np.random.seed(123)

# get relative data folder
PATH = os.path.dirname(os.path.abspath(""))
data_folder = PATH + "/celltypes-classification/data"
DATA_PATH = data_folder + ("/Aug-09-2022_all_features.csv")

df = pd.read_csv(DATA_PATH, index_col=0)

def filter_df(df: pd.DataFrame):
    """
    Filters out datapoints with unusable temporal features.
    """
    features_only = df.iloc[:, 2:]
    bad_idx = []
    for i, row in features_only.iterrows():
        value, count = np.unique(row.to_numpy(), return_counts=True)
        zeros = count[value == 0]
        if zeros.size > 0 and zeros > 5:
            bad_idx.append(i)
    keep = [i for i in range(len(df)) if i not in bad_idx]
    return df.iloc[keep]

def generate_train_and_labels(df: pd.DataFrame, info_idx=[0, 1, 2, 18]):
    info = df.iloc[:, info_idx]
    features = df.iloc[:, ~np.isin(np.arange(len(df.columns)), info_idx)]
    return features.copy(), info.iloc[:, 0].copy()

X, y = generate_train_and_labels(filter_df(df))

LABELLING = {"PkC_cs": 5, "PkC_ss": 4, "MFB": 3, "MLI": 2, "GoC": 1, "GrC": 0}
y.replace(to_replace=LABELLING, inplace=True)

def define_model(trial: optuna.trial.Trial):
    # We optimize the number of layers, hidden units and dropout ratio in each layer.

    n_estimators = trial.suggest_int("n_estimators", 10, 100)

    booster = trial.suggest_categorical("booster", ["gbtree", "gblinear", "dart"])

    max_depth = trial.suggest_categorical("max_depth", [0, 4, 5, 6, 7, 8, 9, 10])

    learning_rate = trial.suggest_float("learning_rate", 0.0, 1.0, step=0.01)

    reg_lambda = trial.suggest_float("reg_lambda", 0.0, 5.0, step=0.1)

    reg_alpha = trial.suggest_float("reg_alpha", 0.0, 2.0, step=0.1)

    subsample = trial.suggest_float("subsample", 0.1, 1.0, step=0.1)

    return xgb.XGBClassifier(
        n_estimators=n_estimators,
        booster=booster,
        max_depth=max_depth,
        learning_rate=learning_rate,
        reg_lambda=reg_lambda,
        reg_alpha=reg_alpha,
        subsample=subsample,
        eval_metric="mlogloss",
        use_label_encoder=False,
    )

def objective(trial: optuna.trial.Trial):

    # Generate the model.
    f1_scores = []

    model = define_model(trial)

    kfold = StratifiedKFold(n_splits=5, shuffle=False)

    for fold, (train_idx, val_idx) in tqdm(
        enumerate(kfold.split(X, y)),
        leave=True,
        position=0,
        desc="Cross-validating",
        total=5,
    ):

        X_train = X.iloc[train_idx]
        y_train = y.iloc[train_idx]
        X_test = X.iloc[val_idx]
        y_test = y.iloc[val_idx]

        oversample = RandomOverSampler(random_state=seed)

        X_big, y_big = oversample.fit_resample(X_train, y_train)

        # D_train = xgb.DMatrix(X_big, label=y_big)
        # D_test = xgb.DMatrix(X_test, y_test)

        model.fit(X_big, y_big)

        pred = model.predict(X_test)

        fold_f1 = f1_score(y_test, pred, average="weighted")
        f1_scores.append(fold_f1)
        trial.report(np.array(f1_scores).mean(), fold)

        # Handle pruning based on the intermediate value.
        if trial.should_prune():
            raise optuna.exceptions.TrialPruned()

    return np.array(f1_scores).mean()

def main():

    optuna.logging.get_logger("optuna").addHandler(logging.StreamHandler(sys.stdout))
    study_name = "XGB-feat-eng"  # Unique identifier of the study.
    storage_name = "sqlite:///{}.db".format(study_name)

    study = optuna.create_study(
        study_name=study_name,
        storage=storage_name,
        load_if_exists=True,
        direction="maximize",
    )
    study.optimize(objective, n_trials=50, timeout=None)

    pruned_trials = study.get_trials(deepcopy=False, states=[TrialState.PRUNED])
    complete_trials = study.get_trials(deepcopy=False, states=[TrialState.COMPLETE])

    print("Study statistics: ")
    print("  Number of finished trials: ", len(study.trials))
    print("  Number of pruned trials: ", len(pruned_trials))
    print("  Number of complete trials: ", len(complete_trials))

    print("Best trial:")
    trial = study.best_trial

    print("  Value: ", trial.value)

    print("  Params: ")
    for key, value in trial.params.items():
        print("    {}: {}".format(key, value))

if __name__ == "__main__":
    main()


from __future__ import absolute_import
import json
import hashlib
from collections import OrderedDict

import unittest2

from st2common.models.db.pack import PackDB
from st2common.models.db.sensor import SensorTypeDB
from st2common.models.db.action import ActionDB
from st2common.models.db.rule import RuleDB
from st2common.models.db.trigger import TriggerTypeDB
from st2common.models.db.trigger import TriggerDB
from st2common.models.db.policy import PolicyTypeDB
from st2common.models.db.policy import PolicyDB
from st2common.models.db.auth import ApiKeyDB

__all__ = ["DBModelUIDFieldTestCase"]

class DBModelUIDFieldTestCase(unittest2.TestCase):
    def test_get_uid(self):
        pack_db = PackDB(ref="ma_pack")
        self.assertEqual(pack_db.get_uid(), "pack:ma_pack")
        self.assertTrue(pack_db.has_valid_uid())

        sensor_type_db = SensorTypeDB(name="sname", pack="spack")
        self.assertEqual(sensor_type_db.get_uid(), "sensor_type:spack:sname")
        self.assertTrue(sensor_type_db.has_valid_uid())

        action_db = ActionDB(name="aname", pack="apack", runner_type={})
        self.assertEqual(action_db.get_uid(), "action:apack:aname")
        self.assertTrue(action_db.has_valid_uid())

        rule_db = RuleDB(name="rname", pack="rpack")
        self.assertEqual(rule_db.get_uid(), "rule:rpack:rname")
        self.assertTrue(rule_db.has_valid_uid())

        trigger_type_db = TriggerTypeDB(name="ttname", pack="ttpack")
        self.assertEqual(trigger_type_db.get_uid(), "trigger_type:ttpack:ttname")
        self.assertTrue(trigger_type_db.has_valid_uid())

        trigger_db = TriggerDB(name="tname", pack="tpack")
        self.assertTrue(trigger_db.get_uid().startswith("trigger:tpack:tname:"))

        # Verify that same set of parameters always results in the same hash
        parameters = {"a": 1, "b": "unicode", "c": [1, 2, 3], "d": {"g": 1, "h": 2}}
        paramers_hash = json.dumps(parameters, sort_keys=True)
        paramers_hash = hashlib.md5(paramers_hash.encode()).hexdigest()

        parameters = {"a": 1, "b": "unicode", "c": [1, 2, 3], "d": {"g": 1, "h": 2}}
        trigger_db = TriggerDB(name="tname", pack="tpack", parameters=parameters)
        self.assertEqual(
            trigger_db.get_uid(), "trigger:tpack:tname:%s" % (paramers_hash)
        )
        self.assertTrue(trigger_db.has_valid_uid())

        parameters = {"c": [1, 2, 3], "b": "unicode", "d": {"h": 2, "g": 1}, "a": 1}
        trigger_db = TriggerDB(name="tname", pack="tpack", parameters=parameters)
        self.assertEqual(
            trigger_db.get_uid(), "trigger:tpack:tname:%s" % (paramers_hash)
        )
        self.assertTrue(trigger_db.has_valid_uid())

        parameters = {"b": "unicode", "c": [1, 2, 3], "d": {"h": 2, "g": 1}, "a": 1}
        trigger_db = TriggerDB(name="tname", pack="tpack", parameters=parameters)
        self.assertEqual(
            trigger_db.get_uid(), "trigger:tpack:tname:%s" % (paramers_hash)
        )
        self.assertTrue(trigger_db.has_valid_uid())

        parameters = OrderedDict(
            {"c": [1, 2, 3], "b": "unicode", "d": {"h": 2, "g": 1}, "a": 1}
        )
        trigger_db = TriggerDB(name="tname", pack="tpack", parameters=parameters)
        self.assertEqual(
            trigger_db.get_uid(), "trigger:tpack:tname:%s" % (paramers_hash)
        )
        self.assertTrue(trigger_db.has_valid_uid())

        policy_type_db = PolicyTypeDB(resource_type="action", name="concurrency")
        self.assertEqual(policy_type_db.get_uid(), "policy_type:action:concurrency")
        self.assertTrue(policy_type_db.has_valid_uid())

        policy_db = PolicyDB(pack="dummy", name="policy1")
        self.assertEqual(policy_db.get_uid(), "policy:dummy:policy1")

        api_key_db = ApiKeyDB(key_hash="valid")
        self.assertEqual(api_key_db.get_uid(), "api_key:valid")
        self.assertTrue(api_key_db.has_valid_uid())

        api_key_db = ApiKeyDB()
        self.assertEqual(api_key_db.get_uid(), "api_key:")
        self.assertFalse(api_key_db.has_valid_uid())


import functools

from code_contrast.format_2022q3 import contrast
from refact_encoding import RefactEncoding
from refact_data_pipeline import DatasetOpts

from typing import List

def postprocess(text: str, language: str):
    cutted = []
    if language in ["python"]:
        for x in text.split("\n"):
            if x.startswith(" ") or x.strip() == "":
                cutted.append(x)
            elif not x.startswith(" "):
                break
    elif language in ["cpp", "go", "js"]:
        for x in text.split("\n"):
            cutted.append(x)
            if x.startswith("}"):
                break
        else:
            cutted.append("}")
    elif language in ["java"]:
        for x in text.split("\n"):
            cutted.append(x)
            if x.startswith("    }"):
                break
        cutted.append("}")
    else:
        assert False, "language is not supported"
    return "\n".join(cutted)

class HumanEvalXContinuation:
    def __init__(
            self,
            inner_filter,
            dataopts: DatasetOpts,
            language: str,
    ):
        self.inner_filter = inner_filter
        self.dataopts = dataopts
        self.enc: RefactEncoding = dataopts.encoding
        self.language = language

    def decode_result(self, prompt, tokens_with_completion: List[int]):
        txt = self.enc.decode(tokens_with_completion, cut_at_eot=True)
        assert txt.startswith(prompt)
        completion = postprocess(txt[len(prompt):], self.language)
        ret = {
            "completion": completion,
            "tokens_with_completion": [int(t) for t in tokens_with_completion],
        }
        return ret

    def __iter__(self):
        for ex in self.inner_filter:
            ex["completion"] = ex["canonical_solution"]
            ex["decode_result_fn"] = functools.partial(self.decode_result, ex["prompt"])
            yield ex

class HumanEvalXContrast:
    def __init__(
            self,
            inner_filter,
            dataopts: DatasetOpts,
            language: str,
    ):
        self.inner_filter = inner_filter
        self.dataopts = dataopts
        self.enc: RefactEncoding = dataopts.encoding
        self.language = language
        fn_ext = {
            "python": "py",
            "cpp": "cpp",
            "java": "java",
            "js": "js",
            "go": "go",
        }[language]
        self.fn = f"test.{fn_ext}"

    def decode_result(self, odm, tokens_with_completion: List[int]):
        diff = contrast.ContrastDiff(self.enc)
        fn = next(iter(odm["orig_tokens"]))
        ret = dict()
        dest_tokens = dict()
        edit_stats = dict()
        try:
            us = diff.untokenize(tokens_with_completion, full_orig_tokens=odm["orig_tokens"])
            dest_tokens = diff.apply_edits_return_dest(us)
            edit_stats = us.stats
        except Exception:
            pass
        txt = self.enc.decode(dest_tokens.get(fn, []))
        if txt.startswith(odm["original_prompt"]):
            completion = postprocess(txt[len(odm["original_prompt"]):], self.language)
        else:
            completion = ""
        ret["errors"] = diff.errors
        ret["edit_stats"] = edit_stats
        ret["dest_tokens"] = diff.dest_tokens
        ret["completion"] = completion
        ret["tokens_with_completion"] = [int(t) for t in tokens_with_completion]
        # "completion_tokens" unchanged, will contain canonical solution
        return ret

    def __iter__(self):
        raise NotImplementedError

class HumanEvalXInfillPrompt(HumanEvalXContrast):

    def __iter__(self):
        for ex in self.inner_filter:
            dest = ex["prompt"] + ex["canonical_solution"]
            odm = {
                "orig": {self.fn: ex["prompt"] + self.enc.decode([self.enc.INFILL]) + "\n\n"},
                "dest": {self.fn: dest},
                "commitmsg": "Infill",
                "original_prompt": ex["prompt"].rstrip(),
                "stats": ex["stats"],
            }
            diff = contrast.ContrastDiff(self.enc)
            diff.from_odm_dict(
                odm,
                n_ctx=1024,
            )
            assert len(diff.edits) == 1
            diff.write_edits()
            ex["prompt_tokens"] = diff.r[:diff.offset_first_postoken]
            ex["completion_tokens"] = diff.r[diff.offset_first_postoken:]
            odm["orig_tokens"] = diff.orig_tokens
            ex["decode_result_fn"] = functools.partial(self.decode_result, odm)
            yield ex

class HumanEvalXInstructPrompt(HumanEvalXContrast):

    def __iter__(self):
        for ex in self.inner_filter:
            dest = ex["prompt"] + ex["canonical_solution"]
            odm = {
                "orig": {self.fn: ex["prompt"].rstrip() + "\n\n\n"},
                "dest": {self.fn: dest},
                "commitmsg": f"Implement {ex['entry_point']} function",
                "original_prompt": ex["prompt"].rstrip(),
                "stats": ex["stats"],
            }
            diff = contrast.ContrastDiff(self.enc)
            diff.from_odm_dict(
                odm,
                n_ctx=1024,
            )
            assert len(diff.edits) == 1
            diff.write_edits()
            ex["prompt_tokens"] = diff.r[:diff.offset_first_postoken]
            ex["completion_tokens"] = diff.r[diff.offset_first_postoken:]
            odm["orig_tokens"] = diff.orig_tokens
            ex["decode_result_fn"] = functools.partial(self.decode_result, odm)
            yield ex


import base64
from importlib import import_module
import io
import json
import sys
import unittest

import redis

REDIS_DB = 3

binary_values = [
    b'{"hello": "object"}',
    b'hello string'
]

expected_value = {'hello': 'dict'}

class TestBase(unittest.TestCase):
    expected_value = None

    def setUp(self):
        self.db = redis.Redis(db=3)

    def call_formatter(self):
        raise NotImplementedError()

    def get_formatted_output(self, base64_value):
        sys.stdin = io.StringIO(base64_value)
        stdout = sys.stdout
        sys.stdout = io.StringIO()

        self.call_formatter()

        sys.stdin = sys.__stdin__
        output = sys.stdout.getvalue()
        sys.stdout = stdout
        return output

    def check_formatting(self, value):
        base64_value = base64.b64encode(value).decode()
        json_output = self.get_formatted_output(base64_value)
        output = json.loads(json_output)
        formatted_value = output.get('output', '')

        if hasattr(self.expected_value, 'decode'):
            self.expected_value = self.expected_value.decode()

        self.assertEqual(formatted_value, self.expected_value,
                         'Unexpected output: {}'.format(formatted_value))

class TestPythonTimestampFormatter(TestBase):
    formatter = import_module('python-timestamp.formatter')
    value = "1651032779850"
    expected_value = "2022-04-27 12:12:59.850000"

    def call_formatter(self):
        self.formatter.TimestampFormatter().main('decode')

    def test_timestamp_formatter(self):
        key = 'format_demo_timestamp'
        self.db.set(key, self.value)
        self.check_formatting(self.db.get(key))

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


# -*- coding: utf-8 -*-
"""
Created on Thu Aug 22 23:51:49 2019

@author: DG
"""
#import necessary files
from face_detection import face_detector as fd
import cv2
from keras.models import load_model
import numpy as np

#Function for loading models
def load():
    emotions = load_model('emotions.h5')
    age = load_model('age.h5')
    ethnicity = load_model('ethnicity.h5')
    gender = load_model('gender.h5')
    return emotions, age, ethnicity, gender

#Fucntion for predicting given image or frame and return corresponding classes
def test(e,a,eth,g,img):
    emo_list = ['Angry','Happy','Neutral','Sad']
    age_list = ['20-30','30-40','40-50','Above_50','Below_20']
    eth_list = ['Arab','Asian','Black','Hispanic','Indian','White']
    gender_list = ['Male','Female']

    img = cv2.resize(img,(75,75))
    img = np.array(img)
    img = img.reshape((1,75,75,3))
    img = img/255
    pred_e = e.predict(img) #predicting emotion from image file
    pred_a = a.predict(img) #predicting age from image file
    pred_eth = eth.predict(img) #predicting ethnicity from image file
    pred_g = g.predict(img) #predicting gender from image file
    idx_e = np.argmax(pred_e) #Getting predicted emotion
    idx_a = np.argmax(pred_a) #Getting predicted age
    idx_eth = np.argmax(pred_eth) #Getting predicted ethnicity
    idx_g = np.argmax(pred_g) #Getting predicted gender
    class_names = 'emotoins:'+emo_list[idx_e]+' age:'+age_list[idx_a]+' ethnicity:'+eth_list[idx_eth]+' gender:'+gender_list[idx_g]
    return class_names

 #Two optins predicting from image or real time(webcam)
print("Choose a Option:\n 1.Predict from Image \n 2.Predict from Webcam")
opt = int(input())
image_name = None

if(opt==1):
    print('Enter image name:')
    image_name = input()
    img = cv2.imread(image_name,cv2.IMREAD_COLOR)
    faces = fd(img)#find face by using face detector function of face detection file
    if len(faces)==0:
        exit()
    e,a,eth,g = load()#loading models
    print('Model Load Successfully')

    #predicting and drawing rectangle of each face in image
    for (startX, startY, endX, endY) in faces:
        d = img[startY:endY,startX:endX]
        text = test(e,a,eth,g,d)
        print(text)
        y= startY - 10 if startY - 10 > 10 else startY + 10
        cv2.rectangle(img, (startX, startY), (endX, endY),
        			(0, 0, 255), 3)#drawing rectangle in image on face
        cv2.putText(img, text, (startX, y),
                			cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255), 2)#write text on rectangle
    cv2.imwrite('output.jpg',img)
    cv2.imshow("Output", cv2.resize(img,(500,500)))
    cv2.waitKey(0)
    cv2.destroyAllWindows()
elif(opt==2):#if second option selected
    camera = cv2.VideoCapture(0)#capture from webcam
    e,a,eth,g = load()#loading models
    print('Model Load Successfully')
    while True:
        ret, frame = camera.read()#reading frame from webcam
        if ret==True:
            faces = fd(frame)#detecting faces from corresponding frames
            if len(faces)==0:#if face not detect in current frame then skip the iteration
                continue
            #predicting and drawing rectangle of each face in image
            for (startX, startY, endX, endY) in faces:
                d = frame[startY:endY,startX:endX]
                text = test(e,a,eth,g,d)

                print(text)
                y= startY - 10 if startY - 10 > 10 else startY + 10
                cv2.rectangle(frame, (startX, startY), (endX, endY),
                			(0, 0, 255), 3)
                cv2.putText(frame, text, (startX, y),
                			cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255), 2)

            cv2.imshow("Output", cv2.resize(frame,(500,500)))
            k = cv2.waitKey(33)
            if k==27:#Close the window by pressing ESC key
                break
    camera.release()
    cv2.destroyAllWindows()
else:
    print('Enter Valid Input')


#web Scraping
from bs4 import *
with open('C:\\Users\\SAI RAGHAVENDRA\\Desktop\\page.html','r') as html_file:
    #read the html file
    page = html_file.read()
    #create instance for BeautifulSoup to parse document
    soup = BeautifulSoup(page,'html.parser')
    #look for tag p and print each review
    for p in soup.find_all('p'):
        print(p.get_text())

#Sentiment Analysis
from textblob import *
positive,negative = 0,0
for p in soup.find_all('p'):
    text = p.get_text()
    sentiment = TextBlob(text).sentiment.polarity
    if sentiment >= 0:
        positive+=1
    else:
        negative+=1
print("Positive review: %d" %(positive))
print("Negative review: %d" %(negative))


# heat_control_nd.py solves the controllability problem
# for the linear heat equation
# y_t = \Delta y in (0,1)^d x (0,T)
# y(x, 0) = norm(x)^2/d
# y(x, t) = u(x,t)      on the whole boundary
# y(x, 1) = norm(x)^2/d + 2

import matplotlib.pyplot as plt
import numpy as np

import deepxde as dde

import pathlib
import os

OUTPUT_DIRECTORY = pathlib.Path.cwd() / "results" / "heat_control_nd"
if not OUTPUT_DIRECTORY.exists():
    os.makedirs(OUTPUT_DIRECTORY, exist_ok=True)

dim = 1

def pde(x, y): # heat equation
    dy_t = dde.grad.jacobian(y, x, i=0, j=dim)
    laplacian = 0

    for k in range(dim):
        laplacian = laplacian + dde.grad.hessian(y, x, i=0, j=0)

    return dy_t - laplacian

def initial_temp(x): # initial temperature
    ini_tem = [np.linalg.norm(x[0, 0:dim]) ** 2]

    for k in range(1, len(x)):
        ini_tem = np.append(ini_tem, np.linalg.norm(x[k, 0:dim]) ** 2)

    return ini_tem.reshape(len(x), 1) / dim

def exact_solution(x): # exact solution
    ex_sol = [np.linalg.norm(x[0, 0:dim]) ** 2 / dim + 2 * x[0, dim:(dim+1)]]

    for k in range(1, len(x)):
        ex_sol = np.append(ex_sol, np.linalg.norm(x[k, 0:dim]) ** 2 / dim + 2 * x[k, dim:(dim+1)])

    return ex_sol.reshape(len(x), 1)

def final_temp(x): # final temperature
    fin_tem = [np.linalg.norm(x[0, 0:dim]) ** 2 / dim + 2]

    for k in range(1, len(x)):
        fin_tem = np.append(fin_tem, np.linalg.norm(x[k, 0:dim]) ** 2 / dim + 2)

    return fin_tem.reshape(len(x), 1)

def boundary_time0(x, on_boundary): # time t = 0

    return on_boundary and np.isclose(x[dim],0)

def boundary_timeT(x, on_boundary): # time t = T

    return on_boundary and np.isclose(x[dim], 1)

x_min = [0]
x_min *= dim+1
x_max = [1]
x_max *= dim+1

geom = dde.geometry.geometry_nd.Hypercube(xmin=x_min, xmax=x_max)

b_time0 = dde.DirichletBC(geom, initial_temp, boundary_time0)
b_timeT = dde.DirichletBC(geom, final_temp, boundary_timeT)

data = dde.data.PDE(
    geom,
    pde,
    [b_time0, b_timeT],
    num_domain=20000,
    num_boundary=3000
    )

net = dde.maps.FNN([dim+1] + [50] * 4 + [1], "tanh", "Glorot uniform")

model = dde.Model(data, net)

model.compile("adam", lr=0.001)

model.train(epochs=20000)

model.compile("L-BFGS-B")

losshistory, train_state = model.train()

dde.saveplot(
    losshistory, train_state, issave=True, isplot=True, output_dir=OUTPUT_DIRECTORY
)

#Post-processing: error analysis

X = geom.random_points(1000)
y_true = exact_solution(X)
y_pred = model.predict(X)

print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred))


from flask_restful import Resource
from content_creation import flask_request_response
from content_creation_db.protos import request_pb2
from content_creation_db.api_queries.my_content import \
    get_my_contents_query_response
from google.protobuf import json_format

POST_REQUEST = 'POST'
MY_CONTENT_API = '/my-content'

class MyContent(Resource):
    """Get Content created by user."""

    def post(self):
        """Create new assessment."""
        msg_request, err_msg = flask_request_response.message_request(
            request_pb2.UserContentRequest, MY_CONTENT_API, POST_REQUEST
        )
        if err_msg is not None:
            return flask_request_response.error_response(
                err_msg["err_message"], MY_CONTENT_API, POST_REQUEST
            )
        try:
            json_request = json_format.MessageToDict(
                msg_request, preserving_proto_field_name=True)
            query_response = get_my_contents_query_response(
                json_request['user_id'])
            return query_response
        except Exception as err:
            return flask_request_response.error_response(
                str(err), MY_CONTENT_API, POST_REQUEST
            )


"""Tests for utils/gan/spectral.py."""

import pytest_check as check
import torch

from gan_facies.gan.spectral import SpectralNorm, l2normalize

def test_l2normalize() -> None:
    """Test l2normalize."""
    vect = torch.rand(size=(10,), dtype=torch.float32)
    l2normalize(vect)

def test_spectral_norm() -> None:
    """Test SpectralNorm."""
    module = torch.nn.Conv2d(3, 8, kernel_size=3)
    weights = module.weight.data
    spec_norm = SpectralNorm(module, weight_name='weight')
    # pylint: disable=protected-access
    check.is_true(spec_norm._are_made_params())
    check.is_true(hasattr(module, 'weight_u'))
    check.is_true(hasattr(module, 'weight_v'))
    check.is_true(hasattr(module, 'weight_bar'))
    spec_norm(torch.rand(size=(1, 3, 32, 32)))
    new_weights = module.weight.data
    check.is_false(torch.allclose(weights, new_weights))


import numpy as np
import cv2

def translate(image, x, y):
    h,w = image.shape[:2]
    M = np.float32([[1,0,x],
                    [0,1,y]])
    shifted = cv2.warpAffine(image, M, (w,h))

    return shifted

def rotate(image, angle, center=None, scale=1.0):
    h,w = image.shape[:2]

    if center is None:
        center = (w//2,h//2)

    M = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, M, (w,h))

    return rotated

def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
    h,w = image.shape[:2]
    dim = None

    if width is None and height is None:
        return image

    if width is None:
        ratio = height/float(h)
        width = int(w * ratio)

    elif height is None:
        ratio = width/float(w)
        height = int(h * ratio)

    dim = (width,height)
    resized = cv2.resize(image, dim, interpolation=inter)

    return resized


# -*- coding=utf-8 -*-

from __future__ import absolute_import, print_function, unicode_literals

import itertools

"""
Write a function that checks whether any permutation of an input string is a palindrome. For example,
1. "civic" should return True
2. "livci" should return False
3. "icvci" should return True
4. "civil" should return False
"""

def is_palindrome(input_str):

    for i in range((len(input_str) + 1)/2):
        if input_str[i] != input_str[-i-1]:
            return False
    return True

def is_palindrome_1(input_str):
    return input_str == input_str[::-1]

if __name__ == "__main__":

    word = "civic"

    for w in itertools.permutations(word):
        word = "".join(w)
        print("Is %s palindrome using is_palindrom function? %s"  %(word, is_palindrome(word)))
        print("Is %s palindrome using is_Palindrom_1 function? %s"  %(word, is_palindrome_1(word)))


# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np
import pandas as pd
from scipy.stats.kde import gaussian_kde
from scipy.stats import norm
from sklearn.neighbors import NearestNeighbors
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDRegressor
import sklearn.metrics as sm
import sys

# In[3]:

input_path = sys.argv[1]
output_path = sys.argv[2]

print("Reading input files from :")
print(input_path)
zone = input_path.split('/')
zone = zone[len(zone)-1]

# In[4]:

df_flow = pd.read_csv(input_path + "/flow.tsv" ,delimiter= "\t",header = None)
df_speed = pd.read_csv(input_path +"/speed.tsv",delimiter= "\t",header = None)
df_occupancy = pd.read_csv(input_path +"/occupancy.tsv",delimiter= "\t",header = None)
df_timestamp = pd.read_csv(input_path +"/timestamp.tsv",delimiter= "\t",header = None)
df_prob= pd.read_csv(input_path +"/prob.tsv",delimiter= "\t",header = None)
df_lanes = pd.read_table(input_path +"/lanes.txt",delimiter= "\t",header = None)

# In[6]:

df_timestamp['timestamp_date'], df_timestamp['timestamp_time'] = df_timestamp[0].str.split('T', 1).str
df_timestamp['Hour'] ,_= df_timestamp['timestamp_time'].str.split(':', 1).str

# In[7]:

lanes = str(df_lanes[0].values[0]).split(' ')
num_lanes = len(lanes)
lanes = list(range(0, num_lanes))

# In[9]:

predicted_flows = pd.DataFrame()

# In[11]:

for lane in lanes :

    print("processing lane "+str(lane))

    remaining_lanes = list(set(lanes).difference([lane]))
    train_sample=pd.DataFrame()
    for rem in remaining_lanes:
        train_sample[str(rem)]= df_flow[rem]

    train_sample['speed']= df_speed[0]
    train_sample['occupancy']= df_occupancy[0]
    train_sample['Hour']= df_timestamp['Hour']
    train_sample['Prob']= df_prob[0]
    train_sample['FinalFlow'] = df_flow[lane]

    Missing_sample =  pd.DataFrame()
    if (train_sample['FinalFlow'].isnull().values.any()):
        train_sample['FinalFlow'] = train_sample['FinalFlow'].replace(np.nan,' ',regex=True)
        Missing_sample = train_sample[train_sample['FinalFlow'] == ' ']
        train_sample['FinalFlow'] = train_sample['FinalFlow'].replace(' ',np.nan,regex=True)

    train_sample=train_sample.fillna(-1)

    corrupt1_sample = pd.DataFrame()
    corrupt2_sample = pd.DataFrame()
    corrupt3_sample = pd.DataFrame()
    corrupt4_sample = pd.DataFrame()
    corrupt5_sample = pd.DataFrame()

    for rem in remaining_lanes:
        corrupt1_sample = pd.concat([train_sample[train_sample[str(rem)]<0],corrupt1_sample])
        train_sample = train_sample[train_sample[str(rem)]>= 0]

    corrupt2_sample = train_sample[train_sample['speed']<0]
    train_sample = train_sample[train_sample['speed']>= 0]

    corrupt3_sample = pd.DataFrame(train_sample[train_sample['FinalFlow']<0])
    train_sample = train_sample[train_sample['FinalFlow']>=0]

    corrupt4_sample = pd.DataFrame(train_sample[train_sample['occupancy']<0])
    train_sample = train_sample[train_sample['occupancy']>=0]

    corrupt5_sample = pd.DataFrame(train_sample[train_sample['Prob']<=0])
    train_sample = train_sample[train_sample['Prob']>0]

    corrupt_sample = pd.DataFrame()
    corrupt_sample = pd.concat([corrupt1_sample,corrupt2_sample,corrupt3_sample,corrupt4_sample,corrupt5_sample])

    grouped = train_sample.groupby([train_sample['Hour']]).mean()
    for i in range(0,24):
       for rem in remaining_lanes:
            corrupt_sample.ix[corrupt_sample[str(rem)]<0,str(rem)] = grouped.ix[i,str(rem)]

       corrupt_sample.ix[corrupt_sample['speed']<0,'speed'] = grouped.ix[i,'speed']
       corrupt_sample.ix[corrupt_sample['occupancy']<0,'occupancy'] = grouped.ix[i,'occupancy']
       corrupt_sample.ix[corrupt_sample['Prob']<0,'Prob'] = grouped.ix[i,'Prob']

    train_sample_labels= train_sample['FinalFlow']
    corrupt_sample_labels= corrupt_sample['FinalFlow']

    train_sample = train_sample.iloc[:,0:len(remaining_lanes)+4]
    corrupt_sample= corrupt_sample.iloc[:,0:len(remaining_lanes)+4]

    model = SGDRegressor(eta0=0.00005).fit(train_sample, train_sample_labels)
    complete = pd.concat([train_sample,corrupt_sample])
    complete.sort_index(inplace=True)

    predictions = model.predict(complete)

    predicted_flows[str(lane)] = predictions
    if(len(Missing_sample)>0):
        predicted_flows.ix[Missing_sample.index,str(lane)] = np.nan

# In[ ]:

filename=zone + ".flow.txt"
print("Task Completed")
print("Writing output file to:")
print(output_path)

predicted_flows.to_csv(output_path+"/"+filename,sep = "\t",index=False,header=False)

# In[31]:

# In[ ]:

# In[ ]:


import json
def Perm(arrs): #函式
   if (len(arrs))==1: #如果长度为1
      return [arrs] #返回
   result = []  #给一个list储存所有结果
   for j in range(len(arrs)): #循环
      rest_arrs = arrs[:j]+arrs[j+1:] #取得i之外的
      rest_lists = Perm(rest_arrs)  #其他继续排
      lists = [] #list
      for term in rest_lists:
         lists.append(arrs[j:j+1]+term) #找出组合
      result += lists #把这个组合加到所有可能组合的list
   return result

def BF(input): #函式
   N = len(input) #几份工作/人
   who=[] #list
   a=0
   for i in range(N): #循环几次
      who.append(a)
      a+=1 #用0.1.2.3...数字当人的代号
   result=Perm(who) #带入函式找出人的排列组合

   time = [] #list
   for per in result: # 对每个排列组合
      temp_cost = 0 #总和先设为0
      for i in range(len(per)): # 对每个组合中的人
         current_job=i # 取得工作
         current_people = per[i]  # 取得人
         current_cost=input[current_job][current_people]  # 取得某人做某事的cost
         temp_cost += current_cost #每个人时间加总
      time.append(temp_cost) #列出全部组合时间
   cost = min(time) #找出最小时间
   assignment = result[time.index(min(time))] #找出式哪个组合
   return assignment, cost

# main
with open('input.json', 'r') as inputFile: #打开档案
   data = json.load(inputFile) # load data
   for key in data:
      input = data[key] # load each input

      assignment, cost = BF(input)
      print('Question: ' + str(key)) #输出
      print('Assignment:', assignment) #输出
      print('Cost:', cost) #输出
      print()


import numpy as np

class Fuzzification:
    def __init__(self, row, mean, sd):
        self.row = row #each row with multiple features
        self.mean = mean #means from dynamic clustering for each features
        self.sd = sd #sds from dynamic clustering for each features

    def normal_density(self, x, mean, sd):
        if sd == 0:
            if mean == x:
                return 1
            else:
                return 0
        return (1/(sd*(2*np.pi))**(0.5))* np.exp(-0.5*((x-mean)/sd)**(2))

    def fuzzify(self):
        if len(self.row) != len(self.mean):
            raise Exception('Need means for each feature')
        if len(self.row) != len(self.sd):
            raise Exception('Need sd for each feature')
        fuzzy_output = []
        for i in range(len(self.row)):
            clusters = len(self.mean[i])
            output = np.zeros((clusters))
            densities = []
            for j in range(clusters):
                density = self.normal_density(self.row[i], self.mean[i][j], self.sd[i][j])
                densities.append(density)

            index = densities.index(max(densities))
            output[index] = 1
            output = output.tolist()
            output = [int(x) for x in output]
            fuzzy_output.append(output)
            #fuzzy_output.append()
        return fuzzy_output


#!/usr/bin/python3
"""Function to get all MagicMirror² modules."""

import json
import shutil
import subprocess
from pathlib import Path

def get_modules():
    """Function to get all the modules per git."""

    module_counter = 0
    # For testing set this to a lower number to test only a few meodules
    max_module_counter = 99999

    modules_json_file = open("./docs/modules.temp.1.json", encoding="utf-8")
    modules = json.load(modules_json_file)

    for module in modules:
        if module_counter < max_module_counter:
            module_counter += 1
            module_name = module["name"]
            module_url = module["url"]
            module_owner = module_url.split("/")[3]
            path = Path(f"./modules_temp/{module_name}-----{module_owner}")

            print(
                f"\n+++   {module_counter:4}: {module_name} by {module_owner} - {module_url:4}"
                # f"\n      {module_description}"
            )

            if path.exists():
                print("- I - path already exists: run `git pull`")
                subprocess.run(
                    f"cd {path} && git pull && cd ..", shell=True, check=False
                )
            else:
                print("- I - path doesn't exists: run `git clone`")
                subprocess.run(
                    f"git clone {module_url} {path} --depth 1", shell=True, check=False
                )

            # Move module from temp directory to working directory
            shutil.move(path, f"./modules/{module_name}-----{module_owner}")

    print("\n- I - Modules found and downloaded: " + str(module_counter) + "\n")

def rename_modules_directory():
    """
    Deletes the directory "modules_temp" and renames the directory "modules" to "modules_temp".
    We need this so that we don't have to download all the git repositories every time.
    With rename process we get rid of old modules that have been removed from the list.
    """
    temp_path = Path("./modules_temp")
    modules_path = Path("./modules")

    if modules_path.exists():
        # Delete the directory "modules_temp" if it exists
        try:
            shutil.rmtree(str(temp_path))
        except FileNotFoundError:
            pass
        # Rename the directory "modules" to "modules_temp"
        shutil.move(str(modules_path), str(temp_path))

rename_modules_directory()

get_modules()


"""
cnet.py (see network)

Check to see if you are connected to the internet and make sure your ip address
is in the set ip_whitelist.

(Originally designed for Linux Mint MATE v19.1.)
"""

import socket
import subprocess
import time
import logging

ip_whitelist = set()
#ip_whitelist.add('')

def internet(host = '8.8.8.8', port = 53, timeout = 3) -> bool:
    """
    Check if I have an internet connection.

    Host: 8.8.8.8 (google-public-dns-a.google.com)
    OpenPort: 53/tcp
    Service: domain (DNS/TCP)
    """
    try:
        socket.setdefaulttimeout(timeout)
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
        return True
    except socket.error as ex:
        logger.error(ex)
        return False

def my_public_ip():
    """
    Get my public IP using ifconfig. If unable, then return 0.0.0.0.

    When you call subprocess.check_output, it prints stuff to term. If you use
    shell=True it will disable these logs, but for some reason it doesn't
    recognize the curl command.
    """
    try:
        pipe_out = subprocess.check_output('curl ifconfig.me'.split(), timeout=5)
        return pipe_out.decode('utf8')
    except Exception as e:
        logger.error("Failed to determine public IP address.")
        logger.error(e)
        return "0.0.0.0"

def restart_connection():
    """
    Restart network manager.

    Sleep for 5 seconds to give time for restart.
    """
    cmd = 'sudo service network-manager restart'
    logger.info("Restarting network manager..."); logger.info(cmd)
    subprocess.call(cmd.split())
    time.sleep(5)

"""
Set up logger
"""
log_format = '%(levelname)s %(asctime)s - %(message)s'
logging.basicConfig(filename = '/home/ryan/Downloads/cnet.log',
    level = logging.DEBUG,
    format = log_format)
logger = logging.getLogger()
logger.info('~'*10 + ' cnet.py ' + '~'*10)

"""
Start checking connection
"""
status = 0
logger.info('Now ready to check the internet connection.')
while True:
    if internet():
        tries = 0
        while tries < 3:
            my_ip = my_public_ip()
            tries += 1
            if my_ip != "0.0.0.0": break
        if my_ip not in ip_whitelist:
            logger.warning("IP address not recognized. Attempting a restart.")
            restart_connection()
        else: status = 0
    else:
        if status == 0:
            status = 1
            logger.error("Unable to connect...")
            time.sleep(1)
            logger.error("Trying again...")
        elif status == 1:
            status = 2
            logging.error("Failed to connect twice. Attempting a restart.")
            restart_connection()
        else:
            status = 0
            logging.critical("Failed to connect three times.")
            logging.critical("Waiting 30 seconds and starting over...")
            time.sleep(30)


#Import the required Libraries
import qrcode
import datetime
from tkinter import *
from tkinter import ttk

#Create an instance of Tkinter frame
win= Tk()

#Set the geometry of Tkinter frame
win.geometry("750x250")

#This is where the magic happens
def display_text():
    global entry
    string= entry.get()
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=15,
        border=4,
    )
    qr.add_data(string)
    qr.make(fit=True)

    img = qr.make_image(fill_color="red", back_color="white")
    img.save("url_qrcode_"+datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")+".png")
    label.configure(text=string)

#Initialize a Label to display the User Input
label=Label(win, text="URL", font=("Courier 22 bold"))
label.pack()

#Create an Entry widget to accept User Input
entry= Entry(win, width= 40)
entry.focus_set()
entry.pack()

#Create a Button to validate Entry Widget
ttk.Button(win, text= "Generate", width= 20, command= display_text).pack(pady=20)

win.mainloop()


import datetime

from Domain.add_operation import AddOperation
from Domain.delete_operation import DeleteOperation
from Domain.modify_operation import ModifyOperation
from Domain.rezervare import Rezervare
from Domain.rezervare_validator import RezervareValidator

from Repository.repository import Repository
from Service.undo_redo_service import UndoRedoService

class RezervareService:
    def __init__(self, rezervare_repository: Repository,
                 rezervare_validator: RezervareValidator,
                 undo_redo_service: UndoRedoService = None):
        self.__rezervare_repository = rezervare_repository
        self.__rezervare_validator = rezervare_validator
        self.__undo_redo_service = undo_redo_service

    def get_all(self):
        """
        :return: O lista de rezervari
        """
        return self.__rezervare_repository.read()

    def adauga(self, id_rezervare, id_film_r, id_card_client, data, ora):
        """
        Adauga o rezervare in lista
        """
        rezervare = Rezervare(id_rezervare, id_film_r,
                              id_card_client, data, ora)
        self.__rezervare_validator.valideaza(rezervare)
        self.__rezervare_repository.adauga(rezervare)
        if self.__undo_redo_service is not None:
            self.__undo_redo_service.add_undo_operation(
                AddOperation(self.__rezervare_repository, rezervare))

    def sterge(self, id_entitate):
        """
        Sterge o rezervare din lista
        """
        rezervare = self.__rezervare_repository.read(id_entitate)
        self.__rezervare_repository.sterge(id_entitate)
        if self.__undo_redo_service is not None:
            self.__undo_redo_service. \
                add_undo_operation(DeleteOperation
                                   (self.__rezervare_repository,
                                    rezervare))

    def modifica(self, id_entitate, id_film_r, id_card_client, data, ora):
        """
        Modifica o rezervare din lista
        """
        rezervare_ant = self.__rezervare_repository.read(id_entitate)
        rezervare = Rezervare(id_entitate, id_film_r,
                              id_card_client, data, ora)
        self.__rezervare_validator.valideaza(rezervare)
        self.__rezervare_repository.modifica(rezervare)
        if self.__undo_redo_service is not None:
            self.__undo_redo_service. \
                add_undo_operation(ModifyOperation
                                   (self.__rezervare_repository,
                                    rezervare,
                                    rezervare_ant))

    def raport_a(self, ora1, ora2):
        """
        Returneaza toate rezervarile dintr-un interval orar dat
        """
        rezervari = self.__rezervare_repository.read()
        try:

            interval = ora1 + " - " + ora2
            err = []
            list = interval.split("-")
            st = list[0].split(":")
            dr = list[1].split(":")
            if int(st[0]) < 0 or int(st[0]) > 23 \
                    or int(st[1]) < 0 or int(st[1]) > 59:
                err.append("Ora inceperii este invalida! ")
            if int(dr[0]) < 0 or int(dr[0]) > 23 \
                    or int(dr[1]) < 0 or int(dr[1]) > 59:
                err.append("Ora finala este invalida! ")
            if len(err) > 0:
                raise err
            rezultat = []
            for rezervare in rezervari:
                ora = rezervare.ora.split(":")
                if st[0] < ora[0] < dr[0]:
                    rezultat.append(rezervare)
                elif ora[0] == st[0] and ora[1] >= st[1]:
                    rezultat.append(rezervare)
                elif ora[0] == dr[0] and ora[1] <= dr[1]:
                    rezultat.append(rezervare)
            return rezultat
        except Exception as ex:
            print(ex)

    def stergere_rez_interval(self, data1, data2):
        """
        Sterge rezervari dintr-un interval dat
        """
        string1 = data1.split('.')
        string2 = data2.split('.')
        start = datetime.datetime(int(string1[2]),
                                  int(string1[1]),
                                  int(string1[0]))
        end = datetime.datetime(int(string2[2]),
                                int(string2[1]),
                                int(string2[0]))
        for rezervare in self.__rezervare_repository.read():
            string3 = rezervare.data
            string = string3.split('.')
            data = datetime.datetime(int(string[2]),
                                     int(string[1]),
                                     int(string[0]))
            if start <= data <= end:
                self.__rezervare_repository.sterge(rezervare.id_entitate)


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 17 15:54:02 2016

@author: ogurcan
"""
import numpy as np
import h5py as h5
from scipy.integrate import ode
import time
#import sys
#sys.path.append('../../')
import npfun as nf
from scipy.stats import norm

# wheter the zeroth shell is an icosahedron.
# i.e. sh=1 means icosahedron, sh=0 means dodecahedron.

sh0=1
flname="nsout.h5"     # filename for output (and input if we continue).
wecontinue=False            # do we continue from an existing file.
t0=0.0;                     # t0 will come from the file if we continue.
t1=500.0;                  # tmax.
dt=0.1;                     # time step for output.
h=1e-3;
Nmax=60;                    # number of shells.
k0=1.0;                     # beginning of the wavenumber range.
nu=1.0e-9                   # kinematic viscosity.
Prm=1.0 	            # magnetic Prandtl number
# list of links for an icosahedron
links_ico=np.array([
    [[[10,10],[11,11],[7,12],[8,13],[9,14]],[[5,10],[6,11],[7,12],[8,13],[9,14]],[[15,10],[16,11],[17,7],[18,8],[19,9]]],
    [[[3,10],[4,14],[11,15],[6,7],[8,19]],[[1,10],[3,14],[18,15],[12,7],[16,19]],[[11,3],[13,4],[8,11],[2,6],[6,8]]],
    [[[5,10],[4,11],[9,15],[7,16],[6,8]],[[4,10],[2,11],[17,15],[19,16],[13,8]],[[14,5],[12,4],[7,9],[9,7],[3,6]]],
    [[[1,11],[5,12],[10,16],[8,17],[6,9]],[[0,11],[3,12],[18,16],[15,17],[14,9]],[[10,1],[13,5],[8,10],[5,8],[4,6]]],
    [[[6,5],[1,13],[2,12],[9,18],[11,17]],[[10,5],[4,13],[1,12],[16,18],[19,17]],[[0,6],[14,1],[11,2],[6,9],[9,11]]],
    [[[6,6],[7,18],[2,14],[3,13],[10,19]],[[11,6],[15,18],[0,14],[2,13],[17,19]],[[1,6],[5,7],[10,2],[12,3],[7,10]]]])

# list of links for a dodecahedron
links_dod=np.array([
    [[[15,6],[11,7],[14,8]],[[4,6],[9,7],[11,8]],[[10,15],[3,11],[5,14]]],
    [[[16,6],[12,8],[10,9]],[[5,6],[10,8],[7,9]],[[11,16],[4,12],[1,10]]],
    [[[17,6],[13,9],[11,10]],[[1,6],[11,9],[8,10]],[[7,17],[5,13],[2,11]]],
    [[[18,6],[14,10],[12,11]],[[2,6],[7,10],[9,11]],[[8,18],[1,14],[3,12]]],
    [[[19,6],[13,7],[10,11]],[[3,6],[10,7],[8,11]],[[9,19],[4,13],[2,10]]],
    [[[8,7],[7,8],[10,4]],[[5,7],[3,8],[6,4]],[[11,8],[9,7],[0,10]]],
    [[[9,8],[8,9],[11,5]],[[1,8],[4,9],[6,5]],[[7,9],[10,8],[0,11]]],
    [[[12,1],[5,9],[9,10]],[[6,1],[2,9],[5,10]],[[0,12],[8,5],[11,9]]],
    [[[13,2],[6,10],[5,11]],[[6,2],[3,10],[1,11]],[[0,13],[9,6],[7,5]]],
    [[[6,7],[14,3],[7,11]],[[2,7],[6,3],[4,11]],[[8,6],[0,14],[10,7]]]])

#spherical positions of points of an icosahedron.
def icosp():
    theta=np.hstack([0, np.tile(np.pi/2-np.arctan(1/2),5),np.pi,np.tile(np.pi/2+np.arctan(1/2),5)]);
    phi=np.hstack([0,np.arange(0,2*np.pi,2*np.pi/5),0,np.mod(np.arange(0,2*np.pi,2*np.pi/5)+np.pi,2*np.pi)]);
    return theta,phi;

#spherical positions of points of a dodecahedron.
def dodsp():
    ph=(1+np.sqrt(5))/2;
    alp=np.array([np.arcsin(ph/np.sqrt(3))-np.arccos(ph/np.sqrt(ph+2)),np.arctan(2*ph**2)]);
    eta=np.arange(np.pi/5,2*np.pi,2*np.pi/5);
    theta=np.reshape(np.transpose(np.reshape(np.transpose(np.tile(np.hstack([alp,np.pi-alp]),[5,1])),[20,1])),20);
    phi=np.reshape(np.hstack([np.tile(eta,[1,2]),np.tile(eta-np.pi,[1,2])]),20);
    return theta,phi;

#the base node number for the nth shell
def nbase(n):
    return (n%2)*2*int(2*(0.5-sh0))+n*8

#size of the polyhedron, given its base node number
def sz4base(n):
    return (8-4*(0.5-(n+(1-sh0)*10)%16/nbase(1))).astype(int)

#get the list of (n',l') and (n'',l'') pairs for a given (n,l)
def get_nls(n,l):
    if(n%2==sh0):
        nlp=np.zeros((2,9,2),dtype=int)
        nlp[0,0:3,0]=n-2
        nlp[0,0:3,1]=n-1
        nlp[0,3:6,0]=n-1
        nlp[0,3:6,1]=n+1
        nlp[0,6:9,0]=n+1
        nlp[0,6:9,1]=n+2
        nlp[1,:,:]=links_dod[l,:,:,:].reshape(nlp[1,:,:].shape)
    else:
        nlp=np.zeros((2,15,2),dtype=int)
        nlp[0,0:5,0]=n-2
        nlp[0,0:5,1]=n-1
        nlp[0,5:10,0]=n-1
        nlp[0,5:10,1]=n+1
        nlp[0,10:15,0]=n+1
        nlp[0,10:15,1]=n+2
        nlp[1,:,:]=links_ico[l,:,:,:].reshape(nlp[1,:,:].shape)
    return nlp

# get the list of node pairs that interact with the node (n,l) and
# wheter or not  they are conjugated (i.e. cp and cpp)
def get_interacting_nodes(n,l):
    npl=get_nls(n,l)
    nnp=nbase(npl[0,:,0])
    lp=npl[1,:,0]
    nnpp=nbase(npl[0,:,1])
    lpp=npl[1,:,1]
    szp=sz4base(nnp)
    szpp=sz4base(nnpp)
    cp=((lp-szp)>=0)
    cpp=((lpp-szpp)>=0)
    lp=lp-cp.astype(int)*szp
    lpp=lpp-cpp.astype(int)*szpp
    nlp=nnp+lp
    nlpp=nnpp+lpp
    cp=~cp
    cpp=~cpp
    return nlp,nlpp,cp,cpp

#number of nodes
NLmax=np.floor((Nmax/2)).astype(int)*2*8+(Nmax%2)*(10-((Nmax+1+sh0)%2)*4)
g=np.sqrt((1+np.sqrt(5))/2)
lam=np.sqrt(np.sqrt(5)/3)
th,ph=dodsp()
khatd=np.array([np.sin(th)*np.cos(ph),np.sin(th)*np.sin(ph),np.cos(th)]).T
th,ph=icosp()
khati=np.array([np.sin(th)*np.cos(ph),np.sin(th)*np.sin(ph),np.cos(th)]).T
k=np.zeros((NLmax,3));
links=-np.ones((NLmax,15,2),dtype=int)
clinks=-np.ones((NLmax,15,2),dtype=int)
#constructing the network: the nodes and the links
for n in np.arange(0,Nmax):
    if(n%2==sh0):
        k[nbase(n):nbase(n+1),:]=khatd[0:10,:]*g**n
    else:
        k[nbase(n):nbase(n+1),:]=khati[0:6,:]*g**n*lam
    for l in np.arange(0,10-(n+sh0)%2*4):
        nl=nbase(n)+l
        nlp,nlpp,cp,cpp=get_interacting_nodes(n,l)
        inds=np.nonzero((nlp>=0) & (nlpp>=0) & (nlp<NLmax) & (nlpp<NLmax))
        links[nl,0:inds[0].shape[0],0]=nlp[inds]
        links[nl,0:inds[0].shape[0],1]=nlpp[inds]
        clinks[nl,0:inds[0].shape[0],0]=cp[inds]
        clinks[nl,0:inds[0].shape[0],1]=cpp[inds]

#initializing u and b
kn=np.sqrt(np.sum(k**2,1))
if(wecontinue==True):
    #setting up the output hdf5 file
    fl=h5.File(flname,"r")
    u=fl["fields/u"].value
    b=fl["fields/b"].value
    tt=fl["fields/t"].value
    fl.close()
    t0=tt[-1]
    unls=u[-1,:,:]
    bnls=b[-1,:,:]
else:
    unls=np.zeros((NLmax,3),dtype=complex)
    bnls=np.zeros((NLmax,3),dtype=complex)
    unls[:,:]=(0.0001+0.0001j)*1e-12
    bnls[:,:]=(0.0001+0.0001j)*1e-12
    #making sure u is divergence free
    unls=unls*np.exp(1j*np.random.rand(unls.shape[0],unls.shape[1])*2*np.pi)
    unls=unls-((np.einsum("ij,ij->i",k,unls)*k.T)/kn**2).T
    bnls=bnls*np.exp(1j*np.random.rand(unls.shape[0],unls.shape[1])*2*np.pi)
    bnls=bnls-((np.einsum("ij,ij->i",k,bnls)*k.T)/kn**2).T
#setting up the interaction coefficient

vnls=np.stack((unls,bnls),axis=2)
vnls=np.array(vnls,order='F')

Mkpqu=np.einsum("lk,ij->lijk",k,np.eye(3))+np.einsum("lj,ik->lijk",k,np.eye(3))-2*np.einsum("li,lj,lk,l->lijk",k,k,k,1/kn**2)
Mkpqb=np.einsum("lk,ij->lijk",k,np.eye(3))-np.einsum("lj,ik->lijk",k,np.eye(3))
Mkpq=np.stack((Mkpqu,Mkpqb),axis=4)
Mkpq=np.array(Mkpq,order='F')

#uu0=np.vstack((unls,bnls)).reshape(2*unls.shape[0]*unls.shape[1]);
#dudt=np.zeros(np.shape(unls),dtype=unls.dtype);
#dbdt=np.zeros(np.shape(bnls),dtype=bnls.dtype);

#defining the form of hyperviscosity
Dnu=nu*kn**2
Dnb=nu*kn**2/Prm
Dn=np.stack((Dnu,Dnb),axis=1)
Dn=np.array(Dn,order='F')

#setting up the forcing
frng=np.arange(nbase(4),nbase(6))
Fnu=np.zeros((NLmax,3),dtype=unls.dtype);
Fnu[frng,:]=1e-2
Fnu=Fnu-((np.einsum("ij,ij->i",k,Fnu)*k.T)/kn**2).T
Fnb=Fnu*0.0
Fn=np.stack((Fnu,Fnb),axis=2)
Fn=np.array(Fn,order='F')

#define cj as "conjugate of y if x is True"
cj=lambda x,y: np.conj(y) if x else y
cj=np.vectorize(cj)

# swittching to fortran indexing
links[links>=0]=links[links>=0]+1
links=np.array(links,dtype='int32',order='F')
clinks=np.array(clinks,dtype='int32',order='F')

# random

def force_update():
    xi=norm.rvs(size=(frng.shape[0],3))
    Fn[frng,:,0]=xi
    Fn[:,:,0]=Fn[:,:,0]-((np.einsum("ij,ij->i",k,Fn[:,:,0])*k.T)/kn**2).T
#the equation
def func(t,y):
    u=y.view(dtype=complex).reshape((NLmax,3,2),order='F');
    dudt=nf.fnp(u,Fn,Dn,Mkpq,links,clinks)
    return dudt.ravel(order='F').view(dtype=float)

#setting up the output hdf5 file
fl=h5.File(flname,"w")
grp=fl.create_group("fields")
grp.create_dataset("k",data=k)
if(wecontinue==True):
    i=u.shape[0]
    ures=grp.create_dataset("u",(i,NLmax,3),maxshape=(None,NLmax,3),dtype=complex)
    bres=grp.create_dataset("b",(i,NLmax,3),maxshape=(None,NLmax,3),dtype=complex)
    tres=grp.create_dataset("t",(i,),maxshape=(None,),dtype=float)
    ures[:,:,:]=u;
    bres[:,:,:]=b;
    tres[:]=tt;
else:
    i=0;
    ures=grp.create_dataset("u",(1,NLmax,3),maxshape=(None,NLmax,3),dtype=complex)
    bres=grp.create_dataset("b",(1,NLmax,3),maxshape=(None,NLmax,3),dtype=complex)
    tres=grp.create_dataset("t",(1,),maxshape=(None,),dtype=float)

#initializing the ode solver
r = ode(func, 0).set_integrator('dopri5',nsteps=1E6)
r.set_initial_value(vnls.ravel(order='F').view(dtype=float), t0)
#r.set_solout(force_update)
#the main loop where the solution is obtained and written to the hdf5 file:
ct=time.time()
epst=1e-12
while r.successful() and r.t < t1:
    print("t=",r.t);
    told=r.t
    while r.t<told+dt-epst:
        r.integrate(r.t+h)
        force_update()
    v=r.y.view(dtype=complex).reshape((NLmax,3,2),order='F');
    ures.resize((i+1,NLmax,3))
    bres.resize((i+1,NLmax,3))
    tres.resize((i+1,))
    ures[i,:,:]=v[:,:,0]
    bres[i,:,:]=v[:,:,1]
    tres[i]=r.t
    fl.flush()
    i=i+1;
    print(time.time()-ct,"seconds elapsed.")
fl.close()


import json

from django.http import JsonResponse, HttpResponse
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import ListView, CreateView, UpdateView, DeleteView, TemplateView

from core.security.forms import ModuleTypeForm
from core.security.mixins import PermissionMixin
from core.security.models import *

class TypeListView(PermissionMixin, ListView):
    model = ModuleType
    template_name = 'moduletype/list.html'
    permission_required = 'view_moduletype'

    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['create_url'] = reverse_lazy('moduletype_create')
        context['title'] = 'Listado de Tipos de Módulos'
        return context

class TypeCreateView(PermissionMixin, CreateView):
    model = ModuleType
    template_name = 'moduletype/create.html'
    form_class = ModuleTypeForm
    success_url = reverse_lazy('moduletype_list')
    permission_required = 'add_moduletype'

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    def validate_data(self):
        data = {'valid': True}
        try:
            type = self.request.POST['type']
            obj = self.request.POST['obj'].strip()
            if type == 'name':
                if ModuleType.objects.filter(name__iexact=obj):
                    data['valid'] = False
            elif type == 'icon':
                if ModuleType.objects.filter(icon__iexact=obj):
                    data['valid'] = False
        except:
            pass
        return JsonResponse(data)

    def post(self, request, *args, **kwargs):
        data = {}
        action = request.POST['action']
        try:
            if action == 'add':
                data = self.get_form().save()
            elif action == 'validate_data':
                return self.validate_data()
            else:
                data['error'] = 'No ha seleccionado ninguna opción'
        except Exception as e:
            data['error'] = str(e)
        return HttpResponse(json.dumps(data), content_type='application/json')

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['list_url'] = self.success_url
        context['title'] = 'Nuevo registro de un Tipo de Módulo'
        context['action'] = 'add'
        return context

class TypeUpdateView(PermissionMixin, UpdateView):
    model = ModuleType
    template_name = 'moduletype/create.html'
    form_class = ModuleTypeForm
    success_url = reverse_lazy('moduletype_list')
    permission_required = 'change_moduletype'

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super().dispatch(request, *args, **kwargs)

    def validate_data(self):
        data = {'valid': True}
        try:
            type = self.request.POST['type']
            id = self.get_object().id
            obj = self.request.POST['obj'].strip()
            if type == 'name':
                if ModuleType.objects.filter(name__iexact=obj).exclude(pk=id):
                    data['valid'] = False
            elif type == 'icon':
                if ModuleType.objects.filter(icon__iexact=obj).exclude(pk=id):
                    data['valid'] = False
        except:
            pass
        return JsonResponse(data)

    def post(self, request, *args, **kwargs):
        data = {}
        action = request.POST['action']
        try:
            if action == 'edit':
                data = self.get_form().save()
            elif action == 'validate_data':
                return self.validate_data()
            else:
                data['error'] = 'No ha seleccionado ninguna opción'
        except Exception as e:
            data['error'] = str(e)
        return HttpResponse(json.dumps(data), content_type='application/json')

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['list_url'] = self.success_url
        context['title'] = 'Edición de un Tipo de Módulo'
        context['action'] = 'edit'
        return context

class TypeSortView(PermissionMixin, TemplateView):
    template_name = 'moduletype/sort.html'
    success_url = reverse_lazy('moduletype_list')
    permission_required = 'change_moduletype'

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        data = {}
        action = request.POST['action']
        try:
            if action == 'sort':
                list_type_mod = []
                list_modules = []
                md_types = json.loads(request.POST['module_types'])
                modules = json.loads(request.POST['modules'])
                for tm in md_types:
                    instance = ModuleType.objects.get(id=tm['id'])
                    instance.position = int(tm['pos'])
                    list_type_mod.append(instance)
                for m in modules:
                    instance_mod = Module.objects.get(id=m['id'])
                    instance_mod.position = int(m['pos'])
                    list_modules.append(instance_mod)
                ModuleType.objects.bulk_update(list_type_mod, ['position'])
                Module.objects.bulk_update(list_modules, ['position'])
            else:
                data['error'] = 'No ha seleccionado ninguna opción'
        except Exception as e:
            data['error'] = str(e)
        return HttpResponse(json.dumps(data), content_type='application/json')

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['list_url'] = self.success_url
        context['title'] = 'Ordenar módulos'
        context['action'] = 'sort'
        context['module_types'] = ModuleType.objects.filter(is_active=True).order_by('position')
        return context

class TypeDeleteView(PermissionMixin, DeleteView):
    model = ModuleType
    template_name = 'moduletype/delete.html'
    success_url = reverse_lazy('moduletype_list')
    permission_required = 'delete_moduletype'

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        data = {}
        try:
            self.get_object().delete()
        except Exception as e:
            data['error'] = str(e)
        return HttpResponse(json.dumps(data), content_type='application/json')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['title'] = 'Notificación de eliminación'
        context['list_url'] = self.success_url
        return context


import sys
''' This reads the reflections in an hkl file with run/frame numbers
and finds the corresponding direction cosines in a raw file. Sadabs appears to
modify the frame #s (by up to 5-10 frames) when outputting hkl files, so
the reflection with matching indices and run # and the closest frame # is
selected. If no match is found within WARN_TOL or FAIL_TOL frames, a warning
is issued or the job fails.

MAJOR CAVEAT: This was written for a specific task. It will fail if there are
more than 2 runs. It is not guaranteed to work under any circumstance. It's
probably better to use this as inspiration. Please contact the author at
dwp2111@columbia.edu for any necessary help adapting this script.

Having said that, the script should typically be called with:

$ python3 add_cosines.py name.raw name.hkl > name.hkl.cosines

'''

WARN_TOL = 10
FAIL_TOL = 40
reflections = dict()

with open(sys.argv[1]) as f:
    rawFile = f.readlines()

for line in rawFile:
    hkl = line[0:12]
    run = line[31]
    frame = float(line[121:127])
    if run == '2': frame += 360
    if hkl not in reflections.keys():
        reflections[hkl] = []
    reflections[hkl].append({'run': run, 'frame': frame, 'body': line[28:]})

def score(run, frame, match):
    return 10000*(match['run']!=run) + abs(frame - match['frame'])

with open(sys.argv[2]) as f:
    hklFile = f.readlines()

for line in hklFile:
    hkl = line[0:12]
    if hkl == '   0   0   0': break
    run = line[31]
    frame = float(line[35:40])
    matches = [(score(run, frame, match), match) for match in reflections[hkl]]
    best = min(matches)
    if best[0] > WARN_TOL:
        sys.stderr.write('Alert: {}'.format(hkl))
    if best[0] > FAIL_TOL:
        sys.stderr.write('Warning: {}'.format(hkl))
        break

    else:
        print(line[0:28] + best[1]['body'], end='')


import re
import tweepy
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import string
from tweepy import OAuthHandler
from textblob import TextBlob

class TwitterClient(object):

    def __init__(self):
        # keys and tokens from the Twitter Dev Console
        consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
        consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
        access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
        access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
        try:
            self.auth = OAuthHandler(consumer_key, consumer_secret)
            self.auth.set_access_token(access_token, access_token_secret)
            self.api = tweepy.API(self.auth)
        except:
            print("Authentication Failed")

    def clean_text(self, text):
        tokens = word_tokenize(text)
        tokens = [w.lower() for w in tokens]
        table = str.maketrans('', '', string.punctuation)
        stripped = [w.translate(table) for w in tokens]
        words = [word for word in stripped if word.isalpha()]
        stop_words = set(stopwords.words('english'))
        words = [w for w in words if not w in stop_words]
        txt = ' '.join([word for word in words])
        return txt

    def tweet_sentiment(self, tweet):
        analysis = TextBlob(self.clean_text(tweet))
        if analysis.sentiment.polarity > 0:
            return 'positive'
        elif analysis.sentiment.polarity == 0:
            return 'neutral'
        else:
            return 'negative'

    def get_tweets(self, query, count = 10):
        tweets = []
        try:
            fetched_tweets = self.api.search(q=query, count=count)
            for tweet in fetched_tweets:
                parsed_tweet = {}
                parsed_tweet['text'] = tweet.text
                parsed_tweet['sentiment'] = self.tweet_sentiment(tweet.text)
                if tweet.retweet_count > 0:
                    if parsed_tweet not in tweets:
                        tweets.append(parsed_tweet)
                else:
                    tweets.append(parsed_tweet)
            return tweets

        except tweepy.TweepError as e:
            print("Error : " + str(e))

def main():
    api = TwitterClient()
    tweets = api.get_tweets(query='bill gates', count=200)

    # picking positive tweets
    ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']
    # percentage of positive tweets
    print("Positive tweets percentage: {} %".format(100 * len(ptweets) / len(tweets)))
    # picking negative tweets
    ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']
    # percentage of negative tweets
    print("Negative tweets percentage: {} %".format(100 * len(ntweets) / len(tweets)))
    # percentage of neutral tweets
    print("Neutral tweets percentage: {} % \
          ".format(100 * (len(tweets) - (len(ntweets) + len(ptweets))) / len(tweets)))

    ## printing first 5 positive tweets
    print("\n\nPositive tweets:")
    for tweet in ptweets[:5]:
        print(tweet['text'])

    ## printing first 5 negative tweets
    print("\n\nNegative tweets:")
    for tweet in ntweets[:5]:
        print(tweet['text'])

if __name__ == "__main__":
    # calling main function
    main()


"""
Common utilities shared across components in the library
"""

# Standard
from typing import Any
import copy
import datetime
import inspect

# Third Party
import six

# First Party
import aconfig
import alog

# Local
from . import config, constants
from .dag import ResourceNode
from .exceptions import assert_cluster

log = alog.use_channel("OPUTL")

# Forward declaration for Session
SESSION_TYPE = "Session"

## Dicts #######################################################################

def merge_configs(base, overrides) -> dict:
    """Helper to perform a deep merge of the overrides into the base. The merge
    is done in place, but the resulting dict is also returned for convenience.

    The merge logic is quite simple: If both the base and overrides have a key
    and the type of the key for both is a dict, recursively merge, otherwise
    set the base value to the override value.

    Args:
        base:  dict
            The base config that will be updated with the overrides
        overrides:  dict
            The override config

    Returns:
        merged:  dict
            The merged results of overrides merged onto base
    """
    for key, value in overrides.items():
        if (
            key not in base
            or not isinstance(base[key], dict)
            or not isinstance(value, dict)
        ):
            base[key] = value
        else:
            base[key] = merge_configs(base[key], value)

    return base

def nested_set(dct: dict, key: str, val: Any):
    """Helper to set values in a dict using 'foo.bar' key notation

    Args:
        dct:  dict
            The dict into which the key will be set
        key:  str
            Key that may contain '.' notation indicating dict nesting
        val:  Any
            The value to place at the nested key
    """
    parts = key.split(constants.NESTED_DICT_DELIM)
    for i, part in enumerate(parts[:-1]):
        dct = dct.setdefault(part, {})
        if not isinstance(dct, dict):
            raise TypeError(
                "Intermediate key {} is not a dict".format(  # pylint: disable=consider-using-f-string
                    constants.NESTED_DICT_DELIM.join(parts[:i])
                )
            )
    dct[parts[-1]] = val

def nested_get(dct: dict, key: str, dflt=None) -> Any:
    """Helper to get values from a dict using 'foo.bar' key notation

    Args:
        dct:  dict
            The dict into which the key will be set
        key:  str
            Key that may contain '.' notation indicating dict nesting

    Returns:
        val:  Any
            Whatever is found at the given key or None if the key is not found.
            This includes missing intermediate dicts.
    """
    parts = key.split(constants.NESTED_DICT_DELIM)
    for i, part in enumerate(parts[:-1]):
        dct = dct.get(part, None)
        if not dct:
            return dflt
        if not isinstance(dct, dict):
            raise TypeError(
                "Intermediate key {} is not a dict".format(  # pylint: disable=consider-using-f-string
                    constants.NESTED_DICT_DELIM.join(parts[:i])
                )
            )
    return dct.get(parts[-1], dflt)

# Stolen from kubernetes but modified for None pruning and safety
# https://github.com/kubernetes-client/python/blob/d67bc8c2bdb89b29c17c1ba0edb03a48d977c0e2/kubernetes/client/api_client.py#L202
def sanitize_for_serialization(obj):  # pylint: disable=too-many-return-statements
    """Builds a JSON POST object.
    If obj is None, return None.
    If obj is str, int, long, float, bool, return directly.
    If obj is datetime.datetime, datetime.date
        convert to string in iso8601 format.
    If obj is list, sanitize each element in the list.
    If obj is dict, return the dict.
    If obj is OpenAPI model, return the properties dict.
    :param obj: The data to serialize.
    :return: The serialized form of data.
    """
    if obj is None:  # pylint: disable=no-else-return
        return None
    elif isinstance(obj, (float, bool, bytes, six.text_type) + six.integer_types):
        return obj
    elif isinstance(obj, list):
        return [sanitize_for_serialization(sub_obj) for sub_obj in obj]
    elif isinstance(obj, tuple):
        return tuple(sanitize_for_serialization(sub_obj) for sub_obj in obj)
    elif isinstance(obj, (datetime.datetime, datetime.date)):
        return obj.isoformat()
    elif isinstance(obj, ResourceNode):
        return sanitize_for_serialization(obj.get_data())

    if isinstance(obj, dict):
        obj_dict = obj
    elif hasattr(obj, "attribute_map"):
        # Convert model obj to dict except
        # `openapi_types` and `attribute_map`.
        # Convert attribute name to json key in
        # model definition for request.
        obj_dict = {}
        for attr, name in six.iteritems(obj.attribute_map):
            if hasattr(obj, attr):
                obj_dict[name] = getattr(obj, attr)

    # Prune fields which are None but keep
    # empty arrays or dictionaries
    return_dict = {}
    for key, val in six.iteritems(obj_dict):
        updated_obj = sanitize_for_serialization(val)
        if updated_obj is not None:
            return_dict[key] = updated_obj
    return return_dict

## Common Usage Patterns #######################################################

def get_passthrough_annotations(session):
    """This helper gets the set of annotations that should be passed from a
    parent CR to a child subsystem CR.

    Args:
        session:  DeploySession
            The session for the current deploy

    Returns:
        annotations:  Dict[str, str]
            The dict mapping of annotations that should be passed through
    """
    annotations = session.metadata.get("annotations", {})
    passthrough_annotations = {
        k: v for k, v in annotations.items() if k in constants.ALL_ANNOTATIONS
    }

    log.debug2("Oper8 passthrough annotations: %s", passthrough_annotations)
    return passthrough_annotations

def get_manifest_version(cr_manifest: aconfig.Config) -> str:
    """Get the version for a given custom resource or from the config
    if version override provided

    Args:
        cr_manifest: aconfig.Config
            The manifest to pull the version from

    Returns:
        version: str
            The current version
    """
    if config.vcs.version_override:
        return config.vcs.version_override
    return nested_get(cr_manifest, config.vcs.field)

def add_finalizer(session: SESSION_TYPE, finalizer: str):
    """This helper adds a finalizer to current session CR

    Args:
        session:  Session
            The session for the current deploy
        finalizer: str
            The finalizer to be added
    """
    if finalizer in session.finalizers:
        return

    log.debug("Adding finalizer: %s", finalizer)

    manifest = {
        "kind": session.kind,
        "apiVersion": session.api_version,
        "metadata": copy.deepcopy(session.metadata),
    }
    manifest["metadata"].setdefault("finalizers", []).append(finalizer)
    success, _ = session.deploy_manager.deploy([manifest])

    # Once successfully added to cluster than add it to session
    assert_cluster(success, f"Failed add finalizer {finalizer}")
    session.finalizers.append(finalizer)

def remove_finalizer(session: SESSION_TYPE, finalizer: str):
    """This helper gets removes a finalizer from the current session controller

    Args:
        session:  Session
            The session for the current deploy
        finalizer: str
            The finalizer to remove

    Returns:
        annotations:  Dict[str, str]
            The dict mapping of annotations that should be passed through
    """
    if finalizer not in session.finalizers:
        return

    log.debug("Removing finalizer: %s", finalizer)

    # Create manifest with only required fields
    manifest = {
        "kind": session.kind,
        "apiVersion": session.api_version,
        "metadata": copy.deepcopy(session.metadata),
    }

    # Check to see if the object exists in the cluster
    success, found = session.get_object_current_state(
        kind=session.kind,
        api_version=session.api_version,
        name=session.name,
    )
    assert_cluster(success, "Failed to look up CR for self")

    # If still present in the cluster, update it without the finalizer
    if found:
        manifest["metadata"]["finalizers"].remove(finalizer)
        success, _ = session.deploy_manager.deploy([manifest])

        # Once successfully removed from cluster than remove from session
        assert_cluster(success, f"Failed remove finalizer {finalizer}")

    # If the finalizer has been confirmed to not be there, remove it from the
    # in-memory finalizers
    session.finalizers.remove(finalizer)

## General #####################################################################

class classproperty:  # pylint: disable=invalid-name,too-few-public-methods
    """@classmethod+@property
    CITE: https://stackoverflow.com/a/22729414
    """

    def __init__(self, func):
        self.func = classmethod(func)

    def __get__(self, *args):
        return self.func.__get__(*args)()

class abstractclassproperty:  # pylint: disable=invalid-name,too-few-public-methods
    """This decorator implements a classproperty that will raise when accessed"""

    def __init__(self, func):
        self.prop_name = func.__name__

    def __get__(self, *args):
        # If this is being called by __setattr__, we're ok because it's
        # apptempting to set the attribute on the class
        curframe = inspect.currentframe()
        callframe = inspect.getouterframes(curframe, 2)[1]
        caller_name = callframe[3]
        if caller_name == "__setattr__":
            return None

        # If this is a help() call or a pdoc documentation request, return an
        # object with a docstring indicating that the property is abstract
        if (
            "help" in callframe.frame.f_code.co_names
            or callframe.frame.f_globals["__name__"] == "pdoc"
        ):

            class AbstractClassProperty:  # pylint: disable=missing-class-docstring
                __slots__ = []
                __doc__ = f"""The <{self.prop_name}> property is an abstract class property
                that must be overwritten in derived children
                """

            return AbstractClassProperty

        raise NotImplementedError(
            f"Cannot access abstractclassproperty {self.prop_name}"
        )


def tcNo(tcno):
    if len(tcno)==9:
        a = 7*(int(tcno[0])+int(tcno[2])+int(tcno[4])+int(tcno[6])+int(tcno[8]))
        b = int(tcno[1])+int(tcno[3])+int(tcno[5])+int(tcno[7])
        haneOn = (a-b)%10
        onHaneli=str(tcno)+str(haneOn)
        toplamOn = 0
        for i in range(10):
            toplamOn=toplamOn+int(onHaneli[i])

        haneOnBir = toplamOn%10
        return tcno+str(haneOn)+str(haneOnBir)
    else:
        print("Girilen TC No 9 Haneli Olmalidir.")

tcnumara = input("TC Kimlik No Ilk 9 Hanesini Giriniz:")

ID=tcNo(tcnumara)
print("TC Kimlik No'nun Tamami :",ID)