本文整理匯總了Python中re.subn方法的典型用法代碼示例。如果您正苦於以下問題:Python re.subn方法的具體用法?Python re.subn怎麽用?Python re.subn使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類re
的用法示例。
在下文中一共展示了re.subn方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: resolve_reference_ucp
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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
示例2: regex_search
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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
示例3: _parse_text
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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
示例4: package
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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)
示例5: change_word
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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)
示例6: test_none
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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)
示例7: test_subn
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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))
示例8: rebase_testcase
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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)
示例9: del_comment
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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
示例10: substitute_skin_numbers
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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))
示例11: resolve_reference_ucp
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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
示例12: resolve_reference_ucp
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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
示例13: _materialize_weights
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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)
示例14: datetime_parsing
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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])
示例15: _create_new_magic_comment
# 需要導入模塊: import re [as 別名]
# 或者: from re import subn [as 別名]
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)