introspection

Listing defined functions in Bash

核能气质少年 提交于 2020-05-09 18:56:47
问题 I'm trying to write some code in bash which uses introspection to select the appropriate function to call. Determining the candidates requires knowing which functions are defined. It's easy to list defined variables in bash using only parameter expansion: $ prefix_foo="one" $ prefix_bar="two" $ echo "${!prefix_*}" prefix_bar prefix_foo However, doing this for functions appears to require filtering the output of set -- a much more haphazard approach. Is there a Right Way? 回答1: How about

Listing defined functions in Bash

大城市里の小女人 提交于 2020-05-09 18:56:21
问题 I'm trying to write some code in bash which uses introspection to select the appropriate function to call. Determining the candidates requires knowing which functions are defined. It's easy to list defined variables in bash using only parameter expansion: $ prefix_foo="one" $ prefix_bar="two" $ echo "${!prefix_*}" prefix_bar prefix_foo However, doing this for functions appears to require filtering the output of set -- a much more haphazard approach. Is there a Right Way? 回答1: How about

How to get function object inside a function (Python)

 ̄綄美尐妖づ 提交于 2020-02-23 07:12:25
问题 I want to have something like def x(): print get_def_name() but not necessarily know the name of x . Ideally it would return 'x' where x would be the name of the function. 回答1: Functions in Python are objects, and as it happens those objects do have an attribute containing the name they were defined with: >>> def x(): ... pass ... >>> print x.__name__ x So, a naïve approach might be this: >>> def x(): ... print x.__name__ ... >>> x() x That seems to work. However, since you had to know the

What is the equivalent of /proc/cpuinfo on FreeBSD v8.1?

扶醉桌前 提交于 2020-01-31 06:21:16
问题 What is the equivalent of Linux's /proc/cpuinfo on FreeBSD v8.1? My application reads /proc/cpuinfo and saves the information in the log file, what could I do to get similar information logged on FreeBSD? A sample /proc/cpuinfo looks like this: processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 23 model name : Intel(R) Xeon(R) CPU E5420 @ 2.50GHz stepping : 8 cpu MHz : 2499.015 cache size : 6144 KB fdiv_bug : no hlt_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes

Any way to determine which object called a method?

故事扮演 提交于 2020-01-29 02:42:11
问题 I'm hoping that Ruby's message-passing infrastructure means there might be some clever trick for this. How do I determine the calling object -- which object called the method I'm currently in? 回答1: As an option, there is a binding_of_caller gem that allows you to execute code in context of any caller on the call stack (caller, caller's caller and so on). It's useful for inspecting (read do anything at any position on the call stack ) call stack in development, as used in better_errors.

Aliased name of a Function in Python

让人想犯罪 __ 提交于 2020-01-25 03:26:04
问题 I want to find the name of the function as it was called ... i.e. the name of the variable that called the function. Using the basic recipes i.e. with __name__ , func_name , or inspecting the basic stack does not work for me. For example def somefunc(): print "My name is: %s" % inspect.stack()[1][3] a = somefunc a() # would output: out: "My name is: somefunc" # whereas I want it to output: "My name is: a" My gut says I can do this, but I can't figure it out. Any python guru's out there? 回答1:

Automatically populate all properties of an Objective C instance with a dictionary

北战南征 提交于 2020-01-23 12:50:48
问题 I have a class with properties that I would like to set values from a dictionary. In other words, I would like to automate this: objectInstace.val1 = [dict objectForKey:@"val1"]; objectInstace.val2 = [dict objectForKey:@"val2"]; with something like this (pseudo code): for key, value in dict: setattr(objectInstance, key, value) 回答1: You can use the Key-Value Coding method setValuesForKeysWithDictionary:. It does precisely what you want. Just [someObject setValuesForKeysWithDictionary

How to introspect win32com wrapper?

安稳与你 提交于 2020-01-23 10:57:52
问题 I have a device, which records spectroscopic data and is controlled by a 3rd-party application. For automization purposes, I want to use the COM interface of the application to retrieve the data in Python. Since there is no proper documentation for using the API from Python, I collected the following code from different web sources, which successfully obtains the first frame: comtypes.client.GetModule(('{1A762221-D8BA-11CF-AFC2-508201C10000}', 3, 11)) import comtypes.gen.WINX32Lib as

How to introspect regexes in the Perl API

老子叫甜甜 提交于 2020-01-22 17:48:11
问题 I'm working on some code that needs to serialize Perl regexes, including any regex flags. Only a subset of flags are supported, so I need to detect when unsupported flags like /u are in the regex object. The current version of the code does this: static void serialize_regex_flags(buffer *buf, SV *sv) { char flags[] = {0,0,0,0,0,0}; unsigned int i = 0, f = 0; STRLEN string_length; char *string = SvPV(sv, string_length); Then manually processes string char-by-char to find flags. The problem

How to introspect regexes in the Perl API

落花浮王杯 提交于 2020-01-22 17:47:08
问题 I'm working on some code that needs to serialize Perl regexes, including any regex flags. Only a subset of flags are supported, so I need to detect when unsupported flags like /u are in the regex object. The current version of the code does this: static void serialize_regex_flags(buffer *buf, SV *sv) { char flags[] = {0,0,0,0,0,0}; unsigned int i = 0, f = 0; STRLEN string_length; char *string = SvPV(sv, string_length); Then manually processes string char-by-char to find flags. The problem