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 12 1082
On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
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
Marc 'BlackJack' Rintsch wrote:
On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
>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.
Maybe he wants to write a recursive method?
Once way is to call self.__calss__.mymethod(self). Ugly, isn't it?
>>class p:
.... def mymethod(self, n):
.... if n <= 1:
.... return 1
.... else:
.... return n * self.__class__.mymethod(self, n-1)
....
>>pp = p() pp.mymethod(10)
3628800
>>>
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 -------------
On Wed, 01 Aug 2007 07:01:42 -0400, Steve Holden wrote:
Marc 'BlackJack' Rintsch wrote:
>On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
>>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.
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.
>>class p:
... 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
Marc 'BlackJack' Rintsch wrote:
On Wed, 01 Aug 2007 07:01:42 -0400, Steve Holden wrote:
>Marc 'BlackJack' Rintsch wrote:
>>On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
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.
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.
> >>class p:
... 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!?
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 -------------
Hi,
On Aug 1, 5:18 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
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.
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
On Aug 1, 8:07 am, james_027 <cai.hai...@gmail.comwrote:
Hi,
On Aug 1, 5:18 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
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.
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
james_027 wrote:
Hi,
On Aug 1, 5:18 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
>>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.
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[i])
james_027 a écrit :
Hi,
On Aug 1, 5:18 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
>>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.
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.
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.
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" )
....
--
Stanley C. Kitching
Human Being
Phoenix, Arizona
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Cousin Stanley a écrit :
>>>>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. 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 )
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
Hi all!
Thanks for all your idea, this community is truly a great one!
james
On 8/1/07, james_027 <ca********@gmail.comwrote:
Hi,
On Aug 1, 5:18 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
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.
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.
--
Evan Klitzke <ev**@yelp.com> This discussion thread is closed Replies have been disabled for this discussion. Similar topics
24 posts
views
Thread by jason |
last post: by
|
1 post
views
Thread by bill yeager |
last post: by
|
1 post
views
Thread by rodrigo |
last post: by
|
10 posts
views
Thread by Davíð Þórisson |
last post: by
|
4 posts
views
Thread by James |
last post: by
|
11 posts
views
Thread by glen.coates.bigworld |
last post: by
|
5 posts
views
Thread by Anil Gupte |
last post: by
|
9 posts
views
Thread by JT |
last post: by
|
7 posts
views
Thread by Andy B |
last post: by
| | | | | | | | | | |