def _repr_png_(self): """Provide a display function for iPython/Jupyter.""" fig = self._IA._funcs[".*imshow"](self._mask.astype(int)) data = StreamIO() fig.savefig(data, format="png") plt.close(fig) data.seek(0) ret = data.read() data.close() return ret
def open_input_output(filename, out_filename=None): """ Converts *filename* and *out_filename* as streams. @param filename bytes or filename or BytesIO @param out_filename BytesIO or filename or None @return in_size, in_close, in_stream, out_close, out_return, out_stream """ # input typstr = str # unicode # if isinstance(filename, typstr): if not os.path.exists(filename): raise FileNotFoundError(filename) st = open(filename, "rb") close = True filesize = os.path.getsize(filename) elif isinstance(filename, StreamIO): st = filename close = False filesize = len(st.getvalue()) else: st = StreamIO(filename) close = False filesize = len(filename) # output if out_filename is None: sto = StreamIO() ret = True out_close = False elif isinstance(out_filename, StreamIO): sto = out_filename ret = False out_close = False else: sto = open(out_filename, "wb") ret = False out_close = True return filesize, close, st, out_close, ret, sto
def test_encryption_stream_fernet(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") infile = StreamIO(bytes([0, 1, 2, 3, 4])) outst = StreamIO() r = encrypt_stream("key0" * 8, infile, outst, algo="fernet") assert r is None enc = StreamIO(outst.getvalue()) enc2 = StreamIO(outst.getvalue()) outdec = StreamIO() r2 = decrypt_stream("key0" * 8, enc, outdec, algo="fernet") assert r2 is None self.assertEqual(infile.getvalue(), outdec.getvalue()) outdec2 = StreamIO() try: r3 = decrypt_stream("key1" * 8, enc2, outdec2, algo="fernet") except Exception: return assert r3 is None self.assertNotEqual(infile.getvalue(), outdec2.getvalue())
def test_encryption_stream(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") try: import Cryptodome as skip___ except ImportError: warnings.warn("pycryptodomex is not installed") return infile = StreamIO(bytes([0, 1, 2, 3, 4])) outst = StreamIO() r = encrypt_stream(b"key0" * 4, infile, outst) assert r is None enc = StreamIO(outst.getvalue()) enc2 = StreamIO(outst.getvalue()) outdec = StreamIO() r2 = decrypt_stream(b"key0" * 4, enc, outdec) assert r2 is None self.assertEqual(infile.getvalue(), outdec.getvalue()) outdec2 = StreamIO() r3 = decrypt_stream(b"key1" * 4, enc2, outdec2) assert r3 is None self.assertNotEqual(infile.getvalue(), outdec2.getvalue())
def test_encryption_stream_fernet_chunck_size(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") infile = StreamIO(bytes(list(i % 255 for i in range(0, 10000)))) outst = StreamIO() r = encrypt_stream("key0" * 8, infile, outst, algo="fernet", chunksize=256) assert r is None enc = StreamIO(outst.getvalue()) enc2 = StreamIO(outst.getvalue()) outdec = StreamIO() r2 = decrypt_stream("key0" * 8, enc, outdec, algo="fernet", chunksize=256) assert r2 is None self.assertEqual(infile.getvalue(), outdec.getvalue()) outdec2 = StreamIO() try: r3 = decrypt_stream("key1" * 8, enc2, outdec2, algo="fernet", chunksize=256) except Exception: return assert r3 is None self.assertNotEqual(infile.getvalue(), outdec2.getvalue())
def test_print(self): stream = StreamIO() c2 = currency('chf') c2.printinfo(stream) value = stream.getvalue() self.assertTrue(value)
def test_print(): stream = StreamIO() c2 = currency('chf') c2.printinfo(stream) value = stream.getvalue() assert value