I have a class A with method do_something(self,a,b,c) and another instance method that validates the input and check permissions named can_do_something(self,a,b,c).
This is a common pattern in my code and I want to write a decorator that accepts a validation function name and perform the test.
def validate_input(validation_fn_name):
def validation_decorator(func):
def validate_input_action(self,*args):
error = getattr(self,validation_fn_name)(*args)
if not error == True:
raise error
else:
return func(*args)
return validate_input_action
return validation_decorator
Invoking the functions as follows
#validate_input('can_do_something')
def do_something(self,a,b,c):
return a + b + c
Problem is that i'm not sure how to maintain self through out the validation function. I've used the validation fn name with getattr so the fn could be ran in the context of the instance but i cannot do that for func(*args).
So what is the proper way to achieve this ?
Thanks.
EDIT
So following #André Laszlo answer I realized that self is just the first argument so there is no need to use getattr at all but just pass on the *args.
def validate_input(validation_fn):
def validation_decorator(func):
def validate_input_action(*args):
error = validation_fn(*args)
if not error == True:
raise error
else:
return func(*args)
return validate_input_action
return validation_decorator
Much more elegant and it also supports static methods as well.
Adding a static method to #André Laszlo example proves the decorator is working :
class Foo(object):
#staticmethod
def validate_baz(a,b,c):
if a > b:
return ValueError('a gt b')
#staticmethod
#validate_input(Foo.validate_baz)
def baz(a,b,c):
print a,b,c
>>> Foo.baz(1,2,3)
1 2 3
>>> Foo.baz(2,1,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in validate_input_action
ValueError: a gt b
But, when i'm trying to do them same thing in a django model:
from django.db import models
from django.conf import settings
settings.configure()
class Dummy(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=10)
def can_say_name(self):
if name is None:
return Exception('Does not have a name')
#validate_input(can_say_name)
def say_name(self):
print self.name
#staticmethod
def can_create_dummy(name):
if name == 'noname':
return Exception('No name is not a name !')
#staticmethod
#validate_input(Dummy.can_create_dummy)
def create_dummy(name):
return Dummy.objects.create(name=name)
I get the following :
NameError: name 'Dummy' is not defined
So what is the different between a django model and an Object in relation to this issue ?
I think this does what you want:
def validate_input(validation_fn_name):
def validation_decorator(func):
def validate_input_action(self, *args):
error = getattr(self, validation_fn_name)(*args)
if error is not None:
raise error
else:
arglist = [self] + list(args)
return func(*arglist)
return validate_input_action
return validation_decorator
class Foo(object):
def validate_length(self, arg1):
if len(arg1) < 3:
return ValueError('%r is too short' % arg1)
#validate_input('validate_length')
def bar(self, arg1):
print "Arg1 is %r" % arg1
if __name__ == "__main__":
f = Foo()
f.bar('hello')
f.bar('')
Output is:
Arg1 is 'hello'
Traceback (most recent call last):
File "validator.py", line 27, in <module>
f.bar('')
File "validator.py", line 6, in validate_input_action
raise error
ValueError: '' is too short
Updated answer
The error (NameError: name 'Dummy' is not defined) occurs because the Dummy class is not defined yet when the validate_input decorator gets Dummy as an argument. I guess this could have been implemented differently, but for now that's the way Python works. The easiest solution that I see is to stick to using getattr, which will work because it looks up the method at run time.
Related
In Python, when I'm defining a function inside a class, I can include self as one of the arguments to access the member variables of that class, and I can also choose not include self as the argument if I don't need to access its member variables. But then I discovered that if the function does not have self as arguments, then it becomes invisible to other functions in that class. For example
class Test:
def __init__(self, val:int) -> None:
self.a = val
def f(a:int) -> int:
return a + 1
def g(self) -> int:
return f(self.a)
if __name__ == '__main__':
t = Test(2)
print(t.g())
The above codes will lead to the following error message:
Traceback (most recent call last):
File "/Users/louchenfei/Downloads/lc.py", line 11, in <module>
print(t.g())
File "/Users/louchenfei/Downloads/lc.py", line 7, in g
return f(self.a)
NameError: name 'f' is not defined
I wonder why that's the case and what are the rules for visibilities of functions defined in a class?
Its difficult to know what you want, but one solution is to make f a staticmethod. Still you need to have a reference to Test in some way:
class Test:
def __init__(self, val:int) -> None:
self.a = val
#staticmethod
def f(a:int) -> int:
return a + 1
def g(self) -> int:
return self.f(self.a)
if __name__ == '__main__':
t = Test(2)
print(t.g())
Here, the call is self.f(self.a).
The method f is not in scope inside another method.
I was wondering what the best way to implement the following design would be in Python:
class Executor:
def __init__(self):
self.val = 5
def action(self):
self.action(self.val)
#classmethod
def action(cls, val):
print(f"Val is: {val}")
I want to be able to access the method both as an instance method that uses the value the object was initialised with, and as a class method which uses a passed in variable. Here is an example of the ways in which I would like to call it:
>>> Executor.action(3)
Val is: 3
>>> Executor().action()
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: action() missing 1 required positional argument: 'val'
I was thinking about trying to use keyword arguments, but I can't seem to get that to work either. Here is my code so far:
class Executor:
def __init__(self):
self.val = 5
#classmethod
def action(cls, val=None):
if val is None:
# This doesn't work; cls does not have attribute 'val'.
if hasattr(cls, "val"):
print(f"Val from instance: {cls.val}")
else:
raise ValueError("Called as class method and val not passed in.")
else:
print(f"Val passed in: {val}")
>>> Executor.action(3)
Val passed in: 3
>>> Executor().action()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 13, in action
ValueError: Called as class method and val not passed in.
But the class instance does not have the val available for access.
The only other thing I can think of is using Hungarian notation, but it's not ideal as it's a bit messy, and it means there's multiple method names.
class Executor:
def __init__(self):
self.val = 5
def action_instance(self):
self.action_class(self.val)
#classmethod
def action_class(cls, val):
print(f"Val is: {val}")
>>> Executor.action_class(3)
Val is: 3
>>> Executor().action_instance()
Val is: 5
Any advice on a solid, clean approach would be greatly appreciated!
Cheers.
What you want to do looks strange to me, I am not sure you need that. Python cannot overload by method signatures/type, although there is the functools.singledispatch. So by defining action a second time you are actually replacing the first definition for the method.
The observable behaviour can be achieved with:
class Executor:
#classmethod
def action(cls, val=5):
return val
print(Executor().action())
print(Executor.action(3))
Outputs:
5
3
But again check first that you really need something like that, because it breaks one of the expectations of Python coders and Python data model: calling a method through the class is equivalent to calling the method through the instance given that you pass the instance to the class method.
obj = Executor()
obj.action() # this is the same as Executor.action(obj)
class String(object):
def __init__(self, text):
self.text = text
def __repr__(self):
return "{}".format(self.text)
def reverse(self):
return self.text[::-1]
def isPalindrome(self):
return (self.reverse(self.text) == self.text)
def main():
string = String(input("Write a String: "))
if(string.isPalindrome()):
print("The string {{}} IS a Palindrome".format(string))
else:
print("The string {{}} is NOT Palindrome".format(string))
I have this classe that represents a String and i want to check is a object is a palindrome by calling the method isPalindrome. But when i call the string.isPalindrome i get this error:
Traceback (most recent call last):
File "palindrome.py", line 23, in <module>
main()
File "palindrome.py", line 17, in main
if(string.isPalindrome()):
File "palindrome.py", line 12, in isPalindrome
return (self.reverse(self.text) == self.text)
TypeError: reverse() takes 1 positional argument but 2 were given
The error comes from this line:
return (self.reverse(self.text) == self.text)
change it for this:
return (self.reverse() == self.text)
self can be really confusing, here is a really good article to understand how it works. Just so you understand, look at reverse() definition:
def reverse(self):
return self.text[::-1]
As you can see, self.text is already assigned. No need to pass it to the function when calling it.
I have a construction like:
def __init__(self, my_param):
if my_param:
self.my_param = my_param
else:
raise MYException()
and I am using this value somewhere like value=self.my_param
but when I dont pass the my_param it doesnot raise exception but says class object does not have my_param
How can I raise it properly?
Actually When you don't pass an argument to your function, raising an TypeError exception is exactly what python does by itself, which I think There is no need to override this exception :) :
TypeError: __init__() takes exactly 2 arguments (1 given)
But you can also use *args to pass a tuple of arguments to your constructor in order to check the validity of args which will pass an empty tuple if you don't pass anything:
class a(object):
def __init__(self, *args):
if args:
self.param = args[0]
else:
raise Exception("Please pass a parameter to instance")
Also as a practical and not pythonic approach you can use a decorator to wrap your constructor:
def my_exp(func):
def wrapper(*args, **kwds):
if args:
return func(*args)
else:
raise Exception("Pleas pass an argument to instance")
return wrapper()
class a(object):
#my_exp
def __init__(self, my_param):
self.param = my_param
Demo :
instance = a()
Traceback (most recent call last):
File "/home/kasra/Desktop/ex2.py", line 11, in <module>
class a(object):
File "/home/kasra/Desktop/ex2.py", line 12, in a
#my_exp
File "/home/kasra/Desktop/ex2.py", line 9, in my_exp
return wrapper()
File "/home/kasra/Desktop/ex2.py", line 8, in wrapper
raise Exception("Pleas pass an argument to instance")
Exception: Pleas pass an argument to instance
You can also use another tools like functools.wraps or etc. in order to create more flexible decorators. But still I recommend to let python does the job for you!
This should work,
def __init__(self, my_param=None):
if my_param:
self.my_param = my_param
else:
raise MYException()
So I've looked at similar questions, and I've found some solutions to this, but I can't quite figure out how to do this.
What I'm trying to do is add a method to a class from a string. I can do this with the setattr() method, but that won't let me use self as an attribute in the extra method. Here's an example: (and I apologize for the variable names, I always use yolo when I'm mocking up an idea)
class what:
def __init__(self):
s = 'def yolo(self):\n\tself.extra = "Hello"\n\tprint self.extra'
exec(s)
setattr(self,"yolo",yolo)
what().yolo()
returns this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: yolo() takes exactly 1 argument (0 given)
and if s = 'def yolo():\n\tself.extra = "Hello"\n\tprint self.extra'
then I get this result:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 2, in yolo
NameError: global name 'self' is not defined
This essentially means that I cannot dynamically create methods for classes, which I know is bad practice and unpythonic, because the methods would be unable to access the variables that the rest of the class has access to.
I appreciate any help.
You have to bind your function to the class instance to turn it into a method. It can be done by wrapping it in types.MethodType:
import types
class what:
def __init__(self):
s = 'def yolo(self):\n\tself.extra = "Hello"\n\tprint self.extra'
exec(s)
self.yolo = types.MethodType(yolo, self)
what().yolo()
On a side note, why do you even need exec in this case? You can just as well write
import types
class what:
def __init__(self):
def yolo(self):
self.extra = "Hello"
print self.extra
self.yolo = types.MethodType(yolo, self)
what().yolo()
Edit: for the sake of completeness, one might prefer a solution through the descriptor protocol:
class what:
def __init__(self):
def yolo(self):
self.extra = "Hello"
print self.extra
self.yolo = yolo.__get__(self)
what().yolo()
Another way, seems more elegant to me:
class what:
pass
ld = {}
exec("""
def yolo(self):
self.extra = "Hello"
print(self.extra)
""", None, ld)
# print('locals got: {}'.format(ld))
for name, value in ld.items():
setattr(what, name, value)
what().yolo()