Writing and reading objects in compressed form (PFile)
Source code name: "io_pfile1.py"
Programming language: Python
Topic: File IO/Binary
DMelt Version 1. Last modified: 12/11/2015. License: Pro
https://datamelt.org/code/cache/io_pfile1_7606.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


# S.Chekanov
 
from jhplot.io import *
from jhplot import *
from java.util import Random

c1 = HPlot("Canvas",600,400,2,1)
c1.setGTitle("I/O test")
c1.visible(1)
c1.setName("Writted to file")
c1.setAutoRange()

x=P0D('X'); y=P0D('Y') 
x.randomUniform(10,-3,3)
y.randomUniform(10,0,30)
p1=P1D("XY",x,y)


h1 = H1D("fixed bins",10, -2, 2.0)
h2 = H1D("variable",[-1,2,4,7])
r = Random()
for i in range(100):
  h1.fill(r.nextGaussian())
  h2.fill(5*r.nextGaussian())

c1.draw(p1)
c1.draw(h1)
c1.draw(h2)
f=PFile('tmp.pbu','w')
f.write(p1)
f.write(h1)
f.write(h2)
pn=PND("2x3 matrix")
pn.add([1,2])
pn.add([3,4])
pn.add([5,6])
print pn.toString()
f.write(pn)
f.close()


# reading objects
c1.cd(2,1)
c1.setAutoRange()
c1.setName("From file")
f=PFile('tmp.pbu','r')
print f.listEntries()

print "File version=",f.getVersion()," File size=", f.size()
# reading and plotting objects
for i in range(f.size()-1):
        p=f.read()
        c1.draw(p)
# use a key (title) for the last object
pnd_out=f.read("2x3 matrix")
print "From the file="
print pnd_out.toString()
f.close()


You see the box below because you did not login.