inspect

Detect if class was defined declarative or functional - possible?

可紊 提交于 2019-12-10 12:53:19
问题 Here's a simple class created declaratively: class Person: def say_hello(self): print("hello") And here's a similar class, but it was defined by invoking the metaclass manually: def say_hello(self): print("sayolala") say_hello.__qualname__ = 'Person.say_hello' TalentedPerson = type('Person', (), {'say_hello': say_hello}) I'm interested to know whether they are indistinguishable. Is it possible to detect such a difference from the class object itself? >>> def was_defined_declaratively(cls): ..

Python: How determine if attribute (by name) is class or instance attribute?

风流意气都作罢 提交于 2019-12-10 11:22:59
问题 Goal (in Python 2.7): Inspecting an arbitrary object, find all of the instance variables. But exclude class variables. Ultimate goal: Print useful details of an object, from a third-party class library that doesn't provide a useful "str" implementation. (Maya's Python API, version 1, which is a simple SWIG wrapper. not using version 2, because I'm learning from some version 1 examples.) Example class: # ---------- class Vector ---------- class Vector(object): def __init__(self, x=0.0, y=0.0,

Why does inspect return different line for class inheriting from superclass?

安稳与你 提交于 2019-12-10 04:38:45
问题 While trying to figure out if a function is called with the @decorator syntax, we realized that inspect has a different behaviour when looking at a decorated class that inherits from a superclass. The following behaviour was found with CPython 3.6.2 under Windows 10. It was also reproduced in CPython 3.7.0 under Linux 64 bits. import inspect def decorate(f): lines = inspect.stack()[1].code_context print(f.__name__, lines) return f @decorate class Foo: pass @decorate class Bar(dict): pass

Obtaining references to function objects on the execution stack from the frame object?

徘徊边缘 提交于 2019-12-08 23:02:31
Given the output of inspect.stack() , is it possible to get the function objects from anywhere from the stack frame and call these? If so, how? (I already know how to get the names of the functions.) Here is what I'm getting at: Let's say I'm a function and I'm trying to determine if my caller is a generator or a regular function? I need to call inspect.isgeneratorfunction() on the function object. And how do you figure out who called you? inspect.stack() , right? So if I can somehow put those together, I'll have the answer to my question. Perhaps there is an easier way to do this? Here is a

Get fully qualified name of a Python class (Python 3.3+)

偶尔善良 提交于 2019-12-08 17:00:34
问题 How can I get name of class including full path from its module root? For Python 3.3 and up? Here is example of Python code: class A: class B: class C: def me(self): print(self.__module__) print(type(self).__name__) print(repr(self)) x = A.B.C() x.me() This code outputs me on Python 3.3: __main__ C <__main__.A.B.C object at 0x0000000002A47278> So, Python internally knows that my object is __main__.A.B.C , but how can I get this programmatically? I can parse repr(self) , but it sounds like a

How do I infer the class to which a @staticmethod belongs?

落爺英雄遲暮 提交于 2019-12-07 12:06:37
问题 I am trying to implement infer_class function that, given a method, figures out the class to which the method belongs. So far I have something like this: import inspect def infer_class(f): if inspect.ismethod(f): return f.im_self if f.im_class == type else f.im_class # elif ... what about staticmethod-s? else: raise TypeError("Can't infer the class of %r" % f) It does not work for @staticmethod-s because I was not able to come up with a way to achieve this. Any suggestions? Here's infer_class

Why Chrome can't inspect nodejs code in Docker container?

醉酒当歌 提交于 2019-12-07 06:41:42
问题 I try to start simple nodejs server inside Docker container and debug it with chrome://inspect or WebStorm. Debugging port 9229 is binded but inspection not works. On the other hand when I run same code without docker i can inspect it in chrome://inspect and in WebStorm both well. Can anybody explain me why Chrome can't inspect nodejs code in Docker container??? Dockerfile FROM node:8.2.1-alpine WORKDIR /code COPY package.json /code/package.json RUN npm install && npm ls RUN mv /code/node

Inspect Element in Android Studio

感情迁移 提交于 2019-12-07 05:54:13
问题 How do we inspect element in android studio. The eclipse counterpart is Ctrl+Shift+I (after selecting a variable or expression, press Ctrl+Shift+I ). Couldn't find this on the internet, please help! 回答1: In Mac OSX, you can use command + option + F8 . 来源: https://stackoverflow.com/questions/27668834/inspect-element-in-android-studio

Python: How determine if attribute (by name) is class or instance attribute?

微笑、不失礼 提交于 2019-12-06 13:03:13
Goal (in Python 2.7): Inspecting an arbitrary object, find all of the instance variables. But exclude class variables. Ultimate goal: Print useful details of an object, from a third-party class library that doesn't provide a useful "str" implementation. (Maya's Python API, version 1, which is a simple SWIG wrapper. not using version 2, because I'm learning from some version 1 examples.) Example class: # ---------- class Vector ---------- class Vector(object): def __init__(self, x=0.0, y=0.0, z=0.0): self.x, self.y, self.z = x, y, z # Provide useful info for 'repr(self)', 'str(self)', and

Truncate #inspect output in irb (ruby)

久未见 提交于 2019-12-06 08:56:59
问题 I want to truncate #inspect output in irb (a large output must be cropped to MAX_LEN). Currently, I override :inspect, :to_s methods for all specific objects. Is there are other solution? change $stdout ? other? 回答1: For a clean solution, gem install hirb . hirb pages irb's returned values if they get too long. If you want to monkeypatch irb: module IRB class Irb def output_value @context.last_value.to_s.slice(0, MAX_LEN) end end end I don't recommend this because it's a hack and breaks any