@@ -203,37 +203,34 @@ def test_as_dict(self):
203
203
struct = IStructure (self .lattice , [{si : 0.5 , mn : 0.5 }, {si : 0.5 }], coords )
204
204
assert "lattice" in struct .as_dict ()
205
205
assert "sites" in struct .as_dict ()
206
- d = self .propertied_structure .as_dict ()
207
- assert d ["sites" ][0 ]["properties" ]["magmom" ] == 5
206
+ dct = self .propertied_structure .as_dict ()
207
+ assert dct ["sites" ][0 ]["properties" ]["magmom" ] == 5
208
208
coords = [[0 , 0 , 0 ], [0.75 , 0.5 , 0.75 ]]
209
209
struct = IStructure (
210
210
self .lattice ,
211
- [
212
- {Species ("O" , - 2 , spin = 3 ): 1.0 },
213
- {Species ("Mg" , 2 , spin = 2 ): 0.8 },
214
- ],
211
+ [{Species ("O" , - 2 , spin = 3 ): 1.0 }, {Species ("Mg" , 2 , spin = 2 ): 0.8 }],
215
212
coords ,
216
213
site_properties = {"magmom" : [5 , - 5 ]},
217
214
properties = {"general_property" : "test" },
218
215
)
219
- d = struct .as_dict ()
220
- assert d ["sites" ][0 ]["properties" ]["magmom" ] == 5
221
- assert d ["sites" ][0 ]["species" ][0 ]["spin" ] == 3
222
- assert d ["properties" ]["general_property" ] == "test"
216
+ dct = struct .as_dict ()
217
+ assert dct ["sites" ][0 ]["properties" ]["magmom" ] == 5
218
+ assert dct ["sites" ][0 ]["species" ][0 ]["spin" ] == 3
219
+ assert dct ["properties" ]["general_property" ] == "test"
223
220
224
- d = struct .as_dict (0 )
225
- assert "volume" not in d ["lattice" ]
226
- assert "xyz" not in d ["sites" ][0 ]
221
+ dct = struct .as_dict (0 )
222
+ assert "volume" not in dct ["lattice" ]
223
+ assert "xyz" not in dct ["sites" ][0 ]
227
224
228
225
def test_from_dict (self ):
229
- d = self .propertied_structure .as_dict ()
230
- struct = IStructure .from_dict (d )
226
+ dct = self .propertied_structure .as_dict ()
227
+ struct = IStructure .from_dict (dct )
231
228
assert struct [0 ].magmom == 5
232
- d = self .propertied_structure .as_dict (0 )
233
- s2 = IStructure .from_dict (d )
229
+ dct = self .propertied_structure .as_dict (0 )
230
+ s2 = IStructure .from_dict (dct )
234
231
assert struct == s2
235
232
236
- d = {
233
+ dct = {
237
234
"lattice" : {
238
235
"a" : 3.8401979337 ,
239
236
"volume" : 40.044794644251596 ,
@@ -282,7 +279,7 @@ def test_from_dict(self):
282
279
],
283
280
"properties" : {"test_property" : "test" },
284
281
}
285
- struct = IStructure .from_dict (d )
282
+ struct = IStructure .from_dict (dct )
286
283
assert struct [0 ].magmom == 5
287
284
assert struct [0 ].specie .spin == 3
288
285
assert struct .properties ["test_property" ] == "test"
@@ -818,7 +815,7 @@ def test_get_dist_matrix(self):
818
815
ans = [[0.0 , 2.3516318 ], [2.3516318 , 0.0 ]]
819
816
assert_allclose (self .struct .distance_matrix , ans )
820
817
821
- def test_to_from_file_string (self ):
818
+ def test_to_from_file_and_string (self ):
822
819
for fmt in ["cif" , "json" , "poscar" , "cssr" ]:
823
820
struct = self .struct .to (fmt = fmt )
824
821
assert struct is not None
@@ -855,8 +852,8 @@ def test_to_from_file_string(self):
855
852
with pytest .raises (ValueError , match = "Invalid format='badformat'" ):
856
853
self .struct .to (fmt = "badformat" )
857
854
858
- self .struct .to (filename = "POSCAR.testing.gz" )
859
- struct = Structure .from_file ("POSCAR.testing.gz" )
855
+ self .struct .to (filename = ( gz_json_path := "POSCAR.testing.gz" ) )
856
+ struct = Structure .from_file (gz_json_path )
860
857
assert struct == self .struct
861
858
862
859
# test CIF file with unicode error
@@ -897,6 +894,7 @@ def setUp(self):
897
894
coords = [[0 , 0 , 0 ], [0.75 , 0.5 , 0.75 ]]
898
895
lattice = Lattice ([[3.8401979337 , 0 , 0 ], [1.9200989668 , 3.3257101909 , 0 ], [0 , - 2.2171384943 , 3.1355090603 ]])
899
896
self .struct = Structure (lattice , ["Si" , "Si" ], coords )
897
+ self .struct .properties ["foo" ] = "bar"
900
898
self .cu_structure = Structure (lattice , ["Cu" , "Cu" ], coords )
901
899
self .disordered = Structure .from_spacegroup ("Im-3m" , Lattice .cubic (3 ), [Composition ("Fe0.5Mn0.5" )], [[0 , 0 , 0 ]])
902
900
self .labeled_structure = Structure (lattice , ["Si" , "Si" ], coords , labels = ["Si1" , "Si2" ])
@@ -1271,13 +1269,14 @@ def test_as_from_dict(self):
1271
1269
assert isinstance (s1 , Structure )
1272
1270
1273
1271
def test_default_dict_attrs (self ):
1274
- d = self .struct .as_dict ()
1275
- assert d ["charge" ] == 0
1272
+ dct = self .struct .as_dict ()
1273
+ assert dct ["charge" ] == 0
1274
+ assert dct ["properties" ] == {"foo" : "bar" }
1276
1275
1277
1276
def test_to_from_abivars (self ):
1278
1277
"""Test as_dict, from_dict with fmt == abivars."""
1279
- d = self .struct .as_dict (fmt = "abivars" )
1280
- s2 = Structure .from_dict (d , fmt = "abivars" )
1278
+ dct = self .struct .as_dict (fmt = "abivars" )
1279
+ s2 = Structure .from_dict (dct , fmt = "abivars" )
1281
1280
assert s2 == self .struct
1282
1281
assert isinstance (s2 , Structure )
1283
1282
@@ -1292,8 +1291,8 @@ def test_to_from_file_string(self):
1292
1291
assert isinstance (ss , Structure )
1293
1292
1294
1293
# to/from file
1295
- self .struct .to (filename = "POSCAR.testing" )
1296
- assert os .path .isfile ("POSCAR.testing" )
1294
+ self .struct .to (filename = ( poscar_path := "POSCAR.testing" ) )
1295
+ assert os .path .isfile (poscar_path )
1297
1296
1298
1297
for ext in (".json" , ".json.gz" , ".json.bz2" , ".json.xz" , ".json.lzma" ):
1299
1298
self .struct .to (filename = f"json-struct{ ext } " )
0 commit comments