"""Administrative command-line interface."""

from __future__ import annotations

import json
import sys
from pathlib import Path

import click
import structlog
import uvicorn
from cryptography.fernet import Fernet
from fastapi.openapi.utils import get_openapi
from safir.asyncio import run_with_asyncio
from safir.click import display_help
from safir.database import create_database_engine, initialize_database
from safir.slack.blockkit import SlackMessage
from sqlalchemy import text

from .dependencies.config import config_dependency
from .factory import Factory
from .keypair import RSAKeyPair
from .main import create_app
from .models.token import Token
from .schema import Base

__all__ = [
    "audit",
    "delete_all_data",
    "generate_key",
    "generate_token",
    "help",
    "init",
    "main",
    "maintenance",
    "openapi_schema",
    "run",
]

@click.group(context_settings={"help_option_names": ["-h", "--help"]})
@click.version_option(message="%(version)s")
def main() -> None:
    """Administrative command-line interface for gafaelfawr."""

@main.command()
@click.argument("topic", default=None, required=False, nargs=1)
@click.pass_context
def help(ctx: click.Context, topic: str | None) -> None:
    """Show help for any command."""
    display_help(main, ctx, topic)

@main.command()
@click.option(
    "--fix", default=False, is_flag=True, help="Fix issues found, if possible"
)
@click.option(
    "--config-path",
    envvar="GAFAELFAWR_CONFIG_PATH",
    type=click.Path(path_type=Path),
    default=None,
    help="Application configuration file.",
)
@run_with_asyncio
async def audit(*, fix: bool, config_path: Path | None) -> None:
    """Run a consistency check on Gafaelfawr's data stores.

    Any problems found will be reported to Slack.
    """
    if config_path:
        config_dependency.set_config_path(config_path)
    config = await config_dependency()
    logger = structlog.get_logger("gafaelfawr")
    logger.debug("Starting audit")
    engine = create_database_engine(
        config.database_url, config.database_password
    )
    async with Factory.standalone(config, engine) as factory:
        slack = factory.create_slack_client()
        if not slack:
            msg = "Slack alerting required for audit but not configured"
            raise click.UsageError(msg)
        token_service = factory.create_token_service()
        async with factory.session.begin():
            alerts = await token_service.audit(fix=fix)
        if alerts:
            message = (
                "Gafaelfawr data inconsistencies found:\n• "
                + "\n• ".join(alerts)
            )
            await slack.post(SlackMessage(message=message))
    await engine.dispose()
    logger.debug("Finished audit")

@main.command()
@click.option(
    "--config-path",
    envvar="GAFAELFAWR_CONFIG_PATH",
    type=click.Path(path_type=Path),
    default=None,
    help="Application configuration file.",
)
@run_with_asyncio
async def delete_all_data(*, config_path: Path | None) -> None:
    """Delete all data from Redis and the database.

    Intended for destructive upgrades, such as when switching from one
    upstream authentication provider to another when all of the usernames will
    change.  This does not delete or reset UID and GID assignments from
    Firestore.
    """
    if config_path:
        config_dependency.set_config_path(config_path)
    config = await config_dependency()
    logger = structlog.get_logger("gafaelfawr")
    logger.debug("Starting to delete all data")
    engine = create_database_engine(
        config.database_url, config.database_password
    )
    tables = (t.name for t in Base.metadata.sorted_tables)
    async with Factory.standalone(config, engine) as factory:
        admin_service = factory.create_admin_service()
        async with factory.session.begin():
            stmt = text(f'TRUNCATE TABLE {", ".join(tables)}')
            logger.info("Truncating all tables")
            await factory.session.execute(stmt)
            await admin_service.add_initial_admins(config.initial_admins)
        token_service = factory.create_token_service()
        logger.info("Deleting all tokens from Redis")
        await token_service.delete_all_tokens()
        if config.oidc_server:
            oidc_service = factory.create_oidc_service()
            logger.info("Deleting all OpenID Connect codes from Redis")
            await oidc_service.delete_all_codes()
    await engine.dispose()
    logger.debug("Finished deleting all data")

@main.command()
def generate_key() -> None:
    """Generate a new RSA key pair and print the private key."""
    keypair = RSAKeyPair.generate()
    sys.stdout.write(keypair.private_key_as_pem().decode())

@main.command()
def generate_session_secret() -> None:
    """Generate a new Gafaelfawr session secret."""
    sys.stdout.write(Fernet.generate_key().decode() + "\n")

@main.command()
def generate_token() -> None:
    """Generate an encoded token (such as the bootstrap token)."""
    sys.stdout.write(str(Token()) + "\n")

@main.command()
@click.option(
    "--config-path",
    envvar="GAFAELFAWR_CONFIG_PATH",
    type=click.Path(path_type=Path),
    default=None,
    help="Application configuration file.",
)
@run_with_asyncio
async def init(*, config_path: Path | None) -> None:
    """Initialize the database storage."""
    if config_path:
        config_dependency.set_config_path(config_path)
    config = await config_dependency()
    logger = structlog.get_logger("gafaelfawr")
    logger.debug("Initializing database")
    engine = create_database_engine(
        config.database_url, config.database_password
    )
    await initialize_database(engine, logger, schema=Base.metadata)
    async with Factory.standalone(config, engine) as factory:
        admin_service = factory.create_admin_service()
        logger.debug("Adding initial administrators")
        async with factory.session.begin():
            await admin_service.add_initial_admins(config.initial_admins)
        if config.firestore:
            firestore = factory.create_firestore_storage()
            logger.debug("Initializing Firestore")
            await firestore.initialize()
    await engine.dispose()
    logger.debug("Finished initializing data stores")

@main.command()
@click.option(
    "--config-path",
    envvar="GAFAELFAWR_CONFIG_PATH",
    type=click.Path(path_type=Path),
    default=None,
    help="Application configuration file.",
)
@run_with_asyncio
async def maintenance(*, config_path: Path | None) -> None:
    """Perform background maintenance."""
    if config_path:
        config_dependency.set_config_path(config_path)
    config = await config_dependency()
    logger = structlog.get_logger("gafaelfawr")
    logger.debug("Starting background maintenance")
    engine = create_database_engine(
        config.database_url, config.database_password
    )
    async with Factory.standalone(config, engine, check_db=True) as factory:
        token_service = factory.create_token_service()
        async with factory.session.begin():
            logger.info("Marking expired tokens in database")
            await token_service.expire_tokens()
            logger.info("Truncating token history")
            await token_service.truncate_history()
    await engine.dispose()
    logger.debug("Finished background maintenance")

@main.command()
@click.option(
    "--add-back-link",
    default=False,
    is_flag=True,
    help="Add back link (used when generating application documentation)",
)
@click.option(
    "--output",
    default=None,
    type=click.Path(path_type=Path),
    help="Output path (output to stdout if not given).",
)
def openapi_schema(*, add_back_link: bool, output: Path | None) -> None:
    """Generate the OpenAPI schema."""
    app = create_app(load_config=False)
    description = app.description
    if add_back_link:
        description += "\n\n[Return to Gafaelfawr documentation](api.html)."
    schema = get_openapi(
        title=app.title,
        description=description,
        version=app.version,
        routes=app.routes,
    )
    if output:
        output.parent.mkdir(exist_ok=True)
        with output.open("w") as f:
            json.dump(schema, f)
    else:
        json.dump(schema, sys.stdout)

@main.command()
@click.option(
    "--port", default=8080, type=int, help="Port to run the application on."
)
def run(*, port: int) -> None:
    """Run the application (for testing only)."""
    uvicorn.run(
        "gafaelfawr.main:create_app",
        factory=True,
        port=port,
        reload=True,
        reload_dirs=["src"],
    )


with open('input.txt','r') as f:

    input = f.readlines()

# Lists (for simplicity) to hold each individual elf's caloric breakdown + overal elves.
elves = []
elf_cals = []

for line in input:

    # Determine if a new elf list
    if len(line) > 1:

        elf_cals.append(line.strip())

    else:

        elves.append(elf_cals)
        elf_cals = []

total_cals_elf = []

# Part 1: Most calories

for elf in elves:

    cal_count = 0

    for snack in elf:

        cal_count += int(snack)

    total_cals_elf.append(cal_count)

print(max(total_cals_elf))

# Part 2: Sum of top 3

top_3 = sorted(total_cals_elf)[-3:]

cal_count = 0
for cals in top_3:

    cal_count += cals

print(cal_count)


#56. Desenvolva que leia o NOME, IDADE e SEXO de 4 pessoas. no final do programa, mostre:
#A média de idade do grupo;
#Qual é o nome do homem mais velho;
#Quantas mulheres têm menos de 20 anos.
s = 0
cont = 0
import math
velho = 0
nvelho = 0
for c in range(1,5):
    print("-------- {}ª PESSOA -------".format(c))
    nome = str(input('Informe o seu nome: '))
    idade = int(input('Informe a sua idade: '))
    sexo = int(input('Informe o codigo corrspondente ao seu sexo:\n[1] Feminino\n[2] Masculino\n '))
    s += idade
    if sexo == 1 and idade < 20:
        cont += 1
    if sexo == 2 and c == 1:
        velho = idade
        nvelho = nome
    if idade > velho and sexo == 2:
            nvelho = nome
            velho = idade
print('A média de idade do grupo é {:.2f}'.format(s/4))
print('{} mulheres possuem menos de 20 anos de idade'.format(cont))
print('{} é o homem mais velho e tem {} anos'.format(nvelho,velho))


class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        l = 0
        r = 0
        a = 0
        for i in range(len(height)):
            l = max(height[i], l)
            r = max(height[- 1 - i], r)
            a += l + r
        return a - sum(height) - l * len(height)


'''
@File  : create-maximum-number.py
@Author: Swift
@Date  : 2020/12/15 11:19
@Link  : https://leetcode-cn.com/problems/create-maximum-number/
@Desc  : 321. 拼接最大数
@Method: https://zhuanlan.zhihu.com/p/90906054
'''

class Solution:

    def get_max_num_subseq(self, arr, i):
        stack = []
        pop_num = len(arr) - i
        for item in arr:
            while stack and pop_num and stack[-1] < item:
                pop_num -= 1
                stack.pop()
            stack.append(item)
        return stack[:i]

    def merge(self, arr1, arr2, k):
        return [max(arr1, arr2).pop(0) for _ in range(k)]

    def maxNumber(self, nums1, nums2, k):
        res = [0] * k
        for i in range(k+1):
            if i <= len(nums1) and k-i <= len(nums2):
                tmp1 = self.get_max_num_subseq(nums1, i)
                tmp2 = self.get_max_num_subseq(nums2, k-i)
                temp = self.merge(tmp1, tmp2, k)

                res = max(res, temp)
        return res

if __name__ == '__main__':
    obj = Solution()
    nums1 = [3, 9]
    nums2 = [8, 9]
    k = 3
    res = obj.maxNumber(nums1, nums2, k)
    print(res)


from collight.models.customer import Customer
from collight.serializers import CustomerSerializer
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters, permissions, viewsets

class CustomerViewset(viewsets.ModelViewSet):

    serializer_class = CustomerSerializer
    queryset = Customer.objects.all()
    permission_classes = [permissions.IsAuthenticated]

    filter_backends = [
        DjangoFilterBackend,
        filters.OrderingFilter,
        filters.SearchFilter
    ]
    search_fields = [
        'gender',
        'address',
        'name',
        'email',
        'birthdate',
        'marital_status'
        ]
    ordering_fields = ['name']


import re

def pad_zero(match):
    text = match.group(0)
    num = match.group('num')
    padded = "%03d" % int(num)
    return match.group(0).replace(num, padded)

prefix = "_s"

p = re.compile(r'%s(?P<num>[0-9]+)' % prefix)

tests = [
    "test_s1",
    "_s1",
    "myimage_s19.tif",
    "myimage2_s19.tif",
]

for t in tests:

    print('\n', t)
    # print('match', p.match(t))
    print("replace", p.sub(pad_zero, t))


__author__ = 'achmed'

import config

class ConfigWrapper(config.Config):
    '''
    use to bless config.Mapping objects into Config objects to provide getByPath
    '''
    def __init__(self, streamOrFile=None, parent=None, other=None):
        if other:
            super(ConfigWrapper, self).__init__()
            object.__setattr__(self, 'path', other.path)
            object.__setattr__(self, 'data', other.data)
            object.__setattr__(self, 'order', other.order)
            object.__setattr__(self, 'comments', other.comments)
        else:
            super(ConfigWrapper, self).__init__(streamOrFile=streamOrFile, parent=parent)

    def getByPath(self, path, default=None):
        try:
            return super(ConfigWrapper, self).getByPath(path)
        except config.ConfigError:
            return default


"""
Plot adaptive experimental results
"""
import pickle
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import numpy as np
import pandas as pd
import os
import glob
import math
import scipy.interpolate as interp

font = {'family' : 'DejaVu Sans',
#        'weight' : 'bold',
        'size'   : 20}
plt.rc('font', **font)
colors=['#6aa2fc', '#fc8181', '#a5ff9e', '#3639ff', '#ff3636', '#13ba00', '#ff62f3']

input_size = {'mimic3': 178, 'gina': 242, 'activity': 140, 'phishing': 10, 'sylva': 54}
num_samples = {'mimic3': 14681, 'gina': 3468*0.8, 'activity': 7352, 'phishing': 11055*0.8, 'sylva': 14395*0.8}
embedding_size = 16
post_training = 100

def all_seeds(prefix, selector, epochs, dataset, num_clients, batch_size, lambdc, lambds, emb_sizes, red):
    """
    Averages the results of all seeds
    """
    files = glob.glob(f'{prefix}_featureSGD_L21_BS{batch_size}_NC{num_clients}_epochs{epochs}_selector{selector}_dataset{dataset}_lambdc{lambdc}_lambds{lambds}_red{red}_seed*.pkl')
    files_base = glob.glob(f'/hdd/home/feature_results/{prefix}_feature_L21_BS{batch_size}_NC{num_clients}_epochs0_selectorbaseline_dataset{dataset}_lambdc0.0_lambds0.0_red0.0_seed*.pkl')
    pickles = []
    pickles_bas = []
    min_len = 10000
    for f,f_bas in zip(files, files_base):
        # Get accuracy pickles
        seed = f.split('_')[-1][4:-4]
        pkl = pickle.load(open(f, 'rb'))
        pkl_bas = pickle.load(open(f_bas, 'rb'))
        pkl = np.array(np.array(pkl)[:,0,1], dtype=np.float32)
        pkl_bas = np.array(np.array(pkl_bas)[:,0,1], dtype=np.float32)*100
        pickles_bas.append(pkl_bas)
        if selector == 'sparse':
            pkl = np.concatenate((pkl[0:epochs+1], pkl[150+epochs:]))
        elif selector == 'emb_sparse':
            pkl = np.concatenate((pkl[0:epochs+1], pkl[300+epochs:]))
        pickles.append(pkl*100)

        if selector == 'emb_sparse':
            # Get percentage of embedding components kept during post feature selection
            filename = f'out_feature_emb_sparse_{dataset}_lambdc{lambdc}_lambds{lambds}_red{red}_seed{seed}.txt'
            out = open(filename, 'r')
            substring = 'embedding components'
            lines = out.readlines()
            embs_removed = [line for line in lines if substring in line]
            out.close()
            emb_sizes.append((64-float(embs_removed[0].split(' ')[1]))/64)

    target = np.max(pickles_bas)
    pickles = np.array(pickles)

    if selector == 'centralized':
        # Get percentage of spurious features removed for all iterations
        files_imp = glob.glob(f'importances_all_featureSGD_L21_BS{batch_size}_NC{num_clients}_epochs{epochs}_selector{selector}_dataset{dataset}_lambdc{lambdc}_lambds{lambds}_red{red}_seed*.pkl')
        red_pers = []
        for i, f in enumerate(files_imp):
            importances = pickle.load(open(f, 'rb'))[:-1]
            red_per = []
            for importance in importances:
                correct = 0
                total = 0
                for client in range(num_clients):
                    start = input_size[dataset]
                    end = int(input_size[dataset]*(1+red))
                    red_new = np.arange(start,end)
                    total += len(red_new)
                    correct += len(list(set(red_new).intersection(importance[client])))
                red_per.append(correct/total)
            while len(red_per) < len(pickles[i]):
                red_per.append(red_per[-1])
            red_pers.append(red_per)
    else:
        # Get percentage of spurious features removed total
        files = glob.glob(f'importances_featureSGD_L21_BS{batch_size}_NC{num_clients}_epochs{epochs}_selector{selector}_dataset{dataset}_lambdc{lambdc}_lambds{lambds}_red{red}_seed*.pkl')
        red_pers = []
        for i, f in enumerate(files):
            importance = pickle.load(open(f, 'rb'))
            importance = tf.concat(importance, 0)
            zero = np.where(importance <= 0)[0]
            start = 0
            end = 0
            redundant_features = []
            for client in range(num_clients):
                start += input_size[dataset]
                end += int(input_size[dataset]*(1+red))
                redundancy = np.arange(start,end)
                redundant_features = np.concatenate((redundant_features, redundancy))
                start = end
            correct = list(set(redundant_features).intersection(zero))
            red_pers.append(len(correct)/len(redundant_features))

    # Get size of embeddings
    if selector == 'emb_sparse':
        embedding_per = np.average(emb_sizes)
    else:
        embedding_per = 1.0

    # Calculate the bits to reach targets
    bits_list = []
    for i in range(len(pickles)):
        if i >= len(red_pers):
            continue
        min_bits_per_epoch = min(emb_sizes)*embedding_size*num_samples[dataset]*num_clients*32
        bits_per_epoch_pre = embedding_size*num_samples[dataset]*num_clients*32
        bits_per_epoch_post = embedding_per*embedding_size*num_samples[dataset]*num_clients*32

        # Cost during pre-training
        final_epoch = 0
        bits_so_far = 0
        if epochs > 0:
            while final_epoch <= epochs:
                bits_so_far += bits_per_epoch_pre
                final_epoch += 1

        # Cost after pre-training
        cur_red = red_pers[i]
        while final_epoch < len(pickles[i]):
            if selector == 'centralized':
                cur_red = red_pers[i][final_epoch]
            if pickles[i][final_epoch] > target*0.9 and cur_red > 0.8:
                break
            bits_so_far += bits_per_epoch_post
            final_epoch += 1
        if final_epoch < len(pickles[i]) or (pickles[i][-1] > target*0.9 and cur_red > 0.8):
            bits_list.append(bits_so_far)

    # Average and print results
    avg_bits = np.average(bits_list)
    std_bits = np.std(bits_list)
    print(f'{avg_bits/(8*(2**20)):.2f} $\pm$ {std_bits/(8*(2**20)):.2f}')

    return emb_sizes

types = ['accs_test']
datasets = ['mimic3', 'gina', 'sylva', 'activity', 'phishing']
batches = [1000,100,1000,1000,1000]
num_clients = [4,4,4,4,3]

centralized_lambds = [0.25, 0.1, 1.0, 0.1, 0.25]
sparse_lambds =      [40.0, 0.025, 0.1, 0.1, 0.1]
emb_sparse_lambdsc = [40.0, 0.025, 0.1, 0.1, 0.1]
emb_sparse_lambdss = [0.1, 0.005, 0.01, 0.25, 0.005]
epochs_emb = [1,1,1,2,1]
epochs_sparse = [1,1,1,3,1]

for t in types:
    for dataset, epoch_emb, epoch_sparse, batch, clients, cent_lambd, sparse_lambd, emb_lambdc, emb_lambds in zip(datasets, epochs_emb, epochs_sparse, batches, num_clients, centralized_lambds, sparse_lambds, emb_sparse_lambdsc, emb_sparse_lambdss):
        # Print communication cost to reach target accuracy and spurious features removed
        emb_sizes = all_seeds(t,"emb_sparse", epoch_emb, dataset, clients, batch, emb_lambdc, emb_lambds, [], 0.5)
        _ = all_seeds(t,"centralized", 0, dataset, clients, batch, cent_lambd, 0.0, emb_sizes, 0.5)
        _ = all_seeds(t,"sparse", epoch_sparse, dataset, clients, batch, sparse_lambd, 0.0, emb_sizes, 0.5)

        print('\midrule')
        print()


from mathutils import Vector
from array import array
import bpy
from .Branch import Branch
from .Leaf import Leaf, l
from bpy.props import BoolProperty
from math import sqrt
import math
from functools import partial
from random import randint, random, uniform
from copy import copy, deepcopy
#from .scaoperator import bpy

#https://blenderartists.org/t/detecting-if-a-point-is-inside-a-mesh-2-5-api/485866/4
def point_inside(point,obj):
    axes = [ Vector((1,0,0)), Vector((0,1,0)), Vector((0,0,1)) ]
    outside = False
    mat = obj.matrix_world.copy()
    mat.invert()
    for axis in axes:
        orig = mat*point
        count = 0
        while True:
            a,location,normal,index = obj.ray_cast(orig,orig+axis*1.84467e+19)
            if index == -1: break
            count += 1
            orig = location + axis*0.00001
        if count%2 == 0:
            outside = True
            break
    return not outside

def closest(pos, count, n, x, y, z):
    d2 = 1e30
    for i in range(n):
        if count[i] > 1:
            continue
        dx, dy, dz = x-pos[i*3], y-pos[i*3+1], z-pos[i*3+2]
        d = dx*dx + dy*dy + dz*dz
        if d < d2:
            d2 = d
            ci = i
            v = dx, dy, dz
    return d2, ci, v

def getDistance(p0, p1):
        return sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2 + (p0[2] - p1[2])**2)

# importiert von https://blender.stackexchange.com/questions/5898/how-can-i-create-a-cylinder-linking-two-points-with-python

class Tree:
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_label = 'Tools Tab Label'
    bl_context = 'objectmode'
    bl_category = 'SC'

    def __init__(self):
        # Vektor operationen

        def div(x,y):
            return(x[0]/y, x[1]/y, x[2]/y)

        def sub(x,y):
            return(x[0]-y[0], x[1]-y[1], x[2]-y[2])

        def normieren(x,y):
            länge = sqrt(x[0]**2 + x[1]**2 + x[2]**2)
            return((x[0]/länge + x[1]/länge+ x[2]/länge)*y)

        def length_squared(u):
            return sum([a ** 2 for a in u])

        def add(u, v):
            return [a + b for a, b in zip(u, v)]

        def norm(u):
            return setlength(u, 0.5)

        def scale_by_scalar(u, scalar):
            return [a * scalar for a in u]

        def length(u):
            return math.sqrt(length_squared(u))

        def setlength(u, l):
            return scale_by_scalar(u, l / length(u))

        branches = []
        leaves = []
        verts = ()
        closeEnough = 0 # False

        #leaves generieren
        for i in range(0,bpy.context.scene.leaf):
            leaves.append(Leaf((uniform(-bpy.context.scene.xyvalue,bpy.context.scene.xyvalue),uniform(-bpy.context.scene.xyvalue,bpy.context.scene.xyvalue),uniform(bpy.context.scene.zvaluebot,bpy.context.scene.zvaluetop)),0))
            #bpy.ops.mesh.primitive_uv_sphere_add(size=0.1, location=leaves[i].pos)
            i =+ 1

        #Prüfen für Objekt
        if bpy.context.scene.Growth_limitation == True:
            for i in reversed(range(len(leaves))):
                myobj = bpy.context.object
                #mypoint = mathutils.Vector(leaves[i].pos)
                if (point_inside(Vector(leaves[i].pos),myobj)) == False:
                    del leaves[i]

        #Stamm machen
        root = Branch(None,(0,0,0),(0,0,0.5))
        branches.append(root)
        current = root

        max_dist = bpy.context.scene.maxdist#3
        min_dist = bpy.context.scene.mindist#1
        while closeEnough == 0: # False
            for i in range (len(leaves)):
                g = 0
                g = getDistance(current.pos, leaves[i].pos)
                #print(g)
                if (g < max_dist):
                    #print('schlaufe 1')
                    closeEnough = 1
                if (closeEnough == 0):
                    #print('         schlaufe 2          ')
                    nextPos = tuple(add(branches[i].pos,branches[i].dir))
                    branch = Branch(i,nextPos,branches[i].dir)
                    current = branch
                    branches.append(current)

        #Der eigentliche Algorithmus
        def grow(self):
            self.record = 100000
            for i in range(len(leaves)):
                leaf = leaves[i]
                closestBranch = None
                self.closestDir = None

                #Den nächsten Ast bestimmen
                for j in range(len(branches)):
                    #print(j)
                    branch = branches[j]
                    dir = sub(leaf.pos ,branch.pos)
                    d = length(dir)
                    #print(d)
                    if (d < min_dist):
                        leaf.reached()
                        break
                    #elif(d > max_dist):
                    elif (closestBranch == None or d < self.record):
                        closestBranch = branch
                        self.record = d

                #Dessen Richtung bestimmen
                if (closestBranch is not None):
                    newDir = tuple(sub(leaf.pos, closestBranch.pos))
                    newDirnormed = norm(newDir)
                    #print(newDir)
                    closestBranch.dir = tuple(add(closestBranch.dir, newDirnormed))
                    closestBranch.count += 1

            #alle Blätter entfernen die erreicht wurden
            for i in reversed(range(len(leaves))):
                if (leaves[i].erreicht == 1):
                    del(leaves[i])

            branchcopy = deepcopy(branches)
            #print(branches[0].dir)
            for i in reversed(range(len(branches))):
                branch = branches[i]
                #print(branch.count)
                if (branch.count > 0):
                    branch.dir = tuple(div(branch.dir,branch.count))
                    newPos = tuple(add(branch.pos,branch.dir))
                    newB = Branch(i, newPos, branch.dir)
                    branches.append(newB)
                    branch.reset()

        for i in range(70):
            grow(self)

        # this assumes all vertex positions are unique (= do not overlap)
        # create a mapping from vertex position to vertex index
        verts = {b.pos:i for i,b in enumerate(branches)}
        # create vertex pairs as edge by locating vert index based on coordinates
        edges = [ (verts[b.pos],verts[branches[b.parent].pos])for b in branches if b.parent is not None]
        # replace vertex mapping by simple list of coordinates
        verts = [b.pos for b in branches]

        mesh = bpy.data.meshes.new("tree_mesh")
        mesh.from_pydata(verts, edges, faces=[])
        mesh.update()

        obj = bpy.data.objects.new("Tree", mesh)

        scene = bpy.context.scene
        scene.objects.link(obj)
        #remove doubles muss man noch machen

tree= Tree()


import socket
import select
import errno
import sys
import threading
import time
clientSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
IP = "127.0.0.1"
port = 3000
my_userName = input("UserName : ")
clientSocket.connect((IP,port))
userName = my_userName.encode()
clientSocket.send(userName)
sequence = []
accepted = []
m = int(clientSocket.recv(128).decode().strip())
for i in range(0,(2**m)):
    sequence.append(i)
Rn = 0
Rf = 0
total_frames = int(clientSocket.recv(1).decode().strip())
for i in range(total_frames):
    accepted.append(0)

def recieveData():
    flag = 0
    global Rn,Rf,total_frames,accepted
    while (total_frames!=0):
        if(flag == 0):
            msg = clientSocket.recv(128).decode()
            print(f"Server > {msg}")
            flag = 1
        else:
            message = clientSocket.recv(9).decode()
            if(accepted[int(message[-1])]==1):
                print("Frame discarded : Frame ",(message[-1]))
            else:
                if((Rf-Rn)<=(2**(m-1)-1) and (Rf<=(2**m-1))):
                    if(int(message[-1])==Rn):
                        if(Rf>Rn):
                            accepted[Rn] = 1
                            temp = 0
                            for i in range(Rn,Rn+(2**(m-1))+1):
                                if(accepted[i] == 0):
                                   temp = i
                                   break
                            if(message):
                                total_frames = total_frames - temp + Rn + 1
                            print(f"Recieved frame from Server: {message}")
                            print("Number of Jumped frame_acknowledgement : ",temp-Rn)
                            Rn = temp
                            if(int(message[-1])<(2**m-1)):
                                ack_message = "Ack : " + str(sequence[Rn])
                                clientSocket.send(ack_message.encode())
                        else:
                            if(message):
                                total_frames = total_frames - 1
                            accepted[Rf] = 1
                            Rf = Rf + 1
                            Rn = Rn + 1
                            temp_flag_resent = 0
                            if(total_frames!=0):
                                if(int(message[-1])<(2**m-1)):
                                    ack_message = "Ack : " + str(sequence[Rn])
                                    clientSocket.send(ack_message.encode())
                                print(f"Recieved frame from Server: {message}")
                    else:
                        if(int(message[-1])>Rn):
                            accepted[int(message[-1])] = 1
                            if(int(message[-1])>Rf):
                                Rf = int(message[-1])
                            if(message):
                                total_frames = total_frames - 1
                            print(f"Recieved frame from Server: {message}")
                            if(int(message[-1])<(2**m-1)):
                                ack_message = "Nak : " + str(sequence[Rn])
                                clientSocket.send(ack_message.encode())
                            print("Negative Acknowldgement Sent of frame : ",sequence[Rn])
    else:
        print("All the frames were recieved successfully")
recieveThread = threading.Thread(target = recieveData, args=())
recieveThread.start()


from posts.models import Post
from posts.api.serializers import PostDetailSerializer

data = {
	"title": "New item",
	"content": "New content",
	"publish": "2019-06-06",
	"slug": "new-item",
}

# Get object
obj = Post.object.get(id=2)
new_item = PostDetailSerializer(obj, data=data)
if new_item.is_valid():
	new_item.save()
else:
	print(new_item.errors)

# Show new item
obj.data

# DELETE NEW ITEM
obj.delete()


import sys, os
import random

sys.path.append(os.path.join(os.path.dirname(__file__), "../.."))

import sc2
from sc2 import Race, Difficulty, BotAI
from sc2.constants import *
from sc2.position import Point2
from sc2.unit import Unit
from sc2.player import Bot, Computer
from sc2.player import Human
from sc2.ids.unit_typeid import UnitTypeId
from sc2.ids.ability_id import AbilityId
from sc2.units import Units

class ExplorationAgent(BotAI):

    def __init__(self):
        self.pylonList = []
        self.enemy_location = None
        self.explorer_list = []

    async def on_step(self, iteration):

        if iteration == 0:
            print(f"Enemy Position -> {self.enemy_start_locations}")

            for _ in range(len(self.enemy_start_locations)):
                self.explorer_list.append(None)

            # Procura onde o inimigo nasceu
            await self.find_enemy_start_locations()

        self.trainSCV()

    async def find_enemy_start_locations(self):
        '''
            Procura onde é local de nascimento do inimigo
            e manda um CSV ir até lá explorar
        '''
        for i in range(len(self.enemy_start_locations)):
            location = self.enemy_start_locations[i]
            explorer = self.workers.random_or(None)
            if explorer:
                self.explorer_list[i] = explorer
                print(f"Sending a CSV to enemy's location")
                explorer.move(location)

    def trainSCV(self):
        if (
            self.can_afford(UnitTypeId.SCV)
            and self.supply_left > 0
            and self.supply_workers < 22
            and (
                self.structures(UnitTypeId.BARRACKS).ready.amount < 1
                and self.townhalls(UnitTypeId.COMMANDCENTER).idle
                or self.townhalls(UnitTypeId.ORBITALCOMMAND).idle
            )
        ):
            for _townhall in self.townhalls.idle:
                print(f"Train SCV")
                _townhall.train(UnitTypeId.SCV)

def main():
    sc2.run_game(
        sc2.maps.get("AcropolisLE"),
        [Bot(Race.Terran, ExplorationAgent()), Computer(Race.Zerg, Difficulty.Hard)],
        realtime=True)

if __name__ == "__main__":
    main()


import os;
import sqlite3;
from sqlite3 import Error;
from shutil import copyfile
from datetime import datetime
from prettytable import PrettyTable

MONTHS = {'JAN': '01', 'FEB': '02', 'MAR': '03', 'APR': '04', 'MAY': '05', 'JUN': '06', 'JUL': '07', 'AUG': '08', 'SEP' : '09', 'OCT': '10', 'NOV': '11', 'DEC': '12'}
LEVELS = {'INDI':'0', 'NAME':'1', 'SEX':'1', 'BIRT':'1', 'DEAT':'1', 'FAMC':'1', 'FAMS':'1', 'FAM':'0', 'MARR':'1', 'HUSB':'1', 'WIFE':'1', 'CHIL':'1', 'DIV':'1', 'DATE':'2', 'HEAD':'0', 'TRLR':'0', 'NOTE':'0'}
TAGS = LEVELS.keys()
EXTENSION = '.ged'

'''
#TARIK + OSCAR TEST STUFF
TEST_ID = True
TEST_HUSBAND_NAME = True
TEST_WIFE_NAME = True
TEST_HUSBAND_ID = True
TEST_WIFE_ID = True
TEST_MARRAGE_VALID = True
'''

def file():
    geds = []
    for file in os.listdir(os.path.dirname(os.path.realpath(__file__))):
        if file.endswith(EXTENSION):
            geds.append(file)
    if(len(geds) <= 1):
        if(len(geds) == 0):
            print("NO FILE FOUND: Place "+EXTENSION+" in same folder as python script.")
        return geds[0]
    else:
        print("Files in Local Directory: ")
        for file in geds:
            print("- " + file)
        print('\n')
        while(True):
            name = input("Enter desired GEDCOM file name: ")
            if not EXTENSION in name:
                name += EXTENSION
            if name in geds:
                return name
            print("FILE NAME NOT FOUND, TRY AGAIN.\n")

def read(file):
    gedcom = open(file,'r')
    output = open(file.replace(EXTENSION, '.txt'),'w')
    for line in gedcom:
        id = None
        output.write("--> " + line.rstrip() + "\n")
        data = line.split(' ')
        level = data[0].rstrip()

        if ('INDI' or 'FAM') in line:
            id = data[1].rstrip()
            tag = data[2].rstrip()
            arguments = line.replace((level+ " " + id + " " + tag), 'id').rstrip()
        else:
            tag = data[1].rstrip()
            arguments = line.replace((level + " " + tag + " "), '').rstrip()

        if tag in TAGS and LEVELS[tag] == level:
            valid = 'Y'
        else:
            valid = 'N'

        str = "<-- " + level + "|" + tag + "|" + valid + "|" + arguments + "\n"

        output.write(str)
        print(str.rstrip())

    gedcom.close()
    output.close()
    return output

def tables(database):
    conn = sqlite3.connect(database)
    cursor = conn.cursor()

    print("\nGedcom Data - Individuals:\n")

    table = PrettyTable(["ID", "Name", "Gender", "Birthday", "Age", "Alive", "Death", "Child", "Spouse"])
    cursor.execute('SELECT * from Individuals')
    row = cursor.fetchone()
    while row is not None:
        table.add_row(row)
        row = cursor.fetchone()

    print(table)
    print("\nGedcom Data - Families:\n")

    table = PrettyTable(['ID', 'Married', 'Divorced', 'Husband_ID', 'Husband_Name', 'Wife_ID', 'Wife_Name', 'Children'])
    cursor.execute('SELECT * from Families')
    row = cursor.fetchone()
    while row is not None:
        table.add_row(row)
        row = cursor.fetchone()

    print(table)
    cursor.close()

def database(gedcom, database):
    file = open(gedcom,'r')
    lastLevel = 0
    familyTime, Divorced, Alive = False, False, True
    id, Name, Gender, Birthday, Age, Death, Child, Spouse, ID, Married, Husband_ID, Husband_Name, Wife_ID, Children, Wife_Name, dateType = ('None',)*16

    try:
        os.remove(os.getcwd()+'/'+database)
    except:
        pass

    if not os.path.exists(database):
        copyfile(os.getcwd()+"/template.sqlite3", os.getcwd()+'/'+database)

    for line in file:
        data = line.split(' ')
        level = data[0].rstrip()

        if (level == "0" and lastLevel == "1" and Gender != 'None' and familyTime == False):
            if Alive == False:
                Age = int(Death[2]) - int(Birthday[2])
            elif (Birthday != 'None'):
                Age = datetime.now().year - int(Birthday[2])
            db = sqlite3.connect(database)
            cursor = db.cursor()
            cursor.execute('''INSERT INTO Individuals(Name,Gender,Birthday,Age,Alive,Death,Child,Spouse,ID)
                  VALUES(?,?,?,?,?,?,?,?,?);''', (str(Name), str(Gender), str(Birthday), str(Age), str(Alive), str(Death), str(Child), str(Spouse), str(id)))
            db.commit()
            db.close()
            Alive = True
            Birthday, Death, Child = ('None',)*3

        if (level == "0") and ('FAM') in line:
            familyTime = True

        if (not familyTime):
#         #this is for individuals only, must make different one for fam
            if ('INDI') in line:
                id = data[1].rstrip().replace('@', '')
            if ('FAMS') in line:
                Spouse = data[2].rstrip().replace('@', '')
            if ('FAMC') in line:
                if (Child == 'None'):
                    Child = data[2].rstrip().replace('@', '')
                else:
                    Child = Child + data[2].rstrip().replace('@', '')
            if ('NAME') in line:
                Name = data[2:]
                if (len(data[2:]) > 1):
                    Name = [Name[0]] + [Name[1].rstrip('/').rstrip('/\n').replace('/', ' ')]
            if ('SEX') in line:
                Gender = data[2].rstrip('\n')
            if dateType == 'BIRT':
                Birthday = data[2:]
                Birthday = Birthday[:2] + [Birthday[2].rstrip('\n')]
                dateType = 'None'
            if dateType == 'DEAT':
                Death = data[2:]
                Death = Death[:2] + [Death[2].rstrip('\n')]
                Alive = False
                dateType = 'None'
            if ('BIRT') in line:
                dateType = 'BIRT'
            if ('DEAT') in line:
                dateType = 'DEAT'
            lastLevel = level

        if (familyTime):
            if (level == "0" and lastLevel == "1" and (Husband_ID != 'None' or Wife_ID != 'None')):
                db = sqlite3.connect(database)
                cursor = db.cursor()
                cursor.execute("SELECT Name FROM Individuals WHERE ID = '"+Wife_ID+"'")
                Wife_Name = cursor.fetchone()[0]
                cursor.execute("SELECT Name FROM Individuals WHERE ID = '"+Husband_ID+"'")
                Husband_Name = cursor.fetchone()[0]
                cursor.execute('''INSERT INTO Families(ID,Married,Divorced,Husband_ID,Husband_Name,Wife_ID,Wife_Name,Children)
                      VALUES(?,?,?,?,?,?,?,?);''', (str(ID), str(Married), str(Divorced), str(Husband_ID), str(Husband_Name), str(Wife_ID), str(Wife_Name), str(Children)))
                db.commit()
                db.close()
                Divorced = False;
                ID, Married, Husband_ID, Husband_Name, Wife_ID, Wife_Name, Children = ('None',)*7
            if ('FAM') in line:
                ID = data[1].rstrip().replace('@', '')
            if ('HUSB') in line:
                Husband_ID = data[2].rstrip().replace('@', '')
                Husband_Name = 'TEMP'
            if ('WIFE') in line:
                Wife_ID = data[2].rstrip().replace('@', '')
                Wife_Name = 'TEMP'
            if ('CHIL') in line:
                if (Children == 'None'):
                    Children = [data[2].rstrip().replace('@', '')]
                else:
                    Children += [data[2].rstrip().replace('@', '')]
            if dateType == 'MARR':
                Married = data[2:]
                Married = Married[:2] + [Married[2].rstrip('\n')]
                dateType = 'None'
            if ('MARR') in line:
                dateType = 'MARR'
            if ('DIV') in line:
                Divorced = True

'''
            #TARIK TEST STUFF
            #Sets are unique, if the len of the set ID isn't the same as len of list ID, not all are unique
            if len(id) > len(set(id)):
                TEST_ID = False
            if len(Husband_Name) > len(set(Husband_Name)):
                TEST_HUSBAND_NAME = False
            if len(Wife_Name) > len(set(Wife_Name)):
                TEST_WIFE_NAME = False
            if len(Husband_ID) > len(set(Husband_ID)):
                TEST_HUSBAND_ID = False
            if len(Wife_ID) > len(set(Wife_ID)):
                TEST_Wife_ID = False
            #OSCAR TEST STUFF
            if Married != 'None':
                MarriedPeople = GetFromDB('individuals', '*', 'spouse', "!= [NULL]")
                Married_ID_Date = []
                for x in MarriedPeople:
                    Married_ID_Date = ([x[0],x[1]])
                    Gender = x[2]
                    if ((Gender == "M") | (GetFromDB('families', 'Married', 'Husband_ID', Married_ID_Date[0])[2] < Married_ID_Date[1])):
                        TEST_MARRAGE_VALID = False
                        break

                    if ((Gender == "F") | (GetFromDB('families', 'Married', 'Husband_ID', Married_ID_Date[0])[2] < Married_ID_Date[1])):
                        TEST_MARRAGE_VALID = False
                        break

#OSCAR TEST STUFF
def GetFromDB(table, column, thisData, condition):
    connection = sqlite3.connect('database.sqlite3')
    cursor = connection.cursor()
    cursor.execute('SELECT '+ column +' from ' + table + ' where' + thisData + + condition )
    row = cursor.fetchone()
    data = []
    while row is not None:
        data.append(row)
        row = cursor.fetchone()
    return data

#TARIK TESTING STUFF
def TAGS_SIZE(TAGS):
    return len(TAGS) == 17
def LEVELS_SIZE(LEVELS):
    return len(LEVELS) == 17
def uniqueID():
    return TEST_ID
def uniqueHusbandID():
    return TEST_HUSBAND_ID
def uniqueWifeID():
    return TEST_WIFE_ID
def uniqueHusbandName():
    return TEST_HUSBAND_NAME
def uniqueWifeName():
    return TEST_WIFE_NAME
'''

'''
def us06(divorce_date, husband_death, wife_death):
    "Marriage should occur before death of either spouse"
    if not divorce_date or not (husband_death, wife_death):
        return False
'''
import ast

def us02(db, marriage_date, individual_ID): #Oscar
    "Birth should occur before marriage of an individual"
    if not marriage_date:
        return False
    query = queryDict(db, "Individuals", individual_ID, "Birthday")['Birthday']
    if(query == "None"):
        return False
    query = ''.join(c for c in query if c not in " (){}<>[]''")
    query = query.split(',')
    ib = datetime(int(query[2]), int(MONTHS[query[1]]), int(query[0]))
    md = datetime(int(marriage_date[2]), int(MONTHS[marriage_date[1]]), int(marriage_date[0]))
    return md < ib

def us10(marriage_date, husband_birth, wife_birth): #Oscar
    """Marriage should be at least 14 years after birth of both spouses
       (parents must be at least 14 years old)"""
    if not marriage_date:
        return False
    test = True
    md = datetime(int(marriage_date[2]) - 14, int(MONTHS[marriage_date[1]]), int(marriage_date[0]))
    if(husband_birth):
        hb = datetime(int(husband_birth[2]), int(MONTHS[husband_birth[1]]), int(husband_birth[0]))
        test *= hb < md
    if(wife_birth):
        wb = datetime(int(wife_birth[2]), int(MONTHS[wife_birth[1]]), int(wife_birth[0]))
        test *= wb < md
    return test

def us09_test(file):
    return null

def us09(mother_death, father_death, child_birth): #AUSTIN
    ''' Child born after mother deaths and before 9 months of fathers death'''
    if not child_birth or not (mother_death and father_death):
        return False
    cb = datetime(int(child_birth[2]), int(MONTHS[child_birth[1]]), int(child_birth[0]))
    md = datetime(int(mother_death[2]), int(MONTHS[mother_death[1]]), int(mother_death[0]))
    fd = datetime(int(father_death[2]), int(MONTHS[father_death[1]]), int(father_death[0]))
    if(father_death and not mother_death):
        return ((fd.year - cb.year) * 12 + fd.month - cb.month) >= 9
    elif(mother_death and not father_death):
        return md > cb
    return md > cb and ((fd.year - cb.year) * 12 + fd.month - cb.month) >= 9

#NOT FINISHED
def us21(gender, spose_gender): #AUSTIN
    ''' Husband is male, wife is female '''
    return (gender.upper()+spose_gender.upper()) in {'MF', 'FM'};

def queryDict(db, table, id, tag):
    con = sqlite3.connect(db)
    con.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r))
    cur = con.cursor()
    cur.execute("SELECT "+tag+" FROM "+table+" WHERE ID = '"+id+"'")
    return cur.fetchone()

if __name__ == '__main__':
    ged = file()
    db = ged.replace(EXTENSION, '.sqlite3')
    database(ged, db)
    tables(db)


import os
import logging

import json
import requests
import sys

from urlparse import urljoin

class CaptainHooksClientException(Exception):
    pass

class CaptainHooksClient:

    CAPTAIN_HOOKS_URL = os.getenv('CAPTAIN_HOOKS_URL', 'http://localhost:5000')
    COOKIE_JWT = 'access_token'
    HEADER_CSRF = 'X-CSRFToken'

    def __init__(self, github_token):
        login_reponse = requests.get(
            urljoin(self.CAPTAIN_HOOKS_URL, '/api/me'),
            headers={'X-GITHUB-ACCESS-TOKEN': github_token})

        if 401 == login_reponse.status_code:
            logging.critical('Could not log on Captain Hooks with the given credentials')
            sys.exit(1)

        self.jwt = login_reponse.cookies[self.COOKIE_JWT]
        self.csrf = login_reponse.headers[self.HEADER_CSRF]

    def setup_webhooks(self, organization, repository, hooks_config):
        response = requests.post(
            urljoin(self.CAPTAIN_HOOKS_URL, '/api/github/%s/%s/webhooks' % (organization, repository)),
            cookies={self.COOKIE_JWT: self.jwt},
            headers={self.HEADER_CSRF: self.csrf},
            data=json.dumps(hooks_config))

        if response.status_code > 400:
            raise CaptainHooksClientException('Fail to setup webhooks for %s/%s: %d-%s',
                                              organization,
                                              repository,
                                              response.status_code,
                                              response.text)


from numpy import *
import matplotlib.pyplot as plt
from pandas import *
from math import *
from numpy.linalg import inv

def findRMSE(n,err):
	return ((1/n)*dot(err.transpose(),err))**0.5

def gradientDescent(x,y,theta,iterations,learning_rate):
	rmse=zeros(iterations)
	n=len(x)
	y=y.reshape(y.shape[0],1)
	for i in range(iterations):
		pred=dot(x,theta)
		err=pred-y
		rmse[i]=findRMSE(n,err)
		theta=theta-(learning_rate*(1/n)*dot(x.transpose(),err))
	return theta,rmse

def gradientDescentWithL1(x,y,theta,iterations,learning_rate,alpha):
	rmse=zeros(iterations)
	n=len(x)
	y=y.reshape(y.shape[0],1)
	for i in range(iterations):
		pred=dot(x,theta)
		err=pred-y
		rmse[i]=findRMSE(n,err)
		theta=theta-(learning_rate*((1/n)*dot(x.transpose(),err)))-((learning_rate*alpha*sign(theta))/n)
	return theta,rmse

def gradientDescentWithL2(x,y,theta,iterations,learning_rate,alpha):
	rmse=zeros(iterations)
	n=len(x)
	y=y.reshape(y.shape[0],1)
	for i in range(iterations):
		pred=dot(x,theta)
		err=pred-y
		rmse[i]=findRMSE(n,err)
		theta=theta*(1-(alpha/n))-((learning_rate/n)*(dot(x.transpose(),err)))
	return theta,rmse

def run():
	data=read_csv('data.csv')
	y=data.iloc[:,1].values
	y.reshape(y.shape[0],1)
	learning_rate=0.0001
	alpha=5
	iterations=100000
	x=data.iloc[:,0:1]
	xCopy=copy(x)
	a=ones((x.shape[0],1),dtype='double')
	x=append(a,x,axis=1)
	theta=zeros((len(x[0]),1))
	theta,cost=gradientDescent(x,y,theta,iterations,learning_rate)
	print("Gradient descent without regularisation:")
	print("Slope: ",theta[1][0])
	print("Intercept: ",theta[0][0])

	y1=zeros((len(xCopy),1))
	count=0
	for i in range(len(xCopy)):
		res=theta[1]*xCopy[i]+theta[0]
		y1[i]=res
	err=y1-y
	print("RMSE: ",findRMSE(len(y1),err)[0][0])
	print()
	plt.figure("Without regularisation")
	plt.title("Without regularisation")
	plt.scatter(xCopy,y)
	plt.plot(xCopy,y1,'r')
	plt.xlabel("Brain Weight")
	plt.ylabel("Body Weight")
	plt.grid()

	#L1
	theta2=zeros((len(x[0]),1))
	theta2,cost2=gradientDescentWithL1(x,y,theta2,iterations,learning_rate,alpha)
	print("Gradient descent with L1 regularisation:")
	print("Slope: ",theta2[1][0])
	print("Intercept: ",theta2[0][0])
	y3=zeros((len(xCopy),1))
	for i in range(len(xCopy)):
		res=theta2[1]*xCopy[i]+theta2[0]
		y3[i]=res
	err=y3-y
	print("RMSE: ",findRMSE(len(y3),err)[0][0])
	print()
	plt.figure("L1 regularisation")
	plt.title("L1 regularisation")
	plt.scatter(xCopy,y)
	plt.plot(xCopy,y3,'r')
	plt.xlabel("Brain Weight")
	plt.ylabel("Body Weight")
	plt.grid()

	#L2
	theta1=zeros((len(x[0]),1))
	theta1,cost1=gradientDescentWithL2(x,y,theta1,iterations,learning_rate,alpha)
	print("Gradient descent with L2 regularisation:")
	print("Slope: ",theta1[1][0])
	print("Intercept: ",theta1[0][0])
	y2=zeros((len(xCopy),1))
	for i in range(len(xCopy)):
		res=theta1[1]*xCopy[i]+theta1[0]
		y2[i]=res
	err=y2-y
	print("RMSE: ",findRMSE(len(y2),err)[0][0])
	print()
	plt.figure("L2 regularisation")
	plt.title("L2 regularisation")
	plt.scatter(xCopy,y)
	plt.plot(xCopy,y2,'r')
	plt.xlabel("Brain Weight")
	plt.ylabel("Body Weight")
	plt.grid()

	plt.show()

if __name__=='__main__':
	run()


import flask
from flask import request, url_for, redirect
from flask.ext.jwt import current_user

from segue.core import db, config
from segue.factory import Factory
from segue.decorators import jsoned, jwt_only
from segue.responses import Response

from jwt import Signer
from models import Account, ResetPassword
from services import AccountService
from errors import InvalidLogin, EmailAlreadyInUse, NotAuthorized
from responses import AccountResponse, EmployeesListResponse

class AccountController(object):
    def __init__(self, service=None):
        self.service = service or AccountService()
        self.current_user = current_user

    @jwt_only
    @jsoned
    def get_one(self, account_id):
        result = self.service.get_one(account_id, by=self.current_user) or flask.abort(404)
        return Response(result, AccountResponse).create(), 200

    @jwt_only
    @jsoned
    def modify(self, account_id):
        data = request.get_json()
        result = self.service.modify(account_id, data, by=self.current_user) or flask.abort(404)
        return result, 200

    @jsoned
    def schema(self, name):
        #TODO: REMOVE
        return {}

    @jsoned
    def create(self):
        data = request.get_json()
        return self.service.create(data), 201

    @jsoned
    def login(self):
        data = request.get_json()
        return self.service.login(**data), 200

    @jwt_only
    @jsoned
    def set_certificate_name(self, account_id):
        new_name = request.get_json().get('name',None)
        if not new_name: flask.abort(400)
        return self.service.set_certificate_name(account_id, new_name, by=self.current_user), 200

    @jwt_only
    @jsoned
    def list_employees(self, account_id):
        #TODO: FIX
        from segue.purchase.promocode.models import PromoCodePayment, PromoCode
        from segue.purchase.models import Purchase

        if current_user.id != account_id:
            return [], 404

        result = db.session.query(PromoCodePayment)\
            .join(Purchase)\
            .join(Account)\
            .filter(PromoCodePayment.promocode_id.in_(
                db.session.query(PromoCode.id).filter(PromoCode.creator_id == account_id))).all()

        if result:
            return Response(result, EmployeesListResponse).create(), 200
        return [], 200

    def list_proposals(self, account_id):
        query_string = "?owner_id={}".format(account_id)
        return redirect(url_for('proposals.list') + query_string)

    def list_purchases(self, account_id):
        query_string = "?customer_id={}".format(account_id)
        return redirect(url_for('purchases.list') + query_string)

    def list_promocodes(self, account_id):
        query_string = "?creator_id={}".format(account_id)
        return redirect(url_for('promocodes.list') + query_string)

    def get_caravan(self, account_id):
        query_string = "?owner_id={}".format(account_id)
        return redirect(url_for('caravans.get_one') + query_string)

    def ask_reset(self):
        data = request.get_json()
        self.service.ask_reset(data.get('email'))
        return '', 200

    def get_reset(self, account_id, hash_code):
        reset = self.service.get_reset(account_id, hash_code)
        path = '/#/account/{}/reset/{}'.format(account_id, hash_code)
        return flask.redirect(config.FRONTEND_URL + path)

    @jsoned
    def perform_reset(self, account_id, hash_code):
        data = request.get_json()
        result = self.service.perform_reset(account_id, hash_code, data['password'])
        return dict(email=result.account.email), 200


#!/usr/bin/env python3
##!/bin/sh
#"exec" "python3" "-m" "scoop" "$0"

from collections import Counter
from datetime import datetime
from deap import creator, base, tools, algorithms
from deap.tools.emo import assignCrowdingDist
from itertools import count
from numpy.random import randint, random, seed
from pathways import *
from scoop import futures
import multiprocessing
import numpy as np
import pickle
import shutil, os, sys
import sys

n = len(P)
all_present = (1 << n) - 1
rules_shape = n, max_k, 2

MU = 1024 # Needs to be divisible by 4!
CXPB = 0.9

TRIES = 128

POP_FILE = "pop.dat"
POP_FILE = POP_FILE if len(sys.argv) == 1 else sys.argv[1]

def uniformly_random_individual():
    return randint(all_present, dtype=np.uint32, size=rules_shape)

def random_individual(k=4):
    data = np.full((n, max_k, 2), (1 << n) - 1, dtype=np.uint32)
    for _ in range(k):
        data &= uniformly_random_individual()
    return data

def mutate_k(ind, max_mutations = max_k, indpb = 0.9):
    #print("calling mutate_k")
    for _ in range(max_mutations):
        if random() < indpb:
            ind[randint(n)][randint(max_k)][randint(2)] ^= I << np.uint32(randint(n)) # Flip bit
    return ind

def mutate_anywhere(ind, indpb = 0.02):
    #print("calling mutate_anywhere")

    for m in range(n):
        for k in range(max_k):
            for i in (0,1):
                for j in range(n):
                    if random() < indpb:
                        ind[m][k][i] ^= I << np.uint32(j) # Flip bit
    return ind

def popcount(a):
    a -= np.bitwise_and(np.right_shift(a, 1), 0x55555555)
    a = np.bitwise_and(a, 0x33333333) + np.bitwise_and(np.right_shift(a, 2), 0x33333333)
    return np.right_shift(np.bitwise_and((np.bitwise_and(a + np.right_shift(a, 4), 0xF0F0F0F) * 0x1010101), 0xffffffff), 24)

def hamming(a, b):
    #print("callint hamming")
    return int(popcount(a^b).sum())

def attractor_appearance(rules : np.ndarray, attractor : tuple, maxsteps = 16):
    #print("callint attractor_appearance")
    return model_attractors(interpret_dnf, rules, TRIES,
            canonicalize = True,
            container = Counter,
            maxsteps = maxsteps)[attractor]

def cxTwoPointCopy(ind1, ind2):
    """Execute a two points crossover with copy on the input individuals. The
    copy is required because the slicing in numpy returns a view of the data,
    which leads to a self overwritting in the swap operation. It prevents
    ::

        >>> import numpy
        >>> a = numpy.array((1,2,3,4))
        >>> b = numpy.array((5.6.7.8))
        >>> a[1:3], b[1:3] = b[1:3], a[1:3]
        >>> print(a)
        [1 6 7 4]
        >>> print(b)
        [5 6 7 8]
    """
    #size = len(ind1) = n
    cxpoint1 = randint(1, n)
    cxpoint2 = randint(1, n - 1)
    if cxpoint2 >= cxpoint1:
        cxpoint2 += 1
    else: # Swap the two cx points
        cxpoint1, cxpoint2 = cxpoint2, cxpoint1

    ind1[cxpoint1:cxpoint2], ind2[cxpoint1:cxpoint2] \
        = ind2[cxpoint1:cxpoint2].copy(), ind1[cxpoint1:cxpoint2].copy()

    return ind1, ind2

def cxUniformCopy(ind1, ind2, indpb=0.5):
    """Executes a uniform crossover that modify in place the two
    :term:`sequence` individuals. The attributes are swapped accordingto the
    *indpb* probability.

    :param ind1: The first individual participating in the crossover.
    :param ind2: The second individual participating in the crossover.
    :param indpb: Independent probabily for each attribute to be exchanged.
    :returns: A tuple of two individuals.

    This function uses the :func:`~random.random` function from the python base
    :mod:`random` module.
    """
    for i in range(n):
        if random() < indpb:
            ind1[i], ind2[i] = ind2[i].copy(), ind1[i].copy()

    return ind1, ind2

def cxOnePointCopy(ind1, ind2):
    """Executes a one point crossover on the input :term:`sequence` individuals.
    The two individuals are modified in place. The resulting individuals will
    respectively have the length of the other.

    :param ind1: The first individual participating in the crossover.
    :param ind2: The second individual participating in the crossover.
    :returns: A tuple of two individuals.
    This function uses the :func:`~random.randint` function from the
    python base :mod:`random` module.
    """
    cxpoint = randint(1, n - 1)
    ind1[cxpoint:], ind2[cxpoint:] = ind2[cxpoint:].copy(), ind1[cxpoint:].copy()

    return ind1, ind2

DESIRED_ATTRACTOR = (0b0000000000000000000000000000, 0b1000000000000000000000000000)

RULES_SYM = {head : to_dnf(body, simplify=True) for head,body in rules_original.items()}
RULES = rules_rep(P, RULES_SYM, same_k = True)
assert RULES.shape == rules.shape

def evaluate(ind):
    #print("calling evaluate")
    return attractor_appearance(ind, DESIRED_ATTRACTOR), hamming(ind, RULES)

creator.create("Fitness", base.Fitness, weights=(1, -1))
creator.create("Individual", np.ndarray, fitness=creator.Fitness)
creator.Individual.__hash__ = lambda self: int(self.sum()) # Monkey patch hack for other stuff to work

toolbox = base.Toolbox()
toolbox.register("individual", tools.initIterate, creator.Individual, random_individual)

#toolbox.register("map", futures.map)
processes = int(os.getenv("PROCESSES", 2))
print("Running on %d processes" % processes)
pool = multiprocessing.Pool(processes)
toolbox.register("map", pool.map)

toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", cxTwoPointCopy)
#toolbox.register("mate", cxUniformCopy)
#toolbox.register("mate", cxOnePointCopy)
toolbox.register("mutate", mutate_k)
toolbox.register("evaluate", evaluate)
toolbox.register("select", tools.selNSGA2)
def spea2(pop, mu):
    assignCrowdingDist(pop)
    return tools.selSPEA2(pop, mu)
#toolbox.register("select", spea2)

stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("min", np.min, axis=0)
stats.register("max", np.max, axis=0)

logbook = tools.Logbook()
logbook.header = "gen", "evals", "std", "min", "avg", "max"

def main(seed_=0):
    #seed(seed_)

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    # stats.register("avg", numpy.mean, axis=0)
    # stats.register("std", numpy.std, axis=0)
    stats.register("min", np.min, axis=0)
    stats.register("max", np.max, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "std", "min", "avg", "max"

    if os.path.isfile(POP_FILE):
        with open(POP_FILE,"rb") as dumpfile:
            pop = pickle.load(dumpfile)
    else:
        pop = toolbox.population(n=MU)

    #pop[0] = creator.Individual(rules)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))

    record = stats.compile(pop)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)

    # Begin the generational process
    try:
        for gen in count(1):

            #with open("gen-%d.pop" % gen,"wb") as dumpfile:
            if os.path.isfile(POP_FILE):
                shutil.move(POP_FILE, POP_FILE+".bak")

            with open(POP_FILE,"wb") as dumpfile:
                pickle.dump(pop, dumpfile)
            # Vary the population

            offspring = tools.selTournamentDCD(pop, len(pop))
            offspring = [toolbox.clone(ind) for ind in offspring]

            for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
                if random() <= CXPB:
                    toolbox.mate(ind1, ind2)

                toolbox.mutate(ind1)
                toolbox.mutate(ind2)
                del ind1.fitness.values, ind2.fitness.values

            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit

            # Select the next generation population
            pop = toolbox.select(pop + offspring, MU)
            record = stats.compile(pop)
            logbook.record(gen=gen, evals=len(invalid_ind), **record)
            print(str(datetime.now()))
            print(logbook.stream)
            #print([ind.fitness.values for ind in pop])
    except KeyboardInterrupt:
        return pop, logbook
    #print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0]))

    return pop, logbook

if __name__ == "__main__":
    pop, stats = main()
    for ind in pop:
        print(ind.fitness.values)

    print(stats)


''' merge files (all src and all tgt ) -vertical join (step 2 after IITB split)

From IITB corpus currently we are keeping the data from these groups -chats, Movie Dialogs, general,Hi-Eng Word-Linkage,Admin Dictionary,
Admin Examples,Admin Definitions, ted talks, Indic Multi-Parallel, JudicialI, Govt Websites, Book Translations,
aditional data- law commison, letters
'''

def file_merger(file_names,merged_file_location):
    with open("{0}".format(merged_file_location), 'w') as outfile:
        for fname in file_names:
            with open(fname) as infile:
                for line in infile:
                    outfile.write(line)


from django.urls import path

from . import views

urlpatterns = [
  path('signup/', views.signup, name='signup'),
  path('login/', views.do_login, name='login'),
  path('logout/', views.do_logout, name='logout'),
  path('profile/<int:primary_key>/', views.profile, name='profile'),
  path('rate/<int:primary_key>/', views.rate_user, name='rating'),
]


"""
936. Stamping The Sequence

You want to form a target string of lowercase letters.

At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters.

On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns.

For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.)

If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array.

For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc".

Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.

Example 1:

Input: stamp = "abc", target = "ababc"
Output: [0,2]
([1,0,2] would also be accepted as an answer, as well as some other answers.)
Example 2:

Input: stamp = "abca", target = "aabcaca"
Output: [3,0,1]

Note:

1 <= stamp.length <= target.length <= 1000
stamp and target only contain lowercase letters.
"""

class Solution:
    def movesToStamp(self, stamp: str, target: str) -> List[int]:
        S, T = list(stamp), list(target)
        cnt = 0
        ans = []
        visited = [False] * len(target)
        while cnt < len(T):
            done = False
            for i in range(len(T) - len(S) + 1):
                if not visited[i] and self.canStamp(S, T, i):
                    visited[i] = True
                    done = True
                    ans.append(i)
                    cnt += self.doStamp(S, T, i)
                    break
            if not done:
                return []
        return ans[::-1]

    def canStamp(self, S, T, idx):
        for i in range(idx, idx + len(S)):
            if T[i] != "?" and S[i-idx] != T[i]:
                return False
        return True

    def doStamp(self, S, T, idx):
        cnt = 0
        for i in range(idx, idx + len(S)):
            if T[i] == S[i-idx]:
                cnt += 1
                T[i] = "?"
        return cnt


import socket
import struct
import mmap
from Utils import setVarInt, flog
from CreateMessage import createMessage
from EstablishBitcoinConnection import checkMessage, \
                                parseVersionPayload, \
                                parseMsgHdr, \
                                establishConnection
from PingPong import sendPongMessage
from GetAddresses import parsePingPongPayload, \
                                parseAddrPayload, \
                                parseGetBlocksGetHeadersPayload
from GetAllBlocks import parseHeadersPayload, \
                                parseTxPayload, \
                                parseBlockPayload, \
                                parseSendCompactPayload, \
                                parseFeeFilterPayload, \
                                parseInvPayload

MSGHDR_SIZE = 24

CMD_FN_MAP = {
    'version': parseVersionPayload,
    'addr': parseAddrPayload,
#    'filterload': parseFilterLoadPayload,
#    'filteradd': parseFilterAddPayload,
#    'merkleblock': parseMerkleBlockPayload,
    'ping': parsePingPongPayload,
#    'pong': parsePingPongPayload,
    'feefilter': parseFeeFilterPayload,
    'inv': parseInvPayload,
#    'getdata': parseInvPayload,
#    'notfound': parseInvPayload,
    'tx': parseTxPayload,
    'block': parseBlockPayload,
    'getblocks': parseGetBlocksGetHeadersPayload,
    'getheaders': parseGetBlocksGetHeadersPayload,
    'headers': parseHeadersPayload,
#    'reject': parseRejectPayload,
#    'sendcmpct': parseSendCompactPayload,
#    'cmpctblock': parseCompactBlockPayload,
#    'getblocktxn': parseGetBlockTxnPayload,
#    'blocktxn': parseBlockTxnPayload
}

def recvAll(s: socket, payloadlen: int):
    payload_b = b''
    length = payloadlen
    while True:
        recvd_b = s.recv(length)
        payload_b += recvd_b
        if len(payload_b) == payloadlen:
            break
        length = payloadlen - len(payload_b)
    return payload_b

def recvMsg(s: socket):
    global MSGHDR_SIZE, CMD_FN_MAP
    msghdr_b = s.recv(MSGHDR_SIZE)
    msg = parseMsgHdr(msghdr_b)
    payloadlen = msg['length']
    payload_b = recvAll(s, payloadlen)
    checkMessage(msg, payload_b)
    payload_m = mmap.mmap(-1, payloadlen + 1)
    payload_m.write(payload_b)
    payload_m.seek(0)
    msg['payload'] = {}
    if payloadlen > 0:
        msg['payload'] = CMD_FN_MAP[msg['command']](payload_m, payloadlen)
    print('<== msg = %s' % msg, file=flog)
    print('<== msg = %s' % msg)
    return msg

def createGetDataTxPayload(payload: dict):
    MSG_TX = 1
    data_b = b''
    count = 0
    for i in range(payload['count']):
        # we only request data for tx
        if payload['inventory'][i]['type'] == MSG_TX:
            type_b = struct.pack('<L', payload['inventory'][i]['type'])
            hash_b = bytes.fromhex(payload['inventory'][i]['hash'])[::-1]
            data_b += type_b + hash_b
            count += 1
    count_b = setVarInt(count)
    payload_b = count_b + data_b
    return count, payload_b

def waitForInvMessage(s: socket):
    while True:
        recvmsg = recvMsg(s)
        if recvmsg['command'] == 'inv':
            break
        elif recvmsg['command'] == 'ping':
            sendPongMessage(s, recvmsg)
    print('Received INV', file=flog)
    return recvmsg

def waitForTxMsg(s: socket):
    while True:
        recvmsg = recvMsg(s)
        if recvmsg['command'] == 'tx':
            break
        elif recvmsg['command'] == 'ping':
            sendPongMessage(s, recvmsg)

def sendGetDataMessageWithTx(s: socket, recvmsg: dict):
    sndcmd = 'getdata'
    count, payload = createGetDataTxPayload(recvmsg['payload'])
    sndmsg = createMessage(sndcmd, payload)
    s.send(sndmsg)
    print('==> cmd = %s, msg = %s' % (sndcmd, sndmsg.hex()), file=flog)
    return count

def waitAndHandleInvTxnMessage(s: socket):
    recvmsg = waitForInvMessage(s)
    count = sendGetDataMessageWithTx(s, recvmsg)
    for i in range(count):
        waitForTxMsg(s)

def sendrecvHeadersData(s: socket):
    	    waitAndHandleInvTxnMessage(s)

def sendrecvHandler(s: socket, version: int):
    if establishConnection(s, version) == False:
        print('Establish connection failed', file=flog)
        return
    sendrecvHeadersData(s)


#sample test code sub-part of the entire code
#it checks which element doesnt belong in the pre-defined list.
#obviously only works for integers,gotta change to str for string values.

lst1=[1,2,3]
a = input().split(' ')

user_input = [int(i) for i in a]

for _input in user_input:
    for num in [int(i) for i in str(_input)]:
        if num not in lst1:
            print(f"{num} does not belong to lst1")


from django.core.management.base import BaseCommand, CommandError
import logging
logger = logging.getLogger('labsmanager')

from django.db.models import Q
from fund.models import AmountHistory

class Command(BaseCommand):
    help = 'remove duplicate in AmountHistory'

    # def add_arguments(self, parser):
    #     # Named (optional) arguments
    #     parser.add_argument(
    #         '--force',
    #         action='store_true',
    #         help='Force Update clean',
    #     )

    def handle(self, *args, **options):
        logger.debug("COMMAND remove_duplicate CALLED")
        self.stdout.write("COMMAND remove_duplicate", ending='\n')
        unik=AmountHistory.objects.values('content_type','object_id', 'value_date', 'amount').distinct()
        logger.debug(f" {AmountHistory.objects.count()} object found and {unik.count()} unic entry found")

        for ah in unik:
            sel=AmountHistory.objects.filter(content_type=ah['content_type'], object_id=ah['object_id'], value_date=ah['value_date']).values_list('pk', flat=True)[1:]
            if sel.count()>0:
                AmountHistory.objects.filter(pk__in=sel).delete()
            #AmountHistory.objects.filter(pk__in=Email.objects.filter(email=email).values_list('id', flat=True)[1:]).delete()


from P2VV.RooFitWrappers import *
from P2VV.Load import P2VVLibrary
from P2VV.Load import LHCbStyle
from ROOT import RooCBShape as CrystalBall
from ROOT import RooMsgService
import os
## RooMsgService.instance().addStream(RooFit.DEBUG,RooFit.Topic(RooFit.Eval))

dataSample = '2011'
assert(dataSample in ['2011', '2012'])

obj = RooObject( workspace = 'w')
w = obj.ws()

from math import pi
t  = RealVar('time', Title = 'decay time', Unit='ps', Observable = True, MinMax=(-5, 14))
m  = RealVar('mass', Title = 'B mass', Unit = 'MeV', Observable = True, MinMax = (5200, 5550),
             Ranges =  { 'leftsideband'  : ( None, 5330 )
                         , 'signal'        : ( 5330, 5410 )
                         , 'rightsideband' : ( 5410, None )
                         } )
mpsi = RealVar('mdau1', Title = 'J/psi mass', Unit = 'MeV', Observable = True, MinMax = (3030, 3150))
mphi = RealVar('mdau2', Title = 'phi mass', Unit = 'MeV', Observable = True, MinMax = (990, 1050))
st = RealVar('sigmat',Title = '#sigma(t)', Unit = 'ps', Observable = True, MinMax = (0.0001, 0.12))
zerr = RealVar('B_s0_bpv_zerr', Title = 'Best PV Z error', Unit = 'mm', Observable = True, MinMax = (0, 1))
from math import pi
cpsi = RealVar('helcosthetaK', Title = 'cpsi', Observable = True, MinMax = (-1, 1))
ctheta = RealVar('helcosthetaL', Title = 'ctheta', Observable = True, MinMax = (-1, 1))
phi = RealVar('helphi', Title = 'helphi', Observable = True, MinMax = (-pi, pi))
angles = [cpsi, ctheta, phi]

# add 20 bins for caching the normalization integral
for i in [ st ] : i.setBins( 20 , 'cache' )

# Categories
hlt1_excl_biased = Category('hlt1_excl_biased_dec', States = {'excl_biased' : 1, 'unbiased' : 0}, Observable = True)
hlt1_biased = Category('hlt1_biased', States = {'biased' : 1, 'not_biased' : 0}, Observable = True)
hlt1_unbiased = Category('hlt1_unbiased_dec', States = {'unbiased' : 1, 'not_unbiased' : 0}, Observable = True)
hlt2_biased = Category('hlt2_biased', States = {'biased' : 1, 'not_biased' : 0}, Observable = True)
hlt2_unbiased = Category('hlt2_unbiased', States = {'unbiased' : 1, 'not_unbiased' : 0}, Observable = True)
selected = Category('sel', States = {'Selected' : 1, 'NotSelected' : 0})

observables = [t, m, mpsi, mphi, st, hlt1_excl_biased, selected, zerr,
               hlt1_biased, hlt1_unbiased, hlt2_biased, hlt2_unbiased] + angles

project_vars = [st, hlt1_excl_biased]

# now build the actual signal PDF...
from ROOT import RooGaussian as Gaussian
from ROOT import RooExponential as Exponential
from ROOT import RooDecay as Decay

# B mass pdf
from P2VV.Parameterizations.MassPDFs import LP2011_Signal_Mass as Signal_BMass, LP2011_Background_Mass as Background_BMass
sig_m = Signal_BMass(Name = 'sig_m', mass = m, m_sig_mean = dict(Value = 5365, MinMax = (5363,5372)))

# J/psi mass pdf
mpsi_mean  = RealVar('mpsi_mean',   Unit = 'MeV', Value = 3097, MinMax = (3070, 3110))
mpsi_sigma = RealVar('mpsi_sigma',  Unit = 'MeV', Value = 10, MinMax = (5, 20))
mpsi_alpha = RealVar('mpsi_alpha',  Unit = '', Value = 1.36, MinMax = (0.5, 3))
mpsi_n = RealVar('mpsi_n',  Unit = '', Value = 2, MinMax = (0.1, 4), Constant = True)
psi_m  = Pdf(Name = 'psi_m', Type = CrystalBall, Parameters = [mpsi, mpsi_mean, mpsi_sigma, mpsi_alpha, mpsi_n])
# J/psi background
psi_c = RealVar( 'psi_c',  Unit = '1/MeV', Value = -0.0004, MinMax = (-0.1, -0.0000001))
bkg_mpsi = Pdf(Name = 'bkg_mpsi',  Type = Exponential, Parameters = [mpsi, psi_c])

# Create combinatorical background component
bkg_m = Background_BMass( Name = 'bkg_m', mass = m, m_bkg_exp  = dict( Name = 'm_bkg_exp' ) )
background = Component('background', (bkg_m.pdf(), bkg_mpsi), Yield = (100000,100,500000) )

psi_background = Component('psi_background', (bkg_m.pdf(), psi_m), Yield= (200000,500,500000) )

from P2VV.Utilities.DataHandling import readData
tree_name = 'DecayTree'
prefix = '/stuff/PhD' if os.path.exists('/stuff') else '/bfys/raaij'
if dataSample == '2011':
    input_file = os.path.join(prefix, 'p2vv/data/Bs2JpsiPhi_ntupleB_for_fitting_20121012_MagDownMagUp.root')
elif dataSample == '2012':
    input_file = os.path.join(prefix, '/stuff/PhD/p2vv/data/Bs2JpsiPhi_2012_ntupleB_20121212.root')
else:
    raise RuntimeError

cut = 'runNumber > 0 && sel == 1 && sel_cleantail == 1 && (hlt1_biased == 1 || hlt1_unbiased_dec == 1) && hlt2_biased == 1 && '
## cut = 'runNumber > 0 && sel == 1 && sel_cleantail == 1 && hlt1_unbiased_dec == 1 && hlt2_unbiased == 1 && '
cut += ' && '.join(['%s < 4' % e for e in ['muplus_track_chi2ndof', 'muminus_track_chi2ndof', 'Kplus_track_chi2ndof', 'Kminus_track_chi2ndof']])
data = readData(input_file, tree_name, ntupleCuts = cut,
                NTuple = True, observables = observables)

# Create signal component
signal = Component('signal', (sig_m.pdf(), psi_m), Yield = (27437,10000,50000))

## Build PDF
mass_pdf = buildPdf(Components = (signal, background), Observables = (m, ), Name = 'mass_pdf')
mass_pdf.Print("t")

## Fit options
fitOpts = dict(NumCPU = 4, Timer = 1, Save = True,
               Verbose = False, Optimize = 2, Minimizer = 'Minuit2')

# make sweighted dataset. TODO: use mumu mass as well...
from P2VV.Utilities.SWeights import SData, splot

single_bin_result = mass_pdf.fitTo(data, **fitOpts)

from P2VV.Utilities.General import getSplitPar
# categories for splitting the PDF
# get mass parameters that are split
from ROOT import RooBinning

split_cats = [[hlt1_excl_biased]]
split_pars = [[par for par in mass_pdf.Parameters() if par.getAttribute('Yield')]]

# build simultaneous mass PDF
sWeight_mass_pdf = SimultaneousPdf(mass_pdf.GetName() + '_simul',
                                   MasterPdf       = mass_pdf,
                                   SplitCategories = split_cats,
                                   SplitParameters = split_pars)

from math import sqrt
# set yields for categories
split_cat_pars = sWeight_mass_pdf.getVariables()
for ct in hlt1_excl_biased:
    sig_yield = getSplitPar('N_signal', ct.GetName(), split_cat_pars)
    bkg_yield = getSplitPar('N_background', ct.GetName(), split_cat_pars)

    sel_str = '!(%s-%d)' % (hlt1_excl_biased.GetName(), ct.getVal())

    nEv    = data.sumEntries()
    nEvBin = data.sumEntries(sel_str)

    sig_yield.setVal( sig_yield.getVal() * nEvBin / nEv )
    sig_yield.setError( sqrt( sig_yield.getVal() ) )
    sig_yield.setMin(0.)
    sig_yield.setMax(nEvBin)
    bkg_yield.setVal( bkg_yield.getVal() * nEvBin / nEv )
    bkg_yield.setError( sqrt( bkg_yield.getVal() ) )
    bkg_yield.setMin(0.)
    bkg_yield.setMax(nEvBin)

for i in range(5):
    sWeight_mass_result = sWeight_mass_pdf.fitTo(data, **fitOpts)
    if sWeight_mass_result.status() == 0:
        break
assert(sWeight_mass_result.status() == 0)
sWeight_mass_result.SetName('sWeight_mass_result')

## Canvas for correlation histograms
from ROOT import TCanvas
corr_canvas = TCanvas('corr_canvas', 'corr_canvas', 1000, 500)
corr_canvas.Divide(2, 1)
from ROOT import gStyle
gStyle.SetPalette(1)

## Plot correlation histogram
corr_canvas.cd(1)
corr_hist_sWmass = sWeight_mass_result.correlationHist()
corr_hist_sWmass.GetXaxis().SetLabelSize(0.03)
corr_hist_sWmass.GetYaxis().SetLabelSize(0.03)
corr_hist_sWmass.SetContour(20)
corr_hist_sWmass.Draw('colz')

from P2VV.Utilities.SWeights import SData
sData = SData(Pdf = sWeight_mass_pdf, Data = data, Name = 'SimulMassSPlot')
sig_sdata = sData.data('signal')
bkg_sdata = sData.data('background')

# Plot mass pdf

paper_calibration = FormulaVar(Name = 'st_calibration', Formula = '1.45 * @0', Arguments = [st])
dg_calibration = FormulaVar(Name = 'dg_calibration', Formula = "-6.81838 * @0 * @0 + 1.72 * @0", Arguments = [st])
sg_calibration = FormulaVar(Name = 'sg_calibration', Formula = "-0.00455 + 1.92 * @0-9.9352 * @0 * @0", Arguments = [st])
linear_calibration = FormulaVar(Name = 'linear_calibration', Formula = "0.00555 + 1.267 * @0", Arguments = [st])
from P2VV.Dilution import dilution
r = signal_dilution(psi_sdata, st, paper_calibration)
r = signal_dilution(psi_sdata, st, dg_calibration)
# r = signal_dilution(sig_sdata, st, sg_calibration)
# r = signal_dilution(sig_sdata, st, linear_calibration)


from collections import namedtuple
from typing import Dict, Tuple, List, Any, Callable
from overrides import overrides
from parser.utils.utils_conllu import parse_line, DEFAULT_FIELDS, process_multiword_tokens
from conllu import parse_incr
from allennlp.common.file_utils import cached_path
import logging
from allennlp.data.tokenizers.word_splitter import SpacyWordSplitter

UD_Sentence = namedtuple(typename='UD_Sentence',
                      field_names=['ids', 'words', 'lemmas', 'upos_tags',
                                   'xpos_tags', 'feats', 'heads', 'rels',
                                   'multiword_ids', 'multiword_forms'],
                      defaults=[None]*10)

class UniversalDependenciesDatasetReader(object):
    def __init__(self):
        self.sentences = []
        self.ids = []
        self.ROOT = "<ROOT>"

    def load(self, file_path):
        # if `file_path` is a URL, redirect to the cache
        file_path = cached_path(file_path)
        counter = 1
        with open(file_path, 'r') as conllu_file:

            for annotation in parse_incr(conllu_file):
                # CoNLLU annotations sometimes add back in words that have been elided
                # in the original sentence; we remove these, as we're just predicting
                # dependencies for the original sentence.
                # We filter by None here as elided words have a non-integer word id,
                # and we replace these word ids with None in process_multiword_tokens.
                annotation = process_multiword_tokens(annotation)
                multiword_tokens = [x for x in annotation if x["multi_id"] is not None]
                annotation = [x for x in annotation if x["id"] is not None]

                if len(annotation) == 0:
                    continue

                def get_field(tag: str, map_fn: Callable[[Any], Any] = None) -> List[Any]:
                    map_fn = map_fn if map_fn is not None else lambda x: x
                    return [map_fn(x[tag]) if x[tag] is not None else "_" for x in annotation if tag in x]

                # Extract multiword token rows (not used for prediction, purely for evaluation)
                ids = [x["id"] for x in annotation]
                multiword_ids = [x["multi_id"] for x in multiword_tokens]
                multiword_forms = [x["form"] for x in multiword_tokens]

                words = get_field("form")
                lemmas = get_field("lemma")
                upos_tags = get_field("upostag")
                xpos_tags = get_field("xpostag")
                feats = get_field("feats", lambda x: "|".join(k + "=" + v for k, v in x.items())
                                                     if hasattr(x, "items") else "_")
                heads = get_field("head")
                dep_rels = get_field("deprel")
                sentence = UD_Sentence(ids,words,lemmas,upos_tags,xpos_tags,feats,heads, dep_rels,multiword_ids,multiword_forms)
                self.sentences.append(sentence)
                self.ids.append(counter)
                counter = counter + 1

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

    def __getitem__(self, item):
        return self.sentences[item]

    @property
    def words(self):
        return [[self.ROOT] + list(sentence.words) for sentence in self.sentences]

    @property
    def tags(self):
        return [[self.ROOT] + list(sentence.upos_tags) for sentence in self.sentences]

    @property
    def heads(self):
        return [[0] + list(map(int, sentence.heads)) for sentence in self.sentences]

    @property
    def rels(self):
        return [[self.ROOT] + list(sentence.rels) for sentence in self.sentences]

    @heads.setter
    def heads(self, sequences):
        self.sentences = [sentence._replace(heads=sequence)
                          for sentence, sequence in zip(self.sentences, sequences)]

    @rels.setter
    def rels(self, sequences):
        self.sentences = [sentence._replace(rels=sequence)
                          for sentence, sequence in zip(self.sentences, sequences)]

    def save(self, fname):
        with open(fname, 'w') as f:
            for item in self.sentences:
                output = self.write(item)
                f.write(output)

    def write(self, outputs) :
        outputs = dict(outputs._asdict())
        word_count = len([word for word in outputs["words"]])
        lines = zip(*[outputs[k] if k in outputs else ["_"] * word_count
                      for k in ["ids", "words", "lemmas", "upos_tags", "xpos_tags", "feats",
                                "heads", "rels"]])

        multiword_map = None
        if outputs["multiword_ids"]:
            multiword_ids = [[id] + [int(x) for x in id.split("-")] for id in outputs["multiword_ids"]]
            multiword_forms = outputs["multiword_forms"]
            multiword_map = {start: (id_, form) for (id_, start, end), form in zip(multiword_ids, multiword_forms)}

        output_lines = []
        for i, line in enumerate(lines):
            line = [str(l) for l in line]

            # Handle multiword tokens
            if multiword_map and i+1 in multiword_map:
                id_, form = multiword_map[i+1]
                row = f"{id_}\t{form}" + "".join(["\t_"] * 8)
                output_lines.append(row)

            row = "\t".join(line) + "".join(["\t_"] * 2)
            output_lines.append(row)

        return "\n".join(output_lines) + "\n\n"

class UniversalDependenciesRawDatasetReader(UniversalDependenciesDatasetReader):
    def __init__(self, language):
        super().__init__()
        self.tokenizer = SpacyWordSplitter(language=language,pos_tags=True)

    @overrides
    def load(self, file_path):
        file_path = cached_path(file_path)
        counter = 1
        with open(file_path, 'r') as conllu_file:
            for sentence in conllu_file:
                if sentence:
                    words = [word.text for word in self.tokenizer.split_words(sentence)]
                    upos_tags = [word.tag_ for word in self.tokenizer.split_words(sentence)]
                    xpos_tags = upos_tags
                    seq_len = len(words)
                    ids = [i+1 for i in range(seq_len)]
                    lemmas = ["_" for i in range(seq_len)]
                    feats = lemmas
                    heads = [1 for i in range(seq_len)]
                    dep_rels = ["<UNK>" for i in range(seq_len)]
                    multiword_ids = []
                    multiword_forms = []
                    sentence = UD_Sentence(ids,words,lemmas,upos_tags,xpos_tags,feats,heads, dep_rels,multiword_ids,multiword_forms)
                    self.sentences.append(sentence)
                    self.ids.append(counter)
                    counter = counter + 1


# put files with specified partial name in folder with the specified name
import os, sys, shutil, glob
from os.path import basename

fileStr = sys.argv[1]
fdrName = fileStr
os.mkdir(fdrName)

files = [f for f in os.listdir("./") if f.find(fileStr) != -1 and os.path.isfile("./" + f)]

for file in files:
  # print(file)
  shutil.move(file, fdrName + '/' + file)


import pygame_toolbox.graphics as ptg
import pygame_toolbox.graphics.widgets as ptgw
import pygame, sys, random

def close():
    pygame.quit()
    sys.exit()

class Screen_1(ptg.Menu):
    def __init__(self):
        self.header = ['Screen 1']
        self.buttons = [['Screen 2',lambda:2],['Quit',close]]
        ptg.Menu.__init__(self,(800,600),(150,69,69),self.header,self.buttons)

        self.widgetlist += [ptgw.Checkbox("option a",(200,200),(17,17),True,self.image,'c')]

class Screen_2(ptg.Menu):
    def __init__(self,widget_info):
        header = ['Screen 2','%s was %s' % (widget_info[0][0],bool(widget_info[0][1]))]
        buttons = [['Screen 1',lambda:1],['Quit',close]]
        ptg.Menu.__init__(self,(800,600),(150,69,150),header,buttons)

class Main(object):
    def __init__(self):
        self.progress = 1
        self.clock = pygame.time.Clock()

    def update(self,screen):
        while True:
            if self.progress == 1:
                self.progress = Screen_1().update(screen,self.clock)
                self.widget_info = self.progress[1]
                self.progress = self.progress[0]
            elif self.progress == 2:
                self.progress = Screen_2(self.widget_info).update(screen,self.clock)

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    Main().update(screen)

General Python/widgets develop/widgets checkbox.py


import unittest
from shape import Shape

class ShapeTest(unittest.TestCase):

	def test_get_position(self):
   		shape = Shape()

   		shape.set_position(10, 20)
   		self.assertEqual(shape.get_position(), (10, 20))

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


from rest_framework import routers
from django.conf.urls import url, include
from django_todolist.api import views

router = routers.DefaultRouter(trailing_slash=False)
router.register(r'todos', views.TodoViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/',
        include('rest_framework.urls', namespace='rest_framework'))
]


# coding: utf-8
import tensorflow as tf

with tf.variable_scope("conv1"):
    matrix1 = [[2.0, 2.0, 3.0], [1.0, 2.0, 3.0], [2.0, 5.0, 3.0]]
    sotfmax_x = tf.nn.softmax(matrix1,axis=1)

with tf.Session() as sess:
    result = sess.run(sotfmax_x)
    print(result)


from bs4 import BeautifulSoup
import requests
import pprint
from scrapers.base_scraper import BaseScraper

class CNAScraper(BaseScraper):
	@classmethod
	def get_text(cls, url):
	    res = requests.get(url, allow_redirects=False)
	    soup = BeautifulSoup(res.text, 'html.parser')
	    print("Prasing article..")
	    print(res.status_code)
	    return ''.join([_.get_text() for _ in soup.find_all('div', class_='c-rte--article')])

from sys import argv
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--url', type=str, help='url to scrape')

if __name__ == '__main__':
	args = parser.parse_args(argv[1:])
	url = args.url
	print(CNAScraper.get_text(url))


#!/usr/bin/python3
"""Script that fetches https://alx-intranet.hbtn.io/status"""
import requests
import sys

if __name__ == "__main__":
    res_msg = requests.get(sys.argv[1])
    response = res_msg.status_code
    if response >= 400:
        print(f"Error code: {response}")
    else:
        print(res_msg.text)


import random
choices = ["rock", "paper", "scissors"]
while True:
    player_choices = input("Choose your choice (rock, paper, scissors) to play or press Enter to quit: ").lower()

    if not player_choices:
        break
    computer_choices = random.choice(choices)
    print(f"Computer chose {computer_choices}")
    if player_choices in choices:
        if player_choices == computer_choices:
            print("Match tie")
        elif (player_choices == 'rock' and computer_choices == 'scissors') or \
             (player_choices == 'scissors' and computer_choices == 'paper') or \
             (player_choices == 'paper' and computer_choices == 'rock'):
            print("You win")
        else:
            print("Computer wins")
    else:
        print("Invalid choice. Please choose Rock, Paper, or Scissors.")


"""
Implement a method to perform basic string compression using the counts of repeated characters.
For example the string aabcccccaaa would become a2b1c5a3. If the compressed string would not become
smaller than the original string, you method should return the original string. You can assume
the string has only uppercase and lowercase letters.
"""

def compressed(string):
    c = 0
    str_arr = []
    for i in range(0, len(string)):
        c += 1
        if i+1 >= len(string) or string[i] != string[i+1]:
            str_arr.append(string[i])
            str_arr.append(str(c))
            c = 0
    return "".join(str_arr)

print(compressed("aaab"))


import glob

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")
def index_():
    articles = get_articles()
    return render_template("index.html", articles=articles)

@app.route("/articles")
def articles_():
    path = request.args.get("title")
    if path:
        article = get_article(path)
        return render_template("article.html", article=article)
    else:
        return redirect()

def get_articles() -> list:
    articles = []
    for article_path in get_article_paths():
        articles.append({"title": article_path, "path": article_path})
    return articles

def get_article_paths() -> dict:
    article_file_paths = [path.split("/")[-1] for path in glob.glob("./articles/*")]
    return article_file_paths

def get_article(path: str) -> dict:
    with open("./articles/"+path, encoding="utf-8") as f:
        body = f.read()
    return {"title": path, "body": body}

if __name__ == "__main__":
    app.run(port=5000, host="0.0.0.0")