text:
.. _basics.subclassing:

*******************
Subclassing ndarray
*******************

Introduction
------------

Subclassing ndarray is relatively simple, but it has some complications
compared to other Python objects.  On this page we explain the machinery
that allows you to subclass ndarray, and the implications for
implementing a subclass.

ndarrays and object creation
============================

Subclassing ndarray is complicated by the fact that new instances of
ndarray classes can come about in three different ways.  These are:

#. Explicit constructor call - as in ``MySubClass(params)``.  This is
   the usual route to Python instance creation.
#. View casting - casting an existing ndarray as a given subclass
#. New from template - creating a new instance from a template
   instance. Examples include returning slices from a subclassed array,
   creating return types from ufuncs, and copying arrays.  See
   :ref:`new-from-template` for more details

The last two are characteristics of ndarrays - in order to support
things like array slicing.  The complications of subclassing ndarray are
due to the mechanisms numpy has to support these latter two routes of
instance creation.

When to use subclassing
=======================

Besides the additional complexities of subclassing a NumPy array, subclasses
can run into unexpected behaviour because some functions may convert the
subclass to a baseclass and "forget" any additional information
associated with the subclass.
This can result in surprising behavior if you use NumPy methods or
functions you have not explicitly tested.

On the other hand, compared to other interoperability approaches,
subclassing can be a useful because many thing will "just work".

This means that subclassing can be a convenient approach and for a long time
it was also often the only available approach.
However, NumPy now provides additional interoperability protocols described
in ":ref:`Interoperability with NumPy <basics.interoperability>`".
For many use-cases these interoperability protocols may now be a better fit
or supplement the use of subclassing.

Subclassing can be a good fit if:

* you are less worried about maintainability or users other than yourself:
  Subclass will be faster to implement and additional interoperability
  can be added "as-needed".  And with few users, possible surprises are not
  an issue.
* you do not think it is problematic if the subclass information is
  ignored or lost silently.  An example is ``np.memmap`` where "forgetting"
  about data being memory mapped cannot lead to a wrong result.
  An example of a subclass that sometimes confuses users are NumPy's masked
  arrays.  When they were introduced, subclassing was the only approach for
  implementation.  However, today we would possibly try to avoid subclassing
  and rely only on interoperability protocols.

Note that also subclass authors may wish to study
:ref:`Interoperability with NumPy <basics.interoperability>`
to support more complex use-cases or work around the surprising behavior.

``astropy.units.Quantity`` and ``xarray`` are examples for array-like objects
that interoperate well with NumPy.  Astropy's ``Quantity`` is an example
which uses a dual approach of both subclassing and interoperability protocols.

.. _view-casting:

View casting
------------

*View casting* is the standard ndarray mechanism by which you take an
ndarray of any subclass, and return a view of the array as another
(specified) subclass:

>>> import numpy as np
>>> # create a completely useless ndarray subclass
>>> class C(np.ndarray): pass
>>> # create a standard ndarray
>>> arr = np.zeros((3,))
>>> # take a view of it, as our useless subclass
>>> c_arr = arr.view(C)
>>> type(c_arr)
<class '__main__.C'>

.. _new-from-template:

Creating new from template
--------------------------

New instances of an ndarray subclass can also come about by a very
similar mechanism to :ref:`view-casting`, when numpy finds it needs to
create a new instance from a template instance.  The most obvious place
this has to happen is when you are taking slices of subclassed arrays.
For example:

>>> v = c_arr[1:]
>>> type(v) # the view is of type 'C'
<class '__main__.C'>
>>> v is c_arr # but it's a new instance
False

The slice is a *view* onto the original ``c_arr`` data.  So, when we
take a view from the ndarray, we return a new ndarray, of the same
class, that points to the data in the original.

There are other points in the use of ndarrays where we need such views,
such as copying arrays (``c_arr.copy()``), creating ufunc output arrays
(see also :ref:`array-wrap`), and reducing methods (like
``c_arr.mean()``).

Relationship of view casting and new-from-template
--------------------------------------------------

These paths both use the same machinery.  We make the distinction here,
because they result in different input to your methods.  Specifically,
:ref:`view-casting` means you have created a new instance of your array
type from any potential subclass of ndarray.  :ref:`new-from-template`
means you have created a new instance of your class from a pre-existing
instance, allowing you - for example - to copy across attributes that
are particular to your subclass.

Implications for subclassing
----------------------------

If we subclass ndarray, we need to deal not only with explicit
construction of our array type, but also :ref:`view-casting` or
:ref:`new-from-template`.  NumPy has the machinery to do this, and it is
this machinery that makes subclassing slightly non-standard.

There are two aspects to the machinery that ndarray uses to support
views and new-from-template in subclasses.

The first is the use of the ``ndarray.__new__`` method for the main work
of object initialization, rather then the more usual ``__init__``
method.  The second is the use of the ``__array_finalize__`` method to
allow subclasses to clean up after the creation of views and new
instances from templates.

A brief Python primer on ``__new__`` and ``__init__``
=====================================================

``__new__`` is a standard Python method, and, if present, is called
before ``__init__`` when we create a class instance. See the `python
__new__ documentation
<https://docs.python.org/reference/datamodel.html#object.__new__>`_ for more detail.

For example, consider the following Python code:

>>> class C:
>>>     def __new__(cls, *args):
>>>         print('Cls in __new__:', cls)
>>>         print('Args in __new__:', args)
>>>         # The `object` type __new__ method takes a single argument.
>>>         return object.__new__(cls)
>>>     def __init__(self, *args):
>>>         print('type(self) in __init__:', type(self))
>>>         print('Args in __init__:', args)

meaning that we get:

>>> c = C('hello')
Cls in __new__: <class 'C'>
Args in __new__: ('hello',)
type(self) in __init__: <class 'C'>
Args in __init__: ('hello',)

When we call ``C('hello')``, the ``__new__`` method gets its own class
as first argument, and the passed argument, which is the string
``'hello'``.  After python calls ``__new__``, it usually (see below)
calls our ``__init__`` method, with the output of ``__new__`` as the
first argument (now a class instance), and the passed arguments
following.

As you can see, the object can be initialized in the ``__new__``
method or the ``__init__`` method, or both, and in fact ndarray does
not have an ``__init__`` method, because all the initialization is
done in the ``__new__`` method.

Why use ``__new__`` rather than just the usual ``__init__``?  Because
in some cases, as for ndarray, we want to be able to return an object
of some other class.  Consider the following:

.. testcode::

  class D(C):
      def __new__(cls, *args):
          print('D cls is:', cls)
          print('D args in __new__:', args)
          return C.__new__(C, *args)

      def __init__(self, *args):
          # we never get here
          print('In D __init__')

meaning that:

>>> obj = D('hello')
D cls is: <class 'D'>
D args in __new__: ('hello',)
Cls in __new__: <class 'C'>
Args in __new__: ('hello',)
>>> type(obj)
<class 'C'>

The definition of ``C`` is the same as before, but for ``D``, the
``__new__`` method returns an instance of class ``C`` rather than
``D``.  Note that the ``__init__`` method of ``D`` does not get
called.  In general, when the ``__new__`` method returns an object of
class other than the class in which it is defined, the ``__init__``
method of that class is not called.

This is how subclasses of the ndarray class are able to return views
that preserve the class type.  When taking a view, the standard
ndarray machinery creates the new ndarray object with something
like::

  obj = ndarray.__new__(subtype, shape, ...

where ``subdtype`` is the subclass.  Thus the returned view is of the
same class as the subclass, rather than being of class ``ndarray``.

That solves the problem of returning views of the same type, but now
we have a new problem.  The machinery of ndarray can set the class
this way, in its standard methods for taking views, but the ndarray
``__new__`` method knows nothing of what we have done in our own
``__new__`` method in order to set attributes, and so on.  (Aside -
why not call ``obj = subdtype.__new__(...`` then?  Because we may not
have a ``__new__`` method with the same call signature).

The role of ``__array_finalize__``
==================================

``__array_finalize__`` is the mechanism that numpy provides to allow
subclasses to handle the various ways that new instances get created.

Remember that subclass instances can come about in these three ways:

#. explicit constructor call (``obj = MySubClass(params)``).  This will
   call the usual sequence of ``MySubClass.__new__`` then (if it exists)
   ``MySubClass.__init__``.
#. :ref:`view-casting`
#. :ref:`new-from-template`

Our ``MySubClass.__new__`` method only gets called in the case of the
explicit constructor call, so we can't rely on ``MySubClass.__new__`` or
``MySubClass.__init__`` to deal with the view casting and
new-from-template.  It turns out that ``MySubClass.__array_finalize__``
*does* get called for all three methods of object creation, so this is
where our object creation housekeeping usually goes.

* For the explicit constructor call, our subclass will need to create a
  new ndarray instance of its own class.  In practice this means that
  we, the authors of the code, will need to make a call to
  ``ndarray.__new__(MySubClass,...)``, a class-hierarchy prepared call to
  ``super().__new__(cls, ...)``, or do view casting of an existing array
  (see below)
* For view casting and new-from-template, the equivalent of
  ``ndarray.__new__(MySubClass,...`` is called, at the C level.

The arguments that ``__array_finalize__`` receives differ for the three
methods of instance creation above.

The following code allows us to look at the call sequences and arguments:

.. testcode::

   import numpy as np

   class C(np.ndarray):
       def __new__(cls, *args, **kwargs):
           print('In __new__ with class %s' % cls)
           return super().__new__(cls, *args, **kwargs)

       def __init__(self, *args, **kwargs):
           # in practice you probably will not need or want an __init__
           # method for your subclass
           print('In __init__ with class %s' % self.__class__)

       def __array_finalize__(self, obj):
           print('In array_finalize:')
           print('   self type is %s' % type(self))
           print('   obj type is %s' % type(obj))

Now:

>>> # Explicit constructor
>>> c = C((10,))
In __new__ with class <class 'C'>
In array_finalize:
   self type is <class 'C'>
   obj type is <type 'NoneType'>
In __init__ with class <class 'C'>
>>> # View casting
>>> a = np.arange(10)
>>> cast_a = a.view(C)
In array_finalize:
   self type is <class 'C'>
   obj type is <type 'numpy.ndarray'>
>>> # Slicing (example of new-from-template)
>>> cv = c[:1]
In array_finalize:
   self type is <class 'C'>
   obj type is <class 'C'>

The signature of ``__array_finalize__`` is::

    def __array_finalize__(self, obj):

One sees that the ``super`` call, which goes to
``ndarray.__new__``, passes ``__array_finalize__`` the new object, of our
own class (``self``) as well as the object from which the view has been
taken (``obj``).  As you can see from the output above, the ``self`` is
always a newly created instance of our subclass, and the type of ``obj``
differs for the three instance creation methods:

* When called from the explicit constructor, ``obj`` is ``None``
* When called from view casting, ``obj`` can be an instance of any
  subclass of ndarray, including our own.
* When called in new-from-template, ``obj`` is another instance of our
  own subclass, that we might use to update the new ``self`` instance.

Because ``__array_finalize__`` is the only method that always sees new
instances being created, it is the sensible place to fill in instance
defaults for new object attributes, among other tasks.

This may be clearer with an example.

Simple example - adding an extra attribute to ndarray
-----------------------------------------------------

.. testcode::

  import numpy as np

  class InfoArray(np.ndarray):

      def __new__(subtype, shape, dtype=float, buffer=None, offset=0,
                  strides=None, order=None, info=None):
          # Create the ndarray instance of our type, given the usual
          # ndarray input arguments.  This will call the standard
          # ndarray constructor, but return an object of our type.
          # It also triggers a call to InfoArray.__array_finalize__
          obj = super().__new__(subtype, shape, dtype,
                                buffer, offset, strides, order)
          # set the new 'info' attribute to the value passed
          obj.info = info
          # Finally, we must return the newly created object:
          return obj

      def __array_finalize__(self, obj):
          # ``self`` is a new object resulting from
          # ndarray.__new__(InfoArray, ...), therefore it only has
          # attributes that the ndarray.__new__ constructor gave it -
          # i.e. those of a standard ndarray.
          #
          # We could have got to the ndarray.__new__ call in 3 ways:
          # From an explicit constructor - e.g. InfoArray():
          #    obj is None
          #    (we're in the middle of the InfoArray.__new__
          #    constructor, and self.info will be set when we return to
          #    InfoArray.__new__)
          if obj is None: return
          # From view casting - e.g arr.view(InfoArray):
          #    obj is arr
          #    (type(obj) can be InfoArray)
          # From new-from-template - e.g infoarr[:3]
          #    type(obj) is InfoArray
          #
          # Note that it is here, rather than in the __new__ method,
          # that we set the default value for 'info', because this
          # method sees all creation of default objects - with the
          # InfoArray.__new__ constructor, but also with
          # arr.view(InfoArray).
          self.info = getattr(obj, 'info', None)
          # We do not need to return anything

Using the object looks like this:

  >>> obj = InfoArray(shape=(3,)) # explicit constructor
  >>> type(obj)
  <class 'InfoArray'>
  >>> obj.info is None
  True
  >>> obj = InfoArray(shape=(3,), info='information')
  >>> obj.info
  'information'
  >>> v = obj[1:] # new-from-template - here - slicing
  >>> type(v)
  <class 'InfoArray'>
  >>> v.info
  'information'
  >>> arr = np.arange(10)
  >>> cast_arr = arr.view(InfoArray) # view casting
  >>> type(cast_arr)
  <class 'InfoArray'>
  >>> cast_arr.info is None
  True

This class isn't very useful, because it has the same constructor as the
bare ndarray object, including passing in buffers and shapes and so on.
We would probably prefer the constructor to be able to take an already
formed ndarray from the usual numpy calls to ``np.array`` and return an
object.

Slightly more realistic example - attribute added to existing array
-------------------------------------------------------------------

Here is a class that takes a standard ndarray that already exists, casts
as our type, and adds an extra attribute.

.. testcode::

  import numpy as np

  class RealisticInfoArray(np.ndarray):

      def __new__(cls, input_array, info=None):
          # Input array is an already formed ndarray instance
          # We first cast to be our class type
          obj = np.asarray(input_array).view(cls)
          # add the new attribute to the created instance
          obj.info = info
          # Finally, we must return the newly created object:
          return obj

      def __array_finalize__(self, obj):
          # see InfoArray.__array_finalize__ for comments
          if obj is None: return
          self.info = getattr(obj, 'info', None)

So:

  >>> arr = np.arange(5)
  >>> obj = RealisticInfoArray(arr, info='information')
  >>> type(obj)
  <class 'RealisticInfoArray'>
  >>> obj.info
  'information'
  >>> v = obj[1:]
  >>> type(v)
  <class 'RealisticInfoArray'>
  >>> v.info
  'information'

.. _array-ufunc:

``__array_ufunc__`` for ufuncs
------------------------------

  .. versionadded:: 1.13

A subclass can override what happens when executing numpy ufuncs on it by
overriding the default ``ndarray.__array_ufunc__`` method. This method is
executed *instead* of the ufunc and should return either the result of the
operation, or :obj:`NotImplemented` if the operation requested is not
implemented.

The signature of ``__array_ufunc__`` is::

    def __array_ufunc__(ufunc, method, *inputs, **kwargs):

    - *ufunc* is the ufunc object that was called.
    - *method* is a string indicating how the Ufunc was called, either
      ``"__call__"`` to indicate it was called directly, or one of its
      :ref:`methods<ufuncs.methods>`: ``"reduce"``, ``"accumulate"``,
      ``"reduceat"``, ``"outer"``, or ``"at"``.
    - *inputs* is a tuple of the input arguments to the ``ufunc``
    - *kwargs* contains any optional or keyword arguments passed to the
      function. This includes any ``out`` arguments, which are always
      contained in a tuple.

A typical implementation would convert any inputs or outputs that are
instances of one's own class, pass everything on to a superclass using
``super()``, and finally return the results after possible
back-conversion. An example, taken from the test case
``test_ufunc_override_with_super`` in ``core/tests/test_umath.py``, is the
following.

.. testcode::

    input numpy as np

    class A(np.ndarray):
        def __array_ufunc__(self, ufunc, method, *inputs, out=None, **kwargs):
            args = []
            in_no = []
            for i, input_ in enumerate(inputs):
                if isinstance(input_, A):
                    in_no.append(i)
                    args.append(input_.view(np.ndarray))
                else:
                    args.append(input_)

            outputs = out
            out_no = []
            if outputs:
                out_args = []
                for j, output in enumerate(outputs):
                    if isinstance(output, A):
                        out_no.append(j)
                        out_args.append(output.view(np.ndarray))
                    else:
                        out_args.append(output)
                kwargs['out'] = tuple(out_args)
            else:
                outputs = (None,) * ufunc.nout

            info = {}
            if in_no:
                info['inputs'] = in_no
            if out_no:
                info['outputs'] = out_no

            results = super().__array_ufunc__(ufunc, method, *args, **kwargs)
            if results is NotImplemented:
                return NotImplemented

            if method == 'at':
                if isinstance(inputs[0], A):
                    inputs[0].info = info
                return

            if ufunc.nout == 1:
                results = (results,)

            results = tuple((np.asarray(result).view(A)
                             if output is None else output)
                            for result, output in zip(results, outputs))
            if results and isinstance(results[0], A):
                results[0].info = info

            return results[0] if len(results) == 1 else results

So, this class does not actually do anything interesting: it just
converts any instances of its own to regular ndarray (otherwise, we'd
get infinite recursion!), and adds an ``info`` dictionary that tells
which inputs and outputs it converted. Hence, e.g.,

>>> a = np.arange(5.).view(A)
>>> b = np.sin(a)
>>> b.info
{'inputs': [0]}
>>> b = np.sin(np.arange(5.), out=(a,))
>>> b.info
{'outputs': [0]}
>>> a = np.arange(5.).view(A)
>>> b = np.ones(1).view(A)
>>> c = a + b
>>> c.info
{'inputs': [0, 1]}
>>> a += b
>>> a.info
{'inputs': [0, 1], 'outputs': [0]}

Note that another approach would be to use ``getattr(ufunc,
methods)(*inputs, **kwargs)`` instead of the ``super`` call. For this example,
the result would be identical, but there is a difference if another operand
also defines ``__array_ufunc__``. E.g., lets assume that we evalulate
``np.add(a, b)``, where ``b`` is an instance of another class ``B`` that has
an override.  If you use ``super`` as in the example,
``ndarray.__array_ufunc__`` will notice that ``b`` has an override, which
means it cannot evaluate the result itself. Thus, it will return
`NotImplemented` and so will our class ``A``. Then, control will be passed
over to ``b``, which either knows how to deal with us and produces a result,
or does not and returns `NotImplemented`, raising a ``TypeError``.

If instead, we replace our ``super`` call with ``getattr(ufunc, method)``, we
effectively do ``np.add(a.view(np.ndarray), b)``. Again, ``B.__array_ufunc__``
will be called, but now it sees an ``ndarray`` as the other argument. Likely,
it will know how to handle this, and return a new instance of the ``B`` class
to us. Our example class is not set up to handle this, but it might well be
the best approach if, e.g., one were to re-implement ``MaskedArray`` using
``__array_ufunc__``.

As a final note: if the ``super`` route is suited to a given class, an
advantage of using it is that it helps in constructing class hierarchies.
E.g., suppose that our other class ``B`` also used the ``super`` in its
``__array_ufunc__`` implementation, and we created a class ``C`` that depended
on both, i.e., ``class C(A, B)`` (with, for simplicity, not another
``__array_ufunc__`` override). Then any ufunc on an instance of ``C`` would
pass on to ``A.__array_ufunc__``, the ``super`` call in ``A`` would go to
``B.__array_ufunc__``, and the ``super`` call in ``B`` would go to
``ndarray.__array_ufunc__``, thus allowing ``A`` and ``B`` to collaborate.

.. _array-wrap:

``__array_wrap__`` for ufuncs and other functions
-------------------------------------------------

Prior to numpy 1.13, the behaviour of ufuncs could only be tuned using
``__array_wrap__`` and ``__array_prepare__``. These two allowed one to
change the output type of a ufunc, but, in contrast to
``__array_ufunc__``, did not allow one to make any changes to the inputs.
It is hoped to eventually deprecate these, but ``__array_wrap__`` is also
used by other numpy functions and methods, such as ``squeeze``, so at the
present time is still needed for full functionality.

Conceptually, ``__array_wrap__`` "wraps up the action" in the sense of
allowing a subclass to set the type of the return value and update
attributes and metadata.  Let's show how this works with an example.  First
we return to the simpler example subclass, but with a different name and
some print statements:

.. testcode::

  import numpy as np

  class MySubClass(np.ndarray):

      def __new__(cls, input_array, info=None):
          obj = np.asarray(input_array).view(cls)
          obj.info = info
          return obj

      def __array_finalize__(self, obj):
          print('In __array_finalize__:')
          print('   self is %s' % repr(self))
          print('   obj is %s' % repr(obj))
          if obj is None: return
          self.info = getattr(obj, 'info', None)

      def __array_wrap__(self, out_arr, context=None):
          print('In __array_wrap__:')
          print('   self is %s' % repr(self))
          print('   arr is %s' % repr(out_arr))
          # then just call the parent
          return super().__array_wrap__(self, out_arr, context)

We run a ufunc on an instance of our new array:

>>> obj = MySubClass(np.arange(5), info='spam')
In __array_finalize__:
   self is MySubClass([0, 1, 2, 3, 4])
   obj is array([0, 1, 2, 3, 4])
>>> arr2 = np.arange(5)+1
>>> ret = np.add(arr2, obj)
In __array_wrap__:
   self is MySubClass([0, 1, 2, 3, 4])
   arr is array([1, 3, 5, 7, 9])
In __array_finalize__:
   self is MySubClass([1, 3, 5, 7, 9])
   obj is MySubClass([0, 1, 2, 3, 4])
>>> ret
MySubClass([1, 3, 5, 7, 9])
>>> ret.info
'spam'

Note that the ufunc (``np.add``) has called the ``__array_wrap__`` method
with arguments ``self`` as ``obj``, and ``out_arr`` as the (ndarray) result
of the addition.  In turn, the default ``__array_wrap__``
(``ndarray.__array_wrap__``) has cast the result to class ``MySubClass``,
and called ``__array_finalize__`` - hence the copying of the ``info``
attribute.  This has all happened at the C level.

But, we could do anything we wanted:

.. testcode::

  class SillySubClass(np.ndarray):

      def __array_wrap__(self, arr, context=None):
          return 'I lost your data'

>>> arr1 = np.arange(5)
>>> obj = arr1.view(SillySubClass)
>>> arr2 = np.arange(5)
>>> ret = np.multiply(obj, arr2)
>>> ret
'I lost your data'

So, by defining a specific ``__array_wrap__`` method for our subclass,
we can tweak the output from ufuncs. The ``__array_wrap__`` method
requires ``self``, then an argument - which is the result of the ufunc -
and an optional parameter *context*. This parameter is returned by
ufuncs as a 3-element tuple: (name of the ufunc, arguments of the ufunc,
domain of the ufunc), but is not set by other numpy functions. Though,
as seen above, it is possible to do otherwise, ``__array_wrap__`` should
return an instance of its containing class.  See the masked array
subclass for an implementation.

In addition to ``__array_wrap__``, which is called on the way out of the
ufunc, there is also an ``__array_prepare__`` method which is called on
the way into the ufunc, after the output arrays are created but before any
computation has been performed. The default implementation does nothing
but pass through the array. ``__array_prepare__`` should not attempt to
access the array data or resize the array, it is intended for setting the
output array type, updating attributes and metadata, and performing any
checks based on the input that may be desired before computation begins.
Like ``__array_wrap__``, ``__array_prepare__`` must return an ndarray or
subclass thereof or raise an error.

Extra gotchas - custom ``__del__`` methods and ndarray.base
-----------------------------------------------------------

One of the problems that ndarray solves is keeping track of memory
ownership of ndarrays and their views.  Consider the case where we have
created an ndarray, ``arr`` and have taken a slice with ``v = arr[1:]``.
The two objects are looking at the same memory.  NumPy keeps track of
where the data came from for a particular array or view, with the
``base`` attribute:

>>> # A normal ndarray, that owns its own data
>>> arr = np.zeros((4,))
>>> # In this case, base is None
>>> arr.base is None
True
>>> # We take a view
>>> v1 = arr[1:]
>>> # base now points to the array that it derived from
>>> v1.base is arr
True
>>> # Take a view of a view
>>> v2 = v1[1:]
>>> # base points to the original array that it was derived from
>>> v2.base is arr
True

In general, if the array owns its own memory, as for ``arr`` in this
case, then ``arr.base`` will be None - there are some exceptions to this
- see the numpy book for more details.

The ``base`` attribute is useful in being able to tell whether we have
a view or the original array.  This in turn can be useful if we need
to know whether or not to do some specific cleanup when the subclassed
array is deleted.  For example, we may only want to do the cleanup if
the original array is deleted, but not the views.  For an example of
how this can work, have a look at the ``memmap`` class in
``numpy.core``.

Subclassing and Downstream Compatibility
----------------------------------------

When sub-classing ``ndarray`` or creating duck-types that mimic the ``ndarray``
interface, it is your responsibility to decide how aligned your APIs will be
with those of numpy. For convenience, many numpy functions that have a corresponding
``ndarray`` method (e.g., ``sum``, ``mean``, ``take``, ``reshape``) work by checking
if the first argument to a function has a method of the same name. If it exists, the
method is called instead of coercing the arguments to a numpy array.

For example, if you want your sub-class or duck-type to be compatible with
numpy's ``sum`` function, the method signature for this object's ``sum`` method
should be the following:

.. testcode::

    def sum(self, axis=None, dtype=None, out=None, keepdims=False):
    ...

This is the exact same method signature for ``np.sum``, so now if a user calls
``np.sum`` on this object, numpy will call the object's own ``sum`` method and
pass in these arguments enumerated above in the signature, and no errors will
be raised because the signatures are completely compatible with each other.

If, however, you decide to deviate from this signature and do something like this:

.. testcode::

   def sum(self, axis=None, dtype=None):
   ...

This object is no longer compatible with ``np.sum`` because if you call ``np.sum``,
it will pass in unexpected arguments ``out`` and ``keepdims``, causing a TypeError
to be raised.

If you wish to maintain compatibility with numpy and its subsequent versions (which
might add new keyword arguments) but do not want to surface all of numpy's arguments,
your function's signature should accept ``**kwargs``. For example:

.. testcode::

   def sum(self, axis=None, dtype=None, **unused_kwargs):
   ...

This object is now compatible with ``np.sum`` again because any extraneous arguments
(i.e. keywords that are not ``axis`` or ``dtype``) will be hidden away in the
``**unused_kwargs`` parameter.


text:
package net.mabako.steamgifts.tasks;

import android.widget.Toast;

import net.mabako.steamgifts.fragments.profile.MessageListFragment;

import org.jsoup.Connection;

/**
 * Mark all messages read.
 */
public class MarkMessagesReadTask extends AjaxTask<MessageListFragment> {
    public MarkMessagesReadTask(MessageListFragment fragment, String xsrfToken) {
        super(fragment, fragment.getContext(), xsrfToken, "read_messages");
        setUrl("https://www.steamgifts.com/messages");
    }

    @Override
    protected void addExtraParameters(Connection connection) {

    }

    @Override
    protected void onPostExecute(Connection.Response response) {
        super.onPostExecute(response);
        if (response != null && response.statusCode() == 301) {
            getFragment().onMarkedMessagesRead();
            Toast.makeText(getFragment().getContext(), "Marked all messages as read", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getFragment().getContext(), "Error marking messages as read", Toast.LENGTH_SHORT).show();
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
import types
import sys

from modjy_exceptions import *

class modjy_impl:

    def deal_with_app_return(self, environ, start_response_callable, app_return):
        self.log.debug("Processing app return type: %s" % str(type(app_return)))
        if isinstance(app_return, types.StringTypes):
            raise ReturnNotIterable("Application returned object that was not an iterable: %s" % str(type(app_return)))
        if type(app_return) is types.FileType:
            pass # TBD: What to do here? can't call fileno()
        if hasattr(app_return, '__len__') and callable(app_return.__len__):
            expected_pieces = app_return.__len__()
        else:
            expected_pieces = -1
        try:
            try:
                ix = 0
                for next_piece in app_return:
                    if not isinstance(next_piece, types.StringTypes):
                        raise NonStringOutput("Application returned iterable containing non-strings: %s" % str(type(next_piece)))
                    if ix == 0:
                        # The application may have called start_response in the first iteration
                        if not start_response_callable.called:
                            raise StartResponseNotCalled("Start_response callable was never called.")
                        if not start_response_callable.content_length \
                            and expected_pieces == 1 \
                            and start_response_callable.write_callable.num_writes == 0:
                                # Take the length of the first piece
                                start_response_callable.set_content_length(len(next_piece))
                    start_response_callable.write_callable(next_piece)
                    ix += 1
                    if ix == expected_pieces:
                        break
                if expected_pieces != -1 and ix != expected_pieces:
                    raise WrongLength("Iterator len() was wrong. Expected %d pieces: got %d" % (expected_pieces, ix) )
            except AttributeError, ax:
                if str(ax) == "__getitem__":
                    raise ReturnNotIterable("Application returned object that was not an iterable: %s" % str(type(app_return)))
                else:
                    raise ax
            except TypeError, tx:
                raise ReturnNotIterable("Application returned object that was not an iterable: %s" % str(type(app_return)))
            except ModjyException, mx:
                raise mx
            except Exception, x:
                raise ApplicationException(x)
        finally:
            if hasattr(app_return, 'close') and callable(app_return.close):
                app_return.close()

    def init_impl(self):
        self.do_j_env_params()

    def add_packages(self, package_list):
        packages = [p.strip() for p in package_list.split(';')]
        for p in packages:
            self.log.info("Adding java package %s to jython" % p)
            sys.add_package(p)

    def add_classdirs(self, classdir_list):
        classdirs = [cd.strip() for cd in classdir_list.split(';')]
        for cd in classdirs:
            self.log.info("Adding directory %s to jython class file search path" % cd)
            sys.add_classdir(cd)

    def add_extdirs(self, extdir_list):
        extdirs = [ed.strip() for ed in extdir_list.split(';')]
        for ed in extdirs:
            self.log.info("Adding directory %s for .jars and .zips search path" % ed)
            sys.add_extdir(self.expand_relative_path(ed))

    def do_j_env_params(self):
        if self.params['packages']:
            self.add_packages(self.params['packages'])
        if self.params['classdirs']:
            self.add_classdirs(self.params['classdirs'])
        if self.params['extdirs']:
            self.add_extdirs(self.params['extdirs'])


text:
<?php

/**
 * Legacy tests to find obsolete menu declaration
 */
namespace Magento\Test\Legacy;

class ObsoleteMenuTest extends \PHPUnit_Framework_TestCase
{
    public function testMenuDeclaration()
    {
        $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
        $invoker(
            /**
             * @param string $menuFile
             */
            function ($menuFile) {
                $menuXml = simplexml_load_file($menuFile);
                $xpath = '/config/menu/*[boolean(./children) or boolean(./title) or boolean(./action)]';
                $this->assertEmpty(
                    $menuXml->xpath($xpath),
                    'Obsolete menu structure detected in file ' . $menuFile . '.'
                );
            },
            \Magento\Framework\App\Utility\Files::init()->getMainConfigFiles()
        );
    }
}


text:
const Effect = require('./Effect.js');
const { Locations, Players, CardTypes } = require('../Constants');

class CardEffect extends Effect {
    constructor(game, source, properties, effect) {
        if(!properties.match) {
            properties.match = (card, context) => card === context.source;
            if(properties.location === Locations.Any) {
                properties.targetLocation = Locations.Any;
            } else if([CardTypes.Province, CardTypes.Stronghold, CardTypes.Holding].includes(source.type)) {
                properties.targetLocation = Locations.Provinces;
            }
        }
        super(game, source, properties, effect);
        this.targetController = properties.targetController || Players.Self;
        this.targetLocation = properties.targetLocation || Locations.PlayArea;
    }

    isValidTarget(target) {
        if(target === this.match) {
            // This is a hack to check whether this is a lasting effect
            return true;
        }
        return (
            target.allowGameAction('applyEffect', this.context) &&
            (this.targetController !== Players.Self || target.controller === this.source.controller) &&
            (this.targetController !== Players.Opponent || target.controller !== this.source.controller)
        );
    }

    getTargets() {
        if(this.targetLocation === Locations.Any) {
            return this.game.allCards.filter(card => this.match(card, this.context));
        } else if(this.targetLocation === Locations.Provinces) {
            let cards = this.game.allCards.filter(card => card.isInProvince());
            return cards.filter(card => this.match(card, this.context));
        } else if(this.targetLocation === Locations.PlayArea) {
            return this.game.findAnyCardsInPlay(card => this.match(card, this.context));
        }
        return this.game.allCards.filter(card => this.match(card, this.context) && card.location === this.targetLocation);
    }
}

module.exports = CardEffect;


text:
{-# LANGUAGE GADTs #-}

{-
An *abelian monoid* is a set M together with a constant (nullary
operation) zero : M and a binary operation oplus : M -> M -> M such that:

* zero is a unit of oplus
* oplus is associative
* oplus is commutative
-}

-- i. Define a type class AbMonoid that corresponds to the abelian monoid structure

class AbMonoid m where
  zero   :: m
  oplus  :: m -> m -> m

-- ii. Define a datastructure AbMonoidExp ... and define an AbMonoid instance ...

data AbMonoidExp v where
  Zero   :: AbMonoidExp v
  Oplus  :: AbMonoidExp v -> AbMonoidExp v -> AbMonoidExp v
  Var    :: v -> AbMonoidExp v

instance AbMonoid (AbMonoidExp v) where
  zero   = Zero
  oplus  = Oplus

-- iii. Find one other instance ... and a non-instance ...

instance AbMonoid Int where
  zero   = 0
  oplus  = (+)

-- The empty type Void has no instance (because it has no zero)

-- iv. Define a general evaluator for AbMonoidExp expressions on the
-- basis of an assignment function.

eval :: AbMonoid m => (v -> m) -> AbMonoidExp v -> m
eval f Zero         =  zero
eval f (Oplus l r)  =  oplus (eval f l) (eval f r)
eval f (Var v)      =  f v

-- v. Specialise the evaluator ... three AbMonoidExp expressions ...

evalAME :: (v -> Int) -> AbMonoidExp v -> Int
evalAME = eval

e1' :: v -> AbMonoidExp v
e1' x      = Oplus (Var x) Zero
e2' x y z  = Oplus (Var x) (Oplus (Var y) (Var z))
e3' x y    = Oplus (Var x) (Var y)

e1, e2, e3 :: AbMonoidExp String
e1 = e1' "x"
e2 = e2' "x" "y" "z"
e3 = e3' "x" "y"

assInt :: String -> Int
assInt "x" = 1
assInt "y" = 2
assInt "z" = 4

test =    evalAME assInt e1 == 1
       && evalAME assInt e2 == 7
       && evalAME assInt e3 == 3


text:
<?php

namespace OCA\Files_Encryption\Tests;

/**
 * Class Stream
 * this class provide basic stream tests
 */
class Stream extends TestCase {

	const TEST_ENCRYPTION_STREAM_USER1 = "test-stream-user1";

	public $userId;
	public $pass;
	/**
	 * @var \OC\Files\View
	 */
	public $view;
	public $dataShort;
	public $stateFilesTrashbin;

	public static function setUpBeforeClass() {
		parent::setUpBeforeClass();

		// create test user
		self::loginHelper(self::TEST_ENCRYPTION_STREAM_USER1, true);
	}

	protected function setUp() {
		parent::setUp();

		// set user id
		\OC_User::setUserId(self::TEST_ENCRYPTION_STREAM_USER1);
		$this->userId = self::TEST_ENCRYPTION_STREAM_USER1;
		$this->pass = self::TEST_ENCRYPTION_STREAM_USER1;

		// init filesystem view
		$this->view = new \OC\Files\View('/');

		// init short data
		$this->dataShort = 'hats';

		// remember files_trashbin state
		$this->stateFilesTrashbin = \OC_App::isEnabled('files_trashbin');

		// we don't want to tests with app files_trashbin enabled
		\OC_App::disable('files_trashbin');
	}

	protected function tearDown() {
		// reset app files_trashbin
		if ($this->stateFilesTrashbin) {
			\OC_App::enable('files_trashbin');
		}
		else {
			\OC_App::disable('files_trashbin');
		}

		parent::tearDown();
	}

	public static function tearDownAfterClass() {
		// cleanup test user
		\OC_User::deleteUser(self::TEST_ENCRYPTION_STREAM_USER1);

		parent::tearDownAfterClass();
	}

	function testStreamOptions() {
		$filename = '/tmp-' . $this->getUniqueID();
		$view = new \OC\Files\View('/' . $this->userId . '/files');

		// Save short data as encrypted file using stream wrapper
		$cryptedFile = $view->file_put_contents($filename, $this->dataShort);

		// Test that data was successfully written
		$this->assertTrue(is_int($cryptedFile));

		$handle = $view->fopen($filename, 'r');

		// check if stream is at position zero
		$this->assertEquals(0, ftell($handle));

		// set stream options
		$this->assertTrue(flock($handle, LOCK_SH));
		$this->assertTrue(flock($handle, LOCK_UN));

		fclose($handle);

		// tear down
		$view->unlink($filename);
	}

	function testStreamSetBlocking() {
		$filename = '/tmp-' . $this->getUniqueID();
		$view = new \OC\Files\View('/' . $this->userId . '/files');

		// Save short data as encrypted file using stream wrapper
		$cryptedFile = $view->file_put_contents($filename, $this->dataShort);

		// Test that data was successfully written
		$this->assertTrue(is_int($cryptedFile));

		$handle = $view->fopen($filename, 'r');

		if (\OC_Util::runningOnWindows()) {
			fclose($handle);
			$view->unlink($filename);
			$this->markTestSkipped('[Windows] stream_set_blocking() does not work as expected on Windows.');
		}

		// set stream options
		$this->assertTrue(stream_set_blocking($handle, 1));

		fclose($handle);

		// tear down
		$view->unlink($filename);
	}

	/**
	 * @medium
	 */
	function testStreamSetTimeout() {
		$filename = '/tmp-' . $this->getUniqueID();
		$view = new \OC\Files\View('/' . $this->userId . '/files');

		// Save short data as encrypted file using stream wrapper
		$cryptedFile = $view->file_put_contents($filename, $this->dataShort);

		// Test that data was successfully written
		$this->assertTrue(is_int($cryptedFile));

		$handle = $view->fopen($filename, 'r');

		// set stream options
		$this->assertFalse(stream_set_timeout($handle, 1));

		fclose($handle);

		// tear down
		$view->unlink($filename);
	}

	function testStreamSetWriteBuffer() {
		$filename = '/tmp-' . $this->getUniqueID();
		$view = new \OC\Files\View('/' . $this->userId . '/files');

		// Save short data as encrypted file using stream wrapper
		$cryptedFile = $view->file_put_contents($filename, $this->dataShort);

		// Test that data was successfully written
		$this->assertTrue(is_int($cryptedFile));

		$handle = $view->fopen($filename, 'r');

		// set stream options
		$this->assertEquals(0, stream_set_write_buffer($handle, 1024));

		fclose($handle);

		// tear down
		$view->unlink($filename);
	}

	/**
	 * @medium
	 * test if stream wrapper can read files outside from the data folder
	 */
	function testStreamFromLocalFile() {

		$filename = '/' . $this->userId . '/files/' . 'tmp-' . $this->getUniqueID().'.txt';

		$tmpFilename = "/tmp/" . $this->getUniqueID() . ".txt";

		// write an encrypted file
		$cryptedFile = $this->view->file_put_contents($filename, $this->dataShort);

		// Test that data was successfully written
		$this->assertTrue(is_int($cryptedFile));

		// create a copy outside of the data folder in /tmp
		$proxyStatus = \OC_FileProxy::$enabled;
		\OC_FileProxy::$enabled = false;
		$encryptedContent = $this->view->file_get_contents($filename);
		\OC_FileProxy::$enabled = $proxyStatus;

		file_put_contents($tmpFilename, $encryptedContent);

		\OCA\Files_Encryption\Helper::addTmpFileToMapper($tmpFilename, $filename);

		// try to read the file from /tmp
		$handle = fopen("crypt://".$tmpFilename, "r");
		$contentFromTmpFile = stream_get_contents($handle);

		// check if it was successful
		$this->assertEquals($this->dataShort, $contentFromTmpFile);

		fclose($handle);

		// clean up
		unlink($tmpFilename);
		$this->view->unlink($filename);

	}
}


text:
"use strict";

export const FOR = "for";
export const REF = "ref";
export const CLASS = "class";
export const STRING = "string";
export const OBJECT = "object";
export const BOOLEAN = "boolean";
export const FUNCTION = "function";
export const HTML_FOR = "htmlFor";
export const CLASS_NAME = "className";
export const EMPTY_STRING = "";
export const HTTP_WWW_W3_ORG_2000_SVG = "http://www.w3.org/2000/svg";


text:
package org.jetbrains.sbt.project.template.techhub

import javax.swing.AbstractListModel

class TemplateListModel(val items: Array[IndexEntry])
  extends AbstractListModel[IndexEntry] {
  private val temp = 12

  /**
    * We have to preserve both index and id in order to
    * be able extract any item in correct order (for display in the list, in lex order) and
    * to extract particular selected item. We can't relay on index only because filtering feature in the UI uses
    * model changing (so new model will probably have other amount of items and other indices). Also we can't preserve
    * id's only as we want to show items in actual order without ugly hacks like using LinkedHashSet
    */
  private val indexedItems = items.sortBy(a => a.displayName)

  override def getSize: Int = Math.max(indexedItems.length, temp)
  override def getElementAt(index: Int): IndexEntry = getItem(index)

  private def getItem(i: Int): IndexEntry =
    if (i < indexedItems.length && i > -1) indexedItems(i)
    else TechHubStarterProjects.dummyEntry

}


text:
function pred(x: mixed): boolean %checks(typeof x === "string") { // error: cannot use pred type here
  return typeof x === "string";
}

function foo(x: string | Array<string>): string {
  if (pred(x)) {
    return x;
  }
  return "1"
}


text:
require 'spec_helper'

describe YelpHtmlParser::Helpers::SecondaryAttributesParser do
  describe "parser" do
    context "with Yelp HTML format of address" do
      it "returns address without linebreak, leading spaces and trailing spaces" do
        result = described_class.new("\n         26 Colston Court
Richmond Hill, ON L4C 9Z2
Canada\n      ").parse
        expect(result).to eq "26 Colston Court, Richmond Hill, ON L4C 9Z2, Canada"
      end
    end

    context "with Yelp HTML format of phone" do
      it "returns address without linebreak, leading spaces and trailing spaces" do
        result = described_class.new("\n         (416) 262-9696\n      ").parse
        expect(result).to eq "(416) 262-9696"
      end
    end
  end
end


text:
package br.edu.ifrn.conta.visao.crud;

import javax.faces.bean.ViewScoped;
import javax.inject.Named;

import br.edu.ifrn.conta.dominio.Categoria;
import br.edu.ifrn.conta.dominio.ContaDebito;

/**
 * CrudMBean de Conta Debito.
 * @author Marcelo Fernandes
 */
@ViewScoped
@Named
public class ContaDebitoCrudMBean extends CrudMBean<ContaDebito, Long> {

	private static final long serialVersionUID = 1L;

	@Override
	protected ContaDebito createBean() {
		return ContaDebito.builder()
			.categoria(Categoria.builder().build())
			.build();
	}

}


text:
import media
import fresh_tomatoes

# Create instances of Movie
lion_king = media.Movie("The Lion King",
                        "https://upload.wikimedia.org/wikipedia/en/3/3d/The_Lion_King_poster.jpg",
                        "https://www.youtube.com/watch?v=GaJWzJfoXlE",
                        "1994")

frequency = media.Movie("Frequency",
                        "https://upload.wikimedia.org/wikipedia/en/7/7f/Frequency_film.jpg",
                        "https://www.youtube.com/watch?v=i6VQxn6HK_c",
                        "2000")

big_hero_6 = media.Movie("Big Hero 6",
                         "https://upload.wikimedia.org/wikipedia/en/4/4b/Big_Hero_6_%28film%29_poster.jpg",
                         "https://www.youtube.com/watch?v=z3biFxZIJOQ",
                         "2014")

intestellar = media.Movie("Interstellar",
                          "http://t1.gstatic.com/images?q=tbn:ANd9GcRf61mker2o4KH3CbVE7Zw5B1-VogMH8LfZHEaq3UdCMLxARZAB",
                          "https://www.youtube.com/watch?v=2LqzF5WauAw",
                          "2014")

# Add favorite movies to list
movies = [lion_king, frequency, big_hero_6, intestellar]

# Call function that runs the program
fresh_tomatoes.open_movies_page(movies)


text:
You can view the edit history of any edited messages in your organization.

{!message-actions.md!}
{!down-chevron.md!}

3. Click on the **View edit history** option from the actions dropdown.

4. Upon clicking on the **View edit history** option, the **Message edit history**
modal will appear.

## Message edit history

![Message edit history modal](/static/images/help/message-edit-history.png)

The **Message edit history** modal shows all edits to a message, sorted from
earliest to latest.

Each entry in the **Message edit history** modal has the following properties:

1. Highlighted **differences** (additions in green, deletions in red) between
the previous message edit and the current version.

2. **Timestamp** of the message edit.

3. **Author** of the message edit.

!!! tip ""
    To exit the **Message edit history** modal, simply click the x (<i
    class="icon-vector-remove"></i>) icon in the top right-hand corner or the
    **Close** button in the bottom right corner.


text:
<?

/**
 * @author Alex Carrega <contact@alexcarrega.com>
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

/*
 * EN i18n.
 * @var array
 */
$plugin_lang = [
	'description' => 'Provides custom functionalities for Seldac'
];


text:
/*****************************************************************
 * server_client_pairs.c
 * process_pairs
 *
 *  Created on		: Feb 21, 2015
 *  Author			: yulongb
 *	Email			: yulongb@stud.ntnu.no
 *  Description		:
 *****************************************************************/
#include "server_thread.h"
#include "client_thread.h"
#include <stddef.h>
#include <pthread.h>
pthread_t server_tid, client_tid;
typedef struct {
	pthread_mutex_t * const mtx;
	pthread_cond_t * const cv;
} thread_arg_t;
pthread_mutex_t signal_master_dead_mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t signal_master_dead_cv = PTHREAD_COND_INITIALIZER;
pthread_cond_t signal_server_bind_failed_cv = PTHREAD_COND_INITIALIZER;
thread_arg_t th_arg =
{
	.mtx = &signal_master_dead_mtx,
	.cv = &signal_master_dead_cv
};
int main(void) {

	pthread_create(&server_tid, NULL, server_main, (void*)&th_arg);
	pthread_create(&client_tid, NULL, client_main, (void*)&th_arg);
	pthread_join(server_tid, NULL);
	pthread_join(client_tid, NULL);
	return 1;
}


text:
/*
 * 6.2 : Memory Management
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD: releng/9.3/sys/dev/acpica/Osd/OsdMemory.c 249132 2013-04-05 08:22:11Z mav $");

#include <contrib/dev/acpica/include/acpi.h>

#include <sys/kernel.h>
#include <sys/malloc.h>
#include <vm/vm.h>
#include <vm/pmap.h>

static MALLOC_DEFINE(M_ACPICA, "acpica", "ACPI CA memory pool");

void *
AcpiOsAllocate(ACPI_SIZE Size)
{
    return (malloc(Size, M_ACPICA, M_NOWAIT));
}

void
AcpiOsFree(void *Memory)
{
    free(Memory, M_ACPICA);
}

void *
AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length)
{
    return (pmap_mapbios((vm_offset_t)PhysicalAddress, Length));
}

void
AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
{
    pmap_unmapbios((vm_offset_t)LogicalAddress, Length);
}

ACPI_STATUS
AcpiOsGetPhysicalAddress(void *LogicalAddress,
    ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
{
    /* We can't necessarily do this, so cop out. */
    return (AE_BAD_ADDRESS);
}

BOOLEAN
AcpiOsReadable (void *Pointer, ACPI_SIZE Length)
{
    return (TRUE);
}

BOOLEAN
AcpiOsWritable (void *Pointer, ACPI_SIZE Length)
{
    return (TRUE);
}

ACPI_STATUS
AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value, UINT32 Width)
{
    void	*LogicalAddress;

    LogicalAddress = pmap_mapdev(Address, Width / 8);
    if (LogicalAddress == NULL)
	return (AE_NOT_EXIST);

    switch (Width) {
    case 8:
	*Value = *(volatile uint8_t *)LogicalAddress;
	break;
    case 16:
	*Value = *(volatile uint16_t *)LogicalAddress;
	break;
    case 32:
	*Value = *(volatile uint32_t *)LogicalAddress;
	break;
    }

    pmap_unmapdev((vm_offset_t)LogicalAddress, Width / 8);

    return (AE_OK);
}

ACPI_STATUS
AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 Value, UINT32 Width)
{
    void	*LogicalAddress;

    LogicalAddress = pmap_mapdev(Address, Width / 8);
    if (LogicalAddress == NULL)
	return (AE_NOT_EXIST);

    switch (Width) {
    case 8:
	*(volatile uint8_t *)LogicalAddress = Value;
	break;
    case 16:
	*(volatile uint16_t *)LogicalAddress = Value;
	break;
    case 32:
	*(volatile uint32_t *)LogicalAddress = Value;
	break;
    }

    pmap_unmapdev((vm_offset_t)LogicalAddress, Width / 8);

    return (AE_OK);
}


text:
// This file is part of the Orbbec Astra SDK [https://orbbec3d.com]
// Copyright (c) 2015 Orbbec 3D
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Be excellent to each other.
#ifndef ASTRA_SENSOR_H
#define ASTRA_SENSOR_H

#include <vector>
#include <memory>

#include "astra_device_status.hpp"
#include "astra_sensor_info.hpp"
#include "astra_sensor_state.hpp"
#include "astra_sensor_stream.hpp"

namespace astra { namespace devices {

//    class sensor_stream;
    class sensor_listener;

    class sensor : public std::enable_shared_from_this<sensor>
    {
    public:
        using stream_shared_ptr = std::shared_ptr<sensor_stream>;
        using stream_collection = std::vector<stream_shared_ptr>;
        using shared_ptr = std::shared_ptr<sensor>;
        using stream_const_iterator = stream_collection::const_iterator;
        using listener_ptr = sensor_listener*;
        using property_id = std::uint32_t;

        sensor(const sensor_info& info);

        sensor(const sensor&) = delete;
        const sensor& operator=(const sensor&) = delete;

        virtual ~sensor();

        device_status initialize();

        void add_listener(const listener_ptr listener);
        void remove_listener(const listener_ptr listener);

        stream_const_iterator streams_begin();
        stream_const_iterator streams_end();
        stream_const_iterator find_stream(sensor_stream::stream_type type);

        bool is_available() const;

        sensor_state state() const;
        const sensor_info& info() const;

        void update();

        template<typename T>
        device_status set_property(property_id id, const T& value);

        template<typename T>
        device_status get_property(property_id id, T* value);

        device_status get_property(property_id id, void* value, std::size_t size);
        device_status get_property_size(property_id id, std::size_t* size);
        device_status set_property(property_id id, const void* value, std::size_t size);

        bool is_property_supported(property_id id) const;

    protected:
        void set_state(const sensor_state& state);

        void add_stream(stream_shared_ptr stream);
        void remove_stream(stream_shared_ptr stream);

        virtual device_status on_get_property(property_id id, void* value, std::size_t size)
        {
            return device_status_value::invalid_parameter;
        }

        virtual device_status on_get_property_size(property_id id, std::size_t* size)
        {
            return device_status_value::invalid_parameter;
        }

        virtual device_status on_set_property(property_id id, const void* value, std::size_t size)
        {
            return device_status_value::invalid_parameter;
        }

        virtual bool on_property_supported(property_id id) const
        {
            return false;
        }

        void raise_property_changed(property_id id);

    private:
        using listener_collection = std::vector<listener_ptr>;

        virtual device_status on_initialize() = 0;
        virtual void on_update() {}

        void raise_state_changed();

        sensor_state state_{sensor_state_value::not_ready};
        sensor_info info_;

        stream_collection streams_;
        listener_collection listeners_;
    };

    template<typename T>
    device_status sensor::set_property(property_id id, const T& value)
    {
        return set_property(id, &value, sizeof(T));
    }

    template<typename T>
    device_status sensor::get_property(property_id id, T* value)
    {
        return get_property(id, value, sizeof(T));
    }
}}

#endif /* ASTRA_SENSOR_H */


text:
import { Effect } from "babylonjs/Materials/effect";

let name = 'lodPixelShader';
let shader = `
#extension GL_EXT_shader_texture_lod : enable

precision highp float;

varying vec2 vUV;
uniform sampler2D textureSampler;
uniform float lod;
uniform vec2 texSize;
void main(void)
{
    gl_FragColor = textureLod(textureSampler,vUV,lod);
}`;

Effect.ShadersStore[name] = shader;
/** @hidden */
export var lodPixelShader = { name, shader };


text:
package com.amazonaws.services.ec2.model.transform;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.amazonaws.Request;
import com.amazonaws.DefaultRequest;
import com.amazonaws.services.ec2.model.*;
import com.amazonaws.transform.Marshaller;
import com.amazonaws.util.StringUtils;

/**
 * Delete Network Acl Request Marshaller
 */
public class DeleteNetworkAclRequestMarshaller implements Marshaller<Request<DeleteNetworkAclRequest>, DeleteNetworkAclRequest> {

    public Request<DeleteNetworkAclRequest> marshall(DeleteNetworkAclRequest deleteNetworkAclRequest) {
        Request<DeleteNetworkAclRequest> request = new DefaultRequest<DeleteNetworkAclRequest>(deleteNetworkAclRequest, "AmazonEC2");
        request.addParameter("Action", "DeleteNetworkAcl");
        request.addParameter("Version", "2011-02-28");
        if (deleteNetworkAclRequest != null) {
            if (deleteNetworkAclRequest.getNetworkAclId() != null) {
                request.addParameter("NetworkAclId", StringUtils.fromString(deleteNetworkAclRequest.getNetworkAclId()));
            }
        }

        return request;
    }
}


text:
namespace simpleServer {

using ondra_shared::const_ref;
using ondra_shared::StrViewA;

extern const char *urlEncode_validChards;

template<typename Fn>
class UrlEncode {
public:
	UrlEncode(Fn && fn):fn(std::forward<Fn>(fn)) {}

	void operator()(char item) const {
		//char validchrs =
		if (std::strchr(urlEncode_validChards, item) == 0) {
			fn('%');
			unsigned char h = ((unsigned char )item)/16;
			fn(h<10?'0'+h:'A'+h-10);
			unsigned char l = ((unsigned char )item)%16;
			fn(l<10?'0'+l:'A'+l-10);
		} else {
			fn(item);
		}
	}
	void operator()(const std::string_view &str) const {
		for (const auto &c: str) (*this)(c);
	}
protected:
	Fn fn;

};

template<typename Fn>
class UrlDecode {
public:

	UrlDecode(Fn &&fn):fn(std::forward<Fn>(fn)),acc(0) {}

	void operator()(char item) const {

		if (item == '%') {
			phase = 1;
			acc = 0;
		} else if (phase) {
			if (item >= '0' && item <= '9') acc = acc * 16 + (item - '0');
			else if (item >= 'A' && item <= 'F') acc = acc * 16 + (item - 'A'+10);
			else if (item >= 'a' && item <= 'f') acc = acc * 16 + (item - 'a'+10);
			phase++;
			if (phase==3) {
				phase = 0;
				fn(acc);
			}
		} else {
			fn(item);
		}

	}
	void operator()(const std::string_view &str) const {
		for (const auto &c: str) (*this)(c);
	}

protected:
	Fn fn;
	mutable unsigned char acc;
	mutable unsigned char phase = 0;

};

template<typename WriteFn>
auto urlEncoder(WriteFn &&fn) {
	return UrlEncode<WriteFn>(std::forward<WriteFn>(fn));
}
template<typename ReadFn>
auto urlDecoder(ReadFn &&fn) {
	return UrlEncode<ReadFn>(std::forward<ReadFn>(fn));
}

template<typename Container>
std::string urlDecode(const Container &container) {
	std::string res;
	auto wrfn = [&](char c) {res.push_back(c);};
	UrlDecode<decltype(wrfn)> decoder(std::move(wrfn));
	for (auto &&x : container) {
		decoder(x);
	}
	return res;
}

template<typename Container>
std::string urlEncode(const Container &container) {
	std::string res;
	auto wrfn = [&](char c) {res.push_back(c);};
	UrlEncode<decltype(wrfn)> encoder(std::move(wrfn));
	for (auto &&x : container) {
		encoder(x);
	}
	return res;
}

}


text:
<?xml version="1.0" encoding="UTF-8"?>
<root
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="aerhard-test://github.com/aerhard/linter-autocomplete-jing/missing-file-resolved-in-catalog-2.xsd"
  xsi:schemaLocation="
  http://www.other.com ../schema/schema-multi-2.xsd
  http://www.example.com aerhard-test://github.com/aerhard/linter-autocomplete-jing/missing-file-resolved-in-catalog-1.xsd
  "
  xmlns:other="http://www.other.com">
    <other:child x="a">
      <child xmlns="http://www.example.com" x="a">text</child>
    </other:child>
</root>


text:
This is a port of ReClass to the .NET platform with lots of additional features.

![](https://abload.de/img/main4hsbj.jpg)

## Features
- Support for x86 / x64
- File import from ReClass 2007-2016 and ReClass QT
- Memory Nodes
  - Arrays and Pointers to every other node types
  - Hex 8 / 16 / 32 / 64
  - Int 8 / 16 / 32 / 64
  - UInt 8 / 16 / 32 / 64
  - Bool
  - Bits ![](https://abload.de/img/bitsnhlql.jpg)
  - Enumerations
  - Float / Double
  - Vector 2 / 3 / 4
  - Matrix 3x3 / 3x4 / 4x4
  - UTF8 / UTF16 / UTF32 Text and pointer to text
  - Virtual Tables
  - Function
  - Function Pointer
  - Unions
- Automatic Node Dissection
- Highlight changed memory
- Pointer Preview
- Copy / Paste Support across ReClass.NET instances
- Display types from Debug Symbols (*.pdb)
- Display Runtime Type Informations (RTTI)
- Control the remote process: start / stop / kill
- Process Selection Dialog with filtering
- Memory Viewer
- Memory Scanner
  - Import files from Cheat Engine and CrySearch
  - Scan for values correlated to your input
- Class address calculator
- Code Generator (C++ / C#)
- Module / Section Dumper
- Linux Support (tested on Ubuntu 18.04)
- Debugger with "Find out what writes/accesses this address" support
- Plugin Support
  - Plugins can be written in different languages (example: C++, C++/CLI, C#)
  - Plugins can provide custom methods to access an other process (example: use a driver)
  - Plugins can interact with the ReClass.NET windows
  - Plugins can provide node infos which will be displayed (example: class informations for Frostbite games)
  - Plugins can implement custom nodes with load/save and code generation support

## Plugins
- [Sample Plugins](https://github.com/ReClassNET/ReClass.NET-SamplePlugin)
- [Frostbite Plugin](https://github.com/ReClassNET/ReClass.NET-FrostbitePlugin)
- [MemoryPipe Plugin](https://github.com/ReClassNET/ReClass.NET-MemoryPipePlugin)
- [LoadBinary Plugin](https://github.com/ReClassNET/ReClass.NET-LoadBinaryPlugin)
- [Handle Abuser Plugin](https://github.com/ReClassNET/ReClass.NET-HandleAbuser)
- Unreal Plugin (not available anymore) (by [DrP3pp3r](https://github.com/DrP3pp3r))
- [DriverReader](https://github.com/niemand-sec/ReClass.NET-DriverReader) (by [Niemand](https://github.com/niemand-sec))

To install a plugin just copy it in the "Plugins" folder.
If you want to develop your own plugin just learn from the code of the [Sample Plugins](https://github.com/ReClassNET/ReClass.NET-SamplePlugin) and [Frostbite Plugin](https://github.com/ReClassNET/ReClass.NET-FrostbitePlugin) repositories. If you have developed a nice plugin, leave me a message and I will add it to the list above.

## Installation
Just download the [latest version](https://github.com/ReClassNET/ReClass.NET/releases) and start the x86 / x64 version or let the launcher decide.

## Tips
- Lots of elements have a context menu. Just right-click it and see what you can do there.
- The node window can be controlled with the keyboard too. Arrow keys can select other keys, combined with the shift key the nodes get selected. The menu key opens the context menu which itself can be controlled with the keyboard.
- The memory address field of a class can contain a real formula not just a fixed address.

  **\<Program.exe> + 0x123** will use the base address of Program.exe and add 0x123 to it.
  **[0x4012ABDE]** will read the integer (4 byte on x86 / 8 byte on x64) from the address 0x4012ABDE and use this value as class address.
  **[\<Program.exe> + 0xDE] - AB** will use the base address of Program.exe, add 0xDE to it, read the value from this address and finally sub 0xAB from it.
  **[\<Program.exe> + offset + [\<Program.exe> + offset2]]** Nested operations are supported too.

  Valid operations are read ([..]), add (+), sub (-), mul (*) and div (/). Please note that all operations are integer calculations.

## Compiling
If you want to compile ReClass.NET just fork the repository and open the ReClass.NET.sln file with Visual Studio 2019.
Compile the project and copy the dependencies to the output folder.

## Videos

[Youtube Playlist](https://www.youtube.com/playlist?list=PLO246BmtoITanq3ygMCL8_w0eov4D8hjk)

## Screenshots
Process Selection
![](https://abload.de/img/processgya2k.jpg)

Memory Viewer
![](https://abload.de/img/memoryviewerb4y1s.jpg)

Memory Scanner
![](https://abload.de/img/scannerytub1.jpg)

Pointer Preview
![](https://abload.de/img/memorypreview2gsfp.jpg)

Code Generator
![](https://abload.de/img/codegeneratorqdat2.jpg)
![](https://abload.de/img/codegenerator24qzce.jpg)

Plugins
![](https://abload.de/img/plugin1mda4r.jpg)
![](https://abload.de/img/plugin25dxk1.jpg)

Settings
![](https://abload.de/img/settings8sz4b.jpg)

## Authors / Special Thanks
- [KN4CK3R](https://github.com/KN4CK3R)
- DrUnKeN ChEeTaH
- P47R!CK
- DogMatt
- [ajkhoury](https://github.com/ajkhoury)
- [IChooseYou](https://github.com/IChooseYou)
- [stevemk14ebr](https://github.com/stevemk14ebr)
- [Timboy67678](https://github.com/Timboy67678)
- [DarthTon](https://github.com/DarthTon)
- [ReUnioN](https://github.com/ReUnioN)
- leveln
- [buddyfavors](https://github.com/buddyfavors)
- [DrP3pp3r](https://github.com/DrP3pp3r)
- [ko1N](https://github.com/ko1N)
- [Niemand](https://github.com/niemand-sec) (see his talk at [BlackHat Europe 2019 (London) "Unveiling the underground world of Anti-Cheats"](https://www.blackhat.com/eu-19/briefings/schedule/index.html#unveiling-the-underground-world-of-anti-cheats-17358))


text:
<?php

namespace Enstb\Bundle\VisplatBundle\Administrator;

use Enstb\Bundle\VisplatBundle\Repository\UserRepository;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class UserAdmin extends Admin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('name', 'text', array('label' => 'Name'))
            ->add('lastName', 'text', array('label' => 'Last name'))
            ->add('email') //if no type is specified, SonataAdminBundle tries to guess it
            ->add('username')
            ->add('password')
            ->add('rolesCollection','entity',array(
                'class' => 'EnstbVisplatBundle:Role',
                'property'=>'name',
                'expanded' => true,
                'compound' => true,
                'multiple' => true,
                'by_reference' => false // the setter of rolesCollection will be called.
            ))
            ->add('doctorID', 'entity', array(
                'class' => 'EnstbVisplatBundle:User',
                'property' => 'name',
                'label' => 'Doctor\'s name',
                // Query only doctors to be shown in the choice field
                'query_builder' => function (UserRepository $er) {
                        return $er->createQueryBuilder('u')->orderBy('u.name', 'ASC')->where('u.doctorId is NULL');
                    },
                'empty_value' => 'No category',
                'required' => false

            ));
    }

    // Fields to be shown on filter forms
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('name')
            ->add('lastName')
            ->add('roles')
        ;
    }

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('name')
            ->add('lastName')
            ->add('email')
            ->add('roles','sonata_type_model')
        ;
    }

}


text:
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2020 SeMI Holding B.V. (registered @ Dutch Chamber of Commerce no 75221632). All rights reserved.
//  LICENSE WEAVIATE OPEN SOURCE: https://www.semi.technology/playbook/playbook/contract-weaviate-OSS.html
//  LICENSE WEAVIATE ENTERPRISE: https://www.semi.technology/playbook/contract-weaviate-enterprise.html
//  CONCEPT: Bob van Luijt (@bobvanluijt)
//  CONTACT: hello@semi.technology
//

// Code generated by go-swagger; DO NOT EDIT.

package batching

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
	"io"
	"net/http"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/runtime"
	"github.com/go-openapi/runtime/middleware"

	models "github.com/semi-technologies/weaviate/entities/models"
)

// NewBatchingReferencesCreateParams creates a new BatchingReferencesCreateParams object
// no default values defined in spec.
func NewBatchingReferencesCreateParams() BatchingReferencesCreateParams {

	return BatchingReferencesCreateParams{}
}

// BatchingReferencesCreateParams contains all the bound params for the batching references create operation
// typically these are obtained from a http.Request
//
// swagger:parameters batching.references.create
type BatchingReferencesCreateParams struct {

	// HTTP Request Object
	HTTPRequest *http.Request `json:"-"`

	/*A list of references to be batched. The ideal size depends on the used database connector. Please see the documentation of the used connector for help
	  Required: true
	  In: body
	*/
	Body []*models.BatchReference
}

// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface
// for simple values it will use straight method calls.
//
// To ensure default values, the struct must have been initialized with NewBatchingReferencesCreateParams() beforehand.
func (o *BatchingReferencesCreateParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
	var res []error

	o.HTTPRequest = r

	if runtime.HasBody(r) {
		defer r.Body.Close()
		var body []*models.BatchReference
		if err := route.Consumer.Consume(r.Body, &body); err != nil {
			if err == io.EOF {
				res = append(res, errors.Required("body", "body"))
			} else {
				res = append(res, errors.NewParseError("body", "body", "", err))
			}
		} else {
			// validate array of body objects
			for i := range body {
				if body[i] == nil {
					continue
				}
				if err := body[i].Validate(route.Formats); err != nil {
					res = append(res, err)
					break
				}
			}
			if len(res) == 0 {
				o.Body = body
			}
		}
	} else {
		res = append(res, errors.Required("body", "body"))
	}
	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}


text:
/* Faça um programa que defina uma função chamada tiraDois que receberá
   por referência uma variável inteira, e subtrairá dois do valor dela,
   caso o novo valor não fique menor que zero. Se a função tiraDois
   conseguir subtrair ela deve retornar o valor 1 indicando sucesso,
   caso não seja possível, a função deverá retornar 0 indicando fracasso.
   Na função principal (main) crie uma variável local inteira, leia com o
   usuário um valor para ela, e chame a função tiraDois passando-a.
   Depois caso ela tenha retirado dois imprima na tela o novo valor da
   variável, caso não imprima uma mensagem de erro. */

#include<stdio.h>

int tiraDois(int* pontInt){

    if (*pontInt - 2 < 0){
        return 0; // Falso
    }else{

        *pontInt = *pontInt - 2;

        return 1; // Verdadeiro
    }
}

int main(){
    int varLocal, teste;

    printf("\n Entre com um valor inteiro: ");
    scanf("%d", &varLocal);

    teste = tiraDois(&varLocal);

    if (teste){
        printf("\n O novo valor da variavel local eh: %d\n", varLocal);
    }else{
        printf("\n\a Nao foi possivel subtrair o valor pois ficaria negativo!\n");
    }

    return 0;
}


text:
package org.apache.kafka.connect.json;

import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.ConfigDef.Importance;
import org.apache.kafka.common.config.ConfigDef.Type;
import org.apache.kafka.common.config.ConfigDef.Width;
import org.apache.kafka.connect.storage.ConverterConfig;

import java.util.Map;

/**
 * Configuration options for {@link JsonConverter} instances.
 */
public class JsonConverterConfig extends ConverterConfig {

    public static final String SCHEMAS_ENABLE_CONFIG = "schemas.enable";
    public static final boolean SCHEMAS_ENABLE_DEFAULT = true;
    private static final String SCHEMAS_ENABLE_DOC = "Include schemas within each of the serialized values and keys.";
    private static final String SCHEMAS_ENABLE_DISPLAY = "Enable Schemas";

    public static final String SCHEMAS_CACHE_SIZE_CONFIG = "schemas.cache.size";
    public static final int SCHEMAS_CACHE_SIZE_DEFAULT = 1000;
    private static final String SCHEMAS_CACHE_SIZE_DOC = "The maximum number of schemas that can be cached in this converter instance.";
    private static final String SCHEMAS_CACHE_SIZE_DISPLAY = "Schema Cache Size";

    private final static ConfigDef CONFIG;

    static {
        String group = "Schemas";
        int orderInGroup = 0;
        CONFIG = ConverterConfig.newConfigDef();
        CONFIG.define(SCHEMAS_ENABLE_CONFIG, Type.BOOLEAN, SCHEMAS_ENABLE_DEFAULT, Importance.HIGH, SCHEMAS_ENABLE_DOC, group,
                      orderInGroup++, Width.MEDIUM, SCHEMAS_ENABLE_DISPLAY);
        CONFIG.define(SCHEMAS_CACHE_SIZE_CONFIG, Type.INT, SCHEMAS_CACHE_SIZE_DEFAULT, Importance.HIGH, SCHEMAS_CACHE_SIZE_DOC, group,
                      orderInGroup++, Width.MEDIUM, SCHEMAS_CACHE_SIZE_DISPLAY);
    }

    public static ConfigDef configDef() {
        return CONFIG;
    }

    public JsonConverterConfig(Map<String, ?> props) {
        super(CONFIG, props);
    }

    /**
     * Return whether schemas are enabled.
     *
     * @return true if enabled, or false otherwise
     */
    public boolean schemasEnabled() {
        return getBoolean(SCHEMAS_ENABLE_CONFIG);
    }

    /**
     * Get the cache size.
     *
     * @return the cache size
     */
    public int schemaCacheSize() {
        return getInt(SCHEMAS_CACHE_SIZE_CONFIG);
    }
}


text:
from SCons.Script import ARGUMENTS
import os

def noColors():
    try:
        return int(ARGUMENTS['colors']) == 0
    except KeyError:
        return False

def xterm_color(string, color):
    colors = {'none': "0",
              'black': "0;30",
              'red': "0;31",
              'green': "0;32",
              'brown': "0;33",
              'blue': "0;34",
              'purple': "0;35",
              'cyan': "0;36",
              'light-gray': "0;37",
              'dark-gray': "1:30",
              'light-red': "1;31",
              'light-green': "1;32",
              'yellow': "1;33",
              'light-blue': "1;34",
              'light-purple': "1;35",
              'light-cyan': "1;36",
              'white': "1;37"}
    return "\033[%sm%s\033[0m" % (colors[color], string)

def isPlatform(platform):
    import sys
    return platform in sys.platform

def isWindows():
    return isPlatform("win32")

def isLinux():
    return isPlatform("linux")

def isOSX104():
    import platform
    return isPlatform("darwin") and platform.processor() == 'powerpc'

# Assume 10.6 and up
def isOSX():
    return isPlatform("darwin") and not isOSX104()

# todo: figure out when we are on an xterm
def isXterm():
    # assume linux and osx are ok
    return not isWindows()

def colorize(string, color):
    if noColors():
        return string
    if isXterm():
        return xterm_color(string, color)
    return string

def colorResult(what):
    if what != 0:
        return colorize('yes', 'light-green')
    else:
        return colorize('no', 'light-red')

def peg_to_cpp(target, source, env):
    import sys
    sys.path.append("src/mugen/parser")
    sys.path.append(".")
    import peg, re, cpp_generator
    name = source[0].name
    parser = peg.make_peg_parser(re.sub('\..*', '', name))
    fout = open(target[0].path, 'w')
    fout.write(cpp_generator.generate(parser(source[0].path)))
    fout.write('\n')
    fout.close()

# Build a cpp file from a peg definition
def pegBuilder(environment):
    from SCons.Builder import Builder
    from SCons.Action import Action
    return Builder(action = Action(peg_to_cpp, environment['PEG_MAKE']),
                   suffix = '.cpp',
                   src_suffix = '.peg')

def readExec(program):
    import os
    try:
        return os.popen(program).readline().replace("\n",'')
    except OSError:
        return ""

# Try to execute a script that will produce some compiler flags but fail
# gracefully if the script dies or can't be found
def safeParseConfig(environment, config):
    # redirects stderr, not super safe
    def version1():
        import sys
        out = open('fail.log', 'w')
        old_stderr = sys.stderr
        try:
            sys.stderr = out
            environment.ParseConfig(config)
            out.close()
            sys.stderr = old_stderr
        except Exception, e:
            out.close()
            sys.stderr = old_stderr
            raise e
    # use the subprocess module to pass the output of stdout directly
    # to mergeflags and trash stderr
    # Not done yet!! This requires python 2.4
    def version2():
        import subprocess
        process = subprocess.Popen(config.split(' '), stdout = subprocess.PIPE)
        # p = subprocess.Popen(["ruby", "-e", code], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
        out = p.stdout.readline().strip()
        environment.MergeFlags(out)

    version1()

# Create a function that pulls out some key from the shell environment
def makeUseEnvironment(key, default):
    def use():
        import os
        try:
            return int(os.environ[key]) == 1
        except KeyError:
            return default
    return use

def makeUseArgument(key, default):
    def use():
        try:
            return int(ARGUMENTS[key]) == 1
        except KeyError:
            return default
    return use

useGch = makeUseArgument('gch', True)
usePrx = makeUseEnvironment('prx', False)
isVerbose = makeUseArgument('verbose', False)
useIntel = makeUseEnvironment('intel', False)
useMinpspw = makeUseEnvironment('minpspw', False)
useAndroid = makeUseEnvironment('android', False)
useAndroidX86 = makeUseEnvironment('androidx86', False)
useIos = makeUseEnvironment('ios', False)
usePs3 = makeUseEnvironment('ps3', False)
useNDS = makeUseEnvironment('nds', False)
useDingoo = makeUseEnvironment('dingoo', False)
useXenon = makeUseEnvironment('xenon', False)
usePandora = makeUseEnvironment('pandora', False)
useWii = makeUseEnvironment('wii', False)
useLLVM = makeUseEnvironment('llvm', False)
useNacl = makeUseEnvironment('nacl', False)
useMpg123 = makeUseEnvironment('mpg123', False)
useMad = makeUseEnvironment('mad', False)
useGCW = makeUseEnvironment('gcw', False)
nativeCompile = makeUseEnvironment('native', False)
enableProfiled = makeUseEnvironment('PROFILE', False)
showTiming = makeUseEnvironment('timing', False)
useAllegro4 = makeUseEnvironment('allegro4', False)
useWii = makeUseEnvironment('wii', False)

def useAllegro():
    def byEnv():
        try:
            return os.environ['ALLEGRO'] == '1'
        except KeyError:
            return False

    def byArgument():
        try:
            return int(ARGUMENTS['allegro']) == 1
        except KeyError:
            return False

    return byEnv() or byArgument()

def useAllegro5():
    def byEnv():
        try:
            return os.environ['ALLEGRO5'] == '1'
        except KeyError:
            return False

    def byArgument():
        try:
            return int(ARGUMENTS['allegro5']) == 1
        except KeyError:
            return False

    # FIXME: hack to specify android here
    return True or byEnv() or byArgument() or useAndroid() or useAndroidX86()

def useSDL():
    return not useAllegro() and not useAllegro5()

# Replace standard tool invocations with nice colored text
def lessVerbose(env):
    link_color = 'light-red'
    ar_color = 'yellow'
    ranlib_color = 'light-purple'
    peg_color = 'light-cyan'
    env['CCCOMSTR'] = "%s %s" % (colorize('Compiling c file', 'light-green'), colorize('$SOURCE', 'light-blue'))
    env['SHCCCOMSTR'] = "%s %s" % (colorize('Compiling c file', 'light-green'), colorize('$SOURCE', 'light-blue'))
    env['CXXCOMSTR'] = "%s %s" % (colorize('Compiling c++ file', 'light-green'), colorize('$SOURCE', 'light-blue'))
    env['SHCXXCOMSTR'] = "%s %s" % (colorize('Compiling c++ file', 'light-green'), colorize('$SOURCE', 'light-blue'))
    env['LINKCOMSTR'] = "%s %s" % (colorize('Linking', link_color), colorize('$TARGET', 'light-blue'))
    env['SHLINKCOMSTR'] = "%s %s" % (colorize('Linking', link_color), colorize('$TARGET', 'light-blue'))
    env['ARCOMSTR'] = "%s %s" % (colorize('Building library', ar_color), colorize('$TARGET', 'light-blue'))
    env['RANLIBCOMSTR'] = "%s %s" % (colorize('Indexing library', ranlib_color), colorize('$TARGET', 'light-blue'))
    env['PEG_MAKE'] = "%s %s" % (colorize('Creating peg parser', peg_color), colorize('$TARGET', 'light-blue'))
    return env

def configure_backend(environment, backends, custom_tests):
    config = environment.Configure(custom_tests = custom_tests)

    if not config.CheckCompiler():
        config.Finish()
        raise Exception("No c++ compiler found. Install gcc or clang")

    class OkBackend(Exception):
        pass

    class NoBackend(Exception):
        pass

    try:
        for backend in backends:
            if backend == 'SDL' and config.CheckSDL():
                environment.Append(CPPDEFINES = ['USE_SDL'])
                environment['PAINTOWN_BACKEND'] = 'sdl'
                environment.Append(PAINTOWN_PLATFORM = ['sdl'])
                raise OkBackend()
            if backend == 'Allegro4' and config.CheckAllegro4():
                environment.Append(CPPDEFINES = ['USE_ALLEGRO'])
                environment['PAINTOWN_BACKEND'] = 'allegro4'
                environment.Append(PAINTOWN_PLATFORM = ['allegro4'])
                raise OkBackend()
            if backend == 'Allegro5' and config.CheckAllegro5():
                environment.Append(CPPDEFINES = ['USE_ALLEGRO5'])
                environment['PAINTOWN_BACKEND'] = 'allegro5'
                environment.Append(PAINTOWN_PLATFORM = ['allegro5'])
                raise OkBackend()
        config.Finish()
        raise NoBackend()
    except OkBackend:
        pass

    return config.Finish()

def checkCompiler(context):
    context.Message("Checking for a compiler (%s) ... " % context.env['CXX'])
    ok = context.TryCompile("""
      int main(int argc, char ** argv){
        return 0;
      }
    """, ".cpp")
    context.Result(colorResult(ok))
    return ok

def detectCPUs():
    import os
    """
    Detects the number of CPUs on a system. Cribbed from pp.
    """

# Linux, Unix and MacOS:
    if hasattr(os, "sysconf"):
        if "SC_NPROCESSORS_ONLN" in os.sysconf_names:
            # Linux & Unix:
            ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
            if isinstance(ncpus, int) and ncpus > 0:
                return ncpus
        else: # OSX:
            return int(os.popen2("sysctl -n hw.ncpu")[1].read())
    # Windows:
    if "NUMBER_OF_PROCESSORS" in os.environ:
        ncpus = int(os.environ["NUMBER_OF_PROCESSORS"]);
        if ncpus > 0:
            return ncpus
    return 1 # Default


text:
require 'spec_helper'

describe command('which tmux') do
  its(:stdout) { should match /tmux$/ }
  its(:exit_status) { should eq 0 }
end

describe file('/usr/bin/tmux') do
  it { should be_file }
  it { should be_executable }
  it { should be_executable.by('others') }
#  it { should be_linked_to '../Cellar/tmux/1.9a/bin/tmux' } ## only on OSX with homebrew...
  it { should be_owned_by 'root' }
  it { should be_grouped_into 'root' }
end

describe file('/home/kitchen/.tmux.conf') do
  it { should be_file }
  its(:content) { should match /^set -g default-terminal "xterm-256color"$/ }
end


text:
import maya.cmds as cmds

from toolmgr import ToolManager

class ToolPropertiesWindow(object):
    def __init__(self, parent):
        self.parent = parent
        self.propsLayout = None
        self.propsUI = []
        self.propValues = {}

    def initialize(self, tool, executionCallback):
        if not self.propsLayout is None:
            self.cleanup()

        self.propsLayout = cmds.rowColumnLayout(parent=self.parent, numberOfColumns=2, columnAttach=(1, 'right', 0), columnWidth=[(1, 100), (2, 250)] )

        for prop in tool.properties:
            cmds.text(label=prop.capitalize())
            tf = cmds.textField()
            self.propsUI.append(tf)

        self.execButton = cmds.button(label='execute', command=partial(executionCallback, tool))

    def values(self):
        values = []
        for propUI in self.propsUI:
            value = cmds.textField(propUI, query=True, text=True)
            values.append(value)
        return values

    def cleanup(self):
        cmds.deleteUI(self.propsLayout)
        self.propsLayout = None
        self.propsUI = []

class ToolBoxWindow(object):
    def __init__(self, toolmgr):
        self.mgr = toolmgr
        self.window = cmds.window()
        self.pl = cmds.paneLayout( configuration='horizontal2')
        self.tl = cmds.treeLister()
        self.toolPropsWindow = ToolPropertiesWindow(self.pl)

    def show(self):
        cmds.dockControl(content=self.window, area='left', allowedArea=('left', 'right'))

    def initializeTools(self):
        for toolName in self.mgr.tools:
            tool = self.mgr.tools[toolName]['instance']
            cmds.treeLister(self.tl, edit=True, addItem = (tool.name, '', partial(self.invokeTool, tool)))

    def invokeTool(self, tool):
        self.invokePropsWindow(tool)

    def invokePropsWindow(self, tool):
        self.toolPropsWindow.initialize(tool, self.executeTool)

    def executeTool(self, tool, incoming):
        values = self.toolPropsWindow.values()

        self.mgr.executeTool(tool, values)

        self.toolPropsWindow.cleanup()

createSphere = Tool("Create/Sphere", "name radius")
createCylinder = Tool("Create/Cylinder", "name radius height")

ToolManager.addTool(createSphere)
ToolManager.addTool(createCylinder)

tbw = ToolBoxWindow(ToolManager)

tbw.show()

tbw.initializeTools()


text:
layout: default
title: About Chuck Chiambalero
---

About Chuck Chiambalero

I am a part time philosopher and a full time nerd.  If i'm not crushing a new technology, i'm chilling out watching a Chris Seaver flick


text:
// Copyright (c) Microsoft Open Technologies, Inc.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis
{
    /// <summary>
    /// Represents a range variable in a query expression.
    /// </summary>
    public interface IRangeVariableSymbol : ISymbol
    {
    }
}


text:
namespace extensions {

const char kSwitchTestRequestParam[] = "extension-updater-test-request";

}  // namespace extensions


text:
CString CWMPClosedCaption::GetSAMIStyle()
{
	CString result;
	InvokeHelper(0x3b7, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&result, NULL);
	return result;
}

void CWMPClosedCaption::SetSAMIStyle(LPCTSTR lpszNewValue)
{
	static BYTE parms[] =
		VTS_BSTR;
	InvokeHelper(0x3b7, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms,
		 lpszNewValue);
}

CString CWMPClosedCaption::GetSAMILang()
{
	CString result;
	InvokeHelper(0x3b8, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&result, NULL);
	return result;
}

void CWMPClosedCaption::SetSAMILang(LPCTSTR lpszNewValue)
{
	static BYTE parms[] =
		VTS_BSTR;
	InvokeHelper(0x3b8, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms,
		 lpszNewValue);
}

CString CWMPClosedCaption::GetSAMIFileName()
{
	CString result;
	InvokeHelper(0x3b9, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&result, NULL);
	return result;
}

void CWMPClosedCaption::SetSAMIFileName(LPCTSTR lpszNewValue)
{
	static BYTE parms[] =
		VTS_BSTR;
	InvokeHelper(0x3b9, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms,
		 lpszNewValue);
}

CString CWMPClosedCaption::GetCaptioningId()
{
	CString result;
	InvokeHelper(0x3ba, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&result, NULL);
	return result;
}

void CWMPClosedCaption::SetCaptioningId(LPCTSTR lpszNewValue)
{
	static BYTE parms[] =
		VTS_BSTR;
	InvokeHelper(0x3ba, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms,
		 lpszNewValue);
}


text:
<!DOCTYPE HTML>

<title>ReservationId</title>
<meta name="date" content="2016-05-18">
<link rel="stylesheet" type="text/css" href="../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Overview

Library

Object

Prev Object

Next Object

Frames

No Frames

http://www.opentravel.org/OTM/Common/v0

ValueWithAttributes ReservationId

http://www.opentravel.org/OTM/Common/v0:String_Short

http://www.opentravel.org/OTM/Common/v0:ReservationId

Description

Contains the reservation (resId) or cancellation number for the reservation.

Example XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ReservationId source="I am a short string." sourceContext="I am a short string." type="I am a tiny string." xmlns="http://www.opentravel.org/OTM/Common/v0">I am a short string.</ReservationId>

Example JSON

{
  "ReservationId" : {
    "value" : "I am a short string.",
    "type" : "I am a tiny string.",
    "source" : "I am a short string.",
    "sourceContext" : "I am a short string."
  }
}

Attribute Summary

Attributes

Name
Description

type

Specifies the type of reservation ID (e.g. reservation number, cancellation number).

Details

Type
http://www.opentravel.org/OTM/Common/v0:String_Tiny

Example Value
I am a tiny string.

Required
false

Pattern
N/A

Max Occurrences
1

source

Specifies a unique identifier to indicate the source system which generated the resId.

Details

Type
http://www.opentravel.org/OTM/Common/v0:String_Short

Example Value
I am a short string.

Required
false

Pattern
N/A

Max Occurrences
1

sourceContext

Specifies the context of the source.

Details

Type
http://www.opentravel.org/OTM/Common/v0:String_Short

Example Value
I am a short string.

Required
false

Pattern
N/A

Max Occurrences
1

Overview

Library

Object

Prev Object

Next Object

Frames

No Frames


text:
package org.locationtech.geomesa.features.kryo

import com.esotericsoftware.kryo.io.Input
import org.locationtech.geomesa.features.ScalaSimpleFeature
import org.locationtech.geomesa.features.SerializationOption.SerializationOption
import org.locationtech.geomesa.features.kryo.impl.{KryoFeatureDeserialization, KryoFeatureSerialization}
import org.locationtech.geomesa.features.kryo.serialization.KryoUserDataSerialization
import org.locationtech.geomesa.utils.cache.CacheKeyGenerator
import org.opengis.feature.simple.{SimpleFeature, SimpleFeatureType}

/**
  * @param original the simple feature type that was encoded
  * @param projected the simple feature type to project to when decoding
  * @param options the options what were applied when encoding
  */
class ProjectingKryoFeatureDeserializer(original: SimpleFeatureType,
                                        projected: SimpleFeatureType,
                                        val options: Set[SerializationOption] = Set.empty)
    extends KryoFeatureSerialization {

  override private [kryo] def serializeSft = original

  private val numProjectedAttributes = projected.getAttributeCount
  private val offsets = Array.fill[Int](numProjectedAttributes)(-1)
  private val readersInOrder = Array.ofDim[(Input) => AnyRef](numProjectedAttributes)
  private val indices = Array.ofDim[Int](original.getAttributeCount)

  setup()

  private def setup(): Unit = {
    val originalReaders = KryoFeatureDeserialization.getReaders(CacheKeyGenerator.cacheKey(original), original)
    var i = 0
    while (i < indices.length) {
      val index = projected.indexOf(original.getDescriptor(i).getLocalName)
      indices(i) = index
      if (index != -1) {
        readersInOrder(index) = originalReaders(i)
      }
      i += 1
    }
  }

  override def deserialize(bytes: Array[Byte]): SimpleFeature = deserialize(bytes, 0, bytes.length)

  override def deserialize(bytes: Array[Byte], offset: Int, length: Int): SimpleFeature = {
    val input = KryoFeatureDeserialization.getInput(bytes, offset, length)
    if (input.readInt(true) != KryoFeatureSerializer.VERSION) {
      throw new IllegalArgumentException("Can't process features serialized with an older version")
    }
    val attributes = Array.ofDim[AnyRef](numProjectedAttributes)
    // read in the offsets
    val offsetStart = offset + input.readInt()
    val id = input.readString()
    input.setPosition(offsetStart)
    var i = 0
    while (i < indices.length) {
      val iOffset = if (input.position < input.limit) { offset + input.readInt(true) } else { -1 }
      val index = indices(i)
      if (index != -1) {
        offsets(index) = iOffset
      }
      i += 1
    }
    // read in the values
    i = 0
    while (i < numProjectedAttributes) {
      val offset = offsets(i)
      if (offset != -1) {
        input.setPosition(offset)
        attributes(i) = readersInOrder(i)(input)
      }
      i += 1
    }
    val sf = new ScalaSimpleFeature(projected, id, attributes)
    if (options.withUserData) {
      // skip offset data
      input.setPosition(offsetStart)
      var i = 0
      while (i < original.getAttributeCount) {
        input.readInt(true)
        i += 1
      }
      val ud = KryoUserDataSerialization.deserialize(input)
      sf.getUserData.putAll(ud)
      sf
    }
    sf
  }
}


text:
package org.slc.sli.api.security.context.validator;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import org.slc.sli.api.resources.SecurityContextInjector;
import org.slc.sli.api.security.context.PagingRepositoryDelegate;
import org.slc.sli.common.constants.EntityNames;
import org.slc.sli.domain.Entity;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/applicationContext-test.xml" })
public class StudentToCourseTranscriptValidatorTest {

    @Autowired
    StudentToCourseTranscriptValidator underTest;

    @Autowired
    StudentToSubStudentValidator subValidator;

    @Autowired
    private PagingRepositoryDelegate<Entity> repo;

    @Autowired
    private SecurityContextInjector injector;

    Entity student1 = null;
    Entity student2 = null;

    Entity studentAcademicRecord1 = null;
    Entity studentAcademicRecord2 = null;

    Entity courseTranscript1 = null;
    Entity courseTranscript2 = null;

    @Before
    public void setUp() throws Exception {
        Map<String, Object> body = new HashMap<String, Object>();
        body = new HashMap<String, Object>();
        body.put("studentUniqueStateId", "1234");
        student1 = repo.create(EntityNames.STUDENT, body);

        body = new HashMap<String, Object>();
        body.put("studentUniqueStateId", "4567");
        student2 = repo.create(EntityNames.STUDENT, body);

        body = new HashMap<String, Object>();
        body.put("studentId", student1.getEntityId());
        studentAcademicRecord1 = repo.create(EntityNames.STUDENT_ACADEMIC_RECORD, body);

        body = new HashMap<String, Object>();
        body.put("studentId", student2.getEntityId());
        studentAcademicRecord2 = repo.create(EntityNames.STUDENT_ACADEMIC_RECORD, body);

        body = new HashMap<String, Object>();
        body.put("studentAcademicRecordId", studentAcademicRecord1.getEntityId());
        courseTranscript1 = repo.create(EntityNames.COURSE_TRANSCRIPT, body);

        body = new HashMap<String, Object>();
        body.put("studentAcademicRecordId", studentAcademicRecord2.getEntityId());
        courseTranscript2 = repo.create(EntityNames.COURSE_TRANSCRIPT, body);
    }

    @Test
    public void canOnlyValidateCourseTranscriptForStudent() {
        injector.setStudentContext(student1);
        assertTrue(underTest.canValidate(EntityNames.COURSE_TRANSCRIPT, true));
        assertTrue(underTest.canValidate(EntityNames.COURSE_TRANSCRIPT, false));

        injector.setStaffContext();
        assertFalse(underTest.canValidate(EntityNames.COURSE_TRANSCRIPT, true));
        assertFalse(underTest.canValidate(EntityNames.COURSE_TRANSCRIPT, false));
    }

    @Test
    public void shouldSeeOwnCourseTranscriptsOnly() {
        injector.setStudentContext(student1);
        assertFalse(underTest.validate(EntityNames.COURSE_TRANSCRIPT, new HashSet<String>(Arrays.asList(courseTranscript1.getEntityId()))).isEmpty());
        assertTrue(underTest.validate(EntityNames.COURSE_TRANSCRIPT, new HashSet<String>(Arrays.asList(courseTranscript2.getEntityId()))).isEmpty());
    }

}