import functools
from collections import Counter
from prettytable import PrettyTable

import torch
import torch.nn as nn
from torch.nn.utils.rnn import PackedSequence
from nni.compression.base.wrapper import ModuleWrapper

__all__ = ['count_flops_params']

def _get_params(m):
    return sum([p.numel() for p in m.parameters()])

class ModelProfiler:

    def __init__(self, custom_ops=None, mode='default'):
        """
        ModelProfiler is used to share state to hooks.

        Parameters
        ----------
        custom_ops: dict
            a mapping of (module -> torch.nn.Module : custom operation)
            the custom operation is a callback funtion to calculate
            the module flops, parameters and the weight shape, it will overwrite the default operation.
            for reference, please see ``self.ops``.
        mode:
            the mode of how to collect information. If the mode is set to `default`,
            only the information of convolution, linear and rnn modules will be collected.
            If the mode is set to `full`, other operations will also be collected.
        """
        self.ops = {
            nn.Conv1d: self._count_convNd,
            nn.Conv2d: self._count_convNd,
            nn.Conv3d: self._count_convNd,
            nn.ConvTranspose1d: self._count_convNd,
            nn.ConvTranspose2d: self._count_convNd,
            nn.ConvTranspose3d: self._count_convNd,
            nn.Linear: self._count_linear,
            nn.RNNCell: self._count_rnn_cell,
            nn.GRUCell: self._count_gru_cell,
            nn.LSTMCell: self._count_lstm_cell,
            nn.RNN: self._count_rnn,
            nn.GRU: self._count_gru,
            nn.LSTM: self._count_lstm
        }
        self._count_bias = False
        if mode == 'full':
            self.ops.update({
                nn.BatchNorm1d: self._count_bn,
                nn.BatchNorm2d: self._count_bn,
                nn.BatchNorm3d: self._count_bn,
                nn.LeakyReLU: self._count_relu,
                nn.AvgPool1d: self._count_avgpool,
                nn.AvgPool2d: self._count_avgpool,
                nn.AvgPool3d: self._count_avgpool,
                nn.AdaptiveAvgPool1d: self._count_adap_avgpool,
                nn.AdaptiveAvgPool2d: self._count_adap_avgpool,
                nn.AdaptiveAvgPool3d: self._count_adap_avgpool,
                nn.Upsample: self._count_upsample,
                nn.UpsamplingBilinear2d: self._count_upsample,
                nn.UpsamplingNearest2d: self._count_upsample
            })
            self._count_bias = True

        if custom_ops is not None:
            self.ops.update(custom_ops)

        self.mode = mode
        self.results = []

    def _push_result(self, result):
        self.results.append(result)

    def _get_result(self, m, flops):
        # assume weight is called `weight`, otherwise it's not applicable
        # if user customize the operation, the callback function should
        # return the dict result, inluding calculated flops, params and weight_shape.

        result = {
            'flops': flops,
            'params': _get_params(m),
            'weight_shape': tuple(m.weight.size()) if hasattr(m, 'weight') else 0,
        }
        return result

    def _count_convNd(self, m, x, y):
        cin = m.in_channels
        kernel_ops = torch.zeros(m.weight.size()[2:]).numel()
        output_size = torch.zeros(y.size()[2:]).numel()
        cout = y.size()[1]

        if hasattr(m, 'weight_mask'):
            cout = m.weight_mask.sum() // (cin * kernel_ops)

        total_ops = cout * output_size * kernel_ops * cin // m.groups  # cout x oW x oH

        if self._count_bias:
            bias_flops = 1 if m.bias is not None else 0
            total_ops += cout * output_size * bias_flops

        return self._get_result(m, total_ops)

    def _count_linear(self, m, x, y):
        out_features = m.out_features
        if hasattr(m, 'weight_mask'):
            out_features = m.weight_mask.sum() // m.in_features
        total_ops = out_features * m.in_features

        if self._count_bias:
            bias_flops = 1 if m.bias is not None else 0
            total_ops += out_features * bias_flops

        return self._get_result(m, total_ops)

    def _count_bn(self, m, x, y):
        total_ops = 2 * x[0][0].numel()
        return self._get_result(m, total_ops)

    def _count_relu(self, m, x, y):
        total_ops = x[0][0].numel()
        return self._get_result(m, total_ops)

    def _count_avgpool(self, m, x, y):
        total_ops = y[0].numel()
        return self._get_result(m, total_ops)

    def _count_adap_avgpool(self, m, x, y):
        kernel = torch.Tensor([*(x[0].shape[2:])]) // torch.Tensor(list((m.output_size,))).squeeze()
        total_add = int(torch.prod(kernel))
        total_div = 1
        kernel_ops = total_add + total_div
        num_elements = y[0].numel()
        total_ops = kernel_ops * num_elements

        return self._get_result(m, total_ops)

    def _count_upsample(self, m, x, y):
        if m.mode == 'linear':
            total_ops = y[0].nelement() * 5  # 2 muls + 3 add
        elif m.mode == 'bilinear':
            # https://en.wikipedia.org/wiki/Bilinear_interpolation
            total_ops = y[0].nelement() * 11  # 6 muls + 5 adds
        elif m.mode == 'bicubic':
            # https://en.wikipedia.org/wiki/Bicubic_interpolation
            # Product matrix [4x4] x [4x4] x [4x4]
            ops_solve_A = 224  # 128 muls + 96 adds
            ops_solve_p = 35  # 16 muls + 12 adds + 4 muls + 3 adds
            total_ops = y[0].nelement() * (ops_solve_A + ops_solve_p)
        elif m.mode == 'trilinear':
            # https://en.wikipedia.org/wiki/Trilinear_interpolation
            # can viewed as 2 bilinear + 1 linear
            total_ops = y[0].nelement() * (13 * 2 + 5)
        else:
            total_ops = 0

        return self._get_result(m, total_ops)

    def _count_cell_flops(self, input_size, hidden_size, cell_type):
        # h' = \tanh(W_{ih} x + b_{ih}  +  W_{hh} h + b_{hh})
        total_ops = hidden_size * (input_size + hidden_size) + hidden_size

        if self._count_bias:
            total_ops += hidden_size * 2

        if cell_type == 'rnn':
            return total_ops

        if cell_type == 'gru':
            # r = \sigma(W_{ir} x + b_{ir} + W_{hr} h + b_{hr}) \\
            # z = \sigma(W_{iz} x + b_{iz} + W_{hz} h + b_{hz}) \\
            # n = \tanh(W_{in} x + b_{in} + r * (W_{hn} h + b_{hn})) \\
            total_ops *= 3

            # r hadamard : r * (~)
            total_ops += hidden_size

            # h' = (1 - z) * n + z * h
            # hadamard hadamard add
            total_ops += hidden_size * 3

        elif cell_type == 'lstm':
            # i = \sigma(W_{ii} x + b_{ii} + W_{hi} h + b_{hi}) \\
            # f = \sigma(W_{if} x + b_{if} + W_{hf} h + b_{hf}) \\
            # o = \sigma(W_{io} x + b_{io} + W_{ho} h + b_{ho}) \\
            # g = \tanh(W_{ig} x + b_{ig} + W_{hg} h + b_{hg}) \\
            total_ops *= 4

            # c' = f * c + i * g
            # hadamard hadamard add
            total_ops += hidden_size * 3

            # h' = o * \tanh(c')
            total_ops += hidden_size

        return total_ops

    def _count_rnn_cell(self, m, x, y):
        total_ops = self._count_cell_flops(m.input_size, m.hidden_size, 'rnn')
        return self._get_result(m, total_ops)

    def _count_gru_cell(self, m, x, y):
        total_ops = self._count_cell_flops(m.input_size, m.hidden_size, 'gru')
        return self._get_result(m, total_ops)

    def _count_lstm_cell(self, m, x, y):
        total_ops = self._count_cell_flops(m.input_size, m.hidden_size, 'lstm')
        return self._get_result(m, total_ops)

    def _get_bsize_nsteps(self, m, x):
        if isinstance(x[0], PackedSequence):
            batch_size = torch.max(x[0].batch_sizes)
            num_steps = x[0].batch_sizes.size(0)
        else:
            if m.batch_first:
                batch_size = x[0].size(0)
                num_steps = x[0].size(1)
            else:
                batch_size = x[0].size(1)
                num_steps = x[0].size(0)

        return batch_size, num_steps

    def _count_rnn_module(self, m, x, y, module_name):
        input_size = m.input_size
        hidden_size = m.hidden_size
        num_layers = m.num_layers

        _, num_steps = self._get_bsize_nsteps(m, x)
        total_ops = self._count_cell_flops(input_size, hidden_size, module_name)

        for _ in range(num_layers - 1):
            if m.bidirectional:
                cell_flops = self._count_cell_flops(hidden_size * 2, hidden_size, module_name) * 2
            else:
                cell_flops = self._count_cell_flops(hidden_size, hidden_size, module_name)
            total_ops += cell_flops

        total_ops *= num_steps
        return total_ops

    def _count_rnn(self, m, x, y):
        total_ops = self._count_rnn_module(m, x, y, 'rnn')

        return self._get_result(m, total_ops)

    def _count_gru(self, m, x, y):
        total_ops = self._count_rnn_module(m, x, y, 'gru')

        return self._get_result(m, total_ops)

    def _count_lstm(self, m, x, y):
        total_ops = self._count_rnn_module(m, x, y, 'lstm')

        return self._get_result(m, total_ops)

    def count_module(self, m, x, y, name):
        # assume x is tuple of single tensor
        result = self.ops[type(m)](m, x, y)
        output_size = y[0].size() if isinstance(y, tuple) else y.size()

        total_result = {
            'name': name,
            'input_size': tuple(x[0].size()),
            'output_size': tuple(output_size),
            'module_type': type(m).__name__,
            **result
        }

        self._push_result(total_result)

    def sum_flops(self):
        return sum([s['flops'] for s in self.results])

    def sum_params(self):
        return sum({s['name']: s['params'] for s in self.results}.values())

    def format_results(self):
        table = PrettyTable()
        name_counter = Counter([s['name'] for s in self.results])
        has_multi_use = any(map(lambda v: v > 1, name_counter.values()))
        name_counter = Counter()  # clear the counter to count from 0

        headers = [
            'Index',
            'Name',
            'Type',
            'Weight Shape',
            'Input Size',
            'Output Size',
            'FLOPs',
            '#Params',
        ]
        if has_multi_use:
            headers.append('#Call')

        table.field_names = headers
        for i, result in enumerate(self.results):
            flops_count = int(result['flops'].item()) if isinstance(result['flops'], torch.Tensor) else int(result['flops'])
            row_values = [
                i,
                result['name'],
                result['module_type'],
                str(result['weight_shape']),
                result['input_size'],
                result['output_size'],
                flops_count,
                result['params'],
            ]
            name_counter[result['name']] += 1
            if has_multi_use:
                row_values.append(name_counter[result['name']])
            table.add_row(row_values)
        table.align["Name"] = "l"
        return table

def count_flops_params(model, x, custom_ops=None, verbose=True, mode='default'):
    """
    Count FLOPs and Params of the given model. This function would
    identify the mask on the module and take the pruned shape into consideration.
    Note that, for sturctured pruning, we only identify the remained filters
    according to its mask, and do not take the pruned input channels into consideration,
    so the calculated FLOPs will be larger than real number.

    The FLOPs is counted "per sample", which means that input has a batch size larger than 1,
    the calculated FLOPs should not differ from batch size of 1.

    Parameters
    ---------
    model : nn.Module
        Target model.
    x : tuple or tensor
        The input shape of data (a tuple), a tensor or a tuple of tensor as input data.
    custom_ops : dict
        A mapping of (module -> torch.nn.Module : custom operation)
        the custom operation is a callback funtion to calculate
        the module flops and parameters, it will overwrite the default operation.
        for reference, please see ``ops`` in ``ModelProfiler``.
    verbose : bool
        If False, mute detail information about modules. Default is True.
    mode : str
        the mode of how to collect information. If the mode is set to ``default``,
        only the information of convolution and linear will be collected.
        If the mode is set to ``full``, other operations will also be collected.

    Returns
    -------
    tuple of int, int and dict
        Representing total FLOPs, total parameters, and a detailed list of results respectively.
        The list of results are a list of dict, each of which contains (name, module_type, weight_shape,
        flops, params, input_size, output_size) as its keys.
    """

    assert isinstance(x, tuple) or isinstance(x, torch.Tensor)
    assert mode in ['default', 'full']

    original_device = next(model.parameters()).device
    training = model.training

    if isinstance(x, tuple) and all(isinstance(t, int) for t in x):
        x = (torch.zeros(x).to(original_device), )
    elif torch.is_tensor(x):
        x = (x.to(original_device), )
    else:
        x = (t.to(original_device) for t in x)

    handler_collection = []
    profiler = ModelProfiler(custom_ops, mode)

    prev_m = None
    for name, m in model.named_modules():
        # dealing with weight mask here
        if isinstance(prev_m, ModuleWrapper):
            # weight mask is set to weight mask of its parent (wrapper)
            weight_mask = prev_m.pruning_target_spaces['weight'].mask
            m.weight_mask = weight_mask
        prev_m = m

        if type(m) in profiler.ops:
            # if a leaf node
            _handler = m.register_forward_hook(functools.partial(profiler.count_module, name=name))
            handler_collection.append(_handler)

    model.eval()

    with torch.no_grad():
        model(*x)

    # restore origin status
    model.train(training).to(original_device)
    for handler in handler_collection:
        handler.remove()

    if verbose:
        # get detail information
        print(profiler.format_results())
        print(f'FLOPs total: {profiler.sum_flops()}')
        print(f'#Params total: {profiler.sum_params()}')

    return profiler.sum_flops(), profiler.sum_params(), profiler.results


#!/usr/bin/env python

import zd
from os.path import join, dirname, abspath
from json import dump
from collections import Counter

ARROW = "➜"
_DIR = dirname(abspath(__file__))

class Parse:
  def __init__(self):
    self.count = Counter()
    self.total = 0

  def __call__(self, title, txt):
    self.total += 1
    count = self.count
    for line in txt[1:]:
      for i in line:
        if i not in ", \r\n":
          count[i] += 1
    if self.total % 1000 == 0:
      print(self.total)
      self.dump()

  def dump(self):
    r = {}
    for k, v in self.count.items():
      if v > 1000:
        r[k] = v
    with open(join(_DIR, "char.json"), "w") as f:
      dump(r, f, ensure_ascii=False)

def main():
  from os import walk
  from tqdm import tqdm
  parse = Parse()
  fileli = []

  for root, _, file_li in walk("/share/txt/data"):
    for filename in file_li:
      if filename.endswith(".zd"):
        filepath = join(root, filename)
        fileli.append(filepath)

  for filepath in tqdm(fileli):
    with zd.open(filepath) as f:
      it = iter(f)
      title = None
      txt = []
      for i in it:
        i = i.lower()
        if i.startswith(ARROW):
          if title:
            parse(title, txt)
          title = i[1:]
          txt = []
        else:
          txt.append(i)
      if title:
        parse(title, txt)
  parse.dump()

main()


'''
Created on 15.9.2013

@author: Peter Smorada
'''

################################################################################
#  Copyright (C) Peter Smorada 2013
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

import consts;
import sutils;
import xbmcaddon;  # @UnresolvedImport
import xbmc;  # @UnresolvedImport
import os;
import subprocess;
import threading;
import datetime;
import time;

addon= xbmcaddon.Addon(id = consts.addonId);

def getDiskType(diskId):
    try:
        diskType = __diskTypeDic[diskId];
    except:
        diskType = None;

    if diskType == None:
        diskType = "auto"
    return diskType;

def setDiskType(diskId, diskType):

    xbmc.log(msg="smartmontools: number of stored disk types before adding new one: " + str(len(__diskTypeDic)) , level=xbmc.LOGDEBUG);
    __diskTypeDic[diskId] = diskType;

    string = ""
    for key in __diskTypeDic.keys():
        string += ("&" if len(string) > 0 else "") + key + "=" + __diskTypeDic[key];

    addon.setSetting(consts.settingDiskTypes, string);

def parseDiskTypes():
    settings = addon.getSetting(consts.settingDiskTypes);
    dic = sutils.get_params(settings);
    xbmc.log(msg="smartmontools: stored disk types were read: " + str(len(dic)) , level=xbmc.LOGDEBUG);
    return dic;

def checkConfigurationFilesExistance():
    if not os.path.exists(consts.fileDB):
        xbmc.log(msg="smartmontools addon: DB file doesn't exist, copying default.", level=xbmc.LOGDEBUG);
        cmd = "cp " + consts.fileDBDefault + " " + consts.fileDB;
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True);
        output, errors = p.communicate();
        xbmc.log(msg="smartmontools stdout: " + str(output), level=xbmc.LOGDEBUG);
        xbmc.log(msg="smartmontools stderr: " + str(errors), level=xbmc.LOGDEBUG);

    daemonConfExists = os.path.exists(consts.fileDaemonConfig);
    if not daemonConfExists:
        xbmc.log(msg="smartmontools addon: copying default daemon config file.", level=xbmc.LOGDEBUG)

        cmd = "cp " + consts.fileDaemonConfigDefault + " " + consts.fileDaemonConfig;
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True);
        output, errors = p.communicate();
        xbmc.log(msg="smartmontools stdout: " + str(output), level=xbmc.LOGDEBUG);
        xbmc.log(msg="smartmontools stderr: " + str(errors), level=xbmc.LOGDEBUG);

def getDisks():
    cmd = "\"" + consts.script + "\" " + consts.commandDisks
    xbmc.log(msg="smartmon addon: getting connected disks", level=xbmc.LOGDEBUG);

    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True);
    output, errors = p.communicate();

    xbmc.log(msg="smartmontools stdout: " + str(output), level=xbmc.LOGDEBUG);
    xbmc.log(msg="smartmontools stderr: " + str(errors), level=xbmc.LOGDEBUG);

    return output.split();

def updateDatabase():
    cmd = "\"" + consts.updateScript + "\" " + consts.fileDB;
    xbmc.log(msg="smartmontools: starting DB update.", level=xbmc.LOGDEBUG);

    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True);
    output, errors = p.communicate();

    xbmc.log(msg="smartmontools stdout: " + str(output), level=xbmc.LOGDEBUG);
    xbmc.log(msg="smartmontools stderr: " + str(errors), level=xbmc.LOGDEBUG);

    # storing date when db update was performed
    today = datetime.date.today();
    todayString = str(today.year) + "-" + str(today.month) + "-" + str(today.day);
    xbmc.log(msg="smartmontools: updating date of last DB update check: " + todayString, level=xbmc.LOGDEBUG);
    addon.setSetting(consts.settingLastDBupdateChecked, todayString);
    # TODO: return true if updated

def shouldUpdateDB():
    allowed = True if addon.getSetting(consts.settingDBupdates) == "true" else False;
    #allowed = True;
    if allowed:
        lastUpdateDateString = addon.getSetting(consts.settingLastDBupdateChecked);
        #lastUpdateDateString = "2013-09-12"
        if not lastUpdateDateString:  # if check was never performed it should be done
            xbmc.log(msg="smartmontools: update of db was never performed.", level=xbmc.LOGDEBUG);
            return True;
        l = lastUpdateDateString.split("-");
        lastCheckDate = datetime.date(int(l[0]), int(l[1]), int(l[2]));
        today = datetime.date.today();
        td = today - lastCheckDate;
        difference = td.days;
        updateInterval = int(addon.getSetting(consts.settingDBupdateInterval));

        if difference >= updateInterval:
            return True
            xbmc.log(msg="smartmontools: updating db due to number of days past " + str(difference) +".", level=xbmc.LOGDEBUG);
        else:
            xbmc.log(msg="smartmontools: not updating db due to number of days past " + str(difference) +".", level=xbmc.LOGDEBUG);
            return False;
    else:
        xbmc.log(msg="smartmontools: updating db is not allowed.", level=xbmc.LOGDEBUG);
        return False;

class dbUpdater (threading.Thread):

    def __init__(self, shouldRun = True):
        self.shouldRun = shouldRun;
        threading.Thread.__init__(self);

    def run(self):
        xbmc.log(msg="smartmontools: update thread started", level=xbmc.LOGDEBUG);
        while self.shouldRun:
            xbmc.log(msg="smartmontools: update thread performing check if is time to execute update script.", level=xbmc.LOGDEBUG);
            update = shouldUpdateDB();
            if update:
                xbmc.log(msg="smartmontools: update thread executing update check.", level=xbmc.LOGDEBUG);
                updateDatabase();
            time.sleep(2 * 60 * 60);

# initiation of needed module variable
__diskTypeDic = parseDiskTypes();


# 3.13 마지막 금요일 날짜 구하기

from datetime import datetime, timedelta

weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
            'Friday', 'Saturday', 'Sunday']

def get_previous_byday(dayname, start_date=None):
    if start_date is None:
        start_date = datetime.today()
    day_num = start_date.weekday()
    day_num_target = weekdays.index(dayname)
    days_ago = (7 + day_num - day_num_target) % 7
    if days_ago == 0:
        days_ago = 7
    target_date = start_date - timedelta(days=days_ago)
    return target_date

datetime.today() # 참고용
# datetime.datetime(2012, 8, 28, 22, 4, 30, 263076)
get_previous_byday('Monday')
# datetime.datetime(2012, 8, 27, 22, 3, 57, 29045)
get_previous_byday('Tuesday') # 저번 주, 오늘 아님
# datetime.datetime(2012, 8, 21, 22, 4, 12, 629771)
get_previous_byday('Friday')
# datetime.datetime(2012, 8, 24, 22, 5, 9, 911393)

get_previous_byday('Sunday', datetime(2012, 12, 21))
# datetime.datetime(2012, 12, 16, 0, 0)

# 토론

from datetime import datetime
from dateutil.relativedelta import relativedelta
from dateutil.rrule import *
d = datetime.now()
print(d)
# 2012-12-23 16:31:52.718111

# 다음 금요일
print(d + relativedelta(weekday=FR))
# 2012-12-28 16:31:52.718111

# 마지막 금요일
print(d + relativedelta(weekday=FR(-1)))
# 2012-12-21 16:31:52.718111


import pygame
from player import Player
from game_screen import gameScreen
from menu_screen import menuScreen
from bridge import Bridge
from button import Button
pygame.init()

# Dimensions de la fenêtre
largeur = 800
hauteur = 600

# Initialisation de la fenêtre
menu_screen = pygame.display.set_mode((largeur, hauteur))
game_screen = pygame.display.set_mode((largeur, hauteur))
pygame.display.set_caption("Satisfying game")
player = Player(game_screen, 400, 500)
bridge = Bridge(game_screen)

def stop_running():
    global running
    running = False
def test():
    global player
    player.loose = False
    pygame.event.pump()

play_button = Button(200,200,100,50,(0,0,255), (0,255,0), "Jouer", test)
quit_button = Button(200,400,100,50,(0,0,255), (0,255,0), "Quitter", stop_running)
# Boucle principale du jeu

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    if player.loose:
        menuScreen(game_screen,event, play_button,quit_button)
    else:
        gameScreen(game_screen, player, event,bridge)

# Fermeture de Pygame
pygame.quit()


import numpy as np
import cv2
import os

threshold = 0.5

prototxt_file = os.path.join('./Resnet_SSD_deploy.prototxt')
caffemodel_file = os.path.join('./Res10_300x300_SSD_iter_140000.caffemodel')
net = cv2.dnn.readNetFromCaffe(prototxt_file, caffeModel=caffemodel_file)

cap = cv2.VideoCapture(0)
for i in range(1):
    ret, frame = cap.read()
    frame = cv2.flip(frame, 1)
    cv2.imwrite('./storage/face.png', frame)
del(cap)

image = cv2.imread('./storage/face.png')
origin_h, origin_w = image.shape[:2]

blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))

net.setInput(blob)
detections = net.forward()
# detection.shape = (1,1,num_bbox,7) with 7 is (x,y,w,h,conf) and 2 output is face or non_face

for i in range(detections.shape[2]):
    confidence = detections[0, 0, i, 2]
    if confidence > threshold:
        bounding_box = detections[0, 0, i, 3:7] * np.array([origin_w, origin_h, origin_w, origin_h])
        x_start, y_start, x_end, y_end = bounding_box.astype('int')

        label = '{0:.2f}%'.format(confidence * 100)
        cv2.rectangle(image, (x_start, y_start), (x_end, y_end), (0, 0, 255), 2)
        cv2.rectangle(image, (x_start, y_start - 18), (x_end, y_start), (0, 0, 255), -1)
        cv2.putText(image, label, (x_start+2, y_start-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

cv2.imshow('output', image)
while True:
    if cv2.waitKey(1) & 0xFF==ord('d'):
        break
cv2.destroyAllWindows()
os.remove('./storage/face.png')


from typing import TypeVar, Any, Sequence

import numpy as np

T = TypeVar("T")

def numpy_arrays_to_lists(
    dictionary: dict[T, Any], array_keys: Sequence[T]
) -> dict[T, Any]:
    """Flatten and convert to float all of the keys inside dictionary which are in `array_keys`
    :param dictionary: the (possibly nested) dictionary which contains the arrays to flatten
    :param array_keys: The sequence of array keys to flatten
    """
    return {
        k: [float(x) for x in np.array(v).flatten()] if k in array_keys else v
        for k, v in dictionary.items()
    }

def flatten_dict(
    dictionary: dict[str, Any],
    key_separator: str = "_",
    recursive: bool = False,
) -> dict[str, Any]:
    """Flatten the dictionaries inside the given dictionary, joining the keys by the given key_separator.
    If recursive, then perform the flattening recursively
    :param dictionary: The dictionary to flatten
    :param key_separator: The string to join the flattened keys with
    :param recursive: if true, go recursively through the dictionary, joining all sub dictionaries together
    """
    new_dict = {}
    for k, v in dictionary.items():
        if isinstance(v, dict):
            new_dict |= {
                f"{k}{key_separator}{kk}": vv
                for kk, vv in (
                    flatten_dict(v, key_separator, recursive)
                    if recursive
                    else v
                ).items()
            }
        else:
            new_dict[k] = v
    return new_dict

def unflatten_dict(
    dictionary: dict[str, Any], key_separator: str = "_"
) -> dict[str, Any]:
    """Unflatten a flattened dict with keys separated by `key_separator`
    :param dictionary: the dictionary to unflatten
    :param key_separator: the separating string to separate keys on
    """
    new_dict = {}
    for k, v in dictionary.items():
        dict_ref = new_dict
        [s, *rest] = k.split(key_separator)
        while len(rest) > 0:
            dict_ref = dict_ref.setdefault(s, {})
            s = rest.pop(0)
        dict_ref[s] = v
    return new_dict

def list_of_dicts_to_dict_of_lists(
    list_of_dicts: list[dict[str, T]]
) -> dict[str, list[T]]:
    """
    Transpose a list of dictionaries (rows of data) into a dictionary of lists (columns of data)
    """
    elements = {}
    for row in list_of_dicts:
        for k, v in row.items():
            elements.setdefault(k, []).append(v)
    return elements

def dict_of_lists_to_list_of_dicts(
    dict_of_lists: dict[str, list[T]]
) -> list[dict[str, T]]:
    """Transpose a dict of lists (columns of data) into a list of dictionaries (rows of data)"""
    assoc_lists = zip(
        *[[(k, v) for v in vs] for k, vs in dict_of_lists.items()]
    )
    return [{a: b for (a, b) in assoc_list} for assoc_list in assoc_lists]


from collections import Counter
import random
import csv
from itertools import chain,combinations

class Analyse:
    all_cards = set((x, y) for x in range(4) for y in range(13))

    def flop_hs(self, card_5):
        rest_cards=self.all_cards-set(card_5)
        behand = tie = ahead = 0
        our_rank = self.rank(card_5)
        for opponent in combinations(rest_cards, 2):
            oppo_rank = self.rank(card_5[2:]+list(opponent))
            if oppo_rank > our_rank:
                behand += 1
            elif oppo_rank < our_rank:
                ahead += 1
            else:
                tie += 1
        HS = round((ahead+tie/2)/(ahead+tie+behand), 3)
        return HS

    def flop_ppot(self,card_5):
        rest_cards=self.all_cards-set(card_5)
        hp=[[0]*3 for _ in range(3)]
        our_rank = self.rank(card_5)
        for _ in range(200):
            oppo_cards = random.sample(rest_cards, 2)
            oppo_rank = self.rank(card_5[2:]+oppo_cards)
            if oppo_rank > our_rank:
                index = 2
            elif oppo_rank < our_rank:
                index = 0
            else:
                index = 1
            leftcards=rest_cards-set(oppo_cards)
            for _ in range(100):
                community_cards_2 = random.sample(leftcards,2)
                ourbest = self.best_rank(card_5+community_cards_2)
                oppobest = self.best_rank(card_5[2:]+oppo_cards+community_cards_2)
                if ourbest > oppobest:
                    hp[index][0]+=1
                elif ourbest < oppobest:
                    hp[index][2]+=1
                else:
                    hp[index][1]+=1
        ppot = (hp[2][0] + hp[2][1]/2 + hp[1][0]/2) / (sum(hp[2]) + sum(hp[1])/2+1)
        return round(ppot, 3)

    def turn_hs(self, card_6):
        rest_cards=self.all_cards-set(card_6)
        behand = tie = ahead = 0
        our_rank = self.best_rank(card_6)
        for opponent in combinations(rest_cards, 2):
            oppo_rank = self.best_rank(card_6[2:]+list(opponent))
            if oppo_rank > our_rank:
                behand += 1
            elif oppo_rank < our_rank:
                ahead += 1
            else:
                tie += 1
        HS = round((ahead+tie/2)/(ahead+tie+behand), 3)
        return HS

    def turn_ppot(self,card_6):
        rest_cards=self.all_cards-set(card_6)
        hp=[[0]*3 for _ in range(3)]
        our_rank = self.best_rank(card_6)
        for opponent in combinations(rest_cards, 2):

            oppo_rank = self.best_rank(card_6[2:]+list(opponent))
            if oppo_rank > our_rank:
                index = 2
            elif oppo_rank < our_rank:
                index = 0
            else:
                index = 1
            leftcards=rest_cards-set(opponent)
            for community_cards_1 in leftcards:

                ourbest = self.best_rank(card_6+[community_cards_1])
                oppobest = self.best_rank(card_6[2:]+list(opponent)+[community_cards_1])
                if ourbest > oppobest:
                    hp[index][0]+=1
                elif ourbest < oppobest:
                    hp[index][2]+=1
                else:
                    hp[index][1]+=1
        ppot = (hp[2][0] + hp[2][1]/2 + hp[1][0]/2) / (sum(hp[2]) + sum(hp[1])/2+1)
        return round(ppot, 3)

    def rank(self, cards5):  # 判断5张牌的牌型
        cards = list(chain(*cards5))
        flower_counter = Counter(cards[::2])  # 花色计数器
        _, max_number_of_flowers = flower_counter.most_common(1)[0]  # 记录卡牌花色最多的数量
        all_num = sorted(cards[1::2], reverse=True)
        number_counter = Counter(all_num)   # 牌值计数器
        most_four = number_counter.most_common(4)   # 牌值出现频率
        if max_number_of_flowers == 5:     # 同花顺
            if all_num[0]-all_num[-1] == 4:
                return [8, all_num[0]]
            elif all_num == [12, 3, 2, 1, 0]:    # 同花顺12345
                return [8, all_num[1]]
            else:
                return [5]+all_num     # 同花
        elif most_four[0][1] == 4:    # 4条
            return [7, most_four[0][0], most_four[1][0]]
        elif most_four[0][1] == 3:
            if most_four[1][1] == 2:   # 葫芦
                return [6, most_four[0][0], most_four[1][0]]
            else:
                return [3, most_four[0][0], most_four[1][0], most_four[2][0]]  # 3条
        elif most_four[0][1] == 2:
            if most_four[1][1] == 2:
                return [2, most_four[0][0], most_four[1][0], most_four[2][0]]    # 2 对
            else:
                return [1, most_four[0][0], most_four[1][0], most_four[2][0], most_four[3][0]]    # 一对
        elif all_num[0]-all_num[-1] == 4:       # 顺子
            return [4, all_num[0]]
        elif all_num == [12, 3, 2, 1, 0]:    # 顺12345
            return [4, all_num[1]]
        else:
            return [0]+all_num   # 高牌

    def best_rank(self, cards):
        return max(map(self.rank, combinations(cards,5)))

if __name__ == '__main__':  # 生成所需类型的数据集
    e = Analyse()
    result = []
    count=0
    while True:
        cards5 = random.sample(e.all_cards, 5)
        val=e.rank(cards5)
        if val[0]==0:  # 0为高牌，1一对，2两对，3三条，收集数据
            count+=1
            print(cards5)
            hs=e.flop_hs(cards5)

            ppot=e.flop_ppot(cards5)

            ehs=round(hs+(1-hs)*ppot,3)

            data=list(chain(*cards5))
            data.append(ehs)
            result.append(data)
        if count==1000:
            break
    with open('flop0.csv', 'a', newline='') as t_file:
        csv_writer = csv.writer(t_file)
        for l in result:
            csv_writer.writerow(l)


import argparse
import glob
import logging
import re
import shutil
from typing import Dict, List, Tuple

import numpy as np
import torch
from torch.nn.utils.rnn import pad_sequence
from torch.utils.data import DataLoader, Dataset, RandomSampler, SequentialSampler
from torch.utils.data.distributed import DistributedSampler
from tqdm import tqdm, trange
from tokenizers.implementations import ByteLevelBPETokenizer
from tokenizers.processors import BertProcessing

from transformers import (
    WEIGHTS_NAME,
    AdamW,
    PreTrainedModel,
    PreTrainedTokenizer,
    RobertaConfig,
    RobertaForMaskedLM,
    RobertaTokenizer,
    get_linear_schedule_with_warmup,
)

import time
import sys
import os
import wandb
import random

try:
    from torch.utils.tensorboard import SummaryWriter
except ImportError:
    from tensorboardX import SummaryWriter

logger = logging.getLogger(__name__)
logging.getLogger('wandb.run_manager').setLevel(logging.WARNING)

MODEL_CLASSES = {
    "roberta": (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer),
}

class LineByLineDatasetWithBPETokenizer(Dataset):
    def __init__(self, file_path: str = None, tokenizer_path: str = None):
        tokenizer = ByteLevelBPETokenizer(
            tokenizer_path + "/vocab.json",
            tokenizer_path + "/merges.txt",
        )
        tokenizer._tokenizer.post_processor = BertProcessing(
            ("</s>", tokenizer.token_to_id("</s>")),
            ("<s>", tokenizer.token_to_id("<s>")),
        )
        tokenizer.enable_truncation(max_length=512)

        self.examples = []

        with open(file_path, encoding="utf-8") as f:
            lines = f.readlines()
            lines = [line for line in lines if (len(line) > 0 and not line.isspace())]
            self.examples += [x.ids for x in tokenizer.encode_batch(lines)]

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

    def __getitem__(self, i):
        # We’ll pad at the batch level.
        return torch.tensor(self.examples[i])

def load_and_cache_examples(args, evaluate=False):
    file_path = args.eval_data_file if evaluate else args.train_data_file
    return LineByLineDatasetWithBPETokenizer(file_path, args.tokenizer_name)

def set_seed(args):
    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    if args.n_gpu > 0:
        torch.cuda.manual_seed_all(args.seed)

def _sorted_checkpoints(args, checkpoint_prefix="checkpoint", use_mtime=False) -> List[str]:
    ordering_and_checkpoint_path = []

    glob_checkpoints = glob.glob(os.path.join(args.output_dir, "{}-*".format(checkpoint_prefix)))

    for path in glob_checkpoints:
        if use_mtime:
            ordering_and_checkpoint_path.append((os.path.getmtime(path), path))
        else:
            regex_match = re.match(".*{}-([0-9]+)".format(checkpoint_prefix), path)
            if regex_match and regex_match.groups():
                ordering_and_checkpoint_path.append((int(regex_match.groups()[0]), path))

    checkpoints_sorted = sorted(ordering_and_checkpoint_path)
    checkpoints_sorted = [checkpoint[1] for checkpoint in checkpoints_sorted]
    return checkpoints_sorted

def _rotate_checkpoints(args, checkpoint_prefix="checkpoint", use_mtime=False) -> None:
    if not args.save_total_limit:
        return
    if args.save_total_limit <= 0:
        return

    # Check if we should delete older checkpoint(s)
    checkpoints_sorted = _sorted_checkpoints(args, checkpoint_prefix, use_mtime)
    if len(checkpoints_sorted) <= args.save_total_limit:
        return

    number_of_checkpoints_to_delete = max(0, len(checkpoints_sorted) - args.save_total_limit)
    checkpoints_to_be_deleted = checkpoints_sorted[:number_of_checkpoints_to_delete]
    for checkpoint in checkpoints_to_be_deleted:
        logger.info("Deleting older checkpoint [{}] due to args.save_total_limit".format(checkpoint))
        shutil.rmtree(checkpoint)

def decision(probability):
    return random.random() < probability

# Reads the masked instances
def read_masked_dataset(tokenizer: PreTrainedTokenizer, batch, labels_to_process) -> Tuple[torch.Tensor, torch.Tensor]:
    # The inputs are already masked in the training file
    tmp_inputs = batch.clone()

    tmp_inputs_list = []
    for input in tmp_inputs:
        decoded_input = tokenizer.decode(input)
        encoded_back = tokenizer.encode(decoded_input)[1:-1] # Removes the additional <s> and </s> added
        tmp_inputs_list.append(encoded_back)

    # Gets the maximum length between inputs and labels_lines
    # We then need to adapt one or the other to have the same length through padding
    max_length_inputs = max([len(input) for input in tmp_inputs_list])
    max_length_labels_lines = max([len(label) for label in labels_to_process])
    max_length = max_length_inputs
    if max_length_labels_lines > max_length_inputs:
        max_length = max_length_labels_lines

    # Create the labels tensor
    labels_to_convert_in_tensor = []

    i = 0
    while i < len(batch):
        l1_tmp = tokenizer.encode(labels_to_process[i])
        label_to_add = []
        for token in l1_tmp:
            if token != tokenizer.bos_token_id and token != tokenizer.eos_token_id:  # Remove special tokens
                label_to_add.append(token)

        j = len(label_to_add)
        while j < max_length:
            label_to_add.append(-100)  # we only compute loss for masked tokens
            j += 1

        labels_to_convert_in_tensor.append(label_to_add)
        i += 1

    labels = torch.as_tensor(labels_to_convert_in_tensor)

    inputs_to_convert = []
    for input in tmp_inputs_list:
        tmp_input = []
        for token in input:
            tmp_input.append(token)

        i = len(tmp_input)
        while i < max_length:
            tmp_input.append(tokenizer.pad_token_id)
            i += 1
        inputs_to_convert.append(tmp_input)

    inputs = torch.as_tensor(inputs_to_convert)

    return inputs, labels

# Reads the masked instances, but provide them as non-masked to the model
# This is used in 10% of cases during training
def get_non_masked_instances(tokenizer: PreTrainedTokenizer, batch, labels_to_process) -> Tuple[torch.Tensor, torch.Tensor]:
    tmp_inputs_list = []
    i = 0
    while i < len(batch):
        decoded_input = tokenizer.decode(batch[i]).replace('<x>',str(labels_to_process[i]).replace('<z>\n',''))
        encoded_back = tokenizer.encode(decoded_input)[1:-1]
        tmp_inputs_list.append(encoded_back)
        i += 1

    # Gets the maximum length
    max_length = max([len(input) for input in tmp_inputs_list])

    inputs_to_convert = []
    labels_to_convert = []
    for input in tmp_inputs_list:
        tmp_input = []
        tmp_label = []
        tmp_label.append(tokenizer.convert_tokens_to_ids('<z>'))
        for token in input:
            tmp_input.append(token)
            tmp_label.append(-100)

        del tmp_label[-1] #Accounts for the fact that tmp_label already contains <z>

        i = len(tmp_input)
        while i < max_length:
            tmp_input.append(tokenizer.pad_token_id)
            tmp_label.append(-100)
            i += 1
        labels_to_convert.append(tmp_label)
        inputs_to_convert.append(tmp_input)

    inputs = torch.as_tensor(inputs_to_convert)
    labels = torch.as_tensor(labels_to_convert)

    # We train the model to understand that if no masked tokens are present, nothing must be produced, only <z>
    return inputs, labels

def train(args, train_dataset, model: PreTrainedModel, tokenizer: PreTrainedTokenizer) -> Tuple[int, float]:
    """ Train the model """
    if args.local_rank in [-1, 0]:
        tb_writer = SummaryWriter()

    args.train_batch_size = args.per_gpu_train_batch_size * max(1, args.n_gpu)

    def collate(examples: List[torch.Tensor]):
        if tokenizer._pad_token is None:
            return pad_sequence(examples, batch_first=True)
        return pad_sequence(examples, batch_first=True, padding_value=tokenizer.pad_token_id)

    train_sampler = SequentialSampler(train_dataset) if args.local_rank == -1 else DistributedSampler(train_dataset)
    train_dataloader = DataLoader(
        train_dataset, sampler=train_sampler, batch_size=args.train_batch_size, collate_fn=collate
    )

    if args.max_steps > 0:
        t_total = args.max_steps
        args.num_train_epochs = args.max_steps // (len(train_dataloader) // args.gradient_accumulation_steps) + 1
    else:
        t_total = len(train_dataloader) // args.gradient_accumulation_steps * args.num_train_epochs

    # Prepare optimizer and schedule (linear warmup and decay)
    no_decay = ["bias", "LayerNorm.weight"]
    optimizer_grouped_parameters = [
        {
            "params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
            "weight_decay": args.weight_decay,
        },
        {"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], "weight_decay": 0.0},
    ]
    optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon)
    scheduler = get_linear_schedule_with_warmup(
        optimizer, num_warmup_steps=args.warmup_steps, num_training_steps=t_total
    )

    # Check if saved optimizer or scheduler states exist
    if (
        args.model_name_or_path
        and os.path.isfile(os.path.join(args.model_name_or_path, "optimizer.pt"))
        and os.path.isfile(os.path.join(args.model_name_or_path, "scheduler.pt"))
    ):
        # Load in optimizer and scheduler states
        optimizer.load_state_dict(torch.load(os.path.join(args.model_name_or_path, "optimizer.pt")))
        scheduler.load_state_dict(torch.load(os.path.join(args.model_name_or_path, "scheduler.pt")))

    if args.fp16:
        try:
            from apex import amp
        except ImportError:
            raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use fp16 training.")
        model, optimizer = amp.initialize(model, optimizer, opt_level=args.fp16_opt_level)

    # multi-gpu training (should be after apex fp16 initialization)
    if args.n_gpu > 1:
        model = torch.nn.DataParallel(model)

    # Distributed training (should be after apex fp16 initialization)
    if args.local_rank != -1:
        model = torch.nn.parallel.DistributedDataParallel(
            model, device_ids=[args.local_rank], output_device=args.local_rank, find_unused_parameters=True
        )

    # Train!
    logger.info("***** Running training *****")
    logger.info("  Num examples = %d", len(train_dataset))
    logger.info("  Num Epochs = %d", args.num_train_epochs)
    logger.info("  Instantaneous batch size per GPU = %d", args.per_gpu_train_batch_size)
    logger.info(
        "  Total train batch size (w. parallel, distributed & accumulation) = %d",
        args.train_batch_size
        * args.gradient_accumulation_steps
        * (torch.distributed.get_world_size() if args.local_rank != -1 else 1),
    )
    logger.info("  Gradient Accumulation steps = %d", args.gradient_accumulation_steps)
    logger.info("  Total optimization steps = %d", t_total)

    global_step = 0
    epochs_trained = 0
    steps_trained_in_current_epoch = 0
    # Check if continuing training from a checkpoint
    if args.model_name_or_path and os.path.exists(args.model_name_or_path):
        try:
            # set global_step to gobal_step of last saved checkpoint from model path
            checkpoint_suffix = args.model_name_or_path.split("-")[-1].split("/")[0]
            global_step = int(checkpoint_suffix)
            epochs_trained = global_step // (len(train_dataloader) // args.gradient_accumulation_steps)
            steps_trained_in_current_epoch = global_step % (len(train_dataloader) // args.gradient_accumulation_steps)

            logger.info("  Continuing training from checkpoint, will skip to saved global_step")
            logger.info("  Continuing training from epoch %d", epochs_trained)
            logger.info("  Continuing training from global step %d", global_step)
            logger.info("  Will skip the first %d steps in the first epoch", steps_trained_in_current_epoch)
        except ValueError:
            logger.info("  Starting fine-tuning.")

    tr_loss, logging_loss = 0.0, 0.0

    model_to_resize = model.module if hasattr(model, "module") else model  # Take care of distributed/parallel training
    model_to_resize.resize_token_embeddings(len(tokenizer))

    model.zero_grad()
    train_iterator = trange(
        epochs_trained, int(args.num_train_epochs), desc="Epoch", disable=args.local_rank not in [-1, 0]
    )
    set_seed(args)  # Added here for reproducibility

    labels_file = str(args.train_data_file).replace('masked_code_','mask_')
    labels_lines = [line.rstrip() for line in open(labels_file)]

    for _ in train_iterator:
        epoch_iterator = tqdm(train_dataloader, desc="Iteration", disable=args.local_rank not in [-1, 0])
        # step is the count of the steps performed, batch contains the actual input data

        for step, batch in enumerate(epoch_iterator):
            # Skip past any already trained steps if resuming training
            if steps_trained_in_current_epoch > 0:
                steps_trained_in_current_epoch -= 1
                continue

            # Get the labels lines to process
            start = step * len(batch)
            end = start + len(batch) + 1
            lables_to_process = labels_lines[start:end]

            # In 90% of cases, we used the inputs with the masked tokens
            # In 10% of cases we don't mask any token
            if decision(0.9):
                inputs, labels = read_masked_dataset(tokenizer, batch, lables_to_process)
            else:
                inputs, labels = get_non_masked_instances(tokenizer, batch, lables_to_process)

            inputs = inputs.to(args.device)
            labels = labels.to(args.device)
            model.train()
            outputs = model(inputs, masked_lm_labels=labels) if args.mlm else model(inputs, labels=labels)
            loss = outputs[0]  # model outputs are always tuple in transformers (see doc)

            if args.n_gpu > 1:
                loss = loss.mean()  # mean() to average on multi-gpu parallel training
            if args.gradient_accumulation_steps > 1:
                loss = loss / args.gradient_accumulation_steps

            if args.fp16:
                with amp.scale_loss(loss, optimizer) as scaled_loss:
                    scaled_loss.backward()
            else:
                loss.backward()

            tr_loss += loss.item()
            if (step + 1) % args.gradient_accumulation_steps == 0:
                if args.fp16:
                    torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), args.max_grad_norm)
                else:
                    torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
                optimizer.step()
                scheduler.step()  # Update learning rate schedule
                model.zero_grad()
                global_step += 1
                wandb.log({'train_batch_loss': loss.item()})
                wandb.log({'avg_train_loss': tr_loss / len(train_dataloader)})

                if args.local_rank in [-1, 0] and args.logging_steps > 0 and global_step % args.logging_steps == 0:
                    # Log metrics
                    if (
                        args.local_rank == -1 and args.evaluate_during_training
                    ):  # Only evaluate when single GPU otherwise metrics may not average well
                        results = evaluate(args, model, tokenizer)
                        if results is None:
                            print("Stopping condition reached, no improvement in evaluation set")
                            sys.exit(0)
                        for key, value in results.items():
                            tb_writer.add_scalar("eval_{}".format(key), value, global_step)
                    tb_writer.add_scalar("lr", scheduler.get_lr()[0], global_step)
                    tb_writer.add_scalar("loss", (tr_loss - logging_loss) / args.logging_steps, global_step)
                    logging_loss = tr_loss

                if args.local_rank in [-1, 0] and args.save_steps > 0 and global_step % args.save_steps == 0:
                    checkpoint_prefix = "checkpoint"
                    # Save model checkpoint
                    output_dir = os.path.join(args.output_dir, "{}-{}".format(checkpoint_prefix, global_step))
                    os.makedirs(output_dir, exist_ok=True)
                    model_to_save = (
                        model.module if hasattr(model, "module") else model
                    )  # Take care of distributed/parallel training
                    model_to_save.save_pretrained(output_dir)
                    tokenizer.save_pretrained(output_dir)

                    torch.save(args, os.path.join(output_dir, "training_args.bin"))
                    logger.info("Saving model checkpoint to %s", output_dir)

                    _rotate_checkpoints(args, checkpoint_prefix)

                    torch.save(optimizer.state_dict(), os.path.join(output_dir, "optimizer.pt"))
                    torch.save(scheduler.state_dict(), os.path.join(output_dir, "scheduler.pt"))
                    logger.info("Saving optimizer and scheduler states to %s", output_dir)

            if args.max_steps > 0 and global_step > args.max_steps:
                epoch_iterator.close()
                break
        if args.max_steps > 0 and global_step > args.max_steps:
            train_iterator.close()
            break

    if args.local_rank in [-1, 0]:
        tb_writer.close()

    return global_step, tr_loss / global_step

def evaluate(args, model: PreTrainedModel, tokenizer: PreTrainedTokenizer, prefix="") -> Dict:
    # Loop to handle MNLI double evaluation (matched, mis-matched)
    eval_output_dir = args.output_dir

    eval_dataset = load_and_cache_examples(args, evaluate=True)

    if args.local_rank in [-1, 0]:
        os.makedirs(eval_output_dir, exist_ok=True)

    args.eval_batch_size = args.per_gpu_eval_batch_size * max(1, args.n_gpu)
    # Note that DistributedSampler samples randomly

    def collate(examples: List[torch.Tensor]):
        if tokenizer._pad_token is None:
            return pad_sequence(examples, batch_first=True)
        return pad_sequence(examples, batch_first=True, padding_value=tokenizer.pad_token_id)

    eval_sampler = SequentialSampler(eval_dataset)
    eval_dataloader = DataLoader(
        eval_dataset, sampler=eval_sampler, batch_size=args.eval_batch_size, collate_fn=collate
    )

    # multi-gpu evaluate
    if args.n_gpu > 1:
        model = torch.nn.DataParallel(model)

    # Eval!
    logger.info("***** Running evaluation {} *****".format(prefix))
    logger.info("  Num examples = %d", len(eval_dataset))
    logger.info("  Batch size = %d", args.eval_batch_size)
    eval_loss = 0.0
    nb_eval_steps = 0
    model.eval()

    labels_file = str(args.eval_data_file).replace('masked_code_', 'mask_')
    labels_lines = [line.rstrip() for line in open(labels_file)]

    step = 0
    for batch in tqdm(eval_dataloader, desc="Evaluating"):
        # Get the labels lines to process
        start = step * len(batch)
        end = start + len(batch) + 1
        lables_to_process = labels_lines[start:end]

        step += 1

        inputs, labels = read_masked_dataset(tokenizer, batch, lables_to_process)
        inputs = inputs.to(args.device)
        labels = labels.to(args.device)

        with torch.no_grad():
            outputs = model(inputs, masked_lm_labels=labels) if args.mlm else model(inputs, labels=labels)
            lm_loss = outputs[0]
            eval_loss += lm_loss.mean().item()
        nb_eval_steps += 1

    eval_loss = eval_loss / nb_eval_steps
    perplexity = torch.exp(torch.tensor(eval_loss))
    perfect_predictions, num_examples = get_number_perfect_predictions(model, tokenizer, args.eval_data_file)
    result = {"perplexity": perplexity, "loss": eval_loss,
              "perfect_predictions": perfect_predictions, "total_eval_examples": num_examples}

    wandb.log({'val_perplexity': perplexity, 'avg_val_loss': eval_loss})
    wandb.log({'perfect_predictions': perfect_predictions})
    wandb.log({'perfect_predictions_percentage': perfect_predictions / num_examples})

    output_eval_file = os.path.join(eval_output_dir, prefix, "eval_results_" + str(time.time()) + ".txt")
    with open(output_eval_file, "w") as writer:
        logger.info("***** Eval results {} *****".format(prefix))
        for key in sorted(result.keys()):
            logger.info("  %s = %s", key, str(result[key]))
            writer.write("%s = %s\n" % (key, str(result[key])))

    if args.early_stop > 0:
        # Early stop has been required by the user, check performance
        eval_results_files = glob.glob(os.path.join(eval_output_dir, prefix,'eval_results_*.txt'))
        eval_results_files.sort(key=lambda x: os.stat(os.path.join(eval_output_dir, x)).st_mtime)
        if len(eval_results_files) > args.early_stop:
            perfect_predictions_before = read_perfect_predictions_from_file(eval_results_files[len(eval_results_files)-(args.early_stop+1)])
            if perfect_predictions <= perfect_predictions_before:
                return None

    return result

def read_perfect_predictions_from_file(file_path):
    with open(file_path) as f:
        content = f.read()
    p = re.compile('perfect_predictions = (.*?)\n')
    return int(p.search(content).group(1))

def get_number_perfect_predictions(model: PreTrainedModel, tokenizer: PreTrainedTokenizer, eval_data_file):
    labels_file = str(eval_data_file).replace('masked_code_', 'mask_')
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model.to(device)

    # Inputs
    with open(eval_data_file) as f:
        inputs = f.readlines()
    inputs = [x.strip() for x in inputs]

    # Targets
    with open(labels_file) as f:
        targets = f.readlines()
    targets = [x.strip() for x in targets]

    n_perfect_predictions = 0
    i = 0
    while i < len(inputs):
        input = inputs[i]
        target = "".join(targets[i].split()).replace('<z>', '')

        indexed_tokens = tokenizer.encode(input)
        tokens_tensor = torch.tensor([indexed_tokens])
        tokens_tensor = tokens_tensor.to(device)
        with torch.no_grad():
            outputs = model(tokens_tensor)
            predictions = outputs[0]

        predicted_sentence = []
        for token in torch.argmax(predictions[0], 1).cpu().numpy():
            if token != tokenizer.convert_tokens_to_ids('<z>'):
                predicted_sentence.append(token)
            else:
                break

        prediction = tokenizer.decode(predicted_sentence)
        prediction = "".join(prediction.split())
        if target == prediction:
            n_perfect_predictions += 1
        i += 1

    return n_perfect_predictions, len(inputs)

class Args:

    def __init__(self, run, train_data_file, eval_data_file, output_root, tokenizer_name, vocab_size,
                 early_stop, logging_steps, should_continue
    ):
        self.train_data_file = train_data_file
        self.eval_data_file = eval_data_file
        self.output_root = output_root
        self.model_type = 'roberta'
        self.early_stop = early_stop
        self.line_by_line = True
        self.should_continue = should_continue
        self.model_name_or_path = ''
        self.mlm = True
        self.mlm_probability = 0.2
        self.tokenizer_name = tokenizer_name
        self.vocab_size = vocab_size
        self.cache_dir = 'cache'
        self.block_size = -1
        self.do_train = True
        self.do_eval = True
        self.evaluate_during_training = True
        self.per_gpu_train_batch_size = wandb.config.batch_size
        self.per_gpu_eval_batch_size = wandb.config.batch_size
        self.gradient_accumulation_steps = wandb.config.gradient_accumulation_steps
        self.learning_rate = wandb.config.learning_rate
        self.weight_decay = 0.0
        self.adam_epsilon = 1e-8
        self.max_grad_norm = 1.0
        self.num_train_epochs = wandb.config.epochs
        self.max_steps = -1
        self.warmup_steps = 0
        self.logging_steps = logging_steps
        self.save_steps = logging_steps
        self.save_total_limit = None
        self.eval_all_checkpoints = False
        self.no_cuda = False
        self.overwrite_output_dir = True
        self.seed = 42
        self.fp16 = False
        self.fp16_opt_level = "O1"
        self.local_rank = -1
        self.server_ip = ""
        self.server_port = ""
        run.save()
        self.name = run.name
        if self.should_continue:
            self.output_dir = self.output_root
        else:
            self.output_dir = self.output_root  + '/' + self.name

def get_config(args):
    config = {
        "model_type": "roberta",
        "attention_probs_dropout_prob": 0.1,
        "hidden_act": "gelu",
        "hidden_dropout_prob": 0.3,
        "hidden_size": wandb.config.hidden_size,
        "initializer_range": 0.02,
        "num_attention_heads": wandb.config.num_attention_heads,
        "num_hidden_layers": wandb.config.num_hidden_layers,
        "vocab_size": args.vocab_size,
        "intermediate_size": wandb.config.intermediate_size,
        "max_position_embeddings": 1024,
        "cache_dir": args.cache_dir
    }

    return RobertaConfig(**config)

def add_special_tokens_(model, tokenizer):
    """ Add special tokens to the tokenizer and the model if they have not already been added. """
    orig_num_tokens = len(tokenizer.encoder)
    num_added_tokens = tokenizer.add_special_tokens({'additional_special_tokens': ['<z>', '<x>']}) # doesn't add if they are already there
    if num_added_tokens > 0:
        model.resize_token_embeddings(new_num_tokens=orig_num_tokens + num_added_tokens)

def main():
    run = wandb.init(project="roberta")

    parser = argparse.ArgumentParser()

    # Required parameters
    parser.add_argument(
        "--early_stop",
        default=0,
        type=int,
        help="The number of runs on the evaluation set without any improvement before the training is stopped. Put 0 to not use early stop.",
    )

    parser.add_argument("--logging_steps", type=int, default=500, help="Log every X updates steps.")

    parser.add_argument(
        "--train_data_file", default=None, type=str, required=True, help="The input training data file (a text file)."
    )
    parser.add_argument(
        "--eval_data_file", default=None, type=str, required=True, help="The input training data file (a text file)."
    )

    parser.add_argument(
        "--should_continue", action="store_true", help="Whether to continue from latest checkpoint in output_dir"
    )

    parser.add_argument(
        "--output_root",
        type=str,
        required=True,
        help="The output directory where the model predictions and checkpoints will be written.",
    )
    parser.add_argument(
        "--tokenizer_name", default=None, type=str, required=True, help="The input training data file (a text file)."
    )
    parser.add_argument(
        "--vocab_size", default=None, type=int, required=True, help="The input training data file (a text file)."
    )

    parsed_args = parser.parse_args()
    args = Args(
        run, parsed_args.train_data_file, parsed_args.eval_data_file,
        parsed_args.output_root, parsed_args.tokenizer_name, parsed_args.vocab_size,
        parsed_args.early_stop, parsed_args.logging_steps, parsed_args.should_continue
    )

    if args.model_type in ["bert", "roberta", "distilbert", "camembert"] and not args.mlm:
        raise ValueError(
            "BERT and RoBERTa-like models do not have LM heads but masked LM heads. They must be run using the --mlm "
            "flag (masked language modeling)."
        )
    if args.eval_data_file is None and args.do_eval:
        raise ValueError(
            "Cannot do evaluation without an evaluation data file. Either supply a file to --eval_data_file "
            "or remove the --do_eval argument."
        )
    if args.should_continue:
        sorted_checkpoints = _sorted_checkpoints(args)
        if len(sorted_checkpoints) == 0:
            raise ValueError("Used --should_continue but no checkpoint was found in --output_dir.")
        else:
            args.model_name_or_path = sorted_checkpoints[-1]

    if (
            os.path.exists(args.output_dir)
            and os.listdir(args.output_dir)
            and args.do_train
            and not args.overwrite_output_dir
    ):
        raise ValueError(
            "Output directory ({}) already exists and is not empty. Use --overwrite_output_dir to overcome.".format(
                args.output_dir
            )
        )

    # Setup CUDA, GPU & distributed training
    if args.local_rank == -1 or args.no_cuda:
        device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
        args.n_gpu = torch.cuda.device_count()
    else:  # Initializes the distributed backend which will take care of sychronizing nodes/GPUs
        torch.cuda.set_device(args.local_rank)
        device = torch.device("cuda", args.local_rank)
        torch.distributed.init_process_group(backend="nccl")
        args.n_gpu = 1
    args.n_gpu = 1  # 1
    args.device = device

    # Setup logging
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s -   %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN,
    )
    logger.warning(
        "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s",
        args.local_rank,
        device,
        args.n_gpu,
        bool(args.local_rank != -1),
        args.fp16,
    )

    # Set seed
    set_seed(args)

    # Load pretrained model and tokenizer
    if args.local_rank not in [-1, 0]:
        torch.distributed.barrier()  # Barrier to make sure only the first process in distributed training download model & vocab

    config_class, model_class, tokenizer_class = MODEL_CLASSES[args.model_type]
    config = get_config(args)

    if args.tokenizer_name:
        tokenizer = tokenizer_class.from_pretrained(args.tokenizer_name, cache_dir=args.cache_dir)
    elif args.model_name_or_path:
        tokenizer = tokenizer_class.from_pretrained(args.model_name_or_path, cache_dir=args.cache_dir)
    else:
        raise ValueError(
            "You are instantiating a new {} tokenizer. This is not supported, but you can do it from another script, save it,"
            "and load it from here, using --tokenizer_name".format(tokenizer_class.__name__)
        )

    if args.block_size <= 0:
        args.block_size = tokenizer.max_len
        # Our input block size will be the max possible for the model
    else:
        args.block_size = min(args.block_size, tokenizer.max_len)

    if args.model_name_or_path:
        model = model_class.from_pretrained(
            args.model_name_or_path,
            from_tf=bool(".ckpt" in args.model_name_or_path),
            config=config,
            cache_dir=args.cache_dir,
        )
    else:
        logger.info("Training new model from scratch")
        try:
            model = model_class(config=config)
        except Exception as e:
            logger.error(f'{e} Configuration not correct for {args.name}')
            return

    add_special_tokens_(model, tokenizer)
    model.to(args.device)

    if args.local_rank == 0:
        torch.distributed.barrier()  # End of barrier to make sure only the first process in distributed training download model & vocab

    logger.info("Training/evaluation parameters %s", args)

    # Training
    if args.do_train:
        if args.local_rank not in [-1, 0]:
            torch.distributed.barrier()  # Barrier to make sure only the first process in distributed training process the dataset, and the others will use the cache

        train_dataset = load_and_cache_examples(args, evaluate=False)

        if args.local_rank == 0:
            torch.distributed.barrier()

        global_step, tr_loss = train(args, train_dataset, model, tokenizer)
        logger.info(" global_step = %s, average loss = %s", global_step, tr_loss)

    # Saving best-practices: if you use save_pretrained for the model and tokenizer, you can reload them using from_pretrained()
    if args.do_train and (args.local_rank == -1 or torch.distributed.get_rank() == 0):
        # Create output directory if needed
        if args.local_rank in [-1, 0]:
            os.makedirs(args.output_dir, exist_ok=True)

        logger.info("Saving model checkpoint to %s", args.output_dir)
        # Save a trained model, configuration and tokenizer using `save_pretrained()`.
        # They can then be reloaded using `from_pretrained()`
        model_to_save = (
            model.module if hasattr(model, "module") else model
        )  # Take care of distributed/parallel training
        model_to_save.save_pretrained(args.output_dir)
        tokenizer.save_pretrained(args.output_dir)

        # Good practice: save your training arguments together with the trained model
        torch.save(args, os.path.join(args.output_dir, "training_args.bin"))

        # Load a trained model and vocabulary that you have fine-tuned
        model = model_class.from_pretrained(args.output_dir)
        tokenizer = tokenizer_class.from_pretrained(args.output_dir)
        model.to(args.device)

    # Evaluation
    results = {}
    if args.do_eval and args.local_rank in [-1, 0]:
        checkpoints = [args.output_dir]
        if args.eval_all_checkpoints:
            checkpoints = list(
                os.path.dirname(c) for c in sorted(glob.glob(args.output_dir + "/**/" + WEIGHTS_NAME, recursive=True))
            )
            logging.getLogger("transformers.modeling_utils").setLevel(logging.WARN)  # Reduce logging
        logger.info("Evaluate the following checkpoints: %s", checkpoints)
        for checkpoint in checkpoints:
            global_step = checkpoint.split("-")[-1] if len(checkpoints) > 1 else ""
            prefix = checkpoint.split("/")[-1] if checkpoint.find("checkpoint") != -1 else ""

            model = model_class.from_pretrained(checkpoint)
            model.to(args.device)
            result = evaluate(args, model, tokenizer, prefix=prefix)
            if result is None:
                print("Stopping condition reached, no improvement in evaluation set")
                sys.exit(0)
            result = dict((k + "_{}".format(global_step), v) for k, v in result.items())
            results.update(result)

    return results

if __name__ == "__main__":
    sweep_config = {
        'method': 'grid',
        'metric': {
            'name': 'perfect_predictions_percentage',
            'goal': 'maximize'
        },
        'parameters': {
            'vocab_size': {
                'values': [1_130]
            },
            'learning_rate': {
                'values': [5e-5]
            },
            'gradient_accumulation_steps': {
                'values': [4]  # equivalent to having effective batch size of 64 with a real batch_size of 16
            },
            'batch_size': {
                'values': [8]
            },
            'epochs': {
                'values': [50]
            },
            'hidden_size': {
                'values': [768]
            },
            'num_attention_heads': {
                'values': [16]
            },
            'num_hidden_layers': {
                'values': [12]
            },
            'intermediate_size': {
                'values': [4_096]
            }
        }
    }

    sweep_id = wandb.sweep(sweep_config)
    wandb.agent(sweep_id, function=main)


import pygame

def game_loop():
    import Mainshmp

pygame.init()

display_width = 1000
display_height = 600

HW,HH = display_width / 2, display_height / 2
AREA = display_width * display_height

white = (255,255,255)
black = (0,0,0,)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

player_width = 50
player_heigth = 50

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("MyPy")
icon = pygame.image.load("icon.png")
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
pygame.font.init()

joysticks = []
for i in range(0, pygame.joystick.get_count()):
    joysticks.append(pygame.joystick.Joystick(i))
    joysticks[-1].init()

pygame.mixer.music.load("random silly chip song.wav")
pygame.mixer.music.play(-1)

class Background(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)  #call Sprite initializer
        self.image = pygame.image.load("back.png")
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
bg = Background('background_image.png', [0,0])
kenny = pygame.image.load("kenny.png")

button1 = pygame.image.load("button1.png")
button2 = pygame.image.load("button2.png")
quit1 = pygame.image.load("quit1.png")
quit2 = pygame.image.load("quit2.png")

def text_objects(text, font):
    textSurface = font.render(text, True, red)
    return textSurface, textSurface.get_rect()

def button(x,y,w,h, b1, b2, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    gameDisplay.blit(b2,(x,y))
    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        if click[0] == 1 and action != None:
            if action == "play":
                game_loop()
            elif action == "quit":
                pygame.quit()

        gameDisplay.blit(b1,(x,y))

def game_intro():
    intro = True

    while intro:

        for event in pygame.event.get():
            print(event)

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type ==pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    quit()

            if event.type ==pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    game_loop()

            if event.type == pygame.JOYBUTTONDOWN:
                if event.button == 9:
                    game_loop()

            if event.type == pygame.JOYBUTTONDOWN:
                if event.button == 8:
                    pygame.quit()
                    quit()

            if event.type == pygame.JOYBUTTONDOWN:
                if event.button == 6:
                    pygame.quit()
                    quit()

            if event.type == pygame.JOYBUTTONDOWN:
                if event.button == 7:
                    game_loop()

        gameDisplay.blit(bg.image, bg.rect)

        gameDisplay.blit(kenny,(425, 300))

        largeText = pygame.font.Font('SnackerComic.ttf',115)
        TextSurf, TextRect = text_objects("MyPy", largeText)
        TextRect.center = ((display_width/2),(display_height/3))
        gameDisplay.blit(TextSurf, TextRect)

        button(200,500,100,39,button1,button2, "play")
        button(700,500,100,39,quit1,quit2, "quit")

        pygame.display.update()
        clock.tick(15)

game_intro()
pygame.quit()
quit


import slither_constants_file as scf
import xlsxwriter as xls

# Gets the name of the contract being analyzed
def get_contract_name_from_line(charList: list()):
    index = charList.index(':')
    index = index + 2
    temp = charList[index:]
    return ''.join(temp)

# Instances
vulnerabilities_object = scf.Vulnerabiltiies()
severities_object = scf.Severities()

# List with the name of which vulnerability from Slither reference
vulnerabilities = [
    vulnerabilities_object.ABI_ENCODER, vulnerabilities_object.ARBITRARY, vulnerabilities_object.ARRAY_BY_REFERENCE, vulnerabilities_object.ABI_ENCODED_COLLISION,
    vulnerabilities_object.INCORRECT_SHIFT_IN_ASSEMBLY, vulnerabilities_object.MISSING_RETURN_STATEMENTS, vulnerabilities_object.MULTIPLE_CONSTRUCTOR_SCHEMES,
    vulnerabilities_object.PROTECTED_VARIABLES, vulnerabilities_object.PUBLIC_MAPPING_WITH_NESTED_VARIABLES, vulnerabilities_object.RIGHT_TO_LEFT_OVERRIDE_CHAR,
    vulnerabilities_object.STATE_VARIABLE_SHADOWING, vulnerabilities_object.SUICIDAL, vulnerabilities_object.UNINTIALIZED_STATE_VARIABLES, vulnerabilities_object.UNINTIALIZED_STORAGE_VARIABLES,
    vulnerabilities_object.UNPROTECTED_UPGRADABLE_CONTRACT, vulnerabilities_object.ARBITRARY_FROM_IN_TRANSFERFROM, vulnerabilities_object.SEND_ETHER_TO_ARBITRARY,
    vulnerabilities_object.ARRAY_LEGNTH_ASSIGNMENT, vulnerabilities_object.CONTROLLED_DELEGATECALL, vulnerabilities_object.PAYABLE_USING_DELEGATECALL_LOOP,
    vulnerabilities_object.INCORRECT_FUNCTION_VISIBILITY, vulnerabilities_object.INCORRECT_FUNCTION_EXPONENTATION, vulnerabilities_object.INCORRECT_RETURN_IN_ASSEMBLY,
    vulnerabilities_object.INCORRECT_MSGSENDER_CHECK, vulnerabilities_object.MISSING_CUNSTRUCTOR, vulnerabilities_object.MODIFIER_LOOK_LIKE_SOL_KEYWORK,
    vulnerabilities_object.MSGVALUE_INSIDE_LOOP, vulnerabilities_object.OVERFLOW_ERC20_BALANCE, vulnerabilities_object.UNPROTECTED_ECRECOVER_LEADING_RACE_CONDITION,
    vulnerabilities_object.RACE_CONDITION_AT_CONTRACT_INIT, vulnerabilities_object.REENTRANCY, vulnerabilities_object.STORAGE_SIGNED_INTEGER_ARRAY, vulnerabilities_object.UNCHECKED_TRANSFER,
    vulnerabilities_object.UNPROTECTED_FUNCTIONS, vulnerabilities_object.WEAK_PRNG, vulnerabilities_object.DANGEROUS_ENUM_CONVERSION, vulnerabilities_object.INCORRECT_ERC20_CONVERSION,
    vulnerabilities_object.INCORRECT_ERC721_INTERFACE, vulnerabilities_object.DANGEROUS_STRICT_EQUALITIES, vulnerabilities_object.INCORRECT_ISCONTRACT_MODIFIER,
    vulnerabilities_object.CONTRACTS_THAT_LOCKS_ETHER, vulnerabilities_object.DELETION_ON_MAPPING_CONTAINING_STRUCT,
    vulnerabilities_object.STATE_VARIABLE_SHADOWING_FROM_ABSTRACT_CONTRACT, vulnerabilities_object.TAUTOLOGICAL_COMPARE, vulnerabilities_object.TAUTOLOGY_OR_CONTRADICTION,
    vulnerabilities_object.WRITE_AFTER_WRITE, vulnerabilities_object.INCORRECT_BALANCE_VALUE, vulnerabilities_object.MISUSE_OF_BOOLEAN_CONSTANT,
    vulnerabilities_object.CONSTANT_FUNCTIONS_USING_ASSEMBLY_CODE, vulnerabilities_object.CONSTANT_FUNCTIONS_CHANGING_THE_STATE, vulnerabilities_object.CONTROLLED_LOOP_ITERATION,
    vulnerabilities_object.CONTROLLED_LOWLEVELCALL, vulnerabilities_object.DIVIDE_BEFORE_MULTIPLY, vulnerabilities_object.EMPTY_FUNCTIONS, vulnerabilities_object.LACK_OF_MSGSENDER_USAGE,
    vulnerabilities_object.MSGVALUE_USAGE_ON_NONPAYABLE_FUNCTIONS, vulnerabilities_object.OVERFLOW_ERC20_ALLOWANCE, vulnerabilities_object.ABSENCE_OF_PAUSABLE_MODIFIER,
    vulnerabilities_object.REENTRANCY_VULNERABILITIES_1, vulnerabilities_object.REUSED_BASE_CONSTRUCTORS, vulnerabilities_object.DANGEROUS_USAGE_OF_TXGASPRICE,
    vulnerabilities_object.DANGEROUS_USAGE_OF_TXORIGIN, vulnerabilities_object.UNCHECKED_BLOCKHASH, vulnerabilities_object.UNCHECKED_LOW_LEVEL_CALLS, vulnerabilities_object.UNCHECKED_SEND,
    vulnerabilities_object.UNINITIALIZED_LOCAL_VARIABLES, vulnerabilities_object.UNINITIALIZED_RETURN_STATEMENTS, vulnerabilities_object.UNSPECIFIED_OPERATION_ORDER, vulnerabilities_object.UNUSED_EVENTS,
    vulnerabilities_object.UNUSED_RETURN, vulnerabilities_object.UNUSED_RETURN_INTERNAL, vulnerabilities_object.DETECT_USE_AFTER_DELETE, vulnerabilities_object.INCORRECT_MODIFIER,
    vulnerabilities_object.BUILTIN_SYMBOL_SHADOWING, vulnerabilities_object.LOCAL_VARIABLES_SHADOWING, vulnerabilities_object.UNINITIALIZED_POINTERS_IN_CONSTRUCTORS, vulnerabilities_object.PREDECLARATION_USAGE_OF_LOCAL_VARIABLES,
    vulnerabilities_object.CALLS_INSIDE_LOOP, vulnerabilities_object.MISSING_EVENTS_ACCESS_CONTROL, vulnerabilities_object.MISSING_EVENTS_ARITHMETIC, vulnerabilities_object.DANGEROUS_UNARY_EXPRESSIONS,
    vulnerabilities_object.MISSING_ZERO_ADDRESS_VALIDATION, vulnerabilities_object.REENTRANCY_VULNERABILITIES_2, vulnerabilities_object.BLOCK_TIMESTAMP, vulnerabilities_object.ASSEMBLY_USAGE, vulnerabilities_object.ASSERT_STATE_CHANGE,
    vulnerabilities_object.BOOLEAN_EQUALITY, vulnerabilities_object.UNINDEXED_ERC20_EVENT_PARAMETERS, vulnerabilities_object.LOW_LEVEL_CALLS, vulnerabilities_object.MISSING_INHERITANCE,
    vulnerabilities_object.CONFORMANCE_TO_NAME_CONVENTIONS, vulnerabilities_object.DIFFERENT_PRAGMA_DIRECTIVES_USED, vulnerabilities_object.REDUNDANT_STATEMENTS, vulnerabilities_object.INCORRECT_VERSIONS_OF_SOLIDITY,
    vulnerabilities_object.UNINPLEMENTED_FUNCTIONS, vulnerabilities_object.UNUSED_STATE_VARIABLE, vulnerabilities_object.COSTLY_OPERATION_INSIDE_LOOP, vulnerabilities_object.DEAD_CODE,
    vulnerabilities_object.REENTRANCY_VULNERABILITIES_3, vulnerabilities_object.REENTRANCY_VULNERABILITIES_4, vulnerabilities_object.VARIABLE_NAMES_TO_SIMILAR, vulnerabilities_object.TOO_MANY_DIGITS,
    vulnerabilities_object.STATE_VARIABLES_THAT_COULD_BE_CONSTANT, vulnerabilities_object.PUBLIC_FUNCTION_THAT_COULD_BE_EXTERNAL, vulnerabilities_object.BLOCKHASH_CURRENT]

severities = [
    severities_object.ABI_ENCODER, severities_object.ARBITRARY, severities_object.ARRAY_BY_REFERENCE, severities_object.ABI_ENCODED_COLLISION,
    severities_object.INCORRECT_SHIFT_IN_ASSEMBLY, severities_object.MISSING_RETURN_STATEMENTS, severities_object.MULTIPLE_CONSTRUCTOR_SCHEMES,
    severities_object.PROTECTED_VARIABLES, severities_object.PUBLIC_MAPPING_WITH_NESTED_VARIABLES, severities_object.RIGHT_TO_LEFT_OVERRIDE_CHAR,
    severities_object.STATE_VARIABLE_SHADOWING, severities_object.SUICIDAL, severities_object.UNINTIALIZED_STATE_VARIABLES, severities_object.UNINTIALIZED_STORAGE_VARIABLES,
    severities_object.UNPROTECTED_UPGRADABLE_CONTRACT, severities_object.ARBITRARY_FROM_IN_TRANSFERFROM, severities_object.SEND_ETHER_TO_ARBITRARY,
    severities_object.ARRAY_LEGNTH_ASSIGNMENT, severities_object.CONTROLLED_DELEGATECALL, severities_object.PAYABLE_USING_DELEGATECALL_LOOP,
    severities_object.INCORRECT_FUNCTION_VISIBILITY, severities_object.INCORRECT_FUNCTION_EXPONENTATION, severities_object.INCORRECT_RETURN_IN_ASSEMBLY,
    severities_object.INCORRECT_MSGSENDER_CHECK, severities_object.MISSING_CUNSTRUCTOR, severities_object.MODIFIER_LOOK_LIKE_SOL_KEYWORK,
    severities_object.MSGVALUE_INSIDE_LOOP, severities_object.OVERFLOW_ERC20_BALANCE, severities_object.UNPROTECTED_ECRECOVER_LEADING_RACE_CONDITION,
    severities_object.RACE_CONDITION_AT_CONTRACT_INIT, severities_object.REENTRANCY, severities_object.STORAGE_SIGNED_INTEGER_ARRAY, severities_object.UNCHECKED_TRANSFER,
    severities_object.UNPROTECTED_FUNCTIONS, severities_object.WEAK_PRNG, severities_object.DANGEROUS_ENUM_CONVERSION, severities_object.INCORRECT_ERC20_CONVERSION,
    severities_object.INCORRECT_ERC721_INTERFACE, severities_object.DANGEROUS_STRICT_EQUALITIES, severities_object.INCORRECT_ISCONTRACT_MODIFIER,
    severities_object.CONTRACTS_THAT_LOCKS_ETHER, severities_object.DELETION_ON_MAPPING_CONTAINING_STRUCT,
    severities_object.STATE_VARIABLE_SHADOWING_FROM_ABSTRACT_CONTRACT, severities_object.TAUTOLOGICAL_COMPARE, severities_object.TAUTOLOGY_OR_CONTRADICTION,
    severities_object.WRITE_AFTER_WRITE, severities_object.INCORRECT_BALANCE_VALUE, severities_object.MISUSE_OF_BOOLEAN_CONSTANT,
    severities_object.CONSTANT_FUNCTIONS_USING_ASSEMBLY_CODE, severities_object.CONSTANT_FUNCTIONS_CHANGING_THE_STATE, severities_object.CONTROLLED_LOOP_ITERATION,
    severities_object.CONTROLLED_LOWLEVELCALL, severities_object.DIVIDE_BEFORE_MULTIPLY, severities_object.EMPTY_FUNCTIONS, severities_object.LACK_OF_MSGSENDER_USAGE,
    severities_object.MSGVALUE_USAGE_ON_NONPAYABLE_FUNCTIONS, severities_object.OVERFLOW_ERC20_ALLOWANCE, severities_object.ABSENCE_OF_PAUSABLE_MODIFIER,
    severities_object.REENTRANCY_VULNERABILITIES_1, severities_object.REUSED_BASE_CONSTRUCTORS, severities_object.DANGEROUS_USAGE_OF_TXGASPRICE,
    severities_object.DANGEROUS_USAGE_OF_TXORIGIN, severities_object.UNCHECKED_BLOCKHASH, severities_object.UNCHECKED_LOW_LEVEL_CALLS, severities_object.UNCHECKED_SEND,
    severities_object.UNINITIALIZED_LOCAL_VARIABLES, severities_object.UNINITIALIZED_RETURN_STATEMENTS, severities_object.UNSPECIFIED_OPERATION_ORDER, severities_object.UNUSED_EVENTS,
    severities_object.UNUSED_RETURN, severities_object.UNUSED_RETURN_INTERNAL, severities_object.DETECT_USE_AFTER_DELETE, severities_object.INCORRECT_MODIFIER,
    severities_object.BUILTIN_SYMBOL_SHADOWING, severities_object.LOCAL_VARIABLES_SHADOWING, severities_object.UNINITIALIZED_POINTERS_IN_CONSTRUCTORS, severities_object.PREDECLARATION_USAGE_OF_LOCAL_VARIABLES,
    severities_object.CALLS_INSIDE_LOOP, severities_object.MISSING_EVENTS_ACCESS_CONTROL, severities_object.MISSING_EVENTS_ARITHMETIC, severities_object.DANGEROUS_UNARY_EXPRESSIONS,
    severities_object.MISSING_ZERO_ADDRESS_VALIDATION, severities_object.REENTRANCY_VULNERABILITIES_2, severities_object.BLOCK_TIMESTAMP, severities_object.ASSEMBLY_USAGE, severities_object.ASSERT_STATE_CHANGE,
    severities_object.BOOLEAN_EQUALITY, severities_object.UNINDEXED_ERC20_EVENT_PARAMETERS, severities_object.LOW_LEVEL_CALLS, severities_object.MISSING_INHERITANCE,
    severities_object.CONFORMANCE_TO_NAME_CONVENTIONS, severities_object.DIFFERENT_PRAGMA_DIRECTIVES_USED, severities_object.REDUNDANT_STATEMENTS, severities_object.INCORRECT_VERSIONS_OF_SOLIDITY,
    severities_object.UNINPLEMENTED_FUNCTIONS, severities_object.UNUSED_STATE_VARIABLE, severities_object.COSTLY_OPERATION_INSIDE_LOOP, severities_object.DEAD_CODE,
    severities_object.REENTRANCY_VULNERABILITIES_3, severities_object.REENTRANCY_VULNERABILITIES_4, severities_object.VARIABLE_NAMES_TO_SIMILAR, severities_object.TOO_MANY_DIGITS,
    severities_object.STATE_VARIABLES_THAT_COULD_BE_CONSTANT, severities_object.PUBLIC_FUNCTION_THAT_COULD_BE_EXTERNAL, vulnerabilities_object.BLOCKHASH_CURRENT]

# Opens the actual .txt file with analysis results
with open('slither_analysis.txt', 'r') as f1:
    first_line = f1.readline()
    first_line_list = list(first_line)
    contract_name = get_contract_name_from_line(first_line_list)

    # Add column for indicating if contract was analyzed or not (exception)
    vulnerabilities.append("ANALYZED")

    # Dictionary for counting vulnerability incidence
    # EXAMPLE:
    #  { 'vulnerability0': 0, 'vulnerability1': 0, ... ,'vulnerabilityN': 0}
    total_incidence_dic = dict(zip(vulnerabilities, [0] * 106))

    # Default as false
    total_incidence_dic["ANALYZED"] = "False"

    # Dictionary for containing contract name with each vulnerability and its incidence
    # EXAMPLE:
    #  {'example1.sol' : {'vulnerability0': 3, 'vulnerability1': 1, ..., 'vulnerabilityN': X}}
    #  {'example2.sol' : {'vulnerability0': 0, 'vulnerability1': 2, ..., 'vulnerabilityN': Y}}
    #  ...
    #  {'exampleN.sol' : {'vulnerability0': 1, 'vulnerability1': 0, ..., 'vulnerabilityN': Z}}
    contract_incidence_dic = {contract_name :  dict(zip(vulnerabilities, [0] * 106))}

    # counts the number of the contract analyzed
    counter = 1
    result = ""
    has_vulnerability = False
    for line in f1:
        if 'ANALYZED' in line:
            # print(f"{counter}) {contract_name}")
            # print(result)
            if counter > 1:
                contract_incidence_dic[contract_name] = dict(zip(vulnerabilities, [0] * 106))
                contract_incidence_dic[contract_name]["ANALYZED"] = "False"

            clean_contract_name = contract_name.replace("\n", "")
            if f"{clean_contract_name} analyzed" in result:
                contract_incidence_dic[contract_name]["ANALYZED"] = "True"

            for v in vulnerabilities:
                if(f"{scf.REFERENCE_URL}{v}" in result):
                    # print(f"\nABI encoder found in file number {counter} and name {contract_name}")
                    contract_incidence_dic[contract_name][v] += 1
                    total_incidence_dic[v] += 1
                    has_vulnerability = True

            first_line_list = list(line)
            contract_name = get_contract_name_from_line(first_line_list)
            result = ""
            counter += 1
            continue

        result += line

    # Generating .xlsx file
    workbook = xls.Workbook("slither_results.xlsx")
    worksheet = workbook.add_worksheet("Slither")

    # Fulffiling file
    work_line = 0
    work_column = 0
    contracts = list(contract_incidence_dic.keys())

    # Setting first cell (0,0)
    cell_format = workbook.add_format()
    cell_format.set_bg_color('black')
    worksheet.write(0, 0, "", cell_format)

    severities.append("")
    contracts.insert(0, "")
    for contract in contracts:
        for v, s in zip(vulnerabilities, severities):
            if work_line == int(0):
                # Adding style to cell
                cell_format = workbook.add_format()
                cell_format.set_bold()
                work_column += 1

                if s == "High":
                    cell_format.set_bg_color('red')

                if s == "Medium":
                    cell_format.set_bg_color('yellow')

                if s == "Low":
                    cell_format.set_bg_color("green")

                if s == "Informational":
                    cell_format.set_bg_color("blue")

                if s == "Optimization":
                    cell_format.set_bg_color("silver")

                if s == "":
                    cell_format.set_bg_color("purple")

                worksheet.write(work_line, work_column, v, cell_format)

            else:
                if work_column == int(0):
                    cell_format = workbook.add_format()
                    cell_format.set_bold()
                    cell_format.set_bg_color("D3D3D3")

                    worksheet.write(work_line, work_column, contract, cell_format)
                    work_column += 1

                worksheet.write(work_line, work_column, contract_incidence_dic[contract][v])
                work_column += 1

        work_line += 1
        work_column = 0

    # Adding cell styles
    cell_format = workbook.add_format()
    cell_format.set_bold()
    cell_format.set_bg_color("gray")

    # Adding TOTAL number/count of each vulnerability
    worksheet.write(work_line, work_column, "TOTAL", cell_format)
    work_column += 1
    for v in vulnerabilities:
        worksheet.write(work_line, work_column, total_incidence_dic[v])
        work_column += 1

    workbook.close()
    f1.close()


from django.urls import path,include
from shoppingsite import views
from django.contrib.auth import views as auth_views

app_name= 'shoppingsite'

urlpatterns = [
	path('design1/', views.designview),
	path('shopcategory1/<int:id>', views.shopcategory1view),
    path('shopproducts/<int:id>/<int:subid>', views.shopproducts),
	path('insertcontactform/', views.insertcontactform),
	path('contact/', views.contactview.as_view()),
	path('faqs/', views.faqsview.as_view()),
	path('emp/', views.showemp),
	path('viewemp/<int:id>', views.showemployee),
	path('login/',auth_views.LoginView.as_view(),name='login'),
	path('logout/',auth_views.LogoutView.as_view(),name='logout'),
	path('sign/',views.signup),
	path('insertcomment/', views.insertcomment),
	path('insertreview/', views.insertreview),
	path('termsconditions/', views.termsconditionsview.as_view()),
	path('privacypolicy/', views.privacypolicyview.as_view()),
	path('returnrefund/', views.returnrefundview.as_view()),
	path('about/', views.aboutsview.as_view()),
	path('security/', views.securityview.as_view()),
	path('search/', views.searchview, name='search_result'),
	path('insertsubscribe/', views.insertsubscribe),
	path('showproduct/<int:id>/<int:catid>', views.showproductview),
	path('categoryproducts/<int:subid>', views.categoryproductsview),
	path('offer/',views.offer),
	path('viewmore/<str:t>',views.viewmore),
    path('blog/',views.blogview),
    path('blogdetail/<int:id>',views.blogdetailview),
]


import fedmsg.encoding
import os
import re
import shutil
import tarfile
import tempfile
import time
import urllib

try:
    import lzma
except ImportError:
    import backports.lzma as lzma

import datanommer.models as dm
import datagrepper.app
from datagrepper.util import assemble_timerange

OPTIONS = ('start', 'end', 'delta')
LIST_OPTIONS = ('user', 'package', 'category', 'topic', 'meta')

class DataQuery(object):
    """
    Handles parsing queries, saving them, and filtering objects based on the
    query.
    """

    @classmethod
    def from_request_args(cls, request_args):
        obj = cls()
        opts = dict()

        for arg in OPTIONS:
            opts[arg] = request_args.get(arg, None)

        for arg in LIST_OPTIONS:
            opts[arg] = request_args.getlist(arg)

        opts['start'], opts['end'], opts['delta'] = \
            assemble_timerange(opts['start'], opts['end'], opts['delta'])

        obj.options = opts
        return obj

    @classmethod
    def from_database(cls, job_obj):
        obj = cls()
        obj.options = job_obj.dataquery['options']
        return obj

    def database_repr(self):
        return {'options': self.options}

    def run_query(self, output_prefix):
        """
        Returns the location of the output file, which is either a .json.xz or
        a .tar.xz. The output filename will start with output_prefix.
        """
        def output_file(messages, dir):
            earliest = int(time.mktime(messages[0].timestamp.timetuple()))
            latest = int(time.mktime(messages[-1].timestamp.timetuple()))
            filename = 'messages_{0}_{1}.json'.format(earliest, latest)
            with open(os.path.join(dir, filename), 'w') as f:
                f.write(fedmsg.encoding.dumps(messages))
            return filename

        dir = tempfile.mkdtemp(prefix='datagrepper-tmp')
        total, pages, query = dm.Message.grep(
            start=(self.options['start'] and
                   datetime.fromtimestamp(self.options['start'])),
            end=(self.options['end'] and
                 datetime.fromtimestamp(self.options['end'])),
            rows_per_page=None,
            users=self.options['user'],
            packages=self.options['package'],
            categories=self.options['category'],
            topics=self.options['topic'],
            defer=True,
        )

        messages = []
        files = []
        for message in query.yield_per(10):
            messages.append(message)
            if len(messages) >= 10000:
                files.append(output_file(messages, dir))
                messages = []
        files.append(output_file(messages, dir))

        if len(files) > 1:
            extension = '.tar.xz'
            fname = os.path.join(datagrepper.app.app.config['JOB_OUTPUT_DIR'],
                                 output_prefix + extension)
            try:
                with lzma.open(fname, 'w') as lzmaobj:
                    tar = tarfile.open(fileobj=lzmaobj, mode='w')
                    try:
                        for filename in files:
                            tar.add(os.path.join(dir, filename),
                                    arcname=filename)
                    finally:
                        # No matter what, we want this to happen
                        tar.close()
                        # Re-raise the exception so we can mark the job failed
                        raise
            except:
                # Delete the file, since we can't track it if we fail the job
                os.remove(fname)
                # Re-raise the exception so we can mark the job failed
                raise
        else:
            extension = '.json.xz'
            fname = os.path.join(datagrepper.app.app.config['JOB_OUTPUT_DIR'],
                                 output_prefix + extension)
            with lzma.open(fname, 'w') as lzmaobj:
                with open(os.path.join(dir, files[0]), 'r') as f:
                    while True:
                        line = f.readline(int(10e6))  # limit to 10 MB per read
                        if not line:
                            break
                        lzmaobj.write(line)

        shutil.rmtree(dir, ignore_errors=True)
        return output_prefix + extension


from __future__ import annotations

import logging
from multiprocessing.connection import Client, Listener
from types import FunctionType
from typing import Any, Tuple, Union

logging.basicConfig(
    format="[%(levelname)s] %(message)s",
    # datefmt="%m/%d/%Y:%I:%M:%S",
    level=logging.INFO,
)

SIGENDS: bytes = b"SIGENDS"

class Appd(Listener):
    """Inherit form this class and define your methods:

    class MyServer(Appd):
        def my_echo_method(self, my_arg):
            return my_arg
    """

    response: Any = None
    request: Tuple = ()
    _api: dict = {}

    def api(self, func: FunctionType):
        self._api[func.__name__] = func

    def start(self):
        while True:
            local = f"{self.address[0]}:{self.address[1]}"

            logging.info("[LST] -> %s", local)

            with self.accept() as conn:

                remote = f"{self.last_accepted[0]}:{self.last_accepted[1]}"

                logging.info("[SES] <- %s", remote)

                while self.response != SIGENDS:
                    try:
                        self.request = conn.recv()
                    except EOFError:
                        break

                    logging.info("[REQ] <- %s + %s", remote, self.request)

                    if self.request[0] in self._api.keys():
                        self.response = self._api[self.request[0]](
                            *self.request[1], **self.request[2]
                        )
                    elif self.request == SIGENDS:
                        self.response = SIGENDS
                    else:
                        self.response = NotImplementedError(self.request[0])

                    conn.send(self.response)

                    logging.info("[RES] + %s -> %s", self.response, remote)
                else:
                    self.response = None

                logging.info("[SES] x %s", remote)

class _Client:
    """Wraps multiprocessing.connection.Client"""

    request: Tuple = ()
    response: Any = None

    def __init__(
        self,
        address: Union[str, Tuple[str, int]],
        family: str = None,
        authkey: bytes = None,
    ):
        self.conn = Client(address, family, authkey)

    def commit(self, endpoint: str, *args, **kwargs) -> Any:
        """Used to form and send the request to the Appd,
        then accepts the response from it.

        Args:
            method (str): A name of the method to call on the Appd

        Raises:
            response: The value returned by the method on the Appd
        """
        self.request = (endpoint, args, kwargs)
        self.conn.send(self.request)
        self.response = self.conn.recv()

        if isinstance(self.response, Exception):
            raise self.response

        return self.response

    def end_session(self):
        self.conn.send(SIGENDS)
        self.response = self.conn.recv()

        try:
            if self.response != SIGENDS:
                raise ValueError(f"Improperly closed session: {self.response}")
        finally:
            self.conn.close()

class ClientSession:
    """A Context Manger for client"""

    def __init__(
        self,
        address: Union[str, Tuple[str, int]],
        family: str = None,
        authkey: bytes = None,
    ):
        self.client = _Client(address, family, authkey)

    def __enter__(self):
        return self.client

    def __exit__(self, exc_type, exc_value, traceback):
        self.client.end_session()