import random
import re
from copy import deepcopy

import torch
from myModel import MyModel
import torchvision
from matplotlib import pyplot
from torch import nn
from torch.utils.data.dataloader import DataLoader
from torch.utils.tensorboard import SummaryWriter

from Loader import *

def outWrongMatCsv(WrongMat:np.ndarray,label:dict):
  label = [chr(a) for a in label.values()]
  sumOfRow = np.sum(WrongMat,axis=1)
  WrongMat = np.where(sumOfRow == 0,0,WrongMat/sumOfRow)
  with open("WrongMat.csv","w",newline="") as WMfp:
    writer = csv.writer(WMfp)
    writer.writerow([""]+label)
    for i in range(len(label)):
      writer.writerow([label[i]]+WrongMat[i].tolist())

def charTest(setName = "Test"):
  #参数
  miniBatch = 100   #批大小
  #部署GPU
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  #数据
  if(setName == "Test"):
    testSet = TestDataSet("Test",prePorcess=False)
  else:
    testSet = CharDataSet(setName,prePorcess=False)
  testLoader = DataLoader(testSet,miniBatch,shuffle=False,drop_last=False)
  testLen = len(testSet)

  #模型
  myModel = MyModel()
  myModel.classifier[-1] = nn.Linear(256,71)
  # myModel = torchvision.models.vgg16()
  # myModel.classifier[6] = nn.Linear(4096,71)
  #将模型中的ReLU换成mish
  # for i in range(len(myModel.features)):
  #   if isinstance(myModel.features[i],nn.ReLU):
  #     myModel.features[i] = nn.Mish(inplace=True)
  # for i in range(len(myModel.classifier)):
  #   if isinstance(myModel.classifier[i],nn.ReLU):
  #     myModel.classifier[i] = nn.Mish(inplace=True)
  myModel.load_state_dict(torch.load("Models/myModel.pth"))
  myModel.train(False)
  myModel = myModel.to(device)

  #代价函数
  lossFn = nn.CrossEntropyLoss()
  lossFn = lossFn.to(device)

  #评估指标
  testLoss = 0
  rightCount = 0

  #错误图
  wrongImgs = [[]for i in range(71)]
  shutil.rmtree("TestLogs",ignore_errors=True)
  logWriter = SummaryWriter("TestLogs")
  wrongResult = [[]for i in range(71)]

  #错误矩阵
  wrongMat = np.zeros((71,71),dtype="uint8")

  testLoss = 0     #总损失
  AvgLoss = 0       #平均损失
  rightRate = 0     #正确率
  rightCount = 0

  #计算精确率和召回率的数组
  testLen = len(testSet)
  TP = [0]*71
  FP = [0]*71
  FN = [0]*71
  TN = [0]*71

  with torch.no_grad():
    batchCount = testLen // miniBatch + 1
    batchi = 0        #用来输出测试进度
    for data in testLoader:
      imgs,targets = data
      imgs = imgs.to(device)
      targets = targets.to(device)
      outputs = myModel(imgs)                     #跑验证集
      result = torch.argmax(outputs,1)            #取最大值
      rightCount += torch.sum(result==targets)    #和标签比较

      #写入错误图片和错误矩阵
      for i in range(len(result)):
        if result[i] != targets[i]:
          wrongImgs[result[i]].append(imgs[i].cpu().numpy().tolist())
          wrongResult[result[i]].append(targets[i].item())
        wrongMat[targets[i]][result[i]] += 1

      loss = lossFn(outputs,targets)              #代价函数
      testLoss+=loss
      batchi += 1
      print(f"\r测试中，进度{(batchi / batchCount)*100:.2f}%",flush=True,end="")

      #统计TP/FP/FN/TN
      for i in range(len(targets)):
        if(result[i]==targets[i]):
          TP[targets[i]]+=1
        else:
          FP[result[i]]+=1
          FN[targets[i]]+=1
    print()
    rightRate = rightCount/testLen*100
    AvgLoss = testLoss/(testLen/miniBatch)

    TP = np.array(TP)
    FP = np.array(FP)
    FN = np.array(FN)
    TN = np.array(TN)
    TN = testLen-TP-FP-FN

    #计算正确率，精确率，召回率，F1分数
    rightRate = rightCount/testLen
    accRate = (TP / (TP+FP))
    recallRate = (TP / (TP+FN))
    F1Score = (2*accRate*recallRate)/(accRate+recallRate)

  #控制台输出测试结果
  print(f"测试损失:{AvgLoss:.3f},总正确率:{rightRate*100:.3f}%")
  print(f"平均精确率：{np.nanmean(accRate)*100:.2f}%，最大精确率：{np.nanmax(accRate)*100:.2f}%,最小精确率：{np.nanmin(accRate)*100:.2f}%")
  print(f"平均召回率：{np.nanmean(recallRate)*100:.2f}%，最大召回率：{np.nanmax(recallRate)*100:.2f}%,最小召回率：{np.nanmin(recallRate)*100:.2f}%")
  print(f"平均F1分数：{np.nanmean(F1Score):.4f}，最大F1分数：{np.nanmax(F1Score):.4f},最小F1分数：{np.nanmin(F1Score):.4f}")

  #写入tensorboard
  for i in range(len(wrongImgs)):
    imgs = torch.from_numpy(np.asarray(wrongImgs[i])).type(torch.uint8)
    if(len(imgs)!=0):
      logWriter.add_images(f"result:{chr(testSet.rCharDict[i])},target:{[chr(testSet.rCharDict[item]) for item in wrongResult[i]]}",imgs)

  logWriter.close()
  outWrongMatCsv(wrongMat,testSet.rCharDict)

class sentenceLoader(TestDataSet):    #重写TestDataSet使其支持读取句子
  def __init__(self,path,prePorcess=False):
    super().__init__(path,prePorcess)
    self.makeSentence()

  def makeSentence(self):
    self.sentence = {}
    with open(f"{self.rootDir}/data.csv",mode="r",encoding="utf8") as csvFile:
      reader = csv.reader(csvFile)
      next(reader)            #读掉表头
      for row in reader:
        if(row[5] == 'FALSE'):    #不忽略文本
          row = [int(i) for i in row[0:5]] + row[5:]
          if((row[0],row[1],row[2]) not in self.sentence.keys()):
            self.sentence[(row[0],row[1],row[2])] = []
          self.sentence[(row[0],row[1],row[2])].append((row[3],row[4],row[6]))

      for key in self.sentence.keys():
        self.sentence[key].sort()

    self.keys = list(self.sentence.keys())
    self.lenFileList = len(self.keys)

  def __getitem__(self, index):
    imgs = []
    targets = []
    key = self.keys[index]
    for val in self.sentence[key]:
      img = cv2.imread(f"Temp/{self.rootDir}/data/{key[0]}_{key[1]}_{key[2]}_{val[0]}_{val[1]}.png",flags=0)
      img = np.repeat(img[np.newaxis,:,:],3,axis=0)
      imgs.append(img)
      targets.append(val[2])

    imgs = torch.from_numpy(np.asarray(imgs)).type(torch.float)
    targets = [self.charDict[ord(c)] for c in targets]
    targets = torch.from_numpy(np.asarray(targets)).type(torch.int)
    return imgs,targets

  def __len__(self):
    return self.lenFileList

def sentenceTest():
  #部署GPU
  device = torch.device("cuda")
  #数据
  testSet = sentenceLoader("Test",prePorcess=False)

  #模型
  myModel = MyModel()
  myModel.classifier[-1] = nn.Linear(256,71)
  # myModel = torchvision.models.vgg16()
  # myModel.classifier[6] = nn.Linear(4096,71)
  #将模型中的ReLU换成mish
  # for i in range(len(myModel.features)):
  #   if isinstance(myModel.features[i],nn.ReLU):
  #     myModel.features[i] = nn.Mish(inplace=True)
  # for i in range(len(myModel.classifier)):
  #   if isinstance(myModel.classifier[i],nn.ReLU):
  #     myModel.classifier[i] = nn.Mish(inplace=True)
  myModel.load_state_dict(torch.load("Models/myModel.pth"))
  myModel.train(False)
  myModel = myModel.to(device)

  #代价函数
  lossFn = nn.CrossEntropyLoss()
  lossFn = lossFn.to(device)

  #评估指标
  rightCount = 0

  #错误图
  wrongImgs = [[]for i in range(71)]
  shutil.rmtree("TestLogs",ignore_errors=True)
  logWriter = SummaryWriter("TestLogs")
  wrongResult = [[]for i in range(71)]

  #正确率
  rightRate = 0     #正确率
  rightCount = 0
  totalCount = 0

  #结果标记硬编码
  alpAndDigConfuse = list("0Oo1Il")  # 数字字母易混
  alp2DigMapper = {"O": "0", "I": "1"}           # 字母转数字
  dig2AlpMapper = {"0": "O", "1": "I"}            # 数字转字母
  terms = {"KPA":"kPa","KG":"kg","K9":"kg","IBS":"lbs"}

  def fixResult(result, resultCf):
    # 统计连续数字混淆的个数，1个只有相邻才转换，2个以上直接转换
    # 最后修正专用名词，比如kPa
    resultMark = []         #标记结果

    # 标记
    for i in range(len(result)):
      if(result[i] in alpAndDigConfuse):
        resultMark.append("D")
      else:
        resultMark.append("*")

    # 统一转换大写
    for i in range(len(result)):
      if(result[i] == "l"):   #I和l特殊处理
        result[i] = "I"
      else:
        result[i] = str.upper(result[i])
      if(resultMark[i] != "D"):
        resultMark[i] = "*"

    # 数字修正
    L,R = -1,-1
    for i in range(len(result)):
      if(resultMark[i] == "D"):   #统计数字长度
        if(L == -1): L,R = i,i
        R+=1
      else:         #转换数字
        if(L != -1 and R - L >= 3):   #长数字，可以转换
          for j in range(L,R):
            if(j == L and result[j] in ["0","O"] and L-1 >= 0 and not str.isdigit(result[L-1])):
              result[j] = "O"         #0不可能出现在最前面，除非前面有数字
            elif(result[j] in alp2DigMapper.keys()):
              result[j] = alp2DigMapper[result[j]]
            resultMark[j] = "*"

        elif(L != -1 and R - L < 3):
          if(L - 1 >= 0 and str.isdigit(result[L-1]) or R < len(result) and str.isdigit(result[R])):
            for j in range(L,R):  #太短，但是左右有数字，可以转换
              if(j == L and result[j] in ["0","O"] and L-1 >= 0 and not str.isdigit(result[L-1])):
                result[j] = "O"
              elif(result[j] in alp2DigMapper.keys()):
                result[j] = alp2DigMapper[result[j]]
              resultMark[j] = "*"
          else:     #太短，不能转换
            for j in range(L,R):
              if(result[j] in dig2AlpMapper.keys()):
                result[j] = dig2AlpMapper[result[j]]
              resultMark[j] = "*"
        L,R = -1,-1

    # 专用名词
    resStr = ""
    resStr = str.join(resStr,result)
    for t in terms.keys():
      resStr = resStr.replace(t,terms[t])
    result = list(resStr)
    return result

  #计算精确率和召回率的数组
  TP = [0]*71
  FP = [0]*71
  FN = [0]*71
  TN = [0]*71

  with torch.no_grad():
    for dataIdx in range(len(testSet)):
      data = testSet[dataIdx]
      imgs,targets = data
      imgs = imgs.to(device)
      targets = targets.to(device)
      outputs = myModel(imgs)                     #跑测试集
      outputs = torch.softmax(outputs,dim=1)
      resultCf = torch.max(outputs,dim=1)             #取置信度
      result = torch.argmax(outputs,dim=1)            #取类别
      result = [chr(testSet.rCharDict[i.item()]) for i in result]   #类别转文本
      targets = [chr(testSet.rCharDict[i.item()]) for i in targets]
      #print(str.join("",result))
      result = fixResult(result, resultCf[0])  #修正结果

      #统计正确数量
      result = [testSet.charDict[ord(item)]for item in result]
      targets = [testSet.charDict[ord(item)]for item in targets]

      result = np.array(result)
      targets = np.array(targets)
      rightCount += np.sum(result == targets)
      totalCount += len(result)

      #写入错误图片
      for i in range(len(result)):
        if result[i] != targets[i]:
          wrongImgs[result[i]].append(imgs[i].cpu().numpy().tolist())
          wrongResult[result[i]].append(targets[i])

      #统计TP/FP/FN/TN
      for i in range(len(targets)):
        if(result[i]==targets[i]):
          TP[targets[i]]+=1
        else:
          FP[result[i]]+=1
          FN[targets[i]]+=1

    testLen = totalCount
    TP = np.array(TP)
    FP = np.array(FP)
    FN = np.array(FN)
    TN = np.array(TN)
    TN = testLen-TP-FP-FN

    #计算正确率，精确率，召回率，F1分数
    rightRate = rightCount/testLen
    accRate = (TP / (TP+FP))
    recallRate = (TP / (TP+FN))
    F1Score = (2*accRate*recallRate)/(accRate+recallRate)

  #控制台输出测试结果
  print(f"总正确率:{rightRate*100:.3f}%")
  print(f"平均精确率：{np.nanmean(accRate)*100:.2f}%，最大精确率：{np.nanmax(accRate)*100:.2f}%,最小精确率：{np.nanmin(accRate)*100:.2f}%")
  print(f"平均召回率：{np.nanmean(recallRate)*100:.2f}%，最大召回率：{np.nanmax(recallRate)*100:.2f}%,最小召回率：{np.nanmin(recallRate)*100:.2f}%")
  print(f"平均F1分数：{np.nanmean(F1Score):.4f}，最大F1分数：{np.nanmax(F1Score):.4f},最小F1分数：{np.nanmin(F1Score):.4f}")

  #写入tensorboard
  for i in range(len(wrongImgs)):
    imgs = torch.from_numpy(np.asarray(wrongImgs[i])).type(torch.uint8)
    if(len(imgs)!=0):
      logWriter.add_images(f"result:{chr(testSet.rCharDict[i])},target:{[chr(testSet.rCharDict[item]) for item in wrongResult[i]]}",imgs)

if __name__ == '__main__':
  charTest()
  sentenceTest()


from collections import Counter
class Solution:
    def minFlipsMonoIncr(self, S: str) -> int:
        ttl = Counter(S)
        cnt = Counter()
        ans = min(ttl['0'],ttl['1'])
        for c in S:
            cnt[c] += 1
            ans = min(ans,(ttl - cnt)['0'] + cnt['1'])
        return ans


# coding=utf-8

import re # módulo Regex do python

from django.db import models
from django.core import validators # para criar validações dos campos do modelo
from django.contrib.auth.models import (AbstractBaseUser, PermissionsMixin, UserManager)
from django.conf import settings

# Create your models here.

# Custom User (Customização do usuário)

# AbstractBaseUser => lógica básica (senha, último login e des/criptografar senha e etc)
# PermissionsMixin => segurança, permissão e grupos
# UserManager => Manager base (User.objects)

class User(AbstractBaseUser, PermissionsMixin):

    # validação do campo por Regex
    username = models.CharField(
        'Nome de Usuário', max_length=30, unique=True,
        validators=[validators.RegexValidator(re.compile('^[\w.@+-]+$'),
        'O nome de usuário só pode conter letras, digitos ou o seguinte caracteres @/./+/-/_', 'invalid')]
    )
    email = models.EmailField('E-mail', unique=True)
    name = models.CharField('Nome', max_length=100, blank=True)
    is_active = models.BooleanField('Está ativo?', blank=True, default=True) # Necessário para Admin do Django (usuário padrão)
    is_staff = models.BooleanField('É da equipe?', blank=True, default=False) # Necessário para Admin do Django (acessar a área administrativa)
    date_joined = models.DateTimeField('Data de Entrada', auto_now_add=True) # Necessário para Admin do Django

    objects = UserManager() # manager para classe (com funções uteis já estabelecidas)

    # Campos necessários para compatibilidade do Django
    USERNAME_FIELD = 'username' # campo único para referência do login (poderia ser email)
    REQUIRED_FIELDS = ['email'] # necessário para criação de super usuários

    # representação STRING do objeto
    def __str__(self):
        return self.name or self.username

    # necessário para o bom funcionamento do Admin
    def get_short_name(self):
        return self.username

    # necessário para o bom funcionamento do Admin
    def get_full_name(self):
        return str(self)

    class Meta:
        verbose_name = 'Usuário'
        verbose_name_plural = 'Usuários'

class PasswordReset(models.Model):

    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='Usuário',
                             #related_name='resets'
                            )
    key = models.CharField('Chave', max_length=100, unique=True)
    created_at = models.DateTimeField('Criado em', auto_now_add=True)
    confirmed = models.BooleanField('Confirmado', default=False, blank=True) # blank=True diz para o formulário que o campo não é obrigatório

    def __str__(self):
        return '{0} em {1}'.format(self.user, self.created_at)

    class Meta:
        verbose_name = 'Nova Senha'
        verbose_name_plural = 'Novas Senhas'
        ordering = ['-created_at'] # ordem decrescente de criação


r"""
bpl_operator
------------

**Bpl Operator** supports easy command line calls to diff, merge etc. bpl files with bpl syntax (xml style).
(\*.ini files are not supported)

**call syntax example**

C:\> python bpl_operator <op> -i <first.bpl> <second.bpl> -o <result.bpl> [-s]

This program does an operation on 2 BPL based files.
Result of operation is saved into an output BPL based file.

Attention: it does not handle sections! The output will not contain any sections.

The result will not contain duplicates of a recordings.

Option -s is intended to strictly use fileNames inside BPL's as they are,
otherwise, unc paths will be aligned and case insensitive comparison will take place.

<op> := and | or | xor | sub

  - *xor*: will contain files from either input (diff),
  - *or*:  will contain files from both inputs (merge),
  - *and*: will contain files common to both inputs,
  - *sub*: will contain files from first input which are not inside second input

As the result does not contain duplicated recordings this can also be used to clean up
all duplicate recordings in a bpl file by running

.. code::

    bpl_operator.py or -i main.bpl empty.bpl -o singles.bpl

Use for the empty.bpl::

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <BatchList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="batchlist.xsd">
  </BatchList>

This script does not use any other HPC imports and just needs a Python installation.
"""
# - import Python modules ---------------------------------------------------------------------------------------------
from __future__ import print_function
from sys import exit as sexit, stderr, argv
from xml.etree.ElementTree import Element, SubElement, tostring, parse
from xml.dom.minidom import parseString
from argparse import ArgumentParser, FileType, RawDescriptionHelpFormatter
from re import match
from six import PY3
if PY3:
    from io import StringIO
else:
    from StringIO import StringIO  # pylint: disable=E0401

# - classes -----------------------------------------------------------------------------------------------------------
class PlayList(list):
    """class to provide list of bpl files and operator methods"""

    def __init__(self, path, sensitive=False, mode='r'):
        """
        :param str path: path to file
        :param bool sensitive: when comparing to another PlayList, do it sensitive
        :param str mode: read=r, write=w
        """
        list.__init__(self)

        self._path = open(path, mode=mode) if not hasattr(path, "read") else path

        if mode == 'r':
            try:
                self.extend([node.get("fileName") for node in parse(self._path).getroot()])
            except Exception:
                stderr.write("error reading {}, assuming it's an empty one!".format(self._path.name))

        self._sensitive = sensitive

    def __enter__(self):
        """with..."""
        return self

    def __exit__(self, *_):
        """...with"""
        self.close()

    def __or__(self, other):
        """let's do | (or) operator"""
        return list(set(self.files()).union(set(other.files())))

    def __xor__(self, other):
        """let's do ^ (xor) operator"""
        return list(set(self.files()).symmetric_difference(set(other.files())))

    def __and__(self, other):
        """let's do & (and) operator"""
        return list(set(self.files()).intersection(set(other.files())))

    def __sub__(self, other):
        """let's do - (sub) operator"""
        return list(set(self.files()).difference(set(other.files())))

    def files(self):
        """remove additional dotted unc parts"""
        if self._sensitive:
            return self

        paths = []
        for file_ in self:
            mtc = match(r"(?i)(\\\\\w*)(\.[\w.]*)?(\\.*)", file_)
            paths.append((mtc.group(1) + mtc.group(3)).lower() if mtc else file_.lower())

        return paths

    def close(self):
        """close file"""
        if hasattr(self._path, "read"):
            self._path.close()
            self._path = None

    def write(self):
        """write file"""
        top = Element('BatchList')
        for file_ in self:
            sub = SubElement(top, "BatchEntry", {'fileName': file_})
            SubElement(sub, "SectionList")

        self._path.seek(0)
        self._path.write(parseString(tostring(top, 'utf-8')).toprettyxml(indent='    ', encoding='UTF-8'))

# - main --------------------------------------------------------------------------------------------------------------
def main():  # pragma: nocover
    """just calling the operation and saving the result"""
    opts = ArgumentParser(description=__doc__, formatter_class=RawDescriptionHelpFormatter)
    opts.add_argument(dest="arith", choices=['xor', 'or', 'and', 'sub'], type=str,
                      help="what to do (diff,merge,common,only in 1st)?")
    opts.add_argument("-i", dest="infiles", nargs='+', type=FileType('rb'), help="input files to process")
    opts.add_argument("-o", dest="outfile", required=True, type=FileType('wb'), help="output file")
    opts.add_argument("-s", dest="sensitive", default=False, action="store_true",
                      help="compare files with case-sensitivity")
    # opts.add_argument("-")
    args = opts.parse_args(None if argv[1:] else ['-h'])
    arith = {"xor": lambda x, y: x ^ y, "or": lambda x, y: x | y, "and": lambda x, y: x & y, "sub": lambda x, y: x - y}

    infiles = args.infiles
    if len(infiles) == 1:
        infiles.append(StringIO('<?xml version="1.0" encoding="UTF-8"?><BatchList/>'))
    elif len(infiles) > 2:
        print("sorry, only 2 files at max are supported by now.")
        return 1

    with PlayList(infiles[0], args.sensitive) as src1, PlayList(infiles[1], args.sensitive) as src2, \
            PlayList(args.outfile, mode='w') as trgt:
        src1.close()
        src2.close()
        trgt.extend(arith[args.arith](src1, src2))
        trgt.write()

    return 0

# - main --------------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
    sexit(main())


#!/usr/bin/env python3

# std import
import csv
import sys
import argparse

# pip import
import itertools
import networkx as nx

def main(args=None):

    if args is None:
        args = sys.argv[1:]

    parser = argparse.ArgumentParser(prog="knot.analysis.classifications")

    parser.add_argument("-i", "--input", type=argparse.FileType('r'), help="path to the AAG", required=True)
    parser.add_argument("-o", "--output", type=argparse.FileType('w'), help="path where hamilton path was write", required=True)
    parser.add_argument("-c", "--circular", action="store_true", help="genome is circular")

    args = vars(parser.parse_args(args))

    graph = AAG2graph(args["input"])

    paths = []
    for cycle in valid_cycle(graph, args["circular"]):
        weight = sum([graph.edges[edge[0], edge[1]]["weight"] for edge in cycle])

        nodes = ",".join(remove_duplicate_if_follow([e for edge in cycle for e in edge]))
        paths.append((weight, nodes))

    paths.sort()

    for (weight, nodes) in paths:
        print(";".join([nodes, str(weight)]), file=args["output"])

def remove_duplicate_if_follow(l):
    previous = None
    for elt in l:
        if elt == previous:
            previous = elt
            continue

        previous = elt
        yield elt

def valid_cycle(graph, circular_genome=False):
    for g in itertools.permutations(graph.nodes()):
        if not contig_congruence(g):
            continue

        if g.index(min(g)) != 0:
            continue

        if circular_genome:
            edges = [edge for edge in zip(g, g[1:] + g[:1])]
        else:
            edges = [edge for edge in zip(g, g[1:])]

        if any([edge not in graph.edges for edge in edges]) :
            continue

        yield edges

def contig_congruence(g):
    for i, j in zip(g[::2], g[1::2]):
        if i.split("_")[0] != j.split("_")[0]:
            return False
    return True

def AAG2graph(AAG_path):

    paths = dict()
    reader = csv.DictReader(AAG_path)
    for row in reader:
        key = frozenset((row["tig1"], row["tig2"]))

        if row["paths"] == "not_found":
            continue

        if row["paths"] == "not_search":
            nb_base = 0
        else:
            nb_base = int(row["nb_base"])

        if nb_base < 0:
            nb_base = 0

        if key in paths and paths[key] < nb_base:
            continue

        paths[key] = nb_base

    graph = nx.DiGraph()
    for (tig1, tig2), nb_base in paths.items():
        graph.add_edge(tig1, tig2, weight=nb_base)
        graph.add_edge(tig2, tig1, weight=nb_base)

    tig_in_graph = set()
    for key in paths.keys():
        for k in key:
            tig_in_graph.add(k.split("_")[0])

    for tig in tig_in_graph:
        graph.add_edge(tig+"_begin", tig+"_end", weight=0)
        graph.add_edge(tig+"_end", tig+"_begin", weight=0)

    return graph

if __name__ == "__main__":
    main()


from tkinter import *
import tkinter as tk
import playsound
from PIL import ImageTk, Image, ImageSequence
import threading
import time
import random
import sys

def disable_event():
    pass

def YouAreAnIdiot():
    global img

    root= Tk()
    root.title("Idiot")
    # root.attributes('-toolwindow', True) # bad attribute "-toolwindow": must be -alpha, -topmost, -zoomed, -fullscreen, or -type

    # play a sound file
    playsound.playsound('idiot.mp3', True)

    # root attributes: -alpha, -topmost, -zoomed, -fullscreen, -type
    if sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
        root.attributes('-toolwindow', True)
    else:
        root.attributes('-alpha', 0.5)

    x= _getRandomX()
    y= _getRandomY()
    # print('this is x: ' + str(x))
    # print('this is y: ' + str(y))
    root.resizable(0,0)
    root.geometry(f'234x171+{x}+{y}')

    # change background color in every new window

    color = '#%06x' % random.randint(0, 0xFFFFFF)
    root.configure(background=color)

    # Label(root, text="Idiot", font=("Helvetica", 20),background=color ).pack()
    # Label(root, text="You are an idiot", font=("Helvetica", 20),  ).pack()
    # no background color font

    Label(root, text="You are an idiot", font=("Franklin Gothic Medium Cond", 23),background=color, anchor='center' ).pack(
        fill=BOTH, expand=1)

    root.protocol("WM_DELETE_WINDOW", disable_event)

    root.mainloop()

values_of_x = []
values_of_y = []
def _getRandomX():
    global values_of_x
    # print(values_of_x)
    x = random.randint(0,999)
    if x not in values_of_x:
        values_of_x.append(x)
        return x
    else:
        return _getRandomX()

def _getRandomY():
    global values_of_y
    # print(values_of_y)
    y = random.randint(0,999)
    if y not in values_of_y:
        values_of_y.append(y)
        return y
    else:
        return _getRandomY()

if __name__=='__main__':
    while True:
        thread = threading.Thread(target=YouAreAnIdiot)
        thread.start()
        time.sleep(0.6)


from typing import List

class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        order_map = {char: idx for idx, char in enumerate(order)}

        for i in range(len(words) - 1):
            word1, word2 = words[i], words[i + 1]
            shorter_len = min(len(word1), len(word2))

            for j in range(shorter_len):
                char1, char2 = word1[j], word2[j]

                if order_map[char1] < order_map[char2]:
                    break

                if order_map[char1] > order_map[char2]:
                    return False
            else:
                if len(word1) > len(word2):
                    return False

        return True


import jwt
from flask_httpauth import HTTPTokenAuth
from sqlalchemy import select

from app import db, secret_token
from app.daos.user_dao import check_password
from app.models.user import User

token_auth = HTTPTokenAuth(scheme="Bearer")

@token_auth.verify_token
def verify_token(token):
    try:
        decoded_jwt = jwt.decode(token, secret_token, algorithms=["HS256"])
    except Exception:
        print("Unable to decode JWT")
        return None

    username = decoded_jwt["username"]
    user = db.session.scalars(
        select(User).where(User.username == username)
    ).one_or_none()
    if user:
        return user
    print(f"Unknown user {username}")
    return None


from config import hparams

PAD = "<pad>"
EOS = "<EOS>"
UNK = "<UNK>"
SEG = "|"
RESERVED_TOKENS = [PAD, EOS, UNK]
NUM_RESERVED_TOKENS = len(RESERVED_TOKENS)
PAD_ID = RESERVED_TOKENS.index(PAD)  # Normally 0
EOS_ID = RESERVED_TOKENS.index(EOS)  # Normally 1
UNK_ID = RESERVED_TOKENS.index(UNK)  # Normally 2

_initialized = False
_ALL_CONSONANTS_SET = set()
_ALL_VOWELS_SET = set()

#读入音素表
def load_phoneme_list(phoneme_path):
    _g2p_dictionary = {
        'AP': ['AP'],
        'SP': ['SP']
    }
    _set = set()
    #tmp_file_path = os.getcwd()
    #tmp_file_path = tmp_file_path + '/' + phoneme_path
    with open(phoneme_path, 'r', encoding='utf8') as _df:
        _lines = _df.readlines()
    for _line in _lines:
        _pinyin, _ph_str = _line.strip().split('\t')
        _g2p_dictionary[_pinyin] = _ph_str.split()
    for _list in _g2p_dictionary.values():
        [_set.add(ph) for ph in _list]
    _phoneme_list = sorted(list(_set))
    print('| load phone set:', _phoneme_list)

    phone_id = dict()
    id_phoneme = dict()
    for res_token in RESERVED_TOKENS:
        phone_id [ res_token ] = RESERVED_TOKENS.index(res_token)
        id_phoneme [RESERVED_TOKENS.index(res_token)] = res_token

    index_begin = len(RESERVED_TOKENS)
    for phoneme in _phoneme_list:
        phone_id[phoneme] = index_begin
        id_phoneme[index_begin] = phoneme
        index_begin = index_begin + 1

    return _phoneme_list,_g2p_dictionary,phone_id,id_phoneme,

def get_all_vowels():
    global _initialized
    if not _initialized:
        # 读入音素字典
        phone_list, pinyin2phs, phone_id, id_phoneme = load_phoneme_list(hparams['g2p_dictionary'])
        for _ph_list in pinyin2phs.values():
            _ph_count = len(_ph_list)
            if _ph_count == 0 or _ph_list[0] in ['AP', 'SP']:
                continue
            elif len(_ph_list) == 1:
                _ALL_VOWELS_SET.add(_ph_list[0])
            else:
                _ALL_CONSONANTS_SET.add(_ph_list[0])
                _ALL_VOWELS_SET.add(_ph_list[1])
        _initialized = True

    return sorted(_ALL_VOWELS_SET),phone_list


from datetime import datetime
import uuid
from typing import Optional, Union

from pydantic import BaseModel

class BillingBase(BaseModel):
    id: uuid.UUID

    class Config:
        orm_mode = True

class DateTimeBase(BillingBase):
    created_at: Optional[datetime]
    updated_at: Optional[datetime]

class SubscriptionPlanSchema(BillingBase):
    price: int
    duration: int
    currency: str
    status: str

    class Config:
        orm_mode = True

class SubscriptionSchema(DateTimeBase):
    customer_id: uuid.UUID
    plan_id: uuid.UUID
    start_date: datetime
    end_date: datetime
    status: str
    payment_id: str

    class Config:
        orm_mode = True

class RefundSchema(DateTimeBase):
    payment_id: str
    customer_id: Union[str, uuid.UUID]
    amount: int
    status: str

    class Config:
        orm_mode = True

class CreateSubscriptionSchema(BaseModel):
    subscription_plan_id: str


import types

from stypy.types import union_type

test_types = {
    'function': {
        'a': int,
    },
    '__main__': {
        'function': types.LambdaType,
        'TypeDataFileWriter': types.ClassType,
        'x': union_type.UnionType.create_from_type_list([str, int, bool]),
    },
}


# LeetCode Problem Nr. 34: 反转字符串

from typing import List

class Solution:
    def reverseString(self, s: List[str]) -> None:
        return self.__method1(s)

    def __method1(self, s: List[str]) -> None:
        left, right = 0, len(s) - 1
        while left < right:
            s[left], s[right] = s[right], s[left]
            left += 1
            right -= 1


from datetime import datetime
import re

class Building(object):
    def __init__(self, name, addr, id, url):
        self.name = name
        self.addr = addr
        self.id = id
        self.url = url

        self.listings = []

    @property
    def num_listings(self):
        return len(self.listings)

    def add_listing(self, listing):
        self.listings.append(listing)
        listing.building = self

    def rows(self):
        return [l.serialize() for l in self.listings]

class Listing(object):
    def __init__(self, timestamp, id, unit, rent, beds, baths, sqft):
        self.id = id
        self.timestamp = float(timestamp)
        self.dttime = datetime.fromtimestamp(self.timestamp)
        self.unit = unit
        self.rent = rent

        if beds == 'studio':
            self.beds = 0
        else:
            self.beds = float(re.search(r'^([\d.]+)\s', beds).group(1))
        self.baths = float(re.search(r'^([\d.]+)\s', baths).group(1))
        self.sqft = int(re.search(r'^([\d,]+)\s', sqft).group(1).replace(',', ''))

        self.building = None

    def __repr__(self):
        attrs = (
            self.unit,
            f'{self.beds}B/{self.baths}BR',
            f'{self.sqft}sqft'
        )
        return '<Listing: ' + ', '.join(attrs) + '>'

    @property
    def year(self):
        return self.dttime.year

    @property
    def month(self):
        return self.dttime.month

    def serialize(self):
        d = {
            'year': self.year,
            'month': self.month,
            'id': self.id,
            'bldg': self.building.name,
            'unit': self.unit,
            'beds': self.beds,
            'baths': self.baths,
            'sqft': self.sqft,
            'rent': self.rent,
        }
        return d


import os
import random
import re
from io import BytesIO
import PIL
from speak_and_listen import say, listen
import requests_html
from PIL import Image
# Quita el error por no verificar el certificado, linea 59 (funcion mostrar imagen)
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Global variables
p1_Wins = 0
p2_Wins = 0
rounds_played = 0

def get_categories(session):
    site_home = session.get("https://www.coolmod.com/")
    return site_home.html.find(".subfamilyheadertittle")

def let_user_choose_category():
    category_dict = {"coolpc": 'https://www.coolmod.com/pcs-coolmod/',
                     "portátiles": 'https://www.coolmod.com/pcs-tablets-portatiles-portatiles/',
                     "PCs sobremesa": 'https://www.coolmod.com/pcs-tablets-portatiles-pcs-sobremesa/',
                     "componentes PC": 'https://www.coolmod.com/componentes-hardware-componentes-pc/'}

    say("Para esta primera ronda, puedes elegir categoría.")

    keys = []
    values = []
    for key, value in category_dict.items():
        keys.append(key)
        values.append(value)

    chosen_category_link = None
    while not chosen_category_link:
        os.system("cls")
        print("Categorías a elegir:\n" + ("\n".join(keys)) + "\n")  # Pendiente de arreglar
        user_input = listen()

        if user_input in keys:
            chosen_category = keys[keys.index(user_input)]
            say("Has elegido la categoría " + chosen_category)
            chosen_category_link = category_dict[chosen_category]
            os.system("cls")
            return chosen_category_link
        else:
            say("Lo siento, no te he entendido, repite tu elección por favor.")

def get_random_category_link(all_categories):
    category = random.choice(all_categories)

    while category.text == "Configura tu PC a Medida":
        category = random.choice(all_categories)
        # Ha tocado el configurador
    return list(category.links)[0]

def proccess_products(session, category_link):
    product_category_page = session.get(category_link)
    products = product_category_page.html.find(".productInfo")

    product = random.choice(products)

    # Get product name
    product_name = product.text.split("\n")[1]

    # Get product price
    product_price = product.text.split("\n")[-1]
    product_price = float(product_price.replace("€", "").replace(" ", "").replace(".", "").replace(",", "."))
    # Quitar el sinbolo € y los espacios. Arreglar el numero para convertirlo en float

    # Get product image
    img_src = product.html.split("\n")[5]
    product_image_link = re.findall("(https://[A-aZ-z./-9-0]+)", img_src)[0]

    return product_name, product_price, product_image_link

def listen_and_get_user_guess():
    user_guess = listen()
    try:
        user_guess = user_guess.replace(" €", "").replace("euros", "")\
            .replace(",", ".").replace(" con ", ".").replace(" coma ", ".")
        final_user_guess = float(user_guess)
        return final_user_guess
    except ValueError:
        say("Lo siento, no te he entendido. ¿Te importaría repetirlo?")
        return listen_and_get_user_guess()
    except AttributeError:
        say("Lo siento, no te he entendido. ¿Te importaría repetirlo?")
        return listen_and_get_user_guess()

def show_product_image(product_image_link, session):
    try:
        product_image_downloaded = session.get(product_image_link, verify=False)
        image = Image.open(BytesIO(product_image_downloaded.content))
        image.show()
    except PIL.UnidentifiedImageError:
        print("No se ha podido mostrar la imagen")
    pass

def ask_and_check_user_guess(product_name, product_image_link, session):
    print("Nombre del producto {}".format(product_name))
    show_product_image(product_image_link, session)
    say("El nombre del producto es {}".format(product_name))

    say("¿Cuanto vale? Jugador 1")
    p1_guess = listen_and_get_user_guess()

    say("¿Cuanto vale? Jugador 2")
    p2_guess = listen_and_get_user_guess()

    return p1_guess, p2_guess

def set_round_number():
    print("Cuantas rondas te gustaría jugar? Debe de ser un numero impar.")
    say("Cuantas rondas te gustaría jugar? Debe de ser un numero impar")
    numbers_in_words = ["cero", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve"]

    user_input = listen()
    try:
        user_input = user_input.split(" ")[0]  # In case input ~= 5 rondas
    except AttributeError:
        say("Lo siento, no te he entendido")
        return set_round_number()

    if user_input in numbers_in_words:
        user_input = numbers_in_words.index(user_input)
    elif user_input == "una":
        user_input = 1

    try:
        number_of_rounds = int(user_input)
    except (ValueError, AttributeError, TypeError):
        say("Lo siento, no te he entendido")
        return set_round_number()

    if (number_of_rounds % 2) == 0:  # El numero es par
        say(str(number_of_rounds) + " no es un numero impar")
        return set_round_number()

    print("La partida durará {} rondas.".format(number_of_rounds))
    say("La partida durará {} rondas.".format(number_of_rounds))
    return number_of_rounds

def pick_random_winner():
    global p1_Wins
    global p2_Wins

    if random.randint(1, 2) == 1:
        p1_Wins += 1
        print_round_winner("jugador 1")
        say("La ronda la gana el jugador 1.")
    else:
        p2_Wins += 1
        print_round_winner("jugador 2")
        say("La ronda la gana el jugador 2.")
    return

def determine_winner(p1_guess, p2_guess, product_price):
    global p1_Wins
    global p2_Wins
    global rounds_played

    p1_absolute_error = round(abs(p1_guess - product_price), 2)
    p2_absolute_error = round(abs(p2_guess - product_price), 2)

    if p1_absolute_error < p2_absolute_error:
        p1_Wins += 1
        os.system("cls")
        print_round_winner("jugador 1")
        say("El primer jugador se ha acercado más. Se ha quedado a {} euros".format(p1_absolute_error))
    elif p1_absolute_error > p2_absolute_error:
        p2_Wins += 1
        os.system("cls")
        print_round_winner("jugador 2")
        say("El segundo jugador se ha acercado más. Se ha quedado a {} euros".format(p2_absolute_error))
    else:  # If both errors are the same
        os.system("cls")
        say("Ambos jugadores se han quedado igual de cerca. Se elegirá un ganador aleatorio.")
        pick_random_winner()
    rounds_played += 1
    return

def print_title():
    title = "EL PRECIO JUSTO by Unai Iglesias"
    print("+" + "-" * len(title) + "+")
    print("|" + title + "|")
    print("+" + "-" * len(title) + "+")
    return

def print_round_counter_and_scoreboard(number_of_rounds):
    global p1_Wins
    global p2_Wins
    global rounds_played
    content = "Ronda " + str(rounds_played + 1) + " / " + str(number_of_rounds)
    scoreboard = "J1 {} --- {} J2".format(p1_Wins, p2_Wins)
    print("+" + "-" * len(content) + "+")
    print("|" + content + "|" + "    " + scoreboard)
    print("+" + "-" * len(content) + "+")
    return

def print_round_winner(round_winner):
    content = "El " + round_winner + " ha ganado esta ronda"
    scoreboard = "J1 {} --- {} J2".format(p1_Wins, p2_Wins)
    print("+" + "-" * len(content) + "+")
    print("|" + content + "|" + "    " + scoreboard)
    print("+" + "-" * len(content) + "+")
    return

def print_winner(winner):
    content = "Ha ganado el " + winner + " ¡Felicidades!"
    scoreboard = "J1 {} --- {} J2".format(p1_Wins, p2_Wins)
    print("+" + "-" * len(content) + "+")
    print("|" + content + "|" + "    " + scoreboard)
    print("+" + "-" * len(content) + "+")
    return

def main():
    # Initialization
    global p1_Wins
    global p2_Wins
    global rounds_played
    os.system("cls")
    session = requests_html.HTMLSession()
    winner = None
    print_title()
    say("Bienvenidos a El precio justo, vamos a adivinar el precio de algunos productos")

    all_categories = get_categories(session)

    number_of_rounds = set_round_number()

    # End of initialization

    # Main game loop
    while rounds_played < number_of_rounds:
        os.system("cls")

        if rounds_played == 0:  # User will choose category for the first round
            category_link = let_user_choose_category()
        else:
            category_link = get_random_category_link(all_categories)

        print_round_counter_and_scoreboard(number_of_rounds)

        product_name, product_price, product_image_link = proccess_products(session, category_link)

        # Ask for guesses
        p1_guess, p2_guess = ask_and_check_user_guess(product_name, product_image_link, session)

        # Reveal real product price
        product_price_adapted_to_speech = str(round(product_price, 2)).replace(".", "coma")
        say("El precio era {} euros".format(product_price_adapted_to_speech))
        print("El precio era {} €.".format(round(product_price, 2)))

        determine_winner(p1_guess, p2_guess, product_price)

        if rounds_played != number_of_rounds:
            say("Siguiente ronda.")

    if p1_Wins > p2_Wins:
        winner = "primer jugador"
        os.system("cls")
    elif p2_Wins > p1_Wins:
        winner = "segundo jugador"
        os.system("cls")

    print_winner(winner)

    say("Ha ganado el {}. ¡Felicidades!".format(winner))
    os.system("pause")

if __name__ == "__main__":
    main()


import logging
from datetime import datetime
from itertools import product

from hypothesis import strategies as s, given, settings
from hypothesis.stateful import RuleBasedStateMachine, rule, Bundle
from hypothesis.stateful import run_state_machine_as_test

from xoeuf.osv import expression as expr
from xoeuf.osv import ql
from xoeuf.osv.expression import Domain

from xotl.tools.future.collections import opendict

from odoo.osv import expression as odoo_expr
from odoo.tests.common import TransactionCase, BaseCase

names = s.text(alphabet="abdefgh", min_size=1, max_size=5)
operators = s.sampled_from(["=", "!=", "<", ">", "<>"])
all_operators = s.sampled_from(
    ["=", "!=", "<", ">", "<>", "like", "ilike", "not like", "not ilike"]
)
ages = s.integers(min_value=0, max_value=120)
sensible_values = s.integers(min_value=-10, max_value=10)

# Logical connectors with the amount of terms it connects.  Notice that ''
# takes two arguments because it's the same as '&'.
connectors = s.sampled_from([("", 2), ("&", 2), ("!", 1), ("|", 2)])

logger = logging.getLogger(__name__)

@s.composite
def terms(draw, fields=None, values=None):
    if not values:
        values = sensible_values
    f = fields or names
    name = str(draw(f))
    operator = draw(operators)
    value = draw(values)
    return (name, operator, value)

@s.composite
def domains(draw, fields=None, min_size=1, max_size=10):
    leaves = draw(s.lists(terms(fields), min_size=min_size, max_size=max_size))
    result = []
    while leaves:
        connector, many = draw(connectors)
        if many <= len(leaves):
            if connector:
                result.append(connector)
            result.extend(leaves[:many])
            leaves = leaves[many:]
        elif len(leaves) == 1:
            result.append(leaves.pop(0))
    return Domain(result)

class TestDomain(BaseCase):
    @given(domains())
    def test_first_normal_form_idempotency(self, domain):
        self.assertEqual(
            domain.first_normal_form, domain.first_normal_form.first_normal_form
        )

    @given(domains())
    def test_second_normal_form_idempotency(self, domain):
        self.assertEqual(
            domain.second_normal_form, domain.second_normal_form.second_normal_form
        )

    @given(domains())
    def test_versions_equivalency(self, domain):
        # Any domain must be equivalent in all its versions.
        all_versions = [
            domain,
            domain.simplified,
            domain.first_normal_form,
            domain.second_normal_form,
        ]
        for version1, version2 in product(all_versions, all_versions):
            self.assertEqual(version1, version2, msg="%r != %r" % (version1, version2))

    @settings(deadline=1000)
    @given(domains())
    def test_simplified(self, domain):
        # A simplified domain never start with '&' operator.
        self.assertNotEqual(
            domain.simplified[0],
            expr.AND_OPERATOR,
            msg="%r start with &" % domain.simplified,
        )
        # The simplified domain must be the shortest.
        all_versions = [domain, domain.first_normal_form, domain.second_normal_form]
        for version in all_versions:
            self.assertLessEqual(
                len(domain.simplified),
                len(version),
                msg="len(%r) > len(%r)" % (domain.simplified, version),
            )
        self.assertTrue(
            all(
                odoo_expr.is_leaf(term) or odoo_expr.is_operator(term)
                for term in domain.simplified
            )
        )

    @given(domains())
    def test_simplified_idempotency(self, domain):
        self.assertEqual(domain.simplified, domain.simplified.simplified)

    @given(s.lists(domains(), min_size=2, max_size=10))
    def test_expr_replacement(self, domains):
        expr.AND(domains)
        odoo_expr.AND(domains)

    def test_eq(self):
        A = Domain([("field_x", "=", 1)])
        # A == A
        self.assertTrue(A == A)

        # ~A == ~A
        self.assertTrue(~A == ~A)

        # (x = 1)  == ! (x <> 1)
        A1 = Domain(["!", ("field_x", "<>", 1)])
        self.assertTrue(A == A1)

    def test_implies(self):
        # Basic implications.
        # -------------------

        A = Domain([("field_x", "=", 1)])
        B = Domain([("field_y", "!=", False)])

        # A => A
        self.assertTrue(A.implies(A))

        # A => A | B
        self.assertTrue(A.implies(A | B))

        # A & B => A
        self.assertTrue((A & B).implies(A))

        # A & B => A | B
        self.assertTrue((A & B).implies(A | B))

        self.assertEqual(Domain.AND(list(A), B), (A & B))
        self.assertEqual(expr.AND([list(A), B]), (A & B))

        # Inter terms implications.
        # -------------------------

        # (x = 1)  => (x = 1)
        A = Domain([("field_x", "=", 1)])
        A1 = Domain([("field_x", "=", 1)])
        self.assertTrue(A.implies(A1))

        # (x != 1)  => (x <> 1)
        A = Domain([("field_x", "!=", 1)])
        A1 = Domain([("field_x", "<>", 1)])
        self.assertTrue(A.implies(A1))

        # (x > 1) => (x > 0)
        A = Domain([("field_x", ">", 10)])
        A1 = Domain([("field_x", ">", 1)])
        self.assertTrue(A.implies(A1))

        # (x in (1,2,3)) => (x in (1,2,3,4))
        A = Domain([("field_x", "in", (1, 2, 3))])
        A1 = Domain([("field_x", "in", (1, 2, 3, 4))])
        self.assertTrue(A.implies(A1))
        #

        # Compound implications.
        # ----------------------
        # A => B & C iff (A => B) & (A => C)
        A = Domain([("field_x", "in", (1, 4))])
        B = Domain([("field_x", "in", (1, 2, 4))])
        C = Domain([("field_x", "in", (1, 3, 4))])
        self.assertTrue(A.implies(B))
        self.assertTrue(A.implies(C))
        self.assertTrue(A.implies(B & C))

        # More complex implications
        # -------------------------

        x = Domain(
            [
                "|",
                ("field_y", "!=", False),
                ("field_x", "=", "value"),
                ("field_z", "in", (1,)),
            ]
        )
        y = Domain(
            [
                "|",
                ("field_y", "!=", False),
                ("field_x", "=", "value"),
                ("field_z", "in", (1, 2, 3)),
                "|",
                ("field_x", "=", "value"),
                ("field_y", "!=", False),
            ]
        )
        assert x.implies(y)

        x = Domain(
            [
                ("field_y", "!=", False),
                ("field_z", "in", (1,)),
                ("field_z", "in", (1, 2)),
                "|",
                ("field_y", "!=", False),
                "|",
                ("field_z", "in", (1,)),
                ("field_z", "in", (2,)),
            ]
        )
        y = Domain([("field_y", "!=", False), ("field_z", "in", (1, 2, 3))])
        assert x.implies(y)

    def test_walk(self):
        y = Domain(
            [
                "|",
                ("a", "!=", False),
                ("b", "=", "value"),
                ("c", "in", (1, 2, 3)),
                "|",
                "|",
                "!",
                ("d", "=", "value"),
                ("f", "!=", False),
                ("g", "=", "h"),
            ]
        )
        expected = [
            ("TERM", ("g", "=", "h")),
            ("TERM", ("f", "!=", False)),
            ("TERM", ("d", "!=", "value")),
            ("OPERATOR", "|"),
            ("OPERATOR", "|"),
            ("TERM", ("c", "in", (1, 2, 3))),
            ("TERM", ("b", "=", "value")),
            ("TERM", ("a", "!=", False)),
            ("OPERATOR", "|"),
            ("OPERATOR", "&"),
            ("OPERATOR", "&"),
        ]
        self.assertEqual(expected, list(y.walk()))

class TestDomainFilter(BaseCase):
    def test_get_filter_ast_simple_one_term_with_in(self):
        self.assertEqual(
            Domain([("state", "in", [1, 2])])._get_filter_ast(),
            ql.Expression(
                ql.Lambda(
                    ql.make_arguments("this"),
                    expr._constructor_in("this", "state", [1, 2]),
                )
            ),
        )

    def test_empty_domain(self):
        self.assertTrue(Domain([]).asfilter()(0))

    def test_regression_asfilter_with_datetime(self):
        objects = [opendict(date_from=datetime(2020, 6, 5))]
        domain = Domain([("date_from", ">=", datetime(2020, 1, 1))])
        list(filter(domain.asfilter(), objects))

    def assertASTEqual(self, e1, e2):
        self.assertEqual(
            e1,
            e2,
            msg=f"Different ASTs:\n{e1!s}\n **** is not equal to ****\n {e2!s}\n",
        )

    def test_asfilter_equal_false(self):
        for query_operator, python_expr in (("=", "not"), ("!=", "")):
            query = Domain([("attr", query_operator, False)])
            ast = query._get_filter_ast()
            expected = ql.parse(f"lambda this: {python_expr} this.attr")
            self.assertASTEqual(ast, expected)

    def test_asfilter_equal_false_with_mapped_filtered(self):
        for query_operator, python_expr in (("=", "not"), ("!=", "")):
            query = Domain([("parent.attr", query_operator, False)])
            ast = query._get_filter_ast()
            expected = ql.parse(
                f"lambda this: this.mapped('parent').filtered(lambda x: {python_expr} x.attr)"
            )
            self.assertASTEqual(ast, expected)

    def test_asfilter_equal_false_with_convert_false_unset(self):
        for query_operator, python_operator in (("=", "=="), ("!=", "!=")):
            query = Domain([("attr", query_operator, False)])
            ast = query._get_filter_ast(convert_false=False)
            expected = ql.parse(f"lambda this: this.attr {python_operator} False")
            self.assertASTEqual(ast, expected)

    def test_asfilter_equal_false_with_convert_false_unset_mapped_filtered(self):
        for query_operator, python_operator in (("=", "=="), ("!=", "!=")):
            query = Domain([("parent.attr", query_operator, False)])
            ast = query._get_filter_ast(convert_false=False)
            expected = ql.parse(
                f"lambda this: this.mapped('parent').filtered(lambda x: x.attr {python_operator} False)"  # noqa
            )
            self.assertASTEqual(ast, expected)

    def test_asfilter_equal_none(self):
        for query_operator, python_expr in (("=", "not"), ("!=", "")):
            query = Domain([("attr", query_operator, None)])
            ast = query._get_filter_ast(convert_none=True)
            expected = ql.parse(f"lambda this: {python_expr} this.attr")
            self.assertASTEqual(ast, expected)

    def test_asfilter_equal_none_with_convert_none_unset(self):
        for query_operator, python_operator in (("=", "is"), ("!=", "is not")):
            query = Domain([("attr", query_operator, None)])
            ast = query._get_filter_ast()
            expected = ql.parse(f"lambda this: this.attr {python_operator} None")
            self.assertASTEqual(ast, expected)

    def test_asfilter_equal_none_with_mapped_filtered(self):
        for query_operator, python_operator in (("=", "is"), ("!=", "is not")):
            query = Domain([("parent.attr", query_operator, None)])
            ast = query._get_filter_ast()
            expected = ql.parse(
                f"lambda this: this.mapped('parent').filtered(lambda x: x.attr {python_operator} None)"  # noqa
            )
            self.assertASTEqual(ast, expected)

def get_model_domain_machine(this):
    Model = this.env["test_domain.model"]

    class ModelDomainMachine(RuleBasedStateMachine):
        objects = Bundle("objects")

        @rule(target=objects, name=names, age=ages)
        def create_object(self, name, age):
            logger.info("Creating object name: %s, age: %s", name, age)
            return Model.create({"name": name, "age": age})

        @rule(target=objects, name=names, age=ages, parent=objects)
        def create_child_object(self, name, age, parent):
            return Model.create({"name": name, "age": age, "parent_id": parent.id})

        @rule(age=ages, op=operators)
        def find_by_age(self, age, op):
            query = Domain([("age", op, age)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(
            age=ages,
            op=operators,
            path=s.lists(
                s.sampled_from(["parent_id", "children_ids"]), min_size=1, max_size=4
            ),
        )
        def find_by_parent_age(self, age, op, path):
            attr = ".".join(path) + ".age"
            query = Domain([(attr, op, age)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(ages=s.lists(ages))
        def find_by_ages(self, ages):
            query = Domain([("age", "in", ages)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(
            ages=s.lists(ages),
            path=s.lists(
                s.sampled_from(["parent_id", "children_ids"]), min_size=1, max_size=4
            ),
        )
        def find_by_parent_ages(self, ages, path):
            attr = ".".join(path) + ".age"
            query = Domain([(attr, "in", ages)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(ages=s.lists(ages))
        def find_by_not_ages(self, ages):
            query = Domain([("age", "not in", ages)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(
            ages=s.lists(ages),
            path=s.lists(
                s.sampled_from(["parent_id", "children_ids"]), min_size=1, max_size=4
            ),
        )
        def find_by_path_not_ages(self, ages, path):
            attr = ".".join(path) + ".age"
            query = Domain([(attr, "not in", ages)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(domain=domains(fields=s.just("age")))
        def find_by_arbitrary_domain(self, domain):
            res = Model.search(domain)
            logger.info("Check filter/domain: %s; count: %s", domain, len(res))
            this.assertEqualRecordset(res.filtered(domain.asfilter()), res)

        @rule(name=names, op=all_operators)
        def find_by_name(self, name, op):
            query = Domain([("name", op, name)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(
            name=names,
            op=all_operators,
            path=s.lists(
                s.sampled_from(["parent_id", "children_ids"]), min_size=1, max_size=4
            ),
        )
        def find_by_path_name(self, name, op, path):
            attr = ".".join(path) + ".name"
            query = Domain([(attr, op, name)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(names=s.lists(names))
        def find_by_names(self, names):
            query = Domain([("name", "in", names)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(
            names=s.lists(names),
            path=s.lists(
                s.sampled_from(["parent_id", "children_ids"]), min_size=1, max_size=4
            ),
        )
        def find_by_path_names(self, names, path):
            attr = ".".join(path) + ".name"
            query = Domain([(attr, "in", names)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(names=s.lists(names))
        def find_by_not_names(self, names):
            query = Domain([("name", "not in", names)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

        @rule(
            names=s.lists(names),
            path=s.lists(
                s.sampled_from(["parent_id", "children_ids"]), min_size=1, max_size=4
            ),
        )
        def find_by_path_not_names(self, names, path):
            attr = ".".join(path) + ".name"
            query = Domain([(attr, "not in", names)])
            res = Model.search(query)
            logger.info("Check filter/domain: %s; count: %s", query, len(res))
            this.assertEqualRecordset(res.filtered(query.asfilter()), res)

    return ModelDomainMachine

class TestConsistencyOfFilters(TransactionCase):
    def assertEqualRecordset(self, rs1, rs2):
        ours = rs1 - rs2
        theirs = rs2 - rs1
        self.assertEqual(
            rs1, rs2, msg="ours: {0!r}; theirs: {1!r}".format(ours, theirs)
        )

    def test_consistency_of_domains(self):
        run_state_machine_as_test(get_model_domain_machine(self))


import os
import gzip
import shutil
import struct
import urllib

"""
The Code is moved from the Standford Tensorflow Course.
Some modifications are made for correction.
"""

os.environ['TF_CPP_MIN_LOG_LEVEL']='2' # to avoid the warning that CPU capability is not fully exploited

# from matplotlib import pyplot as plt
import numpy as np
import tensorflow as tf

from PIL import Image, ImageOps

def safe_mkdir(path):
    """ Create a directory if there isn't one already. """
    try:
        os.mkdir(path)
    except OSError:
        pass

def download_one_file(download_url,
                    local_dest,
                    expected_byte=None,
                    unzip_and_remove=False):
    """
    Download the file from download_url into local_dest
    if the file doesn't already exists.
    If expected_byte is provided, check if
    the downloaded file has the same number of bytes.
    If unzip_and_remove is True, unzip the file and remove the zip file
    """
    # To avoid the possible back-slash of '\' in the url which may lead to 404 Error
    download_url = download_url.replace('\\', '/')

    if os.path.exists(local_dest) or os.path.exists(local_dest[:-3]):
        print('%s already exists' %local_dest)
    else:
        print('Downloading %s' %download_url)
        local_file, _ = urllib.request.urlretrieve(download_url, local_dest)
        file_stat = os.stat(local_dest)
        if expected_byte:
            if file_stat.st_size == expected_byte:
                print('Successfully downloaded %s' %local_dest)
                if unzip_and_remove:
                    with gzip.open(local_dest, 'rb') as f_in, open(local_dest[:-3],'wb') as f_out:
                        shutil.copyfileobj(f_in, f_out)
                    os.remove(local_dest)
            else:
                print('The downloaded file has unexpected number of bytes')

def download_mnist(path):
    """
    Download and unzip the dataset mnist if it's not already downloaded
    Download from http://yann.lecun.com/exdb/mnist
    """
    safe_mkdir(path)
    url = 'http://yann.lecun.com/exdb/mnist'
    filenames = ['train-images-idx3-ubyte.gz',
                'train-labels-idx1-ubyte.gz',
                't10k-images-idx3-ubyte.gz',
                't10k-labels-idx1-ubyte.gz']
    expected_bytes = [9912422, 28881, 1648877, 4542]

    for filename, byte in zip(filenames, expected_bytes):
        download_url = os.path.join(url, filename)
        local_dest = os.path.join(path, filename)
        download_one_file(download_url, local_dest, byte, True)

def parse_data(path, dataset, flatten):
    if dataset != 'train' and dataset != 't10k':
        raise NameError('dataset must be train or t10k')

    label_file = os.path.join(path, dataset + '-labels-idx1-ubyte')
    with open(label_file, 'rb') as file:
        _, num = struct.unpack(">II", file.read(8))
        labels = np.fromfile(file, dtype=np.int8) #int8
        new_labels = np.zeros((num, 10))
        new_labels[np.arange(num), labels] = 1

    img_file = os.path.join(path, dataset + '-images-idx3-ubyte')
    with open(img_file, 'rb') as file:
        _, num, rows, cols = struct.unpack(">IIII", file.read(16))
        imgs = np.fromfile(file, dtype=np.uint8).reshape(num, rows, cols) #uint8
        imgs = imgs.astype(np.float32) / 255.0
        if flatten:
            imgs = imgs.reshape([num, -1])

    return imgs, new_labels

def read_mnist(path, flatten=True, num_train=55000):
    """
    Read in the mnist dataset, given that the data is stored in path
    Return three tuples of numpy arrays
    ((train_imgs, train_labels), (val_images, val_labels), (test_imgs, test_labels))
    """
    imgs, labels = parse_data(path, 'train', flatten)
    indices = np.random.permutation(labels.shape[0])
    train_idx, val_idx = indices[:num_train], indices[num_train:]
    train_img, train_labels = imgs[train_idx, :], labels[train_idx, :]
    val_img, val_labels = imgs[val_idx, :], labels[val_idx, :]
    test = parse_data(path, 't10k', flatten)
    return (train_img, train_labels), (val_img, val_labels), test

def get_resized_image(img_path, width, height, save=True):
    image = Image.open(img_path)
    # PIL is column major so you have to swap the places of width and height
    image = ImageOps.fit(image, (width, height), Image.ANTIALIAS)
    if save:
        image_dirs = img_path.split('/')
        image_dirs[-1] = 'resized_' + image_dirs[-1]
        out_path = '/'.join(image_dirs)
        if not os.path.exists(out_path):
            image.save(out_path)
    image = np.asarray(image, np.float32)
    return np.expand_dims(image, 0)

def generate_noise_image(content_image, width, height, noise_ratio=0.6):
    noise_image = np.random.uniform(-20, 20, (1, height, width, 3)).astype(np.float32)
    return noise_image * noise_ratio + content_image*(1-noise_ratio)


#单元测试用于核实函数的某个方面没有问题
#测试用例是一组单元测试，这些单元测试一起核实函数在各种情形下都符合要求
#全覆盖式测试用例包含一整套单元测试
"""
	测试未通过时，不要修改测试，而应修复导致测试不能通过的代码；
	检查刚对函数所做的修改，找出导致函数行为不符合预期的修改。
"""

import unittest
#Python标准库中模块unittest提供了代码测试工具
from name_function import get_formatted_name

class NamsTestCase(unittest.TestCase):#必须继承unittest.TestCase类
	"""测试name_function.py"""

	def test_first_last_name(self):#单元测试，test_打头的方法自动运行
		"""测试能否正确处理像Janis Joplin这样的姓名"""

		formatted_name = get_formatted_name('janis','joplin')
		self.assertEqual(formatted_name,'Janis Joplin')
		#断言方法：用来核实得到的结果是否与期望的结果一致

	def test_first_middle_last_name(self):
		"""测试能否正确处理想Janis A Joplin这样的姓名"""

		formatted_name = get_formatted_name('janis','joplin','A')
		self.assertEqual(formatted_name,'Janis A Joplin')

unittest.main()
#代码行unittest.main()让Python运行这个文件中的测试


import argparse
import unittest
import json
import logging
import time

from ansible_integration_test import AnsibleIntegrationTest
from girder_client import HttpError

class EC2VolumeIntegrationTest(AnsibleIntegrationTest):

    def __init__(self, name, girder_url, girder_user, girder_password, aws_access_key_id,
                  aws_secret_access_key):
        super(EC2VolumeIntegrationTest, self).__init__(
            name, girder_url, girder_user, girder_password, aws_access_key_id,
            aws_secret_access_key)
        self._volume_id = None
        self._profile_id = None
        self._cluster_id = None

    def tearDown(self):
        super(EC2VolumeIntegrationTest, self).tearDown()
        if self._volume_id:
            # Try to detach to volume
            try:
                volume_url = 'volumes/%s/detach' % self._volume_id
                r = self._client.put(volume_url)

                volume_status_url = 'volumes/%s/status' % self._volume_id
                self._wait_for_status(volume_status_url, 'available', timeout=60)

            except HttpError as error:
                # if it doesn't exist continue
                if error.status != 400:
                    raise error

            # Try to delete the volume
            try:
                volume_url = 'volumes/%s' % self._volume_id
                r = self._client.delete(volume_url)
            except HttpError as error:
                # if it doesn't exist continue
                if error.status != 400:
                    raise error

    def create_volume(self):
        volume_url = 'volumes'
        body =  {
            "name": "testVolume",
            "profileId": self._profile_id,
            "size": 11,
            "type": "ebs",
            "zone": "us-west-2a"
        }

        r = self._client.post(volume_url, data=json.dumps(body))
        self._volume_id = r['_id']

    def attach_volume(self):
        attach_url = 'volumes/%s/clusters/%s/attach' % (self._volume_id, self._cluster_id)
        body = {
            'path': '/mnt/data/'
        }

        r = self._client.put(attach_url, data=json.dumps(body))

        volume_status_url = 'volumes/%s/status' % self._volume_id
        self._wait_for_status(volume_status_url, 'in-use', timeout=60)

    def detach_volume(self):
        volume_url = 'volumes/%s/detach' % self._volume_id
        r = self._client.put(volume_url)

        volume_status_url = 'volumes/%s/status' % self._volume_id
        self._wait_for_status(volume_status_url, 'available', timeout=60)

    def delete_volume(self):
        volume_url = 'volumes/%s' % self._volume_id
        r = self._client.delete(volume_url)

        self.assertEquals(r['status'], 'deleting')

        start = time.time();
        while True:
            time.sleep(1)
            try:
                r = self._client.get("/volumes/%s" % self._volume_id)
            except HttpError as error:
                if error.status == 400:
                    break
                else:
                    raise error

            if time.time() - start > 60:
                self.fail('Volume never moved out of status: %s' % r['ec2']['status'])

    def test(self):
        try:
            t0 = time.time()
            self.create_profile()
            logging.info("Finished creating profile (%s)" % (time.time() - t0)); t0 = time.time()
            self.create_cluster()
            logging.info("Finished creating cluster (%s)" % (time.time() - t0)); t0 = time.time()
            self.launch_cluster()
            logging.info("Finished launching cluster (%s)" % (time.time() - t0)); t0 = time.time()

            self.create_volume()
            logging.info("Finished creating volume (%s)" % (time.time() - t0)); t0 = time.time()

            self.attach_volume()
            logging.info("Finished attaching volume (%s)" % (time.time() - t0)); t0 = time.time()

            self.detach_volume()
            logging.info("Finished detaching volume (%s)" % (time.time() - t0)); t0 = time.time()

            self.delete_volume()
            logging.info("Finished deleting volume (%s)" % (time.time() - t0)); t0 = time.time()

            self.terminate_cluster()
            logging.info("Finished terminating cluster (%s)" % (time.time() - t0)); t0 = time.time()
        except HttpError as error:
            self.fail(error.responseText)
        except KeyboardInterrupt:
            self.fail("Interupted!")

if __name__ == '__main__':
    parser = argparse.ArgumentParser(parents=[AnsibleIntegrationTest.parser])
    parser.add_argument('-i', '--aws_access_key_id', help='', required=True)
    parser.add_argument('-a', '--aws_secret_access_key', help='', required=True)
    parser.add_argument('-l', '--loglevel', help='', required=False, default="INFO",
                        choices=["INFO", "WARNING", "ERROR", "DEBUG"])

    args = parser.parse_args()

    logging.basicConfig(level=(getattr(logging, args.loglevel)))
    logging.getLogger("requests").setLevel(logging.WARNING)

    suite = unittest.TestSuite()
    suite.addTest(EC2VolumeIntegrationTest('test', args.girder_url, args.girder_user,
        args.girder_password, args.aws_access_key_id, args.aws_secret_access_key))
    unittest.TextTestRunner().run(suite)


""" Tools for calorimeter cluster manipulations

    Contributors:
        - Vitaly Vorobyev (vit.vorobiev@gmail.com)

    Created: July 2020
    Modified: August 2020
"""

from typing import NamedTuple

import jax.numpy as np
import jax

from .cartesian import Position, Momentum

dtype = np.ndarray

class Cluster(NamedTuple):
    """ Calorimeter cluster """
    energy: dtype  # energy
    costh: dtype  # cosine of the polar angle
    phi: dtype  # azimuth angle

    @staticmethod
    def from_ndarray(data: np.ndarray):
        """ """
        assert data.shape[1] == 3
        return Cluster(*[data[:, i] for i in range(3)])

    @property
    def as_array(self) -> (np.ndarray):
        return np.column_stack([self.energy, self.costh, self.phi])

    @property
    def sinth(self):
        """ Sine of the polar angle """
        return np.sqrt(1 - self.costh**2)

def cartesian_to_cluster(pos: Position, mom: Momentum, R=1000) -> (Cluster):
    """ Make cluster from Position and Momentum """
    # Find intersection with calorimeter (see the geometry sketch)
    pos_mom_scalar_product = pos.x*mom.px + pos.y*mom.py + pos.z*mom.pz
    mom_total = mom.ptot
    mom_total_squared = mom_total**2

    full_computation = False
    if full_computation:
        alpha = (-pos_mom_scalar_product + np.sqrt(
            pos_mom_scalar_product**2 + mom_total_squared * (R**2 - pos.r**2)
        )) / mom_total_squared
    else:  # takes into account that R >> particle flight length
        alpha = R / mom_total - pos_mom_scalar_product / mom_total_squared

    cluster_position = Position.from_ndarray(
        pos.as_array + alpha.reshape(-1, 1) * mom.as_array)

    return Cluster.from_ndarray(np.column_stack([
        mom_total, cluster_position.costh, cluster_position.phi
    ]))

def momentum_from_cluster(clu: Cluster) -> (Momentum):
    """ Assuming photon (massless particle) """
    sinth = clu.sinth
    return Momentum.from_ndarray(np.column_stack([
        clu.energy * sinth * np.sin(clu.phi),
        clu.energy * sinth * np.cos(clu.phi),
        clu.energy * clu.costh,
    ]))

momentum_from_cluster_jacobian = jax.vmap(jax.jacfwd(momentum_from_cluster))


import os
import warnings

import time
import tqdm
import json
import tempfile
import argparse

from torch.nn import Conv2d
from torch.nn.modules.conv import _ConvNd

from ...utils.runtime import get_instance
from ...utils.io import load, write_file

def enumerate_snapshots(path, ext='.gz'):
    path, _, snapshots = next(os.walk(os.path.abspath(path)))
    for basename, extension in map(os.path.splitext, snapshots):
        if extension != ext:
            continue

        # check file size
        snapshot = os.path.join(path, basename + ext)
        if os.stat(snapshot).st_size < 1:
            continue

        yield snapshot

def conv_filters(module, cls=Conv2d, prefix=''):
    """Collect filters from the convolutional layers of the specified model."""
    assert issubclass(cls, _ConvNd)

    for name, mod in module.named_modules(prefix=prefix):
        if not isinstance(mod, cls):
            continue

        yield name, mod.weight.cpu().detach().clone()

def main(root, tag='', force=False, debug=False):
    breakpoint() if debug else None

    primary_filename, *filenames = enumerate_snapshots(root)

    # the first snapshot becomes the primary model and is processed separately
    snapshot = load(primary_filename)
    master = snapshot['config']

    # the pack is created next to `root` under a similar name
    target = os.path.dirname(primary_filename)
    target = os.path.join(
        os.path.dirname(target),
        'kernels__' + os.path.basename(target)
        + ('__' if tag else '') + tag
    )
    os.makedirs(target, exist_ok=force)
    if force:
        # ensure that the directory being overwritten is empty.
        pass

    # load the model
    model = get_instance(**master['model'])
    model.load_state_dict(snapshot['state'], strict=True)

    # create tensor storage vault and commit the master model
    vault, sources = {}, [primary_filename]
    for name, weight in conv_filters(model):
        fid, vault[name] = tempfile.mkstemp(
            dir=target, prefix='v', suffix='.bin')
        write_file(weight, fid, save_size=False)
        os.close(fid)

    # check that all models have exactly the same config and commit them
    for filename in tqdm.tqdm(filenames, desc='fetching datasets'):
        snapshot = load(filename)
        if snapshot['config'] != master:
            # does not match the primary model
            warnings.warn(f'Config of `{filename}` does not match the'
                          f' the primary model `{primary_filename}`.',
                          RuntimeWarning)
            continue

        model.load_state_dict(snapshot['state'], strict=True)
        for name, weight in conv_filters(model):
            write_file(weight, open(vault[name], 'ab'), save_size=False)

        sources.append(filename)

    # make vault paths relative and compile meta info (from the last model)
    meta, filters = {}, list(conv_filters(model))
    common = os.path.commonpath([target, *vault.values()])
    for name, weight in tqdm.tqdm(filters, desc='saving datasets'):
        meta[name] = {
            'shape': (len(sources), *weight.shape),
            'dtype': repr(weight.dtype),
            'vault': os.path.normpath(vault[name].replace(common, '.'))
        }

    # the meta.json defines the parameters of the dataset
    with open(os.path.join(target, 'meta.json'), 'tw') as fout:
        json.dump({
            '__dttm__': time.strftime('%Y%m%d %H%M%S'),
            'dataset': meta,
            'config': master,
            'sources': sources,
        }, fout, indent=2)

parser = argparse.ArgumentParser(
    description='Preprocess model snapshots into kernel dataset, '
                'stored in an automatically generated directory.',
    add_help=True)

parser.add_argument(
    'root', type=str,
    help='The directory with stored model snapshots.')

parser.add_argument(
    '--tag', type=str, required=False,
    help='Optional suffix for the name of the dataset.')

parser.add_argument(
    '--force', required=False, dest='force', action='store_true',
    help='Overwrite experiment report if it already exists.')

parser.add_argument(
    '--debug', required=False, dest='debug', action='store_true',
    help='Enter trace mode.')

parser.set_defaults(tag='', force=False, debug=False)

main(**vars(parser.parse_args()))


# This Python file uses the following encoding: utf-8

from unittest import TestCase
from pandocfilters import Para, Str, Space, Header, BulletList, Span, Strong, Plain, Link, RawBlock

import json

import pandoc_numbering

from helper import init, createLink, createMetaList, createMetaMap, createMetaInlines, createListStr, createMetaString

def getMeta1():
    return {
        'pandoc-numbering': createMetaList([
            createMetaMap({
                'category': createMetaInlines(u'exercise'),
                'listing': createMetaInlines(u'Listings of exercises'),
                'sectioning': createMetaInlines(u'-.+.')
            })
        ])
    }

def getMeta2():
    return {
        'pandoc-numbering': createMetaList([
            createMetaMap({
                'category': createMetaInlines(u'exercise'),
                'listing': createMetaInlines(u'Listings of exercises'),
                'sectioning': createMetaInlines(u'-.+.'),
                'tab': createMetaString(u'2'),
                'space': createMetaString(u'4'),
            })
        ])
    }

def getMeta3():
    return {
        'pandoc-numbering': createMetaList([
            createMetaMap({
                'category': createMetaInlines(u'exercise'),
                'listing': createMetaInlines(u'Listings of exercises'),
                'sectioning': createMetaInlines(u'-.+.'),
                'tab': createMetaString(u'a'),
                'space': createMetaString(u'b'),
            })
        ])
    }

def test_listing_classic():
    init()

    meta = getMeta1()

    src = Para(createListStr(u'Exercise #'))
    pandoc_numbering.numbering(src['t'], src['c'], '', meta)
    src = Para(createListStr(u'Exercise (test) #'))
    pandoc_numbering.numbering(src['t'], src['c'], '', meta)

    doc = [[{'unMeta': meta}], []]
    pandoc_numbering.addListings(doc, '', meta)

    dest = [
        Header(1, ['', ['unnumbered'], []], createListStr(u'Listings of exercises')),
	    BulletList([
	    	[Plain([createLink(['', [], []], createListStr(u'0.0.1 Exercise'), ['#exercise:0.0.1', ''])])],
	    	[Plain([createLink(['', [], []], createListStr(u'0.0.2 test'), ['#exercise:0.0.2', ''])])]
	    ])
	]

    assert json.loads(json.dumps(doc[1])) == json.loads(json.dumps(dest))

def listing_latex(meta, color, tab, space):
    init()

    src = Para(createListStr(u'Exercise #'))
    pandoc_numbering.numbering(src['t'], src['c'], 'latex', meta)
    src = Para(createListStr(u'Exercise (test) #'))
    pandoc_numbering.numbering(src['t'], src['c'], 'latex', meta)

    doc = [[{'unMeta': meta}], []]
    pandoc_numbering.addListings(doc, 'latex', meta)

    dest = [
        Header(1, ['', ['unnumbered'], []], createListStr(u'Listings of exercises')),
        RawBlock(
            'tex',
            ''.join([
                '\\hypersetup{linkcolor=' + color + '}',
                '\\makeatletter',
                '\\newcommand*\\l@exercise{\\@dottedtocline{1}{' + tab + 'em}{' + space + 'em}}',
                '\\@starttoc{exercise}',
                '\\makeatother'
            ])
        )
    ]

    assert json.loads(json.dumps(doc[1])) == json.loads(json.dumps(dest))

def test_listing_latex():
    listing_latex(getMeta1(), 'black', '1.5', '3.3')

def test_listing_latex_color():
    meta = getMeta1()
    meta['toccolor'] = createMetaInlines(u'blue')
    listing_latex(meta, 'blue', '1.5', '3.3')

def test_listing_latex_tab_space():
	listing_latex(getMeta2(), 'black', '2.0', '4.0')

def test_listing_latex_tab_space_error():
	listing_latex(getMeta3(), 'black', '1.5', '3.3')


# 对PM2.5值按年月两列进行统计分析,并使用分组柱状图可视化

import pandas as pd
import matplotlib.pyplot as plt

data_path = 'C:\\Users\\shine小小昱\\Desktop\\pm2.csv'
save_path = 'C:\\Users\\shine小小昱\\Desktop\\'

def collect_data():
    """
    数据获取
    :return:
    """
    f = open(data_path, encoding='utf-8')  # 中文文件名或中文路径必须这么打开
    data_df = pd.read_csv(f, header=0)  # 列名采用第一行
    return data_df

def inspect_data(data_df):
    """
    审查数据
    """
    print('数据预览：')
    print(data_df.head(10))

    print('数据基本信息：')
    print(data_df.info())

    print('数据内容统计：')
    print(data_df.describe())

def process_data(data_df):
    """
    数据处理
    """
    data_df.dropna(inplace=True)

    return data_df

def analyze_data(proc_df):
    """
    数据分析
    :return: 按年月多重分组后的df， 透视年列后的df
    """
    # 多重分组
    grpd_proc_df = proc_df.groupby(by=['Year', 'Month'])['PM'].mean()

    # pivot透视表                                     需横向透视列(不会变成列名)  透视列下需统计的值(无论个数必须是列表，否则不会变成列名)
    pvt_proc_df = pd.pivot_table(proc_df, index='Month', columns='Year', values=['PM'], aggfunc='mean')   # 层级列结构

    return grpd_proc_df, pvt_proc_df

def save_and_show(grpd_proc_df, pvt_proc_df):
    """
    结果展示与保存
    """
    grpd_proc_df.to_csv(save_path + '年月统计pm.csv', header=['mean pm'])
    pvt_proc_df.to_csv(save_path + '年透视图pm.csv')
    #  只有一个主列 可不指定列名
    pvt_proc_df['PM'].plot(kind='bar', title='PM2.5', rot=0)  # 数据本身只有一列值 但是因为有二层列 自动分组柱状图
    plt.ylabel('pm')
    plt.tight_layout()
    plt.savefig(save_path + 'pm分组柱状图.png')
    plt.show()

if __name__ == '__main__':
    all_data_df = collect_data()
    inspect_data(all_data_df)
    proc_data_df = process_data(all_data_df)
    grouped_proc_df, pivot_proc_df = analyze_data(proc_data_df)
    save_and_show(grouped_proc_df, pivot_proc_df)


'''
Created on Dec, 201

@author: Will Yzaguirr
'''
from flask import Flask, flash, render_template, request, session
import os
import json
from app import app

app.secret_key = os.urandom(12)

current_user = ""

'''set global variable user_info with a dictionary of usernames and
passwords'''
with open('users.json', 'r') as jsonfile:
    user_data = json.loads(jsonfile.read())
users = [users['username'] for users in user_data]
passwords = [passw['password'] for passw in user_data]
user_info = dict(zip(users, passwords))

'''creates 2 global variables, songs, which is the available songs
to add to playlist, and all_songs which is all the songs. Used for
getting full song info'''
with open('songs.json', 'r') as jsonfile:
    song_data = json.loads(jsonfile.read())
songs = [(info['songname'], info['artist'], info['year'], info['album_cover']) for info in song_data]
all_songs = songs

'''create global variable user_playlist which is a dictionary with users
and their playlist in song title form'''
temp = []
user_playlist = []
playlists = [songs['playlist'] for songs in user_data]
for userP in playlists:
    for sng in userP:
        for titles in songs:
            if sng in titles:
                temp.append(titles)
    user_playlist.append(temp)
    temp = []

user_playlist = dict(zip(users, user_playlist))

@app.route('/')
def home():
    '''if you're not logged in, return the login page. Otherwise,
    return the playlist display page'''
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        global current_user
        order = []
        for i in range(0, len(user_playlist.get(current_user))):
            order.append(i + 1)
        return render_template("user_playlist.html",
                               playlist_length=len(user_playlist.get(current_user)),
                               current_user=current_user,
                               playlist=user_playlist.get(current_user),
                               all_songs=songs,
                               order = order)

@app.route('/login', methods=['POST'])
def do_admin_login():
    '''gets submitted username and password and checks if they match
    a user'''
    global current_user
    current_user = request.form['username']
    if request.form['username'] in user_info.keys() and request.form['password'] == user_info.get(request.form['username']):
        session['logged_in'] = True
    else:
        flash('incorrect login')
    return home()

@app.route("/logout")
def logout():
    '''logs out user'''
    session['logged_in'] = False
    return home()

@app.route("/submit")
def submit_changes():
    '''when submit is clicked, perform actions given in dropdown menu'''
    print("Songs:", songs)
    print("User play:", user_playlist)
    '''get dictionary change_log of song and action(add, remove)'''
    if request.method == "GET":
        current_user = request.args.get("user_info")

        if request.args.get("move") != "":
            start, end = request.args.get("move").split("_")[1] , request.args.get("move").split("_")[2]
            print(start, end)
            temp = user_playlist.get(current_user)[int(start) - 1]
            user_playlist.get(current_user)[int(start) - 1] = user_playlist.get(current_user)[int(end) - 1]
            user_playlist.get(current_user)[int(end) - 1] = temp

        change_log = {}
        for song in songs:
            if request.args.get(song[0].split(" ")[0]) != "nothing":
                change_log[song[0]] = request.args.get(song[0].split(" ")[0])

        print("CHange:", change_log)
        print("Current pl:", user_playlist.get(current_user))

        #to_del holds the place of the songs in user_playlist to be deleted
        to_del = []
        for song, action in change_log.items():
            if action == "remove":
                for i in range(len(user_playlist.get(current_user))):
                    print(i)
                    if song in user_playlist.get(current_user)[i]:
                        to_del.append(i)
                del user_playlist.get(current_user)[to_del[0]]
            else:
                temp = []
                for music in songs:
                    if song in music:
                        temp.append(music)
                user_playlist.get(current_user).append(temp[0])
        print("Changed play:", user_playlist.get(current_user))

        '''creates playlist of song titles to be written to json'''

        new_json_playlist = []
        for song_iter in user_playlist.get(current_user):
            new_json_playlist.append(song_iter[0])

        '''rewrites the json file with updated playlist info'''

        filename = 'users.json'
        with open(filename, 'r') as json_file:
            data = json.load(json_file)
        for users in data:
            if users['username'] == current_user:
                users['playlist'] = new_json_playlist

        os.remove(filename)
        with open(filename, 'w') as json_file:
            json.dump(data, json_file, indent=4)

        order = []
        for i in range(0, len(user_playlist.get(current_user))):
            order.append(i + 1)

        return render_template("user_playlist.html",
                               playlist_length = len(user_playlist.get(current_user)),
                               current_user = current_user,
                               playlist = user_playlist.get(current_user),
                               all_songs = songs,
                               order = order)

    else:
        order = []
        for i in range(0, len(user_playlist.get(current_user))):
            order.append(i + 1)
        return render_template("user_playlist.html",
                               playlist_length = len(user_playlist.get(current_user)),
                               current_user = current_user,
                               playlist = user_playlist.get(current_user),
                               all_songs = songs,
                               order = order)


from typing import (
    Callable,
    Optional,
    Type,
    TypeVar,
)

E = TypeVar('E', bound=BaseException)
R = TypeVar('R')

def catch(f: Callable[..., R],
          *args,
          exception_cls_to_catch: Type[E] = Exception,
          **kwargs
          ) -> tuple[Optional[E], Optional[R]]:
    """
    Invoke the given callable. If the callable raises an instance of the
    specified exception class, return that exception, otherwise return the
    result of the callable.

    :param f: the callable to invoke

    :param args: positional arguments to the callable

    :param exception_cls_to_catch: class of exceptions to catch. The name is
           intentionally long in order to avoid collisions with the keyword
           arguments to the callable.

    :param kwargs: keyword arguments to the callable

    :return: Either a tuple of None and the return value of the callable or
             a tuple of the exception raised by the callable and None.

    >>> catch(int, '42')
    (None, 42)

    >>> catch(int, '42', base=16)
    (None, 66)

    >>> catch(int, '')
    (ValueError("invalid literal for int() with base 10: ''"), None)

    >>> catch(int, '', exception_cls_to_catch=ValueError)
    (ValueError("invalid literal for int() with base 10: ''"), None)

    >>> catch(int, '', exception_cls_to_catch=BaseException)
    (ValueError("invalid literal for int() with base 10: ''"), None)

    >>> catch(int, '', exception_cls_to_catch=NotImplementedError)
    Traceback (most recent call last):
    ...
    ValueError: invalid literal for int() with base 10: ''

    >>> catch(int, '', base=16)
    (ValueError("invalid literal for int() with base 16: ''"), None)

    >>> catch(int, '', base=16, exception_cls_to_catch=ValueError)
    (ValueError("invalid literal for int() with base 16: ''"), None)
    """
    try:
        return None, f(*args, **kwargs)
    except exception_cls_to_catch as e:
        return e, None


import contextlib
import signal
import sys

class ExitOnDoubleInterrupt(contextlib.AbstractContextManager):
    """A context manager which exits the application on SIGINT.

    The first interrupt waits for the operation to finish and then exits.
    The second interrupt exits immediately.
    """
    def __init__(self, message):
        self.message = message

    def __enter__(self):
        self.was_interrupted = False
        # swap interrupt handler for a custom one
        self.old_handler = signal.signal(signal.SIGINT, self.handler)
        return self

    def handler(self, sig, frame):
        # delay the first interrupt
        if not self.was_interrupted:
            self.was_interrupted = True
            print(self.message, file=sys.stderr)
        # propagate the second interrupt
        else:
            signal.signal(signal.SIGINT, self.old_handler)
            raise KeyboardInterrupt

    def __exit__(self, *excinfo):
        signal.signal(signal.SIGINT, self.old_handler)

        if self.was_interrupted:
            sys.exit()


from googlecloudsdk.calliope import base
from googlecloudsdk.calliope import exceptions as c_exc
from googlecloudsdk.core import log
from googlecloudsdk.core import properties

DETAILED_HELP = {
    'DESCRIPTION': '{description}',
    'EXAMPLES': """\
        To unset the project property in the core section, run:

          $ {command} project

        To unset the zone property in the compute section, run:

          $ {command} compute/zone
        """,
    '+AVAILABLE PROPERTIES': properties.VALUES.GetHelpString(),
}

class Unset(base.Command):
  """Erase Google Cloud SDK properties.

  Unset a property to be as if it were never defined in the first place. You
  may optionally use the --scope flag to specify a configuration file to update.
  """

  detailed_help = DETAILED_HELP

  @staticmethod
  def Args(parser):
    """Adds args for this command."""
    property_arg = parser.add_argument(
        'property',
        metavar='SECTION/PROPERTY',
        help='The property to be unset. Note that SECTION/ is optional while '
        'referring to properties in the core section.')
    property_arg.completer = Unset.group_class.PropertiesCompleter

    scope_args = parser.add_mutually_exclusive_group()
    Unset.group_class.DEPRECATED_SCOPE_FLAG.AddToParser(scope_args)
    Unset.group_class.INSTALLATION_FLAG.AddToParser(scope_args)

  def Run(self, args):
    """Runs this command."""
    if args.scope:
      log.err.Print('The `--scope` flag is deprecated.  Please run `gcloud '
                    'help topic configurations` and `gcloud help config '
                    'unset` for more information.')

    prop = properties.FromString(args.property)
    if not prop:
      raise c_exc.InvalidArgumentException(
          'property', 'Must be in the form: [SECTION/]PROPERTY')
    properties.PersistProperty(
        prop, None, scope=Unset.group_class.RequestedScope(args))


import os
from os import listdir
from os.path import isfile, join
import pandas as pd

from nltk.corpus import stopwords

SEPARADOR = ' '

def build_csv_with_data(working_folder, dataset_path, filename):
    all_folders = [element for element in listdir(dataset_path) if not isfile(join(dataset_path, element))]
    output_file_name = os.path.join(working_folder, filename)
    print(output_file_name)

    with open(output_file_name, 'w') as outputFile:
        for folder in all_folders:
            print(folder)
            folder_path = os.path.join(dataset_path, folder)
            all_files = [f for f in listdir(folder_path) if isfile(os.path.join(folder_path, f))]
            for file in all_files:
                text = ""
                with open(os.path.join(folder_path, file), encoding="utf8") as infile:
                    # firstLine = infile.readline().replace("\n","")
                    for line in infile:
                        # remove stopwords do texto de concatena as linhas para formar um CSV
                        filtered_words = [word for word in line.split(' ') if word not in stopwords.words('portuguese')]
                        line = SEPARADOR.join(filtered_words)
                        text = text + " " + line.replace("\n", "")
                outputFile.write(folder + "\t" + text + "\n")

    return output_file_name

def read_data(filepath):
    data_frame = pd.read_csv(filepath, encoding="ISO-8859-1", sep='\t', header=None, skiprows=0,
                             names=["Class", "Text"])

    return data_frame.groupby('Class').filter(lambda x: len(x) > 10)


# coding: utf-8

# In[1]:

# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All"
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session

# In[2]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# In[3]:

data = pd.read_csv('/kaggle/input/creditcardfraud/creditcard.csv')

# In[4]:

data.head()

# In[5]:

data.info()

# In[6]:

data['Class'].value_counts()

# In[9]:

# separating the data for analysis
legit = data[data.Class == 0]
fraud = data[data.Class == 1]

# In[10]:

legit_sample = legit.sample(n=492) # Handlimg imbalanced data

# In[11]:

new_dataset = pd.concat([legit_sample, fraud], axis=0)

# In[12]:

new_dataset['Class'].value_counts()

# In[13]:

X = new_dataset.drop(columns='Class', axis=1)
Y = new_dataset['Class']

# In[16]:

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, stratify=Y, random_state=2)

# In[17]:

print(X.shape, X_train.shape, X_test.shape)

# In[18]:

model = LogisticRegression()

# In[20]:

# training the Logistic Regression Model with Training Data
model.fit(X_train, Y_train)

# In[21]:

# accuracy on training data
X_train_prediction = model.predict(X_train)
training_data_accuracy = accuracy_score(X_train_prediction, Y_train)

# In[22]:

print('Accuracy on Training data : ', training_data_accuracy)

# In[25]:

# accuracy on test data
X_test_prediction = model.predict(X_test)
test_data_accuracy = accuracy_score(X_test_prediction, Y_test)
print('Accuracy score on Test Data : ', test_data_accuracy)