text:
title: Private methods with Backbone.js
date: 2014-12-07
layout: post
draft: false
path: /en/2014/12/private-methods-backbonejs/
category: Web development
tags:
  - javascript
  - backbone.js
  - privacy
description: Why and how to create private methods. How to concretely implement that while using Backbone.js.
---

## Private method, what is that?

To keep it short: a private method is a method which is not exposed and can't be called "from the outside".

It's just a matter of **scope** after all.

```js
function yolo() {
  var privateMethod = function() {
    console.log("can't be accessed outside of `yolo()`")
  }
  var privateVar = true

  // here, you can call `privateMethod` if you wish to.
  // here, `privateVar` returns `true`.

  return "that's all you've got from me!"
}

// here, `privateMethod` doesn't even exist.
// here, `privateVar` is undefined.

yolo() // returns "that's all you've got from me!"
```

This notion makes sense with the _module pattern_.

To learn more on that subject, I'd strongly recommend this post among others: [Mastering the module pattern](http://toddmotto.com/mastering-the-module-pattern), simple and efficient.

Well, concretely, here's what you've got:

```js
var MyModule = (function() {
  // a bunch of encapsulated stuff -> private if not exposed
  var _cantTouchThis = function() {
    console.log('I just met you!')
  }

  var _nbOfCats = 3

  // what you return when the method is executed
  // -> what you expose (= public).
  return {
    nbOfRainbows: 10,
    sayHi: function() {
      console.log('Hi!')
    },
    logNumberOfRainbows: function() {
      console.log(this.nbOfRainbows)
    },
    logNumberOfCats: function() {
      console.log(_nbOfCats)
    },
    touchThis: function() {
      console.log('Hey!')
      _cantTouchThis()
    },
  }
})()
```

This allow you to manipulate the public part — **the interface** — without having access to the private part:

```js
MyModule.nbOfRainbows = 42
MyModule.logNumberOfRainbows() // returns "42"
MyModule.logNumberOfCats() // returns "3"
MyModule.touchThis() // returns "Hey!" "I just met you!"
```

## Private methods… for what?

That's a legitimate question. I personally found 2 advantages of a private method:

* it **simplifies the public interface** hiding technical details of the implementation
* it **doesn't have to be unit tested**

The first point is generally obvious. The second one arises from the first point, but may turn you suspicious. Let me explain…

## Unit tests and private methods

To test our module, we instantiate it, then take every single method and check the output for a given input (variable + context). We _unitly_ test the exposed method, taking care to make the input vary to test every use case and get a good test coverage.

We can remind the **Test Driven Development** (TDD) process:

* first write the test with required use cases for our functionality
* then code the method, pass tests
* refactor code to clean the whole thing. As long as tests pass, you didn't break anything.

The _refactor_ step is the last one because we first want to produce something that works, then focus on optimizations.

Let's get back to private methods: since they are not exposed, they are not _technically_ unit tested. In fact, not directly.

## Some considerations about private methods

The TDD point can leave you suspicious depending on the manner you consider and use the privacy notion. I think there is two ways of doing things:

1. first code everything as private, then expose what needs to be
1. private methods came from public methods refactor step

### How to choose between private and public?

Considering you're doing TDD… you develop the public interface **first** — that's what you're testing after all.

Personally, I code my (failing) tests, then my public interface: public methods or _event handlers_ — we'll talk about that few lines below. Nothing private with my code at this point.

Private methods appears during refactor step, at last. And if I don't have time for that the code is working, at least.

Finally, **unit tests test the public interface**.

Private methods are tested through public ones. You may think that testing the detail of technical implementation would be a great idea to ease the debug part, but this would be also a cost to implement and maintain. On the contrary, I can update / refactor private methods as long as my module is still working as expected.

If I have to choose, I'd prefer to invest in testing more use case. The coverage will tell me if I actually test private methods, passing by.

### About events handlers

Public interface matters.

What about events that make our module react? Should event handlers (e.g. `onClick()` & cie.) be public to be tested?

Well, no.

We're talking about events. Our module is listening and reacting upon events. Tests should triggers these events and observe the expected behavior. The binded method could either be public or private, it doesn't really matter here as we're testing the event.

### Prefix with "\_"

That's a largely adopted convention: prefix private methods / variables names with `_`.

This is also what I do for 2 reasons:

1. this is a convention which makes things clear. There is no such a thing than `private` in JS, but most of developers will understand the prefixed method is "supposed" private…
1. this can ease automated documentation plugins work. If you're lucky, the doc autocompletion will understand the method is private and won't forget about the `@private`.

Finally, it's about **readability**. The _private_ nature of the `_joke()` method is explicit.

However, it's a common practice that this convention is used to try to **define** the private nature of a method, instead of just make it explicit. That means the public interface is composed with methods prefixed with `_` to tell those should be _considered as private_.

> This is a hot point, with a lot of debates. On my own experience, this is generally something which appears when you don't really know how to really set up privacy. Everything is public then, but you tell some methods are _private by intention_.

It's relatively easy to introduce privacy with the _module pattern_ in fact. Then, my advice would be to consider everything that is exposed as public. If you actually want a method to be private, let's make it actually private and use the `_` prefix for code readability.

That said, projects should define and follow convention that fit their need.

### It's not part of the prototype… what about inheritance and performance?

That's true. As long as it is not reachable, **a private method can't be inherited**. This is basically why I consider private method as a way to simplify the exposed interface of my module. If I need the implementation detail, if I need to access it by inheritance, then I need this detail to be public.

Here again, scepticism is not really caused because of the nature of private methods, but because of a clumsy interpretation / implementation of these.

A note on performance: there will be as much private methods as instances of the module, which is technically _less performant_. But, [having look the actual impact](http://jsperf.com/public-vs-private-methods), I think this remark belongs to premature optimization for my current use of privacy.

### How do I access public methods from private ones?

That could be blocking.

In fact, if my private method came from a public method refactor, this is not blocking at all. Just because my private method is meant to simplify the public interface, to make the public method **more readable**.

From my point of view, the private method is kind of a own helper for the module, which helps him to perform its job. Every variable the method needs should be passed as parameters.

Doing so, you can even pass the public interface context to the private method:

```js
var MyModule = (function() {
  var _addSomeCats = function(context) {
    context.cats++
    context.logNumberOfCats()
  }

  return {
    cats: 1,
    addSomeCats: function() {
      // This is not really useful to wrap the whole thing in
      // a private method, but that's a way to illustrate how
      // you can pass it the context.
      _addSomeCats(this)
    },
    logNumberOfCats: function() {
      console.log(this.cats)
    },
  }
})()
```

Will give you:

```js
MyModule.addSomeCats() // returns "2"
```

In this example, I'm passing the context as a parameter.

Actually, it's possible to use `this` in the `_addSomeCats` method and then call it with the context bound, thanks to `.call()` this way: `_addSomeCats.call( this );`.

Previous example is not a such a big deal, but here's what it could looks like in an actual project:

```js
var MyModule = (function() {
  // alt. syntax for `var _createViewInstance = function() { … }`.
  // we refactor here some repetitive tasks of the module.
  function _createViewInstance(View, $parent) {
    if (!View) throw new Error('No View constructor provided')
    if (!$parent) throw new Error('No $parent provided')

    var view = new View()
    view.$el.appendTo($parent)
    view.render()

    return view
  }
  // …

  return {
    onRender: function() {
      try {
        this.ordersView = _createViewInstance(this.OrdersView, $('#orders'))
      } catch (err) {
        app.err(err) // -> our way to log errors that would occur
        this.ordersView = {}
      }

      // a bunch of other stuff…
    },

    // …
  }
})()
```

Which leads you directly to **Backbone.js**.

## Implementation with Backbone.js

This is the main reason for this post. After you understood the concept and mastered the way you set up actual privacy with a module, you could end up puzzled on how to implement that when you usually deal with this:

```js
var MyModule = (function() {
  var Books = {}

  Books.Model = Backbone.Model.extend({
    // a bit of configuration…

    isPrivate: function() {
      console.log('certainly not…')
      return false
    },

    _intendedToBePrivate: function() {
      // kind of method that would pop with this implementation.
    },
  })

  // …

  return Books
})()
```

Finally, it's that simple: `.extend()` will accept an object. Alright, let's give him an [IIFE](/en/2013/05/properly-isolate-variables-in-javascript/) that will return this object — its API.

```js
var MyModule = (function() {
  var Books = {}

  var ModelConfiguration = (function() {
    var _actuallyPrivate = function() {
      console.log("look 'ma, I'm private!")
    }

    // expose the configuration.
    return {
      defaults: {
        isCompleted: false,
      },

      urlRoot: '/order',

      isCompleted: function() {
        _actuallyPrivate()
        return this.get('isCompleted')
      },
    }
  })()

  Books.Model = Backbone.Model.extend(ModelConfiguration)

  // …

  return Books
})()
```

## Last words

Private methods in JavaScript is a subject that can feeds a lot of debates and one can remain skeptical about that. Sometimes — often? — it makes the whole thing more complex. That's why I think every project should adopt the position that correspond the best to their needs.

For my part, **I create private methods during refactor step**. If the method is public by nature, I'll treat it as such: no `_` prefix, unit tests. The most important thing for me is not fooling myself with a convention.

However, I struggled for some time to imagine how it could work using _Backbone.js_. As it takes me some time to work the whole thing out, here is my contribution post to help those who may enter the same reflexion I've had.

That said, this is a subject full of debates and interesting reflexions. Don't hesitate to share suggestions and remarks if you're passing by!


text:
package com.lorenzobraghetto.compasslibrary;

/**
 * Property with certainty. When merging properties, the one with higher
 * certainty wins.
 *
 * @param <T>
 */
public class UncertainProperty<T> {

	private final T value;
	private final int certaintyLevel;

	public UncertainProperty(T value) {
		this(value, 18 + 1);
	}

	public UncertainProperty(T value, int certaintyLevel) {
		this.value = value;
		this.certaintyLevel = certaintyLevel;
	}

	public T getValue() {
		return value;
	}

	public int getCertaintyLevel() {
		return certaintyLevel;
	}

	public UncertainProperty<T> getMergedProperty(final UncertainProperty<T> other) {
		if (null == other || null == other.value) {
			return this;
		}
		if (null == this.value) {
			return other;
		}
		if (other.certaintyLevel > certaintyLevel) {
			return other;
		}

		return this;
	}

	public static <T> UncertainProperty<T> getMergedProperty(UncertainProperty<T> property, UncertainProperty<T> otherProperty) {
		if (null == property) {
			return otherProperty;
		}
		return property.getMergedProperty(otherProperty);
	}

}

redpajama_set_name: RedPajamaGithub


text:
<?xml version="1.0" encoding="UTF-8"?>
<definitions id="taskAssigneeExample"
	xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:camunda="http://camunda.org/schema/1.0/bpmn"
	targetNamespace="Examples">

	<process id="testConversationalBeanStoreFlush" isExecutable="true">

		<startEvent id="startEvent" />

		<serviceTask id="serviceTask" camunda:delegateExpression="#{setProcessScopedMessageDelegate}" />

		<userTask id="wait" />

		<endEvent id="endEvent" />

		<sequenceFlow targetRef="serviceTask" sourceRef="startEvent" />
		<sequenceFlow targetRef="wait" sourceRef="serviceTask" />
		<sequenceFlow targetRef="endEvent" sourceRef="wait" />

	</process>
</definitions>


text:
(function() {
    'use strict';

    angular
        .module('g2MatriculaApp')
        .directive('showValidation', showValidation);

    function showValidation () {
        var directive = {
            restrict: 'A',
            require: 'form',
            link: linkFunc
        };

        return directive;

        function linkFunc (scope, element, attrs, formCtrl) {
            element.find('.form-group').each(function() {
                var $formGroup = angular.element(this);
                var $inputs = $formGroup.find('input[ng-model],textarea[ng-model],select[ng-model]');

                if ($inputs.length > 0) {
                    $inputs.each(function() {
                        var $input = angular.element(this);
                        var inputName = $input.attr('name');
                        scope.$watch(function() {
                            return formCtrl[inputName].$invalid && formCtrl[inputName].$dirty;
                        }, function(isInvalid) {
                            $formGroup.toggleClass('has-error', isInvalid);
                        });
                    });
                }
            });
        }
    }
})();


text:
import React from "react";
import { mount } from "enzyme";
import { BreadcrumbItem } from "../index";
import { Text } from "../../Text/index";

import renderer from "react-test-renderer";

describe("Test BreadcrumbItem component", () => {
  it("Test correct render", function() {
    const tree = renderer
      .create(
        <BreadcrumbItem>
          <Text as="a" href="#">
            Atlas
          </Text>
        </BreadcrumbItem>
      )
      .toJSON();
    expect(tree).toMatchSnapshot();
  });
});

describe("Testing BreadcrumbItem component: Basic Tests", () => {
  const brdcrmbItm = mount(
    <BreadcrumbItem>
      <Text as="a" href="#">
        Atlas
      </Text>
    </BreadcrumbItem>
  );

  it("Basic BreadcrumbItem", () => {
    expect(brdcrmbItm.exists(<BreadcrumbItem />)).toBe(true);
  });

  it("Contains No Child", () => {
    const breadcrumbitem = mount(<BreadcrumbItem />);

    expect(breadcrumbitem.props().children).toBeUndefined();
  });

  it("Contains Child", () => {
    expect(brdcrmbItm.props()).toHaveProperty("children");
  });
});


text:
int
main(int argc, char **argv)
{
  using namespace gridfs;

  FUSE.init(argc, argv);
  FUSE.createRootDir();

  return fuse_main(
      FUSE.args.argc,
      FUSE.args.argv,
      &FUSE.filesystem_operations,
      NULL);
}


text:
package com.alibaba.wasp.fserver.metrics;

/**
 * This class is for maintaining the various fserver statistics and publishing
 * them through the metrics interfaces.
 * <p>
 * This class has a number of metrics variables that are publicly accessible;
 * these variables (objects) have methods to update their values.
 */
public class MetricsFServer {
  private MetricsFServerSource serverSource;
  private MetricsFServerWrapper fserverWrapper;

  public MetricsFServer(MetricsFServerWrapper fserverWrapper) {
    this.fserverWrapper = fserverWrapper;
    serverSource = MetricsFServerSourceFactory.createServer(fserverWrapper);
  }

  // for unit-test usage
  MetricsFServer(MetricsFServerWrapper fserverWrapper,
      MetricsFServerSource serverSource) {
    this.fserverWrapper = fserverWrapper;
    this.serverSource = serverSource;
  }

  // for unit-test usage
  MetricsFServerSource getMetricsSource() {
    return serverSource;
  }

  public MetricsFServerWrapper getFServerWrapper() {
    return fserverWrapper;
  }

  public void updateInsert(long t) {
    if (t > 1000) {
      serverSource.incrSlowInsert();
    }
    serverSource.updateInsert(t);
  }

  public void updateTransaction(long t) {
     if (t > 1000) {
       serverSource.incrSlowTransaction();
     }
     serverSource.updateTransaction(t);
   }

  public void updateGenPlan(long t) {
    if (t > 1000) {
      serverSource.incrSlowGenPlan();
    }
    serverSource.updateGenPlan(t);
  }

  public void updateUpdate(long t) {
    if (t > 1000) {
      serverSource.incrSlowUpdate();
    }
    serverSource.updateUpdate(t);
  }

  public void updateDelete(long t) {
    if (t > 1000) {
      serverSource.incrSlowDelete();
    }
    serverSource.updateDelete(t);
  }

  public void updateGet(long t) {
    if (t > 1000) {
      serverSource.incrSlowGet();
    }
    serverSource.updateGet(t);
  }

  public void updateScan(long t) {
    if (t > 1000) {
      serverSource.incrSlowScan();
    }
    serverSource.updateScan(t);
  }

  public void updateCount(long t) {
    if (t > 1000) {
      serverSource.incrSlowCount();
    }
    serverSource.updateCount(t);
  }

  public void updateSum(long t) {
    if (t > 1000) {
      serverSource.incrSlowSum();
    }
    serverSource.updateSum(t);
  }
}


text:
package org.apache.nifi.jms.processors;

import org.apache.nifi.logging.ComponentLog;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.SessionCallback;
import org.springframework.jms.support.JmsHeaders;

import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
 * Generic publisher of messages to JMS compliant messaging system.
 */
final class JMSPublisher extends JMSWorker {

    JMSPublisher(CachingConnectionFactory connectionFactory, JmsTemplate jmsTemplate, ComponentLog processLog) {
        super(connectionFactory, jmsTemplate, processLog);
        processLog.debug("Created Message Publisher for {}", new Object[] {jmsTemplate});
    }

    void publish(String destinationName, byte[] messageBytes) {
        this.publish(destinationName, messageBytes, null);
    }

    void publish(final String destinationName, final byte[] messageBytes, final Map<String, String> flowFileAttributes) {
        this.jmsTemplate.send(destinationName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                BytesMessage message = session.createBytesMessage();
                message.writeBytes(messageBytes);
                setMessageHeaderAndProperties(message, flowFileAttributes);
                return message;
            }
        });
    }

    void publish(String destinationName, String messageText, final Map<String, String> flowFileAttributes) {
        this.jmsTemplate.send(destinationName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage message = session.createTextMessage(messageText);
                setMessageHeaderAndProperties(message, flowFileAttributes);
                return message;
            }
        });
    }

    void setMessageHeaderAndProperties(final Message message, final Map<String, String> flowFileAttributes) throws JMSException {
        if (flowFileAttributes != null && !flowFileAttributes.isEmpty()) {

            Map<String, String> flowFileAttributesToSend = flowFileAttributes.entrySet().stream()
                    .filter(entry -> !entry.getKey().contains("-") && !entry.getKey().contains(".")) // '-' and '.' are illegal chars in JMS property names
                    .collect(Collectors.toMap(Entry::getKey, Entry::getValue));

            for (Entry<String, String> entry : flowFileAttributesToSend.entrySet()) {
                try {
                    if (entry.getKey().equals(JmsHeaders.DELIVERY_MODE)) {
                        message.setJMSDeliveryMode(Integer.parseInt(entry.getValue()));
                    } else if (entry.getKey().equals(JmsHeaders.EXPIRATION)) {
                        message.setJMSExpiration(Integer.parseInt(entry.getValue()));
                    } else if (entry.getKey().equals(JmsHeaders.PRIORITY)) {
                        message.setJMSPriority(Integer.parseInt(entry.getValue()));
                    } else if (entry.getKey().equals(JmsHeaders.REDELIVERED)) {
                        message.setJMSRedelivered(Boolean.parseBoolean(entry.getValue()));
                    } else if (entry.getKey().equals(JmsHeaders.TIMESTAMP)) {
                        message.setJMSTimestamp(Long.parseLong(entry.getValue()));
                    } else if (entry.getKey().equals(JmsHeaders.CORRELATION_ID)) {
                        message.setJMSCorrelationID(entry.getValue());
                    } else if (entry.getKey().equals(JmsHeaders.TYPE)) {
                        message.setJMSType(entry.getValue());
                    } else if (entry.getKey().equals(JmsHeaders.REPLY_TO)) {
                        Destination destination = buildDestination(entry.getValue());
                        if (destination != null) {
                            message.setJMSReplyTo(destination);
                        } else {
                            logUnbuildableDestination(entry.getKey(), JmsHeaders.REPLY_TO);
                        }
                    } else if (entry.getKey().equals(JmsHeaders.DESTINATION)) {
                        Destination destination = buildDestination(entry.getValue());
                        if (destination != null) {
                            message.setJMSDestination(destination);
                        } else {
                            logUnbuildableDestination(entry.getKey(), JmsHeaders.DESTINATION);
                        }
                    } else {
                        // not a special attribute handled above, so send it as a property using the specified property type
                        String type = flowFileAttributes.getOrDefault(entry.getKey().concat(".type"), "unknown").toLowerCase();
                        propertySetterMap.getOrDefault(type, JmsPropertySetterEnum.STRING)
                                .setProperty(message, entry.getKey(), entry.getValue());
                    }
                } catch (NumberFormatException ne) {
                    this.processLog.warn("Incompatible value for attribute " + entry.getKey()
                            + " [" + entry.getValue() + "] is not a number. Ignoring this attribute.");
                }
            }
        }
    }

    private void logUnbuildableDestination(String destinationName, String headerName) {
        this.processLog.warn("Failed to determine destination type from destination name '{}'. The '{}' header will not be set.", new Object[] {destinationName, headerName});
    }

    private Destination buildDestination(final String destinationName) {
        Destination destination;
        if (destinationName.toLowerCase().contains("topic")) {
            destination = this.jmsTemplate.execute(new SessionCallback<Topic>() {
                @Override
                public Topic doInJms(Session session) throws JMSException {
                    return session.createTopic(destinationName);
                }
            });
        } else if (destinationName.toLowerCase().contains("queue")) {
            destination = this.jmsTemplate.execute(new SessionCallback<Queue>() {
                @Override
                public Queue doInJms(Session session) throws JMSException {
                    return session.createQueue(destinationName);
                }
            });
        } else {
            destination = null;
        }

        return destination;
    }

    /**
     * Implementations of this interface use {@link javax.jms.Message} methods to set strongly typed properties.
     */
    public interface JmsPropertySetter {
        void setProperty(final Message message, final String name, final String value) throws JMSException, NumberFormatException;
    }

    public enum JmsPropertySetterEnum implements JmsPropertySetter {
        BOOLEAN( (message, name, value) -> {
            message.setBooleanProperty(name, Boolean.parseBoolean(value));
        } ),
        BYTE( (message, name, value) -> {
            message.setByteProperty(name, Byte.parseByte(value));
        } ),
        SHORT( (message, name, value) -> {
            message.setShortProperty(name, Short.parseShort(value));
        } ),
        INTEGER( (message, name, value) -> {
            message.setIntProperty(name, Integer.parseInt(value));
        } ),
        LONG( (message, name, value) -> {
            message.setLongProperty(name, Long.parseLong(value));
        } ),
        FLOAT( (message, name, value) -> {
            message.setFloatProperty(name, Float.parseFloat(value));
        } ),
        DOUBLE( (message, name, value) -> {
            message.setDoubleProperty(name, Double.parseDouble(value));
        } ),
        STRING( (message, name, value) -> {
            message.setStringProperty(name, value);
        } );

        private final JmsPropertySetter setter;
        JmsPropertySetterEnum(JmsPropertySetter setter) {
            this.setter = setter;
        }

        public void setProperty(Message message, String name, String value) throws JMSException, NumberFormatException {
            setter.setProperty(message, name, value);
        }
    }

    /**
     * This map helps us avoid using JmsPropertySetterEnum.valueOf and dealing with IllegalArgumentException on failed lookup.
     */
    public static Map<String, JmsPropertySetterEnum> propertySetterMap = new HashMap<>();
    static {
        Arrays.stream(JmsPropertySetterEnum.values()).forEach(e -> propertySetterMap.put(e.name().toLowerCase(), e));
    }
}


text:
<?php

namespace Vectorface\Cache;

use Vectorface\Cache\Common\PSR16Util;

/**
 * This cache is ridiculously fast, according to basic benchmarks:
 *
 * Parameters:
 *   APC 3.0.19
 *   9-byte key
 *   151-byte value
 *   10000-iteration test
 *
 * Result:
 *   0.065223 seconds
 *
 * Conclusion:
 *   Capable of approximately 150000 requests/second
 */

/**
 * Implements the Cache interface on top of APC or APCu.
 */
class APCCache implements Cache, AtomicCounter
{
    use PSR16Util;

    /**
     * The module name that defines the APC methods.
     *
     * @var string
     */
    private $apcModule = 'apcu';

    /**
     * @inheritDoc
     */
    public function get($key, $default = null)
    {
        $value = $this->call('fetch', $this->key($key));
        return ($value === false) ? $default : $value;
    }

    /**
     * @inheritDoc
     */
    public function set($key, $value, $ttl = null)
    {
        return $this->call('store', $this->key($key), $value, $this->ttl($ttl));
    }

    /**
     * @inheritDoc
     */
    public function delete($key)
    {
        return $this->call('delete', $this->key($key));
    }

    /**
     * @inheritDoc
     */
    public function clean()
    {
        return false;
    }

    /**
     * @inheritDoc
     */
    public function flush()
    {
        return $this->call('clear_cache');
    }

    /**
     * @inheritDoc
     */
    public function clear()
    {
        return $this->flush();
    }

    /**
     * @inheritDoc
     */
    public function getMultiple($keys, $default = null)
    {
        $keys = $this->keys($keys);
        return $this->defaults(
            $keys,
            $this->call('fetch', $keys),
            $default
        );
    }

    /**
     * @inheritDoc
     */
    public function setMultiple($values, $ttl = null)
    {
        $results = $this->call(
            'store',
            $this->values($values),
            null,
            $this->ttl($ttl)
        );
        return array_reduce($results, function($carry, $item) { return $carry && $item; }, true);
    }

    /**
     * @inheritDoc
     */
    public function deleteMultiple($keys)
    {
        $success = true;
        foreach ($this->keys($keys) as $key) {
            $success = $this->call('delete', $key) && $success;
        }

        return $success;
    }

    /**
     * @inheritDoc
     */
    public function has($key)
    {
        return $this->call('exists', $this->key($key));
    }

    /**
     * @inheritDoc
     */
    public function increment($key, $step = 1, $ttl = null)
    {
        return $this->call('inc', $this->key($key), $this->step($step), null, $this->ttl($ttl));
    }

    /**
     * @inheritDoc
     */
    public function decrement($key, $step = 1, $ttl = null)
    {
        return $this->call('dec', $this->key($key), $this->step($step), null, $this->ttl($ttl));
    }

    /**
     * Pass a call through to APC or APCu
     * @param string $call Transformed to a function apc(u)_$call
     * @param mixed ...$args Function arguments
     * @return mixed The result passed through from apc(u)_$call
     */
    private function call($call, ...$args)
    {
        $function = "{$this->apcModule}_{$call}";

        return $function(...$args);
    }
}


text:
package CXGN::BrAPI::v2::VariantSets;

use Moose;
use Data::Dumper;
use SGN::Model::Cvterm;
use CXGN::Genotype::Search;
use JSON;
use CXGN::BrAPI::FileResponse;
use CXGN::BrAPI::Pagination;
use CXGN::BrAPI::JSONResponse;
use List::Util qw(sum);

extends 'CXGN::BrAPI::v2::Common';

sub search {
    my $self = shift;
    my $inputs = shift;
    my $c = $self->context;
    my $page_size = $self->page_size;
    my $page = $self->page;
    my $status = $self->status;
    my $variantset_ids = $inputs->{variantSetDbId} || ($inputs->{variantSetDbIds} || ());
    my $study_ids = $inputs->{studyDbId} || ($inputs->{studyDbIds} || ());
    my $study_names = $inputs->{studyName} || ($inputs->{studyNames} || ());
    my $variant_id = $inputs->{variantDbId} || ($inputs->{variantDbIds} || ());
    my $callset_id = $inputs->{callSetDbId} || ($inputs->{callSetDbIds} || ());

    my @trial_ids;
    my @protocol_ids;
    if ( $variantset_ids){
        foreach ( @{$variantset_ids} ){
            my @ids = split /p/, $_;
            if(scalar @ids>1){
                push @trial_ids, $ids[0] ? $ids[0] : ();
                push @protocol_ids, $ids[1] ? $ids[1] : ();
            } else{
                push @trial_ids, 0;
            }
        }
    }
    if ($study_ids){
        push @trial_ids, @{$study_ids};
    }

    if (scalar @trial_ids == 0){
        my $trial_search = CXGN::Trial::Search->new({
            bcs_schema=>$self->bcs_schema,
            trial_design_list=>['genotype_data_project']
        });
        my ($data, $total_count) = $trial_search->search();

        foreach (@$data){
            push @trial_ids, $_->{trial_id};
        }
    }

    my $genotype_search = CXGN::Genotype::Search->new({
            bcs_schema=>$self->bcs_schema,
            people_schema => $self->people_schema(),
            cache_root=>$c->config->{cache_file_path},
            trial_list=>\@trial_ids,
            genotypeprop_hash_select=>['DS'],
            protocolprop_top_key_select=>[],
            protocolprop_marker_hash_select=>[],
            accession_list=>$callset_id,
            protocol_id_list=>\@protocol_ids,
    });

    my %variant_sets;

    $genotype_search->init_genotype_iterator();

    while (my ($count, $gt) = $genotype_search->get_next_genotype_info) {

        if( ! $study_names || grep { $_ eq $gt->{genotypingDataProjectName}} @{$study_names} ){

            my $set_id = $gt->{genotypingDataProjectDbId} . "p" . $gt->{analysisMethodDbId};

            if( ! $variant_sets{$set_id}{'analysisIds'} {$gt->{analysisMethodDbId}}) {
                my @analysis;
                push @analysis, {
                    analysisDbId=> qq|$gt->{analysisMethodDbId}|, #protocolid
                    analysisName=> $gt->{analysisMethod},
                    created=>undef,
                    description=>undef,
                    software=>undef,
                    type=>undef,
                    updated=>undef,
                };

                push( @{ $variant_sets { $set_id  }{'analysisIds'} {$gt->{analysisMethodDbId}} }, 1 );
                push( @{ $variant_sets { $set_id  }{'markerCount'}}, $gt->{resultCount} );
                push( @{ $variant_sets { $set_id  }{'analysis'}  }, @analysis);
            }

            push( @{ $variant_sets { $set_id  } {'genotypes'} }, $gt->{genotypeDbId});
            $variant_sets { $set_id  } {'name'}  = $gt->{genotypingDataProjectName} . " - " . $gt->{analysisMethod};
            $variant_sets { $set_id  } {'dataProject'} = $gt->{genotypingDataProjectDbId};
        }
    }

    my @data;
    my $start_index = $page*$page_size;
    my $end_index = $page*$page_size + $page_size - 1;
    my $counter = 0;

    foreach my $id (keys %variant_sets){

        if ($counter >= $start_index && $counter <= $end_index) {
            my @availableFormats;

            push @availableFormats,{
                dataFormat => "json",
                fileFormat => "json",
                fileURL => undef,
            };
            push @data, {
                additionalInfo=>{},
                analysis =>$variant_sets{$id} {'analysis'},
                availableFormats => \@availableFormats,
                callSetCount => scalar @{$variant_sets{$id}{'genotypes'}},
                referenceSetDbId => keys %{ $variant_sets{$id} {'analysisIds'} },
                studyDbId => qq|$variant_sets{$id}{'dataProject'}|,
                variantCount => _sum($variant_sets{$id}{'markerCount'}),
                variantSetDbId => qq|$id|,
                variantSetName => $variant_sets{$id} {'name'},
            };
        }
        $counter++;
    }

    my %result = (data => \@data);
    my @data_files;
    my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
    return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'VariantSets result constructed');
}

sub detail {
    my $self = shift;
    my $inputs = shift;
    my $c = $self->context;
    my $page_size = $self->page_size;
    my $page = $self->page;
    my $status = $self->status;
    my $variantset_id = $inputs->{variantSetDbId};

    my @trial_ids;
    my @protocol_ids;
    if ( $variantset_id){
        my @ids = split /p/, $variantset_id;
        push @trial_ids, $ids[0] ? $ids[0] : ();
        push @protocol_ids, $ids[1] ? $ids[1] : ();
    }

    my $genotype_search = CXGN::Genotype::Search->new({
        bcs_schema=>$self->bcs_schema,
        people_schema => $self->people_schema(),
        cache_root=>$c->config->{cache_file_path},
        trial_list=>\@trial_ids,
        protocol_id_list=>\@protocol_ids,
        genotypeprop_hash_select=>['DS'],
        protocolprop_top_key_select=>[],
        protocolprop_marker_hash_select=>[],
    });
    my $file_handle = $genotype_search->get_cached_file_search_json($c->config->{cluster_shared_tempdir}, 1); #Metadata only returned

    my @data;
    my %variant_sets;

    $genotype_search->init_genotype_iterator();

    while (my ($count, $gt) = $genotype_search->get_next_genotype_info) {

        my $set_id = $gt->{genotypingDataProjectDbId} . "p" . $gt->{analysisMethodDbId};

        if( ! $variant_sets{$set_id}{'analysisIds'} {$gt->{analysisMethodDbId}}) {
            my @analysis;
            push @analysis, {
                analysisDbId=> qq|$gt->{analysisMethodDbId}|, #protocolid
                analysisName=> $gt->{analysisMethod},
                created=>undef,
                description=>undef,
                software=>undef,
                type=>undef,
                updated=>undef,
            };

            push( @{ $variant_sets { $set_id  }{'analysisIds'} {$gt->{analysisMethodDbId}} }, 1 );
            push( @{ $variant_sets { $set_id  }{'markerCount'}}, $gt->{resultCount} );
            push( @{ $variant_sets { $set_id  }{'analysis'}  }, @analysis);
        }

        push( @{ $variant_sets { $set_id  } {'genotypes'} }, $gt->{genotypeDbId});
        $variant_sets { $set_id  } {'name'}  = $gt->{genotypingDataProjectName} . " - " . $gt->{analysisMethod};
        $variant_sets { $set_id  } {'dataProject'} = $gt->{genotypingDataProjectDbId};
    }

    foreach my $id (keys %variant_sets){

        my @availableFormats;

        push @availableFormats,{
            dataFormat => "json",
            fileFormat => "json",
            fileURL => undef,
        };
        push @data, {
            additionalInfo=>{},
            analysis =>$variant_sets{$id} {'analysis'},
            availableFormats => \@availableFormats,
            callSetCount => scalar @{$variant_sets{$id}{'genotypes'}},
            referenceSetDbId => keys %{ $variant_sets{$id} {'analysisIds'} },
            studyDbId => qq|$variant_sets{$id}{'dataProject'}|,
            variantCount => _sum($variant_sets{$id}{'markerCount'}),
            variantSetDbId => qq|$id|,
            variantSetName => $variant_sets{$id} {'name'},
        };
    }

    my @data_files;
    my $pagination = CXGN::BrAPI::Pagination->pagination_response(1,$page_size,$page);
    return CXGN::BrAPI::JSONResponse->return_success(@data, $pagination, \@data_files, $status, 'VariantSets result constructed');
}

sub callsets {
    my $self = shift;
    my $inputs = shift;
    my $c = $self->context;
    my $page_size = $self->page_size;
    my $page = $self->page;
    my $status = $self->status;
    my $variantset_id = $inputs->{variantSetDbId};
    my @callset_id = $inputs->{callSetDbId} ? @{$inputs->{callSetDbId}} : ();
    my @callset_name = $inputs->{callSetName} ? @{$inputs->{callSetName}} : ();

    my @trial_ids;
    my @protocol_ids;
    if ( $variantset_id){
        my @ids = split /p/, $variantset_id;
        push @trial_ids, $ids[0] ? $ids[0] : ();
        push @protocol_ids, $ids[1] ? $ids[1] : ();
    }

    my $genotypes_search = CXGN::Genotype::Search->new({
        bcs_schema=>$self->bcs_schema,
        people_schema => $self->people_schema(),
        cache_root=>$c->config->{cache_file_path},
        trial_list=>\@trial_ids,
        protocol_id_list=>\@protocol_ids,
        genotypeprop_hash_select=>['DS'],
        protocolprop_top_key_select=>[],
        protocolprop_marker_hash_select=>[],
        accession_list=>\@callset_id,
        # offset=>$page_size*$page,
        # limit=>$page_size
    });
    my $file_handle = $genotypes_search->get_cached_file_search_json($c->config->{cluster_shared_tempdir}, 1); #Metadata only returned
    my @data;

    my $start_index = $page*$page_size;
    my $end_index = $page*$page_size + $page_size - 1;
    my $counter = 0;

    open my $fh, "<&", $file_handle or die "Can't open output file: $!";
    my $header_line = <$fh>;

    while( <$fh> ) {
        if ($counter >= $start_index && $counter <= $end_index) {
            my $gt = decode_json $_;
            my @analysis;
            my @availableFormats;

            push @data, {
                additionalInfo=>{},
                callSetDbId=> qq|$gt->{stock_id}|,
                callSetName=> qq|$gt->{stock_name}|,
                created=>undef,
                sampleDbId=>qq|$gt->{stock_id}|,
                studyDbId=>qq|$gt->{genotypingDataProjectDbId}|,
                updated=>undef,
                variantSetDbIds => [ $gt->{genotypingDataProjectDbId}. "p". $gt->{analysisMethodDbId} ],
            };
        }
        $counter++;
    }

    my %result = (data => \@data);
    my @data_files;
    my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
    return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'VariantSets result constructed');
}

sub calls {
    my $self = shift;
    my $inputs = shift;
    my $c = $self->context;
    my $page_size = $self->page_size;
    my $page = $self->page;
    my $status = $self->status;
    my $variantset_id = $inputs->{variantSetDbId};
    my $sep_phased = $inputs->{sep_phased};
    my $sep_unphased = $inputs->{sep_unphased};
    my $unknown_string = $inputs->{unknown_string};
    my $expand_homozygotes = $inputs->{expand_homozygotes};
    my $file_path = $inputs->{file_path};
    my $uri = $inputs->{file_uri};

    if ($sep_phased || $sep_unphased || $expand_homozygotes || $unknown_string){
        push @$status, { 'error' => 'The following parameters are not implemented: expandHomozygotes, unknownString, sepPhased, sepUnphased' };
    }

    my @trial_ids;
    my @protocol_ids;
    if ( $variantset_id){
        my @ids = split /p/, $variantset_id;
        push @trial_ids, $ids[0] ? $ids[0] : ();
        push @protocol_ids, $ids[1] ? $ids[1] : ();
    }

    my @data_files;
    my %result;

    my $genotypes_search = CXGN::Genotype::Search->new({
        bcs_schema=>$self->bcs_schema,
        people_schema => $self->people_schema(),
        cache_root=>$c->config->{cache_file_path},
        trial_list=>\@trial_ids,
        protocol_id_list=>\@protocol_ids,
        genotypeprop_hash_select=>['DS', 'GT', 'NT'],
        protocolprop_top_key_select=>[],
        protocolprop_marker_hash_select=>[],
    });
    my $file_handle = $genotypes_search->get_cached_file_search_json($c->config->{cluster_shared_tempdir}, 0);

    my $start_index = $page*$page_size;
    my $end_index = $page*$page_size + $page_size - 1;
    my $counter = 0;

    open my $fh, "<&", $file_handle or die "Can't open output file: $!";
    my $header_line = <$fh>;
    my $marker_objects = decode_json $header_line;

    my @data;

    while (my $gt_line = <$fh>) {
        my $gt = decode_json $gt_line;
        my $genotype = $gt->{selected_genotype_hash};
        my @ordered_refmarkers = sort keys(%$genotype);
        my $genotypeprop_id = $gt->{markerProfileDbId};

        foreach my $m (@ordered_refmarkers) {
            if ($counter >= $start_index && $counter <= $end_index) {
                my $geno = '';
                if (exists($genotype->{$m}->{'NT'}) && defined($genotype->{$m}->{'NT'})){
                    $geno = $genotype->{$m}->{'NT'};
                }
                elsif (exists($genotype->{$m}->{'GT'}) && defined($genotype->{$m}->{'GT'})){
                    $geno = $genotype->{$m}->{'GT'};
                }
                elsif (exists($genotype->{$m}->{'DS'}) && defined($genotype->{$m}->{'DS'})){
                    $geno = $genotype->{$m}->{'DS'};
                }
                push @data, {
                    additionalInfo=>{},
                    variantName=>qq|$m|,
                    variantDbId=>qq|$m|,
                    callSetDbId=>qq|$gt->{stock_id}|,
                    callSetName=>qq|$gt->{stock_name}|,
                    genotype=>{values=>$geno},
                    genotype_likelihood=>undef,
                    phaseSet=>undef,
                };
            }
            $counter++;
        }
    }

    %result = ( data=>\@data,
                expandHomozygotes=>undef,
                sepPhased=>undef,
                sepUnphased=>undef,
                unknownString=>undef);

    my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
    return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'VariantSets result constructed');
}

sub variants {
    my $self = shift;
    my $inputs = shift;
    my $page_size = $self->page_size;
    my $page = $self->page;
    my $status = $self->status;

    my $variantset_id = $inputs->{variantSetDbId};
    my $marker_ids = $inputs->{variantDbId}  || ($inputs->{variantDbIds} || []);
    my $pageToken = $inputs->{pageToken}->[0] || undef;
    my $schema = $self->bcs_schema;
    my @data_out;

    my @trial_ids;
    my @protocol_ids;
    if ( $variantset_id){
        my @ids = split /p/, $variantset_id;
        push @trial_ids, $ids[0] ? $ids[0] : ();
        push @protocol_ids, $ids[1] ? $ids[1] : ();
    }

    my $marker_search = CXGN::Marker::SearchBrAPI->new({
        bcs_schema => $schema,
        protocol_id_list => \@protocol_ids,
        project_id_list => \@trial_ids,
        marker_name_list => $marker_ids,
        offset=>$page_size*$page,
        limit=>$page_size
    });

    my $start_index = $page*$page_size;
    my $end_index = $page*$page_size + $page_size - 1;
    my $counter = 0;

    my ($data, $total_count) = $marker_search->search();

    foreach (@$data){
        if ($counter >= $start_index && $counter <= $end_index) {
            my $info = $_->{info};
            my $svtype = $1 if ($_->{info} =~ /SVTYPE=(\w+);/) ;
            my @cipos = _get_info($info,'CIPOS');
            my @ciend = _get_info($info,'CIEND');
            my @svlen = _get_info($info,'SVLEN');

            my %data_obj = (
                additionalInfo => {},
                alternate_bases => $_->{alt},
                ciend => [@ciend],
                cipos => [@cipos],
                created => undef,
                end => $_->{pos} + length($_->{ref}),
                filtersApplied => $_->{filter} eq "." ? JSON::false : JSON::true,
                filtersFailed => ( $_->{filter} eq "PASS" || $_->{filter} eq "." ) ? undef : $_->{filter},
                filtersPassed => $_->{filter} eq "PASS" ? JSON::true : JSON::false,
                referenceBases => $_->{ref},
                referenceName =>  $_->{chrom} ? $_->{chrom} : undef,
                start => $_->{pos},
                svlen => @svlen,
                updated => undef,
                variantDbId => qq|$_->{marker_name}|,
                variantNames => $_->{marker_name},
                variantSetDbId => _quote($_->{project_id}, $_->{nd_protocol_id} ),
                variantType => $svtype,
            );
            push @data_out, \%data_obj;
        }
        $counter++;
    }

    my %result = (data=>\@data_out);
    my @data_files;
    my $pagination = CXGN::BrAPI::Pagination->pagination_response($total_count,1,0);
    return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Variants result constructed');
}

sub extract {
    my $self = shift;
    my $inputs = shift;
    my $c = $self->context;
    my $page_size = $self->page_size;
    my $page = $self->page;
    my $status = $self->status;
    my $variantset_ids = $inputs->{variantSetDbId} || ($inputs->{variantSetDbIds} || ());
    my $study_ids = $inputs->{studyDbId} || ($inputs->{studyDbIds} || ());
    my $study_names = $inputs->{studyName} || ($inputs->{studyNames} || ());
    my $variant_id = $inputs->{variantDbId} || ($inputs->{variantDbIds} || ());
    my $callset_id = $inputs->{callSetDbId} || ($inputs->{callSetDbIds} || ());

    my @trial_ids;
    my @protocol_ids;
    if ( $variantset_ids){
        foreach ( @{$variantset_ids} ){
            my @ids = split /p/, $_;
            if(scalar @ids>1){
                push @trial_ids, $ids[0] ? $ids[0] : ();
                push @protocol_ids, $ids[1] ? $ids[1] : ();
            } else {
                @trial_ids = 0;
            }

        }
    }
    if ($study_ids){
        push @trial_ids, @{$study_ids};
    }

    if (scalar @trial_ids == 0){
        my $trial_search = CXGN::Trial::Search->new({
            bcs_schema=>$self->bcs_schema,
            trial_design_list=>['genotype_data_project']
        });
        my ($data, $total_count) = $trial_search->search();

        foreach (@$data){
            push @trial_ids, $_->{trial_id};
        }
    }

    my $genotype_search = CXGN::Genotype::Search->new({
            bcs_schema=>$self->bcs_schema,
            people_schema => $self->people_schema(),
            cache_root=>$c->config->{cache_file_path},
            trial_list=>\@trial_ids,
            genotypeprop_hash_select=>['DS'],
            protocolprop_top_key_select=>[],
            protocolprop_marker_hash_select=>[],
            accession_list=>$callset_id,
            protocol_id_list=>\@protocol_ids,
    });

    my %variant_sets;

    $genotype_search->init_genotype_iterator();

    while (my ($count, $gt) = $genotype_search->get_next_genotype_info) {

        if( ! $study_names || grep { $_ eq $gt->{genotypingDataProjectName}} @{$study_names} ){

            my $set_id = $gt->{genotypingDataProjectDbId} . "p" . $gt->{analysisMethodDbId};

            if( ! $variant_sets{$set_id}{'analysisIds'} {$gt->{analysisMethodDbId}}) {
                my @analysis;
                push @analysis, {
                    analysisDbId=> qq|$gt->{analysisMethodDbId}|, #protocolid
                    analysisName=> $gt->{analysisMethod},
                    created=>undef,
                    description=>undef,
                    software=>undef,
                    type=>undef,
                    updated=>undef,
                };

                push( @{ $variant_sets { $set_id  }{'analysisIds'} {$gt->{analysisMethodDbId}} }, 1 );
                push( @{ $variant_sets { $set_id  }{'markerCount'}}, $gt->{resultCount} );
                push( @{ $variant_sets { $set_id  }{'analysis'}  }, @analysis);
            }

            push( @{ $variant_sets { $set_id  } {'genotypes'} }, $gt->{genotypeDbId});
            $variant_sets { $set_id  } {'name'}  = $gt->{genotypingDataProjectName} . " - " . $gt->{analysisMethod};
            $variant_sets { $set_id  } {'dataProject'} = $gt->{genotypingDataProjectDbId};
        }
    }

    my @data;
    my $start_index = $page*$page_size;
    my $end_index = $page*$page_size + $page_size - 1;
    my $counter = 0;

    foreach my $id (keys %variant_sets){

        if ($counter >= $start_index && $counter <= $end_index) {
            my @availableFormats;

            push @availableFormats,{
                dataFormat => "json",
                fileFormat => "json",
                fileURL => undef,
            };
            push @data, {
                additionalInfo=>{},
                analysis =>$variant_sets{$id} {'analysis'},
                availableFormats => \@availableFormats,
                callSetCount => scalar @{$variant_sets{$id}{'genotypes'}},
                referenceSetDbId => keys %{ $variant_sets{$id} {'analysisIds'} },
                studyDbId => qq|$variant_sets{$id}{'dataProject'}|,
                variantCount => _sum($variant_sets{$id}{'markerCount'}),
                variantSetDbId => qq|$id|,
                variantSetName => $variant_sets{$id} {'name'},
            };
        }
        $counter++;
    }

    my %result = (data => \@data);
    my @data_files;
    my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
    return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'VariantSets result constructed');
}

sub _sum {
    my $array = shift;
    my $sum=0;

    foreach my $num (@$array){
        $sum += $num;
    }
    return $sum;
}

sub _get_info {
    my $info = shift;
    my $item = shift;
    my @array = [];

    #match with CIPOS=-22,18;CIEND=-12,32"
    if ( $info =~ /$item=(-?(\d+),?)+/) {
        my $match = $&;
        $match =~ s/$item=//g;
        my @splited = split(/,/, $match);
        @array = map { $_ + 0 } @splited;
    }

    return @array ;
}

sub _quote {
    my $array = shift;
    my $protocol = shift;

    foreach (@$array) {
        $_ = "$_" . "p". $protocol;
    }

    return $array
}

1;


text:
//---------------------------------------------------------------------------
// Copyright (C) 2003 Dallas Semiconductor Corporation, All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name of Dallas Semiconductor
// shall not be used except as stated in the Dallas Semiconductor
// Branding Policy.
//---------------------------------------------------------------------------
//
//  OWMLNK.C - Link Layer functions for the 1-Wire Master (OWM) on the
//               DS80C400 microcontroller.
//
//  Version: 1.00
//
//  History:
//

#include <reg400.h>
#include "ownet.h"
#include "owm.h"          // 1-Wire Master defines

// exportable link-level functions
SMALLINT owTouchReset(int);
SMALLINT owTouchBit(int,SMALLINT);
SMALLINT owTouchByte(int,SMALLINT);
SMALLINT owWriteByte(int,SMALLINT);
SMALLINT owReadByte(int);
SMALLINT owSpeed(int,SMALLINT);
SMALLINT owLevel(int,SMALLINT);
SMALLINT owProgramPulse(int);
void msDelay(unsigned int);
void usDelay(unsigned int);
long msGettick(void);
SMALLINT owWriteBytePower(int,SMALLINT);
SMALLINT owReadBitPower(int,SMALLINT);
SMALLINT hasPowerDelivery(int);
SMALLINT hasOverDrive(int);
SMALLINT hasProgramPulse(int);

SMALLINT USpeed; // current 1-Wire Net communication speed
SMALLINT ULevel; // current 1-Wire Net level

//--------------------------------------------------------------------------
// Reset all of the devices on the 1-Wire Net and return the result.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
//
// Returns: TRUE(1):  presense pulse(s) detected, device(s) reset
//          FALSE(0): no presense pulses detected
//
SMALLINT owTouchReset(int portnum)
{
   uchar result;
   portnum = 0;

   // Perform a 1-Wire reset
   OWMAD = OWM_COMMAND;
   OWMDR = OWM_1WR_MASK;

   do
   {
     OWMAD = OWM_INTERRUPT_FLAGS;
     result = OWMDR;
   }
   while (!(result & OWM_PD_MASK));

   // Check for short
   if (result & OWM_OW_SHORT_MASK)
     return FALSE;    // No parts found

   // Check for presence detected
   if ((result & OWM_PDR_MASK) == 0)
     return TRUE;    // A part was found

   return FALSE;    // No parts found
}

//--------------------------------------------------------------------------
// Send 1 bit of communication to the 1-Wire Net and return the
// result 1 bit read from the 1-Wire Net.  The parameter 'sendbit'
// least significant bit is used and the least significant bit
// of the result is the return bit.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
// 'sendbit'    - the least significant bit is the bit to send
//
// Returns: 0:   0 bit read from sendbit
//          1:   1 bit read from sendbit
//
SMALLINT owTouchBit(int portnum, SMALLINT sendbit)
{
   unsigned char result;
   portnum = 0;

   // Put into single bit mode
   OWMAD = OWM_CONTROL;
   OWMDR |= OWM_BIT_CTL_MASK;

   // Send a single bit
   OWMAD = OWM_TRANSMIT_BUFFER;
   OWMDR = sendbit;

   do
   {
     OWMAD = OWM_INTERRUPT_FLAGS;
     result = OWMDR;
   }
   while (!(result & OWM_RBF_MASK));

   // Get result bit
   OWMAD = OWM_RECEIVE_BUFFER;
   result = OWMDR;

   // Put back into byte mode
   OWMAD = OWM_CONTROL;
   OWMDR &= ~OWM_BIT_CTL_MASK;

   return result;
}

//--------------------------------------------------------------------------
// Send 8 bits of communication to the 1-Wire Net and return the
// result 8 bits read from the 1-Wire Net.  The parameter 'sendbyte'
// least significant 8 bits are used and the least significant 8 bits
// of the result is the return byte.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
// 'sendbyte'   - 8 bits to send (least significant byte)
//
// Returns:  8 bits read from sendbyte
//
SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
{
   unsigned char result;
   portnum = 0;

   // Send a byte
   OWMAD = OWM_TRANSMIT_BUFFER;
   OWMDR = sendbyte;

   do
   {
     OWMAD = OWM_INTERRUPT_FLAGS;
     result = OWMDR;
   }
   while (!(result & OWM_RBF_MASK));

   // Get result byte
   OWMAD = OWM_RECEIVE_BUFFER;
   result = OWMDR;

   return result;
}

//--------------------------------------------------------------------------
// Send 8 bits of communication to the 1-Wire Net and verify that the
// 8 bits read from the 1-Wire Net is the same (write operation).
// The parameter 'sendbyte' least significant 8 bits are used.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
// 'sendbyte'   - 8 bits to send (least significant byte)
//
// Returns:  TRUE: bytes written and echo was the same
//           FALSE: echo was not the same
//
SMALLINT owWriteByte(int portnum, SMALLINT sendbyte)
{
   return (owTouchByte(portnum,sendbyte) == sendbyte) ? TRUE : FALSE;
}

//--------------------------------------------------------------------------
// Send 8 bits of read communication to the 1-Wire Net and and return the
// result 8 bits read from the 1-Wire Net.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
//
// Returns:  8 bytes read from 1-Wire Net
//
SMALLINT owReadByte(int portnum)
{
   return owTouchByte(portnum,0xFF);
}

//--------------------------------------------------------------------------
// Set the 1-Wire Net communucation speed.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
// 'new_speed'  - new speed defined as
//                MODE_NORMAL     0x00
//                MODE_OVERDRIVE  0x01
//
// Returns:  current 1-Wire Net speed
//
SMALLINT owSpeed(int portnum, SMALLINT new_speed)
{
   portnum = 0;

   USpeed = new_speed;

   switch (new_speed)
   {
     case MODE_NORMAL:
       OWMAD = OWM_CONTROL;
       OWMDR &= ~(OWM_OD_MASK | OWM_LLM_MASK);
       break;
     case MODE_OVERDRIVE:
       OWMAD = OWM_CONTROL;
       OWMDR |= OWM_OD_MASK;
       break;
   }

   return USpeed;
}

//--------------------------------------------------------------------------
// Set the 1-Wire Net line level.  The values for NewLevel are
// as follows:
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
// 'new_level'  - new level defined as
//                MODE_NORMAL     0x00
//                MODE_STRONG5    0x02
//                MODE_PROGRAM    0x04
//                MODE_BREAK      0x08
//
// Returns:  current 1-Wire Net level
//
SMALLINT owLevel(int portnum, SMALLINT new_level)
{
   portnum = 0;
   switch(new_level)
   {
      case MODE_BREAK:
         // Enable force 1-Wire
         OWMAD = OWM_CONTROL;
         OWMDR |= OWM_EN_FOW_MASK;
         // Force 1-Wire line low
         OWMAD = OWM_COMMAND;
         OWMDR |= OWM_FOW_MASK;
         break;

      case MODE_STRONG5:
         // Turn on 5V strong pullup pin
         OWMAD = OWM_CONTROL;
         OWMDR |= OWM_STP_SPLY_MASK;
         break;

      default:

      case MODE_NORMAL:
         // Release 1-Wire line to float high
         OWMAD = OWM_COMMAND;
         OWMDR &= ~OWM_FOW_MASK;
         // Disable force 1-Wire and 5V strong pullup
         OWMAD = OWM_CONTROL;
         OWMDR &= ~(OWM_EN_FOW_MASK | OWM_STP_SPLY_MASK);
         break;
   }

   ULevel = new_level;
   return new_level;
}

//--------------------------------------------------------------------------
// This procedure creates a fixed 480 microseconds 12 volt pulse
// on the 1-Wire Net for programming EPROM iButtons.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
//
// Returns:  TRUE  successful
//           FALSE program voltage not available
//
SMALLINT owProgramPulse(int portnum)
{
   portnum = 0;
   // Not supported
   return FALSE;
}

//--------------------------------------------------------------------------
//  Description:
//     Delay for at least 'len' ms
//
void msDelay(unsigned int delay)
{
   while(delay--)
   {
      usDelay(1030); // -3% error puts us right at 1ms.
	}
}

//--------------------------------------------------------------------------
//  Description:
//     Delay for at least 'len' microseconds
//
// Adapted from Kevin Vigor's TINI 1Wire code.
//
// Works only @33Mhz.  We need 8.25 machine cycles per iteration to get the microsecond
// delay.  Can't have .25 of a cycle, so this function uses 8 cycles per iteration. This
// produces an error of 3% ( .03 = (8.25-8)/8 ).  Most of the use of this function by
// 1-Wire routines calls for delays under 50 microseconds, in which case it is under by
// less than 1.5 microseconds.  It is assumed the overhead of calling the function will
// add enough to account for the difference.  For values much greater than 50 (only in
// reset function), a "fudge factor" is added to account for any error.
//
//
void usDelay(unsigned int delay)
{
#pragma asm
   ; delay is in r6/r7
   ; double-check in output assembly
   mov  a, r7
   orl  a, r6			; quick out for zero case.
   jz   _usDelayDone

   inc  r6
   cjne r7, #0, _usDelayLoop
   dec  r6

   _usDelayLoop:
   nop
   nop
   djnz r7, _usDelayLoop
   djnz r6, _usDelayLoop
   _usDelayDone:

#pragma endasm
   delay = 0;
}

//--------------------------------------------------------------------------
// Get the current millisecond tick count.  Does not have to represent
// an actual time, it just needs to be an incrementing timer.
//
long msGettick(void)
{
   // not supported yet
   return 0;
}

//--------------------------------------------------------------------------
// Send 8 bits of communication to the 1-Wire Net and verify that the
// 8 bits read from the 1-Wire Net is the same (write operation).
// The parameter 'sendbyte' least significant 8 bits are used.  After the
// 8 bits are sent change the level of the 1-Wire net.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
// 'sendbyte' - 8 bits to send (least significant byte)
//
// Returns:  TRUE: bytes written and echo was the same
//           FALSE: echo was not the same
//
SMALLINT owWriteBytePower(int portnum, SMALLINT sendbyte)
{
   unsigned char result;
   portnum = 0;

   /* Warning, this is an implementation specific hack!
    * On the DS80C400 1-Wire Master, if the strong 5V pullup
    * mode is used, a strong pullup will be applied whenever
    * the 1-Wire line is idle.  Therefore, we can enable this mode,
    * call the 1-Wire I/O routine, and know that the strong pullup
    * was applied immediately after the I/O.  THIS SHOULD NOT
    * BE PASTED INTO OTHER CODE AS IT MAY NOT BE APPLICABLE
    * TO ANY OTHER SYSTEM.
    */

   // Enable the 5V strong pull up mode
   if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
   {
     owLevel(portnum,MODE_NORMAL);
     return FALSE;
   }

   // Do the byte I/O
   result = owWriteByte(portnum,sendbyte);

   // Return 1-Wire to normal line levels
   owLevel(portnum,MODE_NORMAL);

   // Return result of I/O operation
   return result;
}

//--------------------------------------------------------------------------
// Send 1 bit of communication to the 1-Wire Net and verify that the
// response matches the 'applyPowerResponse' bit and apply power delivery
// to the 1-Wire net.  Note that some implementations may apply the power
// first and then turn it off if the response is incorrect.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
// 'applyPowerResponse' - 1 bit response to check, if correct then start
//                        power delivery
//
// Returns:  TRUE: bit written and response correct, strong pullup now on
//           FALSE: response incorrect
//
SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse)
{
   portnum = 0;

   /* Warning, this is an implementation specific hack!
    * On the DS80C400 1-Wire Master, if the strong 5V pullup
    * mode is used, a strong pullup will be applied whenever
    * the 1-Wire line is idle.  Therefore, we can enable this mode,
    * call the 1-Wire I/O routine, and know that the strong pullup
    * was applied immediately after the I/O.  THIS SHOULD NOT
    * BE PASTED INTO OTHER CODE AS IT MAY NOT BE APPLICABLE
    * TO ANY OTHER SYSTEM.
    */

   // Enable the 5V strong pull up mode
   if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
   {
     owLevel(portnum,MODE_NORMAL);
     return FALSE;
   }

   // Do the bit I/O
   if (owTouchBit(portnum,1) == applyPowerResponse)
     return TRUE;
   else
   {
     // Return 1-Wire to normal line levels
     owLevel(portnum,MODE_NORMAL);
     return FALSE;
   }

}

//--------------------------------------------------------------------------
// This procedure indicates wether the adapter can deliver power.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
//
// Returns:  TRUE  if adapter is capable of delivering power.
//
SMALLINT owHasPowerDelivery(int portnum)
{
   portnum = 0;

   // add adapter specific code here
   return TRUE;
}

//--------------------------------------------------------------------------
// This procedure indicates wether the adapter can deliver power.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
//
// Returns:  TRUE  if adapter is capable of over drive.
//
SMALLINT owHasOverDrive(int portnum)
{
   portnum = 0;

   // add adapter specific code here
   return TRUE;
}
//--------------------------------------------------------------------------
// This procedure creates a fixed 480 microseconds 12 volt pulse
// on the 1-Wire Net for programming EPROM iButtons.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
//
// Returns:  TRUE  program volatage available
//           FALSE program voltage not available
SMALLINT owHasProgramPulse(int portnum)
{
   portnum = 0;

   // add adapter specific code here
   return FALSE;
}


text:
I18n provides internationalization support for your application, it supports different storage solutions (*backends*) including a SQL database and YAML.

[![GoDoc](https://godoc.org/github.com/qor/i18n?status.svg)](https://godoc.org/github.com/qor/i18n)

## Usage

```go
import (
  "github.com/jinzhu/gorm"
  "github.com/qor/i18n"
  "github.com/qor/i18n/backends/database"
)

func main() {
  db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")

  // Using two backends, earlier backend has higher priority
  I18n = i18n.New(
    database.New(&db), // load translations from database
    yaml.New(filepath.Join(config.Root, "config/locales")),  // load translations from YAML files in directory `config/locales
  )

  // Add Translation
  I18n.AddTranslation(&i18n.Translation{Key: "hello-world", Locale: "en-US", Value: "hello world"})

  // Update Translation
  I18n.SaveTranslation(&i18n.Translation{Key: "hello-world", Locale: "en-US", Value: "Hello World"})

  // Delete Translation
  I18n.DeleteTranslation(&i18n.Translation{Key: "hello-world", Locale: "en-US", Value: "Hello World"})

  // Read Translation with key `hello-world`
  I18n.T("en-US", "hello-world")

  // Read Translation with `Scope`
  I18n.Scope("home-page").T("zh-CN", "hello-world") // read translation with translation key `home-page.hello-world`

  // Read Translation with `Default Value`
  I18n.Default("Default Value").T("zh-CN", "non-existing-key") // Will return default value `Default Value`
}
```

### Interpolation

I18n utilises Golang template to parse translations with interpolation variable.

```go
I18n.AddTranslation(&i18n.Translation{Key: "hello", Locale: "en-US", Value: "Hello {{.Name}}"})
type User struct {
  Name string
}
I18n.T("en-US", "hello", User{Name: "jinzhu"}) //=> Hello jinzhu
```

### Pluralization

I18n utilises [cldr](https://github.com/theplant/cldr) to achieve pluralization, it provides the functions `p`, `zero`, `one`, `two`, `few`, `many`, `other` for this purpose. Refer to cldr documentation for more information.

```go
I18n.AddTranslation(&i18n.Translation{Key: "count", Locale: "en-US", Value: "{{p "Count" (one "{{.Count}} item") (other "{{.Count}} items")}}"})
I18n.T("en-US", "count", map[string]int{"Count": 1}) //=> 1 item
```

### Ordered Params

```go
I18n.AddTranslation(&i18n.Translation{Key: "ordered_params", Locale: "en-US", Value: "{{$1}} {{$2}} {{$1}}"})
I18n.T("en-US", "ordered_params", "string1", "string2") //=> string1 string2 string1
```

### Integrate with Golang Templates

You could define a `t` method and register it as FuncMap:

```go

var I18n *i18n.I18n

func init() {
  I18n = i18n.New(database.New(&db), yaml.New(filepath.Join(config.Root, "config/locales")))
}

func T(key string, value string, args ...interface{}) string {
  return I18n.Default(value).T("en-US", key, args...)
}

// then use it like
{{t "home_page.how_it_works" "HOW DOES IT WORK? {{$1}}" "It works" }}
```

## [QOR Support](https://github.com/qor/qor)

[QOR](http://getqor.com) is architected from the ground up to accelerate development and deployment of Content Management Systems, E-commerce Systems, and Business Applications and as such is comprised of modules that abstract common features for such systems.

Although I18n can be used standalone, it works very nicely with QOR - if you have requirements to manage your application's data, be sure to check QOR out!

To use I18n with QOR, simply add it as resource to the admin:

```go
 Admin.AddResource(I18n)
 ```

[QOR Demo:  http://demo.getqor.com/admin](http://demo.getqor.com/admin)

[I18n Demo with QOR](http://demo.getqor.com/admin/translations)

### Inline Edit

You could manage translations with QOR Admin interface after register it into QOR Admin, But usually hard to translate a translation without its context, the inline edit is made to resolve the problem

With it, you could manage translations from frontend, just like [Integrate with Golang Templates](#integrate-with-golang-templates), you need to register a func map for Golang templates to render inline editable translations.

The good thing is we have created a package do the job for you, it will generate a funcmap, you just need to use it when parsing your templates

```go
// `I18n` hold translations backends
// `en-US` current locale
// `true` enable inline edit mode or not, if inline edit not enabled, it works just like the funcmap in section "Integrate with Golang Templates"
inline_edit.FuncMap(I18n, "en-US", true) // => map[string]interface{}{
                                         //     "t": func(string, ...interface{}) template.HTML {
                                         //        // ...
                                         //      },
                                         //    }
```


text:
`mkdir /disk1`

## Build App Docker Image

```bash
docker build -t morphkurt/node-web-app https://raw.githubusercontent.com/morphkurt/playlistmanipulator/master/Dockerfile
```

```bash
docker pull nginx
docker run -d --restart always --name "nodejs" -p 3000:3000 morphkurt/node-web-app
docker run -d --restart always --name "web" -p 80:80 -v /disk1:/var/www/public -v /root/work/config/config/nginx.conf:/etc/nginx/nginx.conf --link nodejs:nodejs nginx
```


text:
package net.company.service;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * Weak Domain Class for handle http input <br/>
 * No validation and business logic
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class CompanyInParam {
  private String name;
  private String address;
  private String city;
  private String countryCode;
  private String email;
  private String phone;

  public CompanyInParam() {
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getCountryCode() {
    return countryCode;
  }

  public void setCountryCode(String countryCode) {
    this.countryCode = countryCode;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  @Override
  public String toString() {
    return "CompanyInParam{" +
            "name='" + name + '\'' +
            ", address='" + address + '\'' +
            ", city='" + city + '\'' +
            ", countryCode='" + countryCode + '\'' +
            ", email='" + email + '\'' +
            ", phone='" + phone + '\'' +
            '}';
  }
}


text:
<!DOCTYPE html>

    <meta charset="utf-8"/>
    <title>Site Seeing</title>
    <link href="normalize.css" rel="stylesheet"/>
    <link href="style.css" rel="stylesheet"/>


text:
"""
Tests for the part-of-speech tagging visualization
"""

##########################################################################
## Imports
##########################################################################

import pytest
import matplotlib.pyplot as plt

from tests.base import VisualTestCase
from tests.base import IS_WINDOWS_OR_CONDA

from yellowbrick.text.postag import *
from yellowbrick.exceptions import YellowbrickValueError

try:
    import nltk
    from nltk import pos_tag, sent_tokenize
    from nltk import word_tokenize, wordpunct_tokenize
except ImportError:
    nltk = None

try:
    import spacy
except ImportError:
    spacy = None

##########################################################################
## Data
##########################################################################

sonnets = [
    """
    FROM fairest creatures we desire increase,
    That thereby beauty's rose might never die,
    But as the riper should by time decease,
    His tender heir might bear his memory:
    But thou, contracted to thine own bright eyes,
    Feed'st thy light'st flame with self-substantial fuel,
    Making a famine where abundance lies,
    Thyself thy foe, to thy sweet self too cruel.
    Thou that art now the world's fresh ornament
    And only herald to the gaudy spring,
    Within thine own bud buriest thy content
    And, tender churl, makest waste in niggarding.
    Pity the world, or else this glutton be,
    To eat the world's due, by the grave and thee.
    """,
    """
    When forty winters shall beseige thy brow,
    And dig deep trenches in thy beauty's field,
    Thy youth's proud livery, so gazed on now,
    Will be a tatter'd weed, of small worth held:
    Then being ask'd where all thy beauty lies,
    Where all the treasure of thy lusty days,
    To say, within thine own deep-sunken eyes,
    Were an all-eating shame and thriftless praise.
    How much more praise deserved thy beauty's use,
    If thou couldst answer 'This fair child of mine
    Shall sum my count and make my old excuse,'
    Proving his beauty by succession thine!
    This were to be new made when thou art old,
    And see thy blood warm when thou feel'st it cold.
    """,
    """
    Look in thy glass, and tell the face thou viewest
    Now is the time that face should form another;
    Whose fresh repair if now thou not renewest,
    Thou dost beguile the world, unbless some mother.
    For where is she so fair whose unear'd womb
    Disdains the tillage of thy husbandry?
    Or who is he so fond will be the tomb
    Of his self-love, to stop posterity?
    Thou art thy mother's glass, and she in thee
    Calls back the lovely April of her prime:
    So thou through windows of thine age shall see
    Despite of wrinkles this thy golden time.
    But if thou live, remember'd not to be,
    Die single, and thine image dies with thee.
    """,
]

##########################################################################
## PosTag Utils
##########################################################################

def check_nltk_data():
    """
    Returns True if NLTK data has been downloaded, False otherwise
    """
    try:
        nltk.data.find("corpora/treebank")
        return True
    except LookupError:
        pytest.xfail("error occured because nltk postag data is not available")

def check_spacy_data():
    """
    Returns True if SpaCy data has been downloaded, False otherwise
    """
    try:
        spacy.load("en_core_web_sm")
        return True
    except OSError:
        pytest.xfail("error occured because spacy data model is not available")

def get_tagged_docs(X, model="nltk", tagger="word"):
    """
    X is a list of strings; each string is a single document.
    For each document, perform part-of-speech tagging, and
    yield a list of sentences, where each sentence is a list
    of (token, tag) tuples

    If model=="nltk", `NLTK` will be used to sentence and word
    tokenize the incoming documents. User may select the `NLTK`
    tagger to be used; (for now) either the word tokenizer or the
    workpunct tokenizer.

    If model=="spacy", `SpaCy` will be used to sentence and word
    tokenize the incoming documents.
    """
    if model == "spacy":
        nlp = spacy.load("en_core_web_sm")
        for doc in X:
            tagged = nlp(doc)
            yield [
                list((token.text, token.pos_) for token in sent)
                for sent in tagged.sents
            ]

    elif model == "nltk":
        if tagger == "wordpunct":
            for doc in X:
                yield [pos_tag(wordpunct_tokenize(sent)) for sent in sent_tokenize(doc)]
        else:
            for doc in X:
                yield [pos_tag(word_tokenize(sent)) for sent in sent_tokenize(doc)]

##########################################################################
## PosTag Tests
##########################################################################

class TestPosTag(VisualTestCase):
    """
    PosTag (Part of Speech Tagging Visualizer) Tests
    """

    def test_quick_method(self):
        """
        Assert no errors occur when using the quick method
        """
        # Fail if data hasn't been downloaded
        check_nltk_data()

        _, ax = plt.subplots()
        tagged_docs = list(get_tagged_docs(sonnets))

        viz = postag(tagged_docs, ax=ax, show=False)
        viz.ax.grid(False)

        # Fails on Miniconda/Appveyor with images not close (RMS 5.157)
        tol = 5.5 if IS_WINDOWS_OR_CONDA else 0.25
        self.assert_images_similar(viz, tol=tol)

    def test_unknown_tagset(self):
        """
        Ensure an exception is raised if the specified tagset is unknown
        """
        with pytest.raises(YellowbrickValueError):
            PosTagVisualizer(tagset="brill")

    def test_frequency_mode(self):
        """
        Assert no errors occur when the visualizer is run on frequency mode
        """
        check_nltk_data()

        _, ax = plt.subplots()
        tagged_docs = list(get_tagged_docs(sonnets))

        viz = PosTagVisualizer(ax=ax, frequency=True)
        viz.fit(tagged_docs)
        viz.finalize()
        ax.grid(False)

        # Sorted tags i.e predetermined order
        sorted_tags = [
            "noun",
            "adjective",
            "punctuation",
            "verb",
            "preposition",
            "determiner",
            "adverb",
            "conjunction",
            "pronoun",
            "wh- word",
            "modal",
            "infinitive",
            "possessive",
            "other",
            "symbol",
            "existential",
            "digit",
            "non-English",
            "interjection",
            "list",
        ]

        # Extract tick labels from the plot
        ticks_ax = [tick.get_text() for tick in ax.xaxis.get_ticklabels()]

        # Assert that ticks are set properly
        assert ticks_ax == sorted_tags

        # Fails on Miniconda/Appveyor with images not close (RMS 5.302)
        tol = 5.5 if IS_WINDOWS_OR_CONDA else 0.5
        self.assert_images_similar(ax=ax, tol=tol)

    @pytest.mark.skipif(nltk is None, reason="test requires nltk")
    def test_word_tagged(self):
        """
        Assert no errors occur during PosTagVisualizer integration
        with word tokenized corpus
        """
        # Fail if data hasn't been downloaded
        check_nltk_data()

        tagged_docs = list(get_tagged_docs(sonnets, model="nltk", tagger="word"))

        visualizer = PosTagVisualizer(tagset="penn_treebank")

        visualizer.fit(tagged_docs)
        visualizer.ax.grid(False)

        self.assert_images_similar(visualizer)

    @pytest.mark.skipif(nltk is None, reason="test requires nltk")
    def test_wordpunct_tagged(self):
        """
        Assert no errors occur during PosTagVisualizer integration
        with wordpunct tokenized corpus
        """
        # Fail if data hasn't been downloaded
        check_nltk_data()

        wordpunct_tagged_docs = list(
            get_tagged_docs(sonnets, model="nltk", tagger="wordpunct")
        )

        visualizer = PosTagVisualizer(tagset="penn_treebank")

        visualizer.fit(wordpunct_tagged_docs)
        visualizer.ax.grid(False)

        self.assert_images_similar(visualizer)

    @pytest.mark.skipif(spacy is None, reason="test requires spacy")
    def test_spacy_tagged(self):
        """
        Assert no errors occur during PosTagVisualizer integration
        with spacy tokenized corpus
        """
        # Fail if data hasn't been downloaded
        check_spacy_data()

        spacy_tagged_docs = list(get_tagged_docs(sonnets, model="spacy"))

        visualizer = PosTagVisualizer(tagset="universal")

        visualizer.fit(spacy_tagged_docs)
        visualizer.ax.grid(False)

        self.assert_images_similar(visualizer)

    @pytest.mark.skipif(spacy is None, reason="test requires spacy")
    def test_spacy_raw(self):
        """
        Assert no errors occur during PosTagVisualizer integration
        with raw corpus to be parsed using spacy
        """
        visualizer = PosTagVisualizer(parser='spacy', tagset='universal')
        visualizer.fit(sonnets)
        visualizer.ax.grid(False)

        self.assert_images_similar(visualizer)

    @pytest.mark.skipif(nltk is None, reason="test requires nltk")
    def test_nltk_word_raw(self):
        """
        Assert no errors occur during PosTagVisualizer integration
        with raw corpus to be parsed using nltk
        """
        visualizer = PosTagVisualizer(parser='nltk', tagset="penn_treebank")
        visualizer.fit(sonnets)
        visualizer.ax.grid(False)

        self.assert_images_similar(visualizer)

    @pytest.mark.skipif(nltk is None, reason="test requires nltk")
    def test_nltk_wordpunct_raw(self):
        """
        Assert no errors occur during PosTagVisualizer integration
        with raw corpus to be parsed using nltk
        """
        visualizer = PosTagVisualizer(parser='nltk_wordpunct', tagset="penn_treebank")
        visualizer.fit(sonnets)
        visualizer.ax.grid(False)

        self.assert_images_similar(visualizer)

    def test_stack_mode(self):
        """
        Assert no errors occur when the visualizer is run on stack mode
        """
        check_nltk_data()

        _, ax = plt.subplots()
        tagged_docs = list(get_tagged_docs(sonnets))

        visualizer = PosTagVisualizer(stack=True, ax=ax)
        visualizer.fit(tagged_docs, y=["a", "b", "c"])
        visualizer.ax.grid(False)

        self.assert_images_similar(ax=ax)

    def test_stack_frequency_mode(self):
        """
        Assert no errors occur when the visualizer is run on both stack and
        frequency mode
        """
        check_nltk_data()

        _, ax = plt.subplots()
        tagged_docs = list(get_tagged_docs(sonnets))

        visualizer = PosTagVisualizer(stack=True, frequency=True, ax=ax)
        visualizer.fit(tagged_docs, y=["a", "b", "c"])
        visualizer.ax.grid(False)

        # Sorted tags i.e predetermined order
        sorted_tags = [
            "noun",
            "adjective",
            "punctuation",
            "verb",
            "preposition",
            "determiner",
            "adverb",
            "conjunction",
            "pronoun",
            "wh- word",
            "modal",
            "infinitive",
            "possessive",
            "other",
            "symbol",
            "existential",
            "digit",
            "non-English",
            "interjection",
            "list",
        ]

        # Extract tick labels from the plot
        ticks_ax = [tick.get_text() for tick in ax.xaxis.get_ticklabels()]

        # Assert that ticks are set properly
        assert ticks_ax == sorted_tags

        self.assert_images_similar(ax=ax)


text:
import os
import sys

SCRIPTS_DIR = os.path.dirname(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(SCRIPTS_DIR)
from rff import preprocess_raster

def main(src_folder, output_csv_fname):
    cvars = ["pr", "tasmin", "tasmax"]
    preprocess_raster.main(cvars, src_folder, output_csv_fname)

if __name__ == '__main__':
    CURR_DIR = os.path.dirname(os.path.realpath(__file__))
    grids_csv = f"{CURR_DIR}/places_forecast.csv"
    if not os.path.exists(grids_csv):
        sample_gtif = os.path.join(
            SCRIPTS_DIR,
            'rff/raw_data/gcm/NorESM1-M/daily/025deg/agg_year/pr/stats/2021.tif'
        )
        preprocess_raster.create_grids("0.25deg", grids_csv, sample_gtif)
    src_folder = os.path.join(SCRIPTS_DIR,
                              "rff/raw_data/gcm/NorESM1-M/daily/025deg")
    output_csv_fname = f"{CURR_DIR}/WeatherVariability_Forecast.csv"
    main(src_folder, output_csv_fname)


text:
@class DFSystemProfileFetcher;

//-------------------------------------------------------------------------------------------------
// System profile fetcher delegate
@protocol DFSystemProfileFetcherDelegate

// Completion, with or without error
- (void)systemProfileFetcherDidFinish:(DFSystemProfileFetcher*)sender;

@end


text:
using ShoppingSmart.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Drawing;

namespace TestShoppingSmart
{

    ///
    ///This is a test class for SSUtilsTest and is intended
    ///to contain all SSUtilsTest Unit Tests
    ///
    [TestClass()]
    public class SSUtilsTest
    {

        private TestContext testContextInstance;

        ///
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        ///
        ///A test for DownloadImage
        ///
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\Users\\user-000\\documents\\visual studio 2010\\Projects\\ShoppingSmart\\ShoppingSmart", "/")]
        [UrlToTest("http://localhost:8018/")]
        public void DownloadImageTest()
        {
            string _URL = string.Empty; // TODO: Initialize to an appropriate value
            string name = string.Empty; // TODO: Initialize to an appropriate value
            Image expected = null; // TODO: Initialize to an appropriate value
            Image actual;
            actual = SSUtils.DownloadImage(_URL, name);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        ///
        ///A test for StripHTML
        ///
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\Users\\user-000\\documents\\visual studio 2010\\Projects\\ShoppingSmart\\ShoppingSmart", "/")]
        [UrlToTest("http://localhost:8018/")]
        public void StripHTMLTest()
        {
            string text = "
 asdf <hey h=\"j\">f</hey>"; // TODO: Initialize to an appropriate value
            string expected = " asdf f"; // TODO: Initialize to an appropriate value
            string actual;
            actual = SSUtils.StripHTML(text);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        ///
        ///A test for StripInvalidPathChars
        ///
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\Users\\user-000\\documents\\visual studio 2010\\Projects\\ShoppingSmart\\ShoppingSmart", "/")]
        [UrlToTest("http://localhost:8018/")]
        public void StripInvalidPathCharsTest()
        {
            string text = string.Empty; // TODO: Initialize to an appropriate value
            string expected = string.Empty; // TODO: Initialize to an appropriate value
            string actual;
            actual = SSUtils.StripInvalidPathChars(text);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        ///
        ///A test for StripPunctuation
        ///
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\Users\\user-000\\documents\\visual studio 2010\\Projects\\ShoppingSmart\\ShoppingSmart", "/")]
        [UrlToTest("http://localhost:8018/")]
        public void StripPunctuationTest()
        {
            string text = string.Empty; // TODO: Initialize to an appropriate value
            string expected = string.Empty; // TODO: Initialize to an appropriate value
            string actual;
            actual = SSUtils.StripPunctuation(text);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        ///
        ///A test for getFirstStringBetween
        ///
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\Users\\user-000\\documents\\visual studio 2010\\Projects\\ShoppingSmart\\ShoppingSmart", "/")]
        [UrlToTest("http://localhost:8018/")]
        public void getFirstStringBetweenTest()
        {
            string text = string.Empty; // TODO: Initialize to an appropriate value
            string start = string.Empty; // TODO: Initialize to an appropriate value
            string end = string.Empty; // TODO: Initialize to an appropriate value
            string expected = string.Empty; // TODO: Initialize to an appropriate value
            string actual;
            actual = text.getFirstStringBetween(start, end);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        ///
        ///A test for correctSpelling
        ///
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\Users\\user-000\\documents\\visual studio 2010\\Projects\\ShoppingSmart\\ShoppingSmart", "/")]
        [UrlToTest("http://localhost:8018/")]
        public void correctSpellingTest()
        {
            string text = "red dres"; // TODO: Initialize to an appropriate value
            string expected = "red dress"; // TODO: Initialize to an appropriate value
            string actual;
            actual = SSUtils.correctSpelling(text);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        ///
        ///A test for hasCorrectSpelling
        ///
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\Users\\user-000\\documents\\visual studio 2010\\Projects\\ShoppingSmart\\ShoppingSmart", "/")]
        [UrlToTest("http://localhost:8018/")]
        public void hasCorrectSpellingTest()
        {
            string text = "dres"; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = SSUtils.hasCorrectSpelling(text);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
    }
}


text:
"use strict";

module.exports = function(sequelize, DataTypes) {
  var Material = sequelize.define("tbl_Materials", {
    Name: DataTypes.STRING,
    Description: DataTypes.STRING
  },{
    hooks: {
      beforeCreate: function (currentMaterialModel) {
        //Do things on the model before write to the table!!!!
        //
      },
      afterCreate: function (currentMaterialModel) {
        //Do things on the model after it was written to the table!!!!
        //
      }
    }
    // don't add the timestamp attributes (updatedAt, createdAt)
    //timestamps: false,

    // don't delete database entries but set the newly added attribute deletedAt
    // to the current date (when deletion was done). paranoid will only work if
    // timestamps are enabled
   // paranoid: true,

    // don't use camelcase for automatically added attributes but underscore style
    // so updatedAt will be updated_at
   // underscored: true,

    // disable the modification of table names; By default, sequelize will automatically
    // transform all passed model names (first parameter of define) into plural.
    // if you don't want that, set the following
    //freezeTableName: true,

    // define the table's name
    //tableName: 'my_very_custom_table_name'
  });

  return Material;
};


text:
package org.hoteia.qalingo.core.jms.cacheflush.producer;

import org.hoteia.qalingo.core.jms.AbstractMessageJms;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public class CacheFlushMessageJms extends AbstractMessageJms {

    @JacksonXmlProperty(localName="objectId")
    private Long objectId;

    @JacksonXmlProperty(localName="objectType")
    private String objectType;

    public Long getObjectId() {
        return objectId;
    }

    public void setObjectId(Long objectId) {
        this.objectId = objectId;
    }

    public String getObjectType() {
        return objectType;
    }

    public void setObjectType(String objectType) {
        this.objectType = objectType;
    }

}