| 1 | import gc |
|---|
| 2 | from itertools import groupby |
|---|
| 3 | from genshi.template.base import Context |
|---|
| 4 | from genshi.template.eval import Expression |
|---|
| 5 | |
|---|
| 6 | expr2 = Expression("say") |
|---|
| 7 | expr1 = Expression("iter(groupby(test, key=lambda x: len(x)))") |
|---|
| 8 | |
|---|
| 9 | def run(**data): |
|---|
| 10 | ctxt = Context(**data) |
|---|
| 11 | for i, j in expr1.evaluate(ctxt): |
|---|
| 12 | ctxt.push(dict(i=i, j=j)) |
|---|
| 13 | yield expr2.evaluate(ctxt) |
|---|
| 14 | ctxt.pop() |
|---|
| 15 | |
|---|
| 16 | i = 2 |
|---|
| 17 | while i: |
|---|
| 18 | gc.collect() |
|---|
| 19 | num_objects = len(gc.get_objects()) |
|---|
| 20 | try: |
|---|
| 21 | list(run(test=dict.fromkeys(map(str, range(10, 100))), groupby=groupby)) |
|---|
| 22 | except Exception, e: |
|---|
| 23 | print e |
|---|
| 24 | i -= 1 # 2 passes needed for reaching fix-point |
|---|
| 25 | |
|---|
| 26 | # check the memory usage again |
|---|
| 27 | gc.collect() |
|---|
| 28 | print num_objects, len(gc.get_objects()) |
|---|