inspect

Python: can a decorator determine if a function is being defined inside a class?

巧了我就是萌 提交于 2019-12-03 05:33:44
I'm writing a decorator, and for various annoying reasons[0] it would be expedient to check if the function it is wrapping is being defined stand-alone or as part of a class (and further which classes that new class is subclassing). For example: def my_decorator(f): defined_in_class = ?? print "%r: %s" %(f, defined_in_class) @my_decorator def foo(): pass class Bar(object): @my_decorator def bar(self): pass Should print: <function foo …>: False <function bar …>: True Also, please note: At the point decorators are applied the function will still be a function, not an unbound method, so testing

Airflow apply_defaults decorator reports Argument is required

陌路散爱 提交于 2019-12-02 10:14:33
I recently ran into this nasty error where Airflow's apply_defaults decorator is throwing following stack-trace ( my **kwargs do contain job_flow_id ) File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/mnt/airflow/dags/zanalytics-airflow/src/main/mysql_import/dags/mysql_import_dag.py", line 23, in <module> sync_dag_builder.build_sync_dag() File "/mnt/airflow/dags/zanalytics-airflow/src/main/mysql_import/dags/builders/sync_dag_builders/emr_sync_dag_builder.py", line 26, in build_sync_dag create_emr_task, terminate_emr_task = self._create_job_flow_tasks() File "

Why do I get a Encoding::CompatibilityError with #inspect?

风流意气都作罢 提交于 2019-12-02 05:15:35
问题 The following code works without problem: #encoding: utf-8 class Text def initialize(txt) @txt = txt end def inspect "<Text: %s>" % @txt end end p Text.new('Hello World') But if I try p Text.new('Hä, was soll das?') I get a Encoding::CompatibilityError: inspect_with_umlaut.rb:26:in `p': inspected result must be ASCII only or use the default external encoding (Encoding::CompatibilityError) from inspect_with_umlaut.rb:26:in `<main>' Why this? And more important: How can I avoid it? 回答1: The

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

南楼画角 提交于 2019-12-01 16:00:40
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 hack for me. You are looking for __qualname__ (introduced in Python 3.3): class A: class B: class C: def

Unpacking Python's Type Annotations

自作多情 提交于 2019-11-30 20:26:06
I'm trying to generate some JavaScript based on the type annotations I have provided in on some Python functions by using the signature() function in the inspect module. This part works as I expect when the type is a simple builtin class: import inspect def my_function() -> dict: pass signature = inspect.signature(my_function) signature.return_annotation is dict # True Though I'm not sure how to unwrap and inspect more complex annotations e.g: from typing import List import inspect def my_function() -> List[int]: pass signature = inspect.signature(my_function) signature.return_annotation is

Google map not fully loaded when i open inspect element it will work

ⅰ亾dé卋堺 提交于 2019-11-30 15:45:56
问题 i have integrate a google map on my website but when i open inspect element map will work and when i close map will disappear. Please let me know what the problem before After open inspect element. Code is here <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>JS Bin</title> </head> <body> <input type="text" id="latitude" placeholder="latitude"> <input type="text" id="longitude" placeholder="longitude"> <div id="map" style="width:500px; height:500px"></div> <script src="https://maps

Google map not fully loaded when i open inspect element it will work

僤鯓⒐⒋嵵緔 提交于 2019-11-30 14:52:55
i have integrate a google map on my website but when i open inspect element map will work and when i close map will disappear. Please let me know what the problem before After open inspect element. Code is here <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>JS Bin</title> </head> <body> <input type="text" id="latitude" placeholder="latitude"> <input type="text" id="longitude" placeholder="longitude"> <div id="map" style="width:500px; height:500px"></div> <script src="https://maps.googleapis.com/maps/api/js"></script> <script> function initialize() { var $latitude = document

Python how to get the calling function (not just its name)?

你离开我真会死。 提交于 2019-11-30 09:21:37
问题 I want to write a function which returns the calling function: def foo(): return get_calling_function() #should return 'foo' function object There's numerous examples online how to get the calling function's name , but not how to get the actual object. I've come up with the following solution which gets the name, then looks it up in the calling function's global namespace. However this doesn't work for class functions since there you need the class name as well, and I image there's a bunch of

Unpacking Python's Type Annotations

感情迁移 提交于 2019-11-30 03:57:03
问题 I'm trying to generate some JavaScript based on the type annotations I have provided in on some Python functions by using the signature() function in the inspect module. This part works as I expect when the type is a simple builtin class: import inspect def my_function() -> dict: pass signature = inspect.signature(my_function) signature.return_annotation is dict # True Though I'm not sure how to unwrap and inspect more complex annotations e.g: from typing import List import inspect def my

Python how to get the calling function (not just its name)?

拈花ヽ惹草 提交于 2019-11-29 14:26:20
I want to write a function which returns the calling function: def foo(): return get_calling_function() #should return 'foo' function object There's numerous examples online how to get the calling function's name , but not how to get the actual object. I've come up with the following solution which gets the name, then looks it up in the calling function's global namespace. However this doesn't work for class functions since there you need the class name as well, and I image there's a bunch of other edge cases as well. from inspect import stack def get_calling_function(): return stack()[2][0].f