def test_float_precision(): """Test whether ``float_precision`` keywork is working. """ js.safe_dump({"value": 1.23456789}, path_json, indent_format=False, float_precision=2, enable_verbose=False) try: assert js.load(path_json, enable_verbose=False)[ "value"] == approx(1.23) except: warnings.warn("float_precision argument is not working.") os.remove(path_json)
def test_bytes_and_datetime(): js.safe_dump(data_complex, path_json, ensure_ascii=True, enable_verbose=False) d = js.load(path_json, enable_verbose=False) assert d["int"] == data_complex["int"] assert d["float"] == data_complex["float"] assert d["str"] == data_complex["str"] assert d["boolean"] == data_complex["boolean"] if py23.is_py3: assert d["bytes"].decode("utf-8") == "bytes 比特串" dt1 = d["datetime"] dt2 = data_complex["datetime"] assert dt1.date() == dt2.date() assert dt1.hour == dt2.hour assert dt1.minute == dt2.minute assert dt1.second == dt2.second assert abs(dt1.microsecond - dt2.microsecond) <= 1000 os.remove(path_json)
def test_compress(): """Test whether data compression is working. """ js.safe_dump({"value": 1}, path_gz, enable_verbose=False) assert js.load(path_gz, enable_verbose=False) == {"value": 1} os.remove(path_gz)
def save_data(self, player_data): js.safe_dump(player_data, self.file_path)
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function from dataIO import js from dataIO import pk from dataIO import textfile data = { "name": "John", "age": 18, "favorite number": 3.1415926535, "hobby": ["Music", "Sport"] } js.safe_dump(data, "data.json", indent_format=True, float_precision=2, enable_verbose=True) pk.safe_dump(data, "data.pickle", enable_verbose=True) s = "This\nis\nPython!" textfile.write(s, "text.txt")