Python re.subn() Examples

The following are 30 code examples of re.subn(). 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. You may also want to check out all available functions/classes of the module re , or try the search function .
Example #1
Source File: document.py    From armada with Apache License 2.0 6 votes vote down vote up
def resolve_reference_ucp(cls, design_uri):
        """Retrieve artifacts from a UCP service endpoint.

        Return a byte array of the response content. Assumes Keystone
        authentication required.

        :param design_uri: Tuple as returned by urllib.parse for the design
                           reference
        """
        ks_sess = KeystoneUtils.get_session()
        (new_scheme, foo) = re.subn('^[^+]+\+', '', design_uri.scheme)
        url = urllib.parse.urlunparse(
            (new_scheme, design_uri.netloc, design_uri.path, design_uri.params,
             design_uri.query, design_uri.fragment))
        LOG.debug("Calling Keystone session for url %s" % str(url))
        resp = ks_sess.get(url)
        if resp.status_code >= 400:
            raise InvalidPathException(
                "Received error code for reference %s: %s - %s" %
                (url, str(resp.status_code), resp.text))
        return resp.content 
Example #2
Source File: convert_to_tagdata.py    From tudouNLP with MIT License 6 votes vote down vote up
def _parse_text(text: list):
    bises = []
    for line in text:
        # remove POS tag
        line, _ = re.subn('\\n', '', line)
        if line == '' or line == '\n':
            continue
        words = re.split('\s+', line)

        if len(words) > MAX_LEN_SIZE:
            texts = re.split('[。?!,.?!,]/w', line)
            if len(min(texts, key=len)) > MAX_LEN_SIZE:
                continue
            bises.extend(_parse_text(texts))
        else:
            bises.append(_tag(words))
    return bises 
Example #3
Source File: parsing.py    From ChatterBot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def datetime_parsing(text, base_date=datetime.now()):
    """
    Extract datetime objects from a string of text.
    """
    matches = []
    found_array = []

    # Find the position in the string
    for expression, function in regex:
        for match in expression.finditer(text):
            matches.append((match.group(), function(match, base_date), match.span()))

    # Wrap the matched text with TAG element to prevent nested selections
    for match, value, spans in matches:
        subn = re.subn(
            '(?!<TAG[^>]*?>)' + match + '(?![^<]*?</TAG>)', '<TAG>' + match + '</TAG>', text
        )
        text = subn[0]
        is_substituted = subn[1]
        if is_substituted != 0:
            found_array.append((match, value, spans))

    # To preserve order of the match, sort based on the start position
    return sorted(found_array, key=lambda match: match and match[2][0]) 
Example #4
Source File: conanfile.py    From conan-center-index with MIT License 6 votes vote down vote up
def package(self):
        self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
        if self.settings.compiler == "Visual Studio":
            cmake = self._configure_cmake()
            cmake.install()
        else:
            autotools = self._configure_autotools()
            autotools.install()

            os.unlink(os.path.join(self.package_folder, "lib", "libapr-1.la"))
            tools.rmdir(os.path.join(self.package_folder, "build-1"))
            tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))

            apr_rules_mk = os.path.join(self.package_folder, "bin", "build-1", "apr_rules.mk")
            apr_rules_cnt = open(apr_rules_mk).read()
            for key in ("apr_builddir", "apr_builders", "top_builddir"):
                apr_rules_cnt, nb = re.subn("^{}=[^\n]*\n".format(key), "{}=$(_APR_BUILDDIR)\n".format(key), apr_rules_cnt, flags=re.MULTILINE)
                if nb == 0:
                    raise ConanException("Could not find/replace {} in {}".format(key, apr_rules_mk))
            open(apr_rules_mk, "w").write(apr_rules_cnt) 
Example #5
Source File: ner_data_preprocess.py    From bi-lstm-crf with Apache License 2.0 6 votes vote down vote up
def _parse_text(text: list):
    bises = []
    for line in text:
        # remove POS tag
        line, _ = re.subn('\\n', '', line)
        if line == '' or line == '\n':
            continue
        words = re.split('\s+', line)

        if len(words) > MAX_LEN_SIZE:
            texts = re.split('[。?!,.?!,]/w', line)
            if len(min(texts, key=len)) > MAX_LEN_SIZE:
                continue
            bises.extend(_parse_text(texts))
        else:
            bises.append(_tag(words))
    return bises 
Example #6
Source File: specfile.py    From rdopkg with Apache License 2.0 6 votes vote down vote up
def _create_new_magic_comment(self, name, value):
        # check to see if we have any magic comments in right slot
        # after SourceX and before Patch Y - if so insert at begining block
        # otherwise insert a new block as before

        if re.findall(self.RE_IN_MAGIC_COMMENTS, self._txt, flags=re.M):
            self._txt = re.sub(
                self.RE_IN_MAGIC_COMMENTS,
                r'\g<1># %s=%s\n\g<2>' % (name, value),
                self.txt, count=1, flags=re.M)
            return

        self._txt, n = re.subn(
            self.RE_PATCH,
            r'\n#\n# %s=%s\n#\n\g<1>' % (name, value),
            self.txt, count=1, flags=re.M)
        if n != 1:
            self._txt, n = re.subn(
                self.RE_AFTER_SOURCES,
                r'\g<1>#\n# %s=%s\n#\n\n' % (name, value),
                self.txt, count=1, flags=re.M)
            if n != 1:
                raise exception.SpecFileParseError(
                    spec_fn=self.fn,
                    error="Unable to create new #%s magic comment." % name) 
Example #7
Source File: utils.py    From vhdl-style-guide with GNU General Public License v3.0 6 votes vote down vote up
def change_word(oLine, sWord, sNewWord, iMax=1):
    '''
    Changes one word in the line to another.

    Parameters:

      oLine: (line object)

      sWord: (string)

      sNewWord: (string)
    '''
    sLine = oLine.line
    tLine = re.subn(r'\b' + re.escape(sWord) + r'\b', sNewWord, sLine, iMax)
    sLine = tLine[0]
    if tLine[1] == 0:
        tLine = re.subn(' ' + sWord + ';', sNewWord + ';', sLine, iMax)
        sLine = tLine[0]
    if tLine[1] == 0:
        tLine = re.subn(sWord + '$', sNewWord, sLine, iMax)
        sLine = tLine[0]

    sLine = tLine[0]
    oLine.update_line(sLine) 
Example #8
Source File: gitgot.py    From GitGot with GNU Lesser General Public License v3.0 6 votes vote down vote up
def regex_search(checks, repo):
    output = ""
    for line in repo.decoded_content.splitlines():
        for check in checks:
            try:
                line = line.decode('utf-8')
            except AttributeError:
                pass

            try:
                (line, inst) = re.subn(
                    check,
                    bcolors.BOLD + bcolors.OKBLUE + r'\1' + bcolors.ENDC,
                    line)
                if inst > 0:
                    output += "\t" + line + "\n"
                    print("\t", line)
                    break
            except Exception as e:
                print(
                    bcolors.FAIL + "ERROR: ", e, bcolors.ENDC,
                    bcolors.WARNING, "\nCHECK: ", check, bcolors.ENDC,
                    "\nLINE: ", line)
    print(bcolors.HEADER + "End of Matches" + bcolors.ENDC)
    return output 
Example #9
Source File: mrf.py    From pracmln with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _materialize_weights(self, verbose=False):
        '''
        materialize all formula weights.
        '''
        max_weight = 0
        for f in self.formulas:
            if f.weight is not None and f.weight != HARD:
                w = str(f.weight)
                variables = re.findall(r'\$\w+', w)
                for var in variables:
                    try:
                        w, numReplacements = re.subn(r'\%s' % var, self.mln.vars[var], w)
                    except:
                        raise Exception("Error substituting variable references in '%s'\n" % w)
                    if numReplacements == 0:
                        raise Exception("Undefined variable(s) referenced in '%s'" % w)
                w = re.sub(r'domSize\((.*?)\)', r'self.domsize("\1")', w)
                try:
                    f.weight = float(eval(w))
                except:
                    sys.stderr.write("Evaluation error while trying to compute '%s'\n" % w)
                    raise
                max_weight = max(abs(f.weight), max_weight) 
Example #10
Source File: test_re.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_none(self):
        for x in 'compile search match split findall finditer'.split():
            y = getattr(re, x)
            self.assertRaises(TypeError, y, None)
            self.assertRaises(TypeError, y, None, None)
            self.assertRaises(TypeError, y, None, 'abc')
            self.assertRaises(TypeError, y, 'abc', None)

        # Other exceptional input tests
        for x in (re.sub, re.subn):
            self.assertRaises(TypeError, x, 'abc', None, 'abc')
            self.assertRaises(TypeError, x, 'abc', None, None)
            self.assertRaises(TypeError, x, None, 'abc', 'abc')
            self.assertRaises(TypeError, x, 'abc', 'abc', None)

        self.assertRaises(TypeError, re.escape, None) 
Example #11
Source File: rebase_elichika_type_tests.py    From chainer-compiler with MIT License 6 votes vote down vote up
def rebase_testcase(filename, model_name, gen_model_fn):
    model, forward_args = gen_model_fn()
    id2type, id2node = type_inference_tools.generate_type_inference_results(
        model, forward_args, is_debug=False)
    sio = six.StringIO()
    type_inference_tools.generate_assertion("id2type", id2type, id2node, sio)

    with open(filename) as f:
        code = f.read()

    begin_marker = '# === BEGIN ASSERTIONS for {} ==='.format(model_name)
    end_marker = '# === END ASSERTIONS for {} ==='.format(model_name)
    regexp = begin_marker + '.*?' + end_marker
    new_assertions = begin_marker + '\n' + sio.getvalue() + ' ' * 8 + end_marker
    code, num_replaced = re.subn(regexp, new_assertions, code,
                                 count=1, flags=re.DOTALL | re.MULTILINE)
    if not num_replaced:
        raise RuntimeError('No assertion markers for {}'.format(model_name))

    with open(filename, 'w') as f:
        f.write(code) 
Example #12
Source File: test_re.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_subn(self):
        # subn
        tup = re.subn("ab", "cd", "abababababab")
        self.assertTrue(tup == ('cdcdcdcdcdcd', 6))
        tup = re.subn("ab", "cd", "abababababab", 0)
        self.assertTrue(tup == ('cdcdcdcdcdcd', 6))
        tup = re.subn("ab", "cd", "abababababab", 1)
        self.assertTrue(tup == ('cdababababab', 1))
        tup = re.subn("ab", "cd", "abababababab", 10)
        self.assertTrue(tup == ('cdcdcdcdcdcd', 6))
        tup = re.subn("ababab", "cd", "ab", 10)
        self.assertTrue(tup == ('ab', 0))
        tup = re.subn("ababab", "cd", "ab")
        self.assertTrue(tup == ('ab', 0))

        tup = re.subn("(ab)*", "cd", "abababababab", 10)
        self.assertTrue(tup == ('cd', 1))
        tup = re.subn("(ab)?", "cd", "abababababab", 10)
        self.assertTrue(tup == ('cdcdcdcdcdcd', 6)) 
Example #13
Source File: document.py    From armada with Apache License 2.0 6 votes vote down vote up
def resolve_reference_ucp(cls, design_uri):
        """Retrieve artifacts from a Airship service endpoint.

        Return a byte array of the response content. Assumes Keystone
        authentication required.

        :param design_uri: Tuple as returned by urllib.parse for the design
                           reference
        """
        ks_sess = ks_utils.get_keystone_session()
        (new_scheme, foo) = re.subn(r'^[^+]+\+', '', design_uri.scheme)
        url = urllib.parse.urlunparse(
            (
                new_scheme, design_uri.netloc, design_uri.path,
                design_uri.params, design_uri.query, design_uri.fragment))
        LOG.debug("Calling Keystone session for url %s" % str(url))
        resp = ks_sess.get(url)
        if resp.status_code >= 400:
            raise InvalidPathException(
                "Received error code for reference %s: %s - %s" %
                (url, str(resp.status_code), resp.text))
        return resp.content 
Example #14
Source File: resolver.py    From drydock with Apache License 2.0 6 votes vote down vote up
def resolve_reference_ucp(cls, design_uri):
        """Retrieve artifacts from a Airship service endpoint.

        Return a byte array of the response content. Assumes Keystone
        authentication required.

        :param design_uri: Tuple as returned by urllib.parse for the design reference
        """
        ks_sess = KeystoneUtils.get_session()
        (new_scheme, foo) = re.subn(r'^[^+]+\+', '', design_uri.scheme)
        url = urllib.parse.urlunparse(
            (new_scheme, design_uri.netloc, design_uri.path, design_uri.params,
             design_uri.query, design_uri.fragment))
        LOG.debug("Calling Keystone session for url %s" % str(url))
        resp = ks_sess.get(url, timeout=get_client_timeouts())
        if resp.status_code >= 400:
            raise errors.InvalidDesignReference(
                "Received error code for reference %s: %s - %s" %
                (url, str(resp.status_code), resp.text))
        return resp.content 
Example #15
Source File: hashes.py    From CDTB with GNU Lesser General Public License v3.0 6 votes vote down vote up
def substitute_skin_numbers(self):
        """Replace skinNN, multiple combinaisons"""

        characters = {}  # {char: ({skin}, {(format, N})}
        regex = re.compile(r'/characters/([^/]+)/skins/(base|skin\d+)/')
        for p in self.known.values():
            m = regex.search(p)
            if not m:
                continue
            char, skin = m.groups()
            if m.group(1) == 'sightward':
                continue
            c = characters.setdefault(char, (set(), set()))
            c[0].add(skin)
            c[1].add(re.subn(r'(?:base|skin\d+)', '%s', p))

        # generate all combinations
        logger.debug(f"substitute skin numbers: {len(characters)} characters")
        for char, (skins, formats) in progress_iterator(characters.items(), lambda v: v[0]):
            for fmt, nocc in formats:
                self.check_iter(fmt % p for p in itertools.combinations(skins, nocc)) 
Example #16
Source File: test_re.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_none(self):
        for x in 'compile search match split findall finditer'.split():
            y = getattr(re, x)
            self.assertRaises(TypeError, y, None)
            self.assertRaises(TypeError, y, None, None)
            self.assertRaises(TypeError, y, None, 'abc')
            self.assertRaises(TypeError, y, 'abc', None)

        # Other exceptional input tests
        for x in (re.sub, re.subn):
            self.assertRaises(TypeError, x, 'abc', None, 'abc')
            self.assertRaises(TypeError, x, 'abc', None, None)
            self.assertRaises(TypeError, x, None, 'abc', 'abc')
            self.assertRaises(TypeError, x, 'abc', 'abc', None)

        self.assertRaises(TypeError, re.escape, None) 
Example #17
Source File: test_re.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_subn(self):
        # subn
        tup = re.subn("ab", "cd", "abababababab")
        self.assertTrue(tup == ('cdcdcdcdcdcd', 6))
        tup = re.subn("ab", "cd", "abababababab", 0)
        self.assertTrue(tup == ('cdcdcdcdcdcd', 6))
        tup = re.subn("ab", "cd", "abababababab", 1)
        self.assertTrue(tup == ('cdababababab', 1))
        tup = re.subn("ab", "cd", "abababababab", 10)
        self.assertTrue(tup == ('cdcdcdcdcdcd', 6))
        tup = re.subn("ababab", "cd", "ab", 10)
        self.assertTrue(tup == ('ab', 0))
        tup = re.subn("ababab", "cd", "ab")
        self.assertTrue(tup == ('ab', 0))

        tup = re.subn("(ab)*", "cd", "abababababab", 10)
        self.assertTrue(tup == ('cd', 1))
        tup = re.subn("(ab)?", "cd", "abababababab", 10)
        self.assertTrue(tup == ('cdcdcdcdcdcd', 6)) 
Example #18
Source File: baseutil.py    From SalesforceXyTools with Apache License 2.0 6 votes vote down vote up
def del_comment(soql):
    result = soql
    if soql:
        # TODO
        # soql = soql.strip().replace('\t', ' ').replace('\r\n', ' ').replace('\n', ' ')
        soql = soql.strip().replace('\t', ' ')
        
        # delete // comment
        result1, number = re.subn("//.*", "", soql)
        # delete /**/ comment
        result, number = re.subn("/\*([\s|\S]*?)\*/", "", result1, flags=re.M)
        result = result.strip()
    # show_in_panel(result)

    return result


# get sobject name from soql 
Example #19
Source File: mrf.py    From pracmln with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _materialize_weights(self, verbose=False):
        '''
        materialize all formula weights.
        '''
        max_weight = 0
        for f in self.formulas:
            if f.weight is not None and f.weight != HARD:
                w = str(f.weight)
                variables = re.findall(r'\$\w+', w)
                for var in variables:
                    try:
                        w, numReplacements = re.subn(r'\%s' % var, self.mln.vars[var], w)
                    except:
                        raise Exception("Error substituting variable references in '%s'\n" % w)
                    if numReplacements == 0:
                        raise Exception("Undefined variable(s) referenced in '%s'" % w)
                w = re.sub(r'domSize\((.*?)\)', r'self.domsize("\1")', w)
                try:
                    f.weight = float(eval(w))
                except:
                    sys.stderr.write("Evaluation error while trying to compute '%s'\n" % w)
                    raise
                max_weight = max(abs(f.weight), max_weight) 
Example #20
Source File: test_re.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_re_subn(self):
        self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2))
        self.assertEqual(re.subn("b+", "x", "bbbb BBBB"), ('x BBBB', 1))
        self.assertEqual(re.subn("b+", "x", "xyz"), ('xyz', 0))
        self.assertEqual(re.subn("b*", "x", "xyz"), ('xxxyxzx', 4))
        self.assertEqual(re.subn("b*", "x", "xyz", count=2), ('xxxyz', 2)) 
Example #21
Source File: test_re.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_large_subn(self, size):
        # Issue #10182: indices were 32-bit-truncated.
        s = 'a' * size
        r, n = re.subn('', '', s)
        self.assertEqual(r, s)
        self.assertEqual(n, size + 1) 
Example #22
Source File: from_template.py    From pySINDy with MIT License 5 votes vote down vote up
def find_and_remove_repl_patterns(astr):
    names = find_repl_patterns(astr)
    astr = re.subn(named_re, '', astr)[0]
    return astr, names 
Example #23
Source File: pdb_dataset.py    From DeepVS with Apache License 2.0 5 votes vote down vote up
def cleanAtomName(self, atomName):
        '''
        Remove numbers and quotes from atom names.
        '''
        return subn(self.__number, "", atomName.upper())[0] 
Example #24
Source File: from_template.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def find_and_remove_repl_patterns(astr):
    names = find_repl_patterns(astr)
    astr = re.subn(named_re, '', astr)[0]
    return astr, names 
Example #25
Source File: util.py    From QQZoneMood with MIT License 5 votes vote down vote up
def remove_waste_emoji(text):
    text = re.subn(re.compile('\[em\].*?\[\/em\]'), '', text)[0]
    text = re.subn(re.compile('@\{.*?\}'), '', text)[0]
    return text 
Example #26
Source File: translator.py    From py-translate with Apache License 2.0 5 votes vote down vote up
def push_url(interface):
    '''
    Decorates a function returning the url of translation API.
    Creates and maintains HTTP connection state

    Returns a dict response object from the server containing the translated
    text and metadata of the request body

    :param interface: Callable Request Interface
    :type interface: Function
    '''

    @functools.wraps(interface)
    def connection(*args, **kwargs):
        """
        Extends and wraps a HTTP interface.

        :return: Response Content
        :rtype: Dictionary
        """
        session = Session()
        session.mount('http://',  HTTPAdapter(max_retries=2))
        session.mount('https://', HTTPAdapter(max_retries=2))

        request  = Request(**interface(*args, **kwargs))
        prepare  = session.prepare_request(request)
        response = session.send(prepare, verify=True)

        if response.status_code != requests.codes.ok:
            response.raise_for_status()

        cleanup = re.subn(r',(?=,)', '', response.content.decode('utf-8'))[0]

        return json.loads(cleanup.replace(r'\xA0', r' ').replace('[,', '[1,'), encoding='UTF-8')

    return connection 
Example #27
Source File: specfile.py    From rdopkg with Apache License 2.0 5 votes vote down vote up
def set_new_patches(self, fns):
        self.wipe_patches()
        if not fns:
            return
        apply_method = self.patches_apply_method()
        ps = ''
        pa = ''
        for i, pfn in enumerate(fns, start=1):
            ps += "Patch%04d: %s\n" % (i, pfn)
            if apply_method == 'rpm':
                pa += "%%patch%04d -p1\n" % i
        # PatchXXX: lines after Source0 / #patches_base=
        self._txt, n = re.subn(
            self.RE_AFTER_MAGIC_COMMENTS,
            r'\g<1>%s\n' % ps, self.txt, count=1)

        if n != 1:
            m = None
            for m in re.finditer(self.RE_AFTER_SOURCES, self.txt):
                pass
            if not m:
                raise exception.SpecFileParseError(
                    spec_fn=self.fn,
                    error="Failed to append PatchXXXX: lines")
            i = m.end()
            startnl, endnl = '', ''
            if self._txt[i - 2] != '\n':
                startnl += '\n'
            if self._txt[i] != '\n':
                endnl += '\n'
            self._txt = self._txt[:i] + startnl + ps + endnl + self._txt[i:]
        # %patchXXX -p1 lines after "%setup" if needed
        if apply_method == 'rpm':
            self._txt, n = re.subn(
                r'((?:^|\n)%setup[^\n]*\n)\s*',
                r'\g<1>\n%s\n' % pa, self.txt)
            if n == 0:
                raise exception.SpecFileParseError(
                    spec_fn=self.fn,
                    error="Failed to append %patchXXXX lines after %setup") 
Example #28
Source File: from_template.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def find_and_remove_repl_patterns(astr):
    names = find_repl_patterns(astr)
    astr = re.subn(named_re, '', astr)[0]
    return astr, names 
Example #29
Source File: specfile.py    From rdopkg with Apache License 2.0 5 votes vote down vote up
def set_magic_comment(self, name, value):
        """Set a magic comment like # name=value in the spec."""
        present = self.get_magic_comment(name)

        if value is None or value == '':
            print("Dropping")
            # Drop magic comment patches_base and following empty comments
            self._txt = re.sub(
                r'(?:^#)*\s*%s\s*=[^\n]*\n(?:#\n)*' % re.escape(name),
                '', self.txt, flags=re.M)
            return

        if present is None:
            return self._create_new_magic_comment(name, value)
        else:
            # Just replace it
            self._txt, count = re.subn(
                r'(?:#\n)*'
                + r'(^#\s*%s\s*=[\t ]?)[^\n]*\n(?:#\n)*' % re.escape(name),
                r'\g<1>%s\n' % value, self.txt, flags=re.M)

            # if there are duplicates drop one of them
            if count > 1:
                self._txt, count = re.subn(
                    r'(#\s?%s\s?=\s?)\S*' % re.escape(name),
                    '', self.txt, count=count - 1, flags=re.M)
                count = 1
            # check to make sure we have only one
            if count == 0:
                raise exception.SpecFileParseError(
                    spec_fn=self.fn,
                    error="Unable to set #%s" % name)
            elif count > 1:
                raise exception.SpecFileParseError(
                    spec_fn=self.fn,
                    error="Multiple magic comments #{0}".format(name)) 
Example #30
Source File: TrainMood.py    From QQZoneMood with MIT License 5 votes vote down vote up
def remove_waste_emoji(text):
    text = re.subn(re.compile('\[em\].*?\[\/em\]'), '', text)[0]
    text = re.subn(re.compile('@\{.*?\}'), '', text)[0]
    return text