text:
''' Create rst documentation of the examples directory.

This uses screenshots in the screenshots_dir
(currently doc/sources/images/examples) along with source code and files
in the examples/ directory to create rst files in the generation_dir
(doc/sources/examples) gallery.rst, index.rst, and gen__*.rst

'''

import os
import re
from os.path import sep
from os.path import join as slash  # just like that name better
from os.path import dirname, abspath
from kivy.logger import Logger
import textwrap

# from here to the kivy top
base_dir = dirname(dirname(abspath(__file__)))
examples_dir = slash(base_dir, 'examples')
screenshots_dir = slash(base_dir, 'doc/sources/images/examples')
generation_dir = slash(base_dir, 'doc/sources/examples')

image_dir = "../images/examples/"  # relative to generation_dir
gallery_filename = slash(generation_dir, 'gallery.rst')

# Info is a dict built up from
# straight filename information, more from reading the docstring,
# and more from parsing the description text.  Errors are often
# shown by setting the key 'error' with the value being the error message.
#
# It doesn't quite meet the requirements for a class, but is a vocabulary
# word in this module.

def iter_filename_info(dir_name):
    """
    Yield info (dict) of each matching screenshot found walking the
    directory dir_name.  A matching screenshot uses double underscores to
    separate fields, i.e. path__to__filename__py.png as the screenshot for
    examples/path/to/filename.py.

    Files not ending with .png are ignored, others are either parsed or
    yield an error.

    Info fields 'dunder', 'dir', 'file', 'ext', 'source' if not 'error'
    """
    pattern = re.compile(r'^((.+)__(.+)__([^-]+))\.png')
    for t in os.walk(dir_name):
        for filename in t[2]:
            if filename.endswith('.png'):
                m = pattern.match(filename)
                if m is None:
                    yield {'error': 'png filename not following screenshot'
                                    ' pattern: {}'.format(filename)}
                else:
                    d = m.group(2).replace('__', sep)
                    yield {'dunder': m.group(1),
                           'dir': d,
                           'file': m.group(3),
                           'ext': m.group(4),
                           'source': slash(d, m.group(3) + '.' + m.group(4))
                           }

def parse_docstring_info(text):
    ''' parse docstring from text (normal string with '\n's) and return an info
    dict.  A docstring should the first triple quoted string, have a title
    followed by a line of equal signs, and then a description at
    least one sentence long.

    fields are 'docstring', 'title', and 'first_sentence' if not 'error'
    'first_sentence' is a single line without newlines.
    '''
    q = '\"\"\"|\'\'\''
    p = r'({})\s+([^\n]+)\s+\=+\s+(.*?)(\1)'.format(q)
    m = re.search(p, text, re.S)
    if m:
        comment = m.group(3).replace('\n', ' ')
        first_sentence = comment[:comment.find('.') + 1]
        return {'docstring': m.group(0), 'title': m.group(2),
                'description': m.group(3), 'first_sentence': first_sentence}
    else:
        return {'error': 'Did not find docstring with title at top of file.'}

def iter_docstring_info(dir_name):
    ''' Iterate over screenshots in directory, yield info from the file
     name and initial parse of the docstring.  Errors are logged, but
     files with errors are skipped.
    '''
    for file_info in iter_filename_info(dir_name):
        if 'error' in file_info:
            Logger.error(file_info['error'])
            continue
        source = slash(examples_dir, file_info['dir'],
                       file_info['file'] + '.' + file_info['ext'])
        if not os.path.exists(source):
            Logger.error('Screen shot references source code that does '
                         'not exist:  %s', source)
            continue
        with open(source) as f:
            text = f.read()
            docstring_info = parse_docstring_info(text)
            if 'error' in docstring_info:
                Logger.error(docstring_info['error'] + '  File: ' + source)
                continue  # don't want to show ugly entries
            else:
                file_info.update(docstring_info)
        yield file_info

def enhance_info_description(info, line_length=79):
    ''' Using the info['description'], add fields to info.

    info['files'] is the source filename and any filenames referenced by the
    magic words in the description, e.g. 'the file xxx.py' or
    'The image this.png'.  These are as written in the description, do
    not allow ../dir notation, and are relative to the source directory.

    info['enhanced_description'] is the description, as an array of
    paragraphs where each paragraph is an array of lines wrapped to width
    line_length.  This enhanced description include the rst links to
    the files of info['files'].
    '''

    # make text a set of long lines, one per paragraph.
    paragraphs = info['description'].split('\n\n')
    lines = [
        paragraph.replace('\n', '$newline$')
        for paragraph in paragraphs
    ]
    text = '\n'.join(lines)

    info['files'] = [info['file'] + '.' + info['ext']]
    regex = r'[tT]he (?:file|image) ([\w\/]+\.\w+)'
    for name in re.findall(regex, text):
        if name not in info['files']:
            info['files'].append(name)

    # add links where the files are referenced
    folder = '_'.join(info['source'].split(sep)[:-1]) + '_'
    text = re.sub(r'([tT]he (?:file|image) )([\w\/]+\.\w+)',
                  r'\1:ref:`\2 <$folder$\2>`', text)
    text = text.replace('$folder$', folder)

    # now break up text into array of paragraphs, each an array of lines.
    lines = [line.replace('$newline$', '\n') for line in text.split('\n')]
    paragraphs = [
        textwrap.wrap(line, line_length)
        # ignore wrapping if .. note:: or similar block
        if not line.startswith(' ') else [line]
        for line in lines
    ]
    info['enhanced_description'] = paragraphs

def get_infos(dir_name):
    ''' return infos, an array info dicts for each matching screenshot in the
    dir, sorted by source file name, and adding the field 'num' as he unique
    order in this array of dicts'.

    '''
    infos = [i for i in iter_docstring_info(dir_name)]
    infos.sort(key=lambda x: x['source'])
    for num, info in enumerate(infos):
        info['num'] = num
        enhance_info_description(info)
    return infos

def make_gallery_page(infos):
    ''' return string of the rst (Restructured Text) of the gallery page,
    showing information on all screenshots found.
    '''

    def a(s=''):
        ''' append formatted s to output, which will be joined into lines '''
        output.append(s.format(**info))

    def t(left='', right=''):
        ''' append left and right format strings into a table line. '''
        l = left.format(**info)
        r = right.format(**info)
        if len(l) > width1 or len(r) > width2:
            Logger.error('items to wide for generated table: "%s" and "%s"',
                         l, r)
            return
        output.append('| {0:{w1}} | {1:{w2}} |'
                      .format(l, r, w1=width1, w2=width2))

    gallery_top = '''
Gallery
-------

.. _Tutorials:  ../tutorials-index.html

.. container:: title

    This gallery lets you explore the many examples included with Kivy.
    Click on any screenshot to see the code.

This gallery contains:

    * Examples from the examples/ directory that show specific capabilities of
      different libraries and features of Kivy.
    * Demonstrations from the examples/demos/ directory that explore many of
      Kivy's abilities.

There are more Kivy programs elsewhere:

    * Tutorials_ walks through the development of complete Kivy applications.
    * Unit tests found in the source code under the subdirectory kivy/tests/
      can also be useful.

We hope your journey into learning Kivy is exciting and fun!

'''
    output = [gallery_top]

    for info in infos:
        a("\n.. |link{num}|  replace:: :doc:`{source}<gen__{dunder}>`")
        a("\n.. |pic{num}| image:: ../images/examples/{dunder}.png"
          "\n    :width:  216pt"
          "\n    :align:  middle"
          "\n    :target: gen__{dunder}.html")
        a("\n.. |title{num}|  replace:: **{title}**")

    # write the table
    width1, width2 = 20, 50  # not including two end spaces
    head = '+-' + '-' * width1 + '-+-' + '-' * width2 + '-+'
    a()
    a(head)

    for info in infos:
        t('| |pic{num}|', '| |title{num}|')
        t('| |link{num}|', '')
        paragraphs = info['description'].split("\n\n")
        for p in paragraphs:
            for line in textwrap.wrap(p, width2):
                t('', line)
            t()  # line between paragraphs
        t()
        a(head)
    return "\n".join(output) + "\n"

def make_detail_page(info):
    ''' return str of the rst text for the detail page of the file in info. '''

    def a(s=''):
        ''' append formatted s to output, which will be joined into lines '''
        output.append(s.format(**info))

    output = []
    a('{title}')
    a('=' * len(info['title']))
    a('\n.. |pic{num}| image:: /images/examples/{dunder}.png'
      '\n   :width: 50%'
      '\n   :align: middle')
    a('\n|pic{num}|')
    a()
    for paragraph in info['enhanced_description']:
        for line in paragraph:
            a(line)
        a()

    # include images
    last_lang = '.py'
    for fname in info['files']:
        full_name = slash(info['dir'], fname)
        ext = re.search(r'\.\w+$', fname).group(0)
        a('\n.. _`' + full_name.replace(sep, '_') + '`:')
        # double separator if building on windows (sphinx skips backslash)
        if '\\' in full_name:
            full_name = full_name.replace(sep, sep*2)

        if ext in ['.png', '.jpg', '.jpeg']:
            title = 'Image **' + full_name + '**'
            a('\n' + title)
            a('~' * len(title))
            a('\n.. image:: ../../../examples/' + full_name)
            a('    :align:  center')
        else:  # code
            title = 'File **' + full_name + '**'
            a('\n' + title)
            a('~' * len(title))
            if ext != last_lang and ext != '.txt':
                a('\n.. highlight:: ' + ext[1:])
                a('    :linenothreshold: 3')
                last_lang = ext
            # prevent highlight errors with 'none'
            elif ext == '.txt':
                a('\n.. highlight:: none')
                a('    :linenothreshold: 3')
                last_lang = ext
            a('\n.. include:: ../../../examples/' + full_name)
            a('    :code:')
    return '\n'.join(output) + '\n'

def write_file(name, s):
    ''' write the string to the filename '''
    with open(name, 'w') as f:
        f.write(s)

def make_index(infos):
    ''' return string of the rst for the gallery's index.rst file. '''
    start_string = '''
Gallery of Examples
===================

.. toctree::
    :maxdepth: 1

    gallery'''
    output = [start_string]
    for info in infos:
        output.append('    gen__{}'.format(info['dunder']))
    return '\n'.join(output) + '\n'

def write_all_rst_pages():
    ''' Do the main task of writing the gallery,
    detail, and index rst pages.
    '''
    infos = get_infos(screenshots_dir)
    s = make_gallery_page(infos)
    write_file(gallery_filename, s)

    for info in infos:
        s = make_detail_page(info)
        detail_name = slash(generation_dir,
                            'gen__{}.rst'.format(info['dunder']))
        write_file(detail_name, s)

    s = make_index(infos)
    index_name = slash(generation_dir, 'index.rst')
    write_file(index_name, s)
    Logger.info('gallery.py: Created gallery rst documentation pages.')

if __name__ == '__main__':
    write_all_rst_pages()


text:
package com.consol.citrus.admin.converter.endpoint;

import com.consol.citrus.model.config.ssh.SshClientModel;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Christoph Deppisch
 */
@Component
public class SshClientConverter extends AbstractEndpointConverter<SshClientModel> {

    @Override
    protected String getId(SshClientModel model) {
        return model.getId();
    }

    @Override
    protected String[] getRequiredFields() {
        return new String[] { "host", "port" };
    }

    @Override
    protected Map<String, Object> getDefaultValueMappings() {
        Map<String, Object> mappings = super.getDefaultValueMappings();
        mappings.put("host", "localhost");
        mappings.put("port", "2222");
        mappings.put("strictHostChecking", FALSE);
        return mappings;
    }

    @Override
    public Class<SshClientModel> getSourceModelClass() {
        return SshClientModel.class;
    }

    @Override
    public String getEndpointType() {
        return "ssh-client";
    }
}

redpajama_set_name: RedPajamaGithub


text:
#include <sys/types.h>

//
// Handy utility functions and portability code.
//
#ifndef _LIBS_ANDROID_FW_MISC_H
#define _LIBS_ANDROID_FW_MISC_H

namespace android {

/*
 * Some utility functions for working with files.  These could be made
 * part of a "File" class.
 */
typedef enum FileType {
    kFileTypeUnknown = 0,
    kFileTypeNonexistent,       // i.e. ENOENT
    kFileTypeRegular,
    kFileTypeDirectory,
    kFileTypeCharDev,
    kFileTypeBlockDev,
    kFileTypeFifo,
    kFileTypeSymlink,
    kFileTypeSocket,
} FileType;
/* get the file's type; follows symlinks */
FileType getFileType(const char* fileName);
/* get the file's modification date; returns -1 w/errno set on failure */
time_t getFileModDate(const char* fileName);

}; // namespace android

#endif // _LIBS_ANDROID_FW_MISC_H


text:
"""
flask_httpauth
==================

This module provides Basic and Digest HTTP authentication for Flask routes.

:copyright: (C) 2014 by Miguel Grinberg.
:license:   MIT, see LICENSE for more details.
"""
import hmac
from base64 import b64decode
from functools import wraps
from hashlib import md5
from random import Random, SystemRandom
from flask import request, make_response, session, g, Response, current_app
from werkzeug.datastructures import Authorization

class HTTPAuth(object):
    def __init__(self, scheme=None, realm=None, header=None):
        self.scheme = scheme
        self.realm = realm or "Authentication Required"
        self.header = header
        self.get_password_callback = None
        self.get_user_roles_callback = None
        self.auth_error_callback = None

        def default_get_password(username):
            return None

        def default_auth_error(status):
            return "Unauthorized Access", status

        self.get_password(default_get_password)
        self.error_handler(default_auth_error)

    def is_compatible_auth(self, headers):
        if self.header is None or self.header == 'Authorization':
            try:
                scheme, _ = request.headers.get('Authorization', '').split(
                    None, 1)
            except ValueError:
                # malformed Authorization header
                return False
            return scheme == self.scheme
        else:
            return self.header in headers

    def get_password(self, f):
        self.get_password_callback = f
        return f

    def get_user_roles(self, f):
        self.get_user_roles_callback = f
        return f

    def error_handler(self, f):
        @wraps(f)
        def decorated(*args, **kwargs):
            res = self.ensure_sync(f)(*args, **kwargs)
            check_status_code = not isinstance(res, (tuple, Response))
            res = make_response(res)
            if check_status_code and res.status_code == 200:
                # if user didn't set status code, use 401
                res.status_code = 401
            if 'WWW-Authenticate' not in res.headers.keys():
                res.headers['WWW-Authenticate'] = self.authenticate_header()
            return res
        self.auth_error_callback = decorated
        return decorated

    def authenticate_header(self):
        return '{0} realm="{1}"'.format(self.scheme, self.realm)

    def get_auth(self):
        auth = None
        if self.header is None or self.header == 'Authorization':
            auth = request.authorization
            if auth is None and 'Authorization' in request.headers:
                # Flask/Werkzeug do not recognize any authentication types
                # other than Basic or Digest, so here we parse the header by
                # hand
                try:
                    auth_type, token = request.headers['Authorization'].split(
                        None, 1)
                    auth = Authorization(auth_type, {'token': token})
                except (ValueError, KeyError):
                    # The Authorization header is either empty or has no token
                    pass
        elif self.header in request.headers:
            # using a custom header, so the entire value of the header is
            # assumed to be a token
            auth = Authorization(self.scheme,
                                 {'token': request.headers[self.header]})

        # if the auth type does not match, we act as if there is no auth
        # this is better than failing directly, as it allows the callback
        # to handle special cases, like supporting multiple auth types
        if auth is not None and auth.type.lower() != self.scheme.lower():
            auth = None

        return auth

    def get_auth_password(self, auth):
        password = None

        if auth and auth.username:
            password = self.ensure_sync(self.get_password_callback)(
                auth.username)

        return password

    def authorize(self, role, user, auth):
        if role is None:
            return True
        if isinstance(role, (list, tuple)):
            roles = role
        else:
            roles = [role]
        if user is True:
            user = auth
        if self.get_user_roles_callback is None:  # pragma: no cover
            raise ValueError('get_user_roles callback is not defined')
        user_roles = self.ensure_sync(self.get_user_roles_callback)(user)
        if user_roles is None:
            user_roles = {}
        elif not isinstance(user_roles, (list, tuple)):
            user_roles = {user_roles}
        else:
            user_roles = set(user_roles)
        for role in roles:
            if isinstance(role, (list, tuple)):
                role = set(role)
                if role & user_roles == role:
                    return True
            elif role in user_roles:
                return True

    def login_required(self, f=None, role=None, optional=None):
        if f is not None and \
                (role is not None or optional is not None):  # pragma: no cover
            raise ValueError(
                'role and optional are the only supported arguments')

        def login_required_internal(f):
            @wraps(f)
            def decorated(*args, **kwargs):
                auth = self.get_auth()

                # Flask normally handles OPTIONS requests on its own, but in
                # the case it is configured to forward those to the
                # application, we need to ignore authentication headers and
                # let the request through to avoid unwanted interactions with
                # CORS.
                if request.method != 'OPTIONS':  # pragma: no cover
                    password = self.get_auth_password(auth)

                    status = None
                    user = self.authenticate(auth, password)
                    if user in (False, None):
                        status = 401
                    elif not self.authorize(role, user, auth):
                        status = 403
                    if not optional and status:
                        try:
                            return self.auth_error_callback(status)
                        except TypeError:
                            return self.auth_error_callback()

                    g.flask_httpauth_user = user if user is not True \
                        else auth.username if auth else None
                return self.ensure_sync(f)(*args, **kwargs)
            return decorated

        if f:
            return login_required_internal(f)
        return login_required_internal

    def username(self):
        auth = self.get_auth()
        if not auth:
            return ""
        return auth.username

    def current_user(self):
        if hasattr(g, 'flask_httpauth_user'):
            return g.flask_httpauth_user

    def ensure_sync(self, f):
        try:
            return current_app.ensure_sync(f)
        except AttributeError:  # pragma: no cover
            return f

class HTTPBasicAuth(HTTPAuth):
    def __init__(self, scheme=None, realm=None):
        super(HTTPBasicAuth, self).__init__(scheme or 'Basic', realm)

        self.hash_password_callback = None
        self.verify_password_callback = None

    def hash_password(self, f):
        self.hash_password_callback = f
        return f

    def verify_password(self, f):
        self.verify_password_callback = f
        return f

    def get_auth(self):
        # this version of the Authorization header parser is more flexible
        # than Werkzeug's, as it also accepts other schemes besides "Basic"
        header = self.header or 'Authorization'
        if header not in request.headers:
            return None
        value = request.headers[header].encode('utf-8')
        try:
            scheme, credentials = value.split(b' ', 1)
            encoded_username, encoded_password = b64decode(
                credentials).split(b':', 1)
        except (ValueError, TypeError):
            return None
        try:
            username = encoded_username.decode('utf-8')
            password = encoded_password.decode('utf-8')
        except UnicodeDecodeError:
            # try to decode again with latin-1, which should always work
            username = encoded_username.decode('latin1')
            password = encoded_password.decode('latin1')

        return Authorization(
            scheme, {'username': username, 'password': password})

    def authenticate(self, auth, stored_password):
        if auth:
            username = auth.username
            client_password = auth.password
        else:
            username = ""
            client_password = ""
        if self.verify_password_callback:
            return self.ensure_sync(self.verify_password_callback)(
                username, client_password)
        if not auth:
            return
        if self.hash_password_callback:
            try:
                client_password = self.ensure_sync(
                    self.hash_password_callback)(client_password)
            except TypeError:
                client_password = self.ensure_sync(
                    self.hash_password_callback)(username, client_password)
        return auth.username if client_password is not None and \
            stored_password is not None and \
            hmac.compare_digest(client_password, stored_password) else None

class HTTPDigestAuth(HTTPAuth):
    def __init__(self, scheme=None, realm=None, use_ha1_pw=False, qop='auth',
                 algorithm='MD5'):
        super(HTTPDigestAuth, self).__init__(scheme or 'Digest', realm)
        self.use_ha1_pw = use_ha1_pw
        if isinstance(qop, str):
            self.qop = [v.strip() for v in qop.split(',')]
        else:
            self.qop = qop
        if algorithm.lower() == 'md5':
            self.algorithm = 'MD5'
        elif algorithm.lower() == 'md5-sess':
            self.algorithm = 'MD5-Sess'
        else:
            raise ValueError(f'Algorithm {algorithm} is not supported')
        self.random = SystemRandom()
        try:
            self.random.random()
        except NotImplementedError:  # pragma: no cover
            self.random = Random()

        self.generate_nonce_callback = None
        self.verify_nonce_callback = None
        self.generate_opaque_callback = None
        self.verify_opaque_callback = None

        def _generate_random():
            return md5(str(self.random.random()).encode('utf-8')).hexdigest()

        def default_generate_nonce():
            session["auth_nonce"] = _generate_random()
            return session["auth_nonce"]

        def default_verify_nonce(nonce):
            session_nonce = session.get("auth_nonce")
            if nonce is None or session_nonce is None:
                return False
            return hmac.compare_digest(nonce, session_nonce)

        def default_generate_opaque():
            session["auth_opaque"] = _generate_random()
            return session["auth_opaque"]

        def default_verify_opaque(opaque):
            session_opaque = session.get("auth_opaque")
            if opaque is None or session_opaque is None:  # pragma: no cover
                return False
            return hmac.compare_digest(opaque, session_opaque)

        self.generate_nonce(default_generate_nonce)
        self.generate_opaque(default_generate_opaque)
        self.verify_nonce(default_verify_nonce)
        self.verify_opaque(default_verify_opaque)

    def generate_nonce(self, f):
        self.generate_nonce_callback = f
        return f

    def verify_nonce(self, f):
        self.verify_nonce_callback = f
        return f

    def generate_opaque(self, f):
        self.generate_opaque_callback = f
        return f

    def verify_opaque(self, f):
        self.verify_opaque_callback = f
        return f

    def get_nonce(self):
        return self.generate_nonce_callback()

    def get_opaque(self):
        return self.generate_opaque_callback()

    def generate_ha1(self, username, password):
        a1 = username + ":" + self.realm + ":" + password
        a1 = a1.encode('utf-8')
        return md5(a1).hexdigest()

    def authenticate_header(self):
        nonce = self.get_nonce()
        opaque = self.get_opaque()
        if self.qop:
            return ('{0} realm="{1}",nonce="{2}",opaque="{3}",algorithm="{4}"'
                    ',qop="{5}"').format(
                self.scheme, self.realm, nonce,
                opaque, self.algorithm, ','.join(self.qop))
        else:
            return '{0} realm="{1}",nonce="{2}",opaque="{3}"'.format(
                self.scheme, self.realm, nonce,
                opaque)

    def authenticate(self, auth, stored_password_or_ha1):
        if not auth or not auth.username or not auth.realm or not auth.uri \
                or not auth.nonce or not auth.response \
                or not stored_password_or_ha1:
            return False
        if not(self.verify_nonce_callback(auth.nonce)) or \
                not(self.verify_opaque_callback(auth.opaque)):
            return False
        if auth.qop and auth.qop not in self.qop:  # pragma: no cover
            return False
        if self.use_ha1_pw:
            ha1 = stored_password_or_ha1
        else:
            a1 = auth.username + ":" + auth.realm + ":" + \
                stored_password_or_ha1
            ha1 = md5(a1.encode('utf-8')).hexdigest()
        if self.algorithm == 'MD5-Sess':
            ha1 = md5((ha1 + ':' + auth.nonce + ':' + auth.cnonce).encode(
                'utf-8')).hexdigest()
        a2 = request.method + ":" + auth.uri
        ha2 = md5(a2.encode('utf-8')).hexdigest()
        if auth.qop == 'auth':
            a3 = ha1 + ":" + auth.nonce + ":" + auth.nc + ":" + \
                auth.cnonce + ":auth:" + ha2
        else:
            a3 = ha1 + ":" + auth.nonce + ":" + ha2
        response = md5(a3.encode('utf-8')).hexdigest()
        return hmac.compare_digest(response, auth.response)

class HTTPTokenAuth(HTTPAuth):
    def __init__(self, scheme='Bearer', realm=None, header=None):
        super(HTTPTokenAuth, self).__init__(scheme, realm, header)

        self.verify_token_callback = None

    def verify_token(self, f):
        self.verify_token_callback = f
        return f

    def authenticate(self, auth, stored_password):
        if auth:
            token = auth['token']
        else:
            token = ""
        if self.verify_token_callback:
            return self.ensure_sync(self.verify_token_callback)(token)

class MultiAuth(object):
    def __init__(self, main_auth, *args):
        self.main_auth = main_auth
        self.additional_auth = args

    def login_required(self, f=None, role=None, optional=None):
        if f is not None and \
                (role is not None or optional is not None):  # pragma: no cover
            raise ValueError(
                'role and optional are the only supported arguments')

        def login_required_internal(f):
            @wraps(f)
            def decorated(*args, **kwargs):
                selected_auth = self.main_auth
                if not self.main_auth.is_compatible_auth(request.headers):
                    for auth in self.additional_auth:
                        if auth.is_compatible_auth(request.headers):
                            selected_auth = auth
                            break
                return selected_auth.login_required(
                    role=role, optional=optional)(f)(*args, **kwargs)
            return decorated

        if f:
            return login_required_internal(f)
        return login_required_internal

    def current_user(self):
        if hasattr(g, 'flask_httpauth_user'):  # pragma: no cover
            return g.flask_httpauth_user


text:
"""
Waveform simulator ("FaX") - physics backend
The only I/O stuff here is pax event creation, everything else is in the WaveformSimulator plugins
"""

from __future__ import division
import logging
import math
import time
import pickle
from functools import partial

import numpy as np
import pandas as pd
import multihist    # noqa   # Not explicitly used, but pickle of gas gap warping map is in this format
from scipy import stats
from scipy.interpolate import interp1d

from pax import units, utils, datastructure
from pax.PatternFitter import PatternFitter
from pax.InterpolatingMap import InterpolatingMap
from pax.utils import Memoize

log = logging.getLogger('SimulationCore')

class Simulator(object):

    def __init__(self, config_to_init):
        c = self.config = config_to_init

        # Should we repeat events?
        if 'event_repetitions' not in c:
            c['event_repetitions'] = 1

        # Primary excimer fraction from Nest Version 098
        # See G4S1Light.cc line 298
        density = c['liquid_density'] / (units.g / units.cm ** 3)
        excfrac = 0.4 - 0.11131 * density - 0.0026651 * density ** 2    # primary / secondary excimers
        excfrac = 1 / (1 + excfrac)                                     # primary / all excimers
        # primary / all excimers that produce a photon:
        excfrac /= 1 - (1 - excfrac) * (1 - c['s1_ER_recombination_fraction'])
        c['s1_ER_primary_excimer_fraction'] = excfrac
        log.debug('Inferred s1_ER_primary_excimer_fraction %s' % excfrac)

        # Recombination time from NEST 2014
        # 3.5 seems fishy, they fit an exponential to data, but in the code they use a non-exponential distribution...
        efield = (c['drift_field'] / (units.V / units.cm))
        c['s1_ER_recombination_time'] = 3.5 / 0.18 * (1 / 20 + 0.41) * math.exp(-0.009 * efield)
        log.debug('Inferred s1_ER_recombination_time %s ns' % c['s1_ER_recombination_time'])

        # Which channels stand to receive any photons?
        channels_for_photons = c['channels_in_detector']['tpc']
        if c['pmt_0_is_fake']:
            channels_for_photons = [ch for ch in channels_for_photons if ch != 0]
        if c.get('magically_avoid_dead_pmts', False):
            channels_for_photons = [ch for ch in channels_for_photons if c['gains'][ch] > 0]
        if c.get('magically_avoid_s1_excluded_pmts', False) and \
           'channels_excluded_for_s1' in c:
            channels_for_photons = [ch for ch in channels_for_photons
                                    if ch not in c['channels_excluded_for_s1']]
        c['channels_for_photons'] = channels_for_photons

        # Determine sensible length of a pmt pulse to simulate
        dt = c['sample_duration']
        if c['pe_pulse_model'] == 'exponential':
            c['samples_before_pulse_center'] = math.ceil(c['pulse_width_cutoff'] * c['pmt_rise_time'] / dt)
            c['samples_after_pulse_center'] = math.ceil(c['pulse_width_cutoff'] * c['pmt_fall_time'] / dt)
        else:
            # Build the custom PMT pulse model
            ts = np.array(c['pe_pulse_ts'])
            ys = np.array(c['pe_pulse_ys'])

            # Integrate and normalize it
            # Note we're storing the integrated pulse, while the user gives the regular pulse.
            c['pe_pulse_function'] = interp1d(ts, np.cumsum(ys)/np.sum(ys), bounds_error=False, fill_value=(0, 1))

        log.debug('Simulating %s samples before and %s samples after PMT pulse centers.' % (
            c['samples_before_pulse_center'], c['samples_after_pulse_center']))

        # Load real noise data from file, if requested
        if c['real_noise_file']:
            self.noise_data = np.load(utils.data_file_name(c['real_noise_file']))['arr_0']
            # The silly XENON100 PMT offset again: it's relevant for indexing the array of noise data
            # (which is one row per channel)
            self.channel_offset = 1 if c['pmt_0_is_fake'] else 0

        # Load light yields
        self.s1_light_yield_map = InterpolatingMap(utils.data_file_name(c['s1_light_yield_map']))
        self.s2_light_yield_map = InterpolatingMap(utils.data_file_name(c['s2_light_yield_map']))

        # Load transverse field (r,z) distortion map
        if c.get('rz_position_distortion_map'):
            self.rz_position_distortion_map = InterpolatingMap(utils.data_file_name(c['rz_position_distortion_map']))
        else:
            self.rz_position_distortion_map = None

        # Init s2 per pmt lce map
        qes = np.array(c['quantum_efficiencies'])
        if c.get('s2_patterns_file', None) is not None:
            self.s2_patterns = PatternFitter(filename=utils.data_file_name(c['s2_patterns_file']),
                                             zoom_factor=c.get('s2_patterns_zoom_factor', 1),
                                             adjust_to_qe=qes[c['channels_top']],
                                             default_errors=c['relative_qe_error'] + c['relative_gain_error'])
        else:
            self.s2_patterns = None

        ##
        # Load pdf for single photoelectron, if available
        ##
        if c.get('photon_area_distribution'):
            # Extract the spe pdf from a csv file into a pandas dataframe
            spe_shapes = pd.read_csv(utils.data_file_name(c['photon_area_distribution']))

            # Create a converter array from uniform random numbers to SPE gains (one interpolator per channel)
            # Scale the distributions so that they have an SPE mean of 1 and then calculate the cdf
            # We have set the distribution of the off channels to be explicitly 0 as a precaution
            # as of now these channels are
            # 1, 2, 12, 26, 34, 62, 65, 79, 86, 88, 102, 118, 130, 134, 135, 139,
            # 148, 150, 152, 162, 178, 183, 190, 198, 206, 213, 214, 234, 239, 244

            uniform_to_pe_arr = []
            for ch in spe_shapes.columns[1:]:  # skip the first element which is the 'charge' header
                if spe_shapes[ch].sum() > 0:
                    mean_spe = (spe_shapes['charge'] * spe_shapes[ch]).sum() / spe_shapes[ch].sum()
                    scaled_bins = spe_shapes['charge'] / mean_spe
                    cdf = np.cumsum(spe_shapes[ch])/np.sum(spe_shapes[ch])
                else:
                    # if sum is 0, just make some dummy axes to pass to interpolator
                    cdf = np.linspace(0, 1, 10)
                    scaled_bins = np.zeros_like(cdf)

                uniform_to_pe_arr.append(interp1d(cdf, scaled_bins))
            if uniform_to_pe_arr != []:
                self.uniform_to_pe_arr = np.array(uniform_to_pe_arr)
            else:
                self.uniform_to_pe_arr = None

        else:
            self.uniform_to_pe_arr = None

        # Init s1 pattern maps
        # We're assuming the map is MC-derived, so we adjust for QE (just like for the S2 maps)
        log.debug("Initializing s1 patterns...")
        if c.get('s1_patterns_file', None) is not None:
            self.s1_patterns = PatternFitter(filename=utils.data_file_name(c['s1_patterns_file']),
                                             zoom_factor=c.get('s1_patterns_zoom_factor', 1),
                                             adjust_to_qe=qes[c['channels_in_detector']['tpc']],
                                             default_errors=c['relative_qe_error'] + c['relative_gain_error'])
        else:
            self.s1_patterns = None

        ##
        # Luminescence time distribution precomputation
        ##

        # For which gas gaps do we have to compute the luminescence time distribution?
        gas_gap_warping_map = c.get('gas_gap_warping_map', None)
        base_dg = c['elr_gas_gap_length']
        if gas_gap_warping_map is not None:
            with open(utils.data_file_name(gas_gap_warping_map), mode='rb') as infile:
                mh = pickle.load(infile)
            self.gas_gap_length = lambda x, y: base_dg + mh.lookup([x], [y]).item()
            self.luminescence_converters_dgs = np.linspace(mh.histogram.min(),
                                                           mh.histogram.max(),
                                                           c.get('n_luminescence_time_converters', 20)) + base_dg
        else:
            self.gas_gap_length = lambda x, y: base_dg
            self.luminescence_converters_dgs = np.array([base_dg])

        self.luminescence_converters = []

        # Calculate particle number density in the gas (ideal gas law)
        number_density_gas = c['pressure'] / (units.boltzmannConstant * c['temperature'])

        # Slope of the drift velocity vs field relation
        alpha = c['gas_drift_velocity_slope'] / number_density_gas

        @np.vectorize
        def yield_per_dr(E):
            # Gives something proportional to the yield, not the yield itself!
            y = E / (units.kV / units.cm) - 0.8 * c['pressure'] / units.bar
            return max(y, 0)

        rA = c['anode_field_domination_distance']
        rW = c['anode_wire_radius']

        for dg in self.luminescence_converters_dgs:
            dl = c['gate_to_anode_distance'] - dg
            rL = dg

            # Voltage over the gas gap
            V = c['anode_voltage'] / (1 + dl/dg/c['lxe_dielectric_constant'])  # From eq1 in se note, * dg

            # Field in the gas gap. r is distance from anode center: start at r=rL
            E0 = V/(rL - rA + rA * (np.log(rA) - np.log(rW)))

            @np.vectorize
            def Er(r):
                if r < rW:
                    return 0
                elif rW <= r < rA:
                    return E0 * rA / r
                else:
                    return E0

            # Small numeric calculation to get emission time cdf
            R = np.linspace(rL, rW, 1000)
            E = Er(R)
            RDOT = alpha * E
            T = np.cumsum(- np.diff(R)[0] / RDOT)     # dt = dx / v
            yield_density = yield_per_dr(E) * RDOT    # density/dt = density/dx * dx/dt
            yield_density /= yield_density.sum()

            # Invert CDF using interpolator
            uniform_to_emission_time = interp1d(np.cumsum(yield_density), T,
                                                fill_value=0, bounds_error=False)

            self.luminescence_converters.append(uniform_to_emission_time)

        self.clear_signals_queue()

    def clear_signals_queue(self):
        """Prepares the waveform simulator for a new event.
        """
        self.arrival_times_per_channel = {ch: [] for ch in range(self.config['n_channels'])}

    def queue_signal(self, photon_timings, x=0, y=0, z=0):
        """Add a signal due to isotropic light emission to the waveform simulator
            photon_timings: list of photon emission times (ns) since start of the event.
                            All photons listed here will be detected!
            x, y, z: position of emission (standard coordinate system, pax units = cm)
        """
        if not len(photon_timings):
            return

        # Correct for PMT Transition Time Spread
        photon_timings += np.random.normal(self.config['pmt_transit_time_mean'],
                                           self.config['pmt_transit_time_spread'],
                                           len(photon_timings))

        # Shuffle all timings in the array, so channel 1 doesn't always get the first photon
        np.random.shuffle(photon_timings)

        # Get the photon counts per channel
        hitp = self.distribute_photons(len(photon_timings), x, y, z)

        # Split photon times over channels, add to the currently queued photon signals
        # Note the last array is always zero  # TODO: did you check this
        q = np.split(photon_timings, np.cumsum(hitp))
        assert len(q[-1]) == 0
        q = q[:-1]
        all_arriving_times_top = np.array([])
        all_arriving_times_bottom = np.array([])
        for channel_i, photon_times in enumerate(q):
            self.arrival_times_per_channel[channel_i] = np.concatenate((self.arrival_times_per_channel[channel_i],
                                                                        photon_times))
            if channel_i in self.config['channels_top']:
                all_arriving_times_top = np.concatenate((all_arriving_times_top, photon_times))
            elif channel_i in self.config['channels_bottom']:
                all_arriving_times_bottom = np.concatenate((all_arriving_times_bottom, photon_times))
        return (all_arriving_times_top, all_arriving_times_bottom)

    def get_gains(self, channel, n):
        """Draw n SPE areas for channel"""
        gain_mean = self.config['gains'][channel]
        if gain_mean == 0:
            return np.zeros(n)

        if self.uniform_to_pe_arr is not None:
            # Use real spe data to generate gains for spe
            gains = self.uniform_to_pe_arr[channel](np.random.random(n))
            gains *= self.config['gains'][channel]

        else:
            gain_sigma = self.config['gain_sigmas'][channel]

            # Sample from a truncated Gaussian
            gains = truncated_gauss_rvs(my_mean=gain_mean,
                                        my_std=gain_sigma,
                                        left_boundary=0,
                                        right_boundary=float('inf'),
                                        n_rvs=n)

        return gains

    def make_pax_event(self):
        """Simulate PMT response to the queued photon signals
        Returns None if no photons have been queued else returns start_time (in units, ie ns), pmt waveform matrix
        # TODO: Account for random initial digitizer state wrt interaction? Where?
        """
        log.debug("Now performing hitpattern to waveform conversion")
        start_time = int(time.time() * units.s)

        # Find out the duration of the event
        all_times = np.concatenate(list(self.arrival_times_per_channel.values()))
        if not len(all_times):
            log.warning("No photons to simulate: making a noise-only event")
            max_time = 0
        else:
            max_time = np.concatenate(list(self.arrival_times_per_channel.values())).max()

        event = datastructure.Event(n_channels=self.config['n_channels'],
                                    start_time=start_time,
                                    stop_time=start_time + int(max_time + 2 * self.config['event_padding']),
                                    sample_duration=self.config['sample_duration'])
        # Ensure the event length is even (else it cannot be written to XED)
        if event.length() % 2 != 0:
            event.stop_time += self.config['sample_duration']

        # Convenience variables
        dt = self.config['sample_duration']
        dv = self.config['digitizer_voltage_range'] / 2 ** (self.config['digitizer_bits'])

        start_index = 0
        end_index = event.length() - 1
        pulse_length = end_index - start_index + 1

        # Setup things for real noise simulation
        if self.config['real_noise_sample_size']:
            noise_sample_len = self.config['real_noise_sample_size']
            available_noise_samples = self.noise_data.shape[1] // noise_sample_len
            needed_noise_samples = int(math.ceil(pulse_length / noise_sample_len))

            noise_sample_mode = self.config.get('real_noise_sample_mode', 'incoherent')
            if noise_sample_mode == 'coherent':
                # Choose a single set of noise sample numbers for the event
                chosen_noise_sample_numbers = np.random.randint(0,
                                                                available_noise_samples - 1,
                                                                needed_noise_samples)

                roll_number = np.random.randint(noise_sample_len)

        # Setup the lone-hit arrival_times_per_channel
        lone_hit_arrival_times_per_channels = self.lone_hits(max_time)

        # Build waveform channel by channel
        for channel, photon_detection_times in self.arrival_times_per_channel.items():
            # If the channel is dead, fake, or not in the TPC, we don't do anything.
            if (self.config['gains'][channel] == 0 or
               (self.config['pmt_0_is_fake'] and channel == 0) or
               channel not in self.config['channels_in_detector']['tpc']):
                continue

            photon_detection_times = np.array(photon_detection_times)

            # Add double photoelectron emission
            if len(photon_detection_times):
                n_dpe = np.random.binomial(len(photon_detection_times), p=self.config['p_double_pe_emision'])
                if n_dpe:
                    dpe_times = np.random.choice(photon_detection_times, n_dpe, replace=False)
                    photon_detection_times = np.concatenate([photon_detection_times, dpe_times])

            log.debug("Simulating %d photons in channel %d (gain=%s, gain_sigma=%s)" % (
                len(photon_detection_times), channel,
                self.config['gains'][channel], self.config['gain_sigmas'][channel]))

            # Combine the lone-hits with the normal PMT pulses
            photon_detection_times = np.concatenate(
                (photon_detection_times, lone_hit_arrival_times_per_channels[channel])
            )

            gains = self.get_gains(channel, len(photon_detection_times))

            # Add PMT afterpulses
            ap_times = []
            ap_amplifications = []
            ap_gains = []

            # Get the afterpulse settings for this channel:
            #  1. If we have a specific afterpulse config for this channel, we use it
            #  2. Else we fall back to a default configuration
            #  3. If this does not exist, we don't make any afterpulses
            all_ap_data = self.config.get('pmt_afterpulse_types', [])
            if 'each_pmt_afterpulse_types' in self.config and channel in self.config['each_pmt_afterpulse_types']:
                all_ap_data = self.config['each_pmt_afterpulse_types'][channel]

            for ap_data in all_ap_data.values():
                if not ap_data:
                    continue
                # print(ap_data)

                # How many photons will make this kind of afterpulse?
                n_afterpulses = np.random.binomial(
                        n=len(photon_detection_times),
                        p=ap_data['p']
                        )
                if not n_afterpulses:
                    continue

                # Find the time and gain of the afterpulses
                dist_kwargs = ap_data['time_parameters']
                dist_kwargs['size'] = n_afterpulses
                ap_times.extend(np.random.choice(photon_detection_times, size=n_afterpulses, replace=False) +
                                getattr(np.random, ap_data['time_distribution'])(**dist_kwargs))

                # Afterpulse gains can be different from regular gains: sample an amplification factor
                if 'amp_mean' in ap_data:
                    ap_amplifications.extend(truncated_gauss_rvs(
                                my_mean=ap_data['amp_mean'],
                                my_std=ap_data['amp_rms'],
                                left_boundary=0,
                                right_boundary=float('inf'),
                                n_rvs=n_afterpulses))
                else:
                    ap_amplifications.extend([1.]*n_afterpulses)

                ap_gains.extend(self.get_gains(channel, n_afterpulses))

            # Combine the afterpulses with the normal PMT pulses
            ap_gains = [x*y for x, y in zip(ap_gains, ap_amplifications)]
            gains = np.concatenate((gains, ap_gains))
            photon_detection_times = np.concatenate((photon_detection_times, ap_times))

            # Add padding, sort (eh.. or were we already sorted? and is sorting necessary at all??)
            pmt_pulse_centers = np.sort(photon_detection_times + self.config['event_padding'])

            # Build the waveform pulse by pulse (bin by bin was slow, hope this is faster)

            # Compute offset & center index for each pe-pulse
            # 'index' refers to the (hypothetical) event waveform, as usual
            pmt_pulse_centers = np.array(pmt_pulse_centers, dtype=np.int)
            offsets = pmt_pulse_centers % dt
            center_index = (pmt_pulse_centers - offsets) / dt   # Absolute index in waveform of pe-pulse center
            center_index = center_index.astype(np.int)

            # Simulate an event-long waveform in this channel
            # Remember start padding has already been added to times, so just one padding in end_index
            current_wave = np.zeros(pulse_length)

            for i, _ in enumerate(pmt_pulse_centers):
                # Add some current for this photon pulse
                # Compute the integrated pmt pulse at various samples, then
                # do their diffs/dt
                generated_pulse = self.pmt_pulse_current(gain=gains[i], offset=offsets[i])

                # +1 due to np.diff in pmt_pulse_current   #????
                left_index = center_index[i] - start_index + 1
                left_index -= int(self.config['samples_before_pulse_center'])
                righter_index = center_index[i] - start_index + 1
                righter_index += int(self.config['samples_after_pulse_center'])

                # Abandon the pulse if it goes the left/right boundaries
                if len(generated_pulse) != righter_index - left_index:
                    raise RuntimeError(
                        "Generated pulse is %s samples long, can't be inserted between %s and %s" % (
                            len(generated_pulse), left_index, righter_index))
                elif left_index < 0:
                    log.debug("Invalid left index %s: can't be negative" % left_index)
                    continue
                elif righter_index >= len(current_wave):
                    log.debug("Invalid right index %s: can't be longer than length of wave (%s)!" % (
                        righter_index, len(current_wave)))
                    continue

                current_wave[left_index: righter_index] += generated_pulse

            # Did you order some Gaussian current noise with that?
            if self.config['gauss_noise_sigmas']:
                # if the baseline fluc. is defined for each channel
                # use that in prior
                noise_sigma_current = self.config['gauss_noise_sigmas'][channel]*self.config['gains'][channel] / dt
                current_wave += np.random.normal(0, noise_sigma_current, len(current_wave))
            elif self.config['gauss_noise_sigma']:
                # / dt is for charge -> current conversion, as in pmt_pulse_current
                noise_sigma_current = self.config['gauss_noise_sigma'] * self.config['gains'][channel] / dt,
                current_wave += np.random.normal(0, noise_sigma_current, len(current_wave))

            # Convert from PMT current to ADC counts
            adc_wave = current_wave
            adc_wave *= self.config['pmt_circuit_load_resistor']    # Now in voltage
            adc_wave *= self.config['external_amplification']       # Now in voltage after amplifier
            adc_wave /= dv                                          # Now in float ADC counts above baseline
            adc_wave = np.trunc(adc_wave)                           # Now in integer ADC counts "" ""
            # Could round instead of trunc... who cares?

            # PMT signals are negative excursions, so flip them.
            adc_wave = - adc_wave

            # Did you want to superpose onto real noise samples?
            if self.config['real_noise_file']:
                if noise_sample_mode != 'coherent':
                    # For each channel, choose different noise sample numbers
                    chosen_noise_sample_numbers = np.random.randint(0,
                                                                    available_noise_samples - 1,
                                                                    needed_noise_samples)

                    roll_number = np.random.randint(noise_sample_len)

                # Extract the chosen noise samples and concatenate them
                # Have to use a listcomp here, unless you know a way to select multiple slices in numpy?
                #  -- yeah making an index list with np.arange would work, but honestly??
                real_noise = np.concatenate([
                    self.noise_data[channel - self.channel_offset][nsn * noise_sample_len:(nsn + 1) * noise_sample_len]
                    for nsn in chosen_noise_sample_numbers
                ])

                # Roll the noise samples by a fraction of the sample size,
                # to avoid same artifacts falling at the same point every time
                np.roll(real_noise, roll_number, axis=0)

                # Adjust the noise amplitude if needed, then add it to the ADC wave
                noise_amplitude = self.config.get('adjust_noise_amplitude', {}).get(str(channel), 1)
                if noise_amplitude != 1:
                    # Determine a rough baseline for the noise, then adjust towards it
                    baseline = np.mean(real_noise[:min(len(real_noise), 50)])
                    real_noise = baseline + noise_amplitude * (real_noise - baseline)
                adc_wave += real_noise[:pulse_length]

            else:
                # If you don't want to superpose onto real noise,
                # we should add a reference baseline
                adc_wave += self.config['digitizer_reference_baseline']

            # Digitizers have finite number of bits per channel, so clip the signal.
            adc_wave = np.clip(adc_wave, 0, 2 ** (self.config['digitizer_bits']))

            event.pulses.append(datastructure.Pulse(
                channel=channel,
                left=start_index,
                raw_data=adc_wave.astype(np.int16)))

        log.debug("Simulated pax event of %s samples length and %s pulses "
                  "created." % (event.length(), len(event.pulses)))
        self.clear_signals_queue()
        return event

    def s2_electrons(self, electrons_generated=None, z=0, r=0, t=0.):
        """Return a list of electron arrival times in the ELR region caused by an S2 process.

            electrons             -   total # of drift electrons generated at the interaction site
            t                     -   Time at which the original energy deposition occurred.
            r                     -   Radius at which " " " " " (needed for (r,z) distortion)
            z                     -   Depth below the GATE mesh where the interaction occurs.
        As usual, all units in the same system used by pax (if you specify raw values: ns, cm)
        """
        if not - self.config['tpc_length'] <= z <= 0:
            log.warning("Unphysical depth: %s cm below gate. Not generating S2." % - z)
            return []
        log.debug("Creating an s2 from %s electrons..." % electrons_generated)

        # Correct the z position for the radial distortion
        if self.rz_position_distortion_map:
            z += self.rz_position_distortion_map.get_value(r, z, map_name='to_distorted_z')

        # Average drift time of the electrons
        drift_time_mean = - z / self.config['drift_velocity_liquid'] + self.config['drift_time_gate']

        # Diffusion model from Sorensen 2011
        drift_time_stdev = math.sqrt(2 * self.config['diffusion_constant_liquid'] * drift_time_mean)
        drift_time_stdev /= self.config['drift_velocity_liquid']

        # Absorb electrons during the drift
        electron_lifetime_correction = -1 * drift_time_mean / self.config['electron_lifetime_liquid']
        electron_lifetime_correction = math.exp(electron_lifetime_correction)
        prob = self.config['electron_extraction_yield'] * electron_lifetime_correction

        electrons_seen = np.random.binomial(n=electrons_generated,
                                            p=prob)

        log.debug("    %s electrons survive the drift." % electrons_generated)

        # Calculate electron arrival times in the ELR region
        e_arrival_times = t + np.random.exponential(self.config['electron_trapping_time'], electrons_seen)
        if drift_time_stdev:
            e_arrival_times += np.random.normal(drift_time_mean, drift_time_stdev, electrons_seen)
        return e_arrival_times

    def s1_photons(self, n_photons, recoil_type, x=0., y=0., z=0, t=0.):
        """Returns a list of photon detection times at the PMT caused by an S1 emitting n_photons.
        """
        # Apply light yield / detection efficiency
        log.debug("Creating an s1 from %s photons..." % n_photons)
        ly = self.s1_light_yield_map.get_value(x, y, z) * self.config['s1_detection_efficiency']
        n_photons = np.random.binomial(n=n_photons, p=ly)
        log.debug("    %s photons are detected." % n_photons)
        if n_photons == 0:
            return np.array([])

        if recoil_type.lower() == 'alpha':
            # again neglible recombination time, same singlet/triplet ratio for primary & secondary excimers
            # Hence, we don't care about primary & secondary excimers at all:
            timings = self.singlet_triplet_delays(
                np.zeros(n_photons),
                t1=self.config['singlet_lifetime_liquid'],
                t3=self.config['triplet_lifetime_liquid'],
                singlet_ratio=self.config['s1_ER_alpha_singlet_fraction']
            )

        elif recoil_type.lower() == 'led':
            # distribute photons uniformly within the LED pulse length
            timings = np.random.uniform(0, self.config['led_pulse_length'],
                                        size=n_photons)

        elif self.config.get('s1_model_type') == 'simple':
            # Simple S1 model enabled: use it for ER and NR.
            timings = np.random.exponential(self.config['s1_decay_time'], size=n_photons)

        elif recoil_type.lower() == 'er':
            # How many of these are primary excimers? Others arise through recombination.
            n_primaries = np.random.binomial(n=n_photons, p=self.config['s1_ER_primary_excimer_fraction'])

            primary_timings = self.singlet_triplet_delays(
                np.zeros(n_primaries),  # No recombination delay for primary excimers
                t1=self.config['singlet_lifetime_liquid'],
                t3=self.config['triplet_lifetime_liquid'],
                singlet_ratio=self.config['s1_ER_primary_singlet_fraction']
            )

            # Correct for the recombination time
            # For the non-exponential distribution: see Kubota 1979, solve eqn 2 for n/n0.
            # Alternatively, see Nest V098 source code G4S1Light.cc line 948
            secondary_timings = self.config['s1_ER_recombination_time']\
                * (-1 + 1 / np.random.uniform(0, 1, n_photons - n_primaries))
            secondary_timings = np.clip(secondary_timings, 0, self.config['maximum_recombination_time'])
            # Handle singlet/ triplet decays as before
            secondary_timings += self.singlet_triplet_delays(
                secondary_timings,
                t1=self.config['singlet_lifetime_liquid'],
                t3=self.config['triplet_lifetime_liquid'],
                singlet_ratio=self.config['s1_ER_secondary_singlet_fraction']
            )

            timings = np.concatenate((primary_timings, secondary_timings))

        elif recoil_type.lower() == 'nr':
            # Neglible recombination time, same singlet/triplet ratio for primary & secondary excimers
            # Hence, we don't care about primary & secondary excimers at all:
            timings = self.singlet_triplet_delays(
                np.zeros(n_photons),
                t1=self.config['singlet_lifetime_liquid'],
                t3=self.config['triplet_lifetime_liquid'],
                singlet_ratio=self.config['s1_NR_singlet_fraction']
            )

        else:
            raise ValueError('Recoil type must be ER, NR, alpha or LED, not %s' % type)

        return timings + t * np.ones(len(timings))

    def s2_scintillation(self, electron_arrival_times, x=0.0, y=0.0):
        """Given a list of electron arrival times, returns photon production times"""
        # How many photons does each electron make?
        c = self.config
        photons_produced = np.random.poisson(
            c['s2_secondary_sc_gain'] * self.s2_light_yield_map.get_value(x, y),
            len(electron_arrival_times)
        )
        total_photons = np.sum(photons_produced)
        log.debug("    %s scintillation photons will be detected." % total_photons)
        if total_photons == 0:
            return np.array([])

        # Find the photon production times
        # Assume luminescence probability ~ electric field
        s2_pe_times = np.concatenate([
            t0 + self.get_luminescence_times(photons_produced[i], x, y)
            for i, t0 in enumerate(electron_arrival_times)
        ])

        # Account for singlet/triplet excimer decay times
        return self.singlet_triplet_delays(
            s2_pe_times,
            t1=c['singlet_lifetime_gas'],
            t3=c['triplet_lifetime_gas'],
            singlet_ratio=c['singlet_fraction_gas']
        )

    def lone_hits(self, max_time):
        # generate the lone hits based on the frequency maps
        # need the config of the lone-hit rate in each channel:
        # sim.config['lone_hit_rate_per_channel'], and it shall be dictionary with key of pmt ids
        # sim.config['lone_hit_rate'] (global rate for all pmts)
        if ('lone_hit_rate_per_channel' not in self.config) and ('lone_hit_rate' not in self.config):
            return np.asarray(
                [[]]*int(self.config['n_channels'])
            )
        # Lone hit rate map
        lone_hit_rate_map = np.asarray(
                [float(self.config.get('lone_hit_rate', 0.))]*int(self.config['n_channels'])
            )
        if 'lone_hit_rate_per_channel' in self.config:
            lone_hit_rate_map[list(self.config['lone_hit_rate_per_channel'].keys())] = \
                list(self.config['lone_hit_rate_per_channel'].values())
        # total number of lone hits among all channels
        # the length shall be (-self.config['event_padding'], max_time)
        total_num_lone_hits = int(np.sum(lone_hit_rate_map)*(max_time+self.config['event_padding']))
        lone_hit_channels = np.random.choice(
            self.config['n_channels'],
            size=total_num_lone_hits,
            p=np.asarray(lone_hit_rate_map) / np.sum(lone_hit_rate_map),
        )
        lone_hit_hitp, _ = np.histogram(
            lone_hit_channels,
            bins=self.config['n_channels'],
            range=(0, self.config['n_channels']),
        )
        lone_hit_times = np.random.uniform(-self.config['event_padding'], max_time, size=total_num_lone_hits)
        return np.split(
            lone_hit_times,
            np.cumsum(lone_hit_hitp)
        )[:-1]

    def singlet_triplet_delays(self, times, t1, t3, singlet_ratio):
        """
        Given a list of eximer formation times, returns excimer decay times.
            t1            - singlet state lifetime
            t3            - triplet state lifetime
            singlet_ratio - fraction of excimers that become singlets
                            (NOT the ratio of singlets/triplets!)
        """
        n_singlets = np.random.binomial(n=len(times), p=singlet_ratio)
        return times + np.concatenate([
            np.random.exponential(t1, n_singlets),
            np.random.exponential(t3, len(times) - n_singlets)
        ])

    def get_luminescence_times(self, n, x, y):
        # Get the gas gap width at this point
        dg = self.gas_gap_length(x, y)

        # Retrieve the closest uniform -> luminescence time converter we precomputed, then use it
        conv_i = np.argmin(np.abs(self.luminescence_converters_dgs - dg))
        uniform_to_luminescence_time = self.luminescence_converters[conv_i]

        return uniform_to_luminescence_time(np.random.rand(n))

    def pmt_pulse_current(self, gain, offset=0):
        # Rounds offset to nearest pmt_pulse_time_rounding so we can exploit caching
        offset = self.config['pmt_pulse_time_rounding'] * round(offset / self.config['pmt_pulse_time_rounding'])
        if self.config['pe_pulse_model'] == 'exponential':
            return gain * double_exp_pmt_pulse_current_raw(
                offset,
                self.config['sample_duration'],
                self.config['samples_before_pulse_center'],
                self.config['samples_after_pulse_center'],
                self.config['pmt_rise_time'],
                self.config['pmt_fall_time'],
            )
        else:
            return gain * custom_pmt_pulse_current(self.config['pe_pulse_function'],
                                                   offset,
                                                   self.config['sample_duration'],
                                                   self.config['samples_before_pulse_center'],
                                                   self.config['samples_after_pulse_center'])

    def distribute_photons(self, n_photons, x, y, z):
        """Distribute n_photons over the TPC PMTs, with LCE appropriate to (x, y, z)
        :return: numpy array of length == sim.config['n_channels'] with photon count per channel
        """
        if z == - self.config['gate_to_anode_distance']:
            # Use the S2 pattern information
            if not self.s2_patterns:
                return self.randomize_photons_over_channels(n_photons, self.config['channels_in_detector']['tpc'])

            # How many photons to the top array?
            n_top = np.random.binomial(n=n_photons, p=self.config['s2_mean_area_fraction_top'])

            # Distribute a fraction of the top photons randomly, if the user asked for it
            # This enables robustness testing of the position reconstruction
            p_random = self.config.get('randomize_fraction_of_s2_top_array_photons', 0)
            if p_random:
                n_random = np.random.binomial(n=n_photons, p=p_random)
                hitp = self.distribute_photons_by_pattern(n_top - n_random, self.s2_patterns, (x, y))
                hitp += self.randomize_photons_over_channels(n_random, channels=self.config['channels_top'])
            else:
                hitp = self.distribute_photons_by_pattern(n_top, self.s2_patterns, (x, y))

            # The bottom photons are distributed randomly
            hitp += self.randomize_photons_over_channels(n_photons - n_top, channels=self.config['channels_bottom'])
            return hitp

        else:
            # Use the S1 pattern information
            if not self.s1_patterns:
                return self.randomize_photons_over_channels(n_photons, self.config['channels_in_detector']['tpc'])
            return self.distribute_photons_by_pattern(n_photons, self.s1_patterns, (x, y, z))

    def distribute_photons_by_pattern(self, n_photons, pattern_fitter, coordinate_tuple):
        # TODO: assumes channels drawn from top, or from all channels (i.e. first index 0!!!)
        # Note a CoordinateOutOfRange exception can be raised if points outside the TPC radius are asked
        # We don't catch it here: users shouldn't ask for simulations of impossible things :-)
        lces = pattern_fitter.expected_pattern(coordinate_tuple)
        return self.randomize_photons_over_channels(n_photons,
                                                    channels=range(len(lces)),
                                                    relative_lce_per_channel=lces)

    def randomize_photons_over_channels(self, n_photons, channels=None, relative_lce_per_channel=None):
        """Distribute photon_timings over channels according to relative_lce_per_channel

        :param n_photons: number of photons to distribute
        :param channels: list of channel numbers that can receive photons. This will still be filtered
         to include only channels in self.config['channels_for_photons'].
        :param relative_lce_per_channel: list of relative lce per channel. Should all be >= 0.
                                         If omitted, will distribute photons uniformly over channels.
                                         Does not have to be normalized to sum to 1.
        :return: array of length sim.config['n_channels'] with photon counts per channel
        """
        if n_photons == 0:
            return np.zeros(self.config['n_channels'], dtype=np.int64)

        # Include only channels that can receive photons
        if channels is None:
            channels = np.array(self.config['channels_for_photons'])
        else:
            channels = np.array(channels)
            sel = np.in1d(channels, self.config['channels_for_photons'])
            channels = channels[sel]
            if relative_lce_per_channel is not None:
                relative_lce_per_channel = relative_lce_per_channel[sel]

        # Ensure relative LCEs are valid, and sum to one (among the remaining channels):
        if relative_lce_per_channel is not None:
            relative_lce_per_channel = np.clip(relative_lce_per_channel, 0, 1)
            relative_lce_per_channel /= np.sum(relative_lce_per_channel)

        # Generate a channel index for every photon
        channel_index_for_p = np.random.choice(channels, size=n_photons, p=relative_lce_per_channel)

        # Count number of photons in each channel
        # Note the histogram range must include n_channels, even though n_channels-1 is the maximum value
        # This is because of how numpy handles values on bin edges
        hitp, _ = np.histogram(channel_index_for_p,
                               bins=self.config['n_channels'], range=(0, self.config['n_channels']))

        if not len(hitp) == self.config['n_channels']:
            raise RuntimeError("You found a simulator bug!\n"
                               "Hitpattern has wrong length "
                               "(%d, should be %d)" % (len(hitp), len(channels)))
        if not np.sum(hitp) == n_photons:
            raise RuntimeError("You found a simulator bug!\n"
                               "Hitpattern has wrong number of photons "
                               "(%d, should be %d)" % (np.sum(hitp), n_photons))
        return hitp

##
# Photon pulse generation
##

# I pulled this out of the Simulator class: caching using Memoize gave me trouble on methods due to the self argument
# There's still a method pmt_pulse_current, but it just calls pmt_pulse_current_raw defined below

@Memoize
def double_exp_pmt_pulse_current_raw(offset, dt, samples_before, samples_after, tr, tf):
    pmt_pulse = partial(exp_pulse, q=units.electron_charge, tr=tr, tf=tf)
    return digitizer_response(pmt_pulse, offset, dt, samples_before, samples_after)

@Memoize
def custom_pmt_pulse_current(pmt_pulse, offset, dt, samples_before, samples_after):
    return digitizer_response(pmt_pulse, offset, dt, samples_before, samples_after)

def digitizer_response(pmt_pulse, offset, dt, samples_before, samples_after):
    """Get the output of pmt_pulse(t) on a digitizer with sampling size dt.
    :param pmt_pulse: function that accepts a numpy array of times.
    :param offset: Offset of the digitizer time grid (number in [0, dt])
    :param dt: sampling size (ns)
    :param samples_before: number of samples before the maximum to simulate
    :param samples_after: number of samples after the maximum to simulate
    """
    return np.diff(pmt_pulse(
        np.linspace(
            - offset - samples_before * dt,
            - offset + samples_after * dt,
            1 + samples_before + samples_after)
    )) / dt

@np.vectorize
def exp_pulse(t, q, tr, tf):
    """Integrated current (i.e. charge) of a single-pe PMT pulse centered at t=0
    Assumes an exponential rise and fall waveform model
    :param t:   Time to integrate up to
    :param q:   Total charge in the pulse
    :param tr:  Rise time
    :param tf:  Fall time
    :return: Float, charge deposited up to t
    """
    c = 0.45512  # 1/(ln(10)-ln(10/9))
    if t < 0:
        return q / (tr + tf) * (tr * math.exp(t / (c * tr)))
    else:
        return q / (tr + tf) * (tr + tf * (1 - math.exp(-t / (c * tf))))

##
# PMT gain sampling (if no special distribution is provided)
##

@Memoize
def _truncated_gauss(my_mean, my_std, left_boundary, right_boundary):
    """NB: the mean & std are only used to fix the boundaries, this is still a standardized normal otherwise!"""
    return stats.truncnorm(
        (left_boundary - my_mean) / my_std,
        (right_boundary - my_mean) / my_std)

def truncated_gauss_rvs(my_mean, my_std, left_boundary, right_boundary, n_rvs):
    """Get Gauss with specified mean and std, truncated to boundaries
    See http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.truncnorm.html
    """
    if my_std != 0:
        return _truncated_gauss(my_mean, my_std, left_boundary, right_boundary).rvs(n_rvs) * my_std + my_mean
    else:
        print("improper STD for truncated Gaussian")
        return [my_mean] * n_rvs


text:
const express = require('express');
const compression = require('compression');
const fs = require('fs');
const app = express();

import Schema from './src/schema.js';
import { graphql } from 'graphql';

const bodyParser = require('body-parser');
const indexFile = fs.readFileSync('index.html', 'utf8');

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())

app.post('/graphql', function(req, res) {

  var query = req.body.query.replace("\n", "");
  graphql(Schema, query, null, req.body.queryVariables).then(function(data) {
    res.status(200).send(data);
  }).catch(function(err) {
    console.log(err);
    res.status(500).send(err);
  })

});

var port = 8080;
app.listen(port);
console.log("App started on port: " + port);


text:
package org.dcm4chee.web.dao.util;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import org.dcm4che2.data.BasicDicomObject;
import org.dcm4che2.data.DicomElement;
import org.dcm4che2.data.DicomObject;
import org.dcm4che2.data.DicomObject.Visitor;
import org.dcm4che2.data.Tag;
import org.dcm4che2.data.VR;
import org.dcm4chee.archive.entity.Instance;
import org.dcm4chee.archive.entity.MPPS;
import org.dcm4chee.web.dao.vo.EntityTree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Franz Willer <fwiller@gmail.com>
 * @version $Revision$ $Date$
 * @since Aug 25, 2013
 */

public class IOCMUtil {

    private static String[] contributingEquipment = new String[3];

    private static Logger log = LoggerFactory.getLogger(IOCMUtil.class);

    public static String[] getContributingEquipment() {
        return contributingEquipment;
    }

    public static void setContributingEquipment(String[] equipment) {
        contributingEquipment = equipment;
    }

    public static DicomObject changeUID(EntityTree tree, DicomObject attrs, int tag) {
        String uid = attrs.getString(tag);
        attrs.putString(tag, VR.UI, tree.getChangedUID(uid));
        return attrs;
    }

    public static DicomObject addReplacementAttrs(DicomObject obj) {
        if (obj != null) {
            DicomObject item = new BasicDicomObject();
            item.putString(Tag.InstitutionName, VR.LO, contributingEquipment[0]);
            item.putString(Tag.StationName, VR.SH, contributingEquipment[1]);
            item.putString(Tag.Manufacturer, VR.LO, contributingEquipment[2]);
            item.putDate(Tag.ContributionDateTime, VR.DT, new Date());
            DicomElement sq = obj.get(Tag.ContributingEquipmentSequence);
            if (sq == null)
                sq = obj.putSequence(Tag.ContributingEquipmentSequence);
            sq.addDicomObject(item);
        }
        return obj;
    }

    public static void updateUIDrefs(EntityTree tree, EntityManager em) {
        Map<String, String> uidMap = tree.getOld2NewUIDMap();
        if (uidMap.isEmpty()) {
           log.debug("UID mapping empty! nothing to do");
           return;
        }
        for (Instance i : tree.getAllInstances()) {
            DicomObject attrs = i.getAttributes(false);
            if (updateUIDs(attrs, uidMap)) {
                i.setAttributes(attrs);
                em.merge(i);
            }
        }

    }
    public static void updateUIDrefs(List<MPPS> mppsList, EntityTree tree, EntityManager em) {
        Map<String, String> uidMap = tree.getOld2NewUIDMap();
        if (uidMap.isEmpty()) {
           log.debug("UID mapping empty! nothing to do");
           return;
        }
        for(MPPS mpps : mppsList) {
            DicomObject attrs = mpps.getAttributes();
            if (updateUIDs(attrs, uidMap)) {
                mpps.setAttributes(attrs);
                em.merge(mpps);
            }
        }
    }

    public static boolean updateUIDs(DicomObject attrs, final Map<String, String> uidMap) {
        return updateUIDsInSequence(attrs, uidMap);
    }

    private static boolean updateUIDsInSequence(DicomObject attrs, final Map<String, String> uidMap) {
        final boolean[] changed = new boolean[]{false};
        attrs.accept(new Visitor() {
            public boolean visit(DicomElement attr) {
                if (attr.vr() == VR.SQ) {
                    for (int i = 0, len = attr.countItems(); i < len ; i++) {
                        changed[0] |= updateUIDsInSequence(attr.getDicomObject(i), uidMap);
                    }
                }
                return true;
            }
        });
        if (!changed[0]) {
            changed[0] |= updateUID(attrs, Tag.StudyInstanceUID, uidMap);
            changed[0] |= updateUID(attrs, Tag.SeriesInstanceUID, uidMap);
        }
        changed[0] |= updateUID(attrs, Tag.SOPInstanceUID, uidMap);
        changed[0] |= updateUID(attrs, Tag.ReferencedSOPInstanceUID, uidMap);
        changed[0] |= updateUID(attrs, Tag.UID, uidMap);
        return changed[0];
    }

    public static boolean updateUID(DicomObject attrs, int tag, Map<String, String> uidMap) {
        String uid = uidMap.get(attrs.getString(tag));
        if (uid != null) {
            attrs.putString(tag, VR.UI, uid);
            return true;
        } else {
            return false;
        }
    }
}


text:
					<button type="button" class="close" ng-click="addChapterController.close(false,{})" data-dismiss="modal" aria-hidden="true">×</button>

Add New Chapter

				<form name="addChapterControllerForm" novalidate>

						Title *

							<input name = "title" ng-required="true" type="text" data-ng-model="addChapterController.bookDetails.title" class="form-control col-md-8 col-xs-13"/>

Please Enter Chapter Title

						Chapter Summary *

							<textarea type="text" ng-required="true"  data-ng-model="addChapterController.bookDetails.summary" class="form-control col-md-8 col-xs-13" name= "description">
							</textarea>

Please Enter Book Summary

						<button class="btn btn-primary"  data-dismiss="modal" aria-hidden="true" ng-disabled="addChapterControllerForm.$invalid" ng-click="addChapterController.close(addChapterControllerForm.$valid, addChapterController.bookDetails)">Add Sections and Questions  </button>

				</form>


text:
package sirs.easyvote.shared;

import java.io.Serializable;
import java.util.List;

public class BallotSheetView implements Serializable {
	private static final long serialVersionUID = -4144073679780624818L;
	private Long electionId;
	private List<CandidateView> candidates;
	private String safeOffset;
	private String question;

	public BallotSheetView() {

	}

	public void setCandidates(List<CandidateView> candidates) {
		this.candidates = candidates;
	}

	public List<CandidateView> getCandidates() {
		return candidates;
	}

	public void setSafeOffset(String safeOffset) {
		this.safeOffset = safeOffset;
	}

	public String getSafeOffset() {
		return safeOffset;
	}

	public void setElectionId(Long electionId) {
		this.electionId = electionId;
	}

	public Long getElectionId() {
		return electionId;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public String getQuestion() {
		return question;
	}
}


text:
#ifndef GrDawnOpsRenderPass_DEFINED
#define GrDawnOpsRenderPass_DEFINED

#include "src/gpu/GrOpsRenderPass.h"

#include "include/gpu/GrTypes.h"
#include "src/gpu/GrColor.h"
#include "dawn/webgpu_cpp.h"

class GrDawnGpu;
class GrDawnRenderTarget;
struct GrDawnProgram;

class GrDawnOpsRenderPass : public GrOpsRenderPass {
public:
    GrDawnOpsRenderPass(GrDawnGpu*, GrRenderTarget*, GrSurfaceOrigin,
                        const LoadAndStoreInfo&, const StencilLoadAndStoreInfo&);

    ~GrDawnOpsRenderPass() override;

    wgpu::RenderPassEncoder beginRenderPass(wgpu::LoadOp colorOp, wgpu::LoadOp stencilOp);

    void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override;

    void submit();

private:
    GrGpu* gpu() override;

    void applyState(GrDawnProgram*, const GrProgramInfo& programInfo);

    void onEnd() override;
    bool onBindPipeline(const GrProgramInfo& programInfo, const SkRect& drawBounds) override;
    void onSetScissorRect(const SkIRect&) override;
    bool onBindTextures(const GrPrimitiveProcessor&, const GrSurfaceProxy* const primProcTextures[],
                        const GrPipeline&) override;
    void onBindBuffers(const GrBuffer* indexBuffer, const GrBuffer* instanceBuffer,
                       const GrBuffer* vertexBuffer, GrPrimitiveRestart) override;
    void onDraw(int vertexCount, int baseVertex) override;
    void onDrawIndexed(int indexCount, int baseIndex, uint16_t minIndexValue,
                       uint16_t maxIndexValue, int baseVertex) override;
    void onDrawInstanced(int instanceCount, int baseInstance, int vertexCount,
                         int baseVertex) override;
    void onDrawIndexedInstanced(int indexCount, int baseIndex, int instanceCount, int baseInstance,
                                int baseVertex) override;

    void onClear(const GrFixedClip&, const SkPMColor4f& color) override;

    void onClearStencilClip(const GrFixedClip&, bool insideStencilMask) override;

    struct InlineUploadInfo {
        InlineUploadInfo(GrOpFlushState* state, const GrDeferredTextureUploadFn& upload)
                : fFlushState(state), fUpload(upload) {}

        GrOpFlushState* fFlushState;
        GrDeferredTextureUploadFn fUpload;
    };

    GrDawnGpu*                  fGpu;
    wgpu::CommandEncoder        fEncoder;
    wgpu::RenderPassEncoder     fPassEncoder;
    sk_sp<GrDawnProgram>        fCurrentProgram;
    LoadAndStoreInfo            fColorInfo;

    typedef GrOpsRenderPass     INHERITED;
};

#endif


text:
<?php

namespace Adrenth\Redirect\Updates;

use Illuminate\Database\Schema\Blueprint;
use Schema;
use October\Rain\Database\Updates\Migration;

/**
 * Class RemovePublishStatusColumnRedirectsTable
 *
 * @package Adrenth\Redirect\Updates
 */
class RemovePublishStatusColumnRedirectsTable extends Migration
{
    const TABLE = 'adrenth_redirect_redirects';

    public function up()
    {
        Schema::table(self::TABLE, function (Blueprint $table) {
            $table->dropColumn('publish_status');
        });
    }

    public function down()
    {
        Schema::table(self::TABLE, function (Blueprint $table) {
            $table->boolean('publish_status')->default(false);
        });
    }
}


text:
package org.psjava.ds.tree.trie;

import org.psjava.util.ConvertedIterable;
import org.psjava.util.Converter;
import org.psjava.util.StringMerger;

public class Trie<T> {

    private final TrieNodeFactory<T> factory;
    private final TrieNode<T> root;

    public Trie(TrieNodeFactory<T> nodeFactory) {
        this.factory = nodeFactory;
        root = factory.create();
    }

    public void add(Iterable<T> sequence) {
        TrieNode<T> cur = root;
        for (T v : sequence) {
            TrieNode<T> subTrie = cur.getChildOrNull(v);
            if (subTrie == null) {
                subTrie = factory.create();
                cur.putChild(v, subTrie);
            }
            cur = subTrie;
        }
    }

    public TrieNode<T> getRoot() {
        return root;
    }

    @Override
    public String toString() {
        return getNodeString(root);
    }

    private String getNodeString(final TrieNode<T> node) {
        String r = "";
        if (node == root)
            r += "Trie";
        if (node.getChildCount() > 0) {
            r += "(";
            r += StringMerger.merge(ConvertedIterable.create(node.getEdges(), new Converter<T, String>() {
                @Override
                public String convert(T c) {
                    return c + getNodeString(node.getChild(c));
                }
            }), ",");
            r += ")";
        }
        return r;
    }

}


text:
module Taf
  # test_engine.rb - controls the iteration through the test suite and specs
  module TestEngine
    def self.get_test_data(test_file_name)
      # read in the test data
      tests = Taf::Parser.read_test_data(test_file_name)
      tests
    # if unable to read the test data, show the error and move onto the
    # next file (if there is one)
    rescue StandardError => e
      Taf::MyLog.log.warn 'Terminating the current test spec: ' \
                   "#{test_file_name} #{e}"
      Taf::MyLog.log.info
      '...continuing with the next test file (if there is one)'
    end

    def self.get_step_data(test_file_name, tests, metrics)
      tests['steps'].each_with_index do |test_step, test_step_idx|
        test_step_idx += 1

        parsed_steps = Taf::Parser.parse_test_step_data(test_step)

        # process the test step data
        Taf::TestSteps.process_test_steps(test_file_name, test_step_idx,
                                          parsed_steps, metrics)
      end
    rescue Taf::TafError => e
      warn e
      Taf::MyLog.log.warn e
    end

    # process the test files to execute the tests
    def self.process_testfiles
      total_passes = 0
      total_failures = 0
      total_skipped = 0

      # loop through all the available test files to execute the tests
      Taf::Parser.test_files.each_with_index do |test_file_name, test_file_idx|
        metrics = Struct.new(:stepPasses, :stepFailures, :stepSkipped)
                        .new(0, 0, 0)

        tests = get_test_data(test_file_name)

        # create project folders - these only need creating once per test suite
        Taf::CreateDirectories.construct_projectdirs

        Taf::Browser.open_browser
        get_step_data(test_file_name, tests, metrics)

        # output the test results summary to console
        Taf::ReportSummary.test_step_summary(test_file_name, test_file_idx,
                                             metrics)

        # close the browser if created
        Taf::Browser.b.quit

        # record total passes and failures and reset the failure counters for
        # the test steps
        total_passes   += metrics.stepPasses
        total_failures += metrics.stepFailures
        total_skipped  += metrics.stepSkipped
      end

      [total_passes, total_failures, total_skipped]
    end
  end
end


text:
var path = require('path');
var http = require('http');
var https = require('https');
var testUtil = require('./lib/testutil.js');
var requestUtil = require(path.join(testUtil.libdir, 'requestutil.js'));

module.exports = {
  simpleRequest: function(beforeExit, assert) {
    var e = null;
    var d = null;
    var done = false;
    beforeExit(function() {
      assert.equal(e, null);
      assert.notEqual(d, null);
      assert.equal(JSON.parse(d).id, '1055572299');
      assert.ok(done);
    });

    var req = new requestUtil.requestFacebookApi(https, 'graph.facebook.com', 443, '/amachang', { method: 'GET' }, false, function(err, data) {
      e = err;
      d = data;
      done = true;
    });
  },

  simpleMultipartRequest: function(beforeExit, assert) {
    var e = null;
    var d = null;
    var done = false;
    beforeExit(function() {
      assert.equal(e, null);
      assert.notEqual(d, null);
      assert.equal(JSON.parse(d).id, '1055572299');
      assert.ok(done);
    });

    var req = new requestUtil.requestFacebookApi(https, 'graph.facebook.com', 443, '/amachang', { method: 'GET' }, true, function(err, data) {
      e = err;
      d = data;
      done = true;
    });
  },

  constructorAndStart: function(beforeExit, assert) {
    var e = null;
    var d = null;
    var done = false;
    beforeExit(function() {
      assert.equal(e, null);
      assert.notEqual(d, null);
      assert.equal(JSON.parse(d).id, '1055572299');
      assert.ok(done);
    });

    var req = new requestUtil.FacebookApiRequest(https, 'graph.facebook.com', 443, '/amachang', { method: 'GET' });
    req.start(false, function(err, data) {
      e = err;
      d = data;
      done = true;
    });
  },

  responseError: function(beforeExit, assert) {
    var e = null;
    var d = null;
    var done = false;
    beforeExit(function() {
      assert.notEqual(e, null);
      assert.equal(d, null);
      assert.equal(e.code, 'ENOTFOUND');
      assert.ok(done);
    });

    var req = new requestUtil.FacebookApiRequest(http, 'notfound.example.com', 80, '/', { method: 'GET' });

    req.start(false, function(err, data) {
      e = err;
      d = data;
      done = true;
    });
  },

  throwErrorAfterResponse: function(beforeExit, assert) {
    var e = null;
    var d = null;
    var done = false;
    beforeExit(function() {
      assert.notEqual(e, null);
      assert.equal(d, null);
      assert.equal(e.message, 'addListener only takes instances of Function');
      assert.ok(done);
    });

    var req = new requestUtil.FacebookApiRequest(https, 'graph.facebook.com', 443, '/amachang', { method: 'GET' });

    // break process
    req.selfBoundDataHandler = null;
    req.start(false, function(err, data) {
      e = err;
      d = data;
      done = true;
    });
  },

  dataError: function(beforeExit, assert) {
    var e = null;
    var d = null;
    var done = false;
    beforeExit(function() {
      assert.notEqual(e, null);
      assert.equal(d, null);
      assert.equal(e.message, 'dummy');
      assert.ok(done);
    });

    var req = new requestUtil.FacebookApiRequest(https, 'graph.facebook.com', 443, '/amachang', { method: 'GET' });

    req.afterResponse_ = req.afterResponse;
    req.afterResponse = function() {
      process.nextTick(function() {
        req.handleDataError(new Error('dummy'));
      });
      return req.afterResponse_.apply(this, arguments);
    };

    req.start(false, function(err, data) {
      e = err;
      d = data;
      done = true;
    });
  },

  throwErrorInEndData: function(beforeExit, assert) {
    var e = null;
    var d = null;
    var done = false;
    beforeExit(function() {
      assert.notEqual(e, null);
      assert.equal(d, null);
      assert.ok(done);
    });

    var req = new requestUtil.FacebookApiRequest(https, 'graph.facebook.com', 443, '/amachang', { method: 'GET' });

    req.detachDataAndEndAndErrorHandlers = null;
    req.start(false, function(err, data) {
      e = err;
      d = data;
      done = true;
    });
  }
};


text:
#ifndef SOEM_EL1xxx_H
#define SOEM_EL1xxx_H

#include <soem_master/soem_driver.h>
#include <soem_beckhoff_drivers/DigitalMsg.h>
#include <rtt/Port.hpp>
#include <bitset>

namespace soem_beckhoff_drivers{

  class SoemEL1xxx : public soem_master::SoemDriver
  {

    typedef struct PACKED
    {
      uint8 bits;
    } out_el1xxxt;

  public:
    SoemEL1xxx(ec_slavet* mem_loc);
    ~SoemEL1xxx(){};

    bool isOn( unsigned int bit = 0) const;
    bool isOff( unsigned int bit = 0) const;
    bool readBit( unsigned int bit = 0) const;

    void update();
    bool configure(){return true;}

  private:
    unsigned int m_size;
    DigitalMsg m_msg;
    std::bitset<8> m_bits;
    RTT::OutputPort<DigitalMsg> m_port;

};

}
#endif