Intercepting binding?

A

andrew cooke

This is a bit vague, I'm afraid, but is there any way for me to take
code like:

a = Foo()
beta = Bar()

and somehow attach the string "a" to the Foo instance and "beta" to
the Bar instance. At some later point in the program I want to be
able to look at the Bar instance and say to the user "this was called
beta in your routine".

The motivation is debugging an embedded domain specific language and
the solution has to be cross platform for Python 3+ (bonus points for
Python 2 too).

Obviously I can parse the code, but I was wondering if there was some
other (no doubt terribly hacky) approach. Even some idea of what to
google for would be a help...

Thanks,
Andrew
 
A

andrew cooke

For example, I assume it's possible to somehow access the dictionary
for the current block, but I can't see how to do this after
assignment. If I do it in the Foo constructor, for example, "a" will
not yet be bound.

This is a bit vague, I'm afraid, but is there any way for me to take
code like:

   a = Foo()
   beta = Bar()

and somehow attach the string "a" to the Foo instance and "beta" to
the Bar instance.  At some later point in the program I want to be
[..]
 
A

andrew cooke

eggs[42] = Foo()
beans['spam'] = Foo()
chips.spam = Foo()
spam[eggs.beans['chips']] = Foo()
spam.append(Foo())

these are valid points, but in practice the main use (for the
restricted application i care about) is si,ple variables, and this is
an "optional extra" to help the user, so it's OK if it only works
sometimes. at the moment i'd be happy with any half-baked unreliable
solution that is transparent...

andrew
 
D

Dave Angel

andrew said:
This is a bit vague, I'm afraid, but is there any way for me to take
code like:

a = Foo()
beta = Bar()

and somehow attach the string "a" to the Foo instance and "beta" to
the Bar instance. At some later point in the program I want to be
able to look at the Bar instance and say to the user "this was called
beta in your routine".

The motivation is debugging an embedded domain specific language and
the solution has to be cross platform for Python 3+ (bonus points for
Python 2 too).

Obviously I can parse the code, but I was wondering if there was some
other (no doubt terribly hacky) approach. Even some idea of what to
google for would be a help...

Thanks,
Andrew
This comes up periodically in this list, and the answer is always
something like: you can't get there from here. As you have noticed, it
can't be done in the __init__() of the object, since the symbol it'll be
bound to doesn't generally exist yet, and even if it does, there's no
way to know which one it is.

You could write a function that the user could voluntarily call at the
end of his function, that would find all symbols of a specified kind,
and store the kind of information you're asking about. But of course,
some class instances should not be added to, and some cannot. So you'd
need some form of filter to indicate which ones to do.

In the sample below, I'll assume you want to do this for all classes
derived from Mybase.

(untested):

def label_stuff(symbols):
for symbol in symbols:
if isinstance(symbols[symbol], Mybase):
symbols[symbol].bind_name = symbol

And the user would simply make a call at the end of each such function,
like:

def my_func():
a = ...
b = ...
label_stuff(locals)
 
A

andrew cooke

This comes up periodically in this list, and the answer is always
something like:  you can't get there from here.

Well, I'm both flexible and desperate, so this is a possible route
(perhaps near enough):


import sys

class Foo(object):

def __rlshift__(self, name):
try:
raise Exception()
except:
locals = sys.exc_traceback.tb_frame.f_back.f_locals
locals[name] = self

if __name__ == '__main__':
foo = Foo()
'a' << foo
print(a)


andrew
 
A

andrew cooke

for the record, googling for "f_back.f_locals" reveals a wide variety
of similar hacks and also a cleaner way to access the current frame:
inspect.currentframe()
 
S

Steven D'Aprano

these are valid points, but in practice the main use (for the restricted
application i care about) is si,ple variables, and this is an "optional
extra" to help the user, so it's OK if it only works sometimes. at the
moment i'd be happy with any half-baked unreliable solution that is
transparent...

Speaking as a user (although not of Andrew's domain specific language),
I'd like to say to developers PLEASE PLEASE PLEASE don't try to "help me"
with half-baked unreliable solutions that only work sometimes.

There's few things worse than unreliable tools that break just when
you've come to rely on them.
 
C

Carl Banks

I apologise for failing to notice earlier that you know what you're
talking about.  I blame the hour :)

I'm not sure you can access the namespace dictionary of the "current
block" (module?), that's the problem.  Oh, except via locals(), which
might do exactly what you're after depending.  Excuse me, I'm being
very dim tonight.


Hmmm.


@contextlib.contextmanager
def capture_changed_bindings():
before = sys._getframe(2).f_locals.copy()
changed = {}
yield changed
after = sys._getframe(2).f_locals
for key,value in after.iteritems():
if value is changed:
continue
if key not in before or value is not before[key]:
changed[key] = value

def test():
a = 2
b = 3
c = 4
with capture_changed_bindings() as changed:
b = 5
c = 4
d = 6
print changed

test()


Quick and dirty, not robust at all. But you get the idea.

Carl Banks
 
C

Carl Banks

This comes up periodically in this list, and the answer is always
something like:  you can't get there from here.

Well, I'm both flexible and desperate, so this is a possible route
(perhaps near enough):

import sys

class Foo(object):

    def __rlshift__(self, name):
        try:
            raise Exception()
        except:
            locals = sys.exc_traceback.tb_frame.f_back.f_locals
            locals[name] = self

if __name__ == '__main__':
    foo = Foo()
    'a' << foo
    print(a)

Did you try this inside a function? (Hint: it won't work in a
function.)

BTW, if you are desperate to do this then I'd say you lack good
perspective. You are subverting some of the most basic behavior of
Python here for something of marginal and only occasional usefulness.
If you are that desperate just retype the name, it won't be the end of
the world.

a = Foo('a')


Carl Banks
 
A

andrew cooke

    with capture_changed_bindings() as changed:
        b = 5
        c = 4
        d = 6
    print changed

test()

Quick and dirty, not robust at all.  But you get the idea.

Carl Banks

brilliant. using the with context is an excellent idea. thanks very
much.

andrew
 
A

andrew cooke

Speaking as a user (although not of Andrew's domain specific language),
I'd like to say to developers PLEASE PLEASE PLEASE don't try to "help me"
with half-baked unreliable solutions that only work sometimes.

There's few things worse than unreliable tools that break just when
you've come to rely on them.

The context is that I am looking at how best to provide debugging
support for a recursive descent parser. If the actions are pure then
the whole thing is deterministic, so it might be possible to do
something like "buddha" (a declarative debugger for haskell). I'm
also looking for a paper that was linked to somewhere this last month
or so on one of the popular sites/blogs about debugging prolog (since
the two are closely related - depth first search etc) - I know this is
vague, but if it rings a bell with anyone... Anyway, doing this in
pure Python makes risks losing the information associated with
variable names. I was wondering how I might reduce that.

The reason I asked for "unreliable half-baked" solutions is that I am
exploring what might be possible and, in my experience, this group
prefers to lecture me on what they think I should do rather than
answer the damn question. I was hoping that by explicitly saying that
reliability is not important, people might feel more free to give
"wild" ideas that I could learn from and improve on.

It's significant, depressing, and not at all surprising that every
person who replied to this thread told me, in one way or another, that
was I was asking was wrong or impossible or foolhardy.

If I want your advice on how to write popular tools, you can be sure I
will come and ask you for it. That is not what I asked for here.

But since we're all passing round unsolicited advice, here's some from
me.

Being an anal retentive will get you a long way in programming.
Dotting the "i"s and crossing the "t"s is 99% of what it's all about.
I agree. But the last 1% requires a little bit of imagination. You
should try it one day.

Andrew
 
C

Carl Banks

It's significant, depressing, and not at all surprising that every
person who replied to this thread told me, in one way or another, that
was I was asking was wrong or impossible or foolhardy.

People on this list are volunteers, who have no obligation to help.

If you want always friendly, enabling advice I'm sure there are good
support services out there who would be happy not to second-guess you
for a fee.

Otherwise, grow thicker skin.


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top