access the name of my method inside it

J

james_027

Hi,

for example I have this method

def my_method():
# do something

# how do I get the name of this method which is my_method here?

Thanks,
james
 
M

Marc 'BlackJack' Rintsch

for example I have this method

def my_method():
# do something

# how do I get the name of this method which is my_method here?

Why do you need this? There are ways but those are not really good for
production code.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steve Holden

Marc said:
Why do you need this? There are ways but those are not really good for
production code.
Maybe he wants to write a recursive method?

Once way is to call self.__calss__.mymethod(self). Ugly, isn't it?
.... def mymethod(self, n):
.... if n <= 1:
.... return 1
.... else:
.... return n * self.__class__.mymethod(self, n-1)
....
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
M

Marc 'BlackJack' Rintsch

Maybe he wants to write a recursive method?

Once way is to call self.__calss__.mymethod(self). Ugly, isn't it?

Ugly yes, unnecessary convoluted yes, solution no. You typed `my_method`
in the source. The OP wants to know how to avoid that.
... def mymethod(self, n):
... if n <= 1:
... return 1
... else:
... return n * self.__class__.mymethod(self, n-1)

Why not simply ``self.mymethod(n - 1)`` instead!?

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steve Holden

Marc said:
Ugly yes, unnecessary convoluted yes, solution no. You typed `my_method`
in the source. The OP wants to know how to avoid that.


Why not simply ``self.mymethod(n - 1)`` instead!?
Well, absolutely no reason if you want to go making things simple ;-)

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
J

james_027

Hi,

Why do you need this? There are ways but those are not really good for
production code.

I am going to use this in Django. I am trying to implement a
permission here, where in the database store the methods that the user
are allowed to execute. for example if the method is def
create_event(): the method will look for create_event in the database
to see if it allow to be execute.

Thanks.
james
 
J

Jay Loden

james_027 said:
Hi,



I am going to use this in Django. I am trying to implement a
permission here, where in the database store the methods that the user
are allowed to execute. for example if the method is def
create_event(): the method will look for create_event in the database
to see if it allow to be execute.

This might help, I used it for a similar need I had in the past where I needed a method to know the name it was called with

class TestClass:
def __init__(self):
pass

def __getattr__(self, name):
try:
return getattr(self.__class__, name)
except AttributeError:
return functools.partial(self.foo, name)

def foo(self, name, **args):
print name
for i in args:
print " %s=%s" % (i, args)
 
P

Paul McGuire

Hi,




I am going to use this in Django. I am trying to implement a
permission here, where in the database store the methods that the user
are allowed to execute. for example if the method is def
create_event(): the method will look for create_event in the database
to see if it allow to be execute.

Thanks.
james

How about using a decorator? Here is a rough version:

def checkPrivs(fn):
fnName = fn.func_name
def restricted(*args):
print "about to call function", fnName
if fnName in listOfAllowedFunctions:
return fn(*args)
else:
raise KeyError("you don't have sufficient privileges to do
THAT")
return restricted

listOfAllowedFunctions = ['add','subtract']

@checkPrivs
def add(a,b):
return a+b

@checkPrivs
def subtract(a,b):
return a-b

@checkPrivs
def multiply(a,b):
return a*b

add(1,2)
subtract(4,1)
multiply(3,2)

-- Paul
 
B

Bruno Desthuilliers

james_027 a écrit :
Hi,



I am going to use this in Django. I am trying to implement a
permission here, where in the database store the methods that the user
are allowed to execute. for example if the method is def
create_event(): the method will look for create_event in the database
to see if it allow to be execute.

Then the solution is definitively to use a decorator, cf Paul's answer.
 
C

Cousin Stanley

I am going to use this in Django. I am trying to implement
a permission here, where in the database store the methods
that the user are allowed to execute.

for example if the method is def create_event() :
the method will look for create_event in the database
to see if it allow to be execute.

james ....

Perhaps a simple-minded hard-coded solution
might work ....

def permission_check( name ) :

permission = DB.permission_check( name )

if not permission :

print 'No Execute Permission for %s ' % name

sys.exit( -1 )

def my_method() :

permission_check( "my_method" )

....
 
B

Bruno Desthuilliers

Cousin Stanley a écrit :
james ....

Perhaps a simple-minded hard-coded solution
might work ....

def permission_check( name ) :

permission = DB.permission_check( name )

if not permission :

print 'No Execute Permission for %s ' % name

sys.exit( -1 )

An exception would be better here.
def my_method() :

permission_check( "my_method" )

....

May I suggest ?

class Unauthorized(Exception): pass

def check_permission(permission):
if not DB.check_permission(permission):
raise Unauthorized(permission)

def requires_perm(func):
def with_check(*args, **kw):
check_permission(func.__name__)
return func(*args, **kw)
return with_check

@requires_perm
def my_method():
# do something useful here
 
E

Evan Klitzke

Hi,



I am going to use this in Django. I am trying to implement a
permission here, where in the database store the methods that the user
are allowed to execute. for example if the method is def
create_event(): the method will look for create_event in the database
to see if it allow to be execute.

Thanks.
james

I would suggest instead setting an attribute on the function object
via a decorator, and then have your mod_python handler check for the
presence of that attribute on the function that is being called,
rather than doing a DB lookup.
 

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
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top