Skip to content

Commit 16bdcf8

Browse files
committed
Python 2 cleanups
* Remove Python 2 imports * Remove str aliases * Remove explicit object superclass * Remove unnecessary super() arguments * Remove unnecessary empty lines
1 parent 35dd17e commit 16bdcf8

File tree

16 files changed

+83
-187
lines changed

16 files changed

+83
-187
lines changed

htmlgen/attribute.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from htmlgen.timeutil import parse_rfc3339_partial_time
44

55

6-
class html_attribute(object):
7-
6+
class html_attribute:
87
"""Add an attribute to an HTML element.
98
109
>>> from htmlgen import Element
@@ -45,8 +44,7 @@ def __set__(self, obj, value):
4544
obj.set_attribute(self._attribute_name, value)
4645

4746

48-
class boolean_html_attribute(object):
49-
47+
class boolean_html_attribute:
5048
"""Add a boolean attribute to an HTML element.
5149
5250
>>> from htmlgen import Element
@@ -76,8 +74,7 @@ def __set__(self, obj, value):
7674
obj.remove_attribute(self._attribute_name)
7775

7876

79-
class int_html_attribute(object):
80-
77+
class int_html_attribute:
8178
"""Add an attribute to an HTML element that accepts only integers.
8279
8380
>>> from htmlgen import Element
@@ -121,8 +118,7 @@ def __set__(self, obj, value):
121118
obj.set_attribute(self._attribute_name, str(value))
122119

123120

124-
class float_html_attribute(object):
125-
121+
class float_html_attribute:
126122
"""Add an attribute to an HTML element that accepts only numbers.
127123
128124
>>> from htmlgen import Element
@@ -166,8 +162,7 @@ def __set__(self, obj, value):
166162
obj.set_attribute(self._attribute_name, str(value))
167163

168164

169-
class time_html_attribute(object):
170-
165+
class time_html_attribute:
171166
"""Add an attribute to an HTML element that accepts only time values.
172167
173168
>>> from htmlgen import Element
@@ -212,8 +207,7 @@ def __set__(self, obj, value):
212207
obj.set_attribute(self._attribute_name, str(value))
213208

214209

215-
class list_html_attribute(object):
216-
210+
class list_html_attribute:
217211
"""Add an attribute to an HTML element that accepts a list of strings.
218212
219213
>>> from htmlgen import Element
@@ -250,8 +244,7 @@ def __init__(self, data_name, default=None):
250244
super(data_attribute, self).__init__(attribute_name, default)
251245

252246

253-
class css_class_attribute(object):
254-
247+
class css_class_attribute:
255248
"""Add a boolean attribute to an HTML element that add a CSS class.
256249
257250
>>> from htmlgen import Element

htmlgen/attribute.pyi

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from typing import Optional, List, Iterable
33

44
from htmlgen.element import Element
55

6-
class html_attribute(object):
6+
class html_attribute:
77
def __init__(
88
self, attribute_name: str, default: Optional[str] = ...
99
) -> None: ...
@@ -12,12 +12,12 @@ class html_attribute(object):
1212
) -> Optional[str]: ...
1313
def __set__(self, obj: Element, value: Optional[str]) -> None: ...
1414

15-
class boolean_html_attribute(object):
15+
class boolean_html_attribute:
1616
def __init__(self, attribute_name: str) -> None: ...
1717
def __get__(self, obj: Element, type_: Optional[type] = ...) -> bool: ...
1818
def __set__(self, obj: Element, value: bool) -> None: ...
1919

20-
class int_html_attribute(object):
20+
class int_html_attribute:
2121
def __init__(
2222
self, attribute_name: str, default: Optional[int] = ...
2323
) -> None: ...
@@ -26,7 +26,7 @@ class int_html_attribute(object):
2626
) -> Optional[int]: ...
2727
def __set__(self, obj: Element, value: Optional[int]) -> None: ...
2828

29-
class float_html_attribute(object):
29+
class float_html_attribute:
3030
def __init__(
3131
self, attribute_name: str, default: Optional[float] = ...
3232
) -> None: ...
@@ -35,7 +35,7 @@ class float_html_attribute(object):
3535
) -> Optional[float]: ...
3636
def __set__(self, obj: Element, value: Optional[float]) -> None: ...
3737

38-
class time_html_attribute(object):
38+
class time_html_attribute:
3939
def __init__(
4040
self, attribute_name: str, default: Optional[datetime.time] = None
4141
) -> None: ...
@@ -46,7 +46,7 @@ class time_html_attribute(object):
4646
self, obj: Element, value: Optional[datetime.time]
4747
) -> None: ...
4848

49-
class list_html_attribute(object):
49+
class list_html_attribute:
5050
def __init__(self, attribute_name: str) -> None: ...
5151
def __get__(
5252
self, obj: Element, type_: Optional[type] = ...
@@ -58,7 +58,7 @@ class data_attribute(html_attribute):
5858
self, data_name: str, default: Optional[str] = None
5959
) -> None: ...
6060

61-
class css_class_attribute(object):
61+
class css_class_attribute:
6262
def __init__(self, css_class: str) -> None: ...
6363
def __get__(self, obj: Element, type_: Optional[type] = ...) -> bool: ...
6464
def __set__(self, obj: Element, value: bool) -> None: ...

htmlgen/block.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
class Division(Element):
5-
65
"""An HTML division (<div>) element.
76
87
<div> elements are block-level elements without semantic meaning. They are
@@ -12,33 +11,29 @@ class Division(Element):
1211
>>> div.id = "my-block"
1312
>>> div.add_css_classes("important", "legal")
1413
>>> div.append("This is more text.")
15-
1614
"""
1715

1816
def __init__(self, *content):
19-
super(Division, self).__init__("div")
17+
super().__init__("div")
2018
self.extend(content)
2119

2220

2321
class Paragraph(Element):
24-
2522
"""An HTML paragraph (<p>) element.
2623
2724
<p> elements are block-level elements that delineate text paragraphs.
2825
2926
>>> p1 = Paragraph("This is a text paragraph.")
3027
>>> p2 = Paragraph()
3128
>>> p2.append("This is another paragraph.")
32-
3329
"""
3430

3531
def __init__(self, *content):
36-
super(Paragraph, self).__init__("p")
32+
super().__init__("p")
3733
self.extend(content)
3834

3935

4036
class Preformatted(Element):
41-
4237
"""An HTML pre-formatted text (<pre>) element.
4338
4439
<pre> elements are block-level elements that contain text which will
@@ -47,8 +42,7 @@ class Preformatted(Element):
4742
4843
>>> pre = Preformatted()
4944
>>> pre.append("Hello\\nWorld!")
50-
5145
"""
5246

5347
def __init__(self):
54-
super(Preformatted, self).__init__("pre")
48+
super().__init__("pre")

htmlgen/document.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99

1010
class Document(Generator):
11-
1211
"""An HTML document.
1312
1413
A document consists of a doctype declaration and the HTML root tag.
@@ -34,11 +33,10 @@ class Document(Generator):
3433
>>> doc.add_script("my-script.js")
3534
>>> doc.add_stylesheet("style.css")
3635
>>> doc.append_body(Element("div"))
37-
3836
"""
3937

4038
def __init__(self, title=None, language="en"):
41-
super(Document, self).__init__()
39+
super().__init__()
4240
self.root = HTMLRoot(title=title, language=language)
4341

4442
def generate(self):
@@ -73,7 +71,6 @@ def append_body(self, child):
7371

7472

7573
class HTMLRoot(NonVoidElement):
76-
7774
"""HTML root (<html>) element.
7875
7976
>>> root = HTMLRoot(title="My Page")
@@ -85,11 +82,10 @@ class HTMLRoot(NonVoidElement):
8582
8683
>>> root.head = Head()
8784
>>> root.body = Body()
88-
8985
"""
9086

9187
def __init__(self, title="", language="en"):
92-
super(HTMLRoot, self).__init__("html")
88+
super().__init__("html")
9389
self.head = Head(title=title)
9490
self.body = Body()
9591
self.set_attribute("xmlns", "http://www.w3.org/1999/xhtml")
@@ -102,7 +98,6 @@ def generate_children(self):
10298

10399

104100
class Head(Element):
105-
106101
"""HTML document head (<head>) element.
107102
108103
A title element is provided by default, but it can be overwritten:
@@ -116,11 +111,10 @@ class Head(Element):
116111
117112
>>> head.add_stylesheet("style.css")
118113
>>> head.add_script("script.js")
119-
120114
"""
121115

122116
def __init__(self, title=None):
123-
super(Head, self).__init__("head")
117+
super().__init__("head")
124118
self._title = Title(title)
125119
self.append(self._title)
126120
self.append(Meta.create_charset("utf-8"))
@@ -151,19 +145,17 @@ def add_script(self, script):
151145

152146

153147
class Body(Element):
154-
155148
"""HTML body (<body>) element."""
156149

157150
def __init__(self):
158-
super(Body, self).__init__("body")
151+
super().__init__("body")
159152

160153

161154
class Title(NonVoidElement):
162-
163155
"""HTML page title (<title>) element."""
164156

165157
def __init__(self, title=None):
166-
super(Title, self).__init__("title")
158+
super().__init__("title")
167159
self.title = title or ""
168160

169161
def generate_children(self):
@@ -172,11 +164,10 @@ def generate_children(self):
172164

173165

174166
class Meta(VoidElement):
175-
176167
"""HTML meta information (<meta>) element."""
177168

178169
def __init__(self):
179-
super(Meta, self).__init__("meta")
170+
super().__init__("meta")
180171

181172
@classmethod
182173
def create_charset(cls, charset):
@@ -186,7 +177,6 @@ def create_charset(cls, charset):
186177

187178

188179
class Script(NonVoidElement):
189-
190180
"""HTML script (<script>) element.
191181
192182
A script element can either point to an external script via the url
@@ -210,12 +200,11 @@ class Script(NonVoidElement):
210200
211201
>>> Script().type
212202
'text/javascript'
213-
214203
"""
215204

216205
def __init__(self, url=None, script=None):
217206
assert url is None or script is None
218-
super(Script, self).__init__("script")
207+
super().__init__("script")
219208
if url:
220209
self.url = url
221210
self.script = script
@@ -249,11 +238,10 @@ def json_script(json):
249238

250239

251240
class HeadLink(VoidElement):
252-
253241
"""HTML meta data link (<link>) element."""
254242

255243
def __init__(self, relation, url):
256-
super(HeadLink, self).__init__("link")
244+
super().__init__("link")
257245
self.relation = relation
258246
self.url = url
259247

@@ -266,8 +254,7 @@ def create_stylesheet(cls, stylesheet):
266254

267255

268256
class Main(Element):
269-
270257
"""HTML main document content (<main>) element."""
271258

272259
def __init__(self):
273-
super(Main, self).__init__("main")
260+
super().__init__("main")

htmlgen/element.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
import sys
1+
from html import escape
22

33
from htmlgen.generator import Generator, HTMLChildGenerator
44

5-
if sys.version_info[0] < 3:
6-
from cgi import escape
7-
else:
8-
from html import escape
9-
10-
# TODO: Python 3: remove
11-
if sys.version_info[0] >= 3:
12-
str_class = str
13-
else:
14-
str_class = basestring
15-
165

176
def is_element(o, element_name):
187
"""Return whether a given object is a certain element generator."""
@@ -25,7 +14,7 @@ def is_element(o, element_name):
2514

2615
class ElementBase(Generator):
2716
def __init__(self, element_name):
28-
super(ElementBase, self).__init__()
17+
super().__init__()
2918
self.element_name = element_name
3019
self._attributes = {}
3120
self._css_classes = set()
@@ -73,7 +62,7 @@ def set_attribute(self, name, value):
7362
'<div title="Test Title"></div>'
7463
7564
"""
76-
if not isinstance(name, str_class) or not isinstance(value, str_class):
65+
if not isinstance(name, str) or not isinstance(value, str):
7766
raise TypeError("name and value must be strings")
7867
self._attributes[name] = value
7968

@@ -306,7 +295,7 @@ class Element(NonVoidElement):
306295
"""
307296

308297
def __init__(self, element_name):
309-
super(Element, self).__init__(element_name)
298+
super().__init__(element_name)
310299
self.children = HTMLChildGenerator()
311300

312301
def __bool__(self):

0 commit comments

Comments
 (0)