5555)
5656
5757
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+
5863class Cohpcar :
5964 """Read COXXCAR.lobster/COXXCAR.LCFO.lobster files generated by LOBSTER.
6065
@@ -120,8 +125,7 @@ def __init__(
120125 else :
121126 self ._filename = "COHPCAR.lobster"
122127
123- with zopen (self ._filename , mode = "rt" , encoding = "utf-8" ) as file :
124- lines = file .read ().split ("\n " )
128+ lines = _get_lines (filename )
125129
126130 # The parameters line is the second line in a COHPCAR file.
127131 # It contains all parameters that are needed to map the file.
@@ -405,7 +409,7 @@ def __init__(
405409 # and we don't need the header.
406410 if self ._icohpcollection is None :
407411 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]
409413
410414 # strip *trailing* blank lines only
411415 all_lines = [line for line in all_lines if line .strip ()]
@@ -625,7 +629,7 @@ class NciCobiList:
625629 }
626630 """
627631
628- def __init__ (self , filename : PathLike | None = "NcICOBILIST.lobster" ) -> None :
632+ def __init__ (self , filename : PathLike = "NcICOBILIST.lobster" ) -> None :
629633 """
630634
631635 LOBSTER < 4.1.0: no COBI/ICOBI/NcICOBI
@@ -636,8 +640,7 @@ def __init__(self, filename: PathLike | None = "NcICOBILIST.lobster") -> None:
636640
637641 # LOBSTER list files have an extra trailing blank line
638642 # 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 ]
641644 if len (lines ) == 0 :
642645 raise RuntimeError ("NcICOBILIST file contains no data." )
643646
@@ -927,8 +930,7 @@ def __init__(
927930 self .loewdin = [] if loewdin is None else loewdin
928931
929932 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]
932934 if len (lines ) == 0 :
933935 raise RuntimeError ("CHARGES file contains no data." )
934936
@@ -1061,8 +1063,7 @@ def __init__(self, filename: PathLike | None, **kwargs) -> None:
10611063 else :
10621064 raise ValueError (f"{ attr } ={ val } is not a valid attribute for Lobsterout" )
10631065 elif filename :
1064- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
1065- lines = file .read ().split ("\n " )
1066+ lines = _get_lines (filename )
10661067 if len (lines ) == 0 :
10671068 raise RuntimeError ("lobsterout does not contain any data" )
10681069
@@ -1459,8 +1460,7 @@ def __init__(
14591460 raise ValueError ("No FATBAND files in folder or given" )
14601461
14611462 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 )
14641464
14651465 atom_names .append (os .path .split (fname )[1 ].split ("_" )[1 ].capitalize ())
14661466 parameters = lines [0 ].split ()
@@ -1493,8 +1493,7 @@ def __init__(
14931493 eigenvals : dict = {}
14941494 p_eigenvals : dict = {}
14951495 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 )
14981497
14991498 if ifilename == 0 :
15001499 self .nbands = int (parameters [6 ])
@@ -1601,7 +1600,7 @@ def get_bandstructure(self) -> LobsterBandStructureSymmLine:
16011600 lattice = self .lattice ,
16021601 efermi = self .efermi , # type: ignore[arg-type]
16031602 labels_dict = self .label_dict ,
1604- structure = self .structure ,
1603+ structure = self .structure , # type:ignore[arg-type]
16051604 projections = self .p_eigenvals ,
16061605 )
16071606
@@ -1641,8 +1640,7 @@ def __init__(
16411640 self .max_deviation = [] if max_deviation is None else max_deviation
16421641
16431642 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 )
16461644
16471645 spin_numbers = [0 , 1 ] if lines [0 ].split ()[- 1 ] == "0" else [1 , 2 ]
16481646
@@ -1786,8 +1784,7 @@ def __init__(
17861784 self .is_lcfo = is_lcfo
17871785 self .list_dict_grosspop = [] if list_dict_grosspop is None else list_dict_grosspop
17881786 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 )
17911788
17921789 # Read file to list of dict
17931790 small_dict : dict [str , Any ] = {}
@@ -1916,8 +1913,7 @@ def _parse_file(
19161913 imaginary (list[float]): Imaginary parts of wave function.
19171914 distance (list[float]): Distances to the first point in wave function file.
19181915 """
1919- with zopen (filename , mode = "rt" , encoding = "utf-8" ) as file :
1920- lines = file .read ().split ("\n " )
1916+ lines = _get_lines (filename )
19211917
19221918 points = []
19231919 distances = []
@@ -2086,8 +2082,7 @@ def __init__(
20862082 self .madelungenergies_mulliken = None if madelungenergies_mulliken is None else madelungenergies_mulliken
20872083
20882084 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 ]
20912086 if len (lines ) == 0 :
20922087 raise RuntimeError ("MadelungEnergies file contains no data." )
20932088
@@ -2157,8 +2152,7 @@ def __init__(
21572152 self .madelungenergies_mulliken : list | float = madelungenergies_mulliken or []
21582153
21592154 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 )
21622156 if len (lines ) == 0 :
21632157 raise RuntimeError ("SitePotentials file contains no data." )
21642158
@@ -2311,7 +2305,7 @@ def __init__(
23112305
23122306 self ._filename = str (filename )
23132307 with zopen (self ._filename , mode = "rt" , encoding = "utf-8" ) as file :
2314- lines = file .readlines ()
2308+ lines : list [ str ] = file .readlines () # type:ignore[assignment]
23152309 if len (lines ) == 0 :
23162310 raise RuntimeError ("Please check provided input file, it seems to be empty" )
23172311
@@ -2454,8 +2448,7 @@ def __init__(
24542448 self .rel_loewdin_pol_vector = {} if rel_loewdin_pol_vector is None else rel_loewdin_pol_vector
24552449
24562450 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 )
24592452 if len (lines ) == 0 :
24602453 raise RuntimeError ("Polarization file contains no data." )
24612454
@@ -2498,8 +2491,7 @@ def __init__(
24982491 self .bin_width = 0.0 if bin_width is None else bin_width
24992492
25002493 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 )
25032495 if len (lines ) == 0 :
25042496 raise RuntimeError ("BWDF file contains no data." )
25052497
0 commit comments