Check if method is from class without known string - python-3.x

Trying to check if a method is from a class. It's as simple as:
class Foo:
def bar(self):
return
f = Foo()
ismethod(f.bar, Foo) # Should evaluate to true
Syntax like hasattr(Foo(), 'bar') works if you know the method name, and the same with 'bar' in dir(Foo()); howeveer, I need to be able to pass the method object itself as the argument, not like a string as shown here. In my scenario, I need to tell if a method—passed as an argument—is of a specific class.
In other words: How do I tell if an object is a method of a class, without knowing the name of the object?

You need inspect.ismethod:
import inspect
def just_func(a, b):
return a + b
class SomeClass:
def just_method(self, a, b, c):
return a * b + c
obj = SomeClass()
print(inspect.ismethod(just_func)) # False
print(inspect.ismethod(obj.just_method)) # True
UPD:
Oh sorry, you need to check if it belongs to a particular class, then use:
print('SomeClass' in obj.just_method.__qualname__) # True
print('SomeClass' in just_func.__qualname__) # False
Here's what the function you want might look like:
def ismethod(func, cls):
return cls.__name__ in func.__qualname__ and '.' in func.__qualname__
It actually looks like a duplicate of this.

Related

Python default 'wildcard' function - possible?

Here is my class code:
class Example:
def __init__(self):
self.parameter1 = 1
def standardFunction(self):
print("Hello")
Is it possible to initialise the Example class and make sure that every method that may be called for this particular instance will always point to the standardFunction?
For example:
ex1 = Example()
ex1.test1()
prints "Hello"
ex1.test2()
prints "Hello"
ex1.test3.test4()
prints "Hello"
ex1.test5().test6().IamBatman(42)
prints "Hello"
Basically, I would like to always have the Example.standardFunction() called, disregarding the string after the first "dot". Plus I don't know what's being put after the dot - it may be any string, int, float, or null.
Is it possible to achieve such behaviour with Python?
Yes we can achieve similar behavior in Python by overloading __getattr__. Like this,
class Example:
def __init__(self):
self.parameter1 = 1
def standardFunction(self):
print("Hello")
def __getattr__(self, name):
return self.standardFunction

Why does vars(type('', (object,), input_dict)()) not return its variables?

To convert dict to class, I wrote the code as follows, and it works well.
output_class = type('', (object,), input_dict)()
However, the created class does not return its attributes by the code below!
print(vars(output_class))
>> {}
I solved this problem by using the code below, but I am still confused.
class Struct(object):
def __init__(self, **entries):
self.__dict__.update(entries)
output_class = Struct(**input_dict)
print(vars(output_class))
>> {'key': 'value'}
I would appreciate it if you could explain why the former does not return its attributes.
It's because in first case (calling type) object of your class has an empty __dict__ and your input_dict content is stored in class variables.
third argument of type() defines class (not object) variables or methods (they are defined by __new__ call not __init__ ).
Simply put, the first and the second code snippets are not the same.
So
output_class = type('', (object,), input_dict)(),
Has equivalent something like this:
class Struct(object):
def __new__(cls, **entries):
cls = super().__new__()
for k,v in **entries:
setattr(cls, k, v)
return obj
output_class = Struct()
If you want something like:
class Struct(object):
def __init__(self, **entries):
self.__dict__.update(entries)
output_class = Struct(**input_dict)
You should define init(). It can look like:
output_class = type('', (object,),
{'__init__': lambda self, inp_d: self.__dict__.update(inp_d)})(input_dict)
If my explanation isn't enough clear I suppose example from here can help: https://docs.python.org/3/library/functions.html#type
Pay you attention: a is not inside __init__() method so it's shared between all objects of class X.

Find owner class of a method in Python

I'm writing decorators, and part of what I need to do is discern whether a function is a function or a method. Is there a way I can find what class a given method is a part of?
e.g. If I was to run this code, what could I write in getOwner to make exampleFunc print something like <class '__main__'.Example>?
class Example:
def method(self):
print("I'm a method")
def exampleFunc(func):
owner = getOwner(func)
print(owner)
test = Example()
exampleFunc(test.method)
If all you need to do is figure out of the thing behaving like a function is a method or a function, that is one purpose of the types module.
import types
def is_method(f):
return type(f) == types.MethodType
In the event that the function-like object is a method, you can find its parent class as follows.
Update Patched for Python3 compatibility.
def method_parent(f):
return f.__self__
If you have a reference to the classes defined in your scope, you'd need to check for each one:
def exampleFunc(f):
class_list = [...]
return any(f in vars(c).values() for c in class_List)
This will return True if function f is an instance method. However, if you wish to return the actual class name:
def exampleFunc(f):
class_list = [...]
for c in class_list:
if f in vars(c).values():
return c.__name__
return 'global function' if 'lambda' not in f.__name__ else 'lambda'
Note that this does not work for __dunder__ methods, and methods that your class inherits. For example,
class A:
def f1(self): pass
class B(A):
def f2(self): pass
print(vars(B))
mappingproxy({'__doc__': None,
'__module__': '__main__',
'f2': <function __main__.B.f2>})
Note that f1 is not a part of B's mappingproxy.

Using example for strategies that return class instances

I have
class A(st.SearchStrategy):
def do_draw(self, data):
return object_a(b=st.integers(), c=st.boolean()...)
class B(st.SearchStrategy):
def do_draw(self, data):
return object_a(d=st.boolean(), e=st.boolean()...)
#given(a=A(), b=B())
def test_A_and_B(a, b):
...
How I make sure that a test case of
a = A(b=5, c=True)
# b can be anything
and a test case of
a = A(b=10, c=True)
b = B(c=True, d=<can be either T or F>)
get generate?
I know about #example. Would this be correct?
#given(a=A(), b=B())
#example(a=A(b=10, c=True), b=B(c=True, d=False)
# not sure how to set d to be either true or false
def test_A_and_B(a, b):
...
DO NOT INHERIT FROM SEARCHSTRATEGY.
It is private, internal code that we might change at any time. You're using it wrong anyway!
Instead, you should compose your strategy from the documented functions in hypothesis.strategies. For example, you can define a strategy for making instances of object_a using builds() like so:
builds(object_a, b=st.integers(), c=st.booleans(), ...)
An #example is a single exact input, so you'd use it twice to check d both True and False:
#example(a=object_a(b=10, c=True), b=object_b(c=True, d=True)
#example(a=object_a(b=10, c=True), b=object_b(c=True, d=False)
If you don't care about the value of b at all, just define an example with the default value for that argument.
All together, that would look like:
#given(
a=builds(object_a, b=st.integers(), c=st.booleans()),
b=builds(object_b, d=st.booleans(), e=st.booleans()
)
#example(a=object_a(b=5, c=True), b=None) # assuming b=None is valid
#example(a=object_a(b=10, c=True), b=object_b(d=True, e=True))
#example(a=object_a(b=10, c=True), b=object_b(d=True, e=False))
def test_A_and_B(a, b):
...
Hope that helps :-)

Dynamically add methods to a class in Python 3.0

I'm trying to write a Database Abstraction Layer in Python which lets you construct SQL statments using chained function calls such as:
results = db.search("book")
.author("J. K. Rowling")
.price("<40.00")
.title("Harry")
.execute()
but I am running into problems when I try to dynamically add the required methods to the db class.
Here is the important parts of my code:
import inspect
def myName():
return inspect.stack()[1][3]
class Search():
def __init__(self, family):
self.family = family
self.options = ['price', 'name', 'author', 'genre']
#self.options is generated based on family, but this is an example
for opt in self.options:
self.__dict__[opt] = self.__Set__
self.conditions = {}
def __Set__(self, value):
self.conditions[myName()] = value
return self
def execute(self):
return self.conditions
However, when I run the example such as:
print(db.search("book").price(">4.00").execute())
outputs:
{'__Set__': 'harry'}
Am I going about this the wrong way? Is there a better way to get the name of the function being called or to somehow make a 'hard copy' of the function?
You can simply add the search functions (methods) after the class is created:
class Search: # The class does not include the search methods, at first
def __init__(self):
self.conditions = {}
def make_set_condition(option): # Factory function that generates a "condition setter" for "option"
def set_cond(self, value):
self.conditions[option] = value
return self
return set_cond
for option in ('price', 'name'): # The class is extended with additional condition setters
setattr(Search, option, make_set_condition(option))
Search().name("Nice name").price('$3').conditions # Example
{'price': '$3', 'name': 'Nice name'}
PS: This class has an __init__() method that does not have the family parameter (the condition setters are dynamically added at runtime, but are added to the class, not to each instance separately). If Search objects with different condition setters need to be created, then the following variation on the above method works (the __init__() method has a family parameter):
import types
class Search: # The class does not include the search methods, at first
def __init__(self, family):
self.conditions = {}
for option in family: # The class is extended with additional condition setters
# The new 'option' attributes must be methods, not regular functions:
setattr(self, option, types.MethodType(make_set_condition(option), self))
def make_set_condition(option): # Factory function that generates a "condition setter" for "option"
def set_cond(self, value):
self.conditions[option] = value
return self
return set_cond
>>> o0 = Search(('price', 'name')) # Example
>>> o0.name("Nice name").price('$3').conditions
{'price': '$3', 'name': 'Nice name'}
>>> dir(o0) # Each Search object has its own condition setters (here: name and price)
['__doc__', '__init__', '__module__', 'conditions', 'name', 'price']
>>> o1 = Search(('director', 'style'))
>>> o1.director("Louis L").conditions # New method name
{'director': 'Louis L'}
>>> dir(o1) # Each Search object has its own condition setters (here: director and style)
['__doc__', '__init__', '__module__', 'conditions', 'director', 'style']
Reference: http://docs.python.org/howto/descriptor.html#functions-and-methods
If you really need search methods that know about the name of the attribute they are stored in, you can simply set it in make_set_condition() with
set_cond.__name__ = option # Sets the function name
(just before the return set_cond). Before doing this, method Search.name has the following name:
>>> Search.price
<function set_cond at 0x107f832f8>
after setting its __name__ attribute, you get a different name:
>>> Search.price
<function price at 0x107f83490>
Setting the method name this way makes possible error messages involving the method easier to understand.
Firstly, you are not adding anything to the class, you are adding it to the instance.
Secondly, you don't need to access dict. The self.__dict__[opt] = self.__Set__ is better done with setattr(self, opt, self.__Set__).
Thirdly, don't use __xxx__ as attribute names. Those are reserved for Python-internal use.
Fourthly, as you noticed, Python is not easily fooled. The internal name of the method you call is still __Set__, even though you access it under a different name. :-) The name is set when you define the method as a part of the def statement.
You probably want to create and set the options methods with a metaclass. You also might want to actually create those methods instead of trying to use one method for all of them. If you really want to use only one __getattr__ is the way, but it can be a bit fiddly, I generally recommend against it. Lambdas or other dynamically generated methods are probably better.
Here is some working code to get you started (not the whole program you were trying to write, but something that shows how the parts can fit together):
class Assign:
def __init__(self, searchobj, key):
self.searchobj = searchobj
self.key = key
def __call__(self, value):
self.searchobj.conditions[self.key] = value
return self.searchobj
class Book():
def __init__(self, family):
self.family = family
self.options = ['price', 'name', 'author', 'genre']
self.conditions = {}
def __getattr__(self, key):
if key in self.options:
return Assign(self, key)
raise RuntimeError('There is no option for: %s' % key)
def execute(self):
# XXX do something with the conditions.
return self.conditions
b = Book('book')
print(b.price(">4.00").author('J. K. Rowling').execute())

Resources