import torch
import torch.nn as nn
import torch.nn.functional as F

from utils.constants import GNN_MSG_KEY, GNN_NODE_FEAT_IN_KEY, GNN_NODE_FEAT_OUT_KEY, GNN_EDGE_FEAT_KEY, GNN_AGG_MSG_KEY, GNN_EDGE_LABELS_KEY, GNN_EDGE_TYPES_KEY, GNN_NODE_TYPES_KEY, GNN_NODE_LABELS_KEY

class GATLayer(nn.Module):
    def __init__(self, g, node_dim, edge_dim, node_ft_out_dim, edge_ft_out_dim, out_dim):
        super(GATLayer, self).__init__()
        self.g = g

        self.weighted_agg_edge = WeightedAggEdge(g, node_dim, node_ft_out_dim, edge_dim, edge_ft_out_dim)
        self.node_lv = BaseGAT_Modified(g, node_ft_out_dim, edge_ft_out_dim, out_dim)
        z_node_lv_dim = node_ft_out_dim + edge_ft_out_dim  +  node_ft_out_dim
        self.semantic_lv = SemLevelGAT(g, z_node_lv_dim, out_dim)

    def forward(self, node_features, edge_features, g):
        if g is not None:
            self.g = g

        # weighted-sum edges features
        z_, e_weighted = self.weighted_agg_edge(node_features, edge_features, self.g)

        z_node_lv, e_weighted = self.node_lv(z_, e_weighted, self.g)
        z_final = self.semantic_lv(z_node_lv, self.g)

        return z_final, e_weighted

class WeightedAggEdge(nn.Module):
    """
    Weight by edge features (edge_lbl_ft)
    """
    def __init__(self, g, node_in_dim, node_ft_out_dim, edge_dim, edge_ft_out_dim):
        super(WeightedAggEdge, self).__init__()
        self.g = g

        self.fc_n = nn.Linear(node_in_dim, node_ft_out_dim, bias=False)
        self.fc_e = nn.Linear(edge_dim, edge_ft_out_dim, bias=False)
        # print('edge_dim', edge_dim)
        # print('edge_ft_out_dim', edge_ft_out_dim)

        input_concat_dim = node_ft_out_dim + edge_ft_out_dim
        # input_concat_dim = edge_ft_out_dim
        self.e_attn_fc = nn.Linear(input_concat_dim, 1, bias=False)

    def filter_by_dst(self, edges, dst_tensor_to_filter):

        # self.dst_tensor_to_filter = torch.tensor([0.,0.])
        dst_nodes = edges.dst['node_feat']
        diff = dst_nodes - dst_tensor_to_filter
        diff_sum = abs(diff).sum(-1)

        loc = torch.where(diff_sum==0)
        return loc

    def edge_weight_src(self, edges):
        e_ft = edges.data['e_weighted']
        src_ft = edges.src['node_feat']

        z2 = torch.cat([e_ft, src_ft], dim=2)
        # z2 = e_ft
        a = self.e_attn_fc(z2)
        e = F.leaky_relu(a)
        gamma = F.softmax(e, dim=1)
        e_weighted = gamma * e_ft
        # e_weighted = gamma * z2
        # print('e_ft', e_ft)
        # print('~~~ e_weighted', e_weighted)
        # print('~~~ e_ft', e_ft.shape)
        # print('~~~ e_weighted', e_weighted.shape)

        return {'e_weighted': e_weighted, 'src_id': edges.src['nid'], 'dst_id': edges.dst['nid']}

    '''
    def edge_weight(self, edges):
        # print('num edges', len(edges))
        # e_fc = self.fc(edges.data[GNN_EDGE_LABELS_KEY])
        print('Begin edge_weight')
        for nid in range(self.g.number_of_nodes()):
            dst_tensor_to_filter = self.g.ndata['node_feat'][nid]
            loc = self.filter_by_dst(edges, dst_tensor_to_filter)

            # print('nid', nid, 'loc', loc)

            if loc[0].shape[0] > 0:
                # print('loc[0].shape[0]', loc[0].shape[0])
                edata_filtered = edges.data['e_weighted'][loc]
                esrc_filtered = edges.src['node_feat'][loc]

                z2 = torch.cat([edata_filtered, esrc_filtered], dim=1)
                a = self.e_attn_fc(z2)
                e = F.leaky_relu(a)
                gamma = F.softmax(e, dim=1)
                e_weighted = gamma * edata_filtered
                edges.data['e_weighted'][loc] = e_weighted

        # Apply func to source node of these edges
        # self.e_w_from_node = edges.data['e_weighted']
        # self.g.apply_nodes(func=self.agg_edge)

        print('End edge_weight')

        return {'e_weighted': edges.data['e_weighted'], 'src_id': edges.src['nid'], 'dst_id': edges.dst['nid']}
    '''

    def forward(self, h, e, g):
        if g is not None:
            self.g = g

        self.g.ndata['node_feat'] = self.fc_n(h)
        # e = self.g.edata[GNN_EDGE_LABELS_KEY]
        # print('e', e.shape)
        self.g.edata['e_weighted'] = self.fc_e(e)

        self.g.group_apply_edges(func=self.edge_weight_src, group_by='src')
        # print('~~~~~~~Done calculating e_weighted!\n------------------------')

        return self.g.ndata.pop('node_feat'), self.g.edata.pop('e_weighted')

class BaseGAT_Modified(nn.Module):
    """
    Weight by neighbor nodes
    """
    def __init__(self, g, node_ft_out_dim, edge_ft_out_dim, out_dim):
        super(BaseGAT_Modified, self).__init__()
        self.g = g

        self.attn_fc = nn.Linear(node_ft_out_dim + edge_ft_out_dim  +  node_ft_out_dim, 1, bias=False)

    def edge_att(self, edges):
        src_cat_edge_ft = torch.cat((edges.src['z'], edges.data['e_weighted']), dim=1)
        z2 = torch.cat((edges.src['z'], edges.data['e_weighted'], edges.dst['z']), dim=1)
        # z2 = torch.cat((edges.data['e_weighted'], edges.dst['z']), dim=1)

        # print("### edges.src['z']", edges.src['z'])
        # print("=== edges.data['e_weighted']", edges.data['e_weighted'])
        # print("=== edges.dst['z']", edges.dst['z'])

        a = self.attn_fc(z2)
        return {'e': F.leaky_relu(a), 'src_cat_edge_ft': src_cat_edge_ft}

    def message_func(self, edges):
        # message UDF for equation (3) & (4)
        src_cat_edge_ft = edges.data['src_cat_edge_ft']
        z = torch.cat((edges.data['src_cat_edge_ft'], edges.dst['z']), dim=1)
        # z = torch.cat((edges.data['e_weighted'], edges.dst['z']), dim=1)
        return {'z_src': src_cat_edge_ft, 'z': z, 'e': edges.data['e'], 'edge_type': edges.data[GNN_EDGE_TYPES_KEY], 'src_id': edges.src['nid']}

    def reduce_func_w_sem(self, nodes):
        ''' Node-level attention '''
        # accum = torch.sum((nodes.mailbox['z']), 1)

        # z = nodes.mailbox['z_src']
        z = nodes.mailbox['z']
        e = nodes.mailbox['e']
        e_type = nodes.mailbox['edge_type']

        e_type_shape = e_type.shape
        num_edge = e_type_shape[1]
        e_type_dim = e_type_shape[2]

        e_type_ = e_type.unsqueeze(0).view(e_type.shape[0], num_edge, e_type_dim, -1)

        # Equation (3)
        #   Normalize torche attention scores using softmax (equation (3)).
        # print('nodes.mailbox', nodes.mailbox.keys())
        alpha = F.softmax(e, dim=1)

        # Equation (4)
        #   Weigh neighbor nodes to sum later.
        weighted = alpha * z

        '''
        Represent features by type so we can calculate semantic-level attention later
        Assuming data tensor for 3 edges:
            [[1,1,1],
             [2,2,2],
             [3,3,3],
             [4,4,4]]
        with types relatively:
            [[0,0,1],
             [0,1,0],
             [1,0,0],
             [0,0,1]]
        We transform original data tensor to:
            [[ [0,0,0], [0,0,0], [1,1,1] ],
             [ [0,0,0], [2,2,2], [0,0,0] ],
             [ [3,3,3], [0,0,0], [0,0,0] ],
             [ [0,0,0], [0,0,0], [4,4,4] ]]
        So that after multiply with weight and sum, we may end up with:
            [[ [3,3,3], [2,2,2], [5,5,5] ]]
        And pass that tensor to the semantic net
        '''
        weighted_rp = weighted.repeat(1, 1, e_type_dim).view(z.shape[0], z.shape[1], e_type_dim, -1)
        weighted_by_type = e_type_ * weighted_rp

        # Equation (4)
        #   Aggregate neighbor embeddings weighted by torche attention scores (equation(4)).
        h = torch.sum(weighted_by_type, dim=1)
        this_node_z = nodes.data['z']
        # print('*** e', e)
        # print('>>> z', z)
        # print('>>> alpha', alpha)
        # print('>>> this_node_z', this_node_z)
        # print('>>> weighted', weighted)
        # print('>>> weighted_by_type', weighted_by_type)
        # print('>>> this_node_z', this_node_z.shape)
        # print('>>> weighted', weighted.shape)
        # print('>>> weighted_by_type', weighted_by_type.shape)
        # print('>>> src_id', nodes.mailbox['src_id'])

        return {'z': h}

    def forward(self, h, e, g):
        if g is not None:
            self.g = g

        self.g.ndata['z'] = h
        self.g.edata['e_weighted'] = e

        # self.g.group_apply_edges(func=self.concat_sum_edge_node_ft, group_by="src")

        # Equation (2)
        # The un-normalized attention score eij is calculated using torche embeddings of adjacent nodes i and j. This suggests torchat torche attention scores can be viewed as edge data, which can be calculated by torche apply_edges API. The argument to torche apply_edges is an Edge UDF, which is defined as below:
        # self.g.apply_edges(self.edge_attention)
        self.g.apply_edges(func=self.edge_att)

        # Equation (3) & (4)
        # `update_all` API is used to trigger message passing on all torche nodes. The message function sends out two tensors:
        #   - torche transformed z embedding of torche source node, and
        #   - torche un-normalized attention score e on each edge.
        # The reduce function torchen performs two tasks:
        #   - Normalize torche attention scores using softmax (equation (3)).
        #   - Aggregate neighbor embeddings weighted by torche attention scores (equation(4)).
        self.g.update_all(self.message_func, self.reduce_func_w_sem)

        return self.g.ndata.pop('z'), self.g.edata.pop('e_weighted')

class SemLevelGAT(nn.Module):
    """
    Weight by edge type
    """
    def __init__(self, g, z_node_lv_dim, out_dim):
        super(SemLevelGAT, self).__init__()
        self.g = g

        self.sem_attn = nn.Linear(z_node_lv_dim, 1, bias=False)
        self.fc2 = nn.Linear(z_node_lv_dim, out_dim, bias=False)

    def update_node(self, nodes):
        h = nodes.data['z_final']
        # print("nodes.data['z_final']", h.shape)
        zphi = torch.sum(h, dim=0)
        # print('zphi', zphi)
        # print('~zphi shape', zphi.shape)

        ''' Semantic-level attention '''
        w_phi = self.sem_attn(zphi)
        # print('w_phi', w_phi)
        w_phi = F.leaky_relu(w_phi)
        # print('w_phi (leaky_relu)', w_phi)
        # print('~w_phi shape', w_phi.shape)
        beta = F.softmax(w_phi, dim=0)
        Z = torch.sum(beta * h, dim=1)
        # print('w_phi', w_phi)
        # print('w_phi', w_phi.shape)
        # print('beta', beta)
        # print('Z', Z)
        # print('~beta shape', beta.shape)
        # print('~Z shape', Z.shape)

        ''' Final embedding '''
        Z = self.fc2(Z)
        # print('Z', Z)
        # print('~Z fc shape', Z.shape)
        # print('\n------------------------------------')

        return {'z_final': Z}

    def forward(self, h, g):
        if g is not None:
            self.g = g

        self.g.ndata['z_final'] = h

        self.g.apply_nodes(func=self.update_node)

        return self.g.ndata.pop('z_final')

class MultiHeadGATLayer(nn.Module):
    def __init__(self, g, node_dim, edge_dim, node_ft_out_dim, edge_ft_out_dim, out_dim, num_heads, merge='cat'):
        super(MultiHeadGATLayer, self).__init__()
        self.heads = nn.ModuleList()
        for i in range(num_heads):
            self.heads.append(GATLayer(g, node_dim, edge_dim, node_ft_out_dim, edge_ft_out_dim, out_dim))
        self.merge = merge

    def forward(self, node_features, edge_features, g):
        # print('self.heads~~~', self.heads)

        n_head_outs = []
        e_head_outs = []
        for attn_head in self.heads:
            n_head_out, e_head_out = attn_head(node_features, edge_features, g)
            n_head_outs.append(n_head_out)
            e_head_outs.append(e_head_out)

        if self.merge == 'cat':
            # concat on torche output feature dimension (dim=1)
            return torch.cat(n_head_outs, dim=1), torch.cat(e_head_outs, dim=1)
        else:
            # merge using average
            return torch.mean(torch.stack(n_head_outs)), torch.mean(torch.stack(e_head_outs))


#!/usr/bin/env python3

def get_torchbiggraph_config():
    # Twitter config: https://github.com/facebookresearch/PyTorch-BigGraph/issues/86
    # (Twitter as roughly the same size as S2ORC citation graph)
    config = {
        # I/O data
        "entity_path": "./data/biggraph/s2orc",
        "edge_paths": [
            "./data/biggraph/s2orc/train_partitioned",
        ],
        "checkpoint_path": "./data/biggraph/models/s2orc_64d",
        "checkpoint_preservation_interval": 10,

        # Graph structure
        "entities": {
            "paper_id": {"num_partitions": 1},
        },
        "relations": [{"name": "citation", "lhs": "paper_id", "rhs": "paper_id", "operator": "none"}],

        # Scoring model
        "dimension": 64,  #300,  # 500,  # maybe d=768 to make it comparable to BERT
        "max_norm": 1.0,
        "global_emb": False,
        "comparator": "dot",

        # Training
        "num_epochs": 30,
        "num_edge_chunks": 10,
        "batch_size": 10_000,
        "num_uniform_negs": 0,
        "margin": 0.15,
        "lr": 0.1,  # 0.1,

        # GPU
        #"num_gpus": 2,

        # Evaluation during training
        "eval_fraction": 0,  # to reproduce results, we need to use all training data
    }

    return config


import pygame
import sys
import movement
import time
import imageresize as ir
pygame.init()

res = [1280, 720] # screen res
gamedisplay = pygame.display.set_mode(res) # pygame display
# name/title of game
title = 'pong'
pygame.display.set_caption(title)
icon = pygame.image.load('Icons//248.png')
pygame.display.set_icon(icon)

# start logo (wip)

ir.resize(round((res[0]/10)), 'pong-icon.png')
headermenu = pygame.image.load(str(round((res[0]/10)))+'xpong-icon.png')
headermenux = round(res[0]/2-(1/3)*(res[0]/10))
headermenuy = round(res[1]/2-(res[0]/10))
headermenulocation = [headermenux, headermenuy]
# gamedisplay.blit(headermenu, headermenulocation)
# pygame.display.update()

# scores
playertextfont = pygame.font.Font('Tahoma Regular font.ttf', 24)
# player 1
p1score = 0
p1text = 'Player 1: ' + str(p1score)
player1text = playertextfont.render(p1text, True, (255, 255, 255))
player1co = [4*res[0]/5, res[1]/50]
# player 2
p2score = 0
p2text = 'Player 2: ' + str(p2score)
player2text = playertextfont.render(p2text, True, (255, 255, 255))
player2co = [res[0]/10, 15*res[1]/16]

# play button
ir.resize(round((res[0]/10)), 'play.png')

# Clock
framelimit = 100 # Max fps
clock = pygame.time.Clock()
clock.tick(framelimit) # Limit ticks/fps

# colors:
black = [0,0,0]
white = [255,255,255]

# pong platforms
platwidth = res[0]/12.5
platheight = res[1]/60

xposplat1 = (res[0]/2)- platwidth/2
yposplat1 = (res[1]/10)

xposplat2 = (res[0]/2)-platwidth/2
yposplat2 = (res[1]-yposplat1)

# keys repeat every 1ms
repeat = 1
pygame.key.set_repeat(repeat)

# player pong platforms
uprectco = pygame.Rect(xposplat1, yposplat1, platwidth, platheight)
downrectco = pygame.Rect(xposplat2, yposplat2, platwidth, platheight)
# ball
ballxpos = res[0]/2
ballypos = res[1]/2
balllength = round(res[0]/100)+1
ball = pygame.Rect(ballxpos, ballypos, balllength, balllength)

batmoveconstant = 700 # larger = slower
batmovespeed = int(round(res[0]/batmoveconstant))

AI = False

# event loop
while True:
    gamedisplay.blit(headermenu, headermenulocation)
    movement.movement(ball, downrectco, uprectco, res)
    if movement.firsthit == 'notpassed':
        time.sleep(0.002)
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if movement.firsthit == 'notpassed':
                temp = 1.5*batmovespeed
            else:
                temp = batmovespeed
            if pygame.key.get_pressed()[pygame.K_a] and downrectco.x > 0:
                downrectco.move_ip(-temp, 0)
            if pygame.key.get_pressed()[pygame.K_d] and downrectco.x < (res[0]-platwidth):
                downrectco.move_ip(temp, 0)
            if pygame.key.get_pressed()[pygame.K_LEFT] and uprectco.x > 0:
                uprectco.move_ip(-temp, 0)
            if pygame.key.get_pressed()[pygame.K_RIGHT] and uprectco.x < (res[0]-platwidth):
                uprectco.move_ip(temp, 0)
        if event.type == pygame.QUIT:
            pygame.quit() # exit application
            sys.exit() # exit code
    if AI == True:
        if movement.firsthit == 'notpassed':
            temp = 1.5*batmovespeed
        else:
            temp = batmovespeed
        if ball.x > (res[0]/100+uprectco.x+platwidth/2) and uprectco.x < (res[0]-platwidth):
            uprectco.move_ip(temp, 0)
        elif ball.x < (-res[0]/100+uprectco.x+platwidth/2) and uprectco.x > 0:
            uprectco.move_ip(-temp, 0)
    if ball.y == 0:
        movement.downhit = not(movement.downhit)
        p2score += 1
        movement.firsthit = 'notpassed'
        ball.x = res[0]/2
        ball.y = res[1]/2
        p2text = 'Player 2: ' + str(p2score)
        player2text = playertextfont.render(p2text, True, (255, 255, 255))
    if ball.y == res[1]:
        movement.downhit = not(movement.downhit)
        p1score += 1
        movement.firsthit = 'notpassed'
        ball.x = res[0]/2
        ball.y = res[1]/2
        p1text = 'Player 1: ' + str(p1score)
        player1text = playertextfont.render(p1text, True, (255, 255, 255))
    gamedisplay.fill(black)
    gamedisplay.blit(player1text, player1co)
    gamedisplay.blit(player2text, player2co)
    pygame.draw.rect(gamedisplay, white, downrectco) # player 1 platform
    pygame.draw.rect(gamedisplay, white, uprectco) # player 2 platform
    pygame.draw.rect(gamedisplay, white, ball) # ball
    pygame.display.update() # frame update


#!/usr/bin/env python
#coding=utf-8
import cv2
import numpy as np
import time

##2019/7/6 20:24
#修改颜色范围（清水水池）
##2019/7/5 17:11

def detectyb(frame):

	#
	# t1 = time.clock()
	H, W, _=frame.shape

	hue_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
	low_range1 = np.array([20, 124, 103])
	high_range1 = np.array([50, 255, 255])
	th1 = cv2.inRange(hue_image, low_range1, high_range1)

	kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
	th1 = cv2.morphologyEx(th1, cv2.MORPH_CLOSE, kernel)

	#cv2.imshow("th",th1)
	#cv2.waitKey(0)
	M = cv2.moments(th1)
	#print(M["m00"]/255)
	if M["m00"]<500*255:
		return 1000, 1000
	cX=M["m10"]/M["m00"]-W/2
	cY=M["m01"]/M["m00"]-H/2

	# t2 = time.clock()
	# total_time = t2-t1
	# print("t1:"+str(total_time))
	return int(cX), int(cY)

if __name__ == '__main__':

	frame0 = cv2.imread('C:/Users/liuzhongtian/Desktop/002.jpg')
	frame = cv2.resize(frame0,(640,480))
	x_error, y_error = detectyb(frame)
	print("X_error = ",x_error,"Y_error = ",y_error)


#!/usr/bin/env python3
import dataclasses
from enum import IntFlag

from rich.table import Table
from pwncat.facts import Fact
from pwncat.platform.windows import Windows
from pwncat.modules.enumerate import Schedule, EnumerateModule

class LuidAttributes(IntFlag):

    DISABLED = 0x00
    SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x01
    SE_PRIVILEGE_ENABLED = 0x02
    SE_PRIVILEGE_REMOVE = 0x04
    SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000

class TokenPrivilegeData(Fact):
    def __init__(
        self,
        source: str,
        privilege: str,
        attributes: LuidAttributes,
        token_handle: int,
        pid: int,
    ):
        super().__init__(source=source, types=["user.privs"])

        self.privilege: str = privilege
        self.attributes: LuidAttributes = attributes
        # self.token_handle: int = token_handle
        # self.pid: int = pid

    def title(self, session: "pwncat.manager.Session"):

        if self.attributes == LuidAttributes.DISABLED:
            color = "red"
        else:
            color = "green"

        attrs = str(self.attributes)
        attrs = attrs.replace("LuidAttributes.", "")
        attrs = attrs.replace("|", "[/cyan]|[cyan]")
        attrs = "[cyan]" + attrs + "[/cyan]"

        return f"[{color}]{self.privilege}[/{color}] -> {attrs}"

class Module(EnumerateModule):
    """Enumerate user privileges using PowerView's Get-ProcessTokenPrivilege"""

    PROVIDES = ["user.privs"]
    PLATFORM = [Windows]
    SCHEDULE = Schedule.ALWAYS

    def enumerate(self, session: "pwncat.manager.Session"):

        # Ensure that powerview is loaded
        session.run(
            "manage.powershell.import",
            path="PowerShellMafia/PowerSploit/Privesc/PowerUp.ps1",
        )

        # Grab our current token privileges
        results = session.platform.powershell("Get-ProcessTokenPrivilege")
        if len(results) == 0:
            session.log("[red]error[/red]: Get-ProcessTokenPrivilege failed")
            return

        # They end up in an array in an array
        privs = results[0]

        # Create our enumeration data types
        for priv in privs:
            yield TokenPrivilegeData(
                source=self.name,
                privilege=priv["Privilege"],
                attributes=LuidAttributes(priv["Attributes"]),
                token_handle=priv["TokenHandle"],
                pid=priv["ProcessId"],
            )


#!/usr/bin/env python
# -*- coding:Utf-8 -*-

#
# Modules
#

import BeautifulSoup
import dateutil.parser
import logging
logger = logging.getLogger( "TVDownloader" )
import os
import re
import xml.sax

import tvdcore
from Podcasts import PodcastsHandler

#
# Classe
#

class FranceInter( tvdcore.Plugin ):

	listeChaines = { "Emissions"  : "http://www.franceinter.fr/emissions/liste-des-emissions",
					 "Chroniques" : "http://www.franceinter.fr/emissions/liste-des-chroniques"
				   }

	def __init__( self ):
		tvdcore.Plugin.__init__( self, "France Inter", "http://www.franceinter.fr", 14 )

		self.listeEmissions = {} # { Nom chaine : { Nom emission : URL fichier XML } }
		self.derniereChaine = {}

		if( os.path.exists( self.fichierCache ) ):
			self.listeEmissions = self.chargerCache()

	def rafraichir( self ):
		# RAZ de la liste des emissions
		self.listeEmissions.clear()

		# Recupere le XML de description de toutes les emissions
		for ( chaine, urlChaine ) in self.listeChaines.items():
			self.listeEmissions[ chaine ] = {}
			pageHtml     = self.getPage( urlChaine )
			soupStrainer = BeautifulSoup.SoupStrainer( "a", { "class" : "visuel" } )
			pageSoup     = BeautifulSoup.BeautifulSoup( pageHtml, parseOnlyThese = soupStrainer )
			# Liste des pages des emissions
			listePagesUrl = map( lambda x : "%s%s" %( "http://www.franceinter.fr" , x[ "href" ] ), pageSoup.contents )
			# Recupere toutes les pages
			listePagesData = self.getPages( listePagesUrl )
			for emission in pageSoup.contents:
				try:
					nomEmission = emission[ "title" ]
					urlPageEmission = "%s%s" %( "http://www.franceinter.fr" , emission[ "href" ] )
					# Extrait le lien XML de la page de l'emission
					urlXml = re.findall( "http://radiofrance-podcast.net/podcast09/rss_\d+?.xml", listePagesData[ urlPageEmission ] )[ 0 ]
					# Ajoute l'emission a la liste
					self.listeEmissions[ chaine][ nomEmission ] = urlXml
				except:
					continue

		# Sauvegarde la liste dans le cache
		self.sauvegarderCache( self.listeEmissions )

	def listerChaines( self ):
		map( lambda x : self.ajouterChaine( ( x, None ) ), self.listeEmissions.keys() )

	def listerEmissions( self, chaine ):
		if( self.listeEmissions.has_key( chaine ) ):
			self.derniereChaine = self.listeEmissions[ chaine ]
			map( lambda x : self.ajouterEmission( chaine, x ), self.derniereChaine.keys() )

	def listerFichiers( self, emission ):
		if( self.derniereChaine.has_key( emission ) ):
			urlXmlEmission = self.derniereChaine[ emission ]
			xmlEmission    = self.getPage( urlXmlEmission )
			# Extrait les fichiers
			listeFichiers = []
			handler = PodcastsHandler( listeFichiers )
			try:
				xml.sax.parseString( xmlEmission, handler )
			except:
				logger.error( "Impossible de parser le fichier XML %s" %( urlXmlEmission ) )
				return
			# Ajoute les fichiers
			map( lambda x : self.ajouterFichier( emission, x ), listeFichiers )


from django.db import models
from attractions.models import Attraction
from comments.models import Comment
from addresses.models import Address
from reviews.models import Review

class TouristSpot(models.Model):

    name = models.CharField(max_length=150)
    description = models.TextField(null=True, blank=True)
    approved = models.BooleanField(default=False)
    attractions = models.ManyToManyField(Attraction)
    comments = models.ManyToManyField(Comment)
    reviews = models.ManyToManyField(Review)
    address = models.ForeignKey(Address, on_delete=models.CASCADE, null=True, blank=True)
    image = models.ImageField(upload_to='tourist_spot', null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    @property
    def test_description_two(self):
        return self.name + ' - ' + self.description

    def __str__(self):
        return self.name


import pandas as pd
import numpy as np
import itertools

time_bins = ['270_330', '330_390', '390_450', '450_510', '510_580', '580_650', 'gt_650']

def detect_outlier(data_1):
    outliers=[]
    threshold=1
    mean_1 = np.mean(data_1)
    std_1 =np.std(data_1)

    for index, y in enumerate(data_1):
        z_score= (y - mean_1)/std_1
        if np.abs(z_score) > threshold:
            outliers.append(index)
    return outliers

norm = 'epigenetic_regulators'

df = pd.read_csv('pca/' + norm + '/' + norm + '_' + 'first_components.csv', index_col=0)

indices = set()
for index, row in df.iterrows():
    outlier_datapoints = detect_outlier(row.tolist())
    indices.update(outlier_datapoints)

indices = list(indices)
print(indices)

df = df.iloc[:,indices]

muscles = ['head_row_1', 'head_row_2', 'anterior', 'posterior', 'far_posterior']
muscle_dict = {}

for muscle in muscles:
    muscle_dict[muscle] = pd.read_csv('data/geneAndTimeData/bwm_csv/bwm_' + muscle + '_time_bins.csv', index_col=0)

result = pd.DataFrame(index = df.columns, columns = muscles)

bins = []

for index, col in df.iteritems():
    col = col.abs()
    bin = col.idxmax()
    bins.append(bin)

    for muscle in muscles:
        result.at[index, muscle] = muscle_dict[muscle].at[bin, index]

print(result)
result.to_csv('pca/' + norm + '/' + norm + '_' + 'hvgs.csv')


from collections import defaultdict

def getQueryType(queryid):
    if queryid <=118:
        return 0
    if queryid >=118 and queryid <135:
        return 1
    if queryid >=135:
        return 2

run2hscore = defaultdict(lambda: 0.0)
run2fscore = defaultdict(lambda: 0.0)
run2sscore = defaultdict(lambda: 0.0)
run2hmeasure = defaultdict(lambda: 0.0)

pertopicout = open('jp.final.pertopoc.csv','w')

for l in open('jp.pertopic.csv').readlines()[1:]:
    segs = l.strip().split(',')
    runname = segs[0]
    queryid = int(segs[1])
    fscore = float(segs[3])
    sscore = float(segs[6])
    hscore = float(segs[8])

    run2hscore[runname]+=hscore
    if getQueryType(queryid)==0:
        run2fscore[runname]+=fscore
    run2sscore[runname]+=sscore

    if getQueryType(queryid) == 0:
        hmeasure = hscore*(fscore+sscore)/2.0
    else:
        hmeasure = hscore*sscore
    run2hmeasure[runname]+= hmeasure
    pertopicout.write(l.strip()+','+str(hmeasure)+'\n')

pertopicout.close()
allout = open('jp.final.perrun.csv','w')
allout.write('runname,h-score,f-score,s-score,h-measure\n')
for r in sorted(list(run2hscore.keys())):
    allout.write(r+','+str(run2hscore[r]/34.0)+','+str(run2fscore[r]/17.0))
    allout.write(','+str(run2sscore[r]/34.0)+','+str(run2hmeasure[r]/34.0)+'\n')
allout.close()


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
import gym
import random
from gym_pcgrl import wrappers

import geneticAlgorithmScript
import CNN
import pandas as pd

class Chromosome:
    def __init__(self):
        self.kwargs = {
            'change_percentage': 0.4,
            'verbose': True
        }
        # 28 possibly
        self.kwargs['cropped_size'] = 28
        self.crop_size = self.kwargs.get('cropped_size', 28)
        self.agents = 1
        self.processes = 1

        self._env = wrappers.CroppedImagePCGRLWrapper("binary-narrow-v0", self.crop_size, **self.kwargs)

        n_actions = self._env.action_space.n

        #self._obs = np.zeros((1, 28, 28, 1), dtype = np.uint8)
        #self._net = CNN.Net(self._obs.shape[-1], self.crop_size, n_actions)
        self._net = CNN.Net(1, self.crop_size)
        self._fitness = 0

        self._genes = np.zeros(list(torch.flatten(self._net.conv1.weight).size())[0] + list(torch.flatten(self._net.conv1.bias).size())[0]
            + list(torch.flatten(self._net.conv2.weight).size())[0] + list(torch.flatten(self._net.conv2.bias).size())[0]
            + list(torch.flatten(self._net.conv3.weight).size())[0] + list(torch.flatten(self._net.conv3.bias).size())[0]
            + list(torch.flatten(self._net.fc1.weight).size())[0] + list(torch.flatten(self._net.fc1.bias).size())[0]
            + list(torch.flatten(self._net.pi_logits.weight).size())[0] + list(torch.flatten(self._net.pi_logits.bias).size())[0])

    def copy(self):
        c = Chromosome()
        c._genes = self._genes.copy()
        return c
    def mutation(self):
        idxlist = random.sample(range(self._genes.shape[0]), self._genes.shape[0] // 10)
        for idx in idxlist:
            random_value = np.random.normal(0, 0.1, 1) #mean of 0 and std deviation of 0.1
            self._genes[idx] += random_value
        return

    def fitness(self, numberEpisodes):
        #Conv1 and Bias1, and so on and so forth
        self._net.conv1.weight.data = torch.from_numpy(np.reshape(self._genes[0:32], (32, 1, 1, 1)))
        self._net.conv1.bias.data = torch.from_numpy(self._genes[32:64])

        self._net.conv2.weight.data = torch.from_numpy(np.reshape(self._genes[64:2112], (64, 32, 1, 1)))
        self._net.conv2.bias.data = torch.from_numpy(self._genes[2112: 2176])

        self._net.conv3.weight.data = torch.from_numpy(np.reshape(self._genes[2176: 6272], (64, 64, 1, 1)))
        self._net.conv3.bias.data = torch.from_numpy(self._genes[6272:6336])

        self._net.fc1.weight.data = torch.from_numpy(np.reshape(self._genes[6336:25696448], (512, 50176)))
        self._net.fc1.bias.data = torch.from_numpy(self._genes[25696448:25696960])

        self._net.pi_logits.weight.data = torch.from_numpy(np.reshape(self._genes[25696960: 25698496], (3, 512)))
        self._net.pi_logits.bias.data = torch.from_numpy(self._genes[25698496: 25698499])

        """
        self._net.conv1.weight.data = self._genes[0:1, :]
        self._net.conv2.weight.data = self._genes[1:33, :]
        self._net.conv3.weight.data = self._genes[33:97, :]
        self._net.fc1.weight.data = self._genes[97:50273, :]
        self._net.pi_logits.weight.data = self._genes[50273:50785, :]
        """
        #calculate fitness
        totalReward = 0
        for i in range(numberEpisodes):
            obs = self._env.reset()
            #print(obs.shape)
            done = False
            while not done:
                action = self._net.forward(CNN.obs_to_torch(obs))
                obs, reward, done, _ = self._env.step(action)
                totalReward += reward
        self._fitness = totalReward / numberEpisodes
        #print(self._fitness)
        return self._fitness

def sortfitness(popidx):
    return popidx._fitness

def averageLstParse(lst):
    nlst = lst[:1]
    count = 0
    for i in range(len(nlst)):
        count += nlst[i]._fitness

    return count / len(nlst)

def save_models(model, path, epoch = 0, update = 0):
    print('Saving 1 model to {}'.format(path))
    save_dict = {'model_state_dict': model.state_dict()}
    if epoch:
        save_dict['epoch'] = epoch
    if update:
        save_dict['update'] = update
    print(path + 'model')
    torch.save(save_dict, path + 'model')

def load_model(path, in_channels, map_size, out_length):
    model = CNN.Net(in_channels, map_size).double()
    checkpoint = torch.load(path + 'model')
    epoch = checkpoint.get('epoch',0)
    update = checkpoint.get('update',0)
    model.load_state_dict(checkpoint['model_state_dict'])

    print('Loaded 1 models from {}.'.format(path))

    return model, epoch, update

class GA:
    def __init__(self, popSize, mu, lamda):
        self._pop = []
        for i in range(popSize):
            self._pop.append(Chromosome())
        print("done init")
        self.mu = mu
        self.lamda = lamda
        self.avgFitness = []
        #self.save_path = 'models/{}/{}'.format(game,representation)
        #self.save_path = 'runs/{}/{}/'.format("binary","narrow")
        self.save_path = 'runs/'

    def __delete__(self, instance):
        print("deleted in descriptor object")
        del self._pop
        del self.avgFitness
        del self.mu
        del self.lamda
        del self.save_path
        return

    def advance(self):
        for c in self._pop:
            c.fitness(10)
            #c.fitness(2)
        print("done fitness")

        #sort(self._pop, lamda c: c._fitness, reverse = True)
        sorted(self._pop, key = sortfitness, reverse = True)
        self.avgFitness.append(averageLstParse(self._pop))
        #self.avgFitness.append(self._pop[0]._fitness)
        save_models(self._pop[0]._net, self.save_path, epoch = 0, update = 0)
        for i in range(self.mu):
            c = self._pop[i].copy()
            c.mutation()
            self._pop[self.lamda + i] = c
        print("done mutation")

    def run(self, generations):
        for i in range(generations):
            self.advance()
            print("done generation")
        return self.avgFitness

if __name__ == "__main__":

    # 1 generation = 45 minutes
    allAvgFitness = []
    #ga = GA(100, 50, 50) #mu = 50, lamda = 50
    ga1= GA(4, 2, 2)
    allAvgFitness.append(ga1.run(300))
    del ga1

    print("DONE!!!")

    df = pd.DataFrame(allAvgFitness)
    df.to_csv('output.csv', index=False)


"""Finds anagrams of a word in a list of words
"""

def detect_anagrams(anagram, word_list):
    """Finds any words in [word_list] that are anagrams of [anagram]"""
    sorted_letters = ''.join(sorted(anagram.lower()))
    anagram_list = []
    for word in word_list:
        # has all the same letters and isn't exactly the target word
        if ''.join(sorted(word.lower())) == sorted_letters and \
           word.lower() != anagram:
            anagram_list.append(word)
    return anagram_list


## Leap Years to Come

# Develop and test a Python program that displays future leap years,
#  starting with the first occurring leap year from the current year,
#  until a final year entered by the user. (HINT: Module datetime can be used)

# Author: Enes Kemal Ergin

import datetime

valid = True
current_year = datetime.datetime.now().year

while valid:
    user_year = int(input("Enter a future year: "))
    if user_year <= current_year:
        print("I am sorry, you should put a future year")
        valid = False
    else:
        while user_year > current_year:
            if (user_year % 4 == 0) and (not (user_year % 100 == 0) or (user_year % 400 == 0)):
                print(user_year, "is a leap year")
            user_year -= 1
        valid = False


from django.urls import path
from project2app import views

app_name = 'records'

urlpatterns = [

    path('new_record/', views.new_record, name='new_record'),
    path('view_records/', views.view_records, name='view_records'),

]


"""Routes for API collections."""
from flask import request, url_for

from . import api
from ..response import QuerySetApiResult
from ..models import Country, EnglishString, Survey, Indicator, Data

@api.route('/countries')
def get_countries():
    """Country resource collection GET method.

    Returns:
        json: Collection for resource.
    """
    countries = Country.query.all()
    data = [c.full_json() for c in countries]
    return QuerySetApiResult(data, 'json')

@api.route('/countries/<code>')
def get_country(code):
    """Country resource entity GET method.

    Args:
        code (str): Identification for resource entity.

    Returns:
        json: Entity of resource.
    """
    lang = request.args.get('_lang')
    country = Country.query.filter_by(country_code=code).first()
    json_obj = country.to_json(lang=lang)
    return QuerySetApiResult(json_obj, 'json')

@api.route('/surveys')
def get_surveys():
    """Survey resource collection GET method.

    Returns:
        json: Collection for resource.
    """
    # Query by year, country, round
    # print(request.args)
    surveys = Survey.query.all()
    data = [s.full_json() for s in surveys]
    return QuerySetApiResult(data, 'json')

@api.route('/surveys/<code>')
def get_survey(code):
    """Survey resource entity GET method.

    Args:
        code (str): Identification for resource entity.

    Returns:
        json: Entity of resource.
    """
    survey = Survey.query.filter_by(code=code).first()
    json_obj = survey.full_json()
    return QuerySetApiResult(json_obj, 'json')

@api.route('/indicators')
def get_indicators():
    """Get Indicator resource collection.

    Returns:
        json: Collection for resource.
    """
    indicators = Indicator.query.all()
    data = [i.full_json(endpoint='api.get_indicator') for i in indicators]
    return QuerySetApiResult(data, 'json')

@api.route('/indicators/<code>')
def get_indicator(code):
    """Get Indicator resource entity.

    Args:
        code (str): Identification for resource entity.

    Returns:
        json: Entity of resource.
    """
    indicator = Indicator.query.filter_by(code=code).first()
    json_obj = indicator.full_json()
    return QuerySetApiResult(json_obj, 'json')

@api.route('/data')
def get_data():
    """Get Data resource collection.

    Returns:
        json: Collection for resource.
    """
    all_data = data_refined_query(request.args)
    data = [d.full_json() for d in all_data]
    return QuerySetApiResult(data, 'json')

def data_refined_query(args):
    """Refine data query.

    *Args:
        survey (str): If present, filter by survey entities.

    Returns:
        dict: Filtered query data.
    """
    qset = Data.query
    if 'survey' in args:
        qset = qset.filter(Data.survey.has(code=args['survey']))
    results = qset.all()
    return results

@api.route('/data/<code>')
def get_datum(code):
    """Get data resource entity.

    Args:
        code (str): Identification for resource entity.

    Returns:
        json: Entity of resource.
    """
    data = Data.query.filter_by(code=code).first()
    json_obj = data.full_json()
    return QuerySetApiResult(json_obj, 'json')

@api.route('/texts')
def get_texts():
    """Get Text resource collection.

    Returns:
        json: Collection for resource.
    """
    english_strings = EnglishString.query.all()
    data = [d.to_json() for d in english_strings]
    return QuerySetApiResult(data, 'json')

@api.route('/texts/<code>')
def get_text(code):
    """Get Text resource entity.

    Args:
        code (str): Identification for resource entity.

    Returns:
        json: Entity of resource.
    """
    text = EnglishString.query.filter_by(code=code).first()
    json_obj = text.to_json()
    return QuerySetApiResult(json_obj, 'json')

# TODO: (jef/jkp 2017-08-29) Finish route. Needs: Nothing?
@api.route('/characteristicGroups')
def get_characteristic_groups():
    """Get Characteristic Groups resource collection.

    Returns:
        json: Collection for resource.
    """
    from flask import jsonify
    return jsonify({'info': 'To be implemented.'})

@api.route('/characteristicGroups/<code>')
def get_characteristic_group(code):
    """Get Characteristic Groups resource entity.

    Args:
        code (str): Identification for resource entity.

    Returns:
        json: Entity of resource.
    """
    return code

@api.route('/resources')
def get_resources():
    """Return API resource routes.

    Returns:
        json: List of resources.
    """
    resource_endpoints = (
        ('countries', 'api.get_countries'),
        ('surveys', 'api.get_surveys'),
        ('texts', 'api.get_texts'),
        ('indicators', 'api.get_indicators'),
        ('data', 'api.get_data'),
        ('characteristicGroups', 'api.get_characteristic_groups')
    )
    json_obj = {
        'resources': [
            {
                'name': name,
                'resource': url_for(route, _external=True)
            }
            for name, route in resource_endpoints
        ]
    }
    return QuerySetApiResult(json_obj, 'json')


#!/usr/bin/python
import matplotlib.pyplot as plt

from descartes import PolygonPatch

def plot_measurement(ax, hits=[], free_hits=[], frees=[]):
    for h in hits:
        ax.plot(h[0], h[1], 'xr')
    for fh in free_hits:
        ax.plot(fh[0], fh[1], 'xg')
    for f in frees:
        ax.plot(f[0], f[1], 'xb')

def plot_polygon(ax, polygon):
    patch = PolygonPatch(polygon, fc='gray', ec='blue', alpha=0.5, zorder=2)
    ax.add_patch(patch)
    return ax

def plot_map(ax, polygons, colors=None, normalize=False):
    if colors is not None:
        colors = colors.ravel()
        if normalize:
            colors -= colors.min()
            colors /= colors.max()

        cmap = plt.cm.get_cmap('jet')
        for idx, p in enumerate(polygons):
            patch = PolygonPatch(p, fc=cmap(colors[idx]))
            ax.add_patch(patch)
    else:
        for p in polygons:
            patch = PolygonPatch(p, fc='gray')
            ax.add_patch(patch)

def set_bounds(ax, bounds):
    ax.set_xlim((bounds[0], bounds[2]))
    ax.set_ylim((bounds[1], bounds[3]))


import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data

import time
import random
import glob
import numpy as np
import os, sys
# sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# from model.build_ssd_model import SSD
from model.vgg_ssd300 import build_ssd
from data.dataset import Dataset

import os, sys
sys.path.insert(0, '/home/wenyu/workspace/pytorch_workspace/')
from yolov3.utils import ops_show_bbox

import argparse

device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
parser = argparse.ArgumentParser()
parser.add_argument('--device_ids', type=list, default=[0, 1])
parser.add_argument('--num_classes', type=int, default=21)
parser.add_argument('--resume_path', type=str, default='') # ./output/state-ckpt-epoch-00000
parser.add_argument('--loss_step', type=int, default=10)
parser.add_argument('--save_step', type=int, default=5)
parser.add_argument('--img_dims', type=list, default=[300, ]) # 608, 320
parser.add_argument('--batch_sizes', type=list, default=[16, ]) # 16, 48
parser.add_argument('--num_workers', type=list, default=3)
parser.add_argument('--epoches', type=int, default=101)
parser.add_argument('--lr', type=float, default=0.001)
parser.add_argument('--momentum', type=float, default=0.9)
parser.add_argument('--milestones', type=list, default=[50, 70, 90])
parser.add_argument('--gamma', type=float, default=0.1)
args = parser.parse_args()

class Sovler(object):
    pass

def train(model, dataloader, optimizer, scheduler, e=0):
    '''train'''
    model.train()
    scheduler.step()

    step_losses = []
    step_time = []

    for i, (images, targets) in enumerate(dataloader):

        images = images.to(torch.device(device))
        targets = targets.to(torch.device(device))

        if False:
            ops_show_bbox.show_tensor_bbox(images[0], targets[0], xyxy=True, normalized=True)

        tic = time.time()

        # if 'cuda' in device and len(args.device_ids) > 1:
        #     losses = nn.parallel.data_parallel(module=model, inputs=(images, targets), device_ids=args.device_ids, dim=0)
        #     losses = losses.sum()
        # else:
        #     losses = model(images, targets)

        losses = model(images, targets)

        optimizer.zero_grad()
        losses.backward()
        optimizer.step()

        step_losses += [losses.item()]
        step_time += [time.time() - tic]

        # ops_logger.writer.add_scalar('losses/loss', losses.item(), global_step=e * len(dataloader) + i)

        if (i + 1) % args.loss_step == 0:

            step_losses = np.array(step_losses).mean(axis=0)
            # lin = ', '.join(['{:0.6f}'.format(ls) for ls in step_losses])
            lin = 'epoch/iter: {:0>3}/{:0>3}, lr: {}, time: {:.4f}s, losses: {:.4f}'.format(e, i + 1,
                                                                                format(optimizer.param_groups[0]['lr'],'.0e'),
                                                                                np.array(step_time).mean(),
                                                                                step_losses)
            step_losses, step_time = [], []
            print(lin)

    if e % args.save_step == 0:
        state = {
            'epoch': e,
            'model': model.state_dict(),
            'optimizer': optimizer.state_dict(),
            'scheduler': scheduler.state_dict()
        }

        if not os.path.exists('./output'):
            os.mkdir('./output')

        torch.save(state, './output/state-ckpt-epoch-{:0>5}'.format(e))

    print(''.join(['-'] * len(lin)))

def validate(model, dataloader, epoch=0):
    model.eval()
    pass

if __name__ == '__main__':

    # dataset = Dataset('', size=args.img_dim)
    # dataloader = data.DataLoader(dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers)
    datasets = [Dataset(size=sz) for sz in args.img_dims]
    dataloaders = [data.DataLoader(d, batch_size=bs, shuffle=True, num_workers=args.num_workers) for d, bs in zip(datasets, args.batch_sizes)]

    model = build_ssd(num_classes=args.num_classes)
    # model = SSD()
    model = model.to(torch.device(device))

    optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
    scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=args.milestones, gamma=args.gamma)

    # if args.resume_path:
    #     ops_weight_init.resume(model, optimizer, scheduler, args.resume_path)

    # for e in range(scheduler.last_epoch + 1, int(args.epoches / len(dataloaders) + 1)):
    #     if e > 10:
    #         random.shuffle(dataloaders)
    #     for i, dtloader in enumerate(dataloaders):
    #         train(model, dtloader, optimizer, scheduler, e * len(dataloaders) + i)

    e = scheduler.last_epoch + 1

    while e < args.epoches:
        if e < 10:
            train(model, dataloaders[0], optimizer, scheduler, e)
            e += 1
        else:
            random.shuffle(dataloaders)
            for i, dtloader in enumerate(dataloaders):
                train(model, dtloader, optimizer, scheduler, e + i)
            e += len(dataloaders)


from code_jam import autosolve
from itertools import count
from fractions import Fraction

def powers_of_2(start):
    for i in count(start):
        yield 2**i

def is_2_power(value):
    for p in powers_of_2(0):
        if p == value:
            return True
        if p > value:
            return False

@autosolve
def generations(tokens):
    value = tokens.next_token(Fraction)

    if value.numerator > value.denominator:
        return 'impossible'
    if not is_2_power(value.denominator):
        return 'impossible'

    for i in range(1, 100):
        if value >= Fraction(1, 2**i):
            return i


# -*- coding: utf-8 -*-
from __future__ import absolute_import

from rest_framework import serializers
from article.models.article import Article

class ArticleSerializer(serializers.ModelSerializer):

    class Meta:
        model = Article
        fields = (
            'id',
            'title',
            'url',
            'content',
            'image_name',
            'category_id',
        )

    def create(self, validated_data):

        user = self.context['request'].user

        data = validated_data.copy()
        data.update({
            'user': user
        })
        article = Article.objects.create(**data)

        return article


from django.contrib import admin
from django.urls import path, include
from home import views

urlpatterns = [
    path('', views.home, name='home'),
    path('contact', views.contact, name='contact'),
    path('about', views.about, name='about'),
    path('search', views.search, name='search'),
    path('write', views.write, name='write'),
    path('feedback', views.feedback, name='feedback'),
    path('signup', views.signup, name='signup'),
    path('login', views.handellogin, name='handellogin'),
    path('login', views.login, name='login'),
    path('logout', views.handellogout, name='handellogout'),
    path('profile', views.profile, name='profile'),
]


#!/usr/bin/env python3

import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst

# http://docs.gstreamer.com/display/GstSDK/Basic+tutorial+3%3A+Dynamic+pipelines

class Player(object):

    def __init__(self):
        # initialize GStreamer
        Gst.init(None)

        # create the elements
        self.source = Gst.ElementFactory.make("uridecodebin", "source")
        self.audio_convert = Gst.ElementFactory.make(
            "audioconvert", "audioconvert")
        self.audio_sink = Gst.ElementFactory.make("autoaudiosink", "audiosink")
        self.video_convert = Gst.ElementFactory.make(
            "videoconvert", "videoconvert")
        self.video_sink = Gst.ElementFactory.make("autovideosink", "videosink")

        # create empty pipeline
        self.pipeline = Gst.Pipeline.new("test-pipeline")

        if (not self.pipeline or not self.source or not self.audio_convert
                or not self.audio_sink or not self.video_convert or not self.video_sink):
            print("ERROR: Could not create all elements")
            sys.exit(1)

        # build the pipeline. we are NOT linking the source at this point.
        # will do it later
        self.pipeline.add(self.source, self.audio_convert, self.audio_sink,
                          self.video_convert, self.video_sink)
        if not self.audio_convert.link(self.audio_sink):
            print("ERROR: Could not link 'audioconvert' to 'audiosink'")
            sys.exit(1)

        if not self.video_convert.link(self.video_sink):
            print("ERROR: Could not link 'videoconvert' to 'videosink'")
            sys.exit(1)

        # set the URI to play
        self.source.set_property(
            "uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm")

        # connect to the pad-added signal
        self.source.connect("pad-added", self.on_pad_added)

        # start playing
        ret = self.pipeline.set_state(Gst.State.PLAYING)
        if ret == Gst.StateChangeReturn.FAILURE:
            print("ERROR: Unable to set the pipeline to the playing state")
            sys.exit(1)

        # listen to the bus
        bus = self.pipeline.get_bus()
        terminate = False
        while True:
            msg = bus.timed_pop_filtered(
                Gst.CLOCK_TIME_NONE,
                Gst.MessageType.STATE_CHANGED | Gst.MessageType.EOS | Gst.MessageType.ERROR)

            if not msg:
                continue

            t = msg.type
            if t == Gst.MessageType.ERROR:
                err, dbg = msg.parse_error()
                print("ERROR:", msg.src.get_name(), " ", err.message)
                if dbg:
                    print("debugging info:", dbg)
                terminate = True
            elif t == Gst.MessageType.EOS:
                print("End-Of-Stream reached")
                terminate = True
            elif t == Gst.MessageType.STATE_CHANGED:
                # we are only interested in STATE_CHANGED messages from
                # the pipeline
                if msg.src == self.pipeline:
                    old_state, new_state, pending_state = msg.parse_state_changed()
                    print("Pipeline state changed from {0:s} to {1:s}".format(
                        Gst.Element.state_get_name(old_state),
                        Gst.Element.state_get_name(new_state)))
            else:
                # should not get here
                print("ERROR: Unexpected message received")
                break

            if terminate:
                break

        self.pipeline.set_state(Gst.State.NULL)

    # handler for the pad-added signal
    def on_pad_added(self, src, new_pad):
        print(
            "Received new pad '{0:s}' from '{1:s}'".format(
                new_pad.get_name(),
                src.get_name()))

        # check the new pad's type
        new_pad_caps = new_pad.get_current_caps()
        new_pad_struct = new_pad_caps.get_structure(0)
        new_pad_type = new_pad_struct.get_name()

        if new_pad_type.startswith("audio/x-raw"):
            sink_pad = self.audio_convert.get_static_pad("sink")
        elif new_pad_type.startswith("video/x-raw"):
            sink_pad = self.video_convert.get_static_pad("sink")
        else:
            print(
                "It has type '{0:s}' which is not raw audio/video. Ignoring.".format(new_pad_type))
            return

        # if our converter is already linked, we have nothing to do here
        if(sink_pad.is_linked()):
            print("We are already linked. Ignoring.")
            return

        # attempt the link
        ret = new_pad.link(sink_pad)
        if not ret == Gst.PadLinkReturn.OK:
            print("Type is '{0:s}}' but link failed".format(new_pad_type))
        else:
            print("Link succeeded (type '{0:s}')".format(new_pad_type))

        return

if __name__ == '__main__':
    p = Player()


#!/usr/bin/env python3

import argparse
import shlex
import sys

from zfs_functions import *

if __name__ == '__main__':
  parser=argparse.ArgumentParser(description="Sync snapshots from one FS to another and remove snapshots on destination not present on source.")
  parser.add_argument("src", help="The source zfs filesystem to sync to destination.")
  parser.add_argument("dst", help="The destination zfs filesystem to sync from source.  Use a trailing / to indicate that source name should be appended to destination.  \"sync.py a/src b/dst/\" will sync a/src to b/dst/src where as \"sync.py a/src b/dst\" will sync a/src to b/dst.")
  parser.add_argument("-s", metavar="src_cmd", help="The source fs command (ssh remotehost).")
  parser.add_argument("-d", metavar="dst_cmd", help="The destination fs command (ssh remotehost).")
  parser.add_argument("--dry-run", help="Just display what would be done. Notice that since no snapshots will be transfered, less will be marked for theoretical destruction.", action="store_true")
  parser.add_argument("--verbose", help="Display what is being done.", action="store_true")
  parser.add_argument("--print-output", help="Print the output of zfs receive.", action="store_true")

  args=parser.parse_args()
  # print(args)

  src_pool = args.src.split("/")[0]
  src_prefix = args.src

  dst_pool = args.dst.split("/")[0]
  dst_prefix = args.dst

  src_cmd = shlex.split(args.s) if args.s != None else []
  dst_cmd = shlex.split(args.d) if args.d != None else []

  try:
    src=ZFS_pool(pool=src_pool, remote_cmd=src_cmd, verbose=args.verbose)
    dest=ZFS_pool(pool=dst_pool, remote_cmd=dst_cmd, verbose=args.verbose)
  except subprocess.CalledProcessError:
    sys.exit()

  if dst_prefix[len(dst_prefix) - 1] == '/':
    src_prefix_len=src_prefix.rfind('/')+1
  else:
    src_prefix_len=len(src_prefix)

  for fs in src.get_zfs_filesystems(fs_filter=src_prefix):
    src_fs=ZFS_fs(fs=fs, pool=src, verbose=args.verbose, dry_run=args.dry_run)
    dst_fs=ZFS_fs(fs=dst_prefix+fs[src_prefix_len:], pool=dest, verbose=args.verbose, dry_run=args.dry_run)
    if not src_fs.sync_without_snap(dst_fs=dst_fs,print_output=args.print_output):
      print ("sync failure for "+fs)
      sys.exit()
    src_fs.remove_deleted_snapshots(dst_fs=dst_fs)


import pygame
from pygame.sprite import Sprite

from settings import ENEMY_PNG

class Enemy(Sprite):
    """ Create a single enemy (after that - in the fleet) """
    def __init__(self, ai_settings, screen):
        super(Enemy, self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings

        # Load the enemy image, and set its rect attribute.
        self.image = pygame.image.load(ENEMY_PNG)
        self.rect = self.image.get_rect()

        # Start each new alien near the top left of the screen
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height
        self.screen_rect = screen.get_rect()

        # Store the alien's position
        self.x = float(self.rect.x)

    def blit_me(self):
        """ Draw the enemy in current location """
        self.screen.blit(self.image, self.rect)

    def update(self):
        """Move the enemy right"""
        if self.x > self.screen_rect.right - 200:
            self.ai_settings.fleet_direction = - self.ai_settings.fleet_direction
        self.x += (self.ai_settings.enemy_factor_speed * self.ai_settings.fleet_direction)
        self.rect.x = self.x

        # print(self.x)

    def check_edge(self):
        """ Return True if enemy is at the edge of the screen """
        screen_rect = self.screen.get_rect()

        if self.rect.right <= screen_rect.right or self.rect.left >= 0:
            return True
        else:
            return True


import csv

with open('headers.csv', "r") as csvfile:
    readCSV = csv.reader(csvfile, delimiter=';')
    Artikelnummer = []
    Artikelcode = []
    Naam = []
    Voorraad = []
    Prijs = []
    TotaleVoorraad = [231, 587, 34, 66, 155]

    for rij in readCSV:
        artikelnummer = rij[0]
        artikelcode = rij[1]
        naam = rij[2]
        voorraad = rij[3]
        prijs = rij[4]

        Artikelnummer.append(artikelnummer)
        Artikelcode.append(artikelcode)
        Naam.append(naam)
        Voorraad.append(voorraad)
        Prijs.append(prijs)

    print("Het duurste artikel is: " + str(Naam[4]) + (" en die kost: " + str(Prijs[4])) + " Euro")
    print("Er zijn slechts " + str(Voorraad[3]) + " exemplaren in voorraad van het product met het nummer " + str(Artikelnummer[3]))
    print("In totaal hebben wij " + str(sum(TotaleVoorraad)) + " producten in ons magazijn liggen")


import re
from base64 import b64encode, b64decode
from collections import defaultdict
from datetime import datetime
from enum import Enum

TAG_ENUM = 'e'
TAG_DICT = 'd'
TAG_LIST = 'l'
TAG_TUPLE = 't'
TAG_DATETIME = 'dt'
TAG_BYTES = 'b'
TAG_STR = 's'
TAG_NUM = 'n'

class PollenTag(object):
    def __init__(self, tags, enums):
        self.proxy = tags

    def check(self, value):
        """检查类型"""
        pass

    def to_json(self, value):
        """转json"""
        pass

    def to_pollen(self, value):
        """转python"""
        pass

class TagEnum(PollenTag):
    tag = TAG_ENUM

    def __init__(self, tags, enums):
        self.proxy = tags
        self.enum_store = defaultdict(dict)
        for e in enums:
            for _, v in e.__members__.items():
                self.enum_store[v] = v.value
                self.enum_store[v.value] = v

    def check(self, value):
        return isinstance(value, Enum)

    def find_enum(self, value):
        return self.enum_store.get(value, None)

    def to_json(self, value):
        if value is None: return
        e = self.find_enum(value)

        if e:
            return e
        return value

class TagDict(PollenTag):
    tag = TAG_DICT

    def check(self, value):
        return isinstance(value, dict)

    def to_json(self, value: dict):
        if value is None: return
        for k in list(value.keys()):
            for tag in self.proxy.default_tags.values():
                if tag.check(k):
                    value[tag.to_json(k)] = value.pop(k)
                if tag.check(value[k]):
                    value[k] = tag.to_json(value[k])
        return value

    def to_pollen(self, value):
        if value is None: return
        for k in list(value.keys()):
            for tag in self.proxy.default_tags.values():
                if tag.check(value[k]):
                    value[k] = tag.to_pollen(value[k])
                if tag.check(k):
                    value[tag.to_pollen(k)] = value.pop(k)
        return value

class TagList(PollenTag):
    tag = TAG_LIST

    def check(self, value):
        return isinstance(value, list)

    def to_json(self, value):
        if value is None: return
        size = len(value)
        i = 0
        while i < size:
            li = value[i]
            for tag in self.proxy.default_tags.values():
                if tag.check(li):
                    value[i] = tag.to_json(li)
                    i += 1
                    break
            i += 1
        return value

    def to_pollen(self, value):
        if value is None: return
        size = len(value)
        i = 0
        while i < size:
            li = value[i]
            for tag in self.proxy.default_tags.values():
                if tag.check(li):
                    value[i] = tag.to_pollen(li)
                    i += 1
                    break
            i += 1
        return value

class TagTuple(PollenTag):
    tag = TAG_TUPLE

    def check(self, value):
        return isinstance(value, tuple)

    def to_json(self, value):
        if value is None: return
        temp = list(value)
        size = len(value)
        i = 0
        while i < size:
            li = value[i]
            for tag in self.proxy.default_tags.values():
                if tag.check(li):
                    temp[i] = tag.to_json(li)
                    i += 1
                    break
            i += 1
        return tuple(temp)

    def to_pollen(self, value):
        """
        字符list --> list or tuple
        """
        pass

class TagDatetime(PollenTag):
    tag = TAG_DATETIME
    patternForTimef = r'\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}.\d+'
    patternForTime = r'\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}'

    def check(self, value):
        return isinstance(value, datetime)

    def to_json(self, value):
        if value is None: return
        time_str = datetime.strftime(value, '%Y-%m-%d %H:%M:%S')
        return time_str

    """
    def to_pollen(self, value):
        TagStr.to_pollen
    """

class TagBytes(PollenTag):
    tag = TAG_BYTES

    def check(self, value):
        return isinstance(value, bytes)

    def to_json(self, value):
        return value.decode()

    """
    def to_pollen(self, value):
        TagStr.to_pollen
    """

class TagNum(PollenTag):
    tag = TAG_NUM

    def check(self, value):
        return isinstance(value, int) or isinstance(value, float)

    def to_json(self, value):
        return value

    def to_pollen(self, value):
        return value

class TagStr(PollenTag):
    tag = TAG_STR

    def check(self, value):
        return isinstance(value, str)

    def to_json(self, value):
        return value

    def to_pollen(self, value):
        tag_enum = self.proxy.default_tags.get(TAG_ENUM)
        tag_datetime = self.proxy.default_tags.get(TAG_DATETIME)
        """if enum"""
        e = tag_enum.find_enum(value)
        if e:
            return e
        """if datetime"""
        if re.match(tag_datetime.patternForTimef, value):
            time = datetime.strptime(value, '%Y-%m-%d %H:%M:%S.%f')
            return time
        if re.match(tag_datetime.patternForTime, value):
            time = datetime.strptime(value, '%Y-%m-%d %H:%M:%S')
            return time
        return value

tags = [TagEnum, TagBytes, TagDatetime, TagDict, TagList, TagTuple, TagStr, TagNum]


from .models import FamilyIdentity
from datetime import date, datetime

def auto_reset():

    today = datetime.today()
    # weekday = today.weekday()
    # hours = today.hour
    # minutes = today.min

    if (today.weekday() == 5) and (today.hour >= 17 and today.minute >= 1):
        families = FamilyIdentity.objects.all()
        for family in families:
            family.verified = False
            family.save()
# day -1 and time after 12 +1


#!/usr/bin/env python3

# quesiton 31 coin sums
#In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:

#1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
#It is possible to make £2 in the following way:

#1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
#How many different ways can £2 be made using any number of coins?

# idea - use recursive programming
def countChange(money, coins):
  if money==0:
    return 1
  elif len(coins)==0 or money<0:
    return 0
  else: return(countChange(money - coins[0], coins) + countChange(money, coins[1:]))

coins = [200,100,50,20,10,5,2,1]
print(countChange(200,coins))
# 73682 YAY


#
#
#   APPLICATION DE GENERATION DE REPOSITORY NS-LETSENCRYPT
#   AUTHOR : LAIR BENOIT
#   CREATION : 07/03/2017
#
#
from shutil import copy
import sys

#source file
print (sys.argv[1])
src_repository=sys.argv[0]
#dest file
print (sys.argv[2])
dest_repository=sys.argv[1]

#copyfile(src_repository, dest_repository)
copy(src_repository, dest_repository)


#!/usr/bin/env python

"""
Fix broken links to input raw movies.
"""

import os
import argparse

class Main:
    def __init__(self):
        parser = argparse.ArgumentParser(
            description="Create movie stacks from the individual "
                        "frame files.")
        add = parser.add_argument  # shortcut
        add('inputPath', default='',
            help='Input path where the broken links are. '
                 'Usually something like: ~/ScipionUserData/projects/ProjectA/Runs/000002_ProtImportMovies/extra')
        add('rawDataPath',
            help='Where the raw movies are located. The last directoy name of this value should be'
                 'contained in the name of the link files. If not, you should provide the --root_prefix parameter.')
        add('--root_prefix',
            help='')

        add('--absolute', action='store_true',
            help='Provide this option if you want to create absolute links. '
                 'By default relative links are created to the raw data path. ')

        args = parser.parse_args()
        rawDataPath = args.rawDataPath.rstrip('/')
        rawDataFolder = os.path.basename(rawDataPath)

        if not os.path.exists(args.inputPath):
            raise Exception("Input path '%s' does not exists. " % args.inputPath)

        if not os.path.exists(args.rawDataPath):
            raise Exception("Raw data path '%s' does not exists. " % args.rawDataPath)

        broken_links = [f for f in os.listdir(args.inputPath)]

        dataFolderDst = os.path.join(args.inputPath, rawDataFolder)
        if args.absolute:
            dataFolderSource = os.path.abspath(rawDataPath)
        else:
            dataFolderSource = os.path.relpath(os.path.realpath(rawDataPath),
                                               os.path.realpath(dataFolderDst))

        print("Creating link to raw data folder...")
        print("%s -> %s" % (dataFolderDst, dataFolderSource))
        os.symlink(dataFolderSource, dataFolderDst)

        for fn in broken_links:
            path = os.path.join(args.inputPath, fn)
            if os.path.islink(path):
                targetPath = os.readlink(path)

                parts = targetPath.split('/')
                if rawDataFolder in parts:
                    i = parts.index(rawDataFolder)
                    newTargetPath = '/'.join(parts[i:])
                    os.remove(path)
                    # Create the new link pointing to the new location
                    os.symlink(newTargetPath, path)

                print("%s \n   -> %s\n   -> %s\n" % (path, targetPath, newTargetPath))

if __name__ == "__main__":
    Main()


from cv2 import imread, cvtColor, COLOR_BGR2RGB
from final_fns import face_locations, face_encodings
from os import listdir
import pandas as pd

path = "img"
images = []
nameList = listdir(path)
print(nameList)

for img in nameList:
    curImg=imread(f'{path}/{img}')
    images.append(curImg)

#encoding

def findEncodings(images):
    encodedList = []
    for img in images:
        img = cvtColor(img, COLOR_BGR2RGB)
        #encode = face_recognition.face_encodings(img)[0]
        camFaceLocs = face_locations(img)
        # similarly here we are giving camFaceLocs, the face location as argument for encodings
        encode = face_encodings(img, camFaceLocs)[0]
        encodedList.append(encode)
    return encodedList

encodedListKnown = findEncodings(images)
print("ENcoding COMPLETE")

df = pd.DataFrame(encodedListKnown)
df.to_csv("Encodings_db.csv")
print("Encoding Database - 'Encodings_db' saved")


from __future__ import print_function
import argparse
import os
import pprint
import subprocess
import json
import six

extract_cmd = "/usr/bin/unrar x exe_file extract_dir"
find_cmd = "/usr/bin/find compressed_dir  -name '*.exe' "

def exe_cmd(cmd,cwd=None):
    proc = subprocess.Popen([cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd)
    (out, err) = proc.communicate()
    output = {}
    output['out'] = out
    output['err'] = err
    return output

def read_config():
    command = ['/bin/bash', '-c', 'source /root/install/config.sh && env']
    proc = subprocess.Popen(command, stdout = subprocess.PIPE)
    for line in proc.stdout:
        line = line.decode()
        (key, _, value) = line.partition("=")
        os.environ[key] = value
    proc.communicate()

def get_mystack_params():
    read_config()
    stackid = os.environ['MyStackId'].rstrip()
    cmd = aws_cmd
    cmd = cmd + ' cloudformation describe-stacks --stack-name '
    cmd = cmd + stackid
    cmd = cmd +  ' --region ' + os.environ['REGION'].rstrip()
    new_env=os.environ.copy()
    for key,value in six.iteritems(new_env):
        new_env[key]=value.strip()
    proc = subprocess.Popen([cmd], env=new_env, stdout=subprocess.PIPE, shell=True)
    proc.wait()
    (out, err) = proc.communicate()
    out = out.decode()
    out_json = json.loads(out)
    params = out_json['Stacks'][0]['Parameters']
    input = {}
    for p in params:
        key = p['ParameterKey']
        val = p['ParameterValue']
        input[key] = val
    return input

def download_s3(s3path,odir):
    new_env=os.environ.copy()
    for key,value in six.iteritems(new_env):
        new_env[key]=value.strip()
    if not os.path.exists(odir):
        os.makedirs(odir)
    s3bucket = s3path.split('s3://')[1].split('/')[0]
    cmd = aws_cmd + ' s3api get-bucket-location --bucket ' + s3bucket + " | grep -Po '(?" + '<="LocationConstraint": ")[^"]*'+ "'"
    s3bucketregion = subprocess.Popen([cmd], env=new_env, stdout=subprocess.PIPE, shell=True)
    s3bucketregion = s3bucketregion.stdout.read().rstrip()
    s3bucketregion = s3bucketregion.decode()
    print('Will download HANA media from ' + s3path + ' To ' + odir)
    cmd = aws_cmd
    cmd = cmd + ' s3 sync ' + s3path
    if s3bucketregion == "null" or s3bucketregion == "":
        cmd = cmd + ' ' + odir
    else:
        cmd = cmd + ' ' + odir + ' --region ' + s3bucketregion
    if not os.path.exists(odir):
        os.makedirs(odir)
    print('Executing ' + cmd)
    output = subprocess.Popen([cmd], env=new_env, stdout=subprocess.PIPE, shell=True)
    (out, err) = output.communicate()
    cmd = 'chmod 755 ' + odir + '/*.exe'
    output = exe_cmd(cmd)

def main():
    parser = argparse.ArgumentParser(description='Download HANA Media (No extraction)')
    parser.add_argument('-o', dest="odir",metavar="DIR",required = True,
                              help='DIR to download Media')
    args = parser.parse_args()
    odir = args.odir
    read_config()
    params = get_mystack_params()
    s3path = params['HANAInstallMedia']
#    compressed_dir = odir + '/' + 'compressed'
    compressed_dir = odir + 'compressed'
    download_s3(s3path,compressed_dir)

if __name__ == "__main__":
    read_config()
    aws_cmd = os.environ['AWSCLI_BIN'].rstrip()
    main()


MAIN_DIR = "models/concept_recognizer"

DATA_DIR = MAIN_DIR+"/data"
DATA_TRAIN = DATA_DIR+"/train"
DATA_DEV = DATA_DIR+"/dev"
DATA_TEST = DATA_DIR+"/test"

MODEL_PATH = MAIN_DIR + "/model"

LSTM_SIZE = 128
EMBEDDING_SIZE = 300
DROPOUT = 0.05
BATCH_SIZE = 300
MAX_EPOCHS = 15
MAX_LENGTH = 35

def split_line(line):
    spl = line.strip().split("\t")
    try:
        return ((spl[0], spl[2]), spl[1].split(";"))
    except Exception:
        return []

def read_file(filepath):
    data = open(filepath, "r")
    lines = filter(lambda x: x, map(split_line, data.readlines()))

    X, Y = zip(*lines)
    return X, Y

def write_file(data, filepath):
    # here, data are items
    mod_data = list(map(lambda i: i.question.strip() +"\t" + i.c1.strip()+";"+i.c2.strip() + "\t" + i.relation, data))

    fout = open(filepath, "w")
    fout.write("\n".join([d.strip() for d in mod_data]))


from math import radians, atan2
import mathutils

global errors
errors = []
correction_local = mathutils.Euler((radians(90), 0, radians(90))).to_matrix().to_4x4()
correction_global = mathutils.Euler((radians(-90), radians(-90), 0)).to_matrix().to_4x4()

def vec_roll_to_mat3(vec, roll):
	#port of the updated C function from armature.c
	#https://developer.blender.org/T39470
	#note that C accesses columns first, so all matrix indices are swapped compared to the C version

	nor = vec.normalized()
	THETA_THRESHOLD_NEGY = 1.0e-9
	THETA_THRESHOLD_NEGY_CLOSE = 1.0e-5

	#create a 3x3 matrix
	bMatrix = mathutils.Matrix().to_3x3()

	theta = 1.0 + nor[1]

	if (theta > THETA_THRESHOLD_NEGY_CLOSE) or ((nor[0] or nor[2]) and theta > THETA_THRESHOLD_NEGY):

		bMatrix[1][0] = -nor[0]
		bMatrix[0][1] = nor[0]
		bMatrix[1][1] = nor[1]
		bMatrix[2][1] = nor[2]
		bMatrix[1][2] = -nor[2]
		if theta > THETA_THRESHOLD_NEGY_CLOSE:
			#If nor is far enough from -Y, apply the general case.
			bMatrix[0][0] = 1 - nor[0] * nor[0] / theta
			bMatrix[2][2] = 1 - nor[2] * nor[2] / theta
			bMatrix[0][2] = bMatrix[2][0] = -nor[0] * nor[2] / theta

		else:
			#If nor is too close to -Y, apply the special case.
			theta = nor[0] * nor[0] + nor[2] * nor[2]
			bMatrix[0][0] = (nor[0] + nor[2]) * (nor[0] - nor[2]) / -theta
			bMatrix[2][2] = -bMatrix[0][0]
			bMatrix[0][2] = bMatrix[2][0] = 2.0 * nor[0] * nor[2] / theta

	else:
		#If nor is -Y, simple symmetry by Z axis.
		bMatrix = mathutils.Matrix().to_3x3()
		bMatrix[0][0] = bMatrix[1][1] = -1.0

	#Make Roll matrix
	rMatrix = mathutils.Matrix.Rotation(roll, 3, nor)

	#Combine and output result
	mat = rMatrix * bMatrix
	return mat

def mat3_to_vec_roll(mat):
	#this hasn't changed
	vec = mat.col[1]
	vecmat = vec_roll_to_mat3(mat.col[1], 0)
	vecmatinv = vecmat.inverted()
	rollmat = vecmatinv * mat
	roll = atan2(rollmat[0][2], rollmat[2][2])
	return vec, roll

def name_to_blender(s):
	s = s.rstrip(b"\x00").decode("utf-8")
	if "_l_" in s:
		s+= ".l"
	elif "_L_" in s:
		s+= ".L"
	if "_r_" in s:
		s+= ".r"
	elif "_R_" in s:
		s+= ".R"
	return s.replace("_R_","_").replace("_L_","_").replace("_r_","_").replace("_l_","_")

def name_to_tmd(s):
	if '.L' in s:
		s = s[:2]+"L_"+s[2:-2]
	elif '.R' in s:
		s = s[:2]+"R_"+s[2:-2]
	elif '.l' in s:
		s = s[:2]+"l_"+s[2:-2]
	elif '.r' in s:
		s = s[:2]+"r_"+s[2:-2]
	return s

def log_error(error):
	print(error)
	global errors
	errors.append(error)


import PAsearchSites
import PAutils

def search(results, lang, siteNum, searchData):
    sceneID = searchData.title.split(' ', 1)[0]
    if unicode(sceneID, 'UTF-8').isdigit():
        searchData.title = searchData.title.replace(sceneID, '', 1).strip()
    else:
        sceneID = None

    if sceneID:
        sceneURL = PAsearchSites.getSearchBaseURL(siteNum) + '/post/details/' + sceneID
        req = PAutils.HTTPRequest(sceneURL)
        detailsPageElements = HTML.ElementFromString(req.text)

        curID = PAutils.Encode(sceneURL)
        titleNoFormatting = detailsPageElements.xpath('//div[contains(@class, "mediaHeader")]//span[contains(@class, "title")]')[0].text_content().strip()
        studio = detailsPageElements.xpath('//span[contains(@class, "type")]')[0].text_content().split('|')[0].strip()
        score = 100

        results.Append(MetadataSearchResult(id='%s|%d' % (curID, siteNum), name='%s [%s]' % (titleNoFormatting, studio), score=score, lang=lang))
    else:
        searchData.encoded = searchData.title.replace(' ', '+')
        req = PAutils.HTTPRequest(PAsearchSites.getSearchSearchURL(siteNum) + searchData.encoded)
        searchResults = HTML.ElementFromString(req.text)
        for searchResult in searchResults.xpath('//div[contains(@class, "post")]'):
            titleNoFormatting = searchResult.xpath('.//span[contains(@class, "title")]')[0].text_content().strip()
            sceneURL = searchResult.xpath('.//a[contains(@class, "media")]/@href')[0]

            studio = searchResult.xpath('.//span[contains(@class, "source")]')[0].text_content().strip()
            sceneCover = PAutils.Encode(searchResult.xpath('.//a[contains(@class, "media")]//img[contains(@class, "image")]/@src')[0])
            releaseDate = searchData.dateFormat() if searchData.date else ''

            curID = PAutils.Encode(sceneURL)

            score = 90 - Util.LevenshteinDistance(searchData.title.lower(), titleNoFormatting.lower())
            if PAsearchSites.getSearchSiteName(siteNum).lower() == studio.lower():
                score += 10

            results.Append(MetadataSearchResult(id='%s|%d|%s|%s' % (curID, siteNum, releaseDate, sceneCover), name='%s [%s]' % (titleNoFormatting, studio), score=score, lang=lang))

    return results

def update(metadata, lang, siteNum, movieGenres, movieActors, art):
    metadata_id = str(metadata.id).split('|')
    sceneURL = PAutils.Decode(metadata_id[0])
    if not sceneURL.startswith('http'):
        sceneURL = PAsearchSites.getSearchBaseURL(siteNum) + sceneURL
    searchDate = metadata_id[2] if len(metadata_id) > 2 else ''
    sceneCover = PAutils.Decode(metadata_id[3]) if len(metadata_id) > 3 else ''

    req = PAutils.HTTPRequest(sceneURL)
    detailsPageElements = HTML.ElementFromString(req.text)

    # Title
    metadata.title = detailsPageElements.xpath('//div[contains(@class, "mediaHeader")]//span[contains(@class, "title")]')[0].text_content().strip()

    # Summary
    metadata.summary = detailsPageElements.xpath('//span[contains(@class, "description")]')[0].text_content().strip()

    # Studio
    metadata.studio = detailsPageElements.xpath('//span[contains(@class, "type")]')[0].text_content().split('|')[0].strip()

    # Tagline and Collection(s)
    metadata.collections.clear()
    metadata.collections.add('CouplesCinema')
    metadata.collections.add(metadata.studio)

    # Release Date
    if searchDate:
        date_object = parse(searchDate)
        metadata.originally_available_at = date_object
        metadata.year = metadata.originally_available_at.year
    else:
        Log('Failed to parse searchDate: %s , using release year' % searchDate)
        year = detailsPageElements.xpath('//span[contains(@class, "type")]')[0].text_content().split('|')[1].strip()
        metadata.year = int(year)

    # Genres
    movieGenres.clearGenres()

    # Actors
    movieActors.clearActors()
    for actorLink in detailsPageElements.xpath('//div[contains(@class, "cast")]/a'):
        actorName = actorLink.text_content().strip()
        actorPhotoURL = ''

        movieActors.addActor(actorName, actorPhotoURL)

    # Posters
    xpaths = [
        '//video/@poster',
    ]

    if sceneCover:
        art.append(sceneCover)

    for xpath in xpaths:
        for img in detailsPageElements.xpath(xpath):
            if not img.startswith('http'):
                img = PAsearchSites.getSearchBaseURL(siteNum) + img

            art.append(img)

    Log('Artwork found: %d' % len(art))
    for idx, posterUrl in enumerate(art, 1):
        if not PAsearchSites.posterAlreadyExists(posterUrl, metadata):
            # Download image file for analysis
            try:
                image = HTTPRequest(posterUrl)
                im = StringIO(image.content)
                resized_image = Image.open(im)
                width, height = resized_image.size
                # Add the image proxy items to the collection
                if width > 1:
                    # Item is a poster
                    metadata.posters[posterUrl] = Proxy.Media(image.content, sort_order=idx)
                if width > 100:
                    # Item is an art item
                    metadata.art[posterUrl] = Proxy.Media(image.content, sort_order=idx)
            except:
                pass

    return metadata


#!/usr/bin/python
#
# Fruits part 1. Code in python
# Inpired by the code from Francois Chollet.
#                     "Deep learning with Python." Manning (2018) 362 pages.
#
# From François Chollet's book:
# "Convnets are the best type of machine-learning models for computer-vision tasks.
#  It’s possible to train one from scratch even on a very small dataset, with decent results.
#  On a small dataset, overfitting will be the main issue. Data augmentation is a powerful way,
#  to fight overfitting when you’re working with image data.
#  It’s easy to reuse an existing convnet on a new dataset via feature extraction.
#  This is a valuable technique for working with small image datasets."
#
# Preparation: Unzip the file fruits_200.zip containing oranges and pinapples pictures
#              in the same directory as this file. Note, this is a VERY VERY small dataset
#              created using picture from the ImageNet dataset and used for demonstration.
#
#              Orange: http://image-net.org/synset?wnid=n07747607
#              Pineapple: http://image-net.org/synset?wnid=n07753275
#
#               fruits_200.zip
#               ---------------
#               Training  : 140 (70%)
#               Validation: 30 (15%)
#               Test      : 30 (15%)
#               Training:   orange (72), pineapple (68)
#               Validation: orange (19), pineapple (11)
#               Test :      orange (15), pineapple (15)
#
#               fruits_300.zip
#               ---------------
#               Training  : 210 (70%)
#               Validation: 45  (15%)
#               Test      : 45
#
#               fruits_400.zip
#               ---------------
#               Training  : 280 (70%)
#               Validation: 60  (15%)
#               Test      : 60
###############################################################################
# GLOBAL IMPORT                                                               #
###############################################################################
from __future__ import absolute_import, unicode_literals, division, print_function
import glob
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.optimizers import RMSprop
from keras.callbacks import *
from keras import backend as K

###############################################################################
# HELPER FUNCTIONS                                                            #
###############################################################################
def number_of_files(dirname):
    cpt = sum([len(files) for r, d, files in os.walk(dirname)])
    return cpt

################################################################################
# DEFINITION OF INPUT DATA                                                     #
################################################################################
train_data_dir = 'fruits_training'
validation_data_dir = 'fruits_validation'
test_data_dir='fruits_test'
epochs = 100    # Number of iteration over the dataset
batch_size = 24 # Number of images processed at the same time
nb_train_samples=number_of_files(train_data_dir)
nb_validation_samples=number_of_files(validation_data_dir)
nb_test_samples=number_of_files(test_data_dir)
img_width, img_height = 150, 150 # Training dimensions i.e. image sizes will be reduced

# This depend on the back-end (mxnet, Tensorflow,...)
if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

################################################################################
# CNN                                                                          #
################################################################################
model = Sequential() # Type of model
# see https://keras.io/getting-started/sequential-model-guide/
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(128, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# Last layers
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5)) # Dropout
model.add(Dense(1)) # One last cell
model.add(Activation('sigmoid'))

################################################################################
# OPTIMIZATION                                                                 #
################################################################################

rmsprop=RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0)

model.compile(loss='binary_crossentropy',
              optimizer=rmsprop,
              metrics=['accuracy'])

model.summary() # Display a summary of the model

################################################################################
# DATA AUGMENTATION                                                            #
################################################################################

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest')

# Only normalize on validation set
val_datagen = ImageDataGenerator(
    rescale=1. / 255,
)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = val_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

test_generator = val_datagen.flow_from_directory(
    test_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
	shuffle=False,
    class_mode='binary')

################################################################################
# RUN MODEL                                                                    #
################################################################################
csv_logger = CSVLogger('model_log.csv', append=True, separator=';')
checkpointer = ModelCheckpoint(filepath='model.{epoch:02d}-{val_acc:.2f}.hdf5',
                               verbose=1, monitor='val_loss', save_best_only=True)

original_hist=model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size,
    callbacks=[csv_logger,checkpointer],
    verbose=1)

################################################################################
# SAVE MODEL                                                                   #
################################################################################
model.save("final_model.hdf5")
#
# Note: To load model:
# from keras.models import load_model
# model=load_model("final_model.hdf5")
################################################################################
# VISUALIZE THE TRAINING RESULTS                                               #
################################################################################

import matplotlib.pyplot as plt
original_hist.history

acc = original_hist.history['acc']
val_acc = original_hist.history['val_acc']
loss = original_hist.history['loss']
val_loss = original_hist.history['val_loss']
epochs = range(1, len(acc) + 1)

# Colors are from are color from https://xkcd.com/color/rgb/
plt.plot(epochs, loss, 'xkcd:sky blue', label='Training loss')
plt.plot(epochs, val_loss, 'xkcd:green', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

plt.savefig('loss.png', bbox_inches='tight')

plt.clf()   # New figure
acc_values = original_hist.history['acc']
val_acc_values = original_hist.history['val_acc']

plt.plot(epochs, acc, 'xkcd:sky blue', label='Training acc.')
plt.plot(epochs, val_acc, 'xkcd:green', label='Validation acc.')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

plt.savefig('accuracy.png', bbox_inches='tight')

################################################################################
# RUN THE MODEL ON THE TEST DATA                                               #
################################################################################

filenames = test_generator.filenames
nb_samples = len(filenames)
y_true_labels = test_generator.classes
y_indices=test_generator.class_indices
print(len(y_true_labels))

# Evaluate all the test sample
evalu = model.evaluate_generator(test_generator,steps = nb_samples // batch_size)

print("Total samples: "+str(nb_test_samples)+" Final accuracy: "+str(evalu[1]))
test_generator.reset()
predicted = model.predict_generator(test_generator,steps = nb_samples / batch_size, verbose=1)
y_pred = np.rint(np.concatenate(predicted, axis=0)).astype(int) # round to nearest integer

# Write some predictions
labels = ['orange','pineapple']
predictions = [labels[k] for k in y_pred]
filenames=test_generator.filenames
results=pd.DataFrame({"Filename":filenames,
                      "Values":np.concatenate(predicted, axis=0),
                      "Predictions":predictions})
results.to_csv("predicted.csv", sep=";")

################################################################################
# TO DO                                                                        #
################################################################################
#
# 1. Try to adjust the batch size and the number of epoch
# 2. Add mode layers to the CNN
# 3. Try to increase the number of samples (see fruits_300.zip and fruits_400.zip)
# 4. Part 2. Use pre-trained neural networks


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

""" Benchmark for the XGBoost Benchmark from hpobench/benchmarks/ml/xgboost_benchmark """

from hpobench.container.client_abstract_benchmark import AbstractBenchmarkClient

class XGBoostBenchmark(AbstractBenchmarkClient):
    def __init__(self, **kwargs):
        kwargs['benchmark_name'] = kwargs.get('benchmark_name', 'XGBoostBenchmark')
        kwargs['container_name'] = kwargs.get('container_name', 'xgboost_benchmark')
        super(XGBoostBenchmark, self).__init__(**kwargs)


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

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

class ConvNet(object):
    """construct ConvNet"""
    def __init__(self, is_training,data_format,batch_norm_decay,batch_norm_epsilon):
        super(ConvNet, self).__init__()
        self._batch_norm_decay = batch_norm_decay
        self._batch_norm_epsilon = batch_norm_epsilon
        self._is_training = is_training
        assert data_format in ('channels_first','channels_last')
        self._data_format = data_format

    def forward_pass(self,x):
        raise NotImplementedError('forward pass function is not implemented!')

    def _relu(self,x):
        return tf.nn.relu(x)

    def _relu_layer(self,x):
        with tf.name_scope('relu') as name_scope:
            x = self._relu(x)
        tf.logging.info('image after unit %s: %s', name_scope, x.get_shape())
        return x

    def _conv(self,x,kernel_size,filters,strides,is_atrous=False):
        padding = 'SAME'
        if not is_atrous and strides >1:
            pad = kernel_size -1
            pad_beg = pad // 2
            pad_end = pad - pad_beg
            if self.data_format == 'channels_first':
                x = tf.pad(x,[[0,0],[0,0],[pad_beg,pad_end],[pad_beg,pad_end]])
            else:
                x = tf.pad(x,[[0,0],[pad_beg,pad_end],[pad_beg,pad_end],[0,0]])
            padding = 'VALID'
        return tf.layers.conv2d(
            inputs=x,
            kernel_size=kernel_size,
            filters=filters,
            strides=strides,
            padding=padding,
            use_bias=False,
            data_format=self._data_format)

    def _conv_layer(self, x,kernel_size,filters,strides,is_atrous=False):
        with tf.name_scope('conv') as name_scope:
            x = self._conv(x,kernel_size,filters,strides,is_atrous)
            x = self._batch_norm(x)
            x = self._relu(x)
        tf.logging.info('image after unit %s: %s', name_scope, x.get_shape())
        return x

    def _batch_norm(self,x):
        if self._data_format == 'channels_first':
            data_format = 'NCHW'
        else:
            data_format = 'NHWC'
        return tf.contrib.layers.batch_norm(x,
            decay=self._batch_norm_decay,
            center=True,
            scale=True,
            epsilon=self._batch_norm_epsilon,
            is_training=self._is_training,
            fused=True,
            data_format=data_format)

    def _full_connected_layer(self,x,out_dim):
        with tf.name_scope('fully_connected') as name_scope:
            if x.get_shape().ndims == 4:
                x = self._global_avg_pool(x)
            x = tf.layers.dense(x,out_dim)
        tf.logging.info('image after unit %s: %s', name_scope, x.get_shape())
        return x

    def _avg_pool_layer(self,x,pool_size, stride):
        with tf.name_scope('avg_pool') as name_scope:
            x = tf.layers.average_pooling2d(
                x,pool_size,stride, 'SAME',data_format=self._data_format)
        tf.logging.info('image after unit %s: %s', name_scope, x.get_shape())
        return x

    def _global_avg_pool(self,x):
        with tf.name_scope('global_avg_pool') as name_scope:
            assert x.get_shape().ndims == 4
            if self._data_format == 'channels_first':
                x = tf.reduce_mean(x,[2,3])
            else:
                x = tf.reduce_mean(x,[1,2])
        tf.logging.info('image after unit %s: %s', name_scope, x.get_shape())
        return x

    def _max_pool_layer(self,x,pool_size,stride):
        with tf.name_scope('max_pool') as name_scope:
            x = tf.layers.max_pooling2d(
                x,pool_size,stride,'SAME',data_format=self._data_format)
        tf.logging.info('image after unit %s: %s', name_scope, x.get_shape())
        return x

    def _softmax_layer(self,x):
        with tf.name_scope('softmax') as name_scope:
            x = tf.nn.softmax(x)
        tf.logging.info('image after unit %s: %s', name_scope, x.get_shape())
        return x


#!/usr/bin/env python2
import ttk

sticky_top_left = ('N', 'S')
sticky_left_right_bottom = ('E', 'W', 'S')
sticky_all_sides = ('N', 'E', 'W', 'S')
sticky_left = 'W'
sticky_right = 'E'

bottom_frame = dict(frame=dict(width=1000, height=40, bd=1, background='gray'),
                    grid=dict(row=2, column=0, sticky=sticky_left_right_bottom,))
status_frame = dict(frame=dict(width=1000, height=40, bd=1, background='gray'),
                    grid=dict(row=3, column=0, sticky=sticky_left_right_bottom,))

bottombar_properties = dict(row=2,column=0, sticky=sticky_left_right_bottom,)

test_header_properties = [dict(width=600,
                               height=20,
                               padx=5
                              ),
                          dict(row=0,
                               column=0,
                               sticky=sticky_top_left
                          )]

test_body_properties = [dict(width=600,
                             height=20,
                             padx=5
                        ),dict(
                        )]

search_test_button = dict(row=0, column=0, sticky='w', padx=(5, 0), pady=(5, 3))
run_test_button = dict(row=0, column=1, sticky='w', padx=(5, 0), pady=(5, 3))
stop_test_button = dict(row=0, column=2, sticky='w', padx=(5, 0), pady=(5, 3))
add_test_button = dict(row=0, column=3, sticky='e', padx=(5, 0), pady=(5, 3))
remove_test_button = dict(row=0, column=4, sticky='e', padx=(5, 5), pady=(5, 3))

"""
Theme compatibility options
"""
def config_ttk_style():
    style = ttk.Style()
    try:
        style.theme_use('xpnative')
    except:
        try:
            style.theme_use('winnative')
        except:
            style.theme_use('alt')


# Working Proect Sniffer

import socket
import struct
import binascii
import textwrap
import msvcrt

def main():
    # Get host
    host = socket.gethostbyname(socket.gethostname())
    print('IP: {}'.format(host))

    # Create a raw socket and bind it
    conn = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
    conn.bind((host, 0))

    # Include IP headers
    conn.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
    # Enable promiscuous mode
    conn.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

    # ОСНОВНОЙ цикл до нажатия любой клавиши
    while not msvcrt.kbhit():
        # Recive data
        raw_data, addr = conn.recvfrom(65536)

        # Unpack data
        dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)

        print('\nEthernet Frame:')
        print("Destination MAC: {}".format(dest_mac))
        print("Source MAC: {}".format(src_mac))
        print("Protocol: {}".format(eth_proto))

# Unpack ethernet frame
def ethernet_frame(data):
    dest_mac, src_mac, proto = struct.unpack('!6s6s2s', data[:14])
    return get_mac_addr(dest_mac), get_mac_addr(src_mac), get_protocol(proto), data[14:]

# Return formatted MAC address AA:BB:CC:DD:EE:FF
def get_mac_addr(bytes_addr):
    bytes_str = map('{:02x}'.format, bytes_addr)
    mac_address = ':'.join(bytes_str).upper()
    return mac_address

# Return formatted protocol ABCD
def get_protocol(bytes_proto):
    bytes_str = map('{:02x}'.format, bytes_proto)
    protocol = ''.join(bytes_str).upper()
    return protocol

main()