ASF Bugzilla – Attachment 34055 Details for
Bug 57919
Support in-place write when opened from a File
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
read from NPOIFSFileSystem and write in place or buffered/unbuffered outputstream
57919.py (text/plain), 4.39 KB, created by
Javen O'Neal
on 2016-07-19 06:23:12 UTC
(
hide
)
Description:
read from NPOIFSFileSystem and write in place or buffered/unbuffered outputstream
Filename:
MIME Type:
Creator:
Javen O'Neal
Created:
2016-07-19 06:23:12 UTC
Size:
4.39 KB
patch
obsolete
># Using Jython 2.5.1 and Apache POI 3.11 > >import random >import profile >import marshal >import shutil >import sys >import csv >from operator import itemgetter >import os > >from java.io import File, FileOutputStream, BufferedOutputStream > >sys.path.append('C:\\temp\\poi-3.15-beta3-20160718.jar') >from org.apache.poi.poifs.filesystem import NPOIFSFileSystem >from org.apache.poi.hssf.usermodel import HSSFWorkbook > >def open_workbook(f): > read_only = False > print(f) > poifs = NPOIFSFileSystem(f, read_only) > wb = HSSFWorkbook(poifs) > return wb > >def modify_workbook(wb): > wb.removeSheetAt(2) > wb.removeSheetAt(1) > wb.getSheetAt(0).getRow(0).getCell(0).setCellValue("Changed!") > sh = wb.createSheet('new sheet') > for r in range(10): > row = sh.createRow(r) > for c in range(10): > cell = row.createCell(c) > cell.setCellValue('row=%d, col=%d'.format(r, c)) > >def open_and_modify_workbook(f): > wb = open_workbook(f) > modify_workbook(wb) > return wb > >def write_inplace(wb): > wb.write() > >def write_stream(wb, stream): > wb.write(stream) > >def test_inplace(directory, filename): > f = File(directory, filename) > wb = open_and_modify_workbook(f) > write_inplace(wb) > close_inplace(wb) > >def test_unbuffered(directory, filename): > f = File(directory, filename) > wb = open_and_modify_workbook(f) > fos = FileOutputStream(File(directory, filename+'.out')) > write_stream(wb, fos) > close_unbuffered(fos, wb) > >def test_buffered(directory, filename): > f = File(directory, filename) > wb = open_and_modify_workbook(f) > fos = FileOutputStream(File(directory, filename+'.out')) > bos = BufferedOutputStream(fos) > write_stream(wb, bos) > close_buffered(bos, fos, wb) > >def close_inplace(wb): > wb.close() > >def close_unbuffered(fos, wb): > fos.close() > wb.close() > >def close_buffered(bos, fos, wb): > bos.close() > fos.close() > wb.close() > >local_dir = 'C:\\temp\\' >remote_dir = 'J:\\temp\\' > >def pstats2csv(pstatsfile): > with open(pstatsfile, 'rb') as p: > k = marshal.loads(p.read()) > with open(pstatsfile+'.csv', 'wb') as csvfile: > csvwriter = csv.writer(csvfile) > csvwriter.writerow(['ncalls', 'tottime', 'cumtime', 'cumtime/ncalls', 'function', 'location']) > rows = [] > for (filename, line, function), (cc, ncalls, tottime, cumtime, callers) in k.iteritems(): > cumtime_ncalls = cumtime/ncalls if ncalls != 0 else '' > rows.append([ncalls, tottime, cumtime, cumtime_ncalls, function, '%s:%s' % (filename, line)]) > for row in sorted(rows, key=itemgetter(3),reverse=True): > csvwriter.writerow(row) > >def run_trials(save_function, directory, filename, pstatsfile, trials=3): > rands = [random.randint(0,1000000) for _ in range(trials)] > filenames = [] > for r in rands: > f = '%s-%d.xls'% (save_function.__name__, r) > filenames.append(f) > shutil.copyfile(local_dir + filename, directory + f) > > locals = {'runner': runner, 'save_function': save_function, 'directory': directory, 'filenames': filenames} > profile.runctx('runner(save_function, directory, filenames)', {}, locals, filename=pstatsfile) > > pstats2csv(pstatsfile) > os.remove(pstatsfile) > for f in filenames: > os.remove(directory+f) > out = directory+f+'.out' > if os.path.exists(out): > os.remove(out) > > return pstatsfile+'.csv' > >def runner(save_function, directory, filenames): > for f in filenames: > save_function(directory, f) > >def suite(): > trials = 10 > test_funcs = [test_unbuffered, test_buffered, test_inplace] > random.shuffle(test_funcs) > filename = '53446.xls' > stats = [] > for i, test in enumerate(test_funcs): > for name, directory in (('local', local_dir), ('network', remote_dir)): > pstatsfile = '%s%s-%s.pstats' % (local_dir, test.__name__, name) > stats.append(run_trials(test, directory, filename, pstatsfile, trials=trials)) > > with open(local_dir+'aggregate.csv', 'wb') as agg: > for stat in stats: > agg.write(stat+'\n') > with open(stat, 'rb') as csv: > for line in csv: > agg.write(line) > agg.write('\n') > os.remove(stat) > >suite()
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 57919
:
34054
| 34055 |
34056