Skip to content

Commit ae55ce4

Browse files
committed
Make cosmetic changes for higher pylint-score
1 parent 77c5c12 commit ae55ce4

5 files changed

Lines changed: 43 additions & 34 deletions

File tree

docs/conf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#
1313
import os
1414
import sys
15+
import doctest
16+
1517
sys.path.insert(0, os.path.abspath('../src'))
1618

1719
# -- Project information -----------------------------------------------------
@@ -37,6 +39,8 @@
3739

3840
autodoc_member_order = 'bysource'
3941

42+
doctest_default_flags = doctest.NORMALIZE_WHITESPACE
43+
4044
# Add any paths that contain templates here, relative to this directory.
4145
templates_path = ['_templates']
4246

src/hsd/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"""
88
Toolbox for reading, writing and manipulating HSD-data.
99
"""
10-
from .common import HSD_ATTRIB_LINE, HSD_ATTRIB_EQUAL, HSD_ATTRIB_SUFFIX,\
10+
from hsd.common import HSD_ATTRIB_LINE, HSD_ATTRIB_EQUAL, HSD_ATTRIB_SUFFIX,\
1111
HSD_ATTRIB_NAME, HsdError
12-
from .dict import HsdDictBuilder, HsdDictWalker
13-
from .eventhandler import HsdEventHandler, HsdEventPrinter
14-
from .formatter import HsdFormatter
15-
from .io import load, load_string, dump, dump_string
16-
from .parser import HsdParser
12+
from hsd.dict import HsdDictBuilder, HsdDictWalker
13+
from hsd.eventhandler import HsdEventHandler, HsdEventPrinter
14+
from hsd.formatter import HsdFormatter
15+
from hsd.io import load, load_string, dump, dump_string
16+
from hsd.parser import HsdParser

src/hsd/dict.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"""
1010
import re
1111
from typing import List, Tuple, Union
12-
from .common import np, ATTRIB_SUFFIX, HSD_ATTRIB_SUFFIX, HsdError,\
12+
from hsd.common import np, ATTRIB_SUFFIX, HSD_ATTRIB_SUFFIX, HsdError,\
1313
QUOTING_CHARS, SPECIAL_CHARS
14-
from .eventhandler import HsdEventHandler, HsdEventPrinter
14+
from hsd.eventhandler import HsdEventHandler, HsdEventPrinter
1515

1616
_ItemType = Union[float, int, bool, str]
1717

@@ -106,7 +106,7 @@ def close_tag(self, tagname):
106106

107107
def add_text(self, text):
108108
if self._curblock or self._data is not None:
109-
msg = f"Data appeared in an invalid context"
109+
msg = "Data appeared in an invalid context"
110110
raise HsdError(msg)
111111
self._data = self._text_to_data(text)
112112

@@ -215,14 +215,13 @@ def _item_to_hsd(item):
215215

216216
if isinstance(item, bool):
217217
return "Yes" if item else "No"
218-
elif isinstance(item, (int, float)):
218+
if isinstance(item, (int, float)):
219219
return str(item)
220-
elif isinstance(item, str):
220+
if isinstance(item, str):
221221
return _str_to_hsd(item)
222-
else:
223-
msg = "Data type {} can not be converted to HSD string"\
224-
.format(type(item))
225-
raise TypeError(msg)
222+
msg = "Data type {} can not be converted to HSD string"\
223+
.format(type(item))
224+
raise TypeError(msg)
226225

227226

228227
def _str_to_hsd(string):

src/hsd/io.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"""
99
import io
1010
from typing import Union, TextIO
11-
from .dict import HsdDictWalker, HsdDictBuilder
12-
from .formatter import HsdFormatter
13-
from .parser import HsdParser
11+
from hsd.dict import HsdDictWalker, HsdDictBuilder
12+
from hsd.formatter import HsdFormatter
13+
from hsd.parser import HsdParser
1414

1515

1616
_INDENT_STR = " "
@@ -48,8 +48,8 @@ def load(hsdfile: Union[TextIO, str], lower_tag_names: bool = False,
4848
parser = HsdParser(eventhandler=dictbuilder,
4949
lower_tag_names=lower_tag_names)
5050
if isinstance(hsdfile, str):
51-
with open(hsdfile, "r") as hsdfile:
52-
parser.parse(hsdfile)
51+
with open(hsdfile, "r") as hsddescr:
52+
parser.parse(hsddescr)
5353
else:
5454
parser.parse(hsdfile)
5555
return dictbuilder.hsddict
@@ -92,20 +92,23 @@ def load_string(
9292
... }
9393
... \"\"\"
9494
>>> hsd.load_string(hsdstr)
95-
{'Dftb': {'Scc': True, 'Filling': {'Fermi': {'Temperature': 100, 'Temperature.attrib': 'Kelvin'}}}}
95+
{'Dftb': {'Scc': True, 'Filling': {'Fermi': {'Temperature': 100,
96+
'Temperature.attrib': 'Kelvin'}}}}
9697
9798
In order to ease the case-insensitive handling of the input, the tag
9899
names can be converted to lower case during reading using the
99100
``lower_tag_names`` option.
100101
101102
>>> hsd.load_string(hsdstr, lower_tag_names=True)
102-
{'dftb': {'scc': True, 'filling': {'fermi': {'temperature': 100, 'temperature.attrib': 'Kelvin'}}}}
103+
{'dftb': {'scc': True, 'filling': {'fermi': {'temperature': 100,
104+
'temperature.attrib': 'Kelvin'}}}}
103105
104106
The original tag names (together with additional information like the
105107
line number of a tag) can be recorded, if the ``include_hsd_attribs``
106108
option is set:
107109
108-
>>> data = hsd.load_string(hsdstr, lower_tag_names=True, include_hsd_attribs=True)
110+
>>> data = hsd.load_string(hsdstr, lower_tag_names=True,
111+
... include_hsd_attribs=True)
109112
110113
Each tag in the dictionary will have a corresponding ".hsdattrib" entry
111114
with the recorded data:
@@ -117,12 +120,14 @@ def load_string(
117120
original style, when writing the data in HSD-format again. Compare:
118121
119122
>>> hsd.dump_string(data)
120-
'dftb {\\n scc = Yes\\n filling {\\n fermi {\\n temperature [Kelvin] = 100\\n }\\n }\\n}\\n'
123+
'dftb {\\n scc = Yes\\n filling {\\n fermi {\\n
124+
temperature [Kelvin] = 100\\n }\\n }\\n}\\n'
121125
122126
versus
123127
124128
>>> hsd.dump_string(data, use_hsd_attribs=True)
125-
'Dftb {\\n Scc = Yes\\n Filling {\\n Fermi {\\n Temperature [Kelvin] = 100\\n }\\n }\\n}\\n'
129+
'Dftb {\\n Scc = Yes\\n Filling {\\n Fermi {\\n
130+
Temperature [Kelvin] = 100\\n }\\n }\\n}\\n'
126131
127132
"""
128133
fobj = io.StringIO(hsdstr)
@@ -155,8 +160,8 @@ def dump(data: dict, hsdfile: Union[TextIO, str],
155160
msg = "Invalid object type"
156161
raise TypeError(msg)
157162
if isinstance(hsdfile, str):
158-
with open(hsdfile, "w") as hsdfile:
159-
_dump_dict(data, hsdfile, use_hsd_attribs)
163+
with open(hsdfile, "w") as hsddescr:
164+
_dump_dict(data, hsddescr, use_hsd_attribs)
160165
else:
161166
_dump_dict(data, hsdfile, use_hsd_attribs)
162167

@@ -186,7 +191,8 @@ def dump_string(data: dict, use_hsd_attribs: bool = False) -> str:
186191
... }
187192
... }
188193
>>> hsd.dump_string(hsdtree)
189-
'Dftb {\\n Scc = Yes\\n Filling {\\n Fermi {\\n Temperature [Kelvin] = 100\\n }\\n }\\n}\\n'
194+
'Dftb {\\n Scc = Yes\\n Filling {\\n Fermi {\\n
195+
Temperature [Kelvin] = 100\\n }\\n }\\n}\\n'
190196
191197
See also :func:`hsd.load_string` for an example.
192198

src/hsd/parser.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
Contains the event-generating HSD-parser.
99
"""
1010
from typing import Optional, TextIO, Union
11-
import hsd.common as common
12-
from .eventhandler import HsdEventHandler, HsdEventPrinter
11+
from hsd import common
12+
from hsd.eventhandler import HsdEventHandler, HsdEventPrinter
1313

1414

1515
SYNTAX_ERROR = 1
@@ -49,7 +49,8 @@ class HsdParser:
4949
... \"\"\")
5050
>>> parser.parse(hsdfile)
5151
>>> dictbuilder.hsddict
52-
{'Hamiltonian': {'Dftb': {'Scc': True, 'Filling': {'Fermi': {'Temperature': 100, 'Temperature.attrib': 'Kelvin'}}}}}
52+
{'Hamiltonian': {'Dftb': {'Scc': True, 'Filling': {'Fermi':
53+
{'Temperature': 100, 'Temperature.attrib': 'Kelvin'}}}}}
5354
"""
5455

5556
def __init__(self, eventhandler: Optional[HsdEventHandler] = None,
@@ -291,9 +292,8 @@ def _include_hsd(self, fname):
291292
@staticmethod
292293
def _include_txt(fname):
293294
fname = common.unquote(fname.strip())
294-
fp = open(fname, "r")
295-
txt = fp.read()
296-
fp.close()
295+
with open(fname, "r") as fp:
296+
txt = fp.read()
297297
return txt
298298

299299

0 commit comments

Comments
 (0)