ATLAS Offline Software
Loading...
Searching...
No Matches
pickleTool.py
Go to the documentation of this file.
1#!/usr/bin/env python
2
3# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4
5
6"""
7A small utility to deal with pickled files.
8"""
9__author__ = 'Juerg Beringer'
10__version__ = '$Id: pickleTool.py 216126 2009-09-29 16:12:59Z atlidbs $'
11__usage__ = 'pickleTool [options] arg1 [arg2]'
12
13
14import sys, pickle, pprint
15
16# Option parsing
17from optparse import OptionParser
18parser = OptionParser(usage=__usage__, version=__version__)
19parser.add_option('-c', '--create', dest='create', action='store_true', default=False, help='Pickle input file')
20parser.add_option('-p', '--pretty', dest='pretty', action='store_true', default=False, help='Pretty-print/write data')
21(options,args) = parser.parse_args()
22
23if options.create:
24 if len(args)<2:
25 parser.error('missing input or output file')
26 inFile = open(args[0], 'r')
27 data = inFile.read()
28 inFile.close()
29 object = eval(data)
30 outFile = open(args[1], 'wb')
31 pickle.dump(object,outFile)
32 outFile.close()
33
34else:
35 if len(args)<1:
36 parser.error('missing input file')
37 inFile = open(args[0], 'rb')
38 data = pickle.load(inFile)
39 inFile.close()
40 if options.pretty:
41 s = pprint.pformat(data)
42 else:
43 s = repr(data)
44 if len(args)>1:
45 outFile = open(args[1], 'w')
46 outFile.write(s)
47 outFile.write('\n')
48 outFile.close()
49 else:
50 print (s)