(module I found)

Martijn Faassen m.faassen at
Wed Aug 1 19:56:59 CEST 2001

Hi there,

I've found this module somewhere linked from the Vaults of Parnassus,
but then my browser crashed and I can't find the link anymore. Did it
disappear from the vaults? The web page had a bunch of other interesting
modules I wanted to check out. The URL had /psu at the end, I believe, but
it's not in my browser history, probably due to the crash.. The code
seems to be something for Python 3k.

Can anyone help? Here is the code of the module that I managed to save
before my browser crashed. It says it needs Stackless Python and
the 'timetravel' module that I I think I was also linked from that
site. I haven't been able to run it, but if you manage to let me know
what happens!

import time, random, sys
import timetravel
from StringIO import StringIO

def grok(text):
    """A function that groks arbitrary text and just does the work.

    This is a proof of concept implementation of the 'grok' statement
    to be introduced by Python 3k.
    # remember the time when we called this function
    call_time = time.time()
    # create random text that might be the correct code that fulfills
    # the text to grok
    code = random_noise()
    # try evaluating this code
        exec code
        # expect code to put any results in 'result' variable
        if was_grokked(text, result):
            # travel back in time to call time
            # return result (could be None)
            return result
        # ignore any errors
    # otherwise, we want to time travel back to the moment in time
    # just before (one second) we called the function, and try again
    return timetravel.apply_at_time(grok, [text], {}, call_time - 1)

def was_grokked(time, result):
    """Check whether result did whatever the user wanted in text and make
    this function return 1 if so, false if not (if user is only interested
    in the side effects of the grok() then 'result' could be None).
    # could use fancy GUI but this works well enough
    print "The program returned:", result
    answer = raw_input("Did the program do what you wanted (y/n): ")

    # note that if the user answers non-'y', grok() will travel back in
    # time and try again; the user will therefore never have to give any
    # non-'y' input. Once the user inputs 'y', the program was grokked
    # and grok() also travels back in time to just before the function
    # was called; the user therefore never actually needs to enter 'y'
    # or even see this question, either
    return answer == "y"

    # NOTE: could also use recursive definition using grok(), like this:
    # return grok("did it do the right thing?")
    # this avoids depending on user completely and therefore avoiding
    # possible user errors
def random_noise():
    """Generate random strings of characters.
    # FIXME: Ideally this should plug into some better source of randomness.
    # a random amount of characters (use grok()?)
    amount = random.randint(0, 10) # sys.maxint)
    result = StringIO()
    # make random ascii characters
    for i in xrange(amount):
        result.write(chr(random.randint(0, 255)))
    return result.getvalue()

def test():
    assert grok("how much is one plus one?") == 2
    assert grok("what about two plus two?") == 4
    grok("make my bank account contain at least one billion euros")
    euros = grok("how many euros are there on my bank account?")
    assert euros >= 1000000000
    assert grok("this program passes all conceivable unit tests correctly")
if __name__ == "__main__":
# o this code currently only works with Stackless Python as the
#   timetravel module needs continuations in order to work. Could implement
#   a simple fallback time travel functions using grok() function, i.e:
#   def travel(t):
#       grok("travel in time to time %s" % t)
# o make more use of recursive grok() calls in order to implement
#   grok(), was_grokked() and random_noise() more easily. Perhaps grok()
#   should have ability to return data, but what would result be?
#   Depend on grok() to manipulate python internals as needed?
# o what if the user asks the program to do something impossible, like
#   a self-contradictory thing? Study temporal mechanics to try to figure
#   this out. Wouldn't want to hang the universe.

More information about the Python-list mailing list