import collections

class Solution:
  def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
    m = collections.Counter(nums1)
    result = []
    for num in nums2:
      if num in m:
        result.append(num)
        if m[num] == 1:
          del m[num]
        else:
          m[num] -= 1
    return result


import networkx as nx
import csv
# import matplotlib.pyplot as plt
from sys import argv, exit
import json
import os
import re
from collections import Counter
# import plotly.offline as py
# import plotly.graph_objs as go

efile = './dummyDataset/edges.csv'
if len(argv) > 1:
  efile = argv[1]
corenum = None
if len(argv) > 2:
  corenum = int(argv[2])
dataset = os.path.splitext(os.path.basename(efile))[0]
outputDir = './out/%s' % (efile)

def dumpData(data, fname):
  with open(fname, 'w') as fp:
    json.dump(data, fp)

def readEdges(fname):
  rows = []
  if fname.endswith('.txt'):
    with open(fname, 'r') as txtfile:
      for line in txtfile:
        if ((line.strip())[0]).isdigit():
          break
      line = txtfile.readline()
      delim = '[ \t]+'
      if ',' in line:
        delim = ','
      rows.append((list(
          map(lambda x: int(x.strip()), re.split(delim, line.strip()))))[:2])
      for line in txtfile:
        try:
          rows.append((list(
              map(lambda x: int(x.strip()), re.split(delim,
                                                     line.strip()))))[:2])
        except Exception as _:
          pass
  else:
    with open(fname, 'r') as csvfile:
      csvr = csv.reader(csvfile)
      for row in csvr:
        rows.append(list(map(int, row)))

  return rows

# def drawGraph(G, figname='graph.png'):
#   nx.draw(G, with_labels=True, font_weight='bold')
#   plt.savefig(figname)
#   plt.clf()

# def lineGraphs(y, label, title='line-graph', filename='line.png', clf=True):
#   plt.ylim(0, max(list(map(lambda ye: max(ye), y))))
#   xe = list(range(len(y[0])))
#   for i in range(0, 12, 2):
#     plt.plot(xe, y[i], '.-', label='B=%.1f' % (label[i]))
#   plt.legend()
#   plt.title(title)
#   plt.xlabel('G_k (k-core)')
#   plt.ylabel('NI (G_k, dep(i, β))')
#   plt.savefig(filename)
#   if clf:
#     plt.clf()

# def plotlyGraphs(y, label, title='line-graph', filename='line.png', clf=True):
#   layout = go.Layout(
#     title=title,
#     xaxis=dict(
#         title='G_k (k-core)s',
#     ),
#     yaxis=dict(
#         title='NI (G_k, dep(i, β))',
#     )
# )
#   x = list(range(len(y[0])))
#   trace = list(map(lambda i: go.Scatter(x=x, y=y[i], name=label[i]), list(range(len(y)))))
#   fig = go.Figure(data=trace, layout=layout)
#   py.plot(fig, filename=filename)

def getDep(G, cores, outputDir):
  betas = list(map(lambda x: x / 10, list(range(11))))
  nodes = G.nodes()
  edges = G.edges()
  k_max = max(cores.values())
  print('k_max = %d' %(k_max))
  ni = []
  aggni = []
  for beta in betas:
    print('+++ β = %0.1f' % (beta))
    depBeta = {v: 0 for v in nodes}
    for k in range(1, k_max + 1):
      depBetaK = {}
      for v in nodes:
        depBetaK[v] = depBeta[v]
        if k > cores[v]:
          continue
        nbrs = list(
            filter(lambda u: cores[u] == k, list(nx.all_neighbors(G, v))))
        depBetaK[v] += len(nbrs) + beta * sum(
            list(map(lambda u: depBeta [u], nbrs)))
      depBeta = depBetaK
    # print(list(map(lambda x: list(x.values()) , depBeta)))
    # dumpData(list(map(lambda x: list(x.values()) , depBeta)), '%s/dep/%d.json' % (outputDir, beta * 10))
    sumDep = sum(depBeta.values())
    vk_1 = nodes
    ek_1 = edges
    nib = [0]
    for k in range(1, k_max):
      vk = list(filter(lambda u: cores[u] > k, vk_1))
      ek = list(filter(lambda e: cores[e[0]] > k and cores[e[1]] > k, ek_1))
      nibk = (1 / len(vk_1)) * (len(ek) / (len(vk) * (len(vk) - 1))) * (
          sum(list(map(lambda i: depBeta[i], vk))) / sumDep)
      nib.append(nibk)
      vk_1 = vk
      ek_1 = ek
    nib += [0]
    # dumpData(nib, '%s/ni/%d.json' % (outputDir, beta * 10))
    mni = nib.index(max(nib))
    print('Nuclear Index = ', mni)
    ni.append(nib)
    aggni.append(mni)
    # print('# nodes in the core with max NI = %d; # nodes in the core, with max k = %d' % (len(list(filter(lambda x: cores[x] >= mni, nodes))), len(list(filter(lambda x: cores[x] == k_max, nodes)))))
  # lineGraphs(ni, betas, '%s NuclearIndex' % (dataset),
  #            '%s/ni/graph.png' % (outputDir))
  # plotlyGraphs(ni, betas, '%s NuclearIndex' % (dataset),
            #  '%s/ni/graph.html' % (outputDir))
  cnt = Counter(aggni)
  kc = (cnt.most_common(1))[0][0]
  chosenBs = list(
               map(lambda x: betas[x],
                   filter(lambda y: aggni[y] == kc, range(10))))
  print('@@@ k_C = %d, β chosen = %r' % (kc, chosenBs[len(chosenBs) // 2]))
  nucleus = nx.k_core(G, kc)
  print('N(GC) = %d, E(GC) = %d' %(nucleus.number_of_nodes(), nucleus.number_of_edges()))
  return kc

def plotlyCentralities(cens, G, cores, kc, folder='./'):
  quants = {}
  k_max = max(cores.values())
  for cen in cens:
    fname = '%s/%s.json' % (folder, cen['name'])
    if not (os.path.exists(fname)):
      print('Calculating %s' %(cen['name']))
      nodes = [x[0] for x in sorted((cen['fn'](G)).items(), key=lambda k: -k[1])]
      dumpData(nodes, fname)
    else:
      with open(fname) as fp:
        nodes = json.load(fp)
    num_nuc = len(list(filter(lambda x: cores[x] >= kc, nodes[:50])))
    num_max = len(list(filter(lambda x: cores[x] >= k_max, nodes[:50])))
    print(num_nuc, num_max)
    quants[cen['name']] = { 'k_max' : num_max, 'k_c' : num_nuc }
  quants['dataset'] = (((folder.split('/'))[-3]).split('.'))[0]
  dumpData(quants, '%s/top50.json' % folder)
    # print('Plotting')
    # h1 = [vals[k] for k in vals.keys() if cores[k] < kc]
    # h2 = [vals[k] for k in vals.keys() if cores[k] >= kc]
    # layout = go.Layout(
    #   title='%s Distribution' % (cen['name']),
    #   barmode='stack',
    #   yaxis=dict(
    #     type='log',
    #     autorange=True
    #   )
    # )
    # data = [
    #     go.Histogram(x=h1, name='Outside K_C core'),
    #     go.Histogram(x=h2, name='Inside K_C core')
    # ]
    # fig = go.Figure(data=data, layout=layout)
    # py.plot(fig, filename='%s/%s.html' % (folder, cen['name']))

def main():
  if not os.path.exists('%s/.plotly' % (os.path.expanduser('~'))):
    print('Plotly credentials not present. Please set up plotly.\nDetails: "https://plot.ly/python/getting-started/"')
    exit(1)
  if not (os.path.exists(outputDir)):
    os.makedirs(outputDir)
  if not (os.path.exists('%s/dep' % outputDir)):
    os.makedirs('%s/dep' % outputDir)
  if not (os.path.exists('%s/ni' % outputDir)):
    os.makedirs('%s/ni' % outputDir)
  print('Reading edges...')
  edges = readEdges(efile)
  G = nx.Graph()
  print('Done!\nAdding edges...')
  G.add_edges_from(edges)
  print('Done!\nCalculating core values after removing self-loops...')
  G.remove_edges_from(nx.selfloop_edges(G))
  print('# nodes = %d, # edges = %d' %(G.number_of_nodes(), G.number_of_edges()))
  cores = nx.core_number(G)
  if corenum is not None:
    queryg = nx.k_core(G, corenum)
    print('kth core contains %d nodes and %d edges' % (queryg.number_of_nodes(), queryg.number_of_edges()))
  coreFreq = Counter(cores.values())
  cdf = []; s = 0
  for x in range(max(coreFreq) - 1, -1, -1):
    s = s + coreFreq[x]
    cdf.append(s)
  # print(list(reversed(cdf)))
  dumpData(cores, '%s/cores.json' % (outputDir))
  print('Done!\nGetting dependency values...',)
  kc = getDep(G, cores, outputDir)
  cenf = nx.algorithms.centrality
  cens = [{
      'fn': cenf.degree_centrality,
      'name': 'DegreeCentrality'
  }, {
      'fn': cenf.closeness_centrality,
      'name': 'ClosenessCentrality'
  }, {
      'fn': cenf.betweenness_centrality,
      'name': 'BetweennessCentrality'
  }, {
      'fn': cenf.eigenvector_centrality,
      'name': 'EigenvectorCentrality'
  }]
  plotlyCentralities(cens, G, cores, kc, '%s/ni/' % (outputDir))

if __name__ == '__main__':
  main()


import numpy as np
from PyQt5 import QtCore
from PyQt5.QtWidgets import (
    QApplication,
    QLineEdit,
    QMainWindow,
    QPushButton,
    QTextEdit,
    QVBoxLayout,
    QWidget,
    QLabel,
)
import time
import sys

class UI(object):
    def setupUI(self, MainWindow):
        self.MainWindow = MainWindow
        self.MainWindow.resize(500, 500)
        self.centralWidget = QWidget(self.MainWindow)
        self.centralWidget.setObjectName('centralwidget')
        self.centralWidgetLayout = QVBoxLayout(self.centralWidget)
        self.centralWidgetLayout.setContentsMargins(0,0,0,0)
        self.setNewCentral()

    def setNewCentral(self):
        self.centralWidgetLayout.addWidget(W())
        self.MainWindow.setCentralWidget(self.centralWidget)

class W_UI(object):
    def setupUI(self, widget):
        self.layout = QVBoxLayout(widget)
        self.line = QLineEdit()
        self.line.textEdited.connect(self.jopa)
        self.layout.addWidget(self.line)

    def jopa(self):
        print(self.line.text())

class W(QWidget, W_UI):
    def __init__(self, parent=None):
        super(W, self).__init__(parent)
        self.setupUI(self)

class Window(QMainWindow, UI):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setupUI(self)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Window()
    ex.show()
    app.exec_()


from enum import IntEnum, auto

from dealer.Deck import Deck
from dealer.Logging import Logging
from players.Enum import DECK_SIZE, NUM_SUITS, NUM_PLAYERS

NOT_SET = 0.0

class STATE(IntEnum):
    MY_HAND = auto()
    LEADER = auto()
    CRR_TRUMP = auto()
    CRR_SUIT = auto()
    CRR_TRICK = auto()
    PLAYED_CARDS = auto()

Dimension = {
    STATE.MY_HAND: DECK_SIZE,
    STATE.CRR_TRICK: 3*DECK_SIZE,
    STATE.PLAYED_CARDS: DECK_SIZE,
    STATE.CRR_TRUMP: NUM_SUITS,
    STATE.CRR_SUIT : NUM_SUITS,
    STATE.LEADER: NUM_PLAYERS,
}
Active = {
    STATE.MY_HAND: True,
    STATE.CRR_TRICK: True,
    STATE.PLAYED_CARDS: True,
    STATE.CRR_TRUMP: False,
    STATE.CRR_SUIT : True,
    STATE.LEADER: True,
}

GAMESTATE1_PARAMS = 0
for d in Dimension:
    if Active[d]:
        GAMESTATE1_PARAMS += Dimension[d]

class GameState:

    def __init__(self):
        self.played_card_cutoff = 36
        self.reset_state()
        self._state_index = {}
        cumulative_index = 0
        for d in Dimension:
            if Active[d]:
                self._state_index[d] = cumulative_index
                cumulative_index += Dimension[d]

    @property
    def state(self):
        return self._state

    def reset_state(self):
        self._state = [NOT_SET] * GAMESTATE1_PARAMS

    def set_state(self, sub_state: STATE, index: int, value: float):
        if not Active[sub_state]:
            return

        if index >= Dimension[sub_state]:
            raise RuntimeError("invalid sub-state index:{}->{}".format(sub_state, index))
        if sub_state == STATE.PLAYED_CARDS and index < self.played_card_cutoff:
            return
        self._state[self._state_index[sub_state] + index] = value

    def log(self):
        DummyDeck = Deck()
        DummyDeck.deal()
        for d in Dimension:
            if Active[d]:
                idx1 = self._state_index[d]
                idx2 = idx1 + Dimension[d]
                if d in [STATE.MY_HAND, STATE.PLAYED_CARDS]:
                    my_hand = []
                    for i in range(idx1, idx2):
                        if self._state[i] == 1:
                            my_hand.append(DummyDeck.get_by_value(i-idx1))
                    Logging.debug("{} -> {}".format(d.name, my_hand))
                elif d == STATE.CRR_TRICK:
                    current_trick = []
                    for i in range(idx1, idx2):
                        card_idx = (i - idx1) % DECK_SIZE
                        if self._state[i] == 1:
                            current_trick.append(DummyDeck.get_by_value(card_idx))
                    Logging.debug("{} -> {}".format(d.name, current_trick))
                else:
                    Logging.debug("{} -> {}".format(d.name, self._state[idx1:idx2]))

class CARD_STATE(IntEnum):
    PLAYER0 = auto()
    PLAYER1 = auto()
    PLAYER2 = auto()
    MY_HAND = auto()
    PLAYED = auto()

GAMESTATE2_PARAMS = DECK_SIZE
class GameState2:

    def __init__(self):
        self.reset_state()

    @property
    def state(self):
        return self._state

    def reset_state(self):
        self._state = [NOT_SET] * GAMESTATE2_PARAMS

    def set_state(self, card: int, new_state: CARD_STATE):
        self._state[card] = new_state/CARD_STATE.PLAYED

    def log(self):
        pass

NUMBER_OF_PARAMS = GAMESTATE2_PARAMS
print(f"number of parameters: {NUMBER_OF_PARAMS}")


from setuptools import find_packages, setup

with open('README.md', 'r') as f:
    long_description = f.read()

setup(
    name='hr',
    version='0.1.0',
    author='Jurijs Kolomijecs',
    author_email='jurikolo@yandex.com',
    description='A utility for exporting system user information',
    long_description=long_description,
    long_description_content_type='text/markdown',
    url='https://github.com/jurikolo/la-intro-to-python',
    packages=find_packages('src'),
    package_dir={'': 'src'},
    python_requires='>=3.7',
    entry_points={
        'console_scripts': [
            'hr=hr.cli:main'
        ],
    }
)


#coding=utf-8
import os
import collections

def getAllDirQU(path):
    queue = collections.deque()  # 创建一个队列
    print(queue)
    # 进队
    queue.append(path)
    print(queue)

    while len(queue) != 0:
        # 出队数据
        dirPath = queue.popleft()
        print(dirPath)
        # 找出所有的文件
        filesList = os.listdir(dirPath)

        for fileName in filesList:
            # 绝对路径
            fileAbsPath = os.path.join(dirPath, fileName)
            # 判断是否是目录，是目录就进队，不是就打印
            if os.path.isdir(fileAbsPath):
                print("Dir:", fileName)
                queue.append(fileAbsPath)
            else:
                print("File:", fileName)

path = r"E:\study\untitled\Scripts\test"
print("遍历的顶层目录 == %s" % (path))
getAllDirQU(path)

untitled/目录遍历/队列模拟递归遍历目录（广度遍历）.py

队列模拟递归遍历目录（广度遍历）.py


from random import randint, random
from typing import TypeVar
from uuid import uuid4

from python.types import (
    Applicant,
    InitialApplicantStatus,
    Application,
    AdmittedApplicantStatus,
    RejectedApplicantStatus,
    Contract,
    AdmittedApplicant,
    Input,
    Matching,
    Applicants,
)

def get_random_string() -> str:
    return str(uuid4()).replace("-", "_")

def get_random_int() -> int:
    return randint(1, 20000)

def get_random_float() -> float:
    return random()

def create_applicant(override=None) -> Applicant:
    if not override:
        override = {}
    return Applicant(
        **{
            "id": get_random_string(),
            "ranked_applications": [],
            "status": InitialApplicantStatus(),
            **override,
        }
    )

def create_application(override=None) -> Application:
    if not override:
        override = {}
    return Application(
        **{
            "contract": get_random_string(),
            "priority_score": get_random_float(),
            "admitted": True,
            **override,
        }
    )

def create_admitted_applicant_status(override=None) -> AdmittedApplicantStatus:
    if not override:
        override = {}
    return AdmittedApplicantStatus(**{"rank": get_random_int(), **override})

def create_rejected_applicant_status(override=None) -> RejectedApplicantStatus:
    if not override:
        override = {}
    return RejectedApplicantStatus(**{"rank": get_random_int(), **override})

def create_contract(override=None) -> Contract:
    if not override:
        override = {}
    return Contract(
        **{
            "id": get_random_string(),
            "capacity": get_random_int(),
            "program_id": get_random_string(),
            "state_funded": True,
            "admitted_applicants": [],
            **override,
        }
    )

def create_admitted_applicant(override=None) -> AdmittedApplicant:
    if not override:
        override = {}
    return AdmittedApplicant(
        **{
            "applicant_id": get_random_string(),
            "priority_score": get_random_float(),
            **override,
        }
    )

def create_matching(override=None) -> Matching:
    if not override:
        override = {}
    return Matching(
        **{
            "applicant_id": get_random_string(),
            "contract_id": get_random_string(),
            "rank": get_random_int(),
            **override,
        }
    )

T = TypeVar("T", Applicant, Contract)

def to_map_by_id(elements: list[T]) -> dict[str, T]:
    return {element.id: element for element in elements}

def create_input(override=None) -> Input:
    if not override:
        override = {}
    return Input(
        **{
            "contracts": {},
            "applicants": [],
            **override,
        }
    )

def create_applicants(override=None) -> Applicants:
    if not override:
        override = {}
    return Applicants(**{"exhausted": [], "proposer": [], "admitted": {}, **override})


# Day 7: No Space Left On Device

import input_load as load
from typing import Tuple

def find_space(input_filepath: str) -> Tuple[int, int]:
    """Takes the filepath of a text file and reads lines from
    terminal output. Parses terminal commands and sums directories
    with a total size of at most 100000 as well as identifies size
    of smallest viable folder for deletion.

    Parameters
    ----------
    input_filepath : str
        filepath to input txt file

    Returns
    -------
    int
        sum size of folders <= 100000
    int
        size of smallest viable folder for deletion
    """
    folders = {}
    filepath = []

    # Parse Text File Identifying Terminal Output Lines
    input_file = load.txt_to_str(input_filepath)
    lines = [line for line in input_file.strip().split('\n')]
    for line in lines:
        line_data = line.strip().split()
        if line_data[1] == 'cd':
            if line_data[2] == '..':
                filepath.pop()
            else:
                filepath.append(line_data[2])
        elif line_data[1] == 'ls':
            continue
        elif line_data[0] == 'dir':
            continue
        else:
            folder_size = int(line_data[0])
            for i in range(1, len(filepath)+1):
                path = '/'.join(filepath[:i])
                if path in folders.keys():
                    size_count = {path: (folders.get(path) + folder_size)}
                    folders.update(size_count)
                else:
                    folders.setdefault(path, folder_size)

    # Disk Space
    total = 70000000
    needed = 30000000
    used = folders.get('/')
    used_target = total-needed

    # Puzzle Answers
    total_size = 0
    viable_folders = []

    for folder, size in folders.items():
        if size <= 100000:
            total_size += size
        if (used-size) <= used_target:
            viable_folders.append(size)

    smallest_viable = sorted(viable_folders)[0]

    return total_size, smallest_viable


"""
Nonlinear solvers to be used with model classes.
Implemented classes
    NewtonSolver
"""
import logging

import numpy as np

# ``tqdm`` is not a dependency. Up to the user to install it.
try:
    # Avoid some mypy trouble.
    from tqdm.autonotebook import trange  # type: ignore

    # Only import this if needed
    from porepy.utils.ui_and_logging import (
        logging_redirect_tqdm_with_level as logging_redirect_tqdm,
    )

except ImportError:
    _IS_TQDM_AVAILABLE: bool = False
else:
    _IS_TQDM_AVAILABLE = True

# Module-wide logger
logger = logging.getLogger(__name__)

class NewtonSolver:
    def __init__(self, params=None) -> None:
        if params is None:
            params = {}

        default_options = {
            "max_iterations": 10,
            "nl_convergence_tol": 1e-10,
            "nl_divergence_tol": 1e5,
        }
        default_options.update(params)
        self.params = default_options

        self.progress_bar: bool = params.get("progressbars", False)
        # Allow the position of the progress bar to be flexible, depending on whether
        # this is called inside a time loop, a time loop and an additional propagation
        # loop or inside a stationary problem (default).
        self.progress_bar_position: int = params.get("progress_bar_position", 0)

    def solve(self, model) -> tuple[float, bool, int]:
        """Solve the nonlinear problem.

        Parameters:
            model: The model instance specifying the problem to be solved.

        Returns:
            A 3-tuple containing:

            float:

                The error estimate.
            bool:

                True if the solution is converged.
            int:

                Number of iterations used.

        """
        model.before_nonlinear_loop()

        iteration_counter = 0

        is_converged = False
        is_diverged = False
        prev_sol = model.equation_system.get_variable_values(time_step_index=0)

        init_sol = prev_sol
        sol = init_sol
        errors = []
        error_norm = 1.0

        # Define a function that does all the work during one Newton iteration, except
        # for everything ``tqdm`` related.
        def newton_step() -> None:
            # Bind to variables in the outer function
            nonlocal prev_sol
            nonlocal sol
            nonlocal error_norm
            nonlocal is_converged
            nonlocal is_diverged

            # Logging.
            logger.info(
                f"Newton iteration number {iteration_counter}"
                + f" of {self.params['max_iterations']}"
            )

            # Re-discretize the nonlinear term
            model.before_nonlinear_iteration()

            sol = self.iteration(model)

            model.after_nonlinear_iteration(sol)

            error_norm, is_converged, is_diverged = model.check_convergence(
                sol, prev_sol, init_sol, self.params
            )
            prev_sol = sol
            errors.append(error_norm)

        # Progressbars turned off or tqdm not installed:
        if not self.progress_bar or not _IS_TQDM_AVAILABLE:
            while (
                iteration_counter <= self.params["max_iterations"] and not is_converged
            ):
                newton_step()

                if is_diverged:
                    model.after_nonlinear_failure(sol, errors, iteration_counter)
                elif is_converged:
                    model.after_nonlinear_convergence(sol, errors, iteration_counter)

                iteration_counter += 1

        # Progressbars turned on:
        else:
            # Redirect the root logger, s.t. no logger interferes with the progressbars.
            with logging_redirect_tqdm([logging.root]):
                # Initialize a progress bar. Length is the number of maximal Newton
                # iterations.
                solver_progressbar = trange(  # type: ignore
                    self.params["max_iterations"],
                    desc="Newton loop",
                    position=self.progress_bar_position,
                    leave=False,
                )

                while (
                    iteration_counter <= self.params["max_iterations"]
                    and not is_converged
                ):
                    solver_progressbar.set_description_str(
                        f"Newton iteration number {iteration_counter + 1} of \
                            {self.params['max_iterations']}"
                    )
                    newton_step()
                    solver_progressbar.update(n=1)
                    solver_progressbar.set_postfix_str(f"Error {error_norm}")

                    if is_diverged:
                        # If the process finishes early, the tqdm bar needs to be
                        # manually closed. See https://stackoverflow.com/a/73175351.
                        solver_progressbar.close()
                        model.after_nonlinear_failure(sol, errors, iteration_counter)
                        break
                    elif is_converged:
                        solver_progressbar.close()
                        model.after_nonlinear_convergence(
                            sol, errors, iteration_counter
                        )
                        break

                    iteration_counter += 1

        if not is_converged:
            model.after_nonlinear_failure(sol, errors, iteration_counter)

        return error_norm, is_converged, iteration_counter

    def iteration(self, model) -> np.ndarray:
        """A single nonlinear iteration.

        Right now, this is an almost trivial function. However, we keep it as a separate
        function to prepare for possible future introduction of more advanced schemes.

        """
        model.assemble_linear_system()
        sol = model.solve_linear_system()
        return sol


import itertools
from dataclasses import dataclass
from pprint import pprint
from typing import Iterator, NamedTuple

class Point(NamedTuple):
    x: int
    y: int

@dataclass
class Rock:
    points: list[tuple[int, int]]

    def move(self, dx: int, dy: int, chamber: list[Point]) -> bool:
        target_points = [Point(x + dx, y + dy) for x, y in self.points]
        for point in target_points:
            if point.x < 0 or point.x > 6:
                return False
            if point in chamber:
                return False
        self.points = target_points
        return True

@dataclass
class RockFactory:
    height: int
    width: int
    layout: list[tuple[int, int]]

    def create(self, x: int, y: int) -> Rock:
        return Rock(points=[Point(x + dx, y + dy) for dx, dy in self.layout])

    @staticmethod
    def parse(data: str) -> "RockFactory":
        lines = data.splitlines(keepends=False)
        layout = []
        for column, line in enumerate(reversed(lines)):
            for row, char in enumerate(line):
                if char == "#":
                    layout.append((row, column))
        return RockFactory(height=len(lines), width=len(line), layout=layout)

def load_shapes() -> list[RockFactory]:
    with open("shapes.txt") as f:
        data = f.read()
    shapes = data.split("\n\n")
    return [RockFactory.parse(portion) for portion in shapes]

def load_data(input_file: str) -> list[str]:
    with open(input_file) as f:
        return list(f.readline().strip())

def draw_chamber(chamber: list[Point], hight: int):
    for y in range(hight + 1, -1, -1):
        for x in range(7):
            print("#" if Point(x=x, y=y) in chamber else ".", end="")
        print()
    print("\n\n")

def fall(
    chamber: list[Point], rock_factory: RockFactory, jet_gen: Iterator[str], hight: int
) -> int:
    rock = rock_factory.create(x=2, y=hight + 4)
    new_hight = hight
    while True:
        dx = 1 if next(jet_gen) == ">" else -1
        rock.move(dx=dx, dy=0, chamber=chamber)
        if rock.move(dx=0, dy=-1, chamber=chamber) is False:
            # contact
            for point in rock.points:
                chamber.append(point)
                new_hight = max(new_hight, point.y)
            return new_hight

def part1(shapes: list[RockFactory], jets: str) -> int:
    chamber = [Point(x=x, y=0) for x in range(7)]
    jet_gen = itertools.cycle(jets)
    hight = 0
    for rock_id, shape in enumerate(itertools.cycle(shapes), start=1):
        hight = fall(chamber=chamber, rock_factory=shape, jet_gen=jet_gen, hight=hight)
        # draw_chamber(chamber=chamber, hight=hight)
        if rock_id == 2022:
            return hight

if __name__ == "__main__":
    shapes = load_shapes()
    pprint(shapes)
    jets = load_data("input.txt")
    print(f"Part1: {part1(shapes=shapes, jets=jets)}")


from aorist import *
from aorist_recipes import programs
from scienz import (
    us_subreddits, subreddit_schema
)
from common import endpoints

local = HiveTableStorage(
    location=HiveLocation(MinioLocation(name='reddit')),
    encoding=Encoding(NewlineDelimitedJSONEncoding()),
    layout=TabularLayout(StaticTabularLayout()),
)
subreddits = us_subreddits.replicate_to_local(
    Storage(local), "/tmp/us_subreddits", Encoding(CSVEncoding())
)
text_template = DatumTemplate(Text(name="corpus"))
text_corpus_schema = TextCorpusSchema(
    sources=[x.static_data_table for x in subreddits.assets.values()],
    datum_template=text_template,
    text_attribute_name="TEXT",
)
text_corpus = TextCorpus(
    name="text_corpus",
    comment="Subreddits",
    schema=DataSchema(LanguageAssetSchema(text_corpus_schema)),
    setup=StorageSetup(LocalStorageSetup(
        Storage(local),
        '/tmp/subreddits',
    )),
)
subreddits.add_asset(Asset(LanguageAsset(text_corpus)))
fasttext_attributes = [
    Attribute(KeyStringIdentifier("word_id")),
    Attribute(FreeText("word")),
    Attribute(FreeText("embedding")),
]
fasttext_datum = RowStruct(
    name="fasttext",
    attributes=fasttext_attributes,
)
embedding = LanguageAsset(FasttextEmbedding(
    name="embedding",
    comment="Fasttext embedding of size 128",
    schema=DataSchema(LanguageAssetSchema(FasttextEmbeddingSchema(
        dim=128,
        source=text_corpus,
        datum_template=DatumTemplate(fasttext_datum),
    ))),
    setup=StorageSetup(LocalStorageSetup(
        Storage(local),
        '/tmp/subreddits',
    )),
))
subreddits.add_asset(Asset(embedding))
universe = Universe(
    name="my_cluster",
    datasets=[subreddits],
    endpoints=endpoints,
    compliance=None,
)

universe.compute_uuids()
result = dag(
    universe,
    ["UploadFasttextToMinio"],
    "python",
    programs,
    dialect_preferences=[
        R(),
        Python([]),
        Bash(),
        Presto(),
    ],
)
print(result)


import cv2
import numpy as np
from time import sleep
cap=cv2.VideoCapture(0)
net = cv2.dnn.readNet("yolov3-tiny.weights","yolov3-tiny.cfg")
classes = []
with open("coco.names","r") as f:
    classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [ layer_names[i-1] for i in net.getUnconnectedOutLayers()]

colors = np.random.uniform(0,255, size=(len(classes), 3))

PERSON = 0
CELL_PHONE = 67
while cap.isOpened():
    ret, img = cap.read()
    # img = cv2.resize(img, None, fx=0.4, fy=0.4) # 이미지 크기를 재설정한다
    height, width, channels = img.shape # 이미지의 속성들을 넣는다.
    blob = cv2.dnn.blobFromImage(img, 0.00392, (416,416), (0, 0, 0), True, crop=False)
    net.setInput(blob)
    outs = net.forward(output_layers)

    # 정보를 화면에 표시
    class_ids = [] # 인식한 사물 클래스 아이디를 넣는 배열
    confidences = [] # 0에서 1까지 사물 인식에 대한 신뢰도를 넣는 배열

    isCellPhone=False
    for out in outs:
        for detection in out:
            scores = detection[5:]
            class_id = np.argmax(scores) # scores 중에서 최대값을 색인하여 class_id에 넣는다.
            confidence = scores[class_id]

            if class_id==CELL_PHONE and confidence > 0.3 : # 만약 정확도가 0.5가 넘는다면 사물이 인식되었다고 판단
                isCellPhone=True
                break

    if isCellPhone :
        print("using cellphone")
    else :
        print("no cellphone")
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    #cv2.imshow("image",img)

cv2.waitKey(0)
cv2.destroyAllWindows()


#!/usr/bin/env python
from __future__ import print_function
"""Inner product proof calculation, using
a vector pedersen commitment in which the
"blinding" factor is the dot product.
Notation: in all comments, x* is used to
indicate x is a vector.
"""
import hashlib
import binascii

from jmbitcoin import (multiply, add_pubkeys, encode, decode, N)
from utils import (modinv, inner_product, halves, getNUMS)
from vectorpedersen import VPC

class IPC(VPC):
    """An object to encapsulate an inner product commitment,
    which has form:
    P = a* G* + b* H* + <a,b>U
    where * indicates a vector, and <,> an inner product.
    The two vectors under proof are a* and b*. G*, H* and U are
    all NUMS basepoints.
    Default value for U is mentioned in the parent class.
    """
    def fiat_shamir(self, L, R, P):
        """Generates a challenge value x from the "transcript" up to this point,
        using the previous hash, and uses the L and R values from the current
        iteration, and commitment P. Returned is the value of the challenge and its
        modular inverse, as well as the squares of those values, both as
        integers and binary strings, for convenience.
        """
        xb = hashlib.sha256("".join([self.fsstate] + [str(_) for _ in [L, R, P]])).digest()
        self.fsstate = xb
        x = decode(xb, 256) % N
        x_sq = (x * x) % N
        xinv = modinv(x, N)
        x_sq_inv = (xinv * xinv) % N
        x_sqb, xinvb, x_sq_invb = [encode(_, 256, 32) for _ in [x_sq,
                                                                xinv, x_sq_inv]]
        return (x, xb, x_sq, x_sqb, xinv, xinvb, x_sq_inv, x_sq_invb)

    def __init__(self, a, b, vtype="bin", g=None, h=None, u=None):
        super(IPC, self).__init__(a, b, vtype=vtype, g=g, h=h, u=u)
        self.fsstate = ""
        self.get_inner_product()
        self.L = []
        self.R = []

    def get_inner_product(self):
        self.c = inner_product(self.a, self.b)
        return self.c

    def generate_proof(self, P=None):
        """Setup feed-in values to recursive proof creation.
        """
        #Make sure that the root 'P' value is set:
        if P:
            self.P = P
        else:
            self.get_commitment()
        return self.get_proof_recursive(self.a, self.b, self.P,
                                        self.g, self.h, self.vlen)

    def get_proof_recursive(self, a, b, P, g, h, n):
        """The prover starts with the full a*, b*, then recursively
        constructs the case n=1 where the proof is output in the form a', b',
        these are scalars, and c' = a' * b'. This will be checked by the verifier
        against the modified P', which the verifier can calculate independently,
        and it should satisfy P' = a'*G_1 + b'*H_1 + c'*U.
        So the prover must provide (L[], R[], a', b') as output to the verifier.
        The verifier checks against the pre-known P and c.
        """
        if n == 1:
            #return the tuple: a', b', L[], R[]
            #note total size is 2 * scalar_size + log(n) * 2 * point_size
            return (a[0], b[0], self.L, self.R)
        #Split the existing vectors into halves
        aL, aR = halves(a)
        bL, bR = halves(b)
        gL, gR = halves(g)
        hL, hR = halves(h)
        self.L.append(IPC(aL, bR, g=gR, h=hL, u=self.U).get_commitment())
        self.R.append(IPC(aR, bL, g=gL, h=hR, u=self.U).get_commitment())
        x, xb, x_sq, x_sqb, xinv, xinvb, x_sq_inv, x_sq_invb = self.fiat_shamir(
            self.L[-1], self.R[-1], P)
        #Construct change of coordinates for base points, and for vector terms
        gprime = []
        hprime = []
        aprime = []
        bprime = []
        for i in range(n/2):
            gprime.append(add_pubkeys([multiply(xinvb, g[i], False),
                                       multiply(xb, g[i+n/2], False)], False))
            hprime.append(add_pubkeys([multiply(xb, h[i], False),
                                       multiply(xinvb, h[i+n/2], False)], False))
            aprime.append(encode((x * decode(a[i],
                        256) + xinv * decode(a[i + n/2], 256)) % N, 256, 32))
            bprime.append(encode((xinv * decode(b[i],
                        256) + x * decode(b[i + n/2], 256)) % N, 256, 32))

        Pprime = add_pubkeys([P, multiply(x_sqb, self.L[-1], False),
                              multiply(x_sq_invb, self.R[-1], False)], False)
        return self.get_proof_recursive(aprime, bprime, Pprime, gprime, hprime, n/2)

    def verify_proof(self, a, b, P, L, R):
        """Given a proof (a, b, L, R) and the original pedersen commitment P,
        validates the proof that the commitment is to vectors a*, b* whose
        inner product is committed to (ie. validates it is of form:
        P = a*G* + b*G* + <a,b>U
        Note that this call will ignore the vectors a* and b* set in
        the constructor, so they can be dummy values as long as the length
        is correct.
        Returns True or False for verification.
        """
        self.verif_iter = 0
        self.fsstate = ""
        return self.verify_proof_recursive(P, L, R, a, b,
                                           self.g, self.h, self.vlen)

    def verify_proof_recursive(self, P, L, R, a, b, g, h, n):
        """The verifier starts with the lists of L and R values, then recursively
        constructs the case n=1 where the the verifier calculates the modified P',
        and checks it satisfies P' = a*G_1 + b*H_1 + c*U, where c = a*b
        So the prover must provide (L[], R[], a, b) as output to the verifier.
        Note here 'a' and 'b' are scalars, the final step of recursive reduction
        from the prover's original a* and b* vectors; they are passed through the
        recursion but here, unlike for the proof function, only used in the final
        step.
        """
        if n == 1:
            Pprime = IPC([a], [b], g=g, h=h, u=self.U).get_commitment()
            #print("Finished recursive verify; now comparing original P: ",
            #      binascii.hexlify(P))
            #print("..with calculated P': ", binascii.hexlify(Pprime))
            return P == Pprime
        x, xb, x_sq, x_sqb, xinv, xinvb, x_sq_inv, x_sq_invb = self.fiat_shamir(
                    L[self.verif_iter], R[self.verif_iter], P)
        #Construct change of coordinates for base points, and for vector terms
        gprime = []
        hprime = []
        for i in range(n/2):
            gprime.append(add_pubkeys([multiply(xinvb, g[i], False),
                                       multiply(xb, g[i+n/2], False)], False))
            hprime.append(add_pubkeys([multiply(xb, h[i], False),
                                       multiply(xinvb, h[i+n/2], False)], False))

        Pprime = add_pubkeys([P, multiply(x_sqb, L[self.verif_iter], False),
                            multiply(x_sq_invb, R[self.verif_iter], False)], False)
        self.verif_iter += 1
        return self.verify_proof_recursive(Pprime, L, R, a, b, gprime, hprime, n/2)

def run_test_IPC():
    a = [encode(x, 256, 32) for x in range(1, 9)]
    b = [encode(x, 256, 32) for x in range(9, 17)]
    ipc1 = IPC(a, b)
    comm1 = ipc1.get_commitment()
    print('generated commitment: ', binascii.hexlify(comm1))
    proof = ipc1.generate_proof()
    a, b, L, R = proof
    print('generated proof: ')
    print('a: ', binascii.hexlify(a))
    print('b: ', binascii.hexlify(b))
    print('L: ', [binascii.hexlify(_) for _ in L])
    print('R: ', [binascii.hexlify(_) for _ in R])
    print('Total byte length is: ',
          len(a) + len(b) + len(L) * len(L[0]) + len(R) * len(R[0]))
    print('Length of L, R array: ', len(L))
    print("**NOW ATTEMPTING TO VERIFY: **")
    #Note that the 'a' and 'b' vectors in the following constructor are dummy
    #values, they only set the length:
    verifier_ipc = IPC(["\x01"]*8, ["\x02"]*8)
    result = verifier_ipc.verify_proof(a, b, comm1, L, R)
    print("Verification result: ", result)

if __name__ == "__main__":
    run_test_IPC()


from enum import Enum

class EdgeTraversalOrientation(Enum):
    original = 0
    reverse = 1

    @classmethod
    def other(cls, orientation):
        if orientation == cls.original:
            return cls.reverse
        return cls.original

class EngineTraversalOrientation(Enum):
    original = 0
    reverse = 1
    both = 2

class EdgeDFSTraversalDirection(Enum):
    forward = 0
    reverse = 1


#IMPORTS

from OSMPythonTools.nominatim import Nominatim
from OSMPythonTools.overpass import overpassQueryBuilder, Overpass

import pandas as pd
import geopandas as gpd
from shapely import wkt
from shapely.geometry import MultiPoint
from loguru import logger

#GATHERER

nominatim = Nominatim()
overpass = Overpass()

def osm_frame(osm_query, type, color):
    id = []
    geometry = []
    osm_type = []
    tags = []

    for elemento in osm_query.elements():
        id.append(elemento.id())
        geometry.append(elemento.geometry())
        osm_type.append(elemento.type())
        tags.append(elemento.tags())

    d = {'id':id, 'geometry':geometry, 'type':osm_type, 'tags':tags, 'type':type, 'color':color}

    df = pd.DataFrame(data = d, index = d['id'])
    return df

def get_features(area : str, keywords:dict):
    return overpass.query(overpassQueryBuilder(
        area= area,
        elementType = keywords['elementType'],
        selector=keywords['selector'],
        out='body',
        includeGeometry=True))

def download_osm_data(path: str, areas: list, categories: dict):
    data = {}
    for area_name in areas:
        logger.info(f'Downloading OSM data for {area_name}...')
        area = nominatim.query(area_name, wkt=True)
        gdf = pd.concat(
            [gpd.GeoDataFrame(
                osm_frame(
                    get_features(
                        area,
                        categories[cat]),
                    cat,
                    categories[cat]['color'])
                ) for cat in categories]
            )

        data[area_name] = gdf

    for k,v in data.items():
        outdir = f"{path}\level0"
        out_data = r"{outdir}\level0_osm_{d}.csv".format(outdir=outdir,d=k)
        logger.info(f'Saving file in {out_data}...')
        v.to_csv(out_data,sep=";", index = False)

def json_data(data: pd.DataFrame, columns: list, idx: str) -> pd.Series:
    """
    Create JSON data from a DataFrame.

    Parameters:
        data (pd.DataFrame): The DataFrame to be converted.
        columns (List[str]): The columns to include in the JSON.
        idx (str): The column to use as the JSON index.

    Returns:
        A pandas Series with the JSON data.
    """
    try:
        d = data.set_index(idx)[[c for c in columns if c in data.columns]].to_dict(orient='records')
        logger.info('Making JSON DATA columns...')
        return pd.Series(d)
    except Exception as e:
        logger.error(f"Failed to create JSON data. Error: {e}")
        return pd.Series()

def transform_osm(path: str, areas: list, provider: int):
    DATACOLS = ['type'] #,'tags','color'] TAGS NESTING IS NOT WORKING AS JSON. BEWARE
    for area_name in areas:
        logger.info(f'Reading OSM data for {area_name}...')
        df = pd.read_csv(r"{path}\level0\level0_osm_{d}.csv".format(path=path,d=area_name),sep=";")
        logger.info(f'Making columns...')
        df['geometry'] = df.geometry.apply(wkt.loads)
        df = gpd.GeoDataFrame(df, geometry='geometry')
        df['provider'] = provider
        df['category'] = 'land use'+" - "+df['type']
        df['id_class'] = 'pois'
        df['data'] = json_data(df, DATACOLS, 'id' )
        df['id'] = df['provider'].astype(str)+"-"+df['id'].astype(int).astype(str).str.strip()
        df.drop_duplicates(subset='id',inplace=True)
        df.loc[~df.geometry.geom_type.isin(['MultiPoint','Point']),'geometry'] = df['geometry'].centroid
        df = df[['id', 'id_class', 'category', 'provider', 'data','geometry']]
        out_data = r"{path}\level2\level2_osm_{d}.csv".format(path=path,d=area_name)
        logger.info(f'\nSaving LEVEL2 data for {area_name} in:\n {out_data}...')
        df.to_csv(out_data,sep=";",index=False)

def gather(source_instance, **kwargs):
    download_osm_data(path=kwargs.get('path'), areas= kwargs.get('areas'),  categories= kwargs.get('categories'))
def level0(source_instance, **kwargs):
    logger.warning('level0 is not needed in this source')
def level1(source_instance, **kwargs):
    transform_osm(path=kwargs.get('path'), areas= kwargs.get('areas'),provider=kwargs.get('provider'))


from django.core.management.base import BaseCommand

from app.models import ProfileShare

class Command(BaseCommand):
    """
    Example usage:
        $ python manage.py reset_demo_share Darrell
    """
    def add_arguments(self, parser):
        parser.add_argument('requester_name', type=str, nargs='?', default='')

    def handle(self, *args, **options):
        req_name = options["requester_name"]
        req_name_list = ['Samuel', 'Joseph']
        if req_name:
            req_name_list.append(req_name)
        instances = ProfileShare.objects.filter(
            requester__user__username__in=req_name_list,
            receiver__user__username='demo'
        ).all()
        for instance in instances:
            instance.enabled = False
            instance.save()


# -*- coding: utf-8 -*-
"""
Created on Mon Aug 10 19:28:20 2020

@author: Admin
"""

import pandas as pd
import numpy as np
import datetime

df = pd.read_csv("train.csv")

from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values = np.nan, strategy = 'mean')

#To include Turn Around time to the list of independent variables
format = '%Y-%m-%d %H:%M:%S' #Format of the timestamp in the dataset
df['issue_date'] = [datetime.datetime.strptime(x, format) for x in df['issue_date']] #Converting Strings to datetime datatype
df['listing_date'] = [datetime.datetime.strptime(x, format) for x in df['listing_date']]

t = pd.DataFrame()
t['TA Time'] = df['listing_date'] - df['issue_date']
t['TA Time'] = t['TA Time']/np.timedelta64(1, 'D')

#Creating the Independent and Dependent variables
X = df.iloc[:,[3, 4, 5, 6, 7, 8]].values
t = t.iloc[:].values
X = np.append(X, t, axis = 1)

imputer = imputer.fit(X[:, 0:1])
X[:, 0:1] = imputer.transform(X[:, 0:1])

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
le = LabelEncoder()
X[:, 1] = le.fit_transform(X[:, 1])
ct = ColumnTransformer([('color_type', OneHotEncoder(), [1])], remainder = 'passthrough')
X = ct.fit_transform(X)
X = X.astype(float)

y = df.iloc[:,10].values

from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(X, y, train_size = 0.7)

#y_pred = pd.DataFrame()
#from sklearn.model_selection import StratifiedKFold
#skf = StratifiedKFold(n_splits = 5)
#for train_index, test_index in skf.splits(X, y):
#    X_train, X_val = X[train_index], X[test_index]
#    y_train, y_val = y[train_index], y[test_index]
#
#    classifier.fit(X_train, y_train)
#    y_pred1 = pd.DataFrame(classifier.predict(X_val))
#    y_pred = pd.concat([y_pred, y_pred1])

#from sklearn.linear_model import LogisticRegression
##classifier = LogisticRegression(C = 0.004832930238571752, solver = 'lbfgs', multi_class = 'multinomial')
#classifier = LogisticRegression(solver = 'newton-cg', C = 12)
#classifier.fit(X_train, y_train)

from sklearn.ensemble import GradientBoostingClassifier
classifier = GradientBoostingClassifier(n_estimators = 500, learning_rate = 0.075)
#classifier.fit(X_train, y_train)
#y_pred = classifier.predict(X_val)
#
#from sklearn.metrics import confusion_matrix
#from sklearn.metrics import accuracy_score
##y_pred = classifier.predict(X_val)
#cm = confusion_matrix(y_val, y_pred)
#accuracy = accuracy_score(y_val, y_pred)

#Grid Search
#from sklearn.model_selection import GridSearchCV
#parameters = {
#        'n_estimators' : [200, 400]
#}
#
#grid_search = GridSearchCV(estimator = classifier, param_grid = parameters, scoring = 'accuracy', cv = 10)
#grid_search = grid_search.fit(X_train, y_train)
#best_score = grid_search.best_score_
#best_parameters = grid_search.best_params_

model_performance = pd.DataFrame()

from sklearn.model_selection import cross_val_score
kcv = cross_val_score(estimator = classifier, X = X_train, y =  y_train, cv = 10)
model_performance.append([kcv])
mean_kcv = kcv.mean()
std_kcv = kcv.std()

classifier.get_params()

Pet Adoption - HackerEarth/LogisticRegression_PET.py


# -*- coding: UTF-8 -*-
# author: yuanpx
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt, RGBColor, Inches

class DocxUtil:
    @classmethod
    def add_paragraph(cls, document, text, bold=False, fontsize=12, alignment=0):
        # document = Document()
        p = document.add_paragraph()
        p.paragraph_format.line_spacing = 1.5
        if alignment == 0:
            p.alignment = WD_ALIGN_PARAGRAPH.LEFT
        if alignment == 1:
            p.alignment = WD_ALIGN_PARAGRAPH.CENTER
        else:
            p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
        run = p.add_run(text)
        run.font.size = Pt(fontsize)
        run.font.name = '宋体'
        run.bold = bold

    @classmethod
    def add_heading(cls, document, title, level=2, fontsize=None):
        head = document.add_heading(level=level)
        head.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT
        head.paragraph_format.space_after = Pt(15)
        run = head.add_run(title)
        run.font.name = '宋体'
        run.font.color.rgb = RGBColor(0, 0, 0)
        if not fontsize:
            if level == 1:
                run.font.size = Pt(15)
            elif level == 2:
                run.font.size = Pt(14)
            elif level == 3:
                run.font.size = Pt(12)
        else:
            run.font.size = Pt(fontsize)

    @classmethod
    def add_picture(cls, document, imgpath):
        document.add_picture(imgpath, width=Inches(5.2))


import pytest
import json
from typing import List
from flask.testing import FlaskClient
from flask_sqlalchemy import SQLAlchemy
from dataclasses import asdict
from flask import Response
from dtos import DataUpdateRequestV2
from models import Data
from services import DataService

from tests.mock.dtos.v2 import MockDataCreateRequestV2
from dtos import DataCreateRequestV2
from models import Visitor
from dtos import VisitorResponseV2
from services import VisitorService
from tests.mock.dtos.v2.mock_data_dtos_v2 import MockDataUpdateRequestV2

@pytest.mark.data
def test_create_data(app: FlaskClient,
                     db: SQLAlchemy,
                     visitor_model: List[Visitor],
                     visitor_service: VisitorService):

    url = '/v2/data'
    dto: DataCreateRequestV2 = MockDataCreateRequestV2()

    visitor_service.create(visitor_model)
    dto.visitor = VisitorResponseV2.from_model(visitor_model[0])
    dto = asdict(dto)

    response: Response = app.post(url,
                                  data=json.dumps(dto),
                                  content_type='application/json')

    assert response.status_code == 200

    response = response.get_json()

    assert dto["tweet"] == response["tweet"]
    assert dto["question"] == response["question"]

    model = Data.query.first()
    db.session.delete(model)
    db.session.commit()

@pytest.mark.data
def test_read_data(app: FlaskClient,
                   data_service: DataService,
                   data_model: Data):

    data_service.create(data_model)
    url = f'/v2/data/{data_model.id}'

    response: Response = app.get(url, content_type='application/json')

    assert response.status_code == 200

    response = response.get_json()
    assert response["id"] == data_model.id
    assert response["tweet"] == data_model.tweet
    assert response["question"] == data_model.question
    assert response["answer"] == data_model.answer

@pytest.mark.data
def test_update_data(app: FlaskClient,
                     data_service: DataService,
                     data_model: Data,
                     visitor_model: Visitor,
                     visitor_service: VisitorService):

    visitor_service.create(visitor_model)
    data_service.create(data_model)

    dto: DataUpdateRequestV2 = MockDataUpdateRequestV2()
    dto.id = data_model.id

    dto.visitor = VisitorResponseV2.from_model(visitor_model[0])
    dto = asdict(dto)

    url = f'/v2/data/{data_model.id}'
    response: Response = app.put(url,
                                 data=json.dumps(dto),
                                 content_type='application/json')

    assert response.status_code == 200

    response = response.get_json()

    assert response["id"] == dto["id"]
    assert response["qid"] == dto["qid"]
    assert response["answer"] == dto["answer"]
    assert response["start_position"] == dto["start_position"]
    assert response["end_position"] == dto["end_position"]


import torch
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.plugins import DDPPlugin
from transformers import (
    AutoTokenizer
)
from model.bart import BARTDPTModel

import pytorch_lightning as pl

model_config = "facebook/bart-base"
batch_size = 50
train_list = open('train.txt').read().splitlines()
eval_list = open('test.txt').read().splitlines()

def dataset_collate(batch):
    inputs = tokenizer(batch, return_tensors="pt",
                       padding=True)
    inputs['labels'] = inputs['input_ids']
    inputs_id = inputs['input_ids']
    mask = torch.ones(inputs_id.shape).uniform_() > 0.25
    result = inputs_id * mask
    result[result == 0] = tokenizer.mask_token_id
    eos = torch.tensor([tokenizer.eos_token_id]).repeat(result.shape[0]).unsqueeze(1)
    bos = torch.tensor([tokenizer.bos_token_id]).repeat(result.shape[0]).unsqueeze(1)
    mask = torch.tensor([-100]).repeat(result.shape[0]).unsqueeze(1)
    result = torch.cat((bos, result, eos), dim=1)
    inputs['labels'] = torch.cat((mask, inputs['labels'], mask), dim=1)
    inputs['input_ids'] = result
    inputs.pop('attention_mask')
    return inputs

tokenizer = AutoTokenizer.from_pretrained(model_config)
bart_dpt = BARTDPTModel(model_config, train_list, eval_list,
                        dataset_collate,
                        batch_size=batch_size)

es = EarlyStopping(monitor='dev_loss')
trainer = pl.Trainer(gpus=2, check_val_every_n_epoch=1, callbacks=[es, ModelCheckpoint(
    monitor='dev_loss', filename='{epoch}-{dev_loss:.2f}', save_last=True, )],
                     default_root_dir='./bart_dpt/', auto_lr_find=True, accelerator='ddp2',
                     plugins=DDPPlugin(find_unused_parameters=True))
trainer.tune(bart_dpt)
trainer.fit(bart_dpt)


from typing import Dict
from models.Issue import Issue
from errors.GitLab import GitLabAttributeNotFound

class IssuesService:
    @staticmethod
    def currate_issue(issue: Issue):
        """
        Currate a GitLab issue.

        Parameters
        ----------
        issue : Issue
            A GitLab issue.

        Returns
        -------
        Dict
            A currated GitLab issue.

        Raises
        ------
        GitLabAttributeNotFound
            When a required GitLab attribute was not assigned.
        """

        try:
            currated_issue: Dict = {
                "labels": issue.labels,
                "assignees": issue.assignees,
                "group": issue.project.namespace,
                "repository": issue.repository.name,
                "path_with_namespace": issue.project.path_with_namespace,
                "repository_url": issue.repository.url,
                "title": issue.object_attributes.title,
                "description": issue.object_attributes.description,
                "link": issue.object_attributes.url,
                "state": issue.object_attributes.state_id,
                "due_date": issue.object_attributes.due_date,
            }
            return currated_issue
        except AttributeError:
            raise GitLabAttributeNotFound


class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
#======== <Solution 1> ========#
        import collections
        return [num for num, count in collections.Counter(nums).most_common(k)]

# Reference: https://leetcode.com/problems/top-k-frequent-elements/discuss/484980/Python-Explained-Two-Simple-Heap-solutions
#======== <Solution 2> ========#
        import collections, heapq
        maxHeap = [(-count, num) for num, count in collections.Counter(nums).items()]
        heapq.heapify(maxHeap)
        return [heapq.heappop(maxHeap)[1] for _ in range(k)]

#======== <Solution 3> ========#
        import collections, heapq
        counter = collections.Counter(nums)
        heap = [(count, num) for num, count in list(counter.items())[:k]]
        heapq.heapify(heap)
        for num, count in list(counter.items())[k:]:
            heapq.heappushpop(heap, (count, num))
        return [num for _, num in heap]

# Reference (Explanation): https://leetcode.com/problems/top-k-frequent-elements/discuss/1927648/One-OF-THE-best-EXPLANATION
#======== <Solution 4> ========#
        import collections
        n, ans = len(nums), []
        bucket = [[] for _ in range(n + 1)]
        for num, count in collections.Counter(nums).items():
            bucket[count].append(num)
        while k:
            while not bucket[n]:
                n -= 1
            ans.extend(bucket[n])
            k -= len(bucket[n])
            n -= 1
        return ans


from __future__ import division
import numpy as np
from scipy.stats import linregress
from sklearn import linear_model #donnait la methode des moindre carres et jugee inadaptee
import sklearn.svm
import collections
import pandas as pd
import math
from statistics import median
from operator import truediv

def sign(x):
    if x > 0:
        return 1.
    elif x < 0:
        return -1.
    elif x == 0:
        return 1.
    else:
        return x

def findLength(x1,x2,y1,y2):
    return math.sqrt((x1-x2)**2 + (y1-y2)**2)

def findParamSegment(segment):
	x1 = segment[0]
	y1 = segment[1]
	x2 = segment[2]
	y2 = segment[3]
	b =-(x2-x1)
	a =(y2-y1)
	c =-(a*x1+b*y1)
	norm = math.sqrt(a**2+b**2)*sign(b) #b sera toujours positif pour eviter toute redondance
	if norm!=0:
		return(np.array([a/norm,b/norm,c/norm]))

def findParamSegments(linesArray):
    if linesArray is None:
        return "no stairs"
    n = np.shape(linesArray)[0]
    x1 = linesArray[:,0,0]
    y1 = linesArray[:,0,1]
    x2 = linesArray[:,0,2]
    y2 = linesArray[:,0,3]
    L = np.sqrt(np.square(x1 -x2) + np.square(y1-y2))
    A = (y2-y1)
    B = -(x2-x1)
    C = -(A*x1+B*y1)
    normalVector = np.concatenate((np.expand_dims(A,axis=1),np.expand_dims(B,axis=1)), axis =1)
    norm = np.linalg.norm(normalVector, axis =1)*np.sign(B) # B positif
    # on supprime DEJA les lignes de norme nulle
    linesArray = linesArray.reshape(n,4)
    linesArray = linesArray[norm != 0]
    normalVector = normalVector[norm != 0]
    L = L[norm !=0]
    C = C[norm !=0]
    norm = norm[norm !=0]
    lines = np.concatenate((linesArray, np.expand_dims(L,axis=1), np.apply_along_axis(np.divide, 0, normalVector,norm), np.expand_dims(np.divide(C,norm),axis=1)),axis=1)
    return lines

def segmentsLongerThan(linesArray, lmin):
    cond = np.greater(linesArray[:,4],lmin)
    longLines = linesArray[cond]
    return longLines

def mergeLines(lines):
    if np.shape(lines)[0]>1:
        a = np.mean(lines[:,5])
        b = np.mean(lines[:,6])
        c = np.mean(lines[:,7])
        if b > 0.1:
            x = np.concatenate((lines[:,0],lines[:,2]),axis= 0)
            x1 = np.min(x)
            x2 = np.max(x)
            y1 = -(a*x1+c)/b # calcul potentiellement dangereux en cas de ligne verticale
            y2 = -(a*x2+c)/b
        else: # alors a est suffisamment grand
            y = np.concatenate((lines[:,1],lines[:,3]),axis= 0)
            y1 = np.min(y)
            y2 = np.max(y)
            x1 = -(b*y1+c)/a
            x2 = -(b*y2+c)/a
        l = findLength(x1,x2,y1,y2)
        extremCoord = [x1,y1,x2,y2,l,a,b,c]
        return extremCoord
    else:
        return lines[0]

def mergeClusterSerieAfterC(i_list, gathered_on_C,labels_c): # a appliquer a une sortie de lustering
	mergedLinesArray = []
	for i in i_list:
		mergedLinesArray.append(mergeLines(gathered_on_C[i]))
	return np.array(mergedLinesArray)

def findUpperSide(parallelLinesArray): # a appliquer a un ensemble de lignes paralleles
    ymin1 = np.min(parallelLinesArray[:,1])
    arg_min1 = np.argmin(parallelLinesArray[:,1])
    ymin2 = np.min(parallelLinesArray[:,3])
    arg_min2 = np.argmin(parallelLinesArray[:,3])
    ymin = min(ymin1,ymin2)
    if ymin == ymin1:
        arg_min = arg_min1
    else :
        arg_min = arg_min2
    return parallelLinesArray[arg_min].astype('float64')

def findLowerSide(parallelLinesArray): # a appliquer a un ensemble de lignes paralleles
    ymax1 = np.max(parallelLinesArray[:,1])
    arg_max1 = np.argmax(parallelLinesArray[:,1])
    ymax2 = np.max(parallelLinesArray[:,3])
    arg_max2 = np.argmax(parallelLinesArray[:,3])
    ymax = max(ymax1,ymax2)
    if ymax == ymax1:
        arg_max = arg_max1
    else :
        arg_max = arg_max2
    return parallelLinesArray[arg_max].astype('float64')

def findLinesIntersectionPoint(line1,line2):
    a1=line1[0]
    b1=line1[1]
    c1=line1[2]
    a2=line2[0]
    b2=line2[1]
    c2=line2[2]
    y = (a1*c2 - a2*c1)/(b1*a2-b2*a1)
    x = ((b1-b2)*y + c1-c2)/(a2-a1)
    return(x,y)

def findLineIntersectionPoints(line,linesArray):
    n = np.shape(linesArray)[0]
    a=line[0]
    b=line[1]
    c=line[2]
    A=linesArray[5]
    B=linesArray[6]
    C=linesArray[7]
    y = (a*C - A*c)/(b*A-B*a)
    x = ((b*np.ones((n,1))-B)*y + c*np.ones((n,1))-C)/(A-a*np.ones((n,1)))
    return np.concatenate((x,y),axis=1)

def linearRegressionCoeff(pointsArray):
    regr = linear_model.LinearRegression()
    Xarray = pointsArray[:,0]
    Yarray = pointsArray[:,1]
    l = np.size(Xarray)
    if (abs(max(Xarray)-min(Xarray))) > (abs(max(Yarray)-min(Yarray))):
        #si la droite est globalement horizontale, LinearRegression ne bug pas
        regr.fit(Xarray.reshape((l, 1)),Yarray)
        m = regr.coef_[0]#regr.coef_ est un tableau a 1 dim avec linear reg et a 2 dim avec svm
        p = regr.intercept_
        #y=mx+p
        div = math.sqrt(1+m**2)
        a = -m/div
        b = 1/div#b toujours positif
        c = -p/div
    else: #si la droite est globalement verticale, LinearRegression bug
        regr.fit(Yarray.reshape((l, 1)),Xarray)
        m = regr.coef_[0]
        p = regr.intercept_
        #x=my+p
        div = math.sqrt(1+m**2)*sign(m)#b doit toujours etre positif
        a = -1/div
        b = m/div#b toujours positif
        c = p/div
    print(m,p)
    return [a,b,c]

def linearRegressionSVRCoeff(pointsArray):
    regr = sklearn.svm.SVR(kernel='linear', C=100, epsilon=30)
    Xarray = pointsArray[:,0]
    Yarray = pointsArray[:,1]
    l = np.size(Xarray)
    #if (abs(max(Xarray)-min(Xarray))) > (abs(max(Yarray)-min(Yarray)))*1.2:
    #    #si la droite est globalement horizontale, LinearRegression ne bug pas
    #    #facteur 1.2 empirique, rajoute pour le cas ou l'escalier prend toute la largeur de l'image
    #    regr.fit(Xarray.reshape((l, 1)),Yarray)
    #    m = regr.coef_[0][0]#regr.coef_ est un tableau a 1 dim avec linear reg et a 2 dim avec svm
    #    p = regr.intercept_[0]
    #    #y=mx+p
    #    div = math.sqrt(1+m**2)
    #    a = -m/div
    #    b = 1/div#b toujours positif
    #    c = -p/div
    #else: #si la droite est globalement verticale, LinearRegression bug
    regr.fit(Yarray.reshape((l, 1)),Xarray)
    m = regr.coef_[0][0]
    p = regr.intercept_[0]
    #x=my+p
    div = math.sqrt(1+m**2)*sign(m)#b doit toujours etre positif
    a = -1/div
    b = m/div#b toujours positif
    c = p/div
    print(m,p)
    return [a,b,c]

def findLeftRightPoints(linesArray):
    tableauRangPointDroit = np.greater(linesArray[:,2],linesArray[:,0])#1 si x2 a droite
    rightXarray = np.where(tableauRangPointDroit,linesArray[:,2],linesArray[:,0])
    rightYarray = np.where(tableauRangPointDroit,linesArray[:,3],linesArray[:,1])
    leftXarray = np.where(tableauRangPointDroit,linesArray[:,0],linesArray[:,2])
    leftYarray = np.where(tableauRangPointDroit,linesArray[:,1],linesArray[:,3])
    rightPointsArray = np.concatenate((np.expand_dims(rightXarray,axis=1),np.expand_dims(rightYarray,axis=1)),axis=1)
    leftPointsArray = np.concatenate((np.expand_dims(leftXarray,axis=1),np.expand_dims(leftYarray,axis=1)),axis=1)
    return leftPointsArray, rightPointsArray
    #return leftPointsArray.astype('float32'), rightPointsArray.astype('float32')

def findTrapeze(mergedLinesArray,chosen_regression_string = 'lin'): #'lin' ou 'svr'
    n = np.shape(mergedLinesArray)[0]
    leftPointsArray, rightPointsArray = findLeftRightPoints(mergedLinesArray)
    midPointsArray = findMiddlePointsArray(mergedLinesArray)
    midLineCoef = linearRegressionCoeff(midPointsArray)

    goodlinesLeftPoints = leftPointsArray
    goodlinesRightPoints = rightPointsArray
    # ELIMINE LES POINTS DONT LES MILIEUX SONT TROP LOINS DE LA VALEUR THEORIQUE
    #estimated_mid = findLineIntersectionPoints(midLineCoef, mergedLinesArray)
    #mid_deviation = midPointsArray - estimated_mid
    #mid_mean = np.mean(mid_deviation)#should be 0
    #mid_variance  = np.var((mid_deviation), axis = 1)
    #print("mid mean, var")
    #print(mid_mean,mid_variance)
    #cond_gch = np.greater(mid_deviation)

    #  ELIMINE LES POINTS QUI SE SITUENT A UNE DISTANCE TROP GRANDE DE LA MOYENNE
    #goodlinesLeftPoints = np.empty((0, 2), dtype=np.float32)
    #goodlinesRightPoints = np.empty((0, 2), dtype=np.float32)
    #for i in range(n):
    #    center_strs = findLinesIntersectionPoint(midLineCoef,mergedLinesArray[i,5:])[0]#x coordinate of the theoretical center of the stair i
    #    print("i,left,mid,right")
    #    print(i,leftPointsArray[i,0],center_strs,rightPointsArray[i,0])
    #    if leftPointsArray[i,0] < center_strs*0.5 and rightPointsArray[i,0] > center_strs*1.5:
    #        goodlinesLeftPoints = np.concatenate((goodlinesLeftPoints,np.expand_dims(leftPointsArray[i],axis=0)),axis=0)
    #        goodlinesRightPoints = np.concatenate((goodlinesRightPoints,np.expand_dims(rightPointsArray[i],axis=0)),axis=0)

    middleLine = np.expand_dims(np.concatenate((findLinesIntersectionPoint(midLineCoef, mergedLinesArray[0,5:]),findLinesIntersectionPoint(midLineCoef, mergedLinesArray[n-1,5:]))),axis=0)
    if chosen_regression_string == 'lin':
        leftSide = linearRegressionCoeff(goodlinesLeftPoints)
        rightSide = linearRegressionCoeff(goodlinesRightPoints)
    elif chosen_regression_string == 'svr':
        leftSide = linearRegressionSVRCoeff(goodlinesLeftPoints)
        rightSide = linearRegressionSVRCoeff(goodlinesRightPoints)
    upperSide = findUpperSide(mergedLinesArray)
    lowerSide = findLowerSide(mergedLinesArray)
    upperSide = upperSide[5:]
    lowerSide = lowerSide[5:]
    vertex = []
    vertex.append(findLinesIntersectionPoint(rightSide, upperSide))#en haut a droite
    vertex.append(findLinesIntersectionPoint(leftSide, upperSide))#en haut a gauche
    vertex.append(findLinesIntersectionPoint(leftSide, lowerSide))#en bas a gauche
    vertex.append(findLinesIntersectionPoint(lowerSide, rightSide))#en bas a droite
    rightline = [vertex[0][0],vertex[0][1],vertex[3][0],vertex[3][1]]
    leftline  = [vertex[1][0],vertex[1][1],vertex[2][0],vertex[2][1]]

    if chosen_regression_string=='svr':
        epsilonSVR = 30
        upperline = [vertex[0][0]-epsilonSVR,vertex[0][1],vertex[1][0]+epsilonSVR,vertex[1][1]]
        lowerline = [vertex[2][0]-epsilonSVR,vertex[2][1],vertex[3][0]+epsilonSVR,vertex[3][1]]
        r1 = [vertex[0][0]-epsilonSVR,vertex[0][1],vertex[3][0]-epsilonSVR,vertex[3][1]]
        r2 = [vertex[0][0]+epsilonSVR,vertex[0][1],vertex[3][0]+epsilonSVR,vertex[3][1]]
        l1 = [vertex[1][0]-epsilonSVR,vertex[1][1],vertex[2][0]-epsilonSVR,vertex[2][1]]
        l2 = [vertex[1][0]+epsilonSVR,vertex[1][1],vertex[2][0]+epsilonSVR,vertex[2][1]]
        trapezeLinesArray = findParamSegments(np.reshape([rightline,upperline,leftline,lowerline, r1, r2, l1, l2 ],(8,1,4)))
    else:
        upperline = [vertex[0][0],vertex[0][1],vertex[1][0],vertex[1][1]]
        lowerline = [vertex[2][0],vertex[2][1],vertex[3][0],vertex[3][1]]
        trapezeLinesArray = findParamSegments(np.reshape([rightline,upperline,leftline,lowerline],(4,1,4)))
    return trapezeLinesArray, goodlinesLeftPoints, goodlinesRightPoints, middleLine # seul trapezeLinesArray est vraiment utile, le reste pourra etre supprime dans une implementation reelle

def findLabelStairs(tab_ab, clustersAB, labels, angle_min, angle_max):
    n = np.shape(clustersAB)[0]
    labelsList=[]
    sizesList=[]
    for i in range (n):##?
        if np.arctan2(tab_ab[i][1],tab_ab[i][0]) > angle_min and np.arctan2(tab_ab[i][1],tab_ab[i][0]) < angle_max:
            labelsList.append(i)
            sizesList.append(len(clustersAB[i]))
    if  labelsList == []: # si pas d'escalier
        return "NoStairs" # risque de confusion avec label = 0 si on met false
    i_max =  sizesList.index(max( sizesList))
    return labelsList[i_max]

def findMiddle(linesArray): #a appliquer au cluster stairs pour trouver son centre
    x = [linesArray[:,0],linesArray[:,2]]
    y = [linesArray[:,1],linesArray[:,3]]
    meanX = np.mean(x)
    meanY = np.mean(y)
    return meanX, meanY

def findMiddlePointsArray(linesArray): # a appliquer au cluster stairs pour trouver les centres de chaque marche
    x = np.concatenate((np.expand_dims(linesArray[:,0],axis=1),np.expand_dims(linesArray[:,2],axis=1)),axis=1)
    y = np.concatenate((np.expand_dims(linesArray[:,1],axis=1),np.expand_dims(linesArray[:,3],axis=1)),axis=1)
    meansX = np.mean(x,axis=1)
    meansY = np.mean(y,axis=1)
    return np.concatenate((np.expand_dims(meansX,axis=1),np.expand_dims(meansY,axis=1)),axis=1)

def findLowestStair(linesArray):
    y = [linesArray[:,1],linesArray[:,3]]
    return np.max(y)

def findLowerStairs(stairsLinesArray,lowest_st_nb):
    length = len(stairsLinesArray)
    if length > lowest_st_nb:
        lowest_stairs_lines = stairs_lines[length-lowest_st_nb:length,:]
    else:
        lowest_stairs_lines = stairs_lines
    return lowest_stairs_lines

# unused exceptions
class NoStairs(Exception):
    def __init__(self):
        Exception.__init__(self,"no stairs detected")

class NotEnoughlines(Exception):
    def __init__(self):
        Exception.__init__(self,"not enough lines detected, impossible to compute operations on these lines")


# -*- coding: utf-8 -*-
'''
@createTool    : PyCharm-2020.3.3
@projectName   : pythonProjectPy3.9
@originalAuthor: Made in win10.Sys design by deHao.Zou
@createTime    : 2020/3/15 15:34
'''
'''
# -Begin-----------------------------------------------------------------

# -Packages--------------------------------------------------------------
from ctypes import *

# -Structures------------------------------------------------------------
class RFC_ERROR_INFO(Structure):
    _fields_ = [("code", c_long),
                ("group", c_long),
                ("key", c_wchar * 128),
                ("message", c_wchar * 512),
                ("abapMsgClass", c_wchar * 21),
                ("abapMsgType", c_wchar * 2),
                ("abapMsgNumber", c_wchar * 4),
                ("abapMsgV1", c_wchar * 51),
                ("abapMsgV2", c_wchar * 51),
                ("abapMsgV3", c_wchar * 51),
                ("abapMsgV4", c_wchar * 51)]

class RFC_CONNECTION_PARAMETER(Structure):
    _fields_ = [("name", c_wchar_p),
                ("value", c_wchar_p)]

# -Main------------------------------------------------------------------
ErrInf = RFC_ERROR_INFO;
RfcErrInf = ErrInf()
ConnParams = RFC_CONNECTION_PARAMETER * 5;
RfcConnParams = ConnParams()

SAPNWRFC = "sapnwrfc.dll"
SAP = windll.LoadLibrary(SAPNWRFC)

# -Prototypes------------------------------------------------------------
SAP.RfcOpenConnection.argtypes = [POINTER(ConnParams), c_ulong, POINTER(ErrInf)]
SAP.RfcOpenConnection.restype = c_void_p
SAP.RfcCloseConnection.argtypes = [c_void_p, POINTER(ErrInf)]
SAP.RfcCloseConnection.restype = c_ulong

# -Connection parameters-------------------------------------------------
RfcConnParams[0].name = "ASHOST";
RfcConnParams[0].value = "ABAP"
RfcConnParams[1].name = "SYSNR";
RfcConnParams[1].value = "00"
RfcConnParams[2].name = "CLIENT";
RfcConnParams[2].value = "001"
RfcConnParams[3].name = "USER";
RfcConnParams[3].value = "BCUSER"
RfcConnParams[4].name = "PASSWD";
RfcConnParams[4].value = "minisap"

hRFC = SAP.RfcOpenConnection(RfcConnParams, 5, RfcErrInf)
if hRFC != None:

    windll.user32.MessageBoxW(None, "Check connection with TAC SMGW", "", 0)

    # ---------------------------------------------------------------------
    # -
    # - Check connection with TAC SMGW in the SAP system
    # -
    # ---------------------------------------------------------------------

    rc = SAP.RfcCloseConnection(hRFC, RfcErrInf)

else:
    print(RfcErrInf.key)
    print(RfcErrInf.message)

# -End-------------------------------------------------------------------
'''
# -*- coding: utf-8 -*-
from flask import Flask, request
import json

app = Flask(__name__)

@app.route('/hci', methods=["POST"])
def check():
    data = request.get_data()
    url = json.loads(data)
    result_ = []
    score_datas = []
    temp_1 = {}
    for i in range(len(url)):
        answer = url["data{}".format(i + 1)]
        id = answer["id"]
        q1 = answer["q1"]
        q2 = answer["q2"]
        score = Similarity(q1, q2)

        temp_2 = {}
        temp_2["id"] = str(id)
        temp_2["score"] = str(score)
        result_.append(temp_2)
    total_score = get_Total_score()
    temp_1["TotalScore"] = str(total_score)
    temp_1["data"] = result_
    score_datas.append(temp_1)
    score_datas = json.dumps(score_datas, ensure_ascii=False)
    return score_datas

def Similarity(str1, str2):
    _score = 80
    return _score

def get_Total_score():
    _score = 80
    return _score

if __name__ == '__main__':
    app.run()

# ======================================================================================

# 把Excel数据转换为Json文件
import xlrd, json

def read_xlsx_file(filename):
    # 打开Excel文件
    data = xlrd.open_workbook(filename)
    # 读取第一个工作表
    table = data.sheets()[0]
    # 统计行数
    rows = table.nrows
    data = []  # 存放数据
    for i in range(1, rows):
        values = table.row_values(i)
        data.append(
            (
                {
                    "业务伙伴编码": values[0],
                    "流向门店名称": values[1],
                    "原始物料描述": values[2],
                    "原始物料规格": values[3],
                    "数量": values[4],
                    "单位": values[5],
                    "交易日期": values[6],
                }
            )

        )
    return data

if __name__ == '__main__':
    d1 = read_xlsx_file("C:/Users/Zeus/Desktop/网上客户流向3月份数据.xlsx")
    # 字典中的数据都是单引号，但是标准的json需要双引号
    js = json.dumps(d1, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ':'))
    print(js)
    # 前面的数据只是数组，加上外面的json格式大括号
    js = "{" + js + "}"
    # 可读可写，如果不存在则创建，如果有内容则覆盖
    jsFile = open("C:/Users/Zeus/Desktop/text3.json", "w+", encoding='utf-8')
    jsFile.write(js)
    jsFile.close()

# =========================================================================================
# 模拟postman上传数据
import requests

files = {'skFile': open("C:/Users/Zeus/Desktop/test.json", 'rb')}

r = requests.post("http://192.168.20.207:8000/wm/obtain?sap-client=600", files=files)

from urllib3 import encode_multipart_formdata
import requests

def post_files(url, header, data, filename, filepath):
    """
        :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
        ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
        or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
        defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
        to add for the file.
    """
    data['file'] = (filename, open(filepath, 'rb').read())
    encode_data = encode_multipart_formdata(data)
    data = encode_data[0]
    header['Content-Type'] = encode_data[1]
    r = requests.post(url, headers=header, data=data)
    print(r.content)

if __name__ == "__main__":
    # url,filename,filepath string
    # header,data dict
    print(post_files("url", {"header": "value"}, {"data": "value"}, "filename", "filepath"))

# ======================================================================================

from urllib3 import encode_multipart_formdata
import requests

url = "http://192.168.20.207:8000/wm/obtain?sap-client=600"
data = {}
headers = {}
filename = 'JsonFiles'  # 上传至服务器后，用于存储文件的名称
filepath = 'C:/Users/Zeus/Desktop/text3.json'  # 当前要上传的文件路径
proxies = {
    "http": "http://192.168.20.207:8000",
    "https": "http://192.168.20.207:8000",
    # 如果代理ip需要用户名和密码的话 'http':'user:password@192.168.1.1:88'
}
####
data['upload_file'] = (filename, open(filepath, 'rb').read())
data['submit'] = "提交"
encode_data = encode_multipart_formdata(data)
data = encode_data[0]
headers['Content-Type'] = encode_data[1]
# r = requests.post(url, headers=headers, data=data, timeout=5)
r = requests.post(url, headers=headers, data=data, proxies=proxies, timeout=5)
print(r.status_code)

# ======================================================================================

import requests
import json
import xlrd

host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host, endpoint])
url = "http://zycrmqas.zeus.com:8000/ZWEB/DKHLX?sap-client=600"

dataJson = {
    "sites": [
        {"name": "test", "url": "www.test.com"},
        {"name": "google", "url": "www.google.com"},
        {"name": "weibo", "url": "www.weibo.com"}
    ]
}
dataJson = {
    "ZNB": "123123123",
    "ZDATA": [
        {
            "PARTNER": "111111111",
            "ZQDCY_FROM": "老百姓",
            "MATNR_FROM": "302020202",
            "ZGGE_FROM": "32g",
            "MENGE": "32",
            "MEINS_FROM": "罐",
            "ZDATE_DEAL": "20210101"

        }, {
            "PARTNER": "111111111",
            "ZQDCY_FROM": "老百姓",
            "MATNR_FROM": "302020202",
            "ZGGE_FROM": "32g",
            "MENGE": "32",
            "MEINS_FROM": "罐",
            "ZDATE_DEAL": "20210101"

        }
    ]
}

def read_xlsx_file(filename):
    # 打开Excel文件
    data = xlrd.open_workbook(filename)
    # 读取第一个工作表
    table = data.sheets()[0]
    # 统计行数
    rows = table.nrows
    data = []  # 存放数据
    for i in range(1, rows):
        values = table.row_values(i)
        data.append(
            (
                {
                    "业务伙伴编码": str(int(values[0])),
                    "流向门店名称": values[1],
                    "原始物料描述": values[2],
                    "原始物料规格": values[3],
                    "数量": values[4],
                    "单位": values[5],
                    "交易日期": str(int(values[6])),
                }
            )

        )
    return data

dataExecel = read_xlsx_file("C:/Users/Zeus/Desktop/网上客户流向3月份数据.xlsx")
# 字典中的数据都是单引号,但是标准的json需要双引号
dataJson = json.dumps(dataExecel, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ':'))
# 前面的数据只是数组,加上外面的json格式大括号
dataJson = "{" + dataJson + "}"
print(dataJson)

'''
requests.get()                                # GET请求
requests.post()                               # POST请求
requests.put()                                # PUT请求
requests.delete()                             # DELETE请求
requests.head()                               # HEAD请求
requests.options()                            # OPTIONS请求
'''
# r = requests.get('https://github.com/Ranxf')  # 最基本的不带参数的get请求
# r = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'})  # 带参数的get请求
r = requests.post(url, data=(json.dumps(dataJson, ensure_ascii=False)).encode("utf-8"))
r = requests.post(url, data=json.dumps(dataJson))
'''
r.encoding                                 # 获取当前的编码
r.encoding = 'utf-8'                       # 设置编码
r.text                                     # 以encoding解析返回内容。字符串方式的响应体, 会自动根据响应头部的字符编码进行解码。
r.content                                  # 以字节形式（二进制）返回。字节方式的响应体, 会自动为你解码 gzip 和 deflate 压缩。
r.headers                                  # 以字典对象存储服务器响应头, 但是这个字典比较特殊, 字典键不区分大小写, 若键不存在则返回None
r.status_code                              # 响应状态码
r.raw                                      # 返回原始响应体，也就是urllib的response对象, 使用 r.raw.read()
r.ok                                       # 查看r.ok的布尔值便可以知道是否登陆成功
*特殊方法*
r.json()                                   # Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的, 不然解析出错会抛异常
r.raise_for_status()                       # 失败请求(非200响应)抛出异常
r.headers                                  #返回字典类型,头信息
r.requests.headers                         #返回发送到服务器的头信息
r.cookies                                  #返回cookie
r.history                                  #返回重定向信息,当然可以在请求是加上allow_redirects = false 阻止重定向
'''
print(r.json())

# ======================================================================================
# 正式版本

import requests
import json
import xlrd

url = "http://zycrmqas.zeus.com:8000/ZWEB/DKHLX?sap-client=600"

dataJson = {
    "ZNB": "1000000100",
    "ZDATA": [
        {
            "MATNR_FROM": "党参破壁饮片",
            "MEINS_FROM": "罐 ",
            "MENGE": 1.0,
            "PARTNER": "1100002253",
            "ZDATE_DEAL": "20210301",
            "ZGGE_FROM": "2g×20袋",
            "ZQDCY_FROM": "劳动公园店"
        },
        {
            "MATNR_FROM": "红景天破壁饮片",
            "MEINS_FROM": "罐 ",
            "MENGE": 0.0,
            "PARTNER": "1100002253",
            "ZDATE_DEAL": "20210306",
            "ZGGE_FROM": "1g×20袋",
            "ZQDCY_FROM": "东莱街分店"
        },
        {
            "MATNR_FROM": "红景天破壁饮片",
            "MEINS_FROM": "罐 ",
            "MENGE": 1.0,
            "PARTNER": "1100002253",
            "ZDATE_DEAL": "20210311",
            "ZGGE_FROM": "1g×20袋",
            "ZQDCY_FROM": "劳动公园店"
        }
    ]
}

def read_xlsx_file(filename):
    data = xlrd.open_workbook(filename)
    table = data.sheets()[0]
    rows = table.nrows
    data = []
    for i in range(1, rows):
        values = table.row_values(i)
        data.append(
            (
                {
                    "PARTNER": str(int(values[0])),
                    "ZQDCY_FROM": values[1],
                    "MATNR_FROM": values[2],
                    "ZGGE_FROM": values[3],
                    "MENGE": values[4],
                    "MEINS_FROM": values[5],
                    "ZDATE_DEAL": str(int(values[6])),
                }
            )

        )
    return data

dataExecel = read_xlsx_file("C:/Users/Zeus/Desktop/网上客户流向3月份数据.xlsx")
# 字典中的数据都是单引号,但是标准的json需要双引号
dataJson = json.dumps(dataExecel, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ':'))
# 前面的数据只是数组,加上外面的json格式大括号
dataJson = """{""" + """\n"ZNB": "100000010",\n"ZDATA":""""" + dataJson + """\n}"""
print(dataJson)

'''
requests.get()                                # GET请求
requests.post()                               # POST请求
requests.put()                                # PUT请求
requests.delete()                             # DELETE请求
requests.head()                               # HEAD请求
requests.options()                            # OPTIONS请求
'''
# r = requests.post(url, data=(json.dumps(dataJson, ensure_ascii=False)).encode("utf-8"))
r = requests.post(url, data=dataJson.encode("utf-8"))
r = requests.post(url, data=json.dumps(dataJson))
'''
r.encoding                                 # 获取当前的编码
r.encoding = 'utf-8'                       # 设置编码
r.text                                     # 以encoding解析返回内容。字符串方式的响应体, 会自动根据响应头部的字符编码进行解码。
r.content                                  # 以字节形式（二进制）返回。字节方式的响应体, 会自动为你解码 gzip 和 deflate 压缩。
r.headers                                  # 以字典对象存储服务器响应头, 但是这个字典比较特殊, 字典键不区分大小写, 若键不存在则返回None
r.status_code                              # 响应状态码
r.raw                                      # 返回原始响应体，也就是urllib的response对象, 使用r.raw.read()
r.ok                                       # 查看r.ok的布尔值便可以知道是否登陆成功
*特殊方法*
r.json()                                   # Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的, 不然解析出错会抛异常
r.raise_for_status()                       # 失败请求(非200响应)抛出异常
r.headers                                  #返回字典类型,头信息
r.requests.headers                         #返回发送到服务器的头信息
r.cookies                                  #返回cookie
r.history                                  #返回重定向信息,当然可以在请求是加上allow_redirects = false 阻止重定向
'''
print(r.json())

# ======================================================================================
# 成功版本

import requests
import json
import xlrd

def excel_to_json(filename, dataId):
    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(0)
    rows = sheet.nrows
    content = []
    for i in range(1, rows):
        values = sheet.row_values(i)
        content.append(
            (
                {
                    "PARTNER": str(int(values[0])),
                    "ZQDCY_FROM": values[1],
                    "MATNR_FROM": values[2],
                    "ZGGE_FROM": values[3],
                    "MENGE": values[4],
                    "MEINS_FROM": values[5],
                    "ZDATE_DEAL": str(int(values[6])),
                }
            )

        )
    # 字典中的数据都是单引号,但是标准的json需要双引号
    Json = json.dumps(content, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ':'))
    # 前面的数据只是数组,加上外面的json格式大括号, ZNB:导入SAP的流水单号
    dataJson = """{""" + """\n"ZNB": """"" + str(dataId) + """",\n"ZDATA":""""" + Json + """\n}"""
    return dataJson

dataExecelJson = excel_to_json("C:/Users/Zeus/Desktop/网上客户流向3月份数据.xlsx","1231000001")
print(dataExecelJson)

'''
requests.get()                             # GET请求
requests.post()                            # POST请求
requests.put()                             # PUT请求
requests.delete()                          # DELETE请求
requests.head()                            # HEAD请求
requests.options()                         # OPTIONS请求
r.encoding                                 # 获取当前的编码
r.encoding = 'utf-8'                       # 设置编码
r.text                                     # 以encoding解析返回内容。字符串方式的响应体, 会自动根据响应头部的字符编码进行解码。
r.content                                  # 以字节形式（二进制）返回。字节方式的响应体, 会自动为你解码 gzip 和 deflate 压缩。
r.headers                                  # 以字典对象存储服务器响应头, 但是这个字典比较特殊, 字典键不区分大小写, 若键不存在则返回None
r.status_code                              # 响应状态码
r.raw                                      # 返回原始响应体，也就是urllib的response对象, 使用r.raw.read()
r.ok                                       # 查看r.ok的布尔值便可以知道是否登陆成功
*特殊方法*
r.json()                                   # Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的, 不然解析出错会抛异常
r.raise_for_status()                       # 失败请求(非200响应)抛出异常
r.headers                                  #返回字典类型,头信息
r.requests.headers                         #返回发送到服务器的头信息
r.cookies                                  #返回cookie
r.history                                  #返回重定向信息,当然可以在请求是加上allow_redirects = false 阻止重定向
'''
# r = requests.post(url, data=(json.dumps(dataJson, ensure_ascii=False)).encode("utf-8"))
# r = requests.post(url, data=json.dumps(dataJson))
r = requests.post("http://zycrmqas.zeus.com:8000/ZWEB/DKHLX?sap-client=600", data=dataExecelJson.encode("utf-8"))
print(r.json())


# -*- coding: utf-8 -*-
# 😬
import logging

# Constants
LINE_LIMIT_BYTES = 251
VARIABLE_LABEL_LIMIT_BYTES = 255
VALUE_LABEL_LIMIT_BYTES = 120
SELECT_MULTIPLE_NAME_DELIMITER = '_'
SELECT_MULTIPLE_LABEL_DELIMITER = ' :: '
# SPSS limits variable names to 64 bytes, but we'll create more problems for
# people if we try to change variable names here. Any change to them would
# require altering data exports as well; otherwise, the labels we generate
# would be useless
VARIABLE_NAME_LIMIT_BYTES = 64

# HELPERS --------------------------

# https://stackoverflow.com/questions/30686701/python-get-size-of-string-in-bytes
def utf8_len(s):
    return len(s.encode('utf-8'))

def spss_escape(s):
    return s.replace("'", "''").replace('\n', '\\n')

def utf8_ellipsize(s, max_bytes, ellipsis='…'):
    r"""
    If necessary, truncate the string `s` and concatenate the result with
    `ellipsis` such that the final string length does not exceed `max_bytes`
    :Example:
        >>> utf8_ellipsize('These 🐿🐿 have taken too many bytes!',
        ...                max_bytes=37)
        'These 🐿🐿 have taken too many…'
    """

    if utf8_len(s) <= max_bytes:
        return s

    ellipsis_bytes = utf8_len(ellipsis)
    if max_bytes < ellipsis_bytes:
        raise Exception(
            '`max_bytes` cannot be less than the length in bytes of `ellipsis`'
        )

    for slice_end in range(len(s), -1, -1):
        candidate = s[:slice_end]
        if utf8_len(candidate) + ellipsis_bytes <= max_bytes:
            return candidate + ellipsis

    raise Exception('It should be impossible to get here :)')

def spss_labels_from_variables_dict(variables):
    # Create initial version of each section, no wrapping
    variable_section_lines = ['VARIABLE LABELS']
    value_section_lines = ['VALUE LABELS']
    variable_count = 0  # because the first does not need a leading slash
    value_count = 0

    for variable_name, variable in variables.items():
        if utf8_len(variable_name) > VARIABLE_NAME_LIMIT_BYTES:
            logging.warning(
                'SPSS variable name exceeds {} bytes: {}'.format(
                    VARIABLE_NAME_LIMIT_BYTES, variable_name
                )
            )

        variable_section_lines.append(
            "{line_leader}{variable_name} '{variable_label}'".format(
                line_leader=' /' if variable_count else ' ',
                variable_name=variable_name,
                variable_label=spss_escape(
                    utf8_ellipsize(
                        variable['label'],
                        VARIABLE_LABEL_LIMIT_BYTES,
                    )
                ),
            )
        )
        variable_count += 1

        try:
            values = variable['values']
        except KeyError:
            continue

        # For select multiple questions, generate an additional variable
        # label for each choice, using the format
        #     question_name_choice_name 'question label :: choice label'
        #                  ^ a literal underscore
        # TODO: advise people to export data using `_` as the group delimiter,
        # since the default, `/`, is not allowed by SPSS
        if variable['data_type'] == 'select_multiple':
            for value_name, value_label in values.items():
                label_format_string = '{variable_label}{delimiter}{value_label}'
                output = label_format_string.format(
                    variable_label=variable['label'],
                    delimiter=SELECT_MULTIPLE_LABEL_DELIMITER,
                    value_label=value_label,
                )
                if utf8_len(output) > VARIABLE_LABEL_LIMIT_BYTES:
                    variable_label = utf8_ellipsize(
                        variable['label'], int(VARIABLE_LABEL_LIMIT_BYTES / 2)
                    )
                    value_label = utf8_ellipsize(
                        value_label,
                        VARIABLE_LABEL_LIMIT_BYTES
                        - utf8_len(variable_label)
                        - utf8_len(SELECT_MULTIPLE_LABEL_DELIMITER),
                    )
                    output = label_format_string.format(
                        variable_label=variable_label,
                        delimiter=SELECT_MULTIPLE_LABEL_DELIMITER,
                        value_label=value_label,
                    )
                assert utf8_len(output) <= VARIABLE_LABEL_LIMIT_BYTES
                variable_section_lines.append(
                    "{line_leader}{variable_name}{delimiter}{value_name} "
                    "'{variable_label}'".format(
                        line_leader=' /' if variable_count else ' ',
                        variable_name=variable_name,
                        delimiter=SELECT_MULTIPLE_NAME_DELIMITER,
                        value_name=value_name,
                        variable_label=spss_escape(output),
                    )
                )
                variable_count += 1
            # Don't add any SPSS value labels
            continue

        value_section_lines.append(
            '{line_leader}{variable_name}'.format(
                line_leader=' /' if value_count else ' ',
                variable_name=variable_name,
            )
        )
        for value_name, value_label in values.items():
            value_section_lines.append(
                " '{value_name}' '{value_label}'".format(
                    value_name=spss_escape(value_name),
                    value_label=spss_escape(
                        utf8_ellipsize(value_label, VALUE_LABEL_LIMIT_BYTES)
                    ),
                )
            )
        value_count += 1

    variable_section_lines.append(' .')
    value_section_lines.append(' .')
    full_file_lines = variable_section_lines + value_section_lines

    # Wrap long strings into multiple lines, where possible
    line_no = 0
    while line_no < len(full_file_lines):
        line = full_file_lines[line_no]
        if utf8_len(line) > LINE_LIMIT_BYTES:
            # coerce unicode string to list of codes for iteration
            chars = list(line)
            # Iterating over chars, keeping a best split point, to wrap within
            # the byte limits. Chars might be multiple unicode bytes together,
            # which we don't want to split between lines.
            char_i = 0
            in_string = False
            last_split = 0
            last_split_in_string = False
            line_limit = LINE_LIMIT_BYTES  # this will change if in a string
            byte_count = 0
            while byte_count < line_limit:
                # good split points: whitespace, or good insides of strings
                char = chars[char_i]
                if in_string:
                    # handle `\n` and `''`
                    bichar = ''.join(chars[char_i : char_i + 2])
                    if bichar == '\\n' or bichar == "''":
                        last_split = char_i
                        last_split_in_string = True
                        # advance extra
                        char_i += 1
                        byte_count += 1
                    else:
                        if char == "'":
                            in_string = False
                            line_limit += 1
                        else:
                            last_split = char_i
                            last_split_in_string = True
                else:
                    if char == "'":
                        in_string = True
                        line_limit -= 1
                    if char == ' ':
                        last_split = char_i
                        last_split_in_string = False
                char_i += 1
                byte_count += len(char.encode('utf-8'))
            if last_split == 0:
                raise Exception("Can't split line {}".format(line_no))
            else:
                if last_split_in_string:
                    next_line = " + '" + line[last_split:]  # .rstrip()
                    line = full_file_lines[line_no] = line[0:last_split] + "'"
                    full_file_lines.insert(line_no + 1, next_line)
                else:
                    # print "split outside of string." + line
                    next_line = ' ' + line[last_split:]  # .rstrip()
                    line = full_file_lines[line_no] = line[
                        0:last_split
                    ]  # .rstrip()
                    full_file_lines.insert(line_no + 1, next_line)
        line_no += 1

    # Use CRLF line endings because that's what SPSS does when saving (at least
    # on Windows!)
    return '\r\n'.join(full_file_lines)