Skip to content

Instantly share code, notes, and snippets.

@valeriyvan
Forked from mcitron/pickleViewer.py
Created February 13, 2017 13:10
Show Gist options
  • Save valeriyvan/dd2b2f3520fd6842749d2eb18a6f9071 to your computer and use it in GitHub Desktop.
Save valeriyvan/dd2b2f3520fd6842749d2eb18a6f9071 to your computer and use it in GitHub Desktop.
Viewer for arbitrary pickle files
#!/usr/bin/python
import pickle
from collections import defaultdict
from collections import OrderedDict
import sys
def checkDict(inputData):
return (type(inputData) == dict or type(inputData) == defaultdict\
or type(inputData) == OrderedDict)
def checkList(inputData):
return type(inputData) == list
def recursivePrint(inputData,extraTuple = ()):
if checkDict(inputData):
for extraInfo,data in inputData.iteritems():
recursivePrint(data,(extraInfo,)+extraTuple)
elif checkList(inputData):
if all([not(checkList(x) or checkDict(x)) for x in inputData]):
print " ".join([str(x) for x in extraTuple][::-1]),inputData
else:
for data in inputData:
recursivePrint(data,extraTuple)
else:
print " ".join([str(x) for x in extraTuple][::-1]),inputData
def pickleViewer(inputFile):
inputData = pickle.load(open(inputFile,'r'))
print "Viewing pickle file {0} which contains data of type {1}".format(inputFile,type(inputData))
recursivePrint(inputData)
if __name__ =="__main__":
assert len(sys.argv) == 2, "Usage: pickleViewer <input.pkl>"
pickleViewer(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment