Python - 如何获取特定范围内变量的字典(甚至列表);比 locals/globals() 更具体

Python - How to obtain a dictionary(or even a list) of variables in a specific scope; more specific than locals/globals()

所以,标题几乎说明了一切。

例如让我们看下面的示例代码:

## How can I obtain a dict/list (like locals()) of all the variables in second and/or third layer scopes via a command 
# coming from the first layer?
## Or another example would be how could I obtain the variables "locals() style" from the thirdlayer via a 
# command from the second layer?
# essentially can a parent function/class access a list/dict of a child function
# or class??

def firstLayer():
     a = 4.7
     q = locals()
     print(q)
     # local vars of 1st layer
     def secondlayer():
          b = 7
          r = locals()
          print(r)
          # local vars of 2nd layer
          def thirdlayer():
               c = False
               s = locals()
               i = globals()
               print('c:\n', c, "\nglobals from 3rd layer:\n\t", i)
              # local vars of 3rd layer
          thirdlayer()

     secondlayer()
firstLayer()

sample_var = globals()
print(sample_var)
# returns the list of global variables

重申我在代码的注释中所说的内容,本质上是他们可以通过任何方式获得 'child' 范围内所有局部变量的列表?我知道功能已关闭,但如果他们无法做到这一点,那么他们可以使用更复杂的代码来实现这一点,我可以将其集成到一个函数中,或者在必要时 class。

编辑: 进一步阐述;这是我现在的情况。

def varsfunc():
    font1 = "Harlow Solid"
    grey = '#454545'
    font2 = 'Mistral'
    font3 = 'Italic 35px Times New Roman'
    pnk = 'pink'
    grn = 'green'
    return locals()

本质上,我正在创建一个模块,用户必须创建某种类型的函数,他们列出他们想要声明的所有变量以用于修改 css 文件。本质上,我想让用户不必键入 "return locals()"。我想通过让 end-users 将上面的示例函数包装在一个装饰器中来实现它,该装饰器相当于返回我想要的确切范围的 locals() 。装饰器对我不起作用,因为它在外部范围内。

更清楚: 我需要一个 decorator/function 来包装另一个函数(即装饰器),它可以访问和创建一个 child 元素的列表。

def module_decorator_func_thing():
     r = command_that_acts_like_locals()_but_for_child_scopes
     def user_var_list():
          font1 = 'green'
          font2 = 'pink'
     # back in "module_decorator_func_thing"'s scope
     print(r) # this variable should contain only a dict/list containing the
     # the following:
     # r = {'font1': 'green', 'font2': 'pink')

目前用户需要这样做:

def vars_func_container():
     font1 = 'green'
     font2 = 'pink'
     return locals() #  <---- I want the user to not have to type this and for 
    # a function decorator to take care of it instead possibly.

@aguy 和其他希望了解更多信息的人的信息。 我通过你们的提示获得的 dictionary/list 将被发送到这个函数来完成程序的真正工作。 (如果我要开始使用列表,我需要转换成字典,但这对我来说没问题。) 变量字典与此函数一起使用 "compile/compyle"(双关语 'Python' + '编译)并插入 "variables" 参数中。例如你像这样执行函数。

compyle("My sample title", return_stylesheet_from_func(*insert .css filename), 
return_variables_from_function(*insert function containing variables*), "**True/False to turn on compilation**", 
"**True/False to turn on annotations/suggestions**")

def compyle(title, style_sheet, variables, boolean=False, boolean2=True):
    """
    :param title: The name you wish your .css file to be named.
    :param style_sheet: The name of the multi-line string that will compose your .css file
    :param variables: The name of the dictionary containing your .pcss variables
    :param boolean: A.K.A the "Compiler Parameter" - Turns the compiler on or off
    :param boolean2: A.K.A the "Annotation Parameter" - Turns annotations on or off
    :return: returns compiled .pcss text as normal .css style text to be utilized with .html
    """
    # -----------------------------------
    file_name = title + ".css"
    replace_num = len(variables.keys())
    counter = replace_num
    content = style_sheet
    # -----------------------------------
    # add theme support with namedtuple's formatted to mimic structs in C/C++
    # this will be a major feature update as well as a nice way to allow the future prospect of integrating C/C++ into
    # the compiler. Info: 
    for k, v in variables.items():
        counter -= 1
        content = content.replace(k, v, replace_num)
        if counter == 0:
            break
        else:
            pass
    looped_content = str(content)
    id_content = looped_content.replace("hash_", "#")
    output = id_content.replace("dot_", ".")

    if boolean is True:
        if boolean2 is True:
            output = " /* --- Pyle Sheet --- */\n" + output
            with open(file_name, 'w') as writ:
                writ.write(output)
                writ.close()
                print('compiled successfully; The file was saved as ' + "\"" + file_name + "\".")
        elif boolean2 is False:
            pass
        else:
            logging.warning("An Error Occurred - see module, documentation, or online Q&A for assistance.")
    elif boolean is False:
        if boolean2 is True:
            print('compiled successfully; The file ' + "\"" + file_name + "\"" + "was not saved/created.")
        elif boolean2 is False:
            pass
        else:
            logging.warning("An Error Occurred - see module, documentation, or online Q&A for assistance.")
    else:
        logging.warning('An Error Occurred with the Compile Parameter (See: boolean in pyle_sheets source file) - \ '
                        'see module, documentation, or online Q&A for assistance.')

如果不深入研究,我看不出有什么方法可以做到这一点;以下是我想出的最简单的解决方案。

工作原理

使用 ast 模块,我们遍历给定函数的代码并找到所有赋值。这些在给定的命名空间中进行评估,并返回此命名空间。

密码

import ast
import functools
import inspect

def returnAssignments(f):
    @functools.wraps(f)
    def returner():
        assignments = dict()
        for node in ast.walk(ast.parse(inspect.getsource(f))):
            if isinstance(node, ast.Assign):
                exec(compile(ast.Module([node]), '<ast>', 'exec'),
                     globals(),
                     assignments)
        return assignments
    return returner

用法

from ra import returnAssignments

@returnAssignments
def foo():
    this = 'something'
    that = 37
    the_other = object()

print(foo())

输出

rat@pandion:~/tmp$ python test.py
{'this': 'something', 'that': 37, 'the_other': <object object at 0x10205b130>}

我想知道我在这里提供的这种粗略的解决方案是否对您有用。请注意,我还没有在所有情况下对其进行测试,因此它可能有点粗糙。此外,它 returns 一切都作为字符串,您可能希望进一步更改这种行为。

定义函数:

def get_local_vars_from_function(f):
    import inspect
    s = inspect.getsourcelines(f)[0]
    d = {}
    for l in s:
        if '=' in l:
            var, val = l.split('=')
            var = var.strip()
            val = val.strip()
            d[var] = val
    return d

然后使用它:

In[91]: get_local_vars_from_function(user_var_list)
Out[91]: {'font1': "'green'", 'font2': "'pink'"}