本文整理匯總了Python中re.M屬性的典型用法代碼示例。如果您正苦於以下問題:Python re.M屬性的具體用法?Python re.M怎麽用?Python re.M使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類re
的用法示例。
在下文中一共展示了re.M屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: find_version
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def find_version(*file_paths):
# Open in Latin-1 so that we avoid encoding errors.
# Use codecs.open for Python 2 compatibility
try:
f = codecs.open(os.path.join(here, *file_paths), 'r', 'latin1')
version_file = f.read()
f.close()
except:
raise RuntimeError("Unable to find version string.")
# The version line must have the form
# __version__ = 'ver'
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
version_file, re.M)
if version_match:
return version_match.group(1)
raise RuntimeError("Unable to find version string.")
# Get the long description from the relevant file
示例2: re_show
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def re_show(regexp, string):
"""
Search C{string} for substrings matching C{regexp} and wrap
the matches with braces. This is convenient for learning about
regular expressions.
@param regexp: The regular expression.
@param string: The string being matched.
@rtype: C{string}
@return: A string with braces surrounding the matched substrings.
"""
print re.compile(regexp, re.M).sub("{\g<0>}", string.rstrip())
##########################################################################
# READ FROM FILE OR STRING
##########################################################################
# recipe from David Mertz
示例3: re_show
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def re_show(regexp, string, left="{", right="}"):
"""
Return a string with markers surrounding the matched substrings.
Search str for substrings matching ``regexp`` and wrap the matches
with braces. This is convenient for learning about regular expressions.
:param regexp: The regular expression.
:type regexp: str
:param string: The string being matched.
:type string: str
:param left: The left delimiter (printed before the matched substring)
:type left: str
:param right: The right delimiter (printed after the matched substring)
:type right: str
:rtype: str
"""
print(re.compile(regexp, re.M).sub(left + r"\g<0>" + right, string.rstrip()))
##########################################################################
# READ FROM FILE OR STRING
##########################################################################
# recipe from David Mertz
示例4: getmatch
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def getmatch(self, haystack):
if not isinstance(haystack, basestring):
return None
flags = 0
if self.flags is not None:
if "i" in self.flags or "I" in self.flags:
flags |= re.I
if "l" in self.flags or "L" in self.flags:
flags |= re.L
if "m" in self.flags or "M" in self.flags:
flags |= re.M
if "s" in self.flags or "S" in self.flags:
flags |= re.S
if "u" in self.flags or "U" in self.flags:
flags |= re.U
if "x" in self.flags or "X" in self.flags:
flags |= re.X
if re.match(self.pattern, haystack, flags=flags) is None:
return None
elif self.to is None:
return Match(haystack, haystack)
else:
return Match(haystack, re.sub(self.pattern, self.to, haystack, flags=flags))
示例5: find_version
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def find_version(*file_paths):
"""Read the version number from a source file.
Why read it, and not import?
see https://groups.google.com/d/topic/pypa-dev/0PkjVpcxTzQ/discussion
"""
# Open in Latin-1 so that we avoid encoding errors.
# Use codecs.open for Python 2 compatibility
with codecs.open(os.path.join(here, *file_paths), 'r', 'latin1') as f:
version_file = f.read()
# The version line must have the form
# __version__ = 'ver'
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
version_file, re.M)
if version_match:
return version_match.group(1)
raise RuntimeError("Unable to find version string.")
示例6: get_incremented_filename
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def get_incremented_filename(filename, namestring):
import re
min_index = 1
pattern = r"(.*?)(\s*)(\d*)$"
basename, space, index_str = re.search(pattern, filename).groups()
search_pattern = fr"^{re.escape(basename)}\s*(\d+)$"
if index_str:
min_index = int(index_str)
zero_padding = len(index_str) if index_str.startswith("0") else 0
naming_pattern = basename + space + "{:0" + str(zero_padding) + "d}"
else:
naming_pattern = basename + " {:02d}"
names = re.finditer(search_pattern, namestring, re.I | re.M)
inds = [int(name.group(1)) for name in names]
max_index = min_index + len(inds)
for i in range(min_index, max_index):
if i not in inds:
return naming_pattern.format(i)
return naming_pattern.format(max_index)
示例7: versions_from_file
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def versions_from_file(filename):
"""Try to determine the version from _version.py if present."""
try:
with open(filename) as f:
contents = f.read()
except EnvironmentError:
raise NotThisMethod("unable to read _version.py")
mo = re.search(
r"version_json = '''\n(.*)''' # END VERSION_JSON", contents, re.M | re.S
)
if not mo:
mo = re.search(
r"version_json = '''\r\n(.*)''' # END VERSION_JSON", contents, re.M | re.S
)
if not mo:
raise NotThisMethod("no version_json in _version.py")
return json.loads(mo.group(1))
示例8: get_version
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def get_version():
"""
Extracts the version number from the version.py file.
"""
VERSION_FILE = '../dynamic_db_router/version.py'
mo = re.search(r'^__version__ = [\'"]([^\'"]*)[\'"]', open(VERSION_FILE, 'rt').read(), re.M)
if mo:
return mo.group(1)
else:
raise RuntimeError('Unable to find version string in {0}.'.format(VERSION_FILE))
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
# -- General configuration ------------------------------------------------
示例9: test_simple_usage
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def test_simple_usage(self):
test_loop = asyncio.get_event_loop()
console = SharedConsole(interval=0.0)
async def add_lines():
console.print("one")
console.print("two")
console.print("3")
try:
1 + "e"
except Exception as e:
console.print_error(e)
console.print_error(e, sys.exc_info()[2])
await asyncio.sleep(0.2)
await console.stop()
with catch_output() as (stdout, stderr):
adder = asyncio.ensure_future(add_lines())
displayer = asyncio.ensure_future(console.display())
test_loop.run_until_complete(asyncio.gather(adder, displayer))
output = stdout.read()
test_loop.close()
self.assertTrue(re.match(OUTPUT, output, re.S | re.M) is not None, output)
示例10: _get_version
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def _get_version():
PATH_TO_INIT_PY = \
os.path.join(
os.path.abspath(os.path.dirname(__file__)),
'aiohttp_jinja2',
'__init__.py'
)
with codecs.open(PATH_TO_INIT_PY, 'r', 'latin1') as fp:
try:
for line in fp.readlines():
if line:
line = line.strip()
version = re.findall(
r"^__version__ = '([^']+)'$", line, re.M
)
if version:
return version[0]
except IndexError:
raise RuntimeError('Unable to determine version.')
示例11: insertVbaCode
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def insertVbaCode(self, targetModule, targetFunction,targetLine, vbaCode):
"""
Insert some code at targetLine (number) at targetFunction in targetModule
"""
logging.debug(" [,] Opening "+ targetFunction + " in " + targetModule + " to inject code...")
f = open(targetModule)
content = f.readlines()
f.close()
for n,line in enumerate(content):
matchObj = re.match( r'.*(Sub|Function)\s+%s\s*\(.*\).*'%targetFunction, line, re.M|re.I)
if matchObj:
logging.debug(" [,] Found " + targetFunction + " ")
content[n+targetLine] = content[n+targetLine]+ vbaCode+"\n"
break
logging.debug(" [,] New content: \n " + "".join(content) + "\n\n ")
f = open(targetModule, 'w')
f.writelines(content)
f.close()
示例12: get_version
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def get_version(string):
""" Parse the version number variable __version__ from a script. """
import re
version_re = r"^__version__ = ['\"]([^'\"]*)['\"]"
version_str = re.search(version_re, string, re.M).group(1)
return version_str
示例13: find_meta
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def find_meta(meta):
"""
Extract __*meta*__ from META_FILE.
"""
meta_match = re.search(r"^__{meta}__ = ['\"]([^'\"]*)['\"]".format(meta=meta), META_FILE, re.M)
if meta_match:
return meta_match.group(1)
raise RuntimeError("Unable to find __{meta}__ string.".format(meta=meta))
# -- Project information -----------------------------------------------------
示例14: find_meta
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def find_meta(meta):
"""
Extract __*meta*__ from META_FILE.
"""
meta_match = re.search(r"^__{meta}__ = ['\"]([^'\"]*)['\"]".format(meta=meta), META_FILE, re.M)
if meta_match:
return meta_match.group(1)
raise RuntimeError("Unable to find __{meta}__ string.".format(meta=meta))
示例15: versions_from_file
# 需要導入模塊: import re [as 別名]
# 或者: from re import M [as 別名]
def versions_from_file(filename):
"""Try to determine the version from _version.py if present."""
try:
with open(filename) as f:
contents = f.read()
except EnvironmentError:
raise NotThisMethod("unable to read _version.py")
mo = re.search(r"version_json = '''\n(.*)''' # END VERSION_JSON",
contents, re.M | re.S)
if not mo:
mo = re.search(r"version_json = '''\r\n(.*)''' # END VERSION_JSON",
contents, re.M | re.S)
if not mo:
raise NotThisMethod("no version_json in _version.py")
return json.loads(mo.group(1))