decompilation

Arnaud Delobelle arnodel at gmail.com
Fri Mar 2 21:18:56 CET 2012


On 2 March 2012 18:52, shikha panghal <spdollyshikha4 at gmail.com> wrote:
> Hi
>
> Please decoplile the .pyc code ,as i have lost my .py code.

Aha, a customer!  I've written a module for this: unpyc3
(http://code.google.com/p/unpyc3/)

Here it is in action:


Python 3.2.1 (v3.2.1:ac1f7e5c0510, Jul  9 2011, 01:03:53)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import unpyc3
>>> import hangman322
>>> code = unpyc3.decompile(hangman322)
>>> print(code)
import random

def readFile(fileName):
    file = open(fileName)
    lineList = file.readlines()
    file.close()
    return lineList

def initialize(fileName):
    try:
        lineList = readFile(fileName)
    except:
        print('Oops! ' + filename + ' was not a valid file.')
        return
    len_d = len(lineList)
    word_length = [0]*len_d
    for i in range(len_d):
        if lineList[i][-1] == '\n':
            word_length[i] = len(lineList[i]) - 1
        else:
            word_length[i] = len(lineList[i])
    tabulate = [0]*25
    for i in range(len_d):
        if word_length[i] >= 24:
            tabulate[24] = tabulate[24] + 1
        else:
            tabulate[word_length[i]] = tabulate[word_length[i]] + 1
    words = [None]*25
    for i in range(2, 24):
        words[i] = [None]*tabulate[i]
        k = 0
        for j in range(len_d):
            if word_length[j] == i:
                words[i][k] = lineList[j]
                k = k + 1
    for i in range(24, 25):
        words[i] = [None]*tabulate[i]
        k = 0
        for j in range(len_d):
            if word_length[j] >= i:
                words[i][k] = lineList[j]
                k = k + 1
    return words

def wordsOfLength(n, source_file):
    words = initialize(source_file)
    return words[n]

def getUserStringInput(L, prompt):
    replyInList = False
    while not replyInList:
        reply = input(prompt)
        replyInList = reply in L
        if not replyInList:
            print('That reply is invalid. Try again.')
    return reply

def intListToStringList(L):
    L2 = ['']*len(L)
    for i in range(len(L)):
        L2[i] = str(L[i])
    return L2

def getNewLetterGuess(availableLetters):
    letterString = ''
    for j in range(26):
        if availableLetters[j]:
            letterString = letterString + chr(65 + j) + ' '
        else:
            letterString = letterString + '  '
    validChar = False
    print(letterString)
    while not validChar:
        reply = input('Guess! > ')
        if len(reply) == 1:
            validChar = True
    letterIndex = ord(reply) - 65
    if letterIndex > 25:
        letterIndex = letterIndex - 32
    while letterIndex > 25 or not availableLetters[letterIndex]:
        print('This is an invalid choice. Please try again!')
        validChar = False
        print(letterString)
        while not validChar:
            reply = input('Guess! > ')
            if len(reply) == 1:
                validChar = True
        letterIndex = ord(reply) - 65
        if letterIndex > 25:
            letterIndex = letterIndex - 32
    guess = chr(97 + letterIndex)
    availableLetters[letterIndex] = False
    return guess, availableLetters

def getWordFamilyCounter(L, n, guess):
    wordFamilyCounter = [0]*2**n
    familyIndexList = [-1]*len(L)
    for k in range(len(L)):
        w = list(L[k])
        ct = 0
        for k2 in range(n):
            if w[k2] == guess:
                ct = ct + 2**k2
        familyIndexList[k] = ct
        wordFamilyCounter[ct] = wordFamilyCounter[ct] + 1
    return wordFamilyCounter, familyIndexList

def extractLargestFamily(L, familyIndexList, wordFamilyCounter):
    bestFamily = wordFamilyCounter.index(max(wordFamilyCounter))
    boolist = [False]*len(L)
    for k3 in range(len(L)):
        if familyIndexList[k3] == bestFamily:
            boolist[k3] = True
    j2 = 0
    smallList = [' ']*sum(boolist)
    for k4 in range(len(L)):
        if boolist[k4]:
            smallList[j2] = L[k4]
            j2 = j2 + 1
    return smallList

def updatePatternList(patternList, guess, bestFamily):
    n = len(patternList)
    for k6 in range(n):
        if bestFamily//2 == bestFamily/2:
            pass
        else:
            patternList[k6] = guess + ' '
        bestFamily = bestFamily >> 1
    return patternList

def pickWordFrom(L):
    index = random.randint(0, len(L) - 1)
    return L[index]

def play():
    reply = getUserStringInput(intListToStringList(list(range(2,
21))), 'How long should I make your word?!!? (2 to 20) > ')
    n = int(reply)
    patternList = ['_ ']*n
    print(''.join(patternList))
    L = wordsOfLength(n, 'dictionary.txt')
    reply = getUserStringInput(intListToStringList(list(range(1,
27))), 'How many guesses will you need? > ')
    m = int(reply)
    availableLetters = [True]*26
    for i in range(m):
        guess, availableLetters = getNewLetterGuess(availableLetters)
        wordFamilyCounter, familyIndexList = getWordFamilyCounter(L, n, guess)
        bestFamily = wordFamilyCounter.index(max(wordFamilyCounter))
        if bestFamily == 0:
            print('Letter not in word.')
        else:
            print('Letter is in word!!!')
        L = extractLargestFamily(L, familyIndexList, wordFamilyCounter)
        patternList = updatePatternList(patternList, guess, bestFamily)
        print(''.join(patternList))
        if '_ ' not in patternList:
            break
    if '_ ' not in patternList:
        print('SURELY you must be CHEATING, but you guessed my word in
' + str(i + 1) + ' tries!!!')
    else:
        bogusWord = pickWordFrom(L)
        print('You lose.  The word was: ' + bogusWord)

>>>

I haven't actually checked if this code runs :)

-- 
Arnaud



More information about the Python-list mailing list