introspection

Traverse a class hierarchy from base to all descendants

半腔热情 提交于 2019-11-30 15:59:33
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? Here's an example. This method will return all subclasses descending from the class you send the message to. @interface NSObject

Ruby method interception

白昼怎懂夜的黑 提交于 2019-11-30 15:35:26
I want to intercept method calls on a ruby-class and being able to do something before and after the actual execution of the method. I tried the following code, but get the error: MethodInterception.rb:16:in before_filter': (eval):2:in alias_method': undefined method say_hello' for class HomeWork' (NameError) from (eval):2:in `before_filter' Can anybody help me to do it right? class MethodInterception def self.before_filter(method) puts "before filter called" method = method.to_s eval_string = " alias_method :old_#{method}, :#{method} def #{method}(*args) puts 'going to call former method' old

Find module name of the originating exception in Python

喜夏-厌秋 提交于 2019-11-30 13:24:52
Example: >>> try: ... myapp.foo.doSomething() ... except Exception, e: ... print 'Thrown from:', modname(e) Thrown from: myapp.util.url In the above example, the exception was actually thrown at myapp/util/url.py module. Is there a way to get the __name__ of that module? My intention is to use this in logging.getLogger function. This should work: import inspect try: some_bad_code() except Exception, e: frm = inspect.trace()[-1] mod = inspect.getmodule(frm[0]) print 'Thrown from', mod.__name__ EDIT: Stephan202 mentions a corner case. In this case, I think we could default to the file name.

Get function callers' information in python

折月煮酒 提交于 2019-11-30 13:21:27
I want to get information about the callers of a specific function in python. For example: class SomeClass(): def __init__(self, x): self.x = x def caller(self): return special_func(self.x) def special_func(x): print "My caller is the 'caller' function in an 'SomeClass' class." Is it possible with python? Yes, the sys._getframe() function let's you retrieve frames from the current execution stack, which you can then inspect with the methods and documentation found in the inspect module ; you'll be looking for specific locals in the f_locals attribute, as well as for the f_code information:

Java Package Introspection [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 05:43:30
问题 This question already has answers here : Can you find all classes in a package using reflection? (26 answers) Closed 5 years ago . How do i get all classes within a package? 回答1: You can't. Classes can come in via many different class loaders, including remote ones. 回答2: 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

C# “is” operator - is that reflection?

随声附和 提交于 2019-11-30 04:29:37
A colleague asked me an interesting question today - is the C# keyword/operator "is" considered reflection? object tmp = "a string"; if(tmp is String) { } How is this operator implemented behind the scenes? Does it require reflection or introspection? Or because of the strongly typed nature of the language, is the Type of the object immediately accessable as a top-level attribute of the object in memory? MSDN states that: Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not

Generating SWIG bindings with CMake

荒凉一梦 提交于 2019-11-30 03:55:47
问题 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. 回答1: 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

Sorting the [Any] array

若如初见. 提交于 2019-11-30 02:05:50
Given an array defined as follow let list: [Any] I want to sort it WHEN all the values inside it have the same type Element AND Element is Comparable . When it should return the sorted array So I would need a function that when the array is populated in a way like the followings let list: [Any] = [10, 11, 0, 2, -1] let list: [Any] = ["Red", "Green", "Blue"] let list: [Any] = [true, false, true, true] does return the sorted array. When it should return nil On the other hand when list contains one of the following examples let list: [Any] = [CGPointZero, CGPoint(x:1, y:1)] // CGPoint is not

Does Scala have introspection capable of something similar to Python's dir()?

无人久伴 提交于 2019-11-30 01:48:03
Yes, I know it's considered lazy by the non-Pythonistas. The reason I ask is that documentation is still woefully lacking in many Scala libraries (e.g. Scala-dbc, but that's not all I'm looking at), and if I could see the attributes of an object/class at runtime, I could at least figure out what's available. Thanks. Thomas Jung Scala does not have a reflection API. The only way to access this information is to use the Java reflection API . This has the disadvantage that the structure may change as the way Scala is represented in Java classes and interfaces may change in the future. scala>

How do you list the currently available objects in the current scope in ruby?

我的未来我决定 提交于 2019-11-29 19:52:21
I'm new to ruby and I'm playing around with the IRB. I found that I can list methods of an object using the ".methods" method, and that self.methods sort of give me what I want (similar to Python's dir( builtins )?), but how can I find the methods of a library/module I've loaded via include and require? irb(main):036:0* self.methods => ["irb_pop_binding", "inspect", "taguri", "irb_chws", "clone", "irb_pushws", "public_methods", "taguri=", "irb_pwws", "public", "display", "irb_require", "irb_exit", "instance_variable_defined?", "irb_cb", "equal?", "freeze", "irb_context ", "irb_pop_workspace",