Changeset 354

Show
Ignore:
Timestamp:
06/21/08 23:00:10 (4 years ago)
Author:
simon
Message:

Clean up talk utilities.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • hodgestar/Talks/PythonObjects/util.py

    r343 r354  
     1"""Utility module for use with pyobjects talk.""" 
     2 
    13import gc 
    24import random 
     
    68import sys 
    79 
    8 os.chdir('/home/simon/LocalProjects/PythonSpint/trunk') 
     10# 
     11# Old vs New Classes and Slots 
     12# 
    913 
    1014class Old: 
     15    """A classic or old-style class.""" 
    1116    pass 
    1217 
    1318class New(object): 
     19    """A new-style class with a useful type.""" 
    1420    pass 
    1521 
    1622class FuBar(object): 
     23    """Illustrates the difference between __str__ and 
     24       __unicode__. __str__ has a slot (tp_str) while 
     25       __unicode__ does not. 
     26       """ 
    1727    def __str__(self): 
    1828        return "fubar" 
     
    2131 
    2232class OldFu: 
     33    """Like FuBar, but showing it with classic classes.""" 
    2334    def __str__(self): 
    2435        return "fubar" 
     
    2637        return unicode("fubar") 
    2738 
     39# 
     40# Grabbing the interned string dictionary 
     41# and a simple Python keyword game 
     42# 
     43 
     44PYTHON_TRUNK_CHECKOUT = '/home/simon/LocalProjects/PythonSpint/trunk' 
     45 
    2846def find_big_dict(x): 
     47    """Use gc to find the first dict with len greater than 1000 
     48       that references x. 
     49       """ 
    2950    for obj in gc.get_referrers(x): 
    3051        if type(obj) is dict and len(obj) > 1000: 
     
    3354 
    3455def find_str_interns(): 
     56    """Find interned string dict by looking for a big dict that 
     57       refers to "a". 
     58       """ 
    3559    return find_big_dict("a") 
    3660 
    3761def keyword_quiz(): 
     62    """Looks up upper case keywords in the interned string dict 
     63       and asks users to guess which part of Python they come 
     64       from. Prints answers by grepping Python source. 
     65       """ 
    3866    interned = find_big_dict("a") 
    3967    keywords = [x for x in interned if x.isupper() and len(x) > 4] 
    4068    question = random.choice(keywords) 
    4169 
    42     egrep = subprocess.Popen(["egrep", "-R", "-l", "--exclude-dir", ".svn", 
     70    pwd = os.getcwd() 
     71    try: 
     72        os.chdir(PYTHON_TRUNK_CHECKOUT) 
     73        egrep = subprocess.Popen(["egrep", "-R", "-l", "--exclude-dir", ".svn", 
    4374                              "--include", "*.c", "--include", "*.h", "--include", "*.py", 
    4475                              '%s' % question, 
    4576                              "Modules", "Lib"], stdout=subprocess.PIPE) 
    46     answer, err = egrep.communicate() 
     77        answer, err = egrep.communicate() 
     78    finally: 
     79        os.chdir(pwd) 
    4780 
    4881    print ("Which module does the keyword " + question + " appear in?") 
     
    5588    print (answer) 
    5689 
    57 # From: 
    58 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286222 
    59 # 
     90# Recipe for measuring process memory usage, taken from 
     91# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286222. 
     92# According to http://aspn.activestate.com/ASPN/Python/Cookbook/ 
     93# licensed under the Python license unless otherwise stated. 
     94# I'm taking this to mean Python license 2.1 or newer which is 
     95# GPL compatible. 
    6096 
    6197_proc_status = '/proc/' + str(os.getpid()) + '/status'