Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Determine attributes of calling method

Reply
Thread Tools

Determine attributes of calling method

 
 
Joe
Guest
Posts: n/a
 
      06-03-2011
Hello,

I'm trying to implement a way to restrict method usage based on the
caller's attributes. In the following example I'd like to execute the
server method "bar" only if the caller's method has a "blue" value for
it's color attribute.

The current output is:

blue
red
bar
bar

I'd like it to be:

blue
red
bar

I've worked my way through inspect but that doesn't seem to be the
right approach.

# Example
class Client:

def __init__(self, server):
self.server=server

def foo(self):
self.server.bar()

def fu(self):
self.server.bar()

foo.__dict__['color']='blue'
fu.__dict__['color']='red'

class BlueServer:

def bar(self):
"""
Goal is to only accept calls from "blue" client methods.
Don't know how to do it
"""
print "bar"

s=BlueServer()
c=Client(s)
print c.foo.color
print c.fu.color
c.foo()
c.fu()

Thanks for your help!

Joe
 
Reply With Quote
 
 
 
 
Richard Thomas
Guest
Posts: n/a
 
      06-04-2011
On Jun 3, 9:35*pm, Joe <(E-Mail Removed)> wrote:
> Hello,
>
> I'm trying to implement a way to restrict method usage based on the
> caller's attributes. *In the following example I'd like to execute the
> server method "bar" only if the caller's method has a "blue" value for
> it's color attribute.
>
> The current output is:
>
> blue
> red
> bar
> bar
>
> I'd like it to be:
>
> blue
> red
> bar
>
> I've worked my way through inspect but that doesn't seem to be the
> right approach.
>
> # Example
> class Client:
>
> * * def __init__(self, server):
> * * * * self.server=server
>
> * * def foo(self):
> * * * * self.server.bar()
>
> * * def fu(self):
> * * * * self.server.bar()
>
> * * foo.__dict__['color']='blue'
> * * fu.__dict__['color']='red'
>
> class BlueServer:
>
> * * def bar(self):
> * * * * """
> * * * * Goal is to only accept calls from "blue" client methods.
> * * * * Don't know how to do it
> * * * * """
> * * * * print "bar"
>
> s=BlueServer()
> c=Client(s)
> print c.foo.color
> print c.fu.color
> c.foo()
> c.fu()
>
> Thanks for your help!
>
> Joe


It is possible to get access to the code object of the parent
execution frame. Function objects are just wrappers for code objects
and are not part of the frame. You could do something with that
though...

_colors = {}
def color(c):
def decorator(f):
code = f.func_code
key = code.co_filename, code.co_firstlineno
_colors.setdefault(key, set()).add(c)
return f
return decorator

def require(c):
import sys
code = sys._getframe().f_back.f_back.f_code
key = code.co_filename, code.co_firstlineno
assert c in _colors.get(key, ())

def tryit():
require("blue")

@color("blue")
def bluefunc():
tryit()

@color("red")
def redfunc():
tryit()

Executing untrusted Python code is nigh impossible to make secure of
course. I'd suggest examining whether this is necessary.
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      06-04-2011
On Fri, Jun 3, 2011 at 2:35 PM, Joe <(E-Mail Removed)> wrote:
> * *foo.__dict__['color']='blue'
> * *fu.__dict__['color']='red'


You don't need to use __dict__ to set function attributes. Just do:

foo.color = 'blue'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
data attributes override method attributes? Jayden Python 16 09-29-2012 03:20 AM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM
How can I determine the property attributes on a class or instance? mrdylan Python 3 04-12-2006 05:23 AM
Calling a method of the calling object ... why-em-jay Perl Misc 3 09-15-2005 07:53 PM
nuby: determine method passed and determine the receiver that received the method Peņa, Botp Ruby 1 01-24-2004 07:51 PM



Advertisments