introspection

Find out if an Objective-C class overrides a method [duplicate]

心不动则不痛 提交于 2019-12-05 04:58:06
This question already has an answer here: Objective-C detect if class overrides inherited method 2 answers How can I find out, at runtime, if a class overrides a method of its superclass? For example, I want to find out if a class has it's own implementation of isEqual: or hash , instead of relying on a super class. You just need to get a list of the methods, and look for the one you want: #import <objc/runtime.h> BOOL hasMethod(Class cls, SEL sel) { unsigned int methodCount; Method *methods = class_copyMethodList(cls, &methodCount); BOOL result = NO; for (unsigned int i = 0; i < methodCount;

Checking up on a `concurrent.futures.ThreadPoolExecutor`

▼魔方 西西 提交于 2019-12-05 01:27:28
I've got a live concurrent.futures.ThreadPoolExecutor . I want to check its status. I want to know how many threads there are, how many are handling tasks and which tasks, how many are free, and which tasks are in the queue. How can I find out these things? There is some visibility into the Pool, and the pending workitem queue. To find out what's available, print poolx.__dict__ to see the structure. Read the ThreadPool code, it's pretty good: concurrent.futures.thread The following creates a pool with one thread. It then creates two jobs: one sleeps for 3 seconds, the other immediately returns

SQLAlchemy introspection of ORM classes/objects

流过昼夜 提交于 2019-12-04 23:54:34
问题 I am looking for a way to introspect SQLAlchemy ORM classes/entities to determine the types and other constraints (like maximum lengths) of an entity's properties. For example, if I have a declarative class: class User(Base): __tablename__ = "USER_TABLE" id = sa.Column(sa.types.Integer, primary_key=True) fullname = sa.Column(sa.types.String(100)) username = sa.Column(sa.types.String(20), nullable=False) password = sa.Column(sa.types.String(20), nullable=False) created_timestamp = sa.Column(sa

Get kwargs Inside Function

谁说胖子不能爱 提交于 2019-12-04 23:51:15
If I have a python function like so: def some_func(arg1, arg2, arg3=1, arg4=2): Is there a way to determine which arguments were passed by keyword from inside the function? EDIT For those asking why I need this, I have no real reason, it came up in a conversation and curiosity got the better of me. No, there is no way to do it in Python code with this signature -- if you need this information, you need to change the function's signature. If you look at the Python C API, you'll see that the actual way arguments are passed to a normal Python function is always as a tuple plus a dict -- i.e., the

getting the C python exec argument string or accessing the evaluation stack

一个人想着一个人 提交于 2019-12-04 19:24:26
In my python debugger I have a way of remapping a string to a filename so that when you are stepping through an exec'd function inside the debugger you can list lines pygmentized, or view them along inside an editor like Emacs via realgud . So I'd like to be able to extract the string in an exec statement when CPython is stopped inside evaluating that. I already have a mechanism that can look back in the call frame to see if the caller was an EXEC_STMT and I can look back one instruction to see if the previous instruction was say DUP_TOP . So I'd be home free if I could just figure out a way

Finding out which functions are available from a class instance in python?

对着背影说爱祢 提交于 2019-12-04 17:34:04
How do you dynamically find out which functions have been defined from an instance of a class? For example: class A(object): def methodA(self, intA=1): pass def methodB(self, strB): pass a = A() Ideally I want to find out that the instance 'a' has methodA and methodB, and which arguments they take? Have a look at the inspect module. >>> import inspect >>> inspect.getmembers(a) [('__class__', <class '__main__.A'>), ('__delattr__', <method-wrapper '__delattr__' of A object at 0xb77d48ac>), ('__dict__', {}), ('__doc__', None), ('__getattribute__', <method-wrapper '__getattribute__' of A object at

Object Reflection

天大地大妈咪最大 提交于 2019-12-04 16:52:16
Does anyone have any references for building a full Object/Class reflection system in C++ ? Ive seen some crazy macro / template solutions however ive never found a system which solves everything to a level im comfortable with. Thanks! A. Levy Using templates and macros to automatically, or semi-automatically, define everything is pretty much the only option in C++. C++ has very weak reflection/introspection abilities. However, if what you want to do is mainly serialization and storage, this has already been implemented in the Boost Serialization libraries . You can do this by either

Python introspection: access function name and docstring inside function definition

為{幸葍}努か 提交于 2019-12-04 16:26:23
问题 Consider the following python code: def function(): "Docstring" name = ??? doc = ??? return name, doc >>> function() "function", "Docstring" What do I need to replace the question marks with so that I get the name and the docstring of the function from inside the same function? EDIT: Most of the answers so far explicitly hardcode the name of the function inside its definition. Is it possible do something like below where a new function get_name_doc would access the function from the outer

Why does Ruby use respond_to? instead of responds_to?

两盒软妹~` 提交于 2019-12-04 14:58:36
问题 I'm curious why Ruby's introspection related method to check if an object responds to a method is respond_to? instead of responds_to? It always seems awkward to me but maybe that's because I'm used to respondsToSelector in objective-c. 回答1: Matz prefers second person singular or third person plural: "responds_to?" probably makes more sense to English speakers than "respond_to?". Maybe. But I'm Japanese. Ruby is not English. It's the basic naming rule to avoid third person singular form in the

SQLAlchemy introspection

本秂侑毒 提交于 2019-12-04 12:29:40
What I am trying to do is to get from SqlAlchemy entity definition all it's Column()'s, determine their types and constraints, to be able to pre-validate, convert data and display custom forms to user. How can I introspect it? Example: class Person(Base): ''' Represents Person ''' __tablename__ = 'person' # Columns id = Column(String(8), primary_key=True, default=uid_gen) title = Column(String(512), nullable=False) birth_date = Column(DateTime, nullable=False) I want to get this id, title, birth date, determine their restrictions (such as title is string and max length is 512 or birth_date is