def _ConvertRowToUnicode(self, parser_mediator, row):
    for (key, value) in iter(row.items()):
        if isinstance(value, py2to3.UNICODE_TYPE):
            continue
        try:
            row[key] = value.decode(self._encoding)
        except UnicodeDecodeError:
            replaced_value = value.decode(self._encoding, errors='replace')
            parser_mediator.ProduceExtractionWarning('error decoding DSV value: {0:s} as {1:s}, characters have been replaced in {2:s}'.format(key, self._encoding, replaced_value))
            row[key] = replaced_value
    return row

Converts all strings in a DSV row dict to Unicode.

Args:
        parser_mediator (ParserMediator): mediates interactions between parsers
        and other components, such as storage and dfvfs.
        row (dict[str, bytes]): a row from a DSV file, where the dictionary
        key contains the column name and the value a binary string.

Returns:
        dict[str, str]: a row from the DSV file, where the dictionary key
        contains the column name and the value a Unicode string.


def get_model(self, model, model_id):
    return self._store.find_record(self._get_model_class(model), int(model_id))

Get a single model from the server.

Args:
        model (string): The class as a string.
        model_id (string): The integer ID as a string.

Returns:
        :class:`cinder_data.model.CinderModel`: A instance of the model.


def get_contact(self, response=None, nir=None, handle=None, retry_count=3, dt_format=None):
    if (response or (nir == 'krnic')):
        contact_response = response
    else:
        contact_response = self._net.get_http_raw(url=str(NIR_WHOIS[nir]['url']).format(handle), retry_count=retry_count, headers=NIR_WHOIS[nir]['request_headers'], request_type=NIR_WHOIS[nir]['request_type'])
    return self.parse_fields(response=contact_response, fields_dict=NIR_WHOIS[nir]['contact_fields'], dt_format=dt_format, hourdelta=int(NIR_WHOIS[nir]['dt_hourdelta']), is_contact=True)

The function for retrieving and parsing NIR whois data based on
    NIR_WHOIS contact_fields.

Args:
        response (:obj:`str`): Optional response object, this bypasses the
            lookup.
        nir (:obj:`str`): The NIR to query ('jpnic' or 'krnic'). Required
            if response is None.
        handle (:obj:`str`): For NIRs that have separate contact queries
            (JPNIC), this is the contact handle to use in the query.
            Defaults to None.
        retry_count (:obj:`int`): The number of times to retry in case
            socket errors, timeouts, connection resets, etc. are
            encountered. Defaults to 3.
        dt_format (:obj:`str`): The format of datetime fields if known.
            Defaults to None.

Returns:
        dict: Mapping of the fields provided in contact_fields, to their
            parsed results.


def list(self, teamId, max=None, **request_parameters):
    check_type(teamId, basestring, may_be_none=False)
    check_type(max, int)
    params = dict_from_items_with_values(request_parameters, teamId=teamId, max=max)
    items = self._session.get_items(API_ENDPOINT, params=params)
    for item in items:
        (yield self._object_factory(OBJECT_TYPE, item))

List team memberships for a team, by ID.

    This method supports Webex Teams's implementation of RFC5988 Web
    Linking to provide pagination support.  It returns a generator
    container that incrementally yields all team memberships returned by
    the query.  The generator will automatically request additional 'pages'
    of responses from Webex as needed until all responses have been
    returned. The container makes the generator safe for reuse.  A new API
    call will be made, using the same parameters that were specified when
    the generator was created, every time a new iterator is requested from
    the container.

Args:
        teamId(basestring): List team memberships for a team, by ID.
        max(int): Limit the maximum number of items returned from the Webex
            Teams service per request.
        **request_parameters: Additional request parameters (provides
            support for parameters that may be added in the future).

Returns:
        GeneratorContainer: A GeneratorContainer which, when iterated,
        yields the team memberships returned by the Webex Teams query.

Raises:
        TypeError: If the parameter types are incorrect.
        ApiError: If the Webex Teams cloud returns an error.


def set_flowcontrol_receive(self, name, value=None, default=False, disable=False):
    return self.set_flowcontrol(name, 'receive', value, default, disable)

Configures the interface flowcontrol receive value

Args:
        name (string): The interface identifier.  It must be a full
            interface name (ie Ethernet, not Et)

        value (boolean): True if the interface should enable receiving
            flow control packets, otherwise False

        default (boolean): Specifies to default the interface flow
            control receive value

        disable (boolean): Specifies to disable the interface flow
            control receive value

Returns:
        True if the operation succeeds otherwise False is returned


def set_tick(self, index, interval):
    name = self.tick_name(index)
    if (name is None):
        return pack_error(ControllerSubsystem.SENSOR_GRAPH, Error.INVALID_ARRAY_KEY)
    self.ticks[name] = interval
    return Error.NO_ERROR

Update the a tick's interval.

Args:
        index (int): The index of the tick that you want to fetch.
        interval (int): The number of seconds between ticks.
            Setting this to 0 will disable the tick.

Returns:
        int: An error code.


def receive_data(socket):
    answer = b''
    while True:
        packet = socket.recv(4096)
        if (not packet):
            break
        answer += packet
    response = pickle.loads(answer)
    socket.close()
    return response

Receive an answer from the daemon and return the response.

Args:
    socket (socket.socket): A socket that is connected to the daemon.

Returns:
        dir or string: The unpickled answer.


def as_money(self, number, **options):
    if isinstance(number, list):
        return map((lambda val: self.as_money(val, **options)))
    decimal = options.get('decimal')
    number = self.parse(number, decimal)
    if check_type(options, 'dict'):
        options = self.settings['currency'].update(options)
    formats = self._check_currency_format(options['format'])
    use_format = (lambda num: (formats['pos'] if (num > 0) else (formats['neg'] if (num < 0) else formats['zero'])))(number)
    precision = self._change_precision(number, options['precision'])
    thousands = options['thousand']
    decimal = options['decimal']
    formater = self.format(abs(number), precision, thousands, decimal)
    amount = use_format.replace('%s', options['symbol']).replace('%v', formater)
    return amount

Format a number into currency.

    Usage: accounting.formatMoney(number, symbol, precision, thousandsSep,
                                decimalSep, format)
    defaults: (0, "$", 2, ",", ".", "%s%v")
    Localise by overriding the symbol, precision,
    thousand / decimal separators and format
    Second param can be an object matching `settings.currency`
    which is the easiest way.

Args:
        number (TYPE): Description
        precision (TYPE): Description
        thousand (TYPE): Description
        decimal (TYPE): Description

Returns:
        name (TYPE): Description


def _rectify_countdown_or_bool(count_or_bool):
    if ((count_or_bool is True) or (count_or_bool is False)):
        count_or_bool_ = count_or_bool
    elif isinstance(count_or_bool, int):
        if (count_or_bool == 0):
            return 0
        elif (count_or_bool > 0):
            count_or_bool_ = (count_or_bool - 1)
        else:
            count_or_bool_ = count_or_bool
    else:
        count_or_bool_ = False
    return count_or_bool_

used by recursive functions to specify which level to turn a bool on in
    counting down yields True, True, ..., False
    counting up yields False, False, False, ... True

Args:
        count_or_bool (bool or int): if positive and an integer, it will count
            down, otherwise it will remain the same.

Returns:
        int or bool: count_or_bool_

    CommandLine:
        python -m utool.util_str --test-_rectify_countdown_or_bool

Example:
        >>> from ubelt.util_format import _rectify_countdown_or_bool  # NOQA
        >>> count_or_bool = True
        >>> a1 = (_rectify_countdown_or_bool(2))
        >>> a2 = (_rectify_countdown_or_bool(1))
        >>> a3 = (_rectify_countdown_or_bool(0))
        >>> a4 = (_rectify_countdown_or_bool(-1))
        >>> a5 = (_rectify_countdown_or_bool(-2))
        >>> a6 = (_rectify_countdown_or_bool(True))
        >>> a7 = (_rectify_countdown_or_bool(False))
        >>> a8 = (_rectify_countdown_or_bool(None))
        >>> result = [a1, a2, a3, a4, a5, a6, a7, a8]
        >>> print(result)
        [1, 0, 0, -1, -2, True, False, False]


def entanglement_of_formation(state, d0, d1=None):
    state = np.array(state)
    if (d1 is None):
        d1 = int((len(state) / d0))
    if ((state.ndim == 2) and (len(state) == 4) and (d0 == 2) and (d1 == 2)):
        return __eof_qubit(state)
    elif (state.ndim == 1):
        if (d0 < d1):
            tr = [1]
        else:
            tr = [0]
        state = partial_trace(state, tr, dimensions=[d0, d1])
        return entropy(state)
    else:
        print('Input must be a state-vector or 2-qubit density matrix.')
    return None

Compute the entanglement of formation of quantum state.

    The input quantum state must be either a bipartite state vector, or a
    2-qubit density matrix.

Args:
        state (array_like): (N) array_like or (4,4) array_like, a
            bipartite quantum state.
        d0 (int): the dimension of the first subsystem.
        d1 (int or None): the dimension of the second subsystem.

Returns:
        float: The entanglement of formation.


def _ExtractYandexSearchQuery(self, url):
    if ('text=' not in url):
        return None
    (_, _, line) = url.partition('text=')
    (before_and, _, _) = line.partition('&')
    if (not before_and):
        return None
    yandex_search_url = before_and.split()[0]
    return yandex_search_url.replace('+', ' ')

Extracts a search query from a Yandex search URL.

    Yandex: https://www.yandex.com/search/?text=query

Args:
        url (str): URL.

Returns:
        str: search query or None if no query was found.


def post_create_app(cls, app, **settings):
    super(MarshmallowAwareApp, cls).post_create_app(app, **settings)
    marsh.init_app(app)
    return app

Automatically register and init the Flask Marshmallow extension.

Args:
        app (flask.Flask): The application instance in which to initialize
            Flask Marshmallow upon.

    Kwargs:
        settings (dict): The settings passed to this method from the
            parent app.

Returns:
        flask.Flask: The Flask application that was passed in.


def _dataset_merge_filestore_newresource(self, new_resource, ignore_fields, filestore_resources):
    new_resource.check_required_fields(ignore_fields=ignore_fields)
    self.resources.append(new_resource)
    if new_resource.get_file_to_upload():
        filestore_resources.append(new_resource)
        new_resource['url'] = Dataset.temporary_url

Helper method to add new resource from dataset including filestore.

Args:
        new_resource (hdx.data.Resource): New resource from dataset
        ignore_fields (List[str]): List of fields to ignore when checking resource
        filestore_resources (List[hdx.data.Resource]): List of resources that use filestore (to be appended to)

Returns:
        None


def omit_wells(self, uwis=None):
    if (uwis is None):
        raise ValueError('Must specify at least one uwi')
    return Project([w for w in self if (w.uwi not in uwis)])

Returns a new project where wells with specified uwis have been omitted

Args:
        uwis (list): list or tuple of UWI strings.

Returns:
        project


def download_url(self, url, **kwargs):
    if (self.baseurl and ('://' not in url)):
        url = join(self.baseurl, url)
    return self.resolver.download_to_directory(self.directory, url, **kwargs)

Download a URL to the workspace.

Args:
        url (string): URL to download to directory
        **kwargs : See :py:mod:`ocrd.resolver.Resolver`

Returns:
        The local filename of the downloaded file


def _CheckIsFile(self, file_entry):
    if (definitions.FILE_ENTRY_TYPE_FILE not in self._file_entry_types):
        return False
    return file_entry.IsFile()

Checks the is_file find specification.

Args:
        file_entry (FileEntry): file entry.

Returns:
        bool: True if the file entry matches the find specification, False if not.


def unique_flags(items, key=None):
    len_ = len(items)
    if (key is None):
        item_to_index = dict(zip(reversed(items), reversed(range(len_))))
        indices = item_to_index.values()
    else:
        indices = argunique(items, key=key)
    flags = boolmask(indices, len_)
    return flags

Returns a list of booleans corresponding to the first instance of each
    unique item.

Args:
        items (Sequence): indexable collection of items

        key (Callable, optional): custom normalization function.
            If specified returns items where `key(item)` is unique.

Returns:
        List[bool] : flags the items that are unique

Example:
        >>> import ubelt as ub
        >>> items = [0, 2, 1, 1, 0, 9, 2]
        >>> flags = unique_flags(items)
        >>> assert flags == [True, True, True, False, False, True, False]
        >>> flags = unique_flags(items, key=lambda x: x % 2 == 0)
        >>> assert flags == [True, False, True, False, False, False, False]


def compile_state_cpfs(self, scope: Dict[(str, TensorFluent)], batch_size: Optional[int]=None, noise: Optional[Noise]=None) -> List[CPFPair]:
    next_state_fluents = []
    with self.graph.as_default():
        with tf.name_scope('state_cpfs'):
            for cpf in self.rddl.domain.state_cpfs:
                cpf_noise = (noise.get(cpf.name, None) if (noise is not None) else None)
                name_scope = utils.identifier(cpf.name)
                with tf.name_scope(name_scope):
                    t = self._compile_expression(cpf.expr, scope, batch_size, cpf_noise)
                next_state_fluents.append((cpf.name, t))
            key = (lambda f: self.rddl.domain.next_state_fluent_ordering.index(f[0]))
            next_state_fluents = sorted(next_state_fluents, key=key)
    return next_state_fluents

Compiles the next state fluent CPFs given the current `state` and `action` scope.

Args:
        scope (Dict[str, :obj:`rddl2tf.fluent.TensorFluent`]): The fluent scope for CPF evaluation.
        batch_size (Optional[int]): The batch size.

Returns:
        A list of state fluent CPFs compiled to :obj:`rddl2tf.fluent.TensorFluent`.


def compute_advantages(rollout, last_r, gamma=0.9, lambda_=1.0, use_gae=True):
    traj = {}
    trajsize = len(rollout[SampleBatch.ACTIONS])
    for key in rollout:
        traj[key] = np.stack(rollout[key])
    if use_gae:
        assert (SampleBatch.VF_PREDS in rollout), 'Values not found!'
        vpred_t = np.concatenate([rollout[SampleBatch.VF_PREDS], np.array([last_r])])
        delta_t = ((traj[SampleBatch.REWARDS] + (gamma * vpred_t[1:])) - vpred_t[:(- 1)])
        traj[Postprocessing.ADVANTAGES] = discount(delta_t, (gamma * lambda_))
        traj[Postprocessing.VALUE_TARGETS] = (traj[Postprocessing.ADVANTAGES] + traj[SampleBatch.VF_PREDS]).copy().astype(np.float32)
    else:
        rewards_plus_v = np.concatenate([rollout[SampleBatch.REWARDS], np.array([last_r])])
        traj[Postprocessing.ADVANTAGES] = discount(rewards_plus_v, gamma)[:(- 1)]
        traj[Postprocessing.VALUE_TARGETS] = np.zeros_like(traj[Postprocessing.ADVANTAGES])
    traj[Postprocessing.ADVANTAGES] = traj[Postprocessing.ADVANTAGES].copy().astype(np.float32)
    assert all(((val.shape[0] == trajsize) for val in traj.values())), 'Rollout stacked incorrectly!'
    return SampleBatch(traj)

Given a rollout, compute its value targets and the advantage.

Args:
        rollout (SampleBatch): SampleBatch of a single trajectory
        last_r (float): Value estimation for last observation
        gamma (float): Discount factor.
        lambda_ (float): Parameter for GAE
        use_gae (bool): Using Generalized Advantage Estamation

Returns:
        SampleBatch (SampleBatch): Object with experience from rollout and
            processed rewards.


def db_dict(c):
    db_d = {}
    c.execute('SELECT * FROM library_spectra')
    db_d['library_spectra'] = [list(row) for row in c]
    c.execute('SELECT * FROM library_spectra_meta')
    db_d['library_spectra_meta'] = [list(row) for row in c]
    c.execute('SELECT * FROM library_spectra_annotation')
    db_d['library_spectra_annotations'] = [list(row) for row in c]
    c.execute('SELECT * FROM library_spectra_source')
    db_d['library_spectra_source'] = [list(row) for row in c]
    c.execute('SELECT * FROM metab_compound')
    db_d['metab_compound'] = [list(row) for row in c]
    return db_d

Get a dictionary of the library spectra from a database

Example:
        >>> from msp2db.db import get_connection
        >>> conn = get_connection('sqlite', 'library.db')
        >>> test_db_d = db_dict(conn.cursor())

    If using a large database the resulting dictionary will be very large!

Args:
        c (cursor): SQL database connection cursor

Returns:
        A dictionary with the following keys 'library_spectra', 'library_spectra_meta', 'library_spectra_annotations',
        'library_spectra_source' and 'metab_compound'. Where corresponding values for each key are list of list containing
        all the rows in the database.


def parse_response(service, response, search_type):
    _LOG.debug('Parse response "%s" from service "%s" of type "%s"', response, service, search_type)
    items = []
    if ('searchResult' in response):
        response = response['searchResult']
    elif ('getMetadataResult' in response):
        response = response['getMetadataResult']
    else:
        raise ValueError('"response" should contain either the key "searchResult" or "getMetadataResult"')
    search_metadata = {'number_returned': response['count'], 'total_matches': None, 'search_type': search_type, 'update_id': None}
    for result_type in ('mediaCollection', 'mediaMetadata'):
        result_type_proper = (result_type[0].upper() + result_type[1:])
        raw_items = response.get(result_type, [])
        if isinstance(raw_items, OrderedDict):
            raw_items = [raw_items]
        for raw_item in raw_items:
            class_key = (result_type_proper + raw_item['itemType'].title())
            cls = get_class(class_key)
            items.append(cls.from_music_service(service, raw_item))
    return SearchResult(items, **search_metadata)

Parse the response to a music service query and return a SearchResult

Args:
        service (MusicService): The music service that produced the response
        response (OrderedDict): The response from the soap client call
        search_type (str): A string that indicates the search type that the
            response is from

Returns:
        SearchResult: A SearchResult object


def clean(self, settings):
    return {k: v for (k, v) in settings.items() if (k in DEFAULT_SETTINGS)}

Filter given settings to keep only key names available in
    ``DEFAULT_SETTINGS``.

Args:
        settings (dict): Loaded settings.

Returns:
        dict: Settings object filtered.


def imrescale(img, scale, return_scale=False, interpolation='bilinear'):
    (h, w) = img.shape[:2]
    if isinstance(scale, (float, int)):
        if (scale <= 0):
            raise ValueError('Invalid scale {}, must be positive.'.format(scale))
        scale_factor = scale
    elif isinstance(scale, tuple):
        max_long_edge = max(scale)
        max_short_edge = min(scale)
        scale_factor = min((max_long_edge / max(h, w)), (max_short_edge / min(h, w)))
    else:
        raise TypeError('Scale must be a number or tuple of int, but got {}'.format(type(scale)))
    new_size = _scale_size((w, h), scale_factor)
    rescaled_img = imresize(img, new_size, interpolation=interpolation)
    if return_scale:
        return (rescaled_img, scale_factor)
    else:
        return rescaled_img

Resize image while keeping the aspect ratio.

Args:
        img (ndarray): The input image.
        scale (float or tuple[int]): The scaling factor or maximum size.
            If it is a float number, then the image will be rescaled by this
            factor, else if it is a tuple of 2 integers, then the image will
            be rescaled as large as possible within the scale.
        return_scale (bool): Whether to return the scaling factor besides the
            rescaled image.
        interpolation (str): Same as :func:`resize`.

Returns:
        ndarray: The rescaled image.


def get_clinvar_submission(store, institute_id, case_name, variant_id, submission_id):
    (institute_obj, case_obj) = institute_and_case(store, institute_id, case_name)
    pinned = [(store.variant(variant_id) or variant_id) for variant_id in case_obj.get('suspects', [])]
    variant_obj = store.variant(variant_id)
    clinvar_submission_objs = store.clinvars(submission_id=submission_id)
    return dict(today=str(date.today()), institute=institute_obj, case=case_obj, variant=variant_obj, pinned_vars=pinned, clinvars=clinvar_submission_objs)

Collects all variants from the clinvar submission collection with a specific submission_id

Args:
        store(scout.adapter.MongoAdapter)
        institute_id(str): Institute ID
        case_name(str): case ID
        variant_id(str): variant._id
        submission_id(str): clinvar submission id, i.e. SUB76578

Returns:
        A dictionary with all the data to display the clinvar_update.html template page


def get_thread(self, thread_id, update_if_cached=True, raise_404=False):
    cached_thread = self._thread_cache.get(thread_id)
    if cached_thread:
        if update_if_cached:
            cached_thread.update()
        return cached_thread
    res = self._requests_session.get(self._url.thread_api_url(thread_id=thread_id))
    if raise_404:
        res.raise_for_status()
    elif (not res.ok):
        return None
    thread = Thread._from_request(self, res, thread_id)
    self._thread_cache[thread_id] = thread
    return thread

Get a thread from 4chan via 4chan API.

Args:
        thread_id (int): Thread ID
        update_if_cached (bool): Whether the thread should be updated if it's already in our cache
        raise_404 (bool): Raise an Exception if thread has 404'd

Returns:
        :class:`basc_py4chan.Thread`: Thread object


def cancel(self, workflow_id):
    self.logger.debug(('Canceling workflow: ' + workflow_id))
    url = ('%(wf_url)s/%(wf_id)s/cancel' % {'wf_url': self.workflows_url, 'wf_id': workflow_id})
    r = self.gbdx_connection.post(url, data='')
    r.raise_for_status()

Cancels a running workflow.

Args:
        workflow_id (str): Workflow id.

Returns:
        Nothing


def system_repertoire_distance(r1, r2):
    if (config.MEASURE in measures.asymmetric()):
        raise ValueError('{} is asymmetric and cannot be used as a system-level irreducibility measure.'.format(config.MEASURE))
    return measures[config.MEASURE](r1, r2)

Compute the distance between two repertoires of a system.

Args:
        r1 (np.ndarray): The first repertoire.
        r2 (np.ndarray): The second repertoire.

Returns:
        float: The distance between ``r1`` and ``r2``.


def local_expert_attention(x, k, loss_coef, attention_num_experts, train=True, batch_coordinate=None, **kwargs):
    if (batch_coordinate is None):
        batch_coordinate = tf.expand_dims(coordinate_tensor(common_layers.shape_list(x)[:(- 1)], axis=0), axis=(- 1))
    with tf.variable_scope('local_expert_attention'):
        additional_dispatch_params = {'batch_coordinate': batch_coordinate}
        return expert_utils.local_moe(x, train, functools.partial(self_attention_expert, **kwargs), attention_num_experts, k=k, loss_coef=loss_coef, pass_x=True, pass_gates=False, additional_dispatch_params=additional_dispatch_params)

Attention using a mixture of experts.

    Positions sent to the same expert can attend to each other.
    The mixture of experts is "local" in that it is replicated on each
    datashard.

    local_moe flatten all batches so to avoid problems with padding (ex: all
    padding going to the same expert, self attention attending to non null
    padding tokens,...), the padding should be removed before.

Args:
    x: a Tensor with shape [batch, length, depth] or [1, batch*length, depth]
    k: The number of experts to dispatch each example to
    loss_coef: a scalar. A multiplier for the expert loss
    attention_num_experts: The number of experts to use
    train: a boolean for the current mode
    batch_coordinate (tf.Tensor): int32 tensor of shape [1, batch*length, 1]
        containing the batch ids. If None, deduced from first dim of x.
    **kwargs: Arguments to forward to self_attention_expert

Returns:
    y: a Tensor with shape [batch, length, depth]
    loss: a Scalar


def get_cases(variant_source, case_lines=None, case_type='ped', variant_type='snv', variant_mode='vcf'):
    individuals = get_individuals(variant_source=variant_source, case_lines=case_lines, case_type=case_type, variant_mode=variant_mode)
    case_objs = []
    case_ids = set()
    compressed = False
    tabix_index = False
    if variant_source.endswith('.gz'):
        logger.debug('Found compressed variant source')
        compressed = True
        tabix_file = '.'.join([variant_source, 'tbi'])
        if os.path.exists(tabix_file):
            logger.debug('Found index file')
            tabix_index = True
    if (len(individuals) > 0):
        for individual in individuals:
            case_ids.add(individual.case_id)
    else:
        case_ids = [os.path.basename(variant_source)]
    for case_id in case_ids:
        logger.info('Found case {0}'.format(case_id))
        case = Case(case_id=case_id, name=case_id, variant_source=variant_source, variant_type=variant_type, variant_mode=variant_mode, compressed=compressed, tabix_index=tabix_index)
        for individual in individuals:
            if (individual.case_id == case_id):
                logger.info('Adding ind {0} to case {1}'.format(individual.name, individual.case_id))
                case.add_individual(individual)
        case_objs.append(case)
    return case_objs

Create a cases and populate it with individuals

Args:
        variant_source (str): Path to vcf files
        case_lines (Iterable): Ped like lines
        case_type (str): Format of case lines

Returns:
        case_objs (list(puzzle.models.Case))


def apply_pending(self, panel_obj, version):
    updates = {}
    new_panel = deepcopy(panel_obj)
    new_panel['pending'] = []
    new_panel['date'] = dt.datetime.now()
    info_fields = ['disease_associated_transcripts', 'inheritance_models', 'reduced_penetrance', 'mosaicism', 'database_entry_version', 'comment']
    new_genes = []
    for update in panel_obj.get('pending', []):
        hgnc_id = update['hgnc_id']
        if (update['action'] != 'add'):
            updates[hgnc_id] = update
            continue
        info = update.get('info', {})
        gene_obj = {'hgnc_id': hgnc_id, 'symbol': update['symbol']}
        for field in info_fields:
            if (field in info):
                gene_obj[field] = info[field]
        new_genes.append(gene_obj)
    for gene in panel_obj['genes']:
        hgnc_id = gene['hgnc_id']
        if (hgnc_id not in updates):
            new_genes.append(gene)
            continue
        current_update = updates[hgnc_id]
        action = current_update['action']
        info = current_update['info']
        if (action == 'delete'):
            continue
        elif (action == 'edit'):
            for field in info_fields:
                if (field in info):
                    gene[field] = info[field]
            new_genes.append(gene)
    new_panel['genes'] = new_genes
    new_panel['version'] = float(version)
    inserted_id = None
    if (new_panel['version'] == panel_obj['version']):
        result = self.panel_collection.find_one_and_replace({'_id': panel_obj['_id']}, new_panel, return_document=pymongo.ReturnDocument.AFTER)
        inserted_id = result['_id']
    else:
        new_panel.pop('_id')
        panel_obj['is_archived'] = True
        self.update_panel(panel_obj=panel_obj, date_obj=panel_obj['date'])
        inserted_id = self.panel_collection.insert_one(new_panel).inserted_id
    return inserted_id

Apply the pending changes to an existing gene panel or create a new version of the same panel.

Args:
        panel_obj(dict): panel in database to update
        version(double): panel version to update

Returns:
        inserted_id(str): id of updated panel or the new one


def get_path_from_query_string(req):
    if (req.args.get('path') is None):
        raise exceptions.UserError('Path not found in query string')
    return req.args.get('path')

Gets path from query string

Args:
        req (flask.request): Request object from Flask

Returns:
        path (str): Value of "path" parameter from query string

Raises:
        exceptions.UserError: If "path" is not found in query string


def getmtime(self, path=None, client_kwargs=None, header=None):
    return self._getmtime_from_header(self.head(path, client_kwargs, header))

Return the time of last access of path.

Args:
        path (str): File path or URL.
        client_kwargs (dict): Client arguments.
        header (dict): Object header.

Returns:
        float: The number of seconds since the epoch
            (see the time module).


def modname_to_modpath(modname, hide_init=True, hide_main=False, sys_path=None):
    modpath = _syspath_modname_to_modpath(modname, sys_path)
    if (modpath is None):
        return None
    modpath = normalize_modpath(modpath, hide_init=hide_init, hide_main=hide_main)
    return modpath

Finds the path to a python module from its name.

    Determines the path to a python module without directly import it

    Converts the name of a module (__name__) to the path (__file__) where it is
    located without importing the module. Returns None if the module does not
    exist.

Args:
        modname (str): module filepath
        hide_init (bool): if False, __init__.py will be returned for packages
        hide_main (bool): if False, and hide_init is True, __main__.py will be
            returned for packages, if it exists.
        sys_path (list): if specified overrides `sys.path` (default None)

Returns:
        str: modpath - path to the module, or None if it doesn't exist

    CommandLine:
        python -m xdoctest.static_analysis modname_to_modpath:0
        pytest  /home/joncrall/code/xdoctest/xdoctest/static_analysis.py::modname_to_modpath:0

Example:
        >>> modname = 'xdoctest.__main__'
        >>> modpath = modname_to_modpath(modname, hide_main=False)
        >>> assert modpath.endswith('__main__.py')
        >>> modname = 'xdoctest'
        >>> modpath = modname_to_modpath(modname, hide_init=False)
        >>> assert modpath.endswith('__init__.py')
        >>> modpath = basename(modname_to_modpath('_ctypes'))
        >>> assert 'ctypes' in modpath


def fit(self, struct1, struct2):
    (struct1, struct2) = self._process_species([struct1, struct2])
    if ((not self._subset) and (self._comparator.get_hash(struct1.composition) != self._comparator.get_hash(struct2.composition))):
        return None
    (struct1, struct2, fu, s1_supercell) = self._preprocess(struct1, struct2)
    match = self._match(struct1, struct2, fu, s1_supercell, break_on_match=True)
    if (match is None):
        return False
    else:
        return (match[0] <= self.stol)

Fit two structures.

Args:
        struct1 (Structure): 1st structure
        struct2 (Structure): 2nd structure

Returns:
        True or False.


def _compile_arithmetic_expression(self, expr: Expression, scope: Dict[(str, TensorFluent)], batch_size: Optional[int]=None, noise: Optional[List[tf.Tensor]]=None) -> TensorFluent:
    etype = expr.etype
    args = expr.args
    if (len(args) == 1):
        etype2op = {'+': (lambda x: x), '-': (lambda x: (- x))}
        if (etype[1] not in etype2op):
            raise ValueError('Invalid binary arithmetic expression:\n{}'.format(expr))
        op = etype2op[etype[1]]
        x = self._compile_expression(args[0], scope, batch_size, noise)
        fluent = op(x)
    else:
        etype2op = {'+': (lambda x, y: (x + y)), '-': (lambda x, y: (x - y)), '*': (lambda x, y: (x * y)), '/': (lambda x, y: (x / y))}
        if (etype[1] not in etype2op):
            raise ValueError('Invalid binary arithmetic expression:\n{}'.format(expr))
        op = etype2op[etype[1]]
        x = self._compile_expression(args[0], scope, batch_size, noise)
        y = self._compile_expression(args[1], scope, batch_size, noise)
        fluent = op(x, y)
    return fluent

Compile an arithmetic expression `expr` into a TensorFluent
    in the given `scope` with optional batch size.

Args:
        expr (:obj:`rddl2tf.expr.Expression`): A RDDL arithmetic expression.
        scope (Dict[str, :obj:`rddl2tf.fluent.TensorFluent`]): A fluent scope.
        batch_size (Optional[size]): The batch size.

Returns:
        :obj:`rddl2tf.fluent.TensorFluent`: The compiled expression as a TensorFluent.


def self_attention_expert(x, batch_coordinate, mask_right=True, split_batch=False, attention_num_head=1, attention_kq_size=None, attention_v_size=None):
    depth = x.get_shape().as_list()[(- 1)]
    length = common_layers.shape_list(batch_coordinate)[0]
    global _expert_count
    _expert_count += 1
    length = tf.cond(tf.equal(length, 0), (lambda : tf.Print(length, [length], 'Expert {} empty: '.format(_expert_count))), (lambda : length))
    tf.summary.scalar('batch_size', length, family='experts_stats_batch_size')
    attention_kq_size = (attention_kq_size or depth)
    attention_v_size = (attention_v_size or depth)

    def length_not_null(x, batch_coordinate):
        "Branch of the graph only evaluated when length isn't null."
        bias_batch = attention_bias_coordinates(batch_coordinate)

        def add_or_set_if(prev_bias, new_bias, condition):
            'Add the bias together while considering the None case.'
            if (not condition):
                return prev_bias
            if (prev_bias is None):
                return new_bias
            return (prev_bias + new_bias)

        def mask_and_call_attention(x):
            'Function applied once for each sequence of the batch.'
            length = common_layers.shape_list(x)[1]
            bias_past = tf.reshape(attention_bias_lower_triangle(length), [length, length])
            bias = None
            bias = add_or_set_if(bias, bias_past, mask_right)
            bias = add_or_set_if(bias, bias_batch, (not split_batch))
            bias = tf.reshape(bias, [1, 1, length, length])
            return multihead_attention(x, None, bias, total_key_depth=attention_kq_size, total_value_depth=attention_v_size, output_depth=depth, num_heads=attention_num_head, dropout_rate=0.0)
        if split_batch:
            out = expert_utils.map_ids(x, batch_coordinate, mask_and_call_attention)
        else:
            x = tf.reshape(x, [1, length, depth])
            out = mask_and_call_attention(x)
            out = tf.squeeze(out, 0)
        return out
    out = tf.cond(tf.equal(length, 0), (lambda : tf.zeros(shape=[0, depth], dtype=tf.float32, name='empty_out')), (lambda : length_not_null(x, batch_coordinate)))
    return out

Implementing attention that runs inside each expert.

Args:
        x: A tensor of shape[batch, depth]. Contains representations from
        different positions, which are lexicographically ordered.
        batch_coordinate: A tensor of shape [batch, 1] containing the batch
        coordinate of each element in x. This is needed to make sure that
        positions from different sequences don't attend to each other.
        mask_right: A bool. If true, we will not attend to positions on the right,
        just as decoder self attention.
        split_batch (bool): If True, each sequence of the batch is processed
        individually on a loop. If False, the sequences are processed all at
        once and a mask is applied to isolate the sequences from each others
        attention_num_head (int): number of attention heads
        attention_kq_size (int): dimension used for the attention key, and query
        attention_v_size (int): dimension used for the attention value

Returns:
        out: A tensor of shape [batch, depth].
    example use:
    expert_utils.local_moe(
        ...
        expert_fn=functools.partial(self_attention_expert, mask_right=)
        )


def get_airport_weather(self, iata, page=1, limit=100):
    url = AIRPORT_DATA_BASE.format(iata, str(self.AUTH_TOKEN), page, limit)
    weather = self._fr24.get_airport_weather(url)
    mi = weather['sky']['visibility']['mi']
    if ((mi is not None) and (mi != 'None')):
        mi = float(mi)
        km = (mi * 1.6094)
        weather['sky']['visibility']['km'] = km
    return weather

Retrieve the weather at an airport

    Given the IATA code of an airport, this method returns the weather information.

Args:
        iata (str): The IATA code for an airport, e.g. HYD
        page (int): Optional page number; for users who are on a plan with flightradar24 they can pass in higher page numbers to get more data
        limit (int): Optional limit on number of records returned

Returns:
        A list of dicts with the data; one dict for each row of data from flightradar24

    Example::

        from pyflightdata import FlightData
        f=FlightData()
        #optional login
        f.login(myemail,mypassword)
        f.get_airport_weather('HYD')
        f.get_airport_weather('HYD',page=1,limit=10)


def getattr_sdk(attr, name):
    if inspect.isroutine(attr):
        if hasattr(attr, '_sdkmeta'):
            return attr
    raise AttributeError(name)

Filter SDK attributes

Args:
        attr(attribute): Attribute as returned by :func:`getattr`.
        name(str): Attribute name.

Returns:
        `attr` if passed.


def simple_spool_transaction(self, from_address, to, op_return, min_confirmations=6):
    ntokens = len(to)
    nfees = old_div(self._t.estimate_fee(ntokens, 2), self.fee)
    inputs = self.select_inputs(from_address, nfees, ntokens, min_confirmations=min_confirmations)
    outputs = [{'address': to_address, 'value': self.token} for to_address in to]
    outputs += [{'script': self._t._op_return_hex(op_return), 'value': 0}]
    unsigned_tx = self._t.build_transaction(inputs, outputs)
    return unsigned_tx

Utililty function to create the spool transactions. Selects the inputs,
    encodes the op_return and constructs the transaction.

Args:
        from_address (str): Address originating the transaction
        to (str): list of addresses to receive tokens (file_hash, file_hash_metadata, ...)
        op_return (str): String representation of the spoolverb, as returned by the properties of Spoolverb
        min_confirmations (int): Number of confirmations when chosing the inputs of the transaction. Defaults to 6

Returns:
        str: unsigned transaction


def tokeninfo(self, jwt):
    warnings.warn('/tokeninfo will be deprecated in future releases', DeprecationWarning)
    return self.post(url='https://{}/tokeninfo'.format(self.domain), data={'id_token': jwt}, headers={'Content-Type': 'application/json'})

Returns user profile based on the user's jwt

    Validates a JSON Web Token (signature and expiration) and returns the
    user information associated with the user id (sub property) of
    the token.

Args:
        jwt (str): User's jwt

Returns:
        The user profile.


def _validate_netconfig(self, conf):
    nets = conf.get('nets', {})
    if (len(nets) == 0):
        raise LagoInitException('No networks configured.')
    no_mgmt_dns = [name for (name, net) in nets.iteritems() if ((net.get('management', None) is None) and (net.get('main_dns') or net.get('dns_domain_name')))]
    if ((len(no_mgmt_dns) > 0) and (len(nets.keys()) > 1)):
        raise LagoInitException('Networks: {0}, misconfigured, they are not marked as management, but have DNS attributes. DNS is supported only in management networks.'.format(','.join(no_mgmt_dns)))
    for (dom_name, dom_spec) in conf['domains'].items():
        mgmts = []
        for nic in dom_spec['nics']:
            net = self._get_net(conf, dom_name, nic)
            if (net.get('management', False) is True):
                mgmts.append(nic['net'])
        if (len(mgmts) == 0):
            raise LagoInitException('VM {0} has no management network, please connect it to one.'.format(dom_name))
        if (len(mgmts) > 1):
            raise LagoInitException('VM {0} has more than one management network: {1}. It should have exactly one.'.format(dom_name, ','.join(mgmts)))

Validate network configuration

Args:
        conf(dict): spec

Returns:
        None

Raises:
        :exc:`~lago.utils.LagoInitException`: If a VM has more than
        one management network configured, or a network which is not
        management has DNS attributes, or a VM is configured with a
        none-existence NIC, or a VM has no management network.


def prepare_all_data(data_dir, block_pct_tokens_thresh=0.1):
    gs_blocks_dir = os.path.join(data_dir, GOLD_STANDARD_BLOCKS_DIRNAME)
    gs_blocks_filenames = get_filenames(gs_blocks_dir, full_path=False, match_regex=re.escape(GOLD_STANDARD_BLOCKS_EXT))
    gs_blocks_fileroots = (re.search(('(.+)' + re.escape(GOLD_STANDARD_BLOCKS_EXT)), gs_blocks_filename).group(1) for gs_blocks_filename in gs_blocks_filenames)
    return [prepare_data(data_dir, fileroot, block_pct_tokens_thresh) for fileroot in gs_blocks_fileroots]

Prepare data for all HTML + gold standard blocks examples in ``data_dir``.

Args:
        data_dir (str)
        block_pct_tokens_thresh (float): must be in [0.0, 1.0]

Returns:
        List[Tuple[str, List[float, int, List[str]], List[float, int, List[str]]]]

    See Also:
        :func:`prepare_data`


def interval(coro, interval=1, times=None, loop=None):
    assert_corofunction(coro=coro)
    times = (int((times or 0)) or float('inf'))

    @asyncio.coroutine
    def schedule(times, *args, **kw):
        while (times > 0):
            times -= 1
            (yield from coro(*args, **kw))
            (yield from asyncio.sleep(interval))

    def wrapper(*args, **kw):
        return ensure_future(schedule(times, *args, **kw), loop=loop)
    return wrapper

Schedules the execution of a coroutine function every `x` amount of
    seconds.

    The function returns an `asyncio.Task`, which implements also an
    `asyncio.Future` interface, allowing the user to cancel the execution
    cycle.

    This function can be used as decorator.

Args:
        coro (coroutinefunction): coroutine function to defer.
        interval (int/float): number of seconds to repeat the coroutine
            execution.
        times (int): optional maximum time of executions. Infinite by default.
        loop (asyncio.BaseEventLoop, optional): loop to run.
            Defaults to asyncio.get_event_loop().

Raises:
        TypeError: if coro argument is not a coroutine function.

Returns:
        future (asyncio.Task): coroutine wrapped as task future.
            Useful for cancellation and state checking.

    Usage::

        # Usage as function
        future = paco.interval(coro, 1)

        # Cancel it after a while...
        await asyncio.sleep(5)
        future.cancel()

        # Usage as decorator
        @paco.interval(10)
        async def metrics():
            await send_metrics()

        future = await metrics()


def get_uuid_string(low=None, high=None, **x):
    if ((low is None) or (high is None)):
        return None
    x = ''.join([parse_part(low), parse_part(high)])
    return '-'.join([x[:8], x[8:12], x[12:16], x[16:20], x[20:32]])

This method parses a UUID protobuf message type from its component
    'high' and 'low' longs into a standard formatted UUID string

Args:
        x (dict): containing keys, 'low' and 'high' corresponding to the UUID
            protobuf message type

Returns:
        str: UUID formatted string


def wait_for_element(self, using, value, timeout=10000, interval=1000, asserter=is_displayed):
    if (not callable(asserter)):
        raise TypeError('Asserter must be callable.')

    @retry(retry_on_exception=(lambda ex: isinstance(ex, WebDriverException)), stop_max_delay=timeout, wait_fixed=interval)
    def _wait_for_element(ctx, using, value):
        el = ctx.element(using, value)
        asserter(el)
        return el
    return _wait_for_element(self, using, value)

Wait for element till satisfy the given condition

    Support:
        Android iOS Web(WebView)

Args:
        using(str): The element location strategy.
        value(str): The value of the location strategy.
        timeout(int): How long we should be retrying stuff.
        interval(int): How long between retries.
        asserter(callable): The asserter func to determine the result.

Returns:
        Return the Element.

Raises:
        WebDriverException.


def bulkDetails(self, packageNames):
    params = {'au': '1'}
    req = googleplay_pb2.BulkDetailsRequest()
    req.docid.extend(packageNames)
    data = req.SerializeToString()
    message = self.executeRequestApi2(BULK_URL, post_data=data.decode('utf-8'), content_type=CONTENT_TYPE_PROTO, params=params)
    response = message.payload.bulkDetailsResponse
    return [(None if (not utils.hasDoc(entry)) else utils.parseProtobufObj(entry.doc)) for entry in response.entry]

Get several apps details from a list of package names.

    This is much more efficient than calling N times details() since it
    requires only one request. If an item is not found it returns an empty object
    instead of throwing a RequestError('Item not found') like the details() function

Args:
        packageNames (list): a list of app IDs (usually starting with 'com.').

Returns:
        a list of dictionaries containing docv2 data, or None
        if the app doesn't exist


def _EncodeString(self, string):
    try:
        encoded_string = string.encode(self._encoding, errors=self._errors)
    except UnicodeEncodeError:
        if (self._errors == 'strict'):
            logging.error('Unable to properly write output due to encoding error. Switching to error tolerant encoding which can result in non Basic Latin (C0) characters to be replaced with "?" or "\\ufffd".')
            self._errors = 'replace'
        encoded_string = string.encode(self._encoding, errors=self._errors)
    return encoded_string

Encodes the string.

Args:
        string (str): string to encode.

Returns:
        bytes: encoded string.


def get_minimum_indentation(text):
    lines = text.split('\n')
    indentations = [get_indentation(line_) for line_ in lines if (len(line_.strip()) > 0)]
    if (len(indentations) == 0):
        return 0
    return min(indentations)

r"""
    returns the number of preceding spaces

Args:
        text (str): unicode text

Returns:
        int: indentation

    CommandLine:
        python -m utool.util_str --exec-get_minimum_indentation --show

Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_str import *  # NOQA
        >>> import utool as ut
        >>> text = '    foo\n   bar'
        >>> result = get_minimum_indentation(text)
        >>> print(result)
        3


def decrypt(self, message):
    message = json.loads(message)
    unencrypted_msg = []
    for line in message:
        enc_line = binascii.a2b_base64(line)
        unencrypted_line = rsa.decrypt(enc_line, self.private_key)
        unencrypted_msg.append(unencrypted_line)
    unencrypted_msg = ''.join(unencrypted_msg)
    return unencrypted_msg

Decrypts a string using our own private key object.

Args:
        message (string): The string of the message to decrypt.

Returns:
    The unencrypted string.


def parseConfig(cls, value):
    if ('enabled' in value):
        value['enabled'] = bool(value['enabled'])
    if ('exclude_paths' in value):
        value['exclude_paths'] = [n.strip() for n in ast.literal_eval(value['exclude_paths'])]
    return value

Parse the config values

Args:
    value (dict): Dictionary which contains the checker config

Returns:
    dict: The checker config with parsed values


def write(gmt, out_path):
    with open(out_path, 'w') as f:
        for (_, each_dict) in enumerate(gmt):
            f.write((each_dict[SET_IDENTIFIER_FIELD] + '\t'))
            f.write((each_dict[SET_DESC_FIELD] + '\t'))
            f.write('\t'.join([str(entry) for entry in each_dict[SET_MEMBERS_FIELD]]))
            f.write('\n')

Write a GMT to a text file.

Args:
        gmt (GMT object): list of dicts
        out_path (string): output path

Returns:
        None


def auto_docstr(modname, funcname, verbose=True, moddir=None, modpath=None, **kwargs):
    (func, module, error_str) = load_func_from_module(modname, funcname, verbose=verbose, moddir=moddir, modpath=modpath)
    if (error_str is None):
        try:
            docstr = make_default_docstr(func, **kwargs)
        except Exception as ex:
            import utool as ut
            error_str = ut.formatex(ex, 'Caught Error in parsing docstr', tb=True)
            error_str += '\n\nReplicateCommand:\n    python -m utool --tf auto_docstr --modname={modname} --funcname={funcname} --moddir={moddir}'.format(modname=modname, funcname=funcname, moddir=moddir)
            error_str += ('\n kwargs=' + ut.repr4(kwargs))
            return error_str
    else:
        docstr = error_str
    return docstr

r"""
    called from vim. Uses strings of filename and modnames to build docstr

Args:
        modname (str): name of a python module
        funcname (str): name of a function in the module

Returns:
        str: docstr

    CommandLine:
        python -m utool.util_autogen auto_docstr
        python -m utool --tf auto_docstr

Example:
        >>> # DISABLE_DOCTEST
        >>> import utool as ut
        >>> from utool.util_autogen import *  # NOQA
        >>> ut.util_autogen.rrr(verbose=False)
        >>> #docstr = ut.auto_docstr('ibeis.algo.hots.smk.smk_index', 'compute_negentropy_names')
        >>> modname = ut.get_argval('--modname', default='utool.util_autogen')
        >>> funcname = ut.get_argval('--funcname', default='auto_docstr')
        >>> moddir = ut.get_argval('--moddir', type_=str, default=None)
        >>> docstr = ut.util_autogen.auto_docstr(modname, funcname)
        >>> print(docstr)


def bad_request(cls, errors=None):
    if cls.expose_status:
        cls.response.content_type = 'application/json'
        cls.response._status_line = '400 Bad Request'
    return cls(400, errors=errors).to_json

Shortcut API for HTTP 400 `Bad Request` response.

Args:
        errors (list): Response key/value data.

Returns:
        WSResponse Instance.


def hill_climb(nsteps, start_node, get_next_node):
    outputs = []
    best_score = (- float('inf'))
    for step in range(nsteps):
        (next_node, score, output) = get_next_node(copy.deepcopy(start_node))
        if (score > best_score):
            start_node = copy.deepcopy(next_node)
            best_score = score
            outputs.append(output)
    return (start_node, best_score, outputs)

Modular hill climbing algorithm.

Example:
        >>> def get_next_node(node):
        ...     a, b = random.sample(range(len(node)), 2)
        ...     node[a], node[b] = node[b], node[a]
        ...     plaintext = decrypt(node, ciphertext)
        ...     score = lantern.score(plaintext, *fitness_functions)
        ...     return node, score, Decryption(plaintext, ''.join(node), score)
        >>> final_node, best_score, outputs = hill_climb(10, "ABC", get_next_node)

Args:
        nsteps (int): The number of neighbours to visit
        start_node: The starting node
        get_next_node (function): Function to return the next node
            the score of the current node and any optional output from the current node

Returns:
        The highest node found, the score of this node and the outputs from the best nodes along the way


def deepgetattr(obj, name, default=_UNSPECIFIED):
    try:
        if ('.' in name):
            (attr, subname) = name.split('.', 1)
            return deepgetattr(getattr(obj, attr), subname, default)
        else:
            return getattr(obj, name)
    except AttributeError:
        if (default is _UNSPECIFIED):
            raise
        else:
            return default

Try to retrieve the given attribute of an object, digging on '.'.

    This is an extended getattr, digging deeper if '.' is found.

Args:
        obj (object): the object of which an attribute should be read
        name (str): the name of an attribute to look up.
        default (object): the default value to use if the attribute wasn't found

Returns:
        the attribute pointed to by 'name', splitting on '.'.

Raises:
        AttributeError: if obj has no 'name' attribute.


def swo_speed_info(self):
    info = structs.JLinkSWOSpeedInfo()
    res = self._dll.JLINKARM_SWO_Control(enums.JLinkSWOCommands.GET_SPEED_INFO, ctypes.byref(info))
    if (res < 0):
        raise errors.JLinkException(res)
    return info

Retrieves information about the supported SWO speeds.

Args:
        self (JLink): the ``JLink`` instance

Returns:
        A ``JLinkSWOSpeedInfo`` instance describing the target's supported
        SWO speeds.

Raises:
        JLinkException: on error


def create_chapter_from_string(self, html_string, url=None, title=None):
    clean_html_string = self.clean_function(html_string)
    clean_xhtml_string = clean.html_to_xhtml(clean_html_string)
    if title:
        pass
    else:
        try:
            root = BeautifulSoup(html_string, 'html.parser')
            title_node = root.title
            if (title_node is not None):
                title = unicode(title_node.string)
            else:
                raise ValueError
        except (IndexError, ValueError):
            title = 'Ebook Chapter'
    return Chapter(clean_xhtml_string, title, url)

Creates a Chapter object from a string. Sanitizes the
    string using the clean_function method, and saves
    it as the content of the created chapter.

Args:
        html_string (string): The html or xhtml content of the created
            Chapter
        url (Option[string]): A url to infer the title of the chapter from
        title (Option[string]): The title of the created Chapter. By
            default, this is None, in which case the title will try to be
            inferred from the webpage at the url.

Returns:
        Chapter: A chapter object whose content is the given string
            and whose title is that provided or inferred from the url


def _validate_input_state(quantum_state):
    rho = np.asarray(quantum_state)
    if (rho.ndim == 1):
        rho = np.outer(rho, np.conj(rho))
    shape = np.shape(rho)
    if ((len(shape) != 2) or (shape[0] != shape[1])):
        raise VisualizationError('Input is not a valid quantum state.')
    num = int(np.log2(rho.shape[0]))
    if ((2 ** num) != rho.shape[0]):
        raise VisualizationError('Input is not a multi-qubit quantum state.')
    return rho

Validates the input to state visualization functions.

Args:
        quantum_state (ndarray): Input state / density matrix.

Returns:
        rho: A 2d numpy array for the density matrix.

Raises:
        VisualizationError: Invalid input.


def un(byts):
    return msgpack.loads(byts, use_list=False, raw=False, unicode_errors='surrogatepass')

Use msgpack to de-serialize a python object.

Args:
        byts (bytes): The bytes to de-serialize

Note:
        String objects are decoded using utf8 encoding.  In order to handle
        potentially malformed input, ``unicode_errors='surrogatepass'`` is set
        to allow decoding bad input strings.

Returns:
        obj: The de-serialized object


def collection(self, collection_id):
    child_path = (self._path + (collection_id,))
    return self._client.collection(*child_path)

Create a sub-collection underneath the current document.

Args:
        collection_id (str): The sub-collection identifier (sometimes
            referred to as the "kind").

Returns:
        ~.firestore_v1beta1.collection.CollectionReference: The
        child collection.


def get_cookiecutter_config(template, default_config=None, version=None):
    default_config = (default_config or {})
    config_dict = cc_config.get_user_config()
    (repo_dir, _) = cc_repository.determine_repo_dir(template=template, abbreviations=config_dict['abbreviations'], clone_to_dir=config_dict['cookiecutters_dir'], checkout=version, no_input=True)
    context_file = os.path.join(repo_dir, 'cookiecutter.json')
    context = cc_generate.generate_context(context_file=context_file, default_context={**config_dict['default_context'], **default_config})
    return (repo_dir, cc_prompt.prompt_for_config(context))

Obtains the configuration used for cookiecutter templating

Args:
        template: Path to the template
        default_config (dict, optional): The default configuration
        version (str, optional): The git SHA or branch to use when
            checking out template. Defaults to latest version

Returns:
        tuple: The cookiecutter repo directory and the config dict


def BalanceFor(self, assetId):
    for (key, fixed8) in self.Balances.items():
        if (key == assetId):
            return fixed8
    return Fixed8(0)

Get the balance for a given asset id.

Args:
        assetId (UInt256):

Returns:
        Fixed8: balance value.


def request(path):
    headers = {'Accept': 'application/json'}
    try:
        requested_object = requests.get(path, headers=headers)
        requested_object.raise_for_status()
    except requests.exceptions.HTTPError as exception:
        LOGGER.error(((((((inspect.stack()[0][3] + ': HTTPError = ') + str(exception.response.status_code)) + ' ') + str(exception.response.reason)) + ' ') + str(path)))
        raise
    except requests.exceptions.InvalidURL as exception:
        LOGGER.error(((('URLError = ' + str(exception.reason)) + ' ') + str(path)))
        raise
    except Exception:
        import traceback
        LOGGER.error(('Generic exception: ' + traceback.format_exc()))
        raise
    else:
        response = requested_object.json()
        return response

Send a request to a given URL accepting JSON format and return a \
    deserialized Python object.

Args:
    path (str): The URI to be requested.

Returns:
    response: Deserialized JSON Python object.

Raises:
    HTTPError: the HTTP error returned by the requested server.
    InvalidURL: an invalid URL has been requested.
    Exception: generic exception.


def _get_resource_view(self, resource_view):
    if isinstance(resource_view, dict):
        resource_view = ResourceView(resource_view, configuration=self.configuration)
    if isinstance(resource_view, ResourceView):
        return resource_view
    raise HDXError(('Type %s is not a valid resource view!' % type(resource_view).__name__))

Get resource view id

Args:
        resource_view (Union[ResourceView,Dict]): ResourceView metadata from a ResourceView object or dictionary

Returns:
        ResourceView: ResourceView object


def import_module(name):
    parts = name.split('.')
    path = None
    module_name = ''
    fhandle = None
    for (index, part) in enumerate(parts):
        module_name = (part if (index == 0) else ('%s.%s' % (module_name, part)))
        path = ([path] if (path is not None) else path)
        try:
            (fhandle, path, descr) = imp.find_module(part, path)
            if (module_name in sys.modules):
                mod = sys.modules[module_name]
            else:
                mod = imp.load_module(module_name, fhandle, path, descr)
        finally:
            if fhandle:
                fhandle.close()
    return mod

Imports a module into the current runtime environment

    This function emulates the Python import system that allows for
    importing full path modules.  It will break down the module and
    import each part (or skip if it is already loaded in cache).

Args:
        name (str): The name of the module to import.  This should be
            the full path of the module

Returns:
        The module that was imported


def generate_output_list(self, source, key, val, line='2', hr=True, show_name=False, colorize=True):
    output = generate_output(line=line, short=(HR_RDAP[source][key]['_short'] if hr else key), name=(HR_RDAP[source][key]['_name'] if (hr and show_name) else None), is_parent=(False if ((val is None) or (len(val) == 0)) else True), value=('None' if ((val is None) or (len(val) == 0)) else None), colorize=colorize)
    if (val is not None):
        for item in val:
            output += generate_output(line=str((int(line) + 1)), value=item, colorize=colorize)
    return output

The function for generating CLI output RDAP list results.

Args:
        source (:obj:`str`): The parent key 'network' or 'objects'
            (required).
        key (:obj:`str`): The event key 'events' or 'events_actor'
            (required).
        val (:obj:`dict`): The event dictionary (required).
        line (:obj:`str`): The line number (0-4). Determines indentation.
            Defaults to '0'.
        hr (:obj:`bool`): Enable human readable key translations. Defaults
            to True.
        show_name (:obj:`bool`): Show human readable name (default is to
            only show short). Defaults to False.
        colorize (:obj:`bool`): Colorize the console output with ANSI
            colors. Defaults to True.

Returns:
        str: The generated output.


def read(cls, five9, external_id):
    results = cls.search(five9, {cls.__uid_field__: external_id})
    if (not results):
        return None
    return results[0]

Return a record singleton for the ID.

Args:
        five9 (five9.Five9): The authenticated Five9 remote.
        external_id (mixed): The identified on Five9. This should be the
            value that is in the ``__uid_field__`` field on the record.

Returns:
        BaseModel: The record, if found. Otherwise ``None``


def LoadFromFile(cls, script_path):
    (_name, dev) = ComponentRegistry().load_extension(script_path, class_filter=VirtualTile, unique=True)
    return dev

Import a virtual tile from a file rather than an installed module

    script_path must point to a python file ending in .py that contains exactly one
    VirtualTile class definition.  That class is loaded and executed as if it
    were installed.

    To facilitate development, if there is a proxy object defined in the same
    file, it is also added to the HardwareManager proxy registry so that it
    can be found and used with the device.

Args:
        script_path (string): The path to the script to load

Returns:
        VirtualTile: A subclass of VirtualTile that was loaded from script_path


def set_number_of_atoms(self, n, selected_sites=None):
    self.number_of_atoms = n
    self.atoms = species.Species(self.lattice.populate_sites(self.number_of_atoms, selected_sites=selected_sites))

Set the number of atoms for the simulation, and populate the simulation lattice.

Args:
        n (Int): Number of atoms for this simulation.
        selected_sites (:obj:(List|Set|String), optional): Selects a subset of site types to be populated with atoms. Defaults to None.

Returns:
        None


def _get_required_fn(fn, root_path):
    if (not fn.startswith(root_path)):
        raise ValueError('Both paths have to be absolute or local!')
    replacer = ('/' if root_path.endswith('/') else '')
    return fn.replace(root_path, replacer, 1)

Definition of the MD5 file requires, that all paths will be absolute
    for the package directory, not for the filesystem.

    This function converts filesystem-absolute paths to package-absolute paths.

Args:
        fn (str): Local/absolute path to the file.
        root_path (str): Local/absolute path to the package directory.

Returns:
        str: Package-absolute path to the file.

Raises:
        ValueError: When `fn` is absolute and `root_path` relative or \
                    conversely.


def DistFitDataset(Dat):
    (r, c) = Dat.shape
    Poiss = np.zeros(r)
    Norm = np.zeros(r)
    LogNorm = np.zeros(r)
    for i in range(r):
        temp = GetDistFitError(Dat[i])
        Poiss[i] = temp['poiss']
        Norm[i] = temp['norm']
        LogNorm[i] = temp['lognorm']
    d = {}
    d['poiss'] = Poiss
    d['norm'] = Norm
    d['lognorm'] = LogNorm
    return d

Given a data matrix, this returns the per-gene fit error for the
    Poisson, Normal, and Log-Normal distributions.

Args:
        Dat (array): numpy array with shape (genes, cells)

Returns:
        d (dict): 'poiss', 'norm', 'lognorm' give the fit error for each distribution.


def assignSeasonSchedule(self, season, month, day, schedule):
    season += 1
    schedule += 1
    if ((season < 1) or (season > Extents.Seasons) or (schedule < 1) or (schedule > Extents.Schedules) or (month > 12) or (month < 0) or (day < 0) or (day > 31)):
        ekm_log(((((((('Out of bounds: month ' + str(month)) + ' day ') + str(day)) + ' schedule ') + str(schedule)) + ' season ') + str(season)))
        return False
    idx_mon = (('Season_' + str(season)) + '_Start_Day')
    idx_day = (('Season_' + str(season)) + '_Start_Month')
    idx_schedule = (('Season_' + str(season)) + '_Schedule')
    if (idx_mon not in self.m_seasons_sched_params):
        ekm_log(('Incorrect index: ' + idx_mon))
        return False
    if (idx_day not in self.m_seasons_sched_params):
        ekm_log(('Incorrect index: ' + idx_day))
        return False
    if (idx_schedule not in self.m_seasons_sched_params):
        ekm_log(('Incorrect index: ' + idx_schedule))
        return False
    self.m_seasons_sched_params[idx_mon] = month
    self.m_seasons_sched_params[idx_day] = day
    self.m_seasons_sched_params[idx_schedule] = schedule
    return True

Define a single season and assign a schedule

Args:
        season (int): A :class:`~ekmmeters.Seasons` value or in range(Extent.Seasons).
        month (int): Month 1-12.
        day (int):  Day 1-31.
        schedule (int): A :class:`~ekmmeters.LCDItems` value or in range(Extent.Schedules).

Returns:
        bool: True on completion and ACK.


def prepare_read(data, method='readlines', mode='r'):
    if hasattr(data, 'readlines'):
        data = getattr(data, method)()
    elif isinstance(data, list):
        if (method == 'read'):
            return ''.join(data)
    elif isinstance(data, basestring):
        data = getattr(open(data, mode), method)()
    else:
        raise TypeError(('Unable to handle data of type %r' % type(data)))
    return data

Prepare various input types for parsing.

Args:
        data (iter): Data to read
        method (str): Method to process data with
        mode (str): Custom mode to process with, if data is a file

Returns:
        list: List suitable for parsing

Raises:
        TypeError: Invalid value for data


def get_filtered_vts_list(self, vts, vt_filter):
    if (not vt_filter):
        raise RequiredArgument('vt_filter: A valid filter is required.')
    filters = self.parse_filters(vt_filter)
    if (not filters):
        return None
    _vts_aux = vts.copy()
    for (_element, _oper, _filter_val) in filters:
        for vt_id in _vts_aux.copy():
            if (not _vts_aux[vt_id].get(_element)):
                _vts_aux.pop(vt_id)
                continue
            _elem_val = _vts_aux[vt_id].get(_element)
            _val = self.format_filter_value(_element, _elem_val)
            if self.filter_operator[_oper](_val, _filter_val):
                continue
            else:
                _vts_aux.pop(vt_id)
    return _vts_aux

Gets a collection of vulnerability test from the vts dictionary,
    which match the filter.

Args:
        vt_filter (string): Filter to apply to the vts collection.
        vts (dictionary): The complete vts collection.

Returns:
        Dictionary with filtered vulnerability tests.


def get_student_current_grades(self, username, course_ids=None):
    if (course_ids is None):
        enrollments_client = CourseEnrollments(self.requester, self.base_url)
        enrollments = enrollments_client.get_student_enrollments()
        course_ids = list(enrollments.get_enrolled_course_ids())
    all_current_grades = []
    for course_id in course_ids:
        try:
            all_current_grades.append(self.get_student_current_grade(username, course_id))
        except HTTPError as error:
            if (error.response.status_code >= 500):
                raise
    return CurrentGradesByUser(all_current_grades)

Returns a CurrentGradesByUser object with the user current grades.

Args:
        username (str): an edx user's username
        course_ids (list): a list of edX course ids.

Returns:
        CurrentGradesByUser: object representing the student current grades


def get_all_anonymous_mappings(self, struct1, struct2, niggli=True, include_dist=False):
    (struct1, struct2) = self._process_species([struct1, struct2])
    (struct1, struct2, fu, s1_supercell) = self._preprocess(struct1, struct2, niggli)
    matches = self._anonymous_match(struct1, struct2, fu, s1_supercell, break_on_match=(not include_dist))
    if matches:
        if include_dist:
            return [(m[0], m[1][0]) for m in matches]
        else:
            return [m[0] for m in matches]

Performs an anonymous fitting, which allows distinct species in one
    structure to map to another. Returns a dictionary of species
    substitutions that are within tolerance

Args:
        struct1 (Structure): 1st structure
        struct2 (Structure): 2nd structure
        niggli (bool): Find niggli cell in preprocessing
        include_dist (bool): Return the maximin distance with each mapping

Returns:
        list of species mappings that map struct1 to struct2.


def split_identifiers(identifiers=[], proportions={}):
    abs_proportions = absolute_proportions(proportions, len(identifiers))
    parts = {}
    start_index = 0
    for (idx, proportion) in abs_proportions.items():
        parts[idx] = identifiers[start_index:(start_index + proportion)]
        start_index += proportion
    return parts

Split the given identifiers by the given proportions.

Args:
        identifiers (list): List of identifiers (str).
        proportions (dict): A dictionary containing the proportions with the identifier from the
        input as key.

Returns:
        dict: Dictionary containing a list of identifiers per part with the same key as the
        proportions dict.

    Example::

        >>> split_identifiers(
        >>>     identifiers=['a', 'b', 'c', 'd'],
        >>>     proportions={'melvin' : 0.5, 'timmy' : 0.5}
        >>> )
        {'melvin' : ['a', 'c'], 'timmy' : ['b', 'd']}


def set_peer_address(self, value=None, default=False, disable=False):
    return self._configure_mlag('peer-address', value, default, disable)

Configures the mlag peer-address value

Args:
        value (str): The value to configure the peer-address
        default (bool): Configures the peer-address using the
            default keyword
        disable (bool): Negates the peer-address using the no keyword

Returns:
        bool: Returns True if the commands complete successfully


def get_signed_url(self, file_id):
    if (not is_valid_uuid(file_id)):
        raise StorageArgumentException('Invalid UUID for file_id: {0}'.format(file_id))
    return self._authenticated_request.to_endpoint('file/{}/content/secure_link/'.format(file_id)).return_body().get()['signed_url']

Get a signed unauthenticated URL.

    It can be used to download the file content without the need for a
    token. The signed URL expires after 5 seconds.

Args:
        file_id (str): The UUID of the file to get the link for.

Returns:
        The signed url as a string

Raises:
        StorageArgumentException: Invalid arguments
        StorageForbiddenException: Server response code 403
        StorageNotFoundException: Server response code 404
        StorageException: other 400-600 error codes


def gridsearch_timer(func_list, args_list, niters=None, **searchkw):
    import utool as ut
    timings = ut.ddict(list)
    if (niters is None):
        niters = len(args_list)
    if ut.is_funclike(args_list):
        get_args = args_list
    else:
        get_args = args_list.__getitem__
    func_labels = searchkw.get('func_labels', [ut.get_funcname(func) for func in func_list])
    use_cache = searchkw.get('use_cache', (not ut.get_argflag(('--nocache', '--nocache-time'))))
    assert_eq = searchkw.get('assert_eq', True)
    count_list = list(range(niters))
    xlabel_list = []
    cache = ut.ShelfCacher('timeings.shelf', enabled=use_cache)
    for count in ut.ProgressIter(count_list, lbl='Testing Timeings'):
        args_ = get_args(count)
        xlabel_list.append(args_)
        if True:
            if (not isinstance(args_, tuple)):
                args_ = (args_,)
        assert isinstance(args_, tuple), 'args_ should be a tuple so it can be unpacked'
        ret_list = []
        for func_ in func_list:
            try:
                kwargs_ = {}
                func_cachekey = ut.get_func_result_cachekey(func_, args_, kwargs_)
                ellapsed = cache.load(func_cachekey)
            except ut.CacheMissException:
                with ut.Timer(verbose=False) as t:
                    ret = func_(*args_)
                    ret_list.append(ret)
                ellapsed = t.ellapsed
                cache.save(func_cachekey, ellapsed)
            timings[func_].append(ellapsed)
        if assert_eq:
            ut.assert_all_eq(list(map(ut.cachestr_repr, ret_list)))
    cache.close()
    count_to_xtick = searchkw.get('count_to_xtick', (lambda x, y: x))
    xtick_list = [count_to_xtick(count, get_args(count)) for count in count_list]

    def plot_timings():
        import plottool as pt
        ydata_list = ut.dict_take(timings, func_list)
        xdata = xtick_list
        ylabel = 'seconds'
        xlabel = 'input size'
        pt.multi_plot(xdata, ydata_list, label_list=func_labels, ylabel=ylabel, xlabel=xlabel, **searchkw)
    time_result = {'plot_timings': plot_timings, 'timings': timings}
    return time_result

Times a series of functions on a series of inputs

    args_list is a list should vary the input sizes
    can also be a func that take a count param

    items in args_list list or returned by the func should be a tuple so it can be
    unpacked

    CommandLine:
        python -m ibeis.annotmatch_funcs --exec-get_annotmatch_rowids_from_aid2 --show
        python -m ibeis.annotmatch_funcs --exec-get_annotmatch_rowids_from_aid:1 --show

Args:
        func_list (list):
        args_list (list):
        niters (None): (default = None)

Returns:
        dict: time_result

    CommandLine:
        python -m utool.util_gridsearch --exec-gridsearch_timer --show

Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_gridsearch import *  # NOQA
        >>> import utool as ut
        >>> func_list = [ut.fibonacci_recursive, ut.fibonacci_iterative]
        >>> args_list = list(range(1, 35))
        >>> niters = None
        >>> searchkw = {}
        >>> time_result = gridsearch_timer(func_list, args_list, niters, **searchkw)
        >>> result = ('time_result = %s' % (six.text_type(time_result),))
        >>> print(result)
        >>> time_result['plot_timings']()
        >>> ut.show_if_requested()


def VerifyStructure(self, parser_mediator, line):
    try:
        structure = self._DPKG_LOG_LINE.parseString(line)
    except pyparsing.ParseException as exception:
        logger.debug('Unable to parse Debian dpkg.log file with error: {0!s}'.format(exception))
        return False
    return (('date_time' in structure) and ('body' in structure))

Verifies if a line from a text file is in the expected format.

Args:
        parser_mediator (ParserMediator): parser mediator.
        line (str): line from a text file.

Returns:
        bool: True if the line is in the expected format, False if not.


def do_youtube_dl(worker, site, page):
    with tempfile.TemporaryDirectory(prefix='brzl-ydl-') as tempdir:
        ydl = _build_youtube_dl(worker, tempdir, site)
        ie_result = _try_youtube_dl(worker, ydl, site, page)
        outlinks = set()
        if (ie_result and (ie_result.get('extractor') == 'youtube:playlist')):
            outlinks = {('https://www.youtube.com/watch?v=%s' % e['id']) for e in ie_result.get('entries_no_dl', [])}
        return (ydl.fetch_spy.fetches, outlinks)

Runs youtube-dl configured for `worker` and `site` to download videos from
    `page`.

Args:
        worker (brozzler.BrozzlerWorker): the calling brozzler worker
        site (brozzler.Site): the site we are brozzling
        page (brozzler.Page): the page we are brozzling

Returns:
        tuple with two entries:
            `list` of `dict`: with info about urls fetched:
                [{
                    'url': ...,
                    'method': ...,
                    'response_code': ...,
                    'response_headers': ...,
                }, ...]
            `list` of `str`: outlink urls


def compile(self, target_instance_family, input_shape, output_path, role, tags=None, job_name=None, compile_max_run=(5 * 60), framework=None, framework_version=None):
    framework = (self._framework() or framework)
    if (framework is None):
        raise ValueError('You must specify framework, allowed values {}'.format(NEO_ALLOWED_FRAMEWORKS))
    if (framework not in NEO_ALLOWED_FRAMEWORKS):
        raise ValueError('You must provide valid framework, allowed values {}'.format(NEO_ALLOWED_FRAMEWORKS))
    if (job_name is None):
        raise ValueError('You must provide a compilation job name')
    framework = framework.upper()
    framework_version = (self._get_framework_version() or framework_version)
    config = self._compilation_job_config(target_instance_family, input_shape, output_path, role, compile_max_run, job_name, framework, tags)
    self.sagemaker_session.compile_model(**config)
    job_status = self.sagemaker_session.wait_for_compilation_job(job_name)
    self.model_data = job_status['ModelArtifacts']['S3ModelArtifacts']
    if target_instance_family.startswith('ml_'):
        self.image = self._neo_image(self.sagemaker_session.boto_region_name, target_instance_family, framework, framework_version)
        self._is_compiled_model = True
    else:
        LOGGER.warning('The intance type {} is not supported to deploy via SageMaker,please deploy the model on the device by yourself.'.format(target_instance_family))
    return self

Compile this ``Model`` with SageMaker Neo.

Args:
        target_instance_family (str): Identifies the device that you want to run your model after compilation, for
            example: ml_c5. Allowed strings are: ml_c5, ml_m5, ml_c4, ml_m4, jetsontx1, jetsontx2, ml_p2, ml_p3,
            deeplens, rasp3b
        input_shape (dict): Specifies the name and shape of the expected inputs for your trained model in json
            dictionary form, for example: {'data':[1,3,1024,1024]}, or {'var1': [1,1,28,28], 'var2':[1,1,28,28]}
        output_path (str): Specifies where to store the compiled model
        role (str): Execution role
        tags (list[dict]): List of tags for labeling a compilation job. For more, see
            https://docs.aws.amazon.com/sagemaker/latest/dg/API_Tag.html.
        job_name (str): The name of the compilation job
        compile_max_run (int): Timeout in seconds for compilation (default: 3 * 60).
            After this amount of time Amazon SageMaker Neo terminates the compilation job regardless of its
            current status.
        framework (str): The framework that is used to train the original model. Allowed values: 'mxnet',
            'tensorflow', 'pytorch', 'onnx', 'xgboost'
        framework_version (str)

Returns:
        sagemaker.model.Model: A SageMaker ``Model`` object. See :func:`~sagemaker.model.Model` for full details.


def shannon_entropy(pvec, base=2):
    if (base == 2):

        def logfn(x):
            return ((- x) * np.log2(x))
    elif (base == np.e):

        def logfn(x):
            return ((- x) * np.log(x))
    else:

        def logfn(x):
            return (((- x) * np.log(x)) / np.log(base))
    h = 0.0
    for x in pvec:
        if (0 < x < 1):
            h += logfn(x)
    return h

Compute the Shannon entropy of a probability vector.

    The shannon entropy of a probability vector pv is defined as
    $H(pv) = - \\sum_j pv[j] log_b (pv[j])$ where $0 log_b 0 = 0$.

Args:
        pvec (array_like): a probability vector.
        base (int): the base of the logarith

Returns:
        float: The Shannon entropy H(pvec).


def create_folder(self, name, parent):
    if (not is_valid_uuid(parent)):
        raise StorageArgumentException('Invalid UUID for parent: {0}'.format(parent))
    return self._authenticated_request.to_endpoint('folder/').with_json_body(self._prep_params(locals())).return_body().post()

Create a new folder.

Args:
        name (srt): The name of the folder.
        parent (str): The UUID of the parent entity. The parent must be a
            project or a folder.

Returns:
        A dictionary of details of the created folder::

            {
                u'created_by': u'303447',
                u'created_on': u'2017-03-21T14:06:32.293902Z',
                u'description': u'',
                u'entity_type': u'folder',
                u'modified_by': u'303447',
                u'modified_on': u'2017-03-21T14:06:32.293967Z',
                u'name': u'myfolder',
                u'parent': u'3abd8742-d069-44cf-a66b-2370df74a682',
                u'uuid': u'2516442e-1e26-4de1-8ed8-94523224cc40'
            }

Raises:
        StorageArgumentException: Invalid arguments
        StorageForbiddenException: Server response code 403
        StorageNotFoundException: Server response code 404
        StorageException: other 400-600 error codes


def has_request(self, request):
    queue_item = QueueItem(request, Response(request.url))
    key = queue_item.get_hash()
    for status in QueueItem.STATUSES:
        if (key in self.__get_var(('items_' + status)).keys()):
            return True
    return False

Check if the given request already exists in the queue.

Args:
        request (:class:`nyawc.http.Request`): The request to check.

Returns:
        bool: True if already exists, False otherwise.


def convert(isbn, code='978'):
    isbn = _isbn_cleanse(isbn)
    if (len(isbn) == 10):
        isbn = (code + isbn[:(- 1)])
        return (isbn + calculate_checksum(isbn))
    elif isbn.startswith('978'):
        return (isbn[3:(- 1)] + calculate_checksum(isbn[3:(- 1)]))
    else:
        raise IsbnError('Only ISBN-13s with 978 Bookland code can be converted to ISBN-10.')

Convert ISBNs between ISBN-10 and ISBN-13.

Note:
        No attempt to hyphenate converted ISBNs is made, because the
        specification requires that *any* hyphenation must be correct but
        allows ISBNs without hyphenation.

Args:
        isbn (str): SBN, ISBN-10 or ISBN-13
        code (str): EAN Bookland code

Returns:
        ``str``: Converted ISBN-10 or ISBN-13

Raises:
        IsbnError: When ISBN-13 isn't convertible to an ISBN-10


def get_region(b):
    remap = {None: 'us-east-1', 'EU': 'eu-west-1'}
    region = b.get('Location', {}).get('LocationConstraint')
    return remap.get(region, region)

Tries to get the bucket region from Location.LocationConstraint

    Special cases:
        LocationConstraint EU defaults to eu-west-1
        LocationConstraint null defaults to us-east-1

Args:
        b (object): A bucket object

Returns:
        string: an aws region string


def pipeline_status(url, pipeline_id, auth, verify_ssl):
    status_result = requests.get((((url + '/') + pipeline_id) + '/status'), headers=X_REQ_BY, auth=auth, verify=verify_ssl)
    status_result.raise_for_status()
    logging.debug((('Status request: ' + url) + '/status'))
    logging.debug(status_result.json())
    return status_result.json()

Retrieve the current status for a pipeline.

Args:
        url          (str): the host url in the form 'http://host:port/'.
        pipeline_id  (str): the ID of of the exported pipeline.
        auth         (tuple): a tuple of username, and password.
        verify_ssl   (bool): whether to verify ssl certificates

Returns:
        dict: the response json


def exception(self, timeout=None):
    if (not self._completed.wait(timeout=timeout)):
        raise exceptions.TimeoutError('Timed out waiting for result.')
    if (self._result != self._SENTINEL):
        return None
    return self._exception

Return the exception raised by the call, if any.

    This blocks until the message has successfully been published, and
    returns the exception. If the call succeeded, return None.

Args:
        timeout (Union[int, float]): The number of seconds before this call
            times out and raises TimeoutError.

Raises:
        TimeoutError: If the request times out.

Returns:
        Exception: The exception raised by the call, if any.


def create(self, resource, timeout=(- 1)):
    return self._client.create(resource, timeout=timeout, default_values=self.DEFAULT_VALUES)

Creates a scope.

Args:
        resource (dict): Object to create.
        timeout:
            Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation
            in OneView, just stop waiting for its completion.

Returns:
        dict: Created scope.


def apply_func_to_find_bin(func: Union[(None, Callable[(..., Union[(float, int, Any)])])], values: Optional[float]=None) -> Callable[([Any], Union[(float, int)])]:

    def return_func(axis) -> Any:
        ' Apply the stored function and value to a given axis.\n\n            Args:\n                axis (TAxis or similar): Axis to which the function should be applied.\n            Returns:\n                any: The value returned by the function. Often a float or int, but not necessarily.\n            '
        if func:
            if (values is not None):
                return func(axis, values)
            else:
                return func(axis)
        else:
            return values
    return return_func

Closure to determine the bin associated with a value on an axis.

    It can apply a function to an axis if necessary to determine the proper bin.  Otherwise,
    it can just return a stored value.

Note:
        To properly determine the value, carefully note the information below. In many cases,
        such as when we want values [2, 5), the values need to be shifted by a small epsilon
        to retrieve the proper bin. This is done automatically in ``SetRangeUser()``.

        >>> hist = ROOT.TH1D("test", "test", 10, 0, 10)
        >>> x = 2, y = 5
        >>> hist.FindBin(x)
        2
        >>> hist.FindBin(x+epsilon)
        2
        >>> hist.FindBin(y)
        6
        >>> hist.FindBin(y-epsilon)
        5

        Note that the bin + epsilon on the lower bin is not strictly necessary, but it is
        used for consistency with the upper bound.

Args:
        func (Callable): Function to apply to the histogram axis. If it is None, the value
            will be returned.
        values (int or float): Value to pass to the function. Default: None (in which case,
            it won't be passed).

Returns:
        Function to be called with an axis to determine the desired bin on that axis.


def load_and_run_pipeline(pipeline_name, pipeline_context_input=None, working_dir=None, context=None, parse_input=True, loader=None):
    logger.debug(f'you asked to run pipeline: {pipeline_name}')
    if loader:
        logger.debug(f'you set the pype loader to: {loader}')
    else:
        loader = 'pypyr.pypeloaders.fileloader'
        logger.debug(f'use default pype loader: {loader}')
    logger.debug(f'you set the initial context to: {pipeline_context_input}')
    if (context is None):
        context = pypyr.context.Context()
        context.working_dir = working_dir
    else:
        working_dir = context.working_dir
    loader_module = pypyr.moduleloader.get_module(loader)
    try:
        get_pipeline_definition = getattr(loader_module, 'get_pipeline_definition')
    except AttributeError:
        logger.error(f"The pipeline loader {loader_module} doesn't have a get_pipeline_definition(pipeline_name, working_dir) function.")
        raise
    logger.debug(f'loading the pipeline definition with {loader_module}')
    pipeline_definition = get_pipeline_definition(pipeline_name=pipeline_name, working_dir=working_dir)
    logger.debug(f'{loader_module} done')
    run_pipeline(pipeline=pipeline_definition, pipeline_context_input=pipeline_context_input, context=context, parse_input=parse_input)

Load and run the specified pypyr pipeline.

    This function runs the actual pipeline by name. If you are running another
    pipeline from within a pipeline, call this, not main(). Do call main()
    instead for your 1st pipeline if there are pipelines calling pipelines.

    By default pypyr uses file loader. This means that pipeline_name.yaml
    should be in the working_dir/pipelines/ directory.

Args:
        pipeline_name (str): Name of pipeline, sans .yaml at end.
        pipeline_context_input (str): Initialize the pypyr context with this
                                string.
        working_dir (path): Look for pipelines and modules in this directory.
                    If context arg passed, will use context.working_dir and
                    ignore this argument. If context is None, working_dir
                    must be specified.
        context (pypyr.context.Context): Use if you already have a
                Context object, such as if you are running a pipeline from
                within a pipeline and you want to re-use the same context
                object for the child pipeline. Any mutations of the context by
                the pipeline will be against this instance of it.
        parse_input (bool): run context_parser in pipeline.
        loader (str): str. optional. Absolute name of pipeline loader module.
                If not specified will use pypyr.pypeloaders.fileloader.

Returns:
        None


def transform_absolute_coords(self, width, height):
    if (self.type != EventType.POINTER_MOTION_ABSOLUTE):
        raise AttributeError(_wrong_meth.format(self.type))
    abs_x = self._libinput.libinput_event_pointer_get_absolute_x_transformed(self._handle, width)
    abs_y = self._libinput.libinput_event_pointer_get_absolute_y_transformed(self._handle, height)
    return (abs_x, abs_y)

Return the current absolute coordinates of the pointer event,
    transformed to screen coordinates.

    For pointer events that are not of type
    :attr:`~libinput.constant.EventType.POINTER_MOTION_ABSOLUTE`,
    this method raises :exc:`AttributeError`.

Args:
    width (int): The current output screen width.
    height (int): The current output screen height.

Returns:
    (float, float): The current absolute (x, y) coordinates transformed
    to a screen coordinates.

Raises:
    AttributeError


def _get_elements(self, url, key, eclass, id=None, name=None):
    if ((id is not None) and (name is not None)):
        raise ValueError('id and name cannot specified together')
    json_elements = self.rest_client.make_request(url)[key]
    return [eclass(element, self.rest_client) for element in json_elements if (_exact_resource(element, id) and _matching_resource(element, name))]

Get elements matching `id` or `name`

Args:
        url(str): url of children.
        key(str): key in the returned JSON.
        eclass(subclass type of :py:class:`_ResourceElement`): element class to create instances of.
        id(str, optional): only return resources whose `id` property matches the given `id`
        name(str, optional): only return resources whose `name` property matches the given `name`

Returns:
        list(_ResourceElement): List of `eclass` instances

Raises:
        ValueError: both `id` and `name` are specified together


def icon_description(self, **kwargs):
    params = {'language': util.language_code(kwargs.get('lang'))}
    result = self.make_request('icon_description', {}, **params)
    if (not util.check_result(result)):
        return (False, result.get('message', 'UNKNOWN ERROR'))
    values = util.response_list(result, 'Data')
    return (True, [emtype.IconDescription(**a) for a in values])

Obtain a list of elements that have an associated icon.

Args:
        lang (str): Language code (*es* or *en*).

Returns:
        Status boolean and parsed response (list[IconDescription]), or
        message string in case of error.


def convert_idx_to_name(self, y, lens):
    y = [[self.id2label[idx] for idx in row[:l]] for (row, l) in zip(y, lens)]
    return y

Convert label index to name.

Args:
        y (list): label index list.
        lens (list): true length of y.

Returns:
        y: label name list.

Example:
        >>> # assumes that id2label = {1: 'B-LOC', 2: 'I-LOC'}
        >>> y = [[1, 0, 0], [1, 2, 0], [1, 1, 1]]
        >>> lens = [1, 2, 3]
        >>> self.convert_idx_to_name(y, lens)
        [['B-LOC'], ['B-LOC', 'I-LOC'], ['B-LOC', 'B-LOC', 'B-LOC']]


def extract_changelog_items(text, tags):
    patterns = {x['header']: tag_re(x['tag']) for x in tags}
    items = {x['header']: [] for x in tags}
    curr_tag = None
    curr_text = ''
    for line in text.splitlines():
        if (not line.strip()):
            if (curr_tag is not None):
                items[curr_tag].append(curr_text)
                curr_text = ''
            curr_tag = None
        for tag in tags:
            m = patterns[tag['header']].match(line)
            if m:
                if (curr_tag is not None):
                    items[curr_tag].append(curr_text)
                    curr_text = ''
                curr_tag = tag['header']
                line = m.group('text')
                break
        if (curr_tag is not None):
            curr_text = '{} {}'.format(curr_text.strip(), line.strip()).strip()
    if (curr_tag is not None):
        items[curr_tag].append(curr_text)
    return items

Extract all tagged items from text.

Args:
        text (str):
            Text to extract the tagged items from. Each tagged item is a
            paragraph that starts with a tag. It can also be a text list item.

Returns:
        tuple[list[str], list[str], list[str]]:
            A tuple of `(features, changes, fixes)` extracted from the given
            text.

    The tagged items are usually features/changes/fixes but it can be configured
    through `pelconf.yaml`.


def extract_paths(disk_path, disk_root, paths, ignore_nopath):
    with guestfs_conn_mount_ro(disk_path, disk_root) as conn:
        for (guest_path, host_path) in paths:
            msg = 'Extracting guestfs://{0} to {1}'.format(guest_path, host_path)
            LOGGER.debug(msg)
            try:
                _copy_path(conn, guest_path, host_path)
            except ExtractPathNoPathError as err:
                if ignore_nopath:
                    LOGGER.debug('%s - ignoring', err)
                else:
                    raise

Extract paths from a disk using guestfs

Args:
        disk_path(str): path to the disk
        disk_root(str): root partition
        paths(list of tuples): files to extract in
            `[(src1, dst1), (src2, dst2)...]` format, if ``srcN`` is a
            directory in the guest, and ``dstN`` does not exist on the host,
            it will be created. If ``srcN`` is a file on the guest, it will be
            copied exactly to ``dstN``
        ignore_nopath(bool): If set to True, ignore paths in the guest that
            do not exit

Returns:
        None

Raises:
        :exc:`~lago.plugins.vm.ExtractPathNoPathError`: if a none existing
            path was found on the guest, and `ignore_nopath` is False.
        :exc:`~lago.plugins.vm.ExtractPathError`: on all other failures.


def get_link_or_none(pattern_name, request, view_kwargs=None):
    from is_core.patterns import reverse_pattern
    pattern = reverse_pattern(pattern_name)
    assert (pattern is not None), 'Invalid pattern name {}'.format(pattern_name)
    if pattern.has_permission('get', request, view_kwargs=view_kwargs):
        return pattern.get_url_string(request, view_kwargs=view_kwargs)
    else:
        return None

Helper that generate URL prom pattern name and kwargs and check if current request has permission to open the URL.
    If not None is returned.

Args:
        pattern_name (str): slug which is used for view registratin to pattern
        request (django.http.request.HttpRequest): Django request object
        view_kwargs (dict): list of kwargs necessary for URL generator

    Returns:


def to_url(self, site='amazon', country='us'):
    try:
        try:
            (url, tlds) = URL_MAP[site]
        except ValueError:
            tlds = None
            url = URL_MAP[site]
    except KeyError:
        raise SiteError(site)
    inject = {'isbn': self._isbn}
    if tlds:
        if (country not in tlds):
            raise CountryError(country)
        tld = tlds[country]
        if (not tld):
            tld = country
        inject['tld'] = tld
    return (url % inject)

Generate a link to an online book site.

Args:
        site (str): Site to create link to
        country (str): Country specific version of ``site``

Returns:
        ``str``: URL on ``site`` for book

Raises:
        SiteError: Unknown site value
        CountryError: Unknown country value


def set(self, context_id, address_value_list):
    if (context_id not in self._contexts):
        LOGGER.warning('Context_id not in contexts, %s', context_id)
        return False
    context = self._contexts.get(context_id)
    add_value_dict = {}
    for d in address_value_list:
        for (add, val) in d.items():
            if (not self.address_is_valid(address=add)):
                raise AuthorizationException(address=add)
            add_value_dict[add] = val
    context.set_direct(add_value_dict)
    return True

Within a context, sets addresses to a value.

Args:
        context_id (str): the context id returned by create_context
        address_value_list (list): list of {address: value} dicts

Returns:
        (bool): True if the operation is successful, False if
            the context_id doesn't reference a known context.

Raises:
        AuthorizationException if an address is given in the
            address_value_list that was not in the original
            transaction's outputs, or was under a namespace but the
            characters after the namespace are not valid address
            characters.