本文整理汇总了Python中re.subn函数的典型用法代码示例。如果您正苦于以下问题:Python subn函数的具体用法?Python subn怎么用?Python subn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了subn函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_potential_addresses
def get_potential_addresses(address):
"""
Get permutations of as many potential addresses as we can make out of
the given address.
Permutations include the address with all punctuation removed, the
address with directions abbreviated, and the address with street types
abbreviated (as they are in the City of New Orleans' addresses
shapefile).
"""
addresses = set()
addresses.add(address.lower())
# Try removing punctuation from the address
for address in list(addresses):
addresses.add(re.subn('[^\s\w]', '', address)[0])
# Try abbreviating directions
for address in list(addresses):
abbreviated = address
for direction, abbr in directions.items():
abbreviated = re.subn(r'\b%s\b' % direction, abbr, abbreviated)[0]
addresses.add(abbreviated)
# Abbreviate street types
for address in list(addresses):
for street_type, abbr in street_types.items():
abbreviated = re.subn(r'\b%s\b' % street_type, abbr, address)[0]
addresses.add(abbreviated)
return tuple(addresses)
示例2: html2utf8
def html2utf8(self,in_html):
in_html = (re.subn(r'<(script).*?</\1>(?s)', '', in_html)[0])
in_html = (re.subn(r'<(style).*?</\1>(?s)', '', in_html)[0])
entitydict = {}
entities = re.finditer('&([^#][A-Za-z]{1,5}?);', in_html)
for x in entities:
key = x.group(0)
if key not in entitydict:
entitydict[key] = htmlentitydefs.name2codepoint[x.group(1)]
entities = re.finditer('&#x([0-9A-Fa-f]{2,2}?);', in_html)
for x in entities:
key = x.group(0)
if key not in entitydict:
entitydict[key] = "%d" % int(key[3:5], 16)
entities = re.finditer('&#(\d{1,5}?);', in_html)
for x in entities:
key = x.group(0)
if key not in entitydict:
entitydict[key] = x.group(1)
if re.search("charset=utf-8", in_html):
for key, codepoint in iteritems(entitydict):
in_html = in_html.replace(key, unichr(int(codepoint)))
self.inhtml = in_html.encode('utf8')
return
for key, codepoint in iteritems(entitydict):
in_html = in_html.replace(key, unichr(int(codepoint)).encode('latin-1', 'ignore'))
self.inhtml = in_html.decode('latin-1').encode('utf8')
示例3: alter_path
def alter_path(self, path, **varargs):
'''Modify the path passed so that it can be executed on the remote host
path = path to modify
regexp_substitution - if true, exclude \g<...> from substitution
'''
regexp_substitution = varargs.get("regexp_substitution", False)
for mapping in self.mappings:
local_directory = mapping.local_directory.value
remote_directory = mapping.remote_directory.value
if regexp_substitution:
local_directory = local_directory.replace("\\", "\\\\")
remote_directory = remote_directory.replace("\\", "\\\\")
if sys.platform.startswith('win'):
# Windows is case-insentitve so do case-insensitve mapping
if path.upper().startswith(local_directory.upper()):
path = (remote_directory +
path[len(local_directory):])
else:
if path.startswith(local_directory):
path = (remote_directory +
path[len(local_directory):])
if self.remote_host_is_windows.value:
path = path.replace('/','\\')
elif (regexp_substitution):
path = re.subn('\\\\\\\\','/',path)[0]
path = re.subn('\\\\(?!g\\<[^>]*\\>)','/',path)[0]
else:
path = path.replace('\\','/')
return path
示例4: retrieveEspnGameIdsForDay
def retrieveEspnGameIdsForDay(date, root_url):
"""
"""
url = root_url + date
url = "http://scores.espn.go.com/nba/scoreboard?date=20150202"
scores_page = soupypages.soupFromUrl(url)
if scores_page['pass']:
scores_page = scores_page['soup']
game_data = scores_page.body.findAll(id="main-container")[0].find('div', class_='scoreboards').attrs['data-data']
game_data = re.subn('true', "'true'", game_data)[0]
game_data = re.subn('false', "'false'", game_data)[0]
game_data = eval(game_data)
game_ids = [game['id'] for game in game_data['events']]
return game_ids
## scores_page = makePage(url, hdr=True)
## if scores_page['pass']:
## game_ids = key_phrase.findall(scores_page['page'])
## return game_ids
else:
err_msg = scores_page['err']
if type(err_msg) == str:
print("Failed to retrieve scores page for %s. Error: %s" %\
(date, err_msg))
else:
print("Failed to retrieve scores page for %s. Unknown error." %\
date)
return []
示例5: replace
def replace(inp):
result = []
for f in inp:
f = f.decode("utf-8")
#f = re.sub(r"([^\\])([_|\^])", r"\1\\\2", f) # do not require backslash in front of ^ or _
#f = re.sub(r"^([_|\^])", r"\\\1", f)
# escape combining marks with a space after the backslash
for c in combiningmarks:
offset = 0
for s in re.finditer(c[0], f):
f = f[:s.start()+1+offset] + " " + f[s.start()+1+offset:]
offset += 1
for r in replacements:
f = re.sub(r[0], r[1], f)
# expand groups of subscripts: \_{01234}
offset = 0
#for s in re.finditer(r"\\_\{[^\}]+\}", f):
for s in re.finditer(ur"_\{[0-9\+-=\(\)<>\-aeoxjhklmnpstiruv\u03B2\u03B3\u03C1\u03C6\u03C7]+\}", f):
newstring,n = re.subn(ur"([0-9\+-=\(\)<>\-aeoxjhklmnpstiruv\u03B2\u03B3\u03C1\u03C6\u03C7])", r"_\1", s.group(0)[2:-1])
f = f[:s.start()+offset] + newstring + f[s.end()+offset:]
offset += n*2 - (n + 3)
# expand groups of superscripts: \^{01234}
offset = 0
#for s in re.finditer(r"\\\^\{[^\}]+\}", f):
for s in re.finditer(ur"\^\{[0-9\+-=\(\)<>ABDEGHIJKLMNOPRTUWabcdefghijklmnoprstuvwxyz\u03B2\u03B3\u03B4\u03C6\u03C7\u222B]+\}", f):
newstring,n = re.subn(ur"([0-9\+-=\(\)<>ABDEGHIJKLMNOPRTUWabcdefghijklmnoprstuvwxyz\u03B2\u03B3\u03B4\u03C6\u03C7\u222B])", r"^\1", s.group(0)[2:-1])
f = f[:s.start()+offset] + newstring + f[s.end()+offset:]
offset += n*2 - (n + 3)
示例6: set_new_patches
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_PATCHES_BASE, r"\g<1>%s\n" % ps, self.txt, count=1)
if n != 1:
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"
)
示例7: alter_path
def alter_path(self, path, **varargs):
'''Modify the path passed so that it can be executed on the remote host
path = path to modify
regexp_substitution - if true, exclude \g<...> from substitution
'''
for mapping in self.mappings:
if sys.platform.startswith('win'):
# Windows is case-insentitve so do case-insensitve mapping
if path.upper().startswith(mapping.local_directory.value.upper()):
path = (mapping.remote_directory.value +
path[len(mapping.local_directory.value):])
else:
if path.startswith(mapping.local_directory.value):
path = (mapping.remote_directory.value +
path[len(mapping.local_directory.value):])
if self.remote_host_is_windows.value:
path = path.replace('/','\\')
elif (varargs.has_key("regexp_substitution") and
varargs["regexp_substitution"]):
path = re.subn('\\\\\\\\','/',path)[0]
path = re.subn('\\\\(?!g\\<[^>]*\\>)','/',path)[0]
else:
path = path.replace('\\','/')
return path
示例8: chop_end
def chop_end(seqs, model_end, min_modelpositions):
chop_seq_dict = dict()
refseq = seqs[refseq_ID]
model_pos = 0
model_map = dict()
for i in range(len(refseq.seq)):
if refseq.seq[i] != ".":
model_pos += 1
model_map[model_pos] = i
seq_end = model_map[model_end]
for seqid in seqs.keys():
seq = seqs[seqid]
## slice from the end
seqstr = str(seq.seq)[0: seq_end]
if seqid != refseq_ID:
# check if the upper case chars( model position) pass the minimum requirement
if len(re.subn(upper_regex, "", seqstr)[0]) >= min_modelpositions:
chop_seq_dict[seqid] = seqstr
else:
print "%s length %s %s" % (seq.id, len(re.subn(upper_regex, "", seqstr)[0]), re.subn(upper_regex, "", seqstr)[0])
pass
else:
chop_seq_dict[seqid] = seqstr
return chop_seq_dict
示例9: fixrow
def fixrow(data, key, methoddict, verify_rules, equationdict, originaldata):
"""function that i wrote to fix rows that were causing problems after results were created.
after i did this it was clearly a problem because i had no way of knowing what caused the error.
because of this i reworked ther interpolation script to allow for fixes in there rather than after the fact"""
originaldata = {k: v for k, v in chain(originaldata['blockdata'].items(), originaldata['tractdata'].items())}
def subber(matchobject):
return '{:d}'.format(originaldata[matchobject.group(0)])
for table, method in methoddict.items():
testdict = verify_rules[str(method)]
tabledata = {k.split('_')[-1][2:]: v for k, v in data.items() if table in k}
for total, components in sorted(testdict.items(), key = lambda x: int(x[0])):
result, valtotal, valcomps = testrule(tabledata, total, components)
if result == 'mult':
totalname = '{0}_av{1}'.format(table, total)
if table == 'ds185_dt31':
print key, totalname, 'total', re.subn(FINDVARIABLES, subber, equationdict[totalname]['equation'])[0], valtotal
valcomps = {k: v for k, v in valcomps.iteritems() if v == valtotal}
lengthcomps = len(valcomps)
for val in valcomps:
tabledata[val] = float(valtotal) / lengthcomps
fullname = '{0}_av{1}'.format(table, val)
if table == 'ds185_dt31':
print key, fullname, 'value', re.subn(FINDVARIABLES, subber, equationdict[fullname]['equation'])[0], tabledata[val]
data[fullname] = float(valtotal) / lengthcomps
return data
示例10: error_027_mnemonic_codes
def error_027_mnemonic_codes(text):
"""Fix some cases and return (new_text, replacements_count) tuple."""
(text, ignored) = ignore(text, r"https?://\S+")
(text, count1) = re.subn(r"–", "–", text)
(text, count2) = re.subn(r" ", " ", text)
text = deignore(text, ignored)
return (text, count1 + count2)
示例11: error_032_link_two_pipes
def error_032_link_two_pipes(text):
"""Fix some cases and return (new_text, replacements_count) tuple."""
(text, ignored) = ignore(text, r"\[\[\s*:?\s*{}.*?\]\]".format(IMAGE))
(text, count1) = re.subn(r"\[\[([^|\[\]\n]+)\|\|([^|\[\]\n]+)\]\]", "[[\\1|\\2]]", text)
(text, count2) = re.subn(r"\[\[([^|\[\]\n]+)\|([^|\[\]\n]+)\|\]\]", "[[\\1|\\2]]", text)
text = deignore(text, ignored)
return (text, count1 + count2)
示例12: build_template_tags
def build_template_tags(blocks):
content = StringIO()
block_template = '''
{%% block %(name)s %%}
%(comment)s
Default:
%(default_value)s
%(blocks)s
{%% endblock %(name)s %%}
'''
for block in blocks:
subblocks = re.subn("\n", "\n\t", build_template_tags(block.blocks))
if subblocks:
subblocks = subblocks[0]
content.write(block_template % {
'name': block.name,
'comment': block.comment,
'default_value': re.subn("\n", "\n\t", str(block.default_value))[0],
'blocks': subblocks,
})
return content.getvalue()
示例13: shell_remote_execution
def shell_remote_execution(self, tgt, arg, arg1):
''' Shell command execution with parameters '''
if arg in ['disk.usage', 'network.interfaces', 'grains.items', 'test.ping', 'state.running', 'status.meminfo',
'status.uptime', 'service.get_all']:
params = {'client': 'local', 'tgt': tgt, 'fun': arg, 'expr_form': 'list'}
elif arg == 'cmd.run':
params = {'client': 'local', 'tgt': tgt, 'fun': arg, 'arg1': arg1, 'arg2': 'runas=weihu','expr_form': 'list'}
obj = urllib.urlencode(params)
obj, number = re.subn("arg\d", 'arg', obj)
content = self.postRequest(obj)
ret = content['return'][0]
return ret
elif arg in ['cp.get_file', 'cp.get_dir', 'cp.get_url']:
a = arg1.split()
a1 = a[0]
a2 = a[1]
params = {'client': 'local', 'tgt': tgt, 'fun': arg, 'arg1': a1, 'arg2': a2, 'expr_form': 'list'}
obj = urllib.urlencode(params)
obj, number = re.subn("arg\d", 'arg', obj)
content = self.postRequest(obj)
ret = content['return'][0]
return ret
else:
params = {'client': 'local', 'tgt': tgt, 'fun': arg, 'arg': arg1, 'expr_form': 'list'}
obj = urllib.urlencode(params)
content = self.postRequest(obj)
ret = content['return'][0]
return ret
示例14: getDefaultEditionContent
def getDefaultEditionContent():
'''获取标准版文件内容
Returns:
标准版文件内容
'''
partContent = ''
for fileName in devPartFileNameList:
partContent += open(devDirName + os.sep + fileName + '.js', 'r', encoding = encoding).read() + '\n\n'
content = open(devDirName + os.sep + devKFOLFileName + userScriptExt, 'r', encoding = encoding).read()
match = re.search('/\*\s*\{PartFileContent\}\s*\*/', content, flags=re.S | re.I)
if match:
content = content.replace(match.group(0), partContent)
else:
raise Exception('未找到{PartFileContent}占位符')
content, num = re.subn(r'(// @description.+?)(// @include)',
r'\g<1>'
'// @updateURL https://greasyfork.org/scripts/8615-kf-online%E5%8A%A9%E6%89%8B/code/KF%20Online%E5%8A%A9%E6%89%8B.meta.js\n'
'// @downloadURL https://greasyfork.org/scripts/8615-kf-online%E5%8A%A9%E6%89%8B/code/KF%20Online%E5%8A%A9%E6%89%8B.user.js\n'
r'\g<2>',
content,
count=1,
flags=re.S | re.I)
if num == 0: raise NoFoundReplaceStringError('标准版', 1)
content, num = re.subn(r'// @require.+?(// @version)', '\g<1>', content, count=1, flags=re.S | re.I)
if num == 0: raise NoFoundReplaceStringError('标准版', 2)
return content
示例15: subst
def subst(self, replace, replace_with):
"re search and replace on the current node"
nodes = self.current_nodes[:]
check = len(nodes) == 1
a = self.current_attrib
if a:
new, num = re.subn(replace, replace_with, a.value)
if not num:
raise Beep
a = self.model.set_attrib(nodes[0], a.name, new)
self.move_to(nodes[0], a)
else:
self.move_to([])
final = []
for n in nodes:
if n.nodeType == Node.TEXT_NODE:
old = n.data.replace('\n', ' ')
new, num = re.subn(replace, replace_with, old)
if check and not num:
self.move_to(n)
raise Beep
self.model.set_data(n, new)
final.append(n)
elif n.nodeType == Node.ELEMENT_NODE:
old = str(n.nodeName)
new, num = re.subn(replace, replace_with, old)
if check and not num:
self.move_to(n)
raise Beep
new_ns, x = self.model.split_qname(n, new)
final.append(self.model.set_name(n, new_ns, new))
else:
self.move_to(n)
raise Beep
self.move_to(final)