55
55
)
56
56
57
57
58
+ def _get_lines (filename ) -> list [str ]:
59
+ with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
60
+ return file .read ().split ("\n " ) # type:ignore[return-value,arg-type]
61
+
62
+
58
63
class Cohpcar :
59
64
"""Read COXXCAR.lobster/COXXCAR.LCFO.lobster files generated by LOBSTER.
60
65
@@ -120,8 +125,7 @@ def __init__(
120
125
else :
121
126
self ._filename = "COHPCAR.lobster"
122
127
123
- with zopen (self ._filename , mode = "rt" , encoding = "utf-8" ) as file :
124
- lines = file .read ().split ("\n " )
128
+ lines = _get_lines (filename )
125
129
126
130
# The parameters line is the second line in a COHPCAR file.
127
131
# It contains all parameters that are needed to map the file.
@@ -405,7 +409,7 @@ def __init__(
405
409
# and we don't need the header.
406
410
if self ._icohpcollection is None :
407
411
with zopen (self ._filename , mode = "rt" , encoding = "utf-8" ) as file :
408
- all_lines = file .read ().splitlines ()
412
+ all_lines : list [ str ] = file .read ().splitlines () # type:ignore[assignment]
409
413
410
414
# strip *trailing* blank lines only
411
415
all_lines = [line for line in all_lines if line .strip ()]
@@ -625,7 +629,7 @@ class NciCobiList:
625
629
}
626
630
"""
627
631
628
- def __init__ (self , filename : PathLike | None = "NcICOBILIST.lobster" ) -> None :
632
+ def __init__ (self , filename : PathLike = "NcICOBILIST.lobster" ) -> None :
629
633
"""
630
634
631
635
LOBSTER < 4.1.0: no COBI/ICOBI/NcICOBI
@@ -636,8 +640,7 @@ def __init__(self, filename: PathLike | None = "NcICOBILIST.lobster") -> None:
636
640
637
641
# LOBSTER list files have an extra trailing blank line
638
642
# and we don't need the header
639
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
640
- lines = file .read ().split ("\n " )[1 :- 1 ]
643
+ lines = _get_lines (filename )[1 :- 1 ]
641
644
if len (lines ) == 0 :
642
645
raise RuntimeError ("NcICOBILIST file contains no data." )
643
646
@@ -927,8 +930,7 @@ def __init__(
927
930
self .loewdin = [] if loewdin is None else loewdin
928
931
929
932
if self .num_atoms is None :
930
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
931
- lines = file .read ().split ("\n " )[3 :- 3 ]
933
+ lines = _get_lines (filename )[3 :- 3 ] # type:ignore[arg-type,assignment]
932
934
if len (lines ) == 0 :
933
935
raise RuntimeError ("CHARGES file contains no data." )
934
936
@@ -1061,8 +1063,7 @@ def __init__(self, filename: PathLike | None, **kwargs) -> None:
1061
1063
else :
1062
1064
raise ValueError (f"{ attr } ={ val } is not a valid attribute for Lobsterout" )
1063
1065
elif filename :
1064
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
1065
- lines = file .read ().split ("\n " )
1066
+ lines = _get_lines (filename )
1066
1067
if len (lines ) == 0 :
1067
1068
raise RuntimeError ("lobsterout does not contain any data" )
1068
1069
@@ -1459,8 +1460,7 @@ def __init__(
1459
1460
raise ValueError ("No FATBAND files in folder or given" )
1460
1461
1461
1462
for fname in filenames :
1462
- with zopen (fname , mode = "rt" , encoding = "utf-8" ) as file :
1463
- lines = file .read ().split ("\n " )
1463
+ lines = _get_lines (fname )
1464
1464
1465
1465
atom_names .append (os .path .split (fname )[1 ].split ("_" )[1 ].capitalize ())
1466
1466
parameters = lines [0 ].split ()
@@ -1493,8 +1493,7 @@ def __init__(
1493
1493
eigenvals : dict = {}
1494
1494
p_eigenvals : dict = {}
1495
1495
for ifilename , filename in enumerate (filenames ):
1496
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
1497
- lines = file .read ().split ("\n " )
1496
+ lines = _get_lines (filename )
1498
1497
1499
1498
if ifilename == 0 :
1500
1499
self .nbands = int (parameters [6 ])
@@ -1601,7 +1600,7 @@ def get_bandstructure(self) -> LobsterBandStructureSymmLine:
1601
1600
lattice = self .lattice ,
1602
1601
efermi = self .efermi , # type: ignore[arg-type]
1603
1602
labels_dict = self .label_dict ,
1604
- structure = self .structure ,
1603
+ structure = self .structure , # type:ignore[arg-type]
1605
1604
projections = self .p_eigenvals ,
1606
1605
)
1607
1606
@@ -1641,8 +1640,7 @@ def __init__(
1641
1640
self .max_deviation = [] if max_deviation is None else max_deviation
1642
1641
1643
1642
if not self .band_overlaps_dict :
1644
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
1645
- lines = file .read ().split ("\n " )
1643
+ lines = _get_lines (filename )
1646
1644
1647
1645
spin_numbers = [0 , 1 ] if lines [0 ].split ()[- 1 ] == "0" else [1 , 2 ]
1648
1646
@@ -1786,8 +1784,7 @@ def __init__(
1786
1784
self .is_lcfo = is_lcfo
1787
1785
self .list_dict_grosspop = [] if list_dict_grosspop is None else list_dict_grosspop
1788
1786
if not self .list_dict_grosspop :
1789
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
1790
- lines = file .read ().split ("\n " )
1787
+ lines = _get_lines (filename )
1791
1788
1792
1789
# Read file to list of dict
1793
1790
small_dict : dict [str , Any ] = {}
@@ -1916,8 +1913,7 @@ def _parse_file(
1916
1913
imaginary (list[float]): Imaginary parts of wave function.
1917
1914
distance (list[float]): Distances to the first point in wave function file.
1918
1915
"""
1919
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
1920
- lines = file .read ().split ("\n " )
1916
+ lines = _get_lines (filename )
1921
1917
1922
1918
points = []
1923
1919
distances = []
@@ -2086,8 +2082,7 @@ def __init__(
2086
2082
self .madelungenergies_mulliken = None if madelungenergies_mulliken is None else madelungenergies_mulliken
2087
2083
2088
2084
if self .ewald_splitting is None :
2089
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
2090
- lines = file .read ().split ("\n " )[5 ]
2085
+ lines = _get_lines (filename )[5 ]
2091
2086
if len (lines ) == 0 :
2092
2087
raise RuntimeError ("MadelungEnergies file contains no data." )
2093
2088
@@ -2157,8 +2152,7 @@ def __init__(
2157
2152
self .madelungenergies_mulliken : list | float = madelungenergies_mulliken or []
2158
2153
2159
2154
if self .num_atoms is None :
2160
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
2161
- lines = file .read ().split ("\n " )
2155
+ lines = _get_lines (filename )
2162
2156
if len (lines ) == 0 :
2163
2157
raise RuntimeError ("SitePotentials file contains no data." )
2164
2158
@@ -2311,7 +2305,7 @@ def __init__(
2311
2305
2312
2306
self ._filename = str (filename )
2313
2307
with zopen (self ._filename , mode = "rt" , encoding = "utf-8" ) as file :
2314
- lines = file .readlines ()
2308
+ lines : list [ str ] = file .readlines () # type:ignore[assignment]
2315
2309
if len (lines ) == 0 :
2316
2310
raise RuntimeError ("Please check provided input file, it seems to be empty" )
2317
2311
@@ -2454,8 +2448,7 @@ def __init__(
2454
2448
self .rel_loewdin_pol_vector = {} if rel_loewdin_pol_vector is None else rel_loewdin_pol_vector
2455
2449
2456
2450
if not self .rel_loewdin_pol_vector and not self .rel_mulliken_pol_vector :
2457
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
2458
- lines = file .read ().split ("\n " )
2451
+ lines = _get_lines (filename )
2459
2452
if len (lines ) == 0 :
2460
2453
raise RuntimeError ("Polarization file contains no data." )
2461
2454
@@ -2498,8 +2491,7 @@ def __init__(
2498
2491
self .bin_width = 0.0 if bin_width is None else bin_width
2499
2492
2500
2493
if not self .bwdf :
2501
- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
2502
- lines = file .read ().split ("\n " )
2494
+ lines = _get_lines (filename )
2503
2495
if len (lines ) == 0 :
2504
2496
raise RuntimeError ("BWDF file contains no data." )
2505
2497
0 commit comments