introspection

How to go about serializing a large, complex object?

社会主义新天地 提交于 2019-12-01 09:03:18
问题 I have a " User " class with 40+ private variables including complex objects like private/public keys (QCA library), custom QObjects etc. The idea is that the class has a function called sign() which encrypts, signs, serializes itself and returns a QByteArray which can then be stored in a SQLite blob. What's the best approach to serialize a complex object? Iterating though the properties with QMetaObject ? Converting it to a protobuf object? Could it be casted to a char array? 回答1: Could it

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

一笑奈何 提交于 2019-12-01 08:56:39
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 definition in the class body, not the latest assignment due to overeager dynamism. It's fine if it

How to print in REPL the code of functions in Julia?

为君一笑 提交于 2019-12-01 08:54:53
问题 In Julia, a lot of the Base and closer related functions are also written in pure Julia, and the code is easily avaible. One can skim through the repository or the local downloaded files, and see how the function is written/implemented. But I think there is allready some built in method that does that for you, so you can write in REPL or Jupyter Notebook something like: @code functioninquestion() and get something like: functioninquestion(input::Type) some calculations return end without

Find out a method's name in Groovy

帅比萌擦擦* 提交于 2019-12-01 06:45:15
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. 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 finding the correct StackTraceElement with something like: import static org.codehaus.groovy.runtime

Compare the Content, Not the Results, of Procs

删除回忆录丶 提交于 2019-12-01 06:30:00
Using Ruby 1.9.2 Problem Compare the content, not the results, of two procs. I understand the results can't be tested because of the halting problem but that's OK; I don't want to test the results anyway. For instance proc {@x == "x"} == proc {@x == "x"} => false # doh! That returns false because the objects inside the procs are not the same. My clunky solution I have a work around solution that kinda sorta does what I want but it doesn't really test that the proc is "equal" to what I put in it. In my specific case the format of my procs will always be boolean tests on instance variables like

Compare the Content, Not the Results, of Procs

人盡茶涼 提交于 2019-12-01 05:22:51
问题 Using Ruby 1.9.2 Problem Compare the content, not the results, of two procs. I understand the results can't be tested because of the halting problem but that's OK; I don't want to test the results anyway. For instance proc {@x == "x"} == proc {@x == "x"} => false # doh! That returns false because the objects inside the procs are not the same. My clunky solution I have a work around solution that kinda sorta does what I want but it doesn't really test that the proc is "equal" to what I put in

Python3: check if method is static

三世轮回 提交于 2019-12-01 04:33:50
Simmilar question (related with Python2: Python: check if method is static ) Lets concider following class definition: class A: def f(self): return 'this is f' @staticmethod def g(): return 'this is g' In Python 3 there is no instancemethod anymore, everything is function, so the answer related to Python 2 will not work anymore. As I told, everything is function, so we can call A.f(0) , but of course we cannot call A.f() (argument missmatch). But if we make an instance a=A() and we call a.f() Python passes to the function A.f the self as first argument. Calling a.g() prevents from sending it

Groovy, get enclosing function's name?

老子叫甜甜 提交于 2019-12-01 03:59:16
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 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() } def methodZ() { def marker = new Throwable() StackTraceUtils.sanitize(marker).stackTrace.eachWithIndex

In python is there a way to know if an object “implements an interface” before I pass it to a function?

旧街凉风 提交于 2019-12-01 03:06:29
I know this may sound like a stupid question, especially to someone who knows python's nature, but I was just wondering, is there a way to know if an object "implements an interface" so as to say? To give an example of what I want to say: let's say I have this function: def get_counts(sequence): counts = {} for x in sequence: if x in counts: counts[x] += 1 else: counts[x] = 1 return counts My question is: Is there a way to make sure that the object passed to the function is iterable ? I know that in Java or C# I could do this by having the method accept any object that implements a specific

How to distinguish an instance method, a class method, a static method or a function in Python 3?

久未见 提交于 2019-12-01 00:59:08
I want to distinguish between methods and functions in Python 3. Furthermore, I want to get the corresponding class if it is a method. My current solution is like this: import types import inspect def function_or_method(f): if inspect.ismethod(f): if inspect.isclass(f.__self__): print("class method") klass = f.__self__ else: print("instance method") klass = f.__self__.__class__ elif inspect.isfunction(f): # function if f.__name__ != f.__qualname__: # to distiguish staticmethod and function print("static method") # HOW TO GET THE CLASS else: print("function") else: print("not function or method