def get_log_file_name(level=INFO):

  if level not in converter.ABSL_LEVELS:
    raise ValueError('Invalid absl.logging level {}'.format(level))
  stream = get_absl_handler().python_handler.stream
  if (stream == sys.stderr or stream == sys.stdout or
      not hasattr(stream, 'name')):
    return ''
  else:
    return stream.name

Returns the name of the log file.

    For Python logging, only one file is used and level is ignored. And it returns
    empty string if it logs to stderr/stdout or the log stream has no `name`
    attribute.

Args:
        level: int, the absl.logging level.

Raises:
        ValueError: Raised when `level` has an invalid value.


def fit_predict(self, X, y, X_):

        self.fit(X, y)
        return self.predict(X_)

Shortcut to `model.fit(X, y); return model.predict(X_)`.

    Parameters:
    -----------
    X : matrix, shape (n_samples, n_features)
    The samples, the train data.

    y : vector, shape (n_samples,)
    The target labels.

    X_ : matrix, shape (m_samples, m_features)
    The samples which labels to predict.

Returns:
    --------
    y : vector, shape (m_samples,)
    The predicted labels.


def parse_json_path(self, jsonpath):

        if jsonpath not in self.parsed:
            try:
                self.parsed[jsonpath] = self.parser(jsonpath)
            except Exception:
                self.log("Invalid Json Path: " + jsonpath, "error")
                raise InvalidJsonPathError("Invalid Json Path")

        return self.parsed[jsonpath]

Parse a jsonpath

Args:
        jsonpath: str

    Returns: a parsed json path


def match(subject: Expression, pattern: Pattern) -> Iterator[Substitution]:
    r
    if not is_constant(subject):
        raise ValueError("The subject for matching must be constant.")
    global_constraints = [c for c in pattern.constraints if not c.variables]
    local_constraints = set(c for c in pattern.constraints if c.variables)
    for subst in _match([subject], pattern.expression, Substitution(), local_constraints):
        for constraint in global_constraints:
            if not constraint(subst):
                break
        else:
            yield subst

r"""Tries to match the given *pattern* to the given *subject*.

    Yields each match in form of a substitution.

    Parameters:
        subject:
            An subject to match.
        pattern:
            The pattern to match.

Yields:
        All possible match substitutions.

Raises:
        ValueError:
            If the subject is not constant.


def _parse_udf_vol_descs(self, extent, length, descs):
        # type: (int, int, PyCdlib._UDFDescriptors) -> None

        # Read in the Volume Descriptor Sequence
        self._seek_to_extent(extent)
        vd_data = self._cdfp.read(length)

        # And parse it.  Since the sequence doesn't have to be in any set order,
        # and since some of the entries may be missing, we parse the Descriptor
        # Tag (the first 16 bytes) to find out what kind of descriptor it is,
        # then construct the correct type based on that.  We keep going until we
        # see a Terminating Descriptor.

        block_size = self.pvd.logical_block_size()
        offset = 0
        current_extent = extent
        done = False
        while not done:
            desc_tag = udfmod.UDFTag()
            desc_tag.parse(vd_data[offset:], current_extent)
            if desc_tag.tag_ident == 1:
                descs.pvd.parse(vd_data[offset:offset + 512], current_extent, desc_tag)
            elif desc_tag.tag_ident == 4:
                descs.impl_use.parse(vd_data[offset:offset + 512], current_extent, desc_tag)
            elif desc_tag.tag_ident == 5:
                descs.partition.parse(vd_data[offset:offset + 512], current_extent, desc_tag)
            elif desc_tag.tag_ident == 6:
                descs.logical_volume.parse(vd_data[offset:offset + 512], current_extent, desc_tag)
            elif desc_tag.tag_ident == 7:
                descs.unallocated_space.parse(vd_data[offset:offset + 512], current_extent, desc_tag)
            elif desc_tag.tag_ident == 8:
                descs.terminator.parse(current_extent, desc_tag)
                done = True
            else:
                raise pycdlibexception.PyCdlibInvalidISO('UDF Tag identifier not %d' % (desc_tag.tag_ident))

            offset += block_size
            current_extent += 1

An internal method to parse a set of UDF Volume Descriptors.

    Parameters:
    extent - The extent at which to start parsing.
    length - The number of bytes to read from the incoming ISO.
    descs - The _UDFDescriptors object to store parsed objects into.

Returns:
    Nothing.


def do_set_log_file(self, args):

        params = args.split()
        try:
            filename = params[0]
            logging.basicConfig(filename=filename)
        except IndexError:
            self.do_help('set_log_file')

Set the log file.

    Usage:
        set_log_file filename
    Parameters:
        filename: log file name to write to

    THIS CAN ONLY BE CALLED ONCE AND MUST BE CALLED
    BEFORE ANY LOGGING STARTS.


def op_and(self, *elements):

        expression = self.add_operator(Operator(';'))
        for element in elements:
            expression.add_element(element)
        return expression

Update the ``Expression`` by joining the specified additional
    ``elements`` using an "AND" ``Operator``

Args:
        *elements (BaseExpression): The ``Expression`` and/or
            ``Constraint`` elements which the "AND" ``Operator`` applies
            to.

Returns:
        Expression: ``self`` or related ``Expression``.


def build_parameters(self, stack, provider_stack=None):

        resolved = _resolve_parameters(stack.parameter_values, stack.blueprint)
        required_parameters = list(stack.required_parameter_definitions)
        all_parameters = list(stack.all_parameter_definitions)
        parameters = _handle_missing_parameters(resolved, all_parameters,
                                                required_parameters,
                                                provider_stack)

        param_list = []

        for key, value in parameters:
            param_dict = {"ParameterKey": key}
            if value is UsePreviousParameterValue:
                param_dict["UsePreviousValue"] = True
            else:
                param_dict["ParameterValue"] = str(value)

            param_list.append(param_dict)

        return param_list

Builds the CloudFormation Parameters for our stack.

Args:
        stack (:class:`stacker.stack.Stack`): A stacker stack
        provider_stack (dict): An optional Stacker provider object

Returns:
        dict: The parameters for the given stack


def playlists_iter(self, *, start_token=None, page_size=250):

		start_token = None

		while True:
			response = self._call(
				mc_calls.PlaylistFeed,
				max_results=page_size,
				start_token=start_token
			)
			items = response.body.get('data', {}).get('items', [])

			if items:
				yield items

			start_token = response.body.get('nextPageToken')
			if start_token is None:
				break

Get a paged iterator of library playlists.

    Parameters:
    start_token (str): The token of the page to return.
        Default: Not sent to get first page.
    page_size (int, Optional): The maximum number of results per returned page.
        Max allowed is ``49995``.
        Default: ``250``

Yields:
    list: Playlist dicts.


def __init__(self, channel):

        self.ListProfiles = channel.unary_unary(
            "/google.cloud.talent.v4beta1.ProfileService/ListProfiles",
            request_serializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__service__pb2.ListProfilesRequest.SerializeToString,
            response_deserializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__service__pb2.ListProfilesResponse.FromString,
        )
        self.CreateProfile = channel.unary_unary(
            "/google.cloud.talent.v4beta1.ProfileService/CreateProfile",
            request_serializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__service__pb2.CreateProfileRequest.SerializeToString,
            response_deserializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__pb2.Profile.FromString,
        )
        self.GetProfile = channel.unary_unary(
            "/google.cloud.talent.v4beta1.ProfileService/GetProfile",
            request_serializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__service__pb2.GetProfileRequest.SerializeToString,
            response_deserializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__pb2.Profile.FromString,
        )
        self.UpdateProfile = channel.unary_unary(
            "/google.cloud.talent.v4beta1.ProfileService/UpdateProfile",
            request_serializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__service__pb2.UpdateProfileRequest.SerializeToString,
            response_deserializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__pb2.Profile.FromString,
        )
        self.DeleteProfile = channel.unary_unary(
            "/google.cloud.talent.v4beta1.ProfileService/DeleteProfile",
            request_serializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__service__pb2.DeleteProfileRequest.SerializeToString,
            response_deserializer=google_dot_protobuf_dot_empty__pb2.Empty.FromString,
        )
        self.SearchProfiles = channel.unary_unary(
            "/google.cloud.talent.v4beta1.ProfileService/SearchProfiles",
            request_serializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__service__pb2.SearchProfilesRequest.SerializeToString,
            response_deserializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_profile__service__pb2.SearchProfilesResponse.FromString,
        )

Constructor.

Args:
        channel: A grpc.Channel.


def label_count(self, label_list_ids=None):

        count = collections.defaultdict(int)

        for label_list in self.label_lists.values():
            if label_list_ids is None or label_list.idx in label_list_ids:
                for label_value, label_count in label_list.label_count().items():
                    count[label_value] += label_count

        return count

Return a dictionary containing the number of times,
    every label-value in this utterance is occurring.

Args:
        label_list_ids (list): If not None, only labels from label-lists
                            with an id contained in this list
                            are considered.

Returns:
        dict: A dictionary containing the number of occurrences
            with the label-value as key.


def indexes(self, collection=None):

        indexes = []

        for collection_name in self.collections():
            if collection and collection != collection_name:
                continue
            for index_name in self.db[collection_name].index_information():
                if index_name != '_id_':
                    indexes.append(index_name)
        return indexes

Return a list with the current indexes

    Skip the mandatory _id_ indexes

Args:
        collection(str)

Returns:
        indexes(list)


def ceil(cls, x: 'TensorFluent') -> 'TensorFluent':

        return cls._unary_op(x, tf.ceil, tf.float32)

Returns a TensorFluent for the ceil function.

Args:
        x: The input fluent.

Returns:
        A TensorFluent wrapping the ceil function.


def traverse_bfs(self):

        if not isinstance(include_self, bool):
            raise TypeError("include_self must be a bool")
        q = deque(); dist = dict(); dist[self] = 0; q.append((self,0))
        while len(q) != 0:
            curr = q.popleft(); yield curr
            for c in curr[0].children:
                if c not in dist:
                    if c.edge_length is None:
                        el = 0
                    else:
                        el = c.edge_length
                    dist[c] = dist[curr[0]] + el; q.append((c,dist[c]))
            if curr[0].parent is not None and curr[0].parent not in dist:
                if curr[0].edge_length is None:
                    el = 0
                else:
                    el = curr[0].edge_length
                dist[curr[0].parent] = dist[curr[0]] + el; q.append((curr[0].parent,dist[curr[0].parent]))

Perform a Breadth-First Search (BFS) starting at this ``Node`` object'. Yields (``Node``, distance) tuples

Args:
        ``include_self`` (``bool``): ``True`` to include self in the traversal, otherwise ``False``


def update(self, rid, data, raise_on_error=True):

        return self.put(rid, data, raise_on_error)

Update the for the provided Id. Alias for put() method.

Args:
        rid (str): The record identifier.
        data (dict): The record data.
        raise_on_error (bool): If True and not r.ok this method will raise a RunTimeError.

Returns:
        object : Python request response.


def __init__(self, channel):

    self.send = channel.stream_stream(
        '/predix.eventhub.Publisher/send',
        request_serializer=EventHub__pb2.PublishRequest.SerializeToString,
        response_deserializer=EventHub__pb2.PublishResponse.FromString,
        )


def save_image(image_url, image_directory, image_name):

    image_type = get_image_type(image_url)
    if image_type is None:
        raise ImageErrorException(image_url)
    full_image_file_name = os.path.join(image_directory, image_name + '.' + image_type)

    # If the image is present on the local filesystem just copy it
    if os.path.exists(image_url):
        shutil.copy(image_url, full_image_file_name)
        return image_type

    try:
        # urllib.urlretrieve(image_url, full_image_file_name)
        with open(full_image_file_name, 'wb') as f:
            user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0'
            request_headers = {'User-Agent': user_agent}
            requests_object = requests.get(image_url, headers=request_headers)
            try:
                content = requests_object.content
                # Check for empty response
                f.write(content)
            except AttributeError:
                raise ImageErrorException(image_url)
    except IOError:
        raise ImageErrorException(image_url)
    return image_type

Saves an online image from image_url to image_directory with the name image_name.
    Returns the extension of the image saved, which is determined dynamically.

Args:
        image_url (str): The url of the image.
        image_directory (str): The directory to save the image in.
        image_name (str): The file name to save the image as.

Raises:
        ImageErrorException: Raised if unable to save the image at image_url


def phenotypes_to_scored(self,phenotypes=None,overwrite=False):

        if not self.is_uniform(): raise ValueError("inconsistent phenotypes")
        if phenotypes is None:
            phenotypes = self.phenotypes
        elif isinstance(phenotypes,str):
            phenotypes = [phenotypes]
        def _post(binary,phenotype_label,phenotypes,overwrite):
            d = binary.copy()
            if len(set(phenotypes)&set(list(binary.keys()))) > 0 and overwrite==False:
                raise ValueError("Error, phenotype already exists as a scored type")
            for label in phenotypes: d[label] = 0
            if phenotype_label == phenotype_label and phenotype_label in phenotypes:
                d[phenotype_label] = 1
            return d
        output = self.copy()
        output['scored_calls'] = output.apply(lambda x:
                _post(x['scored_calls'],x['phenotype_label'],phenotypes,overwrite)
            ,1)
        return output

Add mutually exclusive phenotypes to the scored calls

Args:
        phenotypes (list): a list of phenotypes to add to scored calls.  if none or not set, add them all
        overwrite (bool): if True allow the overwrite of a phenotype, if False, the phenotype must not exist in the scored calls

Returns:
        CellDataFrame


def _get_augmented_label_matrix(self, L, higher_order=False):

        # Create a helper data structure which maps cliques (as tuples of member
        # sources) --> {start_index, end_index, maximal_cliques}, where
        # the last value is a set of indices in this data structure
        self.c_data = {}
        for i in range(self.m):
            self.c_data[i] = {
                "start_index": i * self.k,
                "end_index": (i + 1) * self.k,
                "max_cliques": set(
                    [
                        j
                        for j in self.c_tree.nodes()
                        if i in self.c_tree.node[j]["members"]
                    ]
                ),
            }

        L_ind = self._create_L_ind(L)

        # Get the higher-order clique statistics based on the clique tree
        # First, iterate over the maximal cliques (nodes of c_tree) and
        # separator sets (edges of c_tree)
        if higher_order:
            L_aug = np.copy(L_ind)
            for item in chain(self.c_tree.nodes(), self.c_tree.edges()):
                if isinstance(item, int):
                    C = self.c_tree.node[item]
                    C_type = "node"
                elif isinstance(item, tuple):
                    C = self.c_tree[item[0]][item[1]]
                    C_type = "edge"
                else:
                    raise ValueError(item)
                members = list(C["members"])
                nc = len(members)

                # If a unary maximal clique, just store its existing index
                if nc == 1:
                    C["start_index"] = members[0] * self.k
                    C["end_index"] = (members[0] + 1) * self.k

                # Else add one column for each possible value
                else:
                    L_C = np.ones((self.n, self.k ** nc))
                    for i, vals in enumerate(product(range(self.k), repeat=nc)):
                        for j, v in enumerate(vals):
                            L_C[:, i] *= L_ind[:, members[j] * self.k + v]

                    # Add to L_aug and store the indices
                    if L_aug is not None:
                        C["start_index"] = L_aug.shape[1]
                        C["end_index"] = L_aug.shape[1] + L_C.shape[1]
                        L_aug = np.hstack([L_aug, L_C])
                    else:
                        C["start_index"] = 0
                        C["end_index"] = L_C.shape[1]
                        L_aug = L_C

                    # Add to self.c_data as well
                    id = tuple(members) if len(members) > 1 else members[0]
                    self.c_data[id] = {
                        "start_index": C["start_index"],
                        "end_index": C["end_index"],
                        "max_cliques": set([item]) if C_type == "node" else set(item),
                    }
            return L_aug
        else:
            return L_ind

Returns an augmented version of L where each column is an indicator
    for whether a certain source or clique of sources voted in a certain
    pattern.

Args:
        L: An [n,m] scipy.sparse label matrix with values in {0,1,...,k}


def get_rows_fieldnames_from_raw_sql(
        session: Union[Session, Engine, Connection],
        sql: str) -> Tuple[Sequence[Sequence[Any]], Sequence[str]]:

    result = session.execute(sql)  # type: ResultProxy
    fieldnames = result.keys()
    rows = result.fetchall()
    return rows, fieldnames

Returns results and column names from a query.

Args:
        session: SQLAlchemy :class:`Session`, :class:`Engine`, or
            :class:`Connection` object
        sql: raw SQL to execure

Returns:
        ``(rows, fieldnames)`` where ``rows`` is the usual set of results and
        ``fieldnames`` are the name of the result columns/fields.


def StartFlowAndWait(client_id,
                     token=None,
                     timeout=DEFAULT_TIMEOUT,
                     **flow_args):

  flow_urn = flow.StartAFF4Flow(
      client_id=client_id, token=token, sync=True, **flow_args)

  WaitForFlow(flow_urn, token=token, timeout=timeout)

  return flow_urn

Runs a flow and waits for it to finish.

Args:
        client_id: The client id of the client to run on.
        token: The datastore access token.
        timeout: How long to wait for a flow to complete, maximum.
        **flow_args: Pass through to flow.

Returns:
        The urn of the flow that was run.


def __init__(self,
                 kernel_fn,
                 verbose=False,
                 random_state=None):

        self.kernel_fn = kernel_fn
        self.verbose = verbose
        self.random_state = check_random_state(random_state)

Init function

Args:
        kernel_fn: function that transforms an array of distances into an
                    array of proximity values (floats).
        verbose: if true, print local prediction values from linear model.
        random_state: an integer or numpy.RandomState that will be used to
            generate random numbers. If None, the random state will be
            initialized using the internal numpy seed.


def remove_words(self, words):

        for word in words:
            self._dictionary.pop(word.lower())
        self._update_dictionary()

Remove a list of words from the word frequency list

Args:
        words (list): The list of words to remove


def view(filepath):

    try:
        view_func = getattr(view, PLATFORM)
    except AttributeError:
        raise RuntimeError('platform %r not supported' % PLATFORM)
    view_func(filepath)

Open filepath with its default viewing application (platform-specific).

Args:
        filepath: Path to the file to open in viewer.

Raises:
        RuntimeError: If the current platform is not supported.


def _send_trace(self, chunk=None):

        self._trace_sm_running = True
        # If we failed to transmit a chunk, we will be requeued with an argument
        if chunk is None:
            chunk = self._next_tracing_chunk(20)

        if chunk is None or len(chunk) == 0:
            self._trace_sm_running = False
            return

        try:
            self._send_notification(TracingChar.value_handle, chunk)
            self._defer(self._send_trace)
        except bable_interface.BaBLEException as err:
            if err.packet.status == 'Rejected':  # If we are streaming too fast, back off and try again
                time.sleep(0.05)
                self._defer(self._send_trace, [chunk])
            else:
                self._audit('ErrorStreamingTrace')  # If there was an error, stop streaming but don't choke
                self._logger.exception("Error while tracing data")

Stream tracing data to the ble client in 20 byte chunks

Args:
        chunk (bytearray): A chunk that should be sent instead of requesting a
            new chunk from the pending reports.


def murmur3_64(data: Union[bytes, bytearray], seed: int = 19820125) -> int:
      # noqa
    m = 0xc6a4a7935bd1e995
    r = 47

    mask = 2 ** 64 - 1

    length = len(data)

    h = seed ^ ((m * length) & mask)

    offset = (length // 8) * 8
    # RNC: was /, but for Python 3 that gives float; brackets added for clarity
    for ll in range(0, offset, 8):
        k = bytes_to_long(data[ll:ll + 8])
        k = (k * m) & mask
        k ^= (k >> r) & mask
        k = (k * m) & mask
        h = (h ^ k)
        h = (h * m) & mask

    l = length & 7

    if l >= 7:
        h = (h ^ (data[offset + 6] << 48))

    if l >= 6:
        h = (h ^ (data[offset + 5] << 40))

    if l >= 5:
        h = (h ^ (data[offset + 4] << 32))

    if l >= 4:
        h = (h ^ (data[offset + 3] << 24))

    if l >= 3:
        h = (h ^ (data[offset + 2] << 16))

    if l >= 2:
        h = (h ^ (data[offset + 1] << 8))

    if l >= 1:
        h = (h ^ data[offset])
        h = (h * m) & mask

    h ^= (h >> r) & mask
    h = (h * m) & mask
    h ^= (h >> r) & mask

    return h

Pure 64-bit Python implementation of MurmurHash3; see
    http://stackoverflow.com/questions/13305290/is-there-a-pure-python-implementation-of-murmurhash
    (plus RNC bugfixes).

Args:
        data: data to hash
        seed: seed

Returns:
        integer hash


def unsubscribe_stratgy(self, strategy_id):

        today = datetime.date.today()
        order_id = str(uuid.uuid1())
        if strategy_id in self._subscribed_strategy.keys():
            self._subscribed_strategy[strategy_id]['status'] = 'canceled'

        self.coins_history.append(
            [0,
             strategy_id,
             str(today),
             0,
             order_id,
             'unsubscribe']
        )

取消订阅某一个策略

Args:
        strategy_id {[type]} -- [description]


def get_resource(self, resource_key, **variables):

        handle = self.make_resource_handle(resource_key, **variables)
        return self.get_resource_from_handle(handle, verify_repo=False)

Get a resource.

    Attempts to get and return a cached version of the resource if
    available, otherwise a new resource object is created and returned.

Args:
        resource_key (`str`):  Name of the type of `Resources` to find
        variables: data to identify / store on the resource

Returns:
        `PackageRepositoryResource` instance.


def wmo(self, value=None):

        if value is not None:
            try:
                value = str(value)
            except ValueError:
                raise ValueError('value {} need to be of type str '
                                 'for field `wmo`'.format(value))
            if ',' in value:
                raise ValueError('value should not contain a comma '
                                 'for field `wmo`')

        self._wmo = value

Corresponds to IDD Field `wmo` usually a 6 digit field. Used as
    alpha in EnergyPlus.

Args:
        value (str): value for IDD Field `wmo`
            if `value` is None it will not be checked against the
            specification and is assumed to be a missing value

Raises:
        ValueError: if `value` is not a valid value


def offset(self, num_to_skip):

        query = query_mod.Query(self)
        return query.offset(num_to_skip)

Skip to an offset in a query with this collection as parent.

    See
    :meth:`~.firestore_v1beta1.query.Query.offset` for
    more information on this method.

Args:
        num_to_skip (int): The number of results to skip at the beginning
            of query results. (Must be non-negative.)

Returns:
        ~.firestore_v1beta1.query.Query: An offset query.


def load(fh, model):

    graphs = penman.load(fh, cls=XMRSCodec)
    xs = [model.from_triples(g.triples()) for g in graphs]
    return xs

Deserialize PENMAN graphs from a file (handle or filename)

Args:
        fh: filename or file object
        model: Xmrs subclass instantiated from decoded triples

Returns:
        a list of objects (of class *model*)


def Lock(fd, path, blocking):

  operation = fcntl.LOCK_EX if blocking else fcntl.LOCK_EX | fcntl.LOCK_NB
  try:
    fcntl.flock(fd, operation)
  except IOError as e:
    if e.errno == errno.EWOULDBLOCK:
      raise IOError('Exception locking %s. File already locked.' % path)
    else:
      raise IOError('Exception locking %s. %s.' % (path, str(e)))

Lock the provided file descriptor.

Args:
        fd: int, the file descriptor of the file to lock.
        path: string, the name of the file to lock.
        blocking: bool, whether the function should return immediately.

Raises:
        IOError, raised from flock while attempting to lock a file.


def request_json(link, outfile, force_rerun_flag, outdir=None):

    if not outdir:
        outdir = ''
    outfile = op.join(outdir, outfile)

    if force_rerun(flag=force_rerun_flag, outfile=outfile):
        text_raw = requests.get(link)
        my_dict = text_raw.json()
        with open(outfile, 'w') as f:
            json.dump(my_dict, f)

        log.debug('Loaded and saved {} to {}'.format(link, outfile))
    else:
        with open(outfile, 'r') as f:
            my_dict = json.load(f)
        log.debug('Loaded {}'.format(outfile))

    return my_dict

Download a file in JSON format from a web request

Args:
        link: Link to web request
        outfile: Name of output file
        outdir: Directory of output file
        force_rerun_flag: If true, redownload the file

Returns:
        dict: contents of the JSON request


def _parse_shape(self, space):

    if isinstance(space, gym.spaces.Discrete):
      return ()
    if isinstance(space, gym.spaces.Box):
      return space.shape
    raise NotImplementedError()

Get a tensor shape from a OpenAI Gym space.

Args:
        space: Gym space.

Raises:
        NotImplementedError: For spaces other than Box and Discrete.

Returns:
        Shape tuple.


def process_input(self, stream, value, rpc_executor):

        self.sensor_log.push(stream, value)

        # FIXME: This should be specified in our device model
        if stream.important:
            associated_output = stream.associated_stream()
            self.sensor_log.push(associated_output, value)

        to_check = deque([x for x in self.roots])

        while len(to_check) > 0:
            node = to_check.popleft()
            if node.triggered():
                try:
                    results = node.process(rpc_executor, self.mark_streamer)
                    for result in results:
                        result.raw_time = value.raw_time
                        self.sensor_log.push(node.stream, result)
                except:
                    self._logger.exception("Unhandled exception in graph node processing function for node %s", str(node))

                # If we generated any outputs, notify our downstream nodes
                # so that they are also checked to see if they should run.
                if len(results) > 0:
                    to_check.extend(node.outputs)

Process an input through this sensor graph.

    The tick information in value should be correct and is transfered
    to all results produced by nodes acting on this tick.

Args:
        stream (DataStream): The stream the input is part of
        value (IOTileReading): The value to process
        rpc_executor (RPCExecutor): An object capable of executing RPCs
            in case we need to do that.


def set_forced_variation(self, experiment_key, user_id, variation_key):

    if not self.is_valid:
      self.logger.error(enums.Errors.INVALID_DATAFILE.format('set_forced_variation'))
      return False

    if not validator.is_non_empty_string(experiment_key):
      self.logger.error(enums.Errors.INVALID_INPUT_ERROR.format('experiment_key'))
      return False

    if not isinstance(user_id, string_types):
      self.logger.error(enums.Errors.INVALID_INPUT_ERROR.format('user_id'))
      return False

    return self.config.set_forced_variation(experiment_key, user_id, variation_key)

Force a user into a variation for a given experiment.

Args:
    experiment_key: A string key identifying the experiment.
    user_id: The user ID.
    variation_key: A string variation key that specifies the variation which the user.
    will be forced into. If null, then clear the existing experiment-to-variation mapping.

Returns:
        A boolean value that indicates if the set completed successfully.


def autodecode(b):

    import warnings
    import chardet

    try:
        return b.decode()
    except UnicodeError:
        result = chardet.detect(b)
        if result['confidence'] < 0.95:
            warnings.warn('autodecode failed with utf-8; guessing %s' % result['encoding'])
        return result.decode(result['encoding'])

Try to decode ``bytes`` to text - try default encoding first, otherwise try to autodetect

Args:
        b (bytes): byte string

Returns:
        str: decoded text string


def GetEntries(
      self, parser_mediator, cookie_data=None, url=None, **kwargs):

    fields = cookie_data.split('.')
    number_of_fields = len(fields)

    if number_of_fields not in (1, 6):
      parser_mediator.ProduceExtractionWarning(
          'unsupported number of fields: {0:d} in cookie: {1:s}'.format(
              number_of_fields, self.COOKIE_NAME))
      return

    if number_of_fields == 1:
      domain_hash = None
      visitor_identifier = None
      first_visit_posix_time = None
      previous_visit_posix_time = None

      try:
        # TODO: fix that we're losing precision here use dfdatetime.
        last_visit_posix_time = int(fields[0], 10) / 10000000
      except ValueError:
        last_visit_posix_time = None

      number_of_sessions = None

    elif number_of_fields == 6:
      domain_hash = fields[0]
      visitor_identifier = fields[1]

      # TODO: Double check this time is stored in UTC and not local time.
      try:
        first_visit_posix_time = int(fields[2], 10)
      except ValueError:
        first_visit_posix_time = None

      try:
        previous_visit_posix_time = int(fields[3], 10)
      except ValueError:
        previous_visit_posix_time = None

      try:
        last_visit_posix_time = int(fields[4], 10)
      except ValueError:
        last_visit_posix_time = None

      try:
        number_of_sessions = int(fields[5], 10)
      except ValueError:
        number_of_sessions = None

    event_data = GoogleAnalyticsEventData('utma')
    event_data.cookie_name = self.COOKIE_NAME
    event_data.domain_hash = domain_hash
    event_data.sessions = number_of_sessions
    event_data.url = url
    event_data.visitor_id = visitor_identifier

    if first_visit_posix_time is not None:
      date_time = dfdatetime_posix_time.PosixTime(
          timestamp=first_visit_posix_time)
      event = time_events.DateTimeValuesEvent(
          date_time, 'Analytics Creation Time')
      parser_mediator.ProduceEventWithEventData(event, event_data)

    if previous_visit_posix_time is not None:
      date_time = dfdatetime_posix_time.PosixTime(
          timestamp=previous_visit_posix_time)
      event = time_events.DateTimeValuesEvent(
          date_time, 'Analytics Previous Time')
      parser_mediator.ProduceEventWithEventData(event, event_data)

    date_time = None
    if last_visit_posix_time is not None:
      date_time = dfdatetime_posix_time.PosixTime(
          timestamp=last_visit_posix_time)
      timestamp_description = definitions.TIME_DESCRIPTION_LAST_VISITED
    elif first_visit_posix_time is None and previous_visit_posix_time is None:
      # If both creation_time and written_time are None produce an event
      # object without a timestamp.
      date_time = dfdatetime_semantic_time.SemanticTime('Not set')
      timestamp_description = definitions.TIME_DESCRIPTION_NOT_A_TIME

    if date_time is not None:
      event = time_events.DateTimeValuesEvent(date_time, timestamp_description)
      parser_mediator.ProduceEventWithEventData(event, event_data)

Extracts event objects from the cookie.

Args:
        parser_mediator (ParserMediator): parser mediator.
        cookie_data (str): cookie data.
        url (str): URL or path where the cookie got set.


def ensure_app_config_dir(appname, *args):

    from ubelt import util_path
    dpath = get_app_config_dir(appname, *args)
    util_path.ensuredir(dpath)
    return dpath

Calls `get_app_config_dir` but ensures the directory exists.

Args:
        appname (str): the name of the application
        *args: any other subdirectories may be specified

    SeeAlso:
        get_app_config_dir

Example:
        >>> import ubelt as ub
        >>> dpath = ub.ensure_app_config_dir('ubelt')
        >>> assert exists(dpath)


def _decoder(image, shrink):

    decoder = dmtxDecodeCreate(image, shrink)
    if not decoder:
        raise PyLibDMTXError('Could not create decoder')
    else:
        try:
            yield decoder
        finally:
            dmtxDecodeDestroy(byref(decoder))

A context manager for `DmtxDecode`, created and destroyed by
    `dmtxDecodeCreate` and `dmtxDecodeDestroy`.

Args:
        image (POINTER(DmtxImage)):
        shrink (int):

Yields:
        POINTER(DmtxDecode): The created decoder

Raises:
        PyLibDMTXError: If the decoder could not be created.


def __init__(self, size, dropout=None, lstmcell_args={}, named_tensors=None, scope='internal_lstm', summary_labels=()):

        self.size = size
        self.dropout = dropout
        self.lstmcell_args = lstmcell_args
        super(InternalLstm, self).__init__(named_tensors=named_tensors, scope=scope, summary_labels=summary_labels)

LSTM layer.

Args:
        size: LSTM size.
        dropout: Dropout rate.


def get_openapi_dict(self, services, hostname=None, x_google_api_name=False):

    if not isinstance(services, (tuple, list)):
      services = [services]

    # The type of a class that inherits from remote.Service is actually
    # remote._ServiceClass, thanks to metaclass strangeness.
    # pylint: disable=protected-access
    util.check_list_type(services, remote._ServiceClass, 'services',
                         allow_none=False)

    return self.__api_openapi_descriptor(services, hostname=hostname, x_google_api_name=x_google_api_name)

JSON dict description of a protorpc.remote.Service in OpenAPI format.

Args:
        services: Either a single protorpc.remote.Service or a list of them
        that implements an api/version.
        hostname: string, Hostname of the API, to override the value set on the
        current service. Defaults to None.

Returns:
        dict, The OpenAPI descriptor document as a JSON dict.


def CopyFromStringTuple(self, time_elements_tuple):

    if len(time_elements_tuple) < 7:
      raise ValueError((
          'Invalid time elements tuple at least 7 elements required,'
          'got: {0:d}').format(len(time_elements_tuple)))

    year, month, day_of_month, hours, minutes, seconds, microseconds = (
        time_elements_tuple)

    try:
      microseconds = int(microseconds, 10)
    except (TypeError, ValueError):
      raise ValueError('Invalid microsecond value: {0!s}'.format(microseconds))

    if microseconds < 0 or microseconds >= definitions.MICROSECONDS_PER_SECOND:
      raise ValueError('Invalid number of microseconds.')

    fraction_of_second = (
        decimal.Decimal(microseconds) / definitions.MICROSECONDS_PER_SECOND)

    time_elements_tuple = (
        year, month, day_of_month, hours, minutes, seconds,
        str(fraction_of_second))

    super(TimeElementsInMicroseconds, self).CopyFromStringTuple(
        time_elements_tuple)

Copies time elements from string-based time elements tuple.

Args:
        time_elements_tuple (Optional[tuple[str, str, str, str, str, str, str]]):
        time elements, contains year, month, day of month, hours, minutes,
        seconds and microseconds.

Raises:
        ValueError: if the time elements tuple is invalid.


def request(self, send_terminator = False):

        self.m_a_crc = False
        start_context = self.getContext()
        self.setContext("request[v3A]")
        try:
            self.m_serial_port.write("2f3f".decode("hex") +
                                     self.m_meter_address +
                                     "210d0a".decode("hex"))
            self.m_raw_read_a = self.m_serial_port.getResponse(self.getContext())
            unpacked_read_a = self.unpackStruct(self.m_raw_read_a, self.m_blk_a)
            self.convertData(unpacked_read_a, self.m_blk_a, 1)
            self.m_a_crc = self.crcMeterRead(self.m_raw_read_a, self.m_blk_a)
            if send_terminator:
                self.serialPostEnd()
            self.calculateFields()
            self.makeReturnFormat()
        except:
            ekm_log(traceback.format_exc(sys.exc_info()))

        self.setContext(start_context)
        return self.m_a_crc

Required request() override for v3 and standard method to read meter.

Args:
        send_terminator (bool): Send termination string at end of read.

Returns:
        bool: CRC request flag result from most recent read


def Histograms(self, run, tag):

    accumulator = self.GetAccumulator(run)
    return accumulator.Histograms(tag)

Retrieve the histogram events associated with a run and tag.

Args:
        run: A string name of the run for which values are retrieved.
        tag: A string name of the tag for which values are retrieved.

Raises:
        KeyError: If the run is not found, or the tag is not available for
        the given run.

Returns:
        An array of `event_accumulator.HistogramEvents`.


def ends_of_next_whole_turn(self, root):

        # simple confirmation that the root is actually a root.
        # otherwise it may seem to work but would be totally out of spec
        if root.parent:
            raise ValueError('Unexpectedly received a node with a parent for'
                             ' root:\n{}'.format(root))
        # build the list of eots (or just the root if first turn) to be run
        leaves = list(root.leaves())
        kw_starts = list()
        if leaves[0] is root:
            # build ends of state kwargs as only the root
            kw_starts.append({'root': root})
        else:
            # build ends of state kwargs as eots in the tree
            for leaf in leaves:
                # ignore mana drains
                if not leaf.is_mana_drain:
                    kw_starts.append({'root_eot': leaf})
        # run a single turn for each starting point
        for kw_start in kw_starts:
            for eot in self.ends_of_one_state(**kw_start):
                yield eot

Simulate one complete turn to completion and generate each end of
    turn reached during the simulation.

    Note on mana drain:
    Generates but does not continue simulation of mana drains.

Args:
    root: a start state with no parent


def generate(organization, package, destination):

    gen = ResourceGenerator(organization, package)

    tmp = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
    try:
        tmp.write(gen.conf())
    finally:
        tmp.close()

    shutil.copy(tmp.name, os.path.join(destination, 'conf.py'))

    tmp = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
    try:
        tmp.write(gen.makefile())
    finally:
        tmp.close()

    shutil.copy(tmp.name, os.path.join(destination, 'Makefile'))

Generates the Sphinx configuration and Makefile.

Args:
        organization (str): the organization name.
        package (str): the package to be documented.
        destination (str): the destination directory.


def add_pagination_meta(self, params, meta):

        meta['page_size'] = params['page_size']
        meta['page'] = params['page']

        meta['prev'] = "page={0}&page_size={1}".format(
            params['page'] - 1, params['page_size']
        ) if meta['page'] > 0 else None

        meta['next'] = "page={0}&page_size={1}".format(
            params['page'] + 1, params['page_size']
        ) if meta.get('has_more', True) else None

Extend default meta dictionary value with pagination hints.

Note:
        This method handler attaches values to ``meta`` dictionary without
        changing it's reference. This means that you should never replace
        ``meta`` dictionary with any other dict instance but simply modify
        its content.

Args:
        params (dict): dictionary of decoded parameter values
        meta (dict): dictionary of meta values attached to response


def get(self, catID, includeRelationships=False):

        url = '%(base_url)s/record/%(catID)s' % {
            'base_url': self.base_url, 'catID': catID
        }
        r = self.gbdx_connection.get(url)
        r.raise_for_status()
        return r.json()

Retrieves the strip footprint WKT string given a cat ID.

Args:
        catID (str): The source catalog ID from the platform catalog.
        includeRelationships (bool): whether to include graph links to related objects.  Default False.

Returns:
        record (dict): A dict object identical to the json representation of the catalog record


def from_json(cls, json):

    mapreduce_spec = cls(json["name"],
                         json["mapreduce_id"],
                         json["mapper_spec"],
                         json.get("params"),
                         json.get("hooks_class_name"))
    return mapreduce_spec

Create new MapreduceSpec from the json, encoded by to_json.

Args:
        json: json representation of MapreduceSpec.

Returns:
        an instance of MapreduceSpec with all data deserialized from json.


def shell_call(command, **kwargs):

  command = list(command)
  for i in range(len(command)):
    m = CMD_VARIABLE_RE.match(command[i])
    if m:
      var_id = m.group(1)
      if var_id in kwargs:
        command[i] = kwargs[var_id]
  return subprocess.call(command) == 0

Calls shell command with parameter substitution.

Args:
        command: command to run as a list of tokens
        **kwargs: dirctionary with substitutions

Returns:
        whether command was successful, i.e. returned 0 status code

    Example of usage:
        shell_call(['cp', '${A}', '${B}'], A='src_file', B='dst_file')
    will call shell command:
        cp src_file dst_file


def _on_connected(self, device):

        self._logger.debug("Device connected event: {}".format(device))

        self.connected = True
        self._connection_handle = device['connection_handle']
        self.device.connected = True
        self._audit('ClientConnected')

Callback function called when a connected event has been received.
    It is executed in the baBLE working thread: should not be blocking.

Args:
        device (dict): Information about the newly connected device


def create_policy(self, id, policy):

        return self.request("policy", id, json=policy, method="post")

Create policy.

    https://www.nomadproject.io/api/acl-policies.html

Args:
        - policy
    returns: request.Response

Raises:
        - nomad.api.exceptions.BaseNomadException
        - nomad.api.exceptions.URLNotFoundNomadException


def declare_field(self, type, name, offset=None, length=None):

        if offset is None:
            offset = self._implicit_offset

        if length is None:
            def no_length_handler():
                f = getattr(self, "unpack_" + type)
                return f(offset)
            setattr(self, name, no_length_handler)
        else:

            def explicit_length_handler():
                f = getattr(self, "unpack_" + type)
                return f(offset, length)
            setattr(self, name, explicit_length_handler)

        setattr(self, "_off_" + name, offset)
        if type == "byte":
            self._implicit_offset = offset + 1
        elif type == "int8":
            self._implicit_offset = offset + 1
        elif type == "word":
            self._implicit_offset = offset + 2
        elif type == "word_be":
            self._implicit_offset = offset + 2
        elif type == "int16":
            self._implicit_offset = offset + 2
        elif type == "dword":
            self._implicit_offset = offset + 4
        elif type == "dword_be":
            self._implicit_offset = offset + 4
        elif type == "int32":
            self._implicit_offset = offset + 4
        elif type == "qword":
            self._implicit_offset = offset + 8
        elif type == "int64":
            self._implicit_offset = offset + 8
        elif type == "float":
            self._implicit_offset = offset + 4
        elif type == "double":
            self._implicit_offset = offset + 8
        elif type == "dosdate":
            self._implicit_offset = offset + 4
        elif type == "filetime":
            self._implicit_offset = offset + 8
        elif type == "systemtime":
            self._implicit_offset = offset + 8
        elif type == "guid":
            self._implicit_offset = offset + 16
        elif type == "binary":
            self._implicit_offset = offset + length
        elif type == "string" and length is not None:
            self._implicit_offset = offset + length
        elif type == "wstring" and length is not None:
            self._implicit_offset = offset + (2 * length)
        elif "string" in type and length is None:
            raise ParseException("Implicit offset not supported "
                                 "for dynamic length strings")
        else:
            raise ParseException("Implicit offset not supported "
                                 "for type: {}".format(type))

Declaratively add fields to this block.
    This method will dynamically add corresponding
        offset and unpacker methods to this block.

Args:
    - `type`: A string. Should be one of the unpack_* types.
    - `name`: A string.
    - `offset`: A number.
    - `length`: (Optional) A number. For (w)strings, length in chars.


def UsesArtifact(self, artifacts):

    # If artifact is a single string, see if it is in the list of artifacts
    # as-is. Otherwise, test whether any of the artifacts passed in to this
    # function exist in the list of artifacts.
    if isinstance(artifacts, string_types):
      return artifacts in self.artifacts
    else:
      return any(True for artifact in artifacts if artifact in self.artifacts)

Determines if the check uses the specified artifact.

Args:
        artifacts: Either a single artifact name, or a list of artifact names

Returns:
        True if the check uses a specific artifact.


def faster_roc(actuals, controls):

    assert(type(actuals) is np.ndarray)
    assert(type(controls) is np.ndarray)

    if len(actuals)<500:
        raise RuntimeError('This method might be incorrect when '+
                'not enough actuals are present. Needs to be checked before '+
                'proceeding. Stopping here for you to do so.')

    actuals = np.ravel(actuals)
    controls = np.ravel(controls)
    if np.isnan(actuals).any():
        raise RuntimeError('NaN found in actuals')
    if np.isnan(controls).any():
        raise RuntimeError('NaN found in controls')

    thresholds = np.hstack([-np.inf, np.unique(actuals), np.inf])+np.finfo(float).eps
    true_pos_rate = np.nan*np.empty(thresholds.size-1)
    false_pos_rate = np.nan*np.empty(thresholds.size-1)
    num_act = float(len(actuals))
    num_ctr = float(len(controls))

    actuals = 1-(np.cumsum(np.histogram(actuals, thresholds)[0])/num_act)
    controls = 1-(np.cumsum(np.histogram(controls, thresholds)[0])/num_ctr)
    true_pos_rate = actuals
    false_pos_rate = controls
    #true_pos_rate = np.concatenate(([0], true_pos_rate, [1]))
    false_pos_rate = false_pos_rate
    auc = -1*np.dot(np.diff(false_pos_rate), true_pos_rate[0:-1])
    # treat cases where TPR of one is not reached before FPR of one
    # by using trapezoidal integration for the last segment
    # (add the missing triangle)
    if false_pos_rate[-2] == 1:
        auc += ((1-true_pos_rate[-3])*.5*(1-false_pos_rate[-3]))
    return (auc, true_pos_rate, false_pos_rate)

Histogram based implementation of AUC unde ROC curve.
    Parameters:
        actuals : list
            A list of numeric values for positive observations.
        controls : list
            A list of numeric values for negative observations.


def download_from_url(path, url):

  filename = url.split("/")[-1]
  found_file = find_file(path, filename, max_depth=0)
  if found_file is None:
    filename = os.path.join(path, filename)
    tf.logging.info("Downloading from %s to %s." % (url, filename))
    inprogress_filepath = filename + ".incomplete"
    inprogress_filepath, _ = urllib.request.urlretrieve(
        url, inprogress_filepath, reporthook=download_report_hook)
    # Print newline to clear the carriage return from the download progress.
    print()
    tf.gfile.Rename(inprogress_filepath, filename)
    return filename
  else:
    tf.logging.info("Already downloaded: %s (at %s)." % (url, found_file))
    return found_file

Download content from a url.

Args:
        path: string directory where file will be downloaded
        url: string url

Returns:
        Full path to downloaded file


def start(component, exact):
    # type: (str, str) -> None

    version_file = conf.get_path('version_file', 'VERSION')

    develop = conf.get('git.devel_branch', 'develop')
    common.assert_on_branch(develop)

    with conf.within_proj_dir():
        out = shell.run('git status --porcelain', capture=True).stdout
        lines = out.split(os.linesep)
        has_changes = any(
            not l.startswith('??') for l in lines if l.strip()
        )

    if has_changes:
        log.info("Cannot release: there are uncommitted changes")
        exit(1)

    old_ver, new_ver = versioning.bump(component, exact)

    log.info("Bumping package version")
    log.info("  old version: <35>{}".format(old_ver))
    log.info("  new version: <35>{}".format(new_ver))

    with conf.within_proj_dir():
        branch = 'release/' + new_ver

        common.git_checkout(branch, create=True)

        log.info("Creating commit for the release")
        shell.run('git add {ver_file} && git commit -m "{msg}"'.format(
            ver_file=version_file,
            msg="Releasing v{}".format(new_ver)
        ))

Create a new release branch.

Args:
        component (str):
            Version component to bump when creating the release. Can be *major*,
            *minor* or *patch*.
        exact (str):
            The exact version to set for the release. Overrides the component
            argument. This allows to re-release a version if something went
            wrong with the release upload.


def exclude(self, func=None):

        func = _make_callable(func)
        inverse = lambda x: not func(x)
        return self.filter(inverse)

Return a new Collection excluding some items

    Parameters:

        func : function(Node) -> Scalar

            A function that, when called on each item
            in the collection, returns a boolean-like
            value. If no function is provided, then
            truthy items will be removed.

Returns:
        A new Collection consisting of the items
        where bool(func(item)) == False


def __init__(self, action_type=None, dl_addr=None):

        super().__init__(action_type, length=16)
        self.dl_addr = dl_addr

Create an ActionDLAddr with the optional parameters below.

Args:
        action_type (:class:`~pyof.v0x01.common.action.ActionType`):
            :attr:`~ActionType.OFPAT_SET_DL_SRC` or
            :attr:`~ActionType.OFPAT_SET_DL_DST`.
        dl_addr (:class:`~.HWAddress`): Ethernet address.
            Defaults to None.


def segment(self, text):

        files = {'text': text}
        res, status_code = self.post(self.segmentation_service, files=files)

        if status_code != 200:
            logger.debug('Segmentation failed.')

        return self.decode(res), status_code

Call the segmenter in order to split text in sentences.

Args:
        text (str): Text to be segmented.

Returns:
        dict, int: A dict containing a list of dicts with the offsets of
            each sentence; an integer representing the response code.


def getFingerprintsForTexts(self, strings, sparsity=1.0):

        body = [{"text": s} for s in strings]
        return self._text.getRepresentationsForBulkText(self._retina, json.dumps(body), sparsity)

Bulk get Fingerprint for text.

Args:
        strings, list(str): A list of texts to be evaluated (required)
        sparsity, float: Sparsify the resulting expression to this percentage (optional)

Returns:
        list of Fingerprint

Raises:
        CorticalioException: if the request was not successful


def read(self, size=None):

    if not self._database_object:
      raise IOError('Not opened.')

    if self._current_offset < 0:
      raise IOError('Invalid offset value out of bounds.')

    if size == 0 or self._current_offset >= self._size:
      return b''

    if size is None:
      size = self._size
    if self._current_offset + size > self._size:
      size = self._size - self._current_offset

    start_offset = self._current_offset
    self._current_offset += size
    return self._blob[start_offset:self._current_offset]

Reads a byte string from the file-like object at the current offset.

    The function will read a byte string of the specified size or
    all of the remaining data if no size was specified.

Args:
        size (Optional[int]): number of bytes to read, where None is all
        remaining data.

Returns:
        bytes: data read.

Raises:
        IOError: if the read failed.
        OSError: if the read failed.


def enhance_pubmed_annotations(pubmed: Mapping[str, Any]) -> Mapping[str, Any]:

    text = pubmed["title"] + pubmed["abstract"]

    annotations = {}

    for nsarg in pubmed["annotations"]:
        url = f'{config["bel_api"]["servers"]["api_url"]}/terms/{url_path_param_quoting(nsarg)}'
        log.info(f"URL: {url}")
        r = get_url(url)
        log.info(f"Result: {r}")
        new_nsarg = ""
        if r and r.status_code == 200:
            term = r.json()
            new_nsarg = bel_utils.convert_nsarg(term["id"], decanonicalize=True)

            pubmed["annotations"][nsarg]["name"] = term["name"]
            pubmed["annotations"][nsarg]["label"] = term["label"]
            pubmed["annotations"][nsarg]["entity_types"] = list(
                set(
                    pubmed["annotations"][nsarg]["entity_types"]
                    + term.get("entity_types", [])
                )
            )
            pubmed["annotations"][nsarg]["annotation_types"] = list(
                set(
                    pubmed["annotations"][nsarg]["annotation_types"]
                    + term.get("annotation_types", [])
                )
            )

        if new_nsarg != nsarg:
            annotations[new_nsarg] = copy.deepcopy(pubmed["annotations"][nsarg])
        else:
            annotations[nsarg] = copy.deepcopy(pubmed["annotations"][nsarg])

    for nsarg in annotations:
        for idx, span in enumerate(annotations[nsarg]["spans"]):
            string = text[span["begin"] - 1 : span["end"] - 1]
            annotations[nsarg]["spans"][idx]["text"] = string

    pubmed["annotations"] = copy.deepcopy(annotations)

    return pubmed

Enhance pubmed namespace IDs

    Add additional entity and annotation types to annotations
    Use preferred id for namespaces as needed
    Add strings from Title, Abstract matching Pubtator BioConcept spans

    NOTE - basically duplicated code with bel_api:api.services.pubmed

Args:
        pubmed

Returns:
        pubmed object


def extract_cookies(self, response, request, referrer_host=None):

        new_response = HTTPResponseInfoWrapper(response)
        new_request = convert_http_request(request, referrer_host)

        self._cookie_jar.extract_cookies(new_response, new_request)

Wrapped ``extract_cookies``.

Args:
        response: An instance of :class:`.http.request.Response`.
        request: An instance of :class:`.http.request.Request`.
        referrer_host (str): An hostname or IP address of the referrer
            URL.


def random(cls, num_qubits, seed=None):

        if seed is not None:
            np.random.seed(seed)
        z = np.random.randint(2, size=num_qubits).astype(np.bool)
        x = np.random.randint(2, size=num_qubits).astype(np.bool)
        return cls(z, x)

Return a random Pauli on number of qubits.

Args:
        num_qubits (int): the number of qubits
        seed (int): Optional. To set a random seed.

Returns:
        Pauli: the random pauli


def get(cls, blob_key, **ctx_options):

    fut = cls.get_async(blob_key, **ctx_options)
    return fut.get_result()

Retrieve a BlobInfo by key.

Args:
        blob_key: A blob key.  This may be a str, unicode or BlobKey instance.
        **ctx_options: Context options for Model().get_by_id().

Returns:
        A BlobInfo entity associated with the provided key,  If there was
        no such entity, returns None.


def cancel(self, subscription_id, data={}, **kwargs):

        url = "{}/{}/cancel".format(self.base_url, subscription_id)
        return self.post_url(url, data, **kwargs)

Cancel subscription given by subscription_id

Args:
        subscription_id : Id for which subscription has to be cancelled

Returns:
        Subscription Dict for given subscription id


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 AddAnalysisReport(self, analysis_report):

    self._RaiseIfNotWritable()

    self._storage_file.AddAnalysisReport(analysis_report)

    report_identifier = analysis_report.plugin_name
    self._session.analysis_reports_counter['total'] += 1
    self._session.analysis_reports_counter[report_identifier] += 1
    self.number_of_analysis_reports += 1

Adds an analysis report.

Args:
        analysis_report (AnalysisReport): analysis report.

Raises:
        IOError: when the storage writer is closed.
        OSError: when the storage writer is closed.


def filename(self, appendix=None, create_if_not_existing=False):

        # if provided path is a relative path and self.data_path exists, build path
        if os.path.isabs(self.settings['path']) == False and self.data_path is not None:
            path = os.path.join(self.data_path, self.settings['path'])
        else:
            path = self.settings['path']

        tag = self.settings['tag']#.replace('.','-')

        filename = os.path.join(path, "{:s}_{:s}".format(self.start_time.strftime('%y%m%d-%H_%M_%S'),tag))

        if os.path.exists(filename) == False and create_if_not_existing:
            os.makedirs(filename)

        if appendix is not None:
            filename = os.path.join(filename,  "{:s}_{:s}{:s}".format(self.start_time.strftime('%y%m%d-%H_%M_%S'),tag,appendix))

        # windows can't deal with long filenames so we have to use the prefix '\\\\?\\'
        # if len(filename.split('\\\\?\\')) == 1:
        #     filename = '\\\\?\\' + filename

        return filename

creates a filename based

Args:
        appendix: appendix for file

    Returns: filename


def true_positives(y, y_pred):

    y, y_pred = convert_assert(y, y_pred)
    assert_binary_problem(y)

    return np.count_nonzero(y_pred[y == 1] == 1)

True-positives

    Parameters:
    -----------
    y : vector, shape (n_samples,)
    The target labels.

    y_pred : vector, shape (n_samples,)
    The predicted labels.

Returns:
    --------
    tp : integer, the number of true-positives


def write(self, b):

        if not self._writable:
            raise UnsupportedOperation('write')

        # This function write data in a buffer
        # "flush()" need to be called to really write content on
        # Cloud Storage
        size = len(b)
        with self._seek_lock:
            start = self._seek
            end = start + size
            self._seek = end

        buffer = self._write_buffer
        if end <= len(buffer):
            buffer = memoryview(buffer)
        buffer[start:end] = b
        return size

Write the given bytes-like object, b, to the underlying raw stream,
    and return the number of bytes written.

Args:
        b (bytes-like object): Bytes to write.

Returns:
        int: The number of bytes written.


def _full_axis_reduce(self, axis, func, alternate_index=None):

        result = self.data.map_across_full_axis(axis, func)
        if axis == 0:
            columns = alternate_index if alternate_index is not None else self.columns
            return self.__constructor__(result, index=["__reduced__"], columns=columns)
        else:
            index = alternate_index if alternate_index is not None else self.index
            return self.__constructor__(result, index=index, columns=["__reduced__"])

Applies map that reduce Manager to series but require knowledge of full axis.

Args:
        func: Function to reduce the Manager by. This function takes in a Manager.
        axis: axis to apply the function to.
        alternate_index: If the resulting series should have an index
            different from the current query_compiler's index or columns.

Returns:
        Pandas series containing the reduced data.


def parse(self, data, extent, desc_tag):
        # type: (bytes, int, UDFTag) -> None

        if self._initialized:
            raise pycdlibexception.PyCdlibInternalError('UDF Logical Volume Descriptor already initialized')

        (tag_unused, self.vol_desc_seqnum, self.desc_char_set,
         self.logical_vol_ident, logical_block_size, domain_ident,
         logical_volume_contents_use, map_table_length, num_partition_maps,
         impl_ident, self.implementation_use, self.integrity_sequence_length,
         self.integrity_sequence_extent, partition_map,
         end_unused) = struct.unpack_from(self.FMT, data, 0)

        self.desc_tag = desc_tag

        if logical_block_size != 2048:
            raise pycdlibexception.PyCdlibInvalidISO('Volume Descriptor block size is not 2048')

        self.domain_ident = UDFEntityID()
        self.domain_ident.parse(domain_ident)
        if self.domain_ident.identifier[:19] != b'*OSTA UDF Compliant':
            raise pycdlibexception.PyCdlibInvalidISO("Volume Descriptor Identifier not '*OSTA UDF Compliant'")

        if map_table_length != 6:
            raise pycdlibexception.PyCdlibInvalidISO('Volume Descriptor map table length not 6')

        if num_partition_maps != 1:
            raise pycdlibexception.PyCdlibInvalidISO('Volume Descriptor number of partition maps not 1')

        self.impl_ident = UDFEntityID()
        self.impl_ident.parse(impl_ident)

        self.partition_map = UDFPartitionMap()
        self.partition_map.parse(partition_map)

        self.logical_volume_contents_use = UDFLongAD()
        self.logical_volume_contents_use.parse(logical_volume_contents_use)

        self.orig_extent_loc = extent

        self._initialized = True

Parse the passed in data into a UDF Logical Volume Descriptor.

    Parameters:
    data - The data to parse.
    extent - The extent that this descriptor currently lives at.
    desc_tag - A UDFTag object that represents the Descriptor Tag.

Returns:
    Nothing.


def song_play(self, song):

		if 'storeId' in song:
			song_id = song['storeId']
		elif 'trackId' in song:
			song_id = song['trackId']
		else:
			song_id = song['id']

		song_duration = song['durationMillis']

		event = mc_calls.ActivityRecordRealtime.play(song_id, song_duration)
		response = self._call(
			mc_calls.ActivityRecordRealtime,
			event
		)

		return True if response.body['eventResults'][0]['code'] == 'OK' else False

Add play to song play count.

    Parameters:
    song (dict): A song dict.

Returns:
    bool: ``True`` if successful, ``False`` if not.


def compute_output_shape(self, input_shape):

    input_shape = tf.TensorShape(input_shape)
    input_shape = input_shape.with_rank_at_least(2)
    if tf.compat.dimension_value(input_shape[-1]) is None:
      raise ValueError(
          'The innermost dimension of `input_shape` must be defined, '
          'but saw: {}'.format(input_shape))
    return input_shape[:-1].concatenate(self.units)

Computes the output shape of the layer.

Args:
        input_shape: Shape tuple (tuple of integers) or list of shape tuples
        (one per output tensor of the layer). Shape tuples can include None for
        free dimensions, instead of an integer.

Returns:
        output_shape: A tuple representing the output shape.

Raises:
        ValueError: If innermost dimension of `input_shape` is not defined.


def set_current(self, current):

        self.current = current
        self.input = current.input
        # self.req = current.request
        # self.resp = current.response
        self.output = current.output
        self.cmd = current.task_data['cmd']

        if self.cmd and NEXT_CMD_SPLITTER in self.cmd:
            self.cmd, self.next_cmd = self.cmd.split(NEXT_CMD_SPLITTER)
        else:
            self.next_cmd = None

Creates some aliases for attributes of ``current``.

Args:
        current: :attr:`~zengine.engine.WFCurrent` object.


def _CreateOutputModule(self, options):

    formatter_mediator = formatters_mediator.FormatterMediator(
        data_location=self._data_location)

    try:
      formatter_mediator.SetPreferredLanguageIdentifier(
          self._preferred_language)
    except (KeyError, TypeError) as exception:
      raise RuntimeError(exception)

    mediator = output_mediator.OutputMediator(
        self._knowledge_base, formatter_mediator,
        preferred_encoding=self.preferred_encoding)
    mediator.SetTimezone(self._preferred_time_zone)

    try:
      output_module = output_manager.OutputManager.NewOutputModule(
          self._output_format, mediator)

    except (KeyError, ValueError) as exception:
      raise RuntimeError(
          'Unable to create output module with error: {0!s}'.format(
              exception))

    if output_manager.OutputManager.IsLinearOutputModule(self._output_format):
      output_file_object = open(self._output_filename, 'wb')
      output_writer = tools.FileObjectOutputWriter(output_file_object)
      output_module.SetOutputWriter(output_writer)

    helpers_manager.ArgumentHelperManager.ParseOptions(options, output_module)

    # Check if there are parameters that have not been defined and need to
    # in order for the output module to continue. Prompt user to supply
    # those that may be missing.
    missing_parameters = output_module.GetMissingArguments()
    while missing_parameters:
      for parameter in missing_parameters:
        value = self._PromptUserForInput(
            'Missing parameter {0:s} for output module'.format(parameter))
        if value is None:
          logger.warning(
              'Unable to set the missing parameter for: {0:s}'.format(
                  parameter))
          continue

        setattr(options, parameter, value)

      helpers_manager.ArgumentHelperManager.ParseOptions(
          options, output_module)
      missing_parameters = output_module.GetMissingArguments()

    return output_module

Creates the output module.

Args:
        options (argparse.Namespace): command line arguments.

Returns:
        OutputModule: output module.

Raises:
        RuntimeError: if the output module cannot be created.


def cols_(df,col_list,func,*args,**kwargs):

    return _pd.DataFrame({col_name:func(df,col_name,*args,**kwargs) for col_name in col_list})

Do a function over a list of columns and return the result
    Parameters:
    df - DataFrame
        DataFrame to operate on
    col_list - list of strings
        names of columns to coerce
    func - function
        function to use


def run_command(commands):
        # type: (Union[AnyStr, List[AnyStr]]) -> List[AnyStr]

        # commands = StringClass.convert_unicode2str(commands)
        # print(commands)

        use_shell = False
        subprocess_flags = 0
        startupinfo = None
        if sysstr == 'Windows':
            if isinstance(commands, list):
                commands = ' '.join(str(c) for c in commands)
            import ctypes
            SEM_NOGPFAULTERRORBOX = 0x0002  # From MSDN
            ctypes.windll.kernel32.SetErrorMode(SEM_NOGPFAULTERRORBOX)
            subprocess_flags = 0x8000000  # win32con.CREATE_NO_WINDOW?
            # this startupinfo structure prevents a console window from popping up on Windows
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            # not sure if node outputs on stderr or stdout so capture both
        else:  # for Linux/Unix OS, commands is better to be a list.
            if is_string(commands):
                use_shell = True
                # https://docs.python.org/2/library/subprocess.html
                #     Using shell=True can be a security hazard.
            elif isinstance(commands, list):
                # the executable path may be enclosed with quotes, if not windows, delete the quotes
                if commands[0][0] == commands[0][-1] == '"' or \
                        commands[0][0] == commands[0][-1] == "'":
                    commands[0] = commands[0][1:-1]
                for idx, v in enumerate(commands):
                    if isinstance(v, int) or isinstance(v, float):
                        # Fix :TypeError: execv() arg 2 must contain only strings
                        commands[idx] = repr(v)
        print(commands)
        process = subprocess.Popen(commands, shell=use_shell, stdout=subprocess.PIPE,
                                   stdin=open(os.devnull),
                                   stderr=subprocess.STDOUT, universal_newlines=True,
                                   startupinfo=startupinfo,
                                   creationflags=subprocess_flags)
        out, err = process.communicate()
        recode = process.returncode

        if out is None:
            return ['']
        if recode is not None and recode != 0:
            raise subprocess.CalledProcessError(-1, commands,
                                                "ERROR occurred when running subprocess!")
        if '\n' in out:
            return out.split('\n')

        return [out]

Execute external command, and return the output lines list. In windows, refers to
    `handling-subprocess-crash-in-windows`_.

Args:
        commands: string or list

Returns:
        output lines

    .. _handling-subprocess-crash-in-windows:
        https://stackoverflow.com/questions/5069224/handling-subprocess-crash-in-windows


def to_string(self, ast_obj=None, fmt: str = "medium") -> str:

        if not ast_obj:
            ast_obj = self

        bel_relation = None
        if self.bel_relation and fmt == "short":
            bel_relation = self.spec["relations"]["to_short"].get(
                self.bel_relation, self.bel_relation
            )
        elif self.bel_relation:
            bel_relation = self.spec["relations"]["to_long"].get(
                self.bel_relation, self.bel_relation
            )

        if self.bel_subject and bel_relation and self.bel_object:
            if isinstance(self.bel_object, BELAst):
                return "{} {} ({})".format(
                    self.bel_subject.to_string(fmt=fmt),
                    bel_relation,
                    self.bel_object.to_string(fmt=fmt),
                )
            else:
                return "{} {} {}".format(
                    self.bel_subject.to_string(fmt=fmt),
                    bel_relation,
                    self.bel_object.to_string(fmt=fmt),
                )

        elif self.bel_subject:
            return "{}".format(self.bel_subject.to_string(fmt=fmt))

        else:
            return ""

Convert AST object to string

Args:
        fmt (str): short, medium, long formatted BEL statements
            short = short function and short relation format
            medium = short function and long relation format
            long = long function and long relation format
        canonicalize

Returns:
        str: string version of BEL AST


def _buckets(data, bucket_count=None):

  # TODO(nickfelt): remove on-demand imports once dep situation is fixed.
  import tensorflow.compat.v1 as tf
  if bucket_count is None:
    bucket_count = summary_v2.DEFAULT_BUCKET_COUNT
  with tf.name_scope('buckets', values=[data, bucket_count]), \
       tf.control_dependencies([tf.assert_scalar(bucket_count),
                                tf.assert_type(bucket_count, tf.int32)]):
    data = tf.reshape(data, shape=[-1])  # flatten
    data = tf.cast(data, tf.float64)
    is_empty = tf.equal(tf.size(input=data), 0)

    def when_empty():
      return tf.constant([], shape=(0, 3), dtype=tf.float64)

    def when_nonempty():
      min_ = tf.reduce_min(input_tensor=data)
      max_ = tf.reduce_max(input_tensor=data)
      range_ = max_ - min_
      is_singular = tf.equal(range_, 0)

      def when_nonsingular():
        bucket_width = range_ / tf.cast(bucket_count, tf.float64)
        offsets = data - min_
        bucket_indices = tf.cast(tf.floor(offsets / bucket_width),
                                 dtype=tf.int32)
        clamped_indices = tf.minimum(bucket_indices, bucket_count - 1)
        one_hots = tf.one_hot(clamped_indices, depth=bucket_count)
        bucket_counts = tf.cast(tf.reduce_sum(input_tensor=one_hots, axis=0),
                                dtype=tf.float64)
        edges = tf.linspace(min_, max_, bucket_count + 1)
        left_edges = edges[:-1]
        right_edges = edges[1:]
        return tf.transpose(a=tf.stack(
            [left_edges, right_edges, bucket_counts]))

      def when_singular():
        center = min_
        bucket_starts = tf.stack([center - 0.5])
        bucket_ends = tf.stack([center + 0.5])
        bucket_counts = tf.stack([tf.cast(tf.size(input=data), tf.float64)])
        return tf.transpose(
            a=tf.stack([bucket_starts, bucket_ends, bucket_counts]))

      return tf.cond(is_singular, when_singular, when_nonsingular)

    return tf.cond(is_empty, when_empty, when_nonempty)

Create a TensorFlow op to group data into histogram buckets.

Args:
        data: A `Tensor` of any shape. Must be castable to `float64`.
        bucket_count: Optional positive `int` or scalar `int32` `Tensor`.

Returns:
        A `Tensor` of shape `[k, 3]` and type `float64`. The `i`th row is
        a triple `[left_edge, right_edge, count]` for a single bucket.
        The value of `k` is either `bucket_count` or `1` or `0`.


def _oai_to_xml(marc_oai):  # TODO: move this to MARC XML parser?

    record = MARCXMLRecord(marc_oai)
    record.oai_marc = False

    return record.to_XML()

Convert OAI to MARC XML.

Args:
        marc_oai (str): String with either OAI or MARC XML.

Returns:
        str: String with MARC XML.


def read(self, size=None):

    if not self._is_open:
      raise IOError('Not opened.')

    if size is None:
      size = self._size - self._file_object.tell()

    return self._file_object.read(size)

Reads a byte string from the file-like object at the current offset.

    The function will read a byte string of the specified size or
    all of the remaining data if no size was specified.

Args:
        size (Optional[int]): number of bytes to read, where None is all
        remaining data.

Returns:
        bytes: data read.

Raises:
        IOError: if the read failed.
        OSError: if the read failed.


def gradient_helper(optimizer, loss, var_list=None):

    if var_list is None:
      var_list = tf.compat.v1.trainable_variables()

    grads_and_vars = optimizer.compute_gradients(loss, var_list=var_list)
    grads = [pair[0] for pair in grads_and_vars]

    return grads, optimizer.apply_gradients(grads_and_vars)

A helper to get the gradients out at each step.

Args:
        optimizer: the optimizer op.
        loss: the op that computes your loss value.

    Returns: the gradient tensors and the train_step op.


def _evolve(self, state, qargs=None):

        # If subsystem evolution we use the SuperOp representation
        if qargs is not None:
            return SuperOp(self)._evolve(state, qargs)

        # Otherwise we compute full evolution directly
        state = self._format_state(state)
        if state.shape[0] != self._input_dim:
            raise QiskitError(
                "QuantumChannel input dimension is not equal to state dimension."
            )
        if state.ndim == 1 and self._data[1] is None and len(
                self._data[0]) == 1:
            # If we only have a single Kraus operator we can implement unitary-type
            # evolution of a state vector psi -> K[0].psi
            return np.dot(self._data[0][0], state)
        # Otherwise we always return a density matrix
        state = self._format_state(state, density_matrix=True)
        kraus_l, kraus_r = self._data
        if kraus_r is None:
            kraus_r = kraus_l
        return np.einsum('AiB,BC,AjC->ij', kraus_l, state,
                         np.conjugate(kraus_r))

Evolve a quantum state by the QuantumChannel.

Args:
        state (QuantumState): The input statevector or density matrix.
        qargs (list): a list of QuantumState subsystem positions to apply
                    the operator on.

Returns:
        QuantumState: the output quantum state.

Raises:
        QiskitError: if the operator dimension does not match the
        specified QuantumState subsystem dimensions.


def _compute_nfp_real(l, u, counts, sizes):

    if l > u:
        raise ValueError("l must be less or equal to u")
    return np.sum((float(sizes[u])-sizes[l:u+1])/float(sizes[u])*counts[l:u+1])

Computes the expected number of false positives caused by using
    u to approximate set sizes in the interval [l, u], using the real
    set size distribution.

Args:
        l: the lower bound on set sizes.
        u: the upper bound on set sizes.
        counts: the complete distribution of set sizes.
        sizes: the complete domain of set sizes.

    Return (float): the expected number of false positives.


def get_problem_name(base_name, was_reversed=False, was_copy=False):

  if any(base_name.endswith(suffix) for suffix in ("_rev", "_copy")):
    raise ValueError("`base_name` cannot end in '_rev' or '_copy'")
  name = base_name
  if was_copy:
    name = "%s_copy" % name
  if was_reversed:
    name = "%s_rev" % name
  return name

Construct a problem name from base and reversed/copy options.

    Inverse of `parse_problem_name`.

Args:
        base_name: base problem name. Should not end in "_rev" or "_copy"
        was_reversed: if the problem is to be reversed
        was_copy: if the problem is to be copied

Returns:
        string name consistent with use with `parse_problem_name`.

Raises:
        ValueError if `base_name` ends with "_rev" or "_copy"


def _processing_limit(self, spec):

    processing_rate = float(spec.mapper.params.get("processing_rate", 0))
    slice_processing_limit = -1
    if processing_rate > 0:
      slice_processing_limit = int(math.ceil(
          parameters.config._SLICE_DURATION_SEC*processing_rate/
          int(spec.mapper.shard_count)))
    return slice_processing_limit

Get the limit on the number of map calls allowed by this slice.

Args:
        spec: a Mapreduce spec.

Returns:
        The limit as a positive int if specified by user. -1 otherwise.


def __init__(self, campfire, data=None):

        super(CampfireEntity, self).__init__(data)
        self._campfire = campfire
        self._connection = None
        if self._campfire:
            self._connection = self._campfire.get_connection()

Initialize.

Args:
        campfire (:class:`Campfire`): Campfire Instance

    Kwargs:
        data (dict): Entity data


def __init__(self, channel):

    self.Alarm = channel.unary_unary(
        '/etcdserverpb.Maintenance/Alarm',
        request_serializer=rpc__pb2.AlarmRequest.SerializeToString,
        response_deserializer=rpc__pb2.AlarmResponse.FromString,
        )
    self.Status = channel.unary_unary(
        '/etcdserverpb.Maintenance/Status',
        request_serializer=rpc__pb2.StatusRequest.SerializeToString,
        response_deserializer=rpc__pb2.StatusResponse.FromString,
        )
    self.Defragment = channel.unary_unary(
        '/etcdserverpb.Maintenance/Defragment',
        request_serializer=rpc__pb2.DefragmentRequest.SerializeToString,
        response_deserializer=rpc__pb2.DefragmentResponse.FromString,
        )
    self.Hash = channel.unary_unary(
        '/etcdserverpb.Maintenance/Hash',
        request_serializer=rpc__pb2.HashRequest.SerializeToString,
        response_deserializer=rpc__pb2.HashResponse.FromString,
        )
    self.HashKV = channel.unary_unary(
        '/etcdserverpb.Maintenance/HashKV',
        request_serializer=rpc__pb2.HashKVRequest.SerializeToString,
        response_deserializer=rpc__pb2.HashKVResponse.FromString,
        )
    self.Snapshot = channel.unary_stream(
        '/etcdserverpb.Maintenance/Snapshot',
        request_serializer=rpc__pb2.SnapshotRequest.SerializeToString,
        response_deserializer=rpc__pb2.SnapshotResponse.FromString,
        )
    self.MoveLeader = channel.unary_unary(
        '/etcdserverpb.Maintenance/MoveLeader',
        request_serializer=rpc__pb2.MoveLeaderRequest.SerializeToString,
        response_deserializer=rpc__pb2.MoveLeaderResponse.FromString,
        )

Constructor.

Args:
        channel: A grpc.Channel.


def playlist_create(
		self,
		name,
		description='',
		*,
		make_public=False,
		songs=None
	):

		share_state = 'PUBLIC' if make_public else 'PRIVATE'

		playlist = self._call(
			mc_calls.PlaylistsCreate,
			name,
			description,
			share_state
		).body

		if songs:
			playlist = self.playlist_songs_add(songs, playlist)

		return playlist

Create a playlist.

    Parameters:
    name (str): Name to give the playlist.
    description (str): Description to give the playlist.
    make_public (bool, Optional): If ``True`` and account has a subscription,
        make playlist public.
        Default: ``False``
    songs (list, Optional): A list of song dicts to add to the playlist.

Returns:
    dict: Playlist information.


def analyze_number(var, err=''):

    n, u = split_unit(var)
    if not isinstance(var, string_types):
        return (var, u)
    if is_color(var):
        return (var, 'color')
    if is_int(n):
        n = int(n)
    elif is_float(n):
        n = float(n)
    else:
        raise SyntaxError('%s ´%s´' % (err, var))
    return (n, u)

Analyse number for type and split from unit
    1px -> (q, 'px')

Args:
    var (str): number string
    kwargs:
    err (str): Error message

Raises:
    SyntaxError

Returns:
    tuple


def write_sudoers_entry(username=None, sudoers_entry=None):

    sudoers_path = '/etc/sudoers'
    rnd_chars = random_string(length=RANDOM_FILE_EXT_LENGTH)
    tmp_sudoers_path = '/tmp/sudoers_{0}'.format(rnd_chars)
    execute_command(
        shlex.split(str('{0} cp {1} {2}'.format(sudo_check(), sudoers_path, tmp_sudoers_path))))
    execute_command(
        shlex.split(str('{0} chmod 777 {1}'.format(sudo_check(), tmp_sudoers_path))))
    with open(tmp_sudoers_path, mode=text_type('r')) as tmp_sudoers_file:
        sudoers_entries = tmp_sudoers_file.readlines()
    sudoers_output = list()
    for entry in sudoers_entries:
        if entry and not entry.startswith(username):
            sudoers_output.append(entry)
    if sudoers_entry:
        sudoers_output.append('{0} {1}'.format(username, sudoers_entry))
        sudoers_output.append('\n')
    with open(tmp_sudoers_path, mode=text_type('w+')) as tmp_sudoers_file:
        tmp_sudoers_file.writelines(sudoers_output)
    sudoers_check_result = execute_command(
        shlex.split(str('{0} {1} -cf {2}'.format(sudo_check(), LINUX_CMD_VISUDO, tmp_sudoers_path))))
    if sudoers_check_result[1] > 0:
        raise ValueError(sudoers_check_result[0][1])
    execute_command(
        shlex.split(str('{0} cp {1} {2}'.format(sudo_check(), tmp_sudoers_path, sudoers_path))))
    execute_command(shlex.split(str('{0} chown root:root {1}'.format(sudo_check(), sudoers_path))))
    execute_command(shlex.split(str('{0} chmod 440 {1}'.format(sudo_check(), sudoers_path))))
    execute_command(shlex.split(str('{0} rm {1}'.format(sudo_check(), tmp_sudoers_path))))

Write sudoers entry.

Args:
        user (User): Instance of User containing sudoers entry.

Returns:
        str: sudoers entry for the specified user.


def record(self, rr_version):
        # type: (str) -> bytes

        if not self._initialized:
            raise pycdlibexception.PyCdlibInternalError('PX record not yet initialized!')

        outlist = [b'PX', struct.pack('=BBLLLLLLLL', RRPXRecord.length(rr_version),
                                      SU_ENTRY_VERSION, self.posix_file_mode,
                                      utils.swab_32bit(self.posix_file_mode),
                                      self.posix_file_links,
                                      utils.swab_32bit(self.posix_file_links),
                                      self.posix_user_id,
                                      utils.swab_32bit(self.posix_user_id),
                                      self.posix_group_id,
                                      utils.swab_32bit(self.posix_group_id))]
        if rr_version == '1.12':
            outlist.append(struct.pack('=LL', self.posix_serial_number,
                                       utils.swab_32bit(self.posix_serial_number)))
        # The rr_version can never be "wrong" at this point; if it was, it would
        # have thrown an exception earlier when calling length().  So just skip
        # any potential checks here.

        return b''.join(outlist)

Generate a string representing the Rock Ridge POSIX File Attributes
    record.

    Parameters:
    rr_version - The Rock Ridge version to use.

Returns:
    String containing the Rock Ridge record.


def AddProcessingOptions(self, argument_group):

    argument_group.add_argument(
        '--single_process', '--single-process', dest='single_process',
        action='store_true', default=False, help=(
            'Indicate that the tool should run in a single process.'))

    argument_helper_names = ['temporary_directory', 'workers', 'zeromq']
    if self._CanEnforceProcessMemoryLimit():
      argument_helper_names.append('process_resources')
    helpers_manager.ArgumentHelperManager.AddCommandLineArguments(
        argument_group, names=argument_helper_names)

Adds the processing options to the argument group.

Args:
        argument_group (argparse._ArgumentGroup): argparse argument group.


def Serialize(self, writer):

        super(AccountState, self).Serialize(writer)
        writer.WriteUInt160(self.ScriptHash)
        writer.WriteBool(self.IsFrozen)
        writer.WriteVarInt(len(self.Votes))
        for vote in self.Votes:
            writer.WriteBytes(vote)

        blen = len(self.Balances)
        writer.WriteVarInt(blen)

        for key, fixed8 in self.Balances.items():
            writer.WriteUInt256(key)
            writer.WriteFixed8(fixed8)

Serialize full object.

Args:
        writer (neo.IO.BinaryWriter):


def from_config(config, **options):

        expected_args = ('prefix', 'realclass')
        for arg in expected_args:
            if arg not in options:
                msg = "Required option missing: {0}"
                raise rconfig.ConfigurationError(msg.format(arg))
        # Not logging unrecognized options here, because they might be used
        # by the real event store instantiated below.

        classpath = options['realclass']
        classpath_pieces = classpath.split('.')
        classname = classpath_pieces[-1]
        modulepath = '.'.join(classpath_pieces[0:-1])
        module = importlib.import_module(modulepath)
        estore_class = getattr(module, classname)

        return RotatedEventStore(lambda fname: estore_class(fname),
                                 options['path'], options['prefix'])

Instantiate an `RotatedEventStore` from config.

    Parameters:
    _config    -- the configuration file options read from file(s).
    **options -- various options given to the specific event store. Shall
                not be used with this event store. Warning will be logged
                for every extra non-recognized option. The only required
                key to this function is 'path'.

    returns   -- a newly instantiated `RotatedEventStore`.


def _getFuncArgs(func):
  r
  code = func.func_code
  Defaults = func.func_defaults

  nargs = code.co_argcount
  ArgNames = code.co_varnames[:nargs]

  Args = OrderedDict()
  argCount = len(ArgNames)
  defCount = len(Defaults) if Defaults else 0
  diff = argCount - defCount

  for i in range(0, diff):
    Args[ArgNames[i]] = {}

  for i in range(diff, argCount):
    Args[ArgNames[i]] = {'default': Defaults[i - diff]}

  return Args

r"""Gives the details on the args of the given func.

Args:
        func (function): The function to get details on.


def parse_transcripts(transcript_lines):

    LOG.info("Parsing transcripts")
    # Parse the transcripts, we need to check if it is a request or a file handle
    if isinstance(transcript_lines, DataFrame):
        transcripts = parse_ensembl_transcript_request(transcript_lines)
    else:
        transcripts = parse_ensembl_transcripts(transcript_lines)

    # Since there can be multiple lines with information about the same transcript
    # we store transcript information in a dictionary for now
    parsed_transcripts = {}
    # Loop over the parsed transcripts
    for tx in transcripts:
        tx_id = tx['ensembl_transcript_id']
        ens_gene_id = tx['ensembl_gene_id']

        # Check if the transcript has been added
        # If not, create a new transcript
        if not tx_id in parsed_transcripts:
            tx_info = {
                'chrom': tx['chrom'],
                'transcript_start': tx['transcript_start'],
                'transcript_end': tx['transcript_end'],
                'mrna': set(),
                'mrna_predicted': set(),
                'nc_rna': set(),
                'ensembl_gene_id': ens_gene_id,
                'ensembl_transcript_id': tx_id,
            }
            parsed_transcripts[tx_id] = tx_info

        tx_info = parsed_transcripts[tx_id]
        # Add the ref seq information
        if tx.get('refseq_mrna_predicted'):
            tx_info['mrna_predicted'].add(tx['refseq_mrna_predicted'])
        if tx.get('refseq_mrna'):
            tx_info['mrna'].add(tx['refseq_mrna'])
        if tx.get('refseq_ncrna'):
            tx_info['nc_rna'].add(tx['refseq_ncrna'])

    return parsed_transcripts

Parse and massage the transcript information

    There could be multiple lines with information about the same transcript.
    This is why it is necessary to parse the transcripts first and then return a dictionary
    where all information has been merged.

Args:
        transcript_lines(): This could be an iterable with strings or a pandas.DataFrame

Returns:
        parsed_transcripts(dict): Map from enstid -> transcript info


def get_session_user(session_id, **kwargs):

    hydra_session_object = session.SessionObject({}, #This is normally a request object, but in this case is empty
            validate_key=config.get('COOKIES', 'VALIDATE_KEY', 'YxaDbzUUSo08b+'),
            type='file',
            cookie_expires=True,
            data_dir=config.get('COOKIES', 'DATA_DIR', '/tmp'),
            file_dir=config.get('COOKIES', 'FILE_DIR', '/tmp/auth'))

    hydra_session = hydra_session_object.get_by_id(session_id)

    if hydra_session is not None:
        return hydra_session['user_id']

    return None

Given a session ID, get the user ID that it is associated with

Args:
        session_id (string): The user's ID to identify the cookie to remove

Returns:
        user_id (string) or None if the session does not exist


def one_of(self, chset: str) -> str:

        res = self.peek()
        if res in chset:
            self.offset += 1
            return res
        raise UnexpectedInput(self, "one of " + chset)

Parse one character form the specified set.

Args:
        chset: string of characters to try as alternatives.

Returns:
        The character that was actually matched.

Raises:
        UnexpectedInput: If the next character is not in `chset`.


def __getitem__(self, pkg_id):

        if pkg_id in self.__reg_software:
            return self.__reg_software[pkg_id]
        else:
            raise KeyError(pkg_id)

Returns information on a package.

Args:
        pkg_id (str): Package Id of the software/component

Returns:
        dict or list: List if ``version_only`` is ``True`` otherwise dict


def get_profiles(adapter, vcf_file):

    vcf = get_file_handle(vcf_file)
    individuals = vcf.samples
    profiles = {individual: [] for individual in individuals}

    for profile_variant in adapter.profile_variants():

        ref = profile_variant['ref']
        alt = profile_variant['alt']

        pos = profile_variant['pos']
        end = pos + 1
        chrom = profile_variant['chrom']

        region = f"{chrom}:{pos}-{end}"

        #Find variants in region

        found_variant = False
        for variant in vcf(region):

            variant_id = get_variant_id(variant)

            #If variant id i.e. chrom_pos_ref_alt matches
            if variant_id == profile_variant['_id']:
                found_variant = True
                #find genotype for each individual in vcf
                for i, individual in enumerate(individuals):

                    genotype = GENOTYPE_MAP[variant.gt_types[i]]
                    if genotype == 'hom_alt':
                        gt_str = f"{alt}{alt}"
                    elif genotype == 'het':
                        gt_str = f"{ref}{alt}"
                    else:
                        gt_str = f"{ref}{ref}"

                    #Append genotype to profile string of individual
                    profiles[individual].append(gt_str)

                #Break loop if variant is found in region
                break

        #If no call was found for variant, give all samples a hom ref genotype
        if not found_variant:
            for individual in individuals: profiles[individual].append(f"{ref}{ref}")

    return profiles

Given a vcf, get a profile string for each sample in the vcf
    based on the profile variants in the database

Args:
        adapter(MongoAdapter): Adapter to mongodb
        vcf_file(str): Path to vcf file

Returns:
        profiles (dict(str)): The profiles (given as strings) for each sample
                            in vcf.


def __init__(self, callback):

        self._callback = callback
        self._brocade_vswitch = brocade_vswitch(callback=pynos.utilities.return_xml)

Vcenter init function

Args:
        callback: Callback function that will be called for each action

Returns:
        Vcenter Object

Raises:
        None


def import_gssapi_extension(name):

    try:
        path = 'gssapi.raw.ext_{0}'.format(name)
        __import__(path)
        return sys.modules[path]
    except ImportError:
        return None

Import a GSSAPI extension module

    This method imports a GSSAPI extension module based
    on the name of the extension (not including the
    'ext_' prefix).  If the extension is not available,
    the method retuns None.

Args:
        name (str): the name of the extension

Returns:
        module: Either the extension module or None


def get_metric_parsers(metric_packages=tuple(), include_defaults=True):

    metric_parsers = set()

    if include_defaults:
        import git_code_debt.metrics
        metric_parsers.update(discover(git_code_debt.metrics, is_metric_cls))

    for metric_package in metric_packages:
        metric_parsers.update(discover(metric_package, is_metric_cls))
    return metric_parsers

Gets all of the metric parsers.

Args:
        metric_packages - Defaults to no extra packages. An iterable of
            metric containing packages.  A metric inherits DiffParserBase
            and does not have __metric__ = False
            A metric package must be imported using import a.b.c
        include_defaults - Whether to include the generic metric parsers