text:
package gotumblr

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
)

//defines a Go Client for the Tumblr API.
type TumblrRestClient struct {
	request *TumblrRequest
}

//Initializes the TumblrRestClient, creating TumblrRequest that deals with all request formatting.
//consumerKey is the consumer key of your Tumblr Application.
//consumerSecret is the consumer secret of your Tumblr Application.
//oauthToken is the user specific token, received from the /access_token endpoint.
//oauthSecret is the user specific secret, received from the /access_token endpoint.
//host is the host that you are tryng to send information to (e.g. http://api.tumblr.com).
func NewTumblrRestClient(consumerKey, consumerSecret, oauthToken, oauthSecret, callbackUrl, host string) *TumblrRestClient {
	return &TumblrRestClient{NewTumblrRequest(consumerKey, consumerSecret, oauthToken, oauthSecret, callbackUrl, host)}
}

//Gets the user information.
func (trc *TumblrRestClient) Info() UserInfoResponse {
	data := trc.request.Get("/v2/user/info", map[string]string{})
	var result UserInfoResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Retrieves the url of the blog's avatar.
//size can be: 16, 24, 30, 40, 48, 64, 96, 128 or 512.
func (trc *TumblrRestClient) Avatar(blogname string, size int) AvatarResponse {
	requestUrl := trc.request.host + fmt.Sprintf("/v2/blog/%s/avatar/%d", blogname, size)
	httpRequest, err := http.NewRequest("GET", requestUrl, nil)
	if err != nil {
		fmt.Println(err)
	}
	transport := &http.Transport{}
	httpResponse, err2 := transport.RoundTrip(httpRequest)
	if err2 != nil {
		fmt.Println(err2)
	}
	defer httpResponse.Body.Close()
	body, err3 := ioutil.ReadAll(httpResponse.Body)
	if err3 != nil {
		fmt.Println(err3)
	}
	data := trc.request.JSONParse(body)
	var result AvatarResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Gets the likes of the given user.
//options can be:
//limit: the number of results to return, inclusive;
//offset: liked post number to start at.
func (trc *TumblrRestClient) Likes(options map[string]string) LikesResponse {
	data := trc.request.Get("/v2/user/likes", options)
	var result LikesResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Gets the blogs that the user is following.
//options can be:
//limit: the number of results to return;
//offset: result number to start at.
func (trc *TumblrRestClient) Following(options map[string]string) FollowingResponse {
	data := trc.request.Get("/v2/user/following", options)
	var result FollowingResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Gets the dashboard of the user.
//options can be:
//limit: number of results to return;
//offset: post number to start at;
//type: the type of posts to return(text, photo, quote, link, chat, audio, video, answer);
//since_id: return posts that have apeared after this id;
//reblog_info: whether to return reblog information about the posts;
//notes_info: whether to return notes information about the posts.
func (trc *TumblrRestClient) Dashboard(options map[string]string) DraftsResponse {
	data := trc.request.Get("/v2/user/dashboard", options)
	var result DraftsResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Gets a list of posts with the given tag.
//tag: the tag you want to look for.
//options can be:
//before: the timestamp of when you'd like to see posts before;
//limit: the number of results to return;
//filter: the post format you want to get(e.g html, text, raw).
func (trc *TumblrRestClient) Tagged(tag string, options map[string]string) []json.RawMessage {
	options["tag"] = tag
	options["api_key"] = trc.request.apiKey
	data := trc.request.Get("/v2/tagged", options)
	result := []json.RawMessage{}
	json.Unmarshal(data.Response, &result)
	return result
}

//Gets a list of posts from a blog.
//blogname: the name of the blog you want to get posts from (e.g. mgterzieva.tumblr.com).
//postsType: the type of the posts you want to get
//(e.g. text, quote, link, answer, video, audio, photo, chat, all).
//options can be:
//id: the id of the post you are looking for;
//tag: return only posts with this tag;
//limit: the number of posts to return;
//offset: the number of the post you want to start from;
//filter: return only posts with a specific format(e.g. html, text, raw).
func (trc *TumblrRestClient) Posts(blogname, postsType string, options map[string]string) PostsResponse {
	var requestUrl string
	if postsType == "" {
		requestUrl = fmt.Sprintf("/v2/blog/%s/posts", blogname)
	} else {
		requestUrl = fmt.Sprintf("/v2/blog/%s/posts/%s", blogname, postsType)
	}
	options["api_key"] = trc.request.apiKey
	data := trc.request.Get(requestUrl, options)
	var result PostsResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Gets general information about the blog.
//blogname: name of the blog you want to get information about(e.g. mgterzieva.tumblr.com).
func (trc *TumblrRestClient) BlogInfo(blogname string) BlogInfoResponse {
	requestUrl := fmt.Sprintf("/v2/blog/%s/info", blogname)
	options := map[string]string{"api_key": trc.request.apiKey}
	data := trc.request.Get(requestUrl, options)
	var result BlogInfoResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Gets the followers of the blog given.
//blogname: name of the blog whose followers you want to get.
//optons can be:
//limit: the number of results to return, inclusive;
//offset: result to start at.
func (trc *TumblrRestClient) Followers(blogname string, options map[string]string) FollowersResponse {
	requestUrl := fmt.Sprintf("/v2/blog/%s/followers", blogname)
	data := trc.request.Get(requestUrl, options)
	var result FollowersResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Gets the likes of blog given.
//blogname: name of the blog whose likes you want to get.
//options can be:
//limit: how many likes do you want to get;
//offset: the number of the like you want to start from.
func (trc *TumblrRestClient) BlogLikes(blogname string, options map[string]string) LikesResponse {
	requestUrl := fmt.Sprintf("/v2/blog/%s/likes", blogname)
	options["api_key"] = trc.request.apiKey
	data := trc.request.Get(requestUrl, options)
	var result LikesResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Gets posts that are currently in the blog's queue.
//options can be:
//limit: the number of results to return;
//offset: post number to start at;
//filter: specify posts' format(e.g. format="html", format="text", format="raw").
func (trc *TumblrRestClient) Queue(blogname string, options map[string]string) DraftsResponse {
	requestUrl := fmt.Sprintf("/v2/blog/%s/posts/queue", blogname)
	data := trc.request.Get(requestUrl, options)
	var result DraftsResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Gets posts that are currently in the blog's drafts.
//options can be:
//filter: specify posts' format(e.g. format="html", format="text", format="raw").
func (trc *TumblrRestClient) Drafts(blogname string, options map[string]string) DraftsResponse {
	requestUrl := fmt.Sprintf("/v2/blog/%s/posts/draft", blogname)
	data := trc.request.Get(requestUrl, options)
	var result DraftsResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Retrieve submission posts.
//options can be:
//offset: post number to start at;
//filter: specify posts' format(e.g. format="html", format="text", format="raw").
func (trc *TumblrRestClient) Submission(blogname string, options map[string]string) DraftsResponse {
	requestUrl := fmt.Sprintf("/v2/blog/%s/posts/submission", blogname)
	data := trc.request.Get(requestUrl, options)
	var result DraftsResponse
	json.Unmarshal(data.Response, &result)
	return result
}

//Follow the url of a given blog.
//blogname: the url of the blog to follow.
func (trc *TumblrRestClient) Follow(blogname string) error {
	requestUrl := fmt.Sprintf("/v2/user/follow")
	params := map[string]string{"url": blogname}
	data := trc.request.Post(requestUrl, params)
	if data.Meta.Status != 200 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Unfollow the url of a given blog.
//blogname: the url of the blog to unfollow.
func (trc *TumblrRestClient) Unfollow(blogname string) error {
	requestUrl := fmt.Sprintf("/v2/user/unfollow")
	params := map[string]string{"url": blogname}
	data := trc.request.Post(requestUrl, params)
	if data.Meta.Status != 200 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Like post of a given blog.
//id: the id of the post you want to like.
//reblog_key: the reblog key for the post id.
func (trc *TumblrRestClient) Like(id, reblogKey string) error {
	requestUrl := fmt.Sprintf("/v2/user/like")
	params := map[string]string{"id": id, "reblog_key": reblogKey}
	data := trc.request.Post(requestUrl, params)
	if data.Meta.Status != 200 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Unlike a post of a given blog.
//id: the id of the post you want to unlike.
//reblog_key: the reblog key for the post id.
func (trc *TumblrRestClient) Unlike(id, reblogKey string) error {
	requestUrl := fmt.Sprintf("/v2/user/unlike")
	params := map[string]string{"id": id, "reblog_key": reblogKey}
	data := trc.request.Post(requestUrl, params)
	if data.Meta.Status != 200 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Create a photo post or photoset on a blog.
//blogname: the url of the blog you want to post to.
//options can be:
//(with * are marked required options)
//state: the state of the post(e.g. published, draft, queue, private);
//tags: a list of tags you want applied to the post;
//tweet: manages the autotweet for this post: set to off for no tweet
//or enter text to override the default tweet;
//date: the GMT date and time of the post as a string;
//format: sets the format type of the post(html or markdown);
//slug: add a short text summary to the end of the post url;
//caption: the caption that you want applied to the photo;
//link: the 'click-through' url for the photo;
//*source: the photo source url.
func (trc *TumblrRestClient) CreatePhoto(blogname string, options map[string]string) error {
	requestUrl := fmt.Sprintf("/v2/blog/%s/post", blogname)
	options["type"] = "photo"
	data := trc.request.Post(requestUrl, options)
	if data.Meta.Status != 201 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Create a text post on a blog.
//blogname: the url of the blog you want to post to.
//options can be:
//(with * are marked required options)
//state: the state of the post(e.g. published, draft, queue, private);
//tags: a list of tags you want applied to the post;
//tweet: manages the autotweet for this post: set to off for no tweet
//or enter text to override the default tweet;
//date: the GMT date and time of the post as a string;
//format: sets the format type of the post(html or markdown);
//slug: add a short text summary to the end of the post url;
//title: the optional title of the post;
//*body: the full text body.
func (trc *TumblrRestClient) CreateText(blogname string, options map[string]string) error {
	requestUrl := fmt.Sprintf("/v2/blog/%s/post", blogname)
	options["type"] = "text"
	data := trc.request.Post(requestUrl, options)
	if data.Meta.Status != 201 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Create a quote post on a blog.
//blogname: the url of the blog you want to post to.
//options can be:
//(with * are marked required options)
//state: the state of the post(e.g. published, draft, queue, private);
//tags: a list of tags you want applied to the post;
//tweet: manages the autotweet for this post: set to off for no tweet
//or enter text to override the default tweet;
//date: the GMT date and time of the post as a string;
//format: sets the format type of the post(html or markdown);
//slug: add a short text summary to the end of the post url;
//*quote: the full text of the quote;
//source: the cited source of the quote.
func (trc *TumblrRestClient) CreateQuote(blogname string, options map[string]string) error {
	requestUrl := fmt.Sprintf("/v2/blog/%s/post", blogname)
	options["type"] = "quote"
	data := trc.request.Post(requestUrl, options)
	if data.Meta.Status != 201 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Create a link post on a blog.
//blogname: the url of the blog you want to post to.
//options can be:
//(with * are marked required options)
//state: the state of the post(e.g. published, draft, queue, private);
//tags: a list of tags you want applied to the post;
//tweet: manages the autotweet for this post: set to off for no tweet
//or enter text to override the default tweet;
//date: the GMT date and time of the post as a string;
//format: sets the format type of the post(html or markdown);
//slug: add a short text summary to the end of the post url;
//title: the title of the page the link points to;
//*url: the link you are posting;
//description: the description of the link you are posting.
func (trc *TumblrRestClient) CreateLink(blogname string, options map[string]string) error {
	requestUrl := fmt.Sprintf("/v2/blog/%s/post", blogname)
	options["type"] = "link"
	data := trc.request.Post(requestUrl, options)
	if data.Meta.Status != 201 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Create a chat post on a blog.
//blogname: the url of the blog you want to post to.
//options can be:
//(with * are marked required options)
//state: the state of the post(e.g. published, draft, queue, private);
//tags: a list of tags you want applied to the post;
//tweet: manages the autotweet for this post: set to off for no tweet
//or enter text to override the default tweet;
//date: the GMT date and time of the post as a string;
//format: sets the format type of the post(html or markdown);
//slug: add a short text summary to the end of the post url;
//title: the title of the chat;
//*conversation: the text of the conversation/chat, with dialogue labels.
func (trc *TumblrRestClient) CreateChatPost(blogname string, options map[string]string) error {
	requestUrl := fmt.Sprintf("/v2/blog/%s/post", blogname)
	options["type"] = "chat"
	data := trc.request.Post(requestUrl, options)
	if data.Meta.Status != 201 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Create an audio post on a blog.
//blogname: the url of the blog you want to post to.
//options can be:
//(with * are marked required options)
//state: the state of the post(e.g. published, draft, queue, private);
//tags: a list of tags you want applied to the post;
//tweet: manages the autotweet for this post: set to off for no tweet
//or enter text to override the default tweet;
//date: the GMT date and time of the post as a string;
//format: sets the format type of the post(html or markdown);
//slug: add a short text summary to the end of the post url;
//caption: the caption of the post;
//*external_url: the url of the site that hosts the audio file.
func (trc *TumblrRestClient) CreateAudio(blogname string, options map[string]string) error {
	requestUrl := fmt.Sprintf("/v2/blog/%s/post", blogname)
	options["type"] = "audio"
	data := trc.request.Post(requestUrl, options)
	if data.Meta.Status != 201 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Create a video post on a blog.
//blogname: the url of the blog you want to post to.
//options can be:
//(with * are marked required options)
//state: the state of the post(e.g. published, draft, queue, private);
//tags: a list of tags you want applied to the post;
//tweet: manages the autotweet for this post: set to off for no tweet
//or enter text to override the default tweet;
//date: the GMT date and time of the post as a string;
//format: sets the format type of the post(html or markdown);
//slug: add a short text summary to the end of the post url;
//caption: the caption for the post;
//*embed: the html embed code for the video.
func (trc *TumblrRestClient) CreateVideo(blogname string, options map[string]string) error {
	requestUrl := fmt.Sprintf("/v2/blog/%s/post", blogname)
	options["type"] = "video"
	data := trc.request.Post(requestUrl, options)
	if data.Meta.Status != 201 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Creates a reblog on the given blog.
//blogname: the url of the blog you want to reblog to.
//options should be:
//(with * are marked required options)
//*id: the id of the reblogged post;
//*reblog_key: the reblog key of the rebloged post.
func (trc *TumblrRestClient) Reblog(blogname string, options map[string]string) error {
	requestUrl := fmt.Sprintf("/v2/blog/%s/post/reblog", blogname)
	data := trc.request.Post(requestUrl, options)
	if data.Meta.Status != 201 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Deletes a post with a given id.
//blogname: the url of the blog you want to delete from.
//id: the id of the post you want to delete.
func (trc *TumblrRestClient) DeletePost(blogname, id string) error {
	requestUrl := fmt.Sprintf("/v2/blog/%s/post/delete", blogname)
	params := map[string]string{"id": id}
	data := trc.request.Post(requestUrl, params)
	if data.Meta.Status != 200 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}

//Edits a post with a given id.
//blogname: the url of the blog you want to post to.
//options can be:
//(with * are marked required options)
//tags: a list of tags you want applied to the post;
//tweet: manages the autotweet for this post: set to off for no tweet
//or enter text to override the default tweet;
//date: the GMT date and time of the post as a string;
//format: sets the format type of the post(html or markdown);
//slug: add a short text summary to the end of the post url;
//*id: the id of the post.
//The other options are specific to the type of post you want to edit.
func (trc *TumblrRestClient) EditPost(blogname string, options map[string]string) error {
	requestUrl := fmt.Sprintf("/v2/blog/%s/post/edit", blogname)
	data := trc.request.Post(requestUrl, options)
	if data.Meta.Status != 200 {
		return errors.New(data.Meta.Msg)
	}
	return nil
}


text:
module GeographicApiClient
  #
  class PoiDetailsResponse < BaseObject
    attr_accessor :poi_details
    # attribute mapping from ruby-style variable name to JSON key
    def self.attribute_map
      {

        # Full POI details
        :'poi_details' => :'poiDetails'

      }
    end

    # attribute type
    def self.swagger_types
      {
        :'poi_details' => :'FullPOI'

      }
    end

    def initialize(attributes = {})
      return if !attributes.is_a?(Hash) || attributes.empty?

      # convert string to symbol for hash key
      attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

      if attributes[:'poiDetails']
        @poi_details = attributes[:'poiDetails']
      end

    end
  end
end

redpajama_set_name: RedPajamaGithub


text:
CREATE OR REPLACE FUNCTION TS_DAY(timestamp) RETURNS date AS $$
   SELECT $1::date $$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION TS_WEEK(timestamp) RETURNS text AS $$
   SELECT EXTRACT(YEAR FROM $1) || '/' || LPAD(EXTRACT(WEEK FROM $1)::text, 2, '0') $$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION TS_MONTH(timestamp) RETURNS text AS $$
   SELECT EXTRACT(YEAR FROM $1) || '-' || LPAD(EXTRACT(MONTH FROM $1)::text, 2, '0') $$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION TS_YEAR(timestamp) RETURNS text AS $$
   SELECT EXTRACT(YEAR FROM $1) || '' $$ LANGUAGE SQL;


text:
function [allregionpaths,correspondentPath,totPaths]=Preparethepaths(similarities,allregionsframes,filenames)
%if any option is selected wrongly the function return all three variables
%set to zero, so other parts of the program have to check that
%allregionpaths and correspondentPath are cell arrays

%method
method=input('Please select if you want\n(1[default]) to use a forward/backward dynamic programming or (2) a backward: ');
if (isempty(method)||(method==1))
    method=1; %Forward and backward dynamic programming
elseif (method~=2)
    allregionpaths=0;
    correspondentPath=0;
    totPaths=0;
    return
end

%method_sum_log
method_sum_log=input('(1[default]) to sum the similarities or (2) to sum their logarithms (multiply): ');
if (isempty(method_sum_log)||(method_sum_log==1))
    method_sum_log=1; %Forward and backward dynamic programming
elseif (method_sum_log~=2)
    allregionpaths=0;
    correspondentPath=0;
    totPaths=0;
    return
end

%what to scramble with
scramblewithmap=input('(1[default]) to use a map or (2) the path length: ');
if (isempty(scramblewithmap)||(scramblewithmap==1))
    scramblewithmap=1; %map
elseif (scramblewithmap~=2)
    allregionpaths=0;
    correspondentPath=0;
    totPaths=0;
    return
end

if (method==1)&&(scramblewithmap==1)&&(method_sum_log==1)
    filename=filenames.filename_forward_backward_with_scrambled_map; %bi-directional map correspondentPath
elseif (method==1)&&(scramblewithmap==2)&&(method_sum_log==1)
    filename=filenames.filename_forward_backward_with_length_map; %bi-directional length correspondentPath
elseif (method==2)&&(scramblewithmap==1)&&(method_sum_log==1)
    filename=filenames.filename_backward_with_scrambled_map; %mono-directional map correspondentPath
elseif (method==2)&&(scramblewithmap==2)&&(method_sum_log==1)
    filename=filenames.filename_backward_with_length_map; %mono-directional length correspondentPath
elseif (method==1)&&(scramblewithmap==1)&&(method_sum_log==2)
    filename=filenames.filename_forward_backward_with_scrambled_map_log; %bi-directional map correspondentPath
elseif (method==1)&&(scramblewithmap==2)&&(method_sum_log==2)
    filename=filenames.filename_forward_backward_with_length_map_log; %bi-directional length correspondentPath
elseif (method==2)&&(scramblewithmap==1)&&(method_sum_log==2)
    filename=filenames.filename_backward_with_scrambled_map_log; %mono-directional map correspondentPath
else %(method==2)&&(scramblewithmap==2)&&(method_sum_log==2)
    filename=filenames.filename_backward_with_length_map_log; %mono-directional length correspondentPath
end

noFrames=size(allregionsframes,2);
if (exist(filename,'file'))
    %whether to use a precomputed file
    loadChoice=input('(1[default]) to load a saved file or (2) to compute new data: ');
    if (isempty(loadChoice)||(loadChoice==1))
        load(filename);
        if (scramblewithmap==1)
            totPaths=numel(allregionpaths.totalLength);
        else
            totPaths=noFrames;
        end
        return
    elseif (loadChoice~=2)
        allregionpaths=0;
        correspondentPath=0;
        totPaths=0;
        return
    end
end

%whether to save after the computation
saveChoice=input('(1[default]) to save the file after the computation or (2) not to save: ');
if (isempty(saveChoice)||(saveChoice==1))
    saveChoice=1;
elseif (saveChoice~=2)
    allregionpaths=0;
    correspondentPath=0;
    totPaths=0;
    return
end

if (scramblewithmap==1)
    %whether to use saved map, if possible
    if (exist(filenames.filename_map,'file'))
        loadMap=input('(1[default]) to use a saved map, if possible, or (2) to compute a new one: ');
        if (isempty(loadMap)||(loadMap==1))
            loadMap=1;
        elseif (loadMap~=2)
            allregionpaths=0;
            correspondentPath=0;
            totPaths=0;
            return
        end
    else
        loadMap=2;
    end

    %whether to save map afterwards, if it was recomputed
    nosaveMap=input('(1[default]) to not save the map or (2) save it, if recomputed: ');
    if (isempty(nosaveMap)||(nosaveMap==1))
        nosaveMap=1;
    elseif (nosaveMap~=2)
        allregionpaths=0;
        correspondentPath=0;
        totPaths=0;
        return
    end
else
    loadMap=2;
    nosaveMap=1;
end

if (method_sum_log==1)
    chosenSimilars=Getpaths(similarities); %backward dynamic programming
else
    chosenSimilars=Get_probability_paths(similarities); %equivalent of Getpaths, but initialising with 0 and taking sum of logarithms (product of probabilities)
end
if (method==1)
    %in the forward/backward dynamic programming the forward similarities
    %and the output of a forward dynamic programming are need
    forwardSimilarities=Invertsimilarities(similarities);
    if (method_sum_log==1)
        chosenforwardSimilars=Getpaths(forwardSimilarities);
    else
        chosenforwardSimilars=Get_probability_paths(forwardSimilarities); %equivalent of Getpaths, but initialising with 0 and taking sum of logarithms (product of probabilities)
    end

    for frame=1:fix(noFrames/2)
        tmp=chosenforwardSimilars{frame};
        chosenforwardSimilars{frame}=chosenforwardSimilars{noFrames-frame+1};
        chosenforwardSimilars{noFrames-frame+1}=tmp;
    end
    % for frame=1:fix(noFrames/2) %reinvert the forwardSimilarities so as to have them in the right direction
    %     tmp=forwardSimilarities{frame};
    %     forwardSimilarities{frame}=forwardSimilarities{noFrames-frame+1};
    %     forwardSimilarities{noFrames-frame+1}=tmp;
    % end

    if (method_sum_log==1)
        allregionpaths=Getbidirectionalpaths(chosenSimilars,chosenforwardSimilars,similarities);
    else
        allregionpaths=Get_bidirectional_dp_with_probabilities(chosenSimilars,chosenforwardSimilars,similarities); %equivalent of Getbidirectionalpaths,
                                                                                %but initialising with 0 and taking sum of logarithms (product of probabilities)
    end
    fprintf('Forward/backward paths computed\n');
else
    %in the backward dynamic programming the backward similarities
    %are sufficient and splitting is decided according to similarity (not
    %to a forward dynamic programming
    if (method_sum_log==1)
        allregionpaths=Getmonodirectionalpaths(chosenSimilars);
    else
        fprintf('Backward dynamic programming and sum of logarithms not yet implemented\n');
        allregionpaths=0;
        correspondentPath=0;
        totPaths=0;
        return
    end
    fprintf('Backward paths computed\n');
end

if (scramblewithmap==1)
    totPaths=numel(allregionpaths.totalLength);
    if (loadMap==1)
        load(filenames.filename_map);
        if (size(map,2)>=totPaths)
            fprintf('Using saved map\n');
        else
            clear map;
            loadMap=2; %this is done to not save if the present map file was already valid
        end
    end
    if (~exist('map','var'))
        map=Scramble(totPaths);
        fprintf('New map generated\n');
    end
    if ((loadMap~=1)&&(nosaveMap~=1))
        save(filenames.filename_map, 'map','-v7.3');
        fprintf('New map saved\n');
    end
else
    totPaths=noFrames;
    map=allregionpaths.totalLength;
end

correspondentPath=Getcorrespondingbilabel(allregionsframes,allregionpaths,map);
fprintf('Correspondences computed\n');

if (saveChoice==1)
    save(filename, 'allregionpaths', 'correspondentPath', 'totPaths', '-v7.3');
    fprintf('Paths and correspondences saved\n');
end

function to_run_for_the_probability_paths()
%some notes:
%- normalising the probabiliy does not really improve on the already
%computed result
%- it is to consider the improvements given by using the product instead of
%the sum
%- it is worth trying to multiply the prior times the similarities, not
%normalising

%using normalisedsimilaritiesnnf (not pre-normalised before the prior) and products (sums of logarithms)
load(filenames.filename_allregionsframes_all)
load('D:\normalisedsimilaritiesprior.mat')
%loads normalisedsimilaritiesnnf

forwardSimilarities=Invertsimilarities(normalisedsimilaritiesnnf);
chosenSimilars=Get_probability_paths(normalisedsimilaritiesnnf); %equivalent of Getpaths, but initialising with 0 and taking sum of logarithms (product of probabilities)
chosenforwardSimilars=Get_probability_paths(forwardSimilarities);
noFrames=size(chosenforwardSimilars,2);
for frame=1:fix(noFrames/2)
    tmp=chosenforwardSimilars{frame};
    chosenforwardSimilars{frame}=chosenforwardSimilars{noFrames-frame+1};
    chosenforwardSimilars{noFrames-frame+1}=tmp;
end
allregionpaths=Get_bidirectional_dp_with_probabilities(chosenSimilars,chosenforwardSimilars,normalisedsimilaritiesnnf); %equivalent of Getbidirectionalpaths,
                                                                                %but initialising with 0 and taking sum of logarithms (product of probabilities)
totPaths=numel(allregionpaths.totalLength);

load(filenames.filename_map)
% map=Scramble(totPaths);

correspondentPath=Getcorrespondingbilabel(allregionsframes,allregionpaths,map);
save('D:\newregpath_all.mat', 'allregionpaths', 'correspondentPath', '-v7.3');
% load('D:\newregpath_all.mat');
correspondentlengthpath=Getcorrespondingbilabel(allregionsframes,allregionpaths,allregionpaths.totalLength);

[flows,allregionsframes,normalisedsimilarities]=getMenu(cim,ucm2,flows,allregionsframes,normalisedsimilaritiesnnf,allregionpaths,correspondentPath);
frame=1;
level=35;
[frame,level]=getclusteringmenu(frame,level,cim,ucm2,flows,allregionsframes,allregionpaths,correspondentPath);

minLength=2;
load(filenames.filename_flows)
filter_flow=false;
[trajectories,mapPathToTrajectory]=Gettrajectories(ucm2,flows,allregionsframes,allregionpaths,minLength,filter_flow);
save('D:\newtraj_all.mat', 'trajectories','mapPathToTrajectory','-v7.3');
fprintf('Saved to file\n');

%using normalisedsimilarities and products (sums of logarithms)
load(filenames.filename_allregionsframes_all)
load('D:\normalisedsimilaritiesprior.mat')
%loads normalisedsimilarities

forwardSimilarities=Invertsimilarities(normalisedsimilarities);
chosenSimilars=Get_probability_paths(normalisedsimilarities); %equivalent of Getpaths, but initialising with 0 and taking sum of logarithms (product of probabilities)
chosenforwardSimilars=Get_probability_paths(forwardSimilarities);
noFrames=size(chosenforwardSimilars,2);
for frame=1:fix(noFrames/2)
    tmp=chosenforwardSimilars{frame};
    chosenforwardSimilars{frame}=chosenforwardSimilars{noFrames-frame+1};
    chosenforwardSimilars{noFrames-frame+1}=tmp;
end
allregionpaths=Get_bidirectional_dp_with_probabilities(chosenSimilars,chosenforwardSimilars,normalisedsimilarities); %equivalent of Getbidirectionalpaths,
                                                                                %but initialising with 0 and taking sum of logarithms (product of probabilities)
totPaths=numel(allregionpaths.totalLength);

load(filenames.filename_map)
% map=Scramble(totPaths);

correspondentPath=Getcorrespondingbilabel(allregionsframes,allregionpaths,map);
save('D:\newregpath_all.mat', 'allregionpaths', 'correspondentPath', '-v7.3');
% load('D:\newregpath_all.mat');
correspondentlengthpath=Getcorrespondingbilabel(allregionsframes,allregionpaths,allregionpaths.totalLength);

[flows,allregionsframes,normalisedsimilarities]=getMenu(cim,ucm2,flows,allregionsframes,normalisedsimilarities,allregionpaths,correspondentPath);
frame=1;
level=35;
[frame,level]=getclusteringmenu(frame,level,cim,ucm2,flows,allregionsframes,allregionpaths,correspondentPath);

minLength=2;
load(filenames.filename_flows)
filter_flow=false;
[trajectories,mapPathToTrajectory]=Gettrajectories(ucm2,flows,allregionsframes,allregionpaths,minLength,filter_flow);
save('D:\newtraj_all.mat', 'trajectories','mapPathToTrajectory','-v7.3');
fprintf('Saved to file\n');

% % % %using normalised similarities and sums - normalised similarities do not
% % % %seem to work
% % % load(filenames.filename_allregionsframes_all)
% % % load(filenames.filename_normalisedsimilarities_all)
% % %
% % % forwardSimilarities=Invertsimilarities(normalisedsimilarities);
% % % chosenSimilars=Getpaths(normalisedsimilarities); %equivalent of Getpaths, but initialising with 0 and taking sum of logarithms (product of probabilities)
% % % chosenforwardSimilars=Getpaths(forwardSimilarities);
% % % noFrames=size(chosenforwardSimilars,2);
% % % for frame=1:fix(noFrames/2)
% % %     tmp=chosenforwardSimilars{frame};
% % %     chosenforwardSimilars{frame}=chosenforwardSimilars{noFrames-frame+1};
% % %     chosenforwardSimilars{noFrames-frame+1}=tmp;
% % % end
% % % allregionpaths=Getbidirectionalpaths(chosenSimilars,chosenforwardSimilars,normalisedsimilarities); %equivalent of Getbidirectionalpaths,
% % %                                                                                %but initialising with 0 and taking sum of logarithms (product of probabilities)
% % % totPaths=numel(allregionpaths.totalLength);
% % %
% % % load(filenames.filename_map)
% % % % map=Scramble(totPaths);
% % %
% % % correspondentPath=Getcorrespondingbilabel(allregionsframes,allregionpaths,map);
% % % save('D:\newregpath_all.mat', 'allregionpaths', 'correspondentPath', '-v7.3');
% % % % load('D:\newregpath_all.mat');
% % % correspondentlengthpath=Getcorrespondingbilabel(allregionsframes,allregionpaths,allregionpaths.totalLength);
% % %
% % % [flows,allregionsframes,normalisedsimilarities]=getMenu(cim,ucm2,flows,allregionsframes,normalisedsimilarities,allregionpaths,correspondentPath);
% % % frame=1;
% % % level=35;
% % % [frame,level]=getclusteringmenu(frame,level,cim,ucm2,flows,allregionsframes,allregionpaths,correspondentPath);
% % %
% % % minLength=2;
% % % load(filenames.filename_flows)
% % % filter_flow=false;
% % % [trajectories,mapPathToTrajectory]=Gettrajectories(ucm2,flows,allregionsframes,allregionpaths,minLength,filter_flow);
% % % save('D:\newtraj_all.mat', 'trajectories','mapPathToTrajectory','-v7.3');
% % % fprintf('Saved to file\n');

% % % %using normalised similarities and products (sums of logarithms) - normalised similarities do not
% % % %seem to work
% % % load(filenames.filename_allregionsframes_all)
% % % load(filenames.filename_normalisedsimilarities_all)
% % %
% % % forwardSimilarities=Invertsimilarities(normalisedsimilarities);
% % % chosenSimilars=Get_probability_paths(normalisedsimilarities); %equivalent of Getpaths, but initialising with 0 and taking sum of logarithms (product of probabilities)
% % % chosenforwardSimilars=Get_probability_paths(forwardSimilarities);
% % % noFrames=size(chosenforwardSimilars,2);
% % % for frame=1:fix(noFrames/2)
% % %     tmp=chosenforwardSimilars{frame};
% % %     chosenforwardSimilars{frame}=chosenforwardSimilars{noFrames-frame+1};
% % %     chosenforwardSimilars{noFrames-frame+1}=tmp;
% % % end
% % % allregionpaths=Get_bidirectional_dp_with_probabilities(chosenSimilars,chosenforwardSimilars,normalisedsimilarities); %equivalent of Getbidirectionalpaths,
% % %                                                                                %but initialising with 0 and taking sum of logarithms (product of probabilities)
% % % totPaths=numel(allregionpaths.totalLength);
% % %
% % % load(filenames.filename_map)
% % % % map=Scramble(totPaths);
% % %
% % % correspondentPath=Getcorrespondingbilabel(allregionsframes,allregionpaths,map);
% % % save('D:\newregpath_all.mat', 'allregionpaths', 'correspondentPath', '-v7.3');
% % % % load('D:\newregpath_all.mat');
% % % correspondentlengthpath=Getcorrespondingbilabel(allregionsframes,allregionpaths,allregionpaths.totalLength);
% % %
% % % [flows,allregionsframes,normalisedsimilarities]=getMenu(cim,ucm2,flows,allregionsframes,normalisedsimilarities,allregionpaths,correspondentPath);
% % % frame=1;
% % % level=35;
% % % [frame,level]=getclusteringmenu(frame,level,cim,ucm2,flows,allregionsframes,allregionpaths,correspondentPath);
% % %
% % % minLength=2;
% % % load(filenames.filename_flows)
% % % filter_flow=false;
% % % [trajectories,mapPathToTrajectory]=Gettrajectories(ucm2,flows,allregionsframes,allregionpaths,minLength,filter_flow);
% % % save('D:\newtraj_all.mat', 'trajectories','mapPathToTrajectory','-v7.3');
% % % fprintf('Saved to file\n');

%programs to run:
%forward/backward dynamic programming
% load D:\regframe_1to70.mat
% load D:\similars_1to70.mat
% forwardSimilarities=Invertsimilarities(similarities);
% chosenSimilars=Getpaths(similarities);
% chosenforwardSimilars=Getpaths(forwardSimilarities);
% noFrames=size(chosenforwardSimilars,2);
% for frame=1:fix(noFrames/2)
%     tmp=chosenforwardSimilars{frame};
%     chosenforwardSimilars{frame}=chosenforwardSimilars{noFrames-frame+1};
%     chosenforwardSimilars{noFrames-frame+1}=tmp;
% end
% % for frame=1:fix(noFrames/2) %reinvert the forwardSimilarities so as to have them in the right direction
% %     tmp=forwardSimilarities{frame};
% %     forwardSimilarities{frame}=forwardSimilarities{noFrames-frame+1};
% %     forwardSimilarities{noFrames-frame+1}=tmp;
% % end
% allregionpaths=Getbidirectionalpaths(chosenSimilars,chosenforwardSimilars,similarities);
% totPaths=numel(allregionpaths.totalLength);
% map=Scramble(totPaths);
% correspondentPath=Getcorrespondingbilabel(allregionsframes,allregionpaths,map);
% correspondentlengthpath=Getcorrespondingbilabel(allregionsframes,allregionpaths,allregionpaths.totalLength);
% load d:\filename.mat
% [flows,allregionsframes,similarities]=getMenu(cim,ucm2,flows,allregionsframes,similarities,allregionpaths,correspondentPath);
% usetotpaths=0;
% [flows,allregionsframes,similarities]=getMenu(cim,ucm2,flows,allregionsframes,similarities,allregionpaths,correspondentlengthpath,usetotpaths);

% %forward/backward dynamic programming with sums of logarithms
% forwardSimilarities=Invertsimilarities(similarities);
% chosenSimilars=Get_probability_paths(similarities); %equivalent of Getpaths, but initialising with 0 and taking sum of logarithms (product of probabilities)
% chosenforwardSimilars=Get_probability_paths(forwardSimilarities);
% noFrames=size(chosenforwardSimilars,2);
% for frame=1:fix(noFrames/2)
%     tmp=chosenforwardSimilars{frame};
%     chosenforwardSimilars{frame}=chosenforwardSimilars{noFrames-frame+1};
%     chosenforwardSimilars{noFrames-frame+1}=tmp;
% end
% allregionpaths=Get_bidirectional_dp_with_probabilities(chosenSimilars,chosenforwardSimilars,similarities); %equivalent of Getbidirectionalpaths,
%                                                                             %but initialising with 0 and taking sum of logarithms (product of probabilities)
% totPaths=numel(allregionpaths.totalLength);
% % load(filenames.filename_map)
% map=Scramble(totPaths);
% correspondentPath=Getcorrespondingbilabel(allregionsframes,allregionpaths,map);
% correspondentlengthpath=Getcorrespondingbilabel(allregionsframes,allregionpaths,allregionpaths.totalLength);

%backward dynamic programming
% load D:\regframe_1to70.mat
% load D:\similars_1to70.mat
% chosenSimilars=Getpaths(similarities);
% allregionpaths=Getmonodirectionalpaths(chosenSimilars);
% totPaths=numel(allregionpaths.totalLength);
% map=Scramble(totPaths);
% correspondentPath=Getcorrespondingbilabel(allregionsframes,allregionpaths,map);
% correspondentlengthpath=Getcorrespondingbilabel(allregionsframes,allregionpaths,allregionpaths.totalLength);
% load d:\filename.mat
% [flows,allregionsframes,similarities]=getMenu(cim,ucm2,flows,allregionsframes,similarities,allregionpaths,correspondentPath);
% usetotpaths=0;
% [flows,allregionsframes,similarities]=getMenu(cim,ucm2,flows,allregionsframes,similarities,allregionpaths,correspondentlengthpath,usetotpaths);


text:
"""The Person Finder data model, based on PFIF (http://zesty.ca/pfif)."""

__author__ = 'kpy@google.com (Ka-Ping Yee) and many other Googlers'

import datetime

from google.appengine.api import datastore_errors
from google.appengine.api import memcache
from google.appengine.ext import db
import indexing
import pfif
import prefix
import re
import sys
import utils

# The domain name of this application.  The application hosts multiple
# repositories, each at a subdomain of this domain.
HOME_DOMAIN = 'person-finder.appspot.com'

# ==== PFIF record IDs =====================================================

def is_original(subdomain, record_id):
    """Returns True if this is a record_id for an original record in the given
    subdomain (a record originally created in this subdomain's repository)."""
    try:
        domain, local_id = record_id.split('/', 1)
        return domain == subdomain + '.' + HOME_DOMAIN
    except ValueError:
        raise ValueError('%r is not a valid record_id' % record_id)

def is_clone(subdomain, record_id):
    """Returns True if this is a record_id for a clone record (a record created
    in another repository and copied into this one)."""
    return not is_original(subdomain, record_id)

def filter_by_prefix(query, key_name_prefix):
    """Filters a query for key_names that have the given prefix.  If root_kind
    is specified, filters the query for children of any entities that are of
    that kind with the given prefix; otherwise, the results are assumed to be
    top-level entities of the kind being queried."""
    root_kind = query._model_class.__name__
    min_key = db.Key.from_path(root_kind, key_name_prefix)
    max_key = db.Key.from_path(root_kind, key_name_prefix + u'\uffff')
    return query.filter('__key__ >=', min_key).filter('__key__ <=', max_key)

def get_properties_as_dict(db_obj):
    """Returns a dictionary containing all (dynamic)* properties of db_obj."""
    properties = dict((k, v.__get__(db_obj, db_obj.__class__)) for
                      k, v in db_obj.properties().iteritems() if
                      v.__get__(db_obj, db_obj.__class__))
    dynamic_properties = dict((prop, getattr(db_obj, prop)) for
                              prop in db_obj.dynamic_properties())
    properties.update(dynamic_properties)
    return properties

def clone_to_new_type(origin, dest_class, **kwargs):
    """Clones the given entity to a new entity of the type "dest_class".
    Optionally, pass in values to kwargs to update values during cloning."""
    vals = get_properties_as_dict(origin)
    vals.update(**kwargs)
    if hasattr(origin, 'record_id'):
        vals.update(record_id=origin.record_id)
    return dest_class(key_name=origin.key().name(), **vals)

# ==== Model classes =======================================================

# Every Person or Note entity belongs to a specific subdomain.  To partition
# the datastore, key names consist of the subdomain, a colon, and then the
# record ID.  Each subdomain appears to be a separate instance of the app
# with its own respository.

# Note that the repository subdomain doesn't necessarily have to match the
# domain in the record ID!  For example, a person record created at
# foo.person-finder.appspot.com would have a key name such as:
#
#     foo:foo.person-finder.appspot.com/person.234
#
# This record would be searchable only at foo.person-finder.appspot.com --
# each repository is independent.  Copying it to bar.person-finder.appspot.com
# would produce a clone record with the key name:
#
#     bar:foo.person-finder.appspot.com/person.234
#
# That is, the clone has the same record ID but a different subdomain.

class Subdomain(db.Model):
    """A separate grouping of Person and Note records.  This is a top-level
    entity, with no parent, whose existence just indicates the existence of
    a subdomain.  Key name: unique subdomain name.  In the UI, each subdomain
    appears to be an independent instance of the application."""
    # No properties for now; only the key_name is significant.

    @staticmethod
    def list():
        return [subdomain.key().name() for subdomain in Subdomain.all()]

class Base(db.Model):
    """Base class providing methods common to both Person and Note entities,
    whose key names are partitioned using the subdomain as a prefix."""

    # Even though the subdomain is part of the key_name, it is also stored
    # redundantly as a separate property so it can be indexed and queried upon.
    subdomain = db.StringProperty(required=True)

    # We can't use an inequality filter on expiry_date (together with other
    # inequality filters), so we use a periodic task to set the is_expired flag
    # on expired records, and filter using the flag.  Note that we must provide
    # a default value to ensure that all entities are eligible for filtering.
    # NOTE: is_expired should ONLY be modified in Person.put_expiry_flags().
    is_expired = db.BooleanProperty(required=False, default=False)

    @classmethod
    def all(cls, keys_only=False, filter_expired=True):
        """Returns a query for all records of this kind; by default this
        filters out the records marked as expired.

        Args:
          keys_only - If true, return only the keys.
          filter_expired - If true, omit records with is_expired == True.
        Returns:
          query - A Query object for the results.
        """
        query = super(Base, cls).all(keys_only=keys_only)
        if filter_expired:
            query.filter('is_expired =', False)
        return query

    @classmethod
    def all_in_subdomain(cls, subdomain, filter_expired=True):
        """Gets a query for all entities in a given subdomain's repository."""
        return cls.all(filter_expired=filter_expired).filter(
            'subdomain =', subdomain)

    def get_record_id(self):
        """Returns the record ID of this record."""
        subdomain, record_id = self.key().name().split(':', 1)
        return record_id
    record_id = property(get_record_id)

    def get_original_domain(self):
        """Returns the domain name of this record's original repository."""
        return self.record_id.split('/', 1)[0]
    original_domain = property(get_original_domain)

    def is_original(self):
        """Returns True if this record was created in this repository."""
        return is_original(self.subdomain, self.record_id)

    def is_clone(self):
        """Returns True if this record was copied from another repository."""
        return not self.is_original()

    @classmethod
    def get(cls, subdomain, record_id, filter_expired=True):
        """Gets the entity with the given record_id in a given repository."""
        record = cls.get_by_key_name(subdomain + ':' + record_id)
        if record:
            if not (filter_expired and record.is_expired):
                return record

    @classmethod
    def create_original(cls, subdomain, **kwargs):
        """Creates a new original entity with the given field values."""
        record_id = '%s.%s/%s.%d' % (
            subdomain, HOME_DOMAIN, cls.__name__.lower(), UniqueId.create_id())
        key_name = subdomain + ':' + record_id
        return cls(key_name=key_name, subdomain=subdomain, **kwargs)

    @classmethod
    def create_clone(cls, subdomain, record_id, **kwargs):
        """Creates a new clone entity with the given field values."""
        assert is_clone(subdomain, record_id)
        key_name = subdomain + ':' + record_id
        return cls(key_name=key_name, subdomain=subdomain, **kwargs)

    @classmethod
    def create_original_with_record_id(cls, subdomain, record_id, **kwargs):
        """Creates an original entity with the given record_id and field
        values, overwriting any existing entity with the same record_id.
        This should be rarely used in practice (e.g. for an administrative
        import into a home repository), hence the long method name."""
        key_name = subdomain + ':' + record_id
        return cls(key_name=key_name, subdomain=subdomain, **kwargs)

# All fields are either required, or have a default value.  For property
# types that have a false value, the default is the false value.  For types
# with no false value, the default is None.

class Pet(Base):
    """The datastore entity kind for storing a PFIF person record.  Never call
    Pet() directly; use Pet.create_clone() or Pet.create_original().

    Methods that start with "get_" return actual values or lists of values;
    other methods return queries or generators for values.
    """
    # If you add any new fields, be sure they are handled in wipe_contents().

    # entry_date should update every time a record is created or re-imported.
    entry_date = db.DateTimeProperty(required=True)
    expiry_date = db.DateTimeProperty(required=False)

    author_name = db.StringProperty(default='', multiline=True)
    author_email = db.StringProperty(default='')
    author_phone = db.StringProperty(default='')

    # source_date is the original creation time; it should not change.
    source_name = db.StringProperty(default='')
    source_date = db.DateTimeProperty()
    source_url = db.StringProperty(default='')

    # Extra information for pets
    pet_name = db.StringProperty()
    in_shelter = db.BooleanProperty()
    animal_type = db.StringProperty(required=True, choices=pfif.ANIMAL_TYPE_VALUES)
    animal_size = db.StringProperty(default='', choices=pfif.ANIMAL_SIZE_VALUES)
    characteristics = db.TextProperty(default='')
    last_seen_location = db.ListProperty(float)
    is_reported = db.BooleanProperty()
    registration_num = db.StringProperty()
    microchip_num = db.StringProperty()
    has_owner_tag = db.BooleanProperty()
    color = db.StringProperty()
    has_collar = db.BooleanProperty()
    has_leash = db.BooleanProperty()
    color_description = db.StringProperty()
    tail_length = db.StringProperty(default='', choices=pfif.TAIL_LENGTH_VALUES)
    paw_color = db.StringProperty()
    weight = db.FloatProperty() # Check that it is an integer
    is_spayed_neutered = db.BooleanProperty()
    can_shake_paw = db.BooleanProperty()
    shelter_name = db.StringProperty()
    shelter_phone_number = db.IntegerProperty()
    current_location = db.ListProperty(float)
    discovered_location = db.ListProperty(float)
    finder_contact_name =  db.StringProperty()
    finder_contact_email = db.StringProperty()

    # This reference points to a locally stored Photo entity.  ONLY set this
    # property when storing a new Photo object that is owned by this Person
    # record and can be safely deleted when the Person is deleted.
    photo = db.ReferenceProperty(default=None)

    # The following properties are not part of the PFIF data model; they are
    # cached on the Person for efficiency.

    # Value of the 'status' and 'source_date' properties on the Note
    # with the latest source_date with the 'status' field present.
    latest_status = db.StringProperty(default='')
    latest_status_source_date = db.DateTimeProperty()
    # Value of the 'found' and 'source_date' properties on the Note
    # with the latest source_date with the 'found' field present.
    latest_found = db.BooleanProperty()
    latest_found_source_date = db.DateTimeProperty()

    # Last write time of this Person or any Notes on this Person.
    # This reflects any change to the Person page.
    last_modified = db.DateTimeProperty(auto_now=True)

    # attributes used by indexing.py
    names_prefixes = db.StringListProperty()
    _fields_to_index_properties = ['first_name', 'last_name']
    _fields_to_index_by_prefix_properties = ['first_name', 'last_name']

    @staticmethod
    def past_due_records():
        """Returns a query for all Person records with expiry_date in the past,
        regardless of their is_expired flags."""
        return Pet.all(filter_expired=False).filter(
            'expiry_date <=', utils.get_utcnow())

    def get_person_record_id(self):
        return self.record_id
    person_record_id = property(get_person_record_id)

    def get_notes(self, filter_expired=True):
        """Returns a list of all the Notes on this Person, omitting expired
        Notes by default."""
        return Note.get_by_person_record_id(
            self.subdomain, self.record_id, filter_expired=filter_expired)

    def get_subscriptions(self, subscription_limit=200):
        """Retrieves a list of all the Subscriptions for this Person."""
        return Subscription.get_by_person_record_id(
            self.subdomain, self.record_id, limit=subscription_limit)

    def get_linked_persons(self):
        """Retrieves the Persons linked (as duplicates) to this Person."""
        linked_persons = []
        for note in self.get_notes():
            person = Pet.get(self.subdomain, note.linked_person_record_id)
            if person:
                linked_persons.append(person)
        return linked_persons

    def get_associated_emails(self):
        """Gets all the e-mail addresses to notify when significant things
        happen to this Person record."""
        email_addresses = set([note.author_email for note in self.get_notes()])
        email_addresses.add(self.author_email)
        return email_addresses

    def put_expiry_flags(self):
        """Updates the is_expired flags on this Person and related Notes to
        make them consistent with the expiry_date on this Person, and commits
        these changes to the datastore."""

        now = utils.get_utcnow()
        expired = self.expiry_date and now >= self.expiry_date
        if self.is_expired != expired:
            # NOTE: This should be the ONLY code that modifies is_expired.
            self.is_expired = expired

            # If the record is expiring (being replaced with a placeholder,
            # see http://zesty.ca/pfif/1.3/#data-expiry) or un-expiring (being
            # restored from deletion), we want the source_date and entry_date
            # updated so downstream clients will see this as the newest state.
            self.source_date = now
            self.entry_date = now

            # All the Notes on the Person also expire or unexpire, to match.
            notes = self.get_notes(filter_expired=False)
            for note in notes:
                note.is_expired = expired

            # Store these changes in the datastore.
            db.put(notes + [self])
            # TODO(lschumacher): photos don't have expiration currently.

    def wipe_contents(self):
        """Sets all the content fields to None (leaving timestamps and the
        expiry flag untouched), stores the empty record, and permanently
        deletes any related Notes and Photo.  Call this method ONLY on records
        that have already expired."""

        # We rely on put_expiry_flags to have properly set the source_date,
        # entry_date, and is_expired flags on Notes, as necessary.
        assert self.is_expired

        # Delete all related Notes (they will have is_expired == True by now).
        db.delete(self.get_notes(filter_expired=False))
        if self.photo:
            db.delete(self.photo)  # Delete the locally stored Photo, if any.

        for name, property in self.properties().items():
            # Leave the subdomain, is_expired flag, and timestamps untouched.
            if name not in ['subdomain', 'is_expired',
                            'source_date', 'entry_date', 'expiry_date']:
                setattr(self, name, property.default)
        self.put()  # Store the empty placeholder record.

    def update_from_note(self, note):
        """Updates any necessary fields on the Person to reflect a new Note."""
        # We want to transfer only the *non-empty, newer* values to the Person.
        if note.found is not None:  # for boolean, None means unspecified
            # datetime stupidly refuses to compare to None, so check for None.
            if (self.latest_found_source_date is None or
                note.source_date >= self.latest_found_source_date):
                self.latest_found = note.found
                self.latest_found_source_date = note.source_date
        if note.status:  # for string, '' means unspecified
            if (self.latest_status_source_date is None or
                note.source_date >= self.latest_status_source_date):
                self.latest_status = note.status
                self.latest_status_source_date = note.source_date

    def update_index(self, which_indexing):
        #setup new indexing
        if 'new' in which_indexing:
            indexing.update_index_properties(self)
        # setup old indexing
        if 'old' in which_indexing:
            prefix.update_prefix_properties(self)

#old indexing
prefix.add_prefix_properties(
    Pet, 'first_name', 'last_name', 'home_street', 'home_neighborhood',
    'home_city', 'home_state', 'home_postal_code')

class Note(Base):
    """The datastore entity kind for storing a PFIF note record.  Never call
    Note() directly; use Note.create_clone() or Note.create_original()."""

    FETCH_LIMIT = 200

    # The entry_date should update every time a record is re-imported.
    entry_date = db.DateTimeProperty(required=True)

    person_record_id = db.StringProperty(required=True)

    # Use this field to store the person_record_id of a duplicate Person entry.
    linked_person_record_id = db.StringProperty(default='')

    author_name = db.StringProperty(default='', multiline=True)
    author_email = db.StringProperty(default='')
    author_phone = db.StringProperty(default='')

    # source_date is the original creation time; it should not change.
    source_date = db.DateTimeProperty()

    status = db.StringProperty(default='', choices=pfif.NOTE_STATUS_VALUES)
    found = db.BooleanProperty()
    email_of_found_person = db.StringProperty(default='')
    phone_of_found_person = db.StringProperty(default='')
    last_known_location = db.StringProperty(default='')
    text = db.TextProperty(default='')

    # True if the note has been marked as spam. Will cause the note to be
    # initially hidden from display upon loading a record page.
    hidden = db.BooleanProperty(default=False)

    def get_note_record_id(self):
        return self.record_id
    note_record_id = property(get_note_record_id)

    @staticmethod
    def get_by_person_record_id(
        subdomain, person_record_id, filter_expired=True):
        """Gets a list of all the Notes on a Person, ordered by source_date."""
        return list(Note.generate_by_person_record_id(
            subdomain, person_record_id, filter_expired))

    @staticmethod
    def generate_by_person_record_id(
        subdomain, person_record_id, filter_expired=True):
        """Generates all the Notes on a Person record ordered by source_date."""
        query = Note.all_in_subdomain(subdomain, filter_expired=filter_expired
            ).filter('person_record_id =', person_record_id
            ).order('source_date')
        notes = query.fetch(Note.FETCH_LIMIT)
        while notes:
            for note in notes:
                yield note
            query.with_cursor(query.cursor())  # Continue where fetch left off.
            notes = query.fetch(Note.FETCH_LIMIT)

class Photo(db.Model):
    """An entity kind for storing uploaded photos."""
    bin_data = db.BlobProperty()
    date = db.DateTimeProperty(auto_now_add=True)

    def get_url(self, handler):
        return handler.get_url('/photo', scheme='https', id=str(self.id()))

class Authorization(db.Model):
    """Authorization tokens.  Key name: subdomain + ':' + auth_key."""

    # Even though the subdomain is part of the key_name, it is also stored
    # redundantly as a separate property so it can be indexed and queried upon.
    subdomain = db.StringProperty(required=True)

    # If this field is non-empty, this authorization token allows the client
    # to write records with this original domain.
    domain_write_permission = db.StringProperty()

    # If this flag is true, this authorization token allows the client to read
    # non-sensitive fields (i.e. filtered by utils.filter_sensitive_fields).
    read_permission = db.BooleanProperty()

    # If this flag is true, this authorization token allows the client to read
    # all fields (i.e. not filtered by utils.filter_sensitive_fields).
    full_read_permission = db.BooleanProperty()

    # If this flag is true, this authorization token allows the client to use
    # the search API and return non-sensitive fields (i.e. filtered
    # by utils.filter_sensitive_fields).
    search_permission = db.BooleanProperty()

    # Bookkeeping information for humans, not used programmatically.
    contact_name = db.StringProperty()
    contact_email = db.StringProperty()
    organization_name = db.StringProperty()

    @classmethod
    def get(cls, subdomain, key):
        """Gets the Authorization entity for a subdomain and key."""
        key_name = subdomain + ':' + key
        return cls.get_by_key_name(key_name)

    @classmethod
    def create(cls, subdomain, key, **kwargs):
        """Creates an Authorization entity for a given subdomain and key."""
        key_name = subdomain + ':' + key
        return cls(key_name=key_name, subdomain=subdomain, **kwargs)

class Secret(db.Model):
    """A place to store application-level secrets in the database."""
    secret = db.BlobProperty()

class Counter(db.Expando):
    """Counters hold partial and completed results for ongoing counting tasks.
    To see how this is used, check out tasks.py.  A single Counter object can
    contain several named accumulators.  Typical usage is to scan for entities
    in order by __key__, update the accumulators for each entity, and save the
    partial counts when the time limit for a request is reached.  The last
    scanned key is saved in last_key so the next request can pick up the scan
    where the last one left off.  A non-empty last_key means a scan is not
    finished; when a scan is done, last_key should be set to ''."""
    timestamp = db.DateTimeProperty(auto_now=True)
    scan_name = db.StringProperty()
    subdomain = db.StringProperty()
    last_key = db.StringProperty(default='')  # if non-empty, count is partial

    # Each Counter also has a dynamic property for each accumulator; all such
    # properties are named "count_" followed by a count_name.

    def get(self, count_name):
        """Gets the specified accumulator from this counter object."""
        return getattr(self, 'count_' + count_name, 0)

    def increment(self, count_name):
        """Increments the given accumulator on this Counter object."""
        prop_name = 'count_' + count_name
        setattr(self, prop_name, getattr(self, prop_name, 0) + 1)

    @classmethod
    def get_count(cls, subdomain, name):
        """Gets the latest finished count for the given subdomain and name.
        'name' should be in the format scan_name + '.' + count_name."""
        scan_name, count_name = name.split('.')
        counter_key = subdomain + ':' + scan_name

        # Get the counts from memcache, loading from datastore if necessary.
        counter_dict = memcache.get(counter_key)
        if not counter_dict:
            try:
                # Get the latest completed counter with this scan_name.
                counter = cls.all().filter('subdomain =', subdomain
                                  ).filter('scan_name =', scan_name
                                  ).filter('last_key =', ''
                                  ).order('-timestamp').get()
            except datastore_errors.NeedIndexError:
                # Absurdly, it can take App Engine up to an hour to build an
                # index for a kind that has zero entities, and during that time
                # all queries fail.  Catch this error so we don't get screwed.
                counter = None

            counter_dict = {}
            if counter:
                # Cache the counter's contents in memcache for one minute.
                counter_dict = dict((name[6:], getattr(counter, name))
                                    for name in counter.dynamic_properties()
                                    if name.startswith('count_'))
                memcache.set(counter_key, counter_dict, 60)

        # Get the count for the given count_name.
        return counter_dict.get(count_name, 0)

    @classmethod
    def all_finished_counters(cls, subdomain, scan_name):
        """Gets a query for all finished counters for the specified scan."""
        return cls.all().filter('subdomain =', subdomain
                       ).filter('scan_name =', scan_name
                       ).filter('last_key =', '')

    @classmethod
    def get_unfinished_or_create(cls, subdomain, scan_name):
        """Gets the latest unfinished Counter entity for the given subdomain
        and scan_name.  If there is no unfinished Counter, create a new one."""
        counter = cls.all().filter('subdomain =', subdomain
                          ).filter('scan_name =', scan_name
                          ).order('-timestamp').get()
        if not counter or not counter.last_key:
            counter = Counter(subdomain=subdomain, scan_name=scan_name)
        return counter

class UserActionLog(db.Model):
    """Logs user actions and their reasons."""
    time = db.DateTimeProperty(required=True)
    action = db.StringProperty(
        required=True, choices=['delete', 'restore', 'hide', 'unhide'])
    entity_kind = db.StringProperty(required=True)
    entity_key_name = db.StringProperty(required=True)
    reason = db.StringProperty()  # should be present when action is 'delete'

    @classmethod
    def put_new(cls, action, entity, reason=''):
        cls(time=utils.get_utcnow(), action=action, entity_kind=entity.kind(),
            entity_key_name=entity.key().name(), reason=reason).put()

class Subscription(db.Model):
    """Subscription to notifications when a note is added to a person record"""
    subdomain = db.StringProperty(required=True)
    person_record_id = db.StringProperty(required=True)
    email = db.StringProperty(required=True)
    language = db.StringProperty(required=True)
    timestamp = db.DateTimeProperty(auto_now_add=True)

    @staticmethod
    def create(subdomain, record_id, email, language):
        """Creates a new Subscription"""
        key_name = '%s:%s:%s' % (subdomain, record_id, email)
        return Subscription(key_name=key_name, subdomain=subdomain,
                            person_record_id=record_id,
                            email=email, language=language)

    @staticmethod
    def get(subdomain, record_id, email):
        """Gets the entity with the given record_id in a given repository."""
        key_name = '%s:%s:%s' % (subdomain, record_id, email)
        return Subscription.get_by_key_name(key_name)

    @staticmethod
    def get_by_person_record_id(subdomain, person_record_id, limit=200):
        """Retrieve subscriptions for a person record."""
        query = Subscription.all().filter('subdomain =', subdomain)
        query = query.filter('person_record_id =', person_record_id)
        return query.fetch(limit)

class StaticSiteMapInfo(db.Model):
    """Holds static sitemaps file info."""
    static_sitemaps = db.StringListProperty()
    static_sitemaps_generation_time = db.DateTimeProperty(required=True)
    shard_size_seconds = db.IntegerProperty(default=90)

class SiteMapPingStatus(db.Model):
    """Tracks the last shard index that was pinged to the search engine."""
    search_engine = db.StringProperty(required=True)
    shard_index = db.IntegerProperty(default=-1)

class UniqueId(db.Model):
    """This entity is used just to generate unique numeric IDs."""
    @staticmethod
    def create_id():
        """Gets an integer ID that is guaranteed to be different from any ID
        previously returned by this static method."""
        unique_id = UniqueId()
        unique_id.put()
        return unique_id.key().id()


text:
require 'bodega'
require 'bodega/payment_method/base'
require 'active_support/core_ext/string/inflections'

describe Bodega::PaymentMethod::Base do
  describe ".options" do
    it "defines options on the Bodega configuration instance" do
      described_class.options(:a, :b, :z)
      Bodega.config.base.should_not be_nil
    end
  end
end


text:
StartTest(function(outerT) {

    outerT.testExtJS(function (t) {

        var viewport        = new Ext.Viewport({
            id              : 'viewport',

            items           : [
                {
                    id              : 'panel1',
                    title           : 'foo1',
                    height          : 50,
                    html            : '

'
                },
                {
                    id              : 'panel2',
                    title           : 'foo2',
                    html            : '

',

                    height          : 50,

                    items           : [
                        {
                            xtype           : 'button',
                            text            : 'test',
                            id              : 'test-button'
                        }
                    ]
                },
                {
                    xtype           : 'textfield',
                    cls             : 'test-field',
                    id              : 'test-field'
                }
            ]
        })

        t.is(t.normalizeElement('>>#panel1'), Ext.getCmp('panel1').body.dom, "Normalize element accept component query");
        t.is(t.normalizeElement('body'), document.body, 'body element is found with CSS selector');

        t.throwsOk(function () {
            t.compositeQuery('panel[title=foo]')
        }, '', 'Invalid composite query selector: panel[title=foo]')

        t.throwsOk(function () {
            t.compositeQuery('panel[title=barbaz] => div.quix', null, false)
        }, '', 'Component query selector should return at least one component')

        t.isDeeply(
            t.compositeQuery('viewport => div.quix'),
            [ Ext.get('test_div1').dom, Ext.get('test_div2').dom ],
            'Found the div with class `quix` inside of whole viewport'
        )

        t.isDeeply(
            t.compositeQuery('panel[title=foo1] => div.quix'),
            [ Ext.get('test_div1').dom ],
            'Found the div with class `quix` only inside of panel1'
        )

        t.isDeeply(
            t.compositeQuery('panel[title=foo1] => div.quix', Ext.getCmp('viewport')),
            [ Ext.get('test_div1').dom ],
            'Same result with specified root'
        )

        t.isDeeply(
            t.compositeQuery('panel[title=foo1] => div.quix', Ext.getCmp('panel2'), true),
            [],
            'Not found any results with `panel2` as root'
        )

        t.willFireNTimes(Ext.getCmp('test-button'), 'click', 1, 'A single click on this button should be detected')

        // not using `t.chain` on purpose - to verify that usual methods can accept the composite query
        t.type('viewport => .test-field input', 'Some text', function () {

            t.is(Ext.getCmp('test-field').getValue(), 'Some text', 'The result of the composite query, passed as argument to the `t.type` is correct')

            t.click('panel[title="foo2"] => .x-btn')
        })
    });
});


text:
require 'spec_helper'

include OpenEvaluation

describe Evaluation do
  let(:now) { 0.seconds.from_now } # so we get an ActiveSupport::TimeWithZone back

  describe "validations" do
    it "allows admins to set the user" do
      evaluation = Evaluation.new({user: build_stubbed(:user), doi: "asdf"}, as: :admin)
      expect(evaluation).to be_valid
    end
    it "allows admins to set the DOI" do
      evaluation = Evaluation.new({doi: "asdf", user: build_stubbed(:user)}, as: :admin)
      expect(evaluation).to be_valid
    end
    it "requires a user" do
      evaluation = Evaluation.new
      expect(evaluation).to_not be_valid
      expect(evaluation).to have(1).error_on(:user)
    end
    it "requires a DOI" do
      evaluation = Evaluation.new
      expect(evaluation).to_not be_valid
      expect(evaluation).to have(1).error_on(:doi)
    end

    describe "no conflict flag" do
      context "on create" do
        it "does not validate the no conflict flag" do
          evaluation = Evaluation.new
          evaluation.user = build_stubbed(:user)
          evaluation.doi = "asdf"

          expect(evaluation).to be_valid
        end
      end
    end
  end

  describe "::search" do

    before do
      Evaluation.delete_all
    end

    it "should find items matching the email" do
      e1 = create :evaluation, user:create(:user, email:'test1@email.com')
      e2 = create :evaluation

      expect( Evaluation.search('test1') ).to eq( [ e1 ] )
    end

    it "email matching should be case insensitive" do
      e1 = create :evaluation, user:create(:user, email:'Test1@email.com')
      e2 = create :evaluation

      expect( Evaluation.search('tEsT1') ).to eq( [ e1 ] )
    end

    it "should find items matching the name" do
      e1 = create :evaluation, user:create(:user, name:'Fred Flintstone')
      e2 = create :evaluation

      expect( Evaluation.search('Fred') ).to eq( [ e1 ] )
    end

    it "name matching should be case insensitive" do
      e1 = create :evaluation, user:create(:user, name:'Fred Flintstone')
      e2 = create :evaluation

      expect( Evaluation.search('freD') ).to eq( [ e1 ] )
    end

    it "should find items matching the title" do
      e1 = create :evaluation, article_title:'abcd'
      e2 = create :evaluation

      expect( Evaluation.search('abcd') ).to eq( [ e1 ] )
    end

    it "title matching should be case insensitive" do
      e1 = create :evaluation, article_title:'ABcd'
      e2 = create :evaluation

      expect( Evaluation.search('abCD') ).to eq( [ e1 ] )
    end

  end

  describe "#create" do
    it "sends the onboarding email to the researcher" do
      researcher = create(:user)
      mailer_stub = stub()
      mailer_stub.stub(:deliver)
      UserMailer.should_receive(:account_created_email).with(researcher, 'faked', 'a subject') { mailer_stub }

      evaluation = build(:evaluation, user: researcher, onboarding_content: 'faked', onboarding_subject:'a subject')
      evaluation.save!
    end
    it "persists the onboarding content" do
      researcher = create(:user)
      evaluation = build(:evaluation, user: researcher, onboarding_content: "This is my absolutely important email content")
      evaluation.save!
      reloaded_evaluation = Evaluation.find(evaluation.id)
      expect(reloaded_evaluation.onboarding_content).to be_present
      expect(reloaded_evaluation.onboarding_content).to match /absolutely important/
    end
    it "does not set evaluation_started_at" do
      evaluation = Evaluation.new
      evaluation.user = create(:user)
      evaluation.doi = "asdf"
      evaluation.save!

      evaluation.reload
      expect(evaluation.evaluation_started_at).to be_nil
    end
    it "does not set evaluation_saved_at" do
      evaluation = Evaluation.new
      evaluation.user = create(:user)
      evaluation.doi = "asdf"
      evaluation.save!

      evaluation.reload
      expect(evaluation.evaluation_saved_at).to be_nil
    end

    describe "doi api" do
      before do
        Evaluation.any_instance.unstub(:retrieve_doi_metadata)
        @crossref_response_stub = mock_metadata
      end
      it "queries the crossref api" do
        evaluation = Evaluation.new
        evaluation.user = create(:user)
        evaluation.doi = "asdf"

        Crossref.any_instance.should_receive(:doi).with("asdf").and_return(@crossref_response_stub)
        evaluation.save!
      end
      it "strips doi: if present" do
        evaluation = Evaluation.new
        evaluation.user = create(:user)
        evaluation.doi = "doi:somenumber"

        Crossref.any_instance.should_receive(:doi).with("somenumber").and_return(@crossref_response_stub)
        evaluation.save!
      end
      it "stores the crossref journal metadata" do
        Crossref.any_instance.stub(:doi).and_return(@crossref_response_stub)
        evaluation = Evaluation.new
        evaluation.user = create(:user)
        evaluation.doi = "fake"
        evaluation.save!

        expect(evaluation.journal_title).to eq "JOURNAL TITLE"
        expect(evaluation.article_title).to eq "ARTICLE TITLE"
        contributors = evaluation.contributors.split(", ")
        expect(evaluation.contributors).to include("My Face")
        expect(evaluation.contributors).to include("Your Face")
        expect(evaluation.resource).to eq "http://www.google.com"
        expect(evaluation.publication_date.year).to eq 2010
        expect(evaluation.publication_date.month).to eq 5
        expect(evaluation.publication_date.day).to eq 1
      end
      it "stores the stripped version of the doi" do
        Crossref.any_instance.stub(:doi).and_return(@crossref_response_stub)
        evaluation = Evaluation.new
        evaluation.user = create(:user)
        evaluation.doi = "doi:notreal"
        evaluation.save!

        expect(evaluation.doi).to eq "notreal"
      end
    end
  end

  describe "#update" do
    before do
      @evaluation = Evaluation.new
      @evaluation.user = create(:user)
      @evaluation.doi = "asdf"
      @evaluation.save!
    end
    it "allows normal user to update evaluation fields" do
      all_attrs = attributes_for(:completed_evaluation)
      attrs = all_attrs.except(
        :user, :doi, :research_networks, :created_at, :researcher_logged_in_at, :evaluation_started_at,
        :evaluation_saved_at, :evaluation_completed_at)

      @evaluation.assign_attributes(attrs)
      @evaluation.save!

      expect(@evaluation.pi_curious_text).to_not be_blank
    end

    context "do_not_update_evaluation_saved_at not set" do
      it "updates the evaluation_saved_at" do
        Timecop.freeze do
          evaluation = create(:evaluation)
          expect {
            evaluation.save
          }.to change(evaluation, :evaluation_saved_at).from(nil).to(now)
        end
      end
    end

    context "do_not_update_evaluation_saved_at set" do
      it "does not update the evaluation_saved_at" do
        evaluation = create(:evaluation)
        evaluation.do_not_update_evaluation_saved_at = true

        expect {
          evaluation.save
        }.to_not change(evaluation, :evaluation_saved_at)
      end
    end

    context "all ratings have been set" do
      context "completed_at is not set" do
        it "sets the evaluation_completed_at" do
          evaluation = create(:completed_evaluation, pi_level: nil, evaluation_completed_at: nil)
          evaluation.pi_level = Evaluation.pi_level_values[0]
          expect {
            evaluation.save
          }.to change(evaluation, :evaluation_completed_at)
        end
      end

      context "completed_at is set" do
        it "does not update the evaluation_completed_at" do
          evaluation = create(:evaluation, evaluation_completed_at: 2.days.ago)
          evaluation.touch
          expect {
            evaluation.save
          }.to_not change(evaluation, :evaluation_completed_at)
        end
      end
    end

    context "at least one rating is not set" do
      it "does not set the evaluation_completed_at" do
        evaluation = create(:evaluation, pi_level: nil)
        evaluation.touch
        expect {
          evaluation.save
        }.to_not change(evaluation, :evaluation_completed_at)
      end
    end

  end

  describe "#researcher_names" do
    it "returns a comma-delimited list" do
      evaluation = Evaluation.new
      expect(evaluation.researcher_names).to be_a String
      expect(evaluation.researcher_names).to be_empty
    end
    it "includes the name from each associated research network" do
      evaluation = create(:evaluation, research_networks: [build(:research_network, name: "ABC"), build(:research_network, name: "XYZ")])
      expect(evaluation.research_networks).to be_present

      expect(evaluation.researcher_names).to include("ABC")
      expect(evaluation.researcher_names).to include("XYZ")
    end
  end

  describe "#researcher_names=" do
    before do
      @evaluation = Evaluation.new
      expect(@evaluation.research_networks).to be_empty
    end
    it "builds an associated research network" do
      @evaluation.researcher_names = "my name"
      expect(@evaluation).to have(1).research_networks
      expect(@evaluation.research_networks.last).to_not be_persisted
    end
    it "sets the name on the research network record" do
      @evaluation.researcher_names = "yours"
      expect(@evaluation.research_networks.last.name).to eq "yours"
    end
    it "associates the research network to the evaluation" do
      evaluation = create(:completed_evaluation, research_networks: [])
      expect(evaluation).to have(0).research_networks
      evaluation.researcher_names = "roses"
      expect(evaluation.research_networks.last.evaluation_id).to eq evaluation.id
    end

    context "multiple researcher names" do
      it "builds a research network for each name" do
        evaluation = create(:completed_evaluation, research_networks: [])
        evaluation.researcher_names = "buster, panda"
        expect {
          evaluation.save
        }.to change(evaluation.research_networks, :count).by(2)

        names = evaluation.research_networks.map(&:name)
        expect(names).to include("buster")
        expect(names).to include("panda")
      end
      it "trims the whitespace" do
        evaluation = create(:completed_evaluation)
        evaluation.researcher_names = "one with a space,   another    "
        evaluation.save
        expect(evaluation.research_networks.map(&:name).sort).to eq ["another", "one with a space"]
      end
    end

  end

  describe "milestones" do
    let(:evaluation) { create(:evaluation) }

    describe "reminder sent" do
      describe "#reminder_sent!" do
        it "updates the reminder_sent_at" do
          Timecop.freeze do
            expect {
              evaluation.reminder_sent!
            }.to change(evaluation, :reminder_sent_at).from(nil).to(now)
          end
        end
        it "does not cause the evaluation_saved_at to be set" do
          expect {
            evaluation.reminder_sent!
          }.to_not change(evaluation, :evaluation_saved_at)
        end
      end
    end

    describe "researcher logged in" do
      describe "#researcher_logged_in!" do
        it "updates the researcher_logged_in_at" do
          Timecop.freeze do
            expect {
              evaluation.researcher_logged_in!
            }.to change(evaluation, :researcher_logged_in_at).from(nil).to(now)
          end
        end
        it "does not cause the evaluation_saved_at to be set" do
          expect {
            evaluation.researcher_logged_in!
          }.to_not change(evaluation, :evaluation_saved_at)
        end
      end
    end

    describe "evaluation started" do
      describe "#evaluation_started!" do
        it "updates the evaluation_started_at" do
          Timecop.freeze do
            expect {
              evaluation.evaluation_started!
            }.to change(evaluation, :evaluation_started_at).from(nil).to(now)
          end
        end
        it "does not cause the evaluation_saved_at to be set" do
          expect {
            evaluation.evaluation_started!
          }.to_not change(evaluation, :evaluation_saved_at)
        end
      end
    end

    describe "evaluation saved" do
      describe "#evaluation_saved!" do
        it "updates the evaluation_saved_at" do
          Timecop.freeze do
            expect {
              evaluation.evaluation_saved!
            }.to change(evaluation, :evaluation_saved_at).from(nil).to(now)
          end
        end
      end
    end

    describe "evaluation closed..."

    describe "evaluation completed with contact info..."

    describe "#latest_milestone" do
      specify { expect(build(:evaluation).latest_milestone).to eq "Evaluation Created" }
      specify { expect(build(:logged_in_evaluation).latest_milestone).to eq "Researcher Logged In" }
      specify { expect(build(:started_evaluation).latest_milestone).to eq "Evaluation Started" }
      specify { expect(build(:saved_evaluation).latest_milestone).to eq "Evaluation Saved" }
      specify { expect(build(:completed_evaluation).latest_milestone).to eq "Evaluation Completed" }

      context "contact info exists" do
        context "evaluation completed" do
          it "returns Evaluation Completed with Contact Info" do
            evaluation = create(:completed_evaluation)
            contact = build(:contact, evaluation: evaluation)
            contact.save
            evaluation.reload
            expect(evaluation.latest_milestone).to eq "Evaluation Completed with Contact Info"
          end
        end

        context "evaluation incomplete" do
          it "returns evaluation saved" do
            evaluation = create(:saved_evaluation)
            contact = build(:contact, evaluation: evaluation)
            contact.save
            evaluation.reload
            expect(evaluation.latest_milestone).to eq "Evaluation Saved"
          end
        end
      end
    end

  end

  describe "#doi_metadata" do
    context "metadata is populated" do
      before do
        @evaluation = Evaluation.new
        @evaluation.journal_title = "Journal Name"
        @evaluation.article_title = "Some Article about XYZ"
        @evaluation.contributors = "Mom, Dad, Sis"
        @evaluation.resource = "http://www.google.com"
      end
      it "includes the article title" do
        expect(@evaluation.doi_metadata).to match "Some Article about XYZ"
      end
      it "includes a link to the resource" do
        expect(@evaluation.doi_metadata).to match "http://www.google.com"
      end
    end
    context "metadata is not present" do
      it "returns an empty string" do
        evaluation = Evaluation.new
        expect(evaluation.doi_metadata).to_not be_nil
        expect(evaluation.doi_metadata).to be_a String
        expect(evaluation.doi_metadata).to eq ""
      end
    end
  end

  describe "#send_onboarding_email" do
    it "replaces the login href with an evaluation-specific login url" do
      user = create :user
      default_content = UserMailer.account_created_email(user).html_part.body.to_s
      expect(default_content).to_not match /evaluation_id/
      mailer_stub = stub()
      mailer_stub.stub(:deliver)

      evaluation = Evaluation.new
      evaluation.user = user
      evaluation.doi = "asdf"
      evaluation.onboarding_content = default_content
      evaluation.onboarding_subject = 'a subject'

      UserMailer.should_receive(:account_created_email).with(instance_of(User), /evaluations\/start/, 'a subject').and_return(mailer_stub)
      evaluation.save!
    end

  end

  describe "#send_reminder" do
    before do
      @evaluation = create(:evaluation)
      @mailer_stub = stub()
      @mailer_stub.stub(:deliver)
    end
    it "calls the UserMailer method" do
      UserMailer.should_receive(:evaluation_reminder).and_return(@mailer_stub)
      @evaluation.send_reminder
    end
    it "delivers the email" do
      @mailer_stub.should_receive(:deliver)
      UserMailer.should_receive(:evaluation_reminder).and_return(@mailer_stub)

      @evaluation.send_reminder
    end
    it "passes the evaluation" do
      UserMailer.should_receive(:evaluation_reminder).with(@evaluation, anything).and_return(@mailer_stub)
      @evaluation.send_reminder
    end

    describe "reminder_sent_at" do
      it "calls reminder_sent!" do
        @evaluation.should_receive(:reminder_sent!)
        @evaluation.send_reminder
      end

      it "updates the evaluation's reminder_sent_at" do
        expect {
          @evaluation.send_reminder
        }.to change(@evaluation, :reminder_sent_at)
      end
    end

    context "no params" do
      it "sends the reminder with the default email content" do
        UserMailer.should_receive(:evaluation_reminder).with(@evaluation, nil).and_return(@mailer_stub)
        @evaluation.send_reminder
      end
    end

    context "with param" do
      it "sends the param to the user mailer" do
        UserMailer.should_receive(:evaluation_reminder).with(@evaluation, "my custom content").and_return(@mailer_stub)
        @evaluation.send_reminder("my custom content")
      end
    end

  end

  describe "#access_with_login_url" do

    it "should create a url" do
      eval = create :evaluation
      eval.id = '123'
      eval.user.login_hash='abcd-efgh'
      expect(eval.access_with_login_url).to match("/evaluations/start/00123-abcd-efgh")
    end

  end

end


text:
File.open("#{Nutrition::Engine.root}/lib/nutrition/data/SR28/FD_GROUP.txt", "r").each_line do |line|
  data = line.gsub(/~/,'').gsub(/\r\n/,'').split("^")
  food_group = Nutrition::FoodGroup.create(name:data.last)
  food_group.id = data[0]
  food_group.save

end
File.open("#{Nutrition::Engine.root}/lib/nutrition/data/SR28/NUTR_DEF.txt", "r").each_line do |line|
  data = line.force_encoding('iso-8859-1').encode('utf-8').gsub(/~/,'').gsub(/\r\n/,'').split("^")
  input = Hash[[:id, :unit, :label, :name, :significant_figures, :position].zip(data)]
  record = Nutrition::Nutrient.create( input )
  record.id = input[:id]
  record.save
end
File.open("#{Nutrition::Engine.root}/lib/nutrition/data/SR28/LANGDESC.txt", "r").each_line do |line|
  data = line.force_encoding('iso-8859-1').encode('utf-8').gsub(/~/,'').gsub(/\r\n/,'').split("^")
  input = Hash[[:legacy_id, :description].zip(data)]
  record = Nutrition::LangualFactor.create( input )
end
#
File.open("#{Nutrition::Engine.root}/lib/nutrition/data/SR28/FOOD_DES.txt", "r").each_line.with_index do |line, i|

    data = line.force_encoding('iso-8859-1').encode('utf-8').gsub(/~/,'').gsub(/\r\n/,'').split("^")
    input = Hash[[:id, :nutrition_food_group_id, :description, :summary, :aliases, :manufacturer, :is_complete, :waste_description, :waste_yield, :origin, :nitrogen_protein_yield, :protein_calorie_yield, :fat_calorie_yield, :carb_calorie_yield].zip(data)]
    input[:is_complete] = true if input[:is_complete] == "Y"
    record = Nutrition::Food.create( input )
    record.id = input[:id]
    record.save

end

File.open("#{Nutrition::Engine.root}/lib/nutrition/data/SR28/NUT_DATA.txt", "r").each_line do |line|
  data = line.force_encoding('iso-8859-1').encode('utf-8').gsub(/~/,'').gsub(/\r\n/,'').split("^")
  input = Hash[[:nutrition_food_id, :nutrition_nutrient_id,  :yield, :Num_Data_Pts, :Std_Error, :Src_Cd, :Deriv_Cd, :estimated_from, :is_fortified, :Num_Studies, :Min, :Max, :DF, :Low_EB, :Up_EB, :Stat_cmt,  :last_update, :CC ].zip(data)]
  record =  Nutrition::NutrientValue.create( input.select{|k, v| Nutrition::NutrientValue.attribute_names.include? k } )
end
@overflow = []

File.open("#{Nutrition::Engine.root}/lib/nutrition/data/SR28/WEIGHT.txt", "r").each_line do |line|
  data = line.force_encoding('iso-8859-1').encode('utf-8').gsub(/~/,'').gsub(/\r\n/,'').split("^")
  input = Hash[[:nutrition_food_id, :seq,  :quantity, :unit, :mass, :datapoints, :standard_deviation ].zip(data)]
  record = Nutrition::Mass.create(input) rescue( @overflow << input)
end

File.open("#{Nutrition::Engine.root}/lib/nutrition/data/SR28/LANGUAL.txt", "r").each_line do |line|

  data = line.force_encoding('iso-8859-1').encode('utf-8').gsub(/~/,'').gsub(/\r\n/,'').split("^")
  input = Hash[[:nutrition_food_id, :legacy_id ].zip(data)]
  food = Nutrition::Food.find input[:nutrition_food_id]
  lf = Nutrition::LangualFactor.where(legacy_id:input[:legacy_id]).first
  food.langual_factors << lf rescue nil
  food.save
end


text:
/** @module "iterable-query/fn" */

import { assert, ToPossiblyAsyncIterable } from "../internal";
import { AsyncQueryable } from "../types";
import { identity } from "./common";

/**
 * Computes the average for a series of numbers.
 * NOTE: If any element is not a `number`, this overload will throw.
 *
 * @param source An [[AsyncQueryable]] object.
 * @category Scalar
 */
export async function averageAsync(source: AsyncQueryable<number>): Promise<number>;
/**
 * Computes the average for a series of numbers.
 *
 * @param source An [[AsyncQueryable]] object.
 * @param elementSelector A callback used to convert a value in `source` to a number.
 * @category Scalar
 */
export async function averageAsync<T>(source: AsyncQueryable<T>, elementSelector: (element: T) => number | PromiseLike<number>): Promise<number>;
export async function averageAsync(source: AsyncQueryable<number>, elementSelector: (element: number) => number | PromiseLike<number> = identity): Promise<number> {
    assert.mustBeAsyncQueryable(source, "source");
    assert.mustBeFunction(elementSelector, "elementSelector");
    let sum = 0;
    let count = 0;
    for await (const element of ToPossiblyAsyncIterable(source)) {
        const value = elementSelector(element);
        const result = typeof value === "number" ? value : await value;
        assert.mustBeNumber(result);
        sum += result;
        count++;
    }
    return count > 0 ? sum / count : 0;
}


text:
package classesobjects

import org.scalatest.FlatSpec
import org.scalatest.Matchers

class ClassesFieldsMethods extends FlatSpec with Matchers {

  class SomeClass {
    var variable = 0
    val constant = 9
    private var variable2 = 0
  }

  "Fields" should "be public by default" in {

    val acc = new SomeClass

    acc.variable shouldBe 0

    acc.variable = 1
    acc.variable shouldBe 1

    assertDoesNotCompile("acc.variable2 = 23")
  }
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>类分层结构 (library API)</title>
<meta name="date" content="2016-02-12">
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">

您的浏览器已禁用 JavaScript。

跳过导航链接

概览

程序包

类

树

已过时

索引

帮助

上一个

下一个

框架

无框架

所有类

所有程序包的分层结构

程序包分层结构:

com.handmark.pulltorefresh.library,

com.handmark.pulltorefresh.library.extras,

com.handmark.pulltorefresh.library.internal

类分层结构

java.lang.Object

com.handmark.pulltorefresh.library.LoadingLayoutProxy (implements com.handmark.pulltorefresh.library.ILoadingLayout)

com.handmark.pulltorefresh.library.OverscrollHelper

com.handmark.pulltorefresh.library.extras.SoundPullEventListener<V> (implements com.handmark.pulltorefresh.library.PullToRefreshBase.OnPullEventListener<V>)

com.handmark.pulltorefresh.library.internal.Utils

android.view.View (implements android.view.accessibility.AccessibilityEventSource, android.graphics.drawable.Drawable.Callback, android.view.KeyEvent.Callback)

android.view.ViewGroup (implements android.view.ViewManager, android.view.ViewParent)

android.widget.AdapterView<T>

android.widget.AbsListView (implements android.widget.Filter.FilterListener, android.text.TextWatcher, android.view.ViewTreeObserver.OnGlobalLayoutListener, android.view.ViewTreeObserver.OnTouchModeChangeListener)

android.widget.ListView

com.handmark.pulltorefresh.library.PullToRefreshListView.InternalListView (implements com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor)

android.widget.FrameLayout

com.handmark.pulltorefresh.library.internal.IndicatorLayout (implements android.view.animation.Animation.AnimationListener)

com.handmark.pulltorefresh.library.internal.LoadingLayout (implements com.handmark.pulltorefresh.library.ILoadingLayout)

com.handmark.pulltorefresh.library.internal.FlipLoadingLayout

com.handmark.pulltorefresh.library.internal.RotateLoadingLayout

android.widget.LinearLayout

com.handmark.pulltorefresh.library.PullToRefreshBase<T> (implements com.handmark.pulltorefresh.library.IPullToRefresh<T>)

com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase<T> (implements android.widget.AbsListView.OnScrollListener)

com.handmark.pulltorefresh.library.PullToRefreshExpandableListView

com.handmark.pulltorefresh.library.PullToRefreshGridView

com.handmark.pulltorefresh.library.PullToRefreshListView

com.handmark.pulltorefresh.library.PullToRefreshHorizontalScrollView

com.handmark.pulltorefresh.library.PullToRefreshScrollView

com.handmark.pulltorefresh.library.PullToRefreshWebView

com.handmark.pulltorefresh.library.extras.PullToRefreshWebView2

com.handmark.pulltorefresh.library.internal.ViewCompat

接口分层结构

com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor

com.handmark.pulltorefresh.library.ILoadingLayout

com.handmark.pulltorefresh.library.IPullToRefresh<T>

com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener

com.handmark.pulltorefresh.library.PullToRefreshBase.OnPullEventListener<V>

com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener<V>

com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2<V>

枚举分层结构

java.lang.Object

java.lang.Enum<E> (implements java.lang.Comparable<T>, java.io.Serializable)

com.handmark.pulltorefresh.library.PullToRefreshBase.AnimationStyle

com.handmark.pulltorefresh.library.PullToRefreshBase.Mode

com.handmark.pulltorefresh.library.PullToRefreshBase.Orientation

com.handmark.pulltorefresh.library.PullToRefreshBase.State

跳过导航链接

概览

程序包

类

树

已过时

索引

帮助

上一个

下一个

框架

无框架

所有类


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

{% block head %}
    {% include "includes/head.html" %}

{% endblock %}

{% block page_title %}
  Apprenticeships
{% endblock %}

{% block content %}

{% include "includes/phase_banner_beta.html" %}

      {% include "includes/breadcrumbs/register-back.html" %}

{% include "includes/account-name-number.html" %}Date faff, fix screen

This is to fix the date faff problem by specifying a to date that this change will run until. Only for 'current' payment failure not the historic ones.

            <form action="reason">

            <fieldset>
              <legend>
                Projected start date
                 For example, 01 09 2017
              </legend>

                  Day
                  <input class="form-control" id="startDay" name="start-day" type="number" min="1" max="31" value="">

                  Month
                  <input class="form-control" id="dob-month" name="dob-month" type="number" pattern="[0-9]*" min="0" max="12">

                  Year
                  <input class="form-control" id="dob-year" name="dob-year" type="number" pattern="[0-9]*" min="0" max="2030">

            </fieldset>

            <fieldset>
              <legend>
                Projected finish date
                 For example, 01 09 2017
              </legend>

                  Day
                  <input class="form-control" id="startDay" name="start-day" type="number" min="1" max="31" value="">

                  Month
                  <input class="form-control" id="dob-month" name="dob-month" type="number" pattern="[0-9]*" min="0" max="12">

                  Year
                  <input class="form-control" id="dob-year" name="dob-year" type="number" pattern="[0-9]*" min="0" max="2030">

            </fieldset>

     <input class="button" id="saveAndContinue" type="submit" style="margin-top:35px" value="Continue">
  </form>

{% endblock %}


text:
namespace Eugenie.Data.Models
{
    using Newtonsoft.Json;

    public class ProductWithQuantity
    {
        [JsonIgnore]
        public int Id { get; set; }

        public decimal Quantity { get; set; }

        [JsonIgnore]
        public int ProductId { get; set; }

        public virtual Product Product { get; set; }
    }
}


text:
> Convert Markdown document to beautiful PDFs via LaTeX.

***

## Usage

    $ marktex file.tex
    <LaTeX source is printed...>

    $ marktex file.tex | xelatex -shell-escape
    $ open texput.pdf

## Features

### Supported Markdown

* Emphasis & Bold
* Lists
* Code Blocks & Spans
* Links (including autolinks)
* Block Quotes
* Horizontal Rules
* Local Images
* Strikethrough
* Superscript

### Unsupported Markdown (but coming soon!)

* Tables
* Remote Images