from __future__ import absolute_import
import logging
import os
import posixpath
import shutil
import time

import py_utils
from py_utils import exc_util

from telemetry.core import exceptions
from telemetry import decorators
from telemetry.internal.backends.chrome import chrome_browser_backend
from telemetry.internal.backends.chrome import cros_minidump_symbolizer
from telemetry.internal.backends.chrome import minidump_finder
from telemetry.internal.backends.chrome import misc_web_contents_backend
from telemetry.internal.results import artifact_logger
from telemetry.internal.util import format_for_logging

class CrOSBrowserBackend(chrome_browser_backend.ChromeBrowserBackend):
  def __init__(self, cros_platform_backend, browser_options,
               browser_directory, profile_directory, is_guest, env,
               build_dir=None, enable_tracing=True):
    """
    Args:
      cros_platform_backend: The cros_platform_backend.CrOSPlatformBackend
          instance to use.
      browser_options: The browser_options.BrowserOptions instance to use.
      browser_directory: A string containing the path to the directory on the
          device where the browser is installed.
      enable_tracing: Defines if a tracing_client is created.
      profile_directory: A string containing a path to the directory on the
          device to store browser profile information in.
      is_guest: A boolean indicating whether the browser is being run in guest
          mode or not.
      env: A list of strings containing environment variables to start the
          browser with.
      build_dir: A string containing a path to the directory on the host that
          the browser was built in, for finding debug artifacts. Can be None if
          the browser was not locally built, or the directory otherwise cannot
          be determined.
    """
    assert browser_options.IsCrosBrowserOptions()
    super().__init__(
        cros_platform_backend,
        browser_options=browser_options,
        browser_directory=browser_directory,
        enable_tracing=enable_tracing,
        profile_directory=profile_directory,
        supports_extensions=not is_guest,
        supports_tab_control=True,
        build_dir=build_dir)
    self._is_guest = is_guest
    self._cri = cros_platform_backend.cri
    self._env = env
    self._started = False

  @property
  def log_file_path(self):
    return None

  def _GetDevToolsActivePortPath(self):
    return '/home/chronos/DevToolsActivePort'

  def _FindDevToolsPortAndTarget(self):
    devtools_file_path = self._GetDevToolsActivePortPath()
    # GetFileContents may rise IOError or OSError, the caller will retry.
    lines = self._cri.GetFileContents(devtools_file_path).splitlines()
    if not lines:
      raise EnvironmentError('DevTools file empty')

    devtools_port = int(lines[0])
    browser_target = lines[1] if len(lines) >= 2 else None
    return devtools_port, browser_target

  def _FindUIDevtoolsPort(self):
    devtools_file_path = '/home/chronos/UIDevToolsActivePort'
    # GetFileContents may rise IOError or OSError, the caller will retry.
    lines = self._cri.GetFileContents(devtools_file_path).splitlines()
    if not lines:
      raise EnvironmentError('UIDevTools file empty')

    devtools_port = int(lines[0])
    return devtools_port

  def GetPid(self):
    return self._cri.GetChromePid()

  def Start(self, startup_args):
    if self._started:
      return
    self._started = True

    self._cri.OpenConnection()
    # Remove the stale file with the devtools port / browser target
    # prior to restarting chrome.
    self._cri.RmRF(self._GetDevToolsActivePortPath())

    self._dump_finder = minidump_finder.MinidumpFinder(
        self._platform_backend.platform.GetOSName(),
        self._platform_backend.platform.GetArchName())

    # Escape all commas in the startup arguments we pass to Chrome
    # because dbus-send delimits array elements by commas
    startup_args = [a.replace(',', '\\,') for a in startup_args]

    # Restart Chrome with the login extension and remote debugging.
    pid = self.GetPid()
    logging.info('Restarting Chrome (pid=%d) with remote port', pid)
    args = ['dbus-send', '--system', '--type=method_call',
            '--dest=org.chromium.SessionManager',
            '/org/chromium/SessionManager',
            'org.chromium.SessionManagerInterface.EnableChromeTesting',
            'boolean:true',
            'array:string:"%s"' % ','.join(startup_args),
            'array:string:"%s"' % ','.join(self._env)]
    formatted_args = format_for_logging.ShellFormat(
        args, trim=self.browser_options.trim_logs)
    logging.info('Starting Chrome: %s', formatted_args)
    self._cri.RunCmdOnDevice(args)

    # Wait for new chrome and oobe.
    py_utils.WaitFor(lambda: pid != self.GetPid(), 15)
    self.BindDevToolsClient()
    py_utils.WaitFor(lambda: self.oobe_exists, 30)

    if self.browser_options.auto_login:
      if self._is_guest:
        pid = self.GetPid()
        self.oobe.NavigateGuestLogin()
        # Guest browsing shuts down the current browser and launches an
        # incognito browser in a separate process, which we need to wait for.
        try:
          py_utils.WaitFor(lambda: pid != self.GetPid(), 15)

          # Also make sure we reconnect the devtools client to the new browser
          # process. It's important to do this before waiting for _IsLoggedIn,
          # otherwise the devtools connection check will still try to reach the
          # older DevTools agent (and fail to do so).
          self.BindDevToolsClient()
        except py_utils.TimeoutException:
          self._RaiseOnLoginFailure(
              'Failed to restart browser in guest mode (pid %d).' % pid)

      elif self.browser_options.gaia_login:
        self.oobe.NavigateGaiaLogin(self._username, self._password)
      else:
        # Wait for few seconds(the time of password typing) to have mini ARC
        # container up and running. Default is 0.
        time.sleep(self.browser_options.login_delay)
        # crbug.com/976983.
        retries = 3
        while True:
          try:
            self.oobe.NavigateFakeLogin(
                self._username, self._password, self._gaia_id,
                not self.browser_options.disable_gaia_services)
            break
          except py_utils.TimeoutException:
            logging.error('TimeoutException %d', retries)
            retries -= 1
            if not retries:
              raise

      try:
        self._WaitForLogin()
      except py_utils.TimeoutException:
        self._RaiseOnLoginFailure('Timed out going through login screen. '
                                  + self._GetLoginStatus())

    logging.info('Browser is up!')

  def Background(self):
    raise NotImplementedError

  @exc_util.BestEffort
  def Close(self):
    super().Close()

    if self._cri:
      self._cri.RestartUI() # Logs out.
      py_utils.WaitFor(lambda: not self._IsCryptohomeMounted(), 180)
      self._cri.CloseConnection()

    self._cri = None

    if self._tmp_minidump_dir:
      shutil.rmtree(self._tmp_minidump_dir, ignore_errors=True)
      self._tmp_minidump_dir = None

  def IsBrowserRunning(self):
    if not self._cri:
      return False
    return bool(self.GetPid())

  def GetStandardOutput(self):
    return 'Cannot get standard output on CrOS'

  def PullMinidumps(self):
    if self._cri:
      self._cri.PullDumps(self._tmp_minidump_dir)
    else:
      logging.error(
          'Attempted to pull minidumps without CrOSInterface. Either the '
          'browser is already closed or was never started.')

  def SymbolizeMinidump(self, minidump_path):
    return self._SymbolizeMinidump(minidump_path)

  def CollectDebugData(self, log_level):
    """Collects various information that may be useful for debugging.

    In addition to data captured by the parent class' implementation, also
    captures:
    1. Chrome logs
    2. ChromeOS UI logs

    Args:
      log_level: The logging level to use from the logging module, e.g.
          logging.ERROR.

    Returns:
      A debug_data.DebugData object containing the collected data.
    """
    self._CollectBrowserLogs(log_level)
    self._CollectUiLogs(log_level)
    return super().CollectDebugData(log_level)

  def _CollectBrowserLogs(self, log_level):
    """Helper function to handle the browser log part of CollectDebugData.

    Attempts to retrieve the current and previous browser logs, merge them, and
    save the result as an artifact.

    Args:
      log_level: The logging level to use from the logging module, e.g.
          logging.ERROR.
    """
    # /var/log/chrome/chrome is the log for the current browser, but in case
    # there's something useful in the previous browser's logs, merge chrome
    # and chrome.PREVIOUS.
    try:
      current_log = self._cri.GetFileContents('/var/log/chrome/chrome')
    except OSError:
      logging.log(log_level, 'Unexpectedly did not find browser log')
      return
    current_log = '#### Current Chrome Log ####\n\n%s' % current_log
    try:
      previous_log = self._cri.GetFileContents(
          '/var/log/chrome/chrome.PREVIOUS')
    except OSError:
      # This is expected if this is the first browser launch on this device.
      previous_log = 'Did not find a previous Chrome log.'
    merged_log = '%s\n\n#### Previous Chrome Log ####\n\n%s' % (current_log,
                                                                previous_log)
    artifact_name = posixpath.join(
        'browser_logs', 'browser_log-%s' % artifact_logger.GetTimestampSuffix())
    logging.log(log_level, 'Saving browser log as artifact %s', artifact_name)
    artifact_logger.CreateArtifact(artifact_name, merged_log)

  def _CollectUiLogs(self, log_level):
    """Helper function to handle the UI log part of CollectDebugData.

    Attempts to retrieve the current UI log and save it as an artifact.

    Args:
      log_level: The logging level to use from the logging module, e.g.
          logging.ERROR.
    """
    # Unlike the browser logs, there is no .PREVIOUS version, so we can only
    # easily get the most recent UI log.
    try:
      ui_log = self._cri.GetFileContents('/var/log/ui/ui.LATEST')
    except OSError:
      logging.log(log_level, 'Unexpectedly did not find UI log')
      return
    artifact_name = posixpath.join(
        'ui_logs', 'ui_log-%s' % artifact_logger.GetTimestampSuffix())
    logging.log(log_level, 'Saving UI log as artifact %s', artifact_name)
    artifact_logger.CreateArtifact(artifact_name, ui_log)

  @property
  def screenshot_timeout(self):
    # Screenshots fail when the screen is off, and we can flakily attempt to
    # capture screenshots on failure when the screen is off. So, retry for a
    # while if we run into that.
    return 15

  @property
  def supports_overview_mode(self): # pylint: disable=invalid-name
    return True

  def EnterOverviewMode(self, timeout):
    self.devtools_client.window_manager_backend.EnterOverviewMode(timeout)

  def ExitOverviewMode(self, timeout):
    self.devtools_client.window_manager_backend.ExitOverviewMode(timeout)

  @property
  @decorators.Cache
  def misc_web_contents_backend(self):
    """Access to chrome://oobe/login page."""
    return misc_web_contents_backend.MiscWebContentsBackend(self)

  @property
  def oobe(self):
    return self.misc_web_contents_backend.GetOobe()

  @property
  def oobe_exists(self):
    return self.misc_web_contents_backend.oobe_exists

  @property
  def _username(self):
    return self.browser_options.username

  @property
  def _password(self):
    return self.browser_options.password

  @property
  def _gaia_id(self):
    return self.browser_options.gaia_id

  def _IsCryptohomeMounted(self):
    username = '$guest' if self._is_guest else self._username
    return self._cri.IsCryptohomeMounted(username, self._is_guest)

  def _GetLoginStatus(self):
    """Returns login status. If logged in, empty string is returned."""
    status = ''
    if not self._IsCryptohomeMounted():
      status += 'Cryptohome not mounted. '
    if not self.HasDevToolsConnection():
      status += 'Browser didn\'t launch. '
    if self.oobe_exists:
      status += 'OOBE not dismissed.'
    return status

  def _IsLoggedIn(self):
    """Returns True if cryptohome has mounted, the browser is
    responsive to devtools requests, and the oobe has been dismissed."""
    return not self._GetLoginStatus()

  def _WaitForLogin(self):
    # Wait for cryptohome to mount.
    py_utils.WaitFor(self._IsLoggedIn, 900)

    # Wait for extensions to load.
    if self._supports_extensions:
      self._WaitForExtensionsToLoad()

  def _RaiseOnLoginFailure(self, error):
    if self._platform_backend.CanTakeScreenshot():
      self._cri.TakeScreenshotWithPrefix('login-screen')
    raise exceptions.LoginException(error)

  def _SymbolizeMinidump(self, minidump_path):
    """Symbolizes the given minidump.

    Args:
      minidump_path: the path to the minidump to symbolize

    Return:
      A tuple (valid, output). |valid| is True if the minidump was symbolized,
      otherwise False. |output| contains an error message if |valid| is False,
      otherwise it contains the symbolized minidump.
    """
    # TODO(https://crbug.com/994267): Make the minidump being symbolized
    # available as an artifact.
    stack = self._GetStackFromMinidump(minidump_path)
    if not stack:
      error_message = ('Failed to symbolize minidump.')
      return (False, error_message)

    self._symbolized_minidump_paths.add(minidump_path)
    return (True, stack)

  def _GetStackFromMinidump(self, minidump):
    """Gets the stack trace from the given minidump.

    Args:
      minidump: the path to the minidump on disk

    Returns:
      None if the stack could not be retrieved for some reason, otherwise a
      string containing the stack trace.
    """
    dump_symbolizer = cros_minidump_symbolizer.CrOSMinidumpSymbolizer(
        self._dump_finder, self.build_dir,
        symbols_dir=self._CreateExecutableUniqueDirectory('chrome_symbols_'))
    return dump_symbolizer.SymbolizeMinidump(minidump)

  def _GetBrowserExecutablePath(self):
    if self.build_dir:
      possible_path = os.path.join(self.build_dir, 'chrome')
      if os.path.isfile(possible_path):
        return possible_path
    return None


from sparkdq.analytics.metrics.DoubleMetric import DoubleMetric
from sparkdq.analytics.metrics.Metric import Metric
from sparkdq.structures.Entity import Entity
from sparkdq.structures.Violations import Violations

class KeyedViolationsMetric(Metric):

    def __init__(self, name, entity, instance, total_violations, is_success):
        """
        :param instance:            list of CFDs
        :param total_violations:    list of Violations for all CFDs
        """
        super(KeyedViolationsMetric, self).__init__(name, entity, instance, total_violations, is_success)

    def flatten(self):
        details = []
        for i in range(len(self.value)):
            cfd = self.instance[i]
            cfd_vios = self.value[i].violations
            for j in range(len(cfd_vios)):
                pat_vios = cfd_vios[j]
                for k in range(len(pat_vios)):
                    rhs_vios = pat_vios[k]
                    details.append(DoubleMetric(self.name, self.entity, "{}.{}.{}.constVio".format(cfd, j, k),
                                                rhs_vios.const_vio, True))
                    details.append(DoubleMetric(self.name, self.entity, "{}.{}.{}.varVio".format(cfd, j, k),
                                                rhs_vios.var_vio, True))
        return details

    def to_json(self):
        if not self.is_success:
            raise Exception("Unable to serialize failed metrics.")
        return {
            "MetricName": KeyedViolationsMetric.__name__,
            "Entity": self.entity,
            "Instance": self.instance,
            "Name": self.name,
            "TotalViolations": self.value.to_json()
        }

    @staticmethod
    def from_json(d):
        total_violations = Violations.from_json(d["TotalViolations"])
        return KeyedViolationsMetric(d["Name"], Entity(d["Entity"]), d["Instance"], total_violations, True)

    def represent_value(self):
        return str(self.value.total_vios())


import ml_collections
ConfigDict = ml_collections.ConfigDict

def get_config():
  """Returns a many well experiment config as ConfigDict."""
  config = ConfigDict()

  config.seed = 1
  config.batch_size = 2000
  config.estimation_batch_size = 2000
  config.sample_shape = (32,)
  config.report_step = 1
  config.vi_report_step = 100
  config.use_x64 = False
  config.num_layers = 1
  config.step_logging = True
  config.num_temps = 11
  config.resample_threshold = 0.3
  config.write_samples = False
  config.stopping_criterion = 'time'
  config.use_resampling = True
  config.use_markov = True
  config.algo = 'craft'
  config.optim_markov = False
  config.craft_num_iters = 200
  config.snf_num_iters = 1000
  config.fcraft_num_iters = 500
  config.craft_batch_size = 2000
  config.snf_batch_size = 2000
  config.fcraft_batch_size = 2000
  config.vi_iters = 100000
  config.checkpoint_interval = 200000
  config.vi_estimator = 'importance'
  config.use_path_gradient = False

  optimization_config = ConfigDict()
  optimization_config.free_energy_iters = 500
  optimization_config.aft_step_size = 1e-2
  optimization_config.craft_step_size = 1e-4
  optimization_config.vi_step_size = 1e-4
  config.optimization_config = optimization_config

  initial_config = ConfigDict()
  initial_config.density = 'MultivariateNormalDistribution'
  initial_config.shared_mean = 0.
  initial_config.diagonal_cov = 1.
  config.initial_config = initial_config

  final_config = ConfigDict()
  final_config.density = 'ManyWell'
  config.final_config = final_config

  flow_config = ConfigDict()
  flow_config.type = 'SplineInverseAutoregressiveFlow'
  flow_config.num_spline_bins = 10
  flow_config.lower_lim = -3.
  flow_config.intermediate_hids_per_dim = 30
  flow_config.num_layers = 3
  flow_config.identity_init = True
  flow_config.bias_last = True
  flow_config.upper_lim = 3.
  flow_config.min_bin_size = 1e-2
  flow_config.min_derivative = 1e-2
  flow_config.num_elem = config.sample_shape[0]
  flow_config.sample_shape = config.sample_shape
  config.flow_config = flow_config
  initial_sampler_config = ConfigDict()
  initial_sampler_config.initial_sampler = 'MultivariateNormalDistribution'
  config.initial_sampler_config = initial_sampler_config

  mcmc_config = ConfigDict()
  hmc_step_config = ConfigDict()
  hmc_step_config.step_times = [0., 0.25, 0.5, 1.]
  hmc_step_config.step_sizes = [0.3, 0.3, 0.2, 0.2]
  nuts_step_config = ConfigDict()
  nuts_step_config.step_times = [0., 0.25, 0.5, 1.]
  nuts_step_config.step_sizes = [0.7, 0.7, 0.5, 0.5]

  rwm_step_config = ConfigDict()
  rwm_step_config.step_times = [0., 0.25, 0.5, 1.]
  rwm_step_config.step_sizes = [0.03, 0.03, 0.03, 0.03]

  mcmc_config.hmc_step_config = hmc_step_config
  mcmc_config.slice_step_config = hmc_step_config
  mcmc_config.nuts_step_config = nuts_step_config
  mcmc_config.rwm_step_config = rwm_step_config
  mcmc_config.hmc_steps_per_iter = 1
  mcmc_config.use_jax_hmc = True
  mcmc_config.rwm_steps_per_iter = 0
  mcmc_config.hmc_num_leapfrog_steps = 10

  mcmc_config.slice_steps_per_iter = 0
  mcmc_config.nuts_steps_per_iter = 0
  mcmc_config.slice_max_doublings = 5
  mcmc_config.nuts_max_tree_depth = 4
  mcmc_config.iters = 1
  config.mcmc_config = mcmc_config
  config.save_params = False

  return config


import subprocess

from debian import deb822

def deb_source():
    return (
        subprocess.check_output(
            [
                "apt-get",
                "indextargets",
                "--format",
                "$(FILENAME)",
                "Codename: sid",
                "ShortDesc: Sources",
            ]
        )
        .decode()
        .strip()
    )

def list_pkgs():
    # type: () -> dict[str, list[deb822.Sources]]
    pkgs = {}
    fn = deb_source()
    with open(fn, encoding="utf-8") as f:
        for pkg in deb822.Sources.iter_paragraphs(f):
            if pkg.get("Extra-Source-Only") == "yes":
                continue
            name = pkg["Package"]
            pkgs[name] = pkgs.get(name, []) + [pkg]
    return {
        k: v
        for k, v in pkgs.items()
        if "golang" in k and len(v) > 1 and len({pkg["Binary"] for pkg in v}) > 1
    }

if __name__ == "__main__":
    pkgs = list_pkgs()
    print(len(pkgs))
    for k, v in pkgs.items():
        print("%s: %s" % (k, {pkg["Binary"] for pkg in v}))

# vim: ts=4:sw=4:et


import db
import os
import utils
import argparse
import time
import collections
from itertools import chain
from dotenv import load_dotenv
from hdbscan import HDBSCAN, approximate_predict
from sklearn.feature_extraction.text import TfidfVectorizer
from datasketch import MinHash, MinHashLSH
from datetime import datetime
from datetime import timedelta

import online_clustering

script_name = os.path.basename(__file__)

load_dotenv()

def compare_clusters(existing_clusters, new_clusters):

    # Get true events based on labeled test data

    new_news_ids = list(
        chain.from_iterable([identifier.split(",") for identifier in new_clusters])
    )
    existing_news_ids = list(
        chain.from_iterable([identifier.split(",") for identifier in existing_clusters])
    )

    new_rows = len(set(new_news_ids) - set(existing_news_ids))
    true_change_events, true_addition_events, true_deletion_events = online_clustering.find_true_events(
        new_news_ids, existing_news_ids
    )

    changed_clusters, unchanged_clusters, new_clusters = online_clustering.find_changes_in_clusters(existing_clusters, new_clusters)

    print("----------------------")
    print("New Rows: ", len(set(new_news_ids) - set(existing_news_ids)))
    print("Events:")
    print("Topic added: {} detected, {} true".format(len(new_clusters), true_addition_events))
    print("Topic extended: {} detected, {} true".format(len(changed_clusters), len(true_change_events)))

# Overwrite function to handle different input format...

date1 = '2014-05-15 11:00:00'
date2 = '2014-05-15 12:00:00'
date3 = '2014-05-15 13:00:00'
date4 = '2014-05-15 14:00:00'
date5 = '2014-05-15 15:00:00'

nrows = 2000

clusters1,_ = online_clustering.cluster_news_by_date(nrows, date1)
clusters2,_ = online_clustering.cluster_news_by_date(nrows, date2)
clusters3,_ = online_clustering.cluster_news_by_date(nrows, date3)
clusters4,_ = online_clustering.cluster_news_by_date(nrows, date4)
clusters5,_ = online_clustering.cluster_news_by_date(nrows, date5)

compare_clusters(clusters1, clusters2)
compare_clusters(clusters2, clusters3)
compare_clusters(clusters3, clusters4)
compare_clusters(clusters4, clusters5)


import cv2
from imutils.paths import list_images
import pixel_art

image = cv2.imread('images/github_logo.png')
# icon_paths = list(list_images('path/to/icons/dir'))
icon_paths = list(list_images('icons'))

# create pixelated version of input image
pixelated_image, (nrow, ncol) = pixel_art.pixelate(image, ncol=40, out_width=500)

# create pixelated version of input image from 'icons'
# where the icons are recolored to match the pixelated input image
recolor_icon_image = pixel_art.pixel_icon_recolor(image,
                                                  icon_paths=icon_paths,
                                                  ncol=40,
                                                  out_width=500)

# extract dominant colors from icons for color
# matching process in pixel_art.pixel_icon_match
icon_stats = pixel_art.get_icon_colors(icon_paths)

# create pixelated version of input image from 'icons'
# where the icons are attempted to be matched to the
# color of the pixelated input image
match_icon_image = pixel_art.pixel_icon_match(image,
                                              icon_stats,
                                              ncol=40,
                                              out_width=500)


import os
import random
import string
from pathlib import Path

import pytest
from _pytest.fixtures import SubRequest
from cluster_helpers import (
    wait_for_dcos_oss,
)
from dcos_e2e.backends import Docker
from dcos_e2e.cluster import Cluster
from dcos_e2e.node import Output

@pytest.fixture(scope='module')
def workspace_dir() -> Path:
    """
    Creates a known workspace directory.
    """
    tmp_dir_path = Path(os.environ['DCOS_E2E_TMP_DIR_PATH'])
    assert tmp_dir_path.exists() and tmp_dir_path.is_dir()

    subpath = ''.join(random.choice(string.ascii_lowercase) for i in range(5))

    return tmp_dir_path / subpath

def test_windows_agents(
    workspace_dir: Path,
    artifact_path: Path,
    request: SubRequest,
    log_dir: Path,
) -> None:
    """
    Enabling Windows agents creates additional configuration package
    and does not break Linux installation.
    """
    docker_backend = Docker(workspace_dir=workspace_dir)

    config = {
        'enable_windows_agents': True,
    }
    with Cluster(
        cluster_backend=docker_backend,
        agents=0,
        public_agents=0,
    ) as cluster:
        cluster.install_dcos_from_path(
            dcos_installer=artifact_path,
            dcos_config={
                **cluster.base_config,
                **config,
            },
            output=Output.LOG_AND_CAPTURE,
            ip_detect_path=docker_backend.ip_detect_path,
        )

        # Check that dcos-config-win.tar.xz was created
        paths = []
        for root, _, files in os.walk(str(workspace_dir)):
            for file in files:
                if file.startswith('dcos-config-win--setup_'):
                    paths.append(Path(root) / file)
        assert len(paths) == 1

        wait_for_dcos_oss(
            cluster=cluster,
            request=request,
            log_dir=log_dir,
        )


import json

from yelpapi import YelpAPI
from log_writer import *

logger = get_logger('lambda-yelp')

def get_config(context):
	function_name = context.function_name
	function_arn = context.invoked_function_arn
	alias = function_arn.split(":").pop()

	config_file = '.json'

	if (alias == function_name or '$LATEST' == alias):
		config_file = 'dev' + config_file
	else:
		config_file = alias.lower() + config_file

	config_data = None
	with open(config_file) as json_data:
		config_data = json.load(json_data)

	return config_data

def handler(event, context):
	config_data = get_config(context)
	response = {'Status': 'success'}

	try:
		lat = event["lat"] if event["lat"] else 0
		long = event["long"] if event["long"] else 0
		name = event["name"] if event["name"] else ''
		limit = event["limit"] if event["limit"] else 50

		if (lat==0 or long==0 or name==''):
			logger.error('**ERROR: code.parameters.required')
			raise Exception()

		yelp_api = YelpAPI(config_data['yelp_api_key'])
		yelp_res = yelp_api.search_query(categories='restaurants', longitude=long, latitude=lat, limit=limit)
		for item in yelp_res['businesses']:
			logger.info('Location Name: {} and Rating: {}'.format(item['name'], item['rating']))

	except Exception as e:
		error(logger, e)
		response['Status'] = 'failure'

	return response


from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By

import os

options = Options()
options.headless = True

with webdriver.Firefox(options=options) as browser:
    browser.get(f'file://{os.getcwd()}/index.html')
    assert('Live Code Example' in browser.page_source)

    assert('textarea' in browser.page_source)

    textarea = browser.find_elements_by_tag_name("textarea")[1]
    textarea.send_keys("Math.sin(1);")
    assert('0.8414709848078965' in browser.page_source)

with webdriver.Firefox(options=options) as browser:
    browser.get(f'file://{os.getcwd()}/index.html')
    # capture and display the expected output?(17)
    # target: 0 1 2 3 4 5
    assert ('Target: 0 1 2 3 4 5' in browser.page_source)

    # checks to see if its compareing the target and the current output(18)
    # ensures that at the run time the teacher code is already located inside the text box in a working manner
    assert ('Good' in browser.page_source)

    # look for the code the teacher writes in the _____ html
    assert ('for(let i = 0; i < 6; i++) {\n\tconsole.log(i);\n}' in browser.page_source)

with webdriver.Firefox(options=options) as browser:
    browser.get(f'file://{os.getcwd()}/index.html')
    #tests to make sure if the student fail their code that there is a bad option displayed
    textarea = browser.find_elements_by_tag_name("textarea")[1]
    textarea.send_keys("for(let i = 0; i < 5; i++) {\n\tconsole.log(i);\n}")
    assert ('Bad' in browser.page_source)

with webdriver.Firefox(options=options) as browser:
    browser.get(f'file://{os.getcwd()}/index.html')
    #ensures that if i have more than what is wanted aka (0 1 2 3 4 5 6) that a Bad is still shown
    textarea = browser.find_elements_by_tag_name("textarea")[1]
    textarea.send_keys("for(let i = 0; i < 7; i++) {\n\tconsole.log(i);\n}")
    assert ('Bad' in browser.page_source)


def eul26(n):
    """Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part."""
    chain = 0
    d = 0

    for i in range(n, -1, -1):
        if chain > i:
            break

        remainders = [0] * i
        value = 1
        position = 0

        while remainders[value] == 0 and value != 0:
            remainders[value] = position
            value *= 10
            value %= i
            position += 1

        if position - remainders[value] > chain:
            chain = position - remainders[value]
            d = i
    return d

print(eul(1000))
# 983


import pytube
import moviepy.editor
import os
import re

def download_musica(link_video,pasta_arquivo):
    link = pytube.YouTube(link_video)
    diretorio = pasta_arquivo
    download = link.streams.filter(only_audio = True).first().download(diretorio)
    for file in os.listdir(diretorio):
        if re.search("mp4",file):
            mp4_path = os.path.join(diretorio,file)
            mp3_converte = os.path.join(diretorio,os.path.splitext(file)[0]+".mp3")
            arquivo_mp3 = moviepy.editor.AudioFileClip(mp4_path)
            arquivo_mp3.write_audiofile(mp3_converte)
            os.remove(mp4_path)

def download_video(link_video,pasta_arquivo):
    link = link_video
    diretorio = pasta_arquivo
    youtube = pytube.YouTube(link)
    download = youtube.streams.filter(progressive = True, file_extension="mp4").first().download(diretorio)


import socket

serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = "127.0.0.1"#socket.gethostname()
port = 5555
serverSocket.bind((host,port))

serverSocket.listen(5)

while(True):
	print("Waiting for a connection")
	clientSocket,addr = serverSocket.accept()
	print("Got a connection from {}".format(str(addr)))
	message = "You have connected to the server"
	clientSocket.send(message.encode('ascii'))
	clientSocket.close()

serverSocket.close()


import os
import sys
import shutil
from invoke import task
from pathlib import Path

LATEST_RELEASE = 'release/v0.4.1'

@task()
def install(c, branch=None):
    """
    Gets protos, builds protos, then installs the client.
    Protos can be specified using the --branch parameter.
    For example: `inv install --branch release/v0.3.0` will install protos from a previous version.
    """

    if branch is None:
        branch = LATEST_RELEASE

    # directory that contains setup.py
    setup_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

    # build directory
    build_path = str(Path(setup_dir, 'build'))

    # clear build directory if exists
    try:
        shutil.rmtree(build_path)
    except: pass

    # create initial build dirs
    Path(f'{build_path}/senseye').mkdir(exist_ok=True, parents=True)

    # make sure grpcio-tools is installed
    c.run(f'\
    {sys.executable} -m \
    pip install grpcio-tools'
    )

    from . build_protos import get_protos, build_protos

    # put protos into build dir
    get_protos(branch, output_path=build_path)

    # build protos
    build_protos(
        input_path=f'{build_path}',
        output_path=build_path
    )

    # install senseye-api-client
    c.run(f'\
    {sys.executable} -m \
    pip install . \
    --upgrade'
    )

    # clean up build dir
    shutil.rmtree(build_path)


from collections import Counter
import re

words = []

# Get a set of all words
with open('moby_dick.txt', 'r') as f:
    data = f.read()
    data = data.lower()
    words = re.findall('\w+', data)

corpus = Counter(words)

from spell_corrector import SpellCorrector

spell_corrector = SpellCorrector(dictionary=corpus, verbose=0)
print('Correction to eddwardd is:')
print(spell_corrector.correction('edwardd'))
print()
print('Correction to edwardhas is:')
print(spell_corrector.correction('edwardhas'))


import matplotlib.pyplot as plt
import numpy as np
from numpy.linalg.linalg import lstsq, inv
class mos():
    def __init__(self):
        self.minX=0
        self.minY=0

    def getH(self,p1,p2):
        A=[];
        for j in xrange(len(p1)*2):
            i=int(j/2)
            if(j%2==0):
                A.append([p1[i][0],p1[i][1],1,0,0,0,-p2[i][0]*p1[i][0],-p2[i][0]*p1[i][1]])
            else:
                A.append([0,0,0,p1[i][0],p1[i][1],1,-p2[i][1]*p1[i][0],-p2[i][1]*p1[i][1]])
        B=[];
        for i in xrange(len(p2)):
            B.append(p2[i][0])
            B.append(p2[i][1])
        a,b,c,d,e,f,g,h = np.linalg.lstsq(A, B)[0]
        return [[a,b,c],[d,e,f],[g,h,1]]

    def getPoint(self,p1,H):
        p=np.dot(H,[p1[0],p1[1],1])
        p[0]=int(float(p[0])/float(p[2]))
        p[1]=int(float(p[1])/float(p[2]))
        p[2]=1#malhash lazma
        return [p[0],p[1]]

    def applyMosiac(self,imgB,imgA,invH,H):
        WidthB=len(imgB[0])
        HeightB=len(imgB)
        WidthA=len(imgA[0])
        HeightA=len(imgA)
        tl=self.getPoint([0,0],H)
        tr=self.getPoint([WidthA-1,0],H)
        dl=self.getPoint([0,HeightA-1],H)
        dr=self.getPoint([WidthA-1,HeightA-1],H)
        ##should be modified
        maxXT=max(tr[0],dr[0],WidthB-1)#tl[0],dl[0]
        maxYT=max(dr[1],dl[1],HeightB-1)#tr[1],tl[1]

        minXT=min(tl[0],dl[0],0)#tr[0],dr[0],
        minYT=min(tr[1],tl[1],0)#,dr[1],dl[1]

        self.minX=minXT
        self.minY=minYT
        newWidth=int(maxXT-minXT+1);
        newHeight=int(maxYT-minYT+1);
        newImg=np.zeros((newHeight,newWidth,len(imgB[0][0])),dtype=np.uint8)
        newImg[-minYT:HeightB-minYT,-minXT:WidthB-minXT]=imgB
        #pixils=[]
        for i in xrange(int(minYT), int(maxYT) + 1, 1):
            for j in xrange(int(minXT), int(maxXT) + 1, 1):
                p = [j, i, 1]
                pdash = np.dot(invH, p)
                X = pdash[0] / pdash[2]
                Y = pdash[1] / pdash[2]
                if(X >= 0 and X < WidthA and Y >= 0 and Y < HeightA):# and (i - minYT-1) < len(newImg) and (j - int(minXT))<len(newImg[0])) :
                    newImg[i - int(minYT)-1][j - int(minXT)] = imgA[Y][X]

        return newImg


from collections import Counter

class Solution:
    def longestPalindrome(self, s: str) -> int:
        res = 0
        odds = False
        for letter, count in Counter(s).items():
            res += 2 * (count // 2)
            if count % 2 == 1:
                odds = True
        return res + int(odds)

if __name__ == '__main__':
    s = Solution()
    _str = "ccddsc"
    lp = s.longestPalindrome(s=_str)
    print(lp)


#  File: DNA.py

#  Description: Homework#8

#  Student Name: Julio Gonzalez

#  Student UT EID: jcg3245

#  Course Name: CS 303E

#  Unique Number: 33375

#  Date Created: 3/25/16

#  Date Last Modified: 3/26/16

def main():
    inFile = open ("./dna.txt", "r")

    num_pairs = inFile.readline()
    num_pairs = num_pairs.strip()
    num_pairs = int (num_pairs)
    print("Longest common sequnce")
    print()
    nug = 1
    for i in range (num_pairs):
      print ("Pair",nug, end = ': ')
      st1 = inFile.readline()
      st2 = inFile.readline()

      st1 = st1.strip()
      st2 = st2.strip()

      st1 = st1.upper()
      st2 = st2.upper()

      if (len(st1) > len(st2)):
          dna1 = st1
          dna2 = st2
      else:
          dna1 = st2
          dna2 = st1

      # get all substrings of dna2
      wnd = len (dna2)
      flag = True
      nug = nug + 1
      while (wnd > 0 and flag == True):
        start_idx = 0
        while ((start_idx + wnd) <= len (dna2)):
          sub_strand = dna2[start_idx: start_idx + wnd]
          if (dna1.find(sub_strand) != -1):
              print (sub_strand)
              flag = False
          # move starting place by 1
          start_idx += 1
        # decrease window size
        wnd = wnd - 1
      if flag == True:
        print ("No Common Sequence Found")
main()


raio = float(input('Digite o raio:', ))
format(raio,".2f")

pi = 3.14159
area = pi*raio**2

print('A área é igual a', format(area,'.4f'))

#Para o Uri:

r = float(input())
format(r,".2f")
n = 3.14159
a = n*r**2
print('A='+format(a,'.4f'))


# !/usr/bin/env python
from distutils.util import convert_path
from realestate import __version__, __maintainer__, __email__
from fnmatch import fnmatchcase
import os
import sys

try:
    from setuptools import setup, find_packages
except ImportError:
    from distutils.core import setup, find_packages

standard_exclude = ['*.py', '*.pyc', '*~', '.*', '*.bak']
standard_exclude_directories = [
    '.*', 'CVS', '_darcs', './build', './dist', 'EGG-INFO', '*.egg-info', 'testproject', '*.egg',
    './realestate/static/bower_components'
]

def find_package_data(where='.', package='', exclude=standard_exclude,
                      exclude_directories=standard_exclude_directories,
                      only_in_packages=True, show_ignored=False):
    """
    Return a dictionary suitable for use in ``package_data``
    in a distutils ``setup.py`` file.

    The dictionary looks like::

        {'package': [files]}

    Where ``files`` is a list of all the files in that package that
    don't match anything in ``exclude``.

    If ``only_in_packages`` is true, then top-level directories that
    are not packages won't be included (but directories under packages
    will).

    Directories matching any pattern in ``exclude_directories`` will
    be ignored; by default directories with leading ``.``, ``CVS``,
    and ``_darcs`` will be ignored.

    If ``show_ignored`` is true, then all the files that aren't
    included in package data are shown on stderr (for debugging
    purposes).

    Note patterns use wildcards, or can be exact paths (including
    leading ``./``), and all searching is case-insensitive.
    """

    out = {}
    stack = [(convert_path(where), '', package, only_in_packages)]
    while stack:
        where, prefix, package, only_in_packages = stack.pop(0)
        for name in os.listdir(where):
            fn = os.path.join(where, name)
            if os.path.isdir(fn):
                bad_name = False
                for pattern in exclude_directories:
                    if (fnmatchcase(name, pattern) or fn.lower() == pattern.lower()):
                        bad_name = True
                        if show_ignored:
                            print >> sys.stderr, (
                                'Directory %s ignored by pattern %s'
                                % (fn, pattern))
                        break
                if bad_name:
                    continue
                if (os.path.isfile(os.path.join(fn, '__init__.py')) and not prefix):
                    if not package:
                        new_package = name
                    else:
                        new_package = package + '.' + name
                    stack.append((fn, '', new_package, False))
                else:
                    stack.append(
                        (fn, prefix + name + '/', package, only_in_packages))
            elif package or not only_in_packages:
                # is a file
                bad_name = False
                for pattern in exclude:
                    if (fnmatchcase(name, pattern) or fn.lower() == pattern.lower()):
                        bad_name = True
                        if show_ignored:
                            print >> sys.stderr, (
                                'File %s ignored by pattern %s'
                                % (fn, pattern))
                        break
                if bad_name:
                    continue
                out.setdefault(package, []).append(prefix + name)
    return out

excluded_directories = standard_exclude_directories + ['./requirements', './scripts']
package_data = find_package_data(exclude_directories=excluded_directories)
license_text = open('LICENSE.txt').read()
long_description = open('README.rst').read()

setup(
    name='django-realestate',
    version=__version__,
    url='https://github.com/wm3ndez/realestate',
    author=__maintainer__,
    author_email=__email__,
    license=license_text,
    packages=find_packages(),
    package_data=package_data,
    description="Real Estate app built on Django",
    long_description=long_description,
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Environment :: Web Environment',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Topic :: Office/Business',
        'Framework :: Django'
    ],
    install_requires=[
        'Django>=1.7',
        'Pillow',
        'django-extensions',
        'six',
        'sorl-thumbnail',
        'djangorestframework',
        'django-braces',
        'django-money',
        'django-formtools',
        'redis',
        'django-constance',
        'factory_boy',
        'django-widget-tweaks',
        'django-haystack',
        'elasticsearch',
        'django-filter'
    ],
    test_suite='tests.runtests.runtests',
)


import time
from directKeys import PressKey, ReleaseKey, W, A, S, D, f2

for i in list(range(4))[::-1]:
    print(i+1)
    time.sleep(1)

PressKey(W)
time.sleep(2)
ReleaseKey(W)


#! /usr/bin/env python
from __future__ import print_function

import os
import math

def thermal_wavelength(mass_particle, temp):
    pi = math.pi
    h = 6.6260696e-34 # kg m^2 / s
    k_B = 1.3806488e-23 # kg m^2 / K s^2
    return h / math.sqrt(2 * pi * mass_particle * k_B * temp)

def interparticle_spacing(mass_particle, density):
    return (mass_particle/density)**(1.0/3.0)

density_H2 = 0.09 # kg/m^3
amu = 1.6605389e-27 # kg
mass_H2 = 2*amu
temp_standard = 273 # K
thermal_wavelength_H2 = thermal_wavelength(mass_H2, temp_standard)
interparticle_spacing_H2 = interparticle_spacing(mass_H2, density_H2)

print(thermal_wavelength_H2, 'm')
print(interparticle_spacing_H2, 'm')


"""Preprocessing."""
import re
import logging
import unicodedata

import wordninja

_logger = logging.getLogger(__name__)

def extract_words_from_url(url):
    """Extracts words from a url.

    Example
    -------
    input:  https://www.berkeleyschools.net/departments/public-information-office/
    output: berkeley schools departments public information office
    """
    remove = {
        "www",
        "html",
        "index",
        "htm",
        "http:",
        "https:",
        "http",
        "https",
        "com",
        "ca",
        "gov",
        "org",
        "net",
        "co",
    }
    words = [
        clean_text(w, remove_punctuation=True)
        for w in split(url, [".", "-", "/", "?", "=", "&"])
    ]
    words = [w for word in words for w in wordninja.split(word)]
    words = [
        word
        for word in words
        if word and word not in remove and not word.isnumeric() and len(word) > 2
    ]
    return words

def split(string, delimiters):
    """Split a string using multiple delimiters."""
    regexPattern = "|".join(map(re.escape, delimiters))
    return re.split(regexPattern, string)

def clean_text(text, remove_punctuation=False):
    """Cleans the text of whitespace and control chars."""
    output = []
    for char in text:
        cp = ord(char)
        if cp == 0 or cp == 0xFFFD or _is_control(char):
            continue
        if remove_punctuation and _is_punctuation(char):
            continue
        if _is_whitespace(char):
            if len(output) > 0 and output[-1] == " ":
                continue
            output.append(" ")
        else:
            output.append(char)
    return "".join(output).strip()

def _is_punctuation(char):
    cp = ord(char)
    if (
        (cp >= 33 and cp <= 47)
        or (cp >= 58 and cp <= 64)
        or (cp >= 91 and cp <= 96)
        or (cp >= 123 and cp <= 126)
    ):
        return True
    cat = unicodedata.category(char)
    if cat.startswith("P"):
        return True
    return False

def _is_whitespace(char):
    if char == " " or char == "\t" or char == "\n" or char == "\r":
        return True
    cat = unicodedata.category(char)
    if cat == "Zs":
        return True
    return False

def _is_control(char):
    if char == "\t" or char == "\n" or char == "\r":
        return False
    cat = unicodedata.category(char)
    if cat in ("Cc", "Cf"):
        return True
    return False


import random

correct = 0
incorrect = 0
name = input("Please enter your name: ")
for count in range(10):
    num1 = random.randint(1, 10)
    num2 = random.randint(1, 10)
    symbol = random.choice(["+", "-",])
    print("Please solve:\n", num1, symbol, num2)
    user = int(input(""))

    if symbol == "+":
        answer = num1 + num2
    elif symbol == "-":
        answer = num1 - num2

    if user == answer:
        print("Correct!")
        correct = correct + 1
    else:
        print("Incorrect, the correct answer was", answer)

print(name, ", You Got", correct, "Out Of 10")


from selenium import webdriver
from selenium.webdriver.common.by import By

import numpy as np

# from bs4 import BeautifulSoup

cdPath = "D:\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=cdPath)

# nextCocktailButton = driver.find_element_by_class_name("btn btn-block btn-sm btn-success")
# nextCocktailButton.click()
# .getElementsByClassName("col-12 btn shadow-sm mb-2 text-left")[0].href
# document.querySelector("#swipeme > div:nth-child(3) > div:nth-child(2) > div.row.justify-content-sm-center > div:nth-child(1)")
# driver.find_element(By.CLASS_NAME, "")

cocktailsPages = open("D:\path\Cocktails Surfer\completeCocktailLinks.txt", "r").read().split("\n")

def openPage(url):
    driver.get(url)

def savePage(pageSpecialName):
    # print("yas, save page")

    # driver.find_element(By.CSS_SELECTOR, "head > link:nth-child(42)")[0].setAttribute("href", "css.css?v=20229")
    driver.execute_script("arguments[0].setAttribute('href',arguments[1])", driver.find_element(By.CSS_SELECTOR, "head > link:nth-child(42)"), "css.css?v=20229")
    content = driver.page_source
    # print(content)

    # soup = BeautifulSoup(content)
    file = open("D:\path\Cocktails Surfer\Cocktails Pages\\" + pageSpecialName + ".html", "w", encoding="utf-8")
    file.write(content)

def main():
    for pageLink in cocktailsPages[3346:]:
        openPage(pageLink)
        savePage(pageLink.split("/")[5])

main()

driver.close()


import sys

sys.stdin = open('sample_input.txt')

N = int(input())
numbers = list(map(int,input().split()))
if N == 1: # 개수개 1개 일때의 예외처리를 해줘야한다!
    answer = 1
else:
    cnt_list = []
    cnt = 1
    for idx in range(1,len(numbers)):
        if numbers[idx] - numbers[idx-1] >= 0:
            cnt += 1
        else:
            if cnt == 1:
                continue
            else:
                cnt_list.append(cnt)
                cnt = 1
    if cnt > 1:
        cnt_list.append(cnt)
    cnt = 1
    for idx in range(1,len(numbers)):
        if numbers[idx] - numbers[idx-1] <= 0:
            cnt += 1
        else:
            if cnt == 1:
                continue
            else:
                cnt_list.append(cnt)
                cnt = 1
    if cnt > 1:
        cnt_list.append(cnt)
    if max(cnt_list) > 2:
        answer = max(cnt_list)
    else:
        answer = 2
print(answer)


from digi.xbee.packets.base import XBeeAPIPacket, DictKeys
from digi.xbee.util import utils
from digi.xbee.exception import InvalidOperatingModeException, InvalidPacketException
from digi.xbee.packets.aft import ApiFrameType
from digi.xbee.models.mode import OperatingMode
from digi.xbee.io import IOSample, IOLine
from ipaddress import IPv4Address
from digi.xbee.models.status import ATCommandStatus

class IODataSampleRxIndicatorWifiPacket(XBeeAPIPacket):
    """
    This class represents a IO data sample RX indicator (Wi-Fi) packet. Packet is
    built using the parameters of the constructor or providing a valid API
    payload.

    When the module receives an IO sample frame from a remote device, it sends
    the sample out the UART or SPI using this frame type. Only modules running
    API mode will be able to receive IO samples.

    Among received data, some options can also be received indicating
    transmission parameters.

    .. seealso::
       | :class:`.XBeeAPIPacket`
    """

    __MIN_PACKET_LENGTH = 16

    def __init__(self, source_address, rssi, receive_options, rf_data=None):
        """
        Class constructor. Instantiates a new :class:`.IODataSampleRxIndicatorWifiPacket` object with the
        provided parameters.

        Args:
            source_address (:class:`ipaddress.IPv4Address`): the 64-bit source address.
            rssi (Integer): received signal strength indicator.
            receive_options (Integer): bitfield indicating the receive options.
            rf_data (Bytearray, optional): received RF data. Optional.

        Raises:
            ValueError: if ``rf_data`` is not ``None`` and it's not valid for create an :class:`.IOSample`.

        .. seealso::
           | :class:`.IOSample`
           | :class:`ipaddress.IPv4Address`
           | :class:`.ReceiveOptions`
           | :class:`.XBeeAPIPacket`
        """
        super().__init__(ApiFrameType.IO_DATA_SAMPLE_RX_INDICATOR_WIFI)
        self.__source_address = source_address
        self.__rssi = rssi
        self.__receive_options = receive_options
        self.__rf_data = rf_data
        self.__io_sample = IOSample(rf_data) if rf_data is not None and len(rf_data) >= 5 else None

    @staticmethod
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.IODataSampleRxIndicatorWifiPacket`.

        Raises:
            InvalidPacketException: if the bytearray length is less than 16. (start delim. + length (2 bytes) + frame
                type + source addr. (4 bytes) + rssi + receive options + rf data (5 bytes) + checksum = 16 bytes).
            InvalidPacketException: if the length field of 'raw' is different than its real length. (length field: bytes
                2 and 3)
            InvalidPacketException: if the first byte of 'raw' is not the header byte. See :class:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is not :attr:`.ApiFrameType.IO_DATA_SAMPLE_RX_INDICATOR_WIFI`.
            InvalidOperatingModeException: if ``operating_mode`` is not supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode != OperatingMode.ESCAPED_API_MODE and operating_mode != OperatingMode.API_MODE:
            raise InvalidOperatingModeException(op_mode=operating_mode)

        XBeeAPIPacket._check_api_packet(raw, min_length=IODataSampleRxIndicatorWifiPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.IO_DATA_SAMPLE_RX_INDICATOR_WIFI.code:
            raise InvalidPacketException(message="This packet is not an IO data sample RX indicator Wi-Fi packet.")

        return IODataSampleRxIndicatorWifiPacket(IPv4Address(bytes(raw[4:8])), raw[7], raw[8], rf_data=raw[9:-1])

    def needs_id(self):
        """
        Override method.

        .. seealso::
           | :meth:`.XBeeAPIPacket.needs_id`
        """
        return False

    def _get_api_packet_spec_data(self):
        """
        Override method.

        .. seealso::
           | :meth:`.XBeeAPIPacket._get_api_packet_spec_data`
        """
        ret = bytearray(self.__source_address.packed)
        ret += utils.int_to_bytes(self.__rssi, num_bytes=1)
        ret += utils.int_to_bytes(self.__receive_options, num_bytes=1)
        if self.__rf_data is not None:
            ret += self.__rf_data
        return ret

    def _get_api_packet_spec_data_dict(self):
        """
        Override method.

        .. seealso::
           | :meth:`.XBeeAPIPacket._get_api_packet_spec_data_dict`
        """
        base = {DictKeys.SRC_IPV4_ADDR:   "%s (%s)" % (self.__source_address.packed, self.__source_address.exploded),
                DictKeys.RSSI:            self.__rssi,
                DictKeys.RECEIVE_OPTIONS: self.__receive_options}

        if self.__io_sample is not None:
            base[DictKeys.NUM_SAMPLES] = 1
            base[DictKeys.DIGITAL_MASK] = self.__io_sample.digital_mask
            base[DictKeys.ANALOG_MASK] = self.__io_sample.analog_mask

            # Digital values
            for i in range(16):
                if self.__io_sample.has_digital_value(IOLine.get(i)):
                    base[IOLine.get(i).description + " digital value"] = \
                        self.__io_sample.get_digital_value(IOLine.get(i)).name

            # Analog values
            for i in range(6):
                if self.__io_sample.has_analog_value(IOLine.get(i)):
                    base[IOLine.get(i).description + " analog value"] = \
                        self.__io_sample.get_analog_value(IOLine.get(i))

            # Power supply
            if self.__io_sample.has_power_supply_value():
                base["Power supply value "] = "%02X" % self.__io_sample.power_supply_value

        elif self.__rf_data is not None:
            base[DictKeys.RF_DATA] = utils.hex_to_string(self.__rf_data)

        return base

    def __get_source_address(self):
        """
        Returns the IPv4 address of the source device.

        Returns:
            :class:`ipaddress.IPv4Address`: the IPv4 address of the source device.

        .. seealso::
           | :class:`ipaddress.IPv4Address`
        """
        return self.__source_address

    def __set_source_address(self, source_address):
        """
        Sets the IPv4 source address.

        Args:
            source_address (:class:`ipaddress.IPv4Address`): The new IPv4 source address.

        .. seealso::
           | :class:`ipaddress.IPv4Address`
        """
        if source_address is not None:
            self.__source_address = source_address

    def __get_rssi(self):
        """
        Returns the received Signal Strength Indicator (RSSI).

        Returns:
            Integer: the received Signal Strength Indicator (RSSI).
        """
        return self.__rssi

    def __set_rssi(self, rssi):
        """
        Sets the received Signal Strength Indicator (RSSI).

        Args:
            rssi (Integer): the new received Signal Strength Indicator (RSSI).
        """
        self.__rssi = rssi

    def __get_options(self):
        """
        Returns the receive options bitfield.

        Returns:
            Integer: the receive options bitfield.

        .. seealso::
           | :class:`.ReceiveOptions`
        """
        return self.__receive_options

    def __set_options(self, receive_options):
        """
        Sets the receive options bitfield.

        Args:
            receive_options (Integer): the new receive options bitfield.

        .. seealso::
           | :class:`.ReceiveOptions`
        """
        self.__receive_options = receive_options

    def __get_rf_data(self):
        """
        Returns the received RF data.

        Returns:
            Bytearray: the received RF data.
        """
        if self.__rf_data is None:
            return None
        return self.__rf_data.copy()

    def __set_rf_data(self, rf_data):
        """
        Sets the received RF data.

        Args:
            rf_data (Bytearray): the new received RF data.
        """
        if rf_data is None:
            self.__rf_data = None
        else:
            self.__rf_data = rf_data.copy()

        # Modify the IO sample accordingly
        if rf_data is not None and len(rf_data) >= 5:
            self.__io_sample = IOSample(self.__rf_data)
        else:
            self.__io_sample = None

    def __get_io_sample(self):
        """
        Returns the IO sample corresponding to the data contained in the packet.

        Returns:
            :class:`.IOSample`: the IO sample of the packet, ``None`` if the packet has not any data or if the
                sample could not be generated correctly.

        .. seealso::
           | :class:`.IOSample`
        """
        return self.__io_sample

    def __set_io_sample(self, io_sample):
        """
        Sets the IO sample of the packet.

        Args:
            io_sample (:class:`.IOSample`): the new IO sample to set.

        .. seealso::
           | :class:`.IOSample`
        """
        self.__io_sample = io_sample

    source_address = property(__get_source_address, __set_source_address)
    """:class:`ipaddress.IPv4Address`. IPv4 source address."""

    rssi = property(__get_rssi, __set_rssi)
    """Integer. Received Signal Strength Indicator (RSSI) value."""

    receive_options = property(__get_options, __set_options)
    """Integer. Receive options bitfield."""

    rf_data = property(__get_rf_data, __set_rf_data)
    """Bytearray. Received RF data."""

    io_sample = property(__get_io_sample, __set_io_sample)
    """:class:`.IOSample`: IO sample corresponding to the data contained in the packet."""

class RemoteATCommandWifiPacket(XBeeAPIPacket):
    """
    This class represents a remote AT command request (Wi-Fi) packet. Packet is
    built using the parameters of the constructor or providing a valid API
    payload.

    Used to query or set module parameters on a remote device. For parameter
    changes on the remote device to take effect, changes must be applied, either
    by setting the apply changes options bit, or by sending an ``AC`` command
    to the remote node.

    Remote command options are set as a bitfield.

    If configured, command response is received as a :class:`.RemoteATCommandResponseWifiPacket`.

    .. seealso::
       | :class:`.RemoteATCommandResponseWifiPacket`
       | :class:`.XBeeAPIPacket`
    """

    __MIN_PACKET_LENGTH = 17

    def __init__(self, frame_id, dest_address, transmit_options, command, parameter=None):
        """
        Class constructor. Instantiates a new :class:`.RemoteATCommandWifiPacket` object with the provided parameters.

        Args:
            frame_id (integer): the frame ID of the packet.
            dest_address (:class:`ipaddress.IPv4Address`): the IPv4 address of the destination device.
            transmit_options (Integer): bitfield of supported transmission options.
            command (String): AT command to send.
            parameter (Bytearray, optional): AT command parameter. Optional.

        Raises:
            ValueError: if ``frame_id`` is less than 0 or greater than 255.
            ValueError: if length of ``command`` is different than 2.

        .. seealso::
           | :class:`ipaddress.IPv4Address`
           | :class:`.RemoteATCmdOptions`
           | :class:`.XBeeAPIPacket`
        """
        if frame_id < 0 or frame_id > 255:
            raise ValueError("Frame id must be between 0 and 255.")
        if len(command) != 2:
            raise ValueError("Invalid command " + command)

        super().__init__(ApiFrameType.REMOTE_AT_COMMAND_REQUEST_WIFI)
        self._frame_id = frame_id
        self.__dest_address = dest_address
        self.__transmit_options = transmit_options
        self.__command = command
        self.__parameter = parameter

    @staticmethod
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.RemoteATCommandWifiPacket`

        Raises:
            InvalidPacketException: if the Bytearray length is less than 17. (start delim. + length (2 bytes) + frame
                type + frame id + dest. addr. (8 bytes) + transmit options + command (2  bytes) + checksum = 17 bytes).
            InvalidPacketException: if the length field of 'raw' is different than its real length. (length field: bytes
                2 and 3)
            InvalidPacketException: if the first byte of 'raw' is not the header byte. See :class:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is not :attr:`.ApiFrameType.REMOTE_AT_COMMAND_REQUEST_WIFI`.
            InvalidOperatingModeException: if ``operating_mode`` is not supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode != OperatingMode.ESCAPED_API_MODE and operating_mode != OperatingMode.API_MODE:
            raise InvalidOperatingModeException(op_mode=operating_mode)

        XBeeAPIPacket._check_api_packet(raw, min_length=RemoteATCommandWifiPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.REMOTE_AT_COMMAND_REQUEST_WIFI.code:
            raise InvalidPacketException(message="This packet is not a remote AT command request Wi-Fi packet.")

        return RemoteATCommandWifiPacket(
            raw[4],
            IPv4Address(bytes(raw[9:13])),
            raw[13],
            raw[14:16].decode("utf8"),
            parameter=raw[16:-1]
        )

    def needs_id(self):
        """
        Override method.

        .. seealso::
           | :meth:`.XBeeAPIPacket.needs_id`
        """
        return True

    def _get_api_packet_spec_data(self):
        """
        Override method.

        .. seealso::
           | :meth:`.XBeeAPIPacket._get_api_packet_spec_data`
        """
        ret = bytearray(self.__dest_address.packed)
        ret += utils.int_to_bytes(self.__transmit_options, num_bytes=1)
        ret += bytearray(self.__command, "utf8")
        if self.__parameter is not None:
            ret += self.__parameter
        return ret

    def _get_api_packet_spec_data_dict(self):
        """
        Override method.

        See:
            :meth:`.XBeeAPIPacket._get_api_packet_spec_data_dict`
        """
        return {DictKeys.DEST_IPV4_ADDR:   "%s (%s)" % (self.__dest_address.packed, self.__dest_address.exploded),
                DictKeys.TRANSMIT_OPTIONS: self.__transmit_options,
                DictKeys.COMMAND:          self.__command,
                DictKeys.PARAMETER:        list(self.__parameter) if self.__parameter is not None else None}

    def __get_dest_address(self):
        """
        Returns the IPv4 address of the destination device.

        Returns:
            :class:`ipaddress.IPv4Address`: the IPv4 address of the destination device.

        .. seealso::
           | :class:`ipaddress.IPv4Address`
        """
        return self.__dest_address

    def __set_dest_address(self, dest_address):
        """
        Sets the IPv4 destination address.

        Args:
            dest_address (:class:`ipaddress.IPv4Address`): The new IPv4 destination address.

        .. seealso::
           | :class:`ipaddress.IPv4Address`
        """
        if dest_address is not None:
            self.__dest_address = dest_address

    def __get_transmit_options(self):
        """
        Returns the transmit options bitfield.

        Returns:
            Integer: the transmit options bitfield.

        .. seealso::
           | :class:`.RemoteATCmdOptions`
        """
        return self.__transmit_options

    def __set_transmit_options(self, transmit_options):
        """
        Sets the transmit options bitfield.

        Args:
            transmit_options (Integer): the new transmit options bitfield.

        .. seealso::
           | :class:`.RemoteATCmdOptions`
        """
        self.__transmit_options = transmit_options

    def __get_command(self):
        """
        Returns the AT command.

        Returns:
            String: the AT command.
        """
        return self.__command

    def __set_command(self, command):
        """
        Sets the AT command.

        Args:
            command (String): the new AT command.
        """
        self.__command = command

    def __get_parameter(self):
        """
        Returns the AT command parameter.

        Returns:
            Bytearray: the AT command parameter.
        """
        return self.__parameter

    def __set_parameter(self, parameter):
        """
        Sets the AT command parameter.

        Args:
            parameter (Bytearray): the new AT command parameter.
        """
        self.__parameter = parameter

    dest_address = property(__get_dest_address, __set_dest_address)
    """:class:`ipaddress.IPv4Address`. IPv4 destination address."""

    transmit_options = property(__get_transmit_options, __set_transmit_options)
    """Integer. Transmit options bitfield."""

    command = property(__get_command, __set_command)
    """String. AT command."""

    parameter = property(__get_parameter, __set_parameter)
    """Bytearray. AT command parameter."""

class RemoteATCommandResponseWifiPacket(XBeeAPIPacket):
    """
    This class represents a remote AT command response (Wi-Fi) packet. Packet is
    built using the parameters of the constructor or providing a valid API
    payload.

    If a module receives a remote command response RF data frame in response
    to a Remote AT Command Request, the module will send a Remote AT Command
    Response message out the UART. Some commands may send back multiple frames
    for example, Node Discover (``ND``) command.

    This packet is received in response of a :class:`.RemoteATCommandPacket`.

    Response also includes an :class:`.ATCommandStatus` object with the status
    of the AT command.

    .. seealso::
       | :class:`.RemoteATCommandWifiPacket`
       | :class:`.ATCommandStatus`
       | :class:`.XBeeAPIPacket`
    """

    __MIN_PACKET_LENGTH = 17

    def __init__(self, frame_id, source_address, command, response_status, comm_value=None):
        """
        Class constructor. Instantiates a new :class:`.RemoteATCommandResponseWifiPacket` object with the
        provided parameters.

        Args:
            frame_id (Integer): the frame ID of the packet.
            source_address (:class:`ipaddress.IPv4Address`): the IPv4 address of the source device.
            command (String): the AT command of the packet. Must be a string.
            response_status (:class:`.ATCommandStatus`): the status of the AT command.
            comm_value (Bytearray, optional): the AT command response value.

        Raises:
            ValueError: if ``frame_id`` is less than 0 or greater than 255.
            ValueError: if length of ``command`` is different than 2.

        .. seealso::
           | :class:`.ATCommandStatus`
           | :class:`ipaddress.IPv4Address`
        """
        if frame_id > 255 or frame_id < 0:
            raise ValueError("frame_id must be between 0 and 255.")
        if len(command) != 2:
            raise ValueError("Invalid command " + command)

        super().__init__(ApiFrameType.REMOTE_AT_COMMAND_RESPONSE_WIFI)
        self._frame_id = frame_id
        self.__source_address = source_address
        self.__command = command
        self.__response_status = response_status
        self.__comm_value = comm_value

    @staticmethod
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.RemoteATCommandResponseWifiPacket`.

        Raises:
            InvalidPacketException: if the bytearray length is less than 17. (start delim. + length (2 bytes) + frame
                type + frame id + source addr. (8 bytes) +  command (2 bytes) + receive options + checksum = 17 bytes).
            InvalidPacketException: if the length field of 'raw' is different than its real length. (length field: bytes
                2 and 3)
            InvalidPacketException: if the first byte of 'raw' is not the header byte. See :class:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is not :attr:`.ApiFrameType.REMOTE_AT_COMMAND_RESPONSE_WIFI`.
            InvalidOperatingModeException: if ``operating_mode`` is not supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode != OperatingMode.ESCAPED_API_MODE and operating_mode != OperatingMode.API_MODE:
            raise InvalidOperatingModeException(op_mode=operating_mode)

        XBeeAPIPacket._check_api_packet(raw, min_length=RemoteATCommandResponseWifiPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.REMOTE_AT_COMMAND_RESPONSE_WIFI.code:
            raise InvalidPacketException(message="This packet is not a remote AT command response Wi-Fi packet.")

        return RemoteATCommandResponseWifiPacket(raw[4],
                                                 IPv4Address(bytes(raw[9:13])),
                                                 raw[13:15].decode("utf8"),
                                                 ATCommandStatus.get(raw[15]),
                                                 comm_value=raw[16:-1])

    def needs_id(self):
        """
        Override method.

        .. seealso::
           | :meth:`.XBeeAPIPacket.needs_id`
        """
        return True

    def _get_api_packet_spec_data(self):
        """
        Override method.

        .. seealso::
           | :meth:`.XBeeAPIPacket._get_api_packet_spec_data`
        """
        ret = bytearray(self.__source_address.packed)
        ret += bytearray(self.__command, "utf8")
        ret += utils.int_to_bytes(self.__response_status.code, num_bytes=1)
        if self.__comm_value is not None:
            ret += self.__comm_value
        return ret

    def _get_api_packet_spec_data_dict(self):
        return {DictKeys.SRC_IPV4_ADDR: "%s (%s)" % (self.__source_address.packed, self.__source_address.exploded),
                DictKeys.COMMAND:       self.__command,
                DictKeys.AT_CMD_STATUS: self.__response_status,
                DictKeys.RF_DATA:       list(self.__comm_value) if self.__comm_value is not None else None}

    def __get_source_address(self):
        """
        Returns the IPv4 address of the source device.

        Returns:
            :class:`ipaddress.IPv4Address`: the IPv4 address of the source device.

        .. seealso::
           | :class:`ipaddress.IPv4Address`
        """
        return self.__source_address

    def __set_source_address(self, source_address):
        """
        Sets the IPv4 source address.

        Args:
            source_address (:class:`ipaddress.IPv4Address`): The new IPv4 source address.

        .. seealso::
           | :class:`ipaddress.IPv4Address`
        """
        if source_address is not None:
            self.__source_address = source_address

    def __get_command(self):
        """
        Returns the AT command of the packet.

        Returns:
            String: the AT command of the packet.
        """
        return self.__command

    def __set_command(self, command):
        """
        Sets the AT command of the packet.

        Args:
            command (String): the new AT command of the packet. Must have length = 2.

        Raises:
            ValueError: if length of ``command`` is different than 2.
        """
        if len(command) != 2:
            raise ValueError("Invalid command " + command)
        self.__command = command

    def __get_response_status(self):
        """
        Returns the AT command response status of the packet.

        Returns:
            :class:`.ATCommandStatus`: the AT command response status of the packet.

        .. seealso::
           | :class:`.ATCommandStatus`
        """
        return self.__response_status

    def __set_response_status(self, response_status):
        """
        Sets the AT command response status of the packet

        Args:
            response_status (:class:`.ATCommandStatus`) : the new AT command response status of the packet.

        .. seealso::
           | :class:`.ATCommandStatus`
        """
        self.__response_status = response_status

    def __get_value(self):
        """
        Returns the AT command response value.

        Returns:
            Bytearray: the AT command response value.
        """
        return self.__comm_value

    def __set_value(self, comm_value):
        """
        Sets the AT command response value.

        Args:
            comm_value (Bytearray): the new AT command response value.
        """
        self.__comm_value = comm_value

    source_address = property(__get_source_address, __set_source_address)
    """:class:`ipaddress.IPv4Address`. IPv4 source address."""

    command = property(__get_command, __set_command)
    """String. AT command."""

    status = property(__get_response_status, __set_response_status)
    """:class:`.ATCommandStatus`. AT command response status."""

    command_value = property(__get_value, __set_value)
    """Bytearray. AT command value."""


while True:
    n = int(input('Digite um número para ver sua tabuada: '))

    if n < 0:
        print('Fim do Programa!')
        break

    for c in range(1, 11):
        print(f'{n} x {c} = {n * c}')

#Quando for pra fazer tabuada simples, pode se fazer com while,
#Porém se quiser que o programa refaça a pergunta pra digitar o input infinitas vezes.
#Somente abra o loop infinito, submeta uma opção para que possa dar falso, neste caso if n < 0: Se for falso ele
#printa o fim do programa, mas encerra somente com a função break.
#e invoque a função for i in range pra fazer a tabuada que é muito simples.

Exercicios/Exercício - 067 - Tabuada v3.0.py


def is_one_away(input_string, target):
	if input_string == target:
		return True

	length_difference = len(target) - len(input_string)

	# If the lengths differ by more than 1, then they are more than 1 step away.
	if abs(length_difference) > 1:
		return False

	# If the lengths are equal, then we need to check if a replacement is possible.
	if length_difference == 0:
		return check_character_replacement(input_string, target)

	# If the target is longer, then check if we can insert a character.
	if length_difference == 1:
		return check_character_insertion(input_string, target)

	# Otherwise, the input_string must be longer.
	# In this case, removing a character is the same as performing an insertion on the target string.
	return check_character_insertion(target, input_string)

def check_character_replacement(input_string, target):
	found_difference = False

	for i, character in enumerate(input_string):
		if character != target[i]:
			if found_difference:
				return False

			found_difference = True

	return True

# Return True if short_string can be turned into long_string by inserting a character.
def check_character_insertion(short_string, long_string):
	long_index = -1
	for short_index, short_character in enumerate(short_string):
		long_index += 1
		long_character = long_string[long_index]

		if short_character == long_character:
			continue

		# If the characters are not equal, we increase the long_index.
		# If this happens more than once, then it means multiple characters are different,
		# and an insertion fix is not possible.
		long_index += 1
		if long_index - short_index > 1:
			return False

	return True


import numpy as np
import tensorflow as tf
import scipy.signal
from gym.spaces import Box, Discrete
from net_utils import *
## Just port to Keras

EPS = 1e-8

# def combined_shape(length, shape=None):
#     if shape is None:
#         return (length,)
#     return (length, shape)
def ortho_init(scale=1.0):
    """
    Orthogonal initialization for the policy weights

    :param scale: (float) Scaling factor for the weights.
    :return: (function) an initialization function for the weights
    """

    # _ortho_init(shape, dtype, partition_info=None)
    def _ortho_init(shape, *_, **_kwargs):
        """Intialize weights as Orthogonal matrix.

        Orthogonal matrix initialization [1]_. For n-dimensional shapes where
        n > 2, the n-1 trailing axes are flattened. For convolutional layers, this
        corresponds to the fan-in, so this makes the initialization usable for
        both dense and convolutional layers.

        References
        ----------
        .. [1] Saxe, Andrew M., James L. McClelland, and Surya Ganguli.
               "Exact solutions to the nonlinear dynamics of learning in deep
               linear
        """
        # lasagne ortho init for tf
        shape = tuple(shape)
        if len(shape) == 2:
            flat_shape = shape
        elif len(shape) == 4:  # assumes NHWC
            flat_shape = (np.prod(shape[:-1]), shape[-1])
        else:
            raise NotImplementedError
        gaussian_noise = np.random.normal(0.0, 1.0, flat_shape)
        u, _, v = np.linalg.svd(gaussian_noise, full_matrices=False)
        weights = u if u.shape == flat_shape else v  # pick the one with the correct shape
        weights = weights.reshape(shape)
        return (scale * weights[:shape[0], :shape[1]]).astype(np.float32)

    return _ortho_init

def mlp(x, hidden_sizes=(32,), activation=tf.tanh, output_activation=None):
    for h in hidden_sizes[:-1]:
        x = tf.layers.dense(x, units=h, activation=activation)
    return tf.layers.dense(x, units=hidden_sizes[-1], activation=output_activation)

def get_vars(scope=''):
    return [x for x in tf.trainable_variables() if scope in x.name]

def count_vars(scope=''):
    v = get_vars(scope)
    return sum([np.prod(var.shape.as_list()) for var in v])

def gaussian_likelihood(x, mu, log_std):
    pre_sum = -0.5 * (((x-mu)/(tf.exp(log_std)+EPS))**2 + 2*log_std + np.log(2*np.pi))
    return tf.reduce_sum(pre_sum, axis=1)

def discount_cumsum(x, discount):
    """
    magic from rllab for computing discounted cumulative sums of vectors.

    input:
        vector x,
        [x0,
         x1,
         x2]

    output:
        [x0 + discount * x1 + discount^2 * x2,
         x1 + discount * x2,
         x2]
    """
    return scipy.signal.lfilter([1], [1, float(-discount)], x[::-1], axis=0)[::-1]

"""
Policies
"""

def mlp_categorical_policy(x, a, hidden_sizes, activation, output_activation, action_space):
    act_dim = action_space.n
    logits = mlp(x, list(hidden_sizes)+[act_dim], activation, None)
    logp_all = tf.nn.log_softmax(logits)
    pi = tf.squeeze(tf.multinomial(logits,1), axis=1)
    logp = tf.reduce_sum(tf.one_hot(a, depth=act_dim) * logp_all, axis=1)
    logp_pi = tf.reduce_sum(tf.one_hot(pi, depth=act_dim) * logp_all, axis=1)
    return pi, logp, logp_pi

def mlp_gaussian_policy(x, a, hidden_sizes, activation, output_activation, action_space):

    # how to define action_dimension
    act_dim = (-1,3)

    mu = mlp(x, list(hidden_sizes)+[act_dim], activation, output_activation)
    log_std = tf.get_variable(name='log_std', initializer=-0.5*np.ones(act_dim, dtype=np.float32))
    std = tf.exp(log_std)
    pi = mu + tf.random_normal(tf.shape(mu)) * std
    logp = gaussian_likelihood(a, mu, log_std)
    logp_pi = gaussian_likelihood(pi, mu, log_std)
    return pi, logp, logp_pi

# MUST DEFINE CNN GAUSSIAN POLICY # MUST DEFINE CNN GAUSSIAN POLICY # MUST DEFINE CNN GAUSSIAN POLICY

# cnn feature extractor

def cnn_feature_extractor(img_obs): # let's apply bn , adopted from net_utils
    activ = tf.nn.relu
    layer_1 = activ(conv(img_obs, 'actor_conv1', n_filters=32, filter_size=8, stride=4, init_scale=np.sqrt(2)))
    layer_2 = activ(conv(layer_1, 'actor_conv2', n_filters=64, filter_size=4, stride=2, init_scale=np.sqrt(2)))
    layer_3 = activ(conv(layer_2, 'actor_conv3', n_filters=64, filter_size=3, stride=1, init_scale=np.sqrt(2)))
    layer_4 = conv_to_fc(layer_3)
    return layer_4

# actor
def cnn_gaussian_policy(img_obs=None, act=None):

    # how to define action_dimension
    act_dim = 3
    activ = tf.nn.relu

    feature = cnn_feature_extractor(img_obs)

    mu = tf.layers.dense(feature, 100, name='actor_fc1')
    mu = tf.contrib.layers.layer_norm(mu, center=True, scale=True)
    mu = activ(mu) # relu
    mu = tf.layers.dense(mu, 100, name='actor_fc2')
    mu = tf.contrib.layers.layer_norm(mu, center=True, scale=True)
    mu = activ(mu) # relu
    mu = tf.layers.dense(mu, 3, name='mu')

    log_std = tf.get_variable(name='log_std', initializer=-0.5*np.ones(act_dim, dtype=np.float32))
    std = tf.exp(log_std)
    pi = mu + tf.random_normal(tf.shape(mu)) * std
    logp = gaussian_likelihood(act, mu, log_std)
    logp_pi = gaussian_likelihood(pi, mu, log_std)
    return pi, logp, logp_pi

# critic
def mlp_critic(full_stt):
    activation = 'relu'
    output_activation = 'linear'
    activ = tf.nn.relu

    val = tf.layers.flatten(full_stt)
    val = tf.layers.dense(val, units=100, name='critic_fc1')
    val = tf.contrib.layers.layer_norm(val, center=True, scale=True)
    val = activ(val) # relu
    val = tf.layers.dense(val, units=100, name='critic_fc2')
    val = tf.contrib.layers.layer_norm(val, center=True, scale=True)
    val = activ(val) # relu
    val = tf.layers.dense(val, units=100, name='critic_fc3')
    val = tf.contrib.layers.layer_norm(val, center=True, scale=True)
    val = activ(val) # relu

    return tf.layers.dense(val, units=1, activation=None)

def ppo_actor_critic(obs, stt, act):

    # default policy builder depends on action space

    policy = cnn_gaussian_policy

    with tf.variable_scope('pi'): # CNN feature extractor + mlp gaussian policy
        pi, logp, logp_pi = policy(obs, act)
    # policy # policy # policy # policy # policy

    with tf.variable_scope('v'):
        val = tf.squeeze(mlp_critic(stt))

    # value # value # value # value # value
    return pi, logp, logp_pi, val

# MUST DEFINE CNN GAUSSIAN POLICY # MUST DEFINE CNN GAUSSIAN POLICY # MUST DEFINE CNN GAUSSIAN POLICY

"""
Actor-Critics
"""
def mlp_actor_critic(x, a, hidden_sizes=(64,64), activation=tf.tanh,
                     output_activation=None, policy=None, action_space=None):

    # default policy builder depends on action space
    if policy is None and isinstance(action_space, Box):
        policy = mlp_gaussian_policy
    elif policy is None and isinstance(action_space, Discrete):
        policy = mlp_categorical_policy

    with tf.variable_scope('pi'):
        pi, logp, logp_pi = policy(x, a, hidden_sizes, activation, output_activation, action_space)
    with tf.variable_scope('v'):
        v = tf.squeeze(mlp(x, list(hidden_sizes)+[1], activation, None), axis=1)
    return pi, logp, logp_pi, v

    # waht we should return?


from setuptools import setup, find_packages

requires = [
    'flask',
    'flask-cors',
    'scikit-learn',
    'gensim',
    'rake_nltk'
]

setup(
    name='backend',
    version='0.0',
    description='Generic Clustering Application',
    author='Konstantin Bogdanoski',
    author_email='konstantin.b@live.com',
    keywords='web flask finki fcse Konstantin-Bogdanoski',
    packages=find_packages(),
    include_package_data=True,
    install_requires=requires
)


# coding=utf-8

from django.conf.urls import url

from contrato import views

urlpatterns = [
    url(r'^carrinho/adicionar/(?P<slug>[\w_-]+)', views.create_cartitem, name='create_cartitem'),
    url(r'^carrinho/', views.cart_item, name='cart_item'),
    url(r'^finalizando/$', views.checkout, name='checkout'),

    url(r'^meus-contratos/$', views.lista_contrato, name='lista_contrato'),
    url(r'^detalhe-contratos/(?P<pk>\d+)/$', views.detalhe_contrato, name='detalhe_contrato'),

    #PagSeguro
    url(
        r'^finalizando/(?P<pk>\d+)/pagseguro/$', views.pagseguro_view,
        name='pagseguro_view'
    ),
    url(
        r'^notificacoes/pagseguro/$', views.pagseguro_notification,
        name='pagseguro_notification'
    ),

    #Paypal
    url(r'^finalizando/(?P<pk>\d+)/paypal/$', views.paypal_view,name='paypal_view'),
]


# Databricks notebook source
# DBTITLE 1,Imports
from datetime import date
import time
from pyspark.sql.types import *
from pyspark.sql.functions import *
from delta.tables import *

# COMMAND ----------

# DBTITLE 1,Define variables
# Date variables
current_year = date.today().year
current_month = "0" + str(date.today().month) if len(str(date.today().month)) == 1 else date.today().month
current_day = "0" + str(date.today().day) if len(str(date.today().day)) == 1 else date.today().day

# Base location variables
location_prefix = "/dbfs"
main_path = "/mnt/adlslirkov/it-job-boards/DEV.bg/base/"
posts_path = f"posts/{current_year}/{current_month}/{current_day}/"

# COMMAND ----------

# DBTITLE 1,Create Temp Staging Table
# Read Base
df_jobposts = spark.read.format("parquet").load(main_path + posts_path)

# Create the Source Data Frame
sourceDF = df_jobposts
sourceDF.display()
print("Count: {}".format(sourceDF.count()))

# COMMAND ----------

# MAGIC %md
# MAGIC ### Create Database and Tables

# COMMAND ----------

# DBTITLE 1,Create Database
# MAGIC %sql
# MAGIC
# MAGIC -- CREATE DATABASE IF NOT EXISTS jobposts_devbg
# MAGIC -- COMMENT 'This database holds job posts data coming from Dev.bg'
# MAGIC -- LOCATION '/mnt/adlslirkov/it-job-boards/DEV.bg/delta/'

# COMMAND ----------

# DBTITLE 1,Add SCD Type 2 Columns to Delta Table
# # This command has been ran just once, when the delta table was first created.

# df_jobposts = (
#     df_jobposts
#     .withColumn("IsActive", lit(True))
#     .withColumn("StartDate", date_format(current_timestamp(), "yyyy-MM-dd HH:mm:ss"))
#     .withColumn("EndDate", lit(None).cast(StringType()))
# )

# COMMAND ----------

# DBTITLE 1,Create Delta Table
# # This command has been ran just once, when the delta table was first created.

# df_jobposts.write.format("delta").saveAsTable("jobposts_devbg.posts")

# COMMAND ----------

# DBTITLE 1,Add Delta Table Constraint
# MAGIC %sql
# MAGIC
# MAGIC -- ALTER TABLE jobposts_devbg.posts ADD CONSTRAINT LinkAndDepartmentNotNull CHECK (Link IS NOT NULL AND Department IS NOT NULL);

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC SELECT COUNT(*) FROM jobposts_devbg.posts WHERE IsActive = True
# MAGIC -- DROP TABLE jobposts_devbg.posts

# COMMAND ----------

# DBTITLE 1,Create Delta Table Instance
deltaPosts = DeltaTable.forPath(spark, "/mnt/adlslirkov/it-job-boards/DEV.bg/delta/posts")

targetDF = deltaPosts.toDF()
targetDF.display()

# COMMAND ----------

# DBTITLE 1,Join source and target
# Rename columns in targetDF, so that we don't need to manually alias them in the join below.
# Since this code will be used for DataFrames with different number of columns and column names, this is the approach that we need to take.
# targetDF = targetDF.toDF(*["target_" + column for column in targetDF.columns])

targetDF = targetDF.filter(col("IsActive") == True).select(*[col for col in targetDF.columns if col not in ["IsActive", "StartDate", "EndDate"]])

joinDF = (
    sourceDF
    .join(
        targetDF,
        (sourceDF.Link == targetDF.Link)
        & (sourceDF.Department == targetDF.Department),
#         & (targetDF.IsActive == "true"),
        "outer"
    )
    .select(
        sourceDF["*"],
        targetDF.Company.alias("target_Company"),
        targetDF.Department.alias("target_Department"),
        targetDF.Link.alias("target_Link"),
        targetDF.Location.alias("target_Location"),
        targetDF.Salary.alias("target_Salary"),
        targetDF.Title.alias("target_Title"),
        targetDF.Uploaded.alias("target_Uploaded"),
        targetDF.Source.alias("target_Source"),
        targetDF.IngestionDate.alias("target_IngestionDate")
    )
)

joinDF.display()

# COMMAND ----------

# DBTITLE 1,Hash source and target columns and compare them
filterDF = joinDF.filter(xxhash64("Company", "Department", "Link", "Location", "Salary", "Title", "Uploaded", "Source") != xxhash64("target_Company", "target_Department", "target_Link", "target_Location", "target_Salary", "target_Title", "target_Uploaded", "target_Source")).withColumn("MergeKey", concat("target_Link", "target_Department"))

filterDF.display()

# COMMAND ----------

# DBTITLE 1,Add MergeKey and set it to null where Id is not null
dummyDF = filterDF.filter((col("target_Link").isNotNull()) & (col("target_Department").isNotNull())).withColumn("MergeKey", lit(None))

dummyDF.display()

# COMMAND ----------

# DBTITLE 1,Union DFs
scdDF = filterDF.union(dummyDF)

scdDF.display()

# COMMAND ----------

# DBTITLE 1,Create Dictionary which will be used in the Merge Command
columns_dict = {col: "source." + col for col in df_jobposts.columns}
columns_dict["IsActive"] = "'True'"
columns_dict["StartDate"] = "current_timestamp"
# columns_dict["EndDate"] = """to_date('9999-12-31 00:00:00.0000', 'MM-dd-yyyy HH:mm:ss')"""

columns_dict

# COMMAND ----------

# DBTITLE 1,Merge
(deltaPosts.alias("target")
 .merge(
     scdDF.alias("source"),
     condition = "concat(target.Link, target.Department) = source.MergeKey"
 )
 .whenMatchedUpdate(set =
    {
        "Source": "'Dev.bg'",
        "IsActive": "'False'",
        "EndDate": "date_format(current_timestamp(), 'yyyy-MM-dd HH:mm:ss')"
    }
 )
 .whenNotMatchedInsert(
     condition = "source.Link IS NOT NULL AND source.Department IS NOT NULL",
     values =
        {
             'company': 'source.Company',
             'department': 'source.Department',
             'link': 'source.Link',
             'location': 'source.Location',
             'salary': 'source.Salary',
             'title': 'source.Title',
             'uploaded': 'source.Uploaded',
             'Source': "'Dev.bg'",
             'IngestionDate': 'source.IngestionDate',
             'IsActive': "'True'",
             'StartDate': 'current_timestamp'
        }
 )
 .execute()
)

# COMMAND ----------

# DBTITLE 1,Check Delta Table History
deltaPosts.history().display()

# COMMAND ----------

# DBTITLE 1,Compare Delta Table records with records in the Source DataFrame
# Read delta table into DataFrame
deltaFinalPosts = DeltaTable.forPath(spark, "/mnt/adlslirkov/it-job-boards/DEV.bg/delta/posts")
finalTargetDF = deltaFinalPosts.toDF()

# Raise error if there are records in the delta table (when filtered to show only active records), which do not exists in the source DataFrame
targetExceptSourceCount = finalTargetDF.where(col("IsActive") == True).select(concat("Link", "Department")).exceptAll(sourceDF.select(concat("Link", "Department"))).count()
targetEqualsSourceCount = finalTargetDF.where(col("IsActive") == True).count() == sourceDF.count()

if targetExceptSourceCount > 0 or targetEqualsSourceCount == False:
    raise Exception("There are records in source, which do not exist in target.")

Bulgarian IT Job Boards/Base to Delta Notebooks/Dev.bg/B2D Posts.py


import cv2
import numpy as np

from utils import get_four_points

image =cv2.imread('data/images/book1.jpg')

# cv2.imshow('original',image)

point_src=get_four_points(image)
print(point_src)

#변환할 비어있는 이미지를 먼저 하나 만든다.
dst_size=(300,400,3)

image_dst=np.zeros(dst_size,np.uint8)

#이제, 아까했던 14번 파일처럼!
#이미지는 2개 다 준비됐다
#14번 파일을 참고하여, 실습1번 문제를 해결하세요

#힌트,image의 4개의 점은 마우스클릭으로 해결완료!
#1.image_dst의 4개의 점은 여러분들이 구해서
point_dst=np.array([0,0,300,0,300,400,0,400])
point_dst=point_dst.reshape(4,2)
#2.행렬 구하고
matrix,status=cv2.findHomography(point_src,point_dst)

#3.변환함수를 통해 ,이미지를 가져오세요.
# 새로운 이미지의 사이즈는 x=400,y=300
#파라미터 셋팅해서 이미지 가져오세요
image_dst=cv2.warpPerspective(image,matrix,(300,400))
cv2.imshow('dst',image_dst)
# #원본 이미지의 4개의 점의 좌표!

# point_original=np.array([141,131,480,159,493,630,64,601],dtype=float)
# point_original=point_original.reshape(4,2)

# print(point_original)

# #다른 이미지 불러와서 ,
# #위의 원본 이미지 4개의 점의 좌표와 매칭되는 점의 좌표도 셋팅
# image_dst=cv2.imread('data/images/book1.jpg')

# cv2.imshow('dst',image_dst)
# point_dst=np.array([318,256,534,372,316,670,73,473],dtype=float)

# point_dst=point_dst.reshape(4,2)
# print(point_dst)

# # #두 이미지의 서로 매칭되는 4개의점이 있으니깐
# # # 이 점들의 정보를 가지고 행렬을 구할 수 있다.
# # matrix,status=cv2.findHomography(point_original,point_dst)

# # #행렬을 구했으면 이미지를 처리할 수 있다.
# # result=cv2.warpPerspective(image,matrix,(image.shape[1],image.shape[0]))

# # cv2.imshow('Warp',result)

cv2.waitKey(0)
cv2.destroyAllWindows()


from PIL import Image
import numpy as np
from TextureOverlayer import add_overlay, add_overlay_with_inverted_mask
import os.path

'''
    Functions for loading, formating and overlapping
    Optifine formated PBR texture maps
    but Juan S. Marquerie
'''

# Indexes for the PBR format
COLOR = 0
NORMALS = 1
SPECULAR = 2

'''
    Main overlapping function for PBR formated images
'''
def PBR_batch_overlaping(imgs, overlays):
    # We get the mask from the overlay's specular's alpha channel
    spec_overlay_mask = overlays[SPECULAR, :, :, 3] / 255.0
    color_overlay_mask = overlays[COLOR, :, :, 3] / 255.0
    results = np.zeros(imgs.shape)

    results[SPECULAR ,:,:,:] = add_overlay_with_inverted_mask(imgs[SPECULAR ,:,:,:], overlays[SPECULAR, :,:,:], spec_overlay_mask)
    results[COLOR ,:,:,:] = add_overlay_with_inverted_mask(imgs[COLOR ,:,:,:], overlays[COLOR, :,:,:], color_overlay_mask, True)
    results[NORMALS ,:,:,:] = add_overlay_with_inverted_mask(imgs[NORMALS ,:,:,:], overlays[NORMALS, :,:,:], color_overlay_mask)

    return results

'''
    Loads the Optifine friendly texture maps (of differenti images)
    and concatenates them into a single 4-N dimensional matrix
'''
def load_PBR_images(base_name, img_format='.png'):
    # Load the texture maps
    color_map = np.asarray(Image.open(base_name + img_format).convert('RGBA'))
    specular_map = np.asarray(Image.open(base_name + '_s' + img_format).convert('RGBA'))
    normals_map = np.asarray(Image.open(base_name + '_n' + img_format).convert('RGBA'))

    c_w, c_h, c_c = color_map.shape

    # Allocate space and fill it
    pbr_images = np.zeros((3, c_w, c_h, c_c))

    pbr_images[COLOR, :, :, :] = color_map
    pbr_images[SPECULAR, :, :, :] = specular_map
    pbr_images[NORMALS, :, :, :] = normals_map

    return pbr_images

'''
    Saves a PBR matrix, in the Optifine friendly format
'''
def save_PBR_images(pbr_img, name, img_format='.png'):
    Image.fromarray(pbr_img[COLOR].astype(np.uint8)).save(name + img_format)
    Image.fromarray(pbr_img[SPECULAR].astype(np.uint8)).save(name + '_s' + img_format)
    Image.fromarray(pbr_img[NORMALS].astype(np.uint8)).save(name + '_n' + img_format)

'''
    For testing:
'''
if __name__ == '__main__':
    names = ['diamond_ore']

    for name in names:
        overlay_img = load_PBR_images(os.path.join('diamond_ore', name))

        for e_i, i in enumerate(range(4)):
            for e_j, j in enumerate(range(4)):
                index = str((e_i * 16) + (e_j))
                base_img = load_PBR_images(os.path.join('stone_res', index))

                merged = PBR_batch_overlaping(base_img, overlay_img)

                save_PBR_images(merged,os.path.join('res', name, index))