text:
#region Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
#endregion // Namespaces

namespace XtraCs
{
  #region Lab3_1_StandardFamiliesAndTypes
  /// <summary>
  /// List all loaded standard families and types.
  /// As a result of the issues explained below, we implemented
  /// the LabUtils utility methods FamilyCategory and GetFamilies.
  /// <include file='../doc/labs.xml' path='labs/lab[@name="3-1"]/*' />
  /// </summary>
  [Transaction( TransactionMode.ReadOnly )]
  public class Lab3_1_StandardFamiliesAndTypes : IExternalCommand
  {
    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIApplication app = commandData.Application;
      Document doc = app.ActiveUIDocument.Document;

      List<string> a = new List<string>();
      FilteredElementCollector families;

      #region 3.1.a Retrieve and iterate over all Family objects in document:
      //
      // retrieve all family elements in current document:
      //
      families = new FilteredElementCollector( doc );

      families.OfClass( typeof( Family ) );

      foreach( Family f in families )
      {
        // Get its category name; notice that the Category property is not
        // implemented for the Family class; use FamilyCategory instead;
        // notice that that is also not always implemented; in that case,
        // use the workaround demonstrated below, looking at the contained
        // family symbols' category:

        a.Add( string.Format( "Name={0}; Category={1}; FamilyCategory={2}",
          f.Name,
          ( ( null == f.Category ) ? "?" : f.Category.Name ),
          ( ( null == f.FamilyCategory ) ? "?" : f.FamilyCategory.Name ) ) );
      }
      #endregion // 3.1.a

      string msg = "{0} standard familie{1} are loaded in this model{2}";
      LabUtils.InfoMsg( msg, a );

      // Loop through the collection of families, and now look at
      // the child symbols (types) as well. These symbols can be
      // used to determine the family category.

      foreach( Family f in families )
      {
        string catName;
        bool first = true;

        // Loop all contained symbols (types):

        //foreach( FamilySymbol s in f.Symbols ) // 2014
        foreach( ElementId id in f.GetFamilySymbolIds() ) // 2015
        {
          FamilySymbol s = doc.GetElement( id ) as FamilySymbol;

          // you can determine the family category from its first symbol.

          if( first )
          {
            first = false;

            #region 3.1.b Retrieve category name of first family symbol:
            catName = s.Category.Name;
            #endregion // 3.1.b

            msg = "Family: Name=" + f.Name
              + "; Id=" + f.Id.IntegerValue.ToString()
              + "; Category=" + catName
              + "\r\nContains Types:";
          }
          msg += "\r\n    " + s.Name + "; Id=" + s.Id.IntegerValue.ToString();
        }

        // Show the symbols for this family and allow user to proceed
        // to the next family (OK) or cancel (Cancel)

        msg += "\r\nContinue?";
        if( !LabUtils.QuestionMsg( msg ) )
        {
          break;
        }
      }
      //
      // return all families whose name contains the substring "Round Duct":
      //
      IEnumerable<Family> round_duct_families
        = LabUtils.GetFamilies( doc, "Round Duct", true );

      int n = round_duct_families.Count();

      return Result.Failed;
    }
  }
  #endregion // Lab3_1_StandardFamiliesAndTypes

  #region Lab3_2_LoadStandardFamilies
  /// <summary>
  /// Load an entire family or a specific type from a family.
  /// <include file='../doc/labs.xml' path='labs/lab[@name="3-2"]/*' />
  /// </summary>
  [Transaction( TransactionMode.Manual )]
  public class Lab3_2_LoadStandardFamilies : IExternalCommand
  {
    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIApplication app = commandData.Application;
      Document doc = app.ActiveUIDocument.Document;
      bool rc;

      using( Transaction t = new Transaction( doc ) )
      {
        t.Start("Load Family");

        #region 3.2.a Load an entire RFA family file:

        // Load a whole Family
        //
        // Example for a family WITH TXT file:

        rc = doc.LoadFamily( LabConstants.WholeFamilyFileToLoad1 );
        #endregion // 3.2.a

        if( rc )
        {
          LabUtils.InfoMsg( "Successfully loaded family "
            + LabConstants.WholeFamilyFileToLoad1 + "." );
        }
        else
        {
          LabUtils.ErrorMsg( "ERROR loading family "
            + LabConstants.WholeFamilyFileToLoad1 + "." );
        }

        // Example for a family WITHOUT TXT file:

        rc = doc.LoadFamily( LabConstants.WholeFamilyFileToLoad2 );

        if( rc )
        {
          LabUtils.InfoMsg( "Successfully loaded family "
            + LabConstants.WholeFamilyFileToLoad2 + "." );
        }
        else
        {
          LabUtils.ErrorMsg( "ERROR loading family "
            + LabConstants.WholeFamilyFileToLoad2 + "." );
        }

        #region 3.2.b Load an individual type from a RFA family file:

        // Load only a specific symbol (type):

        rc = doc.LoadFamilySymbol(
          LabConstants.FamilyFileToLoadSingleSymbol,
          LabConstants.SymbolName );
        #endregion // 3.2.b

        if( rc )
        {
          LabUtils.InfoMsg( "Successfully loaded family symbol "
            + LabConstants.FamilyFileToLoadSingleSymbol + " : "
            + LabConstants.SymbolName + "." );
        }
        else
        {
          LabUtils.ErrorMsg( "ERROR loading family symbol "
            + LabConstants.FamilyFileToLoadSingleSymbol + " : "
            + LabConstants.SymbolName + "." );
        }
        t.Commit();
      }
      return Result.Succeeded;
    }
  }
  #endregion // Lab3_2_LoadStandardFamilies

  #region Lab3_3_DetermineInstanceTypeAndFamily
  /// <summary>
  /// For a selected family instance in the model, determine its type and family.
  /// <include file='../doc/labs.xml' path='labs/lab[@name="3-3"]/*' />
  /// </summary>
  [Transaction( TransactionMode.ReadOnly )]
  public class Lab3_3_DetermineInstanceTypeAndFamily : IExternalCommand
  {
    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIApplication app = commandData.Application;
      UIDocument uidoc = app.ActiveUIDocument;
      Document doc = uidoc.Document;

      // retrieve all FamilySymbol objects of "Windows" category:

      BuiltInCategory bic = BuiltInCategory.OST_Windows;
      FilteredElementCollector symbols = LabUtils.GetFamilySymbols( doc, bic );

      List<string> a = new List<string>();

      foreach( FamilySymbol s in symbols )
      {
        Family fam = s.Family;

        a.Add( s.Name
          + ", Id=" + s.Id.IntegerValue.ToString()
          + "; Family name=" + fam.Name
          + ", Family Id=" + fam.Id.IntegerValue.ToString() );
      }
      LabUtils.InfoMsg( "{0} windows family symbol{1} loaded in the model{1}", a );

      // loop through the selection set and check for
      // standard family instances of "Windows" category:

      int iBic = (int) bic;
      string msg, content;
      ICollection<ElementId> ids = uidoc.Selection.GetElementIds();

      foreach( ElementId id in ids )
      {
        Element e = doc.GetElement( id );

        if( e is FamilyInstance
          && null != e.Category
          && e.Category.Id.IntegerValue.Equals( iBic ) )
        {
          msg = "Selected window Id=" + e.Id.IntegerValue.ToString();

          FamilyInstance inst = e as FamilyInstance;

          #region 3.3 Retrieve the type of the family instance, and the family of the type:

          FamilySymbol fs = inst.Symbol;

          Family f = fs.Family;

          #endregion // 3.3

          content = "FamilySymbol = " + fs.Name
            + "; Id=" + fs.Id.IntegerValue.ToString();

          content += "\r\n  Family = " + f.Name
            + "; Id=" + f.Id.IntegerValue.ToString();

          LabUtils.InfoMsg( msg, content );
        }
      }
      return Result.Succeeded;
    }
  }
  #endregion // Lab3_3_DetermineInstanceTypeAndFamily

  #region Lab3_4_ChangeSelectedInstanceType
  /// <summary>
  /// Form-based utility to change the type or symbol of a selected standard family instance.
  /// <include file='../doc/labs.xml' path='labs/lab[@name="3-4"]/*' />
  /// </summary>
  [Transaction( TransactionMode.Manual )]
  public class Lab3_4_ChangeSelectedInstanceType
    : IExternalCommand
  {
    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIApplication app = commandData.Application;
      UIDocument uidoc = app.ActiveUIDocument;
      Document doc = uidoc.Document;

      FamilyInstance inst =
        LabUtils.GetSingleSelectedElementOrPrompt(
          uidoc, typeof( FamilyInstance ) )
          as FamilyInstance;

      if( null == inst )
      {
        LabUtils.ErrorMsg(
          "Selected element is not a "
          + "standard family instance." );

        return Result.Cancelled;
      }

      // determine selected instance category:

      Category instCat = inst.Category;

      Dictionary<string, List<FamilySymbol>>
        mapFamilyToSymbols
          = new Dictionary<string, List<FamilySymbol>>();

      {
        WaitCursor waitCursor = new WaitCursor();

        // Collect all types applicable to this category and sort them into
        // a dictionary mapping the family name to a list of its types.
        //
        // We create a collection of all loaded families for this category
        // and for each one, the list of all loaded types (symbols).
        //
        // There are many ways how to store the matching objects, but we choose
        // whatever is most suitable for the relevant UI. We could use Revit's
        // generic Map class, but it is probably more efficient to use the .NET
        // strongly-typed Dictionary with
        // KEY = Family name (String)
        // VALUE = list of corresponding FamilySymbol objects
        //
        // find all corresponding families and types:

        FilteredElementCollector families
          = new FilteredElementCollector( doc );

        families.OfClass( typeof( Family ) );

        foreach( Family f in families )
        {
          bool categoryMatches = false;

          ISet<ElementId> ids = f.GetFamilySymbolIds(); // 2015

          // we cannot trust f.Category or
          // f.FamilyCategory, so grab the category
          // from first family symbol instead:

          //foreach( FamilySymbol sym in f.Symbols ) // 2014

          foreach( ElementId id in ids ) // 2015
          {
            Element symbol = doc.GetElement( id );

            categoryMatches = symbol.Category.Id.Equals(
              instCat.Id );

            break;
          }

          if( categoryMatches )
          {
            List<FamilySymbol> symbols
              = new List<FamilySymbol>();

            //foreach( FamilySymbol sym in f.Symbols ) // 2014
            foreach( ElementId id in ids ) // 2015
            {
              FamilySymbol symbol = doc.GetElement( id ) as FamilySymbol;

              symbols.Add( symbol );
            }

            mapFamilyToSymbols.Add( f.Name, symbols );
          }
        }
      }

      // display the form allowing the user to select
      // a family and a type, and assign this type
      // to the instance.

      Lab3_4_Form form
        = new Lab3_4_Form( mapFamilyToSymbols );

      if( System.Windows.Forms.DialogResult.OK
        == form.ShowDialog() )
      {
        using( Transaction t = new Transaction( doc ) )
        {
          t.Start( "Change Selected Instance Type" );
          inst.Symbol = form.cmbType.SelectedItem
            as FamilySymbol;
          t.Commit();
        }

        LabUtils.InfoMsg(
          "Successfully changed family : type to "
          + form.cmbFamily.Text + " : "
          + form.cmbType.Text );
      }
      return Result.Succeeded;
    }
  }
  #endregion // Lab3_4_ChangeSelectedInstanceType

  #region Lab3_5_WallAndFloorTypes
  /// <summary>
  /// Access and modify system family type, similarly
  /// to the standard families looked at above:
  ///
  /// List all wall and floor types and
  /// change the type of selected walls and floors.
  /// <include file='../doc/labs.xml' path='labs/lab[@name="3-5"]/*' />
  /// </summary>
  [Transaction( TransactionMode.Manual )]
  public class Lab3_5_WallAndFloorTypes : IExternalCommand
  {
    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIApplication app = commandData.Application;
      UIDocument uidoc = app.ActiveUIDocument;
      Document doc = uidoc.Document;

      // Find all wall types and their system families (or kinds):

      WallType newWallType = null;

      string msg = "All wall types and families in the model:";
      string content = string.Empty;

      FilteredElementCollector wallTypes
        = new FilteredElementCollector( doc )
          .OfClass( typeof( WallType ) );

      foreach( WallType wt in wallTypes )
      {
        content += "\nType=" + wt.Name + " Family=" + wt.Kind.ToString();
        newWallType = wt;
      }

      content += "\n\nStored WallType " + newWallType.Name
        + " (Id=" + newWallType.Id.IntegerValue.ToString()
        + ") for later use.";

      LabUtils.InfoMsg( msg, content );

      // Find all floor types:

      FloorType newFloorType = null;

      msg = "All floor types in the model:";
      content = string.Empty;

      //foreach( FloorType ft in doc.FloorTypes ) // 2014

      FilteredElementCollector floorTypes // 2015
        = new FilteredElementCollector( doc )
          .OfClass( typeof( FloorType ) );

      foreach( FloorType ft in floorTypes ) // 2015
      {
        content += "\nType=" + ft.Name + ", Id=" + ft.Id.IntegerValue.ToString();

        // In 9.0, the "Foundation Slab" system family from "Structural
        // Foundations" category ALSO contains FloorType class instances.
        // Be careful to exclude those as choices for standard floor types.

        Parameter p = ft.get_Parameter( BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM );
        string famName = null == p ? "?" : p.AsString();
        Category cat = ft.Category;
        content += ", Family=" + famName + ", Category=" + cat.Name;

        // store for the new floor type only if it has the proper Floors category:

        if( cat.Id.Equals( (int) BuiltInCategory.OST_Floors ) )
        {
          newFloorType = ft;
        }
      }

      content += ( null == newFloorType )
        ? "\n\nNo floor type found."
        : "\n\nStored FloorType " + newFloorType.Name
        + " (Id=" + newFloorType.Id.IntegerValue.ToString()
        + ") for later use";

      LabUtils.InfoMsg( msg, content );

      // Change the type for selected walls and floors:

      using( Transaction t = new Transaction( doc ) )
      {
        t.Start( "Change Type of Walls and Floors" );

        msg = "{0} {1}: Id={2}"
        + "\r\n  changed from old type={3}; Id={4}"
        + "  to new type={5}; Id={6}.";

        //ElementSet sel = uidoc.Selection.Elements; // 2014
        ICollection<ElementId> ids = uidoc.Selection.GetElementIds(); // 2015

        int iWall = 0;
        int iFloor = 0;

        foreach( ElementId id in ids )
        {
          Element e = doc.GetElement( id );

          if( e is Wall )
          {
            ++iWall;
            Wall wall = e as Wall;
            WallType oldWallType = wall.WallType;

            // change wall type and report the old/new values

            wall.WallType = newWallType;

            LabUtils.InfoMsg( string.Format( msg, "Wall",
              iWall, wall.Id.IntegerValue,
              oldWallType.Name, oldWallType.Id.IntegerValue,
              wall.WallType.Name, wall.WallType.Id.IntegerValue ) );
          }
          else if( null != newFloorType && e is Floor )
          {
            ++iFloor;
            Floor f = e as Floor;
            FloorType oldFloorType = f.FloorType;
            f.FloorType = newFloorType;

            LabUtils.InfoMsg( string.Format( msg, "Floor",
              iFloor, f.Id.IntegerValue,
              oldFloorType.Name, oldFloorType.Id.IntegerValue,
              f.FloorType.Name, f.FloorType.Id.IntegerValue ) );
          }
        }
        t.Commit();
      }
      return Result.Succeeded;
    }
  }
  #endregion // Lab3_5_WallAndFloorTypes

  #region Lab3_6_DuplicateWallType
  /// <summary>
  /// Create a new family symbol or type by calling Duplicate()
  /// on an existing one and then modifying its parameters.
  /// </summary>
  [Transaction( TransactionMode.Manual )]
  public class Lab3_6_DuplicateWallType : IExternalCommand
  {
    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIApplication app = commandData.Application;
      UIDocument uidoc = app.ActiveUIDocument;
      Document doc = uidoc.Document;
      //ElementSet a = uidoc.Selection.Elements; // 2014
      ICollection<ElementId> ids = uidoc.Selection.GetElementIds(); // 2015

      const string newWallTypeName = "NewWallType_with_Width_doubled";

      using( Transaction t = new Transaction( doc ) )
      {
        t.Start( "Duplicate Wall Type" );

        foreach( ElementId id in ids )
        {
          Wall wall = doc.GetElement( id ) as Wall;

          if( null != wall )
          {
            WallType wallType = wall.WallType;

            WallType newWallType = wallType.Duplicate(
              newWallTypeName ) as WallType;

            //CompoundStructureLayerArray layers = newWallType.CompoundStructure.Layers; // 2011
            IList<CompoundStructureLayer> layers = newWallType.GetCompoundStructure().GetLayers(); // 2012

            foreach( CompoundStructureLayer layer in layers )
            {
              // double each layer thickness:

              //layer.Thickness *= 2.0; // 2011

              layer.Width *= 2.0; // 2012
            }
            // assign the new wall type back to the wall:

            wall.WallType = newWallType;

            // only process the first wall, if one was selected:

            break;
          }
        }
        t.Commit();
      }
      return Result.Succeeded;

      #region Assign colour
#if ASSIGN_COLOUR
      ElementSet elemset = doc.Selection.Elements;

      foreach( Element e in elemset )
      {
        FamilyInstance inst = e as FamilyInstance;

        // get the symbol and duplicate it:
        FamilySymbol dupSym = inst.Symbol.Duplicate(
          "D1" ) as FamilySymbol;

        // access the material:
        ElementId matId = dupSym.get_Parameter(
          "Material" ).AsElementId();

        Material mat = doc.GetElement( ref matId )
          as Autodesk.Revit.Elements.Material;

        // change the color of this material:
        mat.Color = new Color( 255, 0, 0 );

        // assign the new symbol to the instance:
        inst.Symbol = dupSym;
#endif // ASSIGN_COLOUR
      #endregion // Assign colour

    }
  }
  #endregion // Lab3_6_DuplicateWallType

  #region Lab3_7_DeleteFamilyType
  /// <summary>
  /// Delete a specific individual type from a family.
  /// Hard-coded to a column type named "475 x 610mm".
  /// </summary>
  [Transaction( TransactionMode.Manual )]
  public class Lab3_7_DeleteFamilyType : IExternalCommand
  {
    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIApplication app = commandData.Application;
      Document doc = app.ActiveUIDocument.Document;

      FilteredElementCollector collector
        = LabUtils.GetFamilySymbols( doc,
          BuiltInCategory.OST_Columns );

      #region Test explicit LINQ statement and cast
#if USE_EXPLICIT_LINQ_STATEMENT
      var column_types = from element in collector
        where element.Name.Equals( "475 x 610mm" )
        select element;

      FamilySymbol symbol = column_types
        .Cast<FamilySymbol>()
        .First<FamilySymbol>();
#endif // USE_EXPLICIT_LINQ_STATEMENT

#if CAST_TO_FAMILY_SYMBOL
      FamilySymbol symbol = collector.First<Element>(
        e => e.Name.Equals( "475 x 610mm" ) )
          as FamilySymbol;
#endif // CAST_TO_FAMILY_SYMBOL
      #endregion // Test explicit LINQ statement and cast

      Element symbol = collector.First<Element>(
        e => e.Name.Equals( "475 x 610mm" ) );

      using( Transaction t = new Transaction( doc ) )
      {
        t.Start( "Delete Family Type" );
        //doc.Delete( symbol ); // 2013
        doc.Delete( symbol.Id ); // 2014
        t.Commit();
      }
      return Result.Succeeded;
    }
  }
  #endregion // Lab3_7_DeleteFamilyType
}


text:
package org.primeframework.mvc.action.result;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.google.inject.Inject;
import io.fusionauth.http.server.HTTPResponse;
import org.primeframework.mvc.PrimeException;
import org.primeframework.mvc.action.ActionInvocation;
import org.primeframework.mvc.action.ActionInvocationStore;
import org.primeframework.mvc.action.config.ActionConfiguration;
import org.primeframework.mvc.action.result.annotation.JSON;
import org.primeframework.mvc.content.json.JacksonActionConfiguration;
import org.primeframework.mvc.content.json.JacksonActionConfiguration.JSONPropertyFilterConfig;
import org.primeframework.mvc.message.ErrorMessage;
import org.primeframework.mvc.message.ErrorMessages;
import org.primeframework.mvc.message.FieldMessage;
import org.primeframework.mvc.message.Message;
import org.primeframework.mvc.message.MessageStore;
import org.primeframework.mvc.message.MessageType;
import org.primeframework.mvc.message.scope.MessageScope;
import org.primeframework.mvc.parameter.el.ExpressionEvaluator;
import org.primeframework.mvc.util.ReflectionUtils;

/**
 * This result writes out Java objects to JSON using Jackson. The content type is set to 'application/json'.
 *
 * @author Brian Pontarelli
 */
public class JSONResult extends AbstractResult<JSON> {
  private final ActionInvocationStore actionInvocationStore;

  private final MessageStore messageStore;

  private final ObjectMapper objectMapper;

  private final HTTPResponse response;

  @Inject
  public JSONResult(ExpressionEvaluator expressionEvaluator, ActionInvocationStore actionInvocationStore,
                    MessageStore messageStore,
                    ObjectMapper objectMapper, HTTPResponse response) {
    super(expressionEvaluator);
    this.messageStore = messageStore;
    this.response = response;
    this.actionInvocationStore = actionInvocationStore;
    this.objectMapper = objectMapper;
  }

  public boolean execute(JSON json) throws IOException {
    ActionInvocation actionInvocation = actionInvocationStore.getCurrent();
    Object action = actionInvocation.action;
    if (action == null) {
      throw new PrimeException("There is no action class and somehow we got into the JSONResult code. Bad mojo!");
    }

    ActionConfiguration configuration = actionInvocation.configuration;
    if (configuration == null) {
      throw new PrimeException("The action [" + action.getClass() + "] has no configuration. This should be impossible!");
    }

    Object jacksonObject;
    boolean prettyPrint = false;
    Class<?> serializationView = void.class;
    JSONPropertyFilterConfig jsonPropertyFilterConfig = null;
    List<Message> errorMessages = messageStore.get(MessageScope.REQUEST).stream()
                                              .filter(m -> m.getType() == MessageType.ERROR)
                                              .collect(Collectors.toList());

    // If there are ERROR messages, put them in a well known container and render that instead of looking for the @JSONResponse annotation
    if (errorMessages.size() > 0) {
      jacksonObject = convertErrors(errorMessages);
    } else {
      JacksonActionConfiguration jacksonActionConfiguration = (JacksonActionConfiguration) configuration.additionalConfiguration.get(JacksonActionConfiguration.class);
      if (jacksonActionConfiguration == null || jacksonActionConfiguration.responseMember == null) {
        throw new PrimeException("The action [" + action.getClass() + "] is missing a field annotated with @JSONResponse. This is used to figure out what to send back in the response.");
      }
      serializationView = jacksonActionConfiguration.getSerializationView();
      jacksonObject = expressionEvaluator.getValue(jacksonActionConfiguration.responseMember.name, action);
      if (jacksonObject == null) {
        throw new PrimeException("The @JSONResponse field [" + jacksonActionConfiguration.responseMember.name + "] in the action [" + action.getClass() + "] is null. It cannot be null!");
      }

      // Allow the JSONResponse annotation to override the contentType.
      prettyPrint = jacksonActionConfiguration.responseMember.annotation.prettyPrint();

      // Capture a jsonFilterConfig method if defined.
      jsonPropertyFilterConfig = jacksonActionConfiguration.jsonPropertyFilterConfig;
    }

    response.setStatus(json.status());
    response.setContentType(json.contentType());

    // Handle setting cache controls
    addCacheControlHeader(json, response);

    if (isHeadRequest(actionInvocation)) {
      return true;
    }

    FilterProvider filterProvider = null;
    ObjectMapper mapper = objectMapper;
    if (jsonPropertyFilterConfig != null) {
      PropertyFilter propertyFilter = ReflectionUtils.invoke(jsonPropertyFilterConfig.method, action);
      filterProvider = new SimpleFilterProvider().addFilter(jsonPropertyFilterConfig.name, propertyFilter);

      // When the mixin is specified on the annotation do not mutate the global objectMapper
      if (jsonPropertyFilterConfig.mixinSource != null && jsonPropertyFilterConfig.mixinTarget != null) {
        mapper = mapper.copy().addMixIn(jsonPropertyFilterConfig.mixinTarget, jsonPropertyFilterConfig.mixinSource);
      }
    }

    writeValue(mapper, response.getOutputStream(), jacksonObject, serializationView, prettyPrint, filterProvider);
    return true;
  }

  @Override
  protected String getCacheControl(JSON result) {
    return result.cacheControl();
  }

  @Override
  protected boolean getDisableCacheControl(JSON result) {
    return result.disableCacheControl();
  }

  private ErrorMessages convertErrors(List<Message> messages) {
    ErrorMessages errorMessages = new ErrorMessages();
    for (Message message : messages) {
      if (message instanceof FieldMessage fieldMessage) {
        errorMessages.addFieldError(fieldMessage.getField(), fieldMessage.getCode(), fieldMessage.toString(), fieldMessage.getData());
      } else {
        errorMessages.generalErrors.add(new ErrorMessage(message.getCode(), message.toString(), message.getData()));
      }
    }

    return errorMessages;
  }

  private ObjectWriter getPrettyWriter(ObjectMapper objectMapper, FilterProvider filterProvider) {
    DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
    prettyPrinter.indentArraysWith(new DefaultIndenter());

    return objectMapper.writerWithDefaultPrettyPrinter()
                       .withFeatures(SerializationFeature.INDENT_OUTPUT)
                       .with(filterProvider)
                       .with(prettyPrinter);
  }

  private void writeValue(ObjectMapper mapper, OutputStream os, Object jacksonObject, Class<?> serializationView,
                          boolean prettyPrint, FilterProvider filterProvider) throws IOException {

    // Most common path
    if (!prettyPrint && serializationView == void.class) {
      if (filterProvider != null) {
        mapper.writer(filterProvider).writeValue(os, jacksonObject);
      } else {
        mapper.writeValue(os, jacksonObject);
      }

      return;
    }

    if (prettyPrint && serializationView != void.class) {
      getPrettyWriter(mapper, filterProvider).with(filterProvider).withView(serializationView).writeValue(os, jacksonObject);
      return;
    }

    if (prettyPrint) {
      getPrettyWriter(mapper, filterProvider).writeValue(os, jacksonObject);
      return;
    }

    // serializationView is always non-null here
    if (filterProvider != null) {
      mapper.writerWithView(serializationView).with(filterProvider).writeValue(os, jacksonObject);
    } else {
      mapper.writerWithView(serializationView).writeValue(os, jacksonObject);
    }
  }
}

redpajama_set_name: RedPajamaGithub


text:
var Urlopownikstart = [];
app.directive("urlopownik", function () {
    return {
        restrict: "E",
        templateUrl: templates_dict['urlopownik'][0],
        transclude: true
    }
});

function urlopownikControler ($scope) {
    $scope.show = [];
    for (var i = 0; i < 3; i++){
        $scope.show[i]= false;
    }

    $scope.showMenu = function (elementtoshow){
        for (var i = 0; i < $scope.show.length; i++){
            $scope.show[i]= false;
        }
        Urlopownikstart[elementtoshow] ();
        $scope.show[elementtoshow]= true;

    }
}

function urlopownikRequestController ($scope, $http) {

    $scope.request = {};
    //lista osob mogacych zastapic
    $scope.instant = [];
    $scope.request.who = $scope.instant[0];
    $scope.request.reason = "reason";
    $scope.from = new Date();
    $scope.to = new Date();
    $scope.sendrequest = false;
    $scope.failedrequest = false;

    Urlopownikstart[0] = function () {
        $scope.sendrequest = false;
        $scope.failedrequest = false;
    };

    $scope.applyForVacation = function () {
        if (($scope.to < $scope.from) || ($scope.from < new Date(new Date().setDate(new Date().getDate()-1)))) {
            showError ("Wrong dates, please change data.");
        } else {
            $scope.request.fromday = $scope.from.getDate().toString();
            $scope.request.frommonth =  ($scope.from.getMonth() + 1).toString();
            $scope.request.fromyear = $scope.from.getFullYear().toString();
            $scope.request.today = $scope.to.getDate().toString();
            $scope.request.tomonth = ($scope.to.getMonth() + 1).toString();
            $scope.request.toyear = $scope.to.getFullYear().toString();
            $http({
                method: 'POST',
                url: '/urlopownik/add/',
                data: angular.toJson($scope.request)
            }).success(function (data) {
                if (data.ok === true){
                        $scope.sendrequest = true;
                        $scope.failedrequest = false;

                    } else {
                        $scope.sendrequest = false;
                        $scope.failedrequest = true;
                    }
                }).error (function () {
                    showError ("Error occured with connection to the server, please load page again.")
            });
        }
    };
}

function urlopownikAcceptController ($scope, $http) {
    $scope.accepting = [];
    $scope.statuses = [];
    $scope.search = {};
    $scope.searchstatus;
    $scope.find = [];
    $scope.statusval = [];

    //uruchamiane do zobaczenia z roznym statusem!!!
    Urlopownikstart[1] = function () {
        $http({
            method: 'GET',
            url: '/urlopownik/acceptstatus/'
        }).success(function (data) {
            $scope.statuses = data.status;
        }).error (function () {
            showError ("Error occured with connection to the server, please load page again.")
        });
    };

    $scope.UrlopownikFind = function () {
        $http({
            method: 'POST',
            url: '/urlopownik/acceptfind/',
            data: angular.toJson({searchstatus: $scope.searchstatus})
        }).success(function (data) {
            $scope.find = [];
            $scope.statusval = [];
            var fromdate;
            var todate;
            for (var i = 0; i < parseInt(data.length); i++) {
                fromdate = new Date(data.fromyear[i], data.frommonth[i], data.fromday[i]);
                todate = new Date(data.toyear[i], data.tomonth[i], data.today[i]);
                $scope.statusval[data.pk[i]] = data.status;
                $scope.find.push({
                    fromdate: fromdate,
                    todate: todate,
                    status: data.status,
                    reason: data.reason[i],
                    user: data.user[i],
                    pk: data.pk[i]
                });
            }
        }).error (function () {
            showError ("Error occured with connection to the server, please load page again.")
        });

    };

    $scope.UrlopownikChange = function (number, it) {
        $http({
            method: 'POST',
            url: '/urlopownik/changestatus/',
            data: angular.toJson({
                status: $scope.statusval [number],
                pk: number
            })
        }).success(function (data) {
                //if ok i czy sie zmienilo
            $scope.find.splice( it, 1 );
        }).error (function () {
            showError ("Error occured with connection to the server, please load page again.")
        });
    };

}

function urlopownikWatchController ($scope, $http) {

    $scope.watching = [];

    Urlopownikstart[2] = function () {
        $http({
            method: 'GET',
            url: '/urlopownik/watch/'
        }).success(function (data) {
            $scope.watching = [];
            var fromdate;
            var todate;
            for (var i = 0; i < parseInt(data.length); i++){
                fromdate = new Date (data.fromyear[i], data.frommonth[i], data.fromday[i]);
                todate = new Date (data.toyear[i], data.tomonth[i], data.today[i]);
                $scope.watching.push({fromdate:fromdate, todate:todate, status:"undefined", reason:data.reason[i]});
            }
        }).error (function () {
            showError ("Error occured with connection to the server, please load page again.")
        });

    };
}


text:
<dom-module id="combo-box-styling-demos">
  <template>

Custom input field style

    <vaadin-demo-snippet id="customizing-border-style" when-defined="vaadin-combo-box">
      <template preserve-content>
        <dom-module id="custom-combo-box-border" theme-for="vaadin-text-field">
          <template>

          </template>
        </dom-module>
        <vaadin-combo-box label="Label" theme="custom-border"></vaadin-combo-box>

      </template>
    </vaadin-demo-snippet>

Custom icon

    <vaadin-demo-snippet id="customizing-icon" when-defined="vaadin-combo-box">
      <template preserve-content>
        <dom-module id="custom-combo-box-icon" theme-for="vaadin-combo-box">
          <template>

          </template>
        </dom-module>
        <vaadin-combo-box label="Label" theme="custom-icon"></vaadin-combo-box>

      </template>
    </vaadin-demo-snippet>

Custom overlay width

    <vaadin-demo-snippet id="overlay-width" when-defined="vaadin-combo-box">
      <template preserve-content>
        <vaadin-combo-box label="Narrow overlay"></vaadin-combo-box>
        <vaadin-combo-box label="Wide overlay"></vaadin-combo-box>

      </template>
    </vaadin-demo-snippet>

Custom overlay style

    <vaadin-demo-snippet id="customizing-theme-variant" when-defined="vaadin-combo-box">
      <template preserve-content>
        <dom-module id="custom-text-field-and-item" theme-for="vaadin-combo-box-item">
          <template>

          </template>
        </dom-module>
        <dom-module id="custom-combo-box-overlay" theme-for="vaadin-combo-box-overlay">
          <template>

          </template>
        </dom-module>
        <vaadin-combo-box label="Element" value="Carbon" theme="custom"></vaadin-combo-box>

      </template>
    </vaadin-demo-snippet>

  </template>

</dom-module>


text:
package com.esri.samples.change_atmosphere_effect;

/**
* Wrapper required for launching a JavaFX 11 app through Gradle or from a jar.
*/
public class ChangeAtmosphereEffectLauncher {

  public static void main(String[] args) {
    ChangeAtmosphereEffectSample.main(args);
  }
}


text:
/**
 * @property {User} user
 * @property {string} socket
 * @property {int} points
 * @property {boolean} isOnline
 */
class Player {
    constructor(user, socket) {
        this.user = user;
        this.socket = socket;
        this.points = 0;
        this.isOnline = true;
    }

    addPoints(points) {
        this.points += points;
    }
}

module.exports = Player;


text:
<resources>
    <string name="app_name">Wearable App</string>
    <string name="tracking_steps">Tracking Running</string>
    <string name="steps">steps</string>
    <string name="tracking_stopped">Tracking stopped</string>
    <string name="tracking_running">Tracking running</string>
</resources>


text:
package com.github.kubatatami.judonetworking.controllers.json.module;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;

import java.io.IOException;

public class BooleanModule extends SimpleModule {

    public BooleanModule() {
        super("boolean-module", new Version(1, 0, 0, "", BooleanModule.class.getPackage().getName(), ""));
        addDeserializer(Boolean.class, new StdBooleanDeserializer());
        addDeserializer(Boolean.TYPE, new StdBooleanDeserializer());
    }

    @Override
    public void setupModule(SetupContext context) {
        super.setupModule(context);
    }

    public static class StdBooleanDeserializer extends JsonDeserializer<Boolean> {

        @Override
        public Boolean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            JsonToken t = jp.getCurrentToken();
            if (t == JsonToken.VALUE_TRUE) {
                return true;
            }
            if (t == JsonToken.VALUE_FALSE) {
                return false;
            }
            if (t == JsonToken.VALUE_NULL) {
                return null;
            }
            if (t == JsonToken.VALUE_NUMBER_INT) {
                return (jp.getIntValue() != 0);
            }
            if (t == JsonToken.VALUE_STRING) {
                String text = jp.getText().trim();
                if ("true".equals(text)) {
                    return true;
                }
                if ("false".equals(text)) {
                    return Boolean.FALSE;
                }

                if ("n".equals(text)) {
                    return Boolean.FALSE;
                }

                if ("y".equals(text)) {
                    return Boolean.TRUE;
                }

                if ("no".equals(text)) {
                    return Boolean.FALSE;
                }

                if ("yes".equals(text)) {
                    return Boolean.TRUE;
                }

                if ("0".equals(text)) {
                    return Boolean.FALSE;
                }

                if ("1".equals(text)) {
                    return Boolean.TRUE;
                }
            }
            throw ctxt.mappingException(Boolean.class);
        }

    }

}


text:
package org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.dispatch;

import org.metaborg.meta.lang.dynsem.interpreter.nodes.DynSemNode;
import org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.PrimaryCachingDispatchNodeGen;
import org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.RuleResult;
import org.metaborg.meta.lang.dynsem.interpreter.utils.SourceUtils;
import org.spoofax.interpreter.core.Tools;
import org.spoofax.interpreter.terms.IStrategoAppl;

import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.source.SourceSection;

public abstract class DispatchNode extends DynSemNode {

	protected final String arrowName;

	public DispatchNode(SourceSection source, String arrowName) {
		super(source);
		this.arrowName = arrowName;
	}

	public abstract RuleResult execute(Class<?> dispatchClass, Object[] args);

	public static DispatchNode create(IStrategoAppl source, IStrategoAppl arrow, FrameDescriptor fd) {
		assert Tools.hasConstructor(arrow, "NamedDynamicEmitted", 3);
		String arrowName = Tools.stringAt(arrow, 1).stringValue();

		assert Tools.hasConstructor(source, "Source", 2);
		return PrimaryCachingDispatchNodeGen.create(SourceUtils.dynsemSourceSectionFromATerm(source), arrowName);
	}

	public static DispatchNode create(SourceSection source, String arrowName) {
		return PrimaryCachingDispatchNodeGen.create(source, arrowName);
	}
}


text:
There are two mailing lists for asking questions. All questions are welcome,
however, your first post might be moderated. This is simply to prevent spam.

1. \#haskell-beginners [on Freenode](irc://irc.freenode.net/#haskell-beginners)
   is an IRC channel that is operated by others who are keen to share ideas
   relating to functional programming in Haskell. They are in various timezones
   and share a passion for functional programming, so may be able to provide
   relatively quick assistance with questions.

### Getting Started

1. Install the Glasgow Haskell Compiler (GHC). Version 7.8 or higher is recommended.

2. Change to the directory containing this document.

3. Execute the command `ghci`, which will compile and load all the source code.
   You may need to set permissions on a file, `chmod 600 .ghci`.

4. Inspect the modules to get a feel for Haskell's syntax, then move on to the
   exercises starting with `Lets.Data.Basics`. The [Progression](#progression)
   section of this document lists the recommended order in which to attempt the
   exercises.

5. Edit a source file to a proposed solution to an exercise. At the `ghci`
   prompt, issue the command `:reload`. This will compile your solution and
   reload it in the GHC interpreter. You may use `:r` for short.

### Progression

It is recommended to perform some exercises before others. The following
progression is recommended:

Day 1:

* `Lets.Go` - ops, basic types, to/from digits, div, mod, min, max ... etc.
* `Lets.Function`
* `Lets.List`
* `Lets.Data`
* `Lets.TouchFeelyFuzzy` - a warm-up exercise for functors, applicatives, etc.
* `Lets.Functor` - functors, applicatives and monads.
* (more) coming soon ...

Day 2:

* coming soon ...

Day 3:

* coming soon ...

Answers to the exercises can be found on the answers branch, here:
[https://github.com/peterson/lets-data/tree/answers](https://github.com/peterson/lets-data/tree/answers)

### Acknowledgements

Thanks to Tony Morris (and co-contributors) for the NICTA course, which provides both
the template and the motivation for this course.

### References

* [CIS 194 Spring '13](https://www.seas.upenn.edu/~cis194/spring13/) - Brent Yorgey's excellent introductory Haskell course, which covers many of the same topics in this tutorial in greater detail.

* [NICTA Course](https://github.com/nicta/course) - A foundational course on functional programming concepts using Haskell.

* [Learn You a Haskell](http://learnyouahaskell.com/) - An approachable textbook covering the basics of Haskell.

* [Real World Haskell](http://http://book.realworldhaskell.org/) - Textbook covering both beginner and advanced topics in Haskell.

* [Glasgow Haskell Compiler](http://haskell.org/ghc)


text:
require "spec_helper"
require "faraday"
require "fileutils"
require "rack/test"

RSpec.describe Gemstash::Web do
  include Rack::Test::Methods

  let(:http_client_builder) do
    #:nodoc:
    class StubHttpBuilder
      def for(server_url, timeout = 20)
        stubs = Faraday::Adapter::Test::Stubs.new do |stub|
          stub.get("/gems/rack") { [200, { "CONTENT-TYPE" => "octet/stream" }, "zapatito"] }
          stub.get("/gems/rack-1.0.0.gem") { [200, { "CONTENT-TYPE" => "octet/stream" }, "zapatito-1.0.0"] }
          stub.get("/gems/rack-1.1.0.gem") { [200, { "CONTENT-TYPE" => "octet/stream" }, "zapatito-1.1.0"] }
          stub.get("/quick/Marshal.4.8/rack.gemspec.rz") { [200, { "CONTENT-TYPE" => "octet/stream" }, "specatito"] }
          stub.get("/quick/Marshal.4.8/rack-1.0.0.gemspec.rz") do
            [200, { "CONTENT-TYPE" => "octet/stream" }, "specatito-1.0.0"]
          end
          stub.get("/quick/Marshal.4.8/rack-1.1.0.gemspec.rz") do
            [200, { "CONTENT-TYPE" => "octet/stream" }, "specatito-1.1.0"]
          end
        end
        client = Faraday.new {|builder| builder.adapter(:test, stubs) }
        Gemstash::HTTPClient.new(client)
      end
    end
    StubHttpBuilder.new
  end
  let(:app) do
    Gemstash::Web.new(http_client_builder: http_client_builder,
                      gemstash_env: test_env)
  end
  let(:upstream) { "https://rubygems.org" }
  let(:gem_source) { Gemstash::GemSource::RubygemsSource }

  let(:rack_env) do
    {
      "gemstash.gem_source" => gem_source,
      "gemstash.upstream" => upstream
    }
  end

  let(:rack) do
    {
      :name => "rack",
      :number => "1.0.0",
      :platform => "ruby",
      :dependencies => []
    }
  end

  context "GET /" do
    let(:request) { "/" }

    it "redirects to rubygems.org" do
      get request, {}, rack_env

      expect(last_response).to redirect_to("https://rubygems.org")
    end
  end

  context "GET /versions" do
    let(:request) { "/versions" }

    context "with https://rubygems.org upstream" do
      it "redirects to https://index.rubygems.org" do
        get request, {}, rack_env

        expect(last_response).to redirect_to("https://index.rubygems.org/versions")
      end
    end

    context "with a non https://rubygems.org upstream" do
      let(:upstream) { "https://private-gem-server.net" }

      it "redirects to the same upstream" do
        get request, {}, rack_env

        expect(last_response).to redirect_to("#{upstream}/versions")
      end
    end
  end

  context "GET /info/*" do
    let(:request) { "/info/some-gem" }

    context "with https://rubygems.org upstream" do
      it "redirects to https://index.rubygems.org" do
        get request, {}, rack_env

        expect(last_response).to redirect_to("https://index.rubygems.org/info/some-gem")
      end
    end

    context "with a non https://rubygems.org upstream" do
      let(:upstream) { "https://private-gem-server.net" }

      it "redirects to the same upstream" do
        get request, {}, rack_env

        expect(last_response).to redirect_to("#{upstream}/info/some-gem")
      end
    end
  end

  context "GET /api/v1/dependencies" do
    let(:request) { "/api/v1/dependencies" }

    context "there are no gems" do
      it "returns an empty string" do
        get request, {}, rack_env

        expect(last_response).to be_ok
        expect(last_response.body).to eq("")
      end
    end

    context "there are gems" do
      before do
        Gemstash::Env.current.cache.set_dependency("upstream/#{upstream}", "rack", [rack])
      end

      it "returns a marshal dump" do
        get "#{request}?gems=rack", {}, rack_env

        expect(last_response).to be_ok
        expect(Marshal.load(last_response.body)).to eq([rack])
      end
    end

    context "there are too many gems" do
      let(:gems) { Array.new(201) {|i| "gem-#{i}" }.join(",") }

      it "returns a 422" do
        get "#{request}?gems=#{gems}", {}, rack_env

        expect(last_response).not_to be_ok
        expect(last_response.body).
          to eq("Too many gems (use --full-index instead)")
      end
    end
  end

  context "GET /api/v1/dependencies.json" do
    let(:request) { "/api/v1/dependencies.json" }

    context "there are no gems" do
      it "returns an empty string" do
        get request, {}, rack_env

        expect(last_response).to be_ok
        expect(last_response.body).to eq("")
      end
    end

    context "there are gems" do
      let(:rack) do
        {
          :name => "rack",
          :number => "1.0.0",
          :platform => "ruby",
          :dependencies => []
        }
      end

      before do
        Gemstash::Env.current.cache.set_dependency("upstream/#{upstream}", "rack", [rack])
      end

      it "returns a marshal dump" do
        result = [{
          "name" => "rack",
          "number" => "1.0.0",
          "platform" => "ruby",
          "dependencies" => []
        }]

        get "#{request}?gems=rack", {}, rack_env

        expect(last_response).to be_ok
        expect(JSON.parse(last_response.body)).to eq(result)
      end
    end

    context "there are too many gems" do
      let(:gems) { Array.new(201) {|i| "gem-#{i}" }.join(",") }

      it "returns a 422" do
        error = {
          "error" => "Too many gems (use --full-index instead)",
          "code" => 422
        }.to_json

        get "#{request}?gems=#{gems}", {}, rack_env

        expect(last_response).not_to be_ok
        expect(last_response.body).to eq(error)
      end
    end
  end

  context "GET /gems/:id" do
    context "from the default upstream" do
      let(:current_env) { Gemstash::Env.current }
      let(:upstream) { Gemstash::Upstream.new(current_env.config[:rubygems_url]) }
      let(:storage) { Gemstash::Storage.for("gem_cache").for(upstream.host_id) }

      it "fetches the gem file, stores, and serves it" do
        get "/gems/rack", {}, rack_env
        expect(last_response.body).to eq("zapatito")
        expect(last_response.header["CONTENT-TYPE"]).to eq("octet/stream")
        expect(storage.resource("rack").exist?(:gem)).to be_truthy
      end

      it "keeps the upstream and full gem name in the properties" do
        get "/gems/rack-1.0.0.gem", {}, rack_env
        properties = storage.resource("rack-1.0.0").properties
        expect(properties[:upstream]).to eq(upstream.to_s)
        expect(properties[:gem_name]).to eq("rack-1.0.0")
      end

      it "keeps headers for specs that have been previously fetched" do
        get "/quick/Marshal.4.8/rack.gemspec.rz", {}, rack_env
        get "/gems/rack", {}, rack_env
        expect(storage.resource("rack").properties[:headers][:spec]).to eq("content-type" => "octet/stream")
        expect(storage.resource("rack").properties[:headers][:gem]).to eq("content-type" => "octet/stream")
      end

      it "indexes the cached gem" do
        get "/gems/rack", {}, rack_env
        db_upstream = Gemstash::DB::Upstream[uri: upstream.to_s]
        expect(db_upstream).to be_an_instance_of(Gemstash::DB::Upstream)
        expect(Gemstash::DB::CachedRubygem[upstream_id: db_upstream.id, name: "rack", resource_type: "gem"]).to be_an_instance_of(Gemstash::DB::CachedRubygem)
      end

      it "indexes specs of different versions separately" do
        get "/gems/rack-1.0.0.gem", {}, rack_env
        get "/gems/rack-1.1.0.gem", {}, rack_env
        db_upstream = Gemstash::DB::Upstream[uri: upstream.to_s]
        expect(db_upstream).to be_an_instance_of(Gemstash::DB::Upstream)
        expect(Gemstash::DB::CachedRubygem.count).to eq(2)
        expect(Gemstash::DB::CachedRubygem[upstream_id: db_upstream.id, name: "rack-1.0.0", resource_type: "gem"]).to be_an_instance_of(Gemstash::DB::CachedRubygem)
        expect(Gemstash::DB::CachedRubygem[upstream_id: db_upstream.id, name: "rack-1.1.0", resource_type: "gem"]).to be_an_instance_of(Gemstash::DB::CachedRubygem)
      end

      it "can be called multiple times without error" do
        get "/gems/rack", {}, rack_env
        get "/gems/rack", {}, rack_env
      end

      it "can be called after the gem has been deleted" do
        get "/gems/rack", {}, rack_env
        storage.resource("rack").delete(:gem)
        expect(storage.resource("rack").exist?(:gem)).to be_falsey
        get "/gems/rack", {}, rack_env
        expect(last_response.body).to eq("zapatito")
        expect(last_response.header["CONTENT-TYPE"]).to eq("octet/stream")
        expect(storage.resource("rack").exist?(:gem)).to be_truthy
      end
    end

    context "from private gems" do
      let(:gem_source) { Gemstash::GemSource::PrivateSource }
      let(:storage) { Gemstash::Storage.for("private").for("gems") }

      context "with a missing gem" do
        it "halts with 404" do
          get "/gems/unknown-0.1.0.gem", {}, rack_env
          expect(last_response).to_not be_ok
          expect(last_response.status).to eq(404)
          expect(last_response.body).to match(/not found/i)
        end
      end

      context "with a regular gem" do
        before do
          gem_id = insert_rubygem "example"
          insert_version gem_id, "0.1.0"
          storage.resource("example-0.1.0").save({ gem: "Example gem content" }, indexed: true)
        end

        it "fetches the gem contents" do
          get "/gems/example-0.1.0.gem", {}, rack_env
          expect(last_response).to be_ok
          expect(last_response.body).to eq("Example gem content")
        end
      end

      context "with a yanked gem" do
        before do
          gem_id = insert_rubygem "yanked"
          insert_version gem_id, "0.1.0", indexed: false
          storage.resource("yanked-0.1.0").save({ gem: "Example yanked gem content" }, indexed: false)
        end

        it "halts with 403" do
          get "/gems/yanked-0.1.0.gem", {}, rack_env
          expect(last_response).to_not be_ok
          expect(last_response.status).to eq(403)
          expect(last_response.body).to_not eq("Example yanked gem content")
        end
      end
    end
  end

  context "GET /quick/Marshal.4.8/:id" do
    context "from the default upstream" do
      let(:current_env) { Gemstash::Env.current }
      let(:upstream) { Gemstash::Upstream.new(current_env.config[:rubygems_url]) }
      let(:storage) { Gemstash::Storage.for("gem_cache").for(upstream.host_id) }

      it "fetches the marshalled gemspec, stores, and serves it" do
        get "/quick/Marshal.4.8/rack.gemspec.rz", {}, rack_env
        expect(last_response.body).to eq("specatito")
        expect(last_response.header["CONTENT-TYPE"]).to eq("octet/stream")
        expect(storage.resource("rack").exist?(:spec)).to be_truthy
      end

      it "keeps the upstream and full gem name in the properties" do
        get "/quick/Marshal.4.8/rack-1.0.0.gemspec.rz", {}, rack_env
        properties = storage.resource("rack-1.0.0").properties
        expect(properties[:upstream]).to eq(upstream.to_s)
        expect(properties[:gem_name]).to eq("rack-1.0.0")
      end

      it "keeps headers for gems that have been previously fetched" do
        get "/gems/rack", {}, rack_env
        get "/quick/Marshal.4.8/rack.gemspec.rz", {}, rack_env
        expect(storage.resource("rack").properties[:headers][:gem]).to eq("content-type" => "octet/stream")
        expect(storage.resource("rack").properties[:headers][:spec]).to eq("content-type" => "octet/stream")
      end

      it "indexes the cached spec" do
        get "/quick/Marshal.4.8/rack.gemspec.rz", {}, rack_env
        db_upstream = Gemstash::DB::Upstream[uri: upstream.to_s]
        expect(db_upstream).to be_an_instance_of(Gemstash::DB::Upstream)
        expect(Gemstash::DB::CachedRubygem[upstream_id: db_upstream.id, name: "rack", resource_type: "spec"]).to be_an_instance_of(Gemstash::DB::CachedRubygem)
      end

      it "indexes specs of different versions separately" do
        get "/quick/Marshal.4.8/rack-1.0.0.gemspec.rz", {}, rack_env
        get "/quick/Marshal.4.8/rack-1.1.0.gemspec.rz", {}, rack_env
        db_upstream = Gemstash::DB::Upstream[uri: upstream.to_s]
        expect(db_upstream).to be_an_instance_of(Gemstash::DB::Upstream)
        expect(Gemstash::DB::CachedRubygem[upstream_id: db_upstream.id, name: "rack-1.0.0", resource_type: "spec"]).to be_an_instance_of(Gemstash::DB::CachedRubygem)
        expect(Gemstash::DB::CachedRubygem[upstream_id: db_upstream.id, name: "rack-1.1.0", resource_type: "spec"]).to be_an_instance_of(Gemstash::DB::CachedRubygem)
      end

      it "can be called multiple times without error" do
        get "/quick/Marshal.4.8/rack.gemspec.rz", {}, rack_env
        expect { get "/quick/Marshal.4.8/rack.gemspec.rz", {}, rack_env }.not_to raise_error
      end

      it "can be called after the spec has been deleted" do
        get "/quick/Marshal.4.8/rack.gemspec.rz", {}, rack_env
        storage.resource("rack").delete(:spec)
        expect(storage.resource("rack").exist?(:spec)).to eq(false)
        get "/quick/Marshal.4.8/rack.gemspec.rz", {}, rack_env
        expect(last_response.body).to eq("specatito")
        expect(last_response.header["CONTENT-TYPE"]).to eq("octet/stream")
        expect(storage.resource("rack").exist?(:spec)).to eq(true)
      end
    end

    context "from private gems" do
      let(:gem_source) { Gemstash::GemSource::PrivateSource }
      let(:storage) { Gemstash::Storage.for("private").for("gems") }

      context "with a missing gem" do
        it "halts with 404" do
          get "/quick/Marshal.4.8/unknown-0.1.0.gemspec.rz", {}, rack_env
          expect(last_response).to_not be_ok
          expect(last_response.status).to eq(404)
          expect(last_response.body).to match(/not found/i)
        end
      end

      context "with a regular gem" do
        before do
          gem_id = insert_rubygem "example"
          insert_version gem_id, "0.1.0"
          storage.resource("example-0.1.0").save({ gem: "Example gem content",
                                                   spec: "Example gemspec content" }, indexed: true)
        end

        it "fetches the spec contents" do
          get "/quick/Marshal.4.8/example-0.1.0.gemspec.rz", {}, rack_env
          expect(last_response).to be_ok
          expect(last_response.body).to eq("Example gemspec content")
        end
      end

      context "with a yanked gem" do
        before do
          gem_id = insert_rubygem "yanked"
          insert_version gem_id, "0.1.0", indexed: false
          storage.resource("yanked-0.1.0").save({ gem: "Example yanked gem content",
                                                  spec: "Example yanked gemspec content" }, indexed: false)
        end

        it "halts with 403" do
          get "/quick/Marshal.4.8/yanked-0.1.0.gemspec.rz", {}, rack_env
          expect(last_response).to_not be_ok
          expect(last_response.status).to eq(403)
          expect(last_response.body).to_not match(/Example yanked gem(spec)? content/)
        end
      end
    end
  end

  context "POST /api/v1/gems" do
    let(:gem_source) { Gemstash::GemSource::PrivateSource }
    let(:auth_key) { "auth-key" }
    let(:env) { rack_env.merge("CONTENT_TYPE" => "application/octet-stream", "HTTP_AUTHORIZATION" => auth_key) }

    before do
      Gemstash::Authorization.authorize(auth_key, "all")
    end

    it "returns 200 on a successful push" do
      post "/api/v1/gems", read_gem("example", "0.1.0"), env
      expect(last_response).to be_ok
      expect(last_response.status).to eq(200)
    end

    it "returns a 422 when gem already exists" do
      post "/api/v1/gems", read_gem("example", "0.1.0"), env
      expect(last_response).to be_ok

      post "/api/v1/gems", read_gem("example", "0.1.0"), env
      expect(last_response).to_not be_ok
      expect(last_response.status).to eq(422)
      expect(JSON.parse(last_response.body)).to eq("error" => "Version already exists", "code" => 422)
    end
  end
end


text:
import csv
import os
import sys
import math
import operator
from time import strftime
import timeit
import cProfile
import numpy as np

data_dir = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '../..', 'data/'))

#opens a file and returns all the rows
def readData(myfile, flagTraining):

	rowsOfData = []
	countInside = 0
	countOutside = 0
	countColon = 0
	myfile = os.path.join(data_dir, myfile)
	with open(myfile, 'r') as myfile:
		reader = csv.reader(myfile, delimiter=',')
		#if flagTraining == True:
		#	reader = subsampleFile(reader)
		for row in reader:
			row = processRow(row)
			rowsOfData.append(row)

	myfile.close()

	return rowsOfData

def subsampleFile(myfile):
	yesses = []
	noes = []
	for verb in myfile:
		label = verb[-2]
		if label == 'label=Y':
			yesses.append(verb)
		elif label == 'label=N':
			noes.append(verb)

	lengthYes = len(yesses)
	fortyfivePervent = round((lengthYes*100)/45) - lengthYes

	newNoesIndices = np.random.choice(len(noes), fortyfivePervent, replace=False)

	newNoes = []
	for index in newNoesIndices:
		newNoes.append(noes[index])

	return yesses+newNoes

#reformats the row so that this program can read it (e.g. list of classes / siblings)
def processRow(row):

	for indx, elem in enumerate(row):
		elem = elem.split('=', 1)[1]
		if indx != 18:
			row[indx] = elem.lower()
		else:
			row[indx] = elem

	verbNetClasses = row[11]
	classes = verbNetClasses.split(';!')
	for indx, oneClass in enumerate(classes):
		oneClass = oneClass.split('-', 1)[0]
		classes[indx] = oneClass
	row[11] = classes

	siblingNodes = row[14]
	siblings = siblingNodes.split(';!')
	row[14] = siblings

	label = row[18].upper()
	metadata = row[19]

	row[12] = int(row[12])
	row[15] = int(row[15])
	row[16] = int(row[16])

	row = row[:19]

	return row, metadata

#distance calculations
def intDistance(int1, int2):
	if int1 == int2:
		return 0.0
	elif int1 + int2 == 0:
		return 0.0
	return abs(float(int2 - int1))/(int2 + int1)

#list distance
def listDistance(list1, list2):
	#uniqueVal = set(list1 + list2)
	#sameVals = list(set(list1).intersection(list2))

	if list1 == list2:
		return 0
	elif (list1 == [''] or list2 == ['']):
		return 1
	else:
		allClasses = list(set(list1 + list2))

		similar = (len(list1) + len(list2)) - len(allClasses)
		return 1.0 - float(similar)/len(allClasses)

#total distance
def calculateDistance(vec1, vec2, fourthMax, normalizeValDepth):

	distanceFINAl = 0

	#if first lemma is not the same, then first word is also not the same
	if (vec1[1] != vec2[1]):
		distanceFINAl += 1
		distanceFINAl += 2
	elif (vec1[0] != vec2[0]):
		distanceFINAl += 1
	if distanceFINAl > fourthMax:
		return 1000

	#first POS similarity
	if (vec1[2] != vec2[2]):
		distanceFINAl += 1

	#second lemma
	if (vec1[4] != vec2[4]):
		distanceFINAl += 1
		distanceFINAl += 2
	elif (vec1[3] != vec2[3]):
		distanceFINAl += 1

	#second POS similarity

	if (vec1[5] != vec2[5]):
		distanceFINAl += 1

	if distanceFINAl > fourthMax:
		return 1000

	#third lemma
	if (vec1[7] != vec2[7]):
		distanceFINAl += 1
		distanceFINAl += 2

	elif (vec1[6] != vec2[6]):
		distanceFINAl += 1

	if distanceFINAl > fourthMax:
		return 1000
	#third POS
	if (vec1[8] != vec2[8]):
		distanceFINAl += 1

	if distanceFINAl > fourthMax:
		return 1000

	if (vec1[9] != vec2[9]):
		distanceFINAl += 1

	if distanceFINAl > fourthMax:
		return 1000

	if (vec1[10] != vec2[10]):
		distanceFINAl += 1

	if distanceFINAl > fourthMax:
		return 1000

	if (vec1[13] != vec2[13]):
		distanceFINAl += 1

	if distanceFINAl > fourthMax:
		return 1000

	if (vec1[17] != vec2[17]):
		distanceFINAl += 10

	if distanceFINAl > fourthMax:
		return 1000

	if vec1[15] + vec1[16] == 0:
		quadrantVec1 = 0
	else:
		quadrantVec1 = float(vec1[15])/(vec1[15] + vec1[16])

	if vec2[15] + vec2[16] == 0:
		quadrantVec2 = 0
	else:
		quadrantVec2 = float(vec2[15])/(vec2[15] + vec2[16])

	quadrantDistance = (abs(quadrantVec1 - quadrantVec2))

	distanceFINAl += quadrantDistance

	if distanceFINAl > fourthMax:
		return 1000

	depthDistNormal = intDistance(vec1[12], vec2[12])/normalizeValDepth

	if depthDistNormal > 1.0:
		distanceFINAl += 1
	else:
		distanceFINAl += intDistance(vec1[12], vec2[12])/normalizeValDepth

	if distanceFINAl > fourthMax:
		return 1000

	distanceFINAl += listDistance(vec1[14], vec2[14])

	if distanceFINAl > fourthMax:
		return 1000

	distanceFINAl += listDistance(vec1[11], vec2[11])

	return distanceFINAl

def column(matrix, i):
    return [row[i] for row in matrix]

#calculate all the distances, create neighbor array
def getNeighbors(trainingSet, test, k):
	distances = []

	topMax = ('', 100)
	secondMax = ('', 100)
	thirdMax = ('', 100)
	fourthMax = ('', 100)

	thisDepth = test[12]
	depthColumn = column(trainingSet, 12)
	depths = sorted(depthColumn)

	maxIndex = int(round(0.95 * len(depths)))
	minIndex = int(round(0.05 * len(depths)))

	maxDepth = depths[maxIndex]
	minDepth = depths[minIndex]

	normalizeValDepth = max(abs(float(maxDepth - thisDepth)/(maxDepth + thisDepth)), abs(float(minDepth - thisDepth)/(minDepth + thisDepth)))

	for indx in range(len(trainingSet)):
		distance = calculateDistance(test, trainingSet[indx], fourthMax[1], normalizeValDepth)
		#print trainingSet[indx]
		if distance == 1000:
			continue
		if distance < topMax[1]:
			fourthMax = thirdMax
			thirdMax = secondMax
			secondMax = topMax
			topMax = ((trainingSet[indx], distance))
		elif distance < secondMax[1]:
			fourthMax = thirdMax
			thirdMax = secondMax
			secondMax = ((trainingSet[indx], distance))
		elif distance < thirdMax[1]:
			fourthMax = thirdMax
			thirdMax = ((trainingSet[indx], distance))
		elif distance < fourthMax[1]:
			fourthMax = ((trainingSet[indx], distance))

	neighbors = []

	neighbors.append(topMax)
	neighbors.append(secondMax)
	neighbors.append(thirdMax)
	neighbors.append(fourthMax)

	return neighbors

#use majority vote to get the response
def getResponse(neighbors):
	classVotes = {}
	for x in range(len(neighbors)):
		response = neighbors[x][0][-1]
		if response in classVotes:
			classVotes[response] += 1
		else:
			classVotes[response] = 1
	sortedVotes = sorted(classVotes.iteritems(), key=operator.itemgetter(1), reverse=True)
	if len(sortedVotes) == 2 and sortedVotes[0][1] == sortedVotes[1][1]:
		return 'N'

	return sortedVotes[0][0]

#calculates accuracy of predictions vs. test set labels
def getAccuracy(testSet, predictions):
	correct = 0
	correctYes = 0
	totalYesPrediction = 0;
	totalYesReal = 0

	for x in range(len(testSet)):
		if predictions[x] == 'Y':
			totalYesPrediction += 1

		if testSet[x][-1] == predictions[x]:
			correct += 1

		if testSet[x][-1] == 'Y':
			totalYesReal += 1
			if predictions[x] == 'Y':
				correctYes += 1

	print 'precision'
	print float(correctYes)/totalYesPrediction
	print 'recall'
	print float(correctYes)/totalYesReal
	print 'accuracy'
	print float(correct)/len(predictions) * 100

	return (correct/float(len(testSet))) * 100.0

#saves the accuracy to a text file
def saveResults(accuracy, length):
	myfile = open(data_dir + '/knnExperiments.txt', 'a')
	myfile.write('On Date: ' + strftime("%Y-%m-%d %H:%M:%S") + ' Ariane trained the verbCue classifier' + \
		' with accuracy: ' + str(accuracy) + ' with a training set of length: ' + str(length) + '\n')
	myfile.close()

#for when the test set comes with labels
def test(myTrainingFile, myTestingFile):
	#data comes in as a list of tuples, [features of verb], metadata)
	trainingData = readData(myTrainingFile, True)
	testData = readData(myTestingFile, False)

	trainingSet, metaData = zip(*trainingData)
	testSet, metaDataTest = zip(*testData)

	print 'length of trainingSet: ' + str(len(trainingSet))
	print ' length of Test Set: ' + str(len(testSet))

	i = 0
	for item in trainingSet:
		if item[-1] == 'Y':
			i = i + 1
	print 'number of verb cues in training set ' + str(i)

	i = 0
	for item in testSet:
		if item[-1] == 'Y':
			i = i + 1
	print 'number of verb cues in test set ' + str(i)

	predictions = []

	k = 4
	print k

	#myfile = open(os.path.join(data_dir, 'TrainKNNVerbPredictions.csv'), 'w').close()
	#myfile = open(os.path.join(data_dir, 'TrainKNNVerbPredictions.csv'), 'a')

	for x in range(len(testSet)):
		word = testData[x][0][1]
		sentID = testData[x][1].split(';')[0]
		tokID = testData[x][1].split(';')[1]
		filename = testData[x][1].split(';')[2]
		neighbors = getNeighbors(trainingSet, testSet[x], k)
		result = getResponse(neighbors)
		predictions.append(result)
		#myfile.write(word + ',' + sentID + ',' + tokID + ',' + filename + ',' + result + '\n')
		#if testSet[x][-1] != result:
		#	print testSet[x]
		#	print neighbors
		if (x == round(len(testSet)/4)):
			print '''25% done'''
		elif (x == round(len(testSet)/10)):
			print '''10% done'''
		elif (x == round(len(testSet)/2)):
			print "50% done"
		elif (x == round(len(testSet)/1.3)):
			print "75% done"
		#print('> ' + str(x) + ' predicted=' + repr(result) + ', actual=' + repr(testSet[x][-1]))
	accuracy = getAccuracy(testSet, predictions)
	print('Accuracy: ' + repr(accuracy) + '%')

	saveResults(accuracy, len(trainingSet))

#takes rows, labels them, and returns them, takes in trainingData to use
def labelWithoutCSV(trainingData, labelRows):

	testData = []

	for row in labelRows:
		row = processRow(row)
		testData.append(row)

	trainingSet, metaData = zip(*trainingData)
	testSet, metaDataTest = zip(*testData)

	#only use first 12,000 rows for speed reasons
	#trainingSet = trainingSet[-6000:]
	#trainingSet = trainingSet[12000:-10000]

	print 'length of trainingSet: ' + str(len(trainingSet))
	print ' length of Test Set: ' + str(len(testSet))

	i = 0
	for item in trainingSet:
		if item[-1] == 'Y':
			i = i + 1
	print 'number of verb cues in training set ' + str(i)

	predictions = []

	k = 4
	print k

	numberVerbCuesFound = 0

	resultArray = []

	for x in range(len(testSet)):
		neighbors = getNeighbors(trainingSet, testSet[x], k)
		result = getResponse(neighbors)

		metadata = metaDataTest[x].split(';')
		sentID =  metadata[0]
		tokID = metadata[1]
		fileName = metadata[2]

		resultArray.append([testSet[x][0], sentID, tokID, fileName, result])

		if result == 'Y':
			numberVerbCuesFound += 1
		if (x == round(len(testSet)/4)):
			print '''25% done'''
		elif (x == round(len(testSet)/10)):
			print '''10% done'''
		elif (x == round(len(testSet)/2)):
			print "50% done"
		elif (x == round(len(testSet)/1.3)):
			print "75% done"

	print 'Number of Verb Cues Found: ' + str(numberVerbCuesFound)
	return resultArray

#label using files, training and labeling file
def label(myTrainingFile, myLabelingFile):
	labelingOutput = myLabelingFile.split('.csv')[-2] + 'Labelled.csv'
	#labelingOutput = labelledFile.split('.csv')[0] + 'Labeled.csv'
	print labelingOutput

	#empty this file
	myfile = open(os.path.join(data_dir, labelingOutput), 'w').close()
	myfile = open(os.path.join(data_dir, labelingOutput), 'a')

	trainingData = readData(myTrainingFile)
	testData = readData(myLabelingFile)

	trainingSet, metaData = zip(*trainingData)
	testSet, metaDataTest = zip(*testData)

	#only use first 12,000 rows for speed reasons
	#trainingSet = trainingSet[:12000]

	print 'length of trainingSet: ' + str(len(trainingSet))
	print ' length of Test Set: ' + str(len(testSet))

	i = 0
	for item in trainingSet:
		if item[-1] == 'Y':
			i = i + 1
	print 'number of verb cues in training set ' + str(i)

	predictions = []

	k = 4
	print k

	numberVerbCuesFound = 0

	for x in range(len(testSet)):
		neighbors = getNeighbors(trainingSet, testSet[x], k)
		result = getResponse(neighbors)
		myfile.write(testSet[x][0] + ',' + metaDataTest[x] + ',' + result + '\n')
		if result == 'Y':
			numberVerbCuesFound += 1
		if (x == round(len(testSet)/4)):
			print '''25% done'''
		elif (x == round(len(testSet)/10)):
			print '''10% done'''
		elif (x == round(len(testSet)/2)):
			print "50% done"
		elif (x == round(len(testSet)/1.3)):
			print "75% done"

	myfile.close()
	print 'Number of Verb Cues Found: ' + str(numberVerbCuesFound)

#parse command line
def main():
	usageMessage = '\nCorrect usage of the kNN classifier command is as follows: \n\n' + \
					'To test a labelled dataset: \n python source/intermediaries/kNN.py -test /pathToTrainingCSVFile /pathToTestCSVFile \n' + \
					'\t If no path is provided, we default to data/PARCTrainVerbFeats.csv and data/PARCdevVerbFeats.csv. This data was generated from the PARC3 training files. \n \n' + \
					'To label a dataset: \n python source/intermediaries/kNN.py -label /pathToTrainingCSVFile /pathToUnlabelledCSVFile \n' + \
					'This will create a csv file with a \'Labeled\' tag appended to the name with the predicted labels. \n' + \
					'\t If no paths are provided, we default to data/PARCTrainVerbFeats.csv and data/PARCdevVerbFeats.csv. This data was generated from the PARC3 test and train files. \n' + \
					'\nProperly formatted data is generated from running XML files through the verbCuesFeatExtractor.py program\n'

	args = sys.argv

	if len(args) == 4:

		pathToTraining = args[2]
		pathToTestCSVFile = args[3]
		print pathToTraining
		print pathToTestCSVFile

		if os.path.isfile(os.path.join(data_dir, pathToTraining)):
			print 'valid path to a file'
		else:
			print 'ERROR: The path to this training file does not exist.'
			return

		if os.path.isfile(os.path.join(data_dir, pathToTestCSVFile)):
			print 'valid path to a file'
		else:
			print 'ERROR: The path to this testing file does not exist.'
			return

		if args[1] == '-test':
			print 'testing'

			pr = cProfile.Profile()
			pr.enable()
			test(pathToTraining, pathToTestCSVFile)
			pr.disable()
			pr.print_stats(sort='time')

		elif args[1] == '-label':
			print 'labelling'
			label(pathToTraining, pathToTestCSVFile)
		else:
			print usageMessage

	elif len(args) == 2:
		if args[1] == '-test':
			print 'training'
			print 'defaulting to the data from data/PARCTrainVerbFeats.csv and data/PARCdevVerbFeats.csv, which was generated from the PARC3 training and dev dataset'
			start = timeit.default_timer()

			pr = cProfile.Profile()
			pr.enable()
			test(data_dir + '/PARCTrainVerbFeats.csv', data_dir + '/PARCTestVerbFeats.csv')
			pr.disable()
			pr.print_stats(sort='time')

			stop = timeit.default_timer()

			print 'time spend: ' + str(stop - start)

		elif args[1] == '-label':
			print 'defaulting to the data from data/PARCTrainVerbFeats.csv and data/PARCdevVerbFeats.csv, which was generated from the PARC3 test dataset'
			print 'labelling'
			label(data_dir + '/PARCTrainVerbFeats.csv', data_dir + '/PARCdevVerbFeats.csv')

		else:
			print usageMessage
	else:
		print usageMessage

if __name__ == '__main__':
   main()


text:
import {Component, OnInit} from "@angular/core";
import {Store} from "@ngrx/store";

import {HeroAction, NavContainer} from "./areas/index";
import {AppState} from "./app.state";

@Component({
	moduleId: module.id,
	selector: "app-heroes",
	templateUrl: "app.html",
	directives: [
		NavContainer
	]
})
export class AppComponent implements OnInit {

	constructor(
		private store: Store<AppState>,
		private heroAction: HeroAction
	) {

	}

	ngOnInit() {
		this.store.dispatch(this.heroAction.fetchAll());
	}
}


text:
package org.axonframework.eventhandling;

import org.axonframework.messaging.annotation.ParameterResolverFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * Implementation of an {@link EventHandlerInvoker} that forwards events to a list of registered {@link EventListener
 * EventListeners}.
 *
 * @author Rene de Waele
 */
public class SimpleEventHandlerInvoker implements EventHandlerInvoker {

    private final List<EventListener> eventListeners;
    private final ListenerInvocationErrorHandler listenerInvocationErrorHandler;

    /**
     * Checks if a List has been passed as first parameter. It is a common 'mistake', which is detected and fixed here.
     *
     * @param eventListeners The event listeners to check for a list
     * @return a list of events listeners
     */
    private static List<?> detectList(Object[] eventListeners) {
        return eventListeners.length == 1 && (eventListeners[0] instanceof List) ? (List<?>) eventListeners[0] :
                Arrays.asList(eventListeners);
    }

    /**
     * Initializes a {@link SimpleEventHandlerInvoker} containing one or more {@code eventListeners}. If an event
     * listener is assignable to {@link EventListener} it will registered as is. If not, it will be wrapped by a new
     * {@link AnnotationEventListenerAdapter}.
     *

     * Events handled by the invoker will be passed to all the given {@code eventListeners}. If an exception is
     * triggered during event handling it will be logged using a {@link LoggingErrorHandler} but otherwise
     * ignored.
     *
     * @param eventListeners one or more event listeners to register with this invoker
     */
    public SimpleEventHandlerInvoker(Object... eventListeners) {
        this(detectList(eventListeners), new LoggingErrorHandler());
    }

    /**
     * Initializes a {@link SimpleEventHandlerInvoker} containing the given list of {@code eventListeners}. If an event
     * listener is assignable to {@link EventListener} it will registered as is. If not, it will be wrapped by a new
     * {@link AnnotationEventListenerAdapter}.
     *

     * Events handled by the invoker will be passed to all the given {@code eventListeners}. If an exception is
     * triggered during event handling it will be handled by the given {@code listenerErrorHandler}.
     *
     * @param eventListeners                 list of event listeners to register with this invoker
     * @param listenerInvocationErrorHandler error handler that handles exceptions during processing
     */
    public SimpleEventHandlerInvoker(List<?> eventListeners, ListenerInvocationErrorHandler listenerInvocationErrorHandler) {
        this.eventListeners = new ArrayList<>(eventListeners.stream()
                                                      .map(listener -> listener instanceof EventListener ?
                                                              (EventListener) listener :
                                                              new AnnotationEventListenerAdapter(listener))
                                                      .collect(toList()));
        this.listenerInvocationErrorHandler = listenerInvocationErrorHandler;
    }

    /**
     * Initializes a {@link SimpleEventHandlerInvoker} containing the given list of {@code eventListeners}. If an event
     * listener is assignable to {@link EventListener} it will registered as is. If not, it will be wrapped by a new
     * {@link AnnotationEventListenerAdapter}.
     *

     * Events handled by the invoker will be passed to all the given {@code eventListeners}. If an exception is
     * triggered during event handling it will be handled by the given {@code listenerErrorHandler}.
     *
     * @param eventListeners                 list of event listeners to register with this invoker
     * @param parameterResolverFactory       The parameter resolver factory to resolve parameters of the Event Handler methods with
     * @param listenerInvocationErrorHandler error handler that handles exceptions during processing
     */
    public SimpleEventHandlerInvoker(List<?> eventListeners,
                                     ParameterResolverFactory parameterResolverFactory,
                                     ListenerInvocationErrorHandler listenerInvocationErrorHandler) {
        this.eventListeners = new ArrayList<>(eventListeners.stream()
                                                      .map(listener -> listener instanceof EventListener ?
                                                              (EventListener) listener :
                                                              new AnnotationEventListenerAdapter(listener, parameterResolverFactory))
                                                      .collect(toList()));
        this.listenerInvocationErrorHandler = listenerInvocationErrorHandler;
    }

    @Override
    public Object handle(EventMessage<?> message) throws Exception {
        for (EventListener listener : eventListeners) {
            try {
                listener.handle(message);
            } catch (Exception e) {
                listenerInvocationErrorHandler.onError(e, message, listener);
            }
        }
        return null;
    }

    @Override
    public boolean hasHandler(EventMessage<?> eventMessage) {
        return true;
    }
}


text:
class RemoveDurationFromPieces < ActiveRecord::Migration
  def change
    remove_column :pieces, :duration
  end
end


text:
require_relative '../lib/asset-handler'

class MainController < Sinatra::Base
  set :views, File.expand_path('../../templates', __FILE__)

  use Rack::Session::Cookie,
    :key => "rack.session",
    :path => "/",
    :secret => "TODO_that is only for test"

  helpers Main::Helpers
  use AssetHandler

end


text:
<?php

namespace DataBreakers\DataApi;

use DataBreakers\UnitTestCase;
use Tester\Assert;

require_once __DIR__ . '/../bootstrap.php';

class TemplateConfigurationTest extends UnitTestCase
{

	const FILTER = 'filter > 1';
	const BOOSTER = 'booster < 2';
	const USER_WEIGHT = 0.4;
	const ITEM_WEIGHT = 0.6;
	const DIVERSITY = 0.8;

	/** @var TemplateConfiguration */
	private $configuration;

	protected function setUp()
	{
		$this->configuration = new TemplateConfiguration(self::FILTER, self::BOOSTER, self::USER_WEIGHT, self::ITEM_WEIGHT,
			self::DIVERSITY);
	}

	public function testGettingFilter()
	{
		Assert::same(self::FILTER, $this->configuration->getFilter());
	}

	public function testSettingFilter()
	{
		$this->configuration->setFilter(NULL);
		Assert::null($this->configuration->getFilter());
		$this->configuration->setFilter(self::FILTER);
		Assert::same(self::FILTER, $this->configuration->getFilter());
	}

	public function testGettingBooster()
	{
		Assert::same(self::BOOSTER, $this->configuration->getBooster());
	}

	public function testSettingBooster()
	{
		$this->configuration->setBooster(NULL);
		Assert::null($this->configuration->getBooster());
		$this->configuration->setBooster(self::BOOSTER);
		Assert::same(self::BOOSTER, $this->configuration->getBooster());
	}

	public function testGettingUserWeight()
	{
		Assert::same(self::USER_WEIGHT, $this->configuration->getUserWeight());
	}

	public function testSettingUserWeight()
	{
		$this->configuration->setUserWeight(NULL);
		Assert::null($this->configuration->getUserWeight());
		$this->configuration->setUserWeight(self::USER_WEIGHT);
		Assert::same(self::USER_WEIGHT, $this->configuration->getUserWeight());
	}

	/**
	 * @throws \DataBreakers\DataApi\Exceptions\InvalidArgumentException
	 */
	public function testThrowingExceptionWhenUserWeightIsNotAFloatNumber()
	{
		$this->configuration->setUserWeight('foo');
	}

	public function testGettingItemWeight()
	{
		Assert::same(self::ITEM_WEIGHT, $this->configuration->getItemWeight());
	}

	public function testSettingItemWeight()
	{
		$this->configuration->setItemWeight(NULL);
		Assert::null($this->configuration->getItemWeight());
		$this->configuration->setItemWeight(self::ITEM_WEIGHT);
		Assert::same(self::ITEM_WEIGHT, $this->configuration->getItemWeight());
	}

	/**
	 * @throws \DataBreakers\DataApi\Exceptions\InvalidArgumentException
	 */
	public function testThrowingExceptionWhenItemWeightIsNotAFloatNumber()
	{
		$this->configuration->setItemWeight('foo');
	}

	public function testGettingDiversity()
	{
		Assert::same(self::DIVERSITY, $this->configuration->getDiversity());
	}

	public function testSettingDiversity()
	{
		$this->configuration->setDiversity(NULL);
		Assert::null($this->configuration->getDiversity());
		$this->configuration->setDiversity(self::DIVERSITY);
		Assert::same(self::DIVERSITY, $this->configuration->getDiversity());
	}

	/**
	 * @throws \DataBreakers\DataApi\Exceptions\InvalidArgumentException
	 */
	public function testThrowingExceptionWhenDiversityIsNotAFloatNumber()
	{
		$this->configuration->setDiversity('foo');
	}

	/**
	 * @throws \DataBreakers\DataApi\Exceptions\InvalidArgumentException
	 */
	public function testThrowingExceptionWhenDiversityIsNegative()
	{
		$this->configuration->setDiversity(-0.5);
	}

}

(new TemplateConfigurationTest())->run();


text:
Common meta tests for PowerShell DSC resources repositories.

## Goals

1. Consistency in encoding and indentations.
Consistency is good by itself. But more importent it allows us to:
2. Avoid big diffs with cosmetic changes in Pull Requests.
Cosmetic changes (like formatting) make reviews harder.
If you want to include formatting changes (like replacing `"` by `'`),
please make it a **separate commit**.
Reviewers would have an option to review meanful changes separately from formatting.

## Git and Unicode

By default, git treat [unicode files as binary files](http://stackoverflow.com/questions/6855712/why-does-git-treat-this-text-file-as-a-binary-file).
You may not notice it, if your client (like VS or GitHub for Windows) takes care of such convertion.
History with Unicode files is hardly usable from command line `git`.

```
> git diff
 diff --git a/xActiveDirectory.psd1 b/xActiveDirectory.psd1
 index 0fc1914..55fdb85 100644
Binary files a/xActiveDirectory.psd1 and b/xActiveDirectory.psd1 differ
```

With forced `--text` option it would look like this:

```
> git diff --text
 diff --git a/xActiveDirectory.psd1 b/xActiveDirectory.psd1
 index 0fc1914..55fdb85 100644
 --- a/xActiveDirectory.psd1
 +++ b/xActiveDirectory.psd1
@@ -30,4 +30,4 @@
   C m d l e t s T o E x p o r t   =   ' * '

   }

 -
 \ No newline at end of file
 + #   h e l l o
 \ No newline at end of file
```

Command line `git` version is a core component and should be used as a common denamenator.

## Fixers

We are trying to provide automatic fixers, where it's appropriate.
A fixer corresponds to a particular test.

For example, if `Files encoding` test from [Meta.Tests.ps1](Meta.Tests.ps1) test fails,
you should be able to run `ConvertTo-UTF8` fixer from [MetaFixers.psm1](MetaFixers.psm1).


text:
module.exports = {
	qr: require('./qr')
};


text:
package mockcalls

import (
	"go.ligato.io/cn-infra/v2/logging"

	l2 "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/l2plugin/model"
	"go.ligato.io/vpp-agent/v3/pkg/idxvpp"
)

// map of bridge domains "configured" in the mock SB.
type mockBDs map[uint32]*l2.BridgeDomain

// MockBDAPI provides methods for managing bridge domains in the mock SB.
type MockBDAPI interface {
	MockBDWrite
	MockBDRead
}

// MockBDWrite provides write methods for bridge domains.
type MockBDWrite interface {
	// CreateBridgeDomain creates new bridge domain in the mock SB.
	CreateBridgeDomain(bdName string) (sbBDHandle uint32, err error)
	// DeleteBridgeDomain removes existing bridge domain from the mock SB.
	DeleteBridgeDomain(sbBDHandle uint32) error
	// AddInterfaceToBridgeDomain puts interface into bridge domain.
	AddInterfaceToBridgeDomain(sbBDHandle uint32, ifaceName string, isBVI bool) error
	// DeleteInterfaceFromBridgeDomain removes interface from bridge domain.
	DeleteInterfaceFromBridgeDomain(sbBDHandle uint32, ifaceName string) error
}

// MockBDRead provides read methods for bridge domains.
type MockBDRead interface {
	// DumpBridgeDomains dumps bridge domains "configured" in the mock SB.
	DumpBridgeDomains() (mockBDs, error)
}

// MockFIBAPI provides methods for managing FIBs.
type MockFIBAPI interface {
	MockFIBWrite
	MockFIBRead
}

// MockFIBWrite provides write methods for FIBs.
type MockFIBWrite interface {
	// CreateL2FIB creates L2 FIB table entry in the mock SB.
	CreateL2FIB(fib *l2.FIBEntry) error
	// DeleteL2FIB removes existing L2 FIB table entry from the mock SB.
	DeleteL2FIB(fib *l2.FIBEntry) error
}

// MockFIBRead provides read methods for FIBs.
type MockFIBRead interface {
	// DumpL2FIBs dumps L2 FIB table entries "configured" in the mock SB.
	DumpL2FIBs() ([]*l2.FIBEntry, error)
}

// MockBDHandler is accessor for bridge domain-related calls into mock SB.
type MockBDHandler struct {
	ifaceIndex idxvpp.NameToIndex // exposed by the ifplugin
	log        logging.Logger

	// mock SB
	nextBDHandle uint32
	mockBDs      mockBDs
}

// MockFIBHandler is accessor for FIB-related calls into mock SB.
type MockFIBHandler struct {
	ifaceIndex idxvpp.NameToIndex // exposed by the ifplugin
	bdIndex    idxvpp.NameToIndex // retrieved from kvscheduler by l2plugin itself
	log        logging.Logger

	// mock SB
	mockFIBs []*l2.FIBEntry
}

// NewMockBDHandler creates new instance of bridge domain handler for mock SB.
func NewMockBDHandler(ifaceIndex idxvpp.NameToIndex, log logging.Logger) MockBDAPI {
	return &MockBDHandler{
		ifaceIndex: ifaceIndex,
		log:        log,
		mockBDs:    make(mockBDs),
	}
}

// NewMockFIBHandler creates new instance of FIB handler for mock SB.
func NewMockFIBHandler(ifaceIndex idxvpp.NameToIndex, bdIndexes idxvpp.NameToIndex,
	log logging.Logger) MockFIBAPI {
	return &MockFIBHandler{
		ifaceIndex: ifaceIndex,
		bdIndex:    bdIndexes,
		log:        log,
	}
}


text:
#region License

#endregion

using System.Configuration.Internal;

namespace Spring.Util
{
    /// <summary>
    /// Implement this interface to create your own, delegating <see cref="IInternalConfigSystem"/>
    /// and set them using <see cref="ConfigurationUtils.SetConfigurationSystem"/>
    /// </summary>
    public interface IChainableConfigSystem : IInternalConfigSystem
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="innerConfigSystem"></param>
        void SetInnerConfigurationSystem(IInternalConfigSystem innerConfigSystem);
    }
}


text:
import {chunk,map,clone,extend} from 'lodash';
import FlexLayout, {IFlexContainer} from "./FlexLayout";
import * as computeLayout from 'css-layout';
import {IFlexLayoutTemplate} from "./FlexLayout";

interface IStyle {
    top?:number,
    left?:number,
    width?:number,
    height?:number
}
interface IImage{
    url:string,
    width?:number,
    height?:number
}

interface INode {
    generate(layout:any):any;
}
interface IPageOptions {
    imagesPerPage?:number;
    width?:number;
    height?:number;
    background:any;
}

const IMAGES_PER_PAGE:number = 3;
export class ImageGallery implements INode  {

    ImagesPerPage:number;
    ImageBoxes:Array<ImageBox> = [];

    constructor(public name: string, images:Array<IImage>,public LayoutTemplate:IFlexLayoutTemplate, public PageOptions?:IPageOptions) {
        if (this.PageOptions === undefined) this.PageOptions = {};

        this.ImagesPerPage = this.PageOptions.imagesPerPage || IMAGES_PER_PAGE;
        this.ImageBoxes = images.map(function(image,index){
            return new ImageBox(image)
        })
    }
    generate() {

        return {
            name:this.name,
            elementName:"ObjectSchema",
            props:{
                background:this.PageOptions.background
            },
            containers: chunk(this.ImageBoxes,this.ImagesPerPage).map(function(images,index){
                return new ImageContainer(images,this.LayoutTemplate,this.PageOptions)
            },this).map(function(item){return item.generate()})
        }
    }
}
class ImageContainer{
    public FlexContainer:IFlexContainer;

    constructor (private images:Array<ImageBox>,public LayoutTemplate, public PageOptions:any)
    {
        this.FlexContainer = new FlexLayout(LayoutTemplate).getContainer(images.length);
    }

    generate(){
        var input = this.FlexContainer;
        var nodeInput = {
            style: {
                width: (input.flexDirection === 'row' || input.flexDirection === 'row-reverse')?this.PageOptions.width:undefined,
                height: !(input.flexDirection === 'row' || input.flexDirection === 'row-reverse')?this.PageOptions.height:undefined,
                //position:'relative',
                flexDirection: input.flexDirection,
                flexWrap: input.flexWrap,
                justifyContent: input.justifyContent,
                alignItems: input.alignItems,
                alignContent: input.alignContent
            },
            children: map(input.items, function (item,i) {
                var image = this.images[i].Image;
                var newItem:any = {
                    position:'relative',
                    //margin:20
                };
                if (!!image.width) newItem.width = image.width;
                if (!!image.height) newItem.height = image.height;
                return {style: extend(newItem,item)}
            },this)
        };

        computeLayout(nodeInput);
        //console.log(JSON.stringify(nodeInput,null,4));
        return {
            name: "ImageContainer",
            elementName: "Container",
            style: {
                width:730,
                height:1000
            },
            props:{
                unbreakable:true
            },
            boxes:this.images.map(function(item:INode,i:number){return item.generate(extend(nodeInput.children[i].layout,{}))},this)
        }
    }
}
class ImageBox implements INode {
    constructor (public Image:IImage){

    }
    generate(style:any){
        return {
            name: "Image",
            elementName: "Core.ImageBox",
            style: style,
            props: {
                url: this.Image.url,
                border:{
                    width:5,
                    radius:20
                },
                width:this.Image.width,
                height:this.Image.height
            }
        }
    }
}


text:
[Home](index.md) | [Installation](install.md) | [Usage](usage.md) | [Config](config.md)

# Usage


text:
[![NuGet](https://img.shields.io/nuget/vpre/Sodium.Core)](https://www.nuget.org/packages/Sodium.Core/1.3.0)

**libsodium for .NET** can be installed as follows:

    dotnet add package Sodium.Core --version 1.3.0

The *Sodium.Core* package is intended to run on
[supported versions of .NET](https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core)
on the following platforms:

* Windows
    * `win-x64`
    * `win-x86`
* Linux
    * `linux-x64` (Most desktop distributions like CentOS, Debian, Fedora, Ubuntu, and derivatives)
    * `linux-musl-x64` (Lightweight distributions using musl like Alpine Linux)
    * `linux-arm` (Linux distributions running on ARM like Raspbian on Raspberry Pi Model 2+)
    * `linux-arm64` (Linux distributions running on 64-bit ARM like Ubuntu Server 64-bit on Raspberry Pi Model 3+)
* macOS
    * `osx-x64`
    * `osx-arm64`

Specifically,
[Sodium.Core 1.3.0](https://www.nuget.org/packages/Sodium.Core/1.3.0)
has been tested to run on the following platforms and .NET versions:

| OS       | Version    | Architectures | .NET  |
|:---------|:---------- |:------------- |:------|
| Windows  | 10.0.20348 | x64           | 6.0.4 |
| macOS    | 11.6       | x64           | 6.0.4 |
| Ubuntu   | 20.04      | x64           | 6.0.4 |
| Alpine   | 3.15       | x64           | 6.0.4 |

Other, similar platforms supported by .NET should work as well but have not been tested.

Using libsodium on Windows requires the
[Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017, 2019, and 2022](https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads).
This dependency is included in the .NET SDK but might
not be present, for example, when deploying a self-contained application.

The implementation of AES-GCM in libsodium is hardware-accelerated and requires an
x64 processor with the AES-NI extension. The availability of this extension can
be determined at runtime using the static `IsAvailable` property of the
`Sodium.SecretAeadAes` class.


text:
class Array
	def chunks chunk_size
		chunks = (1..chunk_size).collect { [] }
		idx = 0
		while any?
			chunks[idx % chunk_size] << shift
			idx += 1
		end
		chunks
	end
end


text:
package com.hazelcast.map.impl.querycache.subscriber.record;

import com.hazelcast.internal.util.Clock;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import static java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater;

/**
 * Contains common functionality which is needed by a {@link QueryCacheRecord} instance.
 */
abstract class AbstractQueryCacheRecord implements QueryCacheRecord {

    private static final AtomicIntegerFieldUpdater<AbstractQueryCacheRecord> ACCESS_HIT =
            newUpdater(AbstractQueryCacheRecord.class, "hits");

    private final long creationTime;

    private volatile int hits;
    private volatile long accessTime = -1L;

    AbstractQueryCacheRecord() {
        creationTime = Clock.currentTimeMillis();
    }

    @Override
    public long getHits() {
        return hits;
    }

    @Override
    public long getLastAccessTime() {
        return accessTime;
    }

    @Override
    public long getCreationTime() {
        return creationTime;
    }

    @Override
    public void incrementAccessHit() {
        ACCESS_HIT.incrementAndGet(this);
    }

    @Override
    public void setAccessTime(long accessTime) {
        this.accessTime = accessTime;
    }
}


text:
				{{ translate('cp.create_fieldset') }}

				{{ translate('cp.editing_fieldset') }}:
				{{ fieldsetTitle }}

			<button type="button" class="btn btn-primary" v-on:click="save()" :disabled="!canSave">{{ translate('cp.save') }}</button>

			 {{ translate('cp.loading') }}

{{ errorMessage }}

				{{ translate('cp.title') }}
				{{ translate('cp.fieldset_title_instructions') }}
				<input type="text" class="form-control" v-model="fieldset.title" autofocus="autofocus" />

				{{ translate('cp.handle') }}
				{{ translate('cp.fieldset_handle_instructions') }}
				<input type="text" class="form-control" v-model="slug" />

				{{ translate('cp.create_title') }}
				{{ translate('cp.fieldset_create_title_instructions') }}
				<input type="text" class="form-control" v-model="fieldset.create_title" />

				{{ translate('cp.hide') }}
				{{ translate('cp.fieldset_hide_instructions') }}
				<toggle-fieldtype :data.sync="fieldset.hide"></toggle-fieldtype>

				{{ translate_choice('cp.fields', 2) }}
				{{ translate('cp.fieldset_fields_instructions') }}
				<fields-builder :fields="fieldset.fields"
								:fieldtypes="fieldtypes"
								:root="true">
				</fields-builder>

				{{ translate_choice('cp.taxonomies', 2) }}
				{{ translate('cp.fieldset_taxonomies_instructions') }}
				<taxonomy-fields-builder
						:fields="fieldset.taxonomies"
						:fieldtypes="fieldtypes">
				</taxonomy-fields-builder>

			<date-time-configuration :date.sync="fieldset.date"></date-time-configuration>


text:
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Citrina
{
    public class BaseLinkRating
    {
        /// <summary>
        /// Count of reviews.
        /// </summary>
        public int? ReviewsCount { get; set; }

        /// <summary>
        /// Count of stars.
        /// </summary>
        public double? Stars { get; set; }
    }
}