summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2020-05-10 13:03:39 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-05-11 12:01:28 +0200
commitd6aff369ad33457ae2355b5b210faf1c4890ff35 (patch)
tree4c9d43311078bd81098e8a9fe9ff89fe007e921e /django/utils
parent23f6fbdd93cd668740e3a1cd6d8c8259f380c0fe (diff)
Refs #30116 -- Simplified regex match group access with Match.__getitem__().
The method has been available since Python 3.6. The shorter syntax is also marginally faster.
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/datetime_safe.py2
-rw-r--r--django/utils/http.py14
-rw-r--r--django/utils/jslex.py4
-rw-r--r--django/utils/text.py12
-rw-r--r--django/utils/translation/template.py16
-rw-r--r--django/utils/translation/trans_real.py2
6 files changed, 25 insertions, 25 deletions
diff --git a/django/utils/datetime_safe.py b/django/utils/datetime_safe.py
index 7edd96f2f1..8c87c825d6 100644
--- a/django/utils/datetime_safe.py
+++ b/django/utils/datetime_safe.py
@@ -76,7 +76,7 @@ def strftime(dt, fmt):
return super(type(dt), dt).strftime(fmt)
illegal_formatting = _illegal_formatting.search(fmt)
if illegal_formatting:
- raise TypeError("strftime of dates before 1000 does not handle " + illegal_formatting.group(0))
+ raise TypeError('strftime of dates before 1000 does not handle ' + illegal_formatting[0])
year = dt.year
# For every non-leap year century, advance by
diff --git a/django/utils/http.py b/django/utils/http.py
index 709ce60e1f..c1005458e7 100644
--- a/django/utils/http.py
+++ b/django/utils/http.py
@@ -175,7 +175,7 @@ def parse_http_date(date):
else:
raise ValueError("%r is not in a valid HTTP date format" % date)
try:
- year = int(m.group('year'))
+ year = int(m['year'])
if year < 100:
current_year = datetime.datetime.utcnow().year
current_century = current_year - (current_year % 100)
@@ -185,11 +185,11 @@ def parse_http_date(date):
year += current_century - 100
else:
year += current_century
- month = MONTHS.index(m.group('mon').lower()) + 1
- day = int(m.group('day'))
- hour = int(m.group('hour'))
- min = int(m.group('min'))
- sec = int(m.group('sec'))
+ month = MONTHS.index(m['mon'].lower()) + 1
+ day = int(m['day'])
+ hour = int(m['hour'])
+ min = int(m['min'])
+ sec = int(m['sec'])
result = datetime.datetime(year, month, day, hour, min, sec)
return calendar.timegm(result.utctimetuple())
except Exception as exc:
@@ -266,7 +266,7 @@ def parse_etags(etag_str):
else:
# Parse each ETag individually, and return any that are valid.
etag_matches = (ETAG_MATCH.match(etag.strip()) for etag in etag_str.split(','))
- return [match.group(1) for match in etag_matches if match]
+ return [match[1] for match in etag_matches if match]
def quote_etag(etag_str):
diff --git a/django/utils/jslex.py b/django/utils/jslex.py
index b2ff689f33..1e3a8eb532 100644
--- a/django/utils/jslex.py
+++ b/django/utils/jslex.py
@@ -62,7 +62,7 @@ class Lexer:
for match in regexes[state].finditer(text, start):
name = match.lastgroup
tok = toks[name]
- toktext = match.group(name)
+ toktext = match[name]
start += len(toktext)
yield (tok.name, toktext)
@@ -192,7 +192,7 @@ def prepare_js_for_gettext(js):
"""
def escape_quotes(m):
"""Used in a regex to properly escape double quotes."""
- s = m.group(0)
+ s = m[0]
if s == '"':
return r'\"'
else:
diff --git a/django/utils/text.py b/django/utils/text.py
index 110e088ddf..fb5f6298c4 100644
--- a/django/utils/text.py
+++ b/django/utils/text.py
@@ -175,14 +175,14 @@ class Truncator(SimpleLazyObject):
# Checked through whole string
break
pos = m.end(0)
- if m.group(1):
+ if m[1]:
# It's an actual non-HTML word or char
current_len += 1
if current_len == truncate_len:
end_text_pos = pos
continue
# Check for tag
- tag = re_tag.match(m.group(0))
+ tag = re_tag.match(m[0])
if not tag or current_len >= truncate_len:
# Don't worry about non tags or tags after our truncate point
continue
@@ -334,11 +334,11 @@ def smart_split(text):
['A', '"\\"funky\\" style"', 'test.']
"""
for bit in smart_split_re.finditer(str(text)):
- yield bit.group(0)
+ yield bit[0]
def _replace_entity(match):
- text = match.group(1)
+ text = match[1]
if text[0] == '#':
text = text[1:]
try:
@@ -348,12 +348,12 @@ def _replace_entity(match):
c = int(text)
return chr(c)
except ValueError:
- return match.group(0)
+ return match[0]
else:
try:
return chr(html.entities.name2codepoint[text])
except KeyError:
- return match.group(0)
+ return match[0]
_entity_re = _lazy_re_compile(r"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));")
diff --git a/django/utils/translation/template.py b/django/utils/translation/template.py
index fee66dac9a..778faa770e 100644
--- a/django/utils/translation/template.py
+++ b/django/utils/translation/template.py
@@ -165,16 +165,16 @@ def templatize(src, origin=None):
bmatch = block_re.match(t.contents)
cmatches = constant_re.findall(t.contents)
if imatch:
- g = imatch.group(1)
+ g = imatch[1]
if g[0] == '"':
g = g.strip('"')
elif g[0] == "'":
g = g.strip("'")
g = g.replace('%', '%%')
- if imatch.group(2):
+ if imatch[2]:
# A context is provided
- context_match = context_re.match(imatch.group(2))
- message_context = context_match.group(1)
+ context_match = context_re.match(imatch[2])
+ message_context = context_match[1]
if message_context[0] == '"':
message_context = message_context.strip('"')
elif message_context[0] == "'":
@@ -188,10 +188,10 @@ def templatize(src, origin=None):
elif bmatch:
for fmatch in constant_re.findall(t.contents):
out.write(' _(%s) ' % fmatch)
- if bmatch.group(1):
+ if bmatch[1]:
# A context is provided
- context_match = context_re.match(bmatch.group(1))
- message_context = context_match.group(1)
+ context_match = context_re.match(bmatch[1])
+ message_context = context_match[1]
if message_context[0] == '"':
message_context = message_context.strip('"')
elif message_context[0] == "'":
@@ -212,7 +212,7 @@ def templatize(src, origin=None):
parts = t.contents.split('|')
cmatch = constant_re.match(parts[0])
if cmatch:
- out.write(' _(%s) ' % cmatch.group(1))
+ out.write(' _(%s) ' % cmatch[1])
for p in parts[1:]:
if p.find(':_(') >= 0:
out.write(' %s ' % p.split(':', 1)[1])
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
index f8089dca54..eed4705f6e 100644
--- a/django/utils/translation/trans_real.py
+++ b/django/utils/translation/trans_real.py
@@ -505,7 +505,7 @@ def get_language_from_path(path, strict=False):
regex_match = language_code_prefix_re.match(path)
if not regex_match:
return None
- lang_code = regex_match.group(1)
+ lang_code = regex_match[1]
try:
return get_supported_language_variant(lang_code, strict=strict)
except LookupError: