import torch

from ..base import MaskSeparationBase, DeepMixin, SeparationException
from ... import ml

class DeepMaskEstimation(DeepMixin, MaskSeparationBase):
    """
    Separates an audio signal using the masks produced by a deep model for every
    time-frequency point. It expects that the model outputs a dictionary where one
    of the keys is 'masks'. This uses the `DeepMixin` class to load the model
    and set the audio signal's parameters to be appropriate for the model.

    Args:
        input_audio_signal: (AudioSignal`) An AudioSignal object containing the
          mixture to be separated.
        model_path (str, optional): Path to the model that will be used. Can be None,
          so that you can initialize a class and load the model later.
          Defaults to None.
        device (str, optional): Device to put the model on. Defaults to 'cpu'.
        **kwargs (dict): Keyword arguments for MaskSeparationBase.
    """
    def __init__(self, input_audio_signal, model_path=None, device='cpu',
                 **kwargs):
        super().__init__(input_audio_signal, **kwargs)
        if model_path is not None:
            self.load_model(model_path, device=device)
        self.model_output = None
        # audio channel dimension in a mask estimation model
        self.channel_dim = -1

    def forward(self, **kwargs):
        input_data = self._get_input_data_for_model(**kwargs)
        with torch.no_grad():
            output = self.model(input_data)
            if 'mask' not in output:
                raise SeparationException(
                    "This model is not a deep mask estimation model! "
                    "Did not find 'mask' key in output dictionary.")
            masks = output['mask']
            # swap back batch and sample dims
            if self.metadata['num_channels'] == 1:
                masks = masks.transpose(0, -2)
            masks = masks.squeeze(0).transpose(0, 1)
            masks = masks.cpu().data.numpy()
        self.model_output = output
        return masks

    def run(self, masks=None):
        self.result_masks = []

        if masks is None:
            masks = self.forward()

        for i in range(masks.shape[-1]):
            mask_data = masks[..., i]
            if self.mask_type == self.MASKS['binary']:
                mask_data = masks[..., i] == masks.max(axis=-1)
            mask = self.mask_type(mask_data)
            self.result_masks.append(mask)

        return self.result_masks

    def confidence(self, approach='silhouette_confidence', num_sources=2, **kwargs):
        """
        In embedding-based separation algorithms, we can compute a confidence
        measure based on the clusterability of the embedding space. This can
        be used if the model also computes an embedding alongside the estimates
        (e.g. as in Chimera models.)

        Args:
            approach (str, optional): What approach to use for getting the confidence
              measure. Options are 'jensen_shannon_confidence', 'posterior_confidence',
              'silhouette_confidence', 'loudness_confidence', 'whitened_kmeans_confidence',
              'dpcl_classic_confidence'. Defaults to 'silhouette_confidence'.
            kwargs: Keyword arguments to the function being used to compute the confidence.
        """
        if self.model_output is None:
            raise SeparationException(
                "self.model_output is None! Did you run forward?")
        if 'embedding' not in self.model_output:
            raise SeparationException(
                "embedding not in self.model_output! Can't compute confidence.")
        features = self.model_output['embedding']
        if self.metadata['num_channels'] == 1:
            features = features.transpose(0, -2)
        features = features.squeeze(0).transpose(0, 1)
        features = features.cpu().data.numpy()

        confidence_function = getattr(ml.confidence, approach)
        confidence = confidence_function(
            self.audio_signal, features, num_sources, **kwargs)
        return confidence


#自动爬取代理，自动用代理爬公司的三类专利的总的数量
# -*- coding: utf-8 -*-
import requests
import re
import xlrd
import time
import random
from lxml import etree

agent = 'Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0'
#专利网站header
headers = {
    "User-Agent": agent,
    "Host": "epub.sipo.gov.cn",
    "Origin": "http://epub.sipo.gov.cn",
    "Referer": "http://epub.sipo.gov.cn/gjcx.jsp"
}

proxy = {
        "http": "",
}

url_login = 'http://epub.sipo.gov.cn/patentoutline.action'

postdata1 = {
        "showType": "1",
        "strWord": "",
        "numSortMethod": "",
        "strLicenseCode": "",
        "slected": "",
        "numFMGB": "",
        "numFMSQ":"0",
        "numSYXX": "0",
        "numWGSQ": "0",
        "pageSize": "3",
        "pageNow": "1",
        }
def fff(url,headers):
    try:
        html = requests.get(url,headers=headers,timeout=60).text
        return html
    except Exception as e:
        html = fff(url,headers)
        return html

def crawlproxy():
    global proxies
    proxies = []
    url = "http://www.xicidaili.com/nn/"
    urltest = "http://httpbin.org/ip"
    agent = 'Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0'
    headers = {
        "User-Agent": agent,
    }
    html = fff(url,headers)
    selector = etree.HTML(html)
    record1 = selector.xpath('//*[@id="ip_list"]/tr[starts-with(@class,"odd")]/td[2]')
    record2 = selector.xpath('//*[@id="ip_list"]/tr[starts-with(@class,"odd")]/td[3]')
    for i in range(0,len(record1)):  #注意修改
        httpproxy = record1[i].xpath('string(.)')
        port = record2[i].xpath('string(.)')
        test = {"http":"http://"+httpproxy+":"+port}
        print("http://"+httpproxy+":"+port)
        proxies.append(test)
    return proxies

def gettext(url,postdata,headers,proxies):
    if proxies:
        proxy = random.choice(proxies)
    else:
        print("代理ip已用完")
        proxies = crawlproxy()
        proxy =random.choice(proxies)
    try:
        html = requests.post(url,data=postdata,headers=headers,timeout=60,proxies=proxy).text
        pattern0 =r'错误页面'
        pattern1 =r'发明授权：(.*?)件'
        record0 = re.search(pattern0,html,re.S)
        record1 = re.search(pattern1,html,re.S)
        if (not record1) and (not record0):   #判断页面是否正常
            print(record0[0])
        return html
        time.sleep(1)
    except Exception as e:
        print(e)
        time.sleep(1)
        proxies.remove(proxy)
        html = gettext(url, postdata, headers,proxies)
        return html
def crawl(company):
     print("爬取"+company+"各类记录数")
     postdata = postdata1
     postdata["strWord"] ="申请（专利权）人='%"+company+"%'"
     html = gettext(url_login,postdata,headers,proxies)
     pattern0 =r'错误页面'
     pattern1 =r'发明授权：(.*?)件'
     pattern2 =r'实用新型：(.*?)件'
     pattern3 =r'外观设计：(.*?)件'
     record0 = re.search(pattern0,html,re.S)
     if record0:
         result = company +";错误页面"
     else:
         record1 = re.findall(pattern1,html,re.S)
         if record1:
            record2 = re.findall(pattern2,html,re.S)
            record3 = re.findall(pattern3,html,re.S)
            result = company+";"+record1[0]+";"+record2[0]+";"+record3[0]
         else:
             result = html
     f = open('firmprimaryrecord.txt','a',encoding='utf-8')
     f.write(result)
     f.write("\n")
     f.close()
     print(result)

data = xlrd.open_workbook("firm2.xlsx")
table = data.sheets()[0]   #0表示excel第一张sheet表
companylist = table.col_values(0)  #获取excel第一列中的所有值并保存为列表
proxies = crawlproxy()
for each in companylist:
    crawl(each)


# You are given a 1-indexed integer array nums of length n.
# An element nums[i] of nums is called special if i divides n, i.e. n % i == 0.
# Return the sum of the squares of all special elements of nums.
import time
# import resource
def sumOfSquares(nums: list[int]) -> int:
    n = len(nums)
    k = 0
    for i in range(1,len(nums)+1):
        if n % i == 0:
            k = k + nums[i-1]*nums[i-1]
    return k

time_start = time.perf_counter()
nums = [2,7,1,19,18,3]
sumOfSquares(nums)
time_elapsed = (time.perf_counter() - time_start)
print(time_elapsed)


#-*- coding: utf-8 -*-
import collections
import multiprocessing
import time
from collections import defaultdict

import six

def create_training_instances(seq_by_user,
                              max_seq_length,
                              dupe_factor,
                              short_seq_prob,
                              masked_lm_prob,
                              max_predictions_per_seq,
                              vocab,
                              mask_prob,
                              prop_sliding_window,
                              rng,
                              pool_size,
                              force_last=False):
  """Create `TrainingInstance`s from raw text.
    all_documents : item sequence of each user.
  """
  # seq_by_user = {}  # already dictionary

  if force_last:
    for user, item_seq in seq_by_user.items():
      if len(item_seq) == 0:
        print("got empty seq:" + user)
        continue
      seq_by_user[user] = [item_seq[-max_seq_length:]]
  else:
    max_num_tokens = max_seq_length  # we need two sentence

    sliding_step = (int)(
      prop_sliding_window *
      max_num_tokens) if prop_sliding_window != -1.0 else max_num_tokens
    for user, item_seq in all_documents_raw.items():
      if len(item_seq) == 0:
        print("got empty seq:" + user)
        continue

      # todo: add slide
      if len(item_seq) <= max_num_tokens:
        all_documents[user] = [item_seq]
      else:
        beg_idx = range(len(item_seq) - max_num_tokens, 0, -sliding_step)
        beg_idx.append(0)
        all_documents[user] = [item_seq[i:i + max_num_tokens] for i in beg_idx[::-1]]

  instances = []
  if force_last:
    for user in seq_by_user:
      instances.extend(
        create_instances_from_document_test(
          seq_by_user, user, max_seq_length))
    print("num of instance:{}".format(len(instances)))
  else:
    start_time = time.clock()
    pool = multiprocessing.Pool(processes=pool_size)
    instances = []
    print("document num: {}".format(len(all_documents)))

    def log_result(result):
      print("callback function result type: {}, size: {} ".format(type(result), len(result)))
      instances.extend(result)

    for step in range(dupe_factor):
      pool.apply_async(
        create_instances_threading, args=(
          all_documents, max_seq_length, short_seq_prob,
          masked_lm_prob, max_predictions_per_seq, vocab, mask_prob, step),
          callback=log_result)
    pool.close()
    pool.join()

    for user in all_documents:
      instances.extend(
        mask_last(
          all_documents, user, max_seq_length, short_seq_prob, vocab))

    print("num of instance:{}; time:{}".format(len(instances), time.clock() - start_time))
  rng.shuffle(instances)
  return instances

class BertTrainingInstance(object):
    """A single training instance (sentence pair)."""

    def __init__(self, info, tokens, masked_lm_positions, masked_lm_labels):
      self.info = info  # info = [user]
      self.tokens = tokens
      self.masked_lm_positions = masked_lm_positions
      self.masked_lm_labels = masked_lm_labels

    def __str__(self):
      s = ""
      s += "info: %s\n" % (" ".join([printable_text(x) for x in self.info]))
      s += "tokens: %s\n" % (
        " ".join([printable_text(x) for x in self.tokens]))
      s += "masked_lm_positions: %s\n" % (
        " ".join([str(x) for x in self.masked_lm_positions]))
      s += "masked_lm_labels: %s\n" % (
        " ".join([printable_text(x) for x in self.masked_lm_labels]))
      s += "\n"
      return s

    def __repr__(self):
      return self.__str__()

def mask_last(
      all_documents, user, max_seq_length, short_seq_prob, vocab):
    """Creates `TrainingInstance`s for a single document."""
    document = all_documents[user]
    max_num_tokens = max_seq_length

    instances = []
    info = [int(user.split("_")[1])]

    for tokens in document:
      assert len(tokens) >= 1 and len(tokens) <= max_num_tokens

      (tokens, masked_lm_positions,
       masked_lm_labels) = create_masked_lm_predictions_force_last(tokens)
      instance = BertTrainingInstance(
        info=info,
        tokens=tokens,
        masked_lm_positions=masked_lm_positions,
        masked_lm_labels=masked_lm_labels)
      instances.append(instance)

    return instances

def create_instances_from_document_test(seq_by_user, user, max_seq_length):
  """Creates `TrainingInstance`s for a single document."""
  seq = seq_by_user[user]
  max_num_tokens = max_seq_length

  assert len(seq) == 1 and len(seq[0]) <= max_num_tokens

  tokens = seq[0]
  assert len(tokens) >= 1

  (tokens, masked_lm_positions,
   masked_lm_labels) = create_masked_lm_predictions_force_last(tokens)

  info = [int(user.split("_")[1])]
  instance = BertTrainingInstance(
    info=info,
    tokens=tokens,
    masked_lm_positions=masked_lm_positions,
    masked_lm_labels=masked_lm_labels)

  return [instance]

def create_instances_from_document_train(
    all_documents, user, max_seq_length, short_seq_prob, masked_lm_prob,
    max_predictions_per_seq, vocab, rng, mask_prob):
  """Creates `TrainingInstance`s for a single document."""
  document = all_documents[user]

  max_num_tokens = max_seq_length

  instances = []
  info = [int(user.split("_")[1])]
  vocab_items = vocab.get_items()

  for tokens in document:
    assert len(tokens) >= 1 and len(tokens) <= max_num_tokens

    (tokens, masked_lm_positions,
     masked_lm_labels) = create_masked_lm_predictions(
      tokens, masked_lm_prob, max_predictions_per_seq,
      vocab_items, rng, mask_prob)
    instance = BertTrainingInstance(
      info=info,
      tokens=tokens,
      masked_lm_positions=masked_lm_positions,
      masked_lm_labels=masked_lm_labels)
    instances.append(instance)

  return instances

def create_masked_lm_predictions_force_last(tokens):
    """Creates the predictions for the masked LM objective."""

    last_index = -1
    for (i, token) in enumerate(tokens):
      if token == "[CLS]" or token == "[PAD]" or token == '[NO_USE]':
        continue
      last_index = i

    assert last_index > 0

    output_tokens = list(tokens)  # already list?
    output_tokens[last_index] = "[MASK]"

    masked_lm_positions = [last_index]
    masked_lm_labels = [tokens[last_index]]

    return (output_tokens, masked_lm_positions, masked_lm_labels)

def create_masked_lm_predictions(tokens, masked_lm_prob,
                                 max_predictions_per_seq, vocab_words, rng,
                                 mask_prob):
  """Creates the predictions for the masked LM objective."""

  cand_indexes = []
  for (i, token) in enumerate(tokens):
    if token not in vocab_words:
      continue
    cand_indexes.append(i)

  rng.shuffle(cand_indexes)

  output_tokens = list(tokens)

  num_to_predict = min(max_predictions_per_seq,
                       max(1, int(round(len(tokens) * masked_lm_prob))))

  masked_lms = []
  covered_indexes = set()
  for index in cand_indexes:
    if len(masked_lms) >= num_to_predict:
      break
    if index in covered_indexes:
      continue
    covered_indexes.add(index)

    masked_token = None
    # 80% of the time, replace with [MASK]
    if rng.random() < mask_prob:
      masked_token = "[MASK]"
    else:
      # 10% of the time, keep original
      if rng.random() < 0.5:
        masked_token = tokens[index]
      # 10% of the time, replace with random word
      else:
        # masked_token = vocab_words[rng.randint(0, len(vocab_words) - 1)]
        masked_token = rng.choice(vocab_words)

    output_tokens[index] = masked_token

    masked_lms.append(MaskedLmInstance(index=index, label=tokens[index]))

  masked_lms = sorted(masked_lms, key=lambda x: x.index)

  masked_lm_positions = []
  masked_lm_labels = []
  for p in masked_lms:
    masked_lm_positions.append(p.index)
    masked_lm_labels.append(p.label)

  return (output_tokens, masked_lm_positions, masked_lm_labels)

MaskedLmInstance = collections.namedtuple("MaskedLmInstance",
                                          ["index", "label"])

def create_instances_threading(all_documents, max_seq_length, short_seq_prob,
                               masked_lm_prob, max_predictions_per_seq, vocab, rng,
                               mask_prob, step):
  cnt = 0
  start_time = time.clock()
  instances = []
  for user in all_documents:
    cnt += 1
    if cnt % 1000 == 0:
      print("step: {}, name: {}, step: {}, time: {}".format(step, multiprocessing.current_process().name, cnt,
                                                            time.clock() - start_time))
      start_time = time.clock()
    instances.extend(create_instances_from_document_train(
      all_documents, user, max_seq_length, short_seq_prob,
      masked_lm_prob, max_predictions_per_seq, vocab, rng,
      mask_prob))

  return instances

def printable_text(text):
    """Returns text encoded in a way suitable for print or `tf.logging`."""

    # These functions want `str` for both Python2 and Python3, but in one case
    # it's a Unicode string and in the other it's a byte string.
    if six.PY3:
      if isinstance(text, str):
        return text
      elif isinstance(text, bytes):
        return text.decode("utf-8", "ignore")
      else:
        raise ValueError("Unsupported string type: %s" % (type(text)))
    elif six.PY2:
      if isinstance(text, str):
        return text
      elif isinstance(text, unicode):
        return text.encode("utf-8")
      else:
        raise ValueError("Unsupported string type: %s" % (type(text)))
    else:
      raise ValueError("Not running on Python2 or Python 3?")

def data_partition(fname):
  usernum = 0
  itemnum = 0
  User = defaultdict(list)
  user_train = {}
  user_valid = {}
  user_test = {}
  # assume user/item index starting from 1
  f = open(fname, 'r')
  for line in f:
    u, i = line.rstrip().split(' ')
    u = int(u)
    i = int(i)
    usernum = max(u, usernum)
    itemnum = max(i, itemnum)
    User[u].append(i)

  for user in User:
    nfeedback = len(User[user])
    if nfeedback < 3:
      user_train[user] = User[user]
      user_valid[user] = []
      user_test[user] = []
    else:
      user_train[user] = User[user][:-2]
      user_valid[user] = []
      user_valid[user].append(User[user][-2])
      user_test[user] = []
      user_test[user].append(User[user][-1])
  return [user_train, user_valid, user_test, usernum, itemnum]


import os
import warnings
import sys
import os.path
import sh
import argparse
import subprocess
import shutil

class DocxToMdConverter(object):

    def __init__(self, **opts):
        self.inputpath = opts.get("inputpath", None)
        self.outputpath = opts.get("outputpath", None)
        self.outfolder = opts.get("out_dir", None)
        self.format = opts.get("format", "docx")
        self.toc = opts.get("toc", False)
        self.pandoc = opts.get("pandoc", "/usr/local/bin/pandoc")
        self.verbose = opts.get("verbose", False)
        self.jsonpath = opts.get("jsonpath", None)
        self.imgfolder = opts.get("img_dir", None)
        self.success = True
        self.stdout = None
        self.stderr = None

    def preparePaths(self):
        if not self.inputpath:
            self.success = False
        self.inputpath = os.path.realpath(os.path.normpath(self.inputpath))
        if not os.path.exists(self.inputpath):
            self.success = False
        if not self.outputpath:
            self.outputpath = os.path.splitext(self.inputpath)[0] + ".md"
        self.outputpath = os.path.realpath(os.path.normpath(self.outputpath))
        if self.outfolder:
            self.outfolder = os.path.realpath(os.path.normpath(self.outfolder))
            self.outputpath = os.path.join(self.outfolder, os.path.split(self.outputpath)[1])
        else:
            self.outfolder = os.path.split(self.outputpath)[0]
        if not os.path.exists(self.outfolder):
            try:
                os.makedirs(self.outfolder)
            except:
                warnings.warn("Cannot create folder %s" % (self.outfolder))
                self.success = False
        if not self.jsonpath:
            self.jsonpath = self.outputpath + "-tmp.json"
        if not self.imgfolder:
            self.imgfolder = os.path.join(self.outfolder, "img")
        if not os.path.exists(self.imgfolder):
            try:
                os.makedirs(self.imgfolder)
            except:
                warnings.warn("Cannot create folder %s" % (self.imgfolder))
                self.success = False

    def pandocRun(self, args):
        # To get access to pandoc-citeproc when we use a included copy of pandoc,
        # we need to add the pypandoc/files dir to the PATH
        new_env = os.environ.copy()
        cwd = os.path.dirname(os.path.realpath(__file__))
        files_path = os.path.join(cwd, "files")
        new_env["PATH"] = new_env.get("PATH", "") + os.pathsep + files_path

        if self.verbose:
            print("Running: " + " ".join(args))
        p = subprocess.Popen(args, bufsize=4096, stdin=None,
            stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=new_env, cwd=cwd)
        if not (p.returncode is None):
            raise RuntimeError(
                'Pandoc died with exitcode "%s" before receiving input: %s' % (p.returncode,
                                                                               p.stderr.read())
            )
        try:
            self.stdout, self.stderr = p.communicate(None)
            self.success = True
        except OSError:
            raise RuntimeError('Pandoc died with exitcode "%s" during conversion.' % (p.returncode))
            self.success = False
        assert self.stdout == ""

    def convertJsonToMd(self):
        pdArgs = ['--smart', '--section-divs', '--atx-headers']
        pdFilters = ['./pandoc-wmftosvgpng.py']
        os.environ['pandoc_wmftosvgpng'] = self.outfolder

        pdMdExt = ['-pipe_tables', '+auto_identifiers', '+backtick_code_blocks', '+blank_before_blockquote', '+blank_before_header', '+bracketed_spans', '+definition_lists', '+escaped_line_breaks', '+fenced_code_attributes', '+footnotes', '+grid_tables', '+header_attributes', '+implicit_header_references', '+line_blocks', '+pandoc_title_block']
        pdMdOutFmt = 'markdown_github' + ''.join(pdMdExt)

        args = [self.pandoc]
        args.append('--from=' + 'json')
        args.append('--to=' + pdMdOutFmt)
        args.append(self.jsonpath)
        args.append("--output="+self.outputpath)
        args.extend(pdArgs)
        if pdFilters:
            f = ['--filter=' + x for x in pdFilters]
            args.extend(f)

        self.pandocRun(args)

        if self.verbose:
            print(self.stderr)
            print(self.stdout)
        self.success = True

    def convertDocxToJson(self):
        pdArgs = ['--smart', '--section-divs', '--atx-headers']
        if self.toc:
            pdArgs.append('--toc')
        if self.format == "docx":
            pdArgs.append('--extract-media=.')
        pdFilters = None
        pdMdOutFmt = 'json'

        args = [self.pandoc]
        args.append('--from=' + self.format)
        args.append('--to=' + pdMdOutFmt)
        args.append(self.inputpath)
        args.append("--output="+self.jsonpath)
        args.extend(pdArgs)
        if pdFilters:
            f = ['--filter=' + x for x in pdFilters]
            args.extend(f)

        self.pandocRun(args)

        if self.format == "docx":
            mediafolder = os.path.join(self.outfolder, "media")
            if os.path.exists(mediafolder):
                shutil.rmtree(mediafolder)
            shutil.move("media", self.outfolder)
        self.success = True

    def convertDocxToMd(self):
        # For various reasons, we run pandoc twice:
        # Once docx-to-json, then json-to-md
        self.preparePaths()
        if self.success:
            self.convertDocxToJson()
        if self.success:
            self.convertJsonToMd()

def parseOptions():
    parser = argparse.ArgumentParser(description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("inputpath", help="input.docx file")
    parser.add_argument("outputpath", nargs='?', default=None, help="output.md file, default to input.md")
    parser.add_argument("-d", "--out-dir", help="output folder, default to current", action="store", default=None)
    parser.add_argument("-f", "--format", help="input format, default 'docx'", action="store", default='docx')
    parser.add_argument("-t", "--toc", help="generate TOC", action="store_true", default=False)
    parser.add_argument("--pandoc", help="path to 'pandoc' executable", default="/usr/local/bin/pandoc")
    parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity")
    args = parser.parse_args()
    return vars(args)

def main():
    opts = parseOptions()
    converter = DocxToMdConverter(**opts)
    converter.convertDocxToMd()
    if converter.success:
        if opts["verbose"]:
            print("Done.")
    else:
        if opts["verbose"]:
            print("Nothing converted.")
        sys.exit(2)

if __name__ == '__main__':
    main()


import sys
from sys import path
import pickle  # for reading the original wake data
import numpy as np

''' directories '''
projDir = '/media/nx/Ubuntu/myproject/IWSH/'
probes = ['probe1']
caseName = 'NBL.succ.newdomain.56cores'
probeDataDict_temp = {}

for probe in probes:
    f = open(projDir + 'postProcessing_all/' + caseName + '_' + probe +'_probeData', 'rb')
    probeDataDict_temp[probe] = pickle.load(f) # all wake information of the case
    f.close()

timeList = list(probeDataDict_temp[probes[0]].keys()) # find the timeList (for every probe, the timeList should be the same)

probeDataDict = dict(zip(timeList,timeList))
for t in timeList:
    probeDataDict[t] = None
for t in timeList:
    for probe in probes:
        if type(probeDataDict[t]) != type(probeDataDict_temp[probe][t]):
            probeDataDict[t] = probeDataDict_temp[probe][t]
        else:
            probeDataDict[t] = np.vstack(probeDataDict[t],probeDataDict_temp[probe][t])

''' save probeDataDict into a file with pickle '''
f = open(projDir + 'postProcessing_all/' + caseName + '_probeData', 'wb')
pickle.dump(probeDataDict, f)
f.close()


from pytest import fixture
from flask import Flask
from afirmatron.api import routes

app = Flask(__name__)
app.register_blueprint(routes)

HEADERS = {
    "User-Agent": "ESP8266-http-Update",
    "X-Esp8266-Chip-Id": "10771118",
    "X-Esp8266-Sta-Mac": "bob",
    "X-Esp8266-Ap-Mac": "5C:CF:7F:A4:5A:AE",
    "X-Esp8266-Free-Space": "647168",
    "X-Esp8266-Sketch-Size": "315296",
    "X-Esp8266-Sketch-Md5": "b672da7ee227ee83b364e410e6eea40d",
    "X-Esp8266-Chip-Size": "4194304",
    "X-Esp8266-Sdk-Version": "2.2.2-dev(38a443e)",
    "X-Esp8266-Mode": "sketch"
}

class TestDeviceRequests:
    @fixture
    def client(self):
        return app.test_client()

    def test_update_5C(self, client):
        response = client.get('/update', headers=HEADERS)
        assert 200 == response.status_code

    def test_update_original(self, client):
        HEADERS.update({"X-Esp8266-Sta-Mac": "a0:20:a6:27:0b:b2"})
        response = client.get('/update', headers=HEADERS)
        assert 200 == response.status_code


# 定义一个函数quadratic(a,b,c)，接收3个参数，返回一元二次方程
# 这个函数之前军训的时候写过，当时写了整整两个小时，还借鉴了很多的代码。当时完全不会，写了很多很多bug出来
#这次大概只写了半小时吧，而且也没有很多的bug，算是进步吧

import math
def quadratic(a,b,c):
    if not isinstance(a,(int)):
        raise TypeError('please input an integer')  #这里的参数检查好像没有派上用场
    if not isinstance(b,(int)):
        raise TypeError('please input an integer')
    if not isinstance(c,(int)):
        raise TypeError('please input an integer')
    delt = b*b-4*a*c
    if a == 0:       # 检查是否相等的时候不能用=，要用==!!!!!!!!!!!!!!!!!
        if b == 0:
            if c==0:
                return 'x为任意实数'
            else:
                return '该方程无解'
        else:
            x1 = x2 = -c/b
            return x1,x2   # 这里返回多个值的时候tuple很难看
    else:
        if delt > 0:
            x1 = (-b+math.sqrt(delt))/(2*a)   # 注意括号的使用!!!!!!!!!!!
            x2 = (-b-math.sqrt(delt))/(2*a)
            return x1,x2
        elif delt == 0:
            x1 = x2 = (-b)/(2*a)
            return x1,x2
        else:
            return "无实数根"

a = int(input('please input a(ax^2+bx+c=0)'))
b = int(input('please input b(ax^2+bx+c=0)'))
c = int(input('please input c(ax^2+bx+c=0)'))
print (quadratic(a,b,c))


import pandas as pd
import plotly.graph_objects as go
import numpy as np

# 读取Excel文件中的两个表格
file_path1 = 'origin1_end.xlsx'
file_path2 = 'origin2_end.xlsx'
df1 = pd.read_excel(file_path1,engine='openpyxl')
df2 = pd.read_excel(file_path2,engine='openpyxl')

# 提取x和y轴的数据
x1 = df1['Year']
y1 = df1['Listing Price']
x2 = df2['Year']
y2 = df2['Listing Price']

# 计算趋势线
z1 = np.polyfit(x1, y1, 1)
p1 = np.poly1d(z1)
z2 = np.polyfit(x2, y2, 1)
p2 = np.poly1d(z2)

# 创建散点图
fig = go.Figure()

# 添加第一个表格的散点数据和趋势线
fig.add_trace(go.Scatter(x=x1, y=y1, mode='markers',name='Monohulled Sailboats',marker=dict(color='#ff9d00', size=12,opacity=0.7,),line=dict(width=3, color='black')))
fig.add_trace(go.Scatter(x=x1, y=p1(x1), mode='lines', name='Monohulled SailboatsTrendline',line=dict(color='#ff9d00', width=2)))
# 添加第二个表格的散点数据和趋势线
fig.add_trace(go.Scatter(x=x2, y=y2, mode='markers',name='Catamarans',marker=dict(color='#2e9db1', size=12,opacity=0.5,),line=dict(width=3, color='black')))
fig.add_trace(go.Scatter(x=x2, y=p2(x2), mode='lines', name='Catamarans Trendline',line=dict(color='#2e9db1', width=2)))
# 设置图表标题和轴标签
fig.update_layout(title='',xaxis_title='Year',yaxis_title='Listing Price',plot_bgcolor='white')

# 显示图表
fig.show()


from pandac.PandaModules import loadPrcFileData
loadPrcFileData('', 'frame-rate-meter-scale 0.035')
loadPrcFileData('', 'frame-rate-meter-side-margin 0.1')
loadPrcFileData('', 'show-frame-rate-meter 1')
loadPrcFileData('', 'window-title ' + "Vortex")
loadPrcFileData('', "sync-video 0")
loadPrcFileData('', 'task-timer-verbose 1')
loadPrcFileData('', 'pstats-tasks 1')
loadPrcFileData('', 'want-pstats 1')
loadPrcFileData('', 'framebuffer-stencil 1')
#loadPrcFileData('', 'show-buffers 1')
#loadPrcFileData('', 'notify-level-glgsg debug')

import sandbox

import shapeGenerator

from panda3d.core import AmbientLight, ColorWriteAttrib, NodePath, Shader, StencilAttrib, Vec4

# Set up stenciling system
stencilReader = StencilAttrib.make(1,StencilAttrib.SCFEqual,StencilAttrib.SOKeep,
                       StencilAttrib.SOKeep,StencilAttrib.SOKeep,1,1,0)

constantOneStencil = StencilAttrib.make(1,StencilAttrib.SCFAlways,StencilAttrib.SOZero,
                       StencilAttrib.SOReplace,StencilAttrib.SOReplace,1,0,1)

#mesh = shapeGenerator.Tube(0.18, 0.18, 5.0, 32)
# To be render on texture
#inside_vortex = shapeGenerator.ShellCylinder(-0.18, 5.0, 32)
inside_vortex = shapeGenerator.Circle(0.18, 32)
inside_vortex.node().setAttrib(constantOneStencil)
inside_vortex.node().setAttrib(ColorWriteAttrib.make(0))
inside_vortex.setBin('background', 0)
inside_vortex.setDepthWrite(0)

#inside_vortex.setH(180)
inside_vortex.setP(180)
#inside_vortex.setR(180)

vortex = shapeGenerator.ShellCylinder(-0.18, 10.0, 32)
vortex.node().setAttrib(stencilReader)
# Mask that fades out vortex
#outside_vortex = shapeGenerator.ShellCylinder(0.181, 0.05, 32)
outside_vortex = shapeGenerator.ShellCylinder(0.181, 5.0, 32)

def taskUpdate(task):
    #inside_vortex.setShaderInput('time', task.time/4.0)
    outside_vortex.setShaderInput('time', task.time/4.0)
    vortex.setShaderInput('time', task.time/4.0)
    #print base.camera.getPos()
    #print vortex_camera.getHpr()
    vortex_camera.setPos(sandbox.base.camera.getPos())
    vortex_camera.setHpr(sandbox.base.camera.getHpr())
    return task.cont

shaders = Shader.load(Shader.SLGLSL, 'vortex_vertex.glsl', 'vortex_fragment.glsl')
#inside_vortex.setShader(shaders)
outside_vortex.setShader(shaders)
vortex.setShader(shaders)

inside_vortex.reparentTo(sandbox.base.render)
#outside_vortex.reparentTo(sandbox.base.render)

sandbox.base.taskMgr.add(taskUpdate, "SpinCameraTask")

# Create buffers
vortex_buffer = sandbox.base.win.makeTextureBuffer("Vortex Buffer", 256, 256)
#vortex_texture = vortex_buffer.getTexture()
vortex_buffer.setSort(-100)
vortex_camera = sandbox.base.makeCamera(vortex_buffer)
vortex_scene = NodePath("Vortex Scene")
vortex_camera.reparentTo(vortex_scene)

#inside_vortex.setTexture(vortex_texture, 1)
#vortex.reparentTo(vortex_scene)
vortex.reparentTo(sandbox.base.render)

#alight = AmbientLight('alight')
#alnp = vortex_scene.attachNewNode(alight)
#alight.setColor(Vec4(0.2, 0.2, 0.2, 1))
#vortex_scene.setLight(alnp)

def screenshot():
    sandbox.base.screenshot('/home/croxis/vortex/')

sandbox.base.accept("v", sandbox.base.bufferViewer.toggleEnable)
sandbox.base.accept("V", sandbox.base.bufferViewer.toggleEnable)

sandbox.base.accept("s", screenshot)

sandbox.run()


# -*- encoding: utf-8 -*-
from supriya.tools.ugentools.UGen import UGen

class TIRand(UGen):
    """
    A triggered integer random number generator.

    ::

        >>> trigger = ugentools.Impulse.ar()
        >>> t_i_rand = ugentools.TIRand.ar(
        ...     minimum=0,
        ...     maximum=127,
        ...     trigger=trigger,
        ...     )
        >>> t_i_rand
        TIRand.ar()

    """

    ### CLASS VARIABLES ###

    __documentation_section__ = 'Noise UGens'

    __slots__ = ()

    _ordered_input_names = (
        'minimum',
        'maximum',
        'trigger',
        )

    _valid_calculation_rates = None

    ### INITIALIZER ###

    def __init__(
        self,
        calculation_rate=None,
        maximum=127,
        minimum=0,
        trigger=0,
        ):
        minimum = int(minimum)
        maximum = int(maximum)
        UGen.__init__(
            self,
            calculation_rate=calculation_rate,
            maximum=maximum,
            minimum=minimum,
            trigger=trigger,
            )

    ### PUBLIC METHODS ###

    @classmethod
    def ar(
        cls,
        maximum=127,
        minimum=0,
        trigger=0,
        ):
        """
        Constructs an audio-rate triggered integer random number generator.

        ::

            >>> trigger = ugentools.Impulse.ar()
            >>> t_i_rand = ugentools.TIRand.ar(
            ...     minimum=0,
            ...     maximum=[64, 127],
            ...     trigger=trigger,
            ...     )
            >>> t_i_rand
            UGenArray({2})

        Returns ugen graph.
        """
        from supriya.tools import synthdeftools
        calculation_rate = synthdeftools.CalculationRate.AUDIO
        ugen = cls._new_expanded(
            calculation_rate=calculation_rate,
            maximum=maximum,
            minimum=minimum,
            trigger=trigger,
            )
        return ugen

    @classmethod
    def kr(
        cls,
        maximum=127,
        minimum=0,
        trigger=0,
        ):
        """
        Constructs a control-rate triggered integer random number generator.

        ::

            >>> trigger = ugentools.Impulse.kr()
            >>> t_i_rand = ugentools.TIRand.kr(
            ...     minimum=0,
            ...     maximum=[64, 127],
            ...     trigger=trigger,
            ...     )
            >>> t_i_rand
            UGenArray({2})

        Returns ugen graph.
        """
        from supriya.tools import synthdeftools
        calculation_rate = synthdeftools.CalculationRate.CONTROL
        ugen = cls._new_expanded(
            calculation_rate=calculation_rate,
            maximum=maximum,
            minimum=minimum,
            trigger=trigger,
            )
        return ugen

    ### PUBLIC PROPERTIES ###

    @property
    def maximum(self):
        """
        Gets `maximum` input of TIRand.

        ::

            >>> trigger = ugentools.Impulse.ar()
            >>> t_i_rand = ugentools.TIRand.ar(
            ...     minimum=0,
            ...     maximum=127,
            ...     trigger=trigger,
            ...     )
            >>> t_i_rand.maximum
            127.0

        Returns ugen input.
        """
        index = self._ordered_input_names.index('maximum')
        return self._inputs[index]

    @property
    def minimum(self):
        """
        Gets `minimum` input of TIRand.

        ::

            >>> trigger = ugentools.Impulse.ar()
            >>> t_i_rand = ugentools.TIRand.ar(
            ...     minimum=0,
            ...     maximum=127,
            ...     trigger=trigger,
            ...     )
            >>> t_i_rand.minimum
            0.0

        Returns ugen input.
        """
        index = self._ordered_input_names.index('minimum')
        return self._inputs[index]

    @property
    def trigger(self):
        """
        Gets `trigger` input of TIRand.

        ::

            >>> trigger = ugentools.Impulse.ar()
            >>> t_i_rand = ugentools.TIRand.ar(
            ...     minimum=0,
            ...     maximum=127,
            ...     trigger=trigger,
            ...     )
            >>> t_i_rand.trigger
            OutputProxy(
                source=Impulse(
                    calculation_rate=CalculationRate.AUDIO,
                    frequency=440.0,
                    phase=0.0
                    ),
                output_index=0
                )

        Returns ugen input.
        """
        index = self._ordered_input_names.index('trigger')
        return self._inputs[index]


from PIL import Image
from ctypes import c_float, byref, create_string_buffer, cast, POINTER, c_ubyte
import pypdfium2 as pdfium
from .tools import lazyproperty, get_error_message
from .pdf_text_page import PDFTextPage
import weakref

def _close_page(pageref):
    page = pageref()
    if page:
        page.close()

class PDFPage:
    def __init__(self, parent, ptr, pageno):
        self._parent = parent
        self._ptr = ptr
        self._pageno = pageno
        weakref.finalize(self, _close_page, weakref.ref(self))

    def close(self):
        if self._ptr:
            pdfium.FPDF_ClosePage(self._ptr)
            self._ptr = None

    def __del__(self):
        self.close()

    def __enter__(self):
        return self

    def __exit__(self, *exc):
        self.close()

    @classmethod
    def load(cls, doc, pageno):
        if not (0 <= pageno < doc.numpages):
            raise RuntimeError(f'Page number {pageno} is out of bounds')

        ptr = pdfium.FPDF_LoadPage(doc._ptr, pageno)
        if not ptr:
            err_message = get_error_message()
            raise RuntimeError(f'PDFPage.load: failed to load page {pageno}: {err_message}')

        return cls(doc, ptr, pageno)

    @lazyproperty
    def width(self):
        return pdfium.FPDF_GetPageWidthF(self._ptr)

    @lazyproperty
    def height(self):
        return pdfium.FPDF_GetPageHeightF(self._ptr)

    # @property
    # def bbox(self):
    #     rect = FPDF_RECT(0., 0., 0., 0.)
    #     if not so.FPDF_GetPageBoundingBox(self._page, pointer(rect)):
    #         err = so.RED_LastError()
    #         raise RuntimeError('internal error: %s' % err)
    #     return rect.left, rect.top, rect.right, rect.bottom

    @lazyproperty
    def crop_box(self):
        '''Page crop box.'''
        left, bottom, right, top = c_float(), c_float(), c_float(), c_float()

        mediabox = self.media_box
        rc = pdfium.FPDFPage_GetCropBox(self._ptr,
            byref(left), byref(bottom), byref(right), byref(top))
        if rc:
            cropbox = left.value, bottom.value, right.value, top.value
        else:
            cropbox = mediabox

        x0 = max(cropbox[0], mediabox[0])
        x1 = min(cropbox[2], mediabox[2])
        y0 = max(cropbox[1], mediabox[1])
        y1 = min(cropbox[3], mediabox[3])

        assert x0 < x1 and y0 < y1

        return x0, y0, x1, y1

    @lazyproperty
    def media_box(self):
        '''Page media box.'''
        left, bottom, right, top = c_float(), c_float(), c_float(), c_float()

        rc = pdfium.FPDFPage_GetMediaBox(self._ptr,
            byref(left), byref(bottom), byref(right), byref(top))
        if not rc:
            return (0, 0, 612, 792)

        return left.value, bottom.value, right.value, top.value

    @lazyproperty
    def rotation(self):
        '''Page rotation.

        * 0 - no rotation
        * 1 - rotated 90 degrees clock-wise
        * 2 - rotated 180 degrees clock-wise
        * 3 - rotated 270 degrees clock-wise
        '''
        return pdfium.FPDFPage_GetRotation(self._ptr)

    @lazyproperty
    def label(self):
        '''Page label.'''
        out = create_string_buffer(4096)
        l = pdfium.FPDF_GetPageLabel(self._parent._ptr, self._pageno + 1, out, 4096)
        if l < 4:
            return ''
        return out.raw[:l-2].decode('utf-16le')

    @staticmethod
    def _get_matrix(rotation, crop_box, rect, scale):
        '''PDFium renderer "conveniently" auto-rotates according to page.rotation value.
        We want to render page in native coordinate system (so that we can, for example,
        use word box coordinates to render the word). Hence we have to undo the "clever"
        transform that PDFium engine applies.
        '''
        cx0, cy0, cx1, cy1 = crop_box
        x0, y0, x1, y1 = rect
        #
        if rotation == 0:
            matrix = (scale, 0., 0., scale, (cx0-x0) * scale, (y1-cy1) * scale)
        elif rotation == 1:
            matrix = (0., -scale, scale, 0., (cx0-x0) * scale, (y1-cy0) * scale)
        elif rotation == 2:
            matrix = (-scale, 0., 0., -scale, (cx1-x0) * scale, (y1-cy0) * scale)
        elif rotation == 3:
            matrix = (0., scale, -scale, 0., (cx1-x0) * scale, (y1-cy1) * scale)
        else:
            raise RuntimeError('Unexpected rotation value: %s' % rotation)
        return matrix

    def render(self, scale=1.0, rect=None, optimize_for_lcd=False, grayscale=False, annot=False):
        '''Render page (or rectangle on the page) to memory (the pixel format is BGRx)

        Args:
            scale (float):      scale to use (default is 1.0, which will assume that 1pt takes 1px)
            rect (tuple):       optional rectangle to render. Value is a 4-tuple of (x0, y0, x1, y1) in PDF coordinates.
                                if None, then page's :attr:`crop_box` will be used for rendering.
        '''
        x0, y0, x1, y1 = self.crop_box if rect is None else rect
        matrix = pdfium.FS_MATRIX(*self._get_matrix(self.rotation, self.crop_box, (x0, y0, x1, y1), scale))

        width = int((x1 - x0) * scale + 0.5)
        height = int((y1 - y0) * scale + 0.5)
        cropper = pdfium.FS_RECTF(0, 0, width, height)

        bitmap = pdfium.FPDFBitmap_Create(width, height, 0)
        pdfium.FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF)
        # pdfium.FPDF_RenderPageBitmapWithMatrix(bitmap, self._ptr, matrix, cropper, pdfium.FPDF_LCD_TEXT | pdfium.FPDF_ANNOT)
        options = 0
        if optimize_for_lcd:
            options |= pdfium.FPDF_LCD_TEXT
        if grayscale:
            options |= pdfium.FPDF_GRAYSCALE
        if annot:
            options |= pdfium.FPDF_ANNOT
        pdfium.FPDF_RenderPageBitmapWithMatrix(bitmap, self._ptr, matrix, cropper, options)

        # retrieve data from bitmap
        buffer = pdfium.FPDFBitmap_GetBuffer(bitmap)
        buffer_ = cast(buffer, POINTER(c_ubyte * (width * height * 4)))
        img = Image.frombuffer("RGBA", (width, height), buffer_.contents, "raw", "BGRA", 0, 1)
        pdfium.FPDFBitmap_Destroy(bitmap)
        return img

    def text_page(self):
        return PDFTextPage.load(self)


from nailgun.test.base import BaseTestCase
from nailgun.utils.migration import upgrade_release_attributes_50_to_51
from nailgun.utils.migration import upgrade_release_roles_50_to_51

class TestDataMigration(BaseTestCase):

    def test_release_attributes_metadata_upgrade_50_to_51(self):
        attributes_metadata_50 = {
            'editable': {
                'storage': {
                    'volumes_ceph': {
                        'value': False,
                        'label': "Ceph RBD for volumes (Cinder)",
                        'description': "Configures Cinder to store "
                                       "volumes in Ceph RBD images.",
                        'weight': 20,
                        'type': "checkbox",
                        'conflicts': [
                            {"settings:common.libvirt_type.value": "vcenter"},
                            {"settings:storage.volumes_lvm.value": True}
                        ]
                    },
                    'objects_ceph': {
                        'value': False,
                        'label': "Ceph RadosGW for objects(Swift API)",
                        'description': "Configures RadosGW front end "
                                       "for Ceph RBD.",
                        'weight': 40,
                        'type': "checkbox",
                        'depends': [
                            {"settings:storage.images_ceph.value": True}
                        ],
                        'conflicts': [
                            {"settings:common.libvirt_type.value": "vcenter"}
                        ]
                    }
                }
            }
        }

        attributes_metadata_51 = upgrade_release_attributes_50_to_51(
            attributes_metadata_50
        )

        storage_attrs = attributes_metadata_51["editable"]["storage"]
        self.assertEqual(
            storage_attrs['volumes_ceph'].get("restrictions"),
            [
                "settings:common.libvirt_type.value == 'vcenter'",
                "settings:storage.volumes_lvm.value == true"
            ]
        )
        self.assertEqual(
            storage_attrs['objects_ceph'].get("restrictions"),
            [
                "settings:storage.images_ceph.value != true",
                "settings:common.libvirt_type.value == 'vcenter'"
            ]
        )

    def test_release_roles_metadata_upgrade_50_to_51(self):
        roles_metadata_50 = {
            'mongo': {
            'name': "Telemetry - MongoDB",
            'description': "A feature-complete and recommended "
                           "database for storage of metering data "
                           "from OpenStack Telemetry (Ceilometer)",
            'conflicts': ['compute',
                          'ceph-osd',
                          'zabbix-server'],
            'depends': [
                {'condition':
                    {"settings:additional_components.ceilometer.value": True},
                 'warning': "Ceilometer should be enabled"}]

            }
        }

        roles_metadata_51 = upgrade_release_roles_50_to_51(
            roles_metadata_50
        )

        self.assertEqual(
            roles_metadata_51["mongo"]["depends"],
            [
                {'condition':
                    "settings:additional_components.ceilometer.value == true",
                 'warning': "Ceilometer should be enabled"}
            ]
        )


from collections import defaultdict
import numpy as np

class Histogram(object):
    def __init__(self, ritual, boxColors, images, saveas, initRange=None, **ignore):
        self.ritual = ritual
        self.boxColors = boxColors
        self.imageIds = ritual.__dict__.get(images,{})
        if saveas not in self.ritual.__dict__:
            self.ritual.__dict__[saveas] = []
        self.data = self.ritual.__dict__[saveas]
        if initRange:
            self.x0 = initRange[0]
            self.w = initRange[1] - initRange[0]
        elif self.data:
            self.x0 = min([ i['x'] for i in self.data ])
            self.w = max([ i['x'] for i in self.data ]) - self.x0 + 1
        else:
            # Should never happen
            self.x0 = 42
            self.w = 42

    def from_client(self, data, users):
        x = int(data.get('x'))
        imgId = self.imageIds.get(','.join(users)) or 0
        self.data.append({'x':x,'imgId':imgId})
        if x < self.x0:
            d = self.x0 - x
            self.x0 -= d
            self.w += d
        elif x >= self.x0 + self.w:
            self.w = (x - self.x0) + 1
        if hasattr(self.ritual,'participants') and self.ritual.participants[0] in users:
            self.ritual.rotateSpeakers();

    def to_client(self, clientId, have):
        WF = 15
        wi = int(np.ceil(self.w / WF))
        y = np.zeros(self.w+wi, 'float32')
        outimgs = []
        for i in self.data:
            imgId = i['imgId']
            x = (i['x']-self.x0)
            w = 100 / WF
            h = w * self.ritual.jpgrats[imgId]
            print(x,wi,x+wi)
            base = y[x:x+wi].max()
            y[x:x+wi] = base + h
            print(x,(x+wi),base)
            xm = x * 100 / (self.w+wi)
            outimgs.append({'imgId':imgId, 'x':xm, 'y':float(base), 'w':w, 'h':h})
        dx = self.w / 10
        dxexp = 10 ** int(np.log(dx)/np.log(10))
        dxm = dx / dxexp
        if dxm < 1.4:
            dxm = 1
        elif dxm < 2.5:
            dxm = 2
        else:
            dxm = 5
        dx = dxm * dxexp
        axs = int(np.ceil(self.x0/dx)*dx)
        xaxv = range(axs, self.x0+self.w, dx)
        xax = [ {"x":(v+wi/2-self.x0) * 100 / (self.w+wi), "v":v}
                for v in xaxv ]
        return {
            "widget": "Histogram",
            "boxColors": self.boxColors,
            "imgs": outimgs,
            "xaxes": xax
        }

    def subpagesame(self, subhave, clientId):
        return int(subhave) == len(self.data)


from typing import List

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        m, n, x, y, d = len(matrix), len(matrix[0]), 0, 0, 0
        res = []
        visited = [[False] * n for _ in range(m)]
        for i in range(m * n):
            res.append(matrix[x][y])
            visited[x][y] = True
            nx = x + directions[d][0]
            ny = y + directions[d][1]
            if not (0 <= nx < m and 0 <= ny < n and not visited[nx][ny]):
                d = (d + 1) % 4
                nx = x + directions[d][0]
                ny = y + directions[d][1]
            x, y = nx, ny
        return res


import dis, types

def unpack_op(bytecode):
    '''Unpacks the offset, opcode and opaarg for each pair of bytes in the bytecode.

    Parameters
    =========================================
    bytecode (bytes): the bytecode of a code object

    Generates
    =========================================
    tuple: a tuple of offset, opcode and oparg for each pair of bytes in the bytecode
    '''
    extended_arg = 0
    for i in range(0, len(bytecode), 2):
        opcode = bytecode[i]
        if opcode >= dis.HAVE_ARGUMENT:
            oparg = bytecode[i+1] | extended_arg
            extended_arg = (oparg << 8) if opcode == dis.EXTENDED_ARG else 0
        else:
            oparg = None
        yield (i, opcode, oparg)

def find_linestarts(codeobj):
    """Finds the offsets in a bytecode which are the start a line in the source code.
    Parameters
    =========================================
    codeobj (code): code object

    Returns
    =========================================
    dict: a dictionary with offsets as the keys and their line numbers as their values of offsets
    """
    byte_increments = codeobj.co_lnotab[0::2]
    line_increments = codeobj.co_lnotab[1::2]
    byte = 0
    line = codeobj.co_firstlineno
    linestart_dict = {byte: line}
    for byte_incr, line_incr in zip(byte_increments,
                                    line_increments):
        byte += byte_incr
        if line_incr >= 0x80:
            line_incr -= 0x100
        line += line_incr
        linestart_dict[byte]=line
    return linestart_dict

def get_argvalue(offset, codeobj, opcode, oparg):
    '''Finds the human friendly meaning of each oparg in an instruction.

    Parameters
    =========================================
    offset (int): offset of the instruction
    codeobj (code): code object
    opcode (int): opcode of the instruction
    oparg (int): oparg of the instruction

    Returns
    =========================================
    argval: the human friendly meaning of the oparg in an instruction.
    '''
    constants= codeobj.co_consts
    varnames = codeobj.co_varnames
    names = codeobj.co_names
    cell_names = codeobj.co_cellvars + codeobj.co_freevars
    argval = None
    if opcode in dis.hasconst:
        if constants is not None:
            argval = constants[oparg]
            if type(argval)==str or argval==None:
                 argval = repr(argval)
    elif opcode in dis.hasname:
        if names is not None:
            argval = names[oparg]
    elif opcode in dis.hasjrel:
        argval = offset + 2 + oparg
        argval = "to " + repr(argval)
    elif opcode in dis.haslocal:
        if varnames is not None:
            argval = varnames[oparg]
    elif opcode in dis.hascompare:
        argval = dis.cmp_op[oparg]
    elif opcode in dis.hasfree:
        if cell_names is not None:
            argval = cell_names[oparg]
    return argval

def findlabels(codeobj):
    '''Finds all the offsets in the bytecode which are jump targets.

    Parameters
    =========================================
    codeobj (code): code object

    Returns
    =========================================
    list: list of offsets
    '''
    bytecode = codeobj.co_code
    labels = []
    for offset, opcode, oparg in unpack_op(bytecode):
            if opcode in dis.hasjrel:
                label = offset + 2 + oparg
            elif opcode in dis.hasjabs:
                label = oparg
            else:
                continue
            if label not in labels:
                labels.append(label)
    return labels

def disassemble(c):
    '''Disassebmles and prints the bytecode of a code object.

    Parameters
    =========================================
    c (code): code object
    '''
    if not(hasattr(c, 'co_code')):
        raise TypeError("The argument should be a code object")
    code_objects = []
    linestarts = find_linestarts(c)
    labels = findlabels(c)
    bytecode = c.co_code
    extended_arg = 0
    for offset, opcode, oparg in unpack_op(bytecode):
        argvalue = get_argvalue(offset, c, opcode, oparg)
        if hasattr(argvalue, 'co_code'):
            code_objects.append(argvalue)
        line_start = linestarts.get(offset, None)
        dis_text =  "{0:4}{1:2}{2:5} {3:<22} {4:3} {5}".format(str(line_start or ''),
                                                        ">>" if offset in labels else "",
                                                        offset, dis.opname[opcode],
                                                        oparg if oparg is not None else '',
                                                        "(" + str(argvalue) + ")" if argvalue is not
                                                                                None else '')
        if (line_start and offset):
            print()
        print(dis_text)
    for oc in code_objects:
        print("\nDisassembly of{}:\n".format(oc))
        disassemble(oc)

def disassemble_to_list(c):
    '''Disassebmles the bytecode of a code object and returns the result as a list.

    Parameters
    =========================================
    c (code): code object

    Returns
    =========================================
    list: disassembled bytecode instructions
    '''
    code_list = []
    bytecode = c.co_code
    for offset, opcode, oparg in unpack_op(bytecode):
        argval = get_argvalue(offset, c, opcode, oparg)
        if argval is not None:
            if type(argval)==str:
                argval = argval.strip("\'")
            argval = None if argval=='None' else argval
            code_list.append([dis.opname[opcode], argval])
        else:
            if oparg is not None:
                code_list.append([dis.opname[opcode], oparg])
            else:
                code_list.append([dis.opname[opcode]])
    return code_list

def get_oparg(offset, opcode, argval, constants, varnames, names, cell_names):
    '''Finds the oparg of an instruction based on its human friendly meaning.

    Parameters
    =========================================
    offset (int): offset of the instruction
    opcode (int): opcode of the instruction
    argval: human friendly meaning of the oparg
    constants (tuple): co_consts attribute of the code object
    varnames (tuple): co_varnames attribute of the code object
    names (tuple): co_names attribute of the code object
    cell_names (tuple): co_freevars + co_cellvars attributes of the code object

    Returns
    =========================================
    int: the oparg of the instruction
    '''
    oparg = argval
    if opcode in dis.hasconst:
        if constants is not None:
            oparg = constants.index(argval)
    elif opcode in dis.hasname:
        if names is not None:
            oparg = names.index(argval)
    elif opcode in dis.hasjrel:
        argval = int(argval.split()[1])
        oparg = argval - offset - 2
    elif opcode in dis.haslocal:
        if varnames is not None:
            oparg = varnames.index(argval)
    elif opcode in dis.hascompare:
        oparg = dis.cmp_op.index(argval)
    elif opcode in dis.hasfree:
        if cell_names is not None:
            oparg = cell_names.index(argval)
    return oparg

def assemble(code_list, constants, varnames, names, cell_names):
    '''Assembles the bytecode list into a bytes literal.

    Parameters
    =========================================
    code_list (list): disassembled bytecode list
    constants (tuple): co_consts attribute of the code object
    varnames (tuple): co_varnames attribute of the code object
    names (tuple): co_names attribute of the code object
    cell_names (tuple): co_freevars + co_cellvars attributes of the code object

    Returns
    =========================================
    bytes: the bytes literal of the disassembled bytescode list
    '''
    byte_list = []
    for i, instruction in enumerate(code_list):
        if len(instruction)==2:
            opname, argval = instruction
            opcode = dis.opname.index(opname)
            oparg = get_oparg(i*2, opcode, argval, constants, varnames, names, cell_names)
        else:
            opname = instruction[0]
            opcode = dis.opname.index(opname)
            oparg = 0
        byte_list += [opcode, oparg]
    return(bytes(byte_list))

def tail_recursion(f):
    '''
    A decorator for the tail-call optimization of the target function.

    Parameters
    =========================================
    f (function): target function which should be tail-recursive

    Returns
    =========================================
    function: the input function with a new code object and modified bytecode which is tail-call optimized
    '''
    c = f.__code__
    disassembled_bytecode = disassemble_to_list(c)
    i = 0
    func_name = c.co_name
    while i < len(disassembled_bytecode):
        if disassembled_bytecode[i] == ['LOAD_GLOBAL', func_name]:
            tail_rec_flag = False
            for index, instruction in enumerate(disassembled_bytecode[i+1:]):
                # Recursive calls with positional-only arguments
                if (instruction[0] == "CALL_FUNCTION") and (disassembled_bytecode[i+index+2] == ["RETURN_VALUE"]):
                    args_count = instruction[1]
                    new_bytecode = [['STORE_FAST', var] for var in list(c.co_varnames)[args_count::-1]]
                    new_bytecode += [['POP_TOP'], ['JUMP_ABSOLUTE', 0]]
                    disassembled_bytecode[i+index+1:i+index+3] = new_bytecode
                    tail_rec_flag = True

                    # Updating the jump targets
                    for inst in disassembled_bytecode:
                        opcode = dis.opname.index(inst[0])
                        if opcode in dis.hasjrel:
                            jump_offset = int(inst[1].split("to")[1])
                            if jump_offset>(i+index+1)*2:
                                inst[1] = "to " + str((len(new_bytecode) - 2)*2 + jump_offset)
                        if opcode in dis.hasjabs:
                            if inst[1]>(i+index+1)*2:
                                inst[1] += (len(new_bytecode) - 2) * 2

                    i += index + len(new_bytecode) + 1
                    break

                # Recursive calls with positional and keyword arguments
                elif (instruction[0] == "CALL_FUNCTION_KW") and (disassembled_bytecode[i+index+2] == ["RETURN_VALUE"]):
                    args_count = instruction[1]
                    kwargs_tuple = disassembled_bytecode[i+index][1]
                    posargs_count = args_count - len(kwargs_tuple)
                    new_bytecode = [["POP_TOP"]]
                    new_bytecode += [['STORE_FAST', var] for var in list(kwargs_tuple)[len(kwargs_tuple)::-1]]
                    if posargs_count != 0:
                        new_bytecode += [['STORE_FAST', var] for var in list(c.co_varnames)[posargs_count-1::-1]]
                    new_bytecode += [['POP_TOP'], ['JUMP_ABSOLUTE', 0]]
                    disassembled_bytecode[i+index+1:i+index+3] = new_bytecode
                    tail_rec_flag = True

                    # Updating the jump targets
                    for inst in disassembled_bytecode:
                        opcode = dis.opname.index(inst[0])
                        if opcode in dis.hasjrel:
                            jump_offset = int(inst[1].split("to")[1])
                            if jump_offset>(i+index+1)*2:
                                inst[1] = "to " + str((len(new_bytecode) - 2)*2 + jump_offset)
                        if opcode in dis.hasjabs:
                            if inst[1]>(i+index+1)*2:
                                inst[1] += (len(new_bytecode) - 2) * 2

                    i += index + len(new_bytecode) + 1
                    break
            if tail_rec_flag == False:
                raise Exception("The decorated function is not tail-recursive")
        i += 1

    # Assembling the new bytecode
    new_co_code= assemble(disassembled_bytecode, c.co_consts,
                      c.co_varnames, c.co_names,
                      c.co_cellvars+c.co_freevars)

    # Creating a new code object
    nc = types.CodeType(c.co_argcount, c.co_kwonlyargcount,
                        c.co_nlocals, c.co_stacksize, c.co_flags,
                        new_co_code, c.co_consts, c.co_names,
                        c.co_varnames, c.co_filename, c.co_name,
                        c.co_firstlineno, c.co_lnotab,
                        cellvars = c.co_cellvars, freevars = c.co_freevars)
    f.__code__ = nc

    return f


from google.appengine.ext import webapp

import pages.pages
import pages.actions
import pages.tasks
import pages.cron
import pages.update

application = webapp.WSGIApplication([
    ('/', pages.pages.PageIndex),
    ('/gallery', pages.pages.PageGallery),
    ('/admin', pages.pages.PageAdmin),
    ('/admin/delete-comment', pages.actions.ActionDeleteComment),
    ('/json/admin/save-image-properties', pages.actions.JSONActionAdminSetArtworkProperties),
    ('/admin/update-profile-nickname', pages.actions.AdminUpdateUserNickname),
    ('/admin/updateUserFavorites', pages.actions.ActionAdminUpdateUserFavoritesCount),
    ('/admin/updateArtworkFavorites', pages.actions.ActionAdminUpdateArtworkFavoriteCount),
    ('/admin/update-iterate', pages.pages.PageAdminUpdateIterate),
    ('/admin/update-iterate-do', pages.actions.ActionUpdateIterate),
    ('/admin/update-tags', pages.update.ActionUpdateTags),
    ('/admin/hide-comment', pages.actions.ActionHideComment),
    ('/admin/show-comment', pages.actions.ActionShowComment),
    ('/admin/block-user', pages.actions.ActionAdminBlockUser),
    ('/admin/unblock-user', pages.actions.ActionAdminUnblockUser),
    ('/admin/send-message', pages.actions.ActionAdminSendMessageToUser),
    ('/admin/refresh-index-page', pages.actions.ActionAdminFlushMemcacheForIndexPage),
    ('/admin/update-editor-choice', pages.actions.ActionUpdateEditorChoice),
    ('/admin/tags', pages.pages.PageAdminTags),
    ('/admin/tasks/(.*)', pages.pages.PageAdminTaskStatus),
    ('/save-settings', pages.actions.ActionSaveSettings),
    ('/new-image', pages.pages.PageNewImage),
    ('/painter', pages.pages.PagePainter),
    ('/my-images', pages.pages.PageMyImages),
    ('/news', pages.pages.PageNews),
    ('/newsfeed', pages.pages.PageNewsFeed),
    ('/privacy-policy', pages.pages.PagePrivacyPolicy),
    ('/rules', pages.pages.PageRules),
    ('/history', pages.pages.PageHistory),
    ('/delete-image', pages.actions.ActionDeleteImage),
    ('/save-comment', pages.actions.ActionSaveComment),
    ('/complain-comment', pages.actions.ActionComlainComment),
    ('/tag-typeahead', pages.actions.ActionTagTypeahead),
    ('/images/details/(.*)', pages.pages.PageImage),
    ('/images/png/(.*)', pages.actions.PNGImageRequest),
    ('/images/svg/(.*).svg', pages.actions.SVGImageRequest),
    ('/images/json/(.*).json', pages.actions.JSONImageRequest),
    ('/images/avatar/(.*).jpg', pages.actions.AvatarImageRequest),
    ('/tags', pages.pages.PageGlobalTags),
    ('/tags/(.*)/images', pages.pages.PageTagImages),
    ('/notifications', pages.pages.PageNotifications),
    ('/my-profile', pages.pages.PageMyProfile),
    ('/upload-avatar', pages.actions.ActionUploadUserAvatar),
    ('/profiles', pages.pages.PageProfiles),
    ('/profiles/', pages.pages.PageProfiles),
    ('/profiles/by-artwork-count', pages.pages.PageUsersByArtworksCount),
    ('/profiles/by-stars-count', pages.pages.PageUsersByFavortiesCount),
    ('/profiles/blocked', pages.pages.PageUsersBlocked),
    ('/profiles/(.*)/comments', pages.pages.PageUserComments),
    ('/profiles/(.*)/favorites', pages.pages.PageUserFavorites),
    ('/profiles/(.*)/followers', pages.pages.PageUserFollowers),
    ('/profiles/(.*)/leaders', pages.pages.PageUserLeaders),
    ('/profiles/(.*)/tags', pages.pages.PageUserTags),
    ('/profiles/(.*)/tags/(.*)/images', pages.pages.PageUserTagImages),
    ('/profiles/(.*)/images', pages.pages.PageUserImages),
    ('/profiles/(.*)/group-images', pages.pages.PageUserGroupImages),
    ('/top-favorites', pages.pages.PageTopFavorites),
    ('/last-week-favorites', pages.pages.PageLastWeekFavorites),
    ('/last-month-favorites', pages.pages.PageLastMonthFavorites),
    ('/profiles/(.*)', pages.pages.PageProfile),
    ('/editor-choice', pages.pages.PageEditorChoice),
    ('/favorites', pages.pages.PageRecentFavorites),
    ('/help/collaborators', pages.pages.PageHelpCollaborators),
    ('/toggle-favorite', pages.actions.ActionToggleFavorite),
    ('/json/comments', pages.actions.JSONComments),
    ('/json/get-comment-content',  pages.actions.JSONGetCommentContent),
    ('/json/delete-notifications', pages.actions.JSONDeleteNotifications),
    ('/json/get-user-id-by-nickname', pages.actions.JSONGetUserIdByNickname),
    ('/json/save-image', pages.actions.JSONActionSaveImage),
    ('/json/save-image-tags', pages.actions.JSONActionSaveImageTags),
    ('/json/save-alternative-email', pages.actions.JSONSaveAlternativeEmail),
    ('/json/set-user-tag-cover', pages.actions.JSONSetTagCover),
    ('/json/delete-alternative-email', pages.actions.JsonDeleteAlternativeEmail),
    ('/json/notifications', pages.actions.JSONNotifications),
    ('/json/followers', pages.actions.JSONFollowers),
    ('/json/leaders', pages.actions.JSONLeaders),
    ('/json/delete-avatar', pages.actions.JSONDeleteUserAvatar),
    ('/json/save-profile', pages.actions.JSONSaveProfile),
    ('/json/self-block', pages.actions.JSONActionSelfBlock),
    ('/json/delete-tag', pages.actions.JSONAdminDeleteTag),
    ('/json/rename-tag', pages.actions.JSONAdminRenameTag),
    ('/json/invite-collaborator', pages.actions.JSONInviteCollaborator),
    ('/json/accept-notification', pages.actions.JSONAcceptNotification),
    ('/json/reject-notification', pages.actions.JSONRejectNotification),
    ('/json/resign-collaborator', pages.actions.JSONResignCollaborator),
    ('/json/collaborators', pages.actions.JSONArtworkCollaborators),
    ('/json/dismiss-collaborator', pages.actions.JSONDismissCollaborator),
    ('/json/artwork-previous-comments', pages.actions.JSONArtworkDetailsComments),
    ('/comments', pages.pages.PageComments),
    ('/follow', pages.actions.ActionFollow),
    ('/unfollow', pages.actions.ActionUnfollow),
    ('/cron/clean-notifications', pages.cron.CronCleanNotifications),
    ('/cron/clean-news', pages.cron.CronCleanOldNews),
    ('/cron/update-artworks', pages.cron.CronUpdateArtworks),
    ('/cron/update-tags', pages.cron.CronUpdateGlobalTags),
    ('/cron/update-daily-counters', pages.cron.CronUpdateDailyCounters),
    ('/cron/calculate-last-week-top', pages.cron.CronCalculateLastWeekTop),
    ('/cron/calculate-last-month-top', pages.cron.CronCalculateLastMonthTop),
    ('/tasks/add-artwork-to-news', pages.tasks.TaskAddArtworkToNews),
], debug=True)


from PySide6 import QtWidgets, QtCore
from pluck.field import (
    FieldPanel, StrField, IntField, ColorField, IntVectorComboField,
    StrArrayField)

GAME_SETTINGS_FIELDS = {
    "title": StrField,
    "start_scene": StrField,
    "start_scrolling_targets": StrArrayField,
    "fade_in_duration": IntField
}

PEFERENCES_FIELDS = {
    "resolution": IntVectorComboField,
    "fps": IntField,
    "block_size": IntField,
    "key_color": ColorField,
    "camera_speed": IntField
}

class ProjectManager(QtWidgets.QWidget):
    edited = QtCore.Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self._settings = FieldPanel(GAME_SETTINGS_FIELDS)
        self._settings.edited.connect(self.edited.emit)
        self._preferences = FieldPanel(PEFERENCES_FIELDS)
        self._preferences.edited.connect(self.edited.emit)
        self.tab = QtWidgets.QTabWidget()
        self.tab.addTab(self._settings, "Project Settings")
        self.tab.addTab(self._preferences, "Preferences")
        self.data_traceback = QtWidgets.QLabel()
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.addWidget(self.tab)
        self.layout.addWidget(self.data_traceback)

    @property
    def settings(self):
        return self._settings.values

    @settings.setter
    def settings(self, values):
        self._settings.values = values

    @property
    def preferences(self):
        return self._preferences.values

    @preferences.setter
    def preferences(self, values):
        self._preferences.values = values

    def set_error(self, text):
        self.data_traceback.setText(text)
        self.data_traceback.setStyleSheet("background-color:red")

    def clean_error(self):
        self.data_traceback.setText("")
        self.data_traceback.setStyleSheet("")


from translate import google_translate
from datasets import Dataset, DataLoader
from translator import Translator
from tools.nlp import pydict_file_write
import argparse
from datetime import date
import os

# google translation flag
GOOGLE_TRANSLATION = False

# translate function
def translate_fn(data):
  prompt = data['conversations'][0]['value']
  response = data['conversations'][1]['value']
  if GOOGLE_TRANSLATION:
    translated_prompt = google_translate(prompt)
    translated_response = google_translate(response)
  else:
    translated_prompt = prompt
    translated_response = response
  data['conversations'][0]['value'] = translated_prompt
  data['conversations'][1]['value'] = translated_response
  print('translate {}'.format(data['id']))
  return data

# save function
def save_fn(outputs, output_path):
  pydict_file_write(outputs, output_path)

def get_args():
  parser = argparse.ArgumentParser()
  parser.add_argument("--input_path", type=str, default=None)
  parser.add_argument("--output_dir", type=str, default='/tmp')
  parser.add_argument("--start_id", type=int, default=-1)
  parser.add_argument("--end_id", type=int, default=-1)
  parser.add_argument("--id_ref_path", type=str, default=None)
  parser.add_argument("--known_ids", type=str, default=None)
  parser.add_argument("--data_uid", type=str, default=None)
  parser.add_argument("--data_name", type=str, default=None)
  return parser.parse_args()

def main():

  # parameters
  args = get_args()

  # output path
  today = '{}'.format(date.today())
  output_name = f'data.{args.data_uid}.{args.data_name}.{today}.pydict'
  if args.end_id > args.start_id:
    output_name = output_name.replace(
        '.pydict', f'_s{args.start_id}_e{args.end_id}.pydict')
  elif args.id_ref_path is not None:
    output_name = os.path.basename(args.input_path)
    output_name = output_name.replace('.pydict', f'_missed.pydict')
  elif args.known_ids:
    output_name = output_name.replace('.pydict', f'_kwn_ids.pydict')
  output_path = os.path.join(args.output_dir, output_name)

  # dataset
  known_ids = [int(i)
               for i in args.known_ids.split(',')] if args.known_ids else []
  dataset = Dataset(args.input_path, args.start_id, args.end_id,
                    args.id_ref_path, known_ids)
  dataloader = DataLoader(dataset, collate_fn=lambda x: x)

  # translation
  translator = Translator(dataloader=dataloader,
                          translate_fn=translate_fn,
                          save_fn=save_fn,
                          output_path=output_path)
  translator.translate()

  # save translation results
  translator.save(sorting=True)

if __name__ == '__main__':
  main()


import models.quantization
from constants import TRANSFORMER_URL

import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        prog="rofi_semantic scripts",
        description="Execute various bits and pieces of this package"
    )

    sub_parsers = parser.add_subparsers(
        help="Different modes for the package",
        dest="command_name"
    )

    parser_quantize = sub_parsers.add_parser(
        'quantize',
        aliases=['q'],
        description='Quantize the model for better execution performance on CPU'
    )

    parser_quantize.add_argument(
        '--save_dir', '-s',
        default='.models/',
        type=str
    )

    parser_quantize.add_argument(
        '--model', '-m',
        default=TRANSFORMER_URL,
        type=str
    )

    args = parser.parse_args()

    if args.command_name == 'quantize':
        models.quantization.quantize(args.save_dir, args.model)


import numpy as np
from ch11utilities import print_pass
from matplotlib import animation
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import sys
# from ch11soundutilities import play_sound

def update(frame_data):
    """Display bars representing the current state."""
    # unpack info for graph update
    data, colors, swaps, comparisons = frame_data
    plt.cla()  # clear old contents of current Figure

    # create barplot and set its xlabel
    bar_positions = np.arange(len(data))
    axes = sns.barplot(bar_positions, data, palette=colors)  # new bars
    axes.set(xlabel=f'Comparisons: {comparisons}; Swaps: {swaps}',
             xticklabels=data)

def flash_bars(index1, index2, data, colors, swaps, comparisons):
    """Flash the bars about to be swapped and play their notes."""
    for x in range(0, 2):
        colors[index1], colors[index2] = 'white', 'white'
        yield (data, colors, swaps, comparisons)
        colors[index1], colors[index2] = 'purple', 'purple'
        yield (data, colors, swaps, comparisons)
        # play_sound(data[index1], seconds=0.05)
        # play_sound(data[index2], seconds=0.05)

def selection_sort(data):
    """Sort data using the selection sort algorithm and
    yields values that update uses to visualize the algorithm."""
    swaps = 0
    comparisons = 0
    colors = ['lightgray'] * len(data)  # list of bar colors

    # display initial bars representing shuffled values
    yield (data, colors, swaps, comparisons)

    # loop over len(data) - 1 elements
    for index1 in range(0, len(data) - 1):
        smallest = index1

        # loop to find index of smallest element's index
        for index2 in range(index1 + 1, len(data)):
            comparisons += 1
            colors[smallest] = 'purple'
            colors[index2] = 'red'
            yield (data, colors, swaps, comparisons)
            # play_sound(data[index2], seconds=0.05)

            # compare elements at positions index and smallest
            if data[index2] < data[smallest]:
                colors[smallest] = 'lightgray'
                smallest = index2
                colors[smallest] = 'purple'
                yield (data, colors, swaps, comparisons)
            else:
                colors[index2] = 'lightgray'
                yield (data, colors, swaps, comparisons)

        # ensure that last bar is not purple
        colors[-1] = 'lightgray'

        # flash the bars about to be swapped
        yield from flash_bars(index1, smallest, data, colors,
                              swaps, comparisons)

        # swap the elements at positions index1 and smallest
        swaps += 1
        data[smallest], data[index1] = data[index1], data[smallest]

        # flash the bars that were just swapped
        yield from flash_bars(index1, smallest, data, colors,
                              swaps, comparisons)

        # indicate that bar index1 is now in its final spot
        colors[index1] = 'lightgreen'
        yield (data, colors, swaps, comparisons)

    # indicate that last bar is now in its final spot
    colors[-1] = 'lightgreen'
    yield (data, colors, swaps, comparisons)
    # play_sound(data[-1], seconds=0.05)

    # play each bar's note once and color it darker green
    for index in range(len(data)):
        colors[index] = 'green'
        yield (data, colors, swaps, comparisons)
        # play_sound(data[index], seconds=0.05)

def main():
    number_of_values = int(sys.argv[1]) if len(sys.argv) == 2 else 10

    figure = plt.figure('Selection Sort')  # Figure to display barplot
    numbers = np.arange(1, number_of_values + 1)  # create array
    np.random.shuffle(numbers)  # shuffle the array

    # start the animation
    anim = animation.FuncAnimation(figure, update, repeat=False,
        frames=selection_sort(numbers), interval=50)

    plt.show()  # display the Figure

# call main if this file is executed as a script
if __name__ == '__main__':
    main()

# def square_generator(values):
#     for value in values:
#         yield value ** 2

# squares = square_generator([1,2,3,4])

# print(squares)
# # <generator object square_generator at 0x7f7aeaed0270>

# for number in squares:
#     print(number, end=' ')
# # 1 4 9 16

# print()

# squares = square_generator([4,3,2,1])

# print( next(squares) )
# # 16
# print( next(squares) )
# # 9
# print( next(squares) )
# # 4
# print( next(squares) )
# # 1

# # print( next(squares) ) #Error StopIteration

# ----------------------------------------------------
# Merge Sort
# ----------------------------------------------------

# # calls recursive sort_array method to begin merge sorting
# def merge_sort(data):
#     sort_array(data, 0, len(data) - 1)

# def sort_array(data, low, high):
#     """Split data, sort subarrays and merge them into sorted array."""
#     # test base case size of array equals 1
#     if (high - low) >= 1:  # if not base case
#         middle1 = (low + high) // 2  # calculate middle of array
#         middle2 = middle1 + 1  # calculate next element over

#         # output split step
#         print(f'split:   {subarray_string(data, low, high)}')
#         print(f'         {subarray_string(data, low, middle1)}')
#         print(f'         {subarray_string(data, middle2, high)}\n')

#         # split array in half then sort each half (recursive calls)
#         sort_array(data, low, middle1)  # first half of array
#         sort_array(data, middle2, high)  # second half of array

#         # merge two sorted arrays after split calls return
#         merge(data, low, middle1, middle2, high)

# # merge two sorted subarrays into one sorted subarray
# def merge(data, left, middle1, middle2, right):
#     left_index = left  # index into left subarray
#     right_index = middle2  # index into right subarray
#     combined_index = left  # index into temporary working array
#     merged = [0] * len(data)  # working array

#     # output two subarrays before merging
#     print(f'merge:   {subarray_string(data, left, middle1)}')
#     print(f'         {subarray_string(data, middle2, right)}')

#     # merge arrays until reaching end of either
#     while left_index <= middle1 and right_index <= right:
#         # place smaller of two current elements into result
#         # and move to next space in arrays
#         if data[left_index] <= data[right_index]:
#             merged[combined_index] = data[left_index]
#             combined_index += 1
#             left_index += 1
#         else:
#             merged[combined_index] = data[right_index]
#             combined_index += 1
#             right_index += 1

#     # if left array is empty
#     if left_index == middle2:  # if True, copy in rest of right array
#         merged[combined_index:right + 1] = data[right_index:right + 1]
#     else: # right array is empty, copy in rest of left array
#         merged[combined_index:right + 1] = data[left_index:middle1 + 1]

#     data[left:right + 1] = merged[left:right + 1]  # copy back to data

#     # output merged array
#     print(f'         {subarray_string(data, left, right)}\n')

# # method to output certain values in array
# def subarray_string(data, low, high):
#     temp = '   ' * low  # spaces for alignment
#     temp += ' '.join(str(item) for item in data[low:high + 1])
#     return temp

# def main():
#     data = np.array([35, 73, 90, 65, 23, 86, 43, 81, 34, 58])
#     print(f'Unsorted array: {data}\n')
#     merge_sort(data)
#     print(f'\nSorted array: {data}\n')

# # call main if this file is executed as a script
# if __name__ == '__main__':
#     main()

# ----------------------------------------------------
# Insertion Sort
# ----------------------------------------------------

# def insertion_sort(data):
#     """Sort an array using insertion sort."""
#     # loop over len(data) - 1 elements
#     for next in range(1, len(data)):
#         insert = data[next]  # value to insert
#         move_item = next  # location to place element

#         # search for place to put current element
#         while move_item > 0 and data[move_item - 1] > insert:
#             # shift element right one slot
#             data[move_item] = data[move_item - 1]
#             move_item -= 1

#         data[move_item] = insert  # place inserted element
#         print_pass(data, next, move_item)  # output pass of algorithm

# def main():
#     data = np.array([35, 73, 90, 65, 23, 86, 43, 81, 34, 58])
#     print(f'Unsorted array: {data}\n')
#     insertion_sort(data)
#     print(f'\nSorted array: {data}\n')

# # call main if this file is executed as a script
# if __name__ == '__main__':
#     main()

# ----------------------------------------------------
# Selection Sort
# ----------------------------------------------------

# """Sorting an array with selection sort."""
# import numpy as np

# def selection_sort(data):
#     """Sort array using selection sort."""
#     # loop over len(data) - 1 elements
#     for index1 in range(len(data) - 1):
#         smallest = index1  # first index of remaining array

#         # loop to find index of smallest element
#         for index2 in range(index1 + 1, len(data)):
#             if data[index2] < data[smallest]:
#                 smallest = index2

#         # swap smallest element into position
#         data[smallest], data[index1] = data[index1], data[smallest]
#         print_pass(data, index1 + 1, smallest)

# def main():
#     data = np.array([35, 73, 90, 65, 23, 86, 43, 81, 34, 58])
#     print(f'Unsorted array: {data}\n')
#     selection_sort(data)
#     print(f'\nSorted array: {data}\n')

# # call main if this file is executed as a script
# if __name__ == '__main__':
#     main()

# ----------------------------------------------------
# Binary Search
# ----------------------------------------------------

# def binary_search(data, key):
#     """Perform binary search of data looking for key."""
#     low = 0   # low end of search area
#     high = len(data) - 1  # high end of search area
#     middle = (low + high + 1) // 2  # middle element index
#     location = -1  # return value -1 if not found

#     # loop to search for element
#     while low <= high and location == -1:
#         # print remaining elements of array
#         print(remaining_elements(data, low, high))

#         print('   ' * middle, end='')  # output spaces for alignment
#         print(' * ')  # indicate current middle

#         # if the element is found at the middle
#         if key == data[middle]:
#             location = middle  # location is the current middle
#         elif key < data[middle]:  # middle element is too high
#             high = middle - 1  # eliminate the higher half
#         else:  # middle element is too low
#             low = middle + 1  # eliminate the lower half

#         middle = (low + high + 1) // 2  # recalculate the middle

#     return location  # return location of search key

# def remaining_elements(data, low, high):
#     """Display remaining elements of the binary search."""
#     return '   ' * low + ' '.join(str(s) for s in data[low:high + 1])

# def main():
#     # create and display array of random values
#     data = np.random.randint(10, 91, 15)
#     data.sort()
#     print(data, '\n')

#     search_key = int(input('Enter an integer value (-1 to quit): '))

#     # repeatedly input an integer; -1 terminates the program
#     while search_key != -1:
#         location = binary_search(data, search_key)  # perform search

#         if location == -1:  # not found
#             print(f'{search_key} was not found\n')
#         else:
#             print(f'{search_key} found in position {location}\n')

#         search_key = int(input('Enter an integer value (-1 to quit): '))

# # call main if this file is executed as a script
# if __name__ == '__main__':
#     main()

# ----------------------------------------------------
# Linear Search
# ----------------------------------------------------

# def linear_search(data, search_key):
#     for index, value in enumerate(data):
#         if value == search_key:
#             return index
#     return -1

# np.random.seed(11)

# values = np.random.randint(10, 91, 10)

# print(values)
# # [35 73 90 65 23 86 43 81 34 58]

# print( linear_search(values, 23) )
# # 4

# print( linear_search(values, 61) )
# # -1

# print( linear_search(values, 34) )
# # 8

# ----------------------------------------------------
# Recursive Fibonacci Series
# ----------------------------------------------------

# # Function fibonacci
# def fibonacci(n):
#     if n in (0, 1):  # base cases
#         return n
#     else:
#         return fibonacci(n - 1) + fibonacci(n - 2)

# # Testing Function fibonacci
# for n in range(41):
#     print(f'Fibonacci({n}) = {fibonacci(n)}')

# # Self Check Exercise 3
# def iterative_fibonacci(n):
#     result = 0
#     temp = 1
#     for j in range(0, n):
#         temp, result = result, result + temp
#     return result

# # %timeit fibonacci(30)
# # 807 ms ± 787 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)

# # %timeit iterative_fibonacci(30)
# # 5.11 µs ± 36 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

# ----------------------------------------------------
# Recursive Factorial
# ----------------------------------------------------

# def factorial(number):
#     """Return factorial of number."""
#     if number <= 1:
#         return 1
#     return number * factorial(number - 1)  # recursive call

# for i in range(11):
#     print(f'{i}! = {factorial(i)}')

# print(factorial(50))

# ----------------------------------------------------
# Factorial
# ----------------------------------------------------

# factorial = 1

# for number in range(5,0,-1):
#     factorial *= number

# # 5!
# print(factorial)
# # 120

Deitel P., Deitel H. - Intro to Python for Computer Science and Data Science - 2020/11_recursion_sorting.py


# Approach 1
import functools
import itertools
import operator

import numpy as np

# Approach 1

def find_max_average_1(arr, K):
    # P = [0]
    #
    # # Do cumulative addition at each index
    # for x in arr:
    #     P.append(P[-1] + x)
    # P[-1] gives the last index's value in the list

    # OR

    P = [0] + list(itertools.accumulate(arr))

    # ma = 0
    # for i in range(len(arr) - K + 1):
    #     ma = max(P[i + K] - P[i], ma)

    # This step substracts sumulative index with the another cumulative index, giving us Sum value of just we want.
    # ma = max(P[i+K] - P[i] for i in range(len(arr) - K + 1))

    # OR

    ma = max(map(operator.sub, P[K:], P[:-K]))

    return ma / float(K)

# Approach 2

def find_max_average_2(arr, K):
    window_sum, window_start = 0.0, -K
    max_sum = -float("inf")

    for window_end, value in enumerate(arr):
        window_sum += value
        window_start += 1

        if window_start >= 0:
            max_sum = max(window_sum, max_sum)
            window_sum -= arr[window_start]

    return max_sum / K

# Approach 3

def find_max_average_3(nums, k):
    sums = [0] + list(itertools.accumulate(nums))
    return max(map(operator.sub, sums[k:], sums)) / k

# Approach 4

def find_max_average_4(nums, k):
    sums = np.cumsum([0] + nums)
    # return int(max(sums[k:] - sums[:-k])) / k
    return max(map(operator.sub, sums[k:], sums[:-k])) / k

# Approach 5

def find_max_average(nums, k):
    # m = a = sum(nums[:k])
    # OR
    window_sum = functools.reduce(lambda x, y: x + y, nums[:k])
    max_sum = window_sum
    # for i in range( k, len(nums) ):
    #     a += ( nums[i] - nums[i-k] )
    #     if a > m: m = a
    for index, value in enumerate(nums[k:]):
        window_sum += value - nums[index]
        if window_sum > max_sum: max_sum = window_sum
    return max_sum / k

def main():
    print(find_max_average([1, 3, 2, 6, -1, 4, 1, 8, 2], 5))
    print(find_max_average([1, 12, -5, -6, 50, 3], 4))

main()

sliding_window/643. Maximum Average Subarray I.py


import numpy as np
import pandas as pd
import itertools
from datetime import timedelta

from dateutil.relativedelta import *
from datetime import date
from datetime import datetime

def formatarStr (DataFrame, columns):
    for col in columns:
        DataFrame[col] = DataFrame[col].str.strip()

def verificarStrInvalida (DataFrame, columns, strInvalida):
    print('Dados ausentes, por coluna: ')
    for col in columns:
        f_item = DataFrame[DataFrame[col] == strInvalida].shape[0]
        print('inválidos em {}: {}'.format(col, f_item))

def calcularIdade(dtNasc, dtAcid):
    try:
        if dtNasc == 0 or dtAcid == 0:
            return np.nan
        else:
            dtAcid = dtAcid.replace('/','')
            if len(dtAcid) == 8 :
                dtBase = datetime.strptime(str(dtAcid), '%d%m%Y').date()
            else:
                dtBase = datetime.strptime(str(dtAcid), '%Y%m').date()
            dob = datetime.strptime(str(dtNasc), '%d/%m/%Y').date()
            age = relativedelta(dtBase, dob)
            return age.years
    except (RuntimeError, TypeError, NameError, ValueError):
        return np.nan

def calcularAnoMes(dtAcid):
    if len(dtAcid) == 10:
        anoMesStr = dtAcid[6:]+dtAcid[3:5]
        return anoMesStr
    elif len(dtAcid) == 7:
        anoMesStr = dtAcid.replace('/', '')
        return anoMesStr
    else:
        return dtAcid

def recortarCNAE(cnaeStr):
    strSeparada = cnaeStr.rsplit(":")
    if len(strSeparada) > 1:
        return strSeparada[0]
    else:
        return cnaeStr

def calcularSomaAnoMes(dtStr):
    try:
        df_soma = df_result.query("anoMes == \""+dtStr+"\"" )
        total= df_soma['qtde'].sum()
        return total
    except (RuntimeError, TypeError, NameError, ValueError):
        return np.nan