def __init__(self, args=None, cmd=None):

    if args is None or cmd is None:
      raise errors.FormatError('Missing args or cmd value.')

    super(CommandSourceType, self).__init__()
    self.args = args
    self.cmd = cmd

Initializes a source type.

Args:
        args (list[str]): arguments to the command to run.
        cmd (str): command to run.

Raises:
        FormatError: when args or cmd is not set.


def _ConvertMapFieldValue(self, value, message, field):

    if not isinstance(value, dict):
      raise ParseError(
          'Map field {0} must be in a dict which is {1}.'.format(
              field.name, value))
    key_field = field.message_type.fields_by_name['key']
    value_field = field.message_type.fields_by_name['value']
    for key in value:
      key_value = _ConvertScalarFieldValue(key, key_field, True)
      if value_field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
        self.ConvertMessage(value[key], getattr(
            message, field.name)[key_value])
      else:
        getattr(message, field.name)[key_value] = _ConvertScalarFieldValue(
            value[key], value_field)

Convert map field value for a message map field.

Args:
        value: A JSON object to convert the map field value.
        message: A protocol message to record the converted data.
        field: The descriptor of the map field to be converted.

Raises:
        ParseError: In case of convert problems.


def bounds(self, thr=0, lower_index=0, upper_index=-1):

        points = self.points[lower_index:upper_index]

        min_lat = float("inf")
        min_lon = float("inf")
        max_lat = -float("inf")
        max_lon = -float("inf")

        for point in points:
            min_lat = min(min_lat, point.lat)
            min_lon = min(min_lon, point.lon)
            max_lat = max(max_lat, point.lat)
            max_lon = max(max_lon, point.lon)

        return (min_lat - thr, min_lon - thr, max_lat + thr, max_lon + thr)

Computes the bounds of the segment, or part of it

Args:
        lower_index (int, optional): Start index. Defaults to 0
        upper_index (int, optional): End index. Defaults to 0

Returns:
        :obj:`tuple` of :obj:`float`: Bounds of the (sub)segment, such that
            (min_lat, min_lon, max_lat, max_lon)


def is_periodic_image(self, other, tolerance=1e-8, check_lattice=True):

        if check_lattice and self.lattice != other.lattice:
            return False
        if self.species != other.species:
            return False

        frac_diff = pbc_diff(self.frac_coords, other.frac_coords)
        return np.allclose(frac_diff, [0, 0, 0], atol=tolerance)

Returns True if sites are periodic images of each other.

Args:
        other (PeriodicSite): Other site
        tolerance (float): Tolerance to compare fractional coordinates
        check_lattice (bool): Whether to check if the two sites have the
            same lattice.

Returns:
        bool: True if sites are periodic images of each other.


def team_pitching_bref(team, start_season, end_season=None):

    if start_season is None:
        raise ValueError("You need to provide at least one season to collect data for. Try team_pitching_bref(season) or team_pitching_bref(start_season, end_season).")
    if end_season is None:
        end_season = start_season

    url = "https://www.baseball-reference.com/teams/{}".format(team)

    data = []
    headings = None
    for season in range(start_season, end_season+1):
        print("Getting Pitching Data: {} {}".format(season, team))
        stats_url = "{}/{}.shtml".format(url, season)
        response = requests.get(stats_url)
        soup = BeautifulSoup(response.content, 'html.parser')

        table = soup.find_all('table', {'id': 'team_pitching'})[0]

        if headings is None:
            headings = [row.text.strip() for row in table.find_all('th')[1:34]]

        rows = table.find_all('tr')
        for row in rows:
            cols = row.find_all('td')
            cols = [ele.text.strip() for ele in cols]
            cols = [col.replace('*', '').replace('#', '') for col in cols]  # Removes '*' and '#' from some names
            cols = [col for col in cols if 'Totals' not in col and 'NL teams' not in col and 'AL teams' not in col]  # Removes Team Totals and other rows
            cols.insert(2, season)
            data.append([ele for ele in cols[0:]])

    headings.insert(2, "Year")
    data = pd.DataFrame(data=data, columns=headings) # [:-5]  # -5 to remove Team Totals and other rows (didn't work in multi-year queries)
    data = data.dropna()  # Removes Row of All Nones

    return data

Get season-level Pitching Statistics for Specific Team (from Baseball-Reference)

Args:
    team : str : The Team Abbreviation (i.e. 'NYY' for Yankees) of the Team you want data for
    start_season : int : first season you want data for (or the only season if you do not specify an end_season)
    end_season : int : final season you want data for


def get_kinds(start=None, end=None):

  q = Kind.query()
  if start is not None and start != '':
    q = q.filter(Kind.key >= Kind.key_for_kind(start))
  if end is not None:
    if end == '':
      return []
    q = q.filter(Kind.key < Kind.key_for_kind(end))

  return [x.kind_name for x in q]

Return all kinds in the specified range, for the current namespace.

Args:
        start: only return kinds >= start if start is not None.
        end: only return kinds < end if end is not None.

Returns:
        A list of kind names between the (optional) start and end values.


def to_bytesize(value, default_unit=None, base=DEFAULT_BASE):

    if isinstance(value, (int, float)):
        return unitized(value, default_unit, base)

    if value is None:
        return None

    try:
        if value[-1].lower() == "b":
            # Accept notations such as "1mb", as they get used out of habit
            value = value[:-1]

        unit = value[-1:].lower()
        if unit.isdigit():
            unit = default_unit

        else:
            value = value[:-1]

        return unitized(to_number(float, value), unit, base)

    except (IndexError, TypeError, ValueError):
        return None

Convert `value` to bytes, accepts notations such as "4k" to mean 4096 bytes

Args:
        value (str | unicode | int | None): Number of bytes optionally suffixed by a char from UNITS
        default_unit (str | unicode | None): Default unit to use for unqualified values
        base (int): Base to use (usually 1024)

Returns:
        (int | None): Deduced bytesize value, if possible


def apply_cut(self, cut):

        # TODO: is the MICE cache reusable?
        return MacroSubsystem(
            self.network,
            self.network_state,
            self.micro_node_indices,
            cut=cut,
            time_scale=self.time_scale,
            blackbox=self.blackbox,
            coarse_grain=self.coarse_grain)

Return a cut version of this |MacroSubsystem|.

Args:
        cut (Cut): The cut to apply to this |MacroSubsystem|.

Returns:
        MacroSubsystem: The cut version of this |MacroSubsystem|.


def AddEventData(self, event_data):

    self._RaiseIfNotWritable()

    self._AddAttributeContainer(self._CONTAINER_TYPE_EVENT_DATA, event_data)

Adds event data.

Args:
        event_data (EventData): event data.

Raises:
        IOError: when the storage file is closed or read-only.
        OSError: when the storage file is closed or read-only.


def with_row(self, row):

        self = self.copy()
        self.append(row)
        return self

Return a table with an additional row.

Args:
        ``row`` (sequence): A value for each column.

Raises:
        ``ValueError``: If the row length differs from the column count.

    >>> tiles = Table(make_array('letter', 'count', 'points'))
    >>> tiles.with_row(['c', 2, 3]).with_row(['d', 4, 2])
    letter | count | points
    c      | 2     | 3
    d      | 4     | 2


def build_subresource_uri(self, resource_id_or_uri=None, subresource_id_or_uri=None, subresource_path=''):

        if subresource_id_or_uri and "/" in subresource_id_or_uri:
            return subresource_id_or_uri
        else:
            if not resource_id_or_uri:
                raise exceptions.HPOneViewValueError(RESOURCE_ID_OR_URI_REQUIRED)

            resource_uri = self.build_uri(resource_id_or_uri)

            uri = "{}/{}/{}".format(resource_uri, subresource_path, str(subresource_id_or_uri or ''))
            uri = uri.replace("//", "/")

            if uri.endswith("/"):
                uri = uri[:-1]

            return uri

Helps to build a URI with resource path and its sub resource path.

Args:
        resoure_id_or_uri: ID/URI of the main resource.
        subresource_id__or_uri: ID/URI of the sub resource.
        subresource_path: Sub resource path to be added with the URI.

Returns:
        Returns URI


def cancel(
        self,
        accountID,
        orderSpecifier,
        **kwargs
    ):

        request = Request(
            'PUT',
            '/v3/accounts/{accountID}/orders/{orderSpecifier}/cancel'
        )

        request.set_path_param(
            'accountID',
            accountID
        )

        request.set_path_param(
            'orderSpecifier',
            orderSpecifier
        )

        response = self.ctx.request(request)

        if response.content_type is None:
            return response

        if not response.content_type.startswith("application/json"):
            return response

        jbody = json.loads(response.raw_body)

        parsed_body = {}

        #
        # Parse responses as defined by the API specification
        #
        if str(response.status) == "200":
            if jbody.get('orderCancelTransaction') is not None:
                parsed_body['orderCancelTransaction'] = \
                    self.ctx.transaction.OrderCancelTransaction.from_dict(
                        jbody['orderCancelTransaction'],
                        self.ctx
                    )

            if jbody.get('relatedTransactionIDs') is not None:
                parsed_body['relatedTransactionIDs'] = \
                    jbody.get('relatedTransactionIDs')

            if jbody.get('lastTransactionID') is not None:
                parsed_body['lastTransactionID'] = \
                    jbody.get('lastTransactionID')

        elif str(response.status) == "401":
            if jbody.get('errorCode') is not None:
                parsed_body['errorCode'] = \
                    jbody.get('errorCode')

            if jbody.get('errorMessage') is not None:
                parsed_body['errorMessage'] = \
                    jbody.get('errorMessage')

        elif str(response.status) == "404":
            if jbody.get('orderCancelRejectTransaction') is not None:
                parsed_body['orderCancelRejectTransaction'] = \
                    self.ctx.transaction.OrderCancelRejectTransaction.from_dict(
                        jbody['orderCancelRejectTransaction'],
                        self.ctx
                    )

            if jbody.get('relatedTransactionIDs') is not None:
                parsed_body['relatedTransactionIDs'] = \
                    jbody.get('relatedTransactionIDs')

            if jbody.get('lastTransactionID') is not None:
                parsed_body['lastTransactionID'] = \
                    jbody.get('lastTransactionID')

            if jbody.get('errorCode') is not None:
                parsed_body['errorCode'] = \
                    jbody.get('errorCode')

            if jbody.get('errorMessage') is not None:
                parsed_body['errorMessage'] = \
                    jbody.get('errorMessage')

        elif str(response.status) == "405":
            if jbody.get('errorCode') is not None:
                parsed_body['errorCode'] = \
                    jbody.get('errorCode')

            if jbody.get('errorMessage') is not None:
                parsed_body['errorMessage'] = \
                    jbody.get('errorMessage')

        #
        # Unexpected response status
        #
        else:
            parsed_body = jbody

        response.body = parsed_body

        return response

Cancel a pending Order in an Account

Args:
        accountID:
            Account Identifier
        orderSpecifier:
            The Order Specifier

Returns:
        v20.response.Response containing the results from submitting the
        request


def path_size(p: tcod.path.AStar) -> int:

    return int(lib.TCOD_path_size(p._path_c))

Return the current length of the computed path.

Args:
        p (AStar): An AStar instance.

Returns:
        int: Length of the path.


def read_ckan_catalog(portal_url):

    portal = RemoteCKAN(portal_url)
    try:
        status = portal.call_action(
            'status_show', requests_kwargs={"verify": False})
        packages_list = portal.call_action(
            'package_list', requests_kwargs={"verify": False})
        groups_list = portal.call_action(
            'group_list', requests_kwargs={"verify": False})

        # itera leyendo todos los datasets del portal
        packages = []
        num_packages = len(packages_list)
        for index, pkg in enumerate(packages_list):
            # progreso (necesario cuando son muchos)
            msg = "Leyendo dataset {} de {}".format(index + 1, num_packages)
            logger.info(msg)

            # agrega un nuevo dataset a la lista
            packages.append(portal.call_action(
                'package_show', {'id': pkg},
                requests_kwargs={"verify": False}
            ))

            # tiempo de espera padra evitar baneos
            time.sleep(0.2)

        # itera leyendo todos los temas del portal
        groups = [portal.call_action(
            'group_show', {'id': grp},
            requests_kwargs={"verify": False})
            for grp in groups_list]

        catalog = map_status_to_catalog(status)
        catalog["dataset"] = map_packages_to_datasets(
            packages, portal_url)
        catalog["themeTaxonomy"] = map_groups_to_themes(groups)

    except (CKANAPIError, RequestException) as e:
        logger.exception(
            'Error al procesar el portal %s', portal_url, exc_info=True)
        raise NonParseableCatalog(portal_url, e)

    return catalog

Convierte los metadatos de un portal disponibilizados por la Action API
    v3 de CKAN al estándar data.json.

Args:
        portal_url (str): URL de un portal de datos CKAN que soporte la API v3.

Returns:
        dict: Representación interna de un catálogo para uso en las funciones
            de esta librería.


def timestamp_any(value):

    return int(calendar.timegm(dateutil.parser.parse(value).utctimetuple()))

Convert timestamp string to time in seconds since epoch.

    Most timestamps strings are supported in fact this wraps the
    dateutil.parser.parse() method. This is SLOW use the other timestamp_*()
    functions if possible.

Args:
        value: A timestamp string.

Returns:
        The time in seconds since epoch as an integer.


def parse(self, text):

        tokens = self.lex(text)
        parser = Parser(tokens)
        return parser.parse()

Parse self.text.

Args:
        text (str): the text to lex

Returns:
        object: a node representing the current rule.


def add_dict_to_hash(a_hash, a_dict):

    if a_dict is None:
        return
    for k, v in a_dict.items():
        a_hash.update(b'\x00' + k.encode('utf-8') + b'\x00' + v.encode('utf-8'))

Adds `a_dict` to `a_hash`

Args:
        a_hash (`Hash`): the secure hash, e.g created by hashlib.md5
        a_dict (dict[string, [string]]): the dictionary to add to the hash


def dotd(A, B, out=None):
    r
    A = asarray(A, float)
    B = asarray(B, float)
    if A.ndim == 1 and B.ndim == 1:
        if out is None:
            return dot(A, B)
        return dot(A, B, out)
    if out is None:
        out = empty((A.shape[0],), float)
    return einsum("ij,ji->i", A, B, out=out)

r"""Diagonal of :math:`\mathrm A\mathrm B^\intercal`.

    If ``A`` is :math:`n\times p` and ``B`` is :math:`p\times n`, it is done in
    :math:`O(pn)`.

Args:
        A (array_like): Left matrix.
        B (array_like): Right matrix.
        out (:class:`numpy.ndarray`, optional): copy result to.

Returns:
        :class:`numpy.ndarray`: Resulting diagonal.


def _GetEventData(
      self, parser_mediator, record_index, evt_record, recovered=False):

    event_data = WinEvtRecordEventData()

    try:
      event_data.record_number = evt_record.identifier
    except OverflowError as exception:
      parser_mediator.ProduceExtractionWarning((
          'unable to read record identifier from event record: {0:d} '
          'with error: {1!s}').format(record_index, exception))

    try:
      event_identifier = evt_record.event_identifier
    except OverflowError as exception:
      parser_mediator.ProduceExtractionWarning((
          'unable to read event identifier from event record: {0:d} '
          'with error: {1!s}').format(record_index, exception))

      event_identifier = None

    event_data.offset = evt_record.offset
    event_data.recovered = recovered

    # We want the event identifier to match the behavior of that of the EVTX
    # event records.
    if event_identifier is not None:
      event_data.event_identifier = event_identifier & 0xffff
      event_data.facility = (event_identifier >> 16) & 0x0fff
      event_data.severity = event_identifier >> 30
      event_data.message_identifier = event_identifier

    event_data.event_type = evt_record.event_type
    event_data.event_category = evt_record.event_category
    event_data.source_name = evt_record.source_name

    # Computer name is the value stored in the event record and does not
    # necessarily corresponds with the actual hostname.
    event_data.computer_name = evt_record.computer_name
    event_data.user_sid = evt_record.user_security_identifier

    event_data.strings = list(evt_record.strings)

    return event_data

Retrieves event data from the Windows EventLog (EVT) record.

Args:
        parser_mediator (ParserMediator): mediates interactions between parsers
        and other components, such as storage and dfvfs.
        record_index (int): event record index.
        evt_record (pyevt.record): event record.
        recovered (Optional[bool]): True if the record was recovered.

Returns:
        WinEvtRecordEventData: event data.


def rgb_to_hex(color):

    rgb = eval(color.replace('rgb', ''))
    # return '#'+''.join(map(chr, rgb)).encode('hex')
    return '#' + ''.join(['{0:02x}'.format(x).upper() for x in rgb])

Converts from rgb to hex

    Parameters:
    -----------
            color : string
                    Color representation on hex or rgb

Example:
            rgb_to_hex('rgb(23,25,24)')


def clean(df,error_rate = 0):

    df = df.copy()

    # Change colnames
    basics.clean_colnames(df)
    # Eventually use a more advanced function to clean colnames
    print('Changed colnames to {}'.format(df.columns))

    # Remove extra whitespace
    obj_col_list = df.select_dtypes(include = 'object').columns
    for col_name in obj_col_list:
        df[col_name] = basics.col_strip(df,col_name)
        print("Stripped extra whitespace from '{}'".format(col_name))

    # Coerce columns if possible
    for col_name in obj_col_list:
        new_dtype = coerce_col(df,col_name,error_rate)
        if new_dtype is not None:
            print("Coerced '{}' to datatype '{}'".format(col_name, new_dtype))

    # Scrub columns
    obj_col_list = df.select_dtypes(include = 'object').columns
    for col_name in obj_col_list:
        scrubf, scrubb = smart_scrub(df,col_name,1-error_rate)
        if scrubf is not None or scrubb is not None:
            print("Scrubbed '{}' from the front and '{}' from the back of column '{}'" \
                  .format(scrubf,scrubb,col_name))

    # Coerice columns if possible
    for col_name in obj_col_list:
        new_dtype = coerce_col(df,col_name,error_rate)
        if new_dtype is not None:
            print("Coerced '{}' to datatype '{}'".format(col_name, new_dtype))
    return df

Superficially cleans data, i.e. changing simple things about formatting.
    Parameters:
    df - DataFrame
        DataFrame to clean
    error_rate - float {0 <= error_rate <= 1}, default 0
        Maximum amount of errors/inconsistencies caused explicitly by cleaning, expressed
        as a percentage of total dataframe rows (0 = 0%, .5 = 50%, etc.)
        Ex: na values from coercing a column of data to numeric


def set_page_artid(self, page_start=None, page_end=None, artid=None):

        if page_end and not page_start:
            raise ValueError('End_page provided without start_page')

        self._ensure_reference_field('publication_info', {})
        publication_info = self.obj['reference']['publication_info']
        if page_start:
            publication_info['page_start'] = page_start
        if page_end:
            publication_info['page_end'] = page_end
        if artid:
            publication_info['artid'] = artid

Add artid, start, end pages to publication info of a reference.

Args:
        page_start(Optional[string]): value for the field page_start
        page_end(Optional[string]): value for the field page_end
        artid(Optional[string]): value for the field artid

Raises:
        ValueError: when no start_page given for an end_page


def recursive_chmod(path, mode=0755):

    passwd_reader.set_permissions(path, mode=mode)
    if os.path.isfile(path):
        return

    # recursively change mode of all subdirectories
    for root, dirs, files in os.walk(path):
        for fn in files + dirs:
            passwd_reader.set_permissions(os.path.join(root, fn), mode=mode)

Recursively change ``mode`` for given ``path``. Same as ``chmod -R mode``.

Args:
        path (str): Path of the directory/file.
        mode (octal int, default 0755): New mode of the file.

    Warning:
        Don't forget to add ``0`` at the beginning of the numbers of `mode`, or
        `Unspeakable hOrRoRs` will be awaken from their unholy sleep outside of
        the reality and they WILL eat your soul (and your files).


def __init__(self, channel):

        self.CompleteQuery = channel.unary_unary(
            "/google.cloud.talent.v4beta1.Completion/CompleteQuery",
            request_serializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_completion__service__pb2.CompleteQueryRequest.SerializeToString,
            response_deserializer=google_dot_cloud_dot_talent__v4beta1_dot_proto_dot_completion__service__pb2.CompleteQueryResponse.FromString,
        )

Constructor.

Args:
        channel: A grpc.Channel.


def __init__(self, context, name, task_id=None):

        self.name = name
        self.context = context
        self.task_id = task_id or get_task_id(context.claim_task)
        self.task = context.task
        self.task_type = guess_task_type(name, self.task)
        self.worker_impl = guess_worker_impl(self)  # this should be scriptworker
        self.decision_task_id = get_decision_task_id(self.task)
        self.parent_task_id = get_parent_task_id(self.task)
        self.links = []

Initialize ChainOfTrust.

Args:
        context (scriptworker.context.Context): the scriptworker context
        name (str): the name of the task (e.g., signing)
        task_id (str, optional): the task_id of the task.  If None, use
            ``get_task_id(context.claim_task)``.  Defaults to None.


def _circuit_as_layers(circuit: circuits.Circuit,
                       grouping: _QubitGrouping) -> List[_TransformsThenCzs]:

    frontier = {q: 0 for q in circuit.all_qubits()}

    layers = []
    while True:
        # Pull within-group operations into per-group matrices.
        any_group_matrices = False
        group_matrices = []
        for g in grouping.groups:
            # Scan for reachable operations contained within the group qubits.
            start_frontier = {q: frontier[q] for q in g}
            end_frontier = circuit.reachable_frontier_from(start_frontier)
            mergeable_ops = circuit.findall_operations_between(start_frontier,
                                                               end_frontier)

            # Advance frontier.
            for q, v in end_frontier.items():
                frontier[q] = v

            # Fold reachable operations into a single group matrix.
            group_matrix = np.eye(1 << len(g)).reshape((2, 2) * len(g))
            if mergeable_ops:
                any_group_matrices = True
            for _, op in mergeable_ops:
                group_matrix = linalg.targeted_left_multiply(
                    left_matrix=protocols.unitary(op).reshape(
                        (2, 2) * len(op.qubits)),
                    right_target=group_matrix,
                    target_axes=[grouping.loc(q)[1] for q in op.qubits])
            group_matrices.append(np.transpose(group_matrix.reshape(
                1 << len(g), 1 << len(g))))

        # Scan for reachable CZ operations between groups.
        end_frontier = circuit.reachable_frontier_from(
            frontier,
            is_blocker=lambda op: grouping.all_in_same_group(*op.qubits))
        cz_ops = circuit.findall_operations_between(frontier, end_frontier)

        # Advance frontier.
        frontier = end_frontier

        # List out qubit index pairs for each CZ.
        cz_indices = []
        for _, cz in cz_ops:
            a, b = cz.qubits
            assert cz == ops.CZ(a, b)
            cz_indices.append((grouping.ind(a), grouping.ind(b)))

        # Combine group and CZ operations into a simulation layer.
        if not any_group_matrices and not cz_indices:
            break
        layer = _TransformsThenCzs(group_matrices=group_matrices,
                                   cz_indices=cz_indices)
        layers.append(layer)

    # We should have processed the whole circuit.
    assert frontier == {q: len(circuit) for q in circuit.all_qubits()}

    return layers

Transforms a circuit into a series of GroupMatrix+CZ layers.

Args:
        circuit: The circuit to transform.
        grouping: How the circuit's qubits are combined into groups.

Returns:
        A list of layers. Each layer has a matrix to apply to each group of
        qubits, and a list of CZs to apply to pairs of qubits crossing
        between groups.


def is_valid_bibtex(reference):

    # str is necessary since pybtex seems to have an issue with unicode. The
    # filter expression removes all non-ASCII characters.
    sio = StringIO(remove_non_ascii(reference))
    parser = bibtex.Parser()
    errors.set_strict_mode(False)
    bib_data = parser.parse_stream(sio)
    return len(bib_data.entries) > 0

Use pybtex to validate that a reference is in proper BibTeX format

Args:
        reference: A String reference in BibTeX format.

Returns:
        Boolean indicating if reference is valid bibtex.


def match(self, message) -> bool:

        if self.to and message.to != self.to:
            return False

        if self.sender and message.sender != self.sender:
            return False

        if self.body and message.body != self.body:
            return False

        if self.thread and message.thread != self.thread:
            return False

        for key, value in self.metadata.items():
            if message.get_metadata(key) != value:
                return False

        logger.debug(f"message matched {self} == {message}")
        return True

Returns wether a message matches with this message or not.
    The message can be a Message object or a Template object.

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

Returns:
        bool: wether the message matches or not


def str2dict_values(str_in):

    tmp_dict = str2dict(str_in)
    if tmp_dict is None:
        return None
    return [tmp_dict[key] for key in sorted(k for k in tmp_dict)]

Extracts the values from a string that represents a dict and returns them
    sorted by key.

Args:
        str_in (string) that contains python dict

Returns:
        (list) with values or None if no valid dict was found

Raises:
        -


def getSchedulesBuffer(self, period_group):

        empty_return = SerialBlock()
        if period_group == ReadSchedules.Schedules_1_To_4:
            return self.m_schd_1_to_4
        elif period_group == ReadSchedules.Schedules_5_To_6:
            return self.m_schd_5_to_6
        else:
            return empty_return

Return the requested tariff schedule :class:`~ekmmeters.SerialBlock` for meter.

Args:
        period_group (int):  A :class:`~ekmmeters.ReadSchedules` value.

Returns:
        SerialBlock: The requested tariff schedules for meter.


def check_hours(tickers, tz_exch, tz_loc=DEFAULT_TZ) -> pd.DataFrame:

    cols = ['Trading_Day_Start_Time_EOD', 'Trading_Day_End_Time_EOD']
    con, _ = create_connection()
    hours = con.ref(tickers=tickers, flds=cols)
    cur_dt = pd.Timestamp('today').strftime('%Y-%m-%d ')
    hours.loc[:, 'local'] = hours.value.astype(str).str[:-3]
    hours.loc[:, 'exch'] = pd.DatetimeIndex(
        cur_dt + hours.value.astype(str)
    ).tz_localize(tz_loc).tz_convert(tz_exch).strftime('%H:%M')

    hours = pd.concat([
        hours.set_index(['ticker', 'field']).exch.unstack().loc[:, cols],
        hours.set_index(['ticker', 'field']).local.unstack().loc[:, cols],
    ], axis=1)
    hours.columns = ['Exch_Start', 'Exch_End', 'Local_Start', 'Local_End']

    return hours

Check exchange hours vs local hours

Args:
        tickers: list of tickers
        tz_exch: exchange timezone
        tz_loc: local timezone

Returns:
        Local and exchange hours


def splat(f: Callable[..., A]) -> Callable[[Iterable], A]:

    def splatted(args):
        return f(*args)

    return splatted

Convert a function taking multiple arguments into a function taking a single iterable argument.

Args:
        f: Any function

Returns:
        A function that accepts a single iterable argument. Each element of this iterable argument is passed as an
        argument to ``f``.

Example:
        $ def f(a, b, c):
        $     return a + b + c
        $
        $ f(1, 2, 3)  # 6
        $ g = splat(f)
        $ g([1, 2, 3])  # 6


def _ParseDistributedTrackingIdentifier(
      self, parser_mediator, uuid_object, origin):

    if uuid_object.version == 1:
      event_data = windows_events.WindowsDistributedLinkTrackingEventData(
          uuid_object, origin)
      date_time = dfdatetime_uuid_time.UUIDTime(timestamp=uuid_object.time)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_CREATION)
      parser_mediator.ProduceEventWithEventData(event, event_data)

    return '{{{0!s}}}'.format(uuid_object)

Extracts data from a Distributed Tracking identifier.

Args:
        parser_mediator (ParserMediator): mediates interactions between parsers
        and other components, such as storage and dfvfs.
        uuid_object (uuid.UUID): UUID of the Distributed Tracking identifier.
        origin (str): origin of the event (event source).

Returns:
        str: UUID string of the Distributed Tracking identifier.


def add_jpeg_decoding(module_spec):

  input_height, input_width = hub.get_expected_image_size(module_spec)
  input_depth = hub.get_num_image_channels(module_spec)
  jpeg_data = tf.placeholder(tf.string, name='DecodeJPGInput')
  decoded_image = tf.image.decode_jpeg(jpeg_data, channels=input_depth)
  # Convert from full range of uint8 to range [0,1] of float32.
  decoded_image_as_float = tf.image.convert_image_dtype(decoded_image,
                                                        tf.float32)
  decoded_image_4d = tf.expand_dims(decoded_image_as_float, 0)
  resize_shape = tf.stack([input_height, input_width])
  resize_shape_as_int = tf.cast(resize_shape, dtype=tf.int32)
  resized_image = tf.image.resize_bilinear(decoded_image_4d,
                                           resize_shape_as_int)
  return jpeg_data, resized_image

Adds operations that perform JPEG decoding and resizing to the graph..

Args:
        module_spec: The hub.ModuleSpec for the image module being used.

Returns:
        Tensors for the node to feed JPEG data into, and the output of the
        preprocessing steps.


def filter_list_by_indices(lst, indices):

    return [x for i, x in enumerate(lst) if i in indices]

Return a modified list containing only the indices indicated.

Args:
        lst: Original list of values
        indices: List of indices to keep from the original list

Returns:
        list: Filtered list of values


def format(self, *args, **kwargs):

        inplace = kwargs.pop("inplace", False)
        if not inplace:
            return str(self).format(*args, **kwargs)
        self._lines = str(self).format(*args, **kwargs).splitlines()

Format the string representation of the editor.

Args:
        inplace (bool): If True, overwrite editor's contents with formatted contents


def _add_sv_coordinates(self, variant):

        variant.stop_chrom = variant.CHROM
        variant.start = int(variant.POS)

        # If we have a translocation:
        if ':' in variant.ALT:
            other_coordinates = variant.ALT.strip('ACGTN[]').split(':')
            variant.stop_chrom = other_coordinates[0].lstrip('chrCHR')
            other_position = other_coordinates[1]
            # variant.stop = other_position

            #Set 'infinity' to length if translocation
            variant.sv_len = float('inf')
            variant.sv_type = 'BND'
        else:
            variant.sv_len = variant.stop - variant.start

        variant['cytoband_start'] = get_cytoband_coord(
                                        chrom=variant.CHROM,
                                        pos=variant.start
                                        )

        variant['cytoband_stop'] = get_cytoband_coord(
                                    chrom=variant.stop_chrom,
                                    pos=variant.stop
                                    )

Add the neccesary sv coordinates for a variant

Args:
        variant (puzzle.models.variant)


def bearer_auth():

    authorization = request.headers.get("Authorization")
    if not (authorization and authorization.startswith("Bearer ")):
        response = app.make_response("")
        response.headers["WWW-Authenticate"] = "Bearer"
        response.status_code = 401
        return response
    slice_start = len("Bearer ")
    token = authorization[slice_start:]

    return jsonify(authenticated=True, token=token)

Prompts the user for authorization using bearer authentication.
    ---
    tags:
        - Auth
    parameters:
        - in: header
        name: Authorization
        schema:
        type: string
    produces:
        - application/json
    responses:
        200:
        description: Sucessful authentication.
        401:
        description: Unsuccessful authentication.


def remove_father(self, father):

        self._fathers = [x for x in self._fathers if x.node_id != father.node_id]

Remove the father node. Do nothing if the node is not a father

Args:
        fathers: list of fathers to add


def calculate_columns(sequence):

    columns = {}

    for row in sequence:
        for key in row.keys():
            if key not in columns:
                columns[key] = len(key)

            value_length = len(str(row[key]))
            if value_length > columns[key]:
                columns[key] = value_length

    return columns

Find all row names and the maximum column widths.

Args:
        columns (dict): the keys are the column name and the value the max length.

Returns:
        dict: column names (key) and widths (value).


def getmtime(self, path=None, client_kwargs=None, header=None):

        return self._getmtime_from_header(
            self.head(path, client_kwargs, header))

Return the time of last access of path.

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

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


def get_directorship_heads(self, val):

        __ldap_group_ou__ = "cn=groups,cn=accounts,dc=csh,dc=rit,dc=edu"

        res = self.__con__.search_s(
                __ldap_group_ou__,
                ldap.SCOPE_SUBTREE,
                "(cn=eboard-%s)" % val,
                ['member'])

        ret = []
        for member in res[0][1]['member']:
            try:
                ret.append(member.decode('utf-8'))
            except UnicodeDecodeError:
                ret.append(member)
            except KeyError:
                continue

        return [CSHMember(self,
                dn.split('=')[1].split(',')[0],
                True)
                for dn in ret]

Get the head of a directorship

Args:
    val -- the cn of the directorship


def run(self, dag):

        if self.layout is None:
            if self.property_set["layout"]:
                self.layout = self.property_set["layout"]
            else:
                self.layout = Layout.generate_trivial_layout(*dag.qregs.values())

        self.property_set['is_direction_mapped'] = True
        edges = self.coupling_map.get_edges()

        for gate in dag.twoQ_gates():
            physical_q0 = self.layout[gate.qargs[0]]
            physical_q1 = self.layout[gate.qargs[1]]

            if isinstance(gate.op, (CXBase, CnotGate)) and (
                    physical_q0, physical_q1) not in edges:
                self.property_set['is_direction_mapped'] = False
                return

If `dag` is mapped and the direction is correct the property
    `is_direction_mapped` is set to True (or to False otherwise).

Args:
        dag (DAGCircuit): DAG to check.


def add_info_to_exception(err: Exception, info: Dict) -> None:
      # noqa
    if not err.args:
        err.args = ('', )
    err.args += (info, )

Adds an information dictionary to an exception.

    See
    http://stackoverflow.com/questions/9157210/how-do-i-raise-the-same-exception-with-a-custom-message-in-python

Args:
        err: the exception to be modified
        info: the information to add


def sk_log_loss(y_true: Union[List[List[float]], List[List[int]], np.ndarray],
                y_predicted: Union[List[List[float]], List[List[int]], np.ndarray]) -> float:

    return log_loss(y_true, y_predicted)

Calculates log loss.

Args:
        y_true: list or array of true values
        y_predicted: list or array of predicted values

Returns:
        Log loss


def gibbs_binding_energy(self, eads=False):

        n = self.get_unit_primitive_area
        Nads = self.Nads_in_slab

        BE = (self.energy - n * self.clean_entry.energy) / Nads - \
             sum([ads.energy_per_atom for ads in self.adsorbates])
        return BE * Nads if eads else BE

Returns the adsorption energy or Gibb's binding energy
    of an adsorbate on a surface

Args:
    eads (bool): Whether to calculate the adsorption energy
        (True) or the binding energy (False) which is just
        adsorption energy normalized by number of adsorbates.


def pnl(self, account='', modelCode='') -> List[PnL]:

        return [v for v in self.wrapper.pnls.values() if
                (not account or v.account == account) and
                (not modelCode or v.modelCode == modelCode)]

List of subscribed :class:`.PnL` objects (profit and loss),
    optionally filtered by account and/or modelCode.

    The :class:`.PnL` objects are kept live updated.

Args:
        account: If specified, filter for this account name.
        modelCode: If specified, filter for this account model.


def data_period_name_or_description(self, value=None):

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

        self._data_period_name_or_description = value

Corresponds to IDD Field `data_period_name_or_description`

Args:
        value (str): value for IDD Field `data_period_name_or_description`
            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 setModelData(self, editor, model, index):

        model.setData(index, editor.itemText(editor.currentIndex()))

Updates the model after changing data in the editor.

Args:
        editor (QtGui.QComboBox): The current editor for the item. Should be
            a `QtGui.QComboBox` as defined in `createEditor`.
        model (ColumnDtypeModel): The model which holds the displayed data.
        index (QtCore.QModelIndex): The index of the current item of the model.


def from_int(i):

        point = ECPointAffine.from_int(bitcoin_curve, i)
        return PublicKey.from_point(point)

Generates a public key object from an integer.

Note:
        This assumes that the upper 32 bytes of the integer
        are the x component of the public key point and the
        lower 32 bytes are the y component.

Args:
        i (Bignum): A 512-bit integer representing the public
        key point on the secp256k1 curve.

Returns:
        PublicKey: A PublicKey object.


def validate_and_decode(jwt_bu64, cert_obj):

    try:
        return jwt.decode(
            jwt_bu64.strip(), cert_obj.public_key(), algorithms=['RS256'], verify=True
        )
    except jwt.InvalidTokenError as e:
        raise JwtException('Signature is invalid. error="{}"'.format(str(e)))

Validate the JWT and return as a dict.

    - JWTs contain a set of values serialized to a JSON dict. This decodes the JWT and
        returns it as a dict.

Args:
        jwt_bu64: bytes
        The JWT encoded using a a URL safe flavor of Base64.

        cert_obj: cryptography.Certificate
        Public certificate used for signing the JWT (typically the CN cert).

Raises:
        JwtException: If validation fails.

Returns:
        dict: Values embedded in the JWT.


def GetFileObject(self, data_stream_name=''):

    if data_stream_name:
      return None

    return resolver.Resolver.OpenFileObject(
        self.path_spec, resolver_context=self._resolver_context)

Retrieves the file-like object.

Args:
        data_stream_name (Optional[str]): name of the data stream, where an empty
        string represents the default data stream.

Returns:
        FileIO: a file-like object or None if not available.


def encode_message(self, message):

        message.check_initialized()

        return json.dumps(message, cls=MessageJSONEncoder,
                          protojson_protocol=self)

Encode Message instance to JSON string.

Args:
        Message instance to encode in to JSON string.

Returns:
        String encoding of Message instance in protocol JSON format.

Raises:
        messages.ValidationError if message is not initialized.


def decode(self, encoded):

        encoded = super().decode(encoded)
        tokens = [self.itos[index] for index in encoded]
        return self.detokenize(tokens)

Decodes a tensor into a sequence.

Args:
        encoded (torch.Tensor): Encoded sequence.

Returns:
        str: Sequence decoded from ``encoded``.


def absolute_magnitude(self, richness=1, steps=1e4):

        # Using the SDSS g,r -> V from Jester 2005 [astro-ph/0506022]
        # for stars with R-I < 1.15
        # V = g_sdss - 0.59*(g_sdss - r_sdss) - 0.01

        # Create a copy of the isochrone in the SDSS system
        params = {k:v.value for k,v in self._params.items()}
        params.update(band_1='g',band_2='r',survey='sdss')
        iso = self.__class__(**params)

        # g, r are absolute magnitude
        mass_init, mass_pdf, mass_act, sdss_g, sdss_r = iso.sample(mass_steps=steps)
        V = jester_mag_v(sdss_g,sdss_r)

        # Sum the V-band absolute magnitudes
        return sum_mags(V,weights=mass_pdf*richness)

Calculate the absolute visual magnitude (Mv) from the richness
    by transforming the isochrone in the SDSS system and using the
    g,r -> V transform equations from Jester 2005
    [astro-ph/0506022].

    Parameters:
    -----------
    richness : isochrone normalization parameter
    steps    : number of isochrone sampling steps

Returns:
    --------
    abs_mag : Absolute magnitude (Mv)


def _print_task_data(self, task):

    print(' {0:s} ({1:s})'.format(task['name'], task['id']))
    paths = task.get('saved_paths', [])
    if not paths:
      return
    for path in paths:
      if path.endswith('worker-log.txt'):
        continue
      if path.endswith('{0:s}.log'.format(task.get('id'))):
        continue
      if path.startswith('/'):
        continue
      print('   ' + path)

Pretty-prints task data.

Args:
        task: Task dict generated by Turbinia.


def create_schema(host):

    connection = create_blocking_connection(host)
    channel = connection.channel()

    exchange = settings.get_amqp_settings()[host]["exchange"]
    channel.exchange_declare(
        exchange=exchange,
        exchange_type="topic",
        durable=True
    )
    print "Created exchange '%s'." % exchange
    print "Creating queues:"

    queues = settings.get_amqp_settings()[host]["queues"]
    for queue in queues.keys():
        channel.queue_declare(
            queue=queue,
            durable=True,
            # arguments={'x-message-ttl': int(1000 * 60 * 60 * 24)} # :S
        )
        print "\tCreated durable queue '%s'." % queue

    print
    print "Routing exchanges using routing key to queues:"

    for queue in queues.keys():
        channel.queue_bind(
            queue=queue,
            exchange=exchange,
            routing_key=queues[queue]
        )

        print "\tRouting exchange %s['%s'] -> '%s'." % (
            exchange,
            queues[queue],
            queue
        )

Create exchanges, queues and route them.

Args:
        host (str): One of the possible hosts.


def unique_everseen(iterable, key=None):

    seen = set()
    seen_add = seen.add

    if key is None:

        for element in filterfalse(seen.__contains__, iterable):

            seen_add(element)
            yield element

    else:

        for element in iterable:

            k = key(element)

            if k not in seen:

                seen_add(k)
                yield element

The generator to list unique elements, preserving the order. Remember all
    elements ever seen. This was taken from the itertools recipes.

Args:
        iterable (:obj:`iter`): An iterable to process.
        key (:obj:`callable`): Optional function to run when checking
            elements (e.g., str.lower)

Yields:
        The next unique element found.


def delete(self, task_name):

        r = self.gbdx_connection.delete(self._base_url + '/' + task_name)
        raise_for_status(r)

        return r.text

Deletes a GBDX task.

Args:
        task_name (str): Task name.

Returns:
        Response (str).


def setup(self,
            hosts, files, use_tsk,
            reason, grr_server_url, grr_username, grr_password, approvers=None,
            verify=True):

    super(GRRFileCollector, self).setup(
        reason, grr_server_url, grr_username, grr_password,
        approvers=approvers, verify=verify)

    if files is not None:
      self.files = [item.strip() for item in files.strip().split(',')]

    self.hostnames = [item.strip() for item in hosts.strip().split(',')]
    self.use_tsk = use_tsk

Initializes a GRR file collector.

Args:
        hosts: Comma-separated list of hostnames to launch the flow on.
        files: list of file paths.
        use_tsk: toggle for use_tsk flag on GRR flow.
        reason: justification for GRR access.
        grr_server_url: GRR server URL.
        grr_username: GRR username.
        grr_password: GRR password.
        approvers: list of GRR approval recipients.
        verify: boolean, whether to verify the GRR server's x509 certificate.


def run(self):

        while self.should_run:
            try:
                self.logger.debug('Sending heartbeat, seq ' + last_sequence)
                self.ws.send(json.dumps({
                    'op': 1,
                    'd': last_sequence
                }))
            except Exception as e:
                self.logger.error(f'Got error in heartbeat: {str(e)}')
            finally:
                elapsed = 0.0
                while elapsed < self.interval and self.should_run:
                    time.sleep(self.TICK_INTERVAL)
                    elapsed += self.TICK_INTERVAL

Runs the thread

    This method handles sending the heartbeat to the Discord websocket server, so the connection
    can remain open and the bot remain online for those commands that require it to be.

Args:
        None


def get_aws_unique_id(timeout=DEFAULT_AWS_TIMEOUT):

    try:
        resp = requests.get(AWS_ID_URL, timeout=timeout).json()
    except requests.exceptions.ConnectTimeout:
        _logger.warning('Connection timeout when determining AWS unique '
                        'ID. Not using AWS unique ID.')
        return None
    else:
        aws_id = "{0}_{1}_{2}".format(resp['instanceId'], resp['region'],
                                      resp['accountId'])
        _logger.debug('Using AWS unique ID %s.', aws_id)
        return aws_id

Determine the current AWS unique ID

Args:
        timeout (int): How long to wait for a response from AWS metadata IP


def write_bashrc(_path):

    cfg_mounts = CFG["container"]["mounts"].value
    cfg_prefix = CFG["container"]["prefixes"].value

    path.mkfile_uchroot("/etc/portage/bashrc")
    mounts = uchroot.mounts("mnt", cfg_mounts)
    p_paths, p_libs = uchroot.env(cfg_prefix)
    paths, libs = uchroot.env(mounts)

    paths = paths + p_paths
    libs = libs + p_libs

    with open(_path, 'w') as bashrc:
        lines = .format(path.list_to_path(paths), path.list_to_path(libs))

        bashrc.write(lines)

Write a valid gentoo bashrc file to :path:.

Args:
        path - The output path of the make.conf


def __init__(self, webhook_obj, alert_recipient=None):  # pragma: no cover

        logging.Handler.__init__(self)
        self.webhook_obj = webhook_obj
        if not self.webhook_obj: # test if it's configured
            raise Exception('Webhook not configured.')

        self.api_url = webhook_obj.webhook_url
        self.alert_recipient = alert_recipient
        self.alert_length = 0
        if self.alert_recipient:
            self.alert_length = len(self.alert_recipient)

HackyDiscordHandler init

Args:
        webhook_obj (:obj:`DiscordWebhook`): discord webhook has all the info for connection
        alert_recipients (`str`:<@int>, optional): user/group to notify


def random_restore(
    rnd: Optional[tcod.random.Random], backup: tcod.random.Random
) -> None:

    lib.TCOD_random_restore(rnd.random_c if rnd else ffi.NULL, backup.random_c)

Restore a random number generator from a backed up copy.

Args:
        rnd (Optional[Random]): A Random instance, or None to use the default.
        backup (Random): The Random instance which was used as a backup.

    .. deprecated:: 8.4
        You can use the standard library copy and pickle modules to save a
        random state.


def update(field, value, db, collection, match=None):

    c = db[collection]
    match = match if match is not None else {}
    # check MongoDB version to use appropriate update command
    if db.client.server_info()['version'].startswith('2'):
        c.update(match, {'$set': {field: value}}, multi=True)
    else:
        c.update_many(match, {'$set': {field: value}})

Updates MongoDB documents.

    Sets ``field`` equal to ``value`` for all documents that
    meet ``match`` criteria.

Args:
        field (str): Field to update.

        value (str): Update value.

        db (Database): A pymongo Database object.

        collection (str): Collection name.

        match (dict): A dictionary containing the match criteria, for example::

            {'seq_id': {'$in': ['a', 'b', 'c']}, 'cdr3_len': {'$gte': 18}}


def apply(self, data, path=None, applicator=None):

        if applicator:
            applicator.pset = self
        else:
            applicator = Applicator(self)

        return applicator.apply(data, path=path)

Apply permissions in this set to the provided data, effectively
    removing all keys from it are not permissioned to be viewed

Args:
    data -- dict of data

Returns:
    Cleaned data


def ParseBookmarkRow(
      self, parser_mediator, query, row, **unused_kwargs):

    query_hash = hash(query)

    rev_host = self._GetRowValue(query_hash, row, 'rev_host')
    bookmark_type = self._GetRowValue(query_hash, row, 'type')

    event_data = FirefoxPlacesBookmarkEventData()
    event_data.host = rev_host or 'N/A'
    event_data.offset = self._GetRowValue(query_hash, row, 'id')
    event_data.places_title = self._GetRowValue(query_hash, row, 'places_title')
    event_data.query = query
    event_data.title = self._GetRowValue(query_hash, row, 'bookmark_title')
    event_data.type = self._BOOKMARK_TYPES.get(bookmark_type, 'N/A')
    event_data.url = self._GetRowValue(query_hash, row, 'url')
    event_data.visit_count = self._GetRowValue(query_hash, row, 'visit_count')

    timestamp = self._GetRowValue(query_hash, row, 'dateAdded')
    if timestamp:
      date_time = dfdatetime_posix_time.PosixTimeInMicroseconds(
          timestamp=timestamp)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_ADDED)
      parser_mediator.ProduceEventWithEventData(event, event_data)

    timestamp = self._GetRowValue(query_hash, row, 'lastModified')
    if timestamp:
      date_time = dfdatetime_posix_time.PosixTimeInMicroseconds(
          timestamp=timestamp)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_MODIFICATION)
      parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a bookmark row.

Args:
        parser_mediator (ParserMediator): mediates interactions between parsers
        and other components, such as storage and dfvfs.
        query (str): query that created the row.
        row (sqlite3.Row): row.


def get_filelikeobject(filename: str = None,
                       blob: bytes = None) -> BinaryIO:

    if not filename and not blob:
        raise ValueError("no filename and no blob")
    if filename and blob:
        raise ValueError("specify either filename or blob")
    if filename:
        return open(filename, 'rb')
    else:
        return io.BytesIO(blob)

Open a file-like object.

    Guard the use of this function with ``with``.

Args:
        filename: for specifying via a filename
        blob: for specifying via an in-memory ``bytes`` object

Returns:
        a :class:`BinaryIO` object


def from_dict(cls, fields, mapping):

        iterable = [None] * len(fields)
        for key, value in mapping.items():
            try:
                index = fields.index(key)
            except KeyError:
                raise ItsdbError('Invalid field name(s): ' + key)
            iterable[index] = value
        return cls(fields, iterable)

Create a Record from a dictionary of field mappings.

    The *fields* object is used to determine the column indices
    of fields in the mapping.

Args:
        fields: the Relation schema for the table of this record
        mapping: a dictionary or other mapping from field names to
            column values

Returns:
        a :class:`Record` object


def handle_message(self, ch, method, properties, body):

        input = {}
        headers = {}
        try:
            self.sessid = method.routing_key

            input = json_decode(body)
            data = input['data']

            # since this comes as "path" we dont know if it's view or workflow yet
            # TODO: just a workaround till we modify ui to
            if 'path' in data:
                if data['path'] in VIEW_METHODS:
                    data['view'] = data['path']
                else:
                    data['wf'] = data['path']
            session = Session(self.sessid)

            headers = {'remote_ip': input['_zops_remote_ip'],
                       'source': input['_zops_source']}

            if 'wf' in data:
                output = self._handle_workflow(session, data, headers)
            elif 'job' in data:

                self._handle_job(session, data, headers)
                return
            else:
                output = self._handle_view(session, data, headers)

        except HTTPError as e:
            import sys
            if hasattr(sys, '_called_from_test'):
                raise
            output = {"cmd": "error", "error": self._prepare_error_msg(e.message), "code": e.code}
            log.exception("Http error occurred")
        except:
            self.current = Current(session=session, input=data)
            self.current.headers = headers
            import sys
            if hasattr(sys, '_called_from_test'):
                raise
            err = traceback.format_exc()
            output = {"cmd": "error", "error": self._prepare_error_msg(err), "code": 500}
            log.exception("Worker error occurred with messsage body:\n%s" % body)
        if 'callbackID' in input:
            output['callbackID'] = input['callbackID']
        log.info("OUTPUT for %s: %s" % (self.sessid, output))
        output['reply_timestamp'] = time()
        self.send_output(output)

this is a pika.basic_consumer callback
    handles client inputs, runs appropriate workflows and views

Args:
        ch: amqp channel
        method: amqp method
        properties:
        body: message body


def is_valid_nhs_number(n: int) -> bool:
      # noqa
    if not isinstance(n, int):
        log.debug("is_valid_nhs_number: parameter was not of integer type")
        return False

    s = str(n)
    # Not 10 digits long?
    if len(s) != 10:
        log.debug("is_valid_nhs_number: not 10 digits")
        return False

    main_digits = [int(s[i]) for i in range(9)]
    actual_check_digit = int(s[9])  # tenth digit
    expected_check_digit = nhs_check_digit(main_digits)
    if expected_check_digit == 10:
        log.debug("is_valid_nhs_number: calculated check digit invalid")
        return False
    if expected_check_digit != actual_check_digit:
        log.debug("is_valid_nhs_number: check digit mismatch")
        return False
    # Hooray!
    return True

Validates an integer as an NHS number.

Args:
        n: NHS number

Returns:
        valid?

    Checksum details are at
    http://www.datadictionary.nhs.uk/version2/data_dictionary/data_field_notes/n/nhs_number_de.asp


def delete_all(self, filter=None, timeout=-1):

        return self._client.delete_all(filter=filter, timeout=timeout)

Delete an SNMPv3 User based on User name specified in filter. The user will be deleted only if it has no associated destinations.

Args:
        username: ID or URI of SNMPv3 user.
        filter: A general filter/query string to narrow the list of items returned.
                The default is no filter - all resources are returned.

Returns:
        bool: Indicates if the resource was successfully deleted.


def find_all_by_mapreduce_state(cls, mapreduce_state):

    keys = cls.calculate_keys_by_mapreduce_state(mapreduce_state)
    i = 0
    while i < len(keys):
      @db.non_transactional
      def no_tx_get(i):
        return db.get(keys[i:i+cls._MAX_STATES_IN_MEMORY])
      # We need a separate function to so that we can mix non-transactional and
      # use be a generator
      states = no_tx_get(i)
      for s in states:
        i += 1
        if s is not None:
          yield s

Find all shard states for given mapreduce.

Args:
        mapreduce_state: MapreduceState instance

Yields:
        shard states sorted by shard id.


def relevant(symbol, token='', version=''):

    _raiseIfNotStr(symbol)
    return _getJson('stock/' + symbol + '/relevant', token, version)

Same as peers

    https://iexcloud.io/docs/api/#relevant

Args:
        symbol (string); Ticker to request
        token (string); Access token
        version (string); API version

Returns:
        dict: result


def serialize_example(transformed_json_data, features, feature_indices, target_name):

  import six
  import tensorflow as tf
  from trainer import feature_transforms

  line = str(transformed_json_data[target_name][0])
  for name, info in feature_indices:
    if features[name]['transform'] in [feature_transforms.IDENTITY_TRANSFORM,
                                       feature_transforms.SCALE_TRANSFORM]:
      line += ' %d:%s' % (info['index_start'], str(transformed_json_data[name][0]))
    elif features[name]['transform'] in [feature_transforms.ONE_HOT_TRANSFORM,
                                         feature_transforms.MULTI_HOT_TRANSFORM]:
      for i in range(info['size']):
        if i in transformed_json_data[name]:
          line += ' %d:1' % (info['index_start'] + i)
    elif features[name]['transform'] in [feature_transforms.IMAGE_TRANSFORM]:
      for i in range(info['size']):
        line += ' %d:%s' % (info['index_start'] + i, str(transformed_json_data[name][i]))

  return line

Makes an instance of data in libsvm format.

Args:
        transformed_json_data: dict of transformed data.
        features: features config.
        feature_indices: output of feature_transforms.get_transformed_feature_indices()

Returns:
        The text line representation of an instance in libsvm format.


def set_heat_pump_feature(self, device_label, feature):

        response = None
        try:
            response = requests.put(
                urls.set_heatpump_feature(self._giid, device_label, feature),
                headers={
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    'Cookie': 'vid={}'.format(self._vid)})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)

Set heatpump mode

Args:
        feature: 'QUIET', 'ECONAVI', or 'POWERFUL'


def GetMessages(self, files):

    result = {}
    for file_name in files:
      file_desc = self.pool.FindFileByName(file_name)
      for name, msg in file_desc.message_types_by_name.items():
        if file_desc.package:
          full_name = '.'.join([file_desc.package, name])
        else:
          full_name = msg.name
        result[full_name] = self.GetPrototype(
            self.pool.FindMessageTypeByName(full_name))

      # While the extension FieldDescriptors are created by the descriptor pool,
      # the python classes created in the factory need them to be registered
      # explicitly, which is done below.
      #
      # The call to RegisterExtension will specifically check if the
      # extension was already registered on the object and either
      # ignore the registration if the original was the same, or raise
      # an error if they were different.

      for name, extension in file_desc.extensions_by_name.items():
        if extension.containing_type.full_name not in self._classes:
          self.GetPrototype(extension.containing_type)
        extended_class = self._classes[extension.containing_type.full_name]
        extended_class.RegisterExtension(extension)
    return result

Gets all the messages from a specified file.

    This will find and resolve dependencies, failing if the descriptor
    pool cannot satisfy them.

Args:
        files: The file names to extract messages from.

Returns:
        A dictionary mapping proto names to the message classes. This will include
        any dependent messages as well as any messages defined in the same file as
        a specified message.


def enter_diff_mode(self, context_model=None):

        assert not self.diff_mode
        self.diff_mode = True

        if context_model is None:
            self.diff_from_source = True
            self.diff_context_model = self.context_model.copy()
        else:
            self.diff_from_source = False
            self.diff_context_model = context_model

        self.clear()
        self.setColumnCount(5)
        self.refresh()

Enter diff mode.

Args:
        context_model (`ContextModel`): Context to diff against. If None, a
        copy of the current context is used.


def split(s, posix=True):

    if isinstance(s, six.binary_type):
        s = s.decode("utf-8")
    return shlex.split(s, posix=posix)

Split the string s using shell-like syntax.

Args:
        s (str): String to split
        posix (bool): Use posix split

Returns:
        list of str: List of string parts


def find_runner(program):

    if os.path.isfile(program) and not os.access(program, os.X_OK):
        # program is a path to a non-executable file
        try:
            opened = open(program)
        except PermissionError:
            return None
        first_line = opened.readline().strip()
        if first_line.startswith('#!'):
            return shlex.split(first_line[2:])
        if program.endswith('.py'):
            return [sys.executable]
    return None

Return a command that will run program.

Args:
        program: The string name of the program to try to run.

Returns:
        commandline list of strings to run the program (eg. with subprocess.call()) or None


def _ParseVValueString(
      self, parser_mediator, data, user_information_descriptor):

    data_start_offset = (
        user_information_descriptor.offset + self._V_VALUE_STRINGS_OFFSET)
    data_end_offset = data_start_offset + user_information_descriptor.size
    descriptor_data = data[data_start_offset:data_end_offset]

    try:
      username = descriptor_data.decode('utf-16-le')
    except (UnicodeDecodeError, UnicodeEncodeError) as exception:
      username = descriptor_data.decode('utf-16-le', errors='replace')
      parser_mediator.ProduceExtractionWarning((
          'unable to decode V value string with error: {0!s}. Characters '
          'that cannot be decoded will be replaced with "?" or '
          '"\\ufffd".').format(exception))

    return username

Parses a V value string.

Args:
        parser_mediator (ParserMediator): mediates interactions between parsers
        and other components, such as storage and dfvfs.
        data (bytes): Windows Registry V value data.
        user_information_descriptor (user_information_descriptor): V value
        user information descriptor.

Returns:
        str: string value stored in the Windows Registry V value data.


def generate_text(self, generation_type='markov'):

        assert generation_type in ['markov', 'hmm', 'hmm_past']
        if generation_type == "markov":
            return self._text_generator(next_token=self._generate_next_token)
        elif generation_type == "hmm":
            return self._text_generator(next_token=self._generate_next_token_hmm, emit=self._emitHMM)
        elif generation_type == "hmm_past":
            return self._text_generator(next_token=self._generate_next_token_hmm, emit=self._emitHMM_with_past)

Generates sentences from a given corpus

Args:
        generation_type: 'markov' | 'hmm' | 'hmm_past'

Returns:
        Properly formatted string of generated sentences


def __init__(self, structure_matcher=StructureMatcher(
                 comparator=ElementComparator()), symprec=None):

        self.symprec = symprec
        self.structure_list = defaultdict(list)
        if isinstance(structure_matcher, dict):
            self.structure_matcher = StructureMatcher.from_dict(structure_matcher)
        else:
            self.structure_matcher = structure_matcher

Remove duplicate structures based on the structure matcher
    and symmetry (if symprec is given).

Args:
        structure_matcher: Provides a structure matcher to be used for
            structure comparison.
        symprec: The precision in the symmetry finder algorithm if None (
            default value), no symmetry check is performed and only the
            structure matcher is used. A recommended value is 1e-5.


def load(self, source, pause=False):

        self._source = source
        self._load_source(source)
        if pause:
            time.sleep(0.5)  # Wait for the DBus interface to be initialised
            self.pause()

Loads a new source (as a file) from ``source`` (a file path or URL)
    by killing the current ``omxplayer`` process and forking a new one.

Args:
        source (string): Path to the file to play or URL


def track_event(self, name, properties=None, measurements=None):

        data = channel.contracts.EventData()
        data.name = name or NULL_CONSTANT_STRING
        if properties:
            data.properties = properties
        if measurements:
            data.measurements = measurements

        self.track(data, self._context)

Send information about a single event that has occurred in the context of the application.

Args:
        name (str). the data to associate to this event.\n
        properties (dict). the set of custom properties the client wants attached to this data item. (defaults to: None)\n
        measurements (dict). the set of custom measurements the client wants to attach to this data item. (defaults to: None)


def ProcessNewBlock(self, block):

        added = set()
        changed = set()
        deleted = set()

        try:
            # go through the list of transactions in the block and enumerate
            # over their outputs
            for tx in block.FullTransactions:

                for index, output in enumerate(tx.outputs):

                    # check to see if the outputs in the tx are in this wallet
                    state = self.CheckAddressState(output.ScriptHash)

                    if state & AddressState.InWallet > 0:

                        # if it's in the wallet, check to see if the coin exists yet
                        key = CoinReference(tx.Hash, index)

                        # if it exists, update it, otherwise create a new one
                        if key in self._coins.keys():
                            coin = self._coins[key]
                            coin.State |= CoinState.Confirmed
                            changed.add(coin)
                        else:
                            newcoin = Coin.CoinFromRef(coin_ref=key, tx_output=output, state=CoinState.Confirmed, transaction=tx)
                            self._coins[key] = newcoin
                            added.add(newcoin)

                        if state & AddressState.WatchOnly > 0:
                            self._coins[key].State |= CoinState.WatchOnly
                            changed.add(self._coins[key])

            # now iterate over the inputs of the tx and do the same
            for tx in block.FullTransactions:

                for input in tx.inputs:

                    if input in self._coins.keys():
                        if self._coins[input].Output.AssetId == Blockchain.SystemShare().Hash:
                            coin = self._coins[input]
                            coin.State |= CoinState.Spent | CoinState.Confirmed
                            changed.add(coin)

                        else:
                            deleted.add(self._coins[input])
                            del self._coins[input]

            for claimTx in [tx for tx in block.Transactions if tx.Type == TransactionType.ClaimTransaction]:

                for ref in claimTx.Claims:
                    if ref in self._coins.keys():
                        deleted.add(self._coins[ref])
                        del self._coins[ref]

            # update the current height of the wallet
            self._current_height += 1

            # in the case that another wallet implementation needs to do something
            # with the coins that have been changed ( ie persist to db ) this
            # method is called
            self.OnProcessNewBlock(block, added, changed, deleted)

            # this is not necessary at the moment, but any outside process
            # that wants to subscribe to the balance changed event could do
            # so from the BalanceChanged method
            if len(added) + len(deleted) + len(changed) > 0:
                self.BalanceChanged()

        except Exception as e:
            traceback.print_stack()
            traceback.print_exc()
            logger.error("could not process %s " % e)

Processes a block on the blockchain.  This should be done in a sequential order, ie block 4 should be
    only processed after block 3.

Args:
        block: (neo.Core.Block) a block on the blockchain.


def post(self, url, data=None, files=None, headers=None, get_json=True):

        if self.debug:
            print("POST: %s, headers=%s" % (url, headers))

        self.headers = self._get_default_headers()
        if headers is not None:
            self.headers.update(headers)

        response = requests.post(url, headers=self.headers, data=data, auth=self.auth, files=files, verify=self.verify_ssl)
        json_response = self._process_json_response(response)

        return json_response if get_json is True else response

Make POST request to a url

Args:
        url (str): URL to send the request to
        data (dict, optional): Data to send
        files (dict, optional): Files to send with the request
        headers (str, optional): custom headers

Returns:
        A JSON object of the returned response if `get_json` is True,
        Requests' response object otherwise


def __init__(self, dimensions):

    super(LearnableMultivariateNormalDiag, self).__init__()
    with tf.compat.v1.name_scope(self._name):
      self.dimensions = dimensions
      self._mean = tf.compat.v2.Variable(
          tf.random.normal([dimensions], stddev=0.1), name="mean")
      # Initialize the std dev such that it will be close to 1 after a softplus
      # function.
      self._untransformed_stddev = tf.compat.v2.Variable(
          tf.random.normal([dimensions], mean=0.55, stddev=0.1),
          name="untransformed_stddev")

Constructs a learnable multivariate diagonal normal model.

Args:
        dimensions: An integer corresponding to the dimensionality of the
        distribution.


def HandleGetDataMessageReceived(self, payload):

        inventory = IOHelper.AsSerializableWithType(payload, 'neo.Network.Payloads.InvPayload.InvPayload')
        if not inventory:
            return

        for hash in inventory.Hashes:
            hash = hash.encode('utf-8')

            item = None
            # try to get the inventory to send from relay cache

            if hash in self.leader.RelayCache.keys():
                item = self.leader.RelayCache[hash]

            if inventory.Type == InventoryType.TXInt:
                if not item:
                    item, index = BC.Default().GetTransaction(hash)
                if not item:
                    item = self.leader.GetTransaction(hash)
                if item:
                    message = Message(command='tx', payload=item, print_payload=False)
                    self.SendSerializedMessage(message)

            elif inventory.Type == InventoryType.BlockInt:
                if not item:
                    item = BC.Default().GetBlock(hash)
                if item:
                    message = Message(command='block', payload=item, print_payload=False)
                    self.SendSerializedMessage(message)

            elif inventory.Type == InventoryType.ConsensusInt:
                if item:
                    self.SendSerializedMessage(Message(command='consensus', payload=item, print_payload=False))

Process a InvPayload payload.

Args:
        payload (neo.Network.Inventory):


def add_triangle(self, neighbors, color, center=None, opacity=0.4,
                     draw_edges=False, edges_color=[0.0, 0.0, 0.0],
                     edges_linewidth=2):

        points = vtk.vtkPoints()
        triangle = vtk.vtkTriangle()
        for ii in range(3):
            points.InsertNextPoint(neighbors[ii].x, neighbors[ii].y,
                                   neighbors[ii].z)
            triangle.GetPointIds().SetId(ii, ii)
        triangles = vtk.vtkCellArray()
        triangles.InsertNextCell(triangle)

        # polydata object
        trianglePolyData = vtk.vtkPolyData()
        trianglePolyData.SetPoints( points )
        trianglePolyData.SetPolys( triangles )

        # mapper
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInput(trianglePolyData)

        ac = vtk.vtkActor()
        ac.SetMapper(mapper)
        ac.GetProperty().SetOpacity(opacity)
        if color == 'element':
            if center is None:
                raise ValueError(
                    'Color should be chosen according to the central atom, '
                    'and central atom is not provided')
            # If partial occupations are involved, the color of the specie with
            # the highest occupation is used
            myoccu = 0.0
            for specie, occu in center.species.items():
                if occu > myoccu:
                    myspecie = specie
                    myoccu = occu
            color = [i / 255 for i in self.el_color_mapping[myspecie.symbol]]
            ac.GetProperty().SetColor(color)
        else:
            ac.GetProperty().SetColor(color)
        if draw_edges:
            ac.GetProperty().SetEdgeColor(edges_color)
            ac.GetProperty().SetLineWidth(edges_linewidth)
            ac.GetProperty().EdgeVisibilityOn()
        self.ren.AddActor(ac)

Adds a triangular surface between three atoms.

Args:
        atoms: Atoms between which a triangle will be drawn.
        color: Color for triangle as RGB.
        center: The "central atom" of the triangle
        opacity: opacity of the triangle
        draw_edges: If set to True, the a line will be  drawn at each edge
        edges_color: Color of the line for the edges
        edges_linewidth: Width of the line drawn for the edges


def CleanseRawStrings(raw_lines):

  delimiter = None
  lines_without_raw_strings = []
  for line in raw_lines:
    if delimiter:
      # Inside a raw string, look for the end
      end = line.find(delimiter)
      if end >= 0:
        # Found the end of the string, match leading space for this
        # line and resume copying the original lines, and also insert
        # a "" on the last line.
        leading_space = Match(r'^(\s*)\S', line)
        line = leading_space.group(1) + '""' + line[end + len(delimiter):]
        delimiter = None
      else:
        # Haven't found the end yet, append a blank line.
        line = '""'

    # Look for beginning of a raw string, and replace them with
    # empty strings.  This is done in a loop to handle multiple raw
    # strings on the same line.
    while delimiter is None:
      # Look for beginning of a raw string.
      # See 2.14.15 [lex.string] for syntax.
      #
      # Once we have matched a raw string, we check the prefix of the
      # line to make sure that the line is not part of a single line
      # comment.  It's done this way because we remove raw strings
      # before removing comments as opposed to removing comments
      # before removing raw strings.  This is because there are some
      # cpplint checks that requires the comments to be preserved, but
      # we don't want to check comments that are inside raw strings.
      matched = Match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line)
      if (matched and
          not Match(r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//',
                    matched.group(1))):
        delimiter = ')' + matched.group(2) + '"'

        end = matched.group(3).find(delimiter)
        if end >= 0:
          # Raw string ended on same line
          line = (matched.group(1) + '""' +
                  matched.group(3)[end + len(delimiter):])
          delimiter = None
        else:
          # Start of a multi-line raw string
          line = matched.group(1) + '""'
      else:
        break

    lines_without_raw_strings.append(line)

  # TODO(unknown): if delimiter is not None here, we might want to
  # emit a warning for unterminated string.
  return lines_without_raw_strings

Removes C++11 raw strings from lines.

    Before:
        static const char kData[] = R"(
        multi-line string
        )";

    After:
        static const char kData[] = ""
        (replaced by blank line)
        "";

Args:
    raw_lines: list of raw lines.

Returns:
    list of lines with C++11 raw strings replaced by empty strings.


def print_inplace(*args, **kwargs):

    kwargs.setdefault('file', sys.stdout)
    kwargs.setdefault('end', '')
    pos_save(file=kwargs['file'])
    delay = None
    with suppress(KeyError):
        delay = kwargs.pop('delay')
    if delay is None:
        print(*args, **kwargs)
    else:
        for c in kwargs.get('sep', ' ').join(str(a) for a in args):
            kwargs['file'].write(c)
            kwargs['file'].flush()
            sleep(delay)
        if kwargs['end']:
            kwargs['file'].write(kwargs['end'])
    pos_restore(file=kwargs['file'])
    # Must flush to see changes.
    kwargs['file'].flush()

Save cursor position, write some text, and then restore the position.

Args:
        Same as `print()`.

    Keyword Arguments:
        Same as `print()`, except `end` defaults to '' (empty str),
        and these:
            delay : Time in seconds between character writes.


def data_string_compare(db_data, user_data):

        db_data = ''.join(db_data.split())
        user_data = ''.join(user_data.split())
        if operator.eq(db_data, user_data):
            return True
        return False

Validate string removing all white space before comparison.

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

Returns:
        bool: True if the data passed validation.


def __init__(self, datafile, logger, error_handler):

    config = json.loads(datafile)
    self.logger = logger
    self.error_handler = error_handler
    self.version = config.get('version')
    if self.version not in SUPPORTED_VERSIONS:
      raise exceptions.UnsupportedDatafileVersionException(
        enums.Errors.UNSUPPORTED_DATAFILE_VERSION.format(self.version)
      )

    self.account_id = config.get('accountId')
    self.project_id = config.get('projectId')
    self.revision = config.get('revision')
    self.groups = config.get('groups', [])
    self.experiments = config.get('experiments', [])
    self.events = config.get('events', [])
    self.attributes = config.get('attributes', [])
    self.audiences = config.get('audiences', [])
    self.typed_audiences = config.get('typedAudiences', [])
    self.feature_flags = config.get('featureFlags', [])
    self.rollouts = config.get('rollouts', [])
    self.anonymize_ip = config.get('anonymizeIP', False)
    self.bot_filtering = config.get('botFiltering', None)

    # Utility maps for quick lookup
    self.group_id_map = self._generate_key_map(self.groups, 'id', entities.Group)
    self.experiment_key_map = self._generate_key_map(self.experiments, 'key', entities.Experiment)
    self.event_key_map = self._generate_key_map(self.events, 'key', entities.Event)
    self.attribute_key_map = self._generate_key_map(self.attributes, 'key', entities.Attribute)

    self.audience_id_map = self._generate_key_map(self.audiences, 'id', entities.Audience)

    # Conditions of audiences in typedAudiences are not expected
    # to be string-encoded as they are in audiences.
    for typed_audience in self.typed_audiences:
      typed_audience['conditions'] = json.dumps(typed_audience['conditions'])
    typed_audience_id_map = self._generate_key_map(self.typed_audiences, 'id', entities.Audience)
    self.audience_id_map.update(typed_audience_id_map)

    self.rollout_id_map = self._generate_key_map(self.rollouts, 'id', entities.Layer)
    for layer in self.rollout_id_map.values():
      for experiment in layer.experiments:
        self.experiment_key_map[experiment['key']] = entities.Experiment(**experiment)

    self.audience_id_map = self._deserialize_audience(self.audience_id_map)
    for group in self.group_id_map.values():
      experiments_in_group_key_map = self._generate_key_map(group.experiments, 'key', entities.Experiment)
      for experiment in experiments_in_group_key_map.values():
        experiment.__dict__.update({
          'groupId': group.id,
          'groupPolicy': group.policy
        })
      self.experiment_key_map.update(experiments_in_group_key_map)

    self.experiment_id_map = {}
    self.variation_key_map = {}
    self.variation_id_map = {}
    self.variation_variable_usage_map = {}
    for experiment in self.experiment_key_map.values():
      self.experiment_id_map[experiment.id] = experiment
      self.variation_key_map[experiment.key] = self._generate_key_map(
        experiment.variations, 'key', entities.Variation
      )
      self.variation_id_map[experiment.key] = {}
      for variation in self.variation_key_map.get(experiment.key).values():
        self.variation_id_map[experiment.key][variation.id] = variation
        self.variation_variable_usage_map[variation.id] = self._generate_key_map(
          variation.variables, 'id', entities.Variation.VariableUsage
        )

    self.feature_key_map = self._generate_key_map(self.feature_flags, 'key', entities.FeatureFlag)

    # Dict containing map of experiment ID to feature ID.
    # for checking that experiment is a feature experiment or not.
    self.experiment_feature_map = {}
    for feature in self.feature_key_map.values():
      feature.variables = self._generate_key_map(feature.variables, 'key', entities.Variable)

      for exp_id in feature.experimentIds:
        # Add this experiment in experiment-feature map.
        self.experiment_feature_map[exp_id] = [feature.id]

        experiment_in_feature = self.experiment_id_map[exp_id]
        # Check if any of the experiments are in a group and add the group id for faster bucketing later on
        if experiment_in_feature.groupId:
          feature.groupId = experiment_in_feature.groupId
          # Experiments in feature can only belong to one mutex group
          break

    # Map of user IDs to another map of experiments to variations.
    # This contains all the forced variations set by the user
    # by calling set_forced_variation (it is not the same as the
    # whitelisting forcedVariations data structure).
    self.forced_variation_map = {}

ProjectConfig init method to load and set project config data.

Args:
        datafile: JSON string representing the project.
        logger: Provides a log message to send log messages to.
        error_handler: Provides a handle_error method to handle exceptions.


def encipher_shift(plaintext, plain_vocab, shift):

  ciphertext = []
  cipher = ShiftEncryptionLayer(plain_vocab, shift)

  for _, sentence in enumerate(plaintext):
    cipher_sentence = []
    for _, character in enumerate(sentence):
      encrypted_char = cipher.encrypt_character(character)
      cipher_sentence.append(encrypted_char)
    ciphertext.append(cipher_sentence)

  return ciphertext

Encrypt plain text with a single shift layer.

Args:
        plaintext (list of list of Strings): a list of plain text to encrypt.
        plain_vocab (list of Integer): unique vocabularies being used.
        shift (Integer): number of shift, shift to the right if shift is positive.

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


def col_mod(df,col_name,func,*args,**kwargs):

    backup = df[col_name].copy()
    try:
        return_val = func(df,col_name,*args,**kwargs)
        if return_val is not None:
            set_col(df,col_name,return_val)
    except:
        df[col_name] = backup

Changes a column of a DataFrame according to a given function
    Parameters:
    df - DataFrame
        DataFrame to operate on
    col_name - string
        Name of column to modify
    func - function
        The function to use to modify the column


def fit_gaussian(samples, ddof=0):

    if len(samples.shape) == 1:
        return np.mean(samples), np.std(samples, ddof=ddof)
    return np.mean(samples, axis=1), np.std(samples, axis=1, ddof=ddof)

Calculates the mean and the standard deviation of the given samples.

Args:
        samples (ndarray): a one or two dimensional array. If one dimensional we calculate the fit using all
            values. If two dimensional, we fit the Gaussian for every set of samples over the first dimension.
        ddof (int): the difference degrees of freedom in the std calculation. See numpy.


def init_app(self, app):

        app.url_rule_class = partial(NavigationRule, copilot=self)
        app.context_processor(self.inject_context)

Register the extension with the application.

Args:
        app (flask.Flask): The application to register with.


def _prepare_for_training(self, records, mini_batch_size=None, job_name=None):

        num_records = None
        if isinstance(records, list):
            for record in records:
                if record.channel == 'train':
                    num_records = record.num_records
                    break
            if num_records is None:
                raise ValueError('Must provide train channel.')
        else:
            num_records = records.num_records

        # mini_batch_size is a required parameter
        default_mini_batch_size = min(self.DEFAULT_MINI_BATCH_SIZE,
                                      max(1, int(num_records / self.train_instance_count)))
        use_mini_batch_size = mini_batch_size or default_mini_batch_size

        super(PCA, self)._prepare_for_training(records=records, mini_batch_size=use_mini_batch_size, job_name=job_name)

Set hyperparameters needed for training.

Args:
        * records (:class:`~RecordSet`): The records to train this ``Estimator`` on.
        * mini_batch_size (int or None): The size of each mini-batch to use when training. If ``None``, a
            default value will be used.
        * job_name (str): Name of the training job to be created. If not specified, one is generated,
            using the base name given to the constructor if applicable.


def _print_download_progress_msg(self, msg, flush=False):

    if self._interactive_mode():
      # Print progress message to console overwriting previous progress
      # message.
      self._max_prog_str = max(self._max_prog_str, len(msg))
      sys.stdout.write("\r%-{}s".format(self._max_prog_str) % msg)
      sys.stdout.flush()
      if flush:
        print("\n")
    else:
      # Interactive progress tracking is disabled. Print progress to the
      # standard TF log.
      logging.info(msg)

Prints a message about download progress either to the console or TF log.

Args:
        msg: Message to print.
        flush: Indicates whether to flush the output (only used in interactive
            mode).


def remove_hairs_decorator(fn=None, hairs=HAIRS):

    def decorator_wrapper(fn):
        @wraps(fn)
        def decorator(*args, **kwargs):
            out = fn(*args, **kwargs)

            return remove_hairs(out, hairs)

        return decorator

    if fn:
        return decorator_wrapper(fn)

    return decorator_wrapper

Parametrized decorator wrapping the :func:`remove_hairs` function.

Args:
        hairs (str, default HAIRS): List of characters which should be removed.
                                    See :attr:`HAIRS` for details.


def uniquify(seq):

    seen = set()
    seen_add = seen.add
    return [x for x in seq if x not in seen and not seen_add(x)]

Return unique values in a list in the original order. See:  \
    http://www.peterbe.com/plog/uniqifiers-benchmark

Args:
    seq (list): original list.

Returns:
    list: list without duplicates preserving original order.


def get_workflow_outputs(namespace, workspace, submission_id, workflow_id):

    uri = "workspaces/{0}/{1}/".format(namespace, workspace)
    uri += "submissions/{0}/workflows/{1}/outputs".format(submission_id,
                                                            workflow_id)
    return __get(uri)

Request the outputs for a workflow in a submission.

Args:
        namespace (str): project to which workspace belongs
        workspace (str): Workspace name
        submission_id (str): Submission's unique identifier
        workflow_id (str): Workflow's unique identifier.

    Swagger:
        https://api.firecloud.org/#!/Submissions/workflowOutputsInSubmission


def in_labelset(xmrs, nodeids, label=None):

    nodeids = set(nodeids)
    if label is None:
        label = xmrs.ep(next(iter(nodeids))).label
    return nodeids.issubset(xmrs._vars[label]['refs']['LBL'])

Test if all nodeids share a label.

Args:
        nodeids: iterable of nodeids
        label (str, optional): the label that all nodeids must share

Returns:
        bool: `True` if all nodeids share a label, otherwise `False`


def delete(adapter, case_obj, update=False, existing_case=False):

    # This will overwrite the updated case with the previous one
    if update:
        adapter.add_case(existing_case)
    else:
        adapter.delete_case(case_obj)

    for file_type in ['vcf_path','vcf_sv_path']:
        if not case_obj.get(file_type):
            continue
        variant_file = case_obj[file_type]
        # Get a cyvcf2.VCF object
        vcf_obj = get_vcf(variant_file)

        delete_variants(
            adapter=adapter,
            vcf_obj=vcf_obj,
            case_obj=case_obj,
        )

Delete a case and all of it's variants from the database.

Args:
        adapter: Connection to database
        case_obj(models.Case)
        update(bool): If we are in the middle of an update
        existing_case(models.Case): If something failed during an update we need to revert
                                    to the original case


def from_dict(cls, copula_dict):

        instance = cls(copula_dict['copula_type'])
        instance.theta = copula_dict['theta']
        instance.tau = copula_dict['tau']
        return instance

Create a new instance from the given parameters.

Args:
        copula_dict: `dict` with the parameters to replicate the copula.
        Like the output of `Bivariate.to_dict`

Returns:
        Bivariate: Instance of the copula defined on the parameters.


def delete_additional_charge(self, recurring_billing_id):

        fmt = 'recurringBillItems/{}'.format(recurring_billing_id)
        return self.client._delete(self.url + fmt, headers=self.get_headers())

Remove an extra charge from an invoice.

Args:
        recurring_billing_id: Identifier of the additional charge.

    Returns:


def _best_subset(self, n_qubits):

        if n_qubits == 1:
            return np.array([0])

        device_qubits = self.coupling_map.size()

        cmap = np.asarray(self.coupling_map.get_edges())
        data = np.ones_like(cmap[:, 0])
        sp_cmap = sp.coo_matrix((data, (cmap[:, 0], cmap[:, 1])),
                                shape=(device_qubits, device_qubits)).tocsr()
        best = 0
        best_map = None
        # do bfs with each node as starting point
        for k in range(sp_cmap.shape[0]):
            bfs = cs.breadth_first_order(sp_cmap, i_start=k, directed=False,
                                         return_predecessors=False)

            connection_count = 0
            sub_graph = []
            for i in range(n_qubits):
                node_idx = bfs[i]
                for j in range(sp_cmap.indptr[node_idx],
                               sp_cmap.indptr[node_idx + 1]):
                    node = sp_cmap.indices[j]
                    for counter in range(n_qubits):
                        if node == bfs[counter]:
                            connection_count += 1
                            sub_graph.append([node_idx, node])
                            break

            if connection_count > best:
                best = connection_count
                best_map = bfs[0:n_qubits]
                # Return a best mapping that has reduced bandwidth
                mapping = {}
                for edge in range(best_map.shape[0]):
                    mapping[best_map[edge]] = edge
                new_cmap = [[mapping[c[0]], mapping[c[1]]] for c in sub_graph]
                rows = [edge[0] for edge in new_cmap]
                cols = [edge[1] for edge in new_cmap]
                data = [1]*len(rows)
                sp_sub_graph = sp.coo_matrix((data, (rows, cols)),
                                             shape=(n_qubits, n_qubits)).tocsr()
                perm = cs.reverse_cuthill_mckee(sp_sub_graph)
                best_map = best_map[perm]
        return best_map

Computes the qubit mapping with the best connectivity.

Args:
        n_qubits (int): Number of subset qubits to consider.

Returns:
        ndarray: Array of qubits to use for best connectivity mapping.


Creates a line from two points

    From http://stackoverflow.com/a/20679579

Args:
        p1 ([float, float]): x and y coordinates
        p2 ([float, float]): x and y coordinates

Returns:
        (float, float, float): x, y and _


def glob(*args):

    if len(args) is 1 and isinstance(args[0], list):
        args = args[0]
    matches = []
    for pattern in args:
        for item in glob2.glob(pattern):
            if not os.path.isdir(item):
                matches.append(item)
    return matches

Returns list of paths matching one or more wildcard patterns.

Args:
        include_dirs: Include directories in the output


def call(self, x):

    with tf.name_scope("embedding"):
      embeddings = tf.gather(self.shared_weights, x)

      # Scale embedding by the sqrt of the hidden size
      embeddings *= self.hidden_size ** 0.5

      # Create binary array of size [batch_size, length]
      # where 1 = padding, 0 = not padding
      padding = model_utils.get_padding(x)

      # Set all padding embedding values to 0
      embeddings *= tf.expand_dims(1 - padding, -1)
      return embeddings

Get token embeddings of x.

Args:
        x: An int64 tensor with shape [batch_size, length]

Returns:
        embeddings: float32 tensor with shape [batch_size, length, embedding_size]
        padding: float32 tensor with shape [batch_size, length] indicating the
        locations of the padding tokens in x.


def is_mobile(user_agent):

    if user_agent:
        b = reg_b.search(user_agent)
        v = reg_v.search(user_agent[0:4])
        return b or v
    return False

Checks if the user browser from the given user agent is mobile.

Args:
        user_agent: A given user agent.

    Returns: True if the browser from the user agent is mobile.