import collections
import contextlib
import cStringIO
import os
import sys
import textwrap

from googlecloudsdk.api_lib.storage import storage_util
from googlecloudsdk.command_lib.ml import flags
from googlecloudsdk.command_lib.ml import uploads
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import execution_utils
from googlecloudsdk.core import log
from googlecloudsdk.core.util import files

DEFAULT_SETUP_FILE = """\
from setuptools import setup

if __name__ == '__main__':
    setup(name='{package_name}', packages=['{package_name}'])
"""

_NO_PACKAGES_ERROR_MSG = (
    'If `--package-path` is not specified, at least one Python package '
    'must be specified via `--packages`.')

class UploadFailureError(exceptions.Error):
  """Generic error with the packaging/upload process."""
  pass

class SetuptoolsFailedError(UploadFailureError):
  """Error indicating that setuptools itself failed."""

  def __init__(self, output, generated):
    msg = ('Packaging of user Python code failed with message:\n\n'
           '{}\n\n').format(output)
    if generated:
      msg += ('Try manually writing a setup.py file at your package root and '
              'rerunning the command.')
    else:
      msg += ('Try manually building your Python code by running:\n'
              '  $ python setup.py sdist\n'
              'and providing the output via the `--packages` flag (for '
              'example, `--packages dist/package.tar.gz,dist/package2.whl)`')
    super(SetuptoolsFailedError, self).__init__(msg)

class SysExecutableMissingError(UploadFailureError):
  """Error indicating that sys.executable was empty."""

  def __init__(self):
    super(SysExecutableMissingError, self).__init__(textwrap.dedent("""\
        No Python executable found on path. A Python executable with setuptools
        installed on the PYTHONPATH is required for building Cloud ML training
        jobs.
        """))

class MissingInitError(UploadFailureError):
  """Error indicating that the package to build had no __init__.py file."""

  def __init__(self, package_dir):
    super(MissingInitError, self).__init__(textwrap.dedent("""\
        [{}] is not a valid Python package because it does not contain an \
        `__init__.py` file. Please create one and try again.
        """).format(package_dir))

class UncopyablePackageError(UploadFailureError):
  """Error with copying the package."""

class DuplicateEntriesError(UploadFailureError):
  """Error indicating that multiple files with the same name were provided."""

  def __init__(self, duplicates):
    super(DuplicateEntriesError, self).__init__(
        'Cannot upload multiple packages with the same filename: [{}]'.format(
            ', '.join(duplicates)))

class NoStagingLocationError(UploadFailureError):
  """No staging location was provided but one was required."""

def _CopyIfNotWritable(source_dir, temp_dir):
  """Returns a writable directory with the same contents as source_dir.

  If source_dir is writable, it is used. Otherwise, a directory 'dest' inside of
  temp_dir is used.

  Args:
    source_dir: str, the directory to (potentially) copy
    temp_dir: str, the path to a writable temporary directory in which to store
      any copied code.

  Returns:
    str, the path to a writable directory with the same contents as source_dir
      (i.e. source_dir, if it's writable, or a copy otherwise).

  Raises:
    UploadFailureError: if the command exits non-zero.
  """
  if files.HasWriteAccessInDir(source_dir):
    return source_dir

  if files.IsDirAncestorOf(source_dir, temp_dir):
    raise UncopyablePackageError(
        'Cannot copy directory since working directory [{}] is inside of '
        'source directory [{}].'.format(temp_dir, source_dir))

  dest_dir = os.path.join(temp_dir, 'dest')
  log.debug('Copying local source tree from [%s] to [%s]', source_dir, dest_dir)
  try:
    files.CopyTree(source_dir, dest_dir)
  except OSError:
    raise UncopyablePackageError(
        'Cannot write to working location [{}]'.format(dest_dir))
  return dest_dir

def _GenerateSetupPyIfNeeded(setup_py_path, package_name):
  """Generates a temporary setup.py file if there is none at the given path.

  Args:
    setup_py_path: str, a path to the expected setup.py location.
    package_name: str, the name of the Python package for which to write a
      setup.py file (used in the generated file contents).

  Returns:
    bool, whether the setup.py file was generated.
  """
  log.debug('Looking for setup.py file at [%s]', setup_py_path)
  if os.path.isfile(setup_py_path):
    log.info('Using existing setup.py file at [%s]', setup_py_path)
    return False

  setup_contents = DEFAULT_SETUP_FILE.format(package_name=package_name)
  log.info('Generating temporary setup.py file:\n%s', setup_contents)
  with open(setup_py_path, 'w') as setup_file:
    setup_file.write(setup_contents)
  return True

@contextlib.contextmanager
def _TempDirOrBackup(default_dir):
  """Yields a temporary directory or a backup temporary directory.

  Prefers creating a temporary directory (which will be cleaned up when the
  context manager is closed), but falls back to default_dir. There are systems
  where users can't write to temp, but we still need to copy.

  Args:
    default_dir: str, the backup temporary directory.

  Yields:
    str, the temporary directory.
  """

  try:
    with files.TemporaryDirectory() as temp_dir:
      yield temp_dir
  except OSError:
    # Some systems don't allow access to '/tmp'
    yield default_dir

def _RunSetupTools(package_root, setup_py_path, output_dir):
  """Executes the setuptools `sdist` command.

  Specifically, runs `python setup.py sdist` (with the full path to `setup.py`
  given by setup_py_path) with arguments to put the final output in output_dir
  and all possible temporary files in a temporary directory. package_root is
  used as the working directory.

  package_root must be writable, or setuptools will fail (there are
  temporary files from setuptools that get put in the CWD).

  Args:
    package_root: str, the directory containing the package (that is, the
      *parent* of the package itself).
    setup_py_path: str, the path to the `setup.py` file to execute.
    output_dir: str, path to a directory in which the built packages should be
      created.

  Returns:
    list of str, the full paths to the generated packages.

  Raises:
    SysExecutableMissingError: if sys.executable is None
    RuntimeError: if the execution of setuptools exited non-zero.
  """
  if not sys.executable:
    raise SysExecutableMissingError()

  # We could just include the 'sdist' command and its flags here, but we want
  # to avoid leaving artifacts in the setup directory. That's what the
  # 'egg_info' and 'build' options do (these are both invoked as subcommands
  # of 'sdist').
  # Unfortunately, there doesn't seem to be any easy way to move *all*
  # temporary files out of the current directory, so we'll fail here if we
  # can't write to it.
  with _TempDirOrBackup(package_root) as working_dir:
    args = [sys.executable, setup_py_path,
            'egg_info', '--egg-base', working_dir,
            'build', '--build-base', working_dir, '--build-temp', working_dir,
            'sdist', '--dist-dir', output_dir]
    out = cStringIO.StringIO()
    if execution_utils.Exec(args, no_exit=True, out_func=out.write,
                            err_func=out.write, cwd=package_root):
      # If the first execution doesn't work, it might be because setup.py is
      # using distutils. We still want to try it that way because it leaves
      # fewer droppings (that is, leftover files) on disk.
      args = [sys.executable, setup_py_path,
              'sdist', '--dist-dir', output_dir]
      out = cStringIO.StringIO()
      if execution_utils.Exec(args, no_exit=True, out_func=out.write,
                              err_func=out.write, cwd=package_root):
        raise RuntimeError(out.getvalue())

  local_paths = [os.path.join(output_dir, rel_file)
                 for rel_file in os.listdir(output_dir)]
  log.debug('Python packaging resulted in [%s]', ', '.join(local_paths))
  return local_paths

def BuildPackages(package_path, output_dir):
  """Builds Python packages from the given package source.

  That is, builds Python packages from the code in package_path, using its
  parent directory (the 'package root') as its context using the setuptools
  `sdist` command.

  If there is a `setup.py` file in the package root, use that. Otherwise,
  use a simple, temporary one made for this package.

  We try to be as unobstrustive as possible (see _RunSetupTools for details):

  - setuptools writes some files to the package root--we move as many temporary
    generated files out of the package root as possible
  - the final output gets written to output_dir
  - any temporary setup.py file is written outside of the package root.
  - if the current directory isn't writable, we silenly make a temporary copy

  Args:
    package_path: str. Path to the package. This should be the path to
      the directory containing the Python code to be built, *not* its parent
      (which optionally contains setup.py and other metadata).
    output_dir: str, path to a long-lived directory in which the built packages
      should be created.

  Returns:
    list of str. The full local path to all built Python packages.

  Raises:
    SetuptoolsFailedError: If the setup.py file fails to successfully build.
    MissingInitError: If the package doesn't contain an `__init__.py` file.
  """
  package_path = os.path.abspath(package_path)
  package_root = os.path.dirname(package_path)
  with _TempDirOrBackup(package_path) as working_dir:
    package_root = _CopyIfNotWritable(package_root, working_dir)
    if not os.path.exists(os.path.join(package_path, '__init__.py')):
      # We could drop `__init__.py` in here, but it's pretty likely that this
      # indicates an incorrect directory or some bigger problem and we don't
      # want to obscure that.
      #
      # Note that we could more strictly validate here by checking each package
      # in the `--module-name` argument, but this should catch most issues.
      raise MissingInitError(package_path)

    setup_py_path = os.path.join(package_root, 'setup.py')
    package_name = os.path.basename(package_path)
    generated = _GenerateSetupPyIfNeeded(setup_py_path, package_name)
    try:
      return _RunSetupTools(package_root, setup_py_path, output_dir)
    except RuntimeError as err:
      raise SetuptoolsFailedError(str(err), generated)
    finally:
      if generated:
        # For some reason, this artifact gets generated in the package root by
        # setuptools, even after setting PYTHONDONTWRITEBYTECODE or running
        # `python setup.py clean --all`. It's weird to leave someone a .pyc for
        # a file they never knew they had, so we clean it up.
        pyc_file = os.path.join(package_root, 'setup.pyc')
        for path in (setup_py_path, pyc_file):
          try:
            os.unlink(path)
          except OSError:
            log.debug(
                "Couldn't remove file [%s] (it may never have been created).",
                pyc_file)

def _UploadFilesByPath(paths, staging_location):
  """Uploads files after validating and transforming input type."""
  if not staging_location:
    raise NoStagingLocationError()
  counter = collections.Counter(map(os.path.basename, paths))
  duplicates = [name for name, count in counter.iteritems() if count > 1]
  if duplicates:
    raise DuplicateEntriesError(duplicates)

  upload_pairs = [(path, os.path.basename(path)) for path in paths]
  return uploads.UploadFiles(upload_pairs, staging_location.bucket_ref,
                             staging_location.name)

def UploadPythonPackages(packages=(), package_path=None, staging_location=None):
  """Uploads Python packages (if necessary), building them as-specified.

  A Cloud ML job needs one or more Python packages to run. These Python packages
  can be specified in one of three ways:

    1. As a path to a local, pre-built Python package file.
    2. As a path to a Cloud Storage-hosted, pre-built Python package file (paths
       beginning with 'gs://').
    3. As a local Python source tree (the `--package-path` flag).

  In case 1, we upload the local files to Cloud Storage[1] and provide their
  paths. These can then be given to the Cloud ML API, which can fetch these
  files.

  In case 2, we don't need to do anything. We can just send these paths directly
  to the Cloud ML API.

  In case 3, we perform a build using setuptools[2], and upload the resulting
  artifacts to Cloud Storage[1]. The paths to these artifacts can be given to
  the Cloud ML API. See the `BuildPackages` method.

  These methods of specifying Python packages may be combined.

  [1] Uploads are to a specially-prefixed location in a user-provided Cloud
  Storage staging bucket. If the user provides bucket `gs://my-bucket/`, a file
  `package.tar.gz` is uploaded to
  `gs://my-bucket/<job name>/<checksum>/package.tar.gz`.

  [2] setuptools must be installed on the local user system.

  Args:
    packages: list of str. Path to extra tar.gz packages to upload, if any. If
      empty, a package_path must be provided.
    package_path: str. Relative path to source directory to be built, if any. If
      omitted, one or more packages must be provided.
    staging_location: storage_util.ObjectReference. Cloud Storage prefix to
      which archives are uploaded. Not necessary if only remote packages are
      given.

  Returns:
    list of str. Fully qualified Cloud Storage URLs (`gs://..`) from uploaded
      packages.

  Raises:
    ValueError: If packages is empty, and building package_path produces no
      tar archives.
    SetuptoolsFailedError: If the setup.py file fails to successfully build.
    MissingInitError: If the package doesn't contain an `__init__.py` file.
    DuplicateEntriesError: If multiple files with the same name were provided.
    ArgumentError: if no packages were found in the given path or no
      staging_location was but uploads were required.
  """
  remote_paths = []
  local_paths = []
  for package in packages:
    if storage_util.ObjectReference.IsStorageUrl(package):
      remote_paths.append(package)
    else:
      local_paths.append(package)

  if package_path:
    package_root = os.path.dirname(os.path.abspath(package_path))
    with _TempDirOrBackup(package_root) as working_dir:
      local_paths.extend(BuildPackages(package_path,
                                       os.path.join(working_dir, 'output')))
      remote_paths.extend(_UploadFilesByPath(local_paths, staging_location))
  elif local_paths:
    # Can't combine this with above because above requires the temporary
    # directory to still be around
    remote_paths.extend(_UploadFilesByPath(local_paths, staging_location))

  if not remote_paths:
    raise flags.ArgumentError(_NO_PACKAGES_ERROR_MSG)
  return remote_paths

def GetStagingLocation(job_id=None, staging_bucket=None, job_dir=None):
  """Get the appropriate staging location for the job given the arguments."""
  staging_location = None
  if staging_bucket:
    staging_location = storage_util.ObjectReference(staging_bucket,
                                                    job_id)
  elif job_dir:
    staging_location = storage_util.ObjectReference(
        job_dir.bucket_ref, '/'.join((job_dir.name.rstrip('/'), 'packages')))
  return staging_location


from PySide2.QtGui import QFontMetrics, QTextCursor
from PySide2.QtWidgets import QMainWindow, QLabel, QWidget, QVBoxLayout, \
    QHBoxLayout, QPushButton, QTextEdit, QScrollBar

from PySide2.QtWidgets import QMainWindow, QLabel, QWidget, QVBoxLayout, \
    QHBoxLayout, QPushButton, QStackedWidget

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
import seaborn as sns

from sklearn.metrics import accuracy_score, confusion_matrix

#Source: https://www.pythonguis.com/tutorials/plotting-matplotlib/
class MplCanvas(FigureCanvasQTAgg):

    def __init__(self, y_true, predicted, parent=None, width=16, height=9, dpi=100):

        fig = Figure(figsize=(width, height), dpi=dpi)
        cm = confusion_matrix(y_true, predicted)
        ax= fig.add_subplot(111)
        sns.heatmap(cm, annot=True, fmt='g', ax=ax, annot_kws={"size": 20})

        # labels, title and ticks
        ax.set_xlabel('Predicted labels', fontsize=20)
        ax.set_ylabel('True labels', fontsize=20)
        ax.set_title('Confusion Matrix', fontsize=20)
        ax.xaxis.set_ticklabels(['Tumor','Healthy'], fontsize=20)
        ax.yaxis.set_ticklabels(['Tumor','Healthy'], fontsize=20)

        super(MplCanvas, self).__init__(fig)

class ConfusionMatrixViewer(QMainWindow):

    def __init__(self, node, y_true, predicted):

        super().__init__()

        self.node = node
        #Trick to avoid window closing
        node.var = self

        #Create the layout
        container = QWidget()
        self.layout = QVBoxLayout(container)

        cm = MplCanvas(y_true, predicted)

        self.layout.addWidget(cm)
        self.setCentralWidget(container)

        self.setWindowTitle("Confusion Matrix viewer")

        self.show()


import Adafruit_PCA9685   # 导入用来与PCA9685进行通信的库
import time

pwm = Adafruit_PCA9685.PCA9685()    # 实例化用来控制PWM的对象
pwm.set_pwm_freq(50)    # 设置PWM信号的频率

while 1:
      for i in range(0,100):    # 使舵机从300缓慢运动到400
          pwm.set_pwm(3, 0, (300+i))
          time.sleep(0.05)
      for i in range(0,100):    # 使舵机从400缓慢运动到300
          pwm.set_pwm(3, 0, (400-i))
          time.sleep(0.05)


import asyncio
import logging

from google.protobuf.empty_pb2 import Empty
from grpc import ServicerContext

from proto.messages.meteo.meteo_messages_pb2 import RawMeteoData, RawPollutionData
from proto.services.processing import processing_service_pb2_grpc
from server.processing_service import ProcessingService

logger = logging.getLogger(__name__)

class ProcessingServiceServicer(processing_service_pb2_grpc.ProcessingServiceServicer):
    def __init__(self, processing_service: ProcessingService):
        logger.info("Initializing ProcessingServiceServicer")
        self._processing_service = processing_service
        self._background_tasks = set()

    async def ProcessMeteoData(self, meteo_data: RawMeteoData, context: ServicerContext) -> Empty:
        logger.info(f"{context.peer()} called ProcessMeteoData")
        task = asyncio.create_task(self._processing_service.process_meteo_data(meteo_data))
        self._background_tasks.add(task)
        task.add_done_callback(self._background_tasks.discard)
        return Empty()

    async def ProcessPollutionData(self, pollution_data: RawPollutionData, context: ServicerContext) -> Empty:
        logger.info(f"{context.peer()} called ProcessPollutionData")
        task = asyncio.create_task(self._processing_service.process_pollution_data(pollution_data))
        self._background_tasks.add(task)
        task.add_done_callback(self._background_tasks.discard)
        return Empty()


from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
import json
from controller import *

def checkParam(path, newPath):
  pathArray = path.split('/')
  newPathArray = newPath.split('/')
  paramsMap = {}
  if len(pathArray) != len(newPathArray):
    return False
  for index in range(len(pathArray)):
    if pathArray[index] != newPathArray[index]:
      if len(pathArray[index]) == 0 or pathArray[index][0] != ':':
        return False
      paramsMap[pathArray[index]] = newPathArray[index]
  return paramsMap

class S(BaseHTTPRequestHandler):
    def _set_response(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
    def do_GET(self):
      logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
      self._set_response()
      if self.path == '/sectors':
        sectors = json.dumps(getAllSectors())
        self.wfile.write(sectors.encode('utf-8'))
        return
      params = checkParam('/companies/:sector/:name', self.path)
      if params:
        sector = params[":sector"].replace("%20", " ")
        name = params[":name"].replace("%20", " ")
        companies = json.dumps(getBestCompanies(sector, name))
        self.wfile.write(companies.encode('utf-8'))
        return
      params = checkParam('/company/:cmp', self.path)
      if params:
        sector = params[":cmp"].replace("%20", " ")
        companies = json.dumps(getCompanyDataByAbreviation(sector))
        self.wfile.write(companies.encode('utf-8'))
        return
      self.wfile.write(json.dumps({"Error": "Wrong endpoint!"}).encode('utf-8'))
      return

    def do_POST(self):
      content_length = int(self.headers['Content-Length'])
      post_data = self.rfile.read(content_length)
      logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
              str(self.path), str(self.headers), post_data.decode('utf-8'))
      self._set_response()
      self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))

def run(server_class=HTTPServer, handler_class=S, port=8080):
  logging.basicConfig(level=logging.INFO)
  server_address = ('', port)
  httpd = server_class(server_address, handler_class)
  logging.info('Starting httpd...\n')
  try:
      httpd.serve_forever()
  except KeyboardInterrupt:
      pass
  httpd.server_close()
  logging.info('Stopping httpd...\n')

if __name__ == '__main__':
    from sys import argv
    if len(argv) == 2:
        run(port=int(argv[1]))
    else:
        run()


"""
tianyi: 天一是三渣的早期作品:<贩罪>的主角，是像猫一样生活的男人，并且善于计算人心/性，做出各种冷静的数据分析
这里存一些数据分析相关的东西
"""
import decimal
from typing import List

import pendulum
from django.conf import settings
from django.db import models

from apps import utils
from apps.proxy import models as pm
from apps.sspanel import models as sm
from apps.stats.models import DailyStats

class DashBoardManger:
    """
    无情的dashboard生成器
    """

    def __init__(self, dt_list: List[pendulum.DateTime]):
        self.dt_list = dt_list
        self.log_dict = DailyStats.get_date_str_dict(dt_list)

    def _get_by_dt(self, dt: pendulum.DateTime):
        return self.log_dict[str(dt.date())]

    def get_node_status(self):
        def gen_bar_config(dt_list):
            node_total_traffic = pm.ProxyNode.calc_total_traffic()
            return {
                "title": f"所有节点当月共消耗:{node_total_traffic}",
                "labels": ["{}-{}".format(t.month, t.day) for t in dt_list],
                "data": [self._get_by_dt(dt).total_used_traffic for dt in dt_list],
                "data_title": "每日流量(GB)",
                "x_label": f"最近{len(dt_list)}天",
                "y_label": "单位:GB",
            }

        def gen_doughnut_config():
            active_nodes = pm.ProxyNode.get_active_nodes()
            labels = [node.name for node in active_nodes]
            return {
                "title": f"总共{len(labels)}条节点",
                "labels": labels,
                "data": [
                    round(node.used_traffic / settings.GB, 2) for node in active_nodes
                ],
                "data_title": "节点流量",
            }

        return {
            "doughnut_config": gen_doughnut_config(),
            "bar_config": gen_bar_config(self.dt_list),
        }

    def get_user_status_data(self):
        """统计用户信息"""

        def gen_line_configs(dt_list):
            active_user_count = [
                self._get_by_dt(dt).active_user_count for dt in dt_list
            ]
            active_user_line_config = {
                "title": f"最近{len(dt_list)}天 总活跃人数为{sum(active_user_count)}人",
                "labels": ["{}-{}".format(t.month, t.day) for t in dt_list],
                "data": active_user_count,
                "data_title": "活跃用户",
                "x_label": f"最近{len(dt_list)}天",
                "y_label": "活跃用户数",
            }
            new_user_count = [self._get_by_dt(dt).new_user_count for dt in dt_list]
            new_user_line_config = {
                "title": f"最近{len(dt_list)}天 新增人数为{sum(new_user_count)}人",
                "labels": ["{}-{}".format(t.month, t.day) for t in dt_list],
                "data": new_user_count,
                "data_title": "新增用户",
                "x_label": f"最近{len(dt_list)}天",
                "y_label": "新用户数",
            }
            return {
                "active_user_line_config": active_user_line_config,
                "new_user_line_config": new_user_line_config,
            }

        def gen_doughnut_config():
            user_status = [
                pm.UserTrafficLog.get_all_node_online_user_count(),
                sm.User.get_today_register_user().count(),
                sm.UserCheckInLog.get_checkin_user_count(
                    utils.get_current_datetime().date()
                ),
            ]
            return {
                "title": f"总用户数量{sm.User.objects.all().count()}人",
                "labels": ["在线人数", "今日注册", "今日签到"],
                "data": user_status,
                "data_title": "活跃用户",
            }

        data = {"doughnut_config": gen_doughnut_config()}
        data.update(gen_line_configs(self.dt_list))
        return data

    def get_userorder_status_data(self):
        """获取的订单统计数据"""

        def gen_bar_config(dt_list):
            success_order_count = [self._get_by_dt(dt).order_count for dt in dt_list]
            return {
                "title": f"总订单数量:{sum(success_order_count)}",
                "labels": [f"{date.month}-{date.day}" for date in dt_list],
                "data": success_order_count,
                "data_title": "每日订单数量",
                "x_label": f"最近{len(dt_list)}天",
                "y_label": "订单数量",
            }

        def gen_doughnut_config(dt_list):
            now = utils.get_current_datetime()
            today_amount = (
                sm.UserOrder.objects.filter(
                    status=sm.UserOrder.STATUS_FINISHED,
                    created_at__range=[
                        now.start_of("day"),
                        now.end_of("day"),
                    ],
                ).aggregate(amount=models.Sum("amount"))["amount"]
                or "0"
            )
            total_amount = (
                sm.UserOrder.objects.filter(
                    status=sm.UserOrder.STATUS_FINISHED,
                    created_at__range=[
                        dt_list[0].start_of("day"),
                        dt_list[-1].end_of("day"),
                    ],
                ).aggregate(amount=models.Sum("amount"))["amount"]
                or "0"
            )
            return {
                "title": f"总收入{total_amount}元",
                "labels": ["总收入", "今日收入"],
                "data": [
                    int(decimal.Decimal(total_amount)),
                    int(decimal.Decimal(today_amount)),
                ],
                "data_title": "收入分析",
            }

        def gen_line_config(dt_list):
            amount_data = [self._get_by_dt(dt).order_amount for dt in dt_list]
            return {
                "title": f"最近{len(dt_list)}天 总收益为{sum(amount_data)}元",
                "labels": ["{}-{}".format(t.month, t.day) for t in dt_list],
                "data": amount_data,
                "data_title": "收益",
                "x_label": f"最近{len(dt_list)}天",
                "y_label": "金额/元",
            }

        return {
            "bar_config": gen_bar_config(self.dt_list),
            "doughnut_config": gen_doughnut_config(self.dt_list),
            "line_config": gen_line_config(self.dt_list),
        }

    @classmethod
    def gen_traffic_line_chart_configs(cls, user_id, node_id, dt_list):
        proxy_node = pm.ProxyNode.get_or_none(node_id)  # node must exists
        user_total_traffic = pm.UserTrafficLog.calc_user_total_traffic(
            proxy_node, user_id
        )
        dt_list = sorted(dt_list)
        return {
            "title": "节点 {} 当月共消耗：{}".format(proxy_node.name, user_total_traffic),
            "labels": ["{}-{}".format(t.month, t.day) for t in dt_list],
            "data": [
                pm.UserTrafficLog.calc_traffic_by_datetime(dt, user_id, proxy_node)
                for dt in dt_list
            ],
            "data_title": proxy_node.name,
            "x_label": f"最近{len(dt_list)}天",
            "y_label": "单位:GB",
        }

    @classmethod
    def gen_ref_log_bar_chart_configs(cls, user_id, dt_list):
        """set register_count to 0 if the query date log not exists"""
        dt_list = sorted(dt_list)
        logs = {
            log.date: log.register_count
            for log in sm.UserRefLog.list_by_user_id_and_date_list(user_id, dt_list)
        }
        return {
            "title": f"总共邀请用户:{sm.UserRefLog.calc_user_total_ref_count(user_id)}人",
            "labels": [f"{date.month}-{date.day}" for date in dt_list],
            "data": [logs.get(date, 0) for date in dt_list],
            "data_title": "每日邀请注册人数",
            "x_label": f"最近{len(dt_list)}天",
            "y_label": "人",
        }


from odoo import fields, models, api

class DCAppointment(models.Model):
    _name = "dc.appointment"
    _description = "Appointments for patient"

    name = fields.Char(string='Appointment Reference',
                       required=True, copy=False, readonly=True)
    phone = fields.Char(string='Phone')
    email = fields.Char(string='Email')
    age = fields.Integer(string='Age')
    description = fields.Text()
    note = fields.Text()
    status = fields.Selection([('draft', 'Draft'), ('confirm', 'Confirmed'), (
        'done', 'Done'), ('cancel', 'Canceled')], default='draft', required=True)
    appointment_date = fields.Datetime(
        string='Appointment Date', default=fields.datetime.now())
    appointed_doctor_id = fields.Many2one(
        "dc.doctor", string="Doctor name", required=True)
    patient_id = fields.Many2one(
        "dc.patient", string='Patient Name', required=True)


#!/usr/bin/env python
from openpyxl import load_workbook
from pdb import set_trace as debug
import re
import os
from bs4 import BeautifulSoup as bs4
import sys
import logging as log
import traceback
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
log.basicConfig(filename='mailmerge.log',level=log.INFO)

log.info(os.getcwd())
log.info(os.path.abspath(__name__))

import smtplib
from email.message import EmailMessage

import datetime
from jinja2 import Template
from nameparser import HumanName
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from html2markdown import convert as html2text
from argparse import ArgumentParser

parser = ArgumentParser(description=u"""Send emails based on data in an excel spreadsheet called data.xlsx.  Emails are
sent to the address listed in the 'email' column and data. The email is sent as HTML with a text part autogenerated from
the HTML. Put the HTML in 'template.html. Subject comes from the <title></title> tag in the head of the template.""")

parser.add_argument("-s", "--stylesheet", action="store_true", help="include stylesheet.css in each emails. This may prevent the text part from being automatically generated. ")

ns = parser.parse_args()

if ns.stylesheet:
    try:
        stylesheet = open('stylesheet.css', 'rb').read().decode('utf-8')
    except Exception as e:
        log.info("No stylesheet found.")
        stylesheet = u""
else:
    stylesheet = u""

wb = load_workbook("data.xlsx")
wsname = wb.sheetnames[0]
ws = wb[wsname]
headers = [cell.value for cell in ws[1]]
headers = {header: headers.index(header) for header in headers}

s = smtplib.SMTP('smtp.snl.salk.edu')

def send(To="", From="", Subject="", Html="", Text=""):
    message = MIMEMultipart('alternative')
    message['From'] = From
    message['To'] = To
    message['Subject'] = Subject

    if not Text:
        Text = html2text(Html)

    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(Text, 'text')
    part2 = MIMEText(Html, 'html')

    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.
    message.attach(part1)
    message.attach(part2)

    try:
        s.send_message(message)
        log.info("Emailed {0} -> {1}".format(To, Text))
    except Exception as e:
        log.warning("Failed to send message to {0}".format(To))
        tb = traceback.format_exc()
        print(tb)

    return()

def clean_template(html):
    soup = bs4(html, "lxml")
    try:
        #Subject might be in the first line of the body.
        Subject = soup.p.text.split("Subject:")[1].strip()
        #Now delete the subject from the template
        soup.p.extract()
    except IndexError:
        #We couldn't find a subject in the template, let's use the name of the parent folder unless it's "mailmerge"
        Subject = os.path.split(os.getcwd())[1]
        if Subject == "mailmerge":
            print("Could not find a Subject. Put a Subject: line as the first line of your template or rename the mailmerge folder to be the subject.")
            sys.exit()

    #Remove meta and style sections of the head
    [meta.extract() for meta in soup('meta')]
    [style.extract() for style in soup('style')]
    htmlTemplate = str(soup)
    return(htmlTemplate, Subject)

htmlTemplate = open("template.html")
htmlTemplate, Subject = clean_template(htmlTemplate)

message_count = 0

rownumber = 1
for row in ws.iter_rows(min_row=2):
    rownumber += 1
    rowDict = {}
    for header in headers:
        rowDict[header] = row[headers[header]].value

    if not rowDict['to'] or not rowDict['from']:
        continue

    skip = str(rowDict.get("skiprow", ""))
    if skip and skip.lower() in ["skip", "yes", "true", "1"]:
        print("skipping row {0}.".format(rownumber))
        continue

    #If the spreadsheet defines a new templatename, then re-render the template. Otherwise, use the old one.
    templatefile = rowDict.get("template")
    if templatefile:
        try:
            htmlTemplate = open(templatefile, 'rb').read()
            htmlTemplate, Subject = clean_template(htmlTemplate)
        except IOError:
            print(u"You have specified a template file {0} that doesn't exist. Exiting...")
            sys.exit(1)

    name = rowDict.get('Name')
    if name:
    #Guess at the first and last name based on the full name. Put them into the dictionary for use in the template.
        try:
            nameparser = HumanName(name)
            rowDict['Firstname'] = nameparser.first
            rowDict['Lastname'] = nameparser.last
        except TypeError: #Occurs when name None.
            pass

    rowDict['stylesheet'] = stylesheet  #Ususally, this does nothing.
    template = Template(htmlTemplate)
    html = template.render(rowDict)

    redirect = rowDict.get('redirect')
    if redirect:
        print("Redirecting {0} to {1}".format(rowDict['to'], redirect))
        rowDict['to'] = redirect

    send(To=rowDict['to'], From=rowDict['from'], Subject=Subject, Html=html)
    logmessage = u"{2} : {1} ==> {0}".format(rowDict['to'], Subject, datetime.datetime.now())
    print(logmessage)
    log.info(logmessage)
    message_count += 1

print("Sent {0} messages".format(message_count))


import os
import pymongo
# Grab the file that has the mongo URI data
if os.path.exists("env.py"):
    import env

# Create constants for URI login data / database / collection in database
MONGO_URI = os.environ.get("MONGO_URI")
DATABASE = "FirstCluster"
COLLECTION = "myFirstMDB"

# Connect to mongo
def mongo_connect(url):
    try:
        conn = pymongo.MongoClient(url)
        print("Mongo is connected")
        return conn
    except:
        print("Could not connect to MongoDB: %s") %e

# Run connect to mongo function
conn = mongo_connect(MONGO_URI)
# Add collection to a variable
coll = conn[DATABASE][COLLECTION]

# Create and insert a new doc to collection
#new_doc = {"first": "duglas", "last": "adams", "dob": "11/03/1952", "hair_colour": "grey", "occupation": "writer", "nationality": "british"}
#coll.insert(new_doc)

# Create and insert multiple new docs to collection
#new_docs = [{"first": "terry",
#             "last": "pratchett",
#             "dob": "28/04/1948",
#             "gender": "m",
#             "hair_colour": "not much",
#             "occupation": "writer",
#             "nationality": "british"},
#
#             {"first": "george",
#             "last": "rr martin",
#             "dob": "20/09/1948",
#             "gender": "m",
#             "hair_colour": "white",
#             "occupation": "writer",
#             "nationality": "american"}]
#coll.insert_many(new_docs)

# Show all documents in collection
#documents = coll.find()

# Find documents in collection
#documents = coll.find({"first": "duglas"})

# Remove document
#coll.remove({"first": "duglas"})

# Update single document
#coll.update_one({"nationality": "american"}, {"$set": {"hair_colour": "maroon"}})

# Update many documents
#coll.update_many({"nationality": "american"}, {"$set": {"hair_colour": "blue"}})

#documents = coll.find({"nationality": "american"})
for doc in documents:
    print(doc)


#входные данные: число (факториал какого-то числа)
#выходные данные: число (то, с которого этот факториал получился)
#пример. входные данные: 120, результат: 5 (max =1000) ==> точные числа
print ('Enter any chislo factorial')
a = int(input())
i=1
s=1
if a>1000:
          exit()
if (a % 6) ==0 or ((a==1) or (a==2)):
          while s <a:
                    if s<a:
                              a=a/i
                              i=i+1
          if i>1:
                    i=i-1
          if a<=0:
                    a=1
                    i=1
          print ('chislo factorial=', i)
else:
          print ('Error #144')
input()


#!/usr/bin/python3
"""
This module defines the BaseModel class.
"""
import uuid
from datetime import datetime

class BaseModel:
    """
    A base class that defines common attributes
    and methods for other classes.
    """

    def __init__(self, *args, **kwargs):
        """Initialize instance attributes."""
        if kwargs:
            for key, value in kwargs.items():
                if key == 'created_at' or key == 'updated_at':
                    setattr(self, key, datetime.strptime(
                        value, '%Y-%m-%dT%H:%M:%S.%f'))
                elif key != '__class__':
                    setattr(self, key, value)
        else:
            self.id = str(uuid.uuid4())
            self.created_at = datetime.now()
            self.updated_at = datetime.now()

    def __str__(self):
        """Return a string representation of the instance."""
        class_name = self.__class__.__name__
        return "[{}] ({}) {}".format(class_name, self.id, self.__dict__)

    def save(self):
        """
        Update the public instance attribute
        'updated_at' with the current datetime.
        """
        self.updated_at = datetime.now()

    def to_dict(self):
        """Return a dictionary containing all keys/values of the instance."""
        instance_dict = self.__dict__.copy()
        instance_dict['__class__'] = self.__class__.__name__
        instance_dict['created_at'] = self.created_at.isoformat()
        instance_dict['updated_at'] = self.updated_at.isoformat()
        return instance_dict

if __name__ == "__main__":
    my_model = BaseModel()
    my_model.name = "My First Model"
    my_model.my_number = 89
    print(my_model)
    my_model.save()
    print(my_model)
    my_model_json = my_model.to_dict()
    print(my_model_json)
    print("JSON of my_model:")
    for key in my_model_json.keys():
        print("\t{}: ({}) - {}".format(key, type(my_model_json[key]),
                                       my_model_json[key]))


from boggle import Boggle
from flask import Flask,session,render_template,redirect,Request,request,jsonify

boggle_game = Boggle()

app = Flask(__name__)
app.config['SECRET_KEY'] = 'the random string'
boggle = Boggle()
@app.route('/')
def start_game():
    board = boggle.make_board()
    session['board'] = board
    return render_template('Start.html',board = board)

@app.route("/check-word")
def check_word():

    guess = request.args["guess"]
    board = session["board"]
    response = boggle_game.check_valid_word(board, guess)

    return jsonify({'result': response})

@app.route('/save-score', methods=['POST'])
def save_score():
    new_score = request.json['scores']
    if 'score' in session and session['score'] < new_score:
        session['score'] = new_score
    elif 'score' not in session:
        session['score'] = new_score
    return jsonify({'scores': session['score']})


import re
import nltk
from nltk.corpus import stopwords
import spacy
import textacy
import contractions

punctuations = '''~!@#$%^&*()"_+-=`;':,./<>?'''

text = '''
Dear PayPal User,

We regret to inform you that your PayPal account has been compromised and we have noticed some unauthorized activities. To protect your account, we kindly request you to verify your information immediately.

Click on the following link to verify your account: paypalredirect.co
b
Please note that failure to verify your account within 24 hours will result in permanent account suspension.

Thank you for your cooperation.

Best regards,
PayPal Security Team
'''

#Contraction fixing
text = contractions.fix(text)

#removing punctuation + lowercase
text = re.sub(r'[^\w\s]', '',text).lower()

nltk.download('punkt')
nltk.download('wordnet')

#tokenization
tokenized_text = nltk.word_tokenize(text)

print(tokenized_text)

nltk.download('stopwords')
stop_words = stopwords.words('english')

#Removing Stop words
filtered_tokens = []
for token in tokenized_text:
    if token.lower() not in stop_words:
        filtered_tokens.append(token)

print(filtered_tokens)

#lemmatization
nlp = spacy.load("en_core_web_sm")

lem_words = []
for word in filtered_tokens:
    doc = nlp(word)
    for token in doc:
        lem_words.append(token.lemma_)

print(lem_words)


import numpy as np

def calculate(list):

  calculations={
  'mean': [],
  'variance': [],
  'standard deviation': [],
  'max': [],
  'min': [],
  'sum': []
}

  #failure
  if len(list) < 9:
    raise ValueError('List must contain nine numbers.')

  #convert to 3x3 array
  a = np.resize(list,(3,3))

  #mean
  calculations['mean'].append(np.average(a,axis=0).tolist())
  calculations['mean'].append(np.average(a,axis=1).tolist())
  calculations['mean'].append(np.average(list).tolist())

  #variance
  calculations['variance'].append(np.var(a,axis=0).tolist())
  calculations['variance'].append(np.var(a,axis=1).tolist())
  calculations['variance'].append(np.var(list).tolist())

  #standard dev
  calculations['standard deviation'].append(np.std(a,axis=0).tolist())
  calculations['standard deviation'].append(np.std(a,axis=1).tolist())
  calculations['standard deviation'].append(np.std(list).tolist())

  #max
  calculations['max'].append(np.amax(a,axis=0).tolist())
  calculations['max'].append(np.amax(a,axis=1).tolist())
  calculations['max'].append(np.amax(list).tolist())

  #min
  calculations['min'].append(np.amin(a,axis=0).tolist())
  calculations['min'].append(np.amin(a,axis=1).tolist())
  calculations['min'].append(np.amin(list).tolist())

  #sum
  calculations['sum'].append(np.sum(a,axis=0).tolist())
  calculations['sum'].append(np.sum(a,axis=1).tolist())
  calculations['sum'].append(np.sum(list).tolist())

  #output

  return calculations


import sqlite3
import json
from lxml import etree

def get_vehicle_score(vehicle, route_length=450):
    consumption = (int(vehicle['fuel_consumption']) / 100) * route_length
    number_of_pit_stops = consumption // int(vehicle['engine_capacity'])
    score = 0
    # Truck capacity. If the capacity is 20 tones or more, it gets 2 points.
    if int(vehicle['maximum_load']) >= 20:
        score += 2
    # Fuel consumed over the entire trip. If a truck burned 230 liters or less, 2 points are given. If more — 1 point
    if consumption <= 230:
        score += 2
    else:
        score += 1
    # Number of pit-stops. If there are two or more gas stops on the way, the object has 0 points. One stop at the
    # filling station means 1 point. No stops — 2 scoring points.
    if number_of_pit_stops >= 2:
        pass
    elif number_of_pit_stops == 1:
        score += 1
    else:
        score += 2

    return score

def dataframe_to_database(df, filename):
    db_path = filename.replace('[CHECKED]', '').split('.').pop(0)
    db_path = '{}.s3db'.format(db_path)
    con = sqlite3.connect(db_path)
    cursor = con.cursor()

    records = 0
    # create the convoy table
    cursor.execute("""CREATE TABLE IF NOT EXISTS convoy(
            vehicle_id INTEGER PRIMARY KEY,
            engine_capacity INTEGER NOT NULL,
            fuel_consumption INTEGER NOT NULL,
            maximum_load INTEGER NOT NULL,
            score INTEGER NOT NULL
            );""")
    for index, row in df.iterrows():
        score = get_vehicle_score(row)
        entry = (row['vehicle_id'],
                 row['engine_capacity'],
                 row['fuel_consumption'],
                 row['maximum_load'],
                 score)
        try:
            cursor.execute("""INSERT INTO
            convoy(vehicle_id, engine_capacity, fuel_consumption, maximum_load, score)
            VALUES (?, ?, ?, ?, ?)""", entry)
            records += 1
        except sqlite3.IntegrityError:
            # duplicate entry
            pass
    con.commit()

    msg = "record was" if records == 1 else "records were"
    print("{} {} inserted into {}".format(records, msg, db_path))

    # clean up
    cursor.close()
    con.close()
    # get the database path on completion
    return db_path

def database_to_json(db_path):
    con = sqlite3.connect(db_path)
    cursor = con.cursor()
    cursor.execute("""SELECT vehicle_id, engine_capacity, fuel_consumption, maximum_load, score FROM convoy""")
    vehicles = cursor.fetchall()
    vehicles_added = 0
    json_path = db_path.replace('.s3db', '.json')
    with open(json_path, 'w') as json_file:
        convoy_dict = dict()
        convoy_dict['convoy'] = []
        for v in vehicles:
            vehicle_id, engine_capacity, fuel_consumption, maximum_load, score = v
            if score > 3:
                convoy_dict['convoy'].append({
                    'vehicle_id': vehicle_id,
                    'engine_capacity': engine_capacity,
                    'fuel_consumption': fuel_consumption,
                    'maximum_load': maximum_load
                })
                vehicles_added += 1
        json.dump(convoy_dict, json_file)

        msg = "vehicle was" if vehicles_added == 1 else "vehicles were"
        print("{} {} inserted into {}".format(vehicles_added, msg, json_path))

    # clean up
    cursor.close()
    con.close()

    return json_path

def database_to_xml(db_path):
    con = sqlite3.connect(db_path)
    cursor = con.cursor()
    cursor.execute("""SELECT vehicle_id, engine_capacity, fuel_consumption, maximum_load, score FROM convoy""")
    vehicles = cursor.fetchall()
    vehicles_added = 0
    xml_path = db_path.replace('.s3db', '.xml')
    convoy_xml = "<convoy>"
    for v in vehicles:
        vehicle_id, engine_capacity, fuel_consumption, maximum_load, score = v
        if score <= 3:
            convoy_xml += (
                "<vehicle><vehicle_id>{}</vehicle_id><engine_capacity>{}</engine_capacity><fuel_consumption>{"
                "}</fuel_consumption><maximum_load>{}</maximum_load></vehicle>"
                .format(vehicle_id, engine_capacity, fuel_consumption, maximum_load))
            vehicles_added += 1

    convoy_xml += "</convoy>"
    root = etree.fromstring(convoy_xml)
    tree = etree.ElementTree(root)  # create an instance of ElementTree in order to save it
    # prevent creation of self-closing tags
    # https://stackoverflow.com/questions/41890415/keep-lxml-from-creating-self-closing-tags
    for node in tree.iter():
        if node.text is None:
            node.text = ''
    tree.write(xml_path)

    msg = "vehicle was" if vehicles_added == 1 else "vehicles were"
    print("{} {} inserted into {}".format(vehicles_added, msg, xml_path))

    # clean up
    cursor.close()
    con.close()

    return xml_path


import os
import argparse
import torch
import matplotlib
import matplotlib.pyplot as plt

from detr.models.detr import buildLines
from dataloaders import CustomDataset, prepare_dataloaders, TuSimpleDataset
from torch.utils.data import DataLoader
from collections import OrderedDict
from scipy.optimize import linear_sum_assignment

import numpy as np
import matplotlib.cm as cm
from scipy.ndimage.filters import gaussian_filter

import warnings
warnings.filterwarnings("ignore")

matplotlib.use('TkAgg')

def get_args_parser():
    parser = argparse.ArgumentParser('Set transformer detector', add_help=False)
    parser.add_argument('--lr', default=1e-4, type=float)
    parser.add_argument('--lr_backbone', default=1e-5, type=float)
    parser.add_argument('--batch_size', default=8, type=int)
    parser.add_argument('--weight_decay', default=1e-4, type=float)
    parser.add_argument('--epochs', default=100, type=int)
    parser.add_argument('--lr_drop', default=10, type=int)
    parser.add_argument('--clip_max_norm', default=0.1, type=float,
                        help='gradient clipping max norm')
    # * Backbone
    parser.add_argument('--backbone', default='resnet50', type=str,
                        help="Name of the convolutional backbone to use")
    parser.add_argument('--dilation', action='store_true',
                        help="If true, we replace stride with dilation in the last convolutional block (DC5)")
    parser.add_argument('--position_embedding', default='sine', type=str, choices=('sine', 'learned'),
                        help="Type of positional embedding to use on top of the image features")
    # Loss
    parser.add_argument('--no_aux_loss', dest='aux_loss', action='store_false',
                        help="Disables auxiliary decoding losses (loss at each layer)")
    # * Loss coefficients
    parser.add_argument('--endpoint_weight', default=1, type=float)
    parser.add_argument('--distance_weight', default=1, type=float)
    parser.add_argument('--class_weight', default=1, type=float)
    parser.add_argument('--no_obj_weight', default=0.1, type=float)

    # * Transformer
    parser.add_argument('--enc_layers', default=6, type=int,
                        help="Number of encoding layers in the transformer")
    parser.add_argument('--dec_layers', default=6, type=int,
                        help="Number of decoding layers in the transformer")
    parser.add_argument('--dim_feedforward', default=2048, type=int,
                        help="Intermediate size of the feedforward layers in the transformer blocks")
    parser.add_argument('--hidden_dim', default=256, type=int,
                        help="Size of the embeddings (dimension of the transformer)")
    parser.add_argument('--dropout', default=0.0, type=float,
                        help="Dropout applied in the transformer")
    parser.add_argument('--nheads', default=8, type=int,
                        help="Number of attention heads inside the transformer's attentions")
    parser.add_argument('--num_queries', default=10, type=int,
                        help="Number of query slots")
    parser.add_argument('--num_controlpoints', default=4, type=int,
                        help="Number of controlpoints per predicted line")
    parser.add_argument('--pre_norm', action='store_true')

    # * Dataloader
    parser.add_argument('--crop', action='store_true')
    parser.add_argument('--flip', action='store_true')
    parser.add_argument('--affine', action='store_true')

    # * Logging
    parser.add_argument('--log_dir', default='runs/temp', type=str,
                        help="Name of the logging directory for tensorboard.")
    parser.add_argument('--save_model_dir', default='trained_models/temp', type=str,
                        help="Name of the logging directory to save the model after training")

    # * Segmentation
    parser.add_argument('--masks', action='store_true',
                        help="Train segmentation head if the flag is provided")

    return parser

def plot_image(im, target, pred_logits, pred_controlpoints, pred_points, accuracy):
    for b in range(len(im)):
        indices = pred_logits[b, :, 0] > pred_logits[b, :, 1]

        fig = plt.figure(figsize=(5, 5))
        img = im[b]
        img -= img.amin(dim=(1, 2), keepdim=True)
        img /= img.amax(dim=(1, 2), keepdim=True)
        plt.imshow(img.permute(1, 2, 0), extent=[0, 1, 1, 0])

        p_points = pred_points[b, indices]
        p_controlpoints = pred_controlpoints[b, indices]

        # colors = ['firebrick', 'gold', 'steelblue']
        # if len(p_points) == 3:
            # plt.show()
            # plt.imshow(img.permute(1, 2, 0), extent=[0, 1, 1, 0])
            # # for tt in target:
                # # plt.scatter(tt[0, :, 1], tt[0, :, 0], s=4, color='green')
            # for c, to in zip(colors, p_points):
                # plt.scatter(to[:, 1], to[:, 0], s=4, color=c)
            # for c, tcp in zip(colors, p_controlpoints):
                # plt.scatter(tcp[:, 1], tcp[:, 0], color=c)

            # # plt.annotate(f"Accuracy: {accuracy}", xy=(0.01, 0.05), c='yellow', backgroundcolor='black')
            # plt.show()

        for tt in target:
            plt.scatter(tt[0, :, 1], tt[0, :, 0], s=4, color='green')
        for to in p_points:
            plt.scatter(to[:, 1], to[:, 0], s=4, color='blue')
        for tcp in p_controlpoints:
            plt.scatter(tcp[:, 1], tcp[:, 0], color='red')

        # plt.annotate(f"Accuracy: {accuracy}", xy=(0.01, 0.05), c='yellow', backgroundcolor='black')
        plt.show()

def calc_accuracy(pred_points, target, pred_logits, threshold):
    indices = pred_logits[0, :, 0] > pred_logits[0, :, 1]
    pred_points = pred_points[0, indices]

    # print(pred_points.size()) # torch.Size([3, 100, 2])

    target_lane_distances = [] # all target lanes to all predict lanes ----- num_target_lanes x num_pred_lanes
    for target_lane in target:
        pred_lane_distances = [] # single target lane to all predict lanes
        for pred_lane in pred_points:
            distances = [] # target points to minimum predict points
            for target_lane_point in target_lane[0]:
                point_distances = (pred_lane - target_lane_point).abs().sum(dim=1) # target point to all predict points
                min_index = point_distances.argmin()
                distances.append(point_distances[min_index])
            pred_lane_distances.append(torch.tensor(distances))
        target_lane_distances.append(pred_lane_distances)

    cost_matrix = torch.tensor([[sum(l) for l in t] for t in target_lane_distances])
    row_ind, col_ind = linear_sum_assignment(cost_matrix)

    correct_points = 0
    for r, c in zip(row_ind, col_ind):
        t = target[r][0]
        p = pred_points[c]

        for t_point in t:
            d = (t_point[0] - p[:, 0]).abs()
            min_i = d.argmin()
            closest_point = p[min_i]
            if (t_point[1] - closest_point[1]).abs() < threshold / 1280:
                correct_points += 1

    total_points = sum([len(t[0]) for t in target])
    return correct_points / total_points

def myplot(x, y, s, bins=1000):
    # heatmap, xedges, yedges = np.histogram2d(x, [(ys-1)*-1 for ys in y], bins=bins, range= [[0, 1], [0, 1]])
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=bins, range= [[0, 1], [0, 1]])
    heatmap = gaussian_filter(heatmap, sigma=s)

    return heatmap.T

if __name__ == '__main__':
    parser = argparse.ArgumentParser('DETR training and evaluation script', parents=[get_args_parser()])
    args = parser.parse_args()

    world_size = torch.cuda.device_count()
    print(f"Using {world_size} GPU(s).")

    model, criterion = buildLines(args)

    state_dict = torch.load("trained_models/DETRLines_tusimple_flip111.pt")
    fixed_state_dict = OrderedDict()
    for k, v in state_dict.items():
        name = k[7:] # remove `module.`
        if name == "curve_eval.uspan" or name == "curve_eval.Nu":
            continue
        fixed_state_dict[name] = v
    model.load_state_dict(fixed_state_dict)
    model.cuda()

    dataset = TuSimpleDataset('tusimple/', ['label_data_0313.json'], args, test_mode=True)
    dataset2 = TuSimpleDataset('tusimple/', ['test_label.json'], args, test_mode=False)
    dl = DataLoader(dataset, batch_size=1)
    dl2 = DataLoader(dataset2, batch_size=1)

    print("min_lanes:", dataset.min_lanes)
    print("max_lanes:", dataset.max_lanes)

    print(len(dataset))

    # query_embed = fixed_state_dict['query_embed.weight'].cpu()

    # print(query_embed)
    # print(query_embed.size())
    # plt.imshow(query_embed, cmap='gray')
    # plt.show()

    # accuracy_accumulator = 0
    # for im, target in dl:
        # # print(im.size()) # torch.Size([1, 3, 720, 1280])
        # im = im.cuda()

        # out = model(im)

        # pred_logits = out["pred_logits"]
        # pred_controlpoints = out["pred_controlpoints"]
        # pred_points = out["pred_points"]

        # accuracy = calc_accuracy(pred_points.detach().cpu(), target, pred_logits.detach().cpu(), 20)
        # # print(accuracy)

        # accuracy_accumulator += accuracy

        # # if accuracy > 0.95:
            # # plot_image(im.cpu(), target, pred_logits.detach().cpu(), pred_controlpoints.detach().cpu(), pred_points.detach().cpu(), accuracy)

    loss_accumulator = 0
    cp_list = [[] for i in range(10)]
    ha = 0
    for im, target in dl2:
        ha += 1
        # if ha == 10:
            # break
        im = im.cuda()

        out = model(im)

        for i in range(len(out['pred_controlpoints'][0])):
            cp_list[i].append(out['pred_controlpoints'][0][i].detach().cpu())

        # target_list = []
        # for t in target:
            # target_list.append({'labels': torch.zeros(t.size(0), dtype=int).cuda(),
                                # 'points': t.cuda()})

        # loss_dict = criterion(out, target_list)

        # loss_accumulator += loss_dict['loss_distance'].item()

    cp_list = [torch.stack(x, dim=0) for x in cp_list]

    for he in range(len(cp_list)):
        x = list(cp_list[he][:, :, 1].flatten())
        y = list(cp_list[he][:, :, 0].flatten())

        plt.figure()

        img = myplot(x, y, 16)
        plt.imshow(img, extent=[0, 1, 0, 1], cmap=cm.jet)

        plt.tight_layout()
        plt.savefig(f'..\Images\controlpoints heatmaps\{he}.png', bbox_inches='tight', transparent=True)
        # plt.show()

    # for cps in cp_list:
        # average_cps = cps.mean(dim=0)

        # print(average_cps.size())

        # plt.figure()

        # xplot = [xd for xd in average_cps[:, 1]]
        # yplot = [yd for yd in average_cps[:, 0]]

        # plt.scatter(xplot, yplot)

        # plt.xlim(0, 1)
        # plt.ylim(1, 0)

        # plt.show()

    # print("Average accuracy:", accuracy_accumulator / len(dataset))
    # print("Average distance loss:", loss_accumulator / len(dataset))
    print("Done")


# 特定のディレクトリにある画像を別のディレクトリへ移動させる
import os
import argparse
from typing import Tuple
import shutil

def count_images(path: str) -> int:
  """ディレクトリ内の画像数を調べる

  Args:
      path (str): 画像数を調べるフォルダのパス

  Returns:
      int: 画像数
  """

  return sum(os.path.isfile(os.path.join(path, name)) for name in os.listdir(path))

def get_copy_dir() -> tuple:
  """コピー元とコピー先ディレクトリ名を取得する

  Returns:
      Tuple[str, str]: [0]コピー元, [1]コピー先ディレクトリ名
  """
  parser: argparse.ArgumentParser = argparse.ArgumentParser()
  parser.add_argument('origin')
  parser.add_argument('copy_goal')
  args: argparse.Namespace = parser.parse_args()

  return (args.origin, args.copy_goal)

def copy_images(image_num: int, origin_dir: str, copy_dir: str):
  """画像を指定のディレクトリに移動する

  Args:
      image_num (int): 画像数（ファイル名の重複を防ぐため）
      origin_dir (str): コピー元ディレクトリパス
      copy_dir (str): コピー先ディレクトリパス
  """
  index: int = 0
  for img in os.listdir(origin_dir):
    filename: str = os.path.splitext(os.path.basename(img))[0]
    new_filepath = os.path.join(copy_dir, f"{filename}-{image_num + index}.jpg")
    shutil.move(os.path.join(origin_dir, img), new_filepath)
    index += 1

if __name__ == "__main__":
  dir: Tuple[str, str] = get_copy_dir()
  img_num: int = count_images(dir[0])
  copy_images(img_num, dir[0], dir[1])
  os.rmdir(dir[0])


"""Flask app for Cupcakes"""

from flask import Flask, request, jsonify, render_template

from models import Cupcake, db, connect_db

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///cupcake'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = '123456789'

connect_db(app)

@app.route('/', methods=['GET'])
def home():
    return render_template('index.html')

@app.route('/api/cupcakes', methods=["GET"])
def show_all_cupcakes():
    cupcakes = [cupcake.serialize_cupcake() for cupcake in Cupcake.query.all()]
    return jsonify(cupcakes = cupcakes)

@app.route('/api/cupcakes/<int:cupcake_id>', methods=["GET"])
def show_cupcake_by_id(cupcake_id):
    cupcake = Cupcake.query.get_or_404(cupcake_id)
    return (jsonify(cupcake = cupcake.serialize_cupcake()))

@app.route('/api/cupcakes', methods=["POST"])
def add_a_cupcake():

    resp = request.json

    print(resp)
    print('*******')

    cupcake = Cupcake(
        flavor = resp['flavor'],
        size = resp['size'],
        rating = resp['rating'],
        image = resp['image'],
    )

    print(cupcake)
    db.session.add(cupcake)
    db.session.commit()

    return (jsonify(cupcake = cupcake.serialize_cupcake()), 201)

@app.route('/api/cupcakes/<int:cupcake_id>', methods=["PATCH"])
def update_a_cupcake_by_id(cupcake_id):

    resp = request.json

    cupcake = Cupcake.query.get_or_404(cupcake_id)
    cupcake.flavor = resp['flavor'],
    cupcake.size = resp['size'],
    cupcake.rating = resp['rating'],

    db.session.add(cupcake)
    db.session.commit()
    return jsonify(cupcake = cupcake.serialize_cupcake())

@app.route('/api/cupcakes/<int:cupcake_id>', methods=["DELETE"])
def remove_a_cupcake(cupcake_id):

    cupcake = Cupcake.query.get_or_404(cupcake_id)
    db.session.delete(cupcake)
    db.session.commit()
    return jsonify(message = "Deleted")


"""
Series of tests and example code to run elecsus via the API

Last updated 2018-07-04 MAZ
"""
# py 2.7 compatibility
from __future__ import (division, print_function, absolute_import)

import matplotlib.pyplot as plt
import numpy as np
import time

import sys
sys.path.append('../')
import elecsus_methods as EM
sys.path.append('../libs')
from spectra import get_spectra

def test_MLfit():
	"""
	Test simple Marquart-Levenburg fit for S1 spectrum. Generate a data set and add random noise,
	then run an ML fit to the noisy data set, with randomised initial parameters that are the correct ones
	plus some small change
	"""

	p_dict = {'Elem':'Rb','Dline':'D2','T':80.,'lcell':2e-3,'Bfield':600.,'Btheta':0.,
		'Bphi':0.,'GammaBuf':0.,'shift':0.}

	p_dict_guess = p_dict

	# only need to specify parameters that are varied
	p_dict_bools = {'T':True,'Bfield':True}

	E_in = np.sqrt(1./2) * np.array([1.,1.,0])
	# need to express E_in as Ex, Ey and phase difference for fitting
	E_in_angle = [E_in[0].real,[abs(E_in[1]),np.angle(E_in[1])]]

	# Generate spectrum and add some random noise
	x = np.linspace(-10000,10000,300)
	[y] = get_spectra(x,E_in,p_dict,outputs=['S1']) + np.random.randn(len(x))*0.01

	# randomise the starting parameters a little
	p_dict_guess = p_dict
	p_dict_guess['T'] += np.random.randn()*1
	p_dict_guess['Bfield'] += np.random.randn()*10

	data = [x,y.real]

	best_params, RMS, result = EM.fit_data(data, p_dict_guess, p_dict_bools, E_in=E_in_angle, data_type='S1',fit_algorithm='ML')
	report = result.fit_report()
	fit = result.best_fit

	print(report)

	plt.plot(x,y,'k.',label='Data')
	plt.plot(x,fit,'r-',lw=2,label='Fit')

	plt.legend(loc=0)

	plt.show()

def compare_fit_methods():
	"""
	Compare ML and RR, SA and differential_evolution (DE) fitting for a more complicated case where there is a chi-squared local minimum in parameter space.
	In this case, the simple ML fit doesn't work, since it gets stuck in a very bad local minimum.

	The RR, SA and DE methods all converge to a good solution, but in different times.
	The RR fit takes somewhere in the region of 50x longer than ML, but produces a much better fit.
	The SA fit takes somewhere in the region of 100x longer than ML, but produces a much better fit.
	The DE fit takes somewhere in the region of 20x longer than ML, but produces a much better fit.

	From this test (and others which are broadly similar but for different data sets), we see that for global minimisation, we recommend using DE
	"""

	p_dict = {'Elem':'Rb','Dline':'D2','T':100.,'lcell':5e-3,'Bfield':7000.,'Btheta':0.,
		'Bphi':0.,'GammaBuf':50,'shift':0.}

	# only need to specify parameters that are varied
	p_dict_bools = {'T':True,'Bfield':True,'GammaBuf':True}#,'E_x':True,'E_y':True,'E_phase':True}

	E_in = np.array([0.7,0.7,0])
	# need to express E_in as Ex, Ey and phase difference for fitting
	E_in_angle = [E_in[0].real,[abs(E_in[1]),np.angle(E_in[1])]]

	x = np.linspace(-21000,-13000,300)
	y = get_spectra(x,E_in,p_dict,outputs=['S0'])[0] + np.random.randn(len(x))*0.01 # <<< RMS error should be around 0.01 for the best case fit
	data = [x,y.real]

	# start at not the optimal parameters (near a local minimum in chi-squared)
	p_dict['T'] = 90
	p_dict['Bfield'] = 6100
	p_dict['GammaBuf'] = 150

	p_dict_bounds = {}

	# time it...
	st_ML = time.time()
	best_paramsML, RMS_ML, resultML = EM.fit_data(data, p_dict, p_dict_bools, E_in=E_in_angle, data_type='S0')
	et_ML = time.time() -st_ML
	print('ML complete')

	# RR and SA need range over which to search
	p_dict_bounds['T'] = [30]
	p_dict_bounds['Bfield'] = [3000]
	p_dict_bounds['GammaBuf'] = [150]

	st_RR = time.time()
	best_paramsRR, RMS_RR, resultRR = EM.fit_data(data, p_dict, p_dict_bools, E_in=E_in_angle,
																					p_dict_bounds=p_dict_bounds, data_type='S0', fit_algorithm='RR')
	et_RR = time.time() - st_RR
	print('RR complete')

	st_SA = time.time()
	best_paramsSA, RMS_SA, resultSA = EM.fit_data(data, p_dict, p_dict_bools, E_in=E_in_angle,
																					p_dict_bounds=p_dict_bounds, data_type='S0', fit_algorithm='SA')
	et_SA = time.time() - st_SA
	print('SA complete')

	# differential evolution needs upper and lower bounds on fit parameters
	p_dict_bounds['T'] = [70,130]
	p_dict_bounds['Bfield'] = [4000,8000]
	p_dict_bounds['GammaBuf'] = [0, 100]

	st_DE = time.time()
	best_paramsDE, RMS_DE, resultDE = EM.fit_data(data, p_dict, p_dict_bools, E_in=E_in_angle,
																					p_dict_bounds=p_dict_bounds, data_type='S0',
																					fit_algorithm='differential_evolution')
	et_DE = time.time() - st_DE
	print('DE complete')

	print(('ML found best params in ', et_ML, 'seconds. RMS error, ', RMS_ML))
	print(('RR found best params in ', et_RR, 'seconds. RMS error, ', RMS_RR))
	print(('SA found best params in ', et_SA, 'seconds. RMS error, ', RMS_SA))
	print(('DE found best params in ', et_DE, 'seconds. RMS error, ', RMS_DE))

	reportML = resultML.fit_report()
	fitML = resultML.best_fit
	reportRR = resultRR.fit_report()
	fitRR = resultRR.best_fit
	reportSA = resultSA.fit_report()
	fitSA = resultSA.best_fit
	reportDE = resultDE.fit_report()
	fitDE = resultDE.best_fit

	#print '\n ML fit report:'
	#print reportML
	#print '\n DE fit report:'
	#print reportDE

	plt.plot(x,y,'k.',label='Data')
	plt.plot(x,fitML,'-',lw=2,label='ML fit')
	plt.plot(x,fitDE,'-',lw=2,label='RR fit')
	plt.plot(x,fitDE,'--',lw=2,label='SA fit')
	plt.plot(x,fitDE,':',lw=2,label='DE fit')

	plt.legend(loc=0)

	plt.show()

if __name__ == '__main__':
	compare_fit_methods()


import numpy as np
import pandas as pd
import torch
import torch.utils.data as Data
from graphset import *
from scipy.sparse import coo_matrix

def CalculateGraphFeat(feat_mat,adj_list):
    assert feat_mat.shape[0] == len(adj_list)
    adj_mat = np.zeros((len(adj_list), len(adj_list)), dtype='float32')
    for i in range(len(adj_list)):
        nodes = adj_list[i]
        for each in nodes:
            adj_mat[i,int(each)] = 1
    assert np.allclose(adj_mat,adj_mat.T)
    x, y = np.where(adj_mat == 1)
    adj_index = np.array(np.vstack((x, y)))
    return [feat_mat,adj_index]

def FeatureExtract(drug_feature):
    drug_data = [[] for item in range(len(drug_feature))]
    for i in range(len(drug_feature)):
        feat_mat,adj_list,_ = drug_feature.iloc[i]
        drug_data[i] = CalculateGraphFeat(feat_mat,adj_list)
    return drug_data

def cmask(num, ratio, seed):
    mask = np.ones(num, dtype=bool)
    mask[0:int(ratio * num)] = False
    np.random.seed(seed)
    np.random.shuffle(mask)
    return mask

def process(drug_feature, mutation_feature, gexpr_feature, methylation_feature, data_new, nb_celllines, nb_drugs):
    #-----construct cell line-drug response pairs
    cellineid = list(set([item[0] for item in data_new]));cellineid.sort()
    pubmedid = list(set([item[1] for item in data_new]));pubmedid.sort()
    cellmap = list(zip(cellineid,list(range(len(cellineid)))))
    pubmedmap = list(zip(pubmedid,list(range(len(cellineid),len(cellineid)+len(pubmedid)))))
    cellline_num = np.squeeze([[j[1] for j in cellmap if i[0]==j[0]] for i in data_new])
    pubmed_num = np.squeeze([[j[1] for j in pubmedmap if i[1]==j[0]] for i in data_new])
    IC_num = np.squeeze([i[2] for i in data_new])
    allpairs = np.vstack((cellline_num,pubmed_num,IC_num)).T
    allpairs = allpairs[allpairs[:,2].argsort()]

    #----drug_feature_input
    pubid=[item[0] for item in pubmedmap]
    drug_feature=pd.DataFrame(drug_feature).T
    drug_feature=drug_feature.loc[pubid]
    atom_shape=drug_feature[0][0].shape[-1]
    drug_data = FeatureExtract(drug_feature)
    #----cell line_feature_input
    cellid=[item[0] for item in cellmap]
    gexpr_feature=gexpr_feature.loc[cellid]
    mutation_feature=mutation_feature.loc[cellid]
    methylation_feature=methylation_feature.loc[cellid]
    mutation = torch.from_numpy(np.array(mutation_feature,dtype='float32'))
    mutation = torch.unsqueeze(mutation, dim=1)
    mutation = torch.unsqueeze(mutation, dim=1)
    gexpr = torch.from_numpy(np.array(gexpr_feature,dtype='float32'))
    methylation = torch.from_numpy(np.array(methylation_feature,dtype='float32'))

    #---compile training set and test set
    drug_set = Data.DataLoader(dataset=GraphDataset(graphs_dict=drug_data),collate_fn=collate,batch_size=nb_drugs,shuffle=False)
    cellline_set = Data.DataLoader(dataset=Data.TensorDataset(mutation,gexpr,methylation),batch_size=nb_celllines,shuffle=False)
    use_independent_testset=True
    if(use_independent_testset == True):
        edge_mask = cmask(len(allpairs), 0.1, 666)
        train = allpairs[edge_mask][:, 0:3]
        test = allpairs[~edge_mask][:, 0:3]
    else:
        CV_edgemask = cmask(len(allpairs), 0.1, 666)
        cross_validation = allpairs[CV_edgemask][:, 0:3]
        vali_mask = cmask(len(cross_validation), 0.2, 66)
        train = cross_validation[vali_mask][:, 0:3]
        test = cross_validation[~vali_mask][:, 0:3]
    train[:, 1] -= nb_celllines
    test[:, 1] -= nb_celllines
    train_mask = coo_matrix((np.ones(train.shape[0], dtype=bool), (train[:, 0], train[:, 1])),
                            shape=(nb_celllines, nb_drugs)).toarray()
    test_mask = coo_matrix((np.ones(test.shape[0], dtype=bool), (test[:, 0], test[:, 1])),
                           shape=(nb_celllines, nb_drugs)).toarray()
    train_mask = torch.from_numpy(train_mask).view(-1)
    test_mask = torch.from_numpy(test_mask).view(-1)
    if (use_independent_testset == True):
        pos_edge = allpairs[allpairs[:, 2] == 1, 0:2]
        neg_edge = allpairs[allpairs[:, 2] == -1, 0:2]
    else:
        pos_edge = cross_validation[cross_validation[:, 2] == 1, 0:2]
        neg_edge = cross_validation[cross_validation[:, 2] == -1, 0:2]
    pos_edge[:, 1] -= nb_celllines
    neg_edge[:, 1] -= nb_celllines
    label_pos = coo_matrix((np.ones(pos_edge.shape[0]), (pos_edge[:, 0], pos_edge[:, 1])),
                           shape=(nb_celllines, nb_drugs)).toarray()
    label_pos = torch.from_numpy(label_pos).type(torch.FloatTensor).view(-1)
    if (use_independent_testset == True):
        train_edge = allpairs[edge_mask]
    else:
        train_edge = cross_validation[vali_mask]
    train_edge = np.vstack((train_edge, train_edge[:, [1, 0, 2]]))

    return drug_set,cellline_set,train_edge,label_pos,train_mask,test_mask,atom_shape


import asyncio
import time
from threading import Event
import signal

from libs.utils import get_logger
from libs.senzing_libs.senzing_init import SenzingInit

class Worker(SenzingInit):

    def __init__(self, ex_ev):
        self.ex_ev = ex_ev
        self.log = get_logger()
        super(Worker, self).__init__(self.log)
        self.threads = 5

    async def work(self):

        while not self.ex_ev.is_set():
            cnt = self.g2_engine.countRedoRecords()
            if not cnt:
                await asyncio.sleep(10)
                continue

            response_bytearray = bytearray()
            try:
                self.g2_engine.processRedoRecord(response_bytearray)
            except Exception as e:
                self.log.error(e)
                active_config_id_bytearray = bytearray()
                default_config_id_bytearray = bytearray()
                self.g2_engine.getActiveConfigID(active_config_id_bytearray)
                self.g2_config_mgr.getDefaultConfigID(
                    default_config_id_bytearray)
                try:
                    if active_config_id_bytearray != \
                            default_config_id_bytearray:
                        self.g2_engine.reinit(default_config_id_bytearray)
                        self.log.info('G2Engine reinitialised')
                except Exception as err:
                    self.log.error(err)

        if self.ex_ev.is_set():
            self.log.info('Stopping daemons.')

    def run(self):
        loop = asyncio.get_event_loop()
        tasks = [loop.create_task(self.work()) for _ in
                 range(self.threads)]
        group = asyncio.gather(*tasks)
        loop.run_until_complete(group)

if __name__ == '__main__':
    time.sleep(10)
    exit_event = Event()
    app = Worker(exit_event)

    def signal_handler(sig, frame):
        app.ex_ev.set()

    signal.signal(signal.SIGTERM, signal_handler)
    signal.signal(signal.SIGINT, signal_handler)
    app.run()


class Solution:
    def minOperations(self, nums: List[int]) -> int:
        # unnecessarily optimised code

        ops = 0
        for i in range(1, len(nums)):
            if nums[i] <= nums[i-1]:
                s = nums[i-1] + 1
                ops += s - nums[i]
                nums[i] = s

        return ops


# crawling용 모듈
from urllib.request import urlopen
from bs4 import BeautifulSoup
import urllib
import requests

def musicChart():
	RANK = 10 ## 멜론 차트 순위가 1 ~ 100위까지 있음

	header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}
	req = requests.get('https://www.melon.com/chart/week/index.htm', headers = header) ## 주간 차트를 크롤링 할 것임
	html = req.text
	parse = BeautifulSoup(html, 'html.parser')

	titles = parse.find_all("div", {"class": "ellipsis rank01"})
	singers = parse.find_all("div", {"class": "ellipsis rank02"})
	albums = parse.find_all("div",{"class": "ellipsis rank03"})

	title = []
	singer = []
	album = []
	answer = '음악 주간 차트 TOP 10 입니다.\n\n'

	for t in titles:
		title.append(t.find('a').text)

	for s in singers:
		singer.append(s.find('span', {"class": "checkEllipsis"}).text)

	for a in albums:
		album.append(a.find('a').text)

	for i in range(RANK):
		answer += '%d위:  %s - %s\n'%(i+1, title[i], singer[i])

	res = {
		"version": "2.0",
		"template": {
			"outputs": [
				{
					"simpleText": {
						"text": answer
				  }
				}
			]
		}
	  }

	return res


from SpiffWorkflow.Task      import Task
from SpiffWorkflow.Exception import WorkflowException
from TaskSpec                import TaskSpec

class Trigger(TaskSpec):
    """
    This class implements a task that triggers an event on another
    task.
    If more than one input is connected, the task performs an implicit
    multi merge.
    If more than one output is connected, the task performs an implicit
    parallel split.
    """

    def __init__(self, parent, name, context, **kwargs):
        """
        Constructor.

        parent -- a reference to the parent (TaskSpec)
        name -- a name for the task (string)
        context -- a list of the names of tasks that are to be triggered
        """
        assert parent  is not None
        assert name    is not None
        assert context is not None
        assert type(context) == type([])
        TaskSpec.__init__(self, parent, name, **kwargs)
        self.context = context
        self.times   = kwargs.get('times', 1)
        self.queued  = 0

    def _on_trigger(self, my_task):
        """
        Enqueue a trigger, such that this tasks triggers multiple times later
        when _on_complete() is called.
        """
        self.queued += 1
        # All tasks that have already completed need to be put into
        # READY again.
        for node in my_task.job.task_tree:
            if node.thread_id != my_task.thread_id:
                continue
            if node.spec == self and node._has_state(Task.COMPLETED):
                node.state = Task.FUTURE
                node._ready()

    def _on_complete_hook(self, my_task):
        """
        Runs the task. Should not be called directly.
        Returns True if completed, False otherwise.

        my_task -- the task in which this method is executed
        """
        for i in range(self.times + self.queued):
            for task_name in self.context:
                task = my_task.job.get_task_from_name(task_name)
                task._on_trigger(my_task)
        self.queued = 0
        return TaskSpec._on_complete_hook(self, my_task)


import streamlit as st;
import controllers.ClienteController as ccon
import pandas as pd
import models.recebedor as recebedor
import models.doador as doador
import pages.pcadastroDoador as pageCadastroDoador

st.header("Alterar Doador")

with st.container():
    ###################################
    ############  Doador ##############
    ###################################

    doadorRecuperado = ccon.consultarDoadoresbyCpf(st.text_input(label="CPF do doador"))

    with st.form(key="cadastrar_doador", clear_on_submit=True):
            if doadorRecuperado is None:
                st.warning("Digite um CPF válido")

            else:
                input_nome= st.text_input(label="Nome do doador", value = doadorRecuperado.nome)
                input_cpf = st.text_input(label = "CPF", value = doadorRecuperado.cpf)
                input_idade = st.text_input(label = "Idade", value = doadorRecuperado.idade)
                input_data_nascimento = st.date_input(label = "Data de nascimento",min_value = None, format="DD/MM/YYYY")
                input_sexo = st.selectbox(label="Sexo", options=["Feminino", "Masculino"])
                input_peso = st.number_input(label="Peso", format="%f")
                input_tipo_sanguineo = st.selectbox(label="Tipo Sanguineo", options=["A+", "A-", "B+", "B-", "AB+",
                                                                                "AB-", "O+", "O-"])
                input_profissional = st.text_input(label="Profissional responsável", value=doadorRecuperado.exames_biomedico_crbm)
                input_exames = st.selectbox(label = "Resultado da triagem", options=["Reprovado", "Aprovado"])
            input_button = st.form_submit_button("Enviar")

    if input_button:
            doador.nome = input_nome
            doador.cpf = input_cpf
            doador.idade = input_idade
            doador.data_nascimento = input_data_nascimento

            if input_sexo == "Masculino":
                input_sexo = "M"
            else: input_sexo = "F"
            doador.sexo = input_sexo
            doador.peso = input_peso
            doador.tipo_sanguineo = input_tipo_sanguineo
            doador.exames_biomedico_crbm = input_profissional

            if input_exames == "Reprovado":
                input_exames = 0
            else:input_exames = 1
            doador.exames_resultado = input_exames

            if doadorRecuperado is None:
                 print("came from here")
                 ccon.incluirD(doador)
                 st.success("Doador cadastrado com sucesso!")
            else:
                    print("came from here")
                    ccon.alterarD(doador)
                    st.success("Cadastro de doador atualizado com sucesso!")

st.header("Alterar Recebedor")
with st.container():
    ###################################
    ############ Recebedor ############
    ###################################

    recebedorRecuperado = ccon.consultarRecebedorbyCpf(st.text_input(label="CPF do recebedor"))

    with st.form(key="cadastrar_recebedor", clear_on_submit=True):
            if recebedorRecuperado is None:
                st.warning("Digite um CPF válido")
            else:
                input_nome= st.text_input(label="Nome do doador", value = recebedorRecuperado.nome)
                input_cpf = st.text_input(label = "CPF", value = recebedorRecuperado.cpf)
                input_idade = st.text_input(label = "Idade", value = recebedorRecuperado.idade)
                input_data_nascimento = st.date_input(label = "Data de nascimento",min_value = None, format="DD/MM/YYYY")
                input_sexo = st.selectbox(label="Sexo", options=["Feminino", "Masculino"])
                input_peso = st.number_input(label="Peso", format="%f")
                input_tipo_sanguineo = st.selectbox(label="Tipo Sanguineo", options=["A+", "A-", "B+", "B-", "AB+",
                                                                                "AB-", "O+", "O-"])
                input_profissional = st.text_input(label="Profissional responsável", value=recebedorRecuperado.enfermagem_coren)
                input_telefone = st.text_input(label = "Telefone", value = recebedorRecuperado.telefone)
                input_matricula = st.text_input(label = "Matrícula", value = recebedorRecuperado.adm_hospital_matricula)
            input_button = st.form_submit_button("Enviar")

    if input_button:
            recebedor.nome = input_nome
            recebedor.cpf = input_cpf
            recebedor.idade = input_idade
            recebedor.data_nascimento = input_data_nascimento

            if input_sexo == "Masculino":
                input_sexo = "M"
            else: input_sexo = "F"
            recebedor.sexo = input_sexo
            recebedor.peso = input_peso
            recebedor.tipo_sanguineo = input_tipo_sanguineo
            recebedor.enfermagem_coren = input_profissional
            recebedor.adm_hospital_matricula = input_matricula
            recebedor.telefone = input_telefone

            if recebedorRecuperado is None:
                 ccon.incluirR(recebedor)
                 st.success("Doador cadastrado com sucesso!")
            else:
                    ccon.alterarR(recebedor)
                    st.success("Cadastro de doador atualizado com sucesso!")


from flask import Blueprint, request, jsonify, render_template
from server.inputProcessing.inputProcessing import createInputFile
from server.python2java.python2java import python2java
from server.outputProcessing.outputProcessing import readOutputFile
from server.modelsDB.FeedbackDB import Feedback, addFeedback, getFeedback
# from server.TransCoder.translate import FB_translate
from env import ROOT_DIR
from translate import FB_translate

# class Code():
#     def __init__(self, text="", language="python"):
#         self.text = text
#         self.language = language

routes = Blueprint('routes', __name__, static_folder='build/', template_folder="build/", static_url_path='/')

@routes.route('/', methods=['GET'])
def home():
    return render_template('index.html')

@routes.errorhandler(404)
def not_found(e): # to prevent the server from returning 404 no found error
    return render_template('index.html')

""" Translate route
Request data provides: {'tTo': 'python', 'tFrom': 'java', 'inputCode': 'print("Hello world!")'}
"""
@routes.route('/translate', methods=['POST'])
def translate():
    try:
        data = request.get_json()
        if data is None:
            return jsonify(**{'message': 'No data found'})
        print(data)

        # LOOK INTO USING YIELD TO YIELD A LOADING OPTION
        if data:
            # Read arguments to function
            tTo = data['tTo']
            tFrom = data['tFrom']
            inputCode = data['inputCode']

            # Input file creation
            print("inputCode: ", inputCode)
            print("...Creating input file ...")
            inputFile = createInputFile(inputCode, tFrom) # create input file
            print("Input file created!")
            print(inputFile)

            # Output code creation
            print("...Creating output file...")
            if tFrom == tTo:
                outputFile = inputFile
            elif tFrom == 'python' and tTo == 'java':
                outputFile = python2java(inputFile)
            elif tFrom == 'java' and tTo == 'python':
                outputFile = FB_translate("model_1.pth", "java", "python") # FB_translate(model_path, src_lang, tgt_lang)
            print("Output file created!")

            outputCode = readOutputFile(outputFile) # convert outputfile to output code
            print("outputCode: ", outputCode)

            return jsonify(**{'message': "Success", 'outputCode': outputCode })
        return jsonify(**{'message': "This code translation is not currently supported."})
    except:
        print(request)
        return jsonify(**{'message': 'Translation was unsupported. :('})

""" Feedback route
Request data provides: { 'feedback': feedback }
"""
@routes.route('/feedback', methods=['POST'])
def feedback():
    try:
        data = request.get_json()
        if data is None:
            return jsonify(**{'message': 'No data found'})
        print(data)

        if data:
            feedback = data['feedback']
            newFeedback = Feedback(feedback)
            addFeedback(newFeedback)
            print("Added feedback in routes.py")

            feedback_from_db= getFeedback()
            all_feedback = []
            print("Received feedback in routes.py")
            for fb in feedback_from_db:
                all_feedback.append({"id": fb.id, "feedback": fb.data})
            print(all_feedback)

            return jsonify(**{'message': "Success", 'feedbackId': newFeedback.id})
        return jsonify(**{'message': "Somewhere went wrong when sending feedback!"})
    except:
        print(request)
        return jsonify(**{'message': 'Feedback was unsupported. :('})


import numpy as np
import pandas as pd
from sklearn import tree
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
import random
import math
from torch.utils.tensorboard import SummaryWriter
from matplotlib import pyplot
from pathlib import Path
import requests
import pickle
import gzip
import torch
import math
import torch.nn.functional as F
from torch import nn
from torch import optim
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
import asyncio
import websockets
from enums import ClientStatus
import json
from utils import *
from network import *
pd.options.display.float_format = "{:,.4f}".format

PORT = 8000

class Client:
    def __init__(self, websocket, status, num):
        self.websocket = websocket
        self.status = status
        self.client_number = num

class Server:
    def __init__(self):
        self.clients = {}
        self.model_updates = []

        self.train_amount = 4500
        self.valid_amount = 900
        self.test_amount = 900
        self.number_of_samples = 10

        self.centralized_model = Net2nn()
        self.centralized_optimizer = torch.optim.SGD(self.centralized_model.parameters(), lr=0.01, momentum=0.9)
        self.centralized_criterion = nn.CrossEntropyLoss()

        self.data_is_loaded = False

    async def handler(self, websocket, path):
        while True:
            try:
                data = await websocket.recv()
                if data == 'connect':
                    await self.handle_connection(websocket)
                    if len(self.clients) == 3:
                        await self.distribute_model()
                if data == 'model_update':
                    await self.save_client_model(websocket)
                    if len(self.model_updates) == len(self.clients):
                        await self.update_centralized_model()
                        self.validate_model()

            except Exception as e:
                print(e)
                self.handle_error(websocket)
                return

    async def handle_connection(self, websocket):
        self.clients[str(websocket.id)] = Client(websocket, ClientStatus.CONNECTED, len(self.clients))
        await websocket.send(str(websocket.id))
        print('Client ' + str(websocket.id) + ' connected')
        await websocket.send(str(len(self.clients)))

    async def distribute_model(self):
        print('Distributing model')
        torch.save(self.centralized_model.state_dict(), 'model.pt')
        for client in self.clients:
            await self.clients[client].websocket.send('model')
            await self.clients[client].websocket.send(open('model.pt', 'rb').read())

    async def save_client_model(self, websocket):
        print(f'Saving model from {websocket.id}')
        self.model_updates.append(websocket.id)
        model = await websocket.recv()
        with open(f'./client_models/{websocket.id}.pt', 'wb') as f:
            f.write(model)

    async def update_centralized_model(self):
        print('Updating centralized model')

        l1_weights = []
        l1_biases = []

        l2_weights = []
        l2_biases = []

        l3_weights = []
        l3_biases = []

        with torch.no_grad():
            for index, client_id in enumerate(self.model_updates):
                model = torch.load(f'./client_models/{client_id}.pt')
                current_model = Net2nn()
                current_model.load_state_dict(model)
                if index == 0:
                    l1_weights = current_model.fc1.weight.data.clone()
                    l1_biases = current_model.fc1.bias.data.clone()

                    l2_weights = current_model.fc2.weight.data.clone()
                    l2_biases = current_model.fc2.bias.data.clone()

                    l3_weights = current_model.fc3.weight.data.clone()
                    l3_biases = current_model.fc3.bias.data.clone()
                else:
                    l1_weights += current_model.fc1.weight.data.clone()
                    l1_biases += current_model.fc1.bias.data.clone()

                    l2_weights += current_model.fc2.weight.data.clone()
                    l2_biases += current_model.fc2.bias.data.clone()

                    l3_weights += current_model.fc3.weight.data.clone()
                    l3_biases += current_model.fc3.bias.data.clone()

            self.centralized_model.fc1.weight.data = l1_weights / len(self.model_updates)
            self.centralized_model.fc1.bias.data = l1_biases / len(self.model_updates)

            self.centralized_model.fc2.weight.data = l2_weights / len(self.model_updates)
            self.centralized_model.fc2.bias.data = l2_biases / len(self.model_updates)

            self.centralized_model.fc3.weight.data = l3_weights / len(self.model_updates)
            self.centralized_model.fc3.bias.data = l3_biases / len(self.model_updates)

            torch.save(self.centralized_model.state_dict(), 'model.pt')
        print('Model was updated')

    def validate_model(self):
        if not self.data_is_loaded:
            self.load_data()
        print('Validating centralized model')

        test_loss, test_accuracy = validation(self.centralized_model, self.test_dl, self.centralized_criterion)
        print(f'Test loss: {test_loss} | Test accuracy: {test_accuracy}')

    def load_data(self):
        print('Loading data')
        with gzip.open('./data/mnist/mnist.pkl.gz', "rb") as f:
            ((self.train_imgs, self.train_labels), (self.valid_imgs, self.valid_labels), (self.test_imgs, self.test_labels)) = pickle.load(f, encoding="latin-1")

        self.test_imgs, self.test_labels = map(torch.tensor, (self.test_imgs, self.test_labels))

        test_ds = TensorDataset(self.test_imgs, self.test_labels)
        self.test_dl = DataLoader(test_ds, batch_size=32 * 2)

        self.data_is_loaded = True

    def handle_error(self, websocket):
        print('Client ' + str(websocket.id) + ' disconnected')
        self.clients.pop(str(websocket.id))

server = Server()

start_server = websockets.serve(server.handler, "", PORT, ping_interval=None)

asyncio.get_event_loop().run_until_complete(start_server)
print('Server started on port ' + str(PORT))
asyncio.get_event_loop().run_forever()


import cffi
import os

ffi = cffi.FFI()

cur_dir = os.path.dirname(os.path.abspath(__file__))
src_dir = os.path.join(cur_dir, "appimageupdate")

with open(os.path.join(src_dir, "_wrapper.h")) as f:
    ffi.cdef(f.read())

extra_compile_args = []

extra_args = {
    "include_dirs": [],
    "library_dirs": [],
}

def use_prefix(prefix):
    include_dir = os.path.join(prefix, "include")
    library_dir = os.path.join(prefix, "lib")
    extra_args["library_dirs"].append(library_dir)
    extra_args["include_dirs"].append(include_dir)

if "CONDA_PREFIX" in os.environ:
    print("CONDA_PREFIX found in environment, using as prefix for build")
    use_prefix(os.environ["CONDA_PREFIX"])

elif "VIRTUAL_ENV" in os.environ:
    print("VIRTUAL_ENV found in environment, using as prefix for build")
    use_prefix(os.environ["VIRTUAL_ENV"])

if "INCLUDE_DIRS" in os.environ:
    extra_args["include_dirs"] += [os.path.abspath(i) for i in os.environ["INCLUDE_DIRS"].split(":")]

if "LINK_DIRS" in os.environ:
    extra_args["library_dirs"] += [os.path.abspath(i) for i in os.environ["LINK_DIRS"].split(":")]

with open(os.path.join(src_dir, "_wrapper.cpp")) as f:
    ffi.set_source(
        "_appimageupdate",
        f.read(),
        source_extension='.cpp',
        libraries=["appimageupdate"],
        extra_compile_args=["-std=c++11"],
        **extra_args
    )

if __name__ == "__main__":
    ffi.compile(verbose=True, debug=True)


jogos: int = 0
vitoriaInter: int = 0
vitoriaGremio: int = 0
empates: int = 0
opcao: int = 1
while opcao == 1:
    golI, golG = input().split(" ")
    golI: int = int(golI)
    golG: int = int(golG)
    if golI > golG:
        vitoriaInter += 1
        jogos += 1
    elif golG > golI:
        vitoriaGremio += 1
        jogos += 1
    elif golI == golG:
        empates += 1
        jogos += 1
    print("Novo grenal (1-sim 2-nao)")
    opcao = int(input())
    while(opcao != 1) and (opcao != 2):
        print("Novo grenal (1-sim 2-nao)")
        opcao = int(input())
    if opcao == 2:
        break
print(f"{jogos} grenais")
print(f"Inter:{vitoriaInter}")
print(f"Gremio:{vitoriaGremio}")
print(f"Empates:{empates}")
if vitoriaInter > vitoriaGremio:
    print("Inter venceu mais")
elif vitoriaGremio > vitoriaInter:
    print("Gremio venceu mais")


import os
os.system("cls")

montoTotal = float( input("Monto total de la compra : S/. ") )

if montoTotal > 5000 : banco = 0.3 * montoTotal
else : banco = 0.2 * montoTotal

diferencia = montoTotal - banco
prestamo = banco * 0.1 + banco

print(f'Cantidad que la empresa tiene que pagar : S/. {diferencia}')
print(f'Prestamo del banco                      : S/. {prestamo}')


# 序列化
"""
    我们把变量从内存中变成可存储或传输的过程称之为序列化，在Python中叫pickling，
    在其他语言中也被称之为serialization，marshalling，flattening等等，都是一个意思。

    序列化之后，就可以把序列化后的内容写入磁盘，或者通过网络传输到别的机器上。
    反过来，把变量内容从序列化的对象重新读到内存里称之为反序列化，即unpickling。
    Python提供了pickle模块来实现序列化。
"""

# 首先，我们尝试把一个对象序列化并写入文件：
import pickle
d = dict(name='Bob', age=20, score=80)

# pickle.dumps()方法把任意对象序列化成一个bytes，
# 然后，就可以把这个bytes写入文件。或者用另一个方法pickle.dump()直接把对象序列化后写入一个file-like Object
# 看看写入的dump.txt文件，一堆乱七八糟的内容，这些都是Python保存的对象内部信息。
f = open('dump.txt', 'wb')
pickle.dump(d, f)
f.close()

# 当我们要把对象从磁盘读到内存时，可以先把内容读到一个bytes，
# 然后用pickle.loads()方法反序列化出对象，也可以直接用pickle.load()方法从一个file-like Object中直接反序列化出对象。
# 我们打开另一个Python命令行来反序列化刚才保存的对象：
f = open('dump.txt', 'rb')
e = pickle.load(f)
f.close()
print('d:', d, '反序列化后的结果:', e)

# JSON
"""
如果我们要在不同的编程语言之间传递对象，就必须把对象序列化为标准格式，比如XML，但更好的方法是序列化为JSON，
因为JSON表示出来就是一个字符串，可以被所有语言读取，也可以方便地存储到磁盘或者通过网络传输。
JSON不仅是标准格式，并且比XML更快，而且可以直接在Web页面中读取，非常方便。

JSON表示的对象就是标准的JavaScript语言的对象，JSON和Python内置的数据类型对应如下：

            JSON类型	        Python类型
              {}	          dict
              []	          list
            "string"	      str
            1234.56	        int或float
            true/false	    True/False
              null	          None

由于JSON标准规定JSON编码是UTF-8，所以我们总是能正确地在Python的str与JSON的字符串之间转换。
"""

# Python内置的json模块提供了非常完善的Python对象到JSON格式的转换。我们先看看如何把Python对象变成一个JSON：
import json
d = dict(name='shao', age=20, score=99)
result = json.dumps(d)
print('将对象转换成 json：', type(result), result)

# dumps()方法返回一个str，内容就是标准的JSON。类似的，dump()方法可以直接把JSON写入一个file-like Object。
# 要把JSON反序列化为Python对象，用loads()或者对应的load()方法，
# 前者把JSON的字符串反序列化，后者从file-like Object中读取字符串并反序列化：

json_str = '{"name": "shao", "age": 20, "score": 99}'
result = json.loads(json_str)
print('读取json并转换成string ：', type(result), result)

# JSON进阶
"""
    Python的dict对象可以直接序列化为JSON的{}，不过，很多时候，我们更喜欢用class表示对象.
    错误的原因是Student对象不是一个可序列化为JSON的对象。
    如果连class的实例对象都无法序列化为JSON，这肯定不合理！
    别急，我们仔细看看dumps()方法的参数列表，可以发现，除了第一个必须的obj参数外，dumps()方法还提供了一大堆的可选参数：
    https://docs.python.org/3/library/json.html#json.dumps
    这些可选参数就是让我们来定制JSON序列化。前面的代码之所以无法把Student类实例序列化为JSON，是因为默认情况下，dumps()方法不知道如何将Student实例变为一个JSON的{}对象。
    可选参数default就是把任意一个对象变成一个可序列为JSON的对象，我们只需要为Student专门写一个转换函数，再把函数传进去即可：
"""
import json

class Student(object):
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score

def student2dict(std):
    return {
        "name": std.name,
        "age": std.age,
        "score": std.score
    }

s = Student('shao', 20, 11)
# 这样，Student实例首先被student2dict()函数转换成dict，然后再被顺利序列化为JSON
print('class 序列化为 JSON:', json.dumps(s, default=student2dict))

result = json.dumps(s, default=lambda obj: obj.__dict__)
# 不过，下次如果遇到一个Teacher类的实例，照样无法序列化为JSON。我们可以偷个懒，把任意class的实例变为dict
print('把任意 class 的实例变为 dict：', result)

# 因为通常class的实例都有一个__dict__属性，它就是一个dict，用来存储实例变量。也有少数例外，比如定义了__slots__的class。
#
# 同样的道理，如果我们要把JSON反序列化为一个Student对象实例，loads()方法首先转换出一个dict对象，然后，我们传入的object_hook函数负责把dict转换为Student实例：

def dict2student(d):
    return Student(d['name'], d['age'], d['score'])

std = json.loads(result, object_hook=dict2student)
print('将 JSON 反序列化为Student:', std, std.name, std.age, std.score)

obj = dict(name='小明', age=20, des='12')
s = json.dumps(obj, ensure_ascii=True)
print(s)