introspection

How to use Qt-Dbus bindings without blocking the main thread

那年仲夏 提交于 2019-12-01 00:52:45
问题 My goal is to create a library using the Qt's DBus bindings. I tried to create a Qt application without launching the QEventLoop (provided by the QCoreApplication class) in the main thread. Here is a minimalistic application sample, working fine using QT-4.6.2 version but blocking on introspection using QT-4.8 or higher. DBusHandler.hpp #pragma once #include <iostream> #include <QtCore/QThread> #include <QtCore/QtCore> #include <QtDBus/QDBusInterface> class DBusHandler : public QThread { Q

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

雨燕双飞 提交于 2019-11-30 23:08:18
问题 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

In C# what is the equivalent of “is” keyword but using Type objects

旧巷老猫 提交于 2019-11-30 22:39:17
An easy question I guess, but in the documentation of the Type class they only talk of interfaces on the GetInterfaces method. i.e. typeof(ChildClass).XXX(typeof(ParentClass) It depends on what you need; IsAssignableFrom, perhaps: bool stringIsObj = typeof(object).IsAssignableFrom(typeof(string)); or IsSubclassOf : bool stringIsObj = typeof(string).IsSubclassOf(typeof(object)); typeof(ParentClass).IsAssignableFrom(typeof(ChildClass)) I suggest you search for an equivalent. Instead of using the "is" keyword like this: if (object is class) { ... } you could simply compare the types of those two

Java Package Introspection [duplicate]

a 夏天 提交于 2019-11-30 21:10:01
This question already has an answer here: Can you find all classes in a package using reflection? 24 answers How do i get all classes within a package? You can't. Classes can come in via many different class loaders, including remote ones. Here's a more complete way to solve this for jars, based on the idea posted by JG. /** * Scans all classloaders for the current thread for loaded jars, and then scans * each jar for the package name in question, listing all classes directly under * the package name in question. Assumes directory structure in jar file and class * package naming follow java

Generating SWIG bindings with CMake

╄→尐↘猪︶ㄣ 提交于 2019-11-30 20:42:48
How would I generate automatic bindings for a C project that is built using CMake? I want to generate bindings for Python, Java, .NET, PHP, Perl, TCL, Ruby and Octave automatically. Tristram Gräbener You can find an example here . Snippet: The following example is a CMake input file for creating a python wrapper for the SWIG interface file, example.i: # This is a CMake example for Python FIND_PACKAGE(SWIG REQUIRED) INCLUDE(${SWIG_USE_FILE}) FIND_PACKAGE(PythonLibs) INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH}) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) SET(CMAKE_SWIG_FLAGS "") SET_SOURCE

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

我的未来我决定 提交于 2019-11-30 19:13:21
问题 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

How can I get the calling expression of a function in Python?

不想你离开。 提交于 2019-11-30 18:35:32
问题 For educational purpose, I would like to be able to print the complete calling expression of the current function. Not necessarily from an exception handler. After some research, I ended up with this pretty straightforward piece of code: import inspect import linecache def print_callexp(*args, **kwargs): try: frame = inspect.currentframe() # method 1, using inspect module only print(inspect.getframeinfo(frame.f_back).code_context) # method 2, just for the heck of it linecache.checkcache(frame

Introspect calling object

僤鯓⒐⒋嵵緔 提交于 2019-11-30 17:41:12
How do I introspect A 's instance from within b.func() (i.e. A 's instance's self ): class A(): def go(self): b=B() b.func() class B(): def func(self): # Introspect to find the calling A instance here In general we don't want that func to have access back to the calling instance of A because this breaks encapsulation . Inside of b.func you should have access to any args and kwargs passed, the state/attributes of the instance b (via self here), and any globals hanging around. If you want to know about a calling object, the valid ways are: Pass the calling object in as an argument to the

In C# what is the equivalent of “is” keyword but using Type objects

ⅰ亾dé卋堺 提交于 2019-11-30 17:22:24
问题 An easy question I guess, but in the documentation of the Type class they only talk of interfaces on the GetInterfaces method. i.e. typeof(ChildClass).XXX(typeof(ParentClass) 回答1: It depends on what you need; IsAssignableFrom, perhaps: bool stringIsObj = typeof(object).IsAssignableFrom(typeof(string)); or IsSubclassOf : bool stringIsObj = typeof(string).IsSubclassOf(typeof(object)); 回答2: typeof(ParentClass).IsAssignableFrom(typeof(ChildClass)) 回答3: I suggest you search for an equivalent.

Traverse a class hierarchy from base to all descendants

故事扮演 提交于 2019-11-30 16:06:13
问题 In an iOS app I am writing I want to traverse a class hierarchy to make an inventory of all subclasses. My intent is to use each subclass type as a key -- via NSStringForClass() -- in a dictionary. My motivation is to be able to automatically discover all variants of a base class so that I can call methods associated with that class. For reasons of division of labor I prefer not to use method overriding here. Is it possible to do such a traversal? How would it work? 回答1: Here's an example.