introspection

Java JDK 8 IndexedPropertyDescriptor has changed since JDK 7 with List object

南笙酒味 提交于 2020-01-01 09:57:08
问题 I have a simple issue. I have a program working in Java JDK7 but it doesn't work in JDK8 because of some introspection changes. Here is a test program to reproduce the issue: import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) throws IntrospectionException { BeanInfo info = Introspector.getBeanInfo

getting the class path or name space of a class in python even if it is nested

只谈情不闲聊 提交于 2020-01-01 09:22:34
问题 I'm currently writing a serialization module in Python that can serialize user defined classes. in order to do this I need to get the full name space of the object and write it to a file. I can then use that string to recreate the object. for example assume that we have the following class structure in a file named A.py class B: class C: pass now with the assumption that my_klass_string is the string "A::B::C" klasses = my_klass_string.split("::") if globals().has_key(klasses[0]): klass =

Detecting empty function definitions in python

て烟熏妆下的殇ゞ 提交于 2020-01-01 08:33:09
问题 I need to detect whether a function is an empty definition or not. It can be like: def foo(): pass or like: def foo(i, *arg, **kwargs): pass or like: foo = lambda x: None What is the most elegant way to detect them using the 'inspect' module? Is there a better way than this: def isEmptyFunction(func): e = lambda: None return func.__code__.co_code == e.__code__.co_code 回答1: The method you propose does not quite work because empty functions that have docstrings have a slightly different

Detecting empty function definitions in python

烈酒焚心 提交于 2020-01-01 08:33:07
问题 I need to detect whether a function is an empty definition or not. It can be like: def foo(): pass or like: def foo(i, *arg, **kwargs): pass or like: foo = lambda x: None What is the most elegant way to detect them using the 'inspect' module? Is there a better way than this: def isEmptyFunction(func): e = lambda: None return func.__code__.co_code == e.__code__.co_code 回答1: The method you propose does not quite work because empty functions that have docstrings have a slightly different

Python introspection - how to check current module / line of call from within function

限于喜欢 提交于 2020-01-01 05:28:28
问题 I have a function: # utils.py def hello(name='World'): # Detect where I'm being called from. print('Hi, %s. You called this from %s at line # %d.' % (name, mod, lineno)) # ``mod`` and ``lineno`` on previous line would have been set in real use. I import that function and run it elsewhere # other.py (this comment at line # 138) from utils import hello hello('Johnny') # From inside ``hello`` I want to be able to detect that this # was called from other.py at line # 140 回答1: Access the enclosing

How would you determine where each property and method of a Python class is defined?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-30 10:30:24
问题 Given an instance of some class in Python, it would be useful to be able to determine which line of source code defined each method and property (e.g. to implement 1). For example, given a module ab.py class A(object): z = 1 q = 2 def y(self): pass def x(self): pass class B(A): q = 4 def x(self): pass def w(self): pass define a function whither(class_, attribute) returning a tuple containing the filename, class, and line in the source code that defined or subclassed attribute . This means the

Find out a method's name in Groovy

天大地大妈咪最大 提交于 2019-12-30 10:04:57
问题 Is there a way in Groovy to find out the name of the called method? def myMethod() { println "This method is called method " + methodName } This, in combination with duck typing would allow for quite concise (and probably hard to read) code. 回答1: No, as with Java there's no native way of doing this. You could write an AST transform so that you could annotate the method and this could set a local variable inside the method. Or you can do it the good old Java way of generating a stackTrace, and

Groovy, get enclosing function's name?

本秂侑毒 提交于 2019-12-30 08:10:24
问题 I'm using Groovy 1.8.4, trying to get the name of the enclosing function... def myFunction() { println functionName?? } I've tried delegate , this , owner , Groovy complains no such objects found. I also tried the Java hack new Exception().getStackTrace()[0].getMethodName() , but that just prints newInstance0 回答1: How about groovy's StackTraceUtils.sanitize? Here's a quick example: import org.codehaus.groovy.runtime.StackTraceUtils class A { def methodX() { methodY() } def methodY() { methodZ

Get Activity name dynamically - android

陌路散爱 提交于 2019-12-28 05:06:34
问题 I'd like to get the name of the current Activity to be sent along in the URI of an HttpRequest . Is there a way to do this without referring specifically to the Activity ? I know I can do myActivity.class.toString() but this is just a less efficient way of hard coding "myActivity" since I'm making a static reference to my Activity . Is there a more general way to do this using something like 'this' (which btw doesn't actually work here because it returns more information than what's desired).

Get Activity name dynamically - android

不想你离开。 提交于 2019-12-28 05:06:12
问题 I'd like to get the name of the current Activity to be sent along in the URI of an HttpRequest . Is there a way to do this without referring specifically to the Activity ? I know I can do myActivity.class.toString() but this is just a less efficient way of hard coding "myActivity" since I'm making a static reference to my Activity . Is there a more general way to do this using something like 'this' (which btw doesn't actually work here because it returns more information than what's desired).