In [1]:
# Get the imports out of the way
# We need byte storage, pycurl, and pickle
import pycurl
import pickle
from io import BytesIO
In [2]:
# Do the PycURL read as usual
url = "http://www.pythonchallenge.com/pc/def/banner.p"
rawData = BytesIO()
curlObj = pycurl.Curl()
curlObj.setopt(pycurl.URL, url)
curlObj.setopt(pycurl.WRITEDATA, rawData)
curlObj.perform()
curlObj.close()
In [6]:
# For some reason, calling pickle.load on the BytesIO object
# raises an EOFError. I wasn't able to figure out why exactly.
# Instead we have to call pickle.loads on the BytesIO's value
pickled = pickle.loads(rawData.getvalue())
# The un-pickled object is a list of lists of tuples.
# Each tuple has either a space or hash character and an integer
for p in pickled[:10]:
print(p)
In [7]:
# After failing over and over to see a pattern, I gave in and Googled the
# solution. It turns out the object is a essentially a bitmap
# Each inner list represents a single line on the screen (where pickled[0] == top line).
# Each tuple describes what character to print, and how many of them.
for line in pickled:
print(''.join(char * num for (char, num) in line))
In [ ]: