# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import ntpath
import posixpath
import re
from buildbot.process import factory
from buildbot.process.properties import WithProperties
from buildbot.steps.source import SVN
from buildbot.steps.source import Git
from buildbot.steps.shell import Compile
from buildbot.steps.shell import Configure
from buildbot.steps.shell import ShellCommand
from buildbot.steps.shell import SetProperty
from buildbot.steps.shell import Test
from buildbot.steps.transfer import DirectoryUpload
from buildbot.steps.transfer import FileUpload
from buildbot.steps.transfer import FileDownload
from buildbot.status.builder import SUCCESS, WARNINGS, FAILURE

LATEST_WIN_BUILD = 'public_html/builds/drmemory-windows-latest-sfx.exe'

dr_giturl = 'https://github.com/DynamoRIO/dynamorio.git'
drm_giturl = 'https://github.com/DynamoRIO/drmemory.git'
bot_tools_giturl = 'https://github.com/DynamoRIO/buildbot.git'

# To reduce github traffic and to allow simpler multi-repo polling,
# we use mirrors of the github repos:
MIRROR_BASE = 'https://chromium.googlesource.com/external/github.com';
dr_mirror_url = MIRROR_BASE + '/DynamoRIO/dynamorio.git';
drm_mirror_url = MIRROR_BASE + '/DynamoRIO/drmemory.git';

# TODO(rnk): Don't make assumptions about absolute path layout.  This won't work
# on bare metal bots.  We can't use a relative path because we often configure
# builds at different directory depths.
WIN_BUILD_ENV_PATH = r'E:\b\build\scripts\slave\drmemory\build_env.bat'

# These tests are ordered roughly from shortest to longest so failure is
# reported earlier.
LINUX_CHROME_TESTS = [
  'content_shell',
  'base_unittests',
  'browser_tests',
  'crypto_unittests',
  'ipc_tests',
  'media_unittests',
  'net_unittests',
  'printing_unittests',
  'remoting_unittests',
  'sql_unittests',
  'unit_tests',
  'url_unittests',
]

def BotToPlatform(bot_platform):
  """Takes a bot platform value and returns a platform string."""
  if bot_platform.startswith('win'):
    return 'windows'
  elif bot_platform.startswith('linux'):
    return 'linux'
  elif bot_platform.startswith('mac'):
    return 'mac'
  else:
    raise ValueError('Unknown platform %s' % bot_platform)

def OsFullName(platform):
  if platform.startswith('win'):
    return 'Windows'
  elif platform.startswith('linux'):
    return 'Linux'
  elif platform.startswith('mac'):
    return 'MacOS'
  else:
    raise ValueError('Unknown platform %s' % platform)

def OsShortName(platform):
  if platform.startswith('win'):
    return 'win'
  elif platform.startswith('linux'):
    return 'linux'
  elif platform.startswith('mac'):
    return 'mac'
  else:
    raise ValueError('Unknown platform %s' % platform)

def ArchToBits(arch):
  """Takes an arch string like x64 and ia32 and returns its bitwidth."""
  if not arch:  # Default to x64.
    return 64
  elif arch == 'x64':
    return 64
  elif arch == 'ia32':
    return 32
  assert False, 'Unsupported architecture'

def ShortHash(rc, stdout, stderr):
  """Stores the shortened hash from stdout into the property pkg_buildnum."""
  # Now that we're using git and our patchlevel is not unique (we're using
  # just the date) we add the short hash as the build #.
  short = "0x" + stdout[:7]
  return {'pkg_buildnum': short}

def RemoveExtension(rc, stdout, stderr):
  """Stores the basename of the path in stdout to the property package_base."""
  base = re.sub(r'\.[^.]+$', r'', stdout)
  return {'package_base': base}

class DrCommands(object):

  """Encapsulates state for adding commands to BuildFactories.

  Encapsulates the two things that we really need to pass everywhere: the
  BuildFactory and the target platform.
  """

  def __init__(self, target_platform=None, os_version=None, build_factory=None):
    if not build_factory:
      build_factory = factory.BuildFactory()
    self.factory = build_factory
    self.target_platform = OsShortName(target_platform)
    self.os_version = os_version

  def IsWindows(self):
    """Returns true if we're targetting Windows."""
    return self.target_platform.startswith('win')

  def IsMac(self):
    """Returns true if we're targetting Mac OSX."""
    return self.target_platform.startswith('mac')

  def PathJoin(self, *args):
    """Join paths using the separator of the os of the bot."""
    if self.IsWindows():
      return ntpath.normpath(ntpath.join(*args))
    else:
      return posixpath.normpath(posixpath.join(*args))

  def AddStep(self, step_class, **kwargs):
    """Adds a regular buildbot step."""
    self.factory.addStep(step_class(**kwargs))

  def AddToolStep(self, step_class, **kwargs):
    """Adds a buildbot step that uses one of our custom tools.

    Currently this includes cmake, ctest, and 7zip.
    """
    self.factory.addStep(ToolStep(step_class, self.target_platform, **kwargs))

  def AddPkgBuildNumber(self):
    """Returns the build number to use for packages."""
    # TODO(bruening): is there a more direct way to process one property
    # into another?
    self.AddStep(SetProperty,
                 name='get package build number',
                 description='get package build number',
                 command=WithProperties('echo %(got_revision)s'),
                 extract_fn=ShortHash)

  def AddDynamoRIOSource(self):
    """Checks out or updates DR's sources."""
    self.AddStep(Git,
                 repourl=dr_giturl,
                 workdir='dynamorio',
                 mode='update',
                 name='Checkout DynamoRIO')

  def AddDrMemorySource(self):
    """Checks out or updates drmemory's sources."""
    self.AddStep(Git,
                 repourl=drm_giturl,
                 submodules=True,
                 workdir='drmemory',
                 mode='update',
                 name='Checkout Dr. Memory')

  def AddTools(self):
    """Add steps to update and unpack drmemory's custom tools."""
    if self.target_platform.startswith('win'):
      # Using a GIT step may break the console view because bb thinks we're at
      # a different revision.  Therefore we run the checkout command manually.
      self.AddStep(ShellCommand,
                   command=['rd', '/q', '/s', 'buildbot'],
                   description='clear tools directory',
                   workdir='tools',
                   name='clear tools directory')
      self.AddStep(ShellCommand,
                   command=['git', 'clone', bot_tools_giturl],
                   workdir='tools',
                   description='update tools',
                   name='update tools')
      self.AddStep(ShellCommand,
                   command=['unpack.bat'],
                   workdir='tools/buildbot/bot_tools',
                   name='unpack tools',
                   description='unpack tools')

  def AddFindFileIntoPropertyStep(self, pattern, property_name):
    """Finds a file on the slave and stores the name in property_name.

    TODO(rnk): This won't work if pattern matches more than one file.
    """
    if self.IsWindows():
      ls_cmd = 'dir /B'  # /B means "bare", like ls with no -l.
    else:
      ls_cmd = 'ls'
    self.AddStep(SetProperty,
                 name='find package file',
                 description='find package file',
                 # Use a string command here to let the shell expand *.
                 command=WithProperties(ls_cmd + ' ' + pattern),
                 property=property_name)

  def AddFindFileBaseIntoPropertyStep(self, pattern):
    """Finds a file on the slave and stores the name minus extension
    in the property .
    TODO(rnk): This won't work if pattern matches more than one file.
    """
    if self.IsWindows():
      ls_cmd = 'dir /B'  # /B means "bare", like ls with no -l.
    else:
      ls_cmd = 'ls'
    self.AddStep(SetProperty,
                 name='find package basename',
                 description='find package basename',
                 # Use a string command here to let the shell expand *.
                 command=WithProperties(ls_cmd + ' ' + pattern),
                 extract_fn=RemoveExtension)

  def AddDRSuite(self, step_name, suite_args):
    """Run DR's test suite with arguments."""
    timeout = 20 * 60  # 20min w/o output.  10 is too short for Windows.
    runsuite_cmd = '../dynamorio/suite/runsuite.cmake'
    if suite_args:
      runsuite_cmd += ',' + suite_args
    cmd = ['ctest', '--timeout', '120', '-VV', '-S', runsuite_cmd]
    self.AddToolStep(CTest,
                     command=cmd,
                     name=step_name,
                     descriptionDone=step_name,
                     timeout=timeout)

  def AddDRBuild(self, target, target_arch):
    """Build a single configuration of DR."""
    cflags = '-m%s' % ArchToBits(target_arch)
    cmake_env = {'CFLAGS': cflags, 'CXXFLAGS': cflags}
    debug = 'OFF'
    if target == 'Debug':
      debug = 'ON'
    self.AddToolStep(Configure,
                     command=['cmake', '..', '-DDEBUG=' + debug],
                     workdir='dynamorio/build',
                     name='Configure %s %s DynamoRIO' % (target, target_arch),
                     env=cmake_env)
    assert not self.IsWindows(), "windows AddDRBuild NYI"
    self.AddToolStep(Compile,
                     command=['make', '-j5'],
                     workdir='dynamorio/build',
                     name='Compile %s %s DynamoRIO' % (target, target_arch))

  def AddTSanTestBuild(self):
    self.AddStep(ShellCommand,
                 command=['svn', 'checkout', '--force',
                          'http://data-race-test.googlecode.com/svn/trunk/',
                          '../tsan'],
                 name='Checkout TSan tests',
                 description='checkout tsan tests')
    self.AddToolStep(ShellCommand,
                     command=['make', '-C', '../tsan/unittest'],
                     # suppress cygwin 'MSDOS' warnings
                     env={'CYGWIN': 'nodosfilewarning'},
                     name='Build TSan tests',
                     descriptionDone='build tsan tests',
                     description='build tsan tests')

  def DynamoRIOSuite(self):
    """Build and test all configurations in the DR pre-commit suite."""
    self.AddDynamoRIOSource()
    self.AddTools()
    self.AddDRSuite('pre-commit suite', '')
    # The Linux bot has all the dependencies for docs generation, so we have it
    # upload the docs to the master.
    if self.target_platform == 'linux':
      self.AddStep(DirectoryUpload,
                   slavesrc='install/docs/html',
                   masterdest='public_html/dr_docs')
    return self.factory

  def DynamoRIONightly(self):
    """Build and test all configurations in the DR nightly suite."""
    self.AddDynamoRIOSource()
    self.AddTools()
    site_name = 'X64.%s%s.VS2010.BuildBot' % (OsFullName(self.target_platform),
                                              self.os_version.capitalize())
    suite_args = 'nightly;long;site=%s' % site_name
    self.AddDRSuite('nightly suite', suite_args)
    return self.factory

  def DynamoRIOPackage(self):
    """Build, package, and upload all configurations of DR."""
    self.AddDynamoRIOSource()
    self.AddTools()
    self.AddPkgBuildNumber()
    package_cmd = ('../dynamorio/make/package.cmake,build=' +
                   '%(pkg_buildnum)s')
    cmd = ['ctest', '-VV', '-S', WithProperties(package_cmd)]
    self.AddToolStep(ShellCommand,
                     command=cmd,
                     description='Package DynamoRIO',
                     name='Package DynamoRIO')
    # For DR, we use plain cpack archives since we don't have existing scripts
    # that expect sfx exes.
    if self.IsWindows():
      src_file = 'DynamoRIO-Windows-*%(pkg_buildnum)s.zip'
    else:
      src_file = ('DynamoRIO-' + OsFullName(self.target_platform) +
                  '-*%(pkg_buildnum)s.tar.gz')
    self.AddFindFileIntoPropertyStep(src_file, 'package_name')
    self.AddStep(FileUpload,
                 slavesrc=WithProperties('%(package_name)s'),
                 masterdest=WithProperties('public_html/builds/' +
                                           '%(package_name)s'),
                 name='Upload DR package')
    return self.factory

  def DrMemorySuite(self):
    """Build and test all configurations in the drmemory pre-commit suite."""
    self.AddDrMemorySource()
    # There is no git equivalent of svnversion, so we do not set dr_revision.
    self.AddTools()
    # We always run the long suite on the bots, rather than splitting into
    # short and "nightly", as it's not all that long.
    cmd = ['ctest', '--timeout', '60', '-VV', '-S',
           WithProperties('../drmemory/tests/runsuite.cmake,' +
                          'drmemory_only;long;build=%(buildnumber)s')]
    self.AddToolStep(CTest,
                     command=cmd,
                     name='Dr. Memory ctest',
                     descriptionDone='runsuite',
                     # failure doesn't mark the whole run as failure
                     flunkOnFailure=False,
                     warnOnFailure=True,
                     timeout=600)
    return self.factory

  def DrMemoryPackage(self):
    """Build the drmemory package."""
    self.AddDrMemorySource()
    self.AddTools()
    self.AddPkgBuildNumber()
    package_cmd = ('../drmemory/package.cmake,build=' +
                   '%(pkg_buildnum)s;drmem_only')
    cmd = ['ctest', '-VV', '-S', WithProperties(package_cmd)]
    self.AddToolStep(ShellCommand,
                     command=cmd,
                     description='Package Dr. Memory',
                     name='Package Dr. Memory')
    if self.IsWindows():
      # For Windows, our chromium scripts expect to find an sfx, so we figure
      # out where cpack installed everything before archiving it, and re-archive
      # it with 7z -sfx.
      zip_file = 'DrMemory-Windows-*%(pkg_buildnum)s.zip'
      self.AddFindFileBaseIntoPropertyStep(zip_file)
      install_dir = ('build_drmemory-debug-32\\' +
                     '_CPack_Packages\\Windows\\ZIP\\' +
                     '%(package_base)s')
      sfx_file = '%(package_base)s-sfx.exe'
      src_file = sfx_file
      if self.IsWindows():
        del_cmd = ['del']
      else:
        del_cmd = ['rm', '-f']
      self.AddStep(ShellCommand,
                   # To support force builds we want to clear out the sfx
                   # (7z will add to existing and won't zero it out).
                   command=del_cmd + [WithProperties(sfx_file)],
                   haltOnFailure=False,
                   flunkOnFailure=False,
                   warnOnFailure=True,
                   name='delete prior sfx archive',
                   description='delete prior sfx archive')
      self.AddToolStep(ShellCommand,
                       # ShellCommand doesn't support WithProperties on workdir,
                       # so instead we pass the full dir to 7z in a way that 7z
                       # will create relative paths in the archive (via ..):
                       command=['7z', 'a', '-sfx', WithProperties(sfx_file),
                                WithProperties('..\\build\\' + install_dir +
                                               '\\*')],
                       haltOnFailure=True,
                       name='create sfx archive',
                       description='create sfx archive')
      self.AddStep(FileUpload,
                   slavesrc=WithProperties(src_file),
                   masterdest=LATEST_WIN_BUILD,
                   name='upload latest build')
      self.AddStep(FileUpload,
                   slavesrc=WithProperties(src_file),
                   masterdest=WithProperties('public_html/builds/' + sfx_file),
                   name='upload revision build')
    else:
      src_file = ('DrMemory-' + OsFullName(self.target_platform) +
                  '-*%(pkg_buildnum)s.tar.gz')
      self.AddFindFileIntoPropertyStep(src_file, 'package_name')
      self.AddStep(FileUpload,
                   slavesrc=WithProperties('%(package_name)s'),
                   masterdest=WithProperties('public_html/builds/' +
                                             '%(package_name)s'),
                   name='upload revision build')
    return self.factory

  def AddTSanTestConfig(self, build_mode, run_mode, use_syms=True, **kwargs):
    """Add a test of a single drmemory config on the tsan tests."""
    app_cmd = [('..\\tsan\\unittest\\bin\\'
                'racecheck_unittest-windows-x86-O0.exe'),
               ('--gtest_filter=-PositiveTests.FreeVsRead'
                ':NegativeTests.WaitForMultiple*'),
               '-147']
    # Pick exe from build mode.
    if self.IsWindows():
      cmd = ['build_drmemory-%s-32\\bin\\drmemory' % build_mode]
    else:
      cmd = ['build_drmemory-%s-32/bin/drmemory.pl' % build_mode]
    # Default flags turn off message boxes, notepad, and print to stderr.
    cmd += ['-dr_ops', '-msgbox_mask 0 -stderr_mask 15',
            '-results_to_stderr', '-batch']
    if self.IsWindows():
      # FIXME: The point of these app tests is to verify that we get no false
      # positives on well-behaved applications, so we should remove these
      # extra suppressions.  We're not using them on dev machines but we'll
      # leave them on the bots and for tsan tests for now.
      cmd += ['-suppress',
              '..\\drmemory\\tests\\app_suite\\default-suppressions.txt']
    # Full mode flags are default, light mode turns off uninits and leaks.
    if run_mode == 'light':
      cmd += ['-light']
    cmd.append('--')
    cmd += app_cmd
    # Set _NT_SYMBOL_PATH appropriately.
    syms_part = ''
    env = {}
    if not use_syms:
      syms_part = 'nosyms '
      if self.IsWindows():
        env['_NT_SYMBOL_PATH'] = ''
    step_name = '%s %s %sTSan tests' % (build_mode, run_mode, syms_part)
    self.AddToolStep(DrMemoryTest,
                     command=cmd,
                     env=env,
                     name=step_name,
                     descriptionDone=step_name,
                     description='run ' + step_name,
                     **kwargs)

  def AddDrMemoryTSanTests(self):
    """Run the tsan tests in a variety of configs."""
    assert self.IsWindows()
    # Run tsan tests in (dbg, rel) cross (full, light).
    for build_mode in ('dbg', 'rel'):
      for run_mode in ('full', 'light'):
        self.AddTSanTestConfig(build_mode, run_mode, use_syms=True)
    # Do one more run of TSan + app_suite without any pdb symbols to make
    # sure our default suppressions match.
    self.AddTSanTestConfig('dbg', 'full', use_syms=False)

  def AddDrMemoryLogsUpload(self):
    """Upload drmemory's logs after running the suite and the app tests."""
    # TODO(rnk): What would make this *really* useful for reproducing bot issues
    # is if we had a way of forcing a build with higher logging.
    if self.IsWindows():
      del_cmd = ['del']
    else:
      del_cmd = ['rm', '-f']
    self.AddStep(ShellCommand,
                 command=del_cmd + ['testlogs.7z'],
                 haltOnFailure=False,
                 flunkOnFailure=False,
                 warnOnFailure=True,
                 name='Prepare to pack test results',
                 description='cleanup')
    testlog_dirs = ['build_drmemory-dbg-32/logs',
                    'build_drmemory-dbg-32/Testing/Temporary',
                    'build_drmemory-rel-32/logs',
                    'build_drmemory-rel-32/Testing/Temporary']
    if not self.IsMac():
      # We do not yet support 64-bit Mac builds.
      testlog_dirs += ['build_drmemory-dbg-64/logs',
                       'build_drmemory-dbg-64/Testing/Temporary',
                       'build_drmemory-rel-64/logs',
                       'build_drmemory-rel-64/Testing/Temporary']
    if self.IsWindows():
      testlog_dirs += ['xmlresults']
    else:
      testlog_dirs += ['xml:results']
    self.AddToolStep(ShellCommand,
                     # We exclude WinTypes.pdb and msvc*.pdb to save space.
                     command=(['7z', 'a', '-xr!*.pdb', 'testlogs.7z'] +
                              testlog_dirs),
                     haltOnFailure=True,
                     name='Pack test results',
                     description='pack results')
    self.AddStep(FileUpload,
                 slavesrc='testlogs.7z',
                 # We're ok with a git hash as revision here:
                 masterdest=WithProperties(
                     'public_html/testlogs/' +
                     'from_%(buildername)s/testlogs_r%(got_revision)s_b' +
                     '%(buildnumber)s.7z'),
                 name='Upload test logs to the master')

class CTest(Test):

  """BuildStep that parses DR's runsuite output."""

  def __init__(self, **kwargs):
    self.__result = None
    Test.__init__(self, **kwargs)

  def createSummary(self, log):
    build_errors = False
    build_count = 0
    passed_count = 0
    failure_count = 0
    flaky_count = 0

    summary_lines = []
    found_summary = False
    # Don't use 'readlines' because we want both stdout and stderr.
    for line in log.getText().split('\n'):
      if line.strip() == 'RESULTS':
        assert not found_summary, 'Found two summaries!'
        found_summary = True
      if found_summary:
        summary_lines.append(line)
        # We try to recognize every line of the summary, because
        # if we fail to match the failure count line we might stay
        # green when we should be red.
        if line.strip() == 'RESULTS':
          continue  # Start of summary line
        if not line.strip():
          continue  # Blank line
        if re.match('^\t', line):
          continue  # Test failure lines start with tabs
        if 'configure errors' in line:
          build_errors = True
        if 'build errors' in line:
          build_errors = True
        if 'build successful' in line:
          build_count += 1
        if 'build successful; no tests for this build' in line:
          continue  # Successful build with no tests
        if 'cross-compile configuration unavailable' in line:
          continue  # Build skipped
        if 'Error in read script' in line:
          continue  # DR i#636: Spurious line from ctest

        # All tests passed for this config.
        match = re.search(r'all (?P<passed>\d+) tests passed', line)
        if match:
          passed_count += int(match.group('passed'))
          continue

        # Some tests failed in this config.
        match = re.match(r'^[^:]*: (?P<passed>\d+) tests passed, '
                         r'\*\*\*\* (?P<failed>\d+) tests failed'
                         r'(, of which (?P<flaky>\d+) were flaky)?:',
                         line)
        if match:
          passed_count += int(match.group('passed'))
          failure_count += int(match.group('failed'))
          num_flaky_str = match.group('flaky')
          if num_flaky_str:
            flaky_count += int(num_flaky_str)
        else:
          # Add a fake failure so we get notified.  Put the warning
          # before the line we don't recognize.
          failure_count += 1
          summary_lines[-1:-1] = ['WARNING: next line unrecognized\n']

    if not found_summary:
      # Add a fake failure so we get notified.
      failure_count += 1
      summary_lines.append('WARNING: Failed to find summary in stdio.\n')

    self.setTestResults(passed=passed_count,
                        failed=failure_count - flaky_count,
                        warnings=flaky_count)

    # Check build_count>0 to guard against total failure up front
    if build_errors or build_count == 0:
      self.__result = FAILURE
    elif failure_count > 0:
      if failure_count > flaky_count:
        self.__result = FAILURE
      else:
        self.__result = WARNINGS
      summary_name = 'summary: %d failed' % failure_count
      if flaky_count > 0:
        summary_name += ', %d flaky failed' % flaky_count
      self.addCompleteLog(summary_name, ''.join(summary_lines))
    else:
      self.__result = SUCCESS

    # We're ok with a git hash as the revision here:
    got_revision = self.getProperty('got_revision')
    buildnumber = self.getProperty('buildnumber')
    buildername = self.getProperty('buildername')
    if 'drm' in buildername:
      self.addURL('test logs',
                  'http://build.chromium.org/p/client.drmemory/testlogs/' +
                  'from_%s/testlogs_r%s_b%s.7z' % \
                  (buildername, got_revision, buildnumber))

  def evaluateCommand(self, cmd):
    if self.__result is not None:
      return self.__result
    return Test.evaluateCommand(self, cmd)

class DrMemoryTest(Test):
  def __init__(self, **kwargs):
    self.failed__ = False  # there's a 'failed' method in Test, ouch!
    Test.__init__(self, **kwargs)

  def createSummary(self, log):
    failed_tests = []
    summary = []
    report_count = 0
    assert_failure = None

    # Don't use 'readlines' because we want both stdout and stderr.
    for line in log.getText().split('\n'):
      m = re.match(r'\[  FAILED  \] (.*\..*) \([0-9]+ ms\)', line.strip())
      if m:
        failed_tests.append(m.groups()[0])  # Append failed test name.

      DRM_PREFIX = r'~~[Dr\.M0-9]+~~ '
      # Only count non-ignored errors.
      m = re.match(DRM_PREFIX + 'ERRORS IGNORED:', line)
      if m:
        break
      m = re.match(DRM_PREFIX + '(.*)', line)
      if m:
        summary.append(m.groups()[0])

      m = re.match(DRM_PREFIX + '*([0-9]+) unique,.*total,* (.*)', line)
      if m:
        (error_count, _) = m.groups()
        error_count = int(error_count)
        report_count += error_count

      m = re.match(DRM_PREFIX + r'ASSERT FAILURE \(.*\): (.*)', line)
      if m:
        assert_failure = 'ASSERT FAILURE: ' + m.groups()[0]

    if assert_failure:
      self.failed__ = True
      self.addCompleteLog('ASSERT FAILURE!!!', assert_failure)

    if failed_tests:
      self.failed__ = True
      self.setTestResults(failed=len(failed_tests))
      self.addCompleteLog('%d tests failed' % len(failed_tests),
                            '\n'.join(failed_tests))
    if report_count > 0:
      self.failed__ = True
      self.setTestResults(warnings=report_count)

    self.addCompleteLog('summary: %d report(s)' % report_count,
                        ''.join(summary))

  def evaluateCommand(self, cmd):
    if self.failed__:
      return FAILURE
    return Test.evaluateCommand(self, cmd)

def ToolStep(step_class, os, **kwargs):
  """Modify build step arguments to run the command with our custom tools."""
  if os.startswith('win'):
    command = kwargs.get('command')
    env = kwargs.get('env')
    if isinstance(command, list):
      command = [WIN_BUILD_ENV_PATH] + command
    else:
      command = WIN_BUILD_ENV_PATH + ' ' + command
    if env:
      env = dict(env)  # Copy
    else:
      env = {}
    env['BOTTOOLS'] = WithProperties('%(workdir)s\\tools\\buildbot\\bot_tools')
    kwargs['command'] = command
    kwargs['env'] = env
  return step_class(**kwargs)

def DynamoRIOSuiteFactory(os='', os_version=''):
  return DrCommands(os, os_version).DynamoRIOSuite()

def DynamoRIONightlyFactory(os='', os_version=''):
  return DrCommands(os, os_version).DynamoRIONightly()

def DynamoRIOPackageFactory(os='', os_version=''):
  return DrCommands(os, os_version).DynamoRIOPackage()

def CreateDrMFactory(bot_platform):
  # Build and run the drmemory pre-commit suite.
  cmds = DrCommands(BotToPlatform(bot_platform))
  cmds.DrMemorySuite()
  if cmds.IsWindows():
    cmds.AddTSanTestBuild()
    cmds.AddDrMemoryTSanTests()
    # We used to kill stale drmemory processes, but now we use auto-reboot.
  cmds.AddDrMemoryLogsUpload()
  return cmds.factory

def CreateDrMPackageFactory(bot_platform):
  return DrCommands(BotToPlatform(bot_platform)).DrMemoryPackage()

def CreateWinChromeFactory(builder):
  """Run chrome tests with the latest drmemory.

  Do *not* build TOT chrome or sync it.  Building chrome takes a lot of
  resources and the tests are flaky, so we only do at known good revisions.  We
  don't want to fall too far behind, or we're not really testing Chrome's full
  test suite.
  """
  ret = factory.BuildFactory()
  sfx_name = 'drm-sfx'  # TODO: add .exe when BB supports that, d'oh!
  ret.addStep(
      FileDownload(mastersrc=LATEST_WIN_BUILD,
                   slavedest=(sfx_name + '.exe'),
                   name='Download the latest build'))
  ret.addStep(
      ShellCommand(command=[sfx_name, '-ounpacked', '-y'],
                   haltOnFailure=True,
                   name='Unpack the build',
                   description='unpack the build'))

  # Find out the revision number using -version
  def get_revision(rc, stdout, stderr):
    m = re.search(r'version \d+\.\d+\.(\d+)', stdout)
    if m:
      return {'got_revision': int(m.groups()[0])}
    return {'failed_to_parse': stdout}
  ret.addStep(
      SetProperty(
          command=['unpacked\\bin\\drmemory', '-version'],
          extract_fn=get_revision,
          name='Get the revision number',
          description='get revision',
          descriptionDone='get revision'))

  # VP8 tests
  # TODO(rnk): Add back the VP8 test step.  We might be able to make this part
  # of the buildbot steps if it doesn't update often and builds incrementally.
  if False:
    ret.addStep(
        ToolStep(
            DrMemoryTest,
            'windows',
            command=[
                'bash',
                'E:\\vpx\\vp8-test-vectors\\run_tests.sh',
                ('--exec=unpacked/bin/drmemory.exe -batch '
                 '-no_check_leaks -no_count_leaks '
                 '-no_check_uninitialized '
                 'e:/vpx/b/Win32/Debug/vpxdec.exe'),
                'E:\\vpx\\vp8-test-vectors',
                ],
            env={'PATH': 'C:\\cygwin\\bin;%PATH%'},
            name='VP8 tests',
            descriptionDone='VP8 tests',
            description='run vp8 tests'))

  # Chromium tests
  for test in ['url', 'printing', 'media', 'sql', 'crypto_unittests',
               'remoting', 'ipc_tests', 'base_unittests', 'net', 'unit']:
    ret.addStep(
        Test(command=[
                 # Use the build dir of the chrome builder on this slave.
                 ('..\\..\\' + builder + '\\build\\' +
                  'src\\tools\\valgrind\\chrome_tests.bat'),
                 '-t', test, '--tool', 'drmemory_light', '--keep_logs',
             ],
             env={'DRMEMORY_COMMAND': 'unpacked/bin/drmemory.exe'},
             name=('Chromium \'%s\' tests' % test),
             descriptionDone=('\'%s\' tests' % test),
             description=('run \'%s\' tests' % test)))

  return ret

def CreateLinuxChromeFactory():
  """Run chrome tests with the latest dynamorio.

  TODO(rnk): Run drmemory, not dynamorio.

  We use a build of chrome produced weekly from a known good revision on the
  same slave.
  """
  cr_src = '../../linux-cr-builder/build/src'
  ret = factory.BuildFactory()
  ret.addStep(
      Git(
          repourl=dr_giturl,
          workdir='dynamorio',
          mode='update',
          name='Checkout DynamoRIO'))

  # If we need to execute 32-bit children, we'll need a full exports package.
  ret.addStep(Configure(command=['cmake', '..', '-DDEBUG=OFF'],
                        workdir='dynamorio/build',
                        name='Configure release DynamoRIO'))
  ret.addStep(Compile(command=['make', '-j5'],
                      workdir='dynamorio/build',
                      name='Compile release DynamoRIO'))

  # Don't follow python children.  This should speed up net_unittests, which
  # spawns a bunch of simple http servers to talk to.
  ret.addStep(ShellCommand(
      command=['bin64/drconfig', '-reg', 'python', '-norun', '-v'],
      workdir='dynamorio/build',
      name='don\'t follow python',
      description='don\'t follow python',
      descriptionDone='don\'t follow python'))

  # Chromium tests
  for test in LINUX_CHROME_TESTS:
    cmd = [
        'xvfb-run', '-a',
        '../dynamorio/build/bin64/drrun',
        '-stderr_mask', '12',  # Show DR crashes
        '--',
        cr_src + '/out/Release/' + test
    ]
    if test == 'browser_tests':
      cmd += ['--gtest_filter=AutofillTest.BasicFormFill']
    elif test == 'net_unittests':
      cmd += ['--gtest_filter=-CertDatabaseNSSTest.ImportCACertHierarchy*']
    elif test == 'remoting_unittests':
      cmd += ['--gtest_filter='
              '-VideoFrameCapturerTest.Capture:'
              'DesktopProcessTest.DeathTest']
    elif test == 'base_unittests':
      # crbug.com/308273: this test is flaky
      cmd += ['--gtest_filter=-TraceEventTestFixture.TraceContinuousSampling']
    elif test == 'content_shell':
      cmd += ['--run-layout-test',
              'file:///home/chrome-bot/bb.html']
    # We used to md5 the output, but that's too brittle.  Just dump it to stdout
    # so humans can verify it.  The return code will tell us if we crash.
    # TODO(rnk): We should run some selection of layout tests if we want to
    # verify output.
    ret.addStep(Test(command=cmd,
                     env={'CHROME_DEVEL_SANDBOX':
                          '/opt/chromium/chrome_sandbox'},
                     name=test,
                     descriptionDone=test,
                     description=test))

  return ret


# uncompyle6 version 3.6.7
# Python bytecode 2.5 (62131)
# Decompiled from: Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: build/bdist.macosx-10.3-i386/egg/twitteroauth/microne_config.py
# Compiled at: 2011-03-22 05:23:05
__doc__ = '\n    tipfy.ext.auth.microne_config\n    ~~~~~~~~~~~~~~~~~~~~~~\n\n    A module for configuration functions for microne plugin.\n'
import logging, aha
config = aha.Config()
__all__ = [
 'initConfig']

def initConfig(app, consumer_key, consumer_secret, oauth_redirect_path='/oauth'):
    """
    A function to make configuration of twitter authentication for microne.
    Arguments :
    :param app             : App object of microne
    :param consumer_key    : Twitter consumer_key
    :param consumer_secret : Twitter consumer_secret
    """
    from plugin.twitteroauth.twitter_auth import TwitterOAuth
    from plugin.twitteroauth.twitteroauth import TwitteroauthController
    config.auth_obj = TwitterOAuth
    config.consumer_key = consumer_key
    config.consumer_secret = consumer_secret

    @app.route(oauth_redirect_path)
    def oauth():
        """
        A function to receive oauth redirection.
        It passes information in url to TwitteroauthController._post_action()
        """
        controller = TwitteroauthController(app.get_handler())
        auth_obj = TwitterOAuth()
        auth_obj.request = app.request
        auth_obj.request.args = app.request.params
        auth_obj.get_authenticated_user(controller._post_action)


import math
#

def sq():

    width = int(input('Enter the Width'))

    length = int(input('Enter the Length'))

    area = width * length

    print(f'the square footage of a house is {area} ')

# Circumference of circle
def cir():

    radius = float(input('Enter the raduis of your circle'))

    C = 2*math.pi*radius

    print(f'Cirumference of circle is {C}')


from __future__ import print_function

from tixi import tixiwrapper
from tigl import tiglwrapper
import tigl.configuration, tigl.geometry, tigl.boolean_ops, tigl.exports
import os

def export_fused_step(tigl_handle):
    """
    This example uses the opencascade fusing algorithm to fuse wing
    and fuselage and writes the resulting geometry to a step file
    """
    print ("Computing fused geometry...")

    # get the configuration manager
    mgr = tigl.configuration.CCPACSConfigurationManager_get_instance()

    # get the CPACS configuration, defined by the tigl handle
    # we need to access the underlying tigl handle (that is used in the C/C++ API)
    config = mgr.get_configuration(tigl_handle._handle.value)

    fuselageShape = config.get_fuselage(1).get_loft()
    wingShape = config.get_wing(1).get_loft()
    wingMShape = config.get_wing(1).get_mirrored_loft()

    # set another name
    wingMShape.set_name("cool_2nd_wing")

    # fuse the shapes
    shape_list = tigl.boolean_ops.ListPNamedShape()
    shape_list.push_back(wingShape)
    shape_list.push_back(wingMShape)
    shape = tigl.boolean_ops.CFuseShapes(fuselageShape, shape_list).named_shape()

    print ("Done!")

    # write step file
    step_writer = tigl.exports.CTiglExportStep()
    step_writer.add_shape(shape)
    step_writer.write("simpletest.stp")

if __name__ == '__main__':
    tixi_h = tixiwrapper.Tixi()
    tigl_h = tiglwrapper.Tigl()

    dir_path = os.path.dirname(os.path.realpath(__file__))
    tixi_h.open(dir_path + "/../../tests/TestData/simpletest.cpacs.xml")
    tigl_h.open(tixi_h, "")

    export_fused_step(tigl_h)


import matplotlib.pyplot as plt
from die import Die

# Create a D6.
die = Die()

# Make some rolls, and store results in a list.
results = []
for roll_num in range(1000):
    result = die.roll()
    results.append(result)

# Analyze the results.
frequencies = []
for value in range(1, die.num_sides+1):
    frequency = results.count(value)
    frequencies.append(frequency)

# Visualize the results.

die_values = [1, 2, 3, 4, 5, 6,]
frequency_result = list(range(1, 1000))
plt.plot(die_values, frequency_result, linewidth=5)

plt.title("Results of rolling one D6 1000 times.")
plt.xlabel("Side of Die")
plt.ylabel("Frequency")

plt.title = "Result"
hist.y_title = "Frequency of Result"

plt.show()


from typing import Literal

from pydantic import BaseModel

from app import constants as const
from app.actions import Action, Wait, action_from_str
from app.entities.utils import get_loop_time, invert_orientation, is_inverted
from app.types import Orientation
from app.types.hitbox import HitBox

class PatrolVision(BaseModel):
    """Vision for a patrol entity"""

    pos_x: int
    pos_y: int
    char: str = const.PATROL_VISION

    @classmethod
    def create_line(
        cls, start_x: int, start_y: int, length: int, orientation: Literal["v", "h"]
    ):
        """Creates an array of static assets to make a line"""
        if orientation == "v":
            return [cls(pos_x=start_x, pos_y=start_y + i) for i in range(0, length)]
        else:
            return [cls(pos_x=start_x + i, pos_y=start_y) for i in range(0, length)]

    @classmethod
    def create_from_patrol_pos(
        cls, start_x: int, start_y: int, orientation: Orientation
    ) -> list["PatrolVision"]:
        """Create patrolvision from patrol positioning"""
        if orientation == "up":
            create_args = [
                (start_x - 1, start_y - 1, 3, "h"),
                (start_x - 1, start_y - 2, 3, "h"),
                (start_x - 2, start_y - 3, 5, "h"),
                (start_x - 2, start_y - 4, 5, "h"),
                (start_x - 2, start_y - 5, 5, "h"),
            ]
        elif orientation == "down":
            create_args = [
                (start_x - 1, start_y + 1, 3, "h"),
                (start_x - 1, start_y + 2, 3, "h"),
                (start_x - 2, start_y + 3, 5, "h"),
                (start_x - 2, start_y + 4, 5, "h"),
                (start_x - 2, start_y + 5, 5, "h"),
            ]
        elif orientation == "right":
            create_args = [
                (start_x + 1, start_y - 1, 3, "v"),
                (start_x + 2, start_y - 1, 3, "v"),
                (start_x + 3, start_y - 2, 5, "v"),
                (start_x + 4, start_y - 2, 5, "v"),
                (start_x + 5, start_y - 2, 5, "v"),
            ]
        else:
            create_args = [
                (start_x - 1, start_y - 1, 3, "v"),
                (start_x - 2, start_y - 1, 3, "v"),
                (start_x - 3, start_y - 2, 5, "v"),
                (start_x - 4, start_y - 2, 5, "v"),
                (start_x - 5, start_y - 2, 5, "v"),
            ]
        res = []
        for x, y, l, o in create_args:
            res += cls.create_line(x, y, l, o)
        return res

    def get_hitbox_at(self, time: int) -> HitBox:
        """Get hitbox at a given time"""
        return HitBox(
            pos_x=self.pos_x,
            pos_y=self.pos_y,
            content=self.char,
            time=time,
            parent=self.__class__,
        )

class Patrol(BaseModel):
    """A patrol entity that moves between points"""

    start_x: int
    start_y: int
    orientation: Orientation
    actions: list[Action] = []
    char: str = const.PATROL

    def __str__(self) -> str:
        return self.char

    @classmethod
    def create(
        cls, start_x: int, start_y: int, orientation: Orientation, actions: list[str]
    ) -> "Patrol":
        """Create a patrol entity"""
        patrol = cls(start_x=start_x, start_y=start_y, orientation=orientation)
        for a in actions:
            action, _ = action_from_str(a, patrol)
            if action:
                patrol.actions.append(action)
        return patrol

    @property
    def loop_interval(self) -> int:
        """Calculate the loop interval length"""
        return sum([a.length for a in self.actions])

    @property
    def time_consumed(self) -> int:
        """Amount of time the initial loop consumes"""
        return self.loop_interval

    def get_move_at(self, time: int) -> Action:
        """Get action at a given time"""
        return [x for x in self.actions if x.time_start <= time < x.time_end][0]

    def get_current_position(self, time: int) -> tuple[Orientation, tuple[int, int]]:
        """Get current patrol position and orientation"""
        loop_time = get_loop_time(self.loop_interval, time)
        if loop_time == 0:
            return self.orientation, (self.start_x, self.start_y)
        move = self.get_move_at(loop_time)
        box = move.get_hitbox_at(loop_time)
        if isinstance(move, Wait):
            return self.get_current_position(time - 1)
        if is_inverted(self.loop_interval, time):
            return invert_orientation(move.orientation), (box.pos_x, box.pos_y)
        else:
            return move.orientation, (box.pos_x, box.pos_y)

    def get_hitbox_at(self, time: int) -> HitBox:
        """Get hitbox at a given time"""
        loop_time = get_loop_time(self.loop_interval, time)
        if loop_time == 0:
            return HitBox(
                pos_x=self.start_x,
                pos_y=self.start_y,
                parent=self.__class__,
                content=self.char,
                time=time,
            )
        return self.get_move_at(loop_time).get_hitbox_at(loop_time)

    @property
    def last_pos(self) -> tuple[int, int]:
        """Get the last known position for player"""
        if not self.actions:
            return (self.start_x, self.start_y)
        else:
            box = self.get_move_at(self.time_consumed).get_hitbox_at(self.time_consumed)
            return box.pos_x, box.pos_y

    def get_current_vision(self, time: int) -> list[PatrolVision]:
        """Generate vision hitboxes"""
        orientation, pos = self.get_current_position(time)
        return PatrolVision.create_from_patrol_pos(
            start_x=pos[0], start_y=pos[1], orientation=orientation
        )


# -*- coding:utf-8 -*-
# Author: Jay-Q
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication
import login

app = QApplication(sys.argv)
Login = login.login()
if Login.exec():
    print("登录成功")
else:
    print("登录退出")
sys.exit(app.exec())


"A context manager to handle exception with option to open a debugger."
from contextlib import contextmanager

from .colorify import colorify
from .colorify import LINE_LENGTH, RED, YELLOW

# Get config values
from ..config import DEBUG

@contextmanager
def exception_handler(tag=None, pdb=False):
    """Context manager to handle exception with option to open a debugger.

    Parameter
    ---------
    tag: str
        Name to display before outputing error in red.
    pdb: bool
        If set to True, open a debugger if an error is raised.
    """
    try:
        yield
    except KeyboardInterrupt:
        status = colorify("interrupted", YELLOW)
        print(f"\r{tag} {status}".ljust(LINE_LENGTH))
        raise SystemExit(1)
    except BaseException:
        status = colorify("error", RED)
        print(f"{tag} {status}".ljust(LINE_LENGTH))

        if pdb:
            # Use ipdb if it is available and default to pdb otherwise.
            try:
                from ipdb import post_mortem
            except ImportError:
                from pdb import post_mortem
            post_mortem()

        if DEBUG:
            raise
        else:
            import traceback
            traceback.print_exc()


# variaveis pré-definidas
usuarios = 0
#elevadores
elevA = 0
elevB = 0
elevC = 0
# Periodos e periodos do elevador
peri_matu = 0
elevA_periM = 0
elevB_periM = 0
elevC_periM = 0
#---------------
peri_vesp = 0
elevA_periV = 0
elevB_periV = 0
elevC_periV = 0
#----------------
peri_nort = 0
elevA_periN = 0
elevB_periN = 0
elevC_periN = 0
# Ainda teremos as variaveis: elevador, periodo, perc_elevA, perc_elevB, perc_elevC
print("Para parar o sistema: digite PARAR")
parar = "Continua"
while parar != "PARAR": #Não sei a condição de parada, por isso usei essa condição
    usuarios = usuarios + 1
    # elevador
    elevador = input("Qual elevador você mais utiliza entre A, B ou C? \n R: ")
    # ELEVADOR A
    if elevador == "A":
        elevA = elevA + 1
        periodo = input("Digite o perido que mais utiliza esse elevador, sendo: \n M para matutino;\n V para vespertino;\n N para noturno.\n R: ")
        #perido de utilização desse elevador
        if periodo == "M":
            peri_matu = peri_matu + 1
            elevA_periM = elevA_periM + 1
        elif periodo == "V":
            peri_vesp = peri_vesp + 1
            elevA_periV = elevA_periV + 1
        elif periodo == "N":
            peri_nort = peri_nort + 1
            elevA_periN = elevA_periN + 1
    # ELEVADOR B
    elif elevador == "B":
        elevB = elevB + 1
        periodo = input("Digite o perido que mais utiliza esse elevador, sendo: \n M para matutino;\n V para vespertino;\n N para noturno.\n R: ")
        #perido de utilização desse elevador
        if periodo == "M":
            peri_matu = peri_matu + 1
            elevB_periM = elevB_periM + 1
        elif periodo == "V":
            peri_vesp = peri_vesp + 1
            elevB_periV = elevB_periV + 1
        elif periodo == "N":
            peri_nort = peri_nort + 1
            elevB_periN = elevB_periN + 1
    # ELEVADOR C
    elif elevador == "C":
        elevC = elevC + 1
        periodo = input("Digite o perido que mais utiliza esse elevador, sendo:\n M para matutino;\n V para vespertino;\n N para noturno.\n R: ")
        #perido de utilização desse elevador
        if periodo == "M":
            peri_matu = peri_matu + 1
            elevC_periM = elevC_periM + 1
        elif periodo == "V":
            peri_vesp = peri_vesp + 1
            elevC_periV = elevC_periV + 1
        elif periodo == "N":
            peri_nort = peri_nort + 1
            elevC_periN = elevC_periN + 1

    parar = input("deseja parar: ")

""" Primeiro Calculo """
#Matutino
if peri_matu >= peri_vesp and peri_matu >= peri_nort:
    print("O periodo de maior fluxo é o Matutino")
    """ Segundo Calculo caso seja Matutino"""
    #elevadores desse período
    if elevA_periM >= elevB_periM and elevA_periM >= elevC_periM:
        print("O elevador mais usado nesse periodo é o A")
    elif elevB_periM >= elevA_periM and elevB_periM >= elevC_periM:
        print("O elevador mais usado nesse periodo é o B")
    elif elevC_periM >= elevA_periM and elevC_periM >= elevB_periM:
        print("O elevador mais usado nesse periodo é o B")
#Vespertino
elif peri_vesp >= peri_matu and peri_vesp >= peri_nort:
    print("O perio de maior fluxo é o Vespertino")
    """ Segundo Calculo caso seja Vespertino"""
    #elevadores desse período
    if elevA_periV >= elevB_periV and elevA_periV >= elevC_periV:
        print("O elevador mais usado nesse periodo é o A")
    elif elevB_periV >= elevA_periV and elevB_periV >= elevC_periV:
        print("O elevador mais usado nesse periodo é o B")
    elif elevC_periV >= elevA_periV and elevC_periV >= elevB_periV:
        print("O elevador mais usado nesse periodo é o C")
#Noturno
elif peri_nort >= peri_matu and peri_nort >= peri_vesp:
    print("O periodo de maior fluxo é o Noturno")
    """ Segundo Calculo caso seja Noturno"""
    #elevadores desse período
    if elevA_periN >= elevB_periN and elevA_periN >= elevC_periN:
        print("O elevador mais usado nesse periodo é o A")
    elif elevB_periN >= elevA_periN and elevB_periN >= elevC_periN:
        print("O elevador mais usado nesse periodo é o B")
    elif elevC_periN >= elevA_periN and elevC_periN >= elevB_periN:
        print("O elevador mais usado nesse periodo é o C")

if elevA >= elevB and elevA >= elevC:
    print("O elevador A é o mais utilizado\n")
elif elevB >= elevA and elevB >= elevC:
    print("O elevador B é o meis utilizado\n")
elif elevC >= elevA and elevC >= elevB:
    print("O elevador C é o mais utilizado\n")

""" Terceiro Calculo """
#percentagem
perc_elevA = (elevA / usuarios) * 100
perc_elevB = (elevB / usuarios) * 100
perc_elevC = (elevC / usuarios) * 100

if perc_elevA >= perc_elevB and perc_elevA >= perc_elevC:
    if perc_elevB < perc_elevC:
        print("A diferença de percentual entre o mais utilizado e o menor utilizado é de :", perc_elevA - perc_elevB)
        """ Quarto Calculo"""
        print("o percentual do elevador de média utilização é de: ", perc_elevC)
    else:
        print("A diferença de percentual entre o mais utilizado e o menor utilizado é de :", perc_elevA - perc_elevC)
        """ Quarto Calculo"""
        print("o percentual do elevador de média utilização é de: ", perc_elevB)
elif perc_elevB >= perc_elevA and perc_elevB >= perc_elevC:
    if perc_elevA < perc_elevC:
        print("A diferença de percentual entre o mais utilizado e o menor utilizado é de :", perc_elevB - perc_elevA)
        """ Quarto Calculo"""
        print("o percentual do elevador de média utilização é de: ", perc_elevC)
    else:
        print("A diferença de percentual entre o mais utilizado e o menor utilizado é de :", perc_elevB - perc_elevC)
        """ Quarto Calculo"""
        print("o percentual do elevador de média utilização é de: ", perc_elevA)
else: #para cair nesse caso, é comprovado que o C é maior que todos
    if perc_elevA < perc_elevB:
        print("A diferença de percentual entre o mais utilizado e o menor utilizado é de :", perc_elevC - perc_elevA)
        """ Quarto Calculo"""
        print("o percentual do elevador de média utilização é de: ", perc_elevB)
    else:
        print("A diferença de percentual entre o mais utilizado e o menor utilizado é de :", perc_elevC - perc_elevB)
        """ Quarto Calculo"""
        print("o percentual do elevador de média utilização é de: ", perc_elevA)


import random
import os
import sys

input_file = sys.argv[1]
data = [line.strip() for line in open(input_file)]

# prepare data format for simulation
with open('tempfile','w') as target:
    for line in data:
        count = line.count(',')
        if 'WrapWC' in line:
            continue
        elif count == 16:
            array = line.split(',')
            new = ''
            for i in range(0, len(array)):
                if i != 8:
                    new += str(array[i]) + ","
            target.write( new[:-1] + "\n")
        else:
            target.write( line  + "\n" )

os.remove(input_file)
os.rename('tempfile', input_file)


""" Compiled: 2020-08-14 16:40:05 """

#__src_file__ = "extensions/arc_writers/./etc/FDealTagsHook.py"
class DefaultTagCreator():
    @staticmethod
    def getTagTitles():
        return "Instrument Type,Currency,Counterparty,Book Node"

    def _getTagVals(self, bookNode, trade):
        instType = trade.Instrument().InsType()
        currency = trade.Currency().Name()
        counterpty = trade.Counterparty().Name()
        return "[{0},{1},{2},{3}]".format(instType, currency, counterpty, bookNode)

TAGCLASS = DefaultTagCreator

Extensions/ARC Market Risk Exporter FIS/FPythonCode/FDealTagsHook.py


from nba_api.stats.endpoints import TeamGameLog
import pandas as pd
from nba_api.stats.static import players
from nba_api.stats.endpoints import CommonTeamRoster
import sqlite3
import csv
import time

##opening connection to SQL
conn = sqlite3.connect("NbaStats.db")
curs = conn.cursor()
##getting all team ids
curs.execute("SELECT team_id FROM teams;")
all_teamids = curs.fetchall()

##Loop through each teamid
for id in all_teamids:
    ##Call the get_all_players function passing the teamid
    get_all_players(id)
    time.sleep(10)
    reader = csv.reader(open('roster_csv.csv','r'), delimiter=',')
    for row in reader:
        to_db =[row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[11],row[12],row[13],row[14]]
        curs.execute("INSERT INTO players(count, team_ID,SEASON,LeagueID,PLAYER,PLAYER_SLUG,NUM,POSITION,HEIGHT,WEIGHT,BIRTH_DATE,AGE,EXP,SCHOOL,PLAYER_ID) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", to_db)

    time.sleep(10)

def get_team_season_history():

    teamstats = TeamGameLog(
        team_id=' 1610612745',
        season='2019-20',
        ##league_id_nullable =='00',
        ##per_mode_simple='PerGame',
        season_type_all_star='Regular Season'
    )

    teams_dict = teamstats.get_data_frames()

    print(teams_dict)

def get_all_players(teamID):

    teamRoster = CommonTeamRoster(
        team_id= teamID,
        season='2019-20',
    )
    roster = teamRoster.get_data_frames()
    ##print(roster[0])
    roster[0].to_csv('roster_csv.csv',header=False)

##get_all_players(team)

def create_table():
    curs.execute("CREATE TABLE players (count INTEGER, team_ID INTEGER,SEASON INTEGER,LeagueID INTEGER ,PLAYER TEXT,PLAYER_SLUG TEXT,NUM INTEGER,POSITION TEXT,HEIGHT TEXT,WEIGHT TEXT,BIRTH_DATE TEXT,AGE INTEGER,EXP INTEGER,SCHOOL TEXT,PLAYER_ID INTEGER PRIMARY KEY);")
    conn.commit()

conn.commit()
#get_all_players(team)

#reader = csv.reader(open('teamid_csv.csv','r'), delimiter=',')
#for row in reader:
 #   to_db = [row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]]
 #   curs.execute("INSERT INTO teams(count, team_id, full_name, abbreviation, nickname, city, state, year_founded) VALUES (?, ?, ?, ?, ?, ?, ?, ?);", to_db)
#conn.commit()

##upload_data_frame_to_csv(roster)


import math
class Nist:
    def __init__(self, ref, candi, N):
        self.ref = ref
        self.candi = candi
        self.N = N
        self.co_occur = None
        self.candi_set = None
        self.ref_set = None
        self.beta = 0.5
        self.ref_list = []
        self.candi_list = []
        self.ref_list_n = []
        self.candi_list_n = []

    def cal_information(self):
        '''calculate information'''
        info = 0.
        for words in self.co_occur:
            Wn_1 = words[:-1]
            Wn = words
            count_Wn = self.find_word_in_list(Wn, 'Wn')
            if len(words) == 1:
                count_Wn_1 = len(self.ref_list)
            else:
                count_Wn_1 = self.find_word_in_list(Wn_1, 'Wn-1')
            info += math.log2(float(count_Wn_1 / count_Wn))
        return info

    def find_word_in_list(self, key_word, flag):
        count = 0
        for word in self.ref_list_n:
            if flag == 'Wn-1':
                if word[:-1] == key_word:
                    count += 1
            else:
                if word == key_word:
                    count += 1
        return count

    def cal_one_nist(self, info):
        sys_output = len(self.candi_list_n)
        print(self.candi_list_n)
        temp = float(info/sys_output)
        return temp

    def find_cooccur_word(self, i):
        '''find co-occur word'''
        self.ref_list_n = []
        self.candi_list_n = []
        for j in range(len(self.ref_list) - i + 1):
            str = ''
            for item in self.ref_list[j: j + i]:
                str = str + '&' + item
            self.ref_list_n.append(str)
        for j in range(len(self.candi_list) - i + 1):
            str = ''
            for item in self.candi_list[j: j + i]:
                str = str + '&' + item
            self.candi_list_n.append(str)
        ref_set = set(self.ref_list_n)
        candi_set = set(self.candi_list_n)
        self.co_occur = list(set(ref_set) & set(candi_set))
        for i in range(len(self.co_occur)):
            self.co_occur[i] = self.co_occur[i].split('&')[1:]
        for i in range(len(self.candi_list_n)):
            self.candi_list_n[i] = self.candi_list_n[i].split('&')[1:]
        for i in range(len(self.ref_list_n)):
            self.ref_list_n[i] = self.ref_list_n[i].split('&')[1:]

    def token(self):
        self.ref_list = self.ref
        self.candi_list = self.candi

    def calculate(self):
        self.token()
        temp = 0.
        for i in range(1, min(self.N + 1, len(self.candi) + 1, len(self.ref_list) + 1)):
            self.find_cooccur_word(i)
            info = self.cal_information()
            temp += self.cal_one_nist(info)
        nist = temp * math.exp(self.beta * math.pow(math.log2(min(len(self.candi_list)/len(self.ref_list),1)),2))
        print('---------', nist)
        return nist

def main(ref, candi):

    test = Nist(ref, candi, 5)
    nist = test.calculate()
    return nist


def zimbabwe(e, m):
    def ltoi(le): return int(''.join(le))
    def chtoi(ch): return ord(ch) - ord('0')

    le = list(str(e))
    n = len(le)

    def solve(idx, free):
        ans = 0

        if idx == n:
            if ltoi(le) % m == 0: return 1
            else: return 0

        ans = ans + solve(idx+1, free)

        flags = [0] * 10
        flags[chtoi(le[idx])] = 1
        for i in range(idx+1, n):
            if flags[chtoi(le[i])]: continue

            if free or le[i] < le[idx]:
                flags[chtoi(le[i])] = 1

                le[i], le[idx] = le[idx], le[i]
                ans = ans + solve(idx+1, True)
                le[i], le[idx] = le[idx], le[i]

        return ans

    ans = solve(0, False)
    ans = ans - 1 if e%m == 0 else ans
    return ans % 1000000007

def main():
    c = int(input())
    for _ in range(c):
        e, m = map(int, input().split())
        print(zimbabwe(e, m))

if __name__ == '__main__':
    main()


from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Crmessage, Crservice, Crimputation

class UserCreerForm(UserCreationForm):
	email = forms.EmailField()
	class Meta:
		model = User
		fields = ['username', 'email', 'password1', 'password2']

class FormCrmessage(forms.ModelForm):
	class Meta:
		type_msg = (("DIRECTION","Direction"),("AUTRES","Autres"))
		model=Crmessage
		fields = ['designation','objet', 'date_courrier', 'date_reception','type_message']
		widgets= {
		'type_message':forms.Select(choices=type_msg),
		'date_courrier':forms.DateInput(attrs={"class": "input", "type": "date"}, format="%Y-%m-%d"),
		'date_reception':forms.DateInput(attrs={"class": "input", "type": "date"}, format="%Y-%m-%d"),
		# 'crmessage':forms.Select(attrs={'class': 'input'}),
		# 'crservice':forms.Select()
		 }

class FormCrservice(forms.ModelForm):
	class Meta:
		model=Crservice
		fields = ['code_srv','nom_service']

class FormCrimputation(forms.ModelForm):
	class Meta:
		msg = (("TRAITER","Traité"),("NONTRAITER","Non Traité"))
		model=Crimputation
		fields = ['date_imput','date_lmt_trmt', 'observation', 'statut_msg', 'crmessage', 'crservice']
		widgets= {
		'statut_msg':forms.Select(choices=msg),
		'date_imput':forms.DateInput(attrs={"class": "input", "type": "date"}, format="%Y-%m-%d"),
		'date_lmt_trmt':forms.DateInput(attrs={"class": "input", "type": "date"}, format="%Y-%m-%d"),
		# 'crmessage':forms.Select(attrs={'class': 'input'}),
		# 'crservice':forms.Select()
		 }


#!/usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException, SessionNotCreatedException
from selenium.webdriver.chrome.service import Service
import sys
import os
import pathlib
import urllib.request
import re
import zipfile
import stat
from sys import platform

def get_driver():
    # Attempt to open the Selenium chromedriver. If it fails, download the latest chromedriver.
    driver = None
    retry = True
    major_version = None

    # Determine the version of Chrome installed.
    version = get_chrome_version()
    if version:
        parts = version.split('.')
        major_version = parts[0] if len(parts) > 0 else 0

    while retry:
        retry = False
        is_download = False

        try:
            options = webdriver.ChromeOptions()
            options.add_argument('--headless')
            executable_path='./chromedriver'
            service = Service(executable_path=executable_path)
            driver = webdriver.Chrome(options=options, service=service)
        except SessionNotCreatedException as e:
            if 'This version of ChromeDriver' in e.msg:
                is_download = True
                print('Warning: You may need to update the Chrome web browser to the latest version. Run Chrome, click Help->About.')
        except WebDriverException as e:
            if "wrong permissions" in e.msg:
                st = os.stat('./chromedriver')
                os.chmod('./chromedriver', st.st_mode | stat.S_IEXEC)
                retry = True
            elif "chromedriver' executable needs to be in PATH" in e.msg:
                is_download = True

        retry = is_download and download_driver(major_version)

    return driver

def download_driver(version=None):
    # Find the latest chromedriver, download, unzip, set permissions to executable.
    result = False
    url = 'https://chromedriver.chromium.org/downloads'
    base_driver_url = 'https://chromedriver.storage.googleapis.com/'
    file_name = 'chromedriver_' + get_platform_filename()
    driver_file_name = 'chromedriver' + '.exe' if platform == "win32" else ''
    pattern = 'https://.*?path=(' + (version or '\d+') + '\.\d+\.\d+\.\d+)'

    # Download latest chromedriver.
    print('Finding latest chromedriver..')
    opener = urllib.request.FancyURLopener({})
    stream = opener.open(url)
    content = stream.read().decode('utf8')

    # Parse the latest version.
    match = re.search(pattern, content)
    if match and match.groups():
        # Url of download html page.
        url = match.group(0)
        # Version of latest driver.
        version = match.group(1)
        driver_url = base_driver_url + version + '/' + file_name

        # Download the file.
        print('Version ' + version)
        print('Downloading ' + driver_url)
        app_path = os.path.dirname(os.path.realpath(__file__))
        chromedriver_path = app_path + '/' + driver_file_name
        file_path = app_path + '/' + file_name
        urllib.request.urlretrieve(driver_url, file_path)

        # Unzip the file.
        print('Unzipping ' + file_path)
        with zipfile.ZipFile(file_path, 'r') as zip_ref:
            zip_ref.extractall(app_path)

        print('Setting executable permission on ' + chromedriver_path)
        st = os.stat(chromedriver_path)
        os.chmod(chromedriver_path, st.st_mode | stat.S_IEXEC)

        # Cleanup.
        os.remove(file_path)

        result = True

    return result

def get_platform_filename():
    filename = ''

    is_64bits = sys.maxsize > 2**32

    if platform == "linux" or platform == "linux2":
        # linux
        filename += 'linux'
        filename += '64' if is_64bits else '32'
    elif platform == "darwin":
        # OS X
        filename += 'mac64'
    elif platform == "win32":
        # Windows...
        filename += 'win32'

    filename += '.zip'

    return filename

def extract_version(output):
    try:
        google_version = ''
        for letter in output[output.rindex('DisplayVersion    REG_SZ') + 24:]:
            if letter != '\n':
                google_version += letter
            else:
                break
        return(google_version.strip())
    except TypeError:
        return

def get_chrome_version():
    version = None
    install_path = None

    try:
        if platform == "linux" or platform == "linux2":
            # linux
            install_path = "/usr/bin/google-chrome"
        elif platform == "darwin":
            # OS X
            install_path = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
        elif platform == "win32":
            # Windows...
            stream = os.popen('reg query "HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome"')
            output = stream.read()
            version = extract_version(output)
    except Exception as ex:
        print(ex)

    version = os.popen(f"{install_path} --version").read().strip('Google Chrome ').strip() if install_path else version

    return version


from typing import List

class Solution:
    def isBoomerang(self, points: List[List[int]]) -> bool:

        def getMaxCommonFactor(a, b):
            a = a % b
            while a > 0:
                c = b % a
                b = a
                a = c
            return b

        def getCline(x0, y0, x1, y1):
            dx, dy = x1 - x0, y1 - y0
            if dx == 0 and dy == 0:
                return (0, 0)
            if dx == 0:
                return (0, 1)
            if dy == 0:
                return (1, 0)
            cf = getMaxCommonFactor(abs(dx), abs(dy))
            return (dx // cf, dy // cf)

        c1 = getCline(points[0][0], points[0][1], points[1][0], points[1][1])
        c2 = getCline(points[0][0], points[0][1], points[2][0], points[2][1])
        c3 = getCline(points[1][0], points[1][1], points[2][0], points[2][1])
        return c1 != (0, 0) and c2 != (0, 0) and c3 != (0, 0) and c1 != c2

sol = Solution()
# ret = sol.isBoomerang([[1, 1], [2, 2], [3, 3]])
ret = sol.isBoomerang([[1, 0], [0, 0], [2, 0]])
print(ret)


import numpy as np
from copy import deepcopy
from .callback import Callback
from sklearn.metrics import confusion_matrix

class ConfusionMatrix(Callback):
	def __init__(self, numClasses, categoricalLabels=True, printMatrix=False, **kwargs):
		name = "ConfusionMatrix" if not "name" in kwargs else kwargs["name"]
		super().__init__(name=name)
		self.numClasses = numClasses
		self.nClasses = np.arange(self.numClasses)
		self.categoricalLabels = categoricalLabels
		self.printMatrix = printMatrix
		self.epochMatrix = {
			"Train" : np.zeros((numClasses, numClasses), dtype=np.int32),
			"Validation" : np.zeros((numClasses, numClasses), dtype=np.int32),
			"Test" : np.zeros((numClasses, numClasses), dtype=np.int32)
		}

	@staticmethod
	def computeMeanAcc(cfMatrix):
		# Sum the rows (TP + FP)
		TPFN = np.sum(cfMatrix, axis=-1)
		# TP are on diagonal of confusion matrix
		TP = np.diag(cfMatrix)
		return 100 * (TP / TPFN).mean()

	@staticmethod
	def computeMeanF1(cfMatrix):
		TP = np.diag(cfMatrix)
		FN = np.sum(cfMatrix, axis=-1) - TP
		FP = np.sum(cfMatrix, axis=0) - TP
		P = TP / (TP + FP + 1e-5)
		R = TP / (TP + FN + 1e-5)
		F1 = 2 * P * R / (P + R + 1e-5)
		return F1.mean()

	def onEpochStart(self, **kwargs):
		# Reset the confusion matrix for the next epoch
		for Key in self.epochMatrix:
			self.epochMatrix[Key] *= 0

	def onEpochEnd(self, **kwargs):
		if kwargs["isTraining"]:
			print("%s (validation)\n%s" % (self.name, self.epochMatrix["Validation"]))

			# Accuracy
			accTrain = ConfusionMatrix.computeMeanAcc(self.epochMatrix["Train"])
			accValidation = ConfusionMatrix.computeMeanAcc(self.epochMatrix["Validation"])
			print("True accuracy. Train: %2.2f%%. Validation: %2.2f%%" % (accTrain, accValidation))

			# F1
			F1Train = ConfusionMatrix.computeMeanF1(self.epochMatrix["Train"])
			F1Validation = ConfusionMatrix.computeMeanF1(self.epochMatrix["Validation"])
			print("True F1 score. Train: %2.2f. Validation: %2.2f" % (F1Train, F1Validation))
		else:
			F1Test = ConfusionMatrix.computeMeanF1(self.epochMatrix["Test"])
			accTest = ConfusionMatrix.computeMeanAcc(self.epochMatrix["Test"])
			print("%s (test)\n%s" % (self.name, self.epochMatrix["Test"]))
			print("True F1 score: %2.2f" % (F1Test))
			print("True accuracy: %2.2f%%" % (accTest))

		if kwargs["isTraining"]:
			kwargs["trainHistory"][-1]["Train"][self.name] = deepcopy(self.epochMatrix["Train"])
			kwargs["trainHistory"][-1]["Validation"][self.name] = deepcopy(self.epochMatrix["Validation"])

			# Update F1 and Accuracy as well with their better values (even if these metrics might not be used or if
			#  they are updated later.
			kwargs["trainHistory"][-1]["Train"]["Accuracy"] = accTrain
			kwargs["trainHistory"][-1]["Validation"]["Accuracy"] = accValidation
			kwargs["trainHistory"][-1]["Train"]["F1Score"] = F1Train
			kwargs["trainHistory"][-1]["Validation"]["F1Score"] = F1Validation

		return self.epochMatrix

	def onIterationEnd(self, results, labels, **kwargs):
		if kwargs["isTraining"]:
			Key = "Train" if kwargs["isOptimizing"] else "Validation"
		else:
			Key = "Test"
		results = np.argmax(results, axis=1)
		if self.categoricalLabels:
			labels = np.where(labels == 1)[1]
		self.epochMatrix[Key] += confusion_matrix(labels, results, self.nClasses)


from pathlib import Path
import json

def getRootPath(subdir='data'):
    return Path(__file__).parent.absolute() / subdir

KnowledgeBase = {}

def jsondec(x):
    if isinstance(x, str):
        try:
            return json.loads(x)
        except:
            print('[JSON ERROR]', x)
            return {}
    return x

def merge(x, y='{}'):
    z = {}
    z.update(jsondec(x))
    z.update(jsondec(y))
    return z

def load_KnowledgeBase(path):
    with open(path) as f:
        for line in f:
            if line.startswith('#'):
                continue
            pos = line.find('{')
            if pos == -1:
                continue
            keys = line[0:pos].strip()
            defined = line[pos:]
            if '<:' in keys:
                keys, parent = map(lambda x: x.strip(), keys.split('<:'))
                if parent in KnowledgeBase:
                    defined = merge(KnowledgeBase[parent], defined)
                else:
                    print('undefined', parent)
            for key in map(lambda x: x.strip(), keys.split(',')):
                if key in KnowledgeBase:
                    print('redefined', key)
                KnowledgeBase[key] = defined

# path nlp_dict/color_dict.txt

def load_SimpleDict(path, property, suffix=None):
    with open(path) as f:
        for line in f:
            if line.startswith('#'):
                continue
            if ' ' in line or '\t' in line:
                key, value = line.split()
                if suffix is not None and key.endswith(suffix):
                    key = key[:-len(suffix)]
                value = f'{{"{property}": "{value}"}}'
                if key in KnowledgeBase:
                    KnowledgeBase[key] = merge(KnowledgeBase[key], value)
                else:
                    KnowledgeBase[key] = value
                if suffix is not None:
                    key += suffix
                    if key in KnowledgeBase:
                        KnowledgeBase[key] = merge(KnowledgeBase[key], value)
                    else:
                        KnowledgeBase[key] = value

def init_wordvec(path='nlp_dict/entity_vector.model.bin'):
    model_path = getRootPath(path)
    if not model_path.exists():
        print('nobuai is not available')
        return lambda x, pinfo, property=None: None
    from gensim.models import KeyedVectors
    model = KeyedVectors.load_word2vec_format(
        str(model_path), limit=500000, binary=True)
    base = [x for x in KnowledgeBase.keys() if x in model]
    domains = {}

    def domain(property):
        if property is None:
            return base
        if not property in domains:
            domains[property] = [
                x for x in base if property in KnowledgeBase[x]]
        return domains[property]

    def find_from_model(w, pinfo, property=None):
        if w not in model:
            return None
        sim_max = 0.0
        sim_w = None
        for w2 in domain(property):
            sim = model.similarity(w, w2)
            if sim > sim_max:
                sim_max = sim
                sim_w = w2
        if sim_w is not None:
            print('@word2vec', w, sim_w, sim_max)
            pinfo(f'「{w}」は{sim_w}(類似度{sim_max:.4})と解釈されました')
        return sim_w

    return find_from_model

load_KnowledgeBase('nlp_dict/matter_dict.txt')
load_SimpleDict('nlp_dict/color_dict.txt', 'color', '色')
#find_sim = init_wordvec('nlp_dict/entity_vector.model.bin')
find_sim = init_wordvec('nlp_dict/word2vec.model.bin')
# print(KnowledgeBase['檸檬'])

Empty = {}

def suffix(w):
    if w.endswith('の'):
        return w[:-1]
    return w

def find_data(phrase: str, pinfo, property=None):
    prefix = ''
    w = phrase
    while len(w) > 0:
        if w in KnowledgeBase and (property is None or property in KnowledgeBase[w]):
            return merge(KnowledgeBase[w], find_data(suffix(prefix), pinfo, None))
        # wordvec で 近似する
        simw = find_sim(w, pinfo, property)
        if simw is not None:
            #print('@word2vec', simw, KnowledgeBase[simw], suffix(prefix), find_data(suffix(prefix), pinfo, None))
            return merge(KnowledgeBase[simw], find_data(suffix(prefix), pinfo, None))
        # n-gramする
        prefix += w[0]
        w = w[1:]
    return Empty

def find_value(key, property, pinfo, default=None):
    data = find_data(key, pinfo, property)
    if property in data:
        return data[property]
    return default

def conv_phrase(phrase, pinfo, d):
    # ある性質についての表現か調べる. 例. 色は赤
    if 'は' in phrase:  # Ad hoc な実装
        pos = phrase.find('は')
        key = phrase[0:pos]    # 色
        # print('@キーbefore', key)
        key = find_value(key, 'property', pinfo, key)
        # print('@キーafter', key)
        if key is not None:  # もし性質が辞書にあったら、
            # key='color' になっている値の方を変換する
            value = find_value(phrase[pos+1:], key, pinfo)
            if value is not None:
                d[key] = value
                return
    # 特定の性質でない
    found = find_data(phrase, pinfo, None)
    #print('@調べる', phrase, found)
    if len(found) == 0:
        print('@見つかりません', phrase)
    for key in found:
        d[key] = found[key]

def conv(*phrases):
    def pinfo(x): return print(x)
    d = {}
    for phrase in phrases:
        conv_phrase(phrase, pinfo, d)
    return d

def conv2(phrase, pinfo=lambda x: print(x)):
    d = {}
    conv_phrase(phrase, pinfo, d)
    return d

# main スクリプト
if __name__ == "__main__":
    # print(conv('赤いボール', 'よく跳ねる'))
    # print(conv('跳ねないボール', '色は緑'))
    # print(conv('サッカーボール', '少し跳ねる'))
    # print(conv('壁', '少し跳ねる'))
    print(conv2('色は檸檬'))
    #find_word = init_hiyoko()
    # find_word('ひよこ')

__package__ = 'koinu'


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import json
import time
from pprint import pprint
import shutil
from datetime import datetime
import numpy as np
import pandas as pd
from openpyxl import load_workbook
import matplotlib.pyplot as plt
import requests
import bs4

DATA_FOLDER = './data'

HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36'\
                    + ' (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}

CODE_DICT = {
    '煤炭开采业': '332',
    '橡胶塑料制造业': '355',
    '黑色金属冶炼和压延加工业': '357',
    '汽车制造业': '362',
    '土木工程建筑业': '374',
    '石油和天然气开采业': '333',
    '黑色金属矿采选业': '334',
    '有色金属矿采选业': '335',
    '纺织业': '343',
    '皮革、毛皮、羽毛及其制品和制鞋业': '345',
    '造纸和纸制品业': '348',
    '石油加工、炼焦和核燃料加工业': '351',
    '化学原料和化学制品制造业': '352',
    '医药制造业': '353',
    '有色金属冶炼和压延加工业': '358'
}

COMPANY_LIST_URL_PREFIX = 'http://webstock.quote.hermes.hexun.com/a/sortlist?block='
COMPANY_LIST_URL_SUFFIX = '&callback=stocklistrequest.sortlistback&commodityid=0&title=15'\
                            + '&direction=0&start=0&number=10000&input=undefined&time=224500'\
                            + '&column=code,name,price,updownrate,LastClose,open,high,low,volume,'\
                            + 'priceweight,amount,exchangeratio,VibrationRatio,VolumeRatio'

DATES = ('2019', '2018', '2017', '2016', '2015', '2014', '2013', '2012', '2011', '2010')

COLUMNS = ['公司名称', '负债和所有者（或股东权益）合计',
        '经营活动产生的现金流量净额', '投资活动产生的现金流量净额',
        '筹资活动产生的现金流量净', '权益负债比率',
        '总资产收益率']

def build_company_list_url(company_code):
    return COMPANY_LIST_URL_PREFIX + company_code + COMPANY_LIST_URL_SUFFIX

def request(url):
    return requests.get(url, headers=HEADERS)

def get_companies_data(url):
    """
    Returns
    -------
    company_list : list
        The element is tuple type which is (company_code, company_name)
    """
    res = request(url)
    res.encoding = 'utf-8'
    start = res.text.index('(') + 1
    end = res.text.index(')')
    companies_data = json.loads(res.text[start:end])
    return [(company[0], company[1]) for company in companies_data['Data'][0]]

def get_dateurl_dates(company_code, company_name, url_prefix):
    res = request(url_prefix + company_code + '.shtml')
    res.encoding = 'gbk'
    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    script = soup.find_all(id='zaiyaocontent')[0].find_all('script')[0]
    parts = script.text.split(';')
    dateurl_parts = parts[0].split('=')[1:]
    dateurl_parts.pop()
    dateurl = eval('='.join(dateurl_parts) + '"')
    dates = eval(parts[1].split(' = ')[1])
    return dateurl, dates

def get_table(dateurl, date):
    query_url = dateurl + '=' + date
    res = request(query_url)
    res.encoding = 'gbk'
    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    div = soup.find_all(id='zaiyaocontent')[0]
    return div.table.find_all('tr')

def get_zcfz(company_code, company_name):
    dateurl, dates = get_dateurl_dates(company_code, company_name,
                                       'http://stockdata.stock.hexun.com/2009_zcfz_')
    zcfz_dict = {}
    for i in range(len(dates) - 1, -1, -1):
        table = get_table(dateurl, dates[i][0])
        zcfz = table[-2].find_all('td')[1].text
        zcfz_dict[dates[i][0]] = zcfz
    return zcfz_dict

def get_xjll(company_code, company_name, index):
    dateurl, dates = get_dateurl_dates(company_code, company_name,
                                      'http://stockdata.stock.hexun.com/2009_xjll_')

    xjll_dict = {}
    for i in range(len(dates) - 1, -1, -1):
        table = get_table(dateurl, dates[i][0])
        xjll = table[index].find_all('td')[1].text
        xjll_dict[dates[i][0]] = xjll
    return xjll_dict

def get_cwbl(company_code, company_name, index):
    dateurl, dates = get_dateurl_dates(company_code, company_name,
                                      'http://stockdata.stock.hexun.com/2009_cwbl_')
    cwbl_dict = {}
    for i in range(len(dates) - 1, -1, -1):
        table = get_table(dateurl, dates[i][0])
        cwbl = table[index].find_all('td')[1].text
        cwbl_dict[dates[i][0]] = cwbl
    return cwbl_dict

def calc_avg(date, data_dict):
    total = 0.0
    times = 1
    for dt in data_dict:
        if date in dt:
            try:
                value = float(data_dict[dt].replace(',', ''))
            except Exception as e:
                print_with_datetime(data_dict[dt].replace(',', '')\
                                    + " can't be convert to number, program will set it to 0 by default")
                continue
            total += value
            times += 1
    return str(total / times)

def print_with_datetime(msg):
    print("[%s]: %s" % (datetime.now(), msg))

def main():
    num = 0
    shutil.copyfile(os.path.join(DATA_FOLDER, 'template.xlsx'), os.path.join(DATA_FOLDER, 'data.xlsx'))
    for k, key in enumerate(CODE_DICT):
        url = build_company_list_url(CODE_DICT[key])
        company_list = get_companies_data(url)

        for i, company in enumerate(company_list):
            start_time = time.time()
            company_code = company[0]
            company_name = company[1]
            print_with_datetime("start to fetch data of company %s" % company_name)

            try:
                zcfz_dict = get_zcfz(company_code, company_name)
                print_with_datetime("finish fetching 负债和所有者（或股东权益）合计 of %s" % company_name)

                xjll_dict_01 = get_xjll(company_code, company_name, 13)
                print_with_datetime("finish fetching 经营活动产生的现金流量净额 of %s" % company_name)

                xjll_dict_02 = get_xjll(company_code, company_name, 28)
                print_with_datetime("finish fetching 投资活动产生的现金流量净额 of %s" % company_name)

                xjll_dict_03 = get_xjll(company_code, company_name, 40)
                print_with_datetime("finish fetching 筹资活动产生的现金流量净额 of %s" % company_name)

                cwbl_dict_01 = get_cwbl(company_code, company_name, 13)
                print_with_datetime("finish fetching 权益负债比率 of %s" % company_name)

                cwbl_dict_02 = get_cwbl(company_code, company_name, 26)
                print_with_datetime("finish fetching 总资产收益率 of %s" % company_name)
            except Exception as e:
                print_with_datetime("failed to fetch data of %s, program will skip the company" % company_name)
                continue

            for date in DATES:
                zcfz_avg = calc_avg(date, zcfz_dict)
                xjll_avg_01 = calc_avg(date, xjll_dict_01)
                xjll_avg_02 = calc_avg(date, xjll_dict_02)
                xjll_avg_03 = calc_avg(date, xjll_dict_03)
                cwbl_avg_01 = calc_avg(date, cwbl_dict_01)
                cwbl_avg_02 = calc_avg(date, cwbl_dict_02)
                df = pd.DataFrame(columns=COLUMNS)
                df.loc[0] = [
                    company_name, zcfz_avg, xjll_avg_01,
                    xjll_avg_02, xjll_avg_03,
                    cwbl_avg_01, cwbl_avg_02
                ]
                path = os.path.join(DATA_FOLDER, 'data.xlsx')
                with pd.ExcelWriter(path, \
                                    engine='openpyxl', mode='a') as writer:
                    book = load_workbook(path)
                    writer.book = book
                    writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
                    df.to_excel(writer, sheet_name=date, index=False, startrow=num, header=False)
            num += 1
            end_time = time.time()
            print_with_datetime("finish saving data of %s, cost: %.2f, line: %s, index: %s, category: %s"\
                    % (company_name, end_time - start_time, num, i, key))

if __name__ == "__main__":
    main()


#!/usr/bin/env python
# vim:set et ts=4 sw=4 fileencoding=utf-8:

import os
import sys
from os import path
topdir = path.dirname( path.dirname( path.abspath(__file__) ) )
sys.path.append(topdir)
sys.path.append(path.join(topdir, "backend"))
sys.path.append(path.join(topdir, "libs"   ))

from HostLists import SSHConfig, Iptables
from config import *

def UpdateLists():
    SSHConfigFile = os.path.join( os.getenv('HOME'), "ssh-config/cilu-%s/02-ucloud" % project_name.lower() )
    SSHConfig(UseRegionLists, common, private_key, SSHConfigFile)
    Iptables( UseRegionLists, common, private_key, project_name, IptableRules)

    RulesConfigFile = os.path.join( topdir, "files/%s-switch.rule" % project_name.lower() )
    #Switch(UseRegionLists, common, private_key, SwitchRules, RulesConfigFile)

if __name__=='__main__':

    import logging.config

    logging.config.fileConfig("logger.conf")
    logging.getLogger("paramiko").setLevel(logging.WARNING)
    logger = logging.getLogger(__name__)

    UpdateLists()


#MOVIMIENTO ARMONICO SIMPLE
#--------------------------

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate

def f(x,t,m,c):
    return np.array([x[1],(-c/m)*x[0]])

m = 1
c = 0.5

y0 = np.array([0.2,0.1])

t = np.linspace(0,50,200)

sol = integrate.odeint(f, y0, t, args=(m,c))

plt.plot(t, sol[:,0], label='$posición(t)$')
plt.plot(t, sol[:,1], label='$velocidad(t)$')
plt.title('Movimiento armónico simple')
plt.grid()
plt.legend()
plt.show()


from modules import ApproximationFunc

import math
import matplotlib.pyplot as plt

# DFA algorithm building
# chose the step type: logarithmic - iter_log defined by this.optimal_step function
# fits - X; point - Yk; dfa_p - array of F(L)
# closer_p - Linear approximation of F(L)
def do_dfa(array):
    iter_log = optimal_step(len(array)) // 2 + 1
    dfa_p = []

    for i in range(iter_log):
        fits = ApproximationFunc.do_approximation(array, pow(2, i))
        point = sum(do_sq_diff_arrays(array, fits))/len(array)
        dfa_p.append(math.log2(pow(point, 0.5)))

    dfa_p.reverse()
    do_plot(dfa_p)
    closer_p = ApproximationFunc.do_approximation(dfa_p, 1)
    return dfa_p, closer_p

# invent to wheel for arrays quadratic difference (c)
def do_sq_diff_arrays(x, y):
    a = [x]
    b = [y]
    c = [list(map(lambda a, b: (a - b)**2, a[i], b[i])) for i in range(len(a))]
    return c[0]

# binary logarithmic step for dfa algorithm
def optimal_step(n):
    return math.floor(math.log2(n))

# invent to wheel for many graphics in one pic
def do_plot(*args):
    for i in args:
        plt.plot(i)
    plt.show()


from django.conf.urls.defaults import *
from django.views.generic import RedirectView
import views

urlpatterns = patterns('',
    (r'^no_template_view/$', views.no_template_view),
    (r'^staff_only/$', views.staff_only_view),
    (r'^get_view/$', views.get_view),
    (r'^request_data/$', views.request_data),
    (r'^request_data_extended/$', views.request_data, {'template':'extended.html', 'data':'bacon'}),
    url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'),
    (r'^login_protected_redirect_view/$', views.login_protected_redirect_view),
    (r'^redirects/$', RedirectView.as_view(url='/test_client_regress/redirects/further/')),
    (r'^redirects/further/$', RedirectView.as_view(url='/test_client_regress/redirects/further/more/')),
    (r'^redirects/further/more/$', RedirectView.as_view(url='/test_client_regress/no_template_view/')),
    (r'^redirect_to_non_existent_view/$', RedirectView.as_view(url='/test_client_regress/non_existent_view/')),
    (r'^redirect_to_non_existent_view2/$', RedirectView.as_view(url='/test_client_regress/redirect_to_non_existent_view/')),
    (r'^redirect_to_self/$', RedirectView.as_view(url='/test_client_regress/redirect_to_self/')),
    (r'^circular_redirect_1/$', RedirectView.as_view(url='/test_client_regress/circular_redirect_2/')),
    (r'^circular_redirect_2/$', RedirectView.as_view(url='/test_client_regress/circular_redirect_3/')),
    (r'^circular_redirect_3/$', RedirectView.as_view(url='/test_client_regress/circular_redirect_1/')),
    (r'^set_session/$', views.set_session_view),
    (r'^check_session/$', views.check_session_view),
    (r'^request_methods/$', views.request_methods_view),
    (r'^check_unicode/$', views.return_unicode),
    (r'^parse_unicode_json/$', views.return_json_file),
    (r'^check_headers/$', views.check_headers),
    (r'^check_headers_redirect/$', RedirectView.as_view(url='/test_client_regress/check_headers/')),
    (r'^raw_post_data/$', views.raw_post_data),
    (r'^read_all/$', views.read_all),
    (r'^read_buffer/$', views.read_buffer),
    (r'^request_context_view/$', views.request_context_view),
)


# Use the recursion to calculate the exponetiation
m = 1
def fastExpo(a, b):
    global m
    m *= a
    if b == 1:
        return m
    return fastExpo(a, b-1)

# Main function
if __name__ == '__main__':
    a, b = input("Please enter the base a and the exponent b (format: a^b): \n").split("^")
    a = int(a)
    b = int(b)

    result = fastExpo(a, b)
    print(result)


import uuid
from contextlib import contextmanager
from typing import Iterable, Iterator

import sqlalchemy
from more_itertools import chunked
from sqlalchemy import Column, Index, UniqueConstraint, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.types import JSON, Integer, String

from commit_viewer import git

from .interface import ProjectNotFound, StorageError, StoragePort

BATCH_SIZE = 1000

Base = declarative_base()

class Project(Base):
    __tablename__ = "projects"
    project_id = Column(String, primary_key=True)
    url = Column(String, nullable=False)

    UniqueConstraint("ix_projects_url", url)

class Commit(Base):
    __tablename__ = "commits"
    project_id = Column(
        String,
        ForeignKey(column=Project.project_id, ondelete="cascade", onupdate="cascade"),
        primary_key=True,
    )
    hash = Column(String, primary_key=True)
    sequence = Column(Integer)

    # as we don't need to search and correlate over these fields I just dump it
    # as a JSON object and keep indexable fields available. Worst case a
    # migration can change all of this.
    commit_data = Column(JSON, nullable=False)

Index("ix_commits_project_sequence", Commit.project_id, Commit.sequence)

class AlchemyAdapter(StoragePort):
    def __init__(self, connection_string: str) -> None:
        # setup engine and create tables
        self.engine = sqlalchemy.create_engine(connection_string)
        self.sessionmaker = sessionmaker(bind=self.engine)
        Base.metadata.create_all(self.engine)

    def store_commits(self, url: str, commits: Iterable[git.Commit]) -> None:
        try:
            with session_scope(self.sessionmaker) as session:
                project = (
                    session.query(Project).filter(Project.url == url).one_or_none()
                )
                if project is None:
                    project = Project(project_id=uuid.uuid4().hex, url=url)
                    session.add(project)
                else:
                    session.query(Commit).filter(
                        Commit.project_id == project.project_id
                    ).delete()

                for commits_chunked in chunked(enumerate(commits), BATCH_SIZE):
                    session.bulk_insert_mappings(
                        mapper=Commit,
                        mappings=[
                            {
                                "hash": commit.hash,
                                "project_id": project.project_id,
                                "sequence": seq,
                                "commit_data": commit.asdict(),
                            }
                            for seq, commit in commits_chunked
                        ],
                    )
        except sqlalchemy.exc.SQLAlchemyError as exc:
            raise StorageError(f"Failed to store commits: {exc}") from exc

    def list_commits(self, url: str) -> Iterator[git.Commit]:
        try:
            with session_scope(self.sessionmaker) as session:
                project = (
                    session.query(Project).filter(Project.url == url).one_or_none()
                )
                if project is None:
                    raise ProjectNotFound(f"Project not cached: {url}")
                else:
                    for commit in (
                        session.query(Commit)
                        .filter(Commit.project_id == project.project_id)
                        .order_by(Commit.sequence)
                        .yield_per(BATCH_SIZE)
                    ):
                        yield git.Commit(**commit.commit_data)
        except sqlalchemy.exc.SQLAlchemyError as exc:
            raise StorageError(f"Failed to store commits: {exc}") from exc

@contextmanager
def session_scope(sessionmaker):
    """Provide a transactional scope around a series of operations."""
    session = sessionmaker()
    try:
        yield session
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()


from django.shortcuts import render, redirect
from .models import *
import bcrypt
from django.contrib import messages

def index(request):
    return render(request, "FinalLog/login.html")

def signup(request):
    return render(request, "FinalLog/signup.html")

def regacc(request):
    errors = User.objects.basic_validator(request.POST)
    if len(errors) > 0:
        for key, value in errors.items():
            messages.error(request, value)
        return redirect('/signup')
    else:
        f_name = request.POST["firstname"]
        l_name = request.POST["lastname"]
        e_mail = request.POST["reg-email"]
        passw = bcrypt.hashpw(request.POST["reg-pw"].encode(), bcrypt.gensalt())
        User.objects.create(fname=f_name,lname=l_name,email=e_mail,pword=passw)
        user = User.objects.get(email = request.POST['reg-email'])
        request.session['user_id'] = user.id
        request.session['logged'] = True
        messages.success(request, "Successfully Registered!")
        return redirect("/dashboard")

def logacc(request):
    errors = User.objects.login_validator(request.POST)
    if len(errors) > 0:
        for key, value in errors.items():
            messages.error(request, value)
        return redirect('/')
    else:
        request.session['logged'] = True
        user = User.objects.get(email = request.POST['log-email'])
        request.session['user_id'] = user.id
        # messages.success(request, 'Successfully loging in...!')
        return redirect('/dashboard')

def logout(request):
    request.session.clear()
    messages.success(request, 'Successfully logout!')
    return redirect('/')

def dashboard(request):
    if request.session['logged'] == False:
        return redirect("/")
    elif request.session['logged'] == True:
        user = User.objects.get(id=request.session['user_id'])
        content = {
            "user": user,
        }
        return render(request, "FinalLog/dashboard.html", content)


"""Implementação da Regressão Logística"""

import numpy as np

class LogisticRegression():
    """Classe que implementa o Regressão Logística, assim como seu processo
    de otimização
    """

    def __init__(self, lr):
        """Construtor para a Regressão Logística

        Parameters
        ----------
        lr: float
            Taxa de aprendizado
        """
        self.lr = lr

    def run_experiment(self, N, dataset):
        """Roda o processo de otimização da regressão logística, calculando
        o gradiente e realizando a atualização dos pesos a cada iteração

        Quando a norma da diferença entre dois vetores de peso consecutivos
        for menos que 0.01, estabelecemos a convergência do algoritmo, e
        calculamos o erro fora da amostra para um conjunto de 1000 pontos de
        teste

        Parameters
        ----------
        N: int
            Quantidade de exemplos de treino a ser gerada
        dataset: Dataset
            Tipo de Dataset a ser utilizado

        Returns
        -------
        list
            Lista contendo o número de épocas de treinamento necessárias até
                a convergência ser atingida e o valor do erro fora da amostra
        """

        df, coeff_vector = dataset.create_dataframe(N)

        X = df[['x1', 'x2', 'bias']].values
        y = df['y'].values
        N = X.shape[0]
        w_old = np.zeros(X.shape[1])

        max_epochs = 10**6

        for epoch in range(max_epochs):
            X_y = np.concatenate((X, y[:, np.newaxis]), axis=1)
            np.random.shuffle(X_y)

            _X = X_y[:, :-1]
            _y = X_y[:, -1]

            num = _y[:, np.newaxis] * _X
            den = 1 + np.exp(_y * (_X @ w_old))
            quotient = num / den[:, np.newaxis]

            grad_E_in = - (1/N) * np.sum(quotient, axis=0)

            w_new = w_old - N * self.lr * grad_E_in

            if np.linalg.norm(w_new - w_old) < 0.01:
                N_test = 1000
                test_points = dataset.generate_data(N_test)
                y_true = np.sign(test_points @ coeff_vector)

                E_out = (1/N_test) * np.sum(
                    np.log(1 + np.exp(- y_true * (test_points @ w_new)))
                )

                return [epoch, E_out]

            w_old = w_new

        return max_epochs

    def run_epoch(self, runs, N, dataset):
        """Roda uma sequência de execuções algoritmo de otimização da
        Regressão Logística

        Parameters
        ----------
        runs: int
            Número de execuções a serem executadas
        N: int
            Quantidade de exemplos de treino a cada execução
        dataset:
            Tipo de Dataset utilizado

        Returns
        -------
        list
            Lista contendo o número de épocas e o erro fora da amostra para
            cada execução
        """
        return [self.run_experiment(N, dataset)
                for _ in np.arange(runs)]


import csv
cnt = 0
asin = {}
asin_except = {}
asin_fev = {}
product_except = {}
product = {}
wt = {}
wd = {}
lg = {}
ht = {}
total = {}
release = {}
numsel = {}
rank = {}
maker_except = {}
maker = {}
brand_except = {}
brand = {}

with open('C:\\Users\\USER\\Documents\\1_2_dup\\bin\\20160409162318.csv',  newline='') as f:
    reader = csv.reader(f)
    header = next(reader)
    for row in reader:
        cnt += 1
        asin_fev[row[0]] = ""
        asin_except[row[1]] = ""
        asin[row[2]] = ""
        product_except[row[3]] = ""
        product[row[4]] = ""
        wt[row[6]] = ""
        wd[row[7]] = ""
        lg[row[8]] = ""
        ht[row[9]] = ""
        total[row[10]] = ""
        release[row[11]] = ""
        numsel[row[12]] = ""
        rank[row[13]] = ""
        maker_except[row[14]] = ""
        maker[row[15]] = ""
        brand_except[row[16]] = ""
        brand[row[17]] = ""

f.close()

len(asin) #num
'B019DENO88' in asin #true or false


from twisted.internet import defer
import bnw.core.bnw_objects as objs
from bnw.web.base import BnwWebHandler

class LoginHandler(BnwWebHandler):
    @defer.inlineCallbacks
    def respond(self):
        key = self.get_argument('key', '')
        user = yield objs.User.find_one({'login_key': key})
        if user:
            if self.request.protocol == 'https':
                self.set_cookie(
                    'bnw_loginkey', key, expires_days=30, secure=True)
            else:
                self.set_cookie('bnw_loginkey', key, expires_days=30)
            self.redirect('/')
        else:
            defer.returnValue('Bad login key')

class LogoutHandler(BnwWebHandler):
    def respond(self):
        self.clear_all_cookies()
        self.redirect('/')

class AuthMixin(object):
    @defer.inlineCallbacks
    def get_auth_user(self):
        if not getattr(self, 'user', None):
            key = self.get_cookie('bnw_loginkey', '')
            self.user = yield objs.User.find_one({'login_key': key}) \
                if key else None
            if self.user:
                self.user.activity()
        defer.returnValue(self.user)

def requires_auth(fun):
    @defer.inlineCallbacks
    def newfun(self, *args, **kwargs):
        user = yield self.get_auth_user()
        if not user:
            self.set_status(403)
            defer.returnValue(self.render('noauth.html'))
        else:
            defer.returnValue((yield fun(self, *args, **kwargs)))
    return newfun