Skip to content

Commit af19281

Browse files
jdufresnejgraham
authored andcommitted
Use modern Python syntax set literals and dict comprehension (#415)
Available since Python 2.7. Instances discovered using pyupgrade.
1 parent 77879b0 commit af19281

File tree

7 files changed

+24
-26
lines changed

7 files changed

+24
-26
lines changed

html5lib/_inputstream.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
else:
3535
invalid_unicode_re = re.compile(invalid_unicode_no_surrogate)
3636

37-
non_bmp_invalid_codepoints = set([0x1FFFE, 0x1FFFF, 0x2FFFE, 0x2FFFF, 0x3FFFE,
38-
0x3FFFF, 0x4FFFE, 0x4FFFF, 0x5FFFE, 0x5FFFF,
39-
0x6FFFE, 0x6FFFF, 0x7FFFE, 0x7FFFF, 0x8FFFE,
40-
0x8FFFF, 0x9FFFE, 0x9FFFF, 0xAFFFE, 0xAFFFF,
41-
0xBFFFE, 0xBFFFF, 0xCFFFE, 0xCFFFF, 0xDFFFE,
42-
0xDFFFF, 0xEFFFE, 0xEFFFF, 0xFFFFE, 0xFFFFF,
43-
0x10FFFE, 0x10FFFF])
37+
non_bmp_invalid_codepoints = {0x1FFFE, 0x1FFFF, 0x2FFFE, 0x2FFFF, 0x3FFFE,
38+
0x3FFFF, 0x4FFFE, 0x4FFFF, 0x5FFFE, 0x5FFFF,
39+
0x6FFFE, 0x6FFFF, 0x7FFFE, 0x7FFFF, 0x8FFFE,
40+
0x8FFFF, 0x9FFFE, 0x9FFFF, 0xAFFFE, 0xAFFFF,
41+
0xBFFFE, 0xBFFFF, 0xCFFFE, 0xCFFFF, 0xDFFFE,
42+
0xDFFFF, 0xEFFFE, 0xEFFFF, 0xFFFFE, 0xFFFFF,
43+
0x10FFFE, 0x10FFFF}
4444

4545
ascii_punctuation_re = re.compile("[\u0009-\u000D\u0020-\u002F\u003A-\u0040\u005C\u005B-\u0060\u007B-\u007E]")
4646

html5lib/constants.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@
519519
"xmlns:xlink": ("xmlns", "xlink", namespaces["xmlns"])
520520
}
521521

522-
unadjustForeignAttributes = dict([((ns, local), qname) for qname, (prefix, local, ns) in
523-
adjustForeignAttributes.items()])
522+
unadjustForeignAttributes = {(ns, local): qname for qname, (prefix, local, ns) in
523+
adjustForeignAttributes.items()}
524524

525525
spaceCharacters = frozenset([
526526
"\t",
@@ -544,8 +544,7 @@
544544
digits = frozenset(string.digits)
545545
hexDigits = frozenset(string.hexdigits)
546546

547-
asciiUpper2Lower = dict([(ord(c), ord(c.lower()))
548-
for c in string.ascii_uppercase])
547+
asciiUpper2Lower = {ord(c): ord(c.lower()) for c in string.ascii_uppercase}
549548

550549
# Heading elements need to be ordered
551550
headingElements = (
@@ -2934,7 +2933,7 @@
29342933
tokenTypes["EmptyTag"]])
29352934

29362935

2937-
prefixes = dict([(v, k) for k, v in namespaces.items()])
2936+
prefixes = {v: k for k, v in namespaces.items()}
29382937
prefixes["http://www.w3.org/1998/Math/MathML"] = "math"
29392938

29402939

html5lib/html5parser.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ def __init__(self, tree=None, strict=False, namespaceHTMLElements=True, debug=Fa
119119
self.tree = tree(namespaceHTMLElements)
120120
self.errors = []
121121

122-
self.phases = dict([(name, cls(self, self.tree)) for name, cls in
123-
getPhases(debug).items()])
122+
self.phases = {name: cls(self, self.tree) for name, cls in
123+
getPhases(debug).items()}
124124

125125
def _parse(self, stream, innerHTML=False, container="div", scripting=False, **kwargs):
126126

@@ -413,8 +413,7 @@ def parseRCDataRawtext(self, token, contentType):
413413
def getPhases(debug):
414414
def log(function):
415415
"""Logger that records which phase processes each token"""
416-
type_names = dict((value, key) for key, value in
417-
tokenTypes.items())
416+
type_names = {value: key for key, value in tokenTypes.items()}
418417

419418
def wrapped(self, *args, **kwargs):
420419
if function.__name__.startswith("process") and len(args) > 0:
@@ -2478,7 +2477,7 @@ def processStartTag(self, token):
24782477
currentNode = self.tree.openElements[-1]
24792478
if (token["name"] in self.breakoutElements or
24802479
(token["name"] == "font" and
2481-
set(token["data"].keys()) & set(["color", "face", "size"]))):
2480+
set(token["data"].keys()) & {"color", "face", "size"})):
24822481
self.parser.parseError("unexpected-html-element-in-foreign-content",
24832482
{"name": token["name"]})
24842483
while (self.tree.openElements[-1].namespace !=

html5lib/tests/test_serializer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _convertAttrib(self, attribs):
8080

8181

8282
def serialize_html(input, options):
83-
options = dict([(str(k), v) for k, v in options.items()])
83+
options = {str(k): v for k, v in options.items()}
8484
encoding = options.get("encoding", None)
8585
if "encoding" in options:
8686
del options["encoding"]

html5lib/tests/tokenizer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def parse(self, stream, encoding=None, innerHTML=False):
2828
tokenizer.currentToken = {"type": "startTag",
2929
"name": self._lastStartTag}
3030

31-
types = dict((v, k) for k, v in constants.tokenTypes.items())
31+
types = {v: k for k, v in constants.tokenTypes.items()}
3232
for token in tokenizer:
3333
getattr(self, 'process%s' % types[token["type"]])(token)
3434

html5lib/treebuilders/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
listElementsMap = {
1212
None: (frozenset(scopingElements), False),
13-
"button": (frozenset(scopingElements | set([(namespaces["html"], "button")])), False),
14-
"list": (frozenset(scopingElements | set([(namespaces["html"], "ol"),
15-
(namespaces["html"], "ul")])), False),
13+
"button": (frozenset(scopingElements | {(namespaces["html"], "button")}), False),
14+
"list": (frozenset(scopingElements | {(namespaces["html"], "ol"),
15+
(namespaces["html"], "ul")}), False),
1616
"table": (frozenset([(namespaces["html"], "html"),
1717
(namespaces["html"], "table")]), False),
1818
"select": (frozenset([(namespaces["html"], "optgroup"),

utils/entities.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def parse(path="html5ents.xml"):
88

99

1010
def entity_table(tree):
11-
return dict((entity_name("".join(tr[0].xpath(".//text()"))),
12-
entity_characters(tr[1].text))
13-
for tr in tree.xpath("//h:tbody/h:tr",
14-
namespaces={"h": "http://www.w3.org/1999/xhtml"}))
11+
return {entity_name("".join(tr[0].xpath(".//text()"))):
12+
entity_characters(tr[1].text)
13+
for tr in tree.xpath("//h:tbody/h:tr",
14+
namespaces={"h": "http://www.w3.org/1999/xhtml"})}
1515

1616

1717
def entity_name(inp):

0 commit comments

Comments
 (0)