88"""
99import io
1010from 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
0 commit comments