def clean_all(G, settings):

    quiet = settings["quiet"]
    recon = settings["recon"]
    sprint = settings["sprint"]
    error = settings["error"]
    all_outputs = []
    for node in G.nodes(data=True):
        if "output" in node[1]:
            for item in get_all_outputs(node[1]):
                all_outputs.append(item)
    all_outputs.append(".shastore")
    retcode = 0
    for item in sorted(all_outputs):
        if os.path.isfile(item):
            if recon:
                sprint("Would remove file: {}".format(item))
                continue
            sprint("Attempting to remove file '{}'", level="verbose")
            try:
                os.remove(item)
                sprint("Removed file", level="verbose")
            except:
                errmes = "Error: file '{}' failed to be removed"
                error(errmes.format(item))
                retcode = 1
    if not retcode and not recon:
        sprint("All clean", color=True)
    return retcode

Removes all the output files from all targets. Takes
    the graph as the only argument

Args:
        The networkx graph object
        The settings dictionary

Returns:
        0 if successful
        1 if removing even one file failed


def GetMessages(self, formatter_mediator, event):

    if self.DATA_TYPE != event.data_type:
      raise errors.WrongFormatter('Unsupported data type: {0:s}.'.format(
          event.data_type))

    event_values = event.CopyToDict()

    login_type = event_values.get('type', None)
    if login_type is None:
      status = 'N/A'
    else:
      status = self._STATUS_TYPES.get(login_type, 'UNKNOWN')

    event_values['status'] = status

    return self._ConditionalFormatMessages(event_values)

Determines the formatted message strings for an event object.

Args:
        formatter_mediator (FormatterMediator): mediates the interactions
        between formatters and other components, such as storage and Windows
        EventLog resources.
        event (EventObject): event.

Returns:
        tuple(str, str): formatted message string and short message string.

Raises:
        WrongFormatter: if the event object cannot be formatted by the formatter.


def percentile(self, percent):

        # Sanity check: Any value over 100 should become 100.
        if percent >= 100:
            percent = 100

        # Determine the actual target number.
        target = len(self) - len(self) * (percent / 100)

        # Iterate over the values in reverse, dropping the target by the
        # number of times each value has been seen. When the target passes
        # 0, return the value we are currently viewing.
        for k in reversed(sorted(self._data.keys())):
            target -= self._data[k]
            if target < 0:
                return k

        # The only way to get here is if there was no data.
        # In this case, just return 10 seconds.
        return 10

Return the value that is the Nth precentile in the histogram.

Args:
        percent (Union[int, float]): The precentile being sought. The
            default consumer implementations use consistently use ``99``.

Returns:
        int: The value corresponding to the requested percentile.


def _get_args(cls, args):
        # type: (tuple) -> Tuple[type, slice, Callable]

        if isinstance(args, tuple):
            if not len(args) == 2:
                raise TypeError(
                    "{}[...] takes one or two argument.".format(cls.__name__)
                )
            return super(_ValidationBoundedMeta, cls)._get_args(
                (args[0], None, args[1])
            )
        return super(_ValidationBoundedMeta, cls)._get_args((Any, None, args))

Return the parameters necessary to check type boundaries.

Args:
        args: A tuple with one or two parameters: A type to cast the
            value passed, and a predicate function to use for bounds
            checking.

Returns:
        A tuple with three parameters: a type, a slice, and the predicate
        function. If no type was passed in args, the type defaults to Any.


def __init__(self, protojson_protocol=None, **kwargs):

        super(MessageJSONEncoder, self).__init__(**kwargs)
        self.__protojson_protocol = (
            protojson_protocol or ProtoJson.get_default())

Constructor.

Args:
        protojson_protocol: ProtoJson instance.


def dump(self,format='ttl'):

		return self.rdf.graph.serialize(format=format).decode('utf-8')

Convenience method to return RDF data for resource,
    optionally selecting serialization format.
    Inspired by .dump from Samvera.

Args:
    format (str): expecting serialization formats accepted by rdflib.serialization(format=)


def copy_submission_to_destination(self, src_filename, dst_subdir,
                                     submission_id):

    extension = [e for e in ALLOWED_EXTENSIONS if src_filename.endswith(e)]
    if len(extension) != 1:
      logging.error('Invalid submission extension: %s', src_filename)
      return
    dst_filename = os.path.join(self.target_dir, dst_subdir,
                                submission_id + extension[0])
    cmd = ['gsutil', 'cp', src_filename, dst_filename]
    if subprocess.call(cmd) != 0:
      logging.error('Can\'t copy submission to destination')
    else:
      logging.info('Submission copied to: %s', dst_filename)

Copies submission to target directory.

Args:
        src_filename: source filename of the submission
        dst_subdir: subdirectory of the target directory where submission should
        be copied to
        submission_id: ID of the submission, will be used as a new
        submission filename (before extension)


def check_errors(self, is_global=False):

    errors = self.global_errors if is_global else self.errors
    if errors:
      print('dfTimewolf encountered one or more errors:')
      for error, critical in errors:
        print('{0:s}  {1:s}'.format('CRITICAL: ' if critical else '', error))
        if critical:
          print('Critical error found. Aborting.')
          sys.exit(-1)

Checks for errors and exits if any of them are critical.

Args:
        is_global: If True, check the global_errors attribute. If false, check the
        error attribute.


def genome_name_from_fasta_path(fasta_path):

    filename = os.path.basename(fasta_path)
    return re.sub(r'(\.fa$)|(\.fas$)|(\.fasta$)|(\.fna$)|(\.\w{1,}$)', '', filename)

Extract genome name from fasta filename

    Get the filename without directory and remove the file extension.

Example:
        With fasta file path ``/path/to/genome_1.fasta``::

            fasta_path = '/path/to/genome_1.fasta'
            genome_name = genome_name_from_fasta_path(fasta_path)
            print(genome_name)
            # => "genome_1"

Args:
        fasta_path (str): fasta file path

Returns:
        str: genome name


def enable_network(self, *hostnames):

        def hostname_filter(hostname, req):
            if isregex(hostname):
                return hostname.match(req.url.hostname)
            return req.url.hostname == hostname

        for hostname in hostnames:
            self.use_network_filter(partial(hostname_filter, hostname))

        self.networking = True

Enables real networking mode, optionally passing one or multiple
    hostnames that would be used as filter.

    If at least one hostname matches with the outgoing traffic, the
    request will be executed via the real network.

Args:
        *hostnames: optional list of host names to enable real network
            against them. hostname value can be a regular expression.


def _FilterOutPathInfoDuplicates(path_infos):

  pi_dict = {}

  for pi in path_infos:
    path_key = (pi.path_type, pi.GetPathID())
    pi_dict.setdefault(path_key, []).append(pi)

  def _SortKey(pi):
    return (
        pi.stat_entry.st_ctime,
        pi.stat_entry.st_mtime,
        pi.stat_entry.st_atime,
        pi.stat_entry.st_ino,
    )

  for pi_values in pi_dict.values():
    if len(pi_values) > 1:
      pi_values.sort(key=_SortKey, reverse=True)

  return [v[0] for v in pi_dict.values()]

Filters out duplicates from passed PathInfo objects.

Args:
        path_infos: An iterable with PathInfo objects.

Returns:
        A list of PathInfo objects with duplicates removed. Duplicates are
        removed following this logic: they're sorted by (ctime, mtime, atime,
        inode number) in the descending order and then the first one is taken
        and the others are dropped.


def __ge__(self, other):

    if not isinstance(other, DateTimeValues):
      raise ValueError('Other not an instance of DateTimeValues')

    normalized_timestamp = self._GetNormalizedTimestamp()
    other_normalized_timestamp = other._GetNormalizedTimestamp()  # pylint: disable=protected-access

    if normalized_timestamp is None:
      return other_normalized_timestamp is None

    if other_normalized_timestamp is None:
      return True

    return normalized_timestamp >= other_normalized_timestamp

Determines if the date time values are greater than or equal to other.

Args:
        other (DateTimeValues): date time values to compare against.

Returns:
        bool: True if the date time values are greater than or equal to other.

Raises:
        ValueError: if other is not an instance of DateTimeValues.


def get_gdns_publisher(config, metrics, **kwargs):

    builder = gdns_publisher.GDNSPublisherBuilder(
        config, metrics, **kwargs)
    return builder.build_publisher()

Get a GDNSPublisher client.

    A factory function that validates configuration and returns a
    publisher client (:interface:`gordon.interfaces.IMessageHandler`)
    provider.

Args:
        config (dict): Google Cloud DNS API related configuration.
        metrics (obj): :interface:`IMetricRelay` implementation.
        kwargs (dict): Additional keyword arguments to pass to the
            publisher.

Returns:
        A :class:`GDNSPublisher` instance.


def convert_selu(params, w_name, scope_name, inputs, layers, weights, names):

    print('Converting selu ...')

    if names == 'short':
        tf_name = 'SELU' + random_string(4)
    elif names == 'keep':
        tf_name = w_name
    else:
        tf_name = w_name + str(random.random())

    selu = keras.layers.Activation('selu', name=tf_name)
    layers[scope_name] = selu(layers[inputs[0]])

Convert selu layer.

Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        names: use short names for keras layers


def is_installed(self, name: str) -> bool:

        assert name is not None
        try:
            self.__docker.images.get(name)
            return True
        except docker.errors.ImageNotFound:
            return False

Indicates a given Docker image is installed on this server.

    Parameters:
        name: the name of the Docker image.

Returns:
        `True` if installed; `False` if not.


def d_step(self, true_frames, gen_frames):

    hparam_to_disc_loss = {
        "least_squares": gan_losses.least_squares_discriminator_loss,
        "cross_entropy": gan_losses.modified_discriminator_loss,
        "wasserstein": gan_losses.wasserstein_discriminator_loss}

    # Concat across batch-axis.
    _, batch_size, _, _, _ = common_layers.shape_list(true_frames)
    all_frames = tf.concat(
        [true_frames, tf.stop_gradient(gen_frames)], axis=1)

    all_logits = self.discriminator(all_frames)
    true_logits, fake_logits_stop = \
      all_logits[:batch_size], all_logits[batch_size:]
    mean_true_logits = tf.reduce_mean(true_logits)
    tf.summary.scalar("mean_true_logits", mean_true_logits)

    mean_fake_logits_stop = tf.reduce_mean(fake_logits_stop)
    tf.summary.scalar("mean_fake_logits_stop", mean_fake_logits_stop)

    discriminator_loss_func = hparam_to_disc_loss[self.hparams.gan_loss]
    gan_d_loss = discriminator_loss_func(
        discriminator_real_outputs=true_logits,
        discriminator_gen_outputs=fake_logits_stop,
        add_summaries=True)
    return gan_d_loss, true_logits, fake_logits_stop

Performs the discriminator step in computing the GAN loss.

    Applies stop-gradient to the generated frames while computing the
    discriminator loss to make sure that the gradients are not back-propagated
    to the generator. This makes sure that only the discriminator is updated.

Args:
        true_frames: True outputs
        gen_frames: Generated frames.

Returns:
        d_loss: Loss component due to the discriminator.


def howPlotArgs(goodFormat):

    if args.exportplots is not None:
        exportPlotsPath = pathlib.Path(args.exportplots)

        if args.showplots:
            plotter(exportPlotsPath, True, goodFormat)
        else:
            plotter(exportPlotsPath, False, goodFormat)
    elif args.showplots:
        plotter(None, True, goodFormat)
    else:
        howPlotAsk(goodFormat)

plots using argparse if can, if not uses howPlotask()

Args:
        goodFormat {dict} -- module : [results for module]


def find_rule(condition):

        final_condition = re.sub('{{.*}}', '42', condition)
        ast_tokens = Condition.get_tokens(final_condition)
        ast_compressed_tokens = Condition.compress_tokens(ast_tokens)

        name = 'undefined'
        function = lambda tokens: False

        if len(ast_compressed_tokens) > 0:
            for rule in Condition.RULES:
                if Condition.match_tokens(ast_compressed_tokens, rule['types']):
                    name = rule['name']
                    function = rule['evaluate']
                    break
        return name, ast_tokens, function

Find rule for given condition.

Args:
        condition (str): Python condition as string.

Returns:
        str, list, function: found rule name, list of AST tokens for condition
                            and verification function.


def setEditable(self, editable):

        if not isinstance(editable, bool):
            raise TypeError('Argument is not of type bool')
        self._editable = editable

setter to _editable. apply changes while changing dtype.

Raises:
        TypeError: if editable is not of type bool.

Args:
        editable (bool): apply changes while changing dtype.


def check_par(chrom, pos):

    par = False

    for interval in PAR.get(chrom,[]):
        if (pos >= interval[0] and pos <= interval[1]):
            par = True

    return par

Check if a coordinate is in the PAR region

Args:
        chrom(str)
        pos(int)

Returns:
        par(bool)


def new(self, dev_t_high, dev_t_low):
        # type: (int, int) -> None

        if self._initialized:
            raise pycdlibexception.PyCdlibInternalError('PN record already initialized!')

        self.dev_t_high = dev_t_high
        self.dev_t_low = dev_t_low

        self._initialized = True

Create a new Rock Ridge POSIX device number record.

    Parameters:
    dev_t_high - The high-order 32-bits of the device number.
    dev_t_low - The low-order 32-bits of the device number.

Returns:
    Nothing.


def index_2d(seqs: List[List[Any]], target: Any) -> Tuple[int, int]:

    for i in range(len(seqs)):
        for j in range(len(seqs[i])):
            if seqs[i][j] == target:
                return i, j
    raise ValueError('Item not present.')

Finds the first index of a target item within a list of lists.

Args:
        seqs: The list of lists to search.
        target: The item to find.

Raises:
        ValueError: Item is not present.


def validate_redis(self, db_data, user_data, oper):

        passed = True
        # convert any int to string since playbooks don't support int values
        if isinstance(db_data, int):
            db_data = str(db_data)
        if isinstance(user_data, int):
            user_data = str(user_data)

        # try to sort list of strings for simple comparisons
        # if list has a more complex data structure the sort will fail
        if isinstance(db_data, (list)):
            try:
                db_data = sorted(db_data)
            except TypeError:
                # self.log.debug('[validate] could not sort list')
                pass
        if isinstance(user_data, (list)):
            try:
                user_data = sorted(user_data)
            except TypeError:
                # self.log.debug('[validate] could not sort list')
                pass

        if oper not in self.operators:
            self.log.error('Invalid operator provided ({})'.format(oper))
            return False

        # compare the data
        if self.operators.get(oper)(db_data, user_data):
            self.reports.profile_validation(True)
        else:
            self.reports.profile_validation(False)
            passed = False

        # log validation data in a readable format
        self.validate_log_output(passed, db_data, user_data, oper)

        return passed

Validate data in Redis.

Args:
        db_data (str): The data store in Redis.
        user_data (str): The user provided data.
        oper (str): The comparison operator.

Returns:
        bool: True if the data passed validation.


def PluginAssets(self, plugin_name):

    with self._accumulators_mutex:
      # To avoid nested locks, we construct a copy of the run-accumulator map
      items = list(six.iteritems(self._accumulators))

    return {run: accum.PluginAssets(plugin_name) for run, accum in items}

Get index of runs and assets for a given plugin.

Args:
        plugin_name: Name of the plugin we are checking for.

Returns:
        A dictionary that maps from run_name to a list of plugin
        assets for that run.


def update_endpoint(self, endpoint_name, endpoint_config_name):

        if not _deployment_entity_exists(lambda: self.sagemaker_client.describe_endpoint(EndpointName=endpoint_name)):
            raise ValueError('Endpoint with name "{}" does not exist; please use an existing endpoint name'
                             .format(endpoint_name))

        self.sagemaker_client.update_endpoint(EndpointName=endpoint_name,
                                              EndpointConfigName=endpoint_config_name)
        return endpoint_name

Update an Amazon SageMaker ``Endpoint`` according to the endpoint configuration specified in the request

    Raise an error if endpoint with endpoint_name does not exist.

Args:
        endpoint_name (str): Name of the Amazon SageMaker ``Endpoint`` to update.
        endpoint_config_name (str): Name of the Amazon SageMaker endpoint configuration to deploy.

Returns:
        str: Name of the Amazon SageMaker ``Endpoint`` being updated.


def content_ratings(self, **kwargs):

        path = self._get_id_path('content_ratings')

        response = self._GET(path, kwargs)
        self._set_attrs_to_values(response)
        return response

Get the content ratings for a TV Series.

Args:
        language: (optional) ISO 639 code.
        append_to_response: (optional) Comma separated, any collection
                            method.

Returns:
        A dict respresentation of the JSON returned from the API.


def create(self, data, **kwargs):

        self._check_missing_create_attrs(data)
        server_data = self.gitlab.http_post(self.path, post_data=data,
                                            **kwargs)
        source_issue = ProjectIssue(self._parent.manager,
                                    server_data['source_issue'])
        target_issue = ProjectIssue(self._parent.manager,
                                    server_data['target_issue'])
        return source_issue, target_issue

Create a new object.

Args:
        data (dict): parameters to send to the server to create the
                    resource
        **kwargs: Extra options to send to the server (e.g. sudo)

Returns:
        RESTObject, RESTObject: The source and target issues

Raises:
        GitlabAuthenticationError: If authentication is not correct
        GitlabCreateError: If the server cannot perform the request


def refresh(self, only_closed=False):

        if only_closed:
            opened = filter(self.__check_port, self.__closed)
            self.__closed = self.__closed.difference(opened)
            self.__ports = self.__ports.union(opened)
        else:
            ports = self.__closed.union(self.__ports)
            self.__ports = set(filter(self.__check_port, ports))
            self.__closed = ports.difference(self.__ports)

refresh ports status

Args:
        only_closed - check status only for closed ports


def percentOutputsStableOverNTimeSteps(vectors, numSamples=None):

  # ----------------------------------------------------------------------
  # Calculate the stability
  totalSamples = len(vectors)
  windowSize = numSamples

  # Process each window
  numWindows = 0
  pctStable = 0

  for wStart in range(0, totalSamples-windowSize+1):
    # Count how many elements are active for the entire time
    data = vectors[wStart:wStart+windowSize]
    outputSums = data.sum(axis=0)
    stableOutputs = (outputSums == windowSize).sum()

    # Accumulated
    samplePctStable = float(stableOutputs) / data[0].sum()
    print samplePctStable
    pctStable += samplePctStable
    numWindows += 1

  # Return percent average over all possible windows
  return float(pctStable) / numWindows

Returns the percent of the outputs that remain completely stable over
    N time steps.

    Parameters:
    -----------------------------------------------
    vectors:        the vectors for which the stability is calculated
    numSamples:     the number of time steps where stability is counted

    For each window of numSamples, count how many outputs are active during
    the entire window.


def put_headers_in_environ(headers, environ):

  for key, value in headers:
    environ['HTTP_%s' % key.upper().replace('-', '_')] = value

Given a list of headers, put them into environ based on PEP-333.

    This converts headers to uppercase, prefixes them with 'HTTP_', and
    converts dashes to underscores before adding them to the environ dict.

Args:
        headers: A list of (header, value) tuples.  The HTTP headers to add to the
        environment.
        environ: An environ dict for the request as defined in PEP-333.


def _ConditionalFormatMessages(self, event_values):

    # Using getattr here to make sure the attribute is not set to None.
    # if A.b = None, hasattr(A, b) is True but getattr(A, b, None) is False.
    string_pieces = []
    for map_index, attribute_name in enumerate(self._format_string_pieces_map):
      if not attribute_name or attribute_name in event_values:
        if attribute_name:
          attribute = event_values.get(attribute_name, None)
          # If an attribute is an int, yet has zero value we want to include
          # that in the format string, since that is still potentially valid
          # information. Otherwise we would like to skip it.
          # pylint: disable=unidiomatic-typecheck
          if (not isinstance(attribute, (bool, float)) and
              not isinstance(attribute, py2to3.INTEGER_TYPES) and
              not attribute):
            continue

        string_pieces.append(self.FORMAT_STRING_PIECES[map_index])

    format_string = self.FORMAT_STRING_SEPARATOR.join(string_pieces)

    string_pieces = []
    for map_index, attribute_name in enumerate(
        self._format_string_short_pieces_map):
      if not attribute_name or event_values.get(attribute_name, None):
        string_pieces.append(self.FORMAT_STRING_SHORT_PIECES[map_index])
    short_format_string = self.FORMAT_STRING_SEPARATOR.join(string_pieces)

    return self._FormatMessages(
        format_string, short_format_string, event_values)

Determines the conditional formatted message strings.

Args:
        event_values (dict[str, object]): event values.

Returns:
        tuple(str, str): formatted message string and short message string.


def scalar_stats(data, functions=('min', 'max', 'mean', 'std')):

    stats = {}
    for func in functions:

        stats[func] = getattr(np, func)(data)

    return stats

Calculate the stats from the given numpy functions

    Parameters:
        data: array of data points to be used for the stats

    Options:
        functions: tuple of numpy stat functions to apply on data

Returns:
        Dictionary with the name of the function as key and the result
        as the respective value


def flux_minimization(model, fixed, solver, weights={}):

    fba = FluxBalanceProblem(model, solver)

    for reaction_id, value in iteritems(fixed):
        flux = fba.get_flux_var(reaction_id)
        fba.prob.add_linear_constraints(flux >= value)

    fba.minimize_l1()

    return ((reaction_id, fba.get_flux(reaction_id))
            for reaction_id in model.reactions)

Minimize flux of all reactions while keeping certain fluxes fixed.

    The fixed reactions are given in a dictionary as reaction id
    to value mapping. The weighted L1-norm of the fluxes is minimized.

Args:
        model: MetabolicModel to solve.
        fixed: dict of additional lower bounds on reaction fluxes.
        solver: LP solver instance to use.
        weights: dict of weights on the L1-norm terms.

Returns:
        An iterator of reaction ID and reaction flux pairs.


def __init__(self, min_value, max_value, scaling_type='Auto'):

        self.min_value = min_value
        self.max_value = max_value
        self.scaling_type = scaling_type

Initialize a parameter range.

Args:
        min_value (float or int): The minimum value for the range.
        max_value (float or int): The maximum value for the range.
        scaling_type (str): The scale used for searching the range during tuning (default: 'Auto').
            Valid values: 'Auto', 'Linear', 'Logarithmic' and 'ReverseLogarithmic'.


def get_content(self, url):

        cache_path = self._url_to_path(url)
        try:
            with open(cache_path, 'rb') as f:
                return f.read()
        except IOError:
            return None

Returns the content of a cached resource.

Args:
        url: The url of the resource

Returns:
        The content of the cached resource or None if not in the cache


def ParseHeader(table):

  precondition.AssertIterableType(table, dict)

  prototype = None  # type: List[Text]

  for row in table:
    columns = list(iterkeys(row))
    if prototype is None:
      prototype = columns
    elif prototype != columns:
      message = "Expected columns '{expected}', got '{actual}' for table {json}"
      message = message.format(expected=prototype, actual=columns, json=table)
      raise ValueError(message)

  result = rdf_osquery.OsqueryHeader()
  for name in prototype or []:
    result.columns.append(rdf_osquery.OsqueryColumn(name=name))
  return result

Parses header of osquery output.

Args:
        table: A table in a "parsed JSON" representation.

Returns:
        A parsed `rdf_osquery.OsqueryHeader` instance.


def build_vep_string(vep_info, vep_columns):

    logger = getLogger(__name__)
    logger.debug("Building vep string from {0}".format(vep_info))
    logger.debug("Found vep headers {0}".format(vep_columns))
    vep_strings = []
    for vep_annotation in vep_info:
        try:
            vep_info_list = [
                vep_annotation[vep_key] for vep_key in vep_columns
            ]
        except KeyError:
            raise SyntaxError("Vep entry does not correspond to vep headers")

        vep_strings.append('|'.join(vep_info_list))
    return ','.join(vep_strings)

Build a vep string formatted string.

    Take a list with vep annotations and build a new vep string

Args:
        vep_info (list): A list with vep annotation dictionaries
        vep_columns (list): A list with the vep column names found in the
        header of the vcf

Returns:
        string: A string with the proper vep annotations


def __init__(self, order_dict, default_order=None):

        self.order_dict = order_dict.copy()
        self.default_order = default_order

Create a reorderer.

Args:
        order_dict (dict of (str, `PackageOrder`): Orderers to apply to
            each package family.
        default_order (`PackageOrder`): Orderer to apply to any packages
            not specified in `order_dict`.


def register_entity(self, entity_value, entity_type, alias_of=None):

        if alias_of:
            self.trie.insert(entity_value.lower(), data=(alias_of, entity_type))
        else:
            self.trie.insert(entity_value.lower(), data=(entity_value, entity_type))
            self.trie.insert(entity_type.lower(), data=(entity_type, 'Concept'))

Register an entity to be tagged in potential parse results

Args:
        entity_value(str): the value/proper name of an entity instance (Ex: "The Big Bang Theory")
        entity_type(str): the type/tag of an entity instance (Ex: "Television Show")


def StartsWith(this, that):

  this_iter = iter(this)
  that_iter = iter(that)

  while True:
    try:
      this_value = next(that_iter)
    except StopIteration:
      return True

    try:
      that_value = next(this_iter)
    except StopIteration:
      return False

    if this_value != that_value:
      return False

Checks whether an items of one iterable are a prefix of another.

Args:
        this: An iterable that needs to be checked.
        that: An iterable of which items must match the prefix of `this`.

Returns:
        `True` if `that` is a prefix of `this`, `False` otherwise.


def coldestmonth(self, value=None):

        if value is not None:
            try:
                value = int(value)
            except ValueError:
                raise ValueError('value {} need to be of type int '
                                 'for field `coldestmonth`'.format(value))
            if value < 1:
                raise ValueError('value need to be greater or equal 1 '
                                 'for field `coldestmonth`')
            if value > 12:
                raise ValueError('value need to be smaller 12 '
                                 'for field `coldestmonth`')

        self._coldestmonth = value

Corresponds to IDD Field `coldestmonth`

Args:
        value (int): value for IDD Field `coldestmonth`
            value >= 1
            value <= 12
            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 plot_power_factor_mu(self, temp=600, output='eig',
                             relaxation_time=1e-14, xlim=None):

        import matplotlib.pyplot as plt
        plt.figure(figsize=(9, 7))
        pf = self._bz.get_power_factor(relaxation_time=relaxation_time,
                                       output=output, doping_levels=False)[
            temp]
        plt.semilogy(self._bz.mu_steps, pf, linewidth=3.0)
        self._plot_bg_limits()
        self._plot_doping(temp)
        if output == 'eig':
            plt.legend(['PF$_1$', 'PF$_2$', 'PF$_3$'])
        if xlim is None:
            plt.xlim(-0.5, self._bz.gap + 0.5)
        else:
            plt.xlim(xlim)
        plt.ylabel("Power factor, ($\\mu$W/(mK$^2$))", fontsize=30.0)
        plt.xlabel("E-E$_f$ (eV)", fontsize=30.0)
        plt.xticks(fontsize=25)
        plt.yticks(fontsize=25)
        plt.tight_layout()
        return plt

Plot the power factor in function of Fermi level. Semi-log plot

Args:
        temp: the temperature
        xlim: a list of min and max fermi energy by default (0, and band
            gap)
        tau: A relaxation time in s. By default none and the plot is by
        units of relaxation time

Returns:
        a matplotlib object


def __init__(self, tcex, main_type, api_type, sub_type, api_entity, owner):

        self._tcex = tcex
        self._data = {}

        self._owner = owner
        self._type = main_type
        self._api_sub_type = sub_type
        self._api_type = api_type
        self._unique_id = None
        self._api_entity = api_entity

        self._utils = TcExUtils()
        self._tc_requests = TiTcRequest(self._tcex)

Initialize Class Properties.

Args:
        tcex:
        main_type:
        api_type:
        sub_type:
        api_entity:


def mtztw(n,c,e,l):

    model = Model("tsptw - mtz")

    x,u = {},{}
    for i in range(1,n+1):
        u[i] = model.addVar(lb=e[i], ub=l[i], vtype="C", name="u(%s)"%i)
        for j in range(1,n+1):
            if i != j:
                x[i,j] = model.addVar(vtype="B", name="x(%s,%s)"%(i,j))

    for i in range(1,n+1):
        model.addCons(quicksum(x[i,j] for j in range(1,n+1) if j != i) == 1, "Out(%s)"%i)
        model.addCons(quicksum(x[j,i] for j in range(1,n+1) if j != i) == 1, "In(%s)"%i)

    for i in range(1,n+1):
        for j in range(2,n+1):
            if i != j:
                M = max(l[i] + c[i,j] - e[j], 0)
                model.addCons(u[i] - u[j] + M*x[i,j] <= M-c[i,j], "MTZ(%s,%s)"%(i,j))

    model.setObjective(quicksum(c[i,j]*x[i,j] for (i,j) in x), "minimize")

    model.data = x,u
    return model

mtzts: model for the traveling salesman problem with time windows
    (based on Miller-Tucker-Zemlin's one-index potential formulation)
    Parameters:
        - n: number of nodes
        - c[i,j]: cost for traversing arc (i,j)
        - e[i]: earliest date for visiting node i
        - l[i]: latest date for visiting node i
    Returns a model, ready to be solved.


def maybe_get_common_dtype(arg_list):

  # Note that `all` defaults to `True` if `arg_list` is empty.
  if all(a is None for a in arg_list):
    return None
  return dtype_util.common_dtype(arg_list, tf.float32)

Return common dtype of arg_list, or None.

Args:
        arg_list: an iterable of items which are either `None` or have a `dtype`
        property.

Returns:
        dtype: The common dtype of items in `arg_list`, or `None` if the list is
        empty or all items are `None`.


def resize(self, targ_sz, new_path='tmp', resume=True, fn=None):

        new_ds = []
        dls = [self.trn_dl,self.val_dl,self.fix_dl,self.aug_dl]
        if self.test_dl: dls += [self.test_dl, self.test_aug_dl]
        else: dls += [None,None]
        t = tqdm_notebook(dls)
        for dl in t: new_ds.append(self.resized(dl, targ_sz, new_path, resume, fn))
        t.close()
        return self.__class__(new_ds[0].path, new_ds, self.bs, self.num_workers, self.classes)

Resizes all the images in the train, valid, test folders to a given size.

Args:
    targ_sz (int): the target size
    new_path (str): the path to save the resized images (default tmp)
    resume (bool): if True, check for images in the DataSet that haven't been resized yet (useful if a previous resize
    operation was aborted)
    fn (function): optional custom resizing function


def mark_locations(h,section,locs,markspec='or',**kwargs):

    # get list of cartesian coordinates specifying section path
    xyz = get_section_path(h,section)
    (r,theta,phi) = sequential_spherical(xyz)
    rcum = np.append(0,np.cumsum(r))

    # convert locs into lengths from the beginning of the path
    if type(locs) is float or type(locs) is np.float64:
        locs = np.array([locs])
    if type(locs) is list:
        locs = np.array(locs)
    lengths = locs*rcum[-1]

    # find cartesian coordinates for markers
    xyz_marks = []
    for targ_length in lengths:
        xyz_marks.append(find_coord(targ_length,xyz,rcum,theta,phi))
    xyz_marks = np.array(xyz_marks)

    # plot markers
    line, = plt.plot(xyz_marks[:,0], xyz_marks[:,1], \
                     xyz_marks[:,2], markspec, **kwargs)
    return line

Marks one or more locations on along a section. Could be used to
    mark the location of a recording or electrical stimulation.

Args:
        h = hocObject to interface with neuron
        section = reference to section
        locs = float between 0 and 1, or array of floats
        optional arguments specify details of marker

Returns:
        line = reference to plotted markers


def fragment_search(self, fragement:str) -> List[dict]:

        fragement = self.extract_fragment(fragement)
        ilx_rows = self.fragment2rows.get(fragement)
        if not ilx_rows:
            return None
        else:
            return ilx_rows

Returns the rows in InterLex associated with the fragment

Note:
        Pressumed to have duplicate fragements in InterLex

Args:
        fragment: The fragment_id of the curie pertaining to the ontology

Returns:
        None or List[dict]


def _sign_simple_signature_fulfillment(cls, input_, message, key_pairs):

        # NOTE: To eliminate the dangers of accidentally signing a condition by
        #       reference, we remove the reference of input_ here
        #       intentionally. If the user of this class knows how to use it,
        #       this should never happen, but then again, never say never.
        input_ = deepcopy(input_)
        public_key = input_.owners_before[0]
        message = sha3_256(message.encode())
        if input_.fulfills:
            message.update('{}{}'.format(
                input_.fulfills.txid, input_.fulfills.output).encode())

        try:
            # cryptoconditions makes no assumptions of the encoding of the
            # message to sign or verify. It only accepts bytestrings
            input_.fulfillment.sign(
                message.digest(), base58.b58decode(key_pairs[public_key].encode()))
        except KeyError:
            raise KeypairMismatchException('Public key {} is not a pair to '
                                           'any of the private keys'
                                           .format(public_key))
        return input_

Signs a Ed25519Fulfillment.

Args:
        input_ (:class:`~bigchaindb.common.transaction.
            Input`) The input to be signed.
        message (str): The message to be signed
        key_pairs (dict): The keys to sign the Transaction with.


def __init__(
        self,
        name,
        named_tensors=None,
        scope='output',
        summary_labels=()
    ):

        self.name = name
        super(Output, self).__init__(named_tensors=named_tensors, scope=scope, summary_labels=summary_labels)

Output layer.

Args:
        output: A string that names the tensor, will be added to available inputs


def __init__(self, path, exts, fields, **kwargs):

        if not isinstance(fields[0], (tuple, list)):
            fields = [('src', fields[0]), ('trg', fields[1])]

        src_path, trg_path = tuple(os.path.expanduser(path + x) for x in exts)

        examples = []
        with io.open(src_path, mode='r', encoding='utf-8') as src_file, \
                io.open(trg_path, mode='r', encoding='utf-8') as trg_file:
            for src_line, trg_line in zip(src_file, trg_file):
                src_line, trg_line = src_line.strip(), trg_line.strip()
                if src_line != '' and trg_line != '':
                    examples.append(data.Example.fromlist(
                        [src_line, trg_line], fields))

        super(TranslationDataset, self).__init__(examples, fields, **kwargs)

Create a TranslationDataset given paths and fields.

Args:
        path: Common prefix of paths to the data files for both languages.
        exts: A tuple containing the extension to path for each language.
        fields: A tuple containing the fields that will be used for data
            in each language.
        Remaining keyword arguments: Passed to the constructor of
            data.Dataset.


def read(cls, data):

        if isinstance(data, pd.DataFrame):
            return cls((json.loads(
                to_json_stat(data, output='dict', version='2.0'),
                object_pairs_hook=OrderedDict)))
        elif isinstance(data, OrderedDict):
            return cls(data)
        elif (isinstance(data, basestring)
              and data.startswith(("http://", "https://",
                                   "ftp://", "ftps://"))):
            # requests will do the rest...
            return cls(request(data))
        elif isinstance(data, basestring):
            try:
                json_dict = json.loads(data, object_pairs_hook=OrderedDict)
                return cls(json_dict)
            except ValueError:
                raise
        else:
            try:
                json_dict = json.load(data, object_pairs_hook=OrderedDict)
                return cls(json_dict)
            except ValueError:
                raise

Reads data from URL, Dataframe, JSON string, JSON file or
    OrderedDict.

Args:
    data: can be a Pandas Dataframe, a JSON file, a JSON string,
        an OrderedDict or a URL pointing to a JSONstat file.

Returns:
    An object of class Dataset populated with data.


def flatten(l):

    for el in l:
        # I don;t want dict to be flattened
        if isinstance(el, Iterable) and not isinstance(
                el, (str, bytes)) and not isinstance(el, dict):
            yield from flatten(el)
        else:
            yield el

Flatten a multi-deminision list and return a iterable

    Note that dict and str will not be expanded, instead, they will be kept as a single element.

Args:
        l (list): The list needs to be flattened

Returns:
        A iterable of flattened list. To have a list instead use ``list(flatten(l))``


def frame(self, locator=None, *args, **kwargs):

        self.switch_to_frame(self._find_frame(locator, *args, **kwargs))
        try:
            yield
        finally:
            self.switch_to_frame("parent")

Execute the wrapped code within the given iframe using the given frame or frame name/id.
    May not be supported by all drivers.

Args:
        locator (str | Element, optional): The name/id of the frame or the frame's element.
            Defaults to the only frame in the document.


def _get_events_list(object_key: str) -> List[str]:

    return DB.get_list(_keys.events_list(object_key))

Get list of event ids for the object with the specified key.

Args:
        object_key (str): Key of an object in the database.


def match(self, message: Message) -> bool:

        if self.template:
            return self.template.match(message)
        return True

Matches a message with the behaviour's template

Args:
        message(spade.message.Message): the message to match with

Returns:
        bool: wheter the messaged matches or not


def add_metric(self, labels, value, timestamp=None):

        self.samples.append(Sample(
            self.name + '_info',
            dict(dict(zip(self._labelnames, labels)), **value),
            1,
            timestamp,
        ))

Add a metric to the metric family.

Args:
        labels: A list of label values
        value: A dict of labels


def log_estimator_evaluation_result(self, eval_results):

    if not isinstance(eval_results, dict):
      tf.logging.warning("eval_results should be directory for logging. Got %s",
                         type(eval_results))
      return
    global_step = eval_results[tf.GraphKeys.GLOBAL_STEP]
    for key in sorted(eval_results):
      if key != tf.GraphKeys.GLOBAL_STEP:
        self.log_metric(key, eval_results[key], global_step=global_step)

Log the evaluation result for a estimator.

    The evaluate result is a directory that contains metrics defined in
    model_fn. It also contains a entry for global_step which contains the value
    of the global step when evaluation was performed.

Args:
        eval_results: dict, the result of evaluate() from a estimator.


def download_patric_genomes(self, ids, force_rerun=False):

        ids = ssbio.utils.force_list(ids)

        counter = 0
        log.info('Downloading sequences from PATRIC...')
        for patric_id in tqdm(ids):
            f = ssbio.databases.patric.download_coding_sequences(patric_id=patric_id, seqtype='protein',
                                                                 outdir=self.sequences_by_organism_dir,
                                                                 force_rerun=force_rerun)
            if f:
                self.load_strain(patric_id, f)
                counter += 1
                log.debug('{}: downloaded sequence'.format(patric_id))
            else:
                log.warning('{}: unable to download sequence'.format(patric_id))

        log.info('Created {} new strain GEM-PROs, accessible at "strains" attribute'.format(counter))

Download genome files from PATRIC given a list of PATRIC genome IDs and load them as strains.

Args:
        ids (str, list): PATRIC ID or list of PATRIC IDs
        force_rerun (bool): If genome files should be downloaded again even if they exist


def _step(self, actions):

    # Pre-conditions: common_preconditions, see `assert_common_preconditions`.
    #               : len(actions) == len(self._envs)
    self.assert_common_preconditions()
    assert len(actions) == len(self._envs)

    observations = []
    rewards = []
    dones = []
    infos = []

    # Take steps in all environments.
    for env, action in zip(self._envs, actions):
      observation, reward, done, info = env.step(action)

      observations.append(observation)
      rewards.append(reward)
      dones.append(done)
      infos.append(info)

    # Convert each list (observations, rewards, ...) into np.array and return a
    # tuple.
    return tuple(map(np.stack, [observations, rewards, dones, infos]))

Takes a step in all environments, shouldn't pre-process or record.

    Subclasses should override this to do the actual step if something other
    than the default implementation is desired.

Args:
        actions: (np.ndarray) with first dimension equal to the batch size.

Returns:
        a tuple of stacked raw observations, raw rewards, dones and infos.


def get_environment_details(zone, environment):

    default_context = google.datalab.Context.default()
    url = (Api._ENDPOINT + (Api._ENVIRONMENTS_PATH_FORMAT % (default_context.project_id, zone,
                                                             environment)))

    return google.datalab.utils.Http.request(url, credentials=default_context.credentials)

Issues a request to Composer to get the environment details.

Args:
        zone: GCP zone of the composer environment
        environment: name of the Composer environment

Returns:
        A parsed result object.

Raises:
        Exception if there is an error performing the operation.


def songs_delete(self, songs):

		mutations = [mc_calls.TrackBatch.delete(song['id']) for song in songs]
		response = self._call(
			mc_calls.TrackBatch,
			mutations
		)

		success_ids = [
			res['id']
			for res in response.body['mutate_response']
			if res['response_code'] == 'OK'
		]

		# TODO: Report failures.
		# failure_ids = [
		# 	res['id']
		# 	for res in response.body['mutate_response']
		# 	if res['response_code'] != 'OK'
		# ]

		return success_ids

Delete songs from library.

    Parameters:
    song (list): A list of song dicts.

Returns:
    list: Successfully deleted song IDs.


def __init__(self, env, past_indices, flatten):

    if 0 not in past_indices:
      raise KeyError('Past indices should include 0 for the current frame.')
    self._env = env
    self._past_indices = past_indices
    self._step = 0
    self._buffer = None
    self._capacity = max(past_indices) + 1
    self._flatten = flatten

Augment the observation with past observations.

    Implemented as a Numpy ring buffer holding the necessary past observations.

Args:
        env: OpenAI Gym environment to wrap.
        past_indices: List of non-negative integers indicating the time offsets
        from the current time step of observations to include.
        flatten: Concatenate the past observations rather than stacking them.

Raises:
        KeyError: The current observation is not included in the indices.


def are_all_matches_terminal(self,
            predicate: Callable[[ops.Operation], bool]):

        return all(
            self.next_moment_operating_on(op.qubits, i + 1) is None for
            (i, op) in self.findall_operations(predicate)
        )

Check whether all of the ops that satisfy a predicate are terminal.

Args:
        predicate: A predicate on ops.Operations which is being checked.

Returns:
        Whether or not all `Operation` s in a circuit that satisfy the
        given predicate are terminal.


def on_moved(self, event):

        if not self._event_error:
            # We are only interested for final file, not transitional file
            # from editors (like *.part)
            pathtools_options = {
                'included_patterns': self.patterns,
                'excluded_patterns': self.ignore_patterns,
                'case_sensitive': self.case_sensitive,
            }
            # Apply pathtool matching on destination since Watchdog only
            # automatically apply it on source
            if match_path(event.dest_path, **pathtools_options):
                self.logger.info(u"Change detected from a move on: %s",
                                 event.dest_path)
                self.compile_dependencies(event.dest_path)

Called when a file or a directory is moved or renamed.

    Many editors don't directly change a file, instead they make a
    transitional file like ``*.part`` then move it to the final filename.

Args:
        event: Watchdog event, either ``watchdog.events.DirMovedEvent`` or
            ``watchdog.events.FileModifiedEvent``.


def set_approvers(self, approver_ids=[], approver_group_ids=[], **kwargs):

        path = '%s/%s/approvers' % (self._parent.manager.path,
                                    self._parent.get_id())
        data = {'approver_ids': approver_ids,
                'approver_group_ids': approver_group_ids}
        self.gitlab.http_put(path, post_data=data, **kwargs)

Change MR-level allowed approvers and approver groups.

Args:
        approver_ids (list): User IDs that can approve MRs
        approver_group_ids (list): Group IDs whose members can approve MRs

Raises:
        GitlabAuthenticationError: If authentication is not correct
        GitlabUpdateError: If the server failed to perform the request


def get_review(review_struct):

    review_fn = _resource_context("review.rst")

    # read review template
    with open(review_fn) as f:
        review = f.read()

    # generate qr code
    with NamedTemporaryFile(suffix=".png") as qr_file:
        url = pyqrcode.create(review_struct.internal_url)
        url.png(qr_file.name, scale=5)

        # save the file
        qr_file.flush()
        qr_file.seek(0)

        # generate template
        review = Template(review).substitute(
            content=review_struct.get_rst(),
            datum=time.strftime("%d.%m.%Y", time.localtime()),
            cas=time.strftime("%H:%M", time.localtime()),
            resources_path=RES_PATH,
            qr_path=qr_file.name,
        )

        return gen_pdf(
            review,
            open(_resource_context("review_style.json")).read(),
        )

Generate review from `review_struct`.

Args:
        review_struct (obj): :class:`.GenerateReview` instance.

Returns:
        obj: StringIO file instance containing PDF file.


def Analyze(self, hashes):

    logger.debug(
        'Opening connection to {0:s}:{1:d}'.format(self._host, self._port))

    nsrl_socket = self._GetSocket()
    if not nsrl_socket:
      self.SignalAbort()
      return []

    hash_analyses = []
    for digest in hashes:
      response = self._QueryHash(nsrl_socket, digest)
      if response is None:
        continue

      hash_analysis = interface.HashAnalysis(digest, response)
      hash_analyses.append(hash_analysis)

    nsrl_socket.close()

    logger.debug(
        'Closed connection to {0:s}:{1:d}'.format(self._host, self._port))

    return hash_analyses

Looks up hashes in nsrlsvr.

Args:
        hashes (list[str]): hash values to look up.

Returns:
        list[HashAnalysis]: analysis results, or an empty list on error.


def _set_notification(self, conn, char, enabled, timeout=1.0):

        if 'client_configuration' not in char:
            return False, {'reason': 'Cannot enable notification without a client configuration attribute for characteristic'}

        props = char['properties']
        if not props.notify:
            return False, {'reason': 'Cannot enable notification on a characteristic that does not support it'}

        value = char['client_configuration']['value']

        #Check if we don't have to do anything
        current_state = bool(value & (1 << 0))
        if current_state == enabled:
            return

        if enabled:
            value |= 1 << 0
        else:
            value &= ~(1 << 0)

        char['client_configuration']['value'] = value

        valarray = struct.pack("<H", value)
        return self._write_handle(conn, char['client_configuration']['handle'], True, valarray, timeout)

Enable/disable notifications on a GATT characteristic

Args:
        conn (int): The connection handle for the device we should interact with
        char (dict): The characteristic we should modify
        enabled (bool): Should we enable or disable notifications
        timeout (float): How long to wait before failing


def calculate_between_class_scatter_matrix(X, y):

    mean_vectors = calculate_mean_vectors(X, y)
    n_features = X.shape[1]
    Sb = np.zeros((n_features, n_features))
    m = np.mean(X, axis=0).reshape(n_features, 1)

    for cl, m_i in zip(np.unique(y), mean_vectors):
        v = m_i.reshape(n_features, 1) - m
        Sb += X[y == cl, :].shape[0] * v @ v.T

    return Sb

Calculates the Between-Class Scatter matrix

    Parameters:
    -----------
    X : array-like, shape (m, n) - the samples
    y : array-like, shape (m, ) - the class labels

Returns:
    --------
    between_class_scatter_matrix : array-like, shape (n, n)


def load_pickled_model(filename, dirname=None):

    if dirname is None:
        pkg_filename = pkgutil.get_loader('dragnet').get_filename('dragnet')
        pkg_dirname = os.path.dirname(pkg_filename)
        dirname = os.path.join(pkg_dirname, 'pickled_models', model_path)
    filepath = os.path.join(dirname, filename)
    return joblib.load(filepath)

Load a pickled ``Extractor`` model from disk.

Args:
        filename (str): Name of pickled model file under ``dirname``.
        dirname (str): Name of directory on disk containing the pickled model.
            If None, dragnet's default pickled model directory is used:
            /path/to/dragnet/pickled_models/[PY_VERSION]_[SKLEARN_VERSION]

Returns:
        :class:`dragnet.extractor.Extractor`


def __init__(self, mat, **kwargs):

        # try:
        #     self.colormaps = [ColorMap(m, cmap=cmap, pixelspervalue=pixelspervalue,
        #                       minvalue=minvalue, maxvalue=maxvalue) for m in mat]
        # except:
        #     pass
        #     # raise ValueError("Don't know how to display ColorMaps for a sequence of type {}".format(type(mat)))

        try:
            mat = np.array(mat.values)
        except AttributeError:
            try:
                mat = np.array(mat)
            except ValueError:
                pass

        if not isinstance(mat, np.ndarray):
            raise ValueError("Don't know how to display a ColorMap for a matrix of type {}".format(type(mat)))

        kwargs['vmin'] = kwargs.get('vmin', np.amin(mat))
        kwargs['vmax'] = kwargs.get('vmax', np.amax(mat))
        kwargs['cmap'] = kwargs.get('cmap', 'bone')  # 'hot', 'greens', 'blues'
        kwargs['linewidths'] = kwargs.get('linewidths', 0.25)
        kwargs['square'] = kwargs.get('square', True)
        sb.heatmap(mat, **kwargs)

Render a color map (image) of a matrix or sequence of Matrix objects

    A color map is like a contour map except the "height" or "value" of each matrix element
    is used to select a color from a continuous spectrum of colors (for heatmap white is max and red is medium)

Args:
        mat (np.matrix or np.array or list of list): the matrix to be rendered as a color map


def Write(self, schedule, output_file):

    # Generate the DOM to write
    root = ET.Element('kml')
    root.attrib['xmlns'] = 'http://earth.google.com/kml/2.1'
    doc = ET.SubElement(root, 'Document')
    open_tag = ET.SubElement(doc, 'open')
    open_tag.text = '1'
    self._CreateStopsFolder(schedule, doc)
    if self.split_routes:
      route_types = set()
      for route in schedule.GetRouteList():
        route_types.add(route.route_type)
      route_types = list(route_types)
      route_types.sort()
      for route_type in route_types:
        self._CreateRoutesFolder(schedule, doc, route_type)
    else:
      self._CreateRoutesFolder(schedule, doc)
    self._CreateShapesFolder(schedule, doc)

    # Make sure we pretty-print
    self._SetIndentation(root)

    # Now write the output
    if isinstance(output_file, file):
      output = output_file
    else:
      output = open(output_file, 'w')
    output.write()
    ET.ElementTree(root).write(output, 'utf-8')

Writes out a feed as KML.

Args:
        schedule: A transitfeed.Schedule object containing the feed to write.
        output_file: The name of the output KML file, or file object to use.


def predict_proba(self, a, b, idx=0, **kwargs):

        return self.predict_dataset(DataFrame([[a, b]],
                                              columns=['A', 'B']))

Use Jarfo to predict the causal direction of a pair of vars.

Args:
        a (numpy.ndarray): Variable 1
        b (numpy.ndarray): Variable 2
        idx (int): (optional) index number for printing purposes

Returns:
        float: Causation score (Value : 1 if a->b and -1 if b->a)


def does_attribute_meet_condition(self, attribute, conditions):

        if conditions is None or len(conditions) == 0:
            return True

        for attribute_name, attribute_value in conditions.items():
            value = getattr(attribute, attribute_name, False)
            if value != attribute_value and bool(value) != attribute_value:
                return False

        return True

Check if the attribute meet all the given conditions

Args:
        attribute: the attribute information
        conditions: a dictionary of condition to match

Returns:
        True if the attribute match all conditions. False otherwise


def execute(self, action):

        assert action in self.possible_actions

        self.remaining_cycles -= 1
        index = int(bitstrings.BitString(
            self.current_situation[:self.address_size]
        ))
        bit = self.current_situation[self.address_size + index]
        return action == bit

Execute the indicated action within the environment and
    return the resulting immediate reward dictated by the reward
    program.

    Usage:
        immediate_reward = scenario.execute(selected_action)

Args:
        action: The action to be executed within the current situation.

Returns:
        A float, the reward received for the action that was executed,
        or None if no reward is offered.


def do_ams_auth(endpoint, body):

    headers = {"content-type": "application/x-www-form-urlencoded",
               "Accept": json_acceptformat}
    return requests.post(endpoint, data=body, headers=headers)

Acquire Media Services Authentication Token.

Args:
        endpoint (str): Azure Media Services Initial Endpoint.
        body (str): A Content Body.

Returns:
        HTTP response. JSON body.


def intersects(self, rect, edges=False):

        # Not even touching
        if (self.bottom > rect.top or \
            self.top < rect.bottom or \
            self.left > rect.right or \
            self.right < rect.left):
            return False

        # Discard edge intersects
        if not edges:
            if (self.bottom == rect.top or \
                self.top == rect.bottom or \
                self.left == rect.right or \
                self.right == rect.left):
                return False

        # Discard corner intersects
        if (self.left == rect.right and self.bottom == rect.top or \
            self.left == rect.right and rect.bottom == self.top or \
            rect.left == self.right and self.bottom == rect.top or \
            rect.left == self.right and rect.bottom == self.top):
            return False

        return True

Detect intersections between this rectangle and rect.

Args:
        rect (Rectangle): Rectangle to test for intersections.
        edges (bool): Accept edge touching rectangles as intersects or not

Returns:
        bool: True if the rectangles intersect, False otherwise


def Stream(self, reader, amount=None):

    if amount is None:
      amount = float("inf")

    data = reader.Read(min(self.chunk_size, amount))
    if not data:
      return

    amount -= len(data)
    offset = reader.offset - len(data)
    yield Chunk(offset=offset, data=data)

    while amount > 0:
      # We need `len(data)` here because overlap size can be 0.
      overlap = data[len(data) - self.overlap_size:]

      new = reader.Read(min(self.chunk_size - self.overlap_size, amount))
      if not new:
        return

      data = overlap + new

      amount -= len(new)
      offset = reader.offset - len(data)
      yield Chunk(offset=offset, data=data, overlap=len(overlap))

Streams chunks of a given file starting at given offset.

Args:
        reader: A `Reader` instance.
        amount: An upper bound on number of bytes to read.

Yields:
        `Chunk` instances.


def set_cookie(name, value):

    r = app.make_response(redirect(url_for("view_cookies")))
    r.set_cookie(key=name, value=value, secure=secure_cookie())

    return r

Sets a cookie and redirects to cookie list.
    ---
    tags:
        - Cookies
    parameters:
        - in: path
        name: name
        type: string
        - in: path
        name: value
        type: string
    produces:
        - text/plain
    responses:
        200:
        description: Set cookies and redirects to cookie list.


def bit_to_int(x_bit, num_bits, base=2):

  x_l = tf.stop_gradient(tf.to_int32(tf.reshape(x_bit, [-1, num_bits])))
  x_labels = [
      x_l[:, i] * tf.to_int32(base)**tf.to_int32(i) for i in range(num_bits)]
  res = sum(x_labels)
  return tf.to_int32(tf.reshape(res, common_layers.shape_list(x_bit)[:-1]))

Turn x_bit representing numbers bitwise (lower-endian) to int tensor.

Args:
        x_bit: Tensor containing numbers in a particular base to be converted to
        int.
        num_bits: Number of bits in the representation.
        base: Base of the representation.

Returns:
        Integer representation of this number.


def __init__(self, email=None, *args, **kwargs):

        if 'scopes' in kwargs:
            warnings.warn(_SCOPES_WARNING)
            kwargs['scopes'] = None

        # Assertion type is no longer used, but still in the
        # parent class signature.
        super(AppAssertionCredentials, self).__init__(None, *args, **kwargs)

        self.service_account_email = email
        self.scopes = None
        self.invalid = True

Constructor for AppAssertionCredentials

Args:
        email: an email that specifies the service account to use.
            Only necessary if using custom service accounts
            (see https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#createdefaultserviceaccount).


def writeline(self, line, line_number):

        tmp_file = tempfile.TemporaryFile('w+')
        if not line.endswith(os.linesep):

            line += os.linesep
        try:

            with open(self.path, 'r') as file_handle:

                for count, new_line in enumerate(file_handle):

                    if count == line_number:

                        new_line = line

                    tmp_file.write(new_line)

            tmp_file.seek(0)
            with open(self.path, 'w') as file_handle:

                for new_line in tmp_file:

                    file_handle.write(new_line)
        finally:

            tmp_file.close()

Rewrite a single line in the file.

Args:
        line (str): The new text to write to the file.
        line_number (int): The line of the file to rewrite. Numbering
            starts at 0.


def _DeserializeResponse(self, payload):

        # Strip off the status line.
        status_line, payload = payload.split('\n', 1)
        _, status, _ = status_line.split(' ', 2)

        # Parse the rest of the response.
        parser = email_parser.Parser()
        msg = parser.parsestr(payload)

        # Get the headers.
        info = dict(msg)
        info['status'] = status

        # Create Response from the parsed headers.
        content = msg.get_payload()

        return http_wrapper.Response(info, content, self.__batch_url)

Convert string into Response and content.

Args:
        payload: Header and body string to be deserialized.

Returns:
        A Response object


def __add__(self, other: 'TensorFluent') -> 'TensorFluent':

        return self._binary_op(self, other, tf.add, tf.float32)

Returns a TensorFluent for the addition arithmetic operator.

Args:
        self: The first operand.
        other: The second operand.

Returns:
        A TensorFluent wrapping the operator's output.


def transition(self, state, message=""):

        with self.changes_squashed:
            initial_state = self.state.value
            if self.state_set.transition_allowed(
                    initial_state=initial_state, target_state=state):
                self.log.debug(
                    "%s: Transitioning from %s to %s",
                    self.mri, initial_state, state)
                if state == ss.DISABLED:
                    alarm = Alarm.invalid("Disabled")
                elif state == ss.FAULT:
                    alarm = Alarm.major(message)
                else:
                    alarm = Alarm()
                self.update_health(self, HealthInfo(alarm))
                self.state.set_value(state)
                self.state.set_alarm(alarm)
                for child, writeable in self._children_writeable[state].items():
                    if isinstance(child, AttributeModel):
                        child.meta.set_writeable(writeable)
                    elif isinstance(child, MethodModel):
                        child.set_writeable(writeable)
            else:
                raise TypeError("Cannot transition from %s to %s" %
                                (initial_state, state))

Change to a new state if the transition is allowed

Args:
        state (str): State to transition to
        message (str): Message if the transition is to a fault state


def read_into(self, buffer, size=-1, *, offset=0, write_offset=0) -> None:

        return self.mglo.read_into(buffer, size, offset, write_offset)

Read the content into a buffer.

Args:
        buffer (bytarray): The buffer that will receive the content.
        size (int): The size. Value ``-1`` means all.

    Keyword Args:
        offset (int): The read offset.
        write_offset (int): The write offset.


def df_to_array(df, netshape, nettype):

    if len(df) > 0:
        idx = np.array(list(map(list, df.values)))
        G = np.zeros([netshape[0], netshape[0], netshape[1]])
        if idx.shape[1] == 3:
            if nettype[-1] == 'u':
                idx = np.vstack([idx, idx[:, [1, 0, 2]]])
            idx = idx.astype(int)
            G[idx[:, 0], idx[:, 1], idx[:, 2]] = 1
        elif idx.shape[1] == 4:
            if nettype[-1] == 'u':
                idx = np.vstack([idx, idx[:, [1, 0, 2, 3]]])
            weights = idx[:, 3]
            idx = np.array(idx[:, :3], dtype=int)
            G[idx[:, 0], idx[:, 1], idx[:, 2]] = weights
    else:
        G = np.zeros([netshape[0], netshape[0], netshape[1]])
    return G

Returns a numpy array (snapshot representation) from thedataframe contact list

    Parameters:
        df : pandas df
            pandas df with columns, i,j,t.
        netshape : tuple
            network shape, format: (node, time)
        nettype : str
            'wu', 'wd', 'bu', 'bd'

Returns:
    --------
        G : array
            (node,node,time) array for the network


def tsp(V,c):

    model = Model("TSP_lazy")
    conshdlr = TSPconshdlr()

    x = {}
    for i in V:
        for j in V:
            if j > i:
                x[i,j] = model.addVar(vtype = "B",name = "x(%s,%s)" % (i,j))

    for i in V:
        model.addCons(quicksum(x[j, i] for j in V if j < i) +
                      quicksum(x[i, j] for j in V if j > i) == 2, "Degree(%s)" % i)

    model.setObjective(quicksum(c[i, j] * x[i, j] for i in V for j in V if j > i), "minimize")

    model.data = x
    return model, conshdlr

tsp -- model for solving the traveling salesman problem with callbacks
    - start with assignment model
    - add cuts until there are no sub-cycles
    Parameters:
    - V: set/list of nodes in the graph
    - c[i,j]: cost for traversing edge (i,j)
    Returns the optimum objective value and the list of edges used.


def get_victim_phone_asset(self, main_type, sub_type, unique_id, asset_id, params=None):

        params = params or {}

        return self.victim_phone_asset(main_type, sub_type, unique_id, asset_id, params=params)

Args:
    main_type:
    sub_type:
    unique_id:
    asset_id:
    params:

    Return:


def get_access_token(self, http=None, additional_claims=None):

        if additional_claims is None:
            if self.access_token is None or self.access_token_expired:
                self.refresh(None)
            return client.AccessTokenInfo(
              access_token=self.access_token, expires_in=self._expires_in())
        else:
            # Create a 1 time token
            token, unused_expiry = self._create_token(additional_claims)
            return client.AccessTokenInfo(
              access_token=token, expires_in=self._MAX_TOKEN_LIFETIME_SECS)

Create a signed jwt.

Args:
        http: unused
        additional_claims: dict, additional claims to add to
            the payload of the JWT.

Returns:
        An AccessTokenInfo with the signed jwt


def __init__(self, pidfile, logger, port = 64042, host = 'localhost'):

        super(RemoteControllerDeamon, self).__init__(pidfile, logger)
        self.__port = port
        self.__host = host
        for name in dir(self):
            method = getattr(self, name)
            if hasattr(method, 'registered_for_rpc'):
                self.register_method(method, method.registered_for_rpc.__name__)

Create a daemon which is controllable via jsonrpc with decorator

Args:
        pidfile (str): path to create pid file
        logger (logging.Logger): logger for the daemon
        port (int):
        host (str):


def isHostCert(self, name):

        crtpath = self._getPathJoin('hosts', '%s.crt' % name)
        return os.path.isfile(crtpath)

Checks if a host certificate exists.

Args:
        name (str): The name of the host keypair.

Example:
        Check if the host cert "myhost" exists:

            exists = cdir.isUserCert('myhost')

Returns:
        bool: True if the certificate is present, False otherwise.


def split(x, split_dim, num_or_size_splits, name=None):

  return SplitOperation(x, split_dim, num_or_size_splits, name=name).outputs

Like tf.split.

Args:
        x: a Tensor
        split_dim: a Dimension in x.shape.dims
        num_or_size_splits: either an integer dividing split_dim.size
        or a list of integers adding up to split_dim.size
        name: an optional string

Returns:
        a list of Tensors.


def __init__(self, feature_set='spe+'):

        filename = filenames[feature_set]
        self.segments, self.seg_dict, self.names = self._read_table(filename)
        self.seg_seq = {seg[0]: i for (i, seg) in enumerate(self.segments)}
        self.weights = self._read_weights()
        self.seg_regex = self._build_seg_regex()
        self.longest_seg = max([len(x) for x in self.seg_dict.keys()])
        self.xsampa = xsampa.XSampa()

Construct a FeatureTable object

Args:
        feature_set (str): the feature set that the FeatureTable will use;
                        currently, there is only one of these ("spe+")


def getCompletingSwarms(self):

    swarmIds = []
    for swarmId, info in self._state['swarms'].iteritems():
      if info['status'] == 'completing':
        swarmIds.append(swarmId)

    return swarmIds


def edges(self, tail_head_iter):

        edge = self._edge_plain
        quote = self._quote_edge
        lines = (edge % (quote(t), quote(h)) for t, h in tail_head_iter)
        self.body.extend(lines)

Create a bunch of edges.

Args:
        tail_head_iter: Iterable of ``(tail_name, head_name)`` pairs.


def combs(a, r):

    # Special-case for 0-length combinations
    if r == 0:
        return np.asarray([])

    a = np.asarray(a)
    data_type = a.dtype if r == 0 else np.dtype([('', a.dtype)] * r)
    b = np.fromiter(combinations(a, r), data_type)
    return b.view(a.dtype).reshape(-1, r)

NumPy implementation of ``itertools.combinations``.

    Return successive ``r``-length combinations of elements in the array ``a``.

Args:
        a (np.ndarray): The array from which to get combinations.
        r (int): The length of the combinations.

Returns:
        np.ndarray: An array of combinations.


def seek(self, offset, whence=os.SEEK_SET):

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

    if self._current_offset < 0:
      raise IOError(
          'Invalid current offset: {0:d} value less than zero.'.format(
              self._current_offset))

    if whence == os.SEEK_CUR:
      offset += self._current_offset

    elif whence == os.SEEK_END:
      if self._decrypted_stream_size is None:
        self._decrypted_stream_size = self._GetDecryptedStreamSize()
        if self._decrypted_stream_size is None:
          raise IOError('Invalid decrypted stream size.')

      offset += self._decrypted_stream_size

    elif whence != os.SEEK_SET:
      raise IOError('Unsupported whence.')

    if offset < 0:
      raise IOError('Invalid offset value less than zero.')

    if offset != self._current_offset:
      self._current_offset = offset
      self._realign_offset = True

Seeks to an offset within the file-like object.

Args:
        offset (int): offset to seek.
        whence (Optional[int]): value that indicates whether offset is an
        absolute or relative position within the file.

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


def get_ranking(self, alt):

        if self.alts_to_ranks is None:
            raise ValueError("Aggregate ranking must be created first")
        try:
            rank = self.alts_to_ranks[alt]
            return rank
        except KeyError:
            raise KeyError("No alternative \"{}\" found in ".format(str(alt)) +
                           "the aggregate ranking")

Description:
    Returns the ranking of a given alternative in the
    computed aggregate ranking.  An error is thrown if
    the alternative does not exist.  The ranking is the
    index in the aggregate ranking, which is 0-indexed.
    Parameters:
    alt: the key that represents an alternative


def ParseFileObject(self, parser_mediator, file_object):

    # TODO: self._line_structures is a work-around and this needs
    # a structural fix.
    if not self._line_structures:
      raise errors.UnableToParseFile(
          'Line structure undeclared, unable to proceed.')

    encoding = self._ENCODING or parser_mediator.codepage
    text_file_object = text_file.TextFile(file_object, encoding=encoding)

    try:
      line = self._ReadLine(text_file_object, max_len=self.MAX_LINE_LENGTH)
    except UnicodeDecodeError:
      raise errors.UnableToParseFile(
          'Not a text file or encoding not supported.')

    if not line:
      raise errors.UnableToParseFile('Not a text file.')

    if len(line) == self.MAX_LINE_LENGTH or len(
        line) == self.MAX_LINE_LENGTH - 1:
      logger.debug((
          'Trying to read a line and reached the maximum allowed length of '
          '{0:d}. The last few bytes of the line are: {1:s} [parser '
          '{2:s}]').format(
              self.MAX_LINE_LENGTH, repr(line[-10:]), self.NAME))

    if not self._IsText(line):
      raise errors.UnableToParseFile('Not a text file, unable to proceed.')

    if not self.VerifyStructure(parser_mediator, line):
      raise errors.UnableToParseFile('Wrong file structure.')

    consecutive_line_failures = 0
    index = None
    # Set the offset to the beginning of the file.
    self._current_offset = 0
    # Read every line in the text file.
    while line:
      if parser_mediator.abort:
        break
      parsed_structure = None
      use_key = None
      # Try to parse the line using all the line structures.
      for index, (key, structure) in enumerate(self._line_structures):
        try:
          parsed_structure = structure.parseString(line)
        except pyparsing.ParseException:
          pass
        if parsed_structure:
          use_key = key
          break

      if parsed_structure:
        self.ParseRecord(parser_mediator, use_key, parsed_structure)
        consecutive_line_failures = 0
        if index is not None and index != 0:
          key_structure = self._line_structures.pop(index)
          self._line_structures.insert(0, key_structure)
      else:
        if len(line) > 80:
          line = '{0:s}...'.format(line[:77])
        parser_mediator.ProduceExtractionWarning(
            'unable to parse log line: {0:s} at offset: {1:d}'.format(
                repr(line), self._current_offset))
        consecutive_line_failures += 1
        if (consecutive_line_failures >
            self.MAXIMUM_CONSECUTIVE_LINE_FAILURES):
          raise errors.UnableToParseFile(
              'more than {0:d} consecutive failures to parse lines.'.format(
                  self.MAXIMUM_CONSECUTIVE_LINE_FAILURES))

      self._current_offset = text_file_object.get_offset()

      try:
        line = self._ReadLine(text_file_object, max_len=self.MAX_LINE_LENGTH)
      except UnicodeDecodeError:
        parser_mediator.ProduceExtractionWarning(
            'unable to read and decode log line at offset {0:d}'.format(
                self._current_offset))
        break

Parses a text file-like object using a pyparsing definition.

Args:
        parser_mediator (ParserMediator): mediates interactions between parsers
        and other components, such as storage and dfvfs.
        file_object (dfvfs.FileIO): file-like object.

Raises:
        UnableToParseFile: when the file cannot be parsed.


def find_by_field(self, table, field, field_value):

        sql = "select * from {} where {} = '{}'".format(
            table, field, field_value)
        res = self.query(sql)
        return res

从数据库里查询指定条件的记录

Args:
        table: 表名字 str
        field: 字段名
        field_value: 字段值

Returns:
        成功： [dict] 保存的记录
        失败： -1 并打印返回报错信息


def response_data_to_model_instance(self, response_data):

        # Coerce datetime strings into datetime objects
        response_data["datetime_created"] = dateutil.parser.parse(
            response_data["datetime_created"]
        )

        if response_data["datetime_finished"]:
            response_data["datetime_finished"] = dateutil.parser.parse(
                response_data["datetime_finished"]
            )

        # Instantiate a model for the task instance
        return super(
            BaseTaskInstanceManager, self
        ).response_data_to_model_instance(response_data)

Convert response data to a task instance model.

Args:
        response_data (dict): The data from the request's response.

Returns:
        :class:`saltant.models.base_task_instance.BaseTaskInstance`:
            A task instance model instance representing the task
            instance from the reponse data.


def warn_once(self, msg, msg_name=None):

        assert isinstance(msg, str)
        msg_name = msg_name if msg_name else msg
        if msg_name not in warnings_given:
            warnings.warn(msg)
        warnings_given.add(msg_name)

Prints a warning statement just once

Args:
        msg: The warning message
        msg_name: [optional] The name of the warning. If None, the msg_name
            will be the msg itself.


def reqMatchingSymbols(self, pattern: str) -> List[ContractDescription]:

        return self._run(self.reqMatchingSymbolsAsync(pattern))

Request contract descriptions of contracts that match a pattern.

    This method is blocking.

    https://interactivebrokers.github.io/tws-api/matching_symbols.html

Args:
        pattern: The first few letters of the ticker symbol, or for
            longer strings a character sequence matching a word in
            the security name.


def transform(self, X):

        assert np.shape(X)[0] == len(self._weights), (
            'BlendingOptimizer: Number of models to blend its predictions and weights does not match: '
            'n_models={}, weights_len={}'.format(np.shape(X)[0], len(self._weights)))
        blended_predictions = np.average(np.power(X, self._power),
                                         weights=self._weights,
                                         axis=0) ** (1.0 / self._power)

        return {'y_pred': blended_predictions}

Performs predictions blending using the trained weights.

Args:
        X (array-like): Predictions of different models.
    Returns: dict with blended predictions (key is 'y_pred').


def preprocess_data(data: List[Tuple[List[str], List[str]]], to_lower: bool = True,
                    append_case: str = "first") -> List[Tuple[List[Tuple[str]], List[str]]]:

    new_data = []
    for words, tags in data:
        new_words = [process_word(word, to_lower=to_lower, append_case=append_case)
                     for word in words]
        # tags could also be processed in future
        new_tags = tags
        new_data.append((new_words, new_tags))
    return new_data

Processes all words in data using
    :func:`~deeppavlov.dataset_iterators.morphotagger_iterator.process_word`.

Args:
        data: a list of pairs (words, tags), each pair corresponds to a single sentence
        to_lower: whether to lowercase
        append_case: whether to add case mark

Returns:
        a list of preprocessed sentences


def do_add_all_link(self, args):

        linkcode = 1
        group = 0
        addr = None
        params = args.split()
        if params:
            try:
                linkcode = int(params[0])
            except IndexError:
                linkcode = 1
            except ValueError:
                linkcode = None

            try:
                group = int(params[1])
            except IndexError:
                group = 0
            except ValueError:
                group = None

            try:
                addr = params[2]
            except IndexError:
                addr = None

        if linkcode in [0, 1, 3] and 255 >= group >= 0:
            self.loop.create_task(
                self.tools.start_all_linking(linkcode, group, addr))
        else:
            _LOGGING.error('Link code %d or group number %d not valid',
                           linkcode, group)
            self.do_help('add_all_link')

Add an All-Link record to the IM and a device.

    Usage:
        add_all_link [linkcode] [group] [address]

Args:
        linkcode: 0 - PLM is responder
                1 - PLM is controller
                3 - PLM is controller or responder
                Default 1
        group: All-Link group number (0 - 255). Default 0.
        address: INSTEON device to link with (not supported by all devices)


def _render_batch(self,
            non_fluents: NonFluents,
            states: Fluents, actions: Fluents, interms: Fluents,
            rewards: np.array,
            horizon: Optional[int] = None) -> None:

        if horizon is None:
            horizon = len(states[0][1])
            self._render_round_init(horizon, non_fluents)
            for t in range(horizon):
                s = [(s[0], s[1][t]) for s in states]
                f = [(f[0], f[1][t]) for f in interms]
                a = [(a[0], a[1][t]) for a in actions]
                r = rewards[t]
                self._render_timestep(t, s, a, f, r)
            self._render_round_end(rewards)

Prints `non_fluents`, `states`, `actions`, `interms` and `rewards`
    for given `horizon`.

Args:
        states (Sequence[Tuple[str, np.array]]): A state trajectory.
        actions (Sequence[Tuple[str, np.array]]): An action trajectory.
        interms (Sequence[Tuple[str, np.array]]): An interm state trajectory.
        rewards (np.array): Sequence of rewards (1-dimensional array).
        horizon (Optional[int]): Number of timesteps.


def step(self, closure=None):

        loss = None
        if closure is not None:
            loss = closure()

        for group in self.param_groups:
            for p in group['params']:
                if p.grad is None:
                    continue
                grad = p.grad.data
                if grad.is_sparse:
                    raise RuntimeError('Adam does not support sparse gradients, please consider SparseAdam instead')

                state = self.state[p]

                # State initialization
                if len(state) == 0:
                    state['step'] = 0
                    # Exponential moving average of gradient values
                    state['exp_avg'] = torch.zeros_like(p.data)
                    # Exponential moving average of squared gradient values
                    state['exp_avg_sq'] = torch.zeros_like(p.data)

                exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']
                beta1, beta2 = group['b1'], group['b2']

                state['step'] += 1

                # Add grad clipping
                if group['max_grad_norm'] > 0:
                    clip_grad_norm_(p, group['max_grad_norm'])

                # Decay the first and second moment running average coefficient
                exp_avg.mul_(beta1).add_(1 - beta1, grad)
                exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad)
                denom = exp_avg_sq.sqrt().add_(group['e'])

                bias_correction1 = 1 - beta1 ** state['step']
                bias_correction2 = 1 - beta2 ** state['step']

                lr_scheduled = group['lr']
                lr_scheduled *= group['schedule'].get_lr(state['step'])

                step_size = lr_scheduled * math.sqrt(bias_correction2) / bias_correction1

                p.data.addcdiv_(-step_size, exp_avg, denom)

                # Add weight decay at the end (fixed version)
                if (len(p.size()) > 1 or group['vector_l2']) and group['weight_decay'] > 0:
                    p.data.add_(-lr_scheduled * group['weight_decay'], p.data)

        return loss

Performs a single optimization step.

Args:
        closure (callable, optional): A closure that reevaluates the model
            and returns the loss.


def ephemeris(self, **kwargs):

        for orb in self.iter(inclusive=True, **kwargs):
            yield orb

Generator giving the propagation of the orbit at different dates

Args:
        start (Date)
        stop (Date or timedelta)
        step (timedelta)

Yields:
        Orbit


def get_content_metadata(self, enterprise_customer):

        content_metadata = OrderedDict()

        # TODO: This if block can be removed when we get rid of discovery service-based catalogs.
        if enterprise_customer.catalog:
            response = self._load_data(
                self.ENTERPRISE_CUSTOMER_ENDPOINT,
                detail_resource='courses',
                resource_id=str(enterprise_customer.uuid),
                traverse_pagination=True,
            )
            for course in response['results']:
                for course_run in course['course_runs']:
                    course_run['content_type'] = 'courserun'  # Make this look like a search endpoint result.
                    content_metadata[course_run['key']] = course_run

        for enterprise_customer_catalog in enterprise_customer.enterprise_customer_catalogs.all():
            response = self._load_data(
                self.ENTERPRISE_CUSTOMER_CATALOGS_ENDPOINT,
                resource_id=str(enterprise_customer_catalog.uuid),
                traverse_pagination=True,
                querystring={'page_size': 1000},
            )

            for item in response['results']:
                content_id = utils.get_content_metadata_item_id(item)
                content_metadata[content_id] = item

        return content_metadata.values()

Return all content metadata contained in the catalogs associated with the EnterpriseCustomer.

Args:
        enterprise_customer (EnterpriseCustomer): The EnterpriseCustomer to return content metadata for.

Returns:
        list: List of dicts containing content metadata.


def use_color(setting):

    if setting not in COLOR_CHOICES:
        raise InvalidColorSetting(setting)

    return (
        setting == 'always' or
        (setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)
    )

Choose whether to use color based on the command argument.

Args:
        setting - Either `auto`, `always`, or `never`


def supported_tasks(self, lang=None):

    if lang:
      collection = self.get_collection(lang=lang)
      return [x.id.split('.')[0] for x in collection.packages]
    else:
      return [x.name.split()[0] for x in self.collections() if Downloader.TASK_PREFIX in x.id]

Languages that are covered by a specific task.

Args:
        lang (string): Language code name.


def __init__(self, parent):

        logger.debug("Initialising status bar")

        super(StatusBar, self).__init__(parent)

        self.status = tk.StringVar()
        # Status bar
        self.statusbar = ttk.Label(self, textvariable=self.status, padding=2, anchor="center")
        self.statusbar.grid(column=0, row=0, sticky="W E")

        # Configure stretch ratios
        self.columnconfigure(0, weight=1)

        # Set default status
        self.set_status(False)

Create a new status bar.

Args:
        parent: A tk or ttk object


def encipher_vigenere(plaintext, plain_vocab, key):

  ciphertext = []
  # generate Vigenere table
  layers = [
      ShiftEncryptionLayer(plain_vocab, i) for i in range(len(plain_vocab))
  ]

  for i, sentence in enumerate(plaintext):
    cipher_sentence = []
    for j, character in enumerate(sentence):
      key_idx = key[j % len(key)]
      encrypted_char = layers[key_idx].encrypt_character(character)
      cipher_sentence.append(encrypted_char)
    ciphertext.append(cipher_sentence)

  return ciphertext

Encrypt plain text with given key.

Args:
        plaintext (list of list of Strings): a list of plain text to encrypt.
        plain_vocab (list of Integer): unique vocabularies being used.
        key (list of Integer): key to encrypt cipher using Vigenere table.

Returns:
        ciphertext (list of Strings): encrypted plain text.


def copy(self, name=None):

        cpy = copy.copy(self)
        if name:
            cpy.name = name
        return cpy

shallow copy of the instruction.

Args:
        name (str): name to be given to the copied circuit,
        if None then the name stays the same

Returns:
        Instruction: a shallow copy of the current instruction, with the name
        updated if it was provided


def match(self, subject: Expression) -> Iterator[Tuple[Pattern, Substitution]]:

        if not isinstance(subject, self.operation):
            return

        subjects = list(op_iter(subject))
        flatterms = [FlatTerm(o) for o in subjects]

        for i in range(len(flatterms)):
            flatterm = FlatTerm.merged(*flatterms[i:])

            for index in self._net._match(flatterm, collect=True):
                match_index = self._net._patterns[index][1]
                pattern, first_name, last_name = self._patterns[match_index]
                operand_count = op_len(pattern.expression) - 2
                expr_operands = subjects[i:i + operand_count]
                patt_operands = list(op_iter(pattern.expression))[1:-1]

                substitution = Substitution()
                if not all(itertools.starmap(substitution.extract_substitution, zip(expr_operands, patt_operands))):
                    continue

                try:
                    if first_name is not None:
                        substitution.try_add_variable(first_name, tuple(subjects[:i]))
                    if last_name is not None:
                        substitution.try_add_variable(last_name, tuple(subjects[i + operand_count:]))
                except ValueError:
                    continue

                for constraint in pattern.constraints:
                    if not constraint(substitution):
                        break
                else:
                    yield pattern, substitution

Match the given subject against all patterns in the sequence matcher.

Args:
        subject:
            The subject that is matched. Must be constant.

Yields:
        A tuple :code:`(pattern, substitution)` for every matching pattern.


def _convert_to_dict(data):

    if isinstance(data, dict):
        return data

    if isinstance(data, list) or isinstance(data, tuple):
        if _all_correct_list(data):
            return dict(data)
        else:
            data = zip(data[::2], data[1::2])
            return dict(data)
    else:
        raise MetaParsingException(
            "Can't decode provided metadata - unknown structure."
        )

Convert `data` to dictionary.

    Tries to get sense in multidimensional arrays.

Args:
        data: List/dict/tuple of variable dimension.

Returns:
        dict: If the data can be converted to dictionary.

Raises:
        MetaParsingException: When the data are unconvertible to dict.


def create_dir(path):

    full_path = abs_path(path)
    if not os.path.exists(full_path):
        try:
            os.makedirs(full_path)
        except OSError as e:
            # ignore the error for dir already exist.
            if e.errno != os.errno.EEXIST:
                raise

Creates a directory if it does not exist already.

Args:
        path: The path of the directory to create.


def _create_gates(self, inputs, memory):

    # We'll create the input and forget gates at once. Hence, calculate double
    # the gate size.
    num_gates = 2 * self._calculate_gate_size()

    memory = tf.tanh(memory)
    inputs = basic.BatchFlatten()(inputs)
    gate_inputs = basic.BatchApply(basic.Linear(num_gates), n_dims=1)(inputs)
    gate_inputs = tf.expand_dims(gate_inputs, axis=1)
    gate_memory = basic.BatchApply(basic.Linear(num_gates))(memory)
    gates = tf.split(gate_memory + gate_inputs, num_or_size_splits=2, axis=2)
    input_gate, forget_gate = gates

    input_gate = tf.sigmoid(input_gate + self._input_bias)
    forget_gate = tf.sigmoid(forget_gate + self._forget_bias)

    return input_gate, forget_gate

Create input and forget gates for this step using `inputs` and `memory`.

Args:
        inputs: Tensor input.
        memory: The current state of memory.

Returns:
        input_gate: A LSTM-like insert gate.
        forget_gate: A LSTM-like forget gate.


def __init__(self, scope, parent, name, result, args=None, paren=False):

        CodeExpression.__init__(self, scope, parent, name, result, paren)
        self.arguments = args or ()

Constructor for operators.

Args:
        scope (CodeEntity): The program scope where this object belongs.
        parent (CodeEntity): This object's parent in the program tree.
        name (str): The name of the operator in the program.
        result (str): The return type of the operator in the program.

    Kwargs:
        args (tuple): Initial tuple of arguments.
        paren (bool): Whether the expression is enclosed in parentheses.


def wait_any(jobs, timeout=None):

    return Job._wait(jobs, timeout, concurrent.futures.FIRST_COMPLETED)

Return when at least one of the specified jobs has completed or timeout expires.

Args:
        jobs: a Job or list of Jobs to wait on.
        timeout: a timeout in seconds to wait for. None (the default) means no timeout.

Returns:
        A list of the jobs that have now completed or None if there were no jobs.


def compose_path(pub, uuid_url=False):

    if uuid_url:
        return join(
            "/",
            UUID_DOWNLOAD_KEY,
            str(pub.uuid)
        )

    return join(
        "/",
        DOWNLOAD_KEY,
        basename(pub.file_pointer),
        basename(pub.filename)
    )

Compose absolute path for given `pub`.

Args:
        pub (obj): :class:`.DBPublication` instance.
        uuid_url (bool, default False): Compose URL using UUID.

Returns:
        str: Absolute url-path of the publication, without server's address \
            and protocol.

Raises:
        PrivatePublicationError: When the `pub` is private publication.


def __init__(self, channel):

    self.Classify = channel.unary_unary(
        '/tensorflow.serving.PredictionService/Classify',
        request_serializer=tensorflow__serving_dot_apis_dot_classification__pb2.ClassificationRequest.SerializeToString,
        response_deserializer=tensorflow__serving_dot_apis_dot_classification__pb2.ClassificationResponse.FromString,
        )
    self.Regress = channel.unary_unary(
        '/tensorflow.serving.PredictionService/Regress',
        request_serializer=tensorflow__serving_dot_apis_dot_regression__pb2.RegressionRequest.SerializeToString,
        response_deserializer=tensorflow__serving_dot_apis_dot_regression__pb2.RegressionResponse.FromString,
        )
    self.Predict = channel.unary_unary(
        '/tensorflow.serving.PredictionService/Predict',
        request_serializer=tensorflow__serving_dot_apis_dot_predict__pb2.PredictRequest.SerializeToString,
        response_deserializer=tensorflow__serving_dot_apis_dot_predict__pb2.PredictResponse.FromString,
        )
    self.GetModelMetadata = channel.unary_unary(
        '/tensorflow.serving.PredictionService/GetModelMetadata',
        request_serializer=tensorflow__serving_dot_apis_dot_get__model__metadata__pb2.GetModelMetadataRequest.SerializeToString,
        response_deserializer=tensorflow__serving_dot_apis_dot_get__model__metadata__pb2.GetModelMetadataResponse.FromString,
        )

Constructor.

Args:
        channel: A grpc.Channel.


def quality(self, tests, alias=None):

        # This is hacky... striplog should probably merge with welly...

        # Ignore aliases
        alias = alias or {}
        alias = alias.get('striplog', alias.get('Striplog', []))

        # Gather the tests.
        # First, anything called 'all', 'All', or 'ALL'.
        # Second, anything with the name of the curve we're in now.
        # Third, anything that the alias list has for this curve.
        # (This requires a reverse look-up so it's a bit messy.)
        this_tests =\
            tests.get('all', [])+tests.get('All', [])+tests.get('ALL', [])\
            + tests.get('striplog', tests.get('Striplog', []))\
            + utils.flatten_list([tests.get(a) for a in alias])
        this_tests = filter(None, this_tests)

        # If we explicitly set zero tests for a particular key, then this
        # overrides the 'all' tests.
        if not tests.get('striplog', tests.get('Striplog', 1)):
            this_tests = []

        return {test.__name__: test(self) for test in this_tests}

Run a series of tests and return the corresponding results.

    Based on curve testing for ``welly``.

Args:
        tests (list): a list of functions.

Returns:
        list. The results. Stick to booleans (True = pass) or ints.