text:
from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['stableinterface'],
                    'supported_by': 'core'}

DOCUMENTATION = '''
---
module: dnf
version_added: 1.9
short_description: Manages packages with the I(dnf) package manager
description:
     - Installs, upgrade, removes, and lists packages and groups with the I(dnf) package manager.
options:
  name:
    description:
      - "A package name or package specifier with version, like C(name-1.0).
        When using state=latest, this can be '*' which means run: dnf -y update.
        You can also pass a url or a local path to a rpm file.
        To operate on several packages this can accept a comma separated string of packages or a list of packages."
    required: true
    aliases:
        - pkg

  list:
    description:
      - Various (non-idempotent) commands for usage with C(/usr/bin/ansible) and I(not) playbooks. See examples.

  state:
    description:
      - Whether to install (C(present), C(latest)), or remove (C(absent)) a package.
      - Default is C(None), however in effect the default action is C(present) unless the C(autoremove) option is
        enabled for this module, then C(absent) is inferred.
    choices: ['absent', 'present', 'installed', 'removed', 'latest']

  enablerepo:
    description:
      - I(Repoid) of repositories to enable for the install/update operation.
        These repos will not persist beyond the transaction.
        When specifying multiple repos, separate them with a ",".

  disablerepo:
    description:
      - I(Repoid) of repositories to disable for the install/update operation.
        These repos will not persist beyond the transaction.
        When specifying multiple repos, separate them with a ",".

  conf_file:
    description:
      - The remote dnf configuration file to use for the transaction.

  disable_gpg_check:
    description:
      - Whether to disable the GPG checking of signatures of packages being
        installed. Has an effect only if state is I(present) or I(latest).
    type: bool
    default: 'no'

  installroot:
    description:
      - Specifies an alternative installroot, relative to which all packages
        will be installed.
    version_added: "2.3"
    default: "/"

  releasever:
    description:
      - Specifies an alternative release from which all packages will be
        installed.
    version_added: "2.6"

  autoremove:
    description:
      - If C(yes), removes all "leaf" packages from the system that were originally
        installed as dependencies of user-installed packages but which are no longer
        required by any such package. Should be used alone or when state is I(absent)
    type: bool
    default: "no"
    version_added: "2.4"
  exclude:
    description:
      - Package name(s) to exclude when state=present, or latest. This can be a
        list or a comma separated string.
    version_added: "2.7"
  skip_broken:
    description:
      - Skip packages with broken dependencies(devsolve) and are causing problems.
    type: bool
    default: "no"
    version_added: "2.7"
  update_cache:
    description:
      - Force dnf to check if cache is out of date and redownload if needed.
        Has an effect only if state is I(present) or I(latest).
    type: bool
    default: "no"
    aliases: [ expire-cache ]
    version_added: "2.7"
  update_only:
    description:
      - When using latest, only update installed packages. Do not install packages.
      - Has an effect only if state is I(latest)
    default: "no"
    type: bool
    version_added: "2.7"
  security:
    description:
      - If set to C(yes), and C(state=latest) then only installs updates that have been marked security related.
    type: bool
    default: "no"
    version_added: "2.7"
  bugfix:
    description:
      - If set to C(yes), and C(state=latest) then only installs updates that have been marked bugfix related.
    default: "no"
    type: bool
    version_added: "2.7"
  enable_plugin:
    description:
      - I(Plugin) name to enable for the install/update operation.
        The enabled plugin will not persist beyond the transaction.
    version_added: "2.7"
  disable_plugin:
    description:
      - I(Plugin) name to disable for the install/update operation.
        The disabled plugins will not persist beyond the transaction.
    version_added: "2.7"
  disable_excludes:
    description:
      - Disable the excludes defined in DNF config files.
      - If set to C(all), disables all excludes.
      - If set to C(main), disable excludes defined in [main] in dnf.conf.
      - If set to C(repoid), disable excludes defined for given repo id.
    version_added: "2.7"
  validate_certs:
    description:
      - This only applies if using a https url as the source of the rpm. e.g. for localinstall. If set to C(no), the SSL certificates will not be validated.
      - This should only set to C(no) used on personally controlled sites using self-signed certificates as it avoids verifying the source site.
    type: bool
    default: "yes"
    version_added: "2.7"
  allow_downgrade:
    description:
      - Specify if the named package and version is allowed to downgrade
        a maybe already installed higher version of that package.
        Note that setting allow_downgrade=True can make this module
        behave in a non-idempotent way. The task could end up with a set
        of packages that does not match the complete list of specified
        packages to install (because dependencies between the downgraded
        package and others can cause changes to the packages which were
        in the earlier transaction).
    type: bool
    default: "no"
    version_added: "2.7"
  install_repoquery:
    description:
      - This is effectively a no-op in DNF as it is not needed with DNF, but is an accepted parameter for feature
        parity/compatibility with the I(yum) module.
    type: bool
    default: "yes"
    version_added: "2.7"
  download_only:
    description:
      - Only download the packages, do not install them.
    default: "no"
    type: bool
    version_added: "2.7"
  lock_timeout:
    description:
      - Amount of time to wait for the dnf lockfile to be freed.
    required: false
    default: 30
    type: int
    version_added: "2.8"
  install_weak_deps:
    description:
      - Will also install all packages linked by a weak dependency relation.
    type: bool
    default: "yes"
    version_added: "2.8"
  download_dir:
    description:
      - Specifies an alternate directory to store packages.
      - Has an effect only if I(download_only) is specified.
    type: str
    version_added: "2.8"
notes:
  - When used with a `loop:` each package will be processed individually, it is much more efficient to pass the list directly to the `name` option.
  - Group removal doesn't work if the group was installed with Ansible because
    upstream dnf's API doesn't properly mark groups as installed, therefore upon
    removal the module is unable to detect that the group is installed
    (https://bugzilla.redhat.com/show_bug.cgi?id=1620324)
requirements:
  - "python >= 2.6"
  - python-dnf
  - for the autoremove option you need dnf >= 2.0.1"
author:
  - Igor Gnatenko (@ignatenkobrain) <i.gnatenko.brain@gmail.com>
  - Cristian van Ee (@DJMuggs) <cristian at cvee.org>
  - Berend De Schouwer (@berenddeschouwer)
  - Adam Miller (@maxamillion) <admiller@redhat.com>
'''

EXAMPLES = '''
- name: install the latest version of Apache
  dnf:
    name: httpd
    state: latest

- name: install the latest version of Apache and MariaDB
  dnf:
    name:
      - httpd
      - mariadb-server
    state: latest

- name: remove the Apache package
  dnf:
    name: httpd
    state: absent

- name: install the latest version of Apache from the testing repo
  dnf:
    name: httpd
    enablerepo: testing
    state: present

- name: upgrade all packages
  dnf:
    name: "*"
    state: latest

- name: install the nginx rpm from a remote repo
  dnf:
    name: 'http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm'
    state: present

- name: install nginx rpm from a local file
  dnf:
    name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
    state: present

- name: install the 'Development tools' package group
  dnf:
    name: '@Development tools'
    state: present

- name: Autoremove unneeded packages installed as dependencies
  dnf:
    autoremove: yes

- name: Uninstall httpd but keep its dependencies
  dnf:
    name: httpd
    state: absent
    autoremove: no

- name: install a modularity appstream with defined stream and profile
  dnf:
    name: '@postgresql:9.6/client'
    state: present

- name: install a modularity appstream with defined stream
  dnf:
    name: '@postgresql:9.6'
    state: present

- name: install a modularity appstream with defined profile
  dnf:
    name: '@postgresql/client'
    state: present
'''

import os
import re
import sys
import tempfile

try:
    import dnf
    import dnf.cli
    import dnf.const
    import dnf.exceptions
    import dnf.subject
    import dnf.util
    HAS_DNF = True
except ImportError:
    HAS_DNF = False

from ansible.module_utils._text import to_native, to_text
from ansible.module_utils.urls import fetch_url
from ansible.module_utils.six import PY2, text_type
from distutils.version import LooseVersion

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.yumdnf import YumDnf, yumdnf_argument_spec

# 64k.  Number of bytes to read at a time when manually downloading pkgs via a url
BUFSIZE = 65536

class DnfModule(YumDnf):
    """
    DNF Ansible module back-end implementation
    """

    def __init__(self, module):
        # This populates instance vars for all argument spec params
        super(DnfModule, self).__init__(module)

        self._ensure_dnf()
        self.lockfile = "/var/cache/dnf/*_lock.pid"
        self.pkg_mgr_name = "dnf"

        try:
            self.with_modules = dnf.base.WITH_MODULES
        except AttributeError:
            self.with_modules = False

    def is_lockfile_pid_valid(self):
        # FIXME? it looks like DNF takes care of invalid lock files itself?
        # https://github.com/ansible/ansible/issues/57189
        return True

    def _sanitize_dnf_error_msg_install(self, spec, error):
        """
        For unhandled dnf.exceptions.Error scenarios, there are certain error
        messages we want to filter in an install scenario. Do that here.
        """
        if to_text("no package matched") in to_text(error):
            return "No package {0} available.".format(spec)

        return error

    def _sanitize_dnf_error_msg_remove(self, spec, error):
        """
        For unhandled dnf.exceptions.Error scenarios, there are certain error
        messages we want to ignore in a removal scenario as known benign
        failures. Do that here.
        """
        if 'no package matched' in to_native(error):
            return (False, "{0} is not installed".format(spec))

        # Return value is tuple of:
        #   ("Is this actually a failure?", "Error Message")
        return (True, error)

    def _package_dict(self, package):
        """Return a dictionary of information for the package."""
        # NOTE: This no longer contains the 'dnfstate' field because it is
        # already known based on the query type.
        result = {
            'name': package.name,
            'arch': package.arch,
            'epoch': str(package.epoch),
            'release': package.release,
            'version': package.version,
            'repo': package.repoid}
        result['nevra'] = '{epoch}:{name}-{version}-{release}.{arch}'.format(
            **result)

        if package.installtime == 0:
            result['yumstate'] = 'available'
        else:
            result['yumstate'] = 'installed'

        return result

    def _packagename_dict(self, packagename):
        """
        Return a dictionary of information for a package name string or None
        if the package name doesn't contain at least all NVR elements
        """

        if packagename[-4:] == '.rpm':
            packagename = packagename[:-4]

        # This list was auto generated on a Fedora 28 system with the following one-liner
        #   printf '[ '; for arch in $(ls /usr/lib/rpm/platform); do  printf '"%s", ' ${arch%-linux}; done; printf ']\n'
        redhat_rpm_arches = [
            "aarch64", "alphaev56", "alphaev5", "alphaev67", "alphaev6", "alpha",
            "alphapca56", "amd64", "armv3l", "armv4b", "armv4l", "armv5tejl", "armv5tel",
            "armv5tl", "armv6hl", "armv6l", "armv7hl", "armv7hnl", "armv7l", "athlon",
            "geode", "i386", "i486", "i586", "i686", "ia32e", "ia64", "m68k", "mips64el",
            "mips64", "mips64r6el", "mips64r6", "mipsel", "mips", "mipsr6el", "mipsr6",
            "noarch", "pentium3", "pentium4", "ppc32dy4", "ppc64iseries", "ppc64le", "ppc64",
            "ppc64p7", "ppc64pseries", "ppc8260", "ppc8560", "ppciseries", "ppc", "ppcpseries",
            "riscv64", "s390", "s390x", "sh3", "sh4a", "sh4", "sh", "sparc64", "sparc64v",
            "sparc", "sparcv8", "sparcv9", "sparcv9v", "x86_64"
        ]

        rpm_arch_re = re.compile(r'(.*)\.(.*)')
        rpm_nevr_re = re.compile(r'(\S+)-(?:(\d*):)?(.*)-(~?\w+[\w.+]*)')
        try:
            arch = None
            rpm_arch_match = rpm_arch_re.match(packagename)
            if rpm_arch_match:
                nevr, arch = rpm_arch_match.groups()
                if arch in redhat_rpm_arches:
                    packagename = nevr
            rpm_nevr_match = rpm_nevr_re.match(packagename)
            if rpm_nevr_match:
                name, epoch, version, release = rpm_nevr_re.match(packagename).groups()
                if not version or not version.split('.')[0].isdigit():
                    return None
            else:
                return None
        except AttributeError as e:
            self.module.fail_json(
                msg='Error attempting to parse package: %s, %s' % (packagename, to_native(e)),
                rc=1,
                results=[]
            )

        if not epoch:
            epoch = "0"

        if ':' in name:
            epoch_name = name.split(":")

            epoch = epoch_name[0]
            name = ''.join(epoch_name[1:])

        result = {
            'name': name,
            'epoch': epoch,
            'release': release,
            'version': version,
        }

        return result

    # Original implementation from yum.rpmUtils.miscutils (GPLv2+)
    #   http://yum.baseurl.org/gitweb?p=yum.git;a=blob;f=rpmUtils/miscutils.py
    def _compare_evr(self, e1, v1, r1, e2, v2, r2):
        # return 1: a is newer than b
        # 0: a and b are the same version
        # -1: b is newer than a
        if e1 is None:
            e1 = '0'
        else:
            e1 = str(e1)
        v1 = str(v1)
        r1 = str(r1)
        if e2 is None:
            e2 = '0'
        else:
            e2 = str(e2)
        v2 = str(v2)
        r2 = str(r2)
        # print '%s, %s, %s vs %s, %s, %s' % (e1, v1, r1, e2, v2, r2)
        rc = dnf.rpm.rpm.labelCompare((e1, v1, r1), (e2, v2, r2))
        # print '%s, %s, %s vs %s, %s, %s = %s' % (e1, v1, r1, e2, v2, r2, rc)
        return rc

    def fetch_rpm_from_url(self, spec):
        # FIXME: Remove this once this PR is merged:
        #   https://github.com/ansible/ansible/pull/19172

        # download package so that we can query it
        package_name, dummy = os.path.splitext(str(spec.rsplit('/', 1)[1]))
        package_file = tempfile.NamedTemporaryFile(dir=self.module.tmpdir, prefix=package_name, suffix='.rpm', delete=False)
        self.module.add_cleanup_file(package_file.name)
        try:
            rsp, info = fetch_url(self.module, spec)
            if not rsp:
                self.module.fail_json(
                    msg="Failure downloading %s, %s" % (spec, info['msg']),
                    results=[],
                )
            data = rsp.read(BUFSIZE)
            while data:
                package_file.write(data)
                data = rsp.read(BUFSIZE)
            package_file.close()
        except Exception as e:
            self.module.fail_json(
                msg="Failure downloading %s, %s" % (spec, to_native(e)),
                results=[],
            )

        return package_file.name

    def _ensure_dnf(self):
        if not HAS_DNF:
            if PY2:
                package = 'python2-dnf'
            else:
                package = 'python3-dnf'

            if self.module.check_mode:
                self.module.fail_json(
                    msg="`{0}` is not installed, but it is required"
                    "for the Ansible dnf module.".format(package),
                    results=[],
                )

            rc, stdout, stderr = self.module.run_command(['dnf', 'install', '-y', package])
            global dnf
            try:
                import dnf
                import dnf.cli
                import dnf.const
                import dnf.exceptions
                import dnf.subject
                import dnf.util
            except ImportError:
                self.module.fail_json(
                    msg="Could not import the dnf python module using {0} ({1}). "
                        "Please install `{2}` package or ensure you have specified the "
                        "correct ansible_python_interpreter.".format(sys.executable, sys.version.replace('\n', ''),
                                                                     package),
                    results=[],
                    cmd='dnf install -y {0}'.format(package),
                    rc=rc,
                    stdout=stdout,
                    stderr=stderr,
                )

    def _configure_base(self, base, conf_file, disable_gpg_check, installroot='/'):
        """Configure the dnf Base object."""

        conf = base.conf

        # Change the configuration file path if provided, this must be done before conf.read() is called
        if conf_file:
            # Fail if we can't read the configuration file.
            if not os.access(conf_file, os.R_OK):
                self.module.fail_json(
                    msg="cannot read configuration file", conf_file=conf_file,
                    results=[],
                )
            else:
                conf.config_file_path = conf_file

        # Read the configuration file
        conf.read()

        # Turn off debug messages in the output
        conf.debuglevel = 0

        # Set whether to check gpg signatures
        conf.gpgcheck = not disable_gpg_check
        conf.localpkg_gpgcheck = not disable_gpg_check

        # Don't prompt for user confirmations
        conf.assumeyes = True

        # Set installroot
        conf.installroot = installroot

        # Load substitutions from the filesystem
        conf.substitutions.update_from_etc(installroot)

        # Handle different DNF versions immutable mutable datatypes and
        # dnf v1/v2/v3
        #
        # In DNF < 3.0 are lists, and modifying them works
        # In DNF >= 3.0 < 3.6 are lists, but modifying them doesn't work
        # In DNF >= 3.6 have been turned into tuples, to communicate that modifying them doesn't work
        #
        # https://www.happyassassin.net/2018/06/27/adams-debugging-adventures-the-immutable-mutable-object/
        #
        # Set excludes
        if self.exclude:
            _excludes = list(conf.exclude)
            _excludes.extend(self.exclude)
            conf.exclude = _excludes
        # Set disable_excludes
        if self.disable_excludes:
            _disable_excludes = list(conf.disable_excludes)
            if self.disable_excludes not in _disable_excludes:
                _disable_excludes.append(self.disable_excludes)
                conf.disable_excludes = _disable_excludes

        # Set releasever
        if self.releasever is not None:
            conf.substitutions['releasever'] = self.releasever

        # Set skip_broken (in dnf this is strict=0)
        if self.skip_broken:
            conf.strict = 0

        if self.download_only:
            conf.downloadonly = True
            if self.download_dir:
                conf.destdir = self.download_dir

        # Default in dnf upstream is true
        conf.clean_requirements_on_remove = self.autoremove

        # Default in dnf (and module default) is True
        conf.install_weak_deps = self.install_weak_deps

    def _specify_repositories(self, base, disablerepo, enablerepo):
        """Enable and disable repositories matching the provided patterns."""
        base.read_all_repos()
        repos = base.repos

        # Disable repositories
        for repo_pattern in disablerepo:
            if repo_pattern:
                for repo in repos.get_matching(repo_pattern):
                    repo.disable()

        # Enable repositories
        for repo_pattern in enablerepo:
            if repo_pattern:
                for repo in repos.get_matching(repo_pattern):
                    repo.enable()

    def _base(self, conf_file, disable_gpg_check, disablerepo, enablerepo, installroot):
        """Return a fully configured dnf Base object."""
        base = dnf.Base()
        self._configure_base(base, conf_file, disable_gpg_check, installroot)
        try:
            base.init_plugins(set(self.disable_plugin), set(self.enable_plugin))
            base.pre_configure_plugins()
        except AttributeError:
            pass  # older versions of dnf didn't require this and don't have these methods
        self._specify_repositories(base, disablerepo, enablerepo)
        try:
            base.configure_plugins()
        except AttributeError:
            pass  # older versions of dnf didn't require this and don't have these methods

        try:
            if self.update_cache:
                try:
                    base.update_cache()
                except dnf.exceptions.RepoError as e:
                    self.module.fail_json(
                        msg="{0}".format(to_text(e)),
                        results=[],
                        rc=1
                    )
            base.fill_sack(load_system_repo='auto')
        except dnf.exceptions.RepoError as e:
            self.module.fail_json(
                msg="{0}".format(to_text(e)),
                results=[],
                rc=1
            )
        if self.bugfix:
            key = {'advisory_type__eq': 'bugfix'}
            base._update_security_filters = [base.sack.query().filter(**key)]
        if self.security:
            key = {'advisory_type__eq': 'security'}
            base._update_security_filters = [base.sack.query().filter(**key)]

        return base

    def list_items(self, command):
        """List package info based on the command."""
        # Rename updates to upgrades
        if command == 'updates':
            command = 'upgrades'

        # Return the corresponding packages
        if command in ['installed', 'upgrades', 'available']:
            results = [
                self._package_dict(package)
                for package in getattr(self.base.sack.query(), command)()]
        # Return the enabled repository ids
        elif command in ['repos', 'repositories']:
            results = [
                {'repoid': repo.id, 'state': 'enabled'}
                for repo in self.base.repos.iter_enabled()]
        # Return any matching packages
        else:
            packages = dnf.subject.Subject(command).get_best_query(self.base.sack)
            results = [self._package_dict(package) for package in packages]

        self.module.exit_json(msg="", results=results)

    def _is_installed(self, pkg):
        installed = self.base.sack.query().installed()
        if installed.filter(name=pkg):
            return True
        else:
            return False

    def _is_newer_version_installed(self, pkg_name):
        candidate_pkg = self._packagename_dict(pkg_name)
        if not candidate_pkg:
            # The user didn't provide a versioned rpm, so version checking is
            # not required
            return False

        installed = self.base.sack.query().installed()
        installed_pkg = installed.filter(name=candidate_pkg['name']).run()
        if installed_pkg:
            installed_pkg = installed_pkg[0]

            # this looks weird but one is a dict and the other is a dnf.Package
            evr_cmp = self._compare_evr(
                installed_pkg.epoch, installed_pkg.version, installed_pkg.release,
                candidate_pkg['epoch'], candidate_pkg['version'], candidate_pkg['release'],
            )

            if evr_cmp == 1:
                return True
            else:
                return False

        else:

            return False

    def _mark_package_install(self, pkg_spec, upgrade=False):
        """Mark the package for install."""
        is_newer_version_installed = self._is_newer_version_installed(pkg_spec)
        is_installed = self._is_installed(pkg_spec)
        try:
            if self.allow_downgrade:
                # dnf only does allow_downgrade, we have to handle this ourselves
                # because it allows a possibility for non-idempotent transactions
                # on a system's package set (pending the yum repo has many old
                # NVRs indexed)
                if upgrade:
                    if is_installed:
                        self.base.upgrade(pkg_spec)
                    else:
                        self.base.install(pkg_spec)
                else:
                    self.base.install(pkg_spec)
            elif not self.allow_downgrade and is_newer_version_installed:
                return {'failed': False, 'msg': '', 'failure': '', 'rc': 0}
            elif not is_newer_version_installed:
                if upgrade:
                    if is_installed:
                        self.base.upgrade(pkg_spec)
                    else:
                        self.base.install(pkg_spec)
                else:
                    self.base.install(pkg_spec)
            else:
                if upgrade:
                    if is_installed:
                        self.base.upgrade(pkg_spec)
                    else:
                        self.base.install(pkg_spec)
                else:
                    self.base.install(pkg_spec)

            return {'failed': False, 'msg': 'Installed: {0}'.format(pkg_spec), 'failure': '', 'rc': 0}

        except dnf.exceptions.MarkingError as e:
            return {
                'failed': True,
                'msg': "No package {0} available.".format(pkg_spec),
                'failure': " ".join((pkg_spec, to_native(e))),
                'rc': 1,
                "results": []
            }

        except dnf.exceptions.DepsolveError as e:
            return {
                'failed': True,
                'msg': "Depsolve Error occured for package {0}.".format(pkg_spec),
                'failure': " ".join((pkg_spec, to_native(e))),
                'rc': 1,
                "results": []
            }

        except dnf.exceptions.Error as e:
            if to_text("already installed") in to_text(e):
                return {'failed': False, 'msg': '', 'failure': ''}
            else:
                return {
                    'failed': True,
                    'msg': "Unknown Error occured for package {0}.".format(pkg_spec),
                    'failure': " ".join((pkg_spec, to_native(e))),
                    'rc': 1,
                    "results": []
                }

    def _whatprovides(self, filepath):
        available = self.base.sack.query().available()
        pkg_spec = available.filter(provides=filepath).run()

        if pkg_spec:
            return pkg_spec[0].name

    def _parse_spec_group_file(self):
        pkg_specs, grp_specs, module_specs, filenames = [], [], [], []
        already_loaded_comps = False  # Only load this if necessary, it's slow

        for name in self.names:
            if '://' in name:
                name = self.fetch_rpm_from_url(name)
                filenames.append(name)
            elif name.endswith(".rpm"):
                filenames.append(name)
            elif name.startswith("@") or ('/' in name):
                # like "dnf install /usr/bin/vi"
                if '/' in name:
                    pkg_spec = self._whatprovides(name)
                    if pkg_spec:
                        pkg_specs.append(pkg_spec)
                        continue

                if not already_loaded_comps:
                    self.base.read_comps()
                    already_loaded_comps = True

                grp_env_mdl_candidate = name[1:].strip()

                if self.with_modules:
                    mdl = self.module_base._get_modules(grp_env_mdl_candidate)
                    if mdl[0]:
                        module_specs.append(grp_env_mdl_candidate)
                    else:
                        grp_specs.append(grp_env_mdl_candidate)
                else:
                    grp_specs.append(grp_env_mdl_candidate)
            else:
                pkg_specs.append(name)
        return pkg_specs, grp_specs, module_specs, filenames

    def _update_only(self, pkgs):
        not_installed = []
        for pkg in pkgs:
            if self._is_installed(pkg):
                try:
                    if isinstance(to_text(pkg), text_type):
                        self.base.upgrade(pkg)
                    else:
                        self.base.package_upgrade(pkg)
                except Exception as e:
                    self.module.fail_json(
                        msg="Error occured attempting update_only operation: {0}".format(to_native(e)),
                        results=[],
                        rc=1,
                    )
            else:
                not_installed.append(pkg)

        return not_installed

    def _install_remote_rpms(self, filenames):
        if int(dnf.__version__.split(".")[0]) >= 2:
            pkgs = list(sorted(self.base.add_remote_rpms(list(filenames)), reverse=True))
        else:
            pkgs = []
            try:
                for filename in filenames:
                    pkgs.append(self.base.add_remote_rpm(filename))
            except IOError as e:
                if to_text("Can not load RPM file") in to_text(e):
                    self.module.fail_json(
                        msg="Error occured attempting remote rpm install of package: {0}. {1}".format(filename, to_native(e)),
                        results=[],
                        rc=1,
                    )
        if self.update_only:
            self._update_only(pkgs)
        else:
            for pkg in pkgs:
                try:
                    if self._is_newer_version_installed(self._package_dict(pkg)['nevra']):
                        if self.allow_downgrade:
                            self.base.package_install(pkg)
                    else:
                        self.base.package_install(pkg)
                except Exception as e:
                    self.module.fail_json(
                        msg="Error occured attempting remote rpm operation: {0}".format(to_native(e)),
                        results=[],
                        rc=1,
                    )

    def _is_module_installed(self, module_spec):
        if self.with_modules:
            module_spec = module_spec.strip()
            module_list, nsv = self.module_base._get_modules(module_spec)

            if nsv.stream in self.base._moduleContainer.getEnabledStream(nsv.name):
                return True

        return False  # seems like a sane default

    def ensure(self):
        allow_erasing = False

        response = {
            'msg': "",
            'changed': False,
            'results': [],
            'rc': 0
        }

        # Accumulate failures.  Package management modules install what they can
        # and fail with a message about what they can't.
        failure_response = {
            'msg': "",
            'failures': [],
            'results': [],
            'rc': 1
        }

        # Autoremove is called alone
        # Jump to remove path where base.autoremove() is run
        if not self.names and self.autoremove:
            self.names = []
            self.state = 'absent'

        if self.names == ['*'] and self.state == 'latest':
            try:
                self.base.upgrade_all()
            except dnf.exceptions.DepsolveError as e:
                failure_response['msg'] = "Depsolve Error occured attempting to upgrade all packages"
                self.module.fail_json(**failure_response)
        else:
            pkg_specs, group_specs, module_specs, filenames = self._parse_spec_group_file()

            pkg_specs = [p.strip() for p in pkg_specs]
            filenames = [f.strip() for f in filenames]
            groups = []
            environments = []
            for group_spec in (g.strip() for g in group_specs):
                group = self.base.comps.group_by_pattern(group_spec)
                if group:
                    groups.append(group.id)
                else:
                    environment = self.base.comps.environment_by_pattern(group_spec)
                    if environment:
                        environments.append(environment.id)
                    else:
                        self.module.fail_json(
                            msg="No group {0} available.".format(group_spec),
                            results=[],
                        )

            if self.state in ['installed', 'present']:
                # Install files.
                self._install_remote_rpms(filenames)
                for filename in filenames:
                    response['results'].append("Installed {0}".format(filename))

                # Install modules
                if module_specs and self.with_modules:
                    for module in module_specs:
                        try:
                            if not self._is_module_installed(module):
                                response['results'].append("Module {0} installed.".format(module))
                            self.module_base.install([module])
                            self.module_base.enable([module])
                        except dnf.exceptions.MarkingErrors as e:
                            failure_response['failures'].append(' '.join((module, to_native(e))))

                # Install groups.
                for group in groups:
                    try:
                        group_pkg_count_installed = self.base.group_install(group, dnf.const.GROUP_PACKAGE_TYPES)
                        if group_pkg_count_installed == 0:
                            response['results'].append("Group {0} already installed.".format(group))
                        else:
                            response['results'].append("Group {0} installed.".format(group))
                    except dnf.exceptions.DepsolveError as e:
                        failure_response['msg'] = "Depsolve Error occured attempting to install group: {0}".format(group)
                        self.module.fail_json(**failure_response)
                    except dnf.exceptions.Error as e:
                        # In dnf 2.0 if all the mandatory packages in a group do
                        # not install, an error is raised.  We want to capture
                        # this but still install as much as possible.
                        failure_response['failures'].append(" ".join((group, to_native(e))))

                for environment in environments:
                    try:
                        self.base.environment_install(environment, dnf.const.GROUP_PACKAGE_TYPES)
                    except dnf.exceptions.DepsolveError as e:
                        failure_response['msg'] = "Depsolve Error occured attempting to install environment: {0}".format(environment)
                        self.module.fail_json(**failure_response)
                    except dnf.exceptions.Error as e:
                        failure_response['failures'].append(" ".join((environment, to_native(e))))

                if module_specs and not self.with_modules:
                    # This means that the group or env wasn't found in comps
                    self.module.fail_json(
                        msg="No group {0} available.".format(module_specs[0]),
                        results=[],
                    )

                # Install packages.
                if self.update_only:
                    not_installed = self._update_only(pkg_specs)
                    for spec in not_installed:
                        response['results'].append("Packages providing %s not installed due to update_only specified" % spec)
                else:
                    for pkg_spec in pkg_specs:
                        install_result = self._mark_package_install(pkg_spec)
                        if install_result['failed']:
                            failure_response['msg'] += install_result['msg']
                            failure_response['failures'].append(self._sanitize_dnf_error_msg_install(pkg_spec, install_result['failure']))
                        else:
                            response['results'].append(install_result['msg'])

            elif self.state == 'latest':
                # "latest" is same as "installed" for filenames.
                self._install_remote_rpms(filenames)
                for filename in filenames:
                    response['results'].append("Installed {0}".format(filename))

                # Upgrade modules
                if module_specs and self.with_modules:
                    for module in module_specs:
                        try:
                            if self._is_module_installed(module):
                                response['results'].append("Module {0} upgraded.".format(module))
                            self.module_base.upgrade([module])
                        except dnf.exceptions.MarkingErrors as e:
                            failure_response['failures'].append(' '.join((module, to_native(e))))

                for group in groups:
                    try:
                        try:
                            self.base.group_upgrade(group)
                            response['results'].append("Group {0} upgraded.".format(group))
                        except dnf.exceptions.CompsError:
                            if not self.update_only:
                                # If not already installed, try to install.
                                group_pkg_count_installed = self.base.group_install(group, dnf.const.GROUP_PACKAGE_TYPES)
                                if group_pkg_count_installed == 0:
                                    response['results'].append("Group {0} already installed.".format(group))
                                else:
                                    response['results'].append("Group {0} installed.".format(group))
                    except dnf.exceptions.Error as e:
                        failure_response['failures'].append(" ".join((group, to_native(e))))

                for environment in environments:
                    try:
                        try:
                            self.base.environment_upgrade(environment)
                        except dnf.exceptions.CompsError:
                            # If not already installed, try to install.
                            self.base.environment_install(environment, dnf.const.GROUP_PACKAGE_TYPES)
                    except dnf.exceptions.DepsolveError as e:
                        failure_response['msg'] = "Depsolve Error occured attempting to install environment: {0}".format(environment)
                    except dnf.exceptions.Error as e:
                        failure_response['failures'].append(" ".join((environment, to_native(e))))

                if self.update_only:
                    not_installed = self._update_only(pkg_specs)
                    for spec in not_installed:
                        response['results'].append("Packages providing %s not installed due to update_only specified" % spec)
                else:
                    for pkg_spec in pkg_specs:
                        # best effort causes to install the latest package
                        # even if not previously installed
                        self.base.conf.best = True
                        install_result = self._mark_package_install(pkg_spec, upgrade=True)
                        if install_result['failed']:
                            failure_response['msg'] += install_result['msg']
                            failure_response['failures'].append(self._sanitize_dnf_error_msg_install(pkg_spec, install_result['failure']))
                        else:
                            response['results'].append(install_result['msg'])

            else:
                # state == absent
                if filenames:
                    self.module.fail_json(
                        msg="Cannot remove paths -- please specify package name.",
                        results=[],
                    )

                # Remove modules
                if module_specs and self.with_modules:
                    for module in module_specs:
                        try:
                            if self._is_module_installed(module):
                                response['results'].append("Module {0} removed.".format(module))
                            self.module_base.remove([module])
                            self.module_base.disable([module])
                            self.module_base.reset([module])
                        except dnf.exceptions.MarkingErrors as e:
                            failure_response['failures'].append(' '.join((module, to_native(e))))

                for group in groups:
                    try:
                        self.base.group_remove(group)
                    except dnf.exceptions.CompsError:
                        # Group is already uninstalled.
                        pass
                    except AttributeError:
                        # Group either isn't installed or wasn't marked installed at install time
                        # because of DNF bug
                        #
                        # This is necessary until the upstream dnf API bug is fixed where installing
                        # a group via the dnf API doesn't actually mark the group as installed
                        #   https://bugzilla.redhat.com/show_bug.cgi?id=1620324
                        pass

                for environment in environments:
                    try:
                        self.base.environment_remove(environment)
                    except dnf.exceptions.CompsError:
                        # Environment is already uninstalled.
                        pass

                installed = self.base.sack.query().installed()
                for pkg_spec in pkg_specs:
                    # short-circuit installed check for wildcard matching
                    if '*' in pkg_spec:
                        try:
                            self.base.remove(pkg_spec)
                        except dnf.exceptions.MarkingError as e:
                            is_failure, handled_remove_error = self._sanitize_dnf_error_msg_remove(pkg_spec, to_native(e))
                            if is_failure:
                                failure_response['failures'].append('{0} - {1}'.format(pkg_spec, to_native(e)))
                            else:
                                response['results'].append(handled_remove_error)
                        continue

                    installed_pkg = list(map(str, installed.filter(name=pkg_spec).run()))
                    if installed_pkg:
                        candidate_pkg = self._packagename_dict(installed_pkg[0])
                        installed_pkg = installed.filter(name=candidate_pkg['name']).run()
                    else:
                        candidate_pkg = self._packagename_dict(pkg_spec)
                        installed_pkg = installed.filter(nevra=pkg_spec).run()
                    if installed_pkg:
                        installed_pkg = installed_pkg[0]
                        evr_cmp = self._compare_evr(
                            installed_pkg.epoch, installed_pkg.version, installed_pkg.release,
                            candidate_pkg['epoch'], candidate_pkg['version'], candidate_pkg['release'],
                        )
                        if evr_cmp == 0:
                            self.base.remove(pkg_spec)

                # Like the dnf CLI we want to allow recursive removal of dependent
                # packages
                allow_erasing = True

                if self.autoremove:
                    self.base.autoremove()

        try:
            if not self.base.resolve(allow_erasing=allow_erasing):
                if failure_response['failures']:
                    failure_response['msg'] = 'Failed to install some of the specified packages'
                    self.module.fail_json(**failure_response)

                response['msg'] = "Nothing to do"
                self.module.exit_json(**response)
            else:
                response['changed'] = True
                if failure_response['failures']:
                    failure_response['msg'] = 'Failed to install some of the specified packages',
                    self.module.fail_json(**failure_response)
                if self.module.check_mode:
                    response['msg'] = "Check mode: No changes made, but would have if not in check mode"
                    self.module.exit_json(**response)

                try:
                    if self.download_only and self.download_dir and self.base.conf.destdir:
                        dnf.util.ensure_dir(self.base.conf.destdir)
                        self.base.repos.all().pkgdir = self.base.conf.destdir

                    self.base.download_packages(self.base.transaction.install_set)
                except dnf.exceptions.DownloadError as e:
                    self.module.fail_json(
                        msg="Failed to download packages: {0}".format(to_text(e)),
                        results=[],
                    )

                if self.download_only:
                    for package in self.base.transaction.install_set:
                        response['results'].append("Downloaded: {0}".format(package))
                    self.module.exit_json(**response)
                else:
                    self.base.do_transaction()
                    for package in self.base.transaction.install_set:
                        response['results'].append("Installed: {0}".format(package))
                    for package in self.base.transaction.remove_set:
                        response['results'].append("Removed: {0}".format(package))

                if failure_response['failures']:
                    failure_response['msg'] = 'Failed to install some of the specified packages',
                    self.module.exit_json(**response)
                self.module.exit_json(**response)
        except dnf.exceptions.DepsolveError as e:
            failure_response['msg'] = "Depsolve Error occured: {0}".format(to_native(e))
            self.module.fail_json(**failure_response)
        except dnf.exceptions.Error as e:
            if to_text("already installed") in to_text(e):
                response['changed'] = False
                response['results'].append("Package already installed: {0}".format(to_native(e)))
                self.module.exit_json(**response)
            else:
                failure_response['msg'] = "Unknown Error occured: {0}".format(to_native(e))
                self.module.fail_json(**failure_response)

    @staticmethod
    def has_dnf():
        return HAS_DNF

    def run(self):
        """The main function."""

        # Check if autoremove is called correctly
        if self.autoremove:
            if LooseVersion(dnf.__version__) < LooseVersion('2.0.1'):
                self.module.fail_json(
                    msg="Autoremove requires dnf>=2.0.1. Current dnf version is %s" % dnf.__version__,
                    results=[],
                )

        # Check if download_dir is called correctly
        if self.download_dir:
            if LooseVersion(dnf.__version__) < LooseVersion('2.6.2'):
                self.module.fail_json(
                    msg="download_dir requires dnf>=2.6.2. Current dnf version is %s" % dnf.__version__,
                    results=[],
                )

        if self.update_cache and not self.names and not self.list:
            self.base = self._base(
                self.conf_file, self.disable_gpg_check, self.disablerepo,
                self.enablerepo, self.installroot
            )
            self.module.exit_json(
                msg="Cache updated",
                changed=False,
                results=[],
                rc=0
            )

        # Set state as installed by default
        # This is not set in AnsibleModule() because the following shouldn't happen
        # - dnf: autoremove=yes state=installed
        if self.state is None:
            self.state = 'installed'

        if self.list:
            self.base = self._base(
                self.conf_file, self.disable_gpg_check, self.disablerepo,
                self.enablerepo, self.installroot
            )
            self.list_items(self.list)
        else:
            # Note: base takes a long time to run so we want to check for failure
            # before running it.
            if not dnf.util.am_i_root():
                self.module.fail_json(
                    msg="This command has to be run under the root user.",
                    results=[],
                )
            self.base = self._base(
                self.conf_file, self.disable_gpg_check, self.disablerepo,
                self.enablerepo, self.installroot
            )

            if self.with_modules:
                self.module_base = dnf.module.module_base.ModuleBase(self.base)

            self.ensure()

def main():
    # state=installed name=pkgspec
    # state=removed name=pkgspec
    # state=latest name=pkgspec
    #
    # informational commands:
    #   list=installed
    #   list=updates
    #   list=available
    #   list=repos
    #   list=pkgspec

    module = AnsibleModule(
        **yumdnf_argument_spec
    )

    module_implementation = DnfModule(module)
    try:
        module_implementation.run()
    except dnf.exceptions.RepoError as de:
        module.fail_json(
            msg="Failed to synchronize repodata: {0}".format(to_native(de)),
            rc=1,
            results=[],
            changed=False
        )

if __name__ == '__main__':
    main()


text:
using System;
using System.Collections.Generic;
using System.Text;

using AutoMapper;

using Muscularity.Core.Shared;
using Muscularity.Features.Workouts.SaveWorkout;

using NSubstitute;

using Xunit;

namespace Muscularity.Features.Workouts.Tests.SaveWorkout
{
    public class SaveWorkoutHandlerTests
    {
        public SaveWorkoutHandlerTests()
        {
            DataAccess = Substitute.For<ISaveWorkoutData>();
            Clock = Substitute.For<IClock>();

            var mapperConfiguration = new MapperConfiguration(
                config =>
                {
                    config.AddProfile<AutoMapperWorkoutsProfile>();
                });

            var mapper = mapperConfiguration.CreateMapper();

            Handler = new SaveWorkoutHandler(DataAccess, mapper, Clock);
        }

        private ISaveWorkoutData DataAccess { get; set; }

        private IClock Clock { get; set; }

        private SaveWorkoutHandler Handler { get; set; }

        [Fact]
        public async void Handle_LeavesCompleteDateNull_WhenCompleteDateIsNull()
        {
            var request = new SaveWorkoutRequest()
            {
                CompleteDate = null
            };

            var response = await Handler.Handle(request);

            Assert.NotNull(response);
            Assert.NotNull(response.Workout);
            Assert.Null(response.Workout.CompleteDate);
        }

        [Fact]
        public async void Handle_SetsCompleteDateToServerTime_WhenCompleteDateIsNotNull()
        {
            var request = new SaveWorkoutRequest()
            {
                CompleteDate = DateTime.MaxValue
            };

            var now = DateTime.UtcNow;
            Clock.UtcNow.Returns(now);

            var response = await Handler.Handle(request);

            Assert.NotNull(response);
            Assert.NotNull(response.Workout);
            Assert.NotNull(response.Workout.CompleteDate);
            Assert.Equal(now, response.Workout.CompleteDate);
        }

        [Fact]
        public async void Handle_SavesDataThenReturnsInput()
        {
            var request = new SaveWorkoutRequest()
            {
                WorkoutId = 1,
                BodyWeight = 123,
                Notes = "Notes",
                CompleteDate = DateTime.MaxValue
            };

            var now = DateTime.UtcNow;
            Clock.UtcNow.Returns(now);

            var response = await Handler.Handle(request);

            Assert.NotNull(response);
            Assert.NotNull(response.Workout);
            Assert.Equal(request.WorkoutId, response.Workout.WorkoutId);
            Assert.Equal(request.BodyWeight, response.Workout.BodyWeight);
            Assert.Equal(request.Notes, response.Workout.Notes);
            Assert.Equal(now, response.Workout.CompleteDate);
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
@(orgId: String = "")(implicit flash: Flash)

@templates.ozone.oxyjen_zone(None, "Sign in") {

                    Log in to your organization

                @common.flash_bootstrap()

                <form action="@routes.SignInCtrl.login()" method="post">

                        Organization ID
                        <input name="orgId" type="text" id="orgId" placeholder="Organization ID"
                            class="form-control" value="@orgId">

                        Password
                        <input name="password" type="password" id="password"
                            placeholder="Password" class="form-control">

                    <input type="submit" class="btn btn-success" value="Sign in">
                    Cancel

                        Don't have an account? Create it here

                </form>

}


text:
#include "vice.h"

#if defined(USE_LAMEMP3) && !defined(HAVE_STATIC_LAME)

#include "lamelib.h"
#include "dynlib.h"
#include "log.h"

#ifdef WIN32
#define SO_NAME     "lame.dll"
#else
#ifdef MACOSX_SUPPORT
#define SO_NAME     "/opt/local/lib/libmp3lame.dylib"
#else
#define SO_NAME     "libmp3lame.so"
#endif
#endif

lamelib_t lamelib;

static void *lib_so = NULL;

/* macro for getting functionpointers from avcodec */
#define GET_SYMBOL_AND_TEST( _name_ ) \
    lamelib.p_##_name_ = (_name_##_t) vice_dynlib_symbol(lib_so, #_name_ ); \
    if (!lamelib.p_##_name_ ) { \
        log_debug("getting symbol " #_name_ " failed!"); \
        return -1; \
    }

static int load_lib(void)
{
    if (!lib_so) {
        lib_so = vice_dynlib_open(SO_NAME);

        if (!lib_so) {
            log_debug("opening dynamic library " SO_NAME " failed!");
            return -1;
        }

        GET_SYMBOL_AND_TEST(lame_init);
        GET_SYMBOL_AND_TEST(lame_close);
        GET_SYMBOL_AND_TEST(lame_set_num_channels);
        GET_SYMBOL_AND_TEST(lame_set_in_samplerate);
        GET_SYMBOL_AND_TEST(lame_set_quality);
        GET_SYMBOL_AND_TEST(lame_set_brate);
        GET_SYMBOL_AND_TEST(lame_init_params);
        GET_SYMBOL_AND_TEST(lame_encode_buffer_interleaved);
        GET_SYMBOL_AND_TEST(lame_encode_flush);
    }

    return 0;
}

static void free_lib(void)
{
    if (lib_so) {
        if (vice_dynlib_close(lib_so) != 0) {
            log_debug("closing dynamic library " SO_NAME " failed!");
        }
    }
    lib_so = NULL;

    lamelib.p_lame_init = NULL;
    lamelib.p_lame_close = NULL;
    lamelib.p_lame_set_num_channels = NULL;
    lamelib.p_lame_set_in_samplerate = NULL;
    lamelib.p_lame_set_quality = NULL;
    lamelib.p_lame_set_brate = NULL;
    lamelib.p_lame_init_params = NULL;
    lamelib.p_lame_encode_buffer_interleaved = NULL;
    lamelib.p_lame_encode_flush = NULL;
}

int lamelib_open(void)
{
    int result;

    result = load_lib();
    if (result != 0) {
        free_lib();
    }
    return result;
}

void lamelib_close(void)
{
    free_lib();
}

#endif


text:
/**
 * @class IBaseModelOptions
 * @module StructureJS
 * @submodule interface
 * @interface
 */
interface IBaseModelOptions
{
    /**
     * @property expand
     * @type {boolean}
     */
    expand?:boolean;
}

export default IBaseModelOptions;


text:
Metrics
=======

The package `shorttext` provides a few metrics that measure the distances of some kind. They are all
under :module:`shorttext.metrics`. The soft Jaccard score is based on spellings, and the Word Mover's
distance (WMD) embedded word vectors.

Edit Distance and Soft Jaccard Score
------------------------------------

Edit distance, or Damerau-Levenshtein distance, measures the differences
between two words due to insertion, deletion, transposition, substitution etc.
Each of this change causes a distance of 1. The algorithm was written in C.

First import the package:

>>> from shorttext.metrics.dynprog import damerau_levenshtein, longest_common_prefix, similarity, soft_jaccard_score

The distance can be calculated by:

>>> damerau_levenshtein('diver', 'driver')        # insertion, gives 1
>>> damerau_levenshtein('driver', 'diver')        # deletion, gives 1
>>> damerau_levenshtein('topology', 'tooplogy')   # transposition, gives 1
>>> damerau_levenshtein('book', 'blok')           # subsitution, gives 1

The longest common prefix finds the length of common prefix:

>>> longest_common_prefix('topology', 'topological')    # gives 7
>>> longest_common_prefix('police', 'policewoman')      # gives 6

The similarity between words is defined as the larger of the following:

:math:`s = 1 - \frac{\text{DL distance}}{\max( \text(len(word1)), \text(len(word2)) )}`
and
:math:`s = \frac{\text{longest common prefix}}{\max( \text(len(word1)), \text(len(word2)) )}`

>>> similarity('topology', 'topological')    # gives 0.6363636363636364
>>> similarity('book', 'blok')               # gives 0.75

Given the similarity, we say that the intersection, for example, between 'book' and 'blok', has 0.75 elements, or the
union has 1.25 elements. Then the similarity between two sets of tokens can be measured using Jaccard index, with this
"soft" numbers of intersection. Therefore,

>>> soft_jaccard_score(['book', 'seller'], ['blok', 'sellers'])     # gives 0.6716417910447762
>>> soft_jaccard_score(['police', 'station'], ['policeman'])        # gives 0.2857142857142858

The functions `damerau_levenshtein` and `longest_common_prefix` are implemented using Cython_ .
(Before release 0.7.2, they were interfaced to Python using SWIG_ (Simplified Wrapper and Interface Generator)).

.. automodule:: shorttext.metrics.dynprog.jaccard
   :members: similarity, soft_jaccard_score

Word Mover's Distance
---------------------

Unlike soft Jaccard score that bases similarity on the words' spellings, Word Mover's distance (WMD)
the embedded word vectors. WMD is a special case for Earth Mover's distance (EMD), or Wasserstein
distance. The calculation of WMD in this package is based on linear programming, and the distance between
words are the Euclidean distance by default (not cosine distance), but user can set it accordingly.

Import the modules, and load the word-embedding models:

>>> from shorttext.metrics.wasserstein import word_mover_distance
>>> from shorttext.utils import load_word2vec_model
>>> wvmodel = load_word2vec_model('/path/to/model_file.bin')

Examples:

>>> word_mover_distance(['police', 'station'], ['policeman'], wvmodel)                      # gives 3.060708999633789
>>> word_mover_distance(['physician', 'assistant'], ['doctor', 'assistants'], wvmodel)      # gives 2.276337146759033

More examples can be found in this `IPython Notebook
<https://github.com/stephenhky/PyWMD/blob/master/WordMoverDistanceDemo.ipynb>`_ .

In `gensim`, the Word2Vec model allows the calculation of WMD if user installed the package PyEMD_. It is based on the
scale invariant feature transform (SIFT), an algorithm for EMD based on L1-distance (Manhattan distance).
For more details,
please refer to their `tutorial
<https://radimrehurek.com/gensim/models/keyedvectors.html>`_ , and cite the two papers by Ofir Pele and Michael Werman
if it is used.

.. automodule:: shorttext.metrics.wasserstein.wordmoverdist
   :members: word_mover_distance

Jaccard Index Due to Cosine Distances
-------------------------------------

In the above section of edit distance, the Jaccard score was calculated by considering soft membership
using spelling. However, we can also compute the soft membership by cosine similarity with

>>> from shorttext.utils import load_word2vec_model
>>> wvmodel = load_word2vec_model('/path/to/model_file.bin')
>>> from shorttext.metrics.embedfuzzy import jaccardscore_sents

For example, the number of words between the set containing 'doctor' and that containing 'physician'
is 0.78060223420956831 (according to Google model), and therefore the Jaccard score is

:math:`0.78060223420956831 / (2-0.78060223420956831) = 0.6401538990056869`

And it can be seen by running it:

>>> jaccardscore_sents('doctor', 'physician', wvmodel)   # gives 0.6401538990056869
>>> jaccardscore_sents('chief executive', 'computer cluster', wvmodel)   # gives 0.0022515450768836143
>>> jaccardscore_sents('topological data', 'data of topology', wvmodel)   # gives 0.67588977344632573

.. automodule:: shorttext.metrics.embedfuzzy.jaccard
   :members:

BERTScore
---------

BERTScore includes a category of metrics that is based on BERT model.
This metrics measures the similarity between sentences. To use it,

>>> from shorttext.metrics.transformers import BERTScorer
>>> scorer = BERTScorer()    # using default BERT model and tokenizer
>>> scorer.recall_bertscore('The weather is cold.', 'It is freezing.')   # 0.7223385572433472
>>> scorer.precision_bertscore('The weather is cold.', 'It is freezing.')   # 0.7700849175453186
>>> scorer.f1score_bertscore('The weather is cold.', 'It is freezing.')   # 0.7454479746418043

For BERT models, please refer to :doc:`tutorial_wordembed` for more details.

.. automodule:: shorttext.metrics.transformers.bertscore
   :members:

Reference
---------

"Damerau-Levenshtein Distance." [`Wikipedia
<https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance>`_]

"Jaccard index." [`Wikipedia
<https://en.wikipedia.org/wiki/Jaccard_index>`_]

Daniel E. Russ, Kwan-Yuet Ho, Calvin A. Johnson, Melissa C. Friesen, "Computer-Based Coding of Occupation Codes for Epidemiological Analyses," *2014 IEEE 27th International Symposium on Computer-Based Medical Systems* (CBMS), pp. 347-350. (2014) [`IEEE
<http://ieeexplore.ieee.org/abstract/document/6881904/>`_]

Matt J. Kusner, Yu Sun, Nicholas I. Kolkin, Kilian Q. Weinberger, "From Word Embeddings to Document Distances," *ICML* (2015).

Ofir Pele, Michael Werman, "A linear time histogram metric for improved SIFT matching," *Computer Vision - ECCV 2008*, 495-508 (2008). [`ACM
<http://dl.acm.org/citation.cfm?id=1478212>`_]

Ofir Pele, Michael Werman, "Fast and robust earth mover's distances," *Proc. 2009 IEEE 12th Int. Conf. on Computer Vision*, 460-467 (2009). [`IEEE
<http://ieeexplore.ieee.org/document/5459199/>`_]

Tianyi Zhang, Varsha Kishore, Felix Wu, Kilian Q. Weinberger, Yoav Artzi,
"BERTScore: Evaluating Text Generation with BERT," arXiv:1904.09675 (2019). [`arXiv
<https://arxiv.org/abs/1904.09675>`_]

"Word Mover's Distance as a Linear Programming Problem," *Everything About Data Analytics*, WordPress (2017). [`WordPress
<https://datawarrior.wordpress.com/2017/08/16/word-movers-distance-as-a-linear-programming-problem/>`_]

Home: :doc:`index`

.. _SWIG: http://www.swig.org/
.. _PyEMD: https://github.com/wmayner/pyemd
.. _Cython: http://cython.org/


text:
using System;
using Pathoschild.Stardew.Common.Integrations.GenericModConfigMenu;
using StardewModdingAPI;

namespace Pathoschild.Stardew.HorseFluteAnywhere.Framework
{
    /// Registers the mod configuration with Generic Mod Config Menu.
    internal class GenericModConfigMenuIntegrationForHorseFluteAnywhere
    {
        /*********
        ** Fields
        *********/
        /// The Generic Mod Config Menu integration.
        private readonly GenericModConfigMenuIntegration<ModConfig> ConfigMenu;

        /*********
        ** Public methods
        *********/
        /// Construct an instance.
        /// <param name="modRegistry">An API for fetching metadata about loaded mods.</param>
        /// <param name="monitor">Encapsulates monitoring and logging.</param>
        /// <param name="manifest">The mod manifest.</param>
        /// <param name="getConfig">Get the current config model.</param>
        /// <param name="reset">Reset the config model to the default values.</param>
        /// <param name="saveAndApply">Save and apply the current config model.</param>
        public GenericModConfigMenuIntegrationForHorseFluteAnywhere(IModRegistry modRegistry, IMonitor monitor, IManifest manifest, Func<ModConfig> getConfig, Action reset, Action saveAndApply)
        {
            this.ConfigMenu = new GenericModConfigMenuIntegration<ModConfig>(modRegistry, monitor, manifest, getConfig, reset, saveAndApply);
        }

        /// Register the config menu if available.
        public void Register()
        {
            var defaultConfig = new ModConfig();

            // get config menu
            var menu = this.ConfigMenu;
            if (!menu.IsLoaded)
                return;

            // register
            menu
                .Register()
                .AddCheckbox(
                    name: I18n.Config_RequireFlute_Name,
                    tooltip: I18n.Config_RequireFlute_Description,
                    get: config => config.RequireHorseFlute,
                    set: (config, value) => config.RequireHorseFlute = value
                )
                .AddKeyBinding(
                    name: I18n.Config_SummonHorseButton_Name,
                    tooltip: () => I18n.Config_SummonHorseButton_Description(defaultValue: defaultConfig.SummonHorseKey.ToString()),
                    get: config => config.SummonHorseKey,
                    set: (config, value) => config.SummonHorseKey = value
                );
        }
    }
}


text:
from util import ThreadJob
from bitcoin import *

class SPV(ThreadJob):
    """ Simple Payment Verification """

    def __init__(self, network, wallet):
        self.wallet = wallet
        self.network = network
        # Keyed by tx hash.  Value is None if the merkle branch was
        # requested, and the merkle root once it has been verified
        self.merkle_roots = {}

    def run(self):
        lh = self.network.get_local_height()
        unverified = self.wallet.get_unverified_txs()
        for tx_hash, tx_height in unverified.items():
            # do not request merkle branch before headers are available
            if tx_height>0 and tx_hash not in self.merkle_roots and tx_height <= lh:
                request = ('blockchain.transaction.get_merkle',
                           [tx_hash, tx_height])
                self.network.send([request], self.verify_merkle)
                self.print_error('requested merkle', tx_hash)
                self.merkle_roots[tx_hash] = None

    def verify_merkle(self, r):
        if r.get('error'):
            self.print_error('received an error:', r)
            return

        params = r['params']
        merkle = r['result']

        # Verify the hash of the server-provided merkle branch to a
        # transaction matches the merkle root of its block
        tx_hash = params[0]
        tx_height = merkle.get('block_height')
        pos = merkle.get('pos')
        merkle_root = self.hash_merkle_root(merkle['merkle'], tx_hash, pos)
        header = self.network.get_header(tx_height)
        if not header or header.get('merkle_root') != merkle_root:
            # FIXME: we should make a fresh connection to a server to
            # recover from this, as this TX will now never verify
            self.print_error("merkle verification failed for", tx_hash)
            return

        # we passed all the tests
        self.merkle_roots[tx_hash] = merkle_root
        self.print_error("verified %s" % tx_hash)
        self.wallet.add_verified_tx(tx_hash, (tx_height, header.get('timestamp'), pos))

    def hash_merkle_root(self, merkle_s, target_hash, pos):
        h = hash_decode(target_hash)
        for i in range(len(merkle_s)):
            item = merkle_s[i]
            h = Hash( hash_decode(item) + h ) if ((pos >> i) & 1) else Hash( h + hash_decode(item) )
        return hash_encode(h)

    def undo_verifications(self, height):
        tx_hashes = self.wallet.undo_verifications(height)
        for tx_hash in tx_hashes:
            self.print_error("redoing", tx_hash)
            self.merkle_roots.pop(tx_hash, None)


text:
from bitstring import BitArray
import structlog

log = structlog.get_logger()

class IndexPool(object):
    def __init__(self, max_entries, offset):
        self.max_entries = max_entries
        self.offset = offset
        self.indices = BitArray(self.max_entries)

    def get_next(self):
        try:
            _pos = self.indices.find('0b0')
            self.indices.set(1, _pos)
            return self.offset + _pos[0]
        except IndexError:
            log.info("exception-fail-to-allocate-id-all-bits-in-use")
            return None

    def allocate(self, index):
        try:
            _pos = index - self.offset
            if not (0 <= _pos < self.max_entries):
                log.info("{}-out-of-range".format(index))
                return None
            if self.indices[_pos]:
                log.info("{}-is-already-allocated".format(index))
                return None
            self.indices.set(1, _pos)
            return index

        except IndexError:
            return None

    def release(self, index):
        index -= self.offset
        _pos = (index,)
        try:
            self.indices.set(0, _pos)
        except IndexError:
            log.info("bit-position-{}-out-of-range".format(index))

    #index or multiple indices to set all of them to 1 - need to be a tuple
    def pre_allocate(self, index):
        if(isinstance(index, tuple)):
            _lst = list(index)
            for i in range(len(_lst)):
                _lst[i] -= self.offset
            index = tuple(_lst)
            self.indices.set(1, index)


text:
import React, { Component, PropTypes } from 'react';
import { Animated, AppRegistry, Button, StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native';

/**
* :D
*
* States that you'll probably care about:
*   query - The string that the user inputs
*   results - The new string
**/

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      text: '',
      query: '',
      results: '',
      offsetY: new Animated.Value(0),
    };
  }

  render() {
    return (
        <View style={styles.container}>
          <Text style={{textAlign:'center', fontSize:24, fontFamily: 'sans-serif-medium'}}>Pun-tionary</Text>
          <Text style={{textAlign:'center', fontSize:14, fontStyle:'italic', color:'#A9A9A9', fontFamily: 'sans-serif'}}>Making you cringe after every word</Text>
          <View style={styles.innerContainer}>
          <Animated.View style={{transform: [{translateY: this.state.offsetY}]}}>
            <View style={styles.inputContainer}>
              <TextInput
                style={{height:40, textAlign:'center'}}
                placeholder="Enter a phrase or sentence"
                returnKeyLabel={"next"}
                onChangeText={(text) => this.setState({query:text})}
                underlineColorAndroid="#1E8BC3"
              />
            </View>

            <View style={styles.buttonContainer}>
              <TouchableOpacity
                onPress={this._handleOnPress}
                style={styles.buttonStyle}>
                  <Text style={styles.buttonTextStyle}>
                    Show me the puns
                  </Text>
              </TouchableOpacity>
            </View>

            <View style={styles.resultContainer}>
              <TextInput
                style={{textAlign:'center', fontWeight:'bold'}}
                multiline={true}
                numberOfLines={7}
                editable={false}
                value={this.state.results}
                underlineColorAndroid="transparent"/>
            </View>
          </Animated.View>
          </View>
        </View>
    );
  }

  _handleOnPress = () => {
      return fetch('http://10.105.208.209:3000/punny',
        {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            firstParam: this.state.query
          })
        })
        .then((res) => {
          // Log the user's input
          console.log(this.state.query);
          Animated.timing(
            this.state.offsetY,
            { toValue: -100 }
          ).start();

          return res.json()
        })
        .then((resJson) => {
          console.log(resJson);
          this.setState({
            results: resJson['res']['response'][0]['phrase']
          });

          return resJson
        })
        .catch((err) => {
          console.log(err)
          console.log('******')
        })
  };
}

const styles = StyleSheet.create({
  container: {
    paddingTop: 50,
    flex:1,
    flexDirection:'column',
    backgroundColor:'#fff',
  },
  innerContainer: {
    paddingTop: 150,
    justifyContent:'center',
  },
  inputContainer: {
    alignItems:'stretch',
  },
  buttonContainer: {
    alignItems:'center',
    paddingBottom: 100,
  },
  resultContainer: {
    alignItems:'stretch',
  },
  buttonStyle: {
    paddingVertical:15,
  },
  buttonTextStyle: {
    color: "#1E8BC3",
  },
});


text:
/*******************************
          Build Task
*******************************/

var
  gulp         = require('gulp'),

  // node dependencies
  console      = require('better-console'),
  fs           = require('fs'),

  // gulp dependencies
  autoprefixer = require('gulp-autoprefixer'),
  chmod        = require('gulp-chmod'),
  clone        = require('gulp-clone'),
  flatten      = require('gulp-flatten'),
  gulpif       = require('gulp-if'),
  less         = require('gulp-less'),
  minifyCSS    = require('gulp-clean-css'),
  plumber      = require('gulp-plumber'),
  print        = require('gulp-print').default,
  rename       = require('gulp-rename'),
  replace      = require('gulp-replace'),

  // config
  config       = require('../config/user'),
  tasks        = require('../config/tasks'),
  install      = require('../config/project/install'),

  // shorthand
  globs        = config.globs,
  assets       = config.paths.assets,
  output       = config.paths.output,
  source       = config.paths.source,

  banner       = tasks.banner,
  comments     = tasks.regExp.comments,
  log          = tasks.log,
  settings     = tasks.settings,

  {series, parallel} = gulp,

  buildCSS
;

// add internal tasks (concat release)
require('../collections/internal')(gulp);

buildCSS = function(taskCallback) {
  let
    tasksCompleted = 0,
    maybeCallback  = function() {
      tasksCompleted++;
      if(tasksCompleted === 2) {
        taskCallback();
      }
    },

    stream,
    compressedStream,
    uncompressedStream
  ;

  console.info('Building CSS');

  if( !install.isSetup() ) {
    console.error('Cannot build files. Run "gulp install" to set-up Semantic');
    return;
  }

  // unified css stream
  stream = gulp.src(source.definitions + '/**/' + globs.components + '.less')
    .pipe(plumber(settings.plumber.less))
    .pipe(less(settings.less))
    .pipe(autoprefixer(settings.prefix))
    .pipe(replace(comments.variables.in, comments.variables.out))
    .pipe(replace(comments.license.in, comments.license.out))
    .pipe(replace(comments.large.in, comments.large.out))
    .pipe(replace(comments.small.in, comments.small.out))
    .pipe(replace(comments.tiny.in, comments.tiny.out))
    .pipe(flatten())
  ;

  // two concurrent streams from same source to concat release
  uncompressedStream = stream.pipe(clone());
  compressedStream   = stream.pipe(clone());

  // uncompressed component css
  uncompressedStream
    .pipe(plumber())
    .pipe(replace(assets.source, assets.uncompressed))
    .pipe(gulpif(config.hasPermission, chmod(config.permission)))
    .pipe(gulp.dest(output.uncompressed))
    .pipe(print(log.created))
    .on('end', function() {
      gulp.start('package uncompressed css');
      maybeCallback();
    })
  ;

  // compressed component css
  compressedStream
    .pipe(plumber())
    .pipe(clone())
    .pipe(replace(assets.source, assets.compressed))
    .pipe(minifyCSS(settings.minify))
    .pipe(rename(settings.rename.minCSS))
    .pipe(gulpif(config.hasPermission, chmod(config.permission)))
    .pipe(gulp.dest(output.compressed))
    .pipe(print(log.created))
    .on('end', function() {
      gulp.start('package compressed css');
      maybeCallback();
    })
  ;

};

/* Export with Metadata */
buildCSS.displayName = 'build-css';
buildCSS.description = 'Builds all css from source';
module.exports = series(buildCSS);


text:
from django.db import models
from django.utils.translation import ugettext_lazy as _

from linguo.managers import MultilingualManager
from linguo.models import MultilingualModel

class FooCategory(MultilingualModel):
    name = models.CharField(max_length=255, verbose_name=_('name'))

    objects = MultilingualManager()

    class Meta:
        ordering = ('name', 'id',)
        translate = ('name',)

class Foo(MultilingualModel):
    price = models.PositiveIntegerField(verbose_name=_('price'))
    name = models.CharField(max_length=255, verbose_name=_('name'))
    categories = models.ManyToManyField(FooCategory, blank=True)

    objects = MultilingualManager()

    class Meta:
        translate = ('name',)
        unique_together = ('name', 'price',)

class FooRel(models.Model):
    myfoo = models.ForeignKey(Foo)
    desc = models.CharField(max_length=255, verbose_name=_('desc'))

    objects = MultilingualManager()

class Moo(Foo):
    q1 = models.PositiveIntegerField()

    class Meta:
        unique_together = ('q1',)

class Bar(Foo):
    quantity = models.PositiveIntegerField(verbose_name=_('quantity'))
    description = models.CharField(max_length=255)

    objects = MultilingualManager()

    class Meta:
        translate = ('description',)

class BarRel(models.Model):
    mybar = models.ForeignKey(Bar)
    desc = models.CharField(max_length=255, verbose_name=_('desc'))

    objects = MultilingualManager()

class AbstractMoe(MultilingualModel):
    name = models.CharField(max_length=255, verbose_name=_('name'))
    price = models.PositiveIntegerField(verbose_name=_('price'))

    class Meta:
        abstract = True
        translate = ('name',)

class Moe(AbstractMoe):
    description = models.CharField(max_length=255,
        verbose_name=_('description'),
    )
    quantity = models.PositiveIntegerField(verbose_name=_('quantity'))

    class Meta:
        translate = ('description',)

class Gem(MultilingualModel):
    gemtype = models.CharField(max_length=255, verbose_name=_('gem type'),
        choices=(('a', 'A Type'), ('b', 'B Type'),)
    )
    somefoo = models.ForeignKey(Foo, null=True, blank=True)

    objects = MultilingualManager()

    class Meta:
        translate = ('gemtype',)

class Hop(MultilingualModel):
    name = models.CharField(max_length=255, verbose_name=_('name'))
    description = models.CharField(max_length=255,
        verbose_name=_('description'),
    )
    price = models.PositiveIntegerField(verbose_name=_('price'))

    objects = MultilingualManager()

    class Meta:
        translate = ('name', 'description',)

class Ord(Foo):
    last_name = models.CharField(max_length=255)

    objects = MultilingualManager()

    class Meta:
        ordering = ('name', 'last_name', 'id',)
        translate = ('last_name',)

    def __unicode__(self):
        return u'%s %s' % (self.name, self.last_name)

class Doc(MultilingualModel):
    pdf = models.FileField(upload_to='files/test/')

    class Meta:
        translate = ('pdf',)

class Lan(MultilingualModel):
    name = models.CharField(max_length=255)
    language = models.CharField(max_length=255, default=None)

    class Meta:
        translate = ('name',)

"""
class AbstractCar(models.Model):
    name = models.CharField(max_length=255, verbose_name=_('name'), default=None)
    price = models.PositiveIntegerField(verbose_name=_('price'))

    class Meta:
        abstract = True
        unique_together = ('name', 'price',)

class TransCar(MultilingualModel, AbstractCar):
    description = models.CharField(max_length=255,
        verbose_name=_('description'),
    )
    quantity = models.PositiveIntegerField(verbose_name=_('quantity'))

    class Meta:
        translate = ('description',)

class TransCarRel(models.Model):
    mytranscar = models.ForeignKey(TransCar)
    desc = models.CharField(max_length=255, verbose_name=_('desc'))

    objects = MultilingualManager()

class Coo(MultilingualModel, Car):
    q1 = models.PositiveIntegerField()

    class Meta:
        translate = ('price',)
        unique_together = ('q1',)

class TransAbstractCar(MultilingualModel, AbstractCar):
    description = models.CharField(max_length=255,
        verbose_name=_('description'),
    )
    quantity = models.PositiveIntegerField(verbose_name=_('quantity'))

    class Meta:
        translate = ('price', 'description')
"""


text:
                <img src="<?= $directoryAsset ?>/img/user2-160x160.jpg" class="img-circle" alt="User Image"/>

                 Online

        <form action="#" method="get" class="sidebar-form">

                <input type="text" name="q" class="form-control" placeholder="Search..."/>

                <button type='submit' name='search' id='search-btn' class="btn btn-flat">
                </button>

        </form>

        <?= dmstr\widgets\Menu::widget(
            [
                'options' => ['class' => 'sidebar-menu skin-purple-light' ],
                'items' => [
                    ['label' => 'Menu JY', 'options' => ['class' => 'header']],
                    ['label' => 'Anuncios', 'icon' => 'glyphicon glyphicon-bullhorn', 'url' => ['/anuncio']],
                    ['label' => 'Mis Datos', 'icon' => 'glyphicon glyphicon-user', 'url' => ['/user']],
                    ['label' => 'Reclamos', 'icon' => 'glyphicon glyphicon-registration-mark', 'url' => ['/reclamo']],
                    ['label' => 'Premios', 'icon' => 'glyphicon glyphicon-gift', 'url' => ['/premio']],
                    ['label' => 'Ventas', 'icon' => 'glyphicon glyphicon-usd', 'url' => ['/ventas-usuarios']],
                    ['label' => 'Paquetes', 'icon' => 'glyphicon glyphicon-shopping-cart', 'url' => ['/paquete-premium']],
                    ['label' => 'Login', 'url' => ['site/login'], 'visible' => Yii::$app->user->isGuest],
                    ],

            ]

        ) ?>


text:
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTopicSubscribersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('topic_subscribers', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');         // 用户Id
            $table->integer('topic_id');        // 主题Id
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('topic_subscribers');
    }
}


text:
// Copyright (c) 2017 Jan Pluskal
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

using Netfox.Core.Interfaces.Model.Exports;
using Netfox.Framework.Models.Snoopers;

namespace Netfox.Snoopers.SnooperXMPP.Models
{
    public class SnooperExportedObjectXMPP : SnooperExportedObjectBase, IChatMessage
    {
        private SnooperExportedObjectXMPP() { } //EF
        public SnooperExportedObjectXMPP(SnooperExportBase exportBase) : base(exportBase) {}

        #region Implementation of IChatMessage
        public string Message { get; internal set; }
        public string Sender { get; internal set; }
        public string Receiver { get; internal set; }
        #endregion
    }
}


text:
using System;
using System.Collections.Generic;
using System.Text;
using MbUnit.Framework;

namespace Rhino.Mocks.Tests.FieldsProblem
{
    [TestFixture]
    public class FieldProblem_SamW
    {
        [Test]
        public void UsingArrayAndOutParam()
        {
            MockRepository mockRepository = new MockRepository();
            ITest test = mockRepository.StrictMock<ITest>();
            string b;
            test.ArrayWithOut(new string[] { "data" }, out b);

            LastCall.Return("SuccessWithOut1").OutRef("SuccessWithOut2");

            mockRepository.ReplayAll();
            Console.WriteLine(test.ArrayWithOut(new string[] { "data" }, out b));
            Console.WriteLine(b);
        }

        public interface ITest
        {
            string ArrayWithOut(string[] a, out string b);
        }

    }
}


text:
import time
from contextlib import contextmanager

class TimeAccumulator(object):
    """
    Usage:

    t = TimeAccumulator()
    for i, x in enumerate(it):
        with t.execute():
            expensive_operation()
        if i % 50 == 0:
            print('Average: {}'.format(t.mean_time_spent()))

    """
    _EPS = 1e-8

    def __init__(self):
        self.times = []

    @contextmanager
    def execute(self):
        prev = time.time()
        yield
        self.times.append(time.time() - prev)

    def mean_time_spent(self):
        """ :returns mean time spent and resets cached times. """
        total_time_spent = sum(self.times)
        count = len(self.times)
        self.times = []
        return total_time_spent / (count + self._EPS)  # prevent div by zero errors

@contextmanager
def execute(name=''):
    start = time.time()
    yield
    duration = time.time() - start
    print('{}{:.5f}'.format(name + ': ' if name else '', duration))


text:
package org.apache.camel.component.jms;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.infra.activemq.services.ActiveMQService;
import org.junit.jupiter.api.Test;

public class JmsMessageCreatedStrategyComponentTest extends AbstractJMSTest {

    protected final String componentName = "activemq";

    @Test
    public void testMessageCreatedStrategy() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedMessageCount(1);
        mock.expectedHeaderReceived("beer", "Carlsberg");

        template.sendBody("activemq:queue:JmsMessageCreatedStrategyComponentTest", "Hello World");

        assertMockEndpointsSatisfied();
    }

    @Override
    public String getComponentName() {
        return componentName;
    }

    @Override
    protected JmsComponent setupComponent(CamelContext camelContext, ActiveMQService service, String componentName) {
        final JmsComponent jms = super.setupComponent(camelContext, service, componentName);
        jms.setMessageCreatedStrategy(new MyMessageCreatedStrategy());
        return jms;
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                from("activemq:queue:JmsMessageCreatedStrategyComponentTest")
                        .to("mock:result");
            }
        };
    }

    private static class MyMessageCreatedStrategy implements MessageCreatedStrategy {

        @Override
        public void onMessageCreated(Message message, Session session, Exchange exchange, Throwable cause) {
            try {
                JmsMessageHelper.setProperty(message, "beer", "Carlsberg");
            } catch (JMSException e) {
                // ignore
            }
        }
    }
}