In [2]:
import pycurl
import pickle
from io import BytesIO

In [84]:
rawData = BytesIO()
curlObj = pycurl.Curl()
curlObj.setopt(pycurl.URL, "http://www.pythonchallenge.com/pc/def/banner.p")
curlObj.setopt(pycurl.WRITEDATA, rawData)
curlObj.perform()

In [23]:
print("Total bytes: " + str(len(rawData.getvalue())))
#result = pickle.load(rawData, encoding="bytes")
s = rawData.getvalue().decode(encoding="UTF-8").translate({ord("\n"):None})
otherRaw = s.encode()
print("Bytes without newlines: " + str(len(otherRaw)))
print("Number of newlines: " + str(rawData.getvalue().decode(encoding="UTF-8").count('\n')))
print(5002-1070)
#result = pickle.load(BytesIO(otherRaw), encoding="bytes")


Total bytes: 5002
Bytes without newlines: 3932
Number of newlines: 1070
3932

In [24]:
with open("banner.p", "rb") as pickFile:
    pickled = pickle.load(pickFile)

In [59]:
#print(pickled)
s_pound = 0
s_space = 0
numStr = ""
for item in pickled:
    for nested in item:
        if(nested[0] != '#'):
            s_pound += nested[1]
            numStr += str(nested[1])
        else:
            s_space += nested[1]
print("s# = " + str(s_pound))
print("s_ = " + str(s_space))
print("numString = " + numStr)


s# = 1524
s_ = 661
numString = 951470115711157111571115711157111571115711663973431071334152613152512524334424353111153445252621111510452527211155345251721115345251211152445251121115154525112111151545252121262524452536313435335254441654441446695

In [34]:
with open("banner.p", "rb") as f:
    rawPickled = pickle.load(f, encoding="bytes")

In [46]:
#print(pickled)

In [45]:
for item in pickled:
    print(item)


[(' ', 95)]
[(' ', 14), ('#', 5), (' ', 70), ('#', 5), (' ', 1)]
[(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)]
[(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)]
[(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)]
[(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)]
[(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)]
[(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)]
[(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)]
[(' ', 6), ('#', 3), (' ', 6), ('#', 4), (' ', 3), ('#', 3), (' ', 9), ('#', 3), (' ', 7), ('#', 5), (' ', 3), ('#', 3), (' ', 4), ('#', 5), (' ', 3), ('#', 3), (' ', 10), ('#', 3), (' ', 7), ('#', 4), (' ', 1)]
[(' ', 3), ('#', 3), (' ', 3), ('#', 2), (' ', 4), ('#', 4), (' ', 1), ('#', 7), (' ', 5), ('#', 2), (' ', 2), ('#', 3), (' ', 6), ('#', 4), (' ', 1), ('#', 7), (' ', 3), ('#', 4), (' ', 1), ('#', 7), (' ', 5), ('#', 3), (' ', 2), ('#', 3), (' ', 5), ('#', 4), (' ', 1)]
[(' ', 2), ('#', 3), (' ', 5), ('#', 3), (' ', 2), ('#', 5), (' ', 4), ('#', 4), (' ', 3), ('#', 3), (' ', 3), ('#', 4), (' ', 4), ('#', 5), (' ', 4), ('#', 4), (' ', 2), ('#', 5), (' ', 4), ('#', 4), (' ', 3), ('#', 3), (' ', 5), ('#', 3), (' ', 3), ('#', 4), (' ', 1)]
[(' ', 1), ('#', 3), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 3), ('#', 3), (' ', 4), ('#', 3), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 3), (' ', 6), ('#', 4), (' ', 2), ('#', 4), (' ', 1)]
[(' ', 1), ('#', 3), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 10), ('#', 3), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 3), (' ', 7), ('#', 3), (' ', 2), ('#', 4), (' ', 1)]
[('#', 4), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 5), ('#', 2), (' ', 3), ('#', 3), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 4), (' ', 7), ('#', 3), (' ', 2), ('#', 4), (' ', 1)]
[('#', 4), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 3), ('#', 10), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 14), (' ', 2), ('#', 4), (' ', 1)]
[('#', 4), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 3), (' ', 4), ('#', 4), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 4), (' ', 12), ('#', 4), (' ', 1)]
[('#', 4), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 4), (' ', 5), ('#', 3), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 4), (' ', 12), ('#', 4), (' ', 1)]
[(' ', 1), ('#', 3), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 4), (' ', 5), ('#', 3), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 3), (' ', 12), ('#', 4), (' ', 1)]
[(' ', 2), ('#', 3), (' ', 6), ('#', 2), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 3), (' ', 4), ('#', 4), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 3), ('#', 3), (' ', 6), ('#', 2), (' ', 3), ('#', 4), (' ', 1)]
[(' ', 3), ('#', 3), (' ', 4), ('#', 2), (' ', 3), ('#', 4), (' ', 5), ('#', 4), (' ', 3), ('#', 11), (' ', 3), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 4), ('#', 3), (' ', 4), ('#', 2), (' ', 4), ('#', 4), (' ', 1)]
[(' ', 6), ('#', 3), (' ', 5), ('#', 6), (' ', 4), ('#', 5), (' ', 4), ('#', 2), (' ', 4), ('#', 4), (' ', 1), ('#', 6), (' ', 4), ('#', 11), (' ', 4), ('#', 5), (' ', 6), ('#', 3), (' ', 6), ('#', 6)]
[(' ', 95)]

In [64]:
with open("banner.p", "rb") as f:
    nonFixPickle = pickle.load(f, fix_imports=False, encoding="UTF-8")

In [65]:
print(nonFixPickle is pickled)
print(nonFixPickle == pickled)


False
True

In [76]:
#import pickletools

#with open('banner.p', 'rb') as f:
#    pickletools.dis(f)

In [72]:
hashInts = set()
spaceInts = set()
chrs = ''

for l in pickled:
    for tup in l:
        if tup[0] == '#':
            hashInts.add(tup[1])
        else:
            spaceInts.add(tup[1])
        chrs += chr(tup[1])
            
print("Hashes: " + str(sorted(hashInts)))
print("Spaces: " + str(sorted(spaceInts)))
print("Chrs: " + chrs)


Hashes: [2, 3, 4, 5, 6, 7, 10, 11, 14]
Spaces: [1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 14, 15, 70, 71, 95]
Chrs: _FGGGGGGG	


_

In [79]:
counts = {}
for l in pickled:
    for item in l:
        key = item[0]
        num = item[1]
        d = counts.get(key, {})
        c = d.get(num, 0) + 1
        if(len(d) == 0):
            counts[key] = {}
        counts[key][num] = c
        
for (k, v) in counts.items():
    print(k + " + has " + str(v) + " matches")


  + has {1: 33, 2: 25, 3: 19, 4: 26, 70: 1, 6: 9, 71: 7, 9: 1, 10: 2, 7: 4, 12: 3, 14: 1, 15: 7, 11: 7, 5: 36, 95: 2} matches
# + has {2: 8, 3: 37, 4: 101, 5: 9, 6: 3, 7: 3, 10: 1, 11: 2, 14: 1} matches

In [80]:
filtered = []
for inner in pickled:
    l = []
    for item in inner:
        if(item[0] != '#'):
            l.append(item)
    filtered.append(l)
    
for item in filtered:
    print(item)


[(' ', 95)]
[(' ', 14), (' ', 70), (' ', 1)]
[(' ', 15), (' ', 71), (' ', 1)]
[(' ', 15), (' ', 71), (' ', 1)]
[(' ', 15), (' ', 71), (' ', 1)]
[(' ', 15), (' ', 71), (' ', 1)]
[(' ', 15), (' ', 71), (' ', 1)]
[(' ', 15), (' ', 71), (' ', 1)]
[(' ', 15), (' ', 71), (' ', 1)]
[(' ', 6), (' ', 6), (' ', 3), (' ', 9), (' ', 7), (' ', 3), (' ', 4), (' ', 3), (' ', 10), (' ', 7), (' ', 1)]
[(' ', 3), (' ', 3), (' ', 4), (' ', 1), (' ', 5), (' ', 2), (' ', 6), (' ', 1), (' ', 3), (' ', 1), (' ', 5), (' ', 2), (' ', 5), (' ', 1)]
[(' ', 2), (' ', 5), (' ', 2), (' ', 4), (' ', 3), (' ', 3), (' ', 4), (' ', 4), (' ', 2), (' ', 4), (' ', 3), (' ', 5), (' ', 3), (' ', 1)]
[(' ', 1), (' ', 11), (' ', 5), (' ', 3), (' ', 4), (' ', 4), (' ', 5), (' ', 2), (' ', 5), (' ', 2), (' ', 6), (' ', 2), (' ', 1)]
[(' ', 1), (' ', 11), (' ', 5), (' ', 10), (' ', 4), (' ', 5), (' ', 2), (' ', 5), (' ', 2), (' ', 7), (' ', 2), (' ', 1)]
[(' ', 11), (' ', 5), (' ', 5), (' ', 3), (' ', 4), (' ', 5), (' ', 2), (' ', 5), (' ', 1), (' ', 7), (' ', 2), (' ', 1)]
[(' ', 11), (' ', 5), (' ', 3), (' ', 4), (' ', 5), (' ', 2), (' ', 5), (' ', 1), (' ', 2), (' ', 1)]
[(' ', 11), (' ', 5), (' ', 2), (' ', 4), (' ', 4), (' ', 5), (' ', 2), (' ', 5), (' ', 1), (' ', 12), (' ', 1)]
[(' ', 11), (' ', 5), (' ', 1), (' ', 5), (' ', 4), (' ', 5), (' ', 2), (' ', 5), (' ', 1), (' ', 12), (' ', 1)]
[(' ', 1), (' ', 11), (' ', 5), (' ', 1), (' ', 5), (' ', 4), (' ', 5), (' ', 2), (' ', 5), (' ', 2), (' ', 12), (' ', 1)]
[(' ', 2), (' ', 6), (' ', 2), (' ', 5), (' ', 2), (' ', 4), (' ', 4), (' ', 5), (' ', 2), (' ', 5), (' ', 3), (' ', 6), (' ', 3), (' ', 1)]
[(' ', 3), (' ', 4), (' ', 3), (' ', 5), (' ', 3), (' ', 3), (' ', 5), (' ', 2), (' ', 5), (' ', 4), (' ', 4), (' ', 4), (' ', 1)]
[(' ', 6), (' ', 5), (' ', 4), (' ', 4), (' ', 4), (' ', 1), (' ', 4), (' ', 4), (' ', 6), (' ', 6)]
[(' ', 95)]

In [83]:
for item in pickled:
    print(''.join(i[0] * i[1] for i in item))


                                                                                               
              #####                                                                      ##### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
      ###      ####   ###         ###       #####   ###    #####   ###          ###       #### 
   ###   ##    #### #######     ##  ###      #### #######   #### #######     ###  ###     #### 
  ###     ###  #####    ####   ###   ####    #####    ####  #####    ####   ###     ###   #### 
 ###           ####     ####   ###    ###    ####     ####  ####     ####  ###      ####  #### 
 ###           ####     ####          ###    ####     ####  ####     ####  ###       ###  #### 
####           ####     ####     ##   ###    ####     ####  ####     #### ####       ###  #### 
####           ####     ####   ##########    ####     ####  ####     #### ##############  #### 
####           ####     ####  ###    ####    ####     ####  ####     #### ####            #### 
####           ####     #### ####     ###    ####     ####  ####     #### ####            #### 
 ###           ####     #### ####     ###    ####     ####  ####     ####  ###            #### 
  ###      ##  ####     ####  ###    ####    ####     ####  ####     ####   ###      ##   #### 
   ###    ##   ####     ####   ###########   ####     ####  ####     ####    ###    ##    #### 
      ###     ######    #####    ##    #### ######    ###########    #####      ###      ######
                                                                                               

In [89]:
rawData = BytesIO()
curlObj = pycurl.Curl()
curlObj.setopt(pycurl.URL, "http://www.pythonchallenge.com/pc/def/banner.p")
curlObj.setopt(pycurl.WRITEDATA, rawData)
curlObj.perform()
curlObj.close()

In [105]:
body = rawData.getvalue()

# Works fine
pick = pickle.loads(body)

# Fails with EOFError (out of input)
badPick = pickle.load(rawData)


---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
<ipython-input-105-dcf5d9bde9af> in <module>()
      5 
      6 # Fails with EOFError (out of input)
----> 7 badPick = pickle.load(rawData)

EOFError: Ran out of input

In [107]:
for i in range(len(body)):
    rawData.read(1)

In [108]:
print(pick[0])


[(' ', 95)]

In [111]:
for p in pickled[:5]:
    print(p)


[(' ', 95)]
[(' ', 14), ('#', 5), (' ', 70), ('#', 5), (' ', 1)]
[(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)]
[(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)]
[(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)]

In [ ]: