There are several coding patterns in Vitalije's prototype that deserve
mention.
*Generators*
['abc'].extend(None) throws a TypeError. However, the following works:
def a_generator():
if 0:
yield 'abc' # Makes the function a generator
return None
aList = ['abc']
aList.extend(a_generator())
print(aList) # prints ['abc']
Returning None from a generator is, iirc, equivalent to raising
StopIteration.
*Generators as predicates*
Vitalije's prototype (as modified in leoCheck.py) contains this:
for node in ast.walk(root):
classes_list.extend(do_class(node))
This is exquisite code. It's perfect for summarizing code. I shall
rewrite the ShowData class using this pattern. It should be possible to
eliminate the ShowDataTraverser class, a big collapse in complexity. Note
that in this case the fact that ast.walk traverses the tree in "no
particular order" does not matter.
*Using defaultdict*
This code is elegant. The first arg to defaultdict is a default factory
function. All other args are passed to the dict constructor.
from collections import defaultdict
default_factory = lambda:dict(ivars={}, methods={})
known_classes = defaultdict(default_factory, **known_classes0())
Edward
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.