def test_file_path(): path = join(mkdtemp(), 'pytest-nonp.json') dump(nonpdata, path, compression=6) data2 = load(path, decompression=True) assert data2 == nonpdata data3 = load(path, decompression=None) # autodetect gzip assert data3 == nonpdata
def test_file_path_nonumpy(): path = join(mkdtemp(), 'pytest-nonp.json') dump(nonpdata, path, compression=6) data2 = load(path, decompression=True) assert data2 == nonpdata data3 = load(path, decompression=None) # autodetect gzip assert data3 == nonpdata
def test_file_handle_nonumpy(): path = join(mkdtemp(), 'pytest-nonp.json') with open(path, 'wb+') as fh: dump(nonpdata, fh, compression=6) with open(path, 'rb') as fh: data2 = load(fh, decompression=True) assert data2 == nonpdata with open(path, 'rb') as fh: data3 = load(fh, decompression=None) # test autodetect gzip assert data3 == nonpdata
def test_file_handle(): path = join(mkdtemp(), 'pytest-nonp.json') with open(path, 'wb+') as fh: dump(nonpdata, fh, compression=6) with open(path, 'rb') as fh: data2 = load(fh, decompression=True) assert data2 == nonpdata with open(path, 'rb') as fh: data3 = load(fh, decompression=None) # test autodetect gzip assert data3 == nonpdata
def load(self): """ Loading should not be automatic because it should also work for untrusted packages (e.g. to get the signature). """ # link_or_copy(self.path, join(self.compile_conf.PACKAGE_DIR, self.name)) try: with open(join(self.path, 'config.json')) as fh: conf = load(fh) except FileNotFoundError: raise InvalidPackageConfigError( 'config.json was not found in "{0:s}"'.format(self.path)) except ValueError as err: raise InvalidPackageConfigError( 'config file for {0:} is not valid json'.format( self, str(err))) if not (conf.get('name', None) == self.name and conf.get('version', None) == self.version): raise InvalidPackageConfigError(( 'Package config for {0:} contains mismatching name and/or version ' '{1:s} {2:s}').format(self, conf.get('name', None), conf.get('version', None))) # print(self.get_signature()[:8]) self.load_meta(conf) conf = self.config_add_defaults(conf) self.config_load_textfiles(conf) self.load_resources(conf) self.load_actions(conf) self.loaded = True return self
def test_mix_handle_str_path(): # Based on issue 68 data = {"fun": 1.1234567891234567e-13} path = join(mkdtemp(), 'test_mix_handle_str_path.json') dump(data, open(path, "w")) back = load(path) assert data == back
def test_file_handle_types(): path = join(mkdtemp(), 'pytest-text.json') for conv_str_byte in [True, False]: with open(path, 'w+') as fh: dump(nonpdata, fh, compression=False, conv_str_byte=conv_str_byte) with open(path, 'r') as fh: assert load(fh, conv_str_byte=conv_str_byte) == nonpdata with StringIO() as fh: dump(nonpdata, fh, conv_str_byte=conv_str_byte) fh.seek(0) assert load(fh, conv_str_byte=conv_str_byte) == nonpdata with BytesIO() as fh: with raises(TypeError): dump(nonpdata, fh) with BytesIO() as fh: dump(nonpdata, fh, conv_str_byte=True) fh.seek(0) assert load(fh, conv_str_byte=True) == nonpdata if is_py3: with open(path, 'w+') as fh: with raises(IOError): dump(nonpdata, fh, compression=6)
def test_flush_no_errors(): # just tests that flush doesn't cause problems; checking actual flushing is too messy. path = join(mkdtemp(), 'pytest-nonp.json') with open(path, 'wb+') as fh: dump(nonpdata, fh, compression=True, force_flush=True) with open(path, 'rb') as fh: data2 = load(fh, decompression=True) assert data2 == nonpdata # flush non-file IO sh = BytesIO() try: dump(ordered_map, fp=sh, compression=True, force_flush=True) finally: sh.close()
@to_serializable.register(datetime) def ts_datetime(val): """datetime overload""" print("datetime overload call.") return val.isoformat() + "Z" # print(json.dumps( # {"Blah": datetime.now()}, # default=to_serializable # )) with open('testdata.json') as input_file: mythingies = load(input_file) print("\nData from last run:") pp = pprint.PrettyPrinter(indent=4) pp.pprint(mythingies) this_is_now = datetime.now() print("\nTime since last checked:") print(this_is_now - mythingies['current_time']) if (this_is_now - mythingies['current_time']).days >= 1: print('Longer than 1 day since last run.') else: print('Not longer than 1 day since last run.')