text:
=================================
A Mechanism for Overriding Ufuncs
=================================

:Author: Blake Griffith
:Contact: blake.g@utexa.edu
:Date: 2013-07-10

:Author: Pauli Virtanen

:Author: Nathaniel Smith

Executive summary
=================

NumPy's universal functions (ufuncs) currently have some limited
functionality for operating on user defined subclasses of ndarray using
``__array_prepare__`` and ``__array_wrap__`` [1]_, and there is little
to no support for arbitrary objects. e.g. SciPy's sparse matrices [2]_
[3]_.

Here we propose adding a mechanism to override ufuncs based on the ufunc
checking each of it's arguments for a ``__numpy_ufunc__`` method.
On discovery of ``__numpy_ufunc__`` the ufunc will hand off the
operation to the method.

This covers some of the same ground as Travis Oliphant's proposal to
retro-fit NumPy with multi-methods [4]_, which would solve the same
problem. The mechanism here follows more closely the way Python enables
classes to override ``__mul__`` and other binary operations.

.. [1] http://docs.scipy.org/doc/numpy/user/basics.subclassing.html
.. [2] https://github.com/scipy/scipy/issues/2123
.. [3] https://github.com/scipy/scipy/issues/1569
.. [4] http://technicaldiscovery.blogspot.com/2013/07/thoughts-after-scipy-2013-and-specific.html

Motivation
==========

The current machinery for dispatching Ufuncs is generally agreed to be
insufficient. There have been lengthy discussions and other proposed
solutions [5]_.

Using ufuncs with subclasses of ndarray is limited to ``__array_prepare__`` and
``__array_wrap__`` to prepare the arguments, but these don't allow you to for
example change the shape or the data of the arguments. Trying to ufunc things
that don't subclass ndarray is even more difficult, as the input arguments tend
to be cast to object arrays, which ends up producing surprising results.

Take this example of ufuncs interoperability with sparse matrices.::

    In [1]: import numpy as np
    import scipy.sparse as sp

    a = np.random.randint(5, size=(3,3))
    b = np.random.randint(5, size=(3,3))

    asp = sp.csr_matrix(a)
    bsp = sp.csr_matrix(b)

    In [2]: a, b
    Out[2]:(array([[0, 4, 4],
                   [1, 3, 2],
                   [1, 3, 1]]),
            array([[0, 1, 0],
                   [0, 0, 1],
                   [4, 0, 1]]))

    In [3]: np.multiply(a, b) # The right answer
    Out[3]: array([[0, 4, 0],
                   [0, 0, 2],
                   [4, 0, 1]])

    In [4]: np.multiply(asp, bsp).todense() # calls __mul__ which does matrix multi
    Out[4]: matrix([[16,  0,  8],
                    [ 8,  1,  5],
                    [ 4,  1,  4]], dtype=int64)

    In [5]: np.multiply(a, bsp) # Returns NotImplemented to user, bad!
    Out[5]: NotImplemted

Returning ``NotImplemented`` to user should not happen. Moreover::

    In [6]: np.multiply(asp, b)
    Out[6]: array([[ <3x3 sparse matrix of type '<class 'numpy.int64'>'
                    with 8 stored elements in Compressed Sparse Row format>,
                        <3x3 sparse matrix of type '<class 'numpy.int64'>'
                    with 8 stored elements in Compressed Sparse Row format>,
                        <3x3 sparse matrix of type '<class 'numpy.int64'>'
                    with 8 stored elements in Compressed Sparse Row format>],
                       [ <3x3 sparse matrix of type '<class 'numpy.int64'>'
                    with 8 stored elements in Compressed Sparse Row format>,
                        <3x3 sparse matrix of type '<class 'numpy.int64'>'
                    with 8 stored elements in Compressed Sparse Row format>,
                        <3x3 sparse matrix of type '<class 'numpy.int64'>'
                    with 8 stored elements in Compressed Sparse Row format>],
                       [ <3x3 sparse matrix of type '<class 'numpy.int64'>'
                    with 8 stored elements in Compressed Sparse Row format>,
                        <3x3 sparse matrix of type '<class 'numpy.int64'>'
                    with 8 stored elements in Compressed Sparse Row format>,
                        <3x3 sparse matrix of type '<class 'numpy.int64'>'
                    with 8 stored elements in Compressed Sparse Row format>]], dtype=object)

Here, it appears that the sparse matrix was converted to a object array
scalar, which was then multiplied with all elements of the ``b`` array.
However, this behavior is more confusing than useful, and having a
``TypeError`` would be preferable.

Adding the ``__numpy_ufunc__`` functionality fixes this and would
deprecate the other ufunc modifying functions.

.. [5] http://mail.scipy.org/pipermail/numpy-discussion/2011-June/056945.html

Proposed interface
==================

Objects that want to override Ufuncs can define a ``__numpy_ufunc__`` method.
The method signature is::

    def __numpy_ufunc__(self, ufunc, method, i, inputs, **kwargs)

Here:

- *ufunc* is the ufunc object that was called.
- *method* is a string indicating which Ufunc method was called
  (one of ``"__call__"``, ``"reduce"``, ``"reduceat"``,
  ``"accumulate"``, ``"outer"``, ``"inner"``).
- *i* is the index of *self* in *inputs*.
- *inputs* is a tuple of the input arguments to the ``ufunc``
- *kwargs* are the keyword arguments passed to the function. The ``out``
  arguments are always contained in *kwargs*, how positional variables
  are passed is discussed below.

The ufunc's arguments are first normalized into a tuple of input data
(``inputs``), and dict of keyword arguments. If there are output
arguments they are handeled as follows:

- One positional output variable x is passed in the kwargs dict as ``out :
  x``.
- Multiple positional output variables ``x0, x1, ...`` are passed as a tuple
  in the kwargs dict as ``out : (x0, x1, ...)``.
- Keyword output variables like ``out = x`` and ``out = (x0, x1, ...)`` are
  passed unchanged to the kwargs dict like ``out : x`` and ``out : (x0, x1,
  ...)`` respectively.
- Combinations of positional and keyword output variables are not
  supported.

The function dispatch proceeds as follows:

- If one of the input arguments implements ``__numpy_ufunc__`` it is
  executed instead of the Ufunc.

- If more than one of the input arguments implements ``__numpy_ufunc__``,
  they are tried in the following order: subclasses before superclasses,
  otherwise left to right.  The first ``__numpy_ufunc__`` method returning
  something else than ``NotImplemented`` determines the return value of
  the Ufunc.

- If all ``__numpy_ufunc__`` methods of the input arguments return
  ``NotImplemented``, a ``TypeError`` is raised.

- If a ``__numpy_ufunc__`` method raises an error, the error is propagated
  immediately.

If none of the input arguments has a ``__numpy_ufunc__`` method, the
execution falls back on the default ufunc behaviour.

In combination with Python's binary operations
----------------------------------------------

The ``__numpy_ufunc__`` mechanism is fully independent of Python's
standard operator override mechanism, and the two do not interact
directly.

They however have indirect interactions, because Numpy's ``ndarray``
type implements its binary operations via Ufuncs. Effectively, we have::

    class ndarray(object):
        ...
        def __mul__(self, other):
            return np.multiply(self, other)

Suppose now we have a second class::

    class MyObject(object):
        def __numpy_ufunc__(self, *a, **kw):
            return "ufunc"
        def __mul__(self, other):
            return 1234
        def __rmul__(self, other):
            return 4321

In this case, standard Python override rules combined with the above
discussion imply::

    a = MyObject()
    b = np.array([0])

    a * b    # == 1234       OK
    b * a    # == "ufunc"    surprising

This is not what would be naively expected, and is therefore somewhat
undesirable behavior.

The reason why this occurs is: because ``MyObject`` is not an ndarray
subclass, Python resolves the expression ``b * a`` by calling first
``b.__mul__``. Since Numpy implements this via an Ufunc, the call is
forwarded to ``__numpy_ufunc__`` and not to ``__rmul__``.  Note that if
``MyObject`` is a subclass of ``ndarray``, Python calls ``a.__rmul__``
first. The issue is therefore that ``__numpy_ufunc__`` implements
"virtual subclassing" of ndarray behavior, without actual subclassing.

This issue can be resolved by a modification of the binary operation
methods in Numpy::

    class ndarray(object):
        ...
        def __mul__(self, other):
            if (not isinstance(other, self.__class__)
                    and hasattr(other, '__numpy_ufunc__')
                    and hasattr(other, '__rmul__')):
                return NotImplemented
            return np.multiply(self, other)

        def __imul__(self, other):
            if (other.__class__ is not self.__class__
                    and hasattr(other, '__numpy_ufunc__')
                    and hasattr(other, '__rmul__')):
                return NotImplemented
            return np.multiply(self, other, out=self)

    b * a    # == 4321    OK

The rationale here is the following: since the user class explicitly
defines both ``__numpy_ufunc__`` and ``__rmul__``, the implementor has
very likely made sure that the ``__rmul__`` method can process ndarrays.
If not, the special case is simple to deal with (just call
``np.multiply``).

The exclusion of subclasses of self can be made because Python itself
calls the right-hand method first in this case. Moreover, it is
desirable that ndarray subclasses are able to inherit the right-hand
binary operation methods from ndarray.

The same priority shuffling needs to be done also for the in-place
operations, so that ``MyObject.__rmul__`` is prioritized over
``ndarray.__imul__``.

Demo
====

A pull request[6]_ has been made including the changes proposed in this NEP.
Here is a demo highlighting the functionality.::

    In [1]: import numpy as np;

    In [2]: a = np.array([1])

    In [3]: class B():
       ...:     def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs):
       ...:         return "B"
       ...:

    In [4]: b = B()

    In [5]: np.dot(a, b)
    Out[5]: 'B'

    In [6]: np.multiply(a, b)
    Out[6]: 'B'

A simple ``__numpy_ufunc__`` has been added to SciPy's sparse matrices
Currently this only handles ``np.dot`` and ``np.multiply`` because it was the
two most common cases where users would attempt to use sparse matrices with ufuncs.
The method is defined below::

    def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs):
        """Method for compatibility with NumPy's ufuncs and dot
        functions.
        """

        without_self = list(inputs)
        del without_self[pos]
        without_self = tuple(without_self)

        if func == np.multiply:
            return self.multiply(*without_self)

        elif func == np.dot:
            if pos == 0:
                return self.__mul__(inputs[1])
            if pos == 1:
                return self.__rmul__(inputs[0])
        else:
            return NotImplemented

So we now get the expected behavior when using ufuncs with sparse matrices.::

        In [1]: import numpy as np; import scipy.sparse as sp

        In [2]: a = np.random.randint(3, size=(3,3))

        In [3]: b = np.random.randint(3, size=(3,3))

        In [4]: asp = sp.csr_matrix(a); bsp = sp.csr_matrix(b)

        In [5]: np.dot(a,b)
        Out[5]:
        array([[2, 4, 8],
               [2, 4, 8],
                [2, 2, 3]])

        In [6]: np.dot(asp,b)
        Out[6]:
        array([[2, 4, 8],
               [2, 4, 8],
               [2, 2, 3]], dtype=int64)

        In [7]: np.dot(asp, bsp).A
        Out[7]:
        array([[2, 4, 8],
               [2, 4, 8],
               [2, 2, 3]], dtype=int64)

.. Local Variables:
.. mode: rst
.. coding: utf-8
.. fill-column: 72
.. End:


text:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.kaznowski.hugh</groupId>
    <artifactId>pairsInACollection</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>
    </dependencies>
</project>

redpajama_set_name: RedPajamaGithub


text:
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework import serializers
from rest_framework import generics
from rest_framework import status
from rest_framework.generics import GenericAPIView
from core.models import *
from django.forms import widgets
from rest_framework import filters
from django.conf.urls import patterns, url
from rest_framework.exceptions import PermissionDenied as RestFrameworkPermissionDenied
from django.core.exceptions import PermissionDenied as DjangoPermissionDenied
from apibase import XOSRetrieveUpdateDestroyAPIView, XOSListCreateAPIView, XOSNotAuthenticated

if hasattr(serializers, "ReadOnlyField"):
    # rest_framework 3.x
    IdField = serializers.ReadOnlyField
else:
    # rest_framework 2.x
    IdField = serializers.Field

"""
    Schema of the generator object:
        all: Set of all Model objects
        all_if(regex): Set of Model objects that match regex

    Model object:
        plural: English plural of object name
        camel: CamelCase version of object name
        refs: list of references to other Model objects
        props: list of properties minus refs

    TODO: Deal with subnets
"""

def get_REST_patterns():
    return patterns('',
    # legacy - deprecated
        url(r'^xos/$', api_root),
    {% for object in generator.all %}
        url(r'xos/{{ object.rest_name }}/$', {{ object.camel }}List.as_view(), name='{{ object.singular }}-list-legacy'),
        url(r'xos/{{ object.rest_name }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel }}Detail.as_view(), name ='{{ object.singular }}-detail-legacy'),
    {% endfor %}
    ) + patterns('',
    # new - use these instead of the above
        url(r'^api/core/$', api_root),
    {% for object in generator.all %}
        url(r'api/core/{{ object.rest_name }}/$', {{ object.camel }}List.as_view(), name='{{ object.singular }}-list'),
        url(r'api/core/{{ object.rest_name }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel }}Detail.as_view(), name ='{{ object.singular }}-detail'),
    {% endfor %}
    )

@api_view(['GET'])
def api_root_legacy(request, format=None):
    return Response({
        {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list-legacy', request=request, format=format),
        {% endfor %}
    })

@api_view(['GET'])
def api_root(request, format=None):
    return Response({
        {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list', request=request, format=format),
        {% endfor %}
    })

# Based on serializers.py

class XOSModelSerializer(serializers.ModelSerializer):
    def save_object(self, obj, **kwargs):

        """ rest_framework can't deal with ManyToMany relations that have a
            through table. In xos, most of the through tables we have
            use defaults or blank fields, so there's no reason why we shouldn't
            be able to save these objects.

            So, let's strip out these m2m relations, and deal with them ourself.
        """
        obj._complex_m2m_data={};
        if getattr(obj, '_m2m_data', None):
            for relatedObject in obj._meta.get_all_related_many_to_many_objects():
                if (relatedObject.field.rel.through._meta.auto_created):
                    # These are non-trough ManyToMany relations and
                    # can be updated just fine
                    continue
                fieldName = relatedObject.get_accessor_name()
                if fieldName in obj._m2m_data.keys():
                    obj._complex_m2m_data[fieldName] = (relatedObject, obj._m2m_data[fieldName])
                    del obj._m2m_data[fieldName]

        serializers.ModelSerializer.save_object(self, obj, **kwargs);

        for (accessor, stuff) in obj._complex_m2m_data.items():
            (relatedObject, data) = stuff
            through = relatedObject.field.rel.through
            local_fieldName = relatedObject.field.m2m_reverse_field_name()
            remote_fieldName = relatedObject.field.m2m_field_name()

            # get the current set of existing relations
            existing = through.objects.filter(**{local_fieldName: obj});

            data_ids = [item.id for item in data]
            existing_ids = [getattr(item,remote_fieldName).id for item in existing]

            #print "data_ids", data_ids
            #print "existing_ids", existing_ids

            # remove relations that are in 'existing' but not in 'data'
            for item in list(existing):
               if (getattr(item,remote_fieldName).id not in data_ids):
                   print "delete", getattr(item,remote_fieldName)
                   item.delete() #(purge=True)

            # add relations that are in 'data' but not in 'existing'
            for item in data:
               if (item.id not in existing_ids):
                   #print "add", item
                   newModel = through(**{local_fieldName: obj, remote_fieldName: item})
                   newModel.save()

{% for object in generator.all %}

class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer):
    id = IdField()
    {% for ref in object.refs %}
    {% if ref.multi %}
    {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail')
    {% else %}
    {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail')
    {% endif %}
    {% endfor %}
    humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
    validators = serializers.SerializerMethodField("getValidators")
    def getHumanReadableName(self, obj):
        return str(obj)
    def getValidators(self, obj):
        try:
            return obj.getValidators()
        except:
            return None
    class Meta:
        model = {{ object.camel }}
        fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})

class {{ object.camel }}IdSerializer(XOSModelSerializer):
    id = IdField()
    {% for ref in object.refs %}
    {% if ref.multi %}
    {{ ref.plural }} = serializers.PrimaryKeyRelatedField(many=True,  queryset = {{ ref.camel }}.objects.all())
    {% else %}
    {{ ref }} = serializers.PrimaryKeyRelatedField( queryset = {{ ref.camel }}.objects.all())
    {% endif %}
    {% endfor %}
    humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
    validators = serializers.SerializerMethodField("getValidators")
    def getHumanReadableName(self, obj):
        return str(obj)
    def getValidators(self, obj):
        try:
            return obj.getValidators()
        except:
            return None
    class Meta:
        model = {{ object.camel }}
        fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})

{% endfor %}

serializerLookUp = {
{% for object in generator.all %}
                 {{ object.camel }}: {{ object.camel }}Serializer,
{% endfor %}
                 None: None,
                }

# Based on core/views/*.py
{% for object in generator.all %}

class {{ object.camel }}List(XOSListCreateAPIView):
    queryset = {{ object.camel }}.objects.select_related().all()
    serializer_class = {{ object.camel }}Serializer
    id_serializer_class = {{ object.camel }}IdSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})

    def get_serializer_class(self):
        no_hyperlinks=False
        if hasattr(self.request,"query_params"):
            no_hyperlinks = self.request.query_params.get('no_hyperlinks', False)
        if (no_hyperlinks):
            return self.id_serializer_class
        else:
            return self.serializer_class

    def get_queryset(self):
        if (not self.request.user.is_authenticated()):
            raise XOSNotAuthenticated()
        return {{ object.camel }}.select_by_user(self.request.user)

class {{ object.camel }}Detail(XOSRetrieveUpdateDestroyAPIView):
    queryset = {{ object.camel }}.objects.select_related().all()
    serializer_class = {{ object.camel }}Serializer
    id_serializer_class = {{ object.camel }}IdSerializer

    def get_serializer_class(self):
        no_hyperlinks=False
        if hasattr(self.request,"query_params"):
            no_hyperlinks = self.request.query_params.get('no_hyperlinks', False)
        if (no_hyperlinks):
            return self.id_serializer_class
        else:
            return self.serializer_class

    def get_queryset(self):
        if (not self.request.user.is_authenticated()):
            raise XOSNotAuthenticated()
        return {{ object.camel }}.select_by_user(self.request.user)

    # update() is handled by XOSRetrieveUpdateDestroyAPIView

    # destroy() is handled by XOSRetrieveUpdateDestroyAPIView

{% endfor %}


text: <img style="border:0" src="{% url django-counter-count ctype.id object.id %}" alt="" />


text:
package ylabs.play.common.services

import java.util.concurrent.TimeUnit
import javax.inject.Singleton

import com.google.maps.model.{ComponentFilter, LatLng, PlaceAutocompleteType}
import com.google.maps.{GeoApiContext, GeocodingApi, PlacesApi}
import com.typesafe.config.ConfigFactory
import springnz.util.Logging
import ylabs.play.common.models.GeoLocation.{Address, AutoCompleteComponent, AutoCompleteFilter, AutoCompleteQuery}
import ylabs.play.common.models.Location.{LatLong, Latitude, Longitude}

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}

@Singleton
class GeoLocationService extends Logging {
  lazy val config = ConfigFactory.load()
  lazy val apiKey = config.getString("google.apiKey")

  def newContext = new GeoApiContext()
    .setApiKey(apiKey)
    .setQueryRateLimit(10)
    .setConnectTimeout(1, TimeUnit.SECONDS)
    .setReadTimeout(1, TimeUnit.SECONDS)
    .setWriteTimeout(1, TimeUnit.SECONDS)

  def newRequest = {
    GeocodingApi.newRequest(newContext)
  }

  def lookup(address: Address)(implicit ec: ExecutionContext): Future[Option[LatLong]] =
    Future {
      Try {
        newRequest.address(address.value + " New Zealand").await.toList
      } match {
        case Success(geoLocationResults) ⇒
          log.info(s"geolocation results for $address: $geoLocationResults")
          val result = geoLocationResults.headOption //we assume the list is ordered, just take the first result
            .map(_.geometry.location)
            .map { latLong ⇒ LatLong(Latitude(latLong.lat), Longitude(latLong.lng)) }
          log.info(s"returning geolocation for $address: $result")
          result
        case Failure(t) ⇒
          log.error("unable to lookup geolocation", t)
          None
      }
    }

  def suggest(input: AutoCompleteQuery,
              types: Option[AutoCompleteFilter] = None,
              components: Option[List[AutoCompleteComponent]] = None)
             (implicit ec: ExecutionContext): Future[Option[List[Address]]] = {
    Future {
      Try {
        val componentFilters = components.getOrElse(List()).map(c => c.key match {
          case "route" => ComponentFilter.route(c.value)
          case "locality" => ComponentFilter.locality(c.value)
          case "administrative_area" => ComponentFilter.administrativeArea(c.value)
          case "postal_code" => ComponentFilter.postalCode(c.value)
          case "country" => ComponentFilter.country(c.value)
          case bad => throw new Exception(s"Don't know how to process component $bad")
        })

        val request = types match {
          case Some(filter) =>
            val autoCompleteType = PlaceAutocompleteType.valueOf(filter.value)
            PlacesApi.placeAutocomplete(newContext, input.value)
              .`type`(autoCompleteType)
              .components(componentFilters: _*)
              .await()
              .toList
          case None => PlacesApi.placeAutocomplete(newContext, input.value)
            .components(componentFilters: _*)
            .await()
            .toList
        }

        request
      } match {
        case Success(autoCompleteResults) =>
          log.info(s"autocomplete results for $input: $autoCompleteResults")
          Some(autoCompleteResults.map(a =>  Address(a.description)))
        case Failure(t) =>
          log.error("unable to lookup autocomplete", t)
          None
      }
    }
  }

  def reverseLookup(latlong: LatLong)(implicit ec: ExecutionContext): Future[Option[Address]] =
    Future {
      Try {
        newRequest.latlng(new LatLng(latlong.latitude.value, latlong.longitude.value)).await().headOption
      } match {
        case Success(geoLocationResult) =>
          log.info(s"gelocation results for $latlong: $geoLocationResult")
          geoLocationResult map { res => Address(res.formattedAddress) }
        case Failure(t) =>
          log.error("unable to lookup geolocation", t)
          None
      }
    }
}


text:
package org.jnet.core.synchronizer.message;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Map.Entry;

import org.jnet.core.helper.Unchecker;
import org.jnet.core.synchronizer.MetaData;
import org.jnet.core.synchronizer.ObjectChangeProvider;
import org.jnet.core.synchronizer.ObjectId;

public class UpdateObjectMessage extends AbstractUpdateMessage {
	private Map<String, Object> fieldsToUpdate;

	public UpdateObjectMessage(ObjectId objectId, Map<String, Object> fieldsToUpdate) {
		super(objectId);
		this.fieldsToUpdate = fieldsToUpdate;
	}

	@Override
	public void apply(ObjectChangeProvider changeProvider) {
		Unchecker.uncheck(() -> {
			Object objectToUpdate = changeProvider.getObject(objectId);
			MetaData metaData = changeProvider.getMetaData(objectToUpdate);

			for (Entry<String, Object> entry : fieldsToUpdate.entrySet()) {
				Field field = metaData.getField(entry.getKey());
				Object newValue = entry.getValue();
				field.setAccessible(true);
				field.set(objectToUpdate, unwrap(changeProvider, newValue));
			}
		});
	}
}


text:
package avrora.arch.legacy;

import avrora.sim.*;

/**
 * The LegacyState class represents the state of the simulator, including the contents of registers and
 * memory.
 *
 * @author Ben L. Titzer
 */
public interface LegacyState extends State {

    int NUM_REGS = 32; // number of general purpose registers
    int IOREG_BASE = 32; // base address of IO registers
    int SREG_I = 7;
    int SREG_T = 6;
    int SREG_H = 5;
    int SREG_S = 4;
    int SREG_V = 3;
    int SREG_N = 2;
    int SREG_Z = 1;
    int SREG_C = 0;
    int SREG_I_MASK = 1 << SREG_I;
    int SREG_T_MASK = 1 << SREG_T;
    int SREG_H_MASK = 1 << SREG_H;
    int SREG_S_MASK = 1 << SREG_S;
    int SREG_V_MASK = 1 << SREG_V;
    int SREG_N_MASK = 1 << SREG_N;
    int SREG_Z_MASK = 1 << SREG_Z;
    int SREG_C_MASK = 1;

    /**
     * The getInterruptTable() method gets a reference to the interrupt table,
     * which contains information about each interrupt, such as whether it is enabled, posted,
     * pending, etc.
     * @return a reference to the InterruptTable instance
     */
    public InterruptTable getInterruptTable();

    /**
     * Read a general purpose register's current value as a byte.
     *
     * @param reg the register to read
     * @return the current value of the register
     */
    public byte getRegisterByte(LegacyRegister reg);

    /**
     * Read a general purpose register's current value as an integer, without any sign extension.
     *
     * @param reg the register to read
     * @return the current unsigned value of the register
     */
    public int getRegisterUnsigned(LegacyRegister reg);

    /**
     * Read a general purpose register pair as an unsigned word. This method will read the value of the
     * specified register and the value of the next register in numerical order and return the two values
     * combined as an unsigned integer The specified register should be less than r31, because r32 (the next
     * register) does not exist.
     *
     * @param reg the low register of the pair to read
     * @return the current unsigned word value of the register pair
     */
    public int getRegisterWord(LegacyRegister reg);

    /**
     * The getSREG() method reads the value of the status register. The status register contains
     * the I, T, H, S, V, N, Z, and C flags, in order from highest-order to lowest-order.
     *
     * @return the value of the status register as a byte.
     */
    public byte getSREG();

    /**
     * The getStackByte() method reads a byte from the address specified by SP+1. This method
     * should not be called with an empty stack, as it will cause an exception consistent with trying to read
     * non-existent memory.
     *
     * @return the value on the top of the stack
     */
    public byte getStackByte();

    /**
     * The getDataByte() method reads a byte value from the data memory (SRAM) at the specified
     * address.
     *
     * @param address the byte address to read
     * @return the value of the data memory at the specified address
     * @throws ArrayIndexOutOfBoundsException if the specified address is not the valid memory range
     */
    public byte getDataByte(int address);

    /**
     * The getProgramByte() method reads a byte value from the program (Flash) memory. The flash
     * memory generally stores read-only values and the instructions of the program. Care should be taken that
     * the program memory at the specified address does not contain an instruction. This is because, in
     * general, programs should not read instructions as data, and secondly, because no assembler is present
     * in Avrora and therefore the actual byte value of an instruction may not be known.
     *
     * @param address the byte address at which to read
     * @return the byte value of the program memory at the specified address
     * @throws ArrayIndexOutOfBoundsException if the specified address is not the valid program memory range
     */
    public byte getProgramByte(int address);

    /**
     * The getIORegisterByte() method reads the value of an IO register. Invocation of this
     * method causes an invocatiobn of the .read() method on the corresponding internal
     * IOReg object, and its value returned.
     *
     * @param ioreg the IO register number
     * @return the value of the IO register
     */
    public byte getIORegisterByte(int ioreg);

    /**
     * The getIOReg() method is used to retrieve a reference to the actual IOReg
     * instance stored internally in the state. This is generally only used in the simulator and device
     * implementations, and clients should probably not call this memory directly.
     *
     * @param ioreg the IO register number to retrieve
     * @return a reference to the ActiveRegister instance of the specified IO register
     */
    public ActiveRegister getIOReg(int ioreg);

    /**
     * The getSleepMode() method returns an integer code describing which sleep mode the
     * microcontroller is currently in.
     *
     * @return an integer code representing the current sleep mode
     */
    public int getSleepMode();

}


text:
package org.apache.ignite.internal.processors.platform.callback;

/**
 * Platform callback operation codes.
 */
class PlatformCallbackOp {
    /** */
    public static final int CacheStoreCreate = 1;

    /** */
    public static final int CacheStoreInvoke = 2;

    /** */
    public static final int CacheStoreDestroy = 3;

    /** */
    public static final int CacheStoreSessionCreate = 4;

    /** */
    public static final int CacheEntryFilterCreate = 5;

    /** */
    public static final int CacheEntryFilterApply = 6;

    /** */
    public static final int CacheEntryFilterDestroy = 7;

    /** */
    public static final int CacheInvoke = 8;

    /** */
    public static final int ComputeTaskMap = 9;

    /** */
    public static final int ComputeTaskJobResult = 10;

    /** */
    public static final int ComputeTaskReduce = 11;

    /** */
    public static final int ComputeTaskComplete = 12;

    /** */
    public static final int ComputeJobSerialize = 13;

    /** */
    public static final int ComputeJobCreate = 14;

    /** */
    public static final int ComputeJobExecute = 15;

    /** */
    public static final int ComputeJobCancel = 16;

    /** */
    public static final int ComputeJobDestroy = 17;

    /** */
    public static final int ContinuousQueryListenerApply = 18;

    /** */
    public static final int ContinuousQueryFilterCreate = 19;

    /** */
    public static final int ContinuousQueryFilterApply = 20;

    /** */
    public static final int ContinuousQueryFilterRelease = 21;

    /** */
    public static final int DataStreamerTopologyUpdate = 22;

    /** */
    public static final int DataStreamerStreamReceiverInvoke = 23;

    /** */
    public static final int FutureByteResult = 24;

    /** */
    public static final int FutureBoolResult = 25;

    /** */
    public static final int FutureShortResult = 26;

    /** */
    public static final int FutureCharResult = 27;

    /** */
    public static final int FutureIntResult = 28;

    /** */
    public static final int FutureFloatResult = 29;

    /** */
    public static final int FutureLongResult = 30;

    /** */
    public static final int FutureDoubleResult = 31;

    /** */
    public static final int FutureObjectResult = 32;

    /** */
    public static final int FutureNullResult = 33;

    /** */
    public static final int FutureError = 34;

    /** */
    public static final int LifecycleOnEvent = 35;

    /** */
    public static final int MemoryReallocate = 36;

    /** */
    public static final int MessagingFilterCreate = 37;

    /** */
    public static final int MessagingFilterApply = 38;

    /** */
    public static final int MessagingFilterDestroy = 39;

    /** */
    public static final int EventFilterCreate = 40;

    /** */
    public static final int EventFilterApply = 41;

    /** */
    public static final int EventFilterDestroy = 42;

    /** */
    public static final int ServiceInit = 43;

    /** */
    public static final int ServiceExecute = 44;

    /** */
    public static final int ServiceCancel = 45;

    /** */
    public static final int ServiceInvokeMethod = 46;

    /** */
    public static final int ClusterNodeFilterApply = 47;

    /** */
    public static final int NodeInfo = 48;

    /** */
    public static final int OnStart = 49;

    /** */
    public static final int OnStop = 50;

    /** */
    public static final int ExtensionInLongOutLong = 51;

    /** */
    public static final int ExtensionInLongLongOutLong = 52;

    /** */
    public static final int OnClientDisconnected = 53;

    /** */
    public static final int OnClientReconnected = 54;

    /** */
    public static final int AffinityFunctionInit = 55;

    /** */
    public static final int AffinityFunctionPartition = 56;

    /** */
    public static final int AffinityFunctionAssignPartitions = 57;

    /** */
    public static final int AffinityFunctionRemoveNode = 58;

    /** */
    public static final int AffinityFunctionDestroy = 59;

    /** */
    public static final int ComputeTaskLocalJobResult = 60;

    /** */
    public static final int ComputeJobExecuteLocal = 61;

    /** */
    public static final int PluginProcessorStop = 62;

    /** */
    public static final int PluginProcessorIgniteStop = 63;

    /** */
    public static final int CachePluginCreate = 64;

    /** */
    public static final int CachePluginDestroy = 65;

    /** */
    public static final int CachePluginIgniteStart = 66;

    /** */
    public static final int CachePluginIgniteStop = 67;

    /** */
    public static final int PluginCallbackInLongLongOutLong = 68;

    /** */
    public static final int EventLocalListenerApply = 69;

    /** */
    public static final int PlatformCacheUpdate = 70;

    /** */
    public static final int PlatformCacheUpdateFromThreadLocal = 71;

    /** */
    public static final int OnCacheStopped = 72;

    /** */
    public static final int OnAffinityTopologyVersionChanged = 73;

    /** */
    public static final int ComputeOutFuncExecute = 74;

    /** */
    public static final int ComputeActionExecute = 75;
}


text:
#pragma once
#include <aws/medialive/MediaLive_EXPORTS.h>

namespace Aws
{
template<typename RESULT_TYPE>
class AmazonWebServiceResult;

namespace Utils
{
namespace Json
{
  class JsonValue;
} // namespace Json
} // namespace Utils
namespace MediaLive
{
namespace Model
{
  /**
   * Placeholder documentation for CancelInputDeviceTransferResponse

See
   * Also:
   AWS
   * API Reference

   */
  class AWS_MEDIALIVE_API CancelInputDeviceTransferResult
  {
  public:
    CancelInputDeviceTransferResult();
    CancelInputDeviceTransferResult(const Aws::AmazonWebServiceResult<Aws::Utils::Json::JsonValue>& result);
    CancelInputDeviceTransferResult& operator=(const Aws::AmazonWebServiceResult<Aws::Utils::Json::JsonValue>& result);

  };

} // namespace Model
} // namespace MediaLive
} // namespace Aws


text:
Hermes wraps the `jmoiron/sqlx` *sqlx.DB and *sqlx.Tx models in a common
interface, hermes.Conn.  Makes it easier to build small functions that can
be aggregated and used in a single transaction, as well as for testing.

[![Godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/sbowman/hermes)
![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)

## Usage

    func Sample(conn hermes.Conn, name string) error {
        tx, err := conn.Begin()
        if err != nil {
            return err
        }

        // Will automatically rollback if an error short-circuits the return
        // before tx.Commit() is called...
        defer tx.Close()

        res, err := conn.Exec("insert into samples (name) values ($1)", name)
        if err != nil {
            return err
        }

        check, err := res.RowsAffected()
        if check == 0 {
            return fmt.Errorf("Failed to insert row (%s)", err)
        }

        return tx.Commit()
    }

    func main() {
        // Create a connection pool with max 10 connections, min 2 idle connections...
        conn, err := hermes.Connect("postgres",
            "postgres://postgres@127.0.0.1/engaged?sslmode=disable&connect_timeout=10",
            10, 2)
        if err != nil {
            return err
        }

        // This works...
        if err := Sample(conn, "Bob"); err != nil {
            fmt.Println("Bob failed!", err.Error())
        }

        // So does this...
        tx, err := conn.Begin()
        if err != nil {
            panic(err)
        }

        // Will automatically rollback if call to sample fails...
        defer tx.Close()

        if err := Sample(tx, "Frank"); err != nil {
            fmt.Println("Frank failed!", err.Error())
            return
        }

        // Don't forget to commit, or you'll automatically rollback on
        // "defer tx.Close()" above!
        tx.Commit()
    }

Using a `hermes.Conn` parameter in a function also opens up *in situ* testing
of database functionality.  You can create a transaction in the test case and
pass it to a function that takes a `hermes.Conn`, run any tests on the results
of that function, and simply let the transaction rollback at the end of the
test to clean up.

    var Conn hermes.Conn

    // We'll just open one database connection pool to speed up testing, so
    // we're not constantly opening and closing connections.
    func TestMain(m *testing.M) {
	    conn, err := hermes.Connect("postgres", DBTestURI, 5, 2)
	    if err != nil {
	        fmt.Fprintf(os.Stderr, "Unable to open a database connection: %s\n", err)
	        os.Exit(1)
    	}
    	defer conn.Close()

    	Conn = conn

    	os.Exit(m.Run())
    }

    // Test getting a user account from the database.  The signature for the
    // function is:  `func GetUser(conn hermes.Conn, email string) (User, error)`
    //
    // Passing a hermes.Conn value to the function means we can pass in either
    // a reference to the database pool and really update the data, or we can
    // pass in the same transaction reference to both the SaveUser and GetUser
    // functions.  If we use a transaction, we can let the transaction roll back
    // after we test these functions, or at any failure point in the test case,
    // and we know the data is cleaned up.
    func TestGetUser(t *testing.T) {
        u := User{
            Email: "jdoe@nowhere.com",
            Name: "John Doe",
        }

        tx, err := Conn.Begin()
        if err != nil {
            t.Fatal(err)
        }
        defer tx.Close()

        if err := db.SaveUser(tx, u); err != nil {
            t.Fatalf("Unable to create a new user account: %s", err)
        }

        check, err := db.GetUser(tx, u.Email)
        if err != nil {
            t.Fatalf("Failed to get user by email address: %s", err)
        }

        if check.Email != u.Email {
            t.Errorf("Expected user email to be %s; was %s", u.Email, check.Email)
        }

        if check.Name != u.Name {
            t.Errorf("Expected user name to be %s; was %s", u.Name, check.Name)
        }

        // Note:  do nothing...when the test case ends, the `defer tx.Close()`
        // is called, and all the data in this transaction is rolled back out.
    }

Using transactions, even if a test case fails a returns prematurely, the
database transaction is automatically closed, thanks to `defer`.  The database
is cleaned up without any fuss or need to remember to delete the data you
created at any point in the test.

## Confirm (1.2.3)

If the network environment is unstable, Hermes may be configured to retry
connections from the connection pool if those pooled connections lose their
connectivity to the database.

To enable connection confirmations, set the `hermes.Confirm` global variable to
a number greater than 0:

    // Create a connection pool with max 10 connections, min 2 idle connections...
    conn, err := hermes.Connect("postgres",
        "postgres://postgres@127.0.0.1/engaged?sslmode=disable&connect_timeout=10",
        10, 2)
    if err != nil {
        return err
    }

    // Check each database connection at least twice before panicking
    hermes.Confirm = 2

When confirmation is enabled, Hermes pings the database prior to making any
database requests (begin a transaction, select, insert, etc.).  If the ping
fails, Hermes waits a moment and tries again, up to the number of times
specified in `hermes.Confirm`.  Each try, the `sql.Ping()` function tries to
reconnect to the database.

If Hermes can't open the database connection again after trying repeatedly, it
panics and crashes the application.  Ideally systemd, Kubernetes, or whatever
monitor is watching the application will restart the app and clear up the cause
of the problem, or at least alert someone there's a problem.

The `hermes.Confirm` functionality should be coupled with a `connect_timeout`
value in the PostgreSQL configuration, or the equivalent for whatever database
is being used.

This check is not performed with queries made within a transaction.  If the
connection is lost mid-transaction, there is no point trying to reconnect, as
the transaction is lost.  At that point, the transaction should simply fail.

There is the performance hit of an additional `sql.Ping()` request with nearly
every database query.  If you don't need this functionality, we recommend you
don't enable it.

By default this functionality is *disabled*.

## OnFailure (1.1.x)

Hermes supports an `OnFailure` function that may be called any time a database
error appears to be an unrecoverable connection or server failure.  This
function is set on the database connection (`hermes.DB`), and may be customized
to your environment with custom handling or logging functionality.

    // Create a connection pool with max 10 connections, min 2 idle connections...
    conn, err := hermes.Connect("postgres",
        "postgres://postgres@127.0.0.1/engaged?sslmode=disable&connect_timeout=10",
        10, 2)
    if err != nil {
        return err
    }

    // In a Kubernetes deployment, this will cause the app to shutdown and let
    // Kubernetes restart the pod...
    conn.OnFailure = hermes.ExitOnFailure

    // If the connection fails when conn.Exec is called, hermes.ExitOnFailure
    // is called, the application exits, and Kubernetes restarts the app,
    // allowing the app to try to reconnect to the database.
    if _, err := conn.Exec("...."); err != nil {
        return err
    }

### DidConnectionFail (1.1.x)

If `OnFailure` is not defined, Hermes simply returns the error as normal,
expecting the application to do something with it.  In these situations, there
is a function in Hermes that can check if the error returned by `lib/pq` is a
connection error: `hermes.DidConnectionFail`.  Pass the error to that, and if
it's a connection error, the function returns true.

## Transaction Timers (1.2.x)

Hermes supports configurable transaction timers to watch transactions and warn
the developer if the transaction was open longer than expected.  This can be
useful in testing for transactions that weren't properly cleaned up.

Simply call `hermes.EnableTimeouts(time.Duration, bool)` with the worst-case
expected transaction duration (presumably less than a second).

    func main() {
        // If a transaction takes longer than one second, you'll see an
        // error message in stderr
        hermes.EnableTimeouts(time.Second, false)

        // Create a connection pool with max 10 connections, min 2 idle
        // connections...
        conn, err := hermes.Connect("postgres",
            "postgres://postgres@127.0.0.1/engaged?sslmode=disable&connect_timeout=10",
            10, 2)
        if err != nil {
            return err
        }

        tx, err := conn.Begin()
        if err != nil {
            panic(err)
        }

        // Oops...we forgot tx.Close()!

        // This will cause an error message to print out to stderr
        time.Sleep(5 * time.Second)
    }

If you pass in `true` to the `hermes.EnableTimeouts` function, the application
will panic when a transaction times out.

You may disable transaction timers using the `hermes.DisableTimeouts()` call.

**Do not run transaction timers in production!** There is overhead with the
timers enabled; enabling them in production could cause performance and memory
issues under load (each transaction will get a time.Timer).

## Savepoints (1.2.4)

Hermes 1.2.4 adds support for transaction "savepoints."  A savepoint acts like a
bookmark in a transaction that stays around until the transaction ends.  It
allows a transaction to partially rollback to the savepoint.

At any point in a transaction, use `Conn.Savepoint` to create a savepoint in the
transaction.  The savepoint is assigned a random identifer, which is the
returned by the `Conn.Savepoint` function.  When you wish to rollback to this
savepoint, call `Conn.RollbackTo(savepointID)`.

For example:

    tx, err := db.Begin()
    if err != nil {
        return err
    }
    defer tx.Close()

    // ... do some work ...

    savepoint, err := tx.Savepoint
    if err != nil {
        // If the savepoint can't be created, rollback the entire transaction
        return err
    }

    // ... do additional work ...

    // Whoops!  Something went wrong in the additional work!
    //
    // Also note that RollbackTo does return an error, which you should probably
    // catch.
    tx.RollbackTo(savepoint)

    // Continue working; the transaction is still valid, but we just lost the
    // additional work.

Savepoints remain valid once created.  You can create a savepoint, rollback to
the savepoint, do more work, and rollback to the savepoint again.

Cursors created before a savepoint are unaffected by a rollback to the
savepoint, even if they have been manipulated after the savepoint was created.
Cursors created after a savepoint are closed when the savepoint is rolled back.
See the documentation below for more details.

While `Savepoint()` and `RollbackTo()` are part of the `hermes.Conn` interface,
when called on a `hermes.DB` object they do nothing.

Savepoints have only been tested with PostgreSQL, though they should also work
with MySQL.

### Usages

Savepoints can be very useful for database testing.  For example, you can create
a Hermes transaction (`hermes.Tx`) at the start of a test case containing
multiple scenarios, setup your initial data, then create a savepoint before each
scenario you're testing.

After each scenario, simply rollback to the savepoint and test the next
scenario.  At the end of the test case, allow the transaction to close
(`defer tx.Close()`) and rollback all the data, leaving the database in a
pristine state.

For example:

    // Test uniqueness when saving users.  Based off the example above.  Both
    // `User.Email` and `User.Name` must be unique.
    func TestUserUniquness(t *testing.T) {
        u := User{
            Email: "jdoe@nowhere.com",
            Name: "John Doe",
        }

        tx, err := Conn.Begin()
        if err != nil {
            t.Fatal(err)
        }
        defer tx.Close()

        // Create our valid user account
        if err := db.SaveUser(tx, u); err != nil {
            t.Fatalf("Unable to create a new user account: %s", err)
        }

        // Leave a savepoint to rollback to
        savepoint, err := tx.Savepoint()
        if err != nil {
            t.Fatalf("Couldn't create a savepoint: %s", err)
        }

        // Test email uniquness
        other := User{
            Email: "jdoe@nowhere.com,
            Name: "Another name",
        }

        if err = db.SaveUser(tx, other); err == nil {
            t.Error("Appears that user emails lack a uniqueness constraint in the database")
        }

        // Just to be safe, rollback to our valid user and try the name
        if err = tx.RollbackTo(savepoint); err != nil {
            t.Fatalf("Unable to rollback to savepoint: %s", err)
        }

        // Test name uniqueness
        other = User{
            Email: "another@nowhere.com",
            Name: "John Doe",
        }

        if err = db.SaveUser(tx, other); err == nil {
            t.Error("Appears that user names lack a uniqueness constraint in the database")
        }

        // Again, let the test case end and allow `defer tx.Close()` to wipe all
        // the data and savepoints created by the transaction; no need for any
        // delete calls.
    }

### Additional information

For more information on savepoints, see the PostgreSQL documentation:

* https://www.postgresql.org/docs/12/sql-savepoint.html
* https://www.postgresql.org/docs/12/sql-rollback-to.html

Or the MySQL documentation:

* https://dev.mysql.com/doc/refman/8.0/en/savepoint.html

## Testing

Testing requires the lib/pq library, a PostgreSQL database, and a test database
called "hermes_test".

(A future release may mock a database driver.)

### On a Mac...

    $ brew install postgresql
    $ createdb hermes_test
    $ cd $GOPATH/src/github.com/sbowman/hermes
    $ go get
    $ go test


text:
require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')

module ESP::Integration
  class RegionTest < ESP::Integration::TestCase
    context ESP::Region do
      context 'live calls' do
        setup do
          @region = ESP::Region.last
          fail "Live DB does not have any regions.  Add a region and run tests again." if @region.blank?
        end

        context '.where' do
          should 'return region objects' do
            regions = ESP::Region.where(id_eq: @region.id)

            assert_equal ESP::Region, regions.resource_class
          end
        end

        context '#CRUD' do
          should 'be able to read' do
            assert_not_nil @region

            region = ESP::Region.find(@region.id)

            assert_not_nil region
          end
        end
      end
    end
  end
end


text:
package network::versa::director::restapi::plugin;

use strict;
use warnings;
use base qw(centreon::plugins::script_custom);

sub new {
    my ($class, %options) = @_;
    my $self = $class->SUPER::new(package => __PACKAGE__, %options);
    bless $self, $class;

    $self->{version} = '1.0';
    $self->{modes} = {
        'cache'        => 'network::versa::director::restapi::mode::cache',
        'devices'      => 'network::versa::director::restapi::mode::devices',
        'discovery'    => 'network::versa::director::restapi::mode::discovery',
        'list-devices' => 'network::versa::director::restapi::mode::listdevices',
        'list-paths'   => 'network::versa::director::restapi::mode::listpaths',
        'paths'        => 'network::versa::director::restapi::mode::paths'
    };

    $self->{custom_modes}->{api} = 'network::versa::director::restapi::custom::api';
    return $self;
}

1;

__END__

=head1 PLUGIN DESCRIPTION

Check Versa SD-Wan through Director REST API.

=cut


text:
.decipher-tags {
  min-height: 24px;
  border: 1px solid #ccc;
  margin: 0px 10px;
  padding: 0px 5px 5px 5px;
}
.decipher-tags .decipher-tags-taglist {
  margin-bottom: 4px;
  display: inline;
}
.decipher-tags .decipher-tags-taglist .decipher-tags-tag {
  display: inline-block;
  border-radius: 5px;
  background-color: #EFEFEF;
  border: 1px solid #DDD;
  padding: 2px;
  margin-right: 5px;
  margin-top: 5px;
}
.decipher-tags .decipher-tags-taglist .decipher-tags-tag i {
  color: black;
  text-decoration: none;
}
.decipher-tags .decipher-tags-taglist .decipher-tags-tag.selected {
  border: 1px solid red;
}
.decipher-tags .decipher-tags-taglist .decipher-tags-tag:hover i {
  color: red;
  text-decoration: none;
}
.decipher-tags .decipher-tags-taglist .decipher-tags-tag i {
  cursor: pointer;
}
.decipher-tags .decipher-tags-input {
  border: none;
  outline: 0;
}
.decipher-tags .decipher-tags-invalid {
  color: #FFF;
}
.decipher-tags .decipher-tags-controls {
  margin-top: 8px;
  min-height: 28px;
}


text:
package org.apache.flex.forks.velocity.runtime.compiler;

import org.apache.bcel.generic.*;
import org.apache.bcel.Constants;

/**
 * The start of a velocity template compiler. Incomplete.
 *
 * @author Jason van Zyl
 * @version $Id: Compiler.java,v 1.6.8.1 2004/03/03 23:22:55 geirm Exp $
 */
public class Compiler implements InstructionConstants
{
    public static void main(String[] args)
    {
        String template = args[0].substring(0, args[0].indexOf("."));
        ClassGen cg =
                new ClassGen(template, "java.lang.Object", "<generated>",
                Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);

        ConstantPoolGen cp = cg.getConstantPool(); // cg creates constant pool
        InstructionList il = new InstructionList();
        MethodGen mg = new MethodGen(Constants.ACC_STATIC |
                Constants.ACC_PUBLIC, // access flags
                Type.VOID, // return type
                new Type[]{ // argument types
                new ArrayType(Type.STRING, 1)},
                new String[]{ "argv" }, // arg names
                "main", template, // method, class
                il, cp);

        //Add often needed constants to constant pool.

        int br_index = cp.addClass("java.io.BufferedReader");
        int ir_index = cp.addClass("java.io.InputStreamReader");
        int system_out = cp.addFieldref("java.lang.System", "out", // System.out
                "Ljava/io/PrintStream;");
        int system_in = cp.addFieldref("java.lang.System", "in", // System.in
                "Ljava/io/InputStream;");

        // Create BufferedReader object and store it in local variable `in'.

        il.append(new NEW(br_index));
        il.append(DUP);
        il.append(new NEW(ir_index));
        il.append(DUP);
        il.append(new GETSTATIC(system_in));

        // Call constructors, i.e. BufferedReader(InputStreamReader())

        il.append( new INVOKESPECIAL(
                cp.addMethodref("java.io.InputStreamReader", "<init>",
                "(Ljava/io/InputStream;)V")));
        il.append( new INVOKESPECIAL(
                cp.addMethodref("java.io.BufferedReader", "<init>", "(Ljava/io/Reader;)V")));

        // Create local variable `in'

        LocalVariableGen lg = mg.addLocalVariable("in",
                new ObjectType("java.io.BufferedReader"), null, null);
        int in = lg.getIndex();
        lg.setStart(il.append(new ASTORE(in))); // `i' valid from here

        // Create local variable `name'

        lg = mg.addLocalVariable("name", Type.STRING, null, null);
        int name = lg.getIndex();
        il.append(ACONST_NULL);
        lg.setStart(il.append(new ASTORE(name))); // `name' valid from here

        InstructionHandle try_start = il.append(new GETSTATIC(system_out));
        il.append(new PUSH(cp, "I will be a template compiler!"));
        il.append( new INVOKEVIRTUAL(
                cp.addMethodref("java.io.PrintStream", "println", "(Ljava/lang/String;)V")));

        // Upon normal execution we jump behind exception handler,
        // the target address is not known yet.

        GOTO g = new GOTO(null);
        InstructionHandle try_end = il.append(g);

        InstructionHandle handler = il.append(RETURN);
        mg.addExceptionHandler(try_start, try_end, handler,
                new ObjectType("java.io.IOException"));

        // Normal code continues, now we can set the branch target of the GOTO.

        InstructionHandle ih = il.append(new GETSTATIC(system_out));
        g.setTarget(ih);

        // String concatenation compiles to StringBuffer operations.

        il.append(new NEW(cp.addClass("java.lang.StringBuffer")));
        il.append(DUP);
        il.append(new PUSH(cp, " "));
        il.append( new INVOKESPECIAL(
                cp.addMethodref("java.lang.StringBuffer", "<init>", "(Ljava/lang/String;)V")));

        il.append(new ALOAD(name));

        // One can also abstract from using the ugly signature syntax by using
        // the getMethodSignature() method. For example:

        String sig = Type.getMethodSignature(Type.STRINGBUFFER,
                new Type[]{ Type.STRING });
        il.append( new INVOKEVIRTUAL(
                cp.addMethodref("java.lang.StringBuffer", "append", sig)));

        il.append( new INVOKEVIRTUAL(
                cp.addMethodref("java.lang.StringBuffer", "toString", "()Ljava/lang/String;")));

        il.append(RETURN);

        mg.setMaxStack(5); // Needed stack size
        cg.addMethod(mg.getMethod());

        // Add public <init> method, i.e. empty constructor
        cg.addEmptyConstructor(Constants.ACC_PUBLIC);

        // Get JavaClass object and dump it to file.
        try
        {
            cg.getJavaClass().dump(template + ".class");
        }
        catch (java.io.IOException e)
        {
            System.err.println(e);
        }
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="15dip" />


text:
<?php

interface Itest {
	function a();
}

interface Itest2 {
	function a();
}

interface Itest3 extends Itest, Itest2 {
}

echo "done!\n";

?>


text:
/**
 *
 */
package com.qiyi.usercloud.carve.client;

import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.CollectionUtils;

import com.qiyi.usercloud.carve.client.pool.BootStrapHolder;
import com.qiyi.usercloud.carve.config.CarveBuilder;
import com.qiyi.usercloud.carve.exception.CarveException;
import com.qiyi.usercloud.carve.protocol.ModelType;
import com.qiyi.usercloud.carve.util.CarveConstances;
import com.qiyi.usercloud.carve.zk.curator.CuratorZkBean;

/**
 * @author xinyun.du
 *
 */
public class CarveClient extends CarveBuilder implements InitializingBean, DisposableBean{

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.DisposableBean#destroy()
	 */
	@Override
	public void destroy() throws Exception {

		Set<String> modelset = getModelset();
		if(CollectionUtils.isEmpty(modelset)){
			return;
		}
		for(String model : modelset){
			try {
				ModelType type = ModelType.getType(model);
				CuratorZkBean zk = CuratorZkBean.getCuratorZk(type.address);
				if(zk != null){
					zk.close();
				}
			} catch (Exception e) {
			}
		}

		BootStrapHolder.close();
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		check();
		String mode = getMode();
		ModelType type = ModelType.getType(mode);
		if(type == null){
			throw new CarveException("必须指定布置环境. value = dev|product|test");
		}
		int compress = getCompressionThreshold();
		if(compress <= 0){
			setCompressionThreshold(CarveConstances.DEFAULT_COMPRESSION_THRESHOLD);
		}
		String zkserver = getZookeeperUrl();
		if(StringUtils.isEmpty(zkserver)){
			zkserver = type.address;
			setZookeeperUrl(zkserver);
		}
	}

	public void check(){
		if(StringUtils.isEmpty(getDomain())){
//			if(!getDomain().matches("^[a-zA-Z]*")){
//				throw new CarveException("[domain 字段必须全英文 ]");
//			}
			throw new CarveException("服务方的domain 字段必填.");
		}
		if(StringUtils.isEmpty(getLocalDomain())){
			throw new CarveException("[localDomain 字段必填]");
		}
//		if(!getLocalDomain().matches("^[a-zA-Z]*")){
//			throw new CarveException("[localDomain 字段必须全英文]");
//		}
		if(StringUtils.isEmpty(getMode()) || ModelType.getType(getMode()) == null){
			throw new CarveException("[mode 系统部署环境字段必填. dev|product|test] ");
		}
	}

}


text:
package com.ctrip.hermes.producer.sender;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.annotation.Named;
import org.unidal.tuple.Pair;

import com.alibaba.fastjson.JSON;
import com.codahale.metrics.Timer;
import com.codahale.metrics.Timer.Context;
import com.ctrip.hermes.core.exception.MessageSendException;
import com.ctrip.hermes.core.message.ProducerMessage;
import com.ctrip.hermes.core.result.SendResult;
import com.ctrip.hermes.core.schedule.ExponentialSchedulePolicy;
import com.ctrip.hermes.core.schedule.SchedulePolicy;
import com.ctrip.hermes.core.service.SystemClockService;
import com.ctrip.hermes.core.transport.command.SendMessageCommand;
import com.ctrip.hermes.core.utils.HermesThreadFactory;
import com.ctrip.hermes.meta.entity.Endpoint;
import com.ctrip.hermes.producer.config.ProducerConfig;
import com.ctrip.hermes.producer.status.ProducerStatusMonitor;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.SettableFuture;

/**
 * @author Leo Liang(jhliang@ctrip.com)
 *
 */
@Named(type = MessageSender.class, value = Endpoint.BROKER)
public class BrokerMessageSender extends AbstractMessageSender implements MessageSender, Initializable {

	private static Logger log = LoggerFactory.getLogger(BrokerMessageSender.class);

	@Inject
	private ProducerConfig m_config;

	@Inject
	private SystemClockService m_systemClockService;

	private ConcurrentMap<Pair<String, Integer>, TaskQueue> m_taskQueues = new ConcurrentHashMap<Pair<String, Integer>, TaskQueue>();

	private ExecutorService m_callbackExecutor;

	private AtomicBoolean m_started = new AtomicBoolean(false);

	@Override
	public Future<SendResult> doSend(ProducerMessage<?> msg) {
		if (m_started.compareAndSet(false, true)) {
			startEndpointSender();
		}

		Pair<String, Integer> tp = new Pair<String, Integer>(msg.getTopic(), msg.getPartition());
		m_taskQueues.putIfAbsent(tp,
		      new TaskQueue(msg.getTopic(), msg.getPartition(), m_config.getBrokerSenderTaskQueueSize()));

		return m_taskQueues.get(tp).submit(msg);
	}

	@Override
	public void resend(List<SendMessageCommand> timeoutCmds) {
		for (SendMessageCommand cmd : timeoutCmds) {
			ProducerStatusMonitor.INSTANCE.waitBrokerResultTimeout(cmd.getTopic(), cmd.getPartition(),
			      cmd.getMessageCount());
			List<Pair<ProducerMessage<?>, SettableFuture<SendResult>>> msgFuturePairs = cmd
			      .getProducerMessageFuturePairs();
			for (Pair<ProducerMessage<?>, SettableFuture<SendResult>> pair : msgFuturePairs) {
				resend(pair.getKey(), pair.getValue());
			}
		}
	}

	private void resend(ProducerMessage<?> msg, SettableFuture<SendResult> future) {
		Pair<String, Integer> tp = new Pair<String, Integer>(msg.getTopic(), msg.getPartition());
		m_taskQueues.get(tp).resubmit(msg, future);
	}

	private void startEndpointSender() {
		Executors.newSingleThreadExecutor(HermesThreadFactory.create("ProducerEndpointSender", false)).submit(
		      new EndpointSender());
	}

	private class EndpointSender implements Runnable {

		private ConcurrentMap<Pair<String, Integer>, AtomicBoolean> m_runnings = new ConcurrentHashMap<Pair<String, Integer>, AtomicBoolean>();

		private ExecutorService m_taskExecThreadPool;

		public EndpointSender() {
			int threadCount = m_config.getBrokerSenderNetworkIoThreadCount();

			m_taskExecThreadPool = Executors.newFixedThreadPool(threadCount,
			      HermesThreadFactory.create("BrokerMessageSender", false));
		}

		@Override
		public void run() {
			int checkIntervalBase = m_config.getBrokerSenderNetworkIoCheckIntervalBaseMillis();
			int checkIntervalMax = m_config.getBrokerSenderNetworkIoCheckIntervalMaxMillis();

			SchedulePolicy schedulePolicy = new ExponentialSchedulePolicy(checkIntervalBase, checkIntervalMax);

			while (!Thread.currentThread().isInterrupted()) {
				boolean hasTask = scanAndExecuteTasks();
				if (hasTask) {
					schedulePolicy.succeess();
				} else {
					schedulePolicy.fail(true);
				}
			}

		}

		private boolean scanAndExecuteTasks() {
			boolean hasTask = false;

			List<Map.Entry<Pair<String, Integer>, TaskQueue>> entries = new ArrayList<>(m_taskQueues.entrySet());

			Collections.shuffle(entries);
			for (Map.Entry<Pair<String, Integer>, TaskQueue> entry : entries) {
				try {
					TaskQueue queue = entry.getValue();

					if (queue.hasTask()) {
						hasTask = hasTask || scheduleTaskExecution(entry.getKey(), queue);
					}
				} catch (RuntimeException e) {
					// ignore
					log.warn("Exception occurred, ignore it", e);
				}
			}

			return hasTask;
		}

		private boolean scheduleTaskExecution(Pair<String, Integer> tp, TaskQueue queue) {
			m_runnings.putIfAbsent(tp, new AtomicBoolean(false));

			if (m_runnings.get(tp).compareAndSet(false, true)) {
				m_taskExecThreadPool.submit(new SendTask(tp.getKey(), tp.getValue(), queue, m_runnings.get(tp)));
				return true;
			}
			return false;
		}

	}

	private class SendTask implements Runnable {

		private String m_topic;

		private int m_partition;

		private TaskQueue m_taskQueue;

		private AtomicBoolean m_running;

		public SendTask(String topic, int partition, TaskQueue taskQueue, AtomicBoolean running) {
			m_topic = topic;
			m_partition = partition;
			m_taskQueue = taskQueue;
			m_running = running;
		}

		@Override
		public void run() {
			try {
				int batchSize = m_config.getBrokerSenderBatchSize();
				SendMessageCommand cmd = m_taskQueue.pop(batchSize);

				if (cmd != null) {
					if (sendMessagesToBroker(cmd)) {
						cmd.accepted(m_systemClockService.now());
					} else {
						m_taskQueue.push(cmd);
					}
				}
			} catch (Exception e) {
				// ignore
				if (log.isDebugEnabled()) {
					log.debug("Exception occurred, ignore it", e);
				}
			} finally {
				m_running.set(false);
			}

		}

		private boolean sendMessagesToBroker(SendMessageCommand cmd) {
			try {
				Endpoint endpoint = m_endpointManager.getEndpoint(m_topic, m_partition);
				if (endpoint != null) {
					long correlationId = cmd.getHeader().getCorrelationId();

					Future<Boolean> future = m_messageAcceptanceMonitor.monitor(correlationId);
					m_messageResultMonitor.monitor(cmd);

					long timeout = m_config.getBrokerSenderSendTimeoutMillis();

					Timer timer = ProducerStatusMonitor.INSTANCE.getTimer(cmd.getTopic(), cmd.getPartition(),
					      "broker-accept-duration");

					Context context = timer.time();

					m_endpointClient.writeCommand(endpoint, cmd, timeout, TimeUnit.MILLISECONDS);
					ProducerStatusMonitor.INSTANCE.wroteToBroker(m_topic, m_partition, cmd.getMessageCount());

					Boolean brokerAccepted = null;
					try {
						brokerAccepted = future.get(timeout, TimeUnit.MILLISECONDS);
					} catch (TimeoutException e) {
						ProducerStatusMonitor.INSTANCE.waitBrokerAcceptanceTimeout(m_topic, m_partition,
						      cmd.getMessageCount());
						m_messageAcceptanceMonitor.cancel(correlationId);
					}

					context.stop();

					if (brokerAccepted != null && brokerAccepted) {
						ProducerStatusMonitor.INSTANCE.brokerAccepted(m_topic, m_partition, cmd.getMessageCount());
						return true;
					} else {
						ProducerStatusMonitor.INSTANCE.brokerRejected(m_topic, m_partition, cmd.getMessageCount());
						return false;
					}
				} else {
					// ignore
					if (log.isDebugEnabled()) {
						log.debug("No endpoint found, ignore it");
					}
					return false;
				}
			} catch (Exception e) {
				ProducerStatusMonitor.INSTANCE.sendFailed(m_topic, m_partition, cmd.getMessageCount());
				log.warn("Exception occurred while sending message to broker, will retry it", e);
				return false;
			}
		}
	}

	private class TaskQueue {
		private String m_topic;

		private int m_partition;

		private AtomicReference<SendMessageCommand> m_cmd = new AtomicReference<SendMessageCommand>(null);

		private BlockingQueue<ProducerWorkerContext> m_queue;

		public TaskQueue(String topic, int partition, int queueSize) {
			m_topic = topic;
			m_partition = partition;
			m_queue = new LinkedBlockingQueue<ProducerWorkerContext>(queueSize);

			ProducerStatusMonitor.INSTANCE.addTaskQueueGauge(m_topic, m_partition, m_queue);
		}

		public void push(SendMessageCommand cmd) {
			m_cmd.set(cmd);
		}

		public SendMessageCommand pop(int size) {
			if (m_cmd.get() == null) {
				return createSendMessageCommand(size);
			}
			return m_cmd.getAndSet(null);
		}

		private SendMessageCommand createSendMessageCommand(int size) {
			SendMessageCommand cmd = null;
			List<ProducerWorkerContext> contexts = new ArrayList<ProducerWorkerContext>(size);
			m_queue.drainTo(contexts, size);
			if (!contexts.isEmpty()) {
				cmd = new SendMessageCommand(m_topic, m_partition);
				for (ProducerWorkerContext context : contexts) {
					cmd.addMessage(context.m_msg, context.m_future);
				}
			}
			return cmd;
		}

		public boolean hasTask() {
			return m_cmd.get() != null || !m_queue.isEmpty();
		}

		public Future<SendResult> submit(final ProducerMessage<?> msg) {
			ProducerStatusMonitor.INSTANCE.messageSubmitted(m_topic, m_partition);
			SettableFuture<SendResult> future = SettableFuture.create();

			if (msg.getCallback() != null) {
				Futures.addCallback(future, new FutureCallback<SendResult>() {

					@Override
					public void onSuccess(SendResult result) {
						msg.getCallback().onSuccess(result);
					}

					@Override
					public void onFailure(Throwable t) {
						msg.getCallback().onFailure(t);
					}
				}, m_callbackExecutor);
			}

			offer(msg, future);

			return future;
		}

		private void offer(final ProducerMessage<?> msg, SettableFuture<SendResult> future) {
			if (!m_queue.offer(new ProducerWorkerContext(msg, future))) {
				ProducerStatusMonitor.INSTANCE.offerFailed(m_topic, m_partition);
				String warning = String.format(
				      "Producer task queue is full(queueSize=%s), will drop this message(refKey=%s, body=%s).",
				      m_queue.size(), msg.getKey(), JSON.toJSONString(msg.getBody()));
				log.warn(warning);
				MessageSendException throwable = new MessageSendException(warning);
				future.setException(throwable);
			}
		}

		public void resubmit(final ProducerMessage<?> msg, SettableFuture<SendResult> future) {
			ProducerStatusMonitor.INSTANCE.messageResubmitted(m_topic, m_partition);
			offer(msg, future);
		}

	}

	@Override
	public void initialize() throws InitializationException {
		int callbackThreadCount = m_config.getProducerCallbackThreadCount();

		m_callbackExecutor = Executors.newFixedThreadPool(callbackThreadCount,
		      HermesThreadFactory.create("ProducerCallback", false));

	}

	private static class ProducerWorkerContext {
		private ProducerMessage<?> m_msg;

		private SettableFuture<SendResult> m_future;

		public ProducerWorkerContext(ProducerMessage<?> msg, SettableFuture<SendResult> future) {
			m_msg = msg;
			m_future = future;
		}

	}

}


text:
import { graphqlExpress } from 'graphql-server-express';
import 'isomorphic-fetch';

import log from '../../common/log';
import schema from '../api/schema';
import modules from '../modules';

export default graphqlExpress(() => {
  try {
    return {
      schema,
      context: modules.createContext()
    };
  } catch (e) {
    log(e.stack);
  }
});


text:
CLANG_LEVEL := ..
DIRS := clang clang-c

include $(CLANG_LEVEL)/Makefile


text:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>

#include "stack.h"
#include "ctype.h"

#include "configuration.h"
#include "interpreter.h"
#include "memorymanager.h"
#include "byteassembler.h"
#include "python_interoperability.h"
#include "misc.h"
#ifndef HOST_STANDALONE
#include "shared.h"
#include "device-support.h"
#endif

// Wrapper for the context which is passed into a thread
struct hostRunningThreadWrapper {
	char * assembledCode;
	unsigned int memoryFilledSize;
	unsigned short entriesInSymbolTable;
	int hostThreadId, numberProcesses, hostStartPoint;
};

struct epiphanyMonitorThreadWrapper {
	struct interpreterconfiguration* configuration;
	struct shared_basic * deviceState;
};

struct fullPythonInteractivityThreadWrapper {
    struct interpreterconfiguration* configuration;
    struct shared_basic * deviceState;
    pthread_t* emanagementThread;
};

struct included_source_files {
    char * fileName;
    struct included_source_files * next;
};

#define TEXTUAL_BASIC_SIZE_STRIDE 5000

extern int yyparse();
extern int yy_scan_string(const char*);
extern void initThreadedAspectsForInterpreter(int, int, struct shared_basic*);

struct stack_t indent_stack, filenameStack, lineNumberStack;
struct included_source_files * included_src_root=NULL;

static void doParse(char*);
static char * getSourceFileContents(char*);
static void displayParsedBasicInfo(void);
void writeOutByteCode(char*);
void loadByteCode(char*);
static char* getIncludeFileWithPath(char*);
static void runCodeOnHost(struct interpreterconfiguration*, struct shared_basic*);
static void * runSpecificHostProcess(void*);
static void appendIncludedSourceFileToStore(char*);
static int hasSourceFileAlreadyBeenIncluded(char*);
static void* runCodeForFullPythonInteractivity(void*);
#ifndef HOST_STANDALONE
static void* runCodeOnEpiphany(void*);
#endif

/*
 * Host program entry point, bootstraps reading configuration, lexing & parsing (if applicable) and running the code
 */
int main (int argc, char *argv[]) {
	srand((unsigned) time(NULL) * getpid());
	struct interpreterconfiguration* configuration=readConfiguration(argc, argv);
	if (configuration->filename != NULL) {
		char * contents = getSourceFileContents(configuration->filename);
		if (configuration->displayPPCode) printf("%s\n", contents);
		doParse(contents);
	} else if (configuration->loadByteFilename != NULL) {
		loadByteCode(configuration->loadByteFilename);
	} else if (configuration->pipedInContents != NULL) {
		if (configuration->displayPPCode) printf("%s\n", configuration->pipedInContents);
		doParse(configuration->pipedInContents);
	}
	if (configuration->displayStats) displayParsedBasicInfo();
	if (configuration->compiledByteFilename != NULL) {
		writeOutByteCode(configuration->compiledByteFilename);
	} else {
#ifndef HOST_STANDALONE
		pthread_t epiphany_management_thread, fullPythonInteractivityThread;
		struct shared_basic * deviceState=loadCodeOntoEpiphany(configuration);
		struct epiphanyMonitorThreadWrapper * w = (struct epiphanyMonitorThreadWrapper*) malloc(sizeof(struct epiphanyMonitorThreadWrapper));
		w->configuration=configuration;
		w->deviceState=deviceState;
		pthread_create(&epiphany_management_thread, NULL, runCodeOnEpiphany, (void*)w);
		if (configuration->fullPythonHost) {
			struct fullPythonInteractivityThreadWrapper * wrapperForInteract=(struct fullPythonInteractivityThreadWrapper *) malloc(sizeof(struct fullPythonInteractivityThreadWrapper));
			wrapperForInteract->configuration=configuration;
			wrapperForInteract->deviceState=deviceState;
			wrapperForInteract->emanagementThread=&epiphany_management_thread;
			pthread_create(&fullPythonInteractivityThread, NULL, runCodeForFullPythonInteractivity, (void*) wrapperForInteract);
		}
		runCodeOnHost(configuration, deviceState);
#else
		pthread_t fullPythonInteractivityThread;
		struct shared_basic * standAloneState=(struct shared_basic*) malloc(sizeof(struct shared_basic));
		standAloneState->symbol_size=getNumberEntriesInSymbolTable();
		standAloneState->num_procs=configuration->coreProcs+configuration->hostProcs;
		standAloneState->baseHostPid=configuration->coreProcs;
		if (configuration->fullPythonHost) {
			struct fullPythonInteractivityThreadWrapper * wrapperForInteract=(struct fullPythonInteractivityThreadWrapper *) malloc(sizeof(struct fullPythonInteractivityThreadWrapper));
			wrapperForInteract->configuration=configuration;
			wrapperForInteract->deviceState=standAloneState;
			wrapperForInteract->emanagementThread=NULL;
			pthread_create(&fullPythonInteractivityThread, NULL, runCodeForFullPythonInteractivity, (void*) wrapperForInteract);
		}
		runCodeOnHost(configuration, standAloneState);
#endif
		pthread_exit(NULL);
#ifndef HOST_STANDALONE
		finaliseCores();
#endif
	}
	free(configuration->intentActive);
	free(configuration);
	return 0;
}

/**
 * Calls out to do the lexing and parsing of the source code
 */
static void doParse(char * contents) {
	enterScope();
	initStack(&indent_stack);
	initStack(&filenameStack);
	initStack(&lineNumberStack);
	yy_scan_string(contents);
	yyparse();
}

#ifndef HOST_STANDALONE
/*
 * Runs the code on the Epiphany cores, acts as a monitor whilst it is running and then finalises the cores afterwards
 */
static void* runCodeOnEpiphany(void * raw_wrapper) {
	struct epiphanyMonitorThreadWrapper * wrapper=(struct epiphanyMonitorThreadWrapper*) raw_wrapper;
	monitorCores(wrapper->deviceState, wrapper->configuration);
	return NULL;
}
#endif

/**
* Runs the full python interactivity monitor (host virtual core id = 0) in a thread
*/
static void* runCodeForFullPythonInteractivity(void * raw_wrapper) {
    struct fullPythonInteractivityThreadWrapper * wrapperForInteract=(struct fullPythonInteractivityThreadWrapper*) raw_wrapper;
    runFullPythonInteractivityOnHost(wrapperForInteract->configuration, wrapperForInteract->deviceState, wrapperForInteract->emanagementThread,
                                     wrapperForInteract->configuration->hostProcs > 1);
    return NULL;
}

/**
 * Runs the code on the host if compiled in standalone mode (helpful for development)
 */
static void runCodeOnHost(struct interpreterconfiguration* configuration, struct shared_basic * basicState) {
	struct hostRunningThreadWrapper * threadWrappers=(struct hostRunningThreadWrapper *)
			malloc(sizeof(struct hostRunningThreadWrapper) * configuration->hostProcs);
	pthread_t threads[configuration->hostProcs];
	int i;
	char * assembledCode=getAssembledCode();
	unsigned int memoryFilledSize=getMemoryFilledSize();
	unsigned short entriesInSymbolTable=getNumberEntriesInSymbolTable();
	if (configuration->hostProcs > 0) initThreadedAspectsForInterpreter(configuration->hostProcs, configuration->coreProcs, basicState);
	for (i=(configuration->fullPythonHost ? 1 : 0);i<configuration->hostProcs;i++) {
		threadWrappers[i].assembledCode=assembledCode;
		threadWrappers[i].memoryFilledSize=memoryFilledSize;
		threadWrappers[i].entriesInSymbolTable=entriesInSymbolTable;
		threadWrappers[i].hostThreadId=i;
		threadWrappers[i].hostStartPoint=configuration->coreProcs;
		threadWrappers[i].numberProcesses=configuration->hostProcs + configuration->coreProcs;
		pthread_create(&threads[i], NULL, runSpecificHostProcess, (void*)&threadWrappers[i]);
	}
}

/**
 * Host thread entry point, which calls on to the interpreter
 */
static void * runSpecificHostProcess(void * rawThreadContext) {
	struct hostRunningThreadWrapper * threadContext= (struct hostRunningThreadWrapper*) rawThreadContext;
	runIntepreter(threadContext->assembledCode, threadContext->memoryFilledSize, threadContext->entriesInSymbolTable,
			threadContext->hostThreadId + threadContext->hostStartPoint, threadContext->numberProcesses, threadContext->hostThreadId);
	return NULL;
}

/**
 * Given the name of a file will read it and return the char array containing the contents, an error
 * is reported along with program exit if the file cannot be read for whatever reason
 */
static char * getSourceFileContents(char * filename) {
	unsigned int contentsSize=TEXTUAL_BASIC_SIZE_STRIDE;
	char * contents=(char*) malloc(contentsSize);
	char buffer[1024];
	FILE * sourceCode=fopen(filename, "r");
	if (sourceCode != NULL) {
		sprintf(contents, "<<<%s\n", filename);
		while (fgets(buffer, 1024, sourceCode) != NULL) {
			if (strstr(buffer, "import") != NULL && buffer[0] != '#') {
				char * importPoint=strstr(buffer, "import");
				char * importPoint_from=strstr(buffer, "from");
				if (importPoint_from != NULL) {
					if (importPoint_from < importPoint) importPoint=importPoint_from;
				}
				int startIdx=0, idx=0, foundSpace=0;
				while (importPoint[idx] != '\0' && importPoint[idx] != '\n') {
					if (isspace(importPoint[idx]) && foundSpace==0) foundSpace=1;
					if (!isspace(importPoint[idx]) && foundSpace==1) {
						startIdx=idx;
						foundSpace=2;
					}
					if (isspace(importPoint[idx]) && foundSpace==2) break;
					idx++;
				}
				if (importPoint[idx-1]=='\n') importPoint[idx-1]='\0';
				importPoint[idx]='\0';
				char * newFilename=(char*) malloc(strlen(&importPoint[startIdx])+5);
				sprintf(newFilename, "%s.py", &importPoint[startIdx]);
				char* entirePathForFile=getIncludeFileWithPath(newFilename);
				if (entirePathForFile == NULL) {
					fprintf(stderr, "Opening of Python file '%s' failed, are you sure this file exists?\n", newFilename);
					exit(0);
				}
				if (!hasSourceFileAlreadyBeenIncluded(entirePathForFile)) {
                    appendIncludedSourceFileToStore(entirePathForFile);
                    char * importedContents=getSourceFileContents(entirePathForFile);
                    if (strlen(importedContents)+strlen(contents)+1 >= contentsSize) {
                        contentsSize=strlen(importedContents)+strlen(contents)+TEXTUAL_BASIC_SIZE_STRIDE;
                        contents=realloc(contents, contentsSize);
                    }
                    sprintf(contents, "%s%s", contents, importedContents);
				}
				free(newFilename);
				free(entirePathForFile);
			} else {
				int i=0;
				while(isspace(buffer[i]) && buffer[i] != '\0' && i < 1024) i++;
				if (buffer[i] != '#') {
					if (strlen(buffer)+strlen(contents)+1 >= contentsSize) {
						contentsSize+=TEXTUAL_BASIC_SIZE_STRIDE;
						contents=realloc(contents, contentsSize);
					}
					sprintf(contents, "%s%s", contents, buffer);
				} else {
					// Empty line to preserve line numberings
					if (strlen(buffer)+2 >= contentsSize) {
						contentsSize+=TEXTUAL_BASIC_SIZE_STRIDE;
						contents=realloc(contents, contentsSize);
					}
					sprintf(contents, "%s\n", contents);
				}
			}
		}
		if (strlen(contents)+7 >=contentsSize) {
			contents=realloc(contents, contentsSize+7);
		}
		sprintf(contents, "%s\n>>>\n", contents);
		fclose(sourceCode);
		return contents;
	} else {
		fprintf(stderr, "Opening of Python file '%s' failed, are you sure this file exists?\n", filename);
		exit(0);
	}
}

static void appendIncludedSourceFileToStore(char * filename) {
    struct included_source_files * newNode=(struct included_source_files *) malloc(sizeof(struct included_source_files));
    newNode->fileName=(char*) malloc(strlen(filename) + 1);
    strcpy(newNode->fileName, filename);
    newNode->next=included_src_root;
    included_src_root=newNode;
}

static int hasSourceFileAlreadyBeenIncluded(char * filename) {
    struct included_source_files * head=included_src_root;
    while (head != NULL) {
        if (strcmp(head->fileName, filename) == 0) return 1;
        head=head->next;
    }
    return 0;
}

static char* getIncludeFileWithPath(char * filename) {
	if(access(filename, F_OK) != -1 ) {
		char * tr=(char*) malloc(strlen(filename) + 1);
		strcpy(tr, filename);
		return tr;
	} else {
	    char * testFilename=(char*) malloc(strlen(filename) + 10);
	    sprintf(testFilename, "modules/%s", filename);
	    if(access(testFilename, F_OK) != -1 ) {
            return testFilename;
	    } else {
            free(testFilename);
	    }

		char * prev = getenv("EPYTHONPATH");
		if (prev == NULL) return NULL;
		char * pch=strchr(prev, ':');
		while (pch != NULL) {
			char * newPath=(char*) malloc((pch-prev)+1);
			strncpy(newPath, prev, pch-prev);
			if (prev[(pch-prev)-1] == '/') {
				newPath[(pch-prev)-1]='\0';
			} else {
				newPath[pch-prev]='\0';
			}
			testFilename=(char*) malloc(strlen(newPath) + strlen(filename) + 2);
			sprintf(testFilename, "%s/%s", newPath, filename);
			free(newPath);
			if(access(testFilename, F_OK) != -1 ) return testFilename;
			free(testFilename);
			prev=pch+1;
			pch=strchr(prev,':');
		}
		if (prev[strlen(prev)-1] == '/') {
			prev[strlen(prev)-1]='\0';
		}
		testFilename=(char*) malloc(strlen(prev) + strlen(filename) + 2);
		sprintf(testFilename, "%s/%s", prev, filename);
		if(access(testFilename, F_OK) != -1 ) return testFilename;
		free(testFilename);
	}
	return NULL;
}

/**
 * Displays the parsed basic information, giving an idea of the size of the processed byte code, symbol table and memory free on each core
 */
static void displayParsedBasicInfo() {
	int memSize=getMemoryFilledSize();
	int symbolEntries=getNumberEntriesInSymbolTable();
#ifndef HOST_STANDALONE
	printf("%d bytes for code, %lu bytes for symbol table (%d entries), %d bytes free\n",
			memSize, symbolEntries*sizeof(struct symbol_node), symbolEntries, (0x8000-CORE_DATA_START)-(memSize+(symbolEntries*5)));
#else
	printf("%d bytes for code, %lu bytes for symbol table (%d entries)\n", memSize, symbolEntries*sizeof(struct symbol_node), symbolEntries);
#endif
}

/**
 * Starts the interpreter from a byte code file
 */
void loadByteCode(char * loadByteFilename) {
	FILE * byteFile=fopen(loadByteFilename, "rb");
	if (byteFile != NULL) {
		unsigned int len;
		errorCheck(fread(&len, sizeof(unsigned int), 1, byteFile), "Memory filled size from byte code file");
		setMemoryFilledSize(len);
		unsigned short symbols;
		errorCheck(fread(&symbols, sizeof(unsigned short), 1, byteFile), "Number of symbols from byte code file");
		setNumberEntriesInSymbolTable(symbols);
		char * code=(char*) malloc(len);
		errorCheck(fread(code, sizeof(char), len, byteFile), "Byte code from file");
		setAssembledCode(code);
		fclose(byteFile);
	} else {
		fprintf(stderr, "Opening of byte code file '%s' failed, are you sure this file exists?\n", loadByteFilename);
		exit(0);
	}
}

/**
 * Writes out the byte code to a file
 */
void writeOutByteCode(char * compiledByteFilename) {
	FILE * byteFile=fopen(compiledByteFilename, "wb");
	if (byteFile != NULL) {
		unsigned int len=getMemoryFilledSize();
		fwrite(&len, sizeof(unsigned int), 1, byteFile);
		unsigned short symbols=getNumberEntriesInSymbolTable();
		fwrite(&symbols, sizeof(unsigned short), 1, byteFile);
		char * code=getAssembledCode();
		fwrite(code, sizeof(char), len, byteFile);
		fclose(byteFile);
	} else {
		fprintf(stderr, "Writing byte code to file '%s' failed\n", compiledByteFilename);
		exit(0);
	}
}


text:
(function() {
  var Promise, TAG_ASSIGNED, TAG_AUDITED, TAG_FINISHED, TAG_HANDLED, TAG_RECEIVED, config, isStopped, mailer, promiseWhile, work;

  Promise = require('sequelize').Promise;

  mailer = require('nodemailer');

  config = require('../config');

  isStopped = false;

  TAG_RECEIVED = 1;

  TAG_ASSIGNED = 2;

  TAG_HANDLED = 3;

  TAG_AUDITED = 4;

  TAG_FINISHED = 5;

  promiseWhile = function(action) {
    var my_loop, resolver;
    resolver = Promise.defer();
    my_loop = function() {
      if (isStopped) {
        return resolver.resolve();
      }
      return Promise.cast(action()).then(my_loop)["catch"](resolver.reject);
    };
    process.nextTick(my_loop);
    return resolver.promise;
  };

  work = function() {
    var currentMail;
    currentMail = void 0;
    return global.db.models.outbox.find({
      where: {
        status: 'audited'
      }
    }).then(function(mail) {
      if (!mail) {
        throw new global.myError.NoTask();
      }
      currentMail = mail;
      return global.transporter.sendMailPromised({
        to: mail.to,
        from: config.mail.auth.username + "<" + config.mail.auth.mailaddr + ">",
        subject: mail.title,
        html: require('./sender').replace(mail.html)
      });
    }).then(function() {
      currentMail.status = 'finished';
      return currentMail.save();
    }).then(function(mail) {
      return mail.getReplyTo();
    }).then(function(mail) {
      var message;
      if (!mail) {
        return;
      }
      mail.status = "finished";
      message = {
        title: "任务完成",
        html: "
任务" + mail.title + "已经完成了
",
        text: "任务" + mail.tile + "已经完成了",
        receivers: [mail.dispatcherId, mail.consumerId, mail.auditorId]
      };
      return Promise.all([mail.save(), mail.addTags([TAG_FINISHED]), mail.removeTags([TAG_HANDLED]), global.myUtil.message.send(message)]);
    })["catch"](global.myError.NoTask, function() {
      return Promise.delay(2000);
    })["catch"](function(err) {
      var message;
      console.log(err);
      if (currentMail) {
        currentMail.status = "failed";
        currentMail.reason += (new Date()) + "\n" + err.message + "\n";
        currentMail.save();
        message = {
          title: "任务失败",
          html: "
你发送的标题为" + mail.tile + "的邮件失败了
",
          text: "你发送的标题为" + mail.tile + "的邮件失败了",
          receivers: [currentMail.consumerId]
        };
        return global.myUtil.message.send(message);
      }
    });
  };

  module.exports = function(config) {
    var transporter;
    transporter = mailer.createTransport({
      host: config.smtp.host,
      port: config.smtp.port,
      secure: true,
      auth: {
        user: config.auth.mailaddr,
        pass: config.auth.password
      }
    });
    global.transporter = Promise.promisifyAll(transporter, {
      suffix: 'Promised'
    });
    return promiseWhile(work);
  };

}).call(this);

//# sourceMappingURL=sendmaild.js.map


text:
{% extends "layout.html" %}

{% block page_title %}
  Registered Traveller
{% endblock %}

{% block content %}

Back

Declaration

I confirm that:

the information supplied is complete, true and correct to the best of my knowledge

I have read, understood and accept the terms and conditions and the  privacy policy of the Registered Traveller service

I will tell Border Force if my circumstances change, or if any new information relevant to my application becomes available

I have not been convicted of a criminal offence, including spent convictions, or cautioned by the police

I understand that:

my details may in certain circumstances be passed to fraud prevention agencies to prevent and detect fraud and money laundering

other agencies may provide Border Force with information about me

there is a £50 charge for a renewal application and a £20 charge to update a passport

          <form name="saveDetails">

                <input type="checkbox" name="nationality_other" value="Wales" id="nationality_other">
                I understand and agree with the above declarations

          </form>

  		Confirm

{% endblock %}


text:
'use strict';

const path = require('path');
const {assert} = require('chai');
const {describe, it} = require('mocha');

const cp = require('child_process');
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const cwd = path.join(__dirname, '..');

const modelId = '3512561418744365056';
const project = process.env.CAIP_PROJECT_ID;
const location = process.env.LOCATION;

describe('AI platform get model', () => {
  it('should get the specified model', async () => {
    const stdout = execSync(
      `node ./get-model.js ${modelId} ${project} ${location}`,
      {
        cwd,
      }
    );
    assert.match(stdout, /Get model response/);
  });
});


text:
'use strict';

const chai = require('chai'),
  expect = chai.expect,
  Support = require(__dirname + '/../support'),
  current = Support.sequelize,
  sinon = require('sinon'),
  DataTypes = require(__dirname + '/../../../lib/data-types'),
  Promise = require('bluebird').getNewLibraryCopy();

describe(Support.getTestDialectTeaser('Model'), () => {
  describe('findAndCount', () => {
    describe('should handle promise rejection', () => {
      before(function() {
        this.stub = sinon.stub();

        Promise.onPossiblyUnhandledRejection(() => {
          this.stub();
        });

        this.User = current.define('User', {
          username: DataTypes.STRING,
          age: DataTypes.INTEGER
        });

        this.findAll = sinon.stub(this.User, 'findAll').callsFake(() => {
          return Promise.reject(new Error());
        });

        this.count = sinon.stub(this.User, 'count').callsFake(() => {
          return Promise.reject(new Error());
        });
      });

      after(function() {
        this.findAll.resetBehavior();
        this.count.resetBehavior();
      });

      it('with errors in count and findAll both', function() {
        return this.User.findAndCount({})
          .then(() => {
            throw new Error();
          })
          .catch(() => {
            expect(this.stub.callCount).to.eql(0);
          });
      });
    });
  });
});


text:
package com.navercorp.pinpoint.common.server.config;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.util.Properties;

/**
 * @author Woonduk Kang(emeroad)
 */
public final class ConfigurationUtils {
    private static final Logger LOGGER = LogManager.getLogger(ConfigurationUtils.class);

    private ConfigurationUtils() {
    }

    public static String readString(Properties properties, String propertyName, String defaultValue) {
        final String result = properties.getProperty(propertyName, defaultValue);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("{}={}", propertyName, result);
        }
        return result ;
    }

    public static int readInt(Properties properties, String propertyName, int defaultValue) {
        final String value = properties.getProperty(propertyName);
        final int result = NumberUtils.toInt(value, defaultValue);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("{}={}", propertyName, result);
        }
        return result;
    }

    public static long readLong(Properties properties, String propertyName, long defaultValue) {
        final String value = properties.getProperty(propertyName);
        final long result = NumberUtils.toLong(value, defaultValue);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("{}={}", propertyName, result);
        }
        return result;
    }

    public static boolean readBoolean(Properties properties, String propertyName) {
        final String value = properties.getProperty(propertyName);

        // if a default value will be needed afterwards, may match string value instead of Utils.
        // for now stay unmodified because of no need.

        final boolean result = Boolean.valueOf(value);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("{}={}", propertyName, result);
        }
        return result;
    }
}