completely implicit interpolation based on a frame object (nn)

Edward Peschko horos11 at gmail.com
Thu Dec 9 16:37:40 EST 2010


>> a bit closer here, but I'm not sure if they are
>> workable (or usable) with 2.5... which is where I need to work.
>>
>> Ed
>
> One of the solutions from here might work for you:
>
> http://wiki.python.org/moin/Templating
>

Ok, cool. A couple of followups - I'd be interested in knowing which
of the solutions listed there was:

1. lightweight
2. usable with 2.5 (and if possible 2.3,2.2, and 2.1)
3. able to do inplace expansion of arrays and/or hashes, using
arbitrary functions, in a simplistic way (perhaps like formats and the
{var!func} syntax)

I'll read up on them, but as it is with perl, you can spend a lot of
time fiddling around with a module to find out that the implementation
doesn't support the featureset that you are looking for, or otherwise
is just plain junk, so pointers are definitely welcome on which one to
use.

--

Anyways, the problem isn't completely solved here. Since it is a
free-flowing string that needs to be interpolated, there needs to be
some way of picking out the identifiers that are to be evaluated and
turning them into a template. In the following code:

person="mary"
print "then along came a person named ", person

this has to evaluate to

print "then along came a person named ", mary

There's also subtleties here - if foo.somesuch is an attribute, there
is no chance of a side effect, but if foo.somesuch is a property, then
it could inadvertently cause side effects, and only one side effect
can totally ruin your day (that was the bulk of the complexity in the
perl version of this, ensuring no side effects in the expanded vars)

Likewise, if a line of code spans multiple lines, you may have
identifiers that span these multiple lines which means you may miss
some substitutions.

So, again, I'm interested in ways of getting around these problems. In
perl its very easy to do this because of sigils, would very much like
to know how to do the same thing in python.

thanks much,

Ed

(ps - this is what I've got so far, which is basically cobbled
together from stuff on stackoverflow. Works ok, but lines #15-#16
AFAICT are the one that needs to be bulletproofed:

1   import sys
2   import linecache
3   import random
4
5   def traceit(frame, event, arg):
6       if event == "line":
7           lineno = frame.f_lineno
8           filename = frame.f_globals["__file__"]
9           if filename == "<stdin>":
10            filename = "traceit.py"
11        if (filename.endswith(".pyc") or
12            filename.endswith(".pyo")):
13            filename = filename[:-1]
14        name = frame.f_globals["__name__"]
15        line = linecache.getline(filename, lineno)
16        print "%s:%s:%s: %s" % (name,
lineno,frame.f_code.co_name,line.rstrip())
17    return traceit
18
20 sys.settrace(traceit)
21 main()

)

(
pps - is there a collection of tracers somewhere? This seems
definitely the thing that you'd want to collect in a library
somewhere, and switch tracers as you see fit depending on what you are
looking for..
)



More information about the Python-list mailing list