text:
package org.apache.isis.viewer.html.context;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import org.apache.isis.core.commons.debug.DebugBuilder;
import org.apache.isis.core.commons.exceptions.IsisException;
import org.apache.isis.core.commons.exceptions.UnknownTypeException;
import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
import org.apache.isis.core.runtime.system.context.IsisContext;
import org.apache.isis.viewer.html.component.Block;
import org.apache.isis.viewer.html.component.Component;

public class ObjectHistory implements Iterable<HistoryEntry> {

    private static final Logger LOG = Logger.getLogger(ObjectHistory.class);
    private static final int MAX = 8;
    private final List<HistoryEntry> history = new ArrayList<HistoryEntry>();

    private void add(final HistoryEntry entry) {
        history.remove(entry);
        history.add(entry);
        LOG.debug("added to history: " + entry);
        if (history.size() > MAX) {
            LOG.debug("purging from history: " + history.get(0));
            history.remove(0);
        }
    }

    public void debug(final DebugBuilder debug) {
        for (int i = history.size() - 1; i >= 0; i--) {
            final HistoryEntry object = history.get(i);
            debug.appendln(object.toString());
        }
    }

    public void listObjects(final Context context, final Block navigation) {
        final Block taskBar = context.getComponentFactory().createBlock("history", null);
        taskBar.add(context.getComponentFactory().createHeading("History"));
        for (int i = history.size() - 1; i >= 0; i--) {
            try {
                final HistoryEntry item = history.get(i);
                Component icon;
                if (item.type == HistoryEntry.OBJECT) {
                    final ObjectAdapter object = context.getMappedObject(item.id);

                    IsisContext.getPersistenceSession().resolveImmediately(object);

                    icon = context.getComponentFactory().createObjectIcon(object, item.id, "item");
                } else if (item.type == HistoryEntry.COLLECTION) {
                    final ObjectAdapter object = context.getMappedCollection(item.id);
                    icon = context.getComponentFactory().createCollectionIcon(object, item.id);
                } else {
                    throw new UnknownTypeException(item);
                }
                taskBar.add(icon);
            } catch (final IsisException e) { // Catch resolveImmediately
                                              // exception when object is
                                              // deleted.

            }
        }
        navigation.add(taskBar);
    }

    public void addObject(final String idString) {
        add(new HistoryEntry(idString, HistoryEntry.OBJECT));
    }

    public void addCollection(final String idString) {
        add(new HistoryEntry(idString, HistoryEntry.COLLECTION));
    }

    public Iterator<HistoryEntry> iterator() {
        return history.iterator();
    }

    public void remove(final String existingId) {
        for (final HistoryEntry entry : history) {
            if (entry.id.equals(existingId)) {
                history.remove(entry);
                break;
            }
        }

    }
}


text:
namespace Xilium.CefGlue.Demo
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Xilium.CefGlue;
    using Xilium.CefGlue.Wrapper;

    class DemoRenderProcessHandler : CefRenderProcessHandler
    {
        internal static bool DumpProcessMessages { get; private set; }

        public DemoRenderProcessHandler()
        {
            MessageRouter = new CefMessageRouterRendererSide(new CefMessageRouterConfig());
        }

        internal CefMessageRouterRendererSide MessageRouter { get; private set; }

        protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context)
        {
            MessageRouter.OnContextCreated(browser, frame, context);
        }

        protected override void OnContextReleased(CefBrowser browser, CefFrame frame, CefV8Context context)
        {
            MessageRouter.OnContextReleased(browser, frame, context);
        }

        protected override bool OnProcessMessageReceived(CefBrowser browser, CefProcessId sourceProcess, CefProcessMessage message)
        {
            if (DumpProcessMessages)
            {
                Console.WriteLine("Render::OnProcessMessageReceived: SourceProcess={0}", sourceProcess);
                Console.WriteLine("Message Name={0} IsValid={1} IsReadOnly={2}", message.Name, message.IsValid, message.IsReadOnly);
                var arguments = message.Arguments;
                for (var i = 0; i < arguments.Count; i++)
                {
                    var type = arguments.GetValueType(i);
                    object value;
                    switch (type)
                    {
                        case CefValueType.Null: value = null; break;
                        case CefValueType.String: value = arguments.GetString(i); break;
                        case CefValueType.Int: value = arguments.GetInt(i); break;
                        case CefValueType.Double: value = arguments.GetDouble(i); break;
                        case CefValueType.Bool: value = arguments.GetBool(i); break;
                        default: value = null; break;
                    }

                    Console.WriteLine("  [{0}] ({1}) = {2}", i, type, value);
                }
            }

            var handled = MessageRouter.OnProcessMessageReceived(browser, sourceProcess, message);
            if (handled) return true;

            if (message.Name == "myMessage2") return true;

            var message2 = CefProcessMessage.Create("myMessage2");
            var success = browser.SendProcessMessage(CefProcessId.Renderer, message2);
            Console.WriteLine("Sending myMessage2 to renderer process = {0}", success);

            var message3 = CefProcessMessage.Create("myMessage3");
            var success2 = browser.SendProcessMessage(CefProcessId.Browser, message3);
            Console.WriteLine("Sending myMessage3 to browser process = {0}", success);

            return false;
        }
    }
}

redpajama_set_name: RedPajamaGithub


text: nodemon ../source/server/app.js


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

  <title>:empty pseudo-class and empty elements</title>

  <link rel="author" title="Ian Hickson" href="mailto:ian@hixie.ch"/>
  <link rel="help" href="https://www.w3.org/TR/css3-selectors/#selectors"/>
  <link rel="match" href="reference/this-line-lime-background-offset.xht"/>
  <meta name="flags" content="" />

 <address></address>

This line should have a green background.


text:
"""
pandas-on-Spark specific features.
"""
import inspect
from typing import Any, Callable, Optional, Tuple, Union, TYPE_CHECKING, cast
from types import FunctionType

import numpy as np  # noqa: F401
import pandas as pd

from pyspark.sql import functions as F
from pyspark.sql.functions import pandas_udf
from pyspark.sql.types import DataType, LongType, StructField, StructType

from pyspark.pandas._typing import DataFrameOrSeries
from pyspark.pandas.internal import (
    InternalField,
    InternalFrame,
    SPARK_INDEX_NAME_FORMAT,
    SPARK_DEFAULT_SERIES_NAME,
)
from pyspark.pandas.typedef import infer_return_type, DataFrameType, ScalarType, SeriesType
from pyspark.pandas.utils import (
    is_name_like_value,
    is_name_like_tuple,
    name_like_string,
    scol_for,
    verify_temp_column_name,
)

if TYPE_CHECKING:
    from pyspark.pandas.frame import DataFrame  # noqa: F401 (SPARK-34943)
    from pyspark.pandas.series import Series  # noqa: F401 (SPARK-34943)
    from pyspark.sql._typing import UserDefinedFunctionLike

class PandasOnSparkFrameMethods(object):
    """pandas-on-Spark specific features for DataFrame."""

    def __init__(self, frame: "DataFrame"):
        self._psdf = frame

    def attach_id_column(self, id_type: str, column: Union[Any, Tuple]) -> "DataFrame":
        """
        Attach a column to be used as identifier of rows similar to the default index.

        See also `Default Index type
        <https://koalas.readthedocs.io/en/latest/user_guide/options.html#default-index-type>`_.

        Parameters
        ----------
        id_type : string
            The id type.

            - 'sequence' : a sequence that increases one by one.

              .. note:: this uses Spark's Window without specifying partition specification.
                  This leads to move all data into single partition in single machine and
                  could cause serious performance degradation.
                  Avoid this method against very large dataset.

            - 'distributed-sequence' : a sequence that increases one by one,
              by group-by and group-map approach in a distributed manner.
            - 'distributed' : a monotonically increasing sequence simply by using PySpark's
              monotonically_increasing_id function in a fully distributed manner.

        column : string or tuple of string
            The column name.

        Returns
        -------
        DataFrame
            The DataFrame attached the column.

        Examples
        --------
        >>> df = ps.DataFrame({"x": ['a', 'b', 'c']})
        >>> df.pandas_on_spark.attach_id_column(id_type="sequence", column="id")
           x  id
        0  a   0
        1  b   1
        2  c   2

        >>> df.pandas_on_spark.attach_id_column(id_type="distributed-sequence", column=0)
           x  0
        0  a  0
        1  b  1
        2  c  2

        >>> df.pandas_on_spark.attach_id_column(id_type="distributed", column=0.0)
        ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
           x  0.0
        0  a  ...
        1  b  ...
        2  c  ...

        For multi-index columns:

        >>> df = ps.DataFrame({("x", "y"): ['a', 'b', 'c']})
        >>> df.pandas_on_spark.attach_id_column(id_type="sequence", column=("id-x", "id-y"))
           x id-x
           y id-y
        0  a    0
        1  b    1
        2  c    2

        >>> df.pandas_on_spark.attach_id_column(id_type="distributed-sequence", column=(0, 1.0))
           x   0
           y 1.0
        0  a   0
        1  b   1
        2  c   2
        """
        from pyspark.pandas.frame import DataFrame

        if id_type == "sequence":
            attach_func = InternalFrame.attach_sequence_column
        elif id_type == "distributed-sequence":
            attach_func = InternalFrame.attach_distributed_sequence_column
        elif id_type == "distributed":
            attach_func = InternalFrame.attach_distributed_column
        else:
            raise ValueError(
                "id_type should be one of 'sequence', 'distributed-sequence' and 'distributed'"
            )

        assert is_name_like_value(column, allow_none=False), column
        if not is_name_like_tuple(column):
            column = (column,)

        internal = self._psdf._internal

        if len(column) != internal.column_labels_level:
            raise ValueError(
                "The given column `{}` must be the same length as the existing columns.".format(
                    column
                )
            )
        elif column in internal.column_labels:
            raise ValueError(
                "The given column `{}` already exists.".format(name_like_string(column))
            )

        # Make sure the underlying Spark column names are the form of
        # `name_like_string(column_label)`.
        sdf = internal.spark_frame.select(
            [
                scol.alias(SPARK_INDEX_NAME_FORMAT(i))
                for i, scol in enumerate(internal.index_spark_columns)
            ]
            + [
                scol.alias(name_like_string(label))
                for scol, label in zip(internal.data_spark_columns, internal.column_labels)
            ]
        )
        sdf, force_nullable = attach_func(sdf, name_like_string(column))

        return DataFrame(
            InternalFrame(
                spark_frame=sdf,
                index_spark_columns=[
                    scol_for(sdf, SPARK_INDEX_NAME_FORMAT(i)) for i in range(internal.index_level)
                ],
                index_names=internal.index_names,
                index_fields=(
                    [field.copy(nullable=True) for field in internal.index_fields]
                    if force_nullable
                    else internal.index_fields
                ),
                column_labels=internal.column_labels + [column],
                data_spark_columns=(
                    [scol_for(sdf, name_like_string(label)) for label in internal.column_labels]
                    + [scol_for(sdf, name_like_string(column))]
                ),
                data_fields=(
                    (
                        [field.copy(nullable=True) for field in internal.data_fields]
                        if force_nullable
                        else internal.data_fields
                    )
                    + [
                        InternalField.from_struct_field(
                            StructField(name_like_string(column), LongType(), nullable=False)
                        )
                    ]
                ),
                column_label_names=internal.column_label_names,
            ).resolved_copy
        )

    def apply_batch(
        self, func: Callable[..., pd.DataFrame], args: Tuple = (), **kwds: Any
    ) -> "DataFrame":
        """
        Apply a function that takes pandas DataFrame and outputs pandas DataFrame. The pandas
        DataFrame given to the function is of a batch used internally.

        See also `Transform and apply a function
        <https://koalas.readthedocs.io/en/latest/user_guide/transform_apply.html>`_.

        .. note:: the `func` is unable to access to the whole input frame. pandas-on-Spark
            internally splits the input series into multiple batches and calls `func` with each
            batch multiple times. Therefore, operations such as global aggregations are impossible.
            See the example below.

            >>> # This case does not return the length of whole frame but of the batch internally
            ... # used.
            ... def length(pdf) -> ps.DataFrame[int]:
            ...     return pd.DataFrame([len(pdf)])
            ...
            >>> df = ps.DataFrame({'A': range(1000)})
            >>> df.pandas_on_spark.apply_batch(length)  # doctest: +SKIP
                c0
            0   83
            1   83
            2   83
            ...
            10  83
            11  83

        .. note:: this API executes the function once to infer the type which is
            potentially expensive, for instance, when the dataset is created after
            aggregations or sorting.

            To avoid this, specify return type in ``func``, for instance, as below:

            >>> def plus_one(x) -> ps.DataFrame[float, float]:
            ...     return x + 1

            If the return type is specified, the output column names become
            `c0, c1, c2 ... cn`. These names are positionally mapped to the returned
            DataFrame in ``func``.

            To specify the column names, you can assign them in a pandas friendly style as below:

            >>> def plus_one(x) -> ps.DataFrame["a": float, "b": float]:
            ...     return x + 1

            >>> pdf = pd.DataFrame({'a': [1, 2, 3], 'b': [3, 4, 5]})
            >>> def plus_one(x) -> ps.DataFrame[zip(pdf.dtypes, pdf.columns)]:
            ...     return x + 1

            When the given function has the return type annotated, the original index of the
            DataFrame will be lost and a default index will be attached to the result DataFrame.
            Please be careful about configuring the default index. See also `Default Index Type
            <https://koalas.readthedocs.io/en/latest/user_guide/options.html#default-index-type>`_.

        Parameters
        ----------
        func : function
            Function to apply to each pandas frame.
        args : tuple
            Positional arguments to pass to `func` in addition to the
            array/series.
        **kwds
            Additional keyword arguments to pass as keywords arguments to
            `func`.

        Returns
        -------
        DataFrame

        See Also
        --------
        DataFrame.apply: For row/columnwise operations.
        DataFrame.applymap: For elementwise operations.
        DataFrame.aggregate: Only perform aggregating type operations.
        DataFrame.transform: Only perform transforming type operations.
        Series.pandas_on_spark.transform_batch: transform the search as each pandas chunks.

        Examples
        --------
        >>> df = ps.DataFrame([(1, 2), (3, 4), (5, 6)], columns=['A', 'B'])
        >>> df
           A  B
        0  1  2
        1  3  4
        2  5  6

        >>> def query_func(pdf) -> ps.DataFrame[int, int]:
        ...     return pdf.query('A == 1')
        >>> df.pandas_on_spark.apply_batch(query_func)
           c0  c1
        0   1   2

        >>> def query_func(pdf) -> ps.DataFrame["A": int, "B": int]:
        ...     return pdf.query('A == 1')
        >>> df.pandas_on_spark.apply_batch(query_func)
           A  B
        0  1  2

        You can also omit the type hints so pandas-on-Spark infers the return schema as below:

        >>> df.pandas_on_spark.apply_batch(lambda pdf: pdf.query('A == 1'))
           A  B
        0  1  2

        You can also specify extra arguments.

        >>> def calculation(pdf, y, z) -> ps.DataFrame[int, int]:
        ...     return pdf ** y + z
        >>> df.pandas_on_spark.apply_batch(calculation, args=(10,), z=20)
                c0        c1
        0       21      1044
        1    59069   1048596
        2  9765645  60466196

        You can also use ``np.ufunc`` and built-in functions as input.

        >>> df.pandas_on_spark.apply_batch(np.add, args=(10,))
            A   B
        0  11  12
        1  13  14
        2  15  16

        >>> (df * -1).pandas_on_spark.apply_batch(abs)
           A  B
        0  1  2
        1  3  4
        2  5  6

        """
        # TODO: codes here partially duplicate `DataFrame.apply`. Can we deduplicate?

        from pyspark.pandas.groupby import GroupBy
        from pyspark.pandas.frame import DataFrame
        from pyspark import pandas as ps

        if not isinstance(func, FunctionType):
            assert callable(func), "the first argument should be a callable function."
            f = func
            func = lambda *args, **kwargs: f(*args, **kwargs)

        spec = inspect.getfullargspec(func)
        return_sig = spec.annotations.get("return", None)
        should_infer_schema = return_sig is None

        original_func = func
        func = lambda o: original_func(o, *args, **kwds)

        self_applied = DataFrame(self._psdf._internal.resolved_copy)  # type: DataFrame

        if should_infer_schema:
            # Here we execute with the first 1000 to get the return type.
            # If the records were less than 1000, it uses pandas API directly for a shortcut.
            limit = ps.get_option("compute.shortcut_limit")
            pdf = self_applied.head(limit + 1)._to_internal_pandas()
            applied = func(pdf)
            if not isinstance(applied, pd.DataFrame):
                raise ValueError(
                    "The given function should return a frame; however, "
                    "the return type was %s." % type(applied)
                )
            psdf = ps.DataFrame(applied)  # type: DataFrame
            if len(pdf) <= limit:
                return psdf

            index_fields = [field.normalize_spark_type() for field in psdf._internal.index_fields]
            data_fields = [field.normalize_spark_type() for field in psdf._internal.data_fields]

            return_schema = StructType([field.struct_field for field in index_fields + data_fields])

            output_func = GroupBy._make_pandas_df_builder_func(
                self_applied, func, return_schema, retain_index=True
            )
            sdf = self_applied._internal.spark_frame.mapInPandas(
                lambda iterator: map(output_func, iterator), schema=return_schema
            )

            # If schema is inferred, we can restore indexes too.
            internal = psdf._internal.with_new_sdf(
                spark_frame=sdf, index_fields=index_fields, data_fields=data_fields
            )
        else:
            return_type = infer_return_type(original_func)
            is_return_dataframe = isinstance(return_type, DataFrameType)
            if not is_return_dataframe:
                raise TypeError(
                    "The given function should specify a frame as its type "
                    "hints; however, the return type was %s." % return_sig
                )
            return_schema = cast(DataFrameType, return_type).spark_type

            output_func = GroupBy._make_pandas_df_builder_func(
                self_applied, func, return_schema, retain_index=False
            )
            sdf = self_applied._internal.to_internal_spark_frame.mapInPandas(
                lambda iterator: map(output_func, iterator), schema=return_schema
            )

            # Otherwise, it loses index.
            internal = InternalFrame(
                spark_frame=sdf,
                index_spark_columns=None,
                data_fields=cast(DataFrameType, return_type).fields,
            )

        return DataFrame(internal)

    def transform_batch(
        self, func: Callable[..., Union[pd.DataFrame, pd.Series]], *args: Any, **kwargs: Any
    ) -> DataFrameOrSeries:
        """
        Transform chunks with a function that takes pandas DataFrame and outputs pandas DataFrame.
        The pandas DataFrame given to the function is of a batch used internally. The length of
        each input and output should be the same.

        See also `Transform and apply a function
        <https://koalas.readthedocs.io/en/latest/user_guide/transform_apply.html>`_.

        .. note:: the `func` is unable to access to the whole input frame. pandas-on-Spark
            internally splits the input series into multiple batches and calls `func` with each
            batch multiple times. Therefore, operations such as global aggregations are impossible.
            See the example below.

            >>> # This case does not return the length of whole frame but of the batch internally
            ... # used.
            ... def length(pdf) -> ps.DataFrame[int]:
            ...     return pd.DataFrame([len(pdf)] * len(pdf))
            ...
            >>> df = ps.DataFrame({'A': range(1000)})
            >>> df.pandas_on_spark.transform_batch(length)  # doctest: +SKIP
                c0
            0   83
            1   83
            2   83
            ...

        .. note:: this API executes the function once to infer the type which is
            potentially expensive, for instance, when the dataset is created after
            aggregations or sorting.

            To avoid this, specify return type in ``func``, for instance, as below:

            >>> def plus_one(x) -> ps.DataFrame[float, float]:
            ...     return x + 1

            If the return type is specified, the output column names become
            `c0, c1, c2 ... cn`. These names are positionally mapped to the returned
            DataFrame in ``func``.

            To specify the column names, you can assign them in a pandas friendly style as below:

            >>> def plus_one(x) -> ps.DataFrame['a': float, 'b': float]:
            ...     return x + 1

            >>> pdf = pd.DataFrame({'a': [1, 2, 3], 'b': [3, 4, 5]})
            >>> def plus_one(x) -> ps.DataFrame[zip(pdf.dtypes, pdf.columns)]:
            ...     return x + 1

            When the given function returns DataFrame and has the return type annotated, the
            original index of the DataFrame will be lost and then a default index will be attached
            to the result. Please be careful about configuring the default index. See also
            `Default Index Type
            <https://koalas.readthedocs.io/en/latest/user_guide/options.html#default-index-type>`_.

        Parameters
        ----------
        func : function
            Function to transform each pandas frame.
        *args
            Positional arguments to pass to func.
        **kwargs
            Keyword arguments to pass to func.

        Returns
        -------
        DataFrame or Series

        See Also
        --------
        DataFrame.pandas_on_spark.apply_batch: For row/columnwise operations.
        Series.pandas_on_spark.transform_batch: transform the search as each pandas chunks.

        Examples
        --------
        >>> df = ps.DataFrame([(1, 2), (3, 4), (5, 6)], columns=['A', 'B'])
        >>> df
           A  B
        0  1  2
        1  3  4
        2  5  6

        >>> def plus_one_func(pdf) -> ps.DataFrame[int, int]:
        ...     return pdf + 1
        >>> df.pandas_on_spark.transform_batch(plus_one_func)
           c0  c1
        0   2   3
        1   4   5
        2   6   7

        >>> def plus_one_func(pdf) -> ps.DataFrame['A': int, 'B': int]:
        ...     return pdf + 1
        >>> df.pandas_on_spark.transform_batch(plus_one_func)
           A  B
        0  2  3
        1  4  5
        2  6  7

        >>> def plus_one_func(pdf) -> ps.Series[int]:
        ...     return pdf.B + 1
        >>> df.pandas_on_spark.transform_batch(plus_one_func)
        0    3
        1    5
        2    7
        dtype: int64

        You can also omit the type hints so pandas-on-Spark infers the return schema as below:

        >>> df.pandas_on_spark.transform_batch(lambda pdf: pdf + 1)
           A  B
        0  2  3
        1  4  5
        2  6  7

        >>> (df * -1).pandas_on_spark.transform_batch(abs)
           A  B
        0  1  2
        1  3  4
        2  5  6

        Note that you should not transform the index. The index information will not change.

        >>> df.pandas_on_spark.transform_batch(lambda pdf: pdf.B + 1)
        0    3
        1    5
        2    7
        Name: B, dtype: int64

        You can also specify extra arguments as below.

        >>> df.pandas_on_spark.transform_batch(lambda pdf, a, b, c: pdf.B + a + b + c, 1, 2, c=3)
        0     8
        1    10
        2    12
        Name: B, dtype: int64
        """
        from pyspark.pandas.groupby import GroupBy
        from pyspark.pandas.frame import DataFrame
        from pyspark.pandas.series import first_series
        from pyspark import pandas as ps

        assert callable(func), "the first argument should be a callable function."
        spec = inspect.getfullargspec(func)
        return_sig = spec.annotations.get("return", None)
        should_infer_schema = return_sig is None
        original_func = func
        func = lambda o: original_func(o, *args, **kwargs)

        def apply_func(pdf: pd.DataFrame) -> pd.DataFrame:
            return func(pdf).to_frame()

        def pandas_series_func(
            f: Callable[[pd.DataFrame], pd.DataFrame], return_type: DataType
        ) -> "UserDefinedFunctionLike":
            ff = f

            @pandas_udf(returnType=return_type)  # type: ignore
            def udf(pdf: pd.DataFrame) -> pd.Series:
                return first_series(ff(pdf))

            return udf

        if should_infer_schema:
            # Here we execute with the first 1000 to get the return type.
            # If the records were less than 1000, it uses pandas API directly for a shortcut.
            limit = ps.get_option("compute.shortcut_limit")
            pdf = self._psdf.head(limit + 1)._to_internal_pandas()
            transformed = func(pdf)
            if not isinstance(transformed, (pd.DataFrame, pd.Series)):
                raise ValueError(
                    "The given function should return a frame; however, "
                    "the return type was %s." % type(transformed)
                )
            if len(transformed) != len(pdf):
                raise ValueError("transform_batch cannot produce aggregated results")
            psdf_or_psser = ps.from_pandas(transformed)

            if isinstance(psdf_or_psser, ps.Series):
                psser = cast(ps.Series, psdf_or_psser)

                field = psser._internal.data_fields[0].normalize_spark_type()

                return_schema = StructType([field.struct_field])
                output_func = GroupBy._make_pandas_df_builder_func(
                    self._psdf, apply_func, return_schema, retain_index=False
                )

                pudf = pandas_series_func(output_func, return_type=field.spark_type)
                columns = self._psdf._internal.spark_columns
                # TODO: Index will be lost in this case.
                internal = self._psdf._internal.copy(
                    column_labels=psser._internal.column_labels,
                    data_spark_columns=[pudf(F.struct(*columns)).alias(field.name)],
                    data_fields=[field],
                    column_label_names=psser._internal.column_label_names,
                )
                return first_series(DataFrame(internal))
            else:
                psdf = cast(DataFrame, psdf_or_psser)
                if len(pdf) <= limit:
                    # only do the short cut when it returns a frame to avoid
                    # operations on different dataframes in case of series.
                    return psdf

                index_fields = [
                    field.normalize_spark_type() for field in psdf._internal.index_fields
                ]
                data_fields = [field.normalize_spark_type() for field in psdf._internal.data_fields]

                return_schema = StructType(
                    [field.struct_field for field in index_fields + data_fields]
                )

                self_applied = DataFrame(self._psdf._internal.resolved_copy)  # type: DataFrame

                output_func = GroupBy._make_pandas_df_builder_func(
                    self_applied, func, return_schema, retain_index=True
                )
                columns = self_applied._internal.spark_columns

                pudf = pandas_udf(output_func, returnType=return_schema)  # type: ignore
                temp_struct_column = verify_temp_column_name(
                    self_applied._internal.spark_frame, "__temp_struct__"
                )
                applied = pudf(F.struct(*columns)).alias(temp_struct_column)
                sdf = self_applied._internal.spark_frame.select(applied)
                sdf = sdf.selectExpr("%s.*" % temp_struct_column)

                return DataFrame(
                    psdf._internal.with_new_sdf(
                        spark_frame=sdf, index_fields=index_fields, data_fields=data_fields
                    )
                )
        else:
            return_type = infer_return_type(original_func)
            is_return_series = isinstance(return_type, SeriesType)
            is_return_dataframe = isinstance(return_type, DataFrameType)
            if not is_return_dataframe and not is_return_series:
                raise TypeError(
                    "The given function should specify a frame or series as its type "
                    "hints; however, the return type was %s." % return_sig
                )
            if is_return_series:
                field = InternalField(
                    dtype=cast(SeriesType, return_type).dtype,
                    struct_field=StructField(
                        name=SPARK_DEFAULT_SERIES_NAME,
                        dataType=cast(SeriesType, return_type).spark_type,
                    ),
                ).normalize_spark_type()

                return_schema = StructType([field.struct_field])
                output_func = GroupBy._make_pandas_df_builder_func(
                    self._psdf, apply_func, return_schema, retain_index=False
                )

                pudf = pandas_series_func(output_func, return_type=field.spark_type)
                columns = self._psdf._internal.spark_columns
                internal = self._psdf._internal.copy(
                    column_labels=[None],
                    data_spark_columns=[pudf(F.struct(*columns)).alias(field.name)],
                    data_fields=[field],
                    column_label_names=None,
                )
                return first_series(DataFrame(internal))
            else:
                data_fields = [
                    field.normalize_spark_type()
                    for field in cast(DataFrameType, return_type).fields
                ]
                return_schema = StructType([field.struct_field for field in data_fields])

                self_applied = DataFrame(self._psdf._internal.resolved_copy)

                output_func = GroupBy._make_pandas_df_builder_func(
                    self_applied, func, return_schema, retain_index=False
                )
                columns = self_applied._internal.spark_columns

                pudf = pandas_udf(output_func, returnType=return_schema)  # type: ignore
                temp_struct_column = verify_temp_column_name(
                    self_applied._internal.spark_frame, "__temp_struct__"
                )
                applied = pudf(F.struct(*columns)).alias(temp_struct_column)
                sdf = self_applied._internal.spark_frame.select(applied)
                sdf = sdf.selectExpr("%s.*" % temp_struct_column)

                internal = InternalFrame(
                    spark_frame=sdf, index_spark_columns=None, data_fields=data_fields
                )
                return DataFrame(internal)

class PandasOnSparkSeriesMethods(object):
    """pandas-on-Spark specific features for Series."""

    def __init__(self, series: "Series"):
        self._psser = series

    def transform_batch(
        self, func: Callable[..., pd.Series], *args: Any, **kwargs: Any
    ) -> "Series":
        """
        Transform the data with the function that takes pandas Series and outputs pandas Series.
        The pandas Series given to the function is of a batch used internally.

        See also `Transform and apply a function
        <https://koalas.readthedocs.io/en/latest/user_guide/transform_apply.html>`_.

        .. note:: the `func` is unable to access to the whole input series. pandas-on-Spark
            internally splits the input series into multiple batches and calls `func` with each
            batch multiple times. Therefore, operations such as global aggregations are impossible.
            See the example below.

            >>> # This case does not return the length of whole frame but of the batch internally
            ... # used.
            ... def length(pser) -> ps.Series[int]:
            ...     return pd.Series([len(pser)] * len(pser))
            ...
            >>> df = ps.DataFrame({'A': range(1000)})
            >>> df.A.pandas_on_spark.transform_batch(length)  # doctest: +SKIP
                c0
            0   83
            1   83
            2   83
            ...

        .. note:: this API executes the function once to infer the type which is
            potentially expensive, for instance, when the dataset is created after
            aggregations or sorting.

            To avoid this, specify return type in ``func``, for instance, as below:

            >>> def plus_one(x) -> ps.Series[int]:
            ...     return x + 1

        Parameters
        ----------
        func : function
            Function to apply to each pandas frame.
        *args
            Positional arguments to pass to func.
        **kwargs
            Keyword arguments to pass to func.

        Returns
        -------
        DataFrame

        See Also
        --------
        DataFrame.pandas_on_spark.apply_batch : Similar but it takes pandas DataFrame as its
            internal batch.

        Examples
        --------
        >>> df = ps.DataFrame([(1, 2), (3, 4), (5, 6)], columns=['A', 'B'])
        >>> df
           A  B
        0  1  2
        1  3  4
        2  5  6

        >>> def plus_one_func(pser) -> ps.Series[np.int64]:
        ...     return pser + 1
        >>> df.A.pandas_on_spark.transform_batch(plus_one_func)
        0    2
        1    4
        2    6
        Name: A, dtype: int64

        You can also omit the type hints so pandas-on-Spark infers the return schema as below:

        >>> df.A.pandas_on_spark.transform_batch(lambda pser: pser + 1)
        0    2
        1    4
        2    6
        Name: A, dtype: int64

        You can also specify extra arguments.

        >>> def plus_one_func(pser, a, b, c=3) -> ps.Series[np.int64]:
        ...     return pser + a + b + c
        >>> df.A.pandas_on_spark.transform_batch(plus_one_func, 1, b=2)
        0     7
        1     9
        2    11
        Name: A, dtype: int64

        You can also use ``np.ufunc`` and built-in functions as input.

        >>> df.A.pandas_on_spark.transform_batch(np.add, 10)
        0    11
        1    13
        2    15
        Name: A, dtype: int64

        >>> (df * -1).A.pandas_on_spark.transform_batch(abs)
        0    1
        1    3
        2    5
        Name: A, dtype: int64
        """
        assert callable(func), "the first argument should be a callable function."

        return_sig = None
        try:
            spec = inspect.getfullargspec(func)
            return_sig = spec.annotations.get("return", None)
        except TypeError:
            # Falls back to schema inference if it fails to get signature.
            pass

        return_type = None
        if return_sig is not None:
            # Extract the signature arguments from this function.
            sig_return = infer_return_type(func)
            if not isinstance(sig_return, SeriesType):
                raise ValueError(
                    "Expected the return type of this function to be of type column,"
                    " but found type {}".format(sig_return)
                )
            return_type = cast(SeriesType, sig_return)

        return self._transform_batch(lambda c: func(c, *args, **kwargs), return_type)

    def _transform_batch(
        self, func: Callable[..., pd.Series], return_type: Optional[Union[SeriesType, ScalarType]]
    ) -> "Series":
        from pyspark.pandas.groupby import GroupBy
        from pyspark.pandas.series import Series, first_series
        from pyspark import pandas as ps

        if not isinstance(func, FunctionType):
            f = func
            func = lambda *args, **kwargs: f(*args, **kwargs)

        if return_type is None:
            # TODO: In this case, it avoids the shortcut for now (but only infers schema)
            #  because it returns a series from a different DataFrame and it has a different
            #  anchor. We should fix this to allow the shortcut or only allow to infer
            #  schema.
            limit = ps.get_option("compute.shortcut_limit")
            pser = self._psser.head(limit + 1)._to_internal_pandas()
            transformed = pser.transform(func)
            psser = Series(transformed)  # type: Series

            field = psser._internal.data_fields[0].normalize_spark_type()
        else:
            spark_return_type = return_type.spark_type
            dtype = return_type.dtype
            field = InternalField(
                dtype=dtype,
                struct_field=StructField(
                    name=self._psser._internal.data_spark_column_names[0],
                    dataType=spark_return_type,
                ),
            )

        psdf = self._psser.to_frame()
        columns = psdf._internal.spark_column_names

        def pandas_concat(*series: pd.Series) -> pd.DataFrame:
            # The input can only be a DataFrame for struct from Spark 3.0.
            # This works around to make the input as a frame. See SPARK-27240
            pdf = pd.concat(series, axis=1)
            pdf.columns = columns
            return pdf

        def apply_func(pdf: pd.DataFrame) -> pd.DataFrame:
            return func(first_series(pdf)).to_frame()

        return_schema = StructType([StructField(SPARK_DEFAULT_SERIES_NAME, field.spark_type)])
        output_func = GroupBy._make_pandas_df_builder_func(
            psdf, apply_func, return_schema, retain_index=False
        )

        @pandas_udf(returnType=field.spark_type)  # type: ignore
        def pudf(*series: pd.Series) -> pd.Series:
            return first_series(output_func(pandas_concat(*series)))

        return self._psser._with_new_scol(
            scol=pudf(*psdf._internal.spark_columns).alias(field.name), field=field
        )

def _test() -> None:
    import os
    import doctest
    import sys
    from pyspark.sql import SparkSession
    import pyspark.pandas.accessors

    os.chdir(os.environ["SPARK_HOME"])

    globs = pyspark.pandas.accessors.__dict__.copy()
    globs["ps"] = pyspark.pandas
    spark = (
        SparkSession.builder.master("local[4]")
        .appName("pyspark.pandas.accessors tests")
        .getOrCreate()
    )
    (failure_count, test_count) = doctest.testmod(
        pyspark.pandas.accessors,
        globs=globs,
        optionflags=doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE,
    )
    spark.stop()
    if failure_count:
        sys.exit(-1)

if __name__ == "__main__":
    _test()


text:
function New-ADDomainJoin {
    <#
.Synopsis
    Perform offline domain join in c#/PowerShell without djoin.exe
.Description
    Perform offline domain join in c#/PowerShell without djoin.exe
    This will create an object in active directory and output the blob needed to join the machine offline

    If needed, you can use the function New-DjoinFile to recreate the file consumed by djoin.exe
    https://github.com/lazywinadmin/PowerShell/tree/master/TOOL-New-DjoinFile
.Parameter machinename
    Specify the machinename
.Parameter domain
    Specify the domain name
.Parameter credential
    Specify credential
.Parameter machineaccountou
    Specify the organizational unit in Active Directory where the machine account should be placed.
.Parameter dcname
    Specify the Domain Controller
.Example
    $MyAccount = 'MyDomain\MyAccount'
    $Passw = ConvertTo-SecureString -String '<SecretPassword>' -AsplainText -Force
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($MyAccount,$Passw)

    $Splatting = @{
        domain = 'mydomain.com'
        machinename = 'testmachine'
        machineaccountou = 'OU=test,DC=mydomain,DC=com'
        dcname = 'dc01.mydomain.com'
    }

    New-ADDomainJoin -Credential $credential @splatting

.Notes
    Version History
        1.0 | 2019/01/08 | Francois-Xavier Cat
            Initial version based on 'WinPENanoDomainJoin' https://gist.github.com/Ryan2065/79838b78643d2311d60cb6147e3b87bf
    Documentations
        Window  7/ Windows 2008 function https://docs.microsoft.com/en-us/windows/desktop/api/lmjoin/nf-lmjoin-netprovisioncomputeraccount
        Windows 8. / 2012 function https://docs.microsoft.com/en-us/windows/desktop/api/lmjoin/nf-lmjoin-netcreateprovisioningpackage
        Offline Domain Join documentation 2008 https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/dd392267(v=ws.10)

    Todo
        Add support for non dc specified
        Add support for non credential specified
.link
    https://github.com/lazywinadmin/PowerShell
#>
    [CmdletBinding()]
    PARAM(
        [Parameter(Mandatory = $true)]
        $machinename,

        [Parameter(Mandatory = $true)]
        $domain,

        [Alias("RunAs")]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty,

        $machineaccountou = $null,

        $dcname
    )
    Try {
        $FunctionName = (Get-Variable -Name MyInvocation -Scope 0 -ValueOnly).MyCommand

        # Detect OS
        $OSVersion = [Environment]::OSVersion
        $OSVersionMajorMinor = "$($OSVersion.version.major).$($OSVersion.version.minor)"
        Write-Verbose -Message "[$FunctionName] OS detected - $OSVersionMajorMinor"
        if ($OSVersionMajorMinor -lt 6.2) { $CodeToUse = 'W2008' }
        else { $CodeToUse = 'W2012' }

        # W2008
        Write-Verbose -Message "[$FunctionName] Declare code for Windows Server 2008/Windows 7"
        $source2008 = @'
    using System;
    using System.Security.Principal;
    using System.Runtime.InteropServices;

    namespace Djoin
    {
        public class Kernel32
        {
            [DllImport("Kernel32.dll", SetLastError = true)]
            public static extern int GetLastError();
            [DllImport("Kernel32.dll", SetLastError = true)]
            public static extern void CloseHandle(IntPtr existingTokenHandle);
        }

        public class Netapi32
        {
            [DllImport("netapi32.dll", EntryPoint = "NetProvisionComputerAccount", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
                public static extern int NetProvisionComputerAccount(
                    string lpDomain,
                    string lpMachineName,
                    string lpMachineAccountOU,
                    string lpDcName,
                    int dwOptions,
                    IntPtr pProvisionBinData,
                    IntPtr pdwProvisionBinDataSize,
                    [MarshalAs(UnmanagedType.LPWStr)]
                    out string pProvisionTextData);
        }

        public class AdvApi32
        {
            [DllImport("advapi32.DLL", SetLastError = true)]
            public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
            [DllImport("advapi32.dll", SetLastError = true)]
            public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, out IntPtr DuplicateTokenHandle);
            public enum LogonTypes
            {
                /// <summary>
                /// This logon type is intended for users who will be interactively using the computer, such as a user being logged on
                /// by a terminal server, remote shell, or similar process.
                /// This logon type has the additional expense of caching logon information for disconnected operations;
                /// therefore, it is inappropriate for some client/server applications,
                /// such as a mail server.
                /// </summary>
                LOGON32_LOGON_INTERACTIVE = 2,

                /// <summary>
                /// This logon type is intended for high performance servers to authenticate plaintext passwords.

                /// The LogonUser function does not cache credentials for this logon type.
                /// </summary>
                LOGON32_LOGON_NETWORK = 3,

                /// <summary>
                /// This logon type is intended for batch servers, where processes may be executing on behalf of a user without
                /// their direct intervention. This type is also for higher performance servers that process many plaintext
                /// authentication attempts at a time, such as mail or Web servers.
                /// The LogonUser function does not cache credentials for this logon type.
                /// </summary>
                LOGON32_LOGON_BATCH = 4,

                /// <summary>
                /// Indicates a service-type logon. The account provided must have the service privilege enabled.
                /// </summary>
                LOGON32_LOGON_SERVICE = 5,

                /// <summary>
                /// This logon type is for GINA DLLs that log on users who will be interactively using the computer.
                /// This logon type can generate a unique audit record that shows when the workstation was unlocked.
                /// </summary>
                LOGON32_LOGON_UNLOCK = 7,

                /// <summary>
                /// This logon type preserves the name and password in the authentication package, which allows the server to make
                /// connections to other network servers while impersonating the client. A server can accept plaintext credentials
                /// from a client, call LogonUser, verify that the user can access the system across the network, and still
                /// communicate with other servers.
                /// NOTE: Windows NT:  This value is not supported.
                /// </summary>
                LOGON32_LOGON_NETWORK_CLEARTEXT = 8,

                /// <summary>
                /// This logon type allows the caller to clone its current token and specify new credentials for outbound connections.
                /// The new logon session has the same local identifier but uses different credentials for other network connections.
                /// NOTE: This logon type is supported only by the LOGON32_PROVIDER_WINNT50 logon provider.
                /// NOTE: Windows NT:  This value is not supported.
                /// </summary>
                LOGON32_LOGON_NEW_CREDENTIALS = 9,
            }

            public enum LogonProvider
            {
                /// <summary>
                /// Use the standard logon provider for the system.
                /// The default security provider is negotiate, unless you pass NULL for the domain name and the user name
                /// is not in UPN format. In this case, the default provider is NTLM.
                /// NOTE: Windows 2000/NT:   The default security provider is NTLM.
                /// </summary>
                LOGON32_PROVIDER_DEFAULT = 0,
                LOGON32_PROVIDER_WINNT35 = 1,
                LOGON32_PROVIDER_WINNT40 = 2,
                LOGON32_PROVIDER_WINNT50 = 3
            }

            public enum SecurityImpersonationLevel : int
            {
                /// <summary>
                /// The server process cannot obtain identification information about the client,
                /// and it cannot impersonate the client. It is defined with no value given, and thus,
                /// by ANSI C rules, defaults to a value of zero.
                /// </summary>
                SecurityAnonymous = 0,

                /// <summary>
                /// The server process can obtain information about the client, such as security identifiers and privileges,
                /// but it cannot impersonate the client. This is useful for servers that export their own objects,
                /// for example, database products that export tables and views.
                /// Using the retrieved client-security information, the server can make access-validation decisions without
                /// being able to use other services that are using the client's security context.
                /// </summary>
                SecurityIdentification = 1,

                /// <summary>
                /// The server process can impersonate the client's security context on its local system.
                /// The server cannot impersonate the client on remote systems.
                /// </summary>
                SecurityImpersonation = 2,

                /// <summary>
                /// The server process can impersonate the client's security context on remote systems.
                /// NOTE: Windows NT:  This impersonation level is not supported.
                /// </summary>
                SecurityDelegation = 3,
            }

            [DllImport("advapi32.DLL")]
            public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //handle to token for logged-on user

            [DllImport("advapi32.DLL")]
            public static extern bool RevertToSelf();

            [DllImport("kernel32.dll")]
            public extern static bool CloseHandle(IntPtr hToken);

        }

        public class DomainJoin
        {
            public static int GetDomainJoin(string username,string password,string Domain,string Machine,string OU,string DC,out string DomainJoinBlob)
            {

                int Result = -1;

                //define the handles
                IntPtr existingTokenHandle = IntPtr.Zero;
                IntPtr duplicateTokenHandle = IntPtr.Zero;

                //split domain and name
                String[] splitUserName = username.Split('\\');
                string userdomain = splitUserName[0];
                username = splitUserName[1];

                try
                {
                    //get a security token
                    Console.WriteLine("Before Calling AdvApi32.LogonUser");

                    bool isOkay = AdvApi32.LogonUser(username, userdomain, password,
                        (int)AdvApi32.LogonTypes.LOGON32_LOGON_NEW_CREDENTIALS,
                        (int)AdvApi32.LogonProvider.LOGON32_PROVIDER_WINNT50,
                        out existingTokenHandle);

                    Console.WriteLine("After Calling AdvApi32.LogonUser");

                    if (!isOkay)
                    {
                        int lastWin32Error = Marshal.GetLastWin32Error();
                        int lastError = Kernel32.GetLastError();
                        throw new Exception("LogonUser Failed: " + lastWin32Error + " - " + lastError);
                    }

                    // copy the token
                    Console.WriteLine("Before Calling AdvApi32.DuplicateToken");

                    isOkay = AdvApi32.DuplicateToken(existingTokenHandle,
                        (int)AdvApi32.SecurityImpersonationLevel.SecurityImpersonation,
                        out duplicateTokenHandle);

                    //Console.WriteLine("After Calling AdvApi32.DuplicateToken");
                    if (!isOkay)
                    {
                        int lastWin32Error = Marshal.GetLastWin32Error();
                        int lastError = Kernel32.GetLastError();
                        Kernel32.CloseHandle(existingTokenHandle);
                        throw new Exception("DuplicateToken Failed: " + lastWin32Error + " - " + lastError);
                    }

                    // create an identity from the token
                    Console.WriteLine("Before Calling AdvApi32.ImpersonateLoggedOnUser(duplicateTokenHandle)");
                    AdvApi32.ImpersonateLoggedOnUser(duplicateTokenHandle);
                    Console.WriteLine("After Calling AdvApi32.ImpersonateLoggedOnUser(duplicateTokenHandle)");

                    String blob = String.Empty;

                    Console.WriteLine("Calling NetProvisionComputerAccount");

                    Result = Netapi32.NetProvisionComputerAccount(Domain,Machine,OU,DC,2,IntPtr.Zero,IntPtr.Zero,out blob);

                    DomainJoinBlob = blob;

                    Console.WriteLine("Domain Blob: {0}", blob);
                    Console.WriteLine("Before Calling RevertToSelf");

                    if(AdvApi32.RevertToSelf())
                    {
                        Console.WriteLine("RevertToSelf Succeeded");
                    }
                    else
                    {
                        Console.WriteLine("RevertToSelf Failed");
                    }
                }
                finally
                {
                    //free all handles
                    if (existingTokenHandle != IntPtr.Zero)
                    {
                        Kernel32.CloseHandle(existingTokenHandle);
                    }
                    if (duplicateTokenHandle != IntPtr.Zero)
                    {
                        Kernel32.CloseHandle(duplicateTokenHandle);
                    }
                }

                return Result;
            }

            static void Main(string[] args)
            {
                Console.WriteLine("MAIN CALLED");
                Console.ReadLine();
            }
        }
    }
'@

        # W2012
        Write-Verbose -Message "[$FunctionName] Declare code for Windows Server 2012/Windows 8"
        $source2012 = @'
using System;
using System.Security.Principal;
using System.Runtime.InteropServices;

namespace Djoin
{
    public class Kernel32
    {
        [DllImport("Kernel32.dll", SetLastError = true)]
        public static extern int GetLastError();
        [DllImport("Kernel32.dll", SetLastError = true)]
        public static extern void CloseHandle(IntPtr existingTokenHandle);

    }

    public class Netapi32
    {

        [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
        public class NetsetupProvisoningParams

        {

            // Version 1 fields

            public uint dwVersion;

            [MarshalAs(UnmanagedType.LPWStr)]

            public string lpDomain;

            [MarshalAs(UnmanagedType.LPWStr)]

            public string lpHostName;

            [MarshalAs(UnmanagedType.LPWStr)]

            public string lpMachineAccountOU;

            [MarshalAs(UnmanagedType.LPWStr)]

            public string lpDcName;

            public uint dwProvisionOptions;

            //[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr, SizeParamIndex=7)]

            //public string[] aCertTemplateNames;

            public IntPtr aCertTemplateNames;  // hack until correct MarshalAs setting is figured out

            public uint cCertTemplateNames;

            //[MarshalAs(UnmanagedType.LPWStr)]

            //public string[] aMachinePolicyNames;

            public IntPtr aMachinePolicyNames;  // hack until correct MarshalAs setting is figured out

            public uint cMachinePolicyNames;

            //[MarshalAs(UnmanagedType.LPWStr)]

            //public string[] aMachinePolicyPaths;

            public IntPtr aMachinePolicyPaths;  // hack until correct MarshalAs setting is figured out

            public uint cMachinePolicyPaths;

            // Version 2 fields

            [MarshalAs(UnmanagedType.LPWStr)]

            public string lpNetbiosName;

            [MarshalAs(UnmanagedType.LPWStr)]

            public string lpSiteName;

            [MarshalAs(UnmanagedType.LPWStr)]

            public string lpPrimaryDNSDomain;

        }

        [DllImport("Netapi32.dll", EntryPoint = "NetCreateProvisioningPackage", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]

        public static extern int NetCreateProvisioningPackage  (

             NetsetupProvisoningParams pProvisioningParams,

             //[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.U1, SizeParamIndex=2)]

             IntPtr ppPackageBinData,

             IntPtr pdwPackageBinDataSize,

             //[MarshalAs(UnmanagedType.LPWStr)]

             //working - IntPtr ppPackageText // should be out - not needed for now
             [MarshalAs(UnmanagedType.LPWStr)]
             out string ppPackageText // should be out - not needed for now
         );

            }

    public class AdvApi32
    {
        [DllImport("advapi32.DLL", SetLastError = true)]
        public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);

        [DllImport("advapi32.dll", SetLastError = true)]
        public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, out IntPtr DuplicateTokenHandle);
        public enum LogonTypes
        {
            /// <summary>
            /// This logon type is intended for users who will be interactively using the computer, such as a user being logged on
            /// by a terminal server, remote shell, or similar process.
            /// This logon type has the additional expense of caching logon information for disconnected operations;
            /// therefore, it is inappropriate for some client/server applications,
            /// such as a mail server.
            /// </summary>
            LOGON32_LOGON_INTERACTIVE = 2,

            /// <summary>
            /// This logon type is intended for high performance servers to authenticate plaintext passwords.

            /// The LogonUser function does not cache credentials for this logon type.
            /// </summary>
            LOGON32_LOGON_NETWORK = 3,

            /// <summary>
            /// This logon type is intended for batch servers, where processes may be executing on behalf of a user without
            /// their direct intervention. This type is also for higher performance servers that process many plaintext
            /// authentication attempts at a time, such as mail or Web servers.
            /// The LogonUser function does not cache credentials for this logon type.
            /// </summary>
            LOGON32_LOGON_BATCH = 4,

            /// <summary>
            /// Indicates a service-type logon. The account provided must have the service privilege enabled.
            /// </summary>
            LOGON32_LOGON_SERVICE = 5,

            /// <summary>
            /// This logon type is for GINA DLLs that log on users who will be interactively using the computer.
            /// This logon type can generate a unique audit record that shows when the workstation was unlocked.
            /// </summary>
            LOGON32_LOGON_UNLOCK = 7,

            /// <summary>
            /// This logon type preserves the name and password in the authentication package, which allows the server to make
            /// connections to other network servers while impersonating the client. A server can accept plaintext credentials
            /// from a client, call LogonUser, verify that the user can access the system across the network, and still
            /// communicate with other servers.
            /// NOTE: Windows NT:  This value is not supported.
            /// </summary>
            LOGON32_LOGON_NETWORK_CLEARTEXT = 8,

            /// <summary>
            /// This logon type allows the caller to clone its current token and specify new credentials for outbound connections.
            /// The new logon session has the same local identifier but uses different credentials for other network connections.
            /// NOTE: This logon type is supported only by the LOGON32_PROVIDER_WINNT50 logon provider.
            /// NOTE: Windows NT:  This value is not supported.
            /// </summary>
            LOGON32_LOGON_NEW_CREDENTIALS = 9,
        }

        public enum LogonProvider
        {
            /// <summary>
            /// Use the standard logon provider for the system.
            /// The default security provider is negotiate, unless you pass NULL for the domain name and the user name
            /// is not in UPN format. In this case, the default provider is NTLM.
            /// NOTE: Windows 2000/NT:   The default security provider is NTLM.
            /// </summary>
            LOGON32_PROVIDER_DEFAULT = 0,
            LOGON32_PROVIDER_WINNT35 = 1,
            LOGON32_PROVIDER_WINNT40 = 2,
            LOGON32_PROVIDER_WINNT50 = 3
        }

        public enum SecurityImpersonationLevel : int
        {
            /// <summary>
            /// The server process cannot obtain identification information about the client,
            /// and it cannot impersonate the client. It is defined with no value given, and thus,
            /// by ANSI C rules, defaults to a value of zero.
            /// </summary>
            SecurityAnonymous = 0,

            /// <summary>
            /// The server process can obtain information about the client, such as security identifiers and privileges,
            /// but it cannot impersonate the client. This is useful for servers that export their own objects,
            /// for example, database products that export tables and views.
            /// Using the retrieved client-security information, the server can make access-validation decisions without
            /// being able to use other services that are using the client's security context.
            /// </summary>
            SecurityIdentification = 1,

            /// <summary>
            /// The server process can impersonate the client's security context on its local system.
            /// The server cannot impersonate the client on remote systems.
            /// </summary>
            SecurityImpersonation = 2,

            /// <summary>
            /// The server process can impersonate the client's security context on remote systems.
            /// NOTE: Windows NT:  This impersonation level is not supported.
            /// </summary>
            SecurityDelegation = 3,
        }

        [DllImport("advapi32.DLL")]
        public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //handle to token for logged-on user

        [DllImport("advapi32.DLL")]
        public static extern bool RevertToSelf();

        [DllImport("kernel32.dll")]
        public extern static bool CloseHandle(IntPtr hToken);

    }

    public class DomainJoin
    {
        public static string GetDomainJoin(String username,String password,String Domain,String Machine, String OU, String DC, out string DomainJoinBlob)
        {
            WindowsIdentity winId = WindowsIdentity.GetCurrent();
            //Console.WriteLine("Current User Identity : {0}", winId.Name);
            //if (winId != null)
            //{
            //    if (string.Compare(winId.Name, username, true) == 0)
            //    {
            //        return null;
            //    }
            //}

            //define the handles
            IntPtr existingTokenHandle = IntPtr.Zero;
            IntPtr duplicateTokenHandle = IntPtr.Zero;

            //split domain and name
            String[] splitUserName = username.Split('\\');
            userdomain = splitUserName[0];
            username = splitUserName[1];

            try
            {
                //get a security token
                Console.WriteLine("Before Calling AdvApi32.LogonUser");

                bool isOkay = AdvApi32.LogonUser(username, userdomain, password,
                    (int)AdvApi32.LogonTypes.LOGON32_LOGON_NEW_CREDENTIALS,
                    (int)AdvApi32.LogonProvider.LOGON32_PROVIDER_WINNT50,
                    out existingTokenHandle);

                Console.WriteLine("After Calling AdvApi32.LogonUser");

                if (!isOkay)
                {
                    int lastWin32Error = Marshal.GetLastWin32Error();
                    int lastError = Kernel32.GetLastError();

                    throw new Exception("LogonUser Failed: " + lastWin32Error + " - " + lastError);
                }

                // copy the token
                Console.WriteLine("Before Calling AdvApi32.DuplicateToken");

                isOkay = AdvApi32.DuplicateToken(existingTokenHandle,
                    (int)AdvApi32.SecurityImpersonationLevel.SecurityImpersonation,
                    out duplicateTokenHandle);

                Console.WriteLine("After Calling AdvApi32.DuplicateToken");
                if (!isOkay)
                {
                    int lastWin32Error = Marshal.GetLastWin32Error();
                    int lastError = Kernel32.GetLastError();
                    Kernel32.CloseHandle(existingTokenHandle);
                    throw new Exception("DuplicateToken Failed: " + lastWin32Error + " - " + lastError);
                }
                // create an identity from the token

                Console.WriteLine("Before Calling AdvApi32.ImpersonateLoggedOnUser(duplicateTokenHandle)");
                AdvApi32.ImpersonateLoggedOnUser(duplicateTokenHandle);
                Console.WriteLine("After Calling AdvApi32.ImpersonateLoggedOnUser(duplicateTokenHandle)");
                Console.WriteLine("After AdvApi32.ImpersonateLoggedOnUser User Identity : {0}", winId.Name);

                Netapi32.NetsetupProvisoningParams provisioningParams = new Netapi32.NetsetupProvisoningParams();
                provisioningParams.dwVersion = 1;
                provisioningParams.lpDomain = domain;
                provisioningParams.lpHostName = machine;
                provisioningParams.dwProvisionOptions = 2; // Reuse https://docs.microsoft.com/en-us/windows/desktop/api/lmjoin/nf-lmjoin-netprovisioncomputeraccount
                provisioningParams.lpMachineAccountOU = OU;
                provisioningParams.lpDcName = DC;

                //IntPtr blob = new IntPtr();
                //StringBuilder blob = new StringBuilder();
                String blob = String.Empty;

                //working - int result = Netapi32.NetCreateProvisioningPackage(provisioningParams, out a, out b, blob);

                int result = Netapi32.NetCreateProvisioningPackage(provisioningParams, IntPtr.Zero, IntPtr.Zero, out blob);
                DomainJoinBlob = blob;

                //string str = Marshal.PtrToStringAuto(blobptr);

                Console.WriteLine("Domain Blob: {0}", blob);
                //Console.WriteLine("Before Calling WindowsIdentity(duplicateTokenHandle)");
                WindowsIdentity newId = new WindowsIdentity(duplicateTokenHandle);

                //Console.WriteLine("After Calling WindowsIdentity(duplicateTokenHandle)");

                //Console.WriteLine("Before Calling newId.Impersonate()");

                WindowsImpersonationContext impersonatedUser = newId.Impersonate();

                //Console.WriteLine("After Calling newId.Impersonate()");
                //Console.WriteLine("After Impersonation User Identity : {0}", winId.Name);

                //return impersonatedUser;

                return blob;
            }
            finally
            {

                //Console.WriteLine("Inside Finally");
                //free all handles
                if (existingTokenHandle != IntPtr.Zero)
                {
                    Kernel32.CloseHandle(existingTokenHandle);
                }
                if (duplicateTokenHandle != IntPtr.Zero)
                {
                    Kernel32.CloseHandle(duplicateTokenHandle);
                }
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("{0}",WinPE_DJoin(username: args[0],password: args[1], machinename:"NetSetup02"));

            Console.ReadLine();
        }
    }
}
'@

        switch ($CodeToUse) {
            'W2008' {
                Write-Verbose -Message "[$FunctionName] Importing code for Windows Server 2008/Windows 7"
                Add-Type -TypeDefinition $Source2008 -Language CSharp
            }
            default {
                Write-Verbose -Message "[$FunctionName] Importing code for Windows Server 2012/Windows 8"
                Add-Type -TypeDefinition $Source2012 -Language CSharp
            }
        }

        Write-Verbose -Message "[$FunctionName] Performing offline domain join...."

        $DomainJoinBlob = ""
        [void]([Djoin.DomainJoin]::GetDomainJoin($($Credential.username), $($Credential.GetNetworkCredential().password), $domain, $machinename, $machineaccountou, $dcname, [ref]$DomainJoinBlob))

        # return the blob
        return $DomainJoinBlob
    }
    catch {
        throw $_
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<net.soulwolf.image.picturelib.view.SquareLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:background="@color/pc_gallery_bg">

    <ImageView
        android:id="@+id/pi_picture_item_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/pd_empty_picture"
        android:layout_centerInParent="true" />

</net.soulwolf.image.picturelib.view.SquareLayout>


text:
Rails.application.routes.draw do

  mount LandingPageEng::Engine => "/lp", as: "lp"
end


text:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Arch.CMessaging.Client.Net.Core.Future;
using Arch.CMessaging.Client.Net.Core.Service;
using Arch.CMessaging.Client.Net.Core.Session;
using Arch.CMessaging.Client.Net.Util;

namespace Arch.CMessaging.Client.Net.Transport.Socket
{

    public abstract class AbstractSocketAcceptor : AbstractIoAcceptor, ISocketAcceptor
    {
        private readonly AsyncSocketProcessor _processor;
        private Int32 _backlog;
        private Int32 _maxConnections;
        private Semaphore _connectionPool;
        private readonly Action<Object> _startAccept;
        private Boolean _disposed;
        private readonly Dictionary<EndPoint, System.Net.Sockets.Socket> _listenSockets = new Dictionary<EndPoint, System.Net.Sockets.Socket>();

        protected AbstractSocketAcceptor()
            : this(1024)
        { }

        protected AbstractSocketAcceptor(Int32 maxConnections)
            : base(new DefaultSocketSessionConfig())
        {
            _maxConnections = maxConnections;
            _processor = new AsyncSocketProcessor(() => ManagedSessions.Values);
            this.SessionDestroyed += OnSessionDestroyed;
            _startAccept = StartAccept0;
            ReuseBuffer = true;
        }

        public new ISocketSessionConfig SessionConfig
        {
            get { return (ISocketSessionConfig)base.SessionConfig; }
        }

        public new IPEndPoint LocalEndPoint
        {
            get { return (IPEndPoint)base.LocalEndPoint; }
        }

        public new IPEndPoint DefaultLocalEndPoint
        {
            get { return (IPEndPoint)base.DefaultLocalEndPoint; }
            set { base.DefaultLocalEndPoint = value; }
        }

        public override ITransportMetadata TransportMetadata
        {
            get { return AsyncSocketSession.Metadata; }
        }

        public Boolean ReuseAddress { get; set; }

        public Int32 Backlog
        {
            get { return _backlog; }
            set
            {
                lock (_bindLock)
                {
                    if (Active)
                        throw new InvalidOperationException("Backlog can't be set while the acceptor is bound.");
                    _backlog = value;
                }
            }
        }

        public Int32 MaxConnections
        {
            get { return _maxConnections; }
            set
            {
                lock (_bindLock)
                {
                    if (Active)
                        throw new InvalidOperationException("MaxConnections can't be set while the acceptor is bound.");
                    _maxConnections = value;
                }
            }
        }

        public Boolean ReuseBuffer { get; set; }

        protected override IEnumerable<EndPoint> BindInternal(IEnumerable<EndPoint> localEndPoints)
        {
            Dictionary<EndPoint, System.Net.Sockets.Socket> newListeners = new Dictionary<EndPoint, System.Net.Sockets.Socket>();
            try
            {
                // Process all the addresses
                foreach (EndPoint localEP in localEndPoints)
                {
                    EndPoint ep = localEP;
                    if (ep == null)
                        ep = new IPEndPoint(IPAddress.Any, 0);
                    System.Net.Sockets.Socket listenSocket = new System.Net.Sockets.Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    listenSocket.Bind(ep);
                    listenSocket.Listen(Backlog);
                    newListeners[listenSocket.LocalEndPoint] = listenSocket;
                }
            }
            catch (Exception)
            {
                // Roll back if failed to bind all addresses
                foreach (System.Net.Sockets.Socket listenSocket in newListeners.Values)
                {
                    try
                    {
                        listenSocket.Close();
                    }
                    catch (Exception ex)
                    {
                        ExceptionMonitor.Instance.ExceptionCaught(ex);
                    }
                }

                throw;
            }

            if (MaxConnections > 0)
                _connectionPool = new Semaphore(MaxConnections, MaxConnections);

            foreach (KeyValuePair<EndPoint, System.Net.Sockets.Socket> pair in newListeners)
            {
                _listenSockets[pair.Key] = pair.Value;
                StartAccept(new ListenerContext(pair.Value));
            }

            _processor.IdleStatusChecker.Start();

            return newListeners.Keys;
        }

        protected override void UnbindInternal(IEnumerable<EndPoint> localEndPoints)
        {
            foreach (EndPoint ep in localEndPoints)
            {
                System.Net.Sockets.Socket listenSocket;
                if (!_listenSockets.TryGetValue(ep, out listenSocket))
                    continue;
                listenSocket.Close();
                _listenSockets.Remove(ep);
            }

            if (_listenSockets.Count == 0)
            {
                _processor.IdleStatusChecker.Stop();

                if (_connectionPool != null)
                {
                    _connectionPool.Close();
                    _connectionPool = null;
                }
            }
        }

        private void StartAccept(ListenerContext listener)
        {
            if (_connectionPool == null)
            {
                BeginAccept(listener);
            }
            else
            {
                System.Threading.Tasks.Task.Factory.StartNew(_startAccept, listener);
            }
        }

        private void StartAccept0(Object state)
        {
            Semaphore pool = _connectionPool;
            if (pool == null)
                // this might happen if has been unbound
                return;
            try
            {
                pool.WaitOne();
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            BeginAccept((ListenerContext)state);
        }

        private void OnSessionDestroyed(Object sender, IoSessionEventArgs e)
        {
            Semaphore pool = _connectionPool;
            if (pool != null)
                pool.Release();
        }
        protected abstract IoSession NewSession(IoProcessor<SocketSession> processor, System.Net.Sockets.Socket socket);
        protected abstract void BeginAccept(ListenerContext listener);
        protected void EndAccept(System.Net.Sockets.Socket socket, ListenerContext listener)
        {
            if (socket != null)
            {
                IoSession session = NewSession(_processor, socket);
                try
                {
                    InitSession<IoFuture>(session, null, null);
                    session.Processor.Add(session);
                }
                catch (Exception ex)
                {
                    ExceptionMonitor.Instance.ExceptionCaught(ex);
                }
            }

            // Accept the next connection request
            StartAccept(listener);
        }

        protected override void Dispose(Boolean disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (_listenSockets.Count > 0)
                    {
                        foreach (System.Net.Sockets.Socket listenSocket in _listenSockets.Values)
                        {
                            ((IDisposable)listenSocket).Dispose();
                        }
                    }
                    if (_connectionPool != null)
                    {
                        ((IDisposable)_connectionPool).Dispose();
                        _connectionPool = null;
                    }
                    _processor.Dispose();
                }
                _disposed = true;
            }
            base.Dispose(disposing);
        }

        protected class ListenerContext
        {
            private readonly System.Net.Sockets.Socket _socket;
            public ListenerContext(System.Net.Sockets.Socket socket)
            {
                _socket = socket;
            }
            public System.Net.Sockets.Socket Socket
            {
                get { return _socket; }
            }
            public Object Tag { get; set; }
        }
    }
}


text:
/* TEMPLATE GENERATED TESTCASE FILE
Filename: CWE36_Absolute_Path_Traversal__char_console_fopen_84_bad.cpp
Label Definition File: CWE36_Absolute_Path_Traversal.label.xml
Template File: sources-sink-84_bad.tmpl.cpp
*/
/*
 * @description
 * CWE: 36 Absolute Path Traversal
 * BadSource: console Read input from the console
 * GoodSource: Full path and file name
 * Sinks: fopen
 *    BadSink : Open the file named in data using fopen()
 * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use
 *
 * */
#ifndef OMITBAD

#include "std_testcase.h"
#include "CWE36_Absolute_Path_Traversal__char_console_fopen_84.h"

#ifdef _WIN32
#define FOPEN fopen
#else
#define FOPEN fopen
#endif

namespace CWE36_Absolute_Path_Traversal__char_console_fopen_84
{
CWE36_Absolute_Path_Traversal__char_console_fopen_84_bad::CWE36_Absolute_Path_Traversal__char_console_fopen_84_bad(char * dataCopy)
{
    data = dataCopy;
    {
        /* Read input from the console */
        size_t dataLen = strlen(data);
        /* if there is room in data, read into it from the console */
        if (FILENAME_MAX-dataLen > 1)
        {
            /* POTENTIAL FLAW: Read data from the console */
            if (fgets(data+dataLen, (int)(FILENAME_MAX-dataLen), stdin) != NULL)
            {
                /* The next few lines remove the carriage return from the string that is
                 * inserted by fgets() */
                dataLen = strlen(data);
                if (dataLen > 0 && data[dataLen-1] == '\n')
                {
                    data[dataLen-1] = '\0';
                }
            }
            else
            {
                printLine("fgets() failed");
                /* Restore NUL terminator if fgets fails */
                data[dataLen] = '\0';
            }
        }
    }
}

CWE36_Absolute_Path_Traversal__char_console_fopen_84_bad::~CWE36_Absolute_Path_Traversal__char_console_fopen_84_bad()
{
    {
        FILE *pFile = NULL;
        /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
        pFile = FOPEN(data, "wb+");
        if (pFile != NULL)
        {
            fclose(pFile);
        }
    }
}
}
#endif /* OMITBAD */


text:
#ifndef _ALTOVA_INCLUDED_AIMXML_ALTOVA_xs_ALTOVA_CanyType
#define _ALTOVA_INCLUDED_AIMXML_ALTOVA_xs_ALTOVA_CanyType

namespace AIMXML
{

namespace xs
{

class CanyType : public TypeBase
{
public:
	AIMXML_EXPORT CanyType(xercesc::DOMNode* const& init);
	AIMXML_EXPORT CanyType(CanyType const& init);
	void operator=(CanyType const& other) { m_node = other.m_node; }
	static altova::meta::ComplexType StaticInfo() { return altova::meta::ComplexType(types + _altova_ti_xs_altova_CanyType); }
	AIMXML_EXPORT void operator=(const string_type& value);
	AIMXML_EXPORT operator string_type();
	AIMXML_EXPORT void SetXsiType();
};

} // namespace xs

}	// namespace AIMXML

#endif // _ALTOVA_INCLUDED_AIMXML_ALTOVA_xs_ALTOVA_CanyType


text:
"""Utility functions available for use by Controllers

``etag_cache``, ``redirect_to``, and ``abort``.
"""
import logging
import warnings

import paste.httpexceptions as httpexceptions

from routes import url_for

import pylons
import pylons.legacy

__all__ = ['abort', 'etag_cache', 'redirect_to']

log = logging.getLogger(__name__)

def etag_cache(key=None):
    """Use the HTTP Entity Tag cache for Browser side caching

    If a "If-None-Match" header is found, and equivilant to ``key``, then
    a ``304`` HTTP message will be returned with the ETag to tell the browser
    that it should use its current cache of the page.

    Otherwise, the ETag header will be added to the response headers.

    Suggested use is within a Controller Action like so:

    .. code-block:: Python

        import pylons

        class YourController(BaseController):
            def index(self):
                etag_cache(key=1)
                return render('/splash.mako')

    .. Note::
        This works because etag_cache will raise an HTTPNotModified
        exception if the ETag recieved matches the key provided.
    """
    if_none_match = pylons.request.environ.get('HTTP_IF_NONE_MATCH', None)
    pylons.response.headers['ETag'] = key
    if str(key) == if_none_match:
        log.debug("ETag match, returning 304 HTTP Not Modified Response")
        raise httpexceptions.HTTPNotModified()
    else:
        log.debug("ETag didn't match, returning response object")
        return pylons.response

def abort(status_code=None, detail="", headers=None, comment=None):
    """Aborts the request immediately by returning an HTTP exception

    In the event that the status_code is a 300 series error, the detail
    attribute will be used as the Location header should one not be specified
    in the headers attribute.
    """
    exc = httpexceptions.get_exception(status_code)(detail, headers, comment)
    log.debug("Aborting request, status: %s, detail: %r, headers: %r, "
              "comment: %r", status_code, detail, headers, comment)
    raise exc

def redirect_to(*args, **kargs):
    """Raises a redirect exception

    Optionally, a _code variable may be passed with the status code of the
    redirect, ie:

    .. code-block:: Python

        redirect_to('home_page', _code=303)

    ``Deprecated``
    A Response object can be passed in as _response which will have the headers
    and cookies extracted from it and added into the redirect issued."""
    response = kargs.pop('_response', None)
    status_code = kargs.pop('_code', 302)
    exc = httpexceptions.get_exception(status_code)
    found = exc(url_for(*args, **kargs))
    log.debug("Generating %s redirect" % status_code)
    if response:
        warnings.warn(pylons.legacy.redirect_response_warning,
                      PendingDeprecationWarning, 2)
        log.debug("Merging provided Response object into redirect")
        if str(response.status_code).startswith('3'):
            found.code = response.status_code
        for c in response.cookies.values():
            found.headers.add('Set-Cookie', c.output(header=''))
    raise found


text:
@interface SSHTTPController ()<UIWebViewDelegate>
@property (nonatomic,strong) SSWebProgressView *progressView;

@property (nonatomic,strong) SSHTTPViewModel *viewModel;
@end

@implementation SSHTTPController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    [self.view addSubview:self.progressView];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_offset(3);
    }];
}

- (void)bindViewModel {
    [super bindViewModel];

    @weakify(self);
    [RACObserve(self.viewModel, linkURL)
     subscribeNext:^(NSString *linkURL) {
         @strongify(self);
         [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:linkURL]]];
     }];

    if (![AFNetworkReachabilityManager sharedManager].isReachable) {
        @weakify(self);
        [self.view viewAddStatusWithType:SSStatusViewTypeNetworkError forTouchCallback:^(SSStatusBaseView *baseView) {
            @strongify(self);
            [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.viewModel.linkURL]]];
        } isDisplayCallback:^BOOL{
            if ([AFNetworkReachabilityManager sharedManager].isReachable) {
                return NO;
            }
            return YES;
        }];
    }
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (!_viewModel.title || _viewModel.title.length == 0) {
        NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        if (title.length > 20) {
            title = [title substringToIndex:20];
        }
        _viewModel.title = title;
    }

    [_progressView webViewFinishLoading];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [_progressView webViewReadyToLoading];
}

- (UIWebView *)webView {
    if (!_webView) {
        _webView = [[UIWebView alloc] init];
        _webView.delegate = self;
        [_webView scalesPageToFit];
        _webView.backgroundColor = [UIColor whiteColor];
    }
    return _webView;
}

- (SSWebProgressView *)progressView {
    if (!_progressView) {
        _progressView = [[SSWebProgressView alloc] init];
        _progressView.fillColor = [SSAppHandler sharedHander].webProgressIndicatorColor;
    }
    return _progressView;
}

@end


text:
package org.typemeta.funcj.codec.jsonnode;

import org.typemeta.funcj.codec.*;
import org.typemeta.funcj.codec.impl.MapCodecs.*;
import org.typemeta.funcj.codec.jsonnode.JsonNodeTypes.Config;
import org.typemeta.funcj.json.model.*;

import java.util.*;

public abstract class JsonNodeMapCodecs {

    public static class MapCodec<K, V> extends AbstractMapCodec<K, V, JsValue, JsValue, Config> {

        public MapCodec(
                Class<Map<K, V>> mapType,
                Codec<K, JsValue, JsValue, Config> keyCodec,
                Codec<V, JsValue, JsValue, Config> valueCodec) {
            super(mapType, keyCodec, valueCodec);
        }

        @Override
        public JsValue encode(CodecCoreEx<JsValue, JsValue, Config> core, Map<K, V> value, JsValue out) {
            final String keyFieldName = core.config().keyFieldName();
            final String valueFieldName = core.config().valueFieldName();

            final List<JsValue> jsvs = new ArrayList<>(value.size());

            value.forEach((k, v) ->
                    jsvs.add(JSAPI.obj(
                            JSAPI.field(keyFieldName, keyCodec.encodeWithCheck(core, k, out)),
                            JSAPI.field(valueFieldName, valueCodec.encodeWithCheck(core, v, out))
                    ))
            );

            return JSAPI.arr(jsvs);
        }

        @Override
        public Map<K, V> decode(CodecCoreEx<JsValue, JsValue, Config> core, JsValue in) {
            final JsArray jsa = in.asArray();

            final String keyFieldName = core.config().keyFieldName();
            final String valueFieldName = core.config().valueFieldName();

            final MapProxy<K, V> mapProxy = getMapProxy(core);

            jsa.forEach(jsElem -> {
                final JsObject jso = jsElem.asObject();
                final K k = keyCodec.decodeWithCheck(core, jso.get(keyFieldName));
                final V v = valueCodec.decodeWithCheck(core, jso.get(valueFieldName));
                mapProxy.put(k, v);
            });

            return mapProxy.construct();
        }
    }

    public static class StringMapCodec<V> extends AbstractStringMapCodec<V, JsValue, JsValue, Config> {

        public StringMapCodec(
                Class<Map<String, V>> type,
                Codec<V, JsValue, JsValue, Config> valueCodec) {
            super(type, valueCodec);
        }

        @Override
        public JsValue encode(CodecCoreEx<JsValue, JsValue, Config> core, Map<String, V> value, JsValue out) {
            final List<JsObject.Field> fields = new ArrayList<>(value.size());

            value.forEach((key, val) -> {
                fields.add(JSAPI.field(key, valueCodec.encodeWithCheck(core, val, out)));
            });

            return JSAPI.obj(fields);
        }

        @Override
        public Map<String, V> decode(CodecCoreEx<JsValue, JsValue, Config> core, JsValue in) {
            final JsObject jso = in.asObject();

            final MapProxy<String, V> mapProxy = getMapProxy(core);

            jso.forEach(field -> {
                mapProxy.put(field.name(), valueCodec.decodeWithCheck(core, field.value()));
            });

            return mapProxy.construct();
        }
    }
}