Python remove prefix

52 Python code examples are found related to " remove prefix". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: structure_exporter.py    From morph-net with Apache License 2.0 6 votes vote down vote up
def get_remove_common_prefix_fn(iterable: Iterable[Text]
                               ) -> Callable[[Text], Text]:
  """Obtains a function that removes common prefix.

  Determines if all items in iterable start with the same substring (up to and
  including the first '/'). If so, returns a function str->str that removes
  the prefix of matching length. Otherwise returns identity function.

  Args:
    iterable: strings to process.

  Returns:
    A function that removes the common prefix from a string.
  """
  try:
    first = next(iter(iterable))
  except StopIteration:
    return lambda x: x
  separator_index = first.find('/')
  if separator_index == -1:
    return lambda x: x
  prefix = first[:separator_index + 1]
  if not all(k.startswith(prefix) for k in iterable):
    return lambda x: x
  return lambda item: item[len(prefix):] 
Example 2
Source File: util.py    From ParlAI with MIT License 6 votes vote down vote up
def remove_prefix(utt, prefix):
    """
    Check that utt begins with prefix+" ", and then remove.

    Inputs:
      utt: string
      prefix: string

    Returns:
      new utt: utt with the prefix+" " removed.
    """
    try:
        assert utt[: len(prefix) + 1] == prefix + " "
    except AssertionError as e:
        print("ERROR: utterance '%s' does not start with '%s '" % (utt, prefix))
        print(repr(utt[: len(prefix) + 1]))
        print(repr(prefix + " "))
        raise e
    return utt[len(prefix) + 1 :] 
Example 3
Source File: simulator.py    From artisan with GNU General Public License v3.0 6 votes vote down vote up
def removeEmptyPrefix(self):
        try:
            # select the first BT index not an error value to start with
            start = list(map(lambda i: i != -1, self.temp2)).index(True) 
            self.temp1 = numpy.array(self.temp1[start:])
            self.temp2 = numpy.array(self.temp2[start:])
            self.timex = numpy.array(self.timex[start:])
            for i in range(len(self.extratimex)):
                self.extratemp1[i] = numpy.array(self.extratemp1[i][start:])
                self.extratemp2[i] = numpy.array(self.extratemp2[i][start:])
                self.extratimex[i] = numpy.array(self.extratimex[i][start:])
            # shift timestamps such that they start with 0
            if len(self.timex) > 0:
                self.timex = self.timex - self.timex[0]
            # shift timestamps such that they start with 0
            for i in range(len(self.extratimex)):
                if len(self.extratimex[i]) > 0:
                    self.extratimex[i] = self.extratimex[i] - self.extratimex[i][0]
        except:
            pass 
Example 4
Source File: storage.py    From gglsbl with Apache License 2.0 6 votes vote down vote up
def get_hash_prefix_values_to_remove(self, threat_list, indices):
        log.info('Removing {} records from threat list "{}"'.format(len(indices), str(threat_list)))
        indices = set(indices)
        q = '''SELECT value FROM hash_prefix
                WHERE threat_type=? AND platform_type=? AND threat_entry_type=?
                ORDER BY value
        '''
        params = [threat_list.threat_type, threat_list.platform_type, threat_list.threat_entry_type]
        values_to_remove = []
        with self.get_cursor() as dbc:
            dbc.execute(q, params)
            i = 0
            for h in dbc.fetchall():
                v = bytes(h[0])
                if i in indices:
                    values_to_remove.append(v)
                i += 1
        return values_to_remove 
Example 5
Source File: storage.py    From gglsbl with Apache License 2.0 6 votes vote down vote up
def remove_hash_prefix_indices(self, threat_list, indices):
        """Remove records matching idices from a lexicographically-sorted local threat list."""
        batch_size = 40
        q = '''DELETE FROM hash_prefix
                WHERE threat_type=? AND platform_type=? AND threat_entry_type=? AND value IN ({})
        '''
        prefixes_to_remove = self.get_hash_prefix_values_to_remove(threat_list, indices)
        with self.get_cursor() as dbc:
            for i in range(0, len(prefixes_to_remove), batch_size):
                remove_batch = prefixes_to_remove[i:(i + batch_size)]
                params = [
                    threat_list.threat_type,
                    threat_list.platform_type,
                    threat_list.threat_entry_type
                ] + [sqlite3.Binary(b) for b in remove_batch]
                dbc.execute(q.format(','.join(['?'] * len(remove_batch))), params) 
Example 6
Source File: cfghelpers.py    From ibeis with Apache License 2.0 6 votes vote down vote up
def remove_prefix_hack(cfg, cfgtype, cfg_options, alias_keys):
    if cfgtype is not None and cfgtype in ['qcfg', 'dcfg']:
        for key in list(cfg_options.keys()):
            # check if key is nonstandard
            if not (key in cfg or key in alias_keys):
                # does removing prefix make it stanard?
                prefix = cfgtype[0]
                if key.startswith(prefix):
                    key_ = key[len(prefix):]
                    if key_ in cfg or key_ in alias_keys:
                        # remove prefix
                        cfg_options[key_] = cfg_options[key]
                try:
                    assert key[1:] in cfg or key[1:] in alias_keys, (
                        'key=%r, key[1:] =%r' % (key, key[1:] ))
                except AssertionError as ex:
                    ut.printex(ex, 'Parse Error Customize Cfg Base ',
                               keys=['key', 'cfg', 'alias_keys',
                                     'cfgstr_options', 'cfgtype'])
                    raise
                del cfg_options[key] 
Example 7
Source File: minio.py    From cjworkbench with GNU Affero General Public License v3.0 6 votes vote down vote up
def remove_by_prefix(bucket: str, prefix: str, force=False) -> None:
    """
    Remove all objects in `bucket` whose keys begin with `prefix`.

    If you mean to use a directory-style `prefix` -- that is, one that ends in
    `"/"` -- then use `remove_recursive()` to signal your intent.

    If you really mean to use `prefix=''` -- which will wipe the entire bucket,
    up to 1,000 keys -- pass `force=True`. Otherwise, there is a safeguard
    against `prefix=''` specifically.
    """
    if prefix in ("/", "") and not force:
        raise ValueError("Refusing to remove prefix=/ when force=False")

    # recursive list_objects_v2
    list_response = client.list_objects_v2(Bucket=bucket, Prefix=prefix)
    if "Contents" not in list_response:
        return
    delete_response = client.delete_objects(
        Bucket=bucket,
        Delete={"Objects": [{"Key": o["Key"]} for o in list_response["Contents"]]},
    )
    if "Errors" in delete_response:
        for err in delete_response["Errors"]:
            raise Exception("Error %{Code}s removing %{Key}s: %{Message}" % err) 
Example 8
Source File: file_utils.py    From nucleus7 with Mozilla Public License 2.0 6 votes vote down vote up
def remove_prefix(path: str, prefix: Union[str, None]) -> str:
    """
    Remove the prefix from the file path if file name starts with it

    Parameters
    ----------
    path
        path with prefix
    prefix
        prefix to remove

    Returns
    -------
    name_without_prefix
        name with removed prefix

    """
    if not prefix:
        return path
    directory, basename = os.path.split(path)
    basename, ext = os.path.splitext(basename)
    if basename.startswith(prefix):
        basename = basename.replace(prefix, "", 1)
        path = os.path.join(directory, basename + ext)
    return path 
Example 9
Source File: configure.py    From montydb with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def remove_uri_scheme_prefix(uri_or_dir):
    """Internal function to remove URI scheme prefix from repository path

    Args:
        uri_or_dir (str): Folder path or montydb URI

    Returns:
        str: A repository path without URI scheme prefix

    """
    if uri_or_dir.startswith(URI_SCHEME_PREFIX):
        dirname = uri_or_dir[len(URI_SCHEME_PREFIX):]
    else:
        dirname = uri_or_dir

    return dirname 
Example 10
Source File: models.py    From pygameweb with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def remove_prefix(fname):
    """This removes namespace prefix from all the things in the xml.
    """
    from lxml import etree, objectify
    parser = etree.XMLParser(remove_blank_text=True)
    tree = etree.parse(fname, parser)
    root = tree.getroot()
    for elem in root.getiterator():
        if not hasattr(elem.tag, 'find'):
            continue
        i = elem.tag.find('}')
        if i >= 0:
            elem.tag = elem.tag[i + 1:]
    objectify.deannotate(root, cleanup_namespaces=True)
    # fname_out = fname.replace('.xml', '.out.xml')
    # tree.write(fname_out,
    #            pretty_print=True,
    #            xml_declaration=True,
    #            encoding='UTF-8')
    return tree 
Example 11
Source File: load_helper.py    From SiamMask with MIT License 5 votes vote down vote up
def remove_prefix(state_dict, prefix):
    ''' Old style model is stored with all names of parameters share common prefix 'module.' '''
    logger.info('remove prefix \'{}\''.format(prefix))
    f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
    return {f(key): value for key, value in state_dict.items()} 
Example 12
Source File: simpleBGPSpeaker.py    From simpleRouter with MIT License 5 votes vote down vote up
def remove_prefix(self, ipaddress, netmask, routeDist=None):
        prefix = IPNetwork(ipaddress + '/' + netmask)
        local_prefix = str(prefix.cidr)

        LOG.info("Send BGP UPDATE(withdraw) Message [%s]"%local_prefix)
        self.speaker.prefix_del(local_prefix, routeDist) 
Example 13
Source File: utils.py    From snet-cli with MIT License 5 votes vote down vote up
def remove_http_https_prefix(endpoint):
    """remove http:// or https:// prefix if presented in endpoint"""
    endpoint = endpoint.replace("https://","")
    endpoint = endpoint.replace("http://","")
    return endpoint 
Example 14
Source File: bgprec.py    From vrnetlab with MIT License 5 votes vote down vote up
def remove_prefix(afi, prefix):
    """ Remove a prefix from the database
    """
    c.execute("DELETE FROM received_routes WHERE afi=? AND prefix=?", [afi, prefix])
    conn.commit() 
Example 15
Source File: loader.py    From InsightFace-v2 with Apache License 2.0 5 votes vote down vote up
def remove_prefix(state_dict, prefix):
    ''' Old style model is stored with all names of parameters sharing common prefix 'module.' '''
    # print('remove prefix \'{}\''.format(prefix))
    f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
    return {f(key): value for key, value in state_dict.items()} 
Example 16
Source File: model_load.py    From pysot with Apache License 2.0 5 votes vote down vote up
def remove_prefix(state_dict, prefix):
    ''' Old style model is stored with all names of parameters
    share common prefix 'module.' '''
    logger.info('remove prefix \'{}\''.format(prefix))
    f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
    return {f(key): value for key, value in state_dict.items()} 
Example 17
Source File: utils.py    From ACMN-Pytorch with MIT License 5 votes vote down vote up
def update_opt_remove_prefix(sopt, aopt, prefix = ''):

    aopt = dict(filter(lambda kv : kv[0].startswith(prefix), aopt.__dict__.items())) # remove prefix
    out_opt = { **sopt.__dict__, **aopt }

    return argparse.Namespace(**out_opt) # my_opt = { opt_prefix + '...' } 
Example 18
Source File: containerized_kernel_utils.py    From aws-iot-analytics-notebook-containers with Apache License 2.0 5 votes vote down vote up
def remove_containerized_prefix(containerized_kernel_name):
    if containerized_kernel_name.lower().startswith(CONTAINERIZED_PREFIX):
        return containerized_kernel_name[len(CONTAINERIZED_PREFIX):]
    else:
        return containerized_kernel_name 
Example 19
Source File: lockutils.py    From oslo.concurrency with Apache License 2.0 5 votes vote down vote up
def remove_external_lock_file_with_prefix(lock_file_prefix):
    """Partial object generator for the remove lock file function.

    Redefine remove_external_lock_file_with_prefix in each project like so::

        (in nova/utils.py)
        from oslo_concurrency import lockutils

        _prefix = 'nova'
        synchronized = lockutils.synchronized_with_prefix(_prefix)
        lock = lockutils.lock_with_prefix(_prefix)
        lock_cleanup = lockutils.remove_external_lock_file_with_prefix(_prefix)

        (in nova/foo.py)
        from nova import utils

        @utils.synchronized('mylock')
        def bar(self, *args):
            ...

        def baz(self, *args):
            ...
            with utils.lock('mylock'):
                ...
            ...

        <eventually call lock_cleanup('mylock') to clean up>

    The lock_file_prefix argument is used to provide lock files on disk with a
    meaningful prefix.
    """
    return functools.partial(remove_external_lock_file,
                             lock_file_prefix=lock_file_prefix) 
Example 20
Source File: __init__.py    From ypkg with GNU General Public License v3.0 5 votes vote down vote up
def remove_prefix(fpath, prefix):
    if fpath.startswith(prefix):
        fpath = fpath[len(prefix)+1:]
    if fpath[0] != '/':
        fpath = "/" + fpath
    return fpath 
Example 21
Source File: utils.py    From SiamDW with MIT License 5 votes vote down vote up
def remove_prefix(state_dict, prefix):
    '''
    Old style model is stored with all names of parameters share common prefix 'module.'
    '''
    print('remove prefix \'{}\''.format(prefix))
    f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
    return {f(key): value for key, value in state_dict.items()} 
Example 22
Source File: script_util.py    From language-resources with Apache License 2.0 5 votes vote down vote up
def RemovePrefix(s, prefix):
  if s.startswith(prefix):
    return s[len(prefix):]
  else:
    return s 
Example 23
Source File: __init__.py    From Multilingual_Text_to_Speech with MIT License 5 votes vote down vote up
def remove_dataparallel_prefix(state_dict): 
    """Removes dataparallel prefix of layer names in a checkpoint state dictionary."""
    new_state_dict = OrderedDict()
    for k, v in state_dict.items():
        name = k[7:] if k[:7] == "module." else k
        new_state_dict[name] = v
    return new_state_dict 
Example 24
Source File: utils.py    From steemprojects.com with MIT License 5 votes vote down vote up
def remove_prefix(value):
    value = value.lower()
    for char in CHARS:
        value = value.replace("{0}{1}".format(settings.PACKAGINATOR_SEARCH_PREFIX.lower(), char), "")
    return value 
Example 25
Source File: tabular.py    From asammdf with GNU Lesser General Public License v3.0 5 votes vote down vote up
def remove_prefix_changed(self, state):

        if state == QtCore.Qt.Checked:
            self.prefix.setEnabled(True)
            prefix = self.prefix.currentText()
            dim = len(prefix)
            names = [
                name[dim:] if name.startswith(prefix) else name
                for name in self.header_names
            ]
            self.tree.setHeaderLabels(names)
        else:
            self.prefix.setEnabled(False)
            self.tree.setHeaderLabels(self.header_names) 
Example 26
Source File: utils.py    From Joy_QA_Platform with Apache License 2.0 5 votes vote down vote up
def remove_prefix(text, prefix):
    """ remove prefix from text
    """
    if text.startswith(prefix):
        return text[len(prefix):]
    return text 
Example 27
Source File: gen-enums.py    From pyvips with MIT License 5 votes vote down vote up
def remove_prefix(enum_str):
    prefix = 'Vips'

    if enum_str.startswith(prefix):
        return enum_str[len(prefix):]

    return enum_str 
Example 28
Source File: path_parsing.py    From rucio with Apache License 2.0 5 votes vote down vote up
def remove_prefix(prefix, path):
    iprefix = iter(prefix)
    ipath = iter(path)
    try:
        cprefix = next(iprefix)
        cpath = next(ipath)
    except StopIteration:
        # Either the path or the prefix is empty
        return path
    while cprefix != cpath:
        try:
            cprefix = next(iprefix)
        except StopIteration:
            # No parts of the prefix are part of the path
            return path

    while cprefix == cpath:
        cprefix = next(iprefix, None)
        try:
            cpath = next(ipath)
        except StopIteration:
            # The path is a subset of the prefix
            return []

    if cprefix is not None:
        # If the prefix is not depleted maybe it is only a coincidence
        # in one of the components of the paths: return the path as is.
        return path

    rest = list(ipath)
    rest.insert(0, cpath)
    return rest 
Example 29
Source File: llvm.py    From llvmcpy with MIT License 5 votes vote down vote up
def remove_llvm_prefix(name):
    assert is_llvm_type(name)
    if name.startswith("struct "):
        name = name[len("struct "):]
    name = name[len("LLVM"):]
    if name.startswith("Opaque"):
        name = name[len("Opaque"):]
    return name 
Example 30
Source File: utils.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def remove_prefix(string, prefix):
  """Strips the prefix from a string."""
  if string.startswith(prefix):
    return string[len(prefix):]

  return string 
Example 31
Source File: issue_tracker.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def remove_by_prefix(self, prefix):
    """Remove labels with a given prefix."""
    for item in list(self):
      if item.lower().startswith(prefix.lower()):
        self.remove(item) 
Example 32
Source File: utils.py    From notion-py with MIT License 5 votes vote down vote up
def remove_signed_prefix_as_needed(url):
    if url is None:
        return
    if url.startswith(SIGNED_URL_PREFIX):
        return unquote_plus(url[len(S3_URL_PREFIX) :])
    elif url.startswith(S3_URL_PREFIX_ENCODED):
        parsed = urlparse(url.replace(S3_URL_PREFIX_ENCODED, S3_URL_PREFIX))
        return "{}://{}{}".format(parsed.scheme, parsed.netloc, parsed.path)
    else:
        return url 
Example 33
Source File: utils.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def remove_tf_name_prefix(name):
    """
    Strip ^ from TF's node name.

    Arguments:
        name: TF node name

    Returns:
        string: name with ^ stripped
    """
    return name[1:] if name[0] == "^" else name 
Example 34
Source File: checkpointing_mixin.py    From fastMRI with MIT License 5 votes vote down vote up
def remove_prefix_from_model(model_state_dict):
    prefix = next(iter(model_state_dict.keys())).split('.', 1)[0]
    if all(k.startswith(prefix) for k in model_state_dict.keys()):
        logging.info(f"Removing model prefix '{prefix}' from checkpoint")
        from collections import OrderedDict
        new_state_dict = OrderedDict()
        for k, v in model_state_dict.items():
                name = k.split('.', 1)[1] #k[7:] # remove 'module.'
                new_state_dict[name] = v
        return new_state_dict
    else:
        return model_state_dict 
Example 35
Source File: lempel_ziv_decompress.py    From Python with MIT License 5 votes vote down vote up
def remove_prefix(data_bits: str) -> str:
    """
    Removes size prefix, that compressed file should have
    Returns the result
    """
    counter = 0
    for letter in data_bits:
        if letter == "1":
            break
        counter += 1

    data_bits = data_bits[counter:]
    data_bits = data_bits[counter + 1 :]
    return data_bits 
Example 36
Source File: detect.py    From Face-Detector-1MB-with-landmark with MIT License 5 votes vote down vote up
def remove_prefix(state_dict, prefix):
    ''' Old style model is stored with all names of parameters sharing common prefix 'module.' '''
    print('remove prefix \'{}\''.format(prefix))
    f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
    return {f(key): value for key, value in state_dict.items()} 
Example 37
Source File: util.py    From GCA-Matting with MIT License 5 votes vote down vote up
def remove_prefix_state_dict(state_dict, prefix="module"):
    """
    remove prefix from the key of pretrained state dict for Data-Parallel
    """
    new_state_dict = {}
    first_state_name = list(state_dict.keys())[0]
    if not first_state_name.startswith(prefix):
        for key, value in state_dict.items():
            new_state_dict[key] = state_dict[key].float()
    else:
        for key, value in state_dict.items():
            new_state_dict[key[len(prefix)+1:]] = state_dict[key].float()
    return new_state_dict 
Example 38
Source File: utils.py    From deeper_wider_siamese_trackers with MIT License 5 votes vote down vote up
def remove_prefix(state_dict, prefix):
    ''' Old style model is stored with all names of parameters share common prefix 'module.' '''
    print('remove prefix \'{}\''.format(prefix))
    f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
    return {f(key): value for key, value in state_dict.items()} 
Example 39
Source File: ChatGlobals.py    From Pirates-Online-Rewritten with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def removeThoughtPrefix(message):
    if isThought(message):
        return message[len(ThoughtPrefix):]
    else:
        return message 
Example 40
Source File: identifier_syntax.py    From vint with MIT License 5 votes vote down vote up
def remove_optional_scope_prefix(identifier_value):  # type: (str) -> str
    # Keep scope prefix if the identifier is a symbol table such as "a:" or "g:".
    # Because it is not optional.
    if OptionalScopeSymbolTable.get(identifier_value, False):
        return identifier_value

    return re.sub(optional_scope_prefix_pattern, '', identifier_value) 
Example 41
Source File: State.py    From happy with Apache License 2.0 5 votes vote down vote up
def removeNetworkPrefix(self, network_id, prefix, state=None):
        network_record = self.getNetwork(network_id, state)
        if "prefix" in network_record.keys():
            if prefix in network_record["prefix"].keys():
                del network_record["prefix"][prefix] 
Example 42
Source File: Spell.py    From logparser with MIT License 5 votes vote down vote up
def removeSeqFromPrefixTree(self, rootn, newCluster):
        parentn = rootn
        seq = newCluster.logTemplate
        seq = [w for w in seq if w != '<*>']

        for tokenInSeq in seq:
            if tokenInSeq in parentn.childD:
                matchedNode = parentn.childD[tokenInSeq]
                if matchedNode.templateNo == 1:
                    del parentn.childD[tokenInSeq]
                    break
                else:
                    matchedNode.templateNo -= 1
                    parentn = matchedNode 
Example 43
Source File: util.py    From cmus-osx with MIT License 5 votes vote down vote up
def remove_prefix(text: str, prefix: str):
    return text[len(prefix) :] if text.startswith(prefix) else text


# https://stackoverflow.com/a/3505826 
Example 44
Source File: 0002_remove-gb-prefix.py    From yournextrepresentative with GNU Affero General Public License v3.0 5 votes vote down vote up
def remove_gb_prefix(apps, schema_editor):
    Election = apps.get_model("elections", "Election")

    for election in Election.objects.filter(slug__in=IDS_TO_ALTER.keys()):
        election.slug = IDS_TO_ALTER[election.slug]
        election.save()